blob: 78e3f201cbe4227e7b2f97b9d3dbd4d59596780f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void op_colon(oparg_T *oap);
36static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010037#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void find_start_of_word(pos_T *);
39static void find_end_of_word(pos_T *);
40static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void clearop(oparg_T *oap);
44static void clearopbeep(oparg_T *oap);
45static void unshift_special(cmdarg_T *cap);
46static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
51/*
52 * nv_*(): functions called to handle Normal and Visual mode commands.
53 * n_*(): functions called to handle Normal mode commands.
54 * v_*(): functions called to handle Visual mode commands.
55 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void nv_ignore(cmdarg_T *cap);
57static void nv_nop(cmdarg_T *cap);
58static void nv_error(cmdarg_T *cap);
59static void nv_help(cmdarg_T *cap);
60static void nv_addsub(cmdarg_T *cap);
61static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_mousescroll(cmdarg_T *cap);
64static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010066static void nv_scroll_line(cmdarg_T *cap);
67static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_ver_scrollbar(cmdarg_T *cap);
70static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000072#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010073static void nv_tabline(cmdarg_T *cap);
74static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000075#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_exmode(cmdarg_T *cap);
77static void nv_colon(cmdarg_T *cap);
78static void nv_ctrlg(cmdarg_T *cap);
79static void nv_ctrlh(cmdarg_T *cap);
80static void nv_clear(cmdarg_T *cap);
81static void nv_ctrlo(cmdarg_T *cap);
82static void nv_hat(cmdarg_T *cap);
83static void nv_Zet(cmdarg_T *cap);
84static void nv_ident(cmdarg_T *cap);
85static void nv_tagpop(cmdarg_T *cap);
86static void nv_scroll(cmdarg_T *cap);
87static void nv_right(cmdarg_T *cap);
88static void nv_left(cmdarg_T *cap);
89static void nv_up(cmdarg_T *cap);
90static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_end(cmdarg_T *cap);
92static void nv_dollar(cmdarg_T *cap);
93static void nv_search(cmdarg_T *cap);
94static void nv_next(cmdarg_T *cap);
95static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
96static void nv_csearch(cmdarg_T *cap);
97static void nv_brackets(cmdarg_T *cap);
98static void nv_percent(cmdarg_T *cap);
99static void nv_brace(cmdarg_T *cap);
100static void nv_mark(cmdarg_T *cap);
101static void nv_findpar(cmdarg_T *cap);
102static void nv_undo(cmdarg_T *cap);
103static void nv_kundo(cmdarg_T *cap);
104static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
107static void v_visop(cmdarg_T *cap);
108static void nv_subst(cmdarg_T *cap);
109static void nv_abbrev(cmdarg_T *cap);
110static void nv_optrans(cmdarg_T *cap);
111static void nv_gomark(cmdarg_T *cap);
112static void nv_pcmark(cmdarg_T *cap);
113static void nv_regname(cmdarg_T *cap);
114static void nv_visual(cmdarg_T *cap);
115static void n_start_visual_mode(int c);
116static void nv_window(cmdarg_T *cap);
117static void nv_suspend(cmdarg_T *cap);
118static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_dot(cmdarg_T *cap);
120static void nv_redo(cmdarg_T *cap);
121static void nv_Undo(cmdarg_T *cap);
122static void nv_tilde(cmdarg_T *cap);
123static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000124#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000126#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_lineop(cmdarg_T *cap);
128static void nv_home(cmdarg_T *cap);
129static void nv_pipe(cmdarg_T *cap);
130static void nv_bck_word(cmdarg_T *cap);
131static void nv_wordcmd(cmdarg_T *cap);
132static void nv_beginline(cmdarg_T *cap);
133static void adjust_cursor(oparg_T *oap);
134static void adjust_for_sel(cmdarg_T *cap);
135static int unadjust_for_sel(void);
136static void nv_select(cmdarg_T *cap);
137static void nv_goto(cmdarg_T *cap);
138static void nv_normal(cmdarg_T *cap);
139static void nv_esc(cmdarg_T *oap);
140static void nv_edit(cmdarg_T *cap);
141static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100143static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100145static void nv_record(cmdarg_T *cap);
146static void nv_at(cmdarg_T *cap);
147static void nv_halfpage(cmdarg_T *cap);
148static void nv_join(cmdarg_T *cap);
149static void nv_put(cmdarg_T *cap);
150static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100155static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100157static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000160static char *e_noident = N_("E349: No identifier under cursor");
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Function to be called for a Normal or Visual mode command.
164 * The argument is a cmdarg_T.
165 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100166typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168/* Values for cmd_flags. */
169#define NV_NCH 0x01 /* may need to get a second char */
170#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
171#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
172#define NV_LANG 0x08 /* second char needs language adjustment */
173
174#define NV_SS 0x10 /* may start selection */
175#define NV_SSS 0x20 /* may start selection with shift modifier */
176#define NV_STS 0x40 /* may stop selection without shift modif. */
177#define NV_RL 0x80 /* 'rightleft' modifies command */
178#define NV_KEEPREG 0x100 /* don't clear regname */
179#define NV_NCW 0x200 /* not allowed in command-line window */
180
181/*
182 * Generally speaking, every Normal mode command should either clear any
183 * pending operator (with *clearop*()), or set the motion type variable
184 * oap->motion_type.
185 *
186 * When a cursor motion command is made, it is marked as being a character or
187 * line oriented motion. Then, if an operator is in effect, the operation
188 * becomes character or line oriented accordingly.
189 */
190
191/*
192 * This table contains one entry for every Normal or Visual mode command.
193 * The order doesn't matter, init_normal_cmds() will create a sorted index.
194 * It is faster when all keys from zero to '~' are present.
195 */
196static const struct nv_cmd
197{
198 int cmd_char; /* (first) command character */
199 nv_func_T cmd_func; /* function for this command */
200 short_u cmd_flags; /* NV_ flags */
201 short cmd_arg; /* value for ca.arg */
202} nv_cmds[] =
203{
204 {NUL, nv_error, 0, 0},
205 {Ctrl_A, nv_addsub, 0, 0},
206 {Ctrl_B, nv_page, NV_STS, BACKWARD},
207 {Ctrl_C, nv_esc, 0, TRUE},
208 {Ctrl_D, nv_halfpage, 0, 0},
209 {Ctrl_E, nv_scroll_line, 0, TRUE},
210 {Ctrl_F, nv_page, NV_STS, FORWARD},
211 {Ctrl_G, nv_ctrlg, 0, 0},
212 {Ctrl_H, nv_ctrlh, 0, 0},
213 {Ctrl_I, nv_pcmark, 0, 0},
214 {NL, nv_down, 0, FALSE},
215 {Ctrl_K, nv_error, 0, 0},
216 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000217 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {Ctrl_N, nv_down, NV_STS, FALSE},
219 {Ctrl_O, nv_ctrlo, 0, 0},
220 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000221 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {Ctrl_R, nv_redo, 0, 0},
223 {Ctrl_S, nv_ignore, 0, 0},
224 {Ctrl_T, nv_tagpop, NV_NCW, 0},
225 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {Ctrl_V, nv_visual, 0, FALSE},
227 {'V', nv_visual, 0, FALSE},
228 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {Ctrl_W, nv_window, 0, 0},
230 {Ctrl_X, nv_addsub, 0, 0},
231 {Ctrl_Y, nv_scroll_line, 0, FALSE},
232 {Ctrl_Z, nv_suspend, 0, 0},
233 {ESC, nv_esc, 0, FALSE},
234 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
235 {Ctrl_RSB, nv_ident, NV_NCW, 0},
236 {Ctrl_HAT, nv_hat, NV_NCW, 0},
237 {Ctrl__, nv_error, 0, 0},
238 {' ', nv_right, 0, 0},
239 {'!', nv_operator, 0, 0},
240 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
241 {'#', nv_ident, 0, 0},
242 {'$', nv_dollar, 0, 0},
243 {'%', nv_percent, 0, 0},
244 {'&', nv_optrans, 0, 0},
245 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
246 {'(', nv_brace, 0, BACKWARD},
247 {')', nv_brace, 0, FORWARD},
248 {'*', nv_ident, 0, 0},
249 {'+', nv_down, 0, TRUE},
250 {',', nv_csearch, 0, TRUE},
251 {'-', nv_up, 0, TRUE},
252 {'.', nv_dot, NV_KEEPREG, 0},
253 {'/', nv_search, 0, FALSE},
254 {'0', nv_beginline, 0, 0},
255 {'1', nv_ignore, 0, 0},
256 {'2', nv_ignore, 0, 0},
257 {'3', nv_ignore, 0, 0},
258 {'4', nv_ignore, 0, 0},
259 {'5', nv_ignore, 0, 0},
260 {'6', nv_ignore, 0, 0},
261 {'7', nv_ignore, 0, 0},
262 {'8', nv_ignore, 0, 0},
263 {'9', nv_ignore, 0, 0},
264 {':', nv_colon, 0, 0},
265 {';', nv_csearch, 0, FALSE},
266 {'<', nv_operator, NV_RL, 0},
267 {'=', nv_operator, 0, 0},
268 {'>', nv_operator, NV_RL, 0},
269 {'?', nv_search, 0, FALSE},
270 {'@', nv_at, NV_NCH_NOP, FALSE},
271 {'A', nv_edit, 0, 0},
272 {'B', nv_bck_word, 0, 1},
273 {'C', nv_abbrev, NV_KEEPREG, 0},
274 {'D', nv_abbrev, NV_KEEPREG, 0},
275 {'E', nv_wordcmd, 0, TRUE},
276 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
277 {'G', nv_goto, 0, TRUE},
278 {'H', nv_scroll, 0, 0},
279 {'I', nv_edit, 0, 0},
280 {'J', nv_join, 0, 0},
281 {'K', nv_ident, 0, 0},
282 {'L', nv_scroll, 0, 0},
283 {'M', nv_scroll, 0, 0},
284 {'N', nv_next, 0, SEARCH_REV},
285 {'O', nv_open, 0, 0},
286 {'P', nv_put, 0, 0},
287 {'Q', nv_exmode, NV_NCW, 0},
288 {'R', nv_Replace, 0, FALSE},
289 {'S', nv_subst, NV_KEEPREG, 0},
290 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
291 {'U', nv_Undo, 0, 0},
292 {'W', nv_wordcmd, 0, TRUE},
293 {'X', nv_abbrev, NV_KEEPREG, 0},
294 {'Y', nv_abbrev, NV_KEEPREG, 0},
295 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
296 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
297 {'\\', nv_error, 0, 0},
298 {']', nv_brackets, NV_NCH_ALW, FORWARD},
299 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
300 {'_', nv_lineop, 0, 0},
301 {'`', nv_gomark, NV_NCH_ALW, FALSE},
302 {'a', nv_edit, NV_NCH, 0},
303 {'b', nv_bck_word, 0, 0},
304 {'c', nv_operator, 0, 0},
305 {'d', nv_operator, 0, 0},
306 {'e', nv_wordcmd, 0, FALSE},
307 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
308 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
309 {'h', nv_left, NV_RL, 0},
310 {'i', nv_edit, NV_NCH, 0},
311 {'j', nv_down, 0, FALSE},
312 {'k', nv_up, 0, FALSE},
313 {'l', nv_right, NV_RL, 0},
314 {'m', nv_mark, NV_NCH_NOP, 0},
315 {'n', nv_next, 0, 0},
316 {'o', nv_open, 0, 0},
317 {'p', nv_put, 0, 0},
318 {'q', nv_record, NV_NCH, 0},
319 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
320 {'s', nv_subst, NV_KEEPREG, 0},
321 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
322 {'u', nv_undo, 0, 0},
323 {'w', nv_wordcmd, 0, FALSE},
324 {'x', nv_abbrev, NV_KEEPREG, 0},
325 {'y', nv_operator, 0, 0},
326 {'z', nv_zet, NV_NCH_ALW, 0},
327 {'{', nv_findpar, 0, BACKWARD},
328 {'|', nv_pipe, 0, 0},
329 {'}', nv_findpar, 0, FORWARD},
330 {'~', nv_tilde, 0, 0},
331
332 /* pound sign */
333 {POUND, nv_ident, 0, 0},
334#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200335 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
336 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
337 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
338 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {K_LEFTMOUSE, nv_mouse, 0, 0},
340 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
341 {K_LEFTDRAG, nv_mouse, 0, 0},
342 {K_LEFTRELEASE, nv_mouse, 0, 0},
343 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100344 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
346 {K_MIDDLEDRAG, nv_mouse, 0, 0},
347 {K_MIDDLERELEASE, nv_mouse, 0, 0},
348 {K_RIGHTMOUSE, nv_mouse, 0, 0},
349 {K_RIGHTDRAG, nv_mouse, 0, 0},
350 {K_RIGHTRELEASE, nv_mouse, 0, 0},
351 {K_X1MOUSE, nv_mouse, 0, 0},
352 {K_X1DRAG, nv_mouse, 0, 0},
353 {K_X1RELEASE, nv_mouse, 0, 0},
354 {K_X2MOUSE, nv_mouse, 0, 0},
355 {K_X2DRAG, nv_mouse, 0, 0},
356 {K_X2RELEASE, nv_mouse, 0, 0},
357#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000358 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000359 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_INS, nv_edit, 0, 0},
361 {K_KINS, nv_edit, 0, 0},
362 {K_BS, nv_ctrlh, 0, 0},
363 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
364 {K_S_UP, nv_page, NV_SS, BACKWARD},
365 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
366 {K_S_DOWN, nv_page, NV_SS, FORWARD},
367 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
368 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
369 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
370 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
371 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
372 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
373 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
374 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
375 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
376 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
377 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
378 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_S_END, nv_end, NV_SS, FALSE},
380 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
381 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
382 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_S_HOME, nv_home, NV_SS, 0},
384 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
385 {K_DEL, nv_abbrev, 0, 0},
386 {K_KDEL, nv_abbrev, 0, 0},
387 {K_UNDO, nv_kundo, 0, 0},
388 {K_HELP, nv_help, NV_NCW, 0},
389 {K_F1, nv_help, NV_NCW, 0},
390 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_GUI
393 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
394 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
395#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000396#ifdef FEAT_GUI_TABLINE
397 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000398 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200401 {K_F8, farsi_f8, 0, 0},
402 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#ifdef FEAT_NETBEANS_INTG
405 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
406#endif
407#ifdef FEAT_DND
408 {K_DROP, nv_drop, NV_STS, 0},
409#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000410 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100411 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412};
413
414/* Number of commands in nv_cmds[]. */
415#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
416
417/* Sorted index of commands in nv_cmds[]. */
418static short nv_cmd_idx[NV_CMDS_SIZE];
419
420/* The highest index for which
421 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
422static int nv_max_linear;
423
424/*
425 * Compare functions for qsort() below, that checks the command character
426 * through the index in nv_cmd_idx[].
427 */
428 static int
429#ifdef __BORLANDC__
430_RTLENTRYF
431#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100432nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433{
434 int c1, c2;
435
436 /* The commands are sorted on absolute value. */
437 c1 = nv_cmds[*(const short *)s1].cmd_char;
438 c2 = nv_cmds[*(const short *)s2].cmd_char;
439 if (c1 < 0)
440 c1 = -c1;
441 if (c2 < 0)
442 c2 = -c2;
443 return c1 - c2;
444}
445
446/*
447 * Initialize the nv_cmd_idx[] table.
448 */
449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int i;
453
454 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000455 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 nv_cmd_idx[i] = i;
457
458 /* Sort the commands by the command character. */
459 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
460
461 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000462 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
464 break;
465 nv_max_linear = i - 1;
466}
467
468/*
469 * Search for a command in the commands table.
470 * Returns -1 for invalid command.
471 */
472 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100473find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 int i;
476 int idx;
477 int top, bot;
478 int c;
479
480#ifdef FEAT_MBYTE
481 /* A multi-byte character is never a command. */
482 if (cmdchar >= 0x100)
483 return -1;
484#endif
485
486 /* We use the absolute value of the character. Special keys have a
487 * negative value, but are sorted on their absolute value. */
488 if (cmdchar < 0)
489 cmdchar = -cmdchar;
490
491 /* If the character is in the first part: The character is the index into
492 * nv_cmd_idx[]. */
493 if (cmdchar <= nv_max_linear)
494 return nv_cmd_idx[cmdchar];
495
496 /* Perform a binary search. */
497 bot = nv_max_linear + 1;
498 top = NV_CMDS_SIZE - 1;
499 idx = -1;
500 while (bot <= top)
501 {
502 i = (top + bot) / 2;
503 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
504 if (c < 0)
505 c = -c;
506 if (cmdchar == c)
507 {
508 idx = nv_cmd_idx[i];
509 break;
510 }
511 if (cmdchar > c)
512 bot = i + 1;
513 else
514 top = i - 1;
515 }
516 return idx;
517}
518
519/*
520 * Execute a command in Normal mode.
521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523normal_cmd(
524 oparg_T *oap,
525 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 cmdarg_T ca; /* command arguments */
528 int c;
529 int ctrl_w = FALSE; /* got CTRL-W command */
530 int old_col = curwin->w_curswant;
531#ifdef FEAT_CMDL_INFO
532 int need_flushbuf; /* need to call out_flush() */
533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 pos_T old_pos; /* cursor position before command */
535 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 static int old_mapped_len = 0;
537 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000538#ifdef FEAT_EVAL
539 int set_prevcount = FALSE;
540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
543 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544
545 /* Use a count remembered from before entering an operator. After typing
546 * "3d" we return from normal_cmd() and come back here, the "3" is
547 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 ca.opcount = opcount;
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 /*
551 * If there is an operator pending, then the command we take this time
552 * will terminate it. Finish_op tells us to finish the operation before
553 * returning this time (unless the operation was cancelled).
554 */
555#ifdef CURSOR_SHAPE
556 c = finish_op;
557#endif
558 finish_op = (oap->op_type != OP_NOP);
559#ifdef CURSOR_SHAPE
560 if (finish_op != c)
561 {
562 ui_cursor_shape(); /* may show different cursor shape */
563# ifdef FEAT_MOUSESHAPE
564 update_mouseshape(-1);
565# endif
566 }
567#endif
568
Bram Moolenaara983fe92008-07-31 20:04:27 +0000569 /* When not finishing an operator and no register name typed, reset the
570 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000574#ifdef FEAT_EVAL
575 set_prevcount = TRUE;
576#endif
577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
Bram Moolenaara983fe92008-07-31 20:04:27 +0000579 /* Restore counts from before receiving K_CURSORHOLD. This means after
580 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
581 * "3 * 2". */
582 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
583 {
584 ca.opcount = oap->prev_opcount;
585 ca.count0 = oap->prev_count0;
586 oap->prev_opcount = 0;
587 oap->prev_count0 = 0;
588 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 State = NORMAL_BUSY;
593#ifdef USE_ON_FLY_SCROLL
594 dont_scroll = FALSE; /* allow scrolling here */
595#endif
596
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100597#ifdef FEAT_EVAL
598 /* Set v:count here, when called from main() and not a stuffed
599 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100600 * when there is no count. Do set it for redo. */
601 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100602 set_vcount_ca(&ca, &set_prevcount);
603#endif
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 /*
606 * Get the command character from the user.
607 */
608 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100609 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * If a mapping was started in Visual or Select mode, remember the length
613 * of the mapping. This is used below to not return to Insert mode for as
614 * long as the mapping is being executed.
615 */
616 if (restart_edit == 0)
617 old_mapped_len = 0;
618 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000619 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 old_mapped_len = typebuf_maplen();
621
622 if (c == NUL)
623 c = K_ZERO;
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /*
626 * In Select mode, typed text replaces the selection.
627 */
628 if (VIsual_active
629 && VIsual_select
630 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
631 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000632 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
633 * 'insertmode' is set) fake a "d"elete command, Insert mode will
634 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000635 * Insert the typed character in the typeahead buffer, so that it can
636 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000637 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000638 if (restart_edit != 0)
639 c = 'd';
640 else
641 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000642 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200643 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646#ifdef FEAT_CMDL_INFO
647 need_flushbuf = add_to_showcmd(c);
648#endif
649
650getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 /*
654 * Handle a count before a command and compute ca.count0.
655 * Note that '0' is a command and not the start of a count, but it's
656 * part of a count after other digits.
657 */
658 while ( (c >= '1' && c <= '9')
659 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
660 {
661 if (c == K_DEL || c == K_KDEL)
662 {
663 ca.count0 /= 10;
664#ifdef FEAT_CMDL_INFO
665 del_from_showcmd(4); /* delete the digit and ~@% */
666#endif
667 }
668 else
669 ca.count0 = ca.count0 * 10 + (c - '0');
670 if (ca.count0 < 0) /* got too large! */
671 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000672#ifdef FEAT_EVAL
673 /* Set v:count here, when called from main() and not a stuffed
674 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100675 * right after the count. Do set it for redo. */
676 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100677 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (ctrl_w)
680 {
681 ++no_mapping;
682 ++allow_keys; /* no mapping for nchar, but keys */
683 }
684 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000685 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_zero_mapping;
688 if (ctrl_w)
689 {
690 --no_mapping;
691 --allow_keys;
692 }
693#ifdef FEAT_CMDL_INFO
694 need_flushbuf |= add_to_showcmd(c);
695#endif
696 }
697
698 /*
699 * If we got CTRL-W there may be a/another count
700 */
701 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
702 {
703 ctrl_w = TRUE;
704 ca.opcount = ca.count0; /* remember first count */
705 ca.count0 = 0;
706 ++no_mapping;
707 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000708 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 --no_mapping;
711 --allow_keys;
712#ifdef FEAT_CMDL_INFO
713 need_flushbuf |= add_to_showcmd(c);
714#endif
715 goto getcount; /* jump back */
716 }
717 }
718
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 /* Save the count values so that ca.opcount and ca.count0 are exactly
722 * the same when coming back here after handling K_CURSORHOLD. */
723 oap->prev_opcount = ca.opcount;
724 oap->prev_count0 = ca.count0;
725 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100726 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000727 {
728 /*
729 * If we're in the middle of an operator (including after entering a
730 * yank buffer with '"') AND we had a count before the operator, then
731 * that count overrides the current value of ca.count0.
732 * What this means effectively, is that commands like "3dw" get turned
733 * into "d3w" which makes things fall into place pretty neatly.
734 * If you give a count before AND after the operator, they are
735 * multiplied.
736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (ca.count0)
738 ca.count0 *= ca.opcount;
739 else
740 ca.count0 = ca.opcount;
741 }
742
743 /*
744 * Always remember the count. It will be set to zero (on the next call,
745 * above) when there is no pending operator.
746 * When called from main(), save the count for use by the "count" built-in
747 * variable.
748 */
749 ca.opcount = ca.count0;
750 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
751
752#ifdef FEAT_EVAL
753 /*
754 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100755 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100757 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000758 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#endif
760
761 /*
762 * Find the command character in the table of commands.
763 * For CTRL-W we already got nchar when looking for a count.
764 */
765 if (ctrl_w)
766 {
767 ca.nchar = c;
768 ca.cmdchar = Ctrl_W;
769 }
770 else
771 ca.cmdchar = c;
772 idx = find_command(ca.cmdchar);
773 if (idx < 0)
774 {
775 /* Not a known command: beep. */
776 clearopbeep(oap);
777 goto normal_end;
778 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000779
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000780 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200782 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000784 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 goto normal_end;
786 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000787 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
788 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /*
791 * In Visual/Select mode, a few keys are handled in a special way.
792 */
793 if (VIsual_active)
794 {
795 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
796 if (km_stopsel
797 && (nv_cmds[idx].cmd_flags & NV_STS)
798 && !(mod_mask & MOD_MASK_SHIFT))
799 {
800 end_visual_mode();
801 redraw_curbuf_later(INVERTED);
802 }
803
804 /* Keys that work different when 'keymodel' contains "startsel" */
805 if (km_startsel)
806 {
807 if (nv_cmds[idx].cmd_flags & NV_SS)
808 {
809 unshift_special(&ca);
810 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000811 if (idx < 0)
812 {
813 /* Just in case */
814 clearopbeep(oap);
815 goto normal_end;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
818 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
819 && (mod_mask & MOD_MASK_SHIFT))
820 {
821 mod_mask &= ~MOD_MASK_SHIFT;
822 }
823 }
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826#ifdef FEAT_RIGHTLEFT
827 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
828 && (nv_cmds[idx].cmd_flags & NV_RL))
829 {
830 /* Invert horizontal movements and operations. Only when typed by the
831 * user directly, not when the result of a mapping or "x" translated
832 * to "dl". */
833 switch (ca.cmdchar)
834 {
835 case 'l': ca.cmdchar = 'h'; break;
836 case K_RIGHT: ca.cmdchar = K_LEFT; break;
837 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
838 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
839 case 'h': ca.cmdchar = 'l'; break;
840 case K_LEFT: ca.cmdchar = K_RIGHT; break;
841 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
842 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
843 case '>': ca.cmdchar = '<'; break;
844 case '<': ca.cmdchar = '>'; break;
845 }
846 idx = find_command(ca.cmdchar);
847 }
848#endif
849
850 /*
851 * Get an additional character if we need one.
852 */
853 if ((nv_cmds[idx].cmd_flags & NV_NCH)
854 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
855 && oap->op_type == OP_NOP)
856 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
857 || (ca.cmdchar == 'q'
858 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200859 && reg_recording == 0
860 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100862 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
864 int *cp;
865 int repl = FALSE; /* get character for replace mode */
866 int lit = FALSE; /* get extra character literally */
867 int langmap_active = FALSE; /* using :lmap mappings */
868 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100869#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 int save_smd; /* saved value of p_smd */
871#endif
872
873 ++no_mapping;
874 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000875 /* Don't generate a CursorHold event here, most commands can't handle
876 * it, e.g., nv_replace(), nv_csearch(). */
877 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (ca.cmdchar == 'g')
879 {
880 /*
881 * For 'g' get the next character now, so that we can check for
882 * "gr", "g'" and "g`".
883 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000884 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_CMDL_INFO
887 need_flushbuf |= add_to_showcmd(ca.nchar);
888#endif
889 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100890 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
892 cp = &ca.extra_char; /* need to get a third character */
893 if (ca.nchar != 'r')
894 lit = TRUE; /* get it literally */
895 else
896 repl = TRUE; /* get it in replace mode */
897 }
898 else
899 cp = NULL; /* no third character needed */
900 }
901 else
902 {
903 if (ca.cmdchar == 'r') /* get it in replace mode */
904 repl = TRUE;
905 cp = &ca.nchar;
906 }
907 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
908
909 /*
910 * Get a second or third character.
911 */
912 if (cp != NULL)
913 {
914#ifdef CURSOR_SHAPE
915 if (repl)
916 {
917 State = REPLACE; /* pretend Replace mode */
918 ui_cursor_shape(); /* show different cursor shape */
919 }
920#endif
921 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
922 {
923 /* Allow mappings defined with ":lmap". */
924 --no_mapping;
925 --allow_keys;
926 if (repl)
927 State = LREPLACE;
928 else
929 State = LANGMAP;
930 langmap_active = TRUE;
931 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100932#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 save_smd = p_smd;
934 p_smd = FALSE; /* Don't let the IM code show the mode here */
935 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
936 im_set_active(TRUE);
937#endif
938
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000939 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 if (langmap_active)
942 {
943 /* Undo the decrement done above */
944 ++no_mapping;
945 ++allow_keys;
946 State = NORMAL_BUSY;
947 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100948#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (lang)
950 {
951 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
952 im_save_status(&curbuf->b_p_iminsert);
953 im_set_active(FALSE);
954 }
955 p_smd = save_smd;
956#endif
957#ifdef CURSOR_SHAPE
958 State = NORMAL_BUSY;
959#endif
960#ifdef FEAT_CMDL_INFO
961 need_flushbuf |= add_to_showcmd(*cp);
962#endif
963
964 if (!lit)
965 {
966#ifdef FEAT_DIGRAPHS
967 /* Typing CTRL-K gets a digraph. */
968 if (*cp == Ctrl_K
969 && ((nv_cmds[idx].cmd_flags & NV_LANG)
970 || cp == &ca.extra_char)
971 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
972 {
973 c = get_digraph(FALSE);
974 if (c > 0)
975 {
976 *cp = c;
977# ifdef FEAT_CMDL_INFO
978 /* Guessing how to update showcmd here... */
979 del_from_showcmd(3);
980 need_flushbuf |= add_to_showcmd(*cp);
981# endif
982 }
983 }
984#endif
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /* adjust chars > 127, except after "tTfFr" commands */
987 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef FEAT_RIGHTLEFT
989 /* adjust Hebrew mapped char */
990 if (p_hkmap && lang && KeyTyped)
991 *cp = hkmap(*cp);
992# ifdef FEAT_FKMAP
993 /* adjust Farsi mapped char */
994 if (p_fkmap && lang && KeyTyped)
995 *cp = fkmap(*cp);
996# endif
997#endif
998 }
999
1000 /*
1001 * When the next character is CTRL-\ a following CTRL-N means the
1002 * command is aborted and we go to Normal mode.
1003 */
1004 if (cp == &ca.extra_char
1005 && ca.nchar == Ctrl_BSL
1006 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1007 {
1008 ca.cmdchar = Ctrl_BSL;
1009 ca.nchar = ca.extra_char;
1010 idx = find_command(ca.cmdchar);
1011 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001012 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001013 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else if (*cp == Ctrl_BSL)
1015 {
1016 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1017
1018 /* There is a busy wait here when typing "f<C-\>" and then
1019 * something different from CTRL-N. Can't be avoided. */
1020 while ((c = vpeekc()) <= 0 && towait > 0L)
1021 {
1022 do_sleep(towait > 50L ? 50L : towait);
1023 towait -= 50L;
1024 }
1025 if (c > 0)
1026 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001027 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (c != Ctrl_N && c != Ctrl_G)
1029 vungetc(c);
1030 else
1031 {
1032 ca.cmdchar = Ctrl_BSL;
1033 ca.nchar = c;
1034 idx = find_command(ca.cmdchar);
1035 }
1036 }
1037 }
1038
1039#ifdef FEAT_MBYTE
1040 /* When getting a text character and the next character is a
1041 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001042 * However, don't wait for it to arrive. Also, do enable mapping,
1043 * because if it's put back with vungetc() it's too late to apply
1044 * mapping. */
1045 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 while (enc_utf8 && lang && (c = vpeekc()) > 0
1047 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1048 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001049 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!utf_iscomposing(c))
1051 {
1052 vungetc(c); /* it wasn't, put it back */
1053 break;
1054 }
1055 else if (ca.ncharC1 == 0)
1056 ca.ncharC1 = c;
1057 else
1058 ca.ncharC2 = c;
1059 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001060 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 }
1063 --no_mapping;
1064 --allow_keys;
1065 }
1066
1067#ifdef FEAT_CMDL_INFO
1068 /*
1069 * Flush the showcmd characters onto the screen so we can see them while
1070 * the command is being executed. Only do this when the shown command was
1071 * actually displayed, otherwise this will slow down a lot when executing
1072 * mappings.
1073 */
1074 if (need_flushbuf)
1075 out_flush();
1076#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001077 if (ca.cmdchar != K_IGNORE)
1078 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 State = NORMAL;
1081
1082 if (ca.nchar == ESC)
1083 {
1084 clearop(oap);
1085 if (restart_edit == 0 && goto_im())
1086 restart_edit = 'a';
1087 goto normal_end;
1088 }
1089
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001090 if (ca.cmdchar != K_IGNORE)
1091 {
1092 msg_didout = FALSE; /* don't scroll screen up for normal command */
1093 msg_col = 0;
1094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 old_pos = curwin->w_cursor; /* remember where cursor was */
1097
1098 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1099 * mode. */
1100 if (!VIsual_active && km_startsel)
1101 {
1102 if (nv_cmds[idx].cmd_flags & NV_SS)
1103 {
1104 start_selection();
1105 unshift_special(&ca);
1106 idx = find_command(ca.cmdchar);
1107 }
1108 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1109 && (mod_mask & MOD_MASK_SHIFT))
1110 {
1111 start_selection();
1112 mod_mask &= ~MOD_MASK_SHIFT;
1113 }
1114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 /*
1117 * Execute the command!
1118 * Call the command function found in the commands table.
1119 */
1120 ca.arg = nv_cmds[idx].cmd_arg;
1121 (nv_cmds[idx].cmd_func)(&ca);
1122
1123 /*
1124 * If we didn't start or finish an operator, reset oap->regname, unless we
1125 * need it later.
1126 */
1127 if (!finish_op
1128 && !oap->op_type
1129 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1130 {
1131 clearop(oap);
1132#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001133 {
1134 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001135
Bram Moolenaar536681b2011-05-10 16:12:45 +02001136 /* Adjust the register according to 'clipboard', so that when
1137 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001138# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001139 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001140# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001141 set_reg_var(regname);
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144 }
1145
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001146 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001147 * character or "z333<cr>". */
1148 if (old_mapped_len > 0)
1149 old_mapped_len = typebuf_maplen();
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /*
1152 * If an operation is pending, handle it...
1153 */
1154 do_pending_operator(&ca, old_col, FALSE);
1155
1156 /*
1157 * Wait for a moment when a message is displayed that will be overwritten
1158 * by the mode message.
1159 * In Visual mode and with "^O" in Insert mode, a short message will be
1160 * overwritten by the mode message. Wait a bit, until a key is hit.
1161 * In Visual mode, it's more important to keep the Visual area updated
1162 * than keeping a message (e.g. from a /pat search).
1163 * Only do this if the command was typed, not from a mapping.
1164 * Don't wait when emsg_silent is non-zero.
1165 * Also wait a bit after an error message, e.g. for "^O:".
1166 * Don't redraw the screen, it would remove the message.
1167 */
1168 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001169 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 || (VIsual_active
1172 && old_pos.lnum == curwin->w_cursor.lnum
1173 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 )
1175 && (clear_cmdline
1176 || redraw_cmdline)
1177 && (msg_didout || (msg_didany && msg_scroll))
1178 && !msg_nowait
1179 && KeyTyped)
1180 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 && (msg_scroll
1183 || emsg_on_display)))
1184 && oap->regname == 0
1185 && !(ca.retval & CA_COMMAND_BUSY)
1186 && stuff_empty()
1187 && typebuf_typed()
1188 && emsg_silent == 0
1189 && !did_wait_return
1190 && oap->op_type == OP_NOP)
1191 {
1192 int save_State = State;
1193
1194 /* Draw the cursor with the right shape here */
1195 if (restart_edit != 0)
1196 State = INSERT;
1197
1198 /* If need to redraw, and there is a "keep_msg", redraw before the
1199 * delay */
1200 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1201 {
1202 char_u *kmsg;
1203
1204 kmsg = keep_msg;
1205 keep_msg = NULL;
1206 /* showmode() will clear keep_msg, but we want to use it anyway */
1207 update_screen(0);
1208 /* now reset it, otherwise it's put in the history again */
1209 keep_msg = kmsg;
1210 msg_attr(kmsg, keep_msg_attr);
1211 vim_free(kmsg);
1212 }
1213 setcursor();
1214 cursor_on();
1215 out_flush();
1216 if (msg_scroll || emsg_on_display)
1217 ui_delay(1000L, TRUE); /* wait at least one second */
1218 ui_delay(3000L, FALSE); /* wait up to three seconds */
1219 State = save_State;
1220
1221 msg_scroll = FALSE;
1222 emsg_on_display = FALSE;
1223 }
1224
1225 /*
1226 * Finish up after executing a Normal mode command.
1227 */
1228normal_end:
1229
1230 msg_nowait = FALSE;
1231
1232 /* Reset finish_op, in case it was set */
1233#ifdef CURSOR_SHAPE
1234 c = finish_op;
1235#endif
1236 finish_op = FALSE;
1237#ifdef CURSOR_SHAPE
1238 /* Redraw the cursor with another shape, if we were in Operator-pending
1239 * mode or did a replace command. */
1240 if (c || ca.cmdchar == 'r')
1241 {
1242 ui_cursor_shape(); /* may show different cursor shape */
1243# ifdef FEAT_MOUSESHAPE
1244 update_mouseshape(-1);
1245# endif
1246 }
1247#endif
1248
1249#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001250 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001251 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 clear_showcmd();
1253#endif
1254
1255 checkpcmark(); /* check if we moved since setting pcmark */
1256 vim_free(ca.searchbuf);
1257
1258#ifdef FEAT_MBYTE
1259 if (has_mbyte)
1260 mb_adjust_cursor();
1261#endif
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (curwin->w_p_scb && toplevel)
1264 {
1265 validate_cursor(); /* may need to update w_leftcol */
1266 do_check_scrollbind(TRUE);
1267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar860cae12010-06-05 23:22:07 +02001269 if (curwin->w_p_crb && toplevel)
1270 {
1271 validate_cursor(); /* may need to update w_leftcol */
1272 do_check_cursorbind();
1273 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001274
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001275#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001276 /* don't go to Insert mode if a terminal has a running job */
1277 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001278 restart_edit = 0;
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
1282 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1283 * if still inside a mapping that started in Visual mode).
1284 * May switch from Visual to Select mode after CTRL-O command.
1285 */
1286 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1288 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && !(ca.retval & CA_COMMAND_BUSY)
1290 && stuff_empty()
1291 && oap->regname == 0)
1292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (restart_VIsual_select == 1)
1294 {
1295 VIsual_select = TRUE;
1296 showmode();
1297 restart_VIsual_select = 0;
1298 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001299 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 (void)edit(restart_edit, FALSE, 1L);
1301 }
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (restart_VIsual_select == 2)
1304 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 /* Save count before an operator for next time. */
1307 opcount = ca.opcount;
1308}
1309
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310#ifdef FEAT_EVAL
1311/*
1312 * Set v:count and v:count1 according to "cap".
1313 * Set v:prevcount only when "set_prevcount" is TRUE.
1314 */
1315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001317{
1318 long count = cap->count0;
1319
1320 /* multiply with cap->opcount the same way as above */
1321 if (cap->opcount != 0)
1322 count = cap->opcount * (count == 0 ? 1 : count);
1323 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1324 *set_prevcount = FALSE; /* only set v:prevcount once */
1325}
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328/*
1329 * Handle an operator after visual mode or when the movement is finished
1330 */
1331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001332do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 oparg_T *oap = cap->oap;
1335 pos_T old_cursor;
1336 int empty_region_error;
1337 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001338#ifdef FEAT_LINEBREAK
1339 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 /* The visual area is remembered for redo */
1343 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1344 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001345 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001347 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001348#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350#endif
1351
1352#if defined(FEAT_CLIPBOARD)
1353 /*
1354 * Yank the visual area into the GUI selection register before we operate
1355 * on it and lose it forever.
1356 * Don't do it if a specific register was specified, so that ""x"*P works.
1357 * This could call do_pending_operator() recursively, but that's OK
1358 * because gui_yank will be TRUE for the nested call.
1359 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001360 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 && oap->op_type != OP_NOP
1362 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 && VIsual_active
1364 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 && oap->regname == 0)
1366 clip_auto_select();
1367#endif
1368 old_cursor = curwin->w_cursor;
1369
1370 /*
1371 * If an operation is pending, handle it...
1372 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001373 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001375#ifdef FEAT_LINEBREAK
1376 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001377 if (curwin->w_p_lbr)
1378 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001379 curwin->w_p_lbr = FALSE;
1380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 oap->is_VIsual = VIsual_active;
1382 if (oap->motion_force == 'V')
1383 oap->motion_type = MLINE;
1384 else if (oap->motion_force == 'v')
1385 {
1386 /* If the motion was linewise, "inclusive" will not have been set.
1387 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1388 if (oap->motion_type == MLINE)
1389 oap->inclusive = FALSE;
1390 /* If the motion already was characterwise, toggle "inclusive" */
1391 else if (oap->motion_type == MCHAR)
1392 oap->inclusive = !oap->inclusive;
1393 oap->motion_type = MCHAR;
1394 }
1395 else if (oap->motion_force == Ctrl_V)
1396 {
1397 /* Change line- or characterwise motion into Visual block mode. */
1398 VIsual_active = TRUE;
1399 VIsual = oap->start;
1400 VIsual_mode = Ctrl_V;
1401 VIsual_select = FALSE;
1402 VIsual_reselect = FALSE;
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar7afea822013-04-24 18:34:45 +02001405 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1406 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001408 && ((!VIsual_active || oap->motion_force)
1409 /* Also redo Operator-pending Visual mode mappings */
1410 || (VIsual_active && cap->cmdchar == ':'
1411 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001412 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef FEAT_FOLDING
1414 && oap->op_type != OP_FOLD
1415 && oap->op_type != OP_FOLDOPEN
1416 && oap->op_type != OP_FOLDOPENREC
1417 && oap->op_type != OP_FOLDCLOSE
1418 && oap->op_type != OP_FOLDCLOSEREC
1419 && oap->op_type != OP_FOLDDEL
1420 && oap->op_type != OP_FOLDDELREC
1421#endif
1422 )
1423 {
1424 prep_redo(oap->regname, cap->count0,
1425 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1426 oap->motion_force, cap->cmdchar, cap->nchar);
1427 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1428 {
1429 /*
1430 * If 'cpoptions' does not contain 'r', insert the search
1431 * pattern to really repeat the same command.
1432 */
1433 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001434 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 AppendToRedobuff(NL_STR);
1436 }
1437 else if (cap->cmdchar == ':')
1438 {
1439 /* do_cmdline() has stored the first typed line in
1440 * "repeat_cmdline". When several lines are typed repeating
1441 * won't be possible. */
1442 if (repeat_cmdline == NULL)
1443 ResetRedobuff();
1444 else
1445 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001446 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001448 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451 }
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (redo_VIsual_busy)
1454 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001455 /* Redo of an operation on a Visual area. Use the same size from
1456 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 oap->start = curwin->w_cursor;
1458 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1459 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1460 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1461 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001462 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001464 if (VIsual_mode == 'v')
1465 {
1466 if (redo_VIsual_line_count <= 1)
1467 {
1468 validate_virtcol();
1469 curwin->w_curswant =
1470 curwin->w_virtcol + redo_VIsual_vcol - 1;
1471 }
1472 else
1473 curwin->w_curswant = redo_VIsual_vcol;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001476 {
1477 curwin->w_curswant = MAXCOL;
1478 }
1479 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 cap->count0 = redo_VIsual_count;
1482 if (redo_VIsual_count != 0)
1483 cap->count1 = redo_VIsual_count;
1484 else
1485 cap->count1 = 1;
1486 }
1487 else if (VIsual_active)
1488 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001489 if (!gui_yank)
1490 {
1491 /* Save the current VIsual area for '< and '> marks, and "gv" */
1492 curbuf->b_visual.vi_start = VIsual;
1493 curbuf->b_visual.vi_end = curwin->w_cursor;
1494 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001495 if (VIsual_mode_orig != NUL)
1496 {
1497 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1498 VIsual_mode_orig = NUL;
1499 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001500 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001502 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
1506 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001507 * characterwise selection.
1508 * Special case: gH<Del> deletes the last line. */
1509 if (VIsual_select && VIsual_mode == 'V'
1510 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001512 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 VIsual.col = 0;
1515 curwin->w_cursor.col =
1516 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1517 }
1518 else
1519 {
1520 curwin->w_cursor.col = 0;
1521 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1522 }
1523 VIsual_mode = 'v';
1524 }
1525 /* If 'selection' is "exclusive", backup one character for
1526 * charwise selections. */
1527 else if (VIsual_mode == 'v')
1528 {
1529# ifdef FEAT_VIRTUALEDIT
1530 include_line_break =
1531# endif
1532 unadjust_for_sel();
1533 }
1534
1535 oap->start = VIsual;
1536 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001539# ifdef FEAT_VIRTUALEDIT
1540 oap->start.coladd = 0;
1541# endif
1542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
1545 /*
1546 * Set oap->start to the first position of the operated text, oap->end
1547 * to the end of the operated text. w_cursor is equal to oap->start.
1548 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001549 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551#ifdef FEAT_FOLDING
1552 /* Include folded lines completely. */
1553 if (!VIsual_active)
1554 {
1555 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1556 oap->start.col = 0;
1557 if (hasFolding(curwin->w_cursor.lnum, NULL,
1558 &curwin->w_cursor.lnum))
1559 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1560 }
1561#endif
1562 oap->end = curwin->w_cursor;
1563 curwin->w_cursor = oap->start;
1564
1565 /* w_virtcol may have been updated; if the cursor goes back to its
1566 * previous position w_virtcol becomes invalid and isn't updated
1567 * automatically. */
1568 curwin->w_valid &= ~VALID_VIRTCOL;
1569 }
1570 else
1571 {
1572#ifdef FEAT_FOLDING
1573 /* Include folded lines completely. */
1574 if (!VIsual_active && oap->motion_type == MLINE)
1575 {
1576 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1577 NULL))
1578 curwin->w_cursor.col = 0;
1579 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1580 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1581 }
1582#endif
1583 oap->end = oap->start;
1584 oap->start = curwin->w_cursor;
1585 }
1586
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001587 /* Just in case lines were deleted that make the position invalid. */
1588 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1590
1591#ifdef FEAT_VIRTUALEDIT
1592 /* Set "virtual_op" before resetting VIsual_active. */
1593 virtual_op = virtual_active();
1594#endif
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (VIsual_active || redo_VIsual_busy)
1597 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001598 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 if (!redo_VIsual_busy && !gui_yank)
1601 {
1602 /*
1603 * Prepare to reselect and redo Visual: this is based on the
1604 * size of the Visual text
1605 */
1606 resel_VIsual_mode = VIsual_mode;
1607 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001608 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001610 {
1611 if (VIsual_mode != Ctrl_V)
1612 getvvcol(curwin, &(oap->end),
1613 NULL, NULL, &oap->end_vcol);
1614 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1615 {
1616 if (VIsual_mode != Ctrl_V)
1617 getvvcol(curwin, &(oap->start),
1618 &oap->start_vcol, NULL, NULL);
1619 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1620 }
1621 else
1622 resel_VIsual_vcol = oap->end_vcol;
1623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 resel_VIsual_line_count = oap->line_count;
1625 }
1626
1627 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1628 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1629 && oap->op_type != OP_COLON
1630#ifdef FEAT_FOLDING
1631 && oap->op_type != OP_FOLD
1632 && oap->op_type != OP_FOLDOPEN
1633 && oap->op_type != OP_FOLDOPENREC
1634 && oap->op_type != OP_FOLDCLOSE
1635 && oap->op_type != OP_FOLDCLOSEREC
1636 && oap->op_type != OP_FOLDDEL
1637 && oap->op_type != OP_FOLDDELREC
1638#endif
1639 && oap->motion_force == NUL
1640 )
1641 {
1642 /* Prepare for redoing. Only use the nchar field for "r",
1643 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001644 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1645 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001646 prep_redo(oap->regname, cap->count0,
1647 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1648 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001649 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001650 {
1651 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1652
1653 /* reverse what nv_replace() did */
1654 if (nchar == REPLACE_CR_NCHAR)
1655 nchar = CAR;
1656 else if (nchar == REPLACE_NL_NCHAR)
1657 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001658 prep_redo(oap->regname, 0L, NUL, 'v',
1659 get_op_char(oap->op_type),
1660 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001661 nchar);
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (!redo_VIsual_busy)
1664 {
1665 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001666 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 redo_VIsual_line_count = resel_VIsual_line_count;
1668 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001669 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 }
1672
1673 /*
1674 * oap->inclusive defaults to TRUE.
1675 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1676 * FALSE. This makes "d}P" and "v}dP" work the same.
1677 */
1678 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1679 oap->inclusive = TRUE;
1680 if (VIsual_mode == 'V')
1681 oap->motion_type = MLINE;
1682 else
1683 {
1684 oap->motion_type = MCHAR;
1685 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001686#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 )
1690 {
1691 oap->inclusive = FALSE;
1692 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001693 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001694 if (*p_sel != 'o'
1695 && !op_on_lines(oap->op_type)
1696 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001698 ++oap->end.lnum;
1699 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001700#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001701 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001702#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001703 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
1705 }
1706 }
1707
1708 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /*
1711 * Switch Visual off now, so screen updating does
1712 * not show inverted text when the screen is redrawn.
1713 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1714 * no screen redraw, so it is done here to remove the inverted
1715 * part.
1716 */
1717 if (!gui_yank)
1718 {
1719 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001720#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 setmouse();
1722 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001723#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001724 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 if ((oap->op_type == OP_YANK
1726 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001727 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 || oap->op_type == OP_FILTER)
1729 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001730 {
1731#ifdef FEAT_LINEBREAK
1732 /* make sure redrawing is correct */
1733 curwin->w_p_lbr = lbr_saved;
1734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
1740#ifdef FEAT_MBYTE
1741 /* Include the trailing byte of a multi-byte char. */
1742 if (has_mbyte && oap->inclusive)
1743 {
1744 int l;
1745
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001746 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (l > 1)
1748 oap->end.col += l - 1;
1749 }
1750#endif
1751 curwin->w_set_curswant = TRUE;
1752
1753 /*
1754 * oap->empty is set when start and end are the same. The inclusive
1755 * flag affects this too, unless yanking and the end is on a NUL.
1756 */
1757 oap->empty = (oap->motion_type == MCHAR
1758 && (!oap->inclusive
1759 || (oap->op_type == OP_YANK
1760 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001761 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#ifdef FEAT_VIRTUALEDIT
1763 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1764#endif
1765 );
1766 /*
1767 * For delete, change and yank, it's an error to operate on an
1768 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1769 */
1770 empty_region_error = (oap->empty
1771 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 /* Force a redraw when operating on an empty Visual region, when
1774 * 'modifiable is off or creating a fold. */
1775 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001776#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001780 {
1781#ifdef FEAT_LINEBREAK
1782 curwin->w_p_lbr = lbr_saved;
1783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 /*
1788 * If the end of an operator is in column one while oap->motion_type
1789 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1790 * character in the previous line. If op_start is on or before the
1791 * first non-blank in the line, the operator becomes linewise
1792 * (strange, but that's the way vi does it).
1793 */
1794 if ( oap->motion_type == MCHAR
1795 && oap->inclusive == FALSE
1796 && !(cap->retval & CA_NO_ADJ_OP_END)
1797 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001799 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 && oap->line_count > 1)
1801 {
1802 oap->end_adjusted = TRUE; /* remember that we did this */
1803 --oap->line_count;
1804 --oap->end.lnum;
1805 if (inindent(0))
1806 oap->motion_type = MLINE;
1807 else
1808 {
1809 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1810 if (oap->end.col)
1811 {
1812 --oap->end.col;
1813 oap->inclusive = TRUE;
1814 }
1815 }
1816 }
1817 else
1818 oap->end_adjusted = FALSE;
1819
1820 switch (oap->op_type)
1821 {
1822 case OP_LSHIFT:
1823 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001824 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 auto_format(FALSE, TRUE);
1826 break;
1827
1828 case OP_JOIN_NS:
1829 case OP_JOIN:
1830 if (oap->line_count < 2)
1831 oap->line_count = 2;
1832 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1833 curbuf->b_ml.ml_line_count)
1834 beep_flush();
1835 else
1836 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001837 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001838 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 auto_format(FALSE, TRUE);
1840 }
1841 break;
1842
1843 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001846 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001847 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001848 CancelRedo();
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 else
1851 {
1852 (void)op_delete(oap);
1853 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1854 u_save_cursor(); /* cursor line wasn't saved yet */
1855 auto_format(FALSE, TRUE);
1856 }
1857 break;
1858
1859 case OP_YANK:
1860 if (empty_region_error)
1861 {
1862 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001863 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001864 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001865 CancelRedo();
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001869 {
1870#ifdef FEAT_LINEBREAK
1871 curwin->w_p_lbr = lbr_saved;
1872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 check_cursor_col();
1876 break;
1877
1878 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001881 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001882 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001883 CancelRedo();
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 else
1886 {
1887 /* This is a new edit command, not a restart. Need to
1888 * remember it to make 'insertmode' work with mappings for
1889 * Visual mode. But do this only once and not when typed and
1890 * 'insertmode' isn't set. */
1891 if (p_im || !KeyTyped)
1892 restart_edit_save = restart_edit;
1893 else
1894 restart_edit_save = 0;
1895 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001896#ifdef FEAT_LINEBREAK
1897 /* Restore linebreak, so that when the user edits it looks as
1898 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001899 if (curwin->w_p_lbr != lbr_saved)
1900 {
1901 curwin->w_p_lbr = lbr_saved;
1902 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1903 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 /* Reset finish_op now, don't want it set inside edit(). */
1906 finish_op = FALSE;
1907 if (op_change(oap)) /* will call edit() */
1908 cap->retval |= CA_COMMAND_BUSY;
1909 if (restart_edit == 0)
1910 restart_edit = restart_edit_save;
1911 }
1912 break;
1913
1914 case OP_FILTER:
1915 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1916 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1917 else
1918 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001919 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 case OP_INDENT:
1922 case OP_COLON:
1923
1924#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1925 /*
1926 * If 'equalprg' is empty, do the indenting internally.
1927 */
1928 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1929 {
1930# ifdef FEAT_LISP
1931 if (curbuf->b_p_lisp)
1932 {
1933 op_reindent(oap, get_lisp_indent);
1934 break;
1935 }
1936# endif
1937# ifdef FEAT_CINDENT
1938 op_reindent(oap,
1939# ifdef FEAT_EVAL
1940 *curbuf->b_p_inde != NUL ? get_expr_indent :
1941# endif
1942 get_c_indent);
1943 break;
1944# endif
1945 }
1946#endif
1947
1948 op_colon(oap);
1949 break;
1950
1951 case OP_TILDE:
1952 case OP_UPPER:
1953 case OP_LOWER:
1954 case OP_ROT13:
1955 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001956 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001957 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001958 CancelRedo();
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961 op_tilde(oap);
1962 check_cursor_col();
1963 break;
1964
1965 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001966#if defined(FEAT_EVAL)
1967 if (*curbuf->b_p_fex != NUL)
1968 op_formatexpr(oap); /* use expression */
1969 else
1970#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001971 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 op_colon(oap); /* use external command */
1973 else
1974 op_format(oap, FALSE); /* use internal function */
1975 break;
1976
1977 case OP_FORMAT2:
1978 op_format(oap, TRUE); /* use internal function */
1979 break;
1980
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001981 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001982#ifdef FEAT_LINEBREAK
1983 /* Restore linebreak, so that when the user edits it looks as
1984 * before. */
1985 curwin->w_p_lbr = lbr_saved;
1986#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001987 op_function(oap); /* call 'operatorfunc' */
1988 break;
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 case OP_INSERT:
1991 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993#ifdef FEAT_VISUALEXTRA
1994 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001995 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001996 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001997 CancelRedo();
1998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 else
2000 {
2001 /* This is a new edit command, not a restart. Need to
2002 * remember it to make 'insertmode' work with mappings for
2003 * Visual mode. But do this only once. */
2004 restart_edit_save = restart_edit;
2005 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002006#ifdef FEAT_LINEBREAK
2007 /* Restore linebreak, so that when the user edits it looks as
2008 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002009 if (curwin->w_p_lbr != lbr_saved)
2010 {
2011 curwin->w_p_lbr = lbr_saved;
2012 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2013 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002016#ifdef FEAT_LINEBREAK
2017 /* Reset linebreak, so that formatting works correctly. */
2018 curwin->w_p_lbr = FALSE;
2019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 /* TODO: when inserting in several lines, should format all
2022 * the lines. */
2023 auto_format(FALSE, TRUE);
2024
2025 if (restart_edit == 0)
2026 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002027 else
2028 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002031 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#endif
2033 break;
2034
2035 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037#ifdef FEAT_VISUALEXTRA
2038 if (empty_region_error)
2039#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002040 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002041 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002042 CancelRedo();
2043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#ifdef FEAT_VISUALEXTRA
2045 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002046 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002047# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002048 /* Restore linebreak, so that when the user edits it looks as
2049 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002050 if (curwin->w_p_lbr != lbr_saved)
2051 {
2052 curwin->w_p_lbr = lbr_saved;
2053 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2054 }
2055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058#endif
2059 break;
2060
2061#ifdef FEAT_FOLDING
2062 case OP_FOLD:
2063 VIsual_reselect = FALSE; /* don't reselect now */
2064 foldCreate(oap->start.lnum, oap->end.lnum);
2065 break;
2066
2067 case OP_FOLDOPEN:
2068 case OP_FOLDOPENREC:
2069 case OP_FOLDCLOSE:
2070 case OP_FOLDCLOSEREC:
2071 VIsual_reselect = FALSE; /* don't reselect now */
2072 opFoldRange(oap->start.lnum, oap->end.lnum,
2073 oap->op_type == OP_FOLDOPEN
2074 || oap->op_type == OP_FOLDOPENREC,
2075 oap->op_type == OP_FOLDOPENREC
2076 || oap->op_type == OP_FOLDCLOSEREC,
2077 oap->is_VIsual);
2078 break;
2079
2080 case OP_FOLDDEL:
2081 case OP_FOLDDELREC:
2082 VIsual_reselect = FALSE; /* don't reselect now */
2083 deleteFold(oap->start.lnum, oap->end.lnum,
2084 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2085 break;
2086#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002087 case OP_NR_ADD:
2088 case OP_NR_SUB:
2089 if (empty_region_error)
2090 {
2091 vim_beep(BO_OPER);
2092 CancelRedo();
2093 }
2094 else
2095 {
2096 VIsual_active = TRUE;
2097#ifdef FEAT_LINEBREAK
2098 curwin->w_p_lbr = lbr_saved;
2099#endif
2100 op_addsub(oap, cap->count1, redo_VIsual_arg);
2101 VIsual_active = FALSE;
2102 }
2103 check_cursor_col();
2104 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 default:
2106 clearopbeep(oap);
2107 }
2108#ifdef FEAT_VIRTUALEDIT
2109 virtual_op = MAYBE;
2110#endif
2111 if (!gui_yank)
2112 {
2113 /*
2114 * if 'sol' not set, go back to old column for some commands
2115 */
2116 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2117 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2118 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002119 {
2120#ifdef FEAT_LINEBREAK
2121 curwin->w_p_lbr = FALSE;
2122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 else
2127 {
2128 curwin->w_cursor = old_cursor;
2129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 clearop(oap);
2132 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002133#ifdef FEAT_LINEBREAK
2134 curwin->w_p_lbr = lbr_saved;
2135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136}
2137
2138/*
2139 * Handle indent and format operators and visual mode ":".
2140 */
2141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002142op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143{
2144 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (oap->is_VIsual)
2146 stuffReadbuff((char_u *)"'<,'>");
2147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 /*
2150 * Make the range look nice, so it can be repeated.
2151 */
2152 if (oap->start.lnum == curwin->w_cursor.lnum)
2153 stuffcharReadbuff('.');
2154 else
2155 stuffnumReadbuff((long)oap->start.lnum);
2156 if (oap->end.lnum != oap->start.lnum)
2157 {
2158 stuffcharReadbuff(',');
2159 if (oap->end.lnum == curwin->w_cursor.lnum)
2160 stuffcharReadbuff('.');
2161 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2162 stuffcharReadbuff('$');
2163 else if (oap->start.lnum == curwin->w_cursor.lnum)
2164 {
2165 stuffReadbuff((char_u *)".+");
2166 stuffnumReadbuff((long)oap->line_count - 1);
2167 }
2168 else
2169 stuffnumReadbuff((long)oap->end.lnum);
2170 }
2171 }
2172 if (oap->op_type != OP_COLON)
2173 stuffReadbuff((char_u *)"!");
2174 if (oap->op_type == OP_INDENT)
2175 {
2176#ifndef FEAT_CINDENT
2177 if (*get_equalprg() == NUL)
2178 stuffReadbuff((char_u *)"indent");
2179 else
2180#endif
2181 stuffReadbuff(get_equalprg());
2182 stuffReadbuff((char_u *)"\n");
2183 }
2184 else if (oap->op_type == OP_FORMAT)
2185 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002186 if (*curbuf->b_p_fp != NUL)
2187 stuffReadbuff(curbuf->b_p_fp);
2188 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002190 else
2191 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002192 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194
2195 /*
2196 * do_cmdline() does the rest
2197 */
2198}
2199
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002200/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002201 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002202 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002205{
2206#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002207 typval_T argv[2];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002208# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002209 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002210# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002211
2212 if (*p_opfunc == NUL)
2213 EMSG(_("E774: 'operatorfunc' is empty"));
2214 else
2215 {
2216 /* Set '[ and '] marks to text to be operated on. */
2217 curbuf->b_op_start = oap->start;
2218 curbuf->b_op_end = oap->end;
2219 if (oap->motion_type != MLINE && !oap->inclusive)
2220 /* Exclude the end position. */
2221 decl(&curbuf->b_op_end);
2222
Bram Moolenaarffa96842018-06-12 22:05:14 +02002223 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002224 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002225 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002226 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002227 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002228 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002229 argv[0].vval.v_string = (char_u *)"char";
2230 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002231
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002232# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002233 /* Reset virtual_op so that 'virtualedit' can be changed in the
2234 * function. */
2235 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002236# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002237
Bram Moolenaarded27a12018-08-01 19:06:03 +02002238 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002239
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002240# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002241 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002242# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002243 }
2244#else
2245 EMSG(_("E775: Eval feature not available"));
2246#endif
2247}
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#if defined(FEAT_MOUSE) || defined(PROTO)
2250/*
2251 * Do the appropriate action for the current mouse click in the current mode.
2252 * Not used for Command-line mode.
2253 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002254 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * event modi- position visual change action
2256 * fier cursor window
2257 * left press - yes end yes
2258 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002259 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 * left drag - yes start if moved no
2261 * left relse - yes start if moved no
2262 * middle press - yes if not active no put register
2263 * middle press - yes if active no yank and put
2264 * right press - yes start or extend yes
2265 * right press S yes no change yes "#" (2)
2266 * right drag - yes extend no
2267 * right relse - yes extend no
2268 *
2269 * Insert or Replace Mode:
2270 * event modi- position visual change action
2271 * fier cursor window
2272 * left press - yes (cannot be active) yes
2273 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2274 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2275 * left drag - yes start or extend (1) no CTRL-O (1)
2276 * left relse - yes start or extend (1) no CTRL-O (1)
2277 * middle press - no (cannot be active) no put register
2278 * right press - yes start or extend yes CTRL-O
2279 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2280 *
2281 * (1) only if mouse pointer moved since press
2282 * (2) only if click is in same buffer
2283 *
2284 * Return TRUE if start_arrow() should be called for edit mode.
2285 */
2286 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002287do_mouse(
2288 oparg_T *oap, /* operator argument, can be NULL */
2289 int c, /* K_LEFTMOUSE, etc */
2290 int dir, /* Direction to 'put' if necessary */
2291 long count,
2292 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
2294 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2295 static int got_click = FALSE; /* got a click some time back */
2296
2297 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2298 int is_click; /* If FALSE it's a drag or release event */
2299 int is_drag; /* If TRUE it's a drag event */
2300 int jump_flags = 0; /* flags for jump_to_mouse() */
2301 pos_T start_visual;
2302 int moved; /* Has cursor moved? */
2303 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002304 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 int c1, c2;
2307#if defined(FEAT_FOLDING)
2308 pos_T save_cursor;
2309#endif
2310 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 static pos_T orig_cursor;
2312 colnr_T leftcol, rightcol;
2313 pos_T end_visual;
2314 int diff;
2315 int old_active = VIsual_active;
2316 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 int regname;
2318
2319#if defined(FEAT_FOLDING)
2320 save_cursor = curwin->w_cursor;
2321#endif
2322
2323 /*
2324 * When GUI is active, always recognize mouse events, otherwise:
2325 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2326 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2327 * - For command line and insert mode 'mouse' is checked before calling
2328 * do_mouse().
2329 */
2330 if (do_always)
2331 do_always = FALSE;
2332 else
2333#ifdef FEAT_GUI
2334 if (!gui.in_use)
2335#endif
2336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (VIsual_active)
2338 {
2339 if (!mouse_has(MOUSE_VISUAL))
2340 return FALSE;
2341 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002342 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 return FALSE;
2344 }
2345
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002346 for (;;)
2347 {
2348 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2349 if (is_drag)
2350 {
2351 /* If the next character is the same mouse event then use that
2352 * one. Speeds up dragging the status line. */
2353 if (vpeekc() != NUL)
2354 {
2355 int nc;
2356 int save_mouse_row = mouse_row;
2357 int save_mouse_col = mouse_col;
2358
2359 /* Need to get the character, peeking doesn't get the actual
2360 * one. */
2361 nc = safe_vgetc();
2362 if (c == nc)
2363 continue;
2364 vungetc(nc);
2365 mouse_row = save_mouse_row;
2366 mouse_col = save_mouse_col;
2367 }
2368 }
2369 break;
2370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002372 if (c == K_MOUSEMOVE)
2373 {
2374 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002375#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002376 ui_may_remove_balloon();
2377 if (p_bevalterm && !VIsual_active)
2378 {
2379 profile_setlimit(p_bdlay, &bevalexpr_due);
2380 bevalexpr_due_set = TRUE;
2381 }
2382#endif
2383 return FALSE;
2384 }
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#ifdef FEAT_MOUSESHAPE
2387 /* May have stopped dragging the status or separator line. The pointer is
2388 * most likely still on the status or separator line. */
2389 if (!is_drag && drag_status_line)
2390 {
2391 drag_status_line = FALSE;
2392 update_mouseshape(SHAPE_IDX_STATUS);
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (!is_drag && drag_sep_line)
2395 {
2396 drag_sep_line = FALSE;
2397 update_mouseshape(SHAPE_IDX_VSEP);
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#endif
2400
2401 /*
2402 * Ignore drag and release events if we didn't get a click.
2403 */
2404 if (is_click)
2405 got_click = TRUE;
2406 else
2407 {
2408 if (!got_click) /* didn't get click, ignore */
2409 return FALSE;
2410 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002413 if (in_tab_line)
2414 {
2415 in_tab_line = FALSE;
2416 return FALSE;
2417 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /*
2422 * CTRL right mouse button does CTRL-T
2423 */
2424 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2425 {
2426 if (State & INSERT)
2427 stuffcharReadbuff(Ctrl_O);
2428 if (count > 1)
2429 stuffnumReadbuff(count);
2430 stuffcharReadbuff(Ctrl_T);
2431 got_click = FALSE; /* ignore drag&release now */
2432 return FALSE;
2433 }
2434
2435 /*
2436 * CTRL only works with left mouse button
2437 */
2438 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2439 return FALSE;
2440
2441 /*
2442 * When a modifier is down, ignore drag and release events, as well as
2443 * multiple clicks and the middle mouse button.
2444 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2445 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002446 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2447 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 && (!is_click
2449 || (mod_mask & MOD_MASK_MULTI_CLICK)
2450 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002451 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 && mouse_model_popup()
2453 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002454 && !((mod_mask & MOD_MASK_ALT)
2455 && !mouse_model_popup()
2456 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 )
2458 return FALSE;
2459
2460 /*
2461 * If the button press was used as the movement command for an operator
2462 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2463 * drag/release events.
2464 */
2465 if (!is_click && which_button == MOUSE_MIDDLE)
2466 return FALSE;
2467
2468 if (oap != NULL)
2469 regname = oap->regname;
2470 else
2471 regname = 0;
2472
2473 /*
2474 * Middle mouse button does a 'put' of the selected text
2475 */
2476 if (which_button == MOUSE_MIDDLE)
2477 {
2478 if (State == NORMAL)
2479 {
2480 /*
2481 * If an operator was pending, we don't know what the user wanted
2482 * to do. Go back to normal mode: Clear the operator and beep().
2483 */
2484 if (oap != NULL && oap->op_type != OP_NOP)
2485 {
2486 clearopbeep(oap);
2487 return FALSE;
2488 }
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /*
2491 * If visual was active, yank the highlighted text and put it
2492 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002493 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
2495 if (VIsual_active)
2496 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002497 if (VIsual_select)
2498 {
2499 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002500 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002501 }
2502 else
2503 {
2504 stuffcharReadbuff('y');
2505 stuffcharReadbuff(K_MIDDLEMOUSE);
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 do_always = TRUE; /* ignore 'mouse' setting next time */
2508 return FALSE;
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /*
2511 * The rest is below jump_to_mouse()
2512 */
2513 }
2514
2515 else if ((State & INSERT) == 0)
2516 return FALSE;
2517
2518 /*
2519 * Middle click in insert mode doesn't move the mouse, just insert the
2520 * contents of a register. '.' register is special, can't insert that
2521 * with do_put().
2522 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2523 * happens for the GUI).
2524 */
2525 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2526 {
2527 if (regname == '.')
2528 insert_reg(regname, TRUE);
2529 else
2530 {
2531#ifdef FEAT_CLIPBOARD
2532 if (clip_star.available && regname == 0)
2533 regname = '*';
2534#endif
2535 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2536 insert_reg(regname, TRUE);
2537 else
2538 {
2539 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2540
2541 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2542 AppendCharToRedobuff(Ctrl_R);
2543 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2544 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2545 }
2546 }
2547 return FALSE;
2548 }
2549 }
2550
2551 /* When dragging or button-up stay in the same window. */
2552 if (!is_click)
2553 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2554
2555 start_visual.lnum = 0;
2556
Bram Moolenaarf740b292006-02-16 22:11:02 +00002557 /* Check for clicking in the tab page line. */
2558 if (mouse_row == 0 && firstwin->w_winrow > 0)
2559 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002560 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002561 {
2562 if (in_tab_line)
2563 {
2564 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002565 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2566 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002567 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002568 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002569 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570
Bram Moolenaarf740b292006-02-16 22:11:02 +00002571 /* click in a tab selects that tab page */
2572 if (is_click
2573# ifdef FEAT_CMDWIN
2574 && cmdwin_type == 0
2575# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002578 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002579 c1 = TabPageIdxs[mouse_col];
2580 if (c1 >= 0)
2581 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002582 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2583 {
2584 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002586 tabpage_new();
2587 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2588 }
2589 else
2590 {
2591 /* Go to specified tab page, or next one if not clicking
2592 * on a label. */
2593 goto_tabpage(c1);
2594
2595 /* It's like clicking on the status line of a window. */
2596 if (curwin != old_curwin)
2597 end_visual_mode();
2598 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002599 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002600 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002601 {
2602 tabpage_T *tp;
2603
2604 /* Close the current or specified tab page. */
2605 if (c1 == -999)
2606 tp = curtab;
2607 else
2608 tp = find_tabpage(-c1);
2609 if (tp == curtab)
2610 {
2611 if (first_tabpage->tp_next != NULL)
2612 tabpage_close(FALSE);
2613 }
2614 else if (tp != NULL)
2615 tabpage_close_other(tp, FALSE);
2616 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002618 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002619 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002620 else if (is_drag && in_tab_line)
2621 {
2622 c1 = TabPageIdxs[mouse_col];
2623 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2624 return FALSE;
2625 }
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2629 * right button up -> pop-up menu
2630 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002631 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
2633 if (mouse_model_popup())
2634 {
2635 if (which_button == MOUSE_RIGHT
2636 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2637 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002638#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002640 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2641 || defined(FEAT_TERM_POPUP_MENU)
2642# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (gui.in_use)
2644 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002645# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2646 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2647 if (!is_click)
2648 /* Ignore right button release events, only shows the popup
2649 * menu on the button down event. */
2650 return FALSE;
2651# endif
2652# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2653 if (is_click || is_drag)
2654 /* Ignore right button down and drag mouse events. Windows
2655 * only shows the popup menu on the button up event. */
2656 return FALSE;
2657# endif
2658 }
2659# endif
2660# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2661 else
2662# endif
2663# if defined(FEAT_TERM_POPUP_MENU)
2664 if (!is_click)
2665 /* Ignore right button release events, only shows the popup
2666 * menu on the button down event. */
2667 return FALSE;
2668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002670 jump_flags = 0;
2671 if (STRCMP(p_mousem, "popup_setpos") == 0)
2672 {
2673 /* First set the cursor position before showing the popup
2674 * menu. */
2675 if (VIsual_active)
2676 {
2677 pos_T m_pos;
2678
2679 /*
2680 * set MOUSE_MAY_STOP_VIS if we are outside the
2681 * selection or the current window (might have false
2682 * negative here)
2683 */
2684 if (mouse_row < curwin->w_winrow
2685 || mouse_row
2686 > (curwin->w_winrow + curwin->w_height))
2687 jump_flags = MOUSE_MAY_STOP_VIS;
2688 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2689 jump_flags = MOUSE_MAY_STOP_VIS;
2690 else
2691 {
2692 if ((LT_POS(curwin->w_cursor, VIsual)
2693 && (LT_POS(m_pos, curwin->w_cursor)
2694 || LT_POS(VIsual, m_pos)))
2695 || (LT_POS(VIsual, curwin->w_cursor)
2696 && (LT_POS(m_pos, VIsual)
2697 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002699 jump_flags = MOUSE_MAY_STOP_VIS;
2700 }
2701 else if (VIsual_mode == Ctrl_V)
2702 {
2703 getvcols(curwin, &curwin->w_cursor, &VIsual,
2704 &leftcol, &rightcol);
2705 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2706 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
2709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002711 else
2712 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002714 if (jump_flags)
2715 {
2716 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2717 update_curbuf(VIsual_active ? INVERTED : VALID);
2718 setcursor();
2719 out_flush(); /* Update before showing popup menu */
2720 }
2721# ifdef FEAT_MENU
2722 show_popupmenu();
2723 got_click = FALSE; /* ignore release events */
2724# endif
2725 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#else
2727 return FALSE;
2728#endif
2729 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002730 if (which_button == MOUSE_LEFT
2731 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 which_button = MOUSE_RIGHT;
2734 mod_mask &= ~MOD_MASK_SHIFT;
2735 }
2736 }
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if ((State & (NORMAL | INSERT))
2739 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2740 {
2741 if (which_button == MOUSE_LEFT)
2742 {
2743 if (is_click)
2744 {
2745 /* stop Visual mode for a left click in a window, but not when
2746 * on a status line */
2747 if (VIsual_active)
2748 jump_flags |= MOUSE_MAY_STOP_VIS;
2749 }
2750 else if (mouse_has(MOUSE_VISUAL))
2751 jump_flags |= MOUSE_MAY_VIS;
2752 }
2753 else if (which_button == MOUSE_RIGHT)
2754 {
2755 if (is_click && VIsual_active)
2756 {
2757 /*
2758 * Remember the start and end of visual before moving the
2759 * cursor.
2760 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002761 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 start_visual = curwin->w_cursor;
2764 end_visual = VIsual;
2765 }
2766 else
2767 {
2768 start_visual = VIsual;
2769 end_visual = curwin->w_cursor;
2770 }
2771 }
2772 jump_flags |= MOUSE_FOCUS;
2773 if (mouse_has(MOUSE_VISUAL))
2774 jump_flags |= MOUSE_MAY_VIS;
2775 }
2776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 /*
2779 * If an operator is pending, ignore all drags and releases until the
2780 * next mouse click.
2781 */
2782 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2783 {
2784 got_click = FALSE;
2785 oap->motion_type = MCHAR;
2786 }
2787
2788 /* When releasing the button let jump_to_mouse() know. */
2789 if (!is_click && !is_drag)
2790 jump_flags |= MOUSE_RELEASED;
2791
2792 /*
2793 * JUMP!
2794 */
2795 jump_flags = jump_to_mouse(jump_flags,
2796 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002797
2798#ifdef FEAT_MENU
2799 /* A click in the window toolbar has no side effects. */
2800 if (jump_flags & MOUSE_WINBAR)
2801 return FALSE;
2802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 moved = (jump_flags & CURSOR_MOVED);
2804 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
2807#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002808 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2810 {
2811 int key = KEY2TERMCAP1(c);
2812
2813 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2814 || key == (int)KE_RIGHTRELEASE)
2815 netbeans_button_release(which_button);
2816 }
2817#endif
2818
2819 /* When jumping to another window, clear a pending operator. That's a bit
2820 * friendlier than beeping and not jumping to that window. */
2821 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2822 clearop(oap);
2823
2824#ifdef FEAT_FOLDING
2825 if (mod_mask == 0
2826 && !is_drag
2827 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2828 && which_button == MOUSE_LEFT)
2829 {
2830 /* open or close a fold at this line */
2831 if (jump_flags & MOUSE_FOLD_OPEN)
2832 openFold(curwin->w_cursor.lnum, 1L);
2833 else
2834 closeFold(curwin->w_cursor.lnum, 1L);
2835 /* don't move the cursor if still in the same window */
2836 if (curwin == old_curwin)
2837 curwin->w_cursor = save_cursor;
2838 }
2839#endif
2840
2841#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2842 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2843 {
2844 clip_modeless(which_button, is_click, is_drag);
2845 return FALSE;
2846 }
2847#endif
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002850 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (VIsual_active && is_drag && p_so)
2852 {
2853 /* In the very first line, allow scrolling one line */
2854 if (mouse_row == 0)
2855 mouse_dragging = 2;
2856 else
2857 mouse_dragging = 1;
2858 }
2859
2860 /* When dragging the mouse above the window, scroll down. */
2861 if (is_drag && mouse_row < 0 && !in_status_line)
2862 {
2863 scroll_redraw(FALSE, 1L);
2864 mouse_row = 0;
2865 }
2866
2867 if (start_visual.lnum) /* right click in visual mode */
2868 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002869 /* When ALT is pressed make Visual mode blockwise. */
2870 if (mod_mask & MOD_MASK_ALT)
2871 VIsual_mode = Ctrl_V;
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 /*
2874 * In Visual-block mode, divide the area in four, pick up the corner
2875 * that is in the quarter that the cursor is in.
2876 */
2877 if (VIsual_mode == Ctrl_V)
2878 {
2879 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2880 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2881 end_visual.col = leftcol;
2882 else
2883 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002884 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 end_visual.lnum = start_visual.lnum;
2887
2888 /* move VIsual to the right column */
2889 start_visual = curwin->w_cursor; /* save the cursor pos */
2890 curwin->w_cursor = end_visual;
2891 coladvance(end_visual.col);
2892 VIsual = curwin->w_cursor;
2893 curwin->w_cursor = start_visual; /* restore the cursor */
2894 }
2895 else
2896 {
2897 /*
2898 * If the click is before the start of visual, change the start.
2899 * If the click is after the end of visual, change the end. If
2900 * the click is inside the visual, change the closest side.
2901 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002902 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002904 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 VIsual = start_visual;
2906 else
2907 {
2908 /* In the same line, compare column number */
2909 if (end_visual.lnum == start_visual.lnum)
2910 {
2911 if (curwin->w_cursor.col - start_visual.col >
2912 end_visual.col - curwin->w_cursor.col)
2913 VIsual = start_visual;
2914 else
2915 VIsual = end_visual;
2916 }
2917
2918 /* In different lines, compare line number */
2919 else
2920 {
2921 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2922 (end_visual.lnum - curwin->w_cursor.lnum);
2923
2924 if (diff > 0) /* closest to end */
2925 VIsual = start_visual;
2926 else if (diff < 0) /* closest to start */
2927 VIsual = end_visual;
2928 else /* in the middle line */
2929 {
2930 if (curwin->w_cursor.col <
2931 (start_visual.col + end_visual.col) / 2)
2932 VIsual = end_visual;
2933 else
2934 VIsual = start_visual;
2935 }
2936 }
2937 }
2938 }
2939 }
2940 /*
2941 * If Visual mode started in insert mode, execute "CTRL-O"
2942 */
2943 else if ((State & INSERT) && VIsual_active)
2944 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 /*
2947 * Middle mouse click: Put text before cursor.
2948 */
2949 if (which_button == MOUSE_MIDDLE)
2950 {
2951#ifdef FEAT_CLIPBOARD
2952 if (clip_star.available && regname == 0)
2953 regname = '*';
2954#endif
2955 if (yank_register_mline(regname))
2956 {
2957 if (mouse_past_bottom)
2958 dir = FORWARD;
2959 }
2960 else if (mouse_past_eol)
2961 dir = FORWARD;
2962
2963 if (fixindent)
2964 {
2965 c1 = (dir == BACKWARD) ? '[' : ']';
2966 c2 = 'p';
2967 }
2968 else
2969 {
2970 c1 = (dir == FORWARD) ? 'p' : 'P';
2971 c2 = NUL;
2972 }
2973 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2974
2975 /*
2976 * Remember where the paste started, so in edit() Insstart can be set
2977 * to this position
2978 */
2979 if (restart_edit != 0)
2980 where_paste_started = curwin->w_cursor;
2981 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2982 }
2983
Bram Moolenaar4033c552017-09-16 20:54:51 +02002984#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 /*
2986 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2987 * error under the mouse pointer.
2988 */
2989 else if (((mod_mask & MOD_MASK_CTRL)
2990 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2991 && bt_quickfix(curbuf))
2992 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002993 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002994 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002995 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002996 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 got_click = FALSE; /* ignore drag&release now */
2998 }
2999#endif
3000
3001 /*
3002 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3003 * under the mouse pointer.
3004 */
3005 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3006 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3007 {
3008 if (State & INSERT)
3009 stuffcharReadbuff(Ctrl_O);
3010 stuffcharReadbuff(Ctrl_RSB);
3011 got_click = FALSE; /* ignore drag&release now */
3012 }
3013
3014 /*
3015 * Shift-Mouse click searches for the next occurrence of the word under
3016 * the mouse pointer
3017 */
3018 else if ((mod_mask & MOD_MASK_SHIFT))
3019 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003020 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 stuffcharReadbuff(Ctrl_O);
3022 if (which_button == MOUSE_LEFT)
3023 stuffcharReadbuff('*');
3024 else /* MOUSE_RIGHT */
3025 stuffcharReadbuff('#');
3026 }
3027
3028 /* Handle double clicks, unless on status line */
3029 else if (in_status_line)
3030 {
3031#ifdef FEAT_MOUSESHAPE
3032 if ((is_drag || is_click) && !drag_status_line)
3033 {
3034 drag_status_line = TRUE;
3035 update_mouseshape(-1);
3036 }
3037#endif
3038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 else if (in_sep_line)
3040 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003041#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if ((is_drag || is_click) && !drag_sep_line)
3043 {
3044 drag_sep_line = TRUE;
3045 update_mouseshape(-1);
3046 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003047#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3050 && mouse_has(MOUSE_VISUAL))
3051 {
3052 if (is_click || !VIsual_active)
3053 {
3054 if (VIsual_active)
3055 orig_cursor = VIsual;
3056 else
3057 {
3058 check_visual_highlight();
3059 VIsual = curwin->w_cursor;
3060 orig_cursor = VIsual;
3061 VIsual_active = TRUE;
3062 VIsual_reselect = TRUE;
3063 /* start Select mode if 'selectmode' contains "mouse" */
3064 may_start_select('o');
3065 setmouse();
3066 }
3067 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003068 {
3069 /* Double click with ALT pressed makes it blockwise. */
3070 if (mod_mask & MOD_MASK_ALT)
3071 VIsual_mode = Ctrl_V;
3072 else
3073 VIsual_mode = 'v';
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3076 VIsual_mode = 'V';
3077 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3078 VIsual_mode = Ctrl_V;
3079#ifdef FEAT_CLIPBOARD
3080 /* Make sure the clipboard gets updated. Needed because start and
3081 * end may still be the same, and the selection needs to be owned */
3082 clip_star.vmode = NUL;
3083#endif
3084 }
3085 /*
3086 * A double click selects a word or a block.
3087 */
3088 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3089 {
3090 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003091 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093 if (is_click)
3094 {
3095 /* If the character under the cursor (skipping white space) is
3096 * not a word character, try finding a match and select a (),
3097 * {}, [], #if/#endif, etc. block. */
3098 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003099 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 inc(&end_visual);
3101 if (oap != NULL)
3102 oap->motion_type = MCHAR;
3103 if (oap != NULL
3104 && VIsual_mode == 'v'
3105 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003106 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 && (pos = findmatch(oap, NUL)) != NULL)
3108 {
3109 curwin->w_cursor = *pos;
3110 if (oap->motion_type == MLINE)
3111 VIsual_mode = 'V';
3112 else if (*p_sel == 'e')
3113 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003114 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 ++VIsual.col;
3116 else
3117 ++curwin->w_cursor.col;
3118 }
3119 }
3120 }
3121
3122 if (pos == NULL && (is_click || is_drag))
3123 {
3124 /* When not found a match or when dragging: extend to include
3125 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003126 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
3128 find_start_of_word(&curwin->w_cursor);
3129 find_end_of_word(&VIsual);
3130 }
3131 else
3132 {
3133 find_start_of_word(&VIsual);
3134 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3135#ifdef FEAT_MBYTE
3136 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003137 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#else
3139 ++curwin->w_cursor.col;
3140#endif
3141 find_end_of_word(&curwin->w_cursor);
3142 }
3143 }
3144 curwin->w_set_curswant = TRUE;
3145 }
3146 if (is_click)
3147 redraw_curbuf_later(INVERTED); /* update the inversion */
3148 }
3149 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003150 {
3151 if (mod_mask & MOD_MASK_ALT)
3152 VIsual_mode = Ctrl_V;
3153 else
3154 VIsual_mode = 'v';
3155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
3157 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003158 if ((!VIsual_active && old_active && mode_displayed)
3159 || (VIsual_active && p_smd && msg_silent == 0
3160 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 return moved;
3164}
3165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166/*
3167 * Move "pos" back to the start of the word it's in.
3168 */
3169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003170find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 char_u *line;
3173 int cclass;
3174 int col;
3175
3176 line = ml_get(pos->lnum);
3177 cclass = get_mouse_class(line + pos->col);
3178
3179 while (pos->col > 0)
3180 {
3181 col = pos->col - 1;
3182#ifdef FEAT_MBYTE
3183 col -= (*mb_head_off)(line, line + col);
3184#endif
3185 if (get_mouse_class(line + col) != cclass)
3186 break;
3187 pos->col = col;
3188 }
3189}
3190
3191/*
3192 * Move "pos" forward to the end of the word it's in.
3193 * When 'selection' is "exclusive", the position is just after the word.
3194 */
3195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197{
3198 char_u *line;
3199 int cclass;
3200 int col;
3201
3202 line = ml_get(pos->lnum);
3203 if (*p_sel == 'e' && pos->col > 0)
3204 {
3205 --pos->col;
3206#ifdef FEAT_MBYTE
3207 pos->col -= (*mb_head_off)(line, line + pos->col);
3208#endif
3209 }
3210 cclass = get_mouse_class(line + pos->col);
3211 while (line[pos->col] != NUL)
3212 {
3213#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
3216 col = pos->col + 1;
3217#endif
3218 if (get_mouse_class(line + col) != cclass)
3219 {
3220 if (*p_sel == 'e')
3221 pos->col = col;
3222 break;
3223 }
3224 pos->col = col;
3225 }
3226}
3227
3228/*
3229 * Get class of a character for selection: same class means same word.
3230 * 0: blank
3231 * 1: punctuation groups
3232 * 2: normal word character
3233 * >2: multi-byte word character.
3234 */
3235 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003236get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 int c;
3239
3240#ifdef FEAT_MBYTE
3241 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3242 return mb_get_class(p);
3243#endif
3244
3245 c = *p;
3246 if (c == ' ' || c == '\t')
3247 return 0;
3248
3249 if (vim_iswordc(c))
3250 return 2;
3251
3252 /*
3253 * There are a few special cases where we want certain combinations of
3254 * characters to be considered as a single word. These are things like
3255 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003256 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 */
3258 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3259 return 1;
3260 return c;
3261}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#endif /* FEAT_MOUSE */
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264/*
3265 * Check if highlighting for visual mode is possible, give a warning message
3266 * if not.
3267 */
3268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 static int did_check = FALSE;
3272
3273 if (full_screen)
3274 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003275 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 MSG(_("Warning: terminal cannot highlight"));
3277 did_check = TRUE;
3278 }
3279}
3280
3281/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003282 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 * This function should ALWAYS be called to end Visual mode, except from
3284 * do_pending_operator().
3285 */
3286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289#ifdef FEAT_CLIPBOARD
3290 /*
3291 * If we are using the clipboard, then remember what was selected in case
3292 * we need to paste it somewhere while we still own the selection.
3293 * Only do this when the clipboard is already owned. Don't want to grab
3294 * the selection when hitting ESC.
3295 */
3296 if (clip_star.available && clip_star.owned)
3297 clip_auto_select();
3298#endif
3299
3300 VIsual_active = FALSE;
3301#ifdef FEAT_MOUSE
3302 setmouse();
3303 mouse_dragging = 0;
3304#endif
3305
3306 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003307 curbuf->b_visual.vi_mode = VIsual_mode;
3308 curbuf->b_visual.vi_start = VIsual;
3309 curbuf->b_visual.vi_end = curwin->w_cursor;
3310 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#ifdef FEAT_EVAL
3312 curbuf->b_visual_mode_eval = VIsual_mode;
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef FEAT_VIRTUALEDIT
3315 if (!virtual_active())
3316 curwin->w_cursor.coladd = 0;
3317#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003318 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003320 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
3322
3323/*
3324 * Reset VIsual_active and VIsual_reselect.
3325 */
3326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003327reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 if (VIsual_active)
3330 {
3331 end_visual_mode();
3332 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3333 }
3334 VIsual_reselect = FALSE;
3335}
3336
3337/*
3338 * Reset VIsual_active and VIsual_reselect if it's set.
3339 */
3340 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003341reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 if (VIsual_active)
3344 {
3345 end_visual_mode();
3346 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3347 VIsual_reselect = FALSE;
3348 }
3349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351/*
3352 * Check for a balloon-eval special item to include when searching for an
3353 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3354 * Returns TRUE if the character at "*ptr" should be included.
3355 * "dir" is FORWARD or BACKWARD, the direction of searching.
3356 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3357 * "bnp" points to a counter for square brackets.
3358 */
3359 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003360find_is_eval_item(
3361 char_u *ptr,
3362 int *colp,
3363 int *bnp,
3364 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 /* Accept everything inside []. */
3367 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3368 ++*bnp;
3369 if (*bnp > 0)
3370 {
3371 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3372 --*bnp;
3373 return TRUE;
3374 }
3375
3376 /* skip over "s.var" */
3377 if (*ptr == '.')
3378 return TRUE;
3379
3380 /* two-character item: s->var */
3381 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3382 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3383 {
3384 *colp += dir;
3385 return TRUE;
3386 }
3387 return FALSE;
3388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390/*
3391 * Find the identifier under or to the right of the cursor.
3392 * "find_type" can have one of three values:
3393 * FIND_IDENT: find an identifier (keyword)
3394 * FIND_STRING: find any non-white string
3395 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003396 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 *
3398 * There are three steps:
3399 * 1. Search forward for the start of an identifier/string. Doesn't move if
3400 * already on one.
3401 * 2. Search backward for the start of this identifier/string.
3402 * This doesn't match the real Vi but I like it a little better and it
3403 * shouldn't bother anyone.
3404 * 3. Search forward to the end of this identifier/string.
3405 * When FIND_IDENT isn't defined, we backup until a blank.
3406 *
3407 * Returns the length of the string, or zero if no string is found.
3408 * If a string is found, a pointer to the string is put in "*string". This
3409 * string is not always NUL terminated.
3410 */
3411 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003412find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3415 curwin->w_cursor.col, string, find_type);
3416}
3417
3418/*
3419 * Like find_ident_under_cursor(), but for any window and any position.
3420 * However: Uses 'iskeyword' from the current window!.
3421 */
3422 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003423find_ident_at_pos(
3424 win_T *wp,
3425 linenr_T lnum,
3426 colnr_T startcol,
3427 char_u **string,
3428 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 char_u *ptr;
3431 int col = 0; /* init to shut up GCC */
3432 int i;
3433#ifdef FEAT_MBYTE
3434 int this_class = 0;
3435 int prev_class;
3436 int prevcol;
3437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 /*
3441 * if i == 0: try to find an identifier
3442 * if i == 1: try to find any non-white string
3443 */
3444 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3445 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3446 {
3447 /*
3448 * 1. skip to start of identifier/string
3449 */
3450 col = startcol;
3451#ifdef FEAT_MBYTE
3452 if (has_mbyte)
3453 {
3454 while (ptr[col] != NUL)
3455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 /* Stop at a ']' to evaluate "a[x]". */
3457 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 this_class = mb_get_class(ptr + col);
3460 if (this_class != 0 && (i == 1 || this_class != 1))
3461 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003462 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 }
3465 else
3466#endif
3467 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003468 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 )
3471 ++col;
3472
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 /* When starting on a ']' count it, so that we include the '['. */
3474 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 /*
3477 * 2. Back up to start of identifier/string.
3478 */
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 {
3482 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3484 this_class = mb_get_class((char_u *)"a");
3485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3490 prev_class = mb_get_class(ptr + prevcol);
3491 if (this_class != prev_class
3492 && (i == 0
3493 || prev_class == 0
3494 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 && (!(find_type & FIND_EVAL)
3496 || prevcol == 0
3497 || !find_is_eval_item(ptr + prevcol, &prevcol,
3498 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 )
3500 break;
3501 col = prevcol;
3502 }
3503
3504 /* If we don't want just any old string, or we've found an
3505 * identifier, stop searching. */
3506 if (this_class > 2)
3507 this_class = 2;
3508 if (!(find_type & FIND_STRING) || this_class == 2)
3509 break;
3510 }
3511 else
3512#endif
3513 {
3514 while (col > 0
3515 && ((i == 0
3516 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003517 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 && (!(find_type & FIND_IDENT)
3519 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 || ((find_type & FIND_EVAL)
3521 && col > 1
3522 && find_is_eval_item(ptr + col - 1, &col,
3523 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 ))
3525 --col;
3526
3527 /* If we don't want just any old string, or we've found an
3528 * identifier, stop searching. */
3529 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3530 break;
3531 }
3532 }
3533
3534 if (ptr[col] == NUL || (i == 0 && (
3535#ifdef FEAT_MBYTE
3536 has_mbyte ? this_class != 2 :
3537#endif
3538 !vim_iswordc(ptr[col]))))
3539 {
3540 /*
3541 * didn't find an identifier or string
3542 */
3543 if (find_type & FIND_STRING)
3544 EMSG(_("E348: No string under cursor"));
3545 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003546 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 return 0;
3548 }
3549 ptr += col;
3550 *string = ptr;
3551
3552 /*
3553 * 3. Find the end if the identifier/string.
3554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 bn = 0;
3556 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 col = 0;
3558#ifdef FEAT_MBYTE
3559 if (has_mbyte)
3560 {
3561 /* Search for point of changing multibyte character class. */
3562 this_class = mb_get_class(ptr);
3563 while (ptr[col] != NUL
3564 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3565 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 || ((find_type & FIND_EVAL)
3567 && col <= (int)startcol
3568 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003570 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573#endif
3574 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003575 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 || ((find_type & FIND_EVAL)
3577 && col <= (int)startcol
3578 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 )
3580 {
3581 ++col;
3582 }
3583
3584 return col;
3585}
3586
3587/*
3588 * Prepare for redo of a normal command.
3589 */
3590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003591prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
3593 prep_redo(cap->oap->regname, cap->count0,
3594 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3595}
3596
3597/*
3598 * Prepare for redo of any command.
3599 * Note that only the last argument can be a multi-byte char.
3600 */
3601 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003602prep_redo(
3603 int regname,
3604 long num,
3605 int cmd1,
3606 int cmd2,
3607 int cmd3,
3608 int cmd4,
3609 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 ResetRedobuff();
3612 if (regname != 0) /* yank from specified buffer */
3613 {
3614 AppendCharToRedobuff('"');
3615 AppendCharToRedobuff(regname);
3616 }
3617 if (num)
3618 AppendNumberToRedobuff(num);
3619
3620 if (cmd1 != NUL)
3621 AppendCharToRedobuff(cmd1);
3622 if (cmd2 != NUL)
3623 AppendCharToRedobuff(cmd2);
3624 if (cmd3 != NUL)
3625 AppendCharToRedobuff(cmd3);
3626 if (cmd4 != NUL)
3627 AppendCharToRedobuff(cmd4);
3628 if (cmd5 != NUL)
3629 AppendCharToRedobuff(cmd5);
3630}
3631
3632/*
3633 * check for operator active and clear it
3634 *
3635 * return TRUE if operator was active
3636 */
3637 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 if (oap->op_type == OP_NOP)
3641 return FALSE;
3642 clearopbeep(oap);
3643 return TRUE;
3644}
3645
3646/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003647 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003649 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
3651 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003652checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003654 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return FALSE;
3656 clearopbeep(oap);
3657 return TRUE;
3658}
3659
3660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003661clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 oap->op_type = OP_NOP;
3664 oap->regname = 0;
3665 oap->motion_force = NUL;
3666 oap->use_reg_one = FALSE;
3667}
3668
3669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003670clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 clearop(oap);
3673 beep_flush();
3674}
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676/*
3677 * Remove the shift modifier from a special key.
3678 */
3679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003680unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
3682 switch (cap->cmdchar)
3683 {
3684 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3685 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3686 case K_S_UP: cap->cmdchar = K_UP; break;
3687 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3688 case K_S_HOME: cap->cmdchar = K_HOME; break;
3689 case K_S_END: cap->cmdchar = K_END; break;
3690 }
3691 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003694/*
3695 * If the mode is currently displayed clear the command line or update the
3696 * command displayed.
3697 */
3698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003699may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003700{
3701 if (mode_displayed)
3702 clear_cmdline = TRUE; /* unshow visual mode later */
3703#ifdef FEAT_CMDL_INFO
3704 else
3705 clear_showcmd();
3706#endif
3707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3710/*
3711 * Routines for displaying a partly typed command
3712 */
3713
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003714#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715static char_u showcmd_buf[SHOWCMD_BUFLEN];
3716static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3717static int showcmd_is_clear = TRUE;
3718static int showcmd_visual = FALSE;
3719
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003720static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003723clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 if (!p_sc)
3726 return;
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (VIsual_active && !char_avail())
3729 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003730 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 long lines;
3732 colnr_T leftcol, rightcol;
3733 linenr_T top, bot;
3734
3735 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003736 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 top = VIsual.lnum;
3739 bot = curwin->w_cursor.lnum;
3740 }
3741 else
3742 {
3743 top = curwin->w_cursor.lnum;
3744 bot = VIsual.lnum;
3745 }
3746# ifdef FEAT_FOLDING
3747 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003748 (void)hasFolding(top, &top, NULL);
3749 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750# endif
3751 lines = bot - top + 1;
3752
3753 if (VIsual_mode == Ctrl_V)
3754 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003755# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003756 char_u *saved_sbr = p_sbr;
3757
3758 /* Make 'sbr' empty for a moment to get the correct size. */
3759 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003762# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003763 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003764# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3766 (long)(rightcol - leftcol + 1));
3767 }
3768 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3769 sprintf((char *)showcmd_buf, "%ld", lines);
3770 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003771 {
3772 char_u *s, *e;
3773 int l;
3774 int bytes = 0;
3775 int chars = 0;
3776
3777 if (cursor_bot)
3778 {
3779 s = ml_get_pos(&VIsual);
3780 e = ml_get_cursor();
3781 }
3782 else
3783 {
3784 s = ml_get_cursor();
3785 e = ml_get_pos(&VIsual);
3786 }
3787 while ((*p_sel != 'e') ? s <= e : s < e)
3788 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003789# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003790 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003791# else
3792 l = (*s == NUL) ? 0 : 1;
3793# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003794 if (l == 0)
3795 {
3796 ++bytes;
3797 ++chars;
3798 break; /* end of line */
3799 }
3800 bytes += l;
3801 ++chars;
3802 s += l;
3803 }
3804 if (bytes == chars)
3805 sprintf((char *)showcmd_buf, "%d", chars);
3806 else
3807 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3810 showcmd_visual = TRUE;
3811 }
3812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814 showcmd_buf[0] = NUL;
3815 showcmd_visual = FALSE;
3816
3817 /* Don't actually display something if there is nothing to clear. */
3818 if (showcmd_is_clear)
3819 return;
3820 }
3821
3822 display_showcmd();
3823}
3824
3825/*
3826 * Add 'c' to string of shown command chars.
3827 * Return TRUE if output has been written (and setcursor() has been called).
3828 */
3829 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003830add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 char_u *p;
3833 int old_len;
3834 int extra_len;
3835 int overflow;
3836#if defined(FEAT_MOUSE)
3837 int i;
3838 static int ignore[] =
3839 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003840# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3842 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003843# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003844 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003845 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3847 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003848 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003850 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 0
3852 };
3853#endif
3854
Bram Moolenaar09df3122006-01-23 22:23:09 +00003855 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 return FALSE;
3857
3858 if (showcmd_visual)
3859 {
3860 showcmd_buf[0] = NUL;
3861 showcmd_visual = FALSE;
3862 }
3863
3864#if defined(FEAT_MOUSE)
3865 /* Ignore keys that are scrollbar updates and mouse clicks */
3866 if (IS_SPECIAL(c))
3867 for (i = 0; ignore[i] != 0; ++i)
3868 if (ignore[i] == c)
3869 return FALSE;
3870#endif
3871
3872 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003873 if (*p == ' ')
3874 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 old_len = (int)STRLEN(showcmd_buf);
3876 extra_len = (int)STRLEN(p);
3877 overflow = old_len + extra_len - SHOWCMD_COLS;
3878 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003879 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3880 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 STRCAT(showcmd_buf, p);
3882
3883 if (char_avail())
3884 return FALSE;
3885
3886 display_showcmd();
3887
3888 return TRUE;
3889}
3890
3891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003892add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 if (!add_to_showcmd(c))
3895 setcursor();
3896}
3897
3898/*
3899 * Delete 'len' characters from the end of the shown command.
3900 */
3901 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003902del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 int old_len;
3905
3906 if (!p_sc)
3907 return;
3908
3909 old_len = (int)STRLEN(showcmd_buf);
3910 if (len > old_len)
3911 len = old_len;
3912 showcmd_buf[old_len - len] = NUL;
3913
3914 if (!char_avail())
3915 display_showcmd();
3916}
3917
3918/*
3919 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3920 * something and there is a partial mapping.
3921 */
3922 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003923push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924{
3925 if (p_sc)
3926 STRCPY(old_showcmd_buf, showcmd_buf);
3927}
3928
3929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003930pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
3932 if (!p_sc)
3933 return;
3934
3935 STRCPY(showcmd_buf, old_showcmd_buf);
3936
3937 display_showcmd();
3938}
3939
3940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003941display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 int len;
3944
3945 cursor_off();
3946
3947 len = (int)STRLEN(showcmd_buf);
3948 if (len == 0)
3949 showcmd_is_clear = TRUE;
3950 else
3951 {
3952 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3953 showcmd_is_clear = FALSE;
3954 }
3955
3956 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003957 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3958 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
3960 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3961
3962 setcursor(); /* put cursor back where it belongs */
3963}
3964#endif
3965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966/*
3967 * When "check" is FALSE, prepare for commands that scroll the window.
3968 * When "check" is TRUE, take care of scroll-binding after the window has
3969 * scrolled. Called from normal_cmd() and edit().
3970 */
3971 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003972do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
3974 static win_T *old_curwin = NULL;
3975 static linenr_T old_topline = 0;
3976#ifdef FEAT_DIFF
3977 static int old_topfill = 0;
3978#endif
3979 static buf_T *old_buf = NULL;
3980 static colnr_T old_leftcol = 0;
3981
3982 if (check && curwin->w_p_scb)
3983 {
3984 /* If a ":syncbind" command was just used, don't scroll, only reset
3985 * the values. */
3986 if (did_syncbind)
3987 did_syncbind = FALSE;
3988 else if (curwin == old_curwin)
3989 {
3990 /*
3991 * Synchronize other windows, as necessary according to
3992 * 'scrollbind'. Don't do this after an ":edit" command, except
3993 * when 'diff' is set.
3994 */
3995 if ((curwin->w_buffer == old_buf
3996#ifdef FEAT_DIFF
3997 || curwin->w_p_diff
3998#endif
3999 )
4000 && (curwin->w_topline != old_topline
4001#ifdef FEAT_DIFF
4002 || curwin->w_topfill != old_topfill
4003#endif
4004 || curwin->w_leftcol != old_leftcol))
4005 {
4006 check_scrollbind(curwin->w_topline - old_topline,
4007 (long)(curwin->w_leftcol - old_leftcol));
4008 }
4009 }
4010 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4011 {
4012 /*
4013 * When switching between windows, make sure that the relative
4014 * vertical offset is valid for the new window. The relative
4015 * offset is invalid whenever another 'scrollbind' window has
4016 * scrolled to a point that would force the current window to
4017 * scroll past the beginning or end of its buffer. When the
4018 * resync is performed, some of the other 'scrollbind' windows may
4019 * need to jump so that the current window's relative position is
4020 * visible on-screen.
4021 */
4022 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4023 }
4024 curwin->w_scbind_pos = curwin->w_topline;
4025 }
4026
4027 old_curwin = curwin;
4028 old_topline = curwin->w_topline;
4029#ifdef FEAT_DIFF
4030 old_topfill = curwin->w_topfill;
4031#endif
4032 old_buf = curwin->w_buffer;
4033 old_leftcol = curwin->w_leftcol;
4034}
4035
4036/*
4037 * Synchronize any windows that have "scrollbind" set, based on the
4038 * number of rows by which the current window has changed
4039 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4040 */
4041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004042check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 int want_ver;
4045 int want_hor;
4046 win_T *old_curwin = curwin;
4047 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 int old_VIsual_select = VIsual_select;
4049 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 colnr_T tgt_leftcol = curwin->w_leftcol;
4051 long topline;
4052 long y;
4053
4054 /*
4055 * check 'scrollopt' string for vertical and horizontal scroll options
4056 */
4057 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4058#ifdef FEAT_DIFF
4059 want_ver |= old_curwin->w_p_diff;
4060#endif
4061 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4062
4063 /*
4064 * loop through the scrollbound windows and scroll accordingly
4065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004067 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 curbuf = curwin->w_buffer;
4070 /* skip original window and windows with 'noscrollbind' */
4071 if (curwin != old_curwin && curwin->w_p_scb)
4072 {
4073 /*
4074 * do the vertical scroll
4075 */
4076 if (want_ver)
4077 {
4078#ifdef FEAT_DIFF
4079 if (old_curwin->w_p_diff && curwin->w_p_diff)
4080 {
4081 diff_set_topline(old_curwin, curwin);
4082 }
4083 else
4084#endif
4085 {
4086 curwin->w_scbind_pos += topline_diff;
4087 topline = curwin->w_scbind_pos;
4088 if (topline > curbuf->b_ml.ml_line_count)
4089 topline = curbuf->b_ml.ml_line_count;
4090 if (topline < 1)
4091 topline = 1;
4092
4093 y = topline - curwin->w_topline;
4094 if (y > 0)
4095 scrollup(y, FALSE);
4096 else
4097 scrolldown(-y, FALSE);
4098 }
4099
4100 redraw_later(VALID);
4101 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104
4105 /*
4106 * do the horizontal scroll
4107 */
4108 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4109 {
4110 curwin->w_leftcol = tgt_leftcol;
4111 leftcol_changed();
4112 }
4113 }
4114 }
4115
4116 /*
4117 * reset current-window
4118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 VIsual_select = old_VIsual_select;
4120 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 curwin = old_curwin;
4122 curbuf = old_curbuf;
4123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125/*
4126 * Command character that's ignored.
4127 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004128 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004131nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004133 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134}
4135
4136/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004137 * Command character that doesn't do anything, but unlike nv_ignore() does
4138 * start edit(). Used for "startinsert" executed while starting up.
4139 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004140 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004141nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004142{
4143}
4144
4145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * Command character doesn't exist.
4147 */
4148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004149nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 clearopbeep(cap->oap);
4152}
4153
4154/*
4155 * <Help> and <F1> commands.
4156 */
4157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004158nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 if (!checkclearopq(cap->oap))
4161 ex_help(NULL);
4162}
4163
4164/*
4165 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4166 */
4167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004168nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004170#ifdef FEAT_JOB_CHANNEL
4171 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4172 clearopbeep(cap->oap);
4173 else
4174#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004175 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004176 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004177 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004178 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4179 op_addsub(cap->oap, cap->count1, cap->arg);
4180 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004181 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004182 else if (VIsual_active)
4183 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004184 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004185 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186}
4187
4188/*
4189 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4190 */
4191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004192nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004195 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004196 if (mod_mask & MOD_MASK_CTRL)
4197 {
4198 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4199 if (cap->arg == BACKWARD)
4200 goto_tabpage(-(int)cap->count1);
4201 else
4202 goto_tabpage((int)cap->count0);
4203 }
4204 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004205 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
4209/*
4210 * Implementation of "gd" and "gD" command.
4211 */
4212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004213nv_gd(
4214 oparg_T *oap,
4215 int nchar,
4216 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
4218 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u *ptr;
4220
Bram Moolenaard9d30582005-05-18 22:10:28 +00004221 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004222 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4223 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004225#ifdef FEAT_FOLDING
4226 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4227 foldOpenCursor();
4228#endif
4229}
4230
4231/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004232 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4233 * otherwise.
4234 */
4235 static int
4236is_ident(char_u *line, int offset)
4237{
4238 int i;
4239 int incomment = FALSE;
4240 int instring = 0;
4241 int prev = 0;
4242
4243 for (i = 0; i < offset && line[i] != NUL; i++)
4244 {
4245 if (instring != 0)
4246 {
4247 if (prev != '\\' && line[i] == instring)
4248 instring = 0;
4249 }
4250 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4251 {
4252 instring = line[i];
4253 }
4254 else
4255 {
4256 if (incomment)
4257 {
4258 if (prev == '*' && line[i] == '/')
4259 incomment = FALSE;
4260 }
4261 else if (prev == '/' && line[i] == '*')
4262 {
4263 incomment = TRUE;
4264 }
4265 else if (prev == '/' && line[i] == '/')
4266 {
4267 return FALSE;
4268 }
4269 }
4270
4271 prev = line[i];
4272 }
4273
4274 return incomment == FALSE && instring == 0;
4275}
4276
4277/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004278 * Search for variable declaration of "ptr[len]".
4279 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4280 * current file ("gD").
4281 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004282 * Return FAIL when not found.
4283 */
4284 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004285find_decl(
4286 char_u *ptr,
4287 int len,
4288 int locally,
4289 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004290 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004291{
4292 char_u *pat;
4293 pos_T old_pos;
4294 pos_T par_pos;
4295 pos_T found_pos;
4296 int t;
4297 int save_p_ws;
4298 int save_p_scs;
4299 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004300 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004301 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004302 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303
4304 if ((pat = alloc(len + 7)) == NULL)
4305 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004306
4307 /* Put "\V" before the pattern to avoid that the special meaning of "."
4308 * and "~" causes trouble. */
4309 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4310 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 old_pos = curwin->w_cursor;
4312 save_p_ws = p_ws;
4313 save_p_scs = p_scs;
4314 p_ws = FALSE; /* don't wrap around end of file now */
4315 p_scs = FALSE; /* don't switch ignorecase off now */
4316
4317 /*
4318 * With "gD" go to line 1.
4319 * With "gd" Search back for the start of the current function, then go
4320 * back until a blank line. If this fails go to line 1.
4321 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004322 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
4324 setpcmark(); /* Set in findpar() otherwise */
4325 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004326 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004330 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4332 --curwin->w_cursor.lnum;
4333 }
4334 curwin->w_cursor.col = 0;
4335
4336 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004337 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004338 for (;;)
4339 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004340 valid = FALSE;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004341 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004342 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004343 if (curwin->w_cursor.lnum >= old_pos.lnum)
4344 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004345
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004346 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004347 {
4348 pos_T *pos;
4349
4350 /* Check that the block the match is in doesn't end before the
4351 * position where we started the search from. */
4352 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4353 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4354 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004355 {
4356 /* There can't be a useful match before the end of this block.
4357 * Skip to the end. */
4358 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004359 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004360 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004361 }
4362
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004363 if (t == FAIL)
4364 {
4365 /* If we previously found a valid position, use it. */
4366 if (found_pos.lnum != 0)
4367 {
4368 curwin->w_cursor = found_pos;
4369 t = OK;
4370 }
4371 break;
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004374 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 {
4376 /* Ignore this line, continue at start of next line. */
4377 ++curwin->w_cursor.lnum;
4378 curwin->w_cursor.col = 0;
4379 continue;
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004382 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4383
4384 /* If the current position is not a valid identifier and a previous
4385 * match is present, favor that one instead. */
4386 if (!valid && found_pos.lnum != 0)
4387 {
4388 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004389 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004390 }
4391
4392 /* Global search: use first valid match found */
4393 if (valid && !locally)
4394 break;
4395 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004396 {
4397 /* If we previously found a valid position, use it. */
4398 if (found_pos.lnum != 0)
4399 curwin->w_cursor = found_pos;
4400 break;
4401 }
4402
Bram Moolenaar226630a2016-10-08 19:21:31 +02004403 /* For finding a local variable and the match is before the "{" or
4404 * inside a comment, continue searching. For K&R style function
4405 * declarations this skips the function header without types. */
4406 if (!valid)
4407 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004408 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004409 }
4410 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004411 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004412 /* Remove SEARCH_START from flags to avoid getting stuck at one
4413 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004414 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416
4417 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004419 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 curwin->w_cursor = old_pos;
4421 }
4422 else
4423 {
4424 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 /* "n" searches forward now */
4426 reset_search_dir();
4427 }
4428
4429 vim_free(pat);
4430 p_ws = save_p_ws;
4431 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004432
4433 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434}
4435
4436/*
4437 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4438 * lines rather than lines in the file.
4439 * 'dist' must be positive.
4440 *
4441 * Return OK if able to move cursor, FAIL otherwise.
4442 */
4443 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004444nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446 int linelen = linetabsize(ml_get_curline());
4447 int retval = OK;
4448 int atend = FALSE;
4449 int n;
4450 int col_off1; /* margin offset for first screen line */
4451 int col_off2; /* margin offset for wrapped screen line */
4452 int width1; /* text width for first screen line */
4453 int width2; /* test width for wrapped screen line */
4454
4455 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004456 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 col_off1 = curwin_col_off();
4459 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004460 width1 = curwin->w_width - col_off1;
4461 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004462 if (width2 == 0)
4463 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 /*
4468 * Instead of sticking at the last character of the buffer line we
4469 * try to stick in the last column of the screen.
4470 */
4471 if (curwin->w_curswant == MAXCOL)
4472 {
4473 atend = TRUE;
4474 validate_virtcol();
4475 if (width1 <= 0)
4476 curwin->w_curswant = 0;
4477 else
4478 {
4479 curwin->w_curswant = width1 - 1;
4480 if (curwin->w_virtcol > curwin->w_curswant)
4481 curwin->w_curswant += ((curwin->w_virtcol
4482 - curwin->w_curswant - 1) / width2 + 1) * width2;
4483 }
4484 }
4485 else
4486 {
4487 if (linelen > width1)
4488 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4489 else
4490 n = width1;
4491 if (curwin->w_curswant > (colnr_T)n + 1)
4492 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4493 * width2;
4494 }
4495
4496 while (dist--)
4497 {
4498 if (dir == BACKWARD)
4499 {
4500 if ((long)curwin->w_curswant >= width2)
4501 /* move back within line */
4502 curwin->w_curswant -= width2;
4503 else
4504 {
4505 /* to previous line */
4506 if (curwin->w_cursor.lnum == 1)
4507 {
4508 retval = FAIL;
4509 break;
4510 }
4511 --curwin->w_cursor.lnum;
4512#ifdef FEAT_FOLDING
4513 /* Move to the start of a closed fold. Don't do that when
4514 * 'foldopen' contains "all": it will open in a moment. */
4515 if (!(fdo_flags & FDO_ALL))
4516 (void)hasFolding(curwin->w_cursor.lnum,
4517 &curwin->w_cursor.lnum, NULL);
4518#endif
4519 linelen = linetabsize(ml_get_curline());
4520 if (linelen > width1)
4521 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4522 + 1) * width2;
4523 }
4524 }
4525 else /* dir == FORWARD */
4526 {
4527 if (linelen > width1)
4528 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4529 else
4530 n = width1;
4531 if (curwin->w_curswant + width2 < (colnr_T)n)
4532 /* move forward within line */
4533 curwin->w_curswant += width2;
4534 else
4535 {
4536 /* to next line */
4537#ifdef FEAT_FOLDING
4538 /* Move to the end of a closed fold. */
4539 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4540 &curwin->w_cursor.lnum);
4541#endif
4542 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4543 {
4544 retval = FAIL;
4545 break;
4546 }
4547 curwin->w_cursor.lnum++;
4548 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004549 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 }
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004555 if (virtual_active() && atend)
4556 coladvance(MAXCOL);
4557 else
4558 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4561 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4562 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004563 colnr_T virtcol;
4564
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 /*
4566 * Check for landing on a character that got split at the end of the
4567 * last line. We want to advance a screenline, not end up in the same
4568 * screenline or move two screenlines.
4569 */
4570 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004571 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004572# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004573 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4574 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004575# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004576
4577 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 && (curwin->w_curswant < (colnr_T)width1
4579 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4580 : ((curwin->w_curswant - width1) % width2
4581 > (colnr_T)width2 / 2)))
4582 --curwin->w_cursor.col;
4583 }
4584#endif
4585
4586 if (atend)
4587 curwin->w_curswant = MAXCOL; /* stick in the last column */
4588
4589 return retval;
4590}
4591
4592#ifdef FEAT_MOUSE
4593/*
4594 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4595 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004596 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4597 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 */
4599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004600nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004602 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004604 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
4606 int row, col;
4607
4608 row = mouse_row;
4609 col = mouse_col;
4610
4611 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004612 wp = mouse_find_win(&row, &col);
4613 if (wp == NULL)
4614 return;
4615 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 curbuf = curwin->w_buffer;
4617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004619 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004621# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004622 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004623 /* This window is a terminal window, send the mouse event there.
4624 * Set "typed" to FALSE to avoid an endless loop. */
4625 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004626 else
4627# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4629 {
4630 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4631 }
4632 else
4633 {
4634 cap->count1 = 3;
4635 cap->count0 = 3;
4636 nv_scroll_line(cap);
4637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004639# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 else
4641 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004642 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4643 if (!curwin->w_p_wrap)
4644 {
4645 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004646
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004647 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004648 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004649 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4650 if (val < 0)
4651 val = 0;
4652
4653 gui_do_horiz_scroll(val, TRUE);
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 curwin->w_redr_status = TRUE;
4659
4660 curwin = old_curwin;
4661 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662}
4663
4664/*
4665 * Mouse clicks and drags.
4666 */
4667 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004668nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669{
4670 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4671}
4672#endif
4673
4674/*
4675 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4676 * cap->arg must be TRUE for CTRL-E.
4677 */
4678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 if (!checkclearop(cap->oap))
4682 scroll_redraw(cap->arg, cap->count1);
4683}
4684
4685/*
4686 * Scroll "count" lines up or down, and redraw.
4687 */
4688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 linenr_T prev_topline = curwin->w_topline;
4692#ifdef FEAT_DIFF
4693 int prev_topfill = curwin->w_topfill;
4694#endif
4695 linenr_T prev_lnum = curwin->w_cursor.lnum;
4696
4697 if (up)
4698 scrollup(count, TRUE);
4699 else
4700 scrolldown(count, TRUE);
4701 if (p_so)
4702 {
4703 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4704 * valid, otherwise the screen jumps back at the end of the file. */
4705 cursor_correct();
4706 check_cursor_moved(curwin);
4707 curwin->w_valid |= VALID_TOPLINE;
4708
4709 /* If moved back to where we were, at least move the cursor, otherwise
4710 * we get stuck at one position. Don't move the cursor up if the
4711 * first line of the buffer is already on the screen */
4712 while (curwin->w_topline == prev_topline
4713#ifdef FEAT_DIFF
4714 && curwin->w_topfill == prev_topfill
4715#endif
4716 )
4717 {
4718 if (up)
4719 {
4720 if (curwin->w_cursor.lnum > prev_lnum
4721 || cursor_down(1L, FALSE) == FAIL)
4722 break;
4723 }
4724 else
4725 {
4726 if (curwin->w_cursor.lnum < prev_lnum
4727 || prev_topline == 1L
4728 || cursor_up(1L, FALSE) == FAIL)
4729 break;
4730 }
4731 /* Mark w_topline as valid, otherwise the screen jumps back at the
4732 * end of the file. */
4733 check_cursor_moved(curwin);
4734 curwin->w_valid |= VALID_TOPLINE;
4735 }
4736 }
4737 if (curwin->w_cursor.lnum != prev_lnum)
4738 coladvance(curwin->w_curswant);
4739 redraw_later(VALID);
4740}
4741
4742/*
4743 * Commands that start with "z".
4744 */
4745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 long n;
4749 colnr_T col;
4750 int nchar = cap->nchar;
4751#ifdef FEAT_FOLDING
4752 long old_fdl = curwin->w_p_fdl;
4753 int old_fen = curwin->w_p_fen;
4754#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004755#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004756 int undo = FALSE;
4757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 if (VIM_ISDIGIT(nchar))
4760 {
4761 /*
4762 * "z123{nchar}": edit the count before obtaining {nchar}
4763 */
4764 if (checkclearop(cap->oap))
4765 return;
4766 n = nchar - '0';
4767 for (;;)
4768 {
4769#ifdef USE_ON_FLY_SCROLL
4770 dont_scroll = TRUE; /* disallow scrolling here */
4771#endif
4772 ++no_mapping;
4773 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004774 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 --no_mapping;
4777 --allow_keys;
4778#ifdef FEAT_CMDL_INFO
4779 (void)add_to_showcmd(nchar);
4780#endif
4781 if (nchar == K_DEL || nchar == K_KDEL)
4782 n /= 10;
4783 else if (VIM_ISDIGIT(nchar))
4784 n = n * 10 + (nchar - '0');
4785 else if (nchar == CAR)
4786 {
4787#ifdef FEAT_GUI
4788 need_mouse_correct = TRUE;
4789#endif
4790 win_setheight((int)n);
4791 break;
4792 }
4793 else if (nchar == 'l'
4794 || nchar == 'h'
4795 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004796 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 cap->count1 = n ? n * cap->count1 : cap->count1;
4799 goto dozet;
4800 }
4801 else
4802 {
4803 clearopbeep(cap->oap);
4804 break;
4805 }
4806 }
4807 cap->oap->op_type = OP_NOP;
4808 return;
4809 }
4810
4811dozet:
4812 if (
4813#ifdef FEAT_FOLDING
4814 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4815 * and "zC" only in Visual mode. "zj" and "zk" are motion
4816 * commands. */
4817 cap->nchar != 'f' && cap->nchar != 'F'
4818 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4819 && cap->nchar != 'j' && cap->nchar != 'k'
4820 &&
4821#endif
4822 checkclearop(cap->oap))
4823 return;
4824
4825 /*
4826 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4827 * If line number given, set cursor.
4828 */
4829 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4830 && cap->count0
4831 && cap->count0 != curwin->w_cursor.lnum)
4832 {
4833 setpcmark();
4834 if (cap->count0 > curbuf->b_ml.ml_line_count)
4835 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4836 else
4837 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004838 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
4840
4841 switch (nchar)
4842 {
4843 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4844 case '+':
4845 if (cap->count0 == 0)
4846 {
4847 /* No count given: put cursor at the line below screen */
4848 validate_botline(); /* make sure w_botline is valid */
4849 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4850 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4851 else
4852 curwin->w_cursor.lnum = curwin->w_botline;
4853 }
4854 /* FALLTHROUGH */
4855 case NL:
4856 case CAR:
4857 case K_KENTER:
4858 beginline(BL_WHITE | BL_FIX);
4859 /* FALLTHROUGH */
4860
4861 case 't': scroll_cursor_top(0, TRUE);
4862 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004863 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 break;
4865
4866 /* "z." and "zz": put cursor in middle of screen */
4867 case '.': beginline(BL_WHITE | BL_FIX);
4868 /* FALLTHROUGH */
4869
4870 case 'z': scroll_cursor_halfway(TRUE);
4871 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004872 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 break;
4874
4875 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4876 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4877 * when <count> is at bottom of window, and puts that one at
4878 * bottom of window. */
4879 if (cap->count0 != 0)
4880 {
4881 scroll_cursor_bot(0, TRUE);
4882 curwin->w_cursor.lnum = curwin->w_topline;
4883 }
4884 else if (curwin->w_topline == 1)
4885 curwin->w_cursor.lnum = 1;
4886 else
4887 curwin->w_cursor.lnum = curwin->w_topline - 1;
4888 /* FALLTHROUGH */
4889 case '-':
4890 beginline(BL_WHITE | BL_FIX);
4891 /* FALLTHROUGH */
4892
4893 case 'b': scroll_cursor_bot(0, TRUE);
4894 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004895 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 break;
4897
4898 /* "zH" - scroll screen right half-page */
4899 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004900 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 /* FALLTHROUGH */
4902
4903 /* "zh" - scroll screen to the right */
4904 case 'h':
4905 case K_LEFT:
4906 if (!curwin->w_p_wrap)
4907 {
4908 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4909 curwin->w_leftcol = 0;
4910 else
4911 curwin->w_leftcol -= (colnr_T)cap->count1;
4912 leftcol_changed();
4913 }
4914 break;
4915
4916 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004917 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 /* FALLTHROUGH */
4919
4920 /* "zl" - scroll screen to the left */
4921 case 'l':
4922 case K_RIGHT:
4923 if (!curwin->w_p_wrap)
4924 {
4925 /* scroll the window left */
4926 curwin->w_leftcol += (colnr_T)cap->count1;
4927 leftcol_changed();
4928 }
4929 break;
4930
4931 /* "zs" - scroll screen, cursor at the start */
4932 case 's': if (!curwin->w_p_wrap)
4933 {
4934#ifdef FEAT_FOLDING
4935 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4936 col = 0; /* like the cursor is in col 0 */
4937 else
4938#endif
4939 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4940 if ((long)col > p_siso)
4941 col -= p_siso;
4942 else
4943 col = 0;
4944 if (curwin->w_leftcol != col)
4945 {
4946 curwin->w_leftcol = col;
4947 redraw_later(NOT_VALID);
4948 }
4949 }
4950 break;
4951
4952 /* "ze" - scroll screen, cursor at the end */
4953 case 'e': if (!curwin->w_p_wrap)
4954 {
4955#ifdef FEAT_FOLDING
4956 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4957 col = 0; /* like the cursor is in col 0 */
4958 else
4959#endif
4960 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004961 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if ((long)col + p_siso < n)
4963 col = 0;
4964 else
4965 col = col + p_siso - n + 1;
4966 if (curwin->w_leftcol != col)
4967 {
4968 curwin->w_leftcol = col;
4969 redraw_later(NOT_VALID);
4970 }
4971 }
4972 break;
4973
4974#ifdef FEAT_FOLDING
4975 /* "zF": create fold command */
4976 /* "zf": create fold operator */
4977 case 'F':
4978 case 'f': if (foldManualAllowed(TRUE))
4979 {
4980 cap->nchar = 'f';
4981 nv_operator(cap);
4982 curwin->w_p_fen = TRUE;
4983
4984 /* "zF" is like "zfzf" */
4985 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4986 {
4987 nv_operator(cap);
4988 finish_op = TRUE;
4989 }
4990 }
4991 else
4992 clearopbeep(cap->oap);
4993 break;
4994
4995 /* "zd": delete fold at cursor */
4996 /* "zD": delete fold at cursor recursively */
4997 case 'd':
4998 case 'D': if (foldManualAllowed(FALSE))
4999 {
5000 if (VIsual_active)
5001 nv_operator(cap);
5002 else
5003 deleteFold(curwin->w_cursor.lnum,
5004 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5005 }
5006 break;
5007
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005008 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 case 'E': if (foldmethodIsManual(curwin))
5010 {
5011 clearFolding(curwin);
5012 changed_window_setting();
5013 }
5014 else if (foldmethodIsMarker(curwin))
5015 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5016 TRUE, FALSE);
5017 else
5018 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5019 break;
5020
5021 /* "zn": fold none: reset 'foldenable' */
5022 case 'n': curwin->w_p_fen = FALSE;
5023 break;
5024
5025 /* "zN": fold Normal: set 'foldenable' */
5026 case 'N': curwin->w_p_fen = TRUE;
5027 break;
5028
5029 /* "zi": invert folding: toggle 'foldenable' */
5030 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5031 break;
5032
5033 /* "za": open closed fold or close open fold at cursor */
5034 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5035 openFold(curwin->w_cursor.lnum, cap->count1);
5036 else
5037 {
5038 closeFold(curwin->w_cursor.lnum, cap->count1);
5039 curwin->w_p_fen = TRUE;
5040 }
5041 break;
5042
5043 /* "zA": open fold at cursor recursively */
5044 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5045 openFoldRecurse(curwin->w_cursor.lnum);
5046 else
5047 {
5048 closeFoldRecurse(curwin->w_cursor.lnum);
5049 curwin->w_p_fen = TRUE;
5050 }
5051 break;
5052
5053 /* "zo": open fold at cursor or Visual area */
5054 case 'o': if (VIsual_active)
5055 nv_operator(cap);
5056 else
5057 openFold(curwin->w_cursor.lnum, cap->count1);
5058 break;
5059
5060 /* "zO": open fold recursively */
5061 case 'O': if (VIsual_active)
5062 nv_operator(cap);
5063 else
5064 openFoldRecurse(curwin->w_cursor.lnum);
5065 break;
5066
5067 /* "zc": close fold at cursor or Visual area */
5068 case 'c': if (VIsual_active)
5069 nv_operator(cap);
5070 else
5071 closeFold(curwin->w_cursor.lnum, cap->count1);
5072 curwin->w_p_fen = TRUE;
5073 break;
5074
5075 /* "zC": close fold recursively */
5076 case 'C': if (VIsual_active)
5077 nv_operator(cap);
5078 else
5079 closeFoldRecurse(curwin->w_cursor.lnum);
5080 curwin->w_p_fen = TRUE;
5081 break;
5082
5083 /* "zv": open folds at the cursor */
5084 case 'v': foldOpenCursor();
5085 break;
5086
5087 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5088 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005089 curwin->w_foldinvalid = TRUE; /* recompute folds */
5090 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 foldOpenCursor();
5092 break;
5093
5094 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5095 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005096 curwin->w_foldinvalid = TRUE; /* recompute folds */
5097 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /* "zm": fold more */
5101 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005102 {
5103 curwin->w_p_fdl -= cap->count1;
5104 if (curwin->w_p_fdl < 0)
5105 curwin->w_p_fdl = 0;
5106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 old_fdl = -1; /* force an update */
5108 curwin->w_p_fen = TRUE;
5109 break;
5110
5111 /* "zM": close all folds */
5112 case 'M': curwin->w_p_fdl = 0;
5113 old_fdl = -1; /* force an update */
5114 curwin->w_p_fen = TRUE;
5115 break;
5116
5117 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005118 case 'r': curwin->w_p_fdl += cap->count1;
5119 {
5120 int d = getDeepestNesting();
5121
5122 if (curwin->w_p_fdl >= d)
5123 curwin->w_p_fdl = d;
5124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 break;
5126
5127 /* "zR": open all folds */
5128 case 'R': curwin->w_p_fdl = getDeepestNesting();
5129 old_fdl = -1; /* force an update */
5130 break;
5131
5132 case 'j': /* "zj" move to next fold downwards */
5133 case 'k': /* "zk" move to next fold upwards */
5134 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5135 cap->count1) == FAIL)
5136 clearopbeep(cap->oap);
5137 break;
5138
5139#endif /* FEAT_FOLDING */
5140
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005141#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005142 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5143 ++no_mapping;
5144 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005145 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005146 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005147 --no_mapping;
5148 --allow_keys;
5149#ifdef FEAT_CMDL_INFO
5150 (void)add_to_showcmd(nchar);
5151#endif
5152 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5153 {
5154 clearopbeep(cap->oap);
5155 break;
5156 }
5157 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005158 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005159
Bram Moolenaarb765d632005-06-07 21:00:02 +00005160 case 'g': /* "zg": add good word to word list */
5161 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005162 case 'G': /* "zG": add good word to temp word list */
5163 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005164 {
5165 char_u *ptr = NULL;
5166 int len;
5167
5168 if (checkclearop(cap->oap))
5169 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5171 == FAIL)
5172 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005173 if (ptr == NULL)
5174 {
5175 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005176
Bram Moolenaar134bf072013-09-25 18:54:24 +02005177 /* Find bad word under the cursor. When 'spell' is
5178 * off this fails and find_ident_under_cursor() is
5179 * used below. */
5180 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005181 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005182 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005183 if (len != 0 && curwin->w_cursor.col <= pos.col)
5184 ptr = ml_get_pos(&curwin->w_cursor);
5185 curwin->w_cursor = pos;
5186 }
5187
Bram Moolenaarb765d632005-06-07 21:00:02 +00005188 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5189 FIND_IDENT)) == 0)
5190 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005191 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005192 (nchar == 'G' || nchar == 'W')
5193 ? 0 : (int)cap->count1,
5194 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005196 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197
Bram Moolenaar43abc522005-12-10 20:15:02 +00005198 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005199 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005200 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202#endif
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 default: clearopbeep(cap->oap);
5205 }
5206
5207#ifdef FEAT_FOLDING
5208 /* Redraw when 'foldenable' changed */
5209 if (old_fen != curwin->w_p_fen)
5210 {
5211# ifdef FEAT_DIFF
5212 win_T *wp;
5213
5214 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5215 {
5216 /* Adjust 'foldenable' in diff-synced windows. */
5217 FOR_ALL_WINDOWS(wp)
5218 {
5219 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5220 {
5221 wp->w_p_fen = curwin->w_p_fen;
5222 changed_window_setting_win(wp);
5223 }
5224 }
5225 }
5226# endif
5227 changed_window_setting();
5228 }
5229
5230 /* Redraw when 'foldlevel' changed. */
5231 if (old_fdl != curwin->w_p_fdl)
5232 newFoldLevel();
5233#endif
5234}
5235
5236#ifdef FEAT_GUI
5237/*
5238 * Vertical scrollbar movement.
5239 */
5240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005241nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242{
5243 if (cap->oap->op_type != OP_NOP)
5244 clearopbeep(cap->oap);
5245
5246 /* Even if an operator was pending, we still want to scroll */
5247 gui_do_scroll();
5248}
5249
5250/*
5251 * Horizontal scrollbar movement.
5252 */
5253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 if (cap->oap->op_type != OP_NOP)
5257 clearopbeep(cap->oap);
5258
5259 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005260 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261}
5262#endif
5263
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005264#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005265/*
5266 * Click in GUI tab.
5267 */
5268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005269nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005270{
5271 if (cap->oap->op_type != OP_NOP)
5272 clearopbeep(cap->oap);
5273
5274 /* Even if an operator was pending, we still want to jump tabs. */
5275 goto_tabpage(current_tab);
5276}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005277
5278/*
5279 * Selected item in tab line menu.
5280 */
5281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005282nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005283{
5284 if (cap->oap->op_type != OP_NOP)
5285 clearopbeep(cap->oap);
5286
5287 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005288 handle_tabmenu();
5289}
5290
5291/*
5292 * Handle selecting an item of the GUI tab line menu.
5293 * Used in Normal and Insert mode.
5294 */
5295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005296handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005297{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005298 switch (current_tabmenu)
5299 {
5300 case TABLINE_MENU_CLOSE:
5301 if (current_tab == 0)
5302 do_cmdline_cmd((char_u *)"tabclose");
5303 else
5304 {
5305 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5306 current_tab);
5307 do_cmdline_cmd(IObuff);
5308 }
5309 break;
5310
5311 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005312 if (current_tab == 0)
5313 do_cmdline_cmd((char_u *)"$tabnew");
5314 else
5315 {
5316 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5317 current_tab - 1);
5318 do_cmdline_cmd(IObuff);
5319 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005320 break;
5321
5322 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005323 if (current_tab == 0)
5324 do_cmdline_cmd((char_u *)"browse $tabnew");
5325 else
5326 {
5327 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5328 current_tab - 1);
5329 do_cmdline_cmd(IObuff);
5330 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005331 break;
5332 }
5333}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005334#endif
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336/*
5337 * "Q" command.
5338 */
5339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 /*
5343 * Ignore 'Q' in Visual mode, just give a beep.
5344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005346 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005347 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 do_exmode(FALSE);
5349}
5350
5351/*
5352 * Handle a ":" command.
5353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005358 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (VIsual_active)
5361 nv_operator(cap);
5362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 if (cap->oap->op_type != OP_NOP)
5365 {
5366 /* Using ":" as a movement is characterwise exclusive. */
5367 cap->oap->motion_type = MCHAR;
5368 cap->oap->inclusive = FALSE;
5369 }
5370 else if (cap->count0)
5371 {
5372 /* translate "count:" into ":.,.+(count - 1)" */
5373 stuffcharReadbuff('.');
5374 if (cap->count0 > 1)
5375 {
5376 stuffReadbuff((char_u *)",.+");
5377 stuffnumReadbuff((long)cap->count0 - 1L);
5378 }
5379 }
5380
5381 /* When typing, don't type below an old message */
5382 if (KeyTyped)
5383 compute_cmdrow();
5384
5385 old_p_im = p_im;
5386
5387 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005388 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5390
5391 /* If 'insertmode' changed, enter or exit Insert mode */
5392 if (p_im != old_p_im)
5393 {
5394 if (p_im)
5395 restart_edit = 'i';
5396 else
5397 restart_edit = 0;
5398 }
5399
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005400 if (cmd_result == FAIL)
5401 /* The Ex command failed, do not execute the operator. */
5402 clearop(cap->oap);
5403 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5405 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005406 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5407 || did_emsg
5408 ))
5409 /* The start of the operator has become invalid by the Ex command.
5410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 clearopbeep(cap->oap);
5412 }
5413}
5414
5415/*
5416 * Handle CTRL-G command.
5417 */
5418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (VIsual_active) /* toggle Selection/Visual mode */
5422 {
5423 VIsual_select = !VIsual_select;
5424 showmode();
5425 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005426 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 /* print full name if count given or :cd used */
5428 fileinfo((int)cap->count0, FALSE, TRUE);
5429}
5430
5431/*
5432 * Handle CTRL-H <Backspace> command.
5433 */
5434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435nv_ctrlh(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 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5440 v_visop(cap);
5441 }
5442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 nv_left(cap);
5444}
5445
5446/*
5447 * CTRL-L: clear screen and redraw.
5448 */
5449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005450nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 if (!checkclearop(cap->oap))
5453 {
5454#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5455 /*
5456 * Right now, the BeBox doesn't seem to have an easy way to detect
5457 * window resizing, so we cheat and make the user detect it
5458 * manually with CTRL-L instead
5459 */
5460 ui_get_shellsize();
5461#endif
5462#ifdef FEAT_SYN_HL
5463 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005464 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005465# ifdef FEAT_RELTIME
5466 {
5467 win_T *wp;
5468
5469 FOR_ALL_WINDOWS(wp)
5470 wp->w_s->b_syn_slow = FALSE;
5471 }
5472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#endif
5474 redraw_later(CLEAR);
5475 }
5476}
5477
5478/*
5479 * CTRL-O: In Select mode: switch to Visual mode for one command.
5480 * Otherwise: Go to older pcmark.
5481 */
5482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005483nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 if (VIsual_active && VIsual_select)
5486 {
5487 VIsual_select = FALSE;
5488 showmode();
5489 restart_VIsual_select = 2; /* restart Select mode later */
5490 }
5491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 cap->count1 = -cap->count1;
5494 nv_pcmark(cap);
5495 }
5496}
5497
5498/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005499 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5500 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 */
5502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005503nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 if (!checkclearopq(cap->oap))
5506 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5507 GETF_SETMARK|GETF_ALT, FALSE);
5508}
5509
5510/*
5511 * "Z" commands.
5512 */
5513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005514nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 if (!checkclearopq(cap->oap))
5517 {
5518 switch (cap->nchar)
5519 {
5520 /* "ZZ": equivalent to ":x". */
5521 case 'Z': do_cmdline_cmd((char_u *)"x");
5522 break;
5523
5524 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5525 case 'Q': do_cmdline_cmd((char_u *)"q!");
5526 break;
5527
5528 default: clearopbeep(cap->oap);
5529 }
5530 }
5531}
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533/*
5534 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5535 */
5536 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 oparg_T oa;
5540 cmdarg_T ca;
5541
5542 clear_oparg(&oa);
5543 vim_memset(&ca, 0, sizeof(ca));
5544 ca.oap = &oa;
5545 ca.cmdchar = c1;
5546 ca.nchar = c2;
5547 nv_ident(&ca);
5548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550/*
5551 * Handle the commands that use the word under the cursor.
5552 * [g] CTRL-] :ta to current identifier
5553 * [g] 'K' run program for current identifier
5554 * [g] '*' / to current identifier or string
5555 * [g] '#' ? to current identifier or string
5556 * g ']' :tselect for current identifier
5557 */
5558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005559nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 char_u *ptr = NULL;
5562 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005563 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005564 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 char_u *p;
5566 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005567 int kp_help; /* 'keywordprg' is ":he" */
5568 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 int n = 0; /* init for GCC */
5570 int cmdchar;
5571 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005572 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 char_u *aux_ptr;
5574 int isman;
5575 int isman_s;
5576
5577 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5578 {
5579 cmdchar = cap->nchar;
5580 g_cmd = TRUE;
5581 }
5582 else
5583 {
5584 cmdchar = cap->cmdchar;
5585 g_cmd = FALSE;
5586 }
5587
5588 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5589 cmdchar = '#';
5590
5591 /*
5592 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5593 */
5594 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 if (checkclearopq(cap->oap))
5599 return;
5600 }
5601
5602 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5603 (cmdchar == '*' || cmdchar == '#')
5604 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5605 {
5606 clearop(cap->oap);
5607 return;
5608 }
5609
5610 /* Allocate buffer to put the command in. Inserting backslashes can
5611 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5612 * and some numbers. */
5613 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5614 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5615 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005616 if (kp_help && *skipwhite(ptr) == NUL)
5617 {
5618 EMSG(_(e_noident)); /* found white space only */
5619 return;
5620 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005621 kp_ex = (*kp == ':');
5622 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5623 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 if (buf == NULL)
5625 return;
5626 buf[0] = NUL;
5627
5628 switch (cmdchar)
5629 {
5630 case '*':
5631 case '#':
5632 /*
5633 * Put cursor at start of word, makes search skip the word
5634 * under the cursor.
5635 * Call setpcmark() first, so "*``" puts the cursor back where
5636 * it was.
5637 */
5638 setpcmark();
5639 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5640
5641 if (!g_cmd && vim_iswordp(ptr))
5642 STRCPY(buf, "\\<");
5643 no_smartcase = TRUE; /* don't use 'smartcase' now */
5644 break;
5645
5646 case 'K':
5647 if (kp_help)
5648 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005649 else if (kp_ex)
5650 {
5651 if (cap->count0 != 0)
5652 vim_snprintf((char *)buf, buflen, "%s %ld",
5653 kp, cap->count0);
5654 else
5655 STRCPY(buf, kp);
5656 STRCAT(buf, " ");
5657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 else
5659 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005660 /* An external command will probably use an argument starting
5661 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005662 while (*ptr == '-' && n > 0)
5663 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005664 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005665 --n;
5666 }
5667 if (n == 0)
5668 {
5669 EMSG(_(e_noident)); /* found dashes only */
5670 vim_free(buf);
5671 return;
5672 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 /* When a count is given, turn it into a range. Is this
5675 * really what we want? */
5676 isman = (STRCMP(kp, "man") == 0);
5677 isman_s = (STRCMP(kp, "man -s") == 0);
5678 if (cap->count0 != 0 && !(isman || isman_s))
5679 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5680
5681 STRCAT(buf, "! ");
5682 if (cap->count0 == 0 && isman_s)
5683 STRCAT(buf, "man");
5684 else
5685 STRCAT(buf, kp);
5686 STRCAT(buf, " ");
5687 if (cap->count0 != 0 && (isman || isman_s))
5688 {
5689 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5690 STRCAT(buf, " ");
5691 }
5692 }
5693 break;
5694
5695 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005696 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697#ifdef FEAT_CSCOPE
5698 if (p_cst)
5699 STRCPY(buf, "cstag ");
5700 else
5701#endif
5702 STRCPY(buf, "ts ");
5703 break;
5704
5705 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005706 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 if (curbuf->b_help)
5708 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005710 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005711 if (g_cmd)
5712 STRCPY(buf, "tj ");
5713 else
5714 sprintf((char *)buf, "%ldta ", cap->count0);
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717
5718 /*
5719 * Now grab the chars in the identifier
5720 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005721 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005723 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005724 if (kp_ex)
5725 /* Escape the argument properly for an Ex command */
5726 p = vim_strsave_fnameescape(ptr, FALSE);
5727 else
5728 /* Escape the argument properly for a shell command */
5729 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005730 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005731 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005733 vim_free(buf);
5734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005736 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5737 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005738 {
5739 vim_free(buf);
5740 vim_free(p);
5741 return;
5742 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005743 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 STRCAT(buf, p);
5745 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 else
5748 {
5749 if (cmdchar == '*')
5750 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5751 else if (cmdchar == '#')
5752 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005753 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005754 {
5755 if (curbuf->b_help)
5756 /* ":help" handles unescaped argument */
5757 aux_ptr = (char_u *)"";
5758 else
5759 aux_ptr = (char_u *)"\\|\"\n[";
5760 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005761 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005762 aux_ptr = (char_u *)"\\|\"\n*?[";
5763
5764 p = buf + STRLEN(buf);
5765 while (n-- > 0)
5766 {
5767 /* put a backslash before \ and some others */
5768 if (vim_strchr(aux_ptr, *ptr) != NULL)
5769 *p++ = '\\';
5770#ifdef FEAT_MBYTE
5771 /* When current byte is a part of multibyte character, copy all
5772 * bytes of that character. */
5773 if (has_mbyte)
5774 {
5775 int i;
5776 int len = (*mb_ptr2len)(ptr) - 1;
5777
5778 for (i = 0; i < len && n >= 1; ++i, --n)
5779 *p++ = *ptr++;
5780 }
5781#endif
5782 *p++ = *ptr++;
5783 }
5784 *p = NUL;
5785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 /*
5788 * Execute the command.
5789 */
5790 if (cmdchar == '*' || cmdchar == '#')
5791 {
5792 if (!g_cmd && (
5793#ifdef FEAT_MBYTE
5794 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5795#endif
5796 vim_iswordc(ptr[-1])))
5797 STRCAT(buf, "\\>");
5798#ifdef FEAT_CMDHIST
5799 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005800 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5802#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005803 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805 else
5806 do_cmdline_cmd(buf);
5807
5808 vim_free(buf);
5809}
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811/*
5812 * Get visually selected text, within one line only.
5813 * Returns FAIL if more than one line selected.
5814 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005815 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005816get_visual_text(
5817 cmdarg_T *cap,
5818 char_u **pp, /* return: start of selected text */
5819 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
5821 if (VIsual_mode != 'V')
5822 unadjust_for_sel();
5823 if (VIsual.lnum != curwin->w_cursor.lnum)
5824 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005825 if (cap != NULL)
5826 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 return FAIL;
5828 }
5829 if (VIsual_mode == 'V')
5830 {
5831 *pp = ml_get_curline();
5832 *lenp = (int)STRLEN(*pp);
5833 }
5834 else
5835 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005836 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838 *pp = ml_get_pos(&curwin->w_cursor);
5839 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5840 }
5841 else
5842 {
5843 *pp = ml_get_pos(&VIsual);
5844 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5845 }
5846#ifdef FEAT_MBYTE
5847 if (has_mbyte)
5848 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005849 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#endif
5851 }
5852 reset_VIsual_and_resel();
5853 return OK;
5854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856/*
5857 * CTRL-T: backwards in tag stack
5858 */
5859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005860nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 if (!checkclearopq(cap->oap))
5863 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5864}
5865
5866/*
5867 * Handle scrolling command 'H', 'L' and 'M'.
5868 */
5869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005870nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 int used = 0;
5873 long n;
5874#ifdef FEAT_FOLDING
5875 linenr_T lnum;
5876#endif
5877 int half;
5878
5879 cap->oap->motion_type = MLINE;
5880 setpcmark();
5881
5882 if (cap->cmdchar == 'L')
5883 {
5884 validate_botline(); /* make sure curwin->w_botline is valid */
5885 curwin->w_cursor.lnum = curwin->w_botline - 1;
5886 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5887 curwin->w_cursor.lnum = 1;
5888 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005889 {
5890#ifdef FEAT_FOLDING
5891 if (hasAnyFolding(curwin))
5892 {
5893 /* Count a fold for one screen line. */
5894 for (n = cap->count1 - 1; n > 0
5895 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5896 {
5897 (void)hasFolding(curwin->w_cursor.lnum,
5898 &curwin->w_cursor.lnum, NULL);
5899 --curwin->w_cursor.lnum;
5900 }
5901 }
5902 else
5903#endif
5904 curwin->w_cursor.lnum -= cap->count1 - 1;
5905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907 else
5908 {
5909 if (cap->cmdchar == 'M')
5910 {
5911#ifdef FEAT_DIFF
5912 /* Don't count filler lines above the window. */
5913 used -= diff_check_fill(curwin, curwin->w_topline)
5914 - curwin->w_topfill;
5915#endif
5916 validate_botline(); /* make sure w_empty_rows is valid */
5917 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5918 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5919 {
5920#ifdef FEAT_DIFF
5921 /* Count half he number of filler lines to be "below this
5922 * line" and half to be "above the next line". */
5923 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5924 + n) / 2 >= half)
5925 {
5926 --n;
5927 break;
5928 }
5929#endif
5930 used += plines(curwin->w_topline + n);
5931 if (used >= half)
5932 break;
5933#ifdef FEAT_FOLDING
5934 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5935 n = lnum - curwin->w_topline;
5936#endif
5937 }
5938 if (n > 0 && used > curwin->w_height)
5939 --n;
5940 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005941 else /* (cap->cmdchar == 'H') */
5942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005944#ifdef FEAT_FOLDING
5945 if (hasAnyFolding(curwin))
5946 {
5947 /* Count a fold for one screen line. */
5948 lnum = curwin->w_topline;
5949 while (n-- > 0 && lnum < curwin->w_botline - 1)
5950 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005951 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005952 ++lnum;
5953 }
5954 n = lnum - curwin->w_topline;
5955 }
5956#endif
5957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 curwin->w_cursor.lnum = curwin->w_topline + n;
5959 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5960 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5961 }
5962
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005963 /* Correct for 'so', except when an operator is pending. */
5964 if (cap->oap->op_type == OP_NOP)
5965 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 beginline(BL_SOL | BL_FIX);
5967}
5968
5969/*
5970 * Cursor right commands.
5971 */
5972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005973nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005976 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005978 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5979 {
5980 /* <C-Right> and <S-Right> move a word or WORD right */
5981 if (mod_mask & MOD_MASK_CTRL)
5982 cap->arg = TRUE;
5983 nv_wordcmd(cap);
5984 return;
5985 }
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 cap->oap->motion_type = MCHAR;
5988 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005989 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005991#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005993 * In virtual edit mode, there's no such thing as "past_line", as lines
5994 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 */
5996 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005997 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998#endif
5999
6000 for (n = cap->count1; n > 0; --n)
6001 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006002 if ((!past_line && oneright() == FAIL)
6003 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006004 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006007 * <Space> wraps to next line if 'whichwrap' has 's'.
6008 * 'l' wraps to next line if 'whichwrap' has 'l'.
6009 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 */
6011 if ( ((cap->cmdchar == ' '
6012 && vim_strchr(p_ww, 's') != NULL)
6013 || (cap->cmdchar == 'l'
6014 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006015 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 && vim_strchr(p_ww, '>') != NULL))
6017 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6018 {
6019 /* When deleting we also count the NL as a character.
6020 * Set cap->oap->inclusive when last char in the line is
6021 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006022 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006024 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 cap->oap->inclusive = TRUE;
6026 else
6027 {
6028 ++curwin->w_cursor.lnum;
6029 curwin->w_cursor.col = 0;
6030#ifdef FEAT_VIRTUALEDIT
6031 curwin->w_cursor.coladd = 0;
6032#endif
6033 curwin->w_set_curswant = TRUE;
6034 cap->oap->inclusive = FALSE;
6035 }
6036 continue;
6037 }
6038 if (cap->oap->op_type == OP_NOP)
6039 {
6040 /* Only beep and flush if not moved at all */
6041 if (n == cap->count1)
6042 beep_flush();
6043 }
6044 else
6045 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006046 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 cap->oap->inclusive = TRUE;
6048 }
6049 break;
6050 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006051 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 {
6053 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006054#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (virtual_active())
6056 oneright();
6057 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006060#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (has_mbyte)
6062 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006063 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 ++curwin->w_cursor.col;
6067 }
6068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
6070#ifdef FEAT_FOLDING
6071 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6072 && cap->oap->op_type == OP_NOP)
6073 foldOpenCursor();
6074#endif
6075}
6076
6077/*
6078 * Cursor left commands.
6079 *
6080 * Returns TRUE when operator end should not be adjusted.
6081 */
6082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006083nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084{
6085 long n;
6086
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006087 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6088 {
6089 /* <C-Left> and <S-Left> move a word or WORD left */
6090 if (mod_mask & MOD_MASK_CTRL)
6091 cap->arg = 1;
6092 nv_bck_word(cap);
6093 return;
6094 }
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 cap->oap->motion_type = MCHAR;
6097 cap->oap->inclusive = FALSE;
6098 for (n = cap->count1; n > 0; --n)
6099 {
6100 if (oneleft() == FAIL)
6101 {
6102 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6103 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6104 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6105 */
6106 if ( (((cap->cmdchar == K_BS
6107 || cap->cmdchar == Ctrl_H)
6108 && vim_strchr(p_ww, 'b') != NULL)
6109 || (cap->cmdchar == 'h'
6110 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006111 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 && vim_strchr(p_ww, '<') != NULL))
6113 && curwin->w_cursor.lnum > 1)
6114 {
6115 --(curwin->w_cursor.lnum);
6116 coladvance((colnr_T)MAXCOL);
6117 curwin->w_set_curswant = TRUE;
6118
6119 /* When the NL before the first char has to be deleted we
6120 * put the cursor on the NUL after the previous line.
6121 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006122 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 if ( (cap->oap->op_type == OP_DELETE
6124 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006125 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006127 char_u *cp = ml_get_cursor();
6128
6129 if (*cp != NUL)
6130 {
6131#ifdef FEAT_MBYTE
6132 if (has_mbyte)
6133 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6134 else
6135#endif
6136 ++curwin->w_cursor.col;
6137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 cap->retval |= CA_NO_ADJ_OP_END;
6139 }
6140 continue;
6141 }
6142 /* Only beep and flush if not moved at all */
6143 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6144 beep_flush();
6145 break;
6146 }
6147 }
6148#ifdef FEAT_FOLDING
6149 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6150 && cap->oap->op_type == OP_NOP)
6151 foldOpenCursor();
6152#endif
6153}
6154
6155/*
6156 * Cursor up commands.
6157 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6158 */
6159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006160nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006162 if (mod_mask & MOD_MASK_SHIFT)
6163 {
6164 /* <S-Up> is page up */
6165 cap->arg = BACKWARD;
6166 nv_page(cap);
6167 }
6168 else
6169 {
6170 cap->oap->motion_type = MLINE;
6171 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6172 clearopbeep(cap->oap);
6173 else if (cap->arg)
6174 beginline(BL_WHITE | BL_FIX);
6175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176}
6177
6178/*
6179 * Cursor down commands.
6180 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6181 */
6182 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006183nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006185 if (mod_mask & MOD_MASK_SHIFT)
6186 {
6187 /* <S-Down> is page down */
6188 cap->arg = FORWARD;
6189 nv_page(cap);
6190 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006191#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006192 /* Quickfix window only: view the result under the cursor. */
6193 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6194 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198#ifdef FEAT_CMDWIN
6199 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006200 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 cmdwin_result = CAR;
6202 else
6203#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006204#ifdef FEAT_JOB_CHANNEL
6205 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6206 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6207 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6208 {
6209 invoke_prompt_callback();
6210 if (restart_edit == 0)
6211 restart_edit = 'a';
6212 }
6213 else
6214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
6216 cap->oap->motion_type = MLINE;
6217 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6218 clearopbeep(cap->oap);
6219 else if (cap->arg)
6220 beginline(BL_WHITE | BL_FIX);
6221 }
6222 }
6223}
6224
6225#ifdef FEAT_SEARCHPATH
6226/*
6227 * Grab the file name under the cursor and edit it.
6228 */
6229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006230nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006233 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006235 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
6237 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006238 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 return;
6240 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006241 if (curbuf_locked())
6242 {
6243 clearop(cap->oap);
6244 return;
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006247 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
6249 if (ptr != NULL)
6250 {
6251 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006252 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006253 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006255 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006256 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006257 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258 {
6259 curwin->w_cursor.lnum = lnum;
6260 check_cursor_lnum();
6261 beginline(BL_SOL | BL_FIX);
6262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 vim_free(ptr);
6264 }
6265 else
6266 clearop(cap->oap);
6267}
6268#endif
6269
6270/*
6271 * <End> command: to end of current line or last line.
6272 */
6273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006274nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006276 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006278 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 nv_goto(cap);
6280 cap->count1 = 1; /* to end of current line */
6281 }
6282 nv_dollar(cap);
6283}
6284
6285/*
6286 * Handle the "$" command.
6287 */
6288 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006289nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 cap->oap->motion_type = MCHAR;
6292 cap->oap->inclusive = TRUE;
6293#ifdef FEAT_VIRTUALEDIT
6294 /* In virtual mode when off the edge of a line and an operator
6295 * is pending (whew!) keep the cursor where it is.
6296 * Otherwise, send it to the end of the line. */
6297 if (!virtual_active() || gchar_cursor() != NUL
6298 || cap->oap->op_type == OP_NOP)
6299#endif
6300 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6301 if (cursor_down((long)(cap->count1 - 1),
6302 cap->oap->op_type == OP_NOP) == FAIL)
6303 clearopbeep(cap->oap);
6304#ifdef FEAT_FOLDING
6305 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6306 foldOpenCursor();
6307#endif
6308}
6309
6310/*
6311 * Implementation of '?' and '/' commands.
6312 * If cap->arg is TRUE don't set PC mark.
6313 */
6314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006315nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006318 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
6320 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6321 {
6322 /* Translate "g??" to "g?g?" */
6323 cap->cmdchar = 'g';
6324 cap->nchar = '?';
6325 nv_operator(cap);
6326 return;
6327 }
6328
Bram Moolenaardda933d2016-09-03 21:04:58 +02006329 /* When using 'incsearch' the cursor may be moved to set a different search
6330 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6332
6333 if (cap->searchbuf == NULL)
6334 {
6335 clearop(oap);
6336 return;
6337 }
6338
Bram Moolenaar46539112015-02-17 15:43:57 +01006339 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006340 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006341 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
6344/*
6345 * Handle "N" and "n" commands.
6346 * cap->arg is SEARCH_REV for "N", 0 for "n".
6347 */
6348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
Bram Moolenaar46539112015-02-17 15:43:57 +01006351 pos_T old = curwin->w_cursor;
6352 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6353
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006354 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006355 {
6356 /* Avoid getting stuck on the current cursor position, which can
6357 * happen when an offset is given and the cursor is on the last char
6358 * in the buffer: Repeat with count + 1. */
6359 cap->count1 += 1;
6360 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6361 cap->count1 -= 1;
6362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365/*
6366 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6367 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006368 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006370 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371normal_search(
6372 cmdarg_T *cap,
6373 int dir,
6374 char_u *pat,
6375 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 int i;
6378
6379 cap->oap->motion_type = MCHAR;
6380 cap->oap->inclusive = FALSE;
6381 cap->oap->use_reg_one = TRUE;
6382 curwin->w_set_curswant = TRUE;
6383
6384 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006385 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (i == 0)
6387 clearop(cap->oap);
6388 else
6389 {
6390 if (i == 2)
6391 cap->oap->motion_type = MLINE;
6392#ifdef FEAT_VIRTUALEDIT
6393 curwin->w_cursor.coladd = 0;
6394#endif
6395#ifdef FEAT_FOLDING
6396 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6397 foldOpenCursor();
6398#endif
6399 }
6400
6401 /* "/$" will put the cursor after the end of the line, may need to
6402 * correct that here */
6403 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006404 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405}
6406
6407/*
6408 * Character search commands.
6409 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6410 * ',' and FALSE for ';'.
6411 * cap->nchar is NUL for ',' and ';' (repeat the search)
6412 */
6413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006414nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int t_cmd;
6417
6418 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6419 t_cmd = TRUE;
6420 else
6421 t_cmd = FALSE;
6422
6423 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6425 clearopbeep(cap->oap);
6426 else
6427 {
6428 curwin->w_set_curswant = TRUE;
6429#ifdef FEAT_VIRTUALEDIT
6430 /* Include a Tab for "tx" and for "dfx". */
6431 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6432 && (t_cmd || cap->oap->op_type != OP_NOP))
6433 {
6434 colnr_T scol, ecol;
6435
6436 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6437 curwin->w_cursor.coladd = ecol - scol;
6438 }
6439 else
6440 curwin->w_cursor.coladd = 0;
6441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443#ifdef FEAT_FOLDING
6444 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6445 foldOpenCursor();
6446#endif
6447 }
6448}
6449
6450/*
6451 * "[" and "]" commands.
6452 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6453 */
6454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006455nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006457 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 pos_T prev_pos;
6459 pos_T *pos = NULL; /* init for GCC */
6460 pos_T old_pos; /* cursor position before command */
6461 int flag;
6462 long n;
6463 int findc;
6464 int c;
6465
6466 cap->oap->motion_type = MCHAR;
6467 cap->oap->inclusive = FALSE;
6468 old_pos = curwin->w_cursor;
6469#ifdef FEAT_VIRTUALEDIT
6470 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6471#endif
6472
6473#ifdef FEAT_SEARCHPATH
6474 /*
6475 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6476 */
6477 if (cap->nchar == 'f')
6478 nv_gotofile(cap);
6479 else
6480#endif
6481
6482#ifdef FEAT_FIND_ID
6483 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006484 * Find the occurrence(s) of the identifier or define under cursor
6485 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 *
6487 * search list jump
6488 * fwd bwd fwd bwd fwd bwd
6489 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6490 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6491 */
6492 if (vim_strchr((char_u *)
6493#ifdef EBCDIC
6494 "iI\005dD\067",
6495#else
6496 "iI\011dD\004",
6497#endif
6498 cap->nchar) != NULL)
6499 {
6500 char_u *ptr;
6501 int len;
6502
6503 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6504 clearop(cap->oap);
6505 else
6506 {
6507 find_pattern_in_path(ptr, 0, len, TRUE,
6508 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6509 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6510 cap->count1,
6511 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6512 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6513 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6514 (linenr_T)MAXLNUM);
6515 curwin->w_set_curswant = TRUE;
6516 }
6517 }
6518 else
6519#endif
6520
6521 /*
6522 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6523 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6524 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6525 * "[m" or "]m" search for prev/next start of (Java) method.
6526 * "[M" or "]M" search for prev/next end of (Java) method.
6527 */
6528 if ( (cap->cmdchar == '['
6529 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6530 || (cap->cmdchar == ']'
6531 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6532 {
6533 if (cap->nchar == '*')
6534 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 prev_pos.lnum = 0;
6536 if (cap->nchar == 'm' || cap->nchar == 'M')
6537 {
6538 if (cap->cmdchar == '[')
6539 findc = '{';
6540 else
6541 findc = '}';
6542 n = 9999;
6543 }
6544 else
6545 {
6546 findc = cap->nchar;
6547 n = cap->count1;
6548 }
6549 for ( ; n > 0; --n)
6550 {
6551 if ((pos = findmatchlimit(cap->oap, findc,
6552 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6553 {
6554 if (new_pos.lnum == 0) /* nothing found */
6555 {
6556 if (cap->nchar != 'm' && cap->nchar != 'M')
6557 clearopbeep(cap->oap);
6558 }
6559 else
6560 pos = &new_pos; /* use last one found */
6561 break;
6562 }
6563 prev_pos = new_pos;
6564 curwin->w_cursor = *pos;
6565 new_pos = *pos;
6566 }
6567 curwin->w_cursor = old_pos;
6568
6569 /*
6570 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6571 * brought us to the match for "[m" and "]M" when inside a method.
6572 * Try finding the '{' or '}' we want to be at.
6573 * Also repeat for the given count.
6574 */
6575 if (cap->nchar == 'm' || cap->nchar == 'M')
6576 {
6577 /* norm is TRUE for "]M" and "[m" */
6578 int norm = ((findc == '{') == (cap->nchar == 'm'));
6579
6580 n = cap->count1;
6581 /* found a match: we were inside a method */
6582 if (prev_pos.lnum != 0)
6583 {
6584 pos = &prev_pos;
6585 curwin->w_cursor = prev_pos;
6586 if (norm)
6587 --n;
6588 }
6589 else
6590 pos = NULL;
6591 while (n > 0)
6592 {
6593 for (;;)
6594 {
6595 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6596 {
6597 /* if not found anything, that's an error */
6598 if (pos == NULL)
6599 clearopbeep(cap->oap);
6600 n = 0;
6601 break;
6602 }
6603 c = gchar_cursor();
6604 if (c == '{' || c == '}')
6605 {
6606 /* Must have found end/start of class: use it.
6607 * Or found the place to be at. */
6608 if ((c == findc && norm) || (n == 1 && !norm))
6609 {
6610 new_pos = curwin->w_cursor;
6611 pos = &new_pos;
6612 n = 0;
6613 }
6614 /* if no match found at all, we started outside of the
6615 * class and we're inside now. Just go on. */
6616 else if (new_pos.lnum == 0)
6617 {
6618 new_pos = curwin->w_cursor;
6619 pos = &new_pos;
6620 }
6621 /* found start/end of other method: go to match */
6622 else if ((pos = findmatchlimit(cap->oap, findc,
6623 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6624 0)) == NULL)
6625 n = 0;
6626 else
6627 curwin->w_cursor = *pos;
6628 break;
6629 }
6630 }
6631 --n;
6632 }
6633 curwin->w_cursor = old_pos;
6634 if (pos == NULL && new_pos.lnum != 0)
6635 clearopbeep(cap->oap);
6636 }
6637 if (pos != NULL)
6638 {
6639 setpcmark();
6640 curwin->w_cursor = *pos;
6641 curwin->w_set_curswant = TRUE;
6642#ifdef FEAT_FOLDING
6643 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6644 && cap->oap->op_type == OP_NOP)
6645 foldOpenCursor();
6646#endif
6647 }
6648 }
6649
6650 /*
6651 * "[[", "[]", "]]" and "][": move to start or end of function
6652 */
6653 else if (cap->nchar == '[' || cap->nchar == ']')
6654 {
6655 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6656 flag = '{';
6657 else
6658 flag = '}'; /* "][" or "[]" */
6659
6660 curwin->w_set_curswant = TRUE;
6661 /*
6662 * Imitate strange Vi behaviour: When using "]]" with an operator
6663 * we also stop at '}'.
6664 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006665 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 (cap->oap->op_type != OP_NOP
6667 && cap->arg == FORWARD && flag == '{')))
6668 clearopbeep(cap->oap);
6669 else
6670 {
6671 if (cap->oap->op_type == OP_NOP)
6672 beginline(BL_WHITE | BL_FIX);
6673#ifdef FEAT_FOLDING
6674 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6675 foldOpenCursor();
6676#endif
6677 }
6678 }
6679
6680 /*
6681 * "[p", "[P", "]P" and "]p": put with indent adjustment
6682 */
6683 else if (cap->nchar == 'p' || cap->nchar == 'P')
6684 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006685 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006687 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6688 ? FORWARD : BACKWARD;
6689 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006690 int was_visual = VIsual_active;
6691 int line_count = curbuf->b_ml.ml_line_count;
6692 pos_T start, end;
6693
6694 if (VIsual_active)
6695 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006696 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006697 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006698 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006699 curwin->w_cursor = (dir == BACKWARD ? start : end);
6700 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006701# ifdef FEAT_CLIPBOARD
6702 adjust_clip_reg(&regname);
6703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006705
6706 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707 if (was_visual)
6708 {
6709 VIsual = start;
6710 curwin->w_cursor = end;
6711 if (dir == BACKWARD)
6712 {
6713 /* adjust lines */
6714 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6715 curwin->w_cursor.lnum +=
6716 curbuf->b_ml.ml_line_count - line_count;
6717 }
6718
6719 VIsual_active = TRUE;
6720 if (VIsual_mode == 'V')
6721 {
6722 /* delete visually selected lines */
6723 cap->cmdchar = 'd';
6724 cap->nchar = NUL;
6725 cap->oap->regname = regname;
6726 nv_operator(cap);
6727 do_pending_operator(cap, 0, FALSE);
6728 }
6729 if (VIsual_active)
6730 {
6731 end_visual_mode();
6732 redraw_later(SOME_VALID);
6733 }
6734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736 }
6737
6738 /*
6739 * "['", "[`", "]'" and "]`": jump to next mark
6740 */
6741 else if (cap->nchar == '\'' || cap->nchar == '`')
6742 {
6743 pos = &curwin->w_cursor;
6744 for (n = cap->count1; n > 0; --n)
6745 {
6746 prev_pos = *pos;
6747 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6748 cap->nchar == '\'');
6749 if (pos == NULL)
6750 break;
6751 }
6752 if (pos == NULL)
6753 pos = &prev_pos;
6754 nv_cursormark(cap, cap->nchar == '\'', pos);
6755 }
6756
6757#ifdef FEAT_MOUSE
6758 /*
6759 * [ or ] followed by a middle mouse click: put selected text with
6760 * indent adjustment. Any other button just does as usual.
6761 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006762 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {
6764 (void)do_mouse(cap->oap, cap->nchar,
6765 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6766 cap->count1, PUT_FIXINDENT);
6767 }
6768#endif /* FEAT_MOUSE */
6769
6770#ifdef FEAT_FOLDING
6771 /*
6772 * "[z" and "]z": move to start or end of open fold.
6773 */
6774 else if (cap->nchar == 'z')
6775 {
6776 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6777 cap->count1) == FAIL)
6778 clearopbeep(cap->oap);
6779 }
6780#endif
6781
6782#ifdef FEAT_DIFF
6783 /*
6784 * "[c" and "]c": move to next or previous diff-change.
6785 */
6786 else if (cap->nchar == 'c')
6787 {
6788 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6789 cap->count1) == FAIL)
6790 clearopbeep(cap->oap);
6791 }
6792#endif
6793
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006794#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006795 /*
6796 * "[s", "[S", "]s" and "]S": move to next spell error.
6797 */
6798 else if (cap->nchar == 's' || cap->nchar == 'S')
6799 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006800 setpcmark();
6801 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006802 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6803 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006804 {
6805 clearopbeep(cap->oap);
6806 break;
6807 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006808 else
6809 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006810# ifdef FEAT_FOLDING
6811 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6812 foldOpenCursor();
6813# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006814 }
6815#endif
6816
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 /* Not a valid cap->nchar. */
6818 else
6819 clearopbeep(cap->oap);
6820}
6821
6822/*
6823 * Handle Normal mode "%" command.
6824 */
6825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006826nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006829#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 linenr_T lnum = curwin->w_cursor.lnum;
6831#endif
6832
6833 cap->oap->inclusive = TRUE;
6834 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6835 {
6836 if (cap->count0 > 100)
6837 clearopbeep(cap->oap);
6838 else
6839 {
6840 cap->oap->motion_type = MLINE;
6841 setpcmark();
6842 /* Round up, so CTRL-G will give same value. Watch out for a
6843 * large line count, the line number must not go negative! */
6844 if (curbuf->b_ml.ml_line_count > 1000000)
6845 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6846 / 100L * cap->count0;
6847 else
6848 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6849 cap->count0 + 99L) / 100L;
6850 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6851 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6852 beginline(BL_SOL | BL_FIX);
6853 }
6854 }
6855 else /* "%" : go to matching paren */
6856 {
6857 cap->oap->motion_type = MCHAR;
6858 cap->oap->use_reg_one = TRUE;
6859 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6860 clearopbeep(cap->oap);
6861 else
6862 {
6863 setpcmark();
6864 curwin->w_cursor = *pos;
6865 curwin->w_set_curswant = TRUE;
6866#ifdef FEAT_VIRTUALEDIT
6867 curwin->w_cursor.coladd = 0;
6868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871 }
6872#ifdef FEAT_FOLDING
6873 if (cap->oap->op_type == OP_NOP
6874 && lnum != curwin->w_cursor.lnum
6875 && (fdo_flags & FDO_PERCENT)
6876 && KeyTyped)
6877 foldOpenCursor();
6878#endif
6879}
6880
6881/*
6882 * Handle "(" and ")" commands.
6883 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6884 */
6885 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006886nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 cap->oap->motion_type = MCHAR;
6889 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006890 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6891 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 curwin->w_set_curswant = TRUE;
6893
6894 if (findsent(cap->arg, cap->count1) == FAIL)
6895 clearopbeep(cap->oap);
6896 else
6897 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006898 /* Don't leave the cursor on the NUL past end of line. */
6899 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900#ifdef FEAT_VIRTUALEDIT
6901 curwin->w_cursor.coladd = 0;
6902#endif
6903#ifdef FEAT_FOLDING
6904 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6905 foldOpenCursor();
6906#endif
6907 }
6908}
6909
6910/*
6911 * "m" command: Mark a position.
6912 */
6913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 if (!checkclearop(cap->oap))
6917 {
6918 if (setmark(cap->nchar) == FAIL)
6919 clearopbeep(cap->oap);
6920 }
6921}
6922
6923/*
6924 * "{" and "}" commands.
6925 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6926 */
6927 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006928nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 cap->oap->motion_type = MCHAR;
6931 cap->oap->inclusive = FALSE;
6932 cap->oap->use_reg_one = TRUE;
6933 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006934 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 clearopbeep(cap->oap);
6936 else
6937 {
6938#ifdef FEAT_VIRTUALEDIT
6939 curwin->w_cursor.coladd = 0;
6940#endif
6941#ifdef FEAT_FOLDING
6942 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6943 foldOpenCursor();
6944#endif
6945 }
6946}
6947
6948/*
6949 * "u" command: Undo or make lower case.
6950 */
6951 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006952nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006954 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
6956 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6957 cap->cmdchar = 'g';
6958 cap->nchar = 'u';
6959 nv_operator(cap);
6960 }
6961 else
6962 nv_kundo(cap);
6963}
6964
6965/*
6966 * <Undo> command.
6967 */
6968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006969nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 if (!checkclearopq(cap->oap))
6972 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006973#ifdef FEAT_JOB_CHANNEL
6974 if (bt_prompt(curbuf))
6975 {
6976 clearopbeep(cap->oap);
6977 return;
6978 }
6979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 u_undo((int)cap->count1);
6981 curwin->w_set_curswant = TRUE;
6982 }
6983}
6984
6985/*
6986 * Handle the "r" command.
6987 */
6988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006989nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990{
6991 char_u *ptr;
6992 int had_ctrl_v;
6993 long n;
6994
6995 if (checkclearop(cap->oap))
6996 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006997#ifdef FEAT_JOB_CHANNEL
6998 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6999 {
7000 clearopbeep(cap->oap);
7001 return;
7002 }
7003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 /* get another character */
7006 if (cap->nchar == Ctrl_V)
7007 {
7008 had_ctrl_v = Ctrl_V;
7009 cap->nchar = get_literal();
7010 /* Don't redo a multibyte character with CTRL-V. */
7011 if (cap->nchar > DEL)
7012 had_ctrl_v = NUL;
7013 }
7014 else
7015 had_ctrl_v = NUL;
7016
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007017 /* Abort if the character is a special key. */
7018 if (IS_SPECIAL(cap->nchar))
7019 {
7020 clearopbeep(cap->oap);
7021 return;
7022 }
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 /* Visual mode "r" */
7025 if (VIsual_active)
7026 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007027 if (got_int)
7028 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007029 if (had_ctrl_v)
7030 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007031 /* Use a special (negative) number to make a difference between a
7032 * literal CR or NL and a line break. */
7033 if (cap->nchar == CAR)
7034 cap->nchar = REPLACE_CR_NCHAR;
7035 else if (cap->nchar == NL)
7036 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 nv_operator(cap);
7039 return;
7040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
7042#ifdef FEAT_VIRTUALEDIT
7043 /* Break tabs, etc. */
7044 if (virtual_active())
7045 {
7046 if (u_save_cursor() == FAIL)
7047 return;
7048 if (gchar_cursor() == NUL)
7049 {
7050 /* Add extra space and put the cursor on the first one. */
7051 coladvance_force((colnr_T)(getviscol() + cap->count1));
7052 curwin->w_cursor.col -= cap->count1;
7053 }
7054 else if (gchar_cursor() == TAB)
7055 coladvance_force(getviscol());
7056 }
7057#endif
7058
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007059 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007061 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#ifdef FEAT_MBYTE
7063 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7064#endif
7065 )
7066 {
7067 clearopbeep(cap->oap);
7068 return;
7069 }
7070
7071 /*
7072 * Replacing with a TAB is done by edit() when it is complicated because
7073 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7074 * Other characters are done below to avoid problems with things like
7075 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7076 */
7077 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7078 {
7079 stuffnumReadbuff(cap->count1);
7080 stuffcharReadbuff('R');
7081 stuffcharReadbuff('\t');
7082 stuffcharReadbuff(ESC);
7083 return;
7084 }
7085
7086 /* save line for undo */
7087 if (u_save_cursor() == FAIL)
7088 return;
7089
7090 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7091 {
7092 /*
7093 * Replace character(s) by a single newline.
7094 * Strange vi behaviour: Only one newline is inserted.
7095 * Delete the characters here.
7096 * Insert the newline with an insert command, takes care of
7097 * autoindent. The insert command depends on being on the last
7098 * character of a line or not.
7099 */
7100#ifdef FEAT_MBYTE
7101 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7102#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007103 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104#endif
7105 stuffcharReadbuff('\r');
7106 stuffcharReadbuff(ESC);
7107
7108 /* Give 'r' to edit(), to get the redo command right. */
7109 invoke_edit(cap, TRUE, 'r', FALSE);
7110 }
7111 else
7112 {
7113 prep_redo(cap->oap->regname, cap->count1,
7114 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7115
7116 curbuf->b_op_start = curwin->w_cursor;
7117#ifdef FEAT_MBYTE
7118 if (has_mbyte)
7119 {
7120 int old_State = State;
7121
7122 if (cap->ncharC1 != 0)
7123 AppendCharToRedobuff(cap->ncharC1);
7124 if (cap->ncharC2 != 0)
7125 AppendCharToRedobuff(cap->ncharC2);
7126
7127 /* This is slow, but it handles replacing a single-byte with a
7128 * multi-byte and the other way around. Also handles adding
7129 * composing characters for utf-8. */
7130 for (n = cap->count1; n > 0; --n)
7131 {
7132 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007133 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7134 {
7135 int c = ins_copychar(curwin->w_cursor.lnum
7136 + (cap->nchar == Ctrl_Y ? -1 : 1));
7137 if (c != NUL)
7138 ins_char(c);
7139 else
7140 /* will be decremented further down */
7141 ++curwin->w_cursor.col;
7142 }
7143 else
7144 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 State = old_State;
7146 if (cap->ncharC1 != 0)
7147 ins_char(cap->ncharC1);
7148 if (cap->ncharC2 != 0)
7149 ins_char(cap->ncharC2);
7150 }
7151 }
7152 else
7153#endif
7154 {
7155 /*
7156 * Replace the characters within one line.
7157 */
7158 for (n = cap->count1; n > 0; --n)
7159 {
7160 /*
7161 * Get ptr again, because u_save and/or showmatch() will have
7162 * released the line. At the same time we let know that the
7163 * line will be changed.
7164 */
7165 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007166 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7167 {
7168 int c = ins_copychar(curwin->w_cursor.lnum
7169 + (cap->nchar == Ctrl_Y ? -1 : 1));
7170 if (c != NUL)
7171 ptr[curwin->w_cursor.col] = c;
7172 }
7173 else
7174 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (p_sm && msg_silent == 0)
7176 showmatch(cap->nchar);
7177 ++curwin->w_cursor.col;
7178 }
7179#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007180 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007182 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar009b2592004-10-24 19:18:58 +00007184 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007185 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007187 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189#endif
7190
7191 /* mark the buffer as changed and prepare for displaying */
7192 changed_bytes(curwin->w_cursor.lnum,
7193 (colnr_T)(curwin->w_cursor.col - cap->count1));
7194 }
7195 --curwin->w_cursor.col; /* cursor on the last replaced char */
7196#ifdef FEAT_MBYTE
7197 /* if the character on the left of the current cursor is a multi-byte
7198 * character, move two characters left */
7199 if (has_mbyte)
7200 mb_adjust_cursor();
7201#endif
7202 curbuf->b_op_end = curwin->w_cursor;
7203 curwin->w_set_curswant = TRUE;
7204 set_last_insert(cap->nchar);
7205 }
7206}
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208/*
7209 * 'o': Exchange start and end of Visual area.
7210 * 'O': same, but in block mode exchange left and right corners.
7211 */
7212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 pos_T old_cursor;
7216 colnr_T left, right;
7217
7218 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7219 {
7220 old_cursor = curwin->w_cursor;
7221 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7222 curwin->w_cursor.lnum = VIsual.lnum;
7223 coladvance(left);
7224 VIsual = curwin->w_cursor;
7225
7226 curwin->w_cursor.lnum = old_cursor.lnum;
7227 curwin->w_curswant = right;
7228 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7229 * right one column */
7230 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7231 ++curwin->w_curswant;
7232 coladvance(curwin->w_curswant);
7233 if (curwin->w_cursor.col == old_cursor.col
7234#ifdef FEAT_VIRTUALEDIT
7235 && (!virtual_active()
7236 || curwin->w_cursor.coladd == old_cursor.coladd)
7237#endif
7238 )
7239 {
7240 curwin->w_cursor.lnum = VIsual.lnum;
7241 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7242 ++right;
7243 coladvance(right);
7244 VIsual = curwin->w_cursor;
7245
7246 curwin->w_cursor.lnum = old_cursor.lnum;
7247 coladvance(left);
7248 curwin->w_curswant = left;
7249 }
7250 }
7251 else
7252 {
7253 old_cursor = curwin->w_cursor;
7254 curwin->w_cursor = VIsual;
7255 VIsual = old_cursor;
7256 curwin->w_set_curswant = TRUE;
7257 }
7258}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260/*
7261 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7262 */
7263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007264nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (VIsual_active) /* "R" is replace lines */
7267 {
7268 cap->cmdchar = 'c';
7269 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007270 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 VIsual_mode = 'V';
7272 nv_operator(cap);
7273 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007274 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 if (!curbuf->b_p_ma)
7277 EMSG(_(e_modifiable));
7278 else
7279 {
7280#ifdef FEAT_VIRTUALEDIT
7281 if (virtual_active())
7282 coladvance(getviscol());
7283#endif
7284 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7285 }
7286 }
7287}
7288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289/*
7290 * "gr".
7291 */
7292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007293nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (VIsual_active)
7296 {
7297 cap->cmdchar = 'r';
7298 cap->nchar = cap->extra_char;
7299 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7300 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007301 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 {
7303 if (!curbuf->b_p_ma)
7304 EMSG(_(e_modifiable));
7305 else
7306 {
7307 if (cap->extra_char == Ctrl_V) /* get another character */
7308 cap->extra_char = get_literal();
7309 stuffcharReadbuff(cap->extra_char);
7310 stuffcharReadbuff(ESC);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02007311#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 if (virtual_active())
7313 coladvance(getviscol());
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02007314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 invoke_edit(cap, TRUE, 'v', FALSE);
7316 }
7317 }
7318}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320/*
7321 * Swap case for "~" command, when it does not work like an operator.
7322 */
7323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007324n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326 long n;
7327 pos_T startpos;
7328 int did_change = 0;
7329#ifdef FEAT_NETBEANS_INTG
7330 pos_T pos;
7331 char_u *ptr;
7332 int count;
7333#endif
7334
7335 if (checkclearopq(cap->oap))
7336 return;
7337
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007338 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
7340 clearopbeep(cap->oap);
7341 return;
7342 }
7343
7344 prep_redo_cmd(cap);
7345
7346 if (u_save_cursor() == FAIL)
7347 return;
7348
7349 startpos = curwin->w_cursor;
7350#ifdef FEAT_NETBEANS_INTG
7351 pos = startpos;
7352#endif
7353 for (n = cap->count1; n > 0; --n)
7354 {
7355 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7356 inc_cursor();
7357 if (gchar_cursor() == NUL)
7358 {
7359 if (vim_strchr(p_ww, '~') != NULL
7360 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7361 {
7362#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007363 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
7365 if (did_change)
7366 {
7367 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007368 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007369 netbeans_removed(curbuf, pos.lnum, pos.col,
7370 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007372 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 }
7374 pos.col = 0;
7375 pos.lnum++;
7376 }
7377#endif
7378 ++curwin->w_cursor.lnum;
7379 curwin->w_cursor.col = 0;
7380 if (n > 1)
7381 {
7382 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7383 break;
7384 u_clearline();
7385 }
7386 }
7387 else
7388 break;
7389 }
7390 }
7391#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007392 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
7394 ptr = ml_get(pos.lnum);
7395 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007396 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7397 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399#endif
7400
7401
7402 check_cursor();
7403 curwin->w_set_curswant = TRUE;
7404 if (did_change)
7405 {
7406 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7407 0L);
7408 curbuf->b_op_start = startpos;
7409 curbuf->b_op_end = curwin->w_cursor;
7410 if (curbuf->b_op_end.col > 0)
7411 --curbuf->b_op_end.col;
7412 }
7413}
7414
7415/*
7416 * Move cursor to mark.
7417 */
7418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007419nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 if (check_mark(pos) == FAIL)
7422 clearop(cap->oap);
7423 else
7424 {
7425 if (cap->cmdchar == '\''
7426 || cap->cmdchar == '`'
7427 || cap->cmdchar == '['
7428 || cap->cmdchar == ']')
7429 setpcmark();
7430 curwin->w_cursor = *pos;
7431 if (flag)
7432 beginline(BL_WHITE | BL_FIX);
7433 else
7434 check_cursor();
7435 }
7436 cap->oap->motion_type = flag ? MLINE : MCHAR;
7437 if (cap->cmdchar == '`')
7438 cap->oap->use_reg_one = TRUE;
7439 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7440 curwin->w_set_curswant = TRUE;
7441}
7442
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443/*
7444 * Handle commands that are operators in Visual mode.
7445 */
7446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007447v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7450
7451 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007452 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (isupper(cap->cmdchar))
7454 {
7455 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007456 {
7457 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7461 curwin->w_curswant = MAXCOL;
7462 }
7463 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7464 nv_operator(cap);
7465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
7467/*
7468 * "s" and "S" commands.
7469 */
7470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007471nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007473#ifdef FEAT_TERMINAL
7474 /* When showing output of term_dumpdiff() swap the top and botom. */
7475 if (term_swap_diff() == OK)
7476 return;
7477#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007478#ifdef FEAT_JOB_CHANNEL
7479 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7480 {
7481 clearopbeep(cap->oap);
7482 return;
7483 }
7484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7486 {
7487 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007488 {
7489 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 cap->cmdchar = 'c';
7493 nv_operator(cap);
7494 }
7495 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 nv_optrans(cap);
7497}
7498
7499/*
7500 * Abbreviated commands.
7501 */
7502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007503nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
7505 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7506 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 /* in Visual mode these commands are operators */
7509 if (VIsual_active)
7510 v_visop(cap);
7511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 nv_optrans(cap);
7513}
7514
7515/*
7516 * Translate a command into another command.
7517 */
7518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007519nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7522 (char_u *)"d$", (char_u *)"c$",
7523 (char_u *)"cl", (char_u *)"cc",
7524 (char_u *)"yy", (char_u *)":s\r"};
7525 static char_u *str = (char_u *)"xXDCsSY&";
7526
7527 if (!checkclearopq(cap->oap))
7528 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007529 /* In Vi "2D" doesn't delete the next line. Can't translate it
7530 * either, because "2." should also not use the count. */
7531 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7532 {
7533 cap->oap->start = curwin->w_cursor;
7534 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007535#ifdef FEAT_EVAL
7536 set_op_var(OP_DELETE);
7537#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007538 cap->count1 = 1;
7539 nv_dollar(cap);
7540 finish_op = TRUE;
7541 ResetRedobuff();
7542 AppendCharToRedobuff('D');
7543 }
7544 else
7545 {
7546 if (cap->count0)
7547 stuffnumReadbuff(cap->count0);
7548 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551 cap->opcount = 0;
7552}
7553
7554/*
7555 * "'" and "`" commands. Also for "g'" and "g`".
7556 * cap->arg is TRUE for "'" and "g'".
7557 */
7558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007559nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 pos_T *pos;
7562 int c;
7563#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007564 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7566#endif
7567
7568 if (cap->cmdchar == 'g')
7569 c = cap->extra_char;
7570 else
7571 c = cap->nchar;
7572 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7573 if (pos == (pos_T *)-1) /* jumped to other file */
7574 {
7575 if (cap->arg)
7576 {
7577 check_cursor_lnum();
7578 beginline(BL_WHITE | BL_FIX);
7579 }
7580 else
7581 check_cursor();
7582 }
7583 else
7584 nv_cursormark(cap, cap->arg, pos);
7585
7586#ifdef FEAT_VIRTUALEDIT
7587 /* May need to clear the coladd that a mark includes. */
7588 if (!virtual_active())
7589 curwin->w_cursor.coladd = 0;
7590#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007591 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592#ifdef FEAT_FOLDING
7593 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007594 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007595 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 && (fdo_flags & FDO_MARK)
7597 && old_KeyTyped)
7598 foldOpenCursor();
7599#endif
7600}
7601
7602/*
7603 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7604 */
7605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007606nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607{
7608#ifdef FEAT_JUMPLIST
7609 pos_T *pos;
7610# ifdef FEAT_FOLDING
7611 linenr_T lnum = curwin->w_cursor.lnum;
7612 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7613# endif
7614
7615 if (!checkclearopq(cap->oap))
7616 {
7617 if (cap->cmdchar == 'g')
7618 pos = movechangelist((int)cap->count1);
7619 else
7620 pos = movemark((int)cap->count1);
7621 if (pos == (pos_T *)-1) /* jump to other file */
7622 {
7623 curwin->w_set_curswant = TRUE;
7624 check_cursor();
7625 }
7626 else if (pos != NULL) /* can jump */
7627 nv_cursormark(cap, FALSE, pos);
7628 else if (cap->cmdchar == 'g')
7629 {
7630 if (curbuf->b_changelistlen == 0)
7631 EMSG(_("E664: changelist is empty"));
7632 else if (cap->count1 < 0)
7633 EMSG(_("E662: At start of changelist"));
7634 else
7635 EMSG(_("E663: At end of changelist"));
7636 }
7637 else
7638 clearopbeep(cap->oap);
7639# ifdef FEAT_FOLDING
7640 if (cap->oap->op_type == OP_NOP
7641 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7642 && (fdo_flags & FDO_MARK)
7643 && old_KeyTyped)
7644 foldOpenCursor();
7645# endif
7646 }
7647#else
7648 clearopbeep(cap->oap);
7649#endif
7650}
7651
7652/*
7653 * Handle '"' command.
7654 */
7655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007656nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 if (checkclearop(cap->oap))
7659 return;
7660#ifdef FEAT_EVAL
7661 if (cap->nchar == '=')
7662 cap->nchar = get_expr_register();
7663#endif
7664 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7665 {
7666 cap->oap->regname = cap->nchar;
7667 cap->opcount = cap->count0; /* remember count before '"' */
7668#ifdef FEAT_EVAL
7669 set_reg_var(cap->oap->regname);
7670#endif
7671 }
7672 else
7673 clearopbeep(cap->oap);
7674}
7675
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676/*
7677 * Handle "v", "V" and "CTRL-V" commands.
7678 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7679 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007680 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 */
7682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007683nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007685 if (cap->cmdchar == Ctrl_Q)
7686 cap->cmdchar = Ctrl_V;
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7689 * characterwise, linewise, or blockwise. */
7690 if (cap->oap->op_type != OP_NOP)
7691 {
7692 cap->oap->motion_force = cap->cmdchar;
7693 finish_op = FALSE; /* operator doesn't finish now but later */
7694 return;
7695 }
7696
7697 VIsual_select = cap->arg;
7698 if (VIsual_active) /* change Visual mode */
7699 {
7700 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7701 end_visual_mode();
7702 else /* toggle char/block mode */
7703 { /* or char/line mode */
7704 VIsual_mode = cap->cmdchar;
7705 showmode();
7706 }
7707 redraw_curbuf_later(INVERTED); /* update the inversion */
7708 }
7709 else /* start Visual mode */
7710 {
7711 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007712 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007714 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 VIsual = curwin->w_cursor;
7716
7717 VIsual_active = TRUE;
7718 VIsual_reselect = TRUE;
7719 if (!cap->arg)
7720 /* start Select mode when 'selectmode' contains "cmd" */
7721 may_start_select('c');
7722#ifdef FEAT_MOUSE
7723 setmouse();
7724#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007725 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 redraw_cmdline = TRUE; /* show visual mode later */
7727 /*
7728 * For V and ^V, we multiply the number of lines even if there
7729 * was only one -- webb
7730 */
7731 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7732 {
7733 curwin->w_cursor.lnum +=
7734 resel_VIsual_line_count * cap->count0 - 1;
7735 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7736 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7737 }
7738 VIsual_mode = resel_VIsual_mode;
7739 if (VIsual_mode == 'v')
7740 {
7741 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007742 {
7743 validate_virtcol();
7744 curwin->w_curswant = curwin->w_virtcol
7745 + resel_VIsual_vcol * cap->count0 - 1;
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007748 curwin->w_curswant = resel_VIsual_vcol;
7749 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007751 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
7753 curwin->w_curswant = MAXCOL;
7754 coladvance((colnr_T)MAXCOL);
7755 }
7756 else if (VIsual_mode == Ctrl_V)
7757 {
7758 validate_virtcol();
7759 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007760 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 coladvance(curwin->w_curswant);
7762 }
7763 else
7764 curwin->w_set_curswant = TRUE;
7765 redraw_curbuf_later(INVERTED); /* show the inversion */
7766 }
7767 else
7768 {
7769 if (!cap->arg)
7770 /* start Select mode when 'selectmode' contains "cmd" */
7771 may_start_select('c');
7772 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007773 if (VIsual_mode != 'V' && *p_sel == 'e')
7774 ++cap->count1; /* include one more char */
7775 if (cap->count0 > 0 && --cap->count1 > 0)
7776 {
7777 /* With a count select that many characters or lines. */
7778 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7779 nv_right(cap);
7780 else if (VIsual_mode == 'V')
7781 nv_down(cap);
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784 }
7785}
7786
7787/*
7788 * Start selection for Shift-movement keys.
7789 */
7790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007791start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 /* if 'selectmode' contains "key", start Select mode */
7794 may_start_select('k');
7795 n_start_visual_mode('v');
7796}
7797
7798/*
7799 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7800 */
7801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007802may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 VIsual_select = (stuff_empty() && typebuf_typed()
7805 && (vim_strchr(p_slm, c) != NULL));
7806}
7807
7808/*
7809 * Start Visual mode "c".
7810 * Should set VIsual_select before calling this.
7811 */
7812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007813n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007815#ifdef FEAT_CONCEAL
7816 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007817 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007818#endif
7819
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 VIsual_mode = c;
7821 VIsual_active = TRUE;
7822 VIsual_reselect = TRUE;
7823#ifdef FEAT_VIRTUALEDIT
7824 /* Corner case: the 0 position in a tab may change when going into
7825 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7826 */
7827 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007828 {
7829 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832#endif
7833 VIsual = curwin->w_cursor;
7834
7835#ifdef FEAT_FOLDING
7836 foldAdjustVisual();
7837#endif
7838
7839#ifdef FEAT_MOUSE
7840 setmouse();
7841#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007842#ifdef FEAT_CONCEAL
7843 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007844 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007845#endif
7846
Bram Moolenaar09df3122006-01-23 22:23:09 +00007847 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 redraw_cmdline = TRUE; /* show visual mode later */
7849#ifdef FEAT_CLIPBOARD
7850 /* Make sure the clipboard gets updated. Needed because start and
7851 * end may still be the same, and the selection needs to be owned */
7852 clip_star.vmode = NUL;
7853#endif
7854
7855 /* Only need to redraw this line, unless still need to redraw an old
7856 * Visual area (when 'lazyredraw' is set). */
7857 if (curwin->w_redr_type < INVERTED)
7858 {
7859 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7860 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7861 }
7862}
7863
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
7865/*
7866 * CTRL-W: Window commands
7867 */
7868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007869nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007871 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007872 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007873 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007874 cap->cmdchar = ':';
7875 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007876 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007877 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007878 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880}
7881
7882/*
7883 * CTRL-Z: Suspend
7884 */
7885 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007886nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (VIsual_active)
7890 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 do_cmdline_cmd((char_u *)"st");
7892}
7893
7894/*
7895 * Commands starting with "g".
7896 */
7897 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007898nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 int i;
7903 int flag = FALSE;
7904
7905 switch (cap->nchar)
7906 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007907 case Ctrl_A:
7908 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#ifdef MEM_PROFILE
7910 /*
7911 * "g^A": dump log of used memory.
7912 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007913 if (!VIsual_active && cap->nchar == Ctrl_A)
7914 vim_mem_profile_dump();
7915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007917 /*
7918 * "g^A/g^X": sequentially increment visually selected region
7919 */
7920 if (VIsual_active)
7921 {
7922 cap->arg = TRUE;
7923 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007924 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007925 nv_addsub(cap);
7926 }
7927 else
7928 clearopbeep(oap);
7929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 /*
7932 * "gR": Enter virtual replace mode.
7933 */
7934 case 'R':
7935 cap->arg = TRUE;
7936 nv_Replace(cap);
7937 break;
7938
7939 case 'r':
7940 nv_vreplace(cap);
7941 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942
7943 case '&':
7944 do_cmdline_cmd((char_u *)"%s//~/&");
7945 break;
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 /*
7948 * "gv": Reselect the previous Visual area. If Visual already active,
7949 * exchange previous and current Visual area.
7950 */
7951 case 'v':
7952 if (checkclearop(oap))
7953 break;
7954
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007955 if ( curbuf->b_visual.vi_start.lnum == 0
7956 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7957 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 beep_flush();
7959 else
7960 {
7961 /* set w_cursor to the start of the Visual area, tpos to the end */
7962 if (VIsual_active)
7963 {
7964 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007965 VIsual_mode = curbuf->b_visual.vi_mode;
7966 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967# ifdef FEAT_EVAL
7968 curbuf->b_visual_mode_eval = i;
7969# endif
7970 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007971 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7972 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007974 tpos = curbuf->b_visual.vi_end;
7975 curbuf->b_visual.vi_end = curwin->w_cursor;
7976 curwin->w_cursor = curbuf->b_visual.vi_start;
7977 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 }
7979 else
7980 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007981 VIsual_mode = curbuf->b_visual.vi_mode;
7982 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7983 tpos = curbuf->b_visual.vi_end;
7984 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
7986
7987 VIsual_active = TRUE;
7988 VIsual_reselect = TRUE;
7989
7990 /* Set Visual to the start and w_cursor to the end of the Visual
7991 * area. Make sure they are on an existing character. */
7992 check_cursor();
7993 VIsual = curwin->w_cursor;
7994 curwin->w_cursor = tpos;
7995 check_cursor();
7996 update_topline();
7997 /*
7998 * When called from normal "g" command: start Select mode when
7999 * 'selectmode' contains "cmd". When called for K_SELECT, always
8000 * start Select mode.
8001 */
8002 if (cap->arg)
8003 VIsual_select = TRUE;
8004 else
8005 may_start_select('c');
8006#ifdef FEAT_MOUSE
8007 setmouse();
8008#endif
8009#ifdef FEAT_CLIPBOARD
8010 /* Make sure the clipboard gets updated. Needed because start and
8011 * end are still the same, and the selection needs to be owned */
8012 clip_star.vmode = NUL;
8013#endif
8014 redraw_curbuf_later(INVERTED);
8015 showmode();
8016 }
8017 break;
8018 /*
8019 * "gV": Don't reselect the previous Visual area after a Select mode
8020 * mapping of menu.
8021 */
8022 case 'V':
8023 VIsual_reselect = FALSE;
8024 break;
8025
8026 /*
8027 * "gh": start Select mode.
8028 * "gH": start Select line mode.
8029 * "g^H": start Select block mode.
8030 */
8031 case K_BS:
8032 cap->nchar = Ctrl_H;
8033 /* FALLTHROUGH */
8034 case 'h':
8035 case 'H':
8036 case Ctrl_H:
8037# ifdef EBCDIC
8038 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8039 if (cap->nchar == Ctrl_H)
8040 cap->cmdchar = Ctrl_V;
8041 else
8042# endif
8043 cap->cmdchar = cap->nchar + ('v' - 'h');
8044 cap->arg = TRUE;
8045 nv_visual(cap);
8046 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008047
8048 /* "gn", "gN" visually select next/previous search match
8049 * "gn" selects next match
8050 * "gN" selects previous match
8051 */
8052 case 'N':
8053 case 'n':
8054 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008055 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008056 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057
8058 /*
8059 * "gj" and "gk" two new funny movement keys -- up and down
8060 * movement based on *screen* line rather than *file* line.
8061 */
8062 case 'j':
8063 case K_DOWN:
8064 /* with 'nowrap' it works just like the normal "j" command; also when
8065 * in a closed fold */
8066 if (!curwin->w_p_wrap
8067#ifdef FEAT_FOLDING
8068 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8069#endif
8070 )
8071 {
8072 oap->motion_type = MLINE;
8073 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8074 }
8075 else
8076 i = nv_screengo(oap, FORWARD, cap->count1);
8077 if (i == FAIL)
8078 clearopbeep(oap);
8079 break;
8080
8081 case 'k':
8082 case K_UP:
8083 /* with 'nowrap' it works just like the normal "k" command; also when
8084 * in a closed fold */
8085 if (!curwin->w_p_wrap
8086#ifdef FEAT_FOLDING
8087 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8088#endif
8089 )
8090 {
8091 oap->motion_type = MLINE;
8092 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8093 }
8094 else
8095 i = nv_screengo(oap, BACKWARD, cap->count1);
8096 if (i == FAIL)
8097 clearopbeep(oap);
8098 break;
8099
8100 /*
8101 * "gJ": join two lines without inserting a space.
8102 */
8103 case 'J':
8104 nv_join(cap);
8105 break;
8106
8107 /*
8108 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8109 * "gm": middle of "g0" and "g$".
8110 */
8111 case '^':
8112 flag = TRUE;
8113 /* FALLTHROUGH */
8114
8115 case '0':
8116 case 'm':
8117 case K_HOME:
8118 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 oap->motion_type = MCHAR;
8120 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008121 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008123 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 int width2 = width1 + curwin_col_off2();
8125
8126 validate_virtcol();
8127 i = 0;
8128 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8129 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8130 }
8131 else
8132 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008133 /* Go to the middle of the screen line. When 'number' or
8134 * 'relativenumber' is on and lines are wrapping the middle can be more
8135 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008137 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 + ((curwin->w_p_wrap && i > 0)
8139 ? curwin_col_off2() : 0)) / 2;
8140 coladvance((colnr_T)i);
8141 if (flag)
8142 {
8143 do
8144 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008145 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008146 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 curwin->w_set_curswant = TRUE;
8149 break;
8150
8151 case '_':
8152 /* "g_": to the last non-blank character in the line or <count> lines
8153 * downward. */
8154 cap->oap->motion_type = MCHAR;
8155 cap->oap->inclusive = TRUE;
8156 curwin->w_curswant = MAXCOL;
8157 if (cursor_down((long)(cap->count1 - 1),
8158 cap->oap->op_type == OP_NOP) == FAIL)
8159 clearopbeep(cap->oap);
8160 else
8161 {
8162 char_u *ptr = ml_get_curline();
8163
8164 /* In Visual mode we may end up after the line. */
8165 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8166 --curwin->w_cursor.col;
8167
8168 /* Decrease the cursor column until it's on a non-blank. */
8169 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008170 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 --curwin->w_cursor.col;
8172 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008173 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175 break;
8176
8177 case '$':
8178 case K_END:
8179 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 int col_off = curwin_col_off();
8182
8183 oap->motion_type = MCHAR;
8184 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008185 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {
8187 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8188 if (cap->count1 == 1)
8189 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008190 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 int width2 = width1 + curwin_col_off2();
8192
8193 validate_virtcol();
8194 i = width1 - 1;
8195 if (curwin->w_virtcol >= (colnr_T)width1)
8196 i += ((curwin->w_virtcol - width1) / width2 + 1)
8197 * width2;
8198 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008199
8200 /* Make sure we stick in this column. */
8201 validate_virtcol();
8202 curwin->w_curswant = curwin->w_virtcol;
8203 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8205 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8206 {
8207 /*
8208 * Check for landing on a character that got split at
8209 * the end of the line. We do not want to advance to
8210 * the next screen line.
8211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 if (curwin->w_virtcol > (colnr_T)i)
8213 --curwin->w_cursor.col;
8214 }
8215#endif
8216 }
8217 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8218 clearopbeep(oap);
8219 }
8220 else
8221 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008222 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008224
8225 /* Make sure we stick in this column. */
8226 validate_virtcol();
8227 curwin->w_curswant = curwin->w_virtcol;
8228 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 }
8230 }
8231 break;
8232
8233 /*
8234 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8235 */
8236 case '*':
8237 case '#':
8238#if POUND != '#'
8239 case POUND: /* pound sign (sometimes equal to '#') */
8240#endif
8241 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8242 case ']': /* :tselect for current identifier */
8243 nv_ident(cap);
8244 break;
8245
8246 /*
8247 * ge and gE: go back to end of word
8248 */
8249 case 'e':
8250 case 'E':
8251 oap->motion_type = MCHAR;
8252 curwin->w_set_curswant = TRUE;
8253 oap->inclusive = TRUE;
8254 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8255 clearopbeep(oap);
8256 break;
8257
8258 /*
8259 * "g CTRL-G": display info about cursor position
8260 */
8261 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008262 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 break;
8264
8265 /*
8266 * "gi": start Insert at the last position.
8267 */
8268 case 'i':
8269 if (curbuf->b_last_insert.lnum != 0)
8270 {
8271 curwin->w_cursor = curbuf->b_last_insert;
8272 check_cursor_lnum();
8273 i = (int)STRLEN(ml_get_curline());
8274 if (curwin->w_cursor.col > (colnr_T)i)
8275 {
8276#ifdef FEAT_VIRTUALEDIT
8277 if (virtual_active())
8278 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8279#endif
8280 curwin->w_cursor.col = i;
8281 }
8282 }
8283 cap->cmdchar = 'i';
8284 nv_edit(cap);
8285 break;
8286
8287 /*
8288 * "gI": Start insert in column 1.
8289 */
8290 case 'I':
8291 beginline(0);
8292 if (!checkclearopq(oap))
8293 invoke_edit(cap, FALSE, 'g', FALSE);
8294 break;
8295
8296#ifdef FEAT_SEARCHPATH
8297 /*
8298 * "gf": goto file, edit file under cursor
8299 * "]f" and "[f": can also be used.
8300 */
8301 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008302 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 nv_gotofile(cap);
8304 break;
8305#endif
8306
8307 /* "g'm" and "g`m": jump to mark without setting pcmark */
8308 case '\'':
8309 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008310 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 case '`':
8312 nv_gomark(cap);
8313 break;
8314
8315 /*
8316 * "gs": Goto sleep.
8317 */
8318 case 's':
8319 do_sleep(cap->count1 * 1000L);
8320 break;
8321
8322 /*
8323 * "ga": Display the ascii value of the character under the
8324 * cursor. It is displayed in decimal, hex, and octal. -- webb
8325 */
8326 case 'a':
8327 do_ascii(NULL);
8328 break;
8329
8330#ifdef FEAT_MBYTE
8331 /*
8332 * "g8": Display the bytes used for the UTF-8 character under the
8333 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008334 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 */
8336 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008337 if (cap->count0 == 8)
8338 utf_find_illegal();
8339 else
8340 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 break;
8342#endif
8343
Bram Moolenaar60402d62017-04-20 18:54:50 +02008344 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008345 case '<':
8346 show_sb_text();
8347 break;
8348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 /*
8350 * "gg": Goto the first line in file. With a count it goes to
8351 * that line number like for "G". -- webb
8352 */
8353 case 'g':
8354 cap->arg = FALSE;
8355 nv_goto(cap);
8356 break;
8357
8358 /*
8359 * Two-character operators:
8360 * "gq" Format text
8361 * "gw" Format text and keep cursor position
8362 * "g~" Toggle the case of the text.
8363 * "gu" Change text to lower case.
8364 * "gU" Change text to upper case.
8365 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008366 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 */
8368 case 'q':
8369 case 'w':
8370 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008371 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 case '~':
8373 case 'u':
8374 case 'U':
8375 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008376 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 nv_operator(cap);
8378 break;
8379
8380 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008381 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 * current function
8383 * "gD": idem, but in the current file.
8384 */
8385 case 'd':
8386 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008387 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 break;
8389
8390#ifdef FEAT_MOUSE
8391 /*
8392 * g<*Mouse> : <C-*mouse>
8393 */
8394 case K_MIDDLEMOUSE:
8395 case K_MIDDLEDRAG:
8396 case K_MIDDLERELEASE:
8397 case K_LEFTMOUSE:
8398 case K_LEFTDRAG:
8399 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008400 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 case K_RIGHTMOUSE:
8402 case K_RIGHTDRAG:
8403 case K_RIGHTRELEASE:
8404 case K_X1MOUSE:
8405 case K_X1DRAG:
8406 case K_X1RELEASE:
8407 case K_X2MOUSE:
8408 case K_X2DRAG:
8409 case K_X2RELEASE:
8410 mod_mask = MOD_MASK_CTRL;
8411 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8412 break;
8413#endif
8414
8415 case K_IGNORE:
8416 break;
8417
8418 /*
8419 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8420 */
8421 case 'p':
8422 case 'P':
8423 nv_put(cap);
8424 break;
8425
8426#ifdef FEAT_BYTEOFF
8427 /* "go": goto byte count from start of buffer */
8428 case 'o':
8429 goto_byte(cap->count0);
8430 break;
8431#endif
8432
8433 /* "gQ": improved Ex mode */
8434 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008435 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {
8437 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008438 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 break;
8440 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (!checkclearopq(oap))
8443 do_exmode(TRUE);
8444 break;
8445
8446#ifdef FEAT_JUMPLIST
8447 case ',':
8448 nv_pcmark(cap);
8449 break;
8450
8451 case ';':
8452 cap->count1 = -cap->count1;
8453 nv_pcmark(cap);
8454 break;
8455#endif
8456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008457 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008458 if (!checkclearop(oap))
8459 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008460 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008461 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008462 if (!checkclearop(oap))
8463 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008464 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008465
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008466 case '+':
8467 case '-': /* "g+" and "g-": undo or redo along the timeline */
8468 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008469 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008470 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008471 break;
8472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 default:
8474 clearopbeep(oap);
8475 break;
8476 }
8477}
8478
8479/*
8480 * Handle "o" and "O" commands.
8481 */
8482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008483n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008485#ifdef FEAT_CONCEAL
8486 linenr_T oldline = curwin->w_cursor.lnum;
8487#endif
8488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (!checkclearopq(cap->oap))
8490 {
8491#ifdef FEAT_FOLDING
8492 if (cap->cmdchar == 'O')
8493 /* Open above the first line of a folded sequence of lines */
8494 (void)hasFolding(curwin->w_cursor.lnum,
8495 &curwin->w_cursor.lnum, NULL);
8496 else
8497 /* Open below the last line of a folded sequence of lines */
8498 (void)hasFolding(curwin->w_cursor.lnum,
8499 NULL, &curwin->w_cursor.lnum);
8500#endif
8501 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8502 (cap->cmdchar == 'O' ? 1 : 0)),
8503 (linenr_T)(curwin->w_cursor.lnum +
8504 (cap->cmdchar == 'o' ? 1 : 0))
8505 ) == OK
8506 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8507#ifdef FEAT_COMMENTS
8508 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8509#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008510 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008512#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008513 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008514 update_single_line(curwin, oldline);
8515#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008516 /* When '#' is in 'cpoptions' ignore the count. */
8517 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8518 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008519#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008520 if (curwin->w_p_cul)
8521 /* force redraw of cursorline */
8522 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8525 }
8526 }
8527}
8528
8529/*
8530 * "." command: redo last change.
8531 */
8532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008533nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 if (!checkclearopq(cap->oap))
8536 {
8537 /*
8538 * If "restart_edit" is TRUE, the last but one command is repeated
8539 * instead of the last command (inserting text). This is used for
8540 * CTRL-O <.> in insert mode.
8541 */
8542 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8543 clearopbeep(cap->oap);
8544 }
8545}
8546
8547/*
8548 * CTRL-R: undo undo
8549 */
8550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008551nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 if (!checkclearopq(cap->oap))
8554 {
8555 u_redo((int)cap->count1);
8556 curwin->w_set_curswant = TRUE;
8557 }
8558}
8559
8560/*
8561 * Handle "U" command.
8562 */
8563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008564nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008567 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
8569 /* translate "gUU" to "gUgU" */
8570 cap->cmdchar = 'g';
8571 cap->nchar = 'U';
8572 nv_operator(cap);
8573 }
8574 else if (!checkclearopq(cap->oap))
8575 {
8576 u_undoline();
8577 curwin->w_set_curswant = TRUE;
8578 }
8579}
8580
8581/*
8582 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8583 * single character.
8584 */
8585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008586nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008588 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008589 {
8590#ifdef FEAT_JOB_CHANNEL
8591 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8592 {
8593 clearopbeep(cap->oap);
8594 return;
8595 }
8596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 else
8600 nv_operator(cap);
8601}
8602
8603/*
8604 * Handle an operator command.
8605 * The actual work is done by do_pending_operator().
8606 */
8607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008608nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 int op_type;
8611
8612 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008613#ifdef FEAT_JOB_CHANNEL
8614 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8615 {
8616 clearopbeep(cap->oap);
8617 return;
8618 }
8619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 if (op_type == cap->oap->op_type) /* double operator works on lines */
8622 nv_lineop(cap);
8623 else if (!checkclearop(cap->oap))
8624 {
8625 cap->oap->start = curwin->w_cursor;
8626 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008627#ifdef FEAT_EVAL
8628 set_op_var(op_type);
8629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 }
8631}
8632
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008633#ifdef FEAT_EVAL
8634/*
8635 * Set v:operator to the characters for "optype".
8636 */
8637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008638set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008639{
8640 char_u opchars[3];
8641
8642 if (optype == OP_NOP)
8643 set_vim_var_string(VV_OP, NULL, 0);
8644 else
8645 {
8646 opchars[0] = get_op_char(optype);
8647 opchars[1] = get_extra_op_char(optype);
8648 opchars[2] = NUL;
8649 set_vim_var_string(VV_OP, opchars, -1);
8650 }
8651}
8652#endif
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654/*
8655 * Handle linewise operator "dd", "yy", etc.
8656 *
8657 * "_" is is a strange motion command that helps make operators more logical.
8658 * It is actually implemented, but not documented in the real Vi. This motion
8659 * command actually refers to "the current line". Commands like "dd" and "yy"
8660 * are really an alternate form of "d_" and "y_". It does accept a count, so
8661 * "d3_" works to delete 3 lines.
8662 */
8663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008664nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666 cap->oap->motion_type = MLINE;
8667 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8668 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008669 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8670 && cap->oap->motion_force != 'v'
8671 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 || cap->oap->op_type == OP_LSHIFT
8673 || cap->oap->op_type == OP_RSHIFT)
8674 beginline(BL_SOL | BL_FIX);
8675 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8676 beginline(BL_WHITE | BL_FIX);
8677}
8678
8679/*
8680 * <Home> command.
8681 */
8682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008683nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008685 /* CTRL-HOME is like "gg" */
8686 if (mod_mask & MOD_MASK_CTRL)
8687 nv_goto(cap);
8688 else
8689 {
8690 cap->count0 = 1;
8691 nv_pipe(cap);
8692 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008693 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8694 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
8696
8697/*
8698 * "|" command.
8699 */
8700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008701nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 cap->oap->motion_type = MCHAR;
8704 cap->oap->inclusive = FALSE;
8705 beginline(0);
8706 if (cap->count0 > 0)
8707 {
8708 coladvance((colnr_T)(cap->count0 - 1));
8709 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8710 }
8711 else
8712 curwin->w_curswant = 0;
8713 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008714 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 curwin->w_set_curswant = FALSE;
8716}
8717
8718/*
8719 * Handle back-word command "b" and "B".
8720 * cap->arg is 1 for "B"
8721 */
8722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008723nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 cap->oap->motion_type = MCHAR;
8726 cap->oap->inclusive = FALSE;
8727 curwin->w_set_curswant = TRUE;
8728 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8729 clearopbeep(cap->oap);
8730#ifdef FEAT_FOLDING
8731 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8732 foldOpenCursor();
8733#endif
8734}
8735
8736/*
8737 * Handle word motion commands "e", "E", "w" and "W".
8738 * cap->arg is TRUE for "E" and "W".
8739 */
8740 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008741nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
8743 int n;
8744 int word_end;
8745 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008746 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747
8748 /*
8749 * Set inclusive for the "E" and "e" command.
8750 */
8751 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8752 word_end = TRUE;
8753 else
8754 word_end = FALSE;
8755 cap->oap->inclusive = word_end;
8756
8757 /*
8758 * "cw" and "cW" are a special case.
8759 */
8760 if (!word_end && cap->oap->op_type == OP_CHANGE)
8761 {
8762 n = gchar_cursor();
8763 if (n != NUL) /* not an empty line */
8764 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008765 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
8767 /*
8768 * Reproduce a funny Vi behaviour: "cw" on a blank only
8769 * changes one character, not all blanks until the start of
8770 * the next word. Only do this when the 'w' flag is included
8771 * in 'cpoptions'.
8772 */
8773 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8774 {
8775 cap->oap->inclusive = TRUE;
8776 cap->oap->motion_type = MCHAR;
8777 return;
8778 }
8779 }
8780 else
8781 {
8782 /*
8783 * This is a little strange. To match what the real Vi does,
8784 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8785 * that we are not on a space or a TAB. This seems impolite
8786 * at first, but it's really more what we mean when we say
8787 * 'cw'.
8788 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008789 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 * will change only one character! This is done by setting
8791 * flag.
8792 */
8793 cap->oap->inclusive = TRUE;
8794 word_end = TRUE;
8795 flag = TRUE;
8796 }
8797 }
8798 }
8799
8800 cap->oap->motion_type = MCHAR;
8801 curwin->w_set_curswant = TRUE;
8802 if (word_end)
8803 n = end_word(cap->count1, cap->arg, flag, FALSE);
8804 else
8805 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8806
Bram Moolenaardfefb982008-04-01 10:06:39 +00008807 /* Don't leave the cursor on the NUL past the end of line. Unless we
8808 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008809 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008810 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811
8812 if (n == FAIL && cap->oap->op_type == OP_NOP)
8813 clearopbeep(cap->oap);
8814 else
8815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817#ifdef FEAT_FOLDING
8818 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8819 foldOpenCursor();
8820#endif
8821 }
8822}
8823
8824/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008825 * Used after a movement command: If the cursor ends up on the NUL after the
8826 * end of the line, may move it back to the last character and make the motion
8827 * inclusive.
8828 */
8829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008830adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008831{
8832 /* The cursor cannot remain on the NUL when:
8833 * - the column is > 0
8834 * - not in Visual mode or 'selection' is "o"
8835 * - 'virtualedit' is not "all" and not "onemore".
8836 */
8837 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008838 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008839#ifdef FEAT_VIRTUALEDIT
8840 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8841#endif
8842 )
8843 {
8844 --curwin->w_cursor.col;
8845#ifdef FEAT_MBYTE
8846 /* prevent cursor from moving on the trail byte */
8847 if (has_mbyte)
8848 mb_adjust_cursor();
8849#endif
8850 oap->inclusive = TRUE;
8851 }
8852}
8853
8854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 * "0" and "^" commands.
8856 * cap->arg is the argument for beginline().
8857 */
8858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008859nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 cap->oap->motion_type = MCHAR;
8862 cap->oap->inclusive = FALSE;
8863 beginline(cap->arg);
8864#ifdef FEAT_FOLDING
8865 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8866 foldOpenCursor();
8867#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008868 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8869 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872/*
8873 * In exclusive Visual mode, may include the last character.
8874 */
8875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008876adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
8878 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008879 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008881#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (has_mbyte)
8883 inc_cursor();
8884 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 ++curwin->w_cursor.col;
8887 cap->oap->inclusive = FALSE;
8888 }
8889}
8890
8891/*
8892 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8893 * Should check VIsual_mode before calling this.
8894 * Returns TRUE when backed up to the previous line.
8895 */
8896 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008897unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 pos_T *pp;
8900
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008901 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008903 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 pp = &curwin->w_cursor;
8905 else
8906 pp = &VIsual;
8907#ifdef FEAT_VIRTUALEDIT
8908 if (pp->coladd > 0)
8909 --pp->coladd;
8910 else
8911#endif
8912 if (pp->col > 0)
8913 {
8914 --pp->col;
8915#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008916 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917#endif
8918 }
8919 else if (pp->lnum > 1)
8920 {
8921 --pp->lnum;
8922 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8923 return TRUE;
8924 }
8925 }
8926 return FALSE;
8927}
8928
8929/*
8930 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8931 */
8932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008933nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 if (VIsual_active)
8936 VIsual_select = TRUE;
8937 else if (VIsual_reselect)
8938 {
8939 cap->nchar = 'v'; /* fake "gv" command */
8940 cap->arg = TRUE;
8941 nv_g_cmd(cap);
8942 }
8943}
8944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945
8946/*
8947 * "G", "gg", CTRL-END, CTRL-HOME.
8948 * cap->arg is TRUE for "G".
8949 */
8950 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008951nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953 linenr_T lnum;
8954
8955 if (cap->arg)
8956 lnum = curbuf->b_ml.ml_line_count;
8957 else
8958 lnum = 1L;
8959 cap->oap->motion_type = MLINE;
8960 setpcmark();
8961
8962 /* When a count is given, use it instead of the default lnum */
8963 if (cap->count0 != 0)
8964 lnum = cap->count0;
8965 if (lnum < 1L)
8966 lnum = 1L;
8967 else if (lnum > curbuf->b_ml.ml_line_count)
8968 lnum = curbuf->b_ml.ml_line_count;
8969 curwin->w_cursor.lnum = lnum;
8970 beginline(BL_SOL | BL_FIX);
8971#ifdef FEAT_FOLDING
8972 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8973 foldOpenCursor();
8974#endif
8975}
8976
8977/*
8978 * CTRL-\ in Normal mode.
8979 */
8980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008981nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8984 {
8985 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008986 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 clear_cmdline = TRUE; /* unshow mode later */
8988 restart_edit = 0;
8989#ifdef FEAT_CMDWIN
8990 if (cmdwin_type != 0)
8991 cmdwin_result = Ctrl_C;
8992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 if (VIsual_active)
8994 {
8995 end_visual_mode(); /* stop Visual */
8996 redraw_curbuf_later(INVERTED);
8997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8999 if (cap->nchar == Ctrl_G && p_im)
9000 restart_edit = 'a';
9001 }
9002 else
9003 clearopbeep(cap->oap);
9004}
9005
9006/*
9007 * ESC in Normal mode: beep, but don't flush buffers.
9008 * Don't even beep if we are canceling a command.
9009 */
9010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009011nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 int no_reason;
9014
9015 no_reason = (cap->oap->op_type == OP_NOP
9016 && cap->opcount == 0
9017 && cap->count0 == 0
9018 && cap->oap->regname == 0
9019 && !p_im);
9020
9021 if (cap->arg) /* TRUE for CTRL-C */
9022 {
9023 if (restart_edit == 0
9024#ifdef FEAT_CMDWIN
9025 && cmdwin_type == 0
9026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009029 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030
9031 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9032 * set again below when halfway a mapping. */
9033 if (!p_im)
9034 restart_edit = 0;
9035#ifdef FEAT_CMDWIN
9036 if (cmdwin_type != 0)
9037 {
9038 cmdwin_result = K_IGNORE;
9039 got_int = FALSE; /* don't stop executing autocommands et al. */
9040 return;
9041 }
9042#endif
9043 }
9044
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 if (VIsual_active)
9046 {
9047 end_visual_mode(); /* stop Visual */
9048 check_cursor_col(); /* make sure cursor is not beyond EOL */
9049 curwin->w_set_curswant = TRUE;
9050 redraw_curbuf_later(INVERTED);
9051 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009052 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009053 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 clearop(cap->oap);
9055
9056 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9057 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009058 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 restart_edit = 'a';
9060}
9061
9062/*
9063 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009064 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 */
9066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009067nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068{
9069 /* <Insert> is equal to "i" */
9070 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9071 cap->cmdchar = 'i';
9072
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 /* in Visual mode "A" and "I" are an operator */
9074 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009075 {
9076#ifdef FEAT_TERMINAL
9077 if (term_in_normal_mode())
9078 {
9079 end_visual_mode();
9080 clearop(cap->oap);
9081 term_enter_job_mode();
9082 return;
9083 }
9084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
9088 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009089 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9090 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 {
9092#ifdef FEAT_TEXTOBJ
9093 nv_object(cap);
9094#else
9095 clearopbeep(cap->oap);
9096#endif
9097 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009098#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009099 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009100 {
9101 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009102 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009103 return;
9104 }
9105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 else if (!curbuf->b_p_ma && !p_im)
9107 {
9108 /* Only give this error when 'insertmode' is off. */
9109 EMSG(_(e_modifiable));
9110 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009111 if (cap->cmdchar == K_PS)
9112 /* drop the pasted text */
9113 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009115 else if (cap->cmdchar == K_PS && VIsual_active)
9116 {
9117 pos_T old_pos = curwin->w_cursor;
9118 pos_T old_visual = VIsual;
9119
9120 /* In Visual mode the selected text is deleted. */
9121 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9122 {
9123 shift_delete_registers();
9124 cap->oap->regname = '1';
9125 }
9126 else
9127 cap->oap->regname = '-';
9128 cap->cmdchar = 'd';
9129 cap->nchar = NUL;
9130 nv_operator(cap);
9131 do_pending_operator(cap, 0, FALSE);
9132 cap->cmdchar = K_PS;
9133
9134 /* When the last char in the line was deleted then append. Detect this
9135 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009136 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9137 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009138 inc_cursor();
9139
9140 /* Insert to replace the deleted text with the pasted text. */
9141 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143 else if (!checkclearopq(cap->oap))
9144 {
9145 switch (cap->cmdchar)
9146 {
9147 case 'A': /* "A"ppend after the line */
9148 curwin->w_set_curswant = TRUE;
9149#ifdef FEAT_VIRTUALEDIT
9150 if (ve_flags == VE_ALL)
9151 {
9152 int save_State = State;
9153
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009154 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 * character past the end of the line */
9156 State = INSERT;
9157 coladvance((colnr_T)MAXCOL);
9158 State = save_State;
9159 }
9160 else
9161#endif
9162 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9163 break;
9164
9165 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009166 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9167 beginline(BL_WHITE);
9168 else
9169 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 break;
9171
Bram Moolenaara1891842017-02-04 21:34:31 +01009172 case K_PS:
9173 /* Bracketed paste works like "a"ppend, unless the cursor is in
9174 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009175 if (curwin->w_cursor.col == 0)
9176 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009177 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009178
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9180#ifdef FEAT_VIRTUALEDIT
9181 /* increment coladd when in virtual space, increment the
9182 * column otherwise, also to append after an unprintable char */
9183 if (virtual_active()
9184 && (curwin->w_cursor.coladd > 0
9185 || *ml_get_cursor() == NUL
9186 || *ml_get_cursor() == TAB))
9187 curwin->w_cursor.coladd++;
9188 else
9189#endif
9190 if (*ml_get_cursor() != NUL)
9191 inc_cursor();
9192 break;
9193 }
9194
9195#ifdef FEAT_VIRTUALEDIT
9196 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9197 {
9198 int save_State = State;
9199
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009200 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 * character past the end of the line */
9202 State = INSERT;
9203 coladvance(getviscol());
9204 State = save_State;
9205 }
9206#endif
9207
9208 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9209 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009210 else if (cap->cmdchar == K_PS)
9211 /* drop the pasted text */
9212 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213}
9214
9215/*
9216 * Invoke edit() and take care of "restart_edit" and the return value.
9217 */
9218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009219invoke_edit(
9220 cmdarg_T *cap,
9221 int repl, /* "r" or "gr" command */
9222 int cmd,
9223 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 int restart_edit_save = 0;
9226
9227 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9228 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9229 * it. */
9230 if (repl || !stuff_empty())
9231 restart_edit_save = restart_edit;
9232 else
9233 restart_edit_save = 0;
9234
9235 /* Always reset "restart_edit", this is not a restarted edit. */
9236 restart_edit = 0;
9237
9238 if (edit(cmd, startln, cap->count1))
9239 cap->retval |= CA_COMMAND_BUSY;
9240
9241 if (restart_edit == 0)
9242 restart_edit = restart_edit_save;
9243}
9244
9245#ifdef FEAT_TEXTOBJ
9246/*
9247 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9248 */
9249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009250nv_object(
9251 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
9253 int flag;
9254 int include;
9255 char_u *mps_save;
9256
9257 if (cap->cmdchar == 'i')
9258 include = FALSE; /* "ix" = inner object: exclude white space */
9259 else
9260 include = TRUE; /* "ax" = an object: include white space */
9261
9262 /* Make sure (), [], {} and <> are in 'matchpairs' */
9263 mps_save = curbuf->b_p_mps;
9264 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9265
9266 switch (cap->nchar)
9267 {
9268 case 'w': /* "aw" = a word */
9269 flag = current_word(cap->oap, cap->count1, include, FALSE);
9270 break;
9271 case 'W': /* "aW" = a WORD */
9272 flag = current_word(cap->oap, cap->count1, include, TRUE);
9273 break;
9274 case 'b': /* "ab" = a braces block */
9275 case '(':
9276 case ')':
9277 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9278 break;
9279 case 'B': /* "aB" = a Brackets block */
9280 case '{':
9281 case '}':
9282 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9283 break;
9284 case '[': /* "a[" = a [] block */
9285 case ']':
9286 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9287 break;
9288 case '<': /* "a<" = a <> block */
9289 case '>':
9290 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9291 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009292 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009293 /* Do not adjust oap->end in do_pending_operator()
9294 * otherwise there are different results for 'dit'
9295 * (note leading whitespace in last line):
9296 * 1) <b> 2) <b>
9297 * foobar foobar
9298 * </b> </b>
9299 */
9300 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009301 flag = current_tagblock(cap->oap, cap->count1, include);
9302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 case 'p': /* "ap" = a paragraph */
9304 flag = current_par(cap->oap, cap->count1, include, 'p');
9305 break;
9306 case 's': /* "as" = a sentence */
9307 flag = current_sent(cap->oap, cap->count1, include);
9308 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009309 case '"': /* "a"" = a double quoted string */
9310 case '\'': /* "a'" = a single quoted string */
9311 case '`': /* "a`" = a backtick quoted string */
9312 flag = current_quote(cap->oap, cap->count1, include,
9313 cap->nchar);
9314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315#if 0 /* TODO */
9316 case 'S': /* "aS" = a section */
9317 case 'f': /* "af" = a filename */
9318 case 'u': /* "au" = a URL */
9319#endif
9320 default:
9321 flag = FAIL;
9322 break;
9323 }
9324
9325 curbuf->b_p_mps = mps_save;
9326 if (flag == FAIL)
9327 clearopbeep(cap->oap);
9328 adjust_cursor_col();
9329 curwin->w_set_curswant = TRUE;
9330}
9331#endif
9332
9333/*
9334 * "q" command: Start/stop recording.
9335 * "q:", "q/", "q?": edit command-line in command-line window.
9336 */
9337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009338nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339{
9340 if (cap->oap->op_type == OP_FORMAT)
9341 {
9342 /* "gqq" is the same as "gqgq": format line */
9343 cap->cmdchar = 'g';
9344 cap->nchar = 'q';
9345 nv_operator(cap);
9346 }
9347 else if (!checkclearop(cap->oap))
9348 {
9349#ifdef FEAT_CMDWIN
9350 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9351 {
9352 stuffcharReadbuff(cap->nchar);
9353 stuffcharReadbuff(K_CMDWIN);
9354 }
9355 else
9356#endif
9357 /* (stop) recording into a named register, unless executing a
9358 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009359 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 clearopbeep(cap->oap);
9361 }
9362}
9363
9364/*
9365 * Handle the "@r" command.
9366 */
9367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009368nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 if (checkclearop(cap->oap))
9371 return;
9372#ifdef FEAT_EVAL
9373 if (cap->nchar == '=')
9374 {
9375 if (get_expr_register() == NUL)
9376 return;
9377 }
9378#endif
9379 while (cap->count1-- && !got_int)
9380 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009381 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 {
9383 clearopbeep(cap->oap);
9384 break;
9385 }
9386 line_breakcheck();
9387 }
9388}
9389
9390/*
9391 * Handle the CTRL-U and CTRL-D commands.
9392 */
9393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009394nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9397 || (cap->cmdchar == Ctrl_D
9398 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9399 clearopbeep(cap->oap);
9400 else if (!checkclearop(cap->oap))
9401 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9402}
9403
9404/*
9405 * Handle "J" or "gJ" command.
9406 */
9407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009408nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 if (VIsual_active) /* join the visual lines */
9411 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009412 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
9414 if (cap->count0 <= 1)
9415 cap->count0 = 2; /* default for join is two lines! */
9416 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9417 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009419 /* can't join when on the last line */
9420 if (cap->count0 <= 2)
9421 {
9422 clearopbeep(cap->oap);
9423 return;
9424 }
9425 cap->count0 = curbuf->b_ml.ml_line_count
9426 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009428
9429 prep_redo(cap->oap->regname, cap->count0,
9430 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9431 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 }
9433}
9434
9435/*
9436 * "P", "gP", "p" and "gp" commands.
9437 */
9438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009439nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 int regname = 0;
9442 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009443 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009444 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 int dir;
9446 int flags = 0;
9447
9448 if (cap->oap->op_type != OP_NOP)
9449 {
9450#ifdef FEAT_DIFF
9451 /* "dp" is ":diffput" */
9452 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9453 {
9454 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009455 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 }
9457 else
9458#endif
9459 clearopbeep(cap->oap);
9460 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009461#ifdef FEAT_JOB_CHANNEL
9462 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9463 {
9464 clearopbeep(cap->oap);
9465 }
9466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 else
9468 {
9469 dir = (cap->cmdchar == 'P'
9470 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9471 ? BACKWARD : FORWARD;
9472 prep_redo_cmd(cap);
9473 if (cap->cmdchar == 'g')
9474 flags |= PUT_CURSEND;
9475
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 if (VIsual_active)
9477 {
9478 /* Putting in Visual mode: The put text replaces the selected
9479 * text. First delete the selected text, then put the new text.
9480 * Need to save and restore the registers that the delete
9481 * overwrites if the old contents is being put.
9482 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009483 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009485#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009487#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009488 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009489 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009490#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
9494 )
9495 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009496 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 * put, save it first. */
9498 reg1 = get_register(regname, TRUE);
9499 }
9500
9501 /* Now delete the selected text. */
9502 cap->cmdchar = 'd';
9503 cap->nchar = NUL;
9504 cap->oap->regname = NUL;
9505 nv_operator(cap);
9506 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009507 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508
9509 /* delete PUT_LINE_BACKWARD; */
9510 cap->oap->regname = regname;
9511
9512 if (reg1 != NULL)
9513 {
9514 /* Delete probably changed the register we want to put, save
9515 * it first. Then put back what was there before the delete. */
9516 reg2 = get_register(regname, FALSE);
9517 put_register(regname, reg1);
9518 }
9519
9520 /* When deleted a linewise Visual area, put the register as
9521 * lines to avoid it joined with the next line. When deletion was
9522 * characterwise, split a line when putting lines. */
9523 if (VIsual_mode == 'V')
9524 flags |= PUT_LINE;
9525 else if (VIsual_mode == 'v')
9526 flags |= PUT_LINE_SPLIT;
9527 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9528 flags |= PUT_LINE_FORWARD;
9529 dir = BACKWARD;
9530 if ((VIsual_mode != 'V'
9531 && curwin->w_cursor.col < curbuf->b_op_start.col)
9532 || (VIsual_mode == 'V'
9533 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9534 /* cursor is at the end of the line or end of file, put
9535 * forward. */
9536 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009537 /* May have been reset in do_put(). */
9538 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 do_put(cap->oap->regname, dir, cap->count1, flags);
9541
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 /* If a register was saved, put it back now. */
9543 if (reg2 != NULL)
9544 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009545
9546 /* What to reselect with "gv"? Selecting the just put text seems to
9547 * be the most useful, since the original text was removed. */
9548 if (was_visual)
9549 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009550 curbuf->b_visual.vi_start = curbuf->b_op_start;
9551 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009552 /* need to adjust cursor position */
9553 if (*p_sel == 'e')
9554 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009555 }
9556
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009557 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009558 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009559 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009560 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009561 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009562
9563 /* If the cursor was in that line, move it to the end of the last
9564 * line. */
9565 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9566 {
9567 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9568 coladvance((colnr_T)MAXCOL);
9569 }
9570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 auto_format(FALSE, TRUE);
9572 }
9573}
9574
9575/*
9576 * "o" and "O" commands.
9577 */
9578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009579nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581#ifdef FEAT_DIFF
9582 /* "do" is ":diffget" */
9583 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9584 {
9585 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009586 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 }
9588 else
9589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (VIsual_active) /* switch start and end of visual */
9591 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009592#ifdef FEAT_JOB_CHANNEL
9593 else if (bt_prompt(curbuf))
9594 {
9595 clearopbeep(cap->oap);
9596 }
9597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 n_opencmd(cap);
9600}
9601
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602#ifdef FEAT_NETBEANS_INTG
9603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009604nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 netbeans_keycommand(cap->nchar);
9607}
9608#endif
9609
9610#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009612nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
9614 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9615}
9616#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009617
Bram Moolenaar3918c952005-03-15 22:34:55 +00009618/*
9619 * Trigger CursorHold event.
9620 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9621 * input buffer. "did_cursorhold" is set to avoid retriggering.
9622 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009624nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009625{
9626 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9627 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009628 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009629}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009630
9631/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009632 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009633 */
9634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009635get_op_vcol(
9636 oparg_T *oap,
9637 colnr_T redo_VIsual_vcol,
9638 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009639{
9640 colnr_T start, end;
9641
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009642 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009643 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009644 return;
9645
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009646 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009647
9648#ifdef FEAT_MBYTE
9649 /* prevent from moving onto a trail byte */
9650 if (has_mbyte)
9651 mb_adjustpos(curwin->w_buffer, &oap->end);
9652#endif
9653
9654 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009655
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009656 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009657 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009658 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9659
9660 if (start < oap->start_vcol)
9661 oap->start_vcol = start;
9662 if (end > oap->end_vcol)
9663 {
9664 if (initial && *p_sel == 'e' && start >= 1
9665 && start - 1 >= oap->end_vcol)
9666 oap->end_vcol = start - 1;
9667 else
9668 oap->end_vcol = end;
9669 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009670 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009671
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009672 /* if '$' was used, get oap->end_vcol from longest line */
9673 if (curwin->w_curswant == MAXCOL)
9674 {
9675 curwin->w_cursor.col = MAXCOL;
9676 oap->end_vcol = 0;
9677 for (curwin->w_cursor.lnum = oap->start.lnum;
9678 curwin->w_cursor.lnum <= oap->end.lnum;
9679 ++curwin->w_cursor.lnum)
9680 {
9681 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9682 if (end > oap->end_vcol)
9683 oap->end_vcol = end;
9684 }
9685 }
9686 else if (redo_VIsual_busy)
9687 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9688 /*
9689 * Correct oap->end.col and oap->start.col to be the
9690 * upper-left and lower-right corner of the block area.
9691 *
9692 * (Actually, this does convert column positions into character
9693 * positions)
9694 */
9695 curwin->w_cursor.lnum = oap->end.lnum;
9696 coladvance(oap->end_vcol);
9697 oap->end = curwin->w_cursor;
9698
9699 curwin->w_cursor = oap->start;
9700 coladvance(oap->start_vcol);
9701 oap->start = curwin->w_cursor;
9702}