blob: b9170ace5508dfe3b974a8a737643e3b0f32032a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
28static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
29#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 Moolenaar071d4272004-06-13 20:20:40 +000034 nv_compare __ARGS((const void *s1, const void *s2));
35static int find_command __ARGS((int cmdchar));
36static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000037static void op_function __ARGS((oparg_T *oap));
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void find_start_of_word __ARGS((pos_T *));
40static void find_end_of_word __ARGS((pos_T *));
41static int get_mouse_class __ARGS((char_u *p));
42#endif
Bram Moolenaar5d1bc782015-07-17 13:03:48 +020043static void prep_redo_visual __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static void prep_redo_cmd __ARGS((cmdarg_T *cap));
45static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
46static int checkclearop __ARGS((oparg_T *oap));
47static int checkclearopq __ARGS((oparg_T *oap));
48static void clearop __ARGS((oparg_T *oap));
49static void clearopbeep __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void unshift_special __ARGS((cmdarg_T *cap));
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +020051static void may_clear_cmdline __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052#ifdef FEAT_CMDL_INFO
53static void del_from_showcmd __ARGS((int));
54#endif
55
56/*
57 * nv_*(): functions called to handle Normal and Visual mode commands.
58 * n_*(): functions called to handle Normal mode commands.
59 * v_*(): functions called to handle Visual mode commands.
60 */
61static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000062static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void nv_error __ARGS((cmdarg_T *cap));
64static void nv_help __ARGS((cmdarg_T *cap));
65static void nv_addsub __ARGS((cmdarg_T *cap));
66static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000067static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
69#ifdef FEAT_MOUSE
70static void nv_mousescroll __ARGS((cmdarg_T *cap));
71static void nv_mouse __ARGS((cmdarg_T *cap));
72#endif
73static void nv_scroll_line __ARGS((cmdarg_T *cap));
74static void nv_zet __ARGS((cmdarg_T *cap));
75#ifdef FEAT_GUI
76static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
77static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
78#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000079#ifdef FEAT_GUI_TABLINE
80static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000081static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000083static void nv_exmode __ARGS((cmdarg_T *cap));
84static void nv_colon __ARGS((cmdarg_T *cap));
85static void nv_ctrlg __ARGS((cmdarg_T *cap));
86static void nv_ctrlh __ARGS((cmdarg_T *cap));
87static void nv_clear __ARGS((cmdarg_T *cap));
88static void nv_ctrlo __ARGS((cmdarg_T *cap));
89static void nv_hat __ARGS((cmdarg_T *cap));
90static void nv_Zet __ARGS((cmdarg_T *cap));
91static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000092static void nv_tagpop __ARGS((cmdarg_T *cap));
93static void nv_scroll __ARGS((cmdarg_T *cap));
94static void nv_right __ARGS((cmdarg_T *cap));
95static void nv_left __ARGS((cmdarg_T *cap));
96static void nv_up __ARGS((cmdarg_T *cap));
97static void nv_down __ARGS((cmdarg_T *cap));
98#ifdef FEAT_SEARCHPATH
99static void nv_gotofile __ARGS((cmdarg_T *cap));
100#endif
101static void nv_end __ARGS((cmdarg_T *cap));
102static void nv_dollar __ARGS((cmdarg_T *cap));
103static void nv_search __ARGS((cmdarg_T *cap));
104static void nv_next __ARGS((cmdarg_T *cap));
Bram Moolenaar46539112015-02-17 15:43:57 +0100105static int normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106static void nv_csearch __ARGS((cmdarg_T *cap));
107static void nv_brackets __ARGS((cmdarg_T *cap));
108static void nv_percent __ARGS((cmdarg_T *cap));
109static void nv_brace __ARGS((cmdarg_T *cap));
110static void nv_mark __ARGS((cmdarg_T *cap));
111static void nv_findpar __ARGS((cmdarg_T *cap));
112static void nv_undo __ARGS((cmdarg_T *cap));
113static void nv_kundo __ARGS((cmdarg_T *cap));
114static void nv_Replace __ARGS((cmdarg_T *cap));
115#ifdef FEAT_VREPLACE
116static void nv_vreplace __ARGS((cmdarg_T *cap));
117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118static void v_swap_corners __ARGS((int cmdchar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119static void nv_replace __ARGS((cmdarg_T *cap));
120static void n_swapchar __ARGS((cmdarg_T *cap));
121static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122static void v_visop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123static void nv_subst __ARGS((cmdarg_T *cap));
124static void nv_abbrev __ARGS((cmdarg_T *cap));
125static void nv_optrans __ARGS((cmdarg_T *cap));
126static void nv_gomark __ARGS((cmdarg_T *cap));
127static void nv_pcmark __ARGS((cmdarg_T *cap));
128static void nv_regname __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void nv_visual __ARGS((cmdarg_T *cap));
130static void n_start_visual_mode __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131static void nv_window __ARGS((cmdarg_T *cap));
132static void nv_suspend __ARGS((cmdarg_T *cap));
133static void nv_g_cmd __ARGS((cmdarg_T *cap));
134static void n_opencmd __ARGS((cmdarg_T *cap));
135static void nv_dot __ARGS((cmdarg_T *cap));
136static void nv_redo __ARGS((cmdarg_T *cap));
137static void nv_Undo __ARGS((cmdarg_T *cap));
138static void nv_tilde __ARGS((cmdarg_T *cap));
139static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000140#ifdef FEAT_EVAL
141static void set_op_var __ARGS((int optype));
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143static void nv_lineop __ARGS((cmdarg_T *cap));
144static void nv_home __ARGS((cmdarg_T *cap));
145static void nv_pipe __ARGS((cmdarg_T *cap));
146static void nv_bck_word __ARGS((cmdarg_T *cap));
147static void nv_wordcmd __ARGS((cmdarg_T *cap));
148static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000149static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void adjust_for_sel __ARGS((cmdarg_T *cap));
151static int unadjust_for_sel __ARGS((void));
152static void nv_select __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void nv_goto __ARGS((cmdarg_T *cap));
154static void nv_normal __ARGS((cmdarg_T *cap));
155static void nv_esc __ARGS((cmdarg_T *oap));
156static void nv_edit __ARGS((cmdarg_T *cap));
157static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
158#ifdef FEAT_TEXTOBJ
159static void nv_object __ARGS((cmdarg_T *cap));
160#endif
161static void nv_record __ARGS((cmdarg_T *cap));
162static void nv_at __ARGS((cmdarg_T *cap));
163static void nv_halfpage __ARGS((cmdarg_T *cap));
164static void nv_join __ARGS((cmdarg_T *cap));
165static void nv_put __ARGS((cmdarg_T *cap));
166static void nv_open __ARGS((cmdarg_T *cap));
167#ifdef FEAT_SNIFF
168static void nv_sniff __ARGS((cmdarg_T *cap));
169#endif
170#ifdef FEAT_NETBEANS_INTG
171static void nv_nbcmd __ARGS((cmdarg_T *cap));
172#endif
173#ifdef FEAT_DND
174static void nv_drop __ARGS((cmdarg_T *cap));
175#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000176#ifdef FEAT_AUTOCMD
177static void nv_cursorhold __ARGS((cmdarg_T *cap));
178#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +0200179static void get_op_vcol __ARGS((oparg_T *oap, colnr_T col, int initial));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000181static char *e_noident = N_("E349: No identifier under cursor");
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183/*
184 * Function to be called for a Normal or Visual mode command.
185 * The argument is a cmdarg_T.
186 */
187typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
188
189/* Values for cmd_flags. */
190#define NV_NCH 0x01 /* may need to get a second char */
191#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
192#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
193#define NV_LANG 0x08 /* second char needs language adjustment */
194
195#define NV_SS 0x10 /* may start selection */
196#define NV_SSS 0x20 /* may start selection with shift modifier */
197#define NV_STS 0x40 /* may stop selection without shift modif. */
198#define NV_RL 0x80 /* 'rightleft' modifies command */
199#define NV_KEEPREG 0x100 /* don't clear regname */
200#define NV_NCW 0x200 /* not allowed in command-line window */
201
202/*
203 * Generally speaking, every Normal mode command should either clear any
204 * pending operator (with *clearop*()), or set the motion type variable
205 * oap->motion_type.
206 *
207 * When a cursor motion command is made, it is marked as being a character or
208 * line oriented motion. Then, if an operator is in effect, the operation
209 * becomes character or line oriented accordingly.
210 */
211
212/*
213 * This table contains one entry for every Normal or Visual mode command.
214 * The order doesn't matter, init_normal_cmds() will create a sorted index.
215 * It is faster when all keys from zero to '~' are present.
216 */
217static const struct nv_cmd
218{
219 int cmd_char; /* (first) command character */
220 nv_func_T cmd_func; /* function for this command */
221 short_u cmd_flags; /* NV_ flags */
222 short cmd_arg; /* value for ca.arg */
223} nv_cmds[] =
224{
225 {NUL, nv_error, 0, 0},
226 {Ctrl_A, nv_addsub, 0, 0},
227 {Ctrl_B, nv_page, NV_STS, BACKWARD},
228 {Ctrl_C, nv_esc, 0, TRUE},
229 {Ctrl_D, nv_halfpage, 0, 0},
230 {Ctrl_E, nv_scroll_line, 0, TRUE},
231 {Ctrl_F, nv_page, NV_STS, FORWARD},
232 {Ctrl_G, nv_ctrlg, 0, 0},
233 {Ctrl_H, nv_ctrlh, 0, 0},
234 {Ctrl_I, nv_pcmark, 0, 0},
235 {NL, nv_down, 0, FALSE},
236 {Ctrl_K, nv_error, 0, 0},
237 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000238 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {Ctrl_N, nv_down, NV_STS, FALSE},
240 {Ctrl_O, nv_ctrlo, 0, 0},
241 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000242 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_R, nv_redo, 0, 0},
244 {Ctrl_S, nv_ignore, 0, 0},
245 {Ctrl_T, nv_tagpop, NV_NCW, 0},
246 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_V, nv_visual, 0, FALSE},
248 {'V', nv_visual, 0, FALSE},
249 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 {Ctrl_W, nv_window, 0, 0},
251 {Ctrl_X, nv_addsub, 0, 0},
252 {Ctrl_Y, nv_scroll_line, 0, FALSE},
253 {Ctrl_Z, nv_suspend, 0, 0},
254 {ESC, nv_esc, 0, FALSE},
255 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
256 {Ctrl_RSB, nv_ident, NV_NCW, 0},
257 {Ctrl_HAT, nv_hat, NV_NCW, 0},
258 {Ctrl__, nv_error, 0, 0},
259 {' ', nv_right, 0, 0},
260 {'!', nv_operator, 0, 0},
261 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
262 {'#', nv_ident, 0, 0},
263 {'$', nv_dollar, 0, 0},
264 {'%', nv_percent, 0, 0},
265 {'&', nv_optrans, 0, 0},
266 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
267 {'(', nv_brace, 0, BACKWARD},
268 {')', nv_brace, 0, FORWARD},
269 {'*', nv_ident, 0, 0},
270 {'+', nv_down, 0, TRUE},
271 {',', nv_csearch, 0, TRUE},
272 {'-', nv_up, 0, TRUE},
273 {'.', nv_dot, NV_KEEPREG, 0},
274 {'/', nv_search, 0, FALSE},
275 {'0', nv_beginline, 0, 0},
276 {'1', nv_ignore, 0, 0},
277 {'2', nv_ignore, 0, 0},
278 {'3', nv_ignore, 0, 0},
279 {'4', nv_ignore, 0, 0},
280 {'5', nv_ignore, 0, 0},
281 {'6', nv_ignore, 0, 0},
282 {'7', nv_ignore, 0, 0},
283 {'8', nv_ignore, 0, 0},
284 {'9', nv_ignore, 0, 0},
285 {':', nv_colon, 0, 0},
286 {';', nv_csearch, 0, FALSE},
287 {'<', nv_operator, NV_RL, 0},
288 {'=', nv_operator, 0, 0},
289 {'>', nv_operator, NV_RL, 0},
290 {'?', nv_search, 0, FALSE},
291 {'@', nv_at, NV_NCH_NOP, FALSE},
292 {'A', nv_edit, 0, 0},
293 {'B', nv_bck_word, 0, 1},
294 {'C', nv_abbrev, NV_KEEPREG, 0},
295 {'D', nv_abbrev, NV_KEEPREG, 0},
296 {'E', nv_wordcmd, 0, TRUE},
297 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
298 {'G', nv_goto, 0, TRUE},
299 {'H', nv_scroll, 0, 0},
300 {'I', nv_edit, 0, 0},
301 {'J', nv_join, 0, 0},
302 {'K', nv_ident, 0, 0},
303 {'L', nv_scroll, 0, 0},
304 {'M', nv_scroll, 0, 0},
305 {'N', nv_next, 0, SEARCH_REV},
306 {'O', nv_open, 0, 0},
307 {'P', nv_put, 0, 0},
308 {'Q', nv_exmode, NV_NCW, 0},
309 {'R', nv_Replace, 0, FALSE},
310 {'S', nv_subst, NV_KEEPREG, 0},
311 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
312 {'U', nv_Undo, 0, 0},
313 {'W', nv_wordcmd, 0, TRUE},
314 {'X', nv_abbrev, NV_KEEPREG, 0},
315 {'Y', nv_abbrev, NV_KEEPREG, 0},
316 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
317 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
318 {'\\', nv_error, 0, 0},
319 {']', nv_brackets, NV_NCH_ALW, FORWARD},
320 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
321 {'_', nv_lineop, 0, 0},
322 {'`', nv_gomark, NV_NCH_ALW, FALSE},
323 {'a', nv_edit, NV_NCH, 0},
324 {'b', nv_bck_word, 0, 0},
325 {'c', nv_operator, 0, 0},
326 {'d', nv_operator, 0, 0},
327 {'e', nv_wordcmd, 0, FALSE},
328 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
329 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
330 {'h', nv_left, NV_RL, 0},
331 {'i', nv_edit, NV_NCH, 0},
332 {'j', nv_down, 0, FALSE},
333 {'k', nv_up, 0, FALSE},
334 {'l', nv_right, NV_RL, 0},
335 {'m', nv_mark, NV_NCH_NOP, 0},
336 {'n', nv_next, 0, 0},
337 {'o', nv_open, 0, 0},
338 {'p', nv_put, 0, 0},
339 {'q', nv_record, NV_NCH, 0},
340 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
341 {'s', nv_subst, NV_KEEPREG, 0},
342 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
343 {'u', nv_undo, 0, 0},
344 {'w', nv_wordcmd, 0, FALSE},
345 {'x', nv_abbrev, NV_KEEPREG, 0},
346 {'y', nv_operator, 0, 0},
347 {'z', nv_zet, NV_NCH_ALW, 0},
348 {'{', nv_findpar, 0, BACKWARD},
349 {'|', nv_pipe, 0, 0},
350 {'}', nv_findpar, 0, FORWARD},
351 {'~', nv_tilde, 0, 0},
352
353 /* pound sign */
354 {POUND, nv_ident, 0, 0},
355#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200356 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
357 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
358 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
359 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_LEFTMOUSE, nv_mouse, 0, 0},
361 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
362 {K_LEFTDRAG, nv_mouse, 0, 0},
363 {K_LEFTRELEASE, nv_mouse, 0, 0},
364 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
365 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
366 {K_MIDDLEDRAG, nv_mouse, 0, 0},
367 {K_MIDDLERELEASE, nv_mouse, 0, 0},
368 {K_RIGHTMOUSE, nv_mouse, 0, 0},
369 {K_RIGHTDRAG, nv_mouse, 0, 0},
370 {K_RIGHTRELEASE, nv_mouse, 0, 0},
371 {K_X1MOUSE, nv_mouse, 0, 0},
372 {K_X1DRAG, nv_mouse, 0, 0},
373 {K_X1RELEASE, nv_mouse, 0, 0},
374 {K_X2MOUSE, nv_mouse, 0, 0},
375 {K_X2DRAG, nv_mouse, 0, 0},
376 {K_X2RELEASE, nv_mouse, 0, 0},
377#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000379 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {K_INS, nv_edit, 0, 0},
381 {K_KINS, nv_edit, 0, 0},
382 {K_BS, nv_ctrlh, 0, 0},
383 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
384 {K_S_UP, nv_page, NV_SS, BACKWARD},
385 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
386 {K_S_DOWN, nv_page, NV_SS, FORWARD},
387 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
389 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
390 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
391 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
392 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
393 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
394 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
395 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
396 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
397 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
398 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {K_S_END, nv_end, NV_SS, FALSE},
400 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
401 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
402 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {K_S_HOME, nv_home, NV_SS, 0},
404 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
405 {K_DEL, nv_abbrev, 0, 0},
406 {K_KDEL, nv_abbrev, 0, 0},
407 {K_UNDO, nv_kundo, 0, 0},
408 {K_HELP, nv_help, NV_NCW, 0},
409 {K_F1, nv_help, NV_NCW, 0},
410 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#ifdef FEAT_GUI
413 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
414 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
415#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#ifdef FEAT_GUI_TABLINE
417 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000418 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#ifdef FEAT_FKMAP
421 {K_F8, farsi_fkey, 0, 0},
422 {K_F9, farsi_fkey, 0, 0},
423#endif
424#ifdef FEAT_SNIFF
425 {K_SNIFF, nv_sniff, 0, 0},
426#endif
427#ifdef FEAT_NETBEANS_INTG
428 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
429#endif
430#ifdef FEAT_DND
431 {K_DROP, nv_drop, NV_STS, 0},
432#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000433#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000434 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436};
437
438/* Number of commands in nv_cmds[]. */
439#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
440
441/* Sorted index of commands in nv_cmds[]. */
442static short nv_cmd_idx[NV_CMDS_SIZE];
443
444/* The highest index for which
445 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
446static int nv_max_linear;
447
448/*
449 * Compare functions for qsort() below, that checks the command character
450 * through the index in nv_cmd_idx[].
451 */
452 static int
453#ifdef __BORLANDC__
454_RTLENTRYF
455#endif
456nv_compare(s1, s2)
457 const void *s1;
458 const void *s2;
459{
460 int c1, c2;
461
462 /* The commands are sorted on absolute value. */
463 c1 = nv_cmds[*(const short *)s1].cmd_char;
464 c2 = nv_cmds[*(const short *)s2].cmd_char;
465 if (c1 < 0)
466 c1 = -c1;
467 if (c2 < 0)
468 c2 = -c2;
469 return c1 - c2;
470}
471
472/*
473 * Initialize the nv_cmd_idx[] table.
474 */
475 void
476init_normal_cmds()
477{
478 int i;
479
480 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000481 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 nv_cmd_idx[i] = i;
483
484 /* Sort the commands by the command character. */
485 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
486
487 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000488 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
490 break;
491 nv_max_linear = i - 1;
492}
493
494/*
495 * Search for a command in the commands table.
496 * Returns -1 for invalid command.
497 */
498 static int
499find_command(cmdchar)
500 int cmdchar;
501{
502 int i;
503 int idx;
504 int top, bot;
505 int c;
506
507#ifdef FEAT_MBYTE
508 /* A multi-byte character is never a command. */
509 if (cmdchar >= 0x100)
510 return -1;
511#endif
512
513 /* We use the absolute value of the character. Special keys have a
514 * negative value, but are sorted on their absolute value. */
515 if (cmdchar < 0)
516 cmdchar = -cmdchar;
517
518 /* If the character is in the first part: The character is the index into
519 * nv_cmd_idx[]. */
520 if (cmdchar <= nv_max_linear)
521 return nv_cmd_idx[cmdchar];
522
523 /* Perform a binary search. */
524 bot = nv_max_linear + 1;
525 top = NV_CMDS_SIZE - 1;
526 idx = -1;
527 while (bot <= top)
528 {
529 i = (top + bot) / 2;
530 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
531 if (c < 0)
532 c = -c;
533 if (cmdchar == c)
534 {
535 idx = nv_cmd_idx[i];
536 break;
537 }
538 if (cmdchar > c)
539 bot = i + 1;
540 else
541 top = i - 1;
542 }
543 return idx;
544}
545
546/*
547 * Execute a command in Normal mode.
548 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 void
550normal_cmd(oap, toplevel)
551 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000552 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 cmdarg_T ca; /* command arguments */
555 int c;
556 int ctrl_w = FALSE; /* got CTRL-W command */
557 int old_col = curwin->w_curswant;
558#ifdef FEAT_CMDL_INFO
559 int need_flushbuf; /* need to call out_flush() */
560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 pos_T old_pos; /* cursor position before command */
562 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 static int old_mapped_len = 0;
564 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000565#ifdef FEAT_EVAL
566 int set_prevcount = FALSE;
567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
569 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
570 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000571
572 /* Use a count remembered from before entering an operator. After typing
573 * "3d" we return from normal_cmd() and come back here, the "3" is
574 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 ca.opcount = opcount;
576
577#ifdef FEAT_SNIFF
578 want_sniff_request = sniff_connected;
579#endif
580
581 /*
582 * If there is an operator pending, then the command we take this time
583 * will terminate it. Finish_op tells us to finish the operation before
584 * returning this time (unless the operation was cancelled).
585 */
586#ifdef CURSOR_SHAPE
587 c = finish_op;
588#endif
589 finish_op = (oap->op_type != OP_NOP);
590#ifdef CURSOR_SHAPE
591 if (finish_op != c)
592 {
593 ui_cursor_shape(); /* may show different cursor shape */
594# ifdef FEAT_MOUSESHAPE
595 update_mouseshape(-1);
596# endif
597 }
598#endif
599
Bram Moolenaara983fe92008-07-31 20:04:27 +0000600 /* When not finishing an operator and no register name typed, reset the
601 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000605#ifdef FEAT_EVAL
606 set_prevcount = TRUE;
607#endif
608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaara983fe92008-07-31 20:04:27 +0000610#ifdef FEAT_AUTOCMD
611 /* Restore counts from before receiving K_CURSORHOLD. This means after
612 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
613 * "3 * 2". */
614 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
615 {
616 ca.opcount = oap->prev_opcount;
617 ca.count0 = oap->prev_count0;
618 oap->prev_opcount = 0;
619 oap->prev_count0 = 0;
620 }
621#endif
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 State = NORMAL_BUSY;
626#ifdef USE_ON_FLY_SCROLL
627 dont_scroll = FALSE; /* allow scrolling here */
628#endif
629
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100630#ifdef FEAT_EVAL
631 /* Set v:count here, when called from main() and not a stuffed
632 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100633 * when there is no count. Do set it for redo. */
634 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100635 set_vcount_ca(&ca, &set_prevcount);
636#endif
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 /*
639 * Get the command character from the user.
640 */
641 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644 /*
645 * If a mapping was started in Visual or Select mode, remember the length
646 * of the mapping. This is used below to not return to Insert mode for as
647 * long as the mapping is being executed.
648 */
649 if (restart_edit == 0)
650 old_mapped_len = 0;
651 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000652 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 old_mapped_len = typebuf_maplen();
654
655 if (c == NUL)
656 c = K_ZERO;
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 /*
659 * In Select mode, typed text replaces the selection.
660 */
661 if (VIsual_active
662 && VIsual_select
663 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
664 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000665 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
666 * 'insertmode' is set) fake a "d"elete command, Insert mode will
667 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000668 * Insert the typed character in the typeahead buffer, so that it can
669 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000670 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000671 if (restart_edit != 0)
672 c = 'd';
673 else
674 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000675 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200676 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679#ifdef FEAT_CMDL_INFO
680 need_flushbuf = add_to_showcmd(c);
681#endif
682
683getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 /*
687 * Handle a count before a command and compute ca.count0.
688 * Note that '0' is a command and not the start of a count, but it's
689 * part of a count after other digits.
690 */
691 while ( (c >= '1' && c <= '9')
692 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
693 {
694 if (c == K_DEL || c == K_KDEL)
695 {
696 ca.count0 /= 10;
697#ifdef FEAT_CMDL_INFO
698 del_from_showcmd(4); /* delete the digit and ~@% */
699#endif
700 }
701 else
702 ca.count0 = ca.count0 * 10 + (c - '0');
703 if (ca.count0 < 0) /* got too large! */
704 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000705#ifdef FEAT_EVAL
706 /* Set v:count here, when called from main() and not a stuffed
707 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100708 * right after the count. Do set it for redo. */
709 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100710 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (ctrl_w)
713 {
714 ++no_mapping;
715 ++allow_keys; /* no mapping for nchar, but keys */
716 }
717 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000718 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 --no_zero_mapping;
721 if (ctrl_w)
722 {
723 --no_mapping;
724 --allow_keys;
725 }
726#ifdef FEAT_CMDL_INFO
727 need_flushbuf |= add_to_showcmd(c);
728#endif
729 }
730
731 /*
732 * If we got CTRL-W there may be a/another count
733 */
734 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
735 {
736 ctrl_w = TRUE;
737 ca.opcount = ca.count0; /* remember first count */
738 ca.count0 = 0;
739 ++no_mapping;
740 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000741 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 --no_mapping;
744 --allow_keys;
745#ifdef FEAT_CMDL_INFO
746 need_flushbuf |= add_to_showcmd(c);
747#endif
748 goto getcount; /* jump back */
749 }
750 }
751
Bram Moolenaara983fe92008-07-31 20:04:27 +0000752#ifdef FEAT_AUTOCMD
753 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000755 /* Save the count values so that ca.opcount and ca.count0 are exactly
756 * the same when coming back here after handling K_CURSORHOLD. */
757 oap->prev_opcount = ca.opcount;
758 oap->prev_count0 = ca.count0;
759 }
760 else
761#endif
762 if (ca.opcount != 0)
763 {
764 /*
765 * If we're in the middle of an operator (including after entering a
766 * yank buffer with '"') AND we had a count before the operator, then
767 * that count overrides the current value of ca.count0.
768 * What this means effectively, is that commands like "3dw" get turned
769 * into "d3w" which makes things fall into place pretty neatly.
770 * If you give a count before AND after the operator, they are
771 * multiplied.
772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (ca.count0)
774 ca.count0 *= ca.opcount;
775 else
776 ca.count0 = ca.opcount;
777 }
778
779 /*
780 * Always remember the count. It will be set to zero (on the next call,
781 * above) when there is no pending operator.
782 * When called from main(), save the count for use by the "count" built-in
783 * variable.
784 */
785 ca.opcount = ca.count0;
786 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
787
788#ifdef FEAT_EVAL
789 /*
790 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100791 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100793 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000794 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#endif
796
797 /*
798 * Find the command character in the table of commands.
799 * For CTRL-W we already got nchar when looking for a count.
800 */
801 if (ctrl_w)
802 {
803 ca.nchar = c;
804 ca.cmdchar = Ctrl_W;
805 }
806 else
807 ca.cmdchar = c;
808 idx = find_command(ca.cmdchar);
809 if (idx < 0)
810 {
811 /* Not a known command: beep. */
812 clearopbeep(oap);
813 goto normal_end;
814 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000815
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000816 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200818 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000820 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 goto normal_end;
822 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000823#ifdef FEAT_AUTOCMD
824 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
825 goto normal_end;
826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 /*
829 * In Visual/Select mode, a few keys are handled in a special way.
830 */
831 if (VIsual_active)
832 {
833 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
834 if (km_stopsel
835 && (nv_cmds[idx].cmd_flags & NV_STS)
836 && !(mod_mask & MOD_MASK_SHIFT))
837 {
838 end_visual_mode();
839 redraw_curbuf_later(INVERTED);
840 }
841
842 /* Keys that work different when 'keymodel' contains "startsel" */
843 if (km_startsel)
844 {
845 if (nv_cmds[idx].cmd_flags & NV_SS)
846 {
847 unshift_special(&ca);
848 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000849 if (idx < 0)
850 {
851 /* Just in case */
852 clearopbeep(oap);
853 goto normal_end;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
857 && (mod_mask & MOD_MASK_SHIFT))
858 {
859 mod_mask &= ~MOD_MASK_SHIFT;
860 }
861 }
862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864#ifdef FEAT_RIGHTLEFT
865 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
866 && (nv_cmds[idx].cmd_flags & NV_RL))
867 {
868 /* Invert horizontal movements and operations. Only when typed by the
869 * user directly, not when the result of a mapping or "x" translated
870 * to "dl". */
871 switch (ca.cmdchar)
872 {
873 case 'l': ca.cmdchar = 'h'; break;
874 case K_RIGHT: ca.cmdchar = K_LEFT; break;
875 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
876 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
877 case 'h': ca.cmdchar = 'l'; break;
878 case K_LEFT: ca.cmdchar = K_RIGHT; break;
879 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
880 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
881 case '>': ca.cmdchar = '<'; break;
882 case '<': ca.cmdchar = '>'; break;
883 }
884 idx = find_command(ca.cmdchar);
885 }
886#endif
887
888 /*
889 * Get an additional character if we need one.
890 */
891 if ((nv_cmds[idx].cmd_flags & NV_NCH)
892 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
893 && oap->op_type == OP_NOP)
894 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
895 || (ca.cmdchar == 'q'
896 && oap->op_type == OP_NOP
897 && !Recording
898 && !Exec_reg)
899 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100900 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 {
902 int *cp;
903 int repl = FALSE; /* get character for replace mode */
904 int lit = FALSE; /* get extra character literally */
905 int langmap_active = FALSE; /* using :lmap mappings */
906 int lang; /* getting a text character */
907#ifdef USE_IM_CONTROL
908 int save_smd; /* saved value of p_smd */
909#endif
910
911 ++no_mapping;
912 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000913#ifdef FEAT_AUTOCMD
914 /* Don't generate a CursorHold event here, most commands can't handle
915 * it, e.g., nv_replace(), nv_csearch(). */
916 did_cursorhold = TRUE;
917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (ca.cmdchar == 'g')
919 {
920 /*
921 * For 'g' get the next character now, so that we can check for
922 * "gr", "g'" and "g`".
923 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000924 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#ifdef FEAT_CMDL_INFO
927 need_flushbuf |= add_to_showcmd(ca.nchar);
928#endif
929 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100930 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 {
932 cp = &ca.extra_char; /* need to get a third character */
933 if (ca.nchar != 'r')
934 lit = TRUE; /* get it literally */
935 else
936 repl = TRUE; /* get it in replace mode */
937 }
938 else
939 cp = NULL; /* no third character needed */
940 }
941 else
942 {
943 if (ca.cmdchar == 'r') /* get it in replace mode */
944 repl = TRUE;
945 cp = &ca.nchar;
946 }
947 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
948
949 /*
950 * Get a second or third character.
951 */
952 if (cp != NULL)
953 {
954#ifdef CURSOR_SHAPE
955 if (repl)
956 {
957 State = REPLACE; /* pretend Replace mode */
958 ui_cursor_shape(); /* show different cursor shape */
959 }
960#endif
961 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
962 {
963 /* Allow mappings defined with ":lmap". */
964 --no_mapping;
965 --allow_keys;
966 if (repl)
967 State = LREPLACE;
968 else
969 State = LANGMAP;
970 langmap_active = TRUE;
971 }
972#ifdef USE_IM_CONTROL
973 save_smd = p_smd;
974 p_smd = FALSE; /* Don't let the IM code show the mode here */
975 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
976 im_set_active(TRUE);
977#endif
978
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000979 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 if (langmap_active)
982 {
983 /* Undo the decrement done above */
984 ++no_mapping;
985 ++allow_keys;
986 State = NORMAL_BUSY;
987 }
988#ifdef USE_IM_CONTROL
989 if (lang)
990 {
991 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
992 im_save_status(&curbuf->b_p_iminsert);
993 im_set_active(FALSE);
994 }
995 p_smd = save_smd;
996#endif
997#ifdef CURSOR_SHAPE
998 State = NORMAL_BUSY;
999#endif
1000#ifdef FEAT_CMDL_INFO
1001 need_flushbuf |= add_to_showcmd(*cp);
1002#endif
1003
1004 if (!lit)
1005 {
1006#ifdef FEAT_DIGRAPHS
1007 /* Typing CTRL-K gets a digraph. */
1008 if (*cp == Ctrl_K
1009 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1010 || cp == &ca.extra_char)
1011 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1012 {
1013 c = get_digraph(FALSE);
1014 if (c > 0)
1015 {
1016 *cp = c;
1017# ifdef FEAT_CMDL_INFO
1018 /* Guessing how to update showcmd here... */
1019 del_from_showcmd(3);
1020 need_flushbuf |= add_to_showcmd(*cp);
1021# endif
1022 }
1023 }
1024#endif
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* adjust chars > 127, except after "tTfFr" commands */
1027 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#ifdef FEAT_RIGHTLEFT
1029 /* adjust Hebrew mapped char */
1030 if (p_hkmap && lang && KeyTyped)
1031 *cp = hkmap(*cp);
1032# ifdef FEAT_FKMAP
1033 /* adjust Farsi mapped char */
1034 if (p_fkmap && lang && KeyTyped)
1035 *cp = fkmap(*cp);
1036# endif
1037#endif
1038 }
1039
1040 /*
1041 * When the next character is CTRL-\ a following CTRL-N means the
1042 * command is aborted and we go to Normal mode.
1043 */
1044 if (cp == &ca.extra_char
1045 && ca.nchar == Ctrl_BSL
1046 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1047 {
1048 ca.cmdchar = Ctrl_BSL;
1049 ca.nchar = ca.extra_char;
1050 idx = find_command(ca.cmdchar);
1051 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001052 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001053 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 else if (*cp == Ctrl_BSL)
1055 {
1056 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1057
1058 /* There is a busy wait here when typing "f<C-\>" and then
1059 * something different from CTRL-N. Can't be avoided. */
1060 while ((c = vpeekc()) <= 0 && towait > 0L)
1061 {
1062 do_sleep(towait > 50L ? 50L : towait);
1063 towait -= 50L;
1064 }
1065 if (c > 0)
1066 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001067 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (c != Ctrl_N && c != Ctrl_G)
1069 vungetc(c);
1070 else
1071 {
1072 ca.cmdchar = Ctrl_BSL;
1073 ca.nchar = c;
1074 idx = find_command(ca.cmdchar);
1075 }
1076 }
1077 }
1078
1079#ifdef FEAT_MBYTE
1080 /* When getting a text character and the next character is a
1081 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001082 * However, don't wait for it to arrive. Also, do enable mapping,
1083 * because if it's put back with vungetc() it's too late to apply
1084 * mapping. */
1085 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 while (enc_utf8 && lang && (c = vpeekc()) > 0
1087 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1088 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001089 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (!utf_iscomposing(c))
1091 {
1092 vungetc(c); /* it wasn't, put it back */
1093 break;
1094 }
1095 else if (ca.ncharC1 == 0)
1096 ca.ncharC1 = c;
1097 else
1098 ca.ncharC2 = c;
1099 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001100 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102 }
1103 --no_mapping;
1104 --allow_keys;
1105 }
1106
1107#ifdef FEAT_CMDL_INFO
1108 /*
1109 * Flush the showcmd characters onto the screen so we can see them while
1110 * the command is being executed. Only do this when the shown command was
1111 * actually displayed, otherwise this will slow down a lot when executing
1112 * mappings.
1113 */
1114 if (need_flushbuf)
1115 out_flush();
1116#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001117#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001118 if (ca.cmdchar != K_IGNORE)
1119 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
1122 State = NORMAL;
1123
1124 if (ca.nchar == ESC)
1125 {
1126 clearop(oap);
1127 if (restart_edit == 0 && goto_im())
1128 restart_edit = 'a';
1129 goto normal_end;
1130 }
1131
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001132 if (ca.cmdchar != K_IGNORE)
1133 {
1134 msg_didout = FALSE; /* don't scroll screen up for normal command */
1135 msg_col = 0;
1136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 old_pos = curwin->w_cursor; /* remember where cursor was */
1139
1140 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1141 * mode. */
1142 if (!VIsual_active && km_startsel)
1143 {
1144 if (nv_cmds[idx].cmd_flags & NV_SS)
1145 {
1146 start_selection();
1147 unshift_special(&ca);
1148 idx = find_command(ca.cmdchar);
1149 }
1150 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1151 && (mod_mask & MOD_MASK_SHIFT))
1152 {
1153 start_selection();
1154 mod_mask &= ~MOD_MASK_SHIFT;
1155 }
1156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 /*
1159 * Execute the command!
1160 * Call the command function found in the commands table.
1161 */
1162 ca.arg = nv_cmds[idx].cmd_arg;
1163 (nv_cmds[idx].cmd_func)(&ca);
1164
1165 /*
1166 * If we didn't start or finish an operator, reset oap->regname, unless we
1167 * need it later.
1168 */
1169 if (!finish_op
1170 && !oap->op_type
1171 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1172 {
1173 clearop(oap);
1174#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001175 {
1176 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001177
Bram Moolenaar536681b2011-05-10 16:12:45 +02001178 /* Adjust the register according to 'clipboard', so that when
1179 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001180# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001181 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001182# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001183 set_reg_var(regname);
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#endif
1186 }
1187
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001188 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001189 * character or "z333<cr>". */
1190 if (old_mapped_len > 0)
1191 old_mapped_len = typebuf_maplen();
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 /*
1194 * If an operation is pending, handle it...
1195 */
1196 do_pending_operator(&ca, old_col, FALSE);
1197
1198 /*
1199 * Wait for a moment when a message is displayed that will be overwritten
1200 * by the mode message.
1201 * In Visual mode and with "^O" in Insert mode, a short message will be
1202 * overwritten by the mode message. Wait a bit, until a key is hit.
1203 * In Visual mode, it's more important to keep the Visual area updated
1204 * than keeping a message (e.g. from a /pat search).
1205 * Only do this if the command was typed, not from a mapping.
1206 * Don't wait when emsg_silent is non-zero.
1207 * Also wait a bit after an error message, e.g. for "^O:".
1208 * Don't redraw the screen, it would remove the message.
1209 */
1210 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001211 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 || (VIsual_active
1214 && old_pos.lnum == curwin->w_cursor.lnum
1215 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 )
1217 && (clear_cmdline
1218 || redraw_cmdline)
1219 && (msg_didout || (msg_didany && msg_scroll))
1220 && !msg_nowait
1221 && KeyTyped)
1222 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 && (msg_scroll
1225 || emsg_on_display)))
1226 && oap->regname == 0
1227 && !(ca.retval & CA_COMMAND_BUSY)
1228 && stuff_empty()
1229 && typebuf_typed()
1230 && emsg_silent == 0
1231 && !did_wait_return
1232 && oap->op_type == OP_NOP)
1233 {
1234 int save_State = State;
1235
1236 /* Draw the cursor with the right shape here */
1237 if (restart_edit != 0)
1238 State = INSERT;
1239
1240 /* If need to redraw, and there is a "keep_msg", redraw before the
1241 * delay */
1242 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1243 {
1244 char_u *kmsg;
1245
1246 kmsg = keep_msg;
1247 keep_msg = NULL;
1248 /* showmode() will clear keep_msg, but we want to use it anyway */
1249 update_screen(0);
1250 /* now reset it, otherwise it's put in the history again */
1251 keep_msg = kmsg;
1252 msg_attr(kmsg, keep_msg_attr);
1253 vim_free(kmsg);
1254 }
1255 setcursor();
1256 cursor_on();
1257 out_flush();
1258 if (msg_scroll || emsg_on_display)
1259 ui_delay(1000L, TRUE); /* wait at least one second */
1260 ui_delay(3000L, FALSE); /* wait up to three seconds */
1261 State = save_State;
1262
1263 msg_scroll = FALSE;
1264 emsg_on_display = FALSE;
1265 }
1266
1267 /*
1268 * Finish up after executing a Normal mode command.
1269 */
1270normal_end:
1271
1272 msg_nowait = FALSE;
1273
1274 /* Reset finish_op, in case it was set */
1275#ifdef CURSOR_SHAPE
1276 c = finish_op;
1277#endif
1278 finish_op = FALSE;
1279#ifdef CURSOR_SHAPE
1280 /* Redraw the cursor with another shape, if we were in Operator-pending
1281 * mode or did a replace command. */
1282 if (c || ca.cmdchar == 'r')
1283 {
1284 ui_cursor_shape(); /* may show different cursor shape */
1285# ifdef FEAT_MOUSESHAPE
1286 update_mouseshape(-1);
1287# endif
1288 }
1289#endif
1290
1291#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001292 if (oap->op_type == OP_NOP && oap->regname == 0
1293# ifdef FEAT_AUTOCMD
1294 && ca.cmdchar != K_CURSORHOLD
1295# endif
1296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 clear_showcmd();
1298#endif
1299
1300 checkpcmark(); /* check if we moved since setting pcmark */
1301 vim_free(ca.searchbuf);
1302
1303#ifdef FEAT_MBYTE
1304 if (has_mbyte)
1305 mb_adjust_cursor();
1306#endif
1307
1308#ifdef FEAT_SCROLLBIND
1309 if (curwin->w_p_scb && toplevel)
1310 {
1311 validate_cursor(); /* may need to update w_leftcol */
1312 do_check_scrollbind(TRUE);
1313 }
1314#endif
1315
Bram Moolenaar860cae12010-06-05 23:22:07 +02001316#ifdef FEAT_CURSORBIND
1317 if (curwin->w_p_crb && toplevel)
1318 {
1319 validate_cursor(); /* may need to update w_leftcol */
1320 do_check_cursorbind();
1321 }
1322#endif
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 /*
1325 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1326 * if still inside a mapping that started in Visual mode).
1327 * May switch from Visual to Select mode after CTRL-O command.
1328 */
1329 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1331 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && !(ca.retval & CA_COMMAND_BUSY)
1333 && stuff_empty()
1334 && oap->regname == 0)
1335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 if (restart_VIsual_select == 1)
1337 {
1338 VIsual_select = TRUE;
1339 showmode();
1340 restart_VIsual_select = 0;
1341 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001342 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 (void)edit(restart_edit, FALSE, 1L);
1344 }
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (restart_VIsual_select == 2)
1347 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 /* Save count before an operator for next time. */
1350 opcount = ca.opcount;
1351}
1352
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001353#ifdef FEAT_EVAL
1354/*
1355 * Set v:count and v:count1 according to "cap".
1356 * Set v:prevcount only when "set_prevcount" is TRUE.
1357 */
1358 static void
1359set_vcount_ca(cap, set_prevcount)
1360 cmdarg_T *cap;
1361 int *set_prevcount;
1362{
1363 long count = cap->count0;
1364
1365 /* multiply with cap->opcount the same way as above */
1366 if (cap->opcount != 0)
1367 count = cap->opcount * (count == 0 ? 1 : count);
1368 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1369 *set_prevcount = FALSE; /* only set v:prevcount once */
1370}
1371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373/*
1374 * Handle an operator after visual mode or when the movement is finished
1375 */
1376 void
1377do_pending_operator(cap, old_col, gui_yank)
1378 cmdarg_T *cap;
1379 int old_col;
1380 int gui_yank;
1381{
1382 oparg_T *oap = cap->oap;
1383 pos_T old_cursor;
1384 int empty_region_error;
1385 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001386#ifdef FEAT_LINEBREAK
1387 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /* The visual area is remembered for redo */
1391 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1392 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001393 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001395#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398
1399#if defined(FEAT_CLIPBOARD)
1400 /*
1401 * Yank the visual area into the GUI selection register before we operate
1402 * on it and lose it forever.
1403 * Don't do it if a specific register was specified, so that ""x"*P works.
1404 * This could call do_pending_operator() recursively, but that's OK
1405 * because gui_yank will be TRUE for the nested call.
1406 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001407 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && oap->op_type != OP_NOP
1409 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && VIsual_active
1411 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 && oap->regname == 0)
1413 clip_auto_select();
1414#endif
1415 old_cursor = curwin->w_cursor;
1416
1417 /*
1418 * If an operation is pending, handle it...
1419 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001420 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001422#ifdef FEAT_LINEBREAK
1423 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001424 if (curwin->w_p_lbr)
1425 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001426 curwin->w_p_lbr = FALSE;
1427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 oap->is_VIsual = VIsual_active;
1429 if (oap->motion_force == 'V')
1430 oap->motion_type = MLINE;
1431 else if (oap->motion_force == 'v')
1432 {
1433 /* If the motion was linewise, "inclusive" will not have been set.
1434 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1435 if (oap->motion_type == MLINE)
1436 oap->inclusive = FALSE;
1437 /* If the motion already was characterwise, toggle "inclusive" */
1438 else if (oap->motion_type == MCHAR)
1439 oap->inclusive = !oap->inclusive;
1440 oap->motion_type = MCHAR;
1441 }
1442 else if (oap->motion_force == Ctrl_V)
1443 {
1444 /* Change line- or characterwise motion into Visual block mode. */
1445 VIsual_active = TRUE;
1446 VIsual = oap->start;
1447 VIsual_mode = Ctrl_V;
1448 VIsual_select = FALSE;
1449 VIsual_reselect = FALSE;
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaar7afea822013-04-24 18:34:45 +02001452 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1453 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001455 && ((!VIsual_active || oap->motion_force)
1456 /* Also redo Operator-pending Visual mode mappings */
1457 || (VIsual_active && cap->cmdchar == ':'
1458 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001459 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460#ifdef FEAT_FOLDING
1461 && oap->op_type != OP_FOLD
1462 && oap->op_type != OP_FOLDOPEN
1463 && oap->op_type != OP_FOLDOPENREC
1464 && oap->op_type != OP_FOLDCLOSE
1465 && oap->op_type != OP_FOLDCLOSEREC
1466 && oap->op_type != OP_FOLDDEL
1467 && oap->op_type != OP_FOLDDELREC
1468#endif
1469 )
1470 {
1471 prep_redo(oap->regname, cap->count0,
1472 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1473 oap->motion_force, cap->cmdchar, cap->nchar);
1474 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1475 {
1476 /*
1477 * If 'cpoptions' does not contain 'r', insert the search
1478 * pattern to really repeat the same command.
1479 */
1480 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001481 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 AppendToRedobuff(NL_STR);
1483 }
1484 else if (cap->cmdchar == ':')
1485 {
1486 /* do_cmdline() has stored the first typed line in
1487 * "repeat_cmdline". When several lines are typed repeating
1488 * won't be possible. */
1489 if (repeat_cmdline == NULL)
1490 ResetRedobuff();
1491 else
1492 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001493 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 AppendToRedobuff(NL_STR);
1495 vim_free(repeat_cmdline);
1496 repeat_cmdline = NULL;
1497 }
1498 }
1499 }
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 if (redo_VIsual_busy)
1502 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001503 /* Redo of an operation on a Visual area. Use the same size from
1504 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 oap->start = curwin->w_cursor;
1506 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1507 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1508 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1509 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001510 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001512 if (VIsual_mode == 'v')
1513 {
1514 if (redo_VIsual_line_count <= 1)
1515 {
1516 validate_virtcol();
1517 curwin->w_curswant =
1518 curwin->w_virtcol + redo_VIsual_vcol - 1;
1519 }
1520 else
1521 curwin->w_curswant = redo_VIsual_vcol;
1522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001524 {
1525 curwin->w_curswant = MAXCOL;
1526 }
1527 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 cap->count0 = redo_VIsual_count;
1530 if (redo_VIsual_count != 0)
1531 cap->count1 = redo_VIsual_count;
1532 else
1533 cap->count1 = 1;
1534 }
1535 else if (VIsual_active)
1536 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001537 if (!gui_yank)
1538 {
1539 /* Save the current VIsual area for '< and '> marks, and "gv" */
1540 curbuf->b_visual.vi_start = VIsual;
1541 curbuf->b_visual.vi_end = curwin->w_cursor;
1542 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001543 if (VIsual_mode_orig != NUL)
1544 {
1545 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1546 VIsual_mode_orig = NUL;
1547 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001548 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001550 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001555 * characterwise selection.
1556 * Special case: gH<Del> deletes the last line. */
1557 if (VIsual_select && VIsual_mode == 'V'
1558 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 if (lt(VIsual, curwin->w_cursor))
1561 {
1562 VIsual.col = 0;
1563 curwin->w_cursor.col =
1564 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1565 }
1566 else
1567 {
1568 curwin->w_cursor.col = 0;
1569 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1570 }
1571 VIsual_mode = 'v';
1572 }
1573 /* If 'selection' is "exclusive", backup one character for
1574 * charwise selections. */
1575 else if (VIsual_mode == 'v')
1576 {
1577# ifdef FEAT_VIRTUALEDIT
1578 include_line_break =
1579# endif
1580 unadjust_for_sel();
1581 }
1582
1583 oap->start = VIsual;
1584 if (VIsual_mode == 'V')
1585 oap->start.col = 0;
1586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 /*
1589 * Set oap->start to the first position of the operated text, oap->end
1590 * to the end of the operated text. w_cursor is equal to oap->start.
1591 */
1592 if (lt(oap->start, curwin->w_cursor))
1593 {
1594#ifdef FEAT_FOLDING
1595 /* Include folded lines completely. */
1596 if (!VIsual_active)
1597 {
1598 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1599 oap->start.col = 0;
1600 if (hasFolding(curwin->w_cursor.lnum, NULL,
1601 &curwin->w_cursor.lnum))
1602 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1603 }
1604#endif
1605 oap->end = curwin->w_cursor;
1606 curwin->w_cursor = oap->start;
1607
1608 /* w_virtcol may have been updated; if the cursor goes back to its
1609 * previous position w_virtcol becomes invalid and isn't updated
1610 * automatically. */
1611 curwin->w_valid &= ~VALID_VIRTCOL;
1612 }
1613 else
1614 {
1615#ifdef FEAT_FOLDING
1616 /* Include folded lines completely. */
1617 if (!VIsual_active && oap->motion_type == MLINE)
1618 {
1619 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1620 NULL))
1621 curwin->w_cursor.col = 0;
1622 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1623 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1624 }
1625#endif
1626 oap->end = oap->start;
1627 oap->start = curwin->w_cursor;
1628 }
1629
1630 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1631
1632#ifdef FEAT_VIRTUALEDIT
1633 /* Set "virtual_op" before resetting VIsual_active. */
1634 virtual_op = virtual_active();
1635#endif
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (VIsual_active || redo_VIsual_busy)
1638 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001639 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 if (!redo_VIsual_busy && !gui_yank)
1642 {
1643 /*
1644 * Prepare to reselect and redo Visual: this is based on the
1645 * size of the Visual text
1646 */
1647 resel_VIsual_mode = VIsual_mode;
1648 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001649 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001651 {
1652 if (VIsual_mode != Ctrl_V)
1653 getvvcol(curwin, &(oap->end),
1654 NULL, NULL, &oap->end_vcol);
1655 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1656 {
1657 if (VIsual_mode != Ctrl_V)
1658 getvvcol(curwin, &(oap->start),
1659 &oap->start_vcol, NULL, NULL);
1660 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1661 }
1662 else
1663 resel_VIsual_vcol = oap->end_vcol;
1664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 resel_VIsual_line_count = oap->line_count;
1666 }
1667
1668 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1669 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1670 && oap->op_type != OP_COLON
1671#ifdef FEAT_FOLDING
1672 && oap->op_type != OP_FOLD
1673 && oap->op_type != OP_FOLDOPEN
1674 && oap->op_type != OP_FOLDOPENREC
1675 && oap->op_type != OP_FOLDCLOSE
1676 && oap->op_type != OP_FOLDCLOSEREC
1677 && oap->op_type != OP_FOLDDEL
1678 && oap->op_type != OP_FOLDDELREC
1679#endif
1680 && oap->motion_force == NUL
1681 )
1682 {
1683 /* Prepare for redoing. Only use the nchar field for "r",
1684 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001685 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1686 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001687 prep_redo(oap->regname, cap->count0,
1688 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1689 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001690 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001691 prep_redo(oap->regname, 0L, NUL, 'v',
1692 get_op_char(oap->op_type),
1693 get_extra_op_char(oap->op_type),
1694 oap->op_type == OP_REPLACE
1695 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (!redo_VIsual_busy)
1697 {
1698 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001699 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 redo_VIsual_line_count = resel_VIsual_line_count;
1701 redo_VIsual_count = cap->count0;
1702 }
1703 }
1704
1705 /*
1706 * oap->inclusive defaults to TRUE.
1707 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1708 * FALSE. This makes "d}P" and "v}dP" work the same.
1709 */
1710 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1711 oap->inclusive = TRUE;
1712 if (VIsual_mode == 'V')
1713 oap->motion_type = MLINE;
1714 else
1715 {
1716 oap->motion_type = MCHAR;
1717 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001718#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 )
1722 {
1723 oap->inclusive = FALSE;
1724 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001725 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001726 if (*p_sel != 'o'
1727 && !op_on_lines(oap->op_type)
1728 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001730 ++oap->end.lnum;
1731 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001732#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001733 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001734#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001735 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
1737 }
1738 }
1739
1740 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Switch Visual off now, so screen updating does
1744 * not show inverted text when the screen is redrawn.
1745 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1746 * no screen redraw, so it is done here to remove the inverted
1747 * part.
1748 */
1749 if (!gui_yank)
1750 {
1751 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001752#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 setmouse();
1754 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001755#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001756 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 if ((oap->op_type == OP_YANK
1758 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001759 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 || oap->op_type == OP_FILTER)
1761 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001762 {
1763#ifdef FEAT_LINEBREAK
1764 /* make sure redrawing is correct */
1765 curwin->w_p_lbr = lbr_saved;
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772#ifdef FEAT_MBYTE
1773 /* Include the trailing byte of a multi-byte char. */
1774 if (has_mbyte && oap->inclusive)
1775 {
1776 int l;
1777
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001778 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (l > 1)
1780 oap->end.col += l - 1;
1781 }
1782#endif
1783 curwin->w_set_curswant = TRUE;
1784
1785 /*
1786 * oap->empty is set when start and end are the same. The inclusive
1787 * flag affects this too, unless yanking and the end is on a NUL.
1788 */
1789 oap->empty = (oap->motion_type == MCHAR
1790 && (!oap->inclusive
1791 || (oap->op_type == OP_YANK
1792 && gchar_pos(&oap->end) == NUL))
1793 && equalpos(oap->start, oap->end)
1794#ifdef FEAT_VIRTUALEDIT
1795 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1796#endif
1797 );
1798 /*
1799 * For delete, change and yank, it's an error to operate on an
1800 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1801 */
1802 empty_region_error = (oap->empty
1803 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 /* Force a redraw when operating on an empty Visual region, when
1806 * 'modifiable is off or creating a fold. */
1807 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001808#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001812 {
1813#ifdef FEAT_LINEBREAK
1814 curwin->w_p_lbr = lbr_saved;
1815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 /*
1820 * If the end of an operator is in column one while oap->motion_type
1821 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1822 * character in the previous line. If op_start is on or before the
1823 * first non-blank in the line, the operator becomes linewise
1824 * (strange, but that's the way vi does it).
1825 */
1826 if ( oap->motion_type == MCHAR
1827 && oap->inclusive == FALSE
1828 && !(cap->retval & CA_NO_ADJ_OP_END)
1829 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001831 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 && oap->line_count > 1)
1833 {
1834 oap->end_adjusted = TRUE; /* remember that we did this */
1835 --oap->line_count;
1836 --oap->end.lnum;
1837 if (inindent(0))
1838 oap->motion_type = MLINE;
1839 else
1840 {
1841 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1842 if (oap->end.col)
1843 {
1844 --oap->end.col;
1845 oap->inclusive = TRUE;
1846 }
1847 }
1848 }
1849 else
1850 oap->end_adjusted = FALSE;
1851
1852 switch (oap->op_type)
1853 {
1854 case OP_LSHIFT:
1855 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001856 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 auto_format(FALSE, TRUE);
1858 break;
1859
1860 case OP_JOIN_NS:
1861 case OP_JOIN:
1862 if (oap->line_count < 2)
1863 oap->line_count = 2;
1864 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1865 curbuf->b_ml.ml_line_count)
1866 beep_flush();
1867 else
1868 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001869 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001870 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 auto_format(FALSE, TRUE);
1872 }
1873 break;
1874
1875 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001878 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001879 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001880 CancelRedo();
1881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
1883 {
1884 (void)op_delete(oap);
1885 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1886 u_save_cursor(); /* cursor line wasn't saved yet */
1887 auto_format(FALSE, TRUE);
1888 }
1889 break;
1890
1891 case OP_YANK:
1892 if (empty_region_error)
1893 {
1894 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001895 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001896 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001897 CancelRedo();
1898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001901 {
1902#ifdef FEAT_LINEBREAK
1903 curwin->w_p_lbr = lbr_saved;
1904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 check_cursor_col();
1908 break;
1909
1910 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001913 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001914 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001915 CancelRedo();
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else
1918 {
1919 /* This is a new edit command, not a restart. Need to
1920 * remember it to make 'insertmode' work with mappings for
1921 * Visual mode. But do this only once and not when typed and
1922 * 'insertmode' isn't set. */
1923 if (p_im || !KeyTyped)
1924 restart_edit_save = restart_edit;
1925 else
1926 restart_edit_save = 0;
1927 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001928#ifdef FEAT_LINEBREAK
1929 /* Restore linebreak, so that when the user edits it looks as
1930 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001931 if (curwin->w_p_lbr != lbr_saved)
1932 {
1933 curwin->w_p_lbr = lbr_saved;
1934 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1935 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 /* Reset finish_op now, don't want it set inside edit(). */
1938 finish_op = FALSE;
1939 if (op_change(oap)) /* will call edit() */
1940 cap->retval |= CA_COMMAND_BUSY;
1941 if (restart_edit == 0)
1942 restart_edit = restart_edit_save;
1943 }
1944 break;
1945
1946 case OP_FILTER:
1947 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1948 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1949 else
1950 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1951
1952 case OP_INDENT:
1953 case OP_COLON:
1954
1955#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1956 /*
1957 * If 'equalprg' is empty, do the indenting internally.
1958 */
1959 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1960 {
1961# ifdef FEAT_LISP
1962 if (curbuf->b_p_lisp)
1963 {
1964 op_reindent(oap, get_lisp_indent);
1965 break;
1966 }
1967# endif
1968# ifdef FEAT_CINDENT
1969 op_reindent(oap,
1970# ifdef FEAT_EVAL
1971 *curbuf->b_p_inde != NUL ? get_expr_indent :
1972# endif
1973 get_c_indent);
1974 break;
1975# endif
1976 }
1977#endif
1978
1979 op_colon(oap);
1980 break;
1981
1982 case OP_TILDE:
1983 case OP_UPPER:
1984 case OP_LOWER:
1985 case OP_ROT13:
1986 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001987 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001988 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001989 CancelRedo();
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 else
1992 op_tilde(oap);
1993 check_cursor_col();
1994 break;
1995
1996 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001997#if defined(FEAT_EVAL)
1998 if (*curbuf->b_p_fex != NUL)
1999 op_formatexpr(oap); /* use expression */
2000 else
2001#endif
2002 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 op_colon(oap); /* use external command */
2004 else
2005 op_format(oap, FALSE); /* use internal function */
2006 break;
2007
2008 case OP_FORMAT2:
2009 op_format(oap, TRUE); /* use internal function */
2010 break;
2011
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002012 case OP_FUNCTION:
2013 op_function(oap); /* call 'operatorfunc' */
2014 break;
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case OP_INSERT:
2017 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#ifdef FEAT_VISUALEXTRA
2020 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002021 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002022 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002023 CancelRedo();
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026 {
2027 /* This is a new edit command, not a restart. Need to
2028 * remember it to make 'insertmode' work with mappings for
2029 * Visual mode. But do this only once. */
2030 restart_edit_save = restart_edit;
2031 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002032#ifdef FEAT_LINEBREAK
2033 /* Restore linebreak, so that when the user edits it looks as
2034 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002035 if (curwin->w_p_lbr != lbr_saved)
2036 {
2037 curwin->w_p_lbr = lbr_saved;
2038 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2039 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002042#ifdef FEAT_LINEBREAK
2043 /* Reset linebreak, so that formatting works correctly. */
2044 curwin->w_p_lbr = FALSE;
2045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 /* TODO: when inserting in several lines, should format all
2048 * the lines. */
2049 auto_format(FALSE, TRUE);
2050
2051 if (restart_edit == 0)
2052 restart_edit = restart_edit_save;
2053 }
2054#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002055 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056#endif
2057 break;
2058
2059 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#ifdef FEAT_VISUALEXTRA
2062 if (empty_region_error)
2063#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002064 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002065 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002066 CancelRedo();
2067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#ifdef FEAT_VISUALEXTRA
2069 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002070 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002071# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002072 /* Restore linebreak, so that when the user edits it looks as
2073 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002074 if (curwin->w_p_lbr != lbr_saved)
2075 {
2076 curwin->w_p_lbr = lbr_saved;
2077 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2078 }
2079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082#endif
2083 break;
2084
2085#ifdef FEAT_FOLDING
2086 case OP_FOLD:
2087 VIsual_reselect = FALSE; /* don't reselect now */
2088 foldCreate(oap->start.lnum, oap->end.lnum);
2089 break;
2090
2091 case OP_FOLDOPEN:
2092 case OP_FOLDOPENREC:
2093 case OP_FOLDCLOSE:
2094 case OP_FOLDCLOSEREC:
2095 VIsual_reselect = FALSE; /* don't reselect now */
2096 opFoldRange(oap->start.lnum, oap->end.lnum,
2097 oap->op_type == OP_FOLDOPEN
2098 || oap->op_type == OP_FOLDOPENREC,
2099 oap->op_type == OP_FOLDOPENREC
2100 || oap->op_type == OP_FOLDCLOSEREC,
2101 oap->is_VIsual);
2102 break;
2103
2104 case OP_FOLDDEL:
2105 case OP_FOLDDELREC:
2106 VIsual_reselect = FALSE; /* don't reselect now */
2107 deleteFold(oap->start.lnum, oap->end.lnum,
2108 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2109 break;
2110#endif
2111 default:
2112 clearopbeep(oap);
2113 }
2114#ifdef FEAT_VIRTUALEDIT
2115 virtual_op = MAYBE;
2116#endif
2117 if (!gui_yank)
2118 {
2119 /*
2120 * if 'sol' not set, go back to old column for some commands
2121 */
2122 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2123 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2124 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002125 {
2126#ifdef FEAT_LINEBREAK
2127 curwin->w_p_lbr = FALSE;
2128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
2132 else
2133 {
2134 curwin->w_cursor = old_cursor;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 clearop(oap);
2138 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002139#ifdef FEAT_LINEBREAK
2140 curwin->w_p_lbr = lbr_saved;
2141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142}
2143
2144/*
2145 * Handle indent and format operators and visual mode ":".
2146 */
2147 static void
2148op_colon(oap)
2149 oparg_T *oap;
2150{
2151 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (oap->is_VIsual)
2153 stuffReadbuff((char_u *)"'<,'>");
2154 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 /*
2157 * Make the range look nice, so it can be repeated.
2158 */
2159 if (oap->start.lnum == curwin->w_cursor.lnum)
2160 stuffcharReadbuff('.');
2161 else
2162 stuffnumReadbuff((long)oap->start.lnum);
2163 if (oap->end.lnum != oap->start.lnum)
2164 {
2165 stuffcharReadbuff(',');
2166 if (oap->end.lnum == curwin->w_cursor.lnum)
2167 stuffcharReadbuff('.');
2168 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2169 stuffcharReadbuff('$');
2170 else if (oap->start.lnum == curwin->w_cursor.lnum)
2171 {
2172 stuffReadbuff((char_u *)".+");
2173 stuffnumReadbuff((long)oap->line_count - 1);
2174 }
2175 else
2176 stuffnumReadbuff((long)oap->end.lnum);
2177 }
2178 }
2179 if (oap->op_type != OP_COLON)
2180 stuffReadbuff((char_u *)"!");
2181 if (oap->op_type == OP_INDENT)
2182 {
2183#ifndef FEAT_CINDENT
2184 if (*get_equalprg() == NUL)
2185 stuffReadbuff((char_u *)"indent");
2186 else
2187#endif
2188 stuffReadbuff(get_equalprg());
2189 stuffReadbuff((char_u *)"\n");
2190 }
2191 else if (oap->op_type == OP_FORMAT)
2192 {
2193 if (*p_fp == NUL)
2194 stuffReadbuff((char_u *)"fmt");
2195 else
2196 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002197 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 }
2199
2200 /*
2201 * do_cmdline() does the rest
2202 */
2203}
2204
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002205/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002206 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002207 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002208 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002209op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002210 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002211{
2212#ifdef FEAT_EVAL
2213 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002214# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002215 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002216# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002217
2218 if (*p_opfunc == NUL)
2219 EMSG(_("E774: 'operatorfunc' is empty"));
2220 else
2221 {
2222 /* Set '[ and '] marks to text to be operated on. */
2223 curbuf->b_op_start = oap->start;
2224 curbuf->b_op_end = oap->end;
2225 if (oap->motion_type != MLINE && !oap->inclusive)
2226 /* Exclude the end position. */
2227 decl(&curbuf->b_op_end);
2228
2229 if (oap->block_mode)
2230 argv[0] = (char_u *)"block";
2231 else if (oap->motion_type == MLINE)
2232 argv[0] = (char_u *)"line";
2233 else
2234 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002235
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002236# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002237 /* Reset virtual_op so that 'virtualedit' can be changed in the
2238 * function. */
2239 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002240# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002241
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002242 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002243
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002244# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002245 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002246# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002247 }
2248#else
2249 EMSG(_("E775: Eval feature not available"));
2250#endif
2251}
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253#if defined(FEAT_MOUSE) || defined(PROTO)
2254/*
2255 * Do the appropriate action for the current mouse click in the current mode.
2256 * Not used for Command-line mode.
2257 *
2258 * Normal Mode:
2259 * event modi- position visual change action
2260 * fier cursor window
2261 * left press - yes end yes
2262 * left press C yes end yes "^]" (2)
2263 * left press S yes end yes "*" (2)
2264 * left drag - yes start if moved no
2265 * left relse - yes start if moved no
2266 * middle press - yes if not active no put register
2267 * middle press - yes if active no yank and put
2268 * right press - yes start or extend yes
2269 * right press S yes no change yes "#" (2)
2270 * right drag - yes extend no
2271 * right relse - yes extend no
2272 *
2273 * Insert or Replace Mode:
2274 * event modi- position visual change action
2275 * fier cursor window
2276 * left press - yes (cannot be active) yes
2277 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2278 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2279 * left drag - yes start or extend (1) no CTRL-O (1)
2280 * left relse - yes start or extend (1) no CTRL-O (1)
2281 * middle press - no (cannot be active) no put register
2282 * right press - yes start or extend yes CTRL-O
2283 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2284 *
2285 * (1) only if mouse pointer moved since press
2286 * (2) only if click is in same buffer
2287 *
2288 * Return TRUE if start_arrow() should be called for edit mode.
2289 */
2290 int
2291do_mouse(oap, c, dir, count, fixindent)
2292 oparg_T *oap; /* operator argument, can be NULL */
2293 int c; /* K_LEFTMOUSE, etc */
2294 int dir; /* Direction to 'put' if necessary */
2295 long count;
2296 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2297{
2298 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2299 static int got_click = FALSE; /* got a click some time back */
2300
2301 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2302 int is_click; /* If FALSE it's a drag or release event */
2303 int is_drag; /* If TRUE it's a drag event */
2304 int jump_flags = 0; /* flags for jump_to_mouse() */
2305 pos_T start_visual;
2306 int moved; /* Has cursor moved? */
2307 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002308#ifdef FEAT_WINDOWS
2309 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#ifdef FEAT_VERTSPLIT
2312 int in_sep_line; /* mouse in vertical separator line */
2313#endif
2314 int c1, c2;
2315#if defined(FEAT_FOLDING)
2316 pos_T save_cursor;
2317#endif
2318 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 static pos_T orig_cursor;
2320 colnr_T leftcol, rightcol;
2321 pos_T end_visual;
2322 int diff;
2323 int old_active = VIsual_active;
2324 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 int regname;
2326
2327#if defined(FEAT_FOLDING)
2328 save_cursor = curwin->w_cursor;
2329#endif
2330
2331 /*
2332 * When GUI is active, always recognize mouse events, otherwise:
2333 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2334 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2335 * - For command line and insert mode 'mouse' is checked before calling
2336 * do_mouse().
2337 */
2338 if (do_always)
2339 do_always = FALSE;
2340 else
2341#ifdef FEAT_GUI
2342 if (!gui.in_use)
2343#endif
2344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (VIsual_active)
2346 {
2347 if (!mouse_has(MOUSE_VISUAL))
2348 return FALSE;
2349 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002350 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 return FALSE;
2352 }
2353
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002354 for (;;)
2355 {
2356 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2357 if (is_drag)
2358 {
2359 /* If the next character is the same mouse event then use that
2360 * one. Speeds up dragging the status line. */
2361 if (vpeekc() != NUL)
2362 {
2363 int nc;
2364 int save_mouse_row = mouse_row;
2365 int save_mouse_col = mouse_col;
2366
2367 /* Need to get the character, peeking doesn't get the actual
2368 * one. */
2369 nc = safe_vgetc();
2370 if (c == nc)
2371 continue;
2372 vungetc(nc);
2373 mouse_row = save_mouse_row;
2374 mouse_col = save_mouse_col;
2375 }
2376 }
2377 break;
2378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380#ifdef FEAT_MOUSESHAPE
2381 /* May have stopped dragging the status or separator line. The pointer is
2382 * most likely still on the status or separator line. */
2383 if (!is_drag && drag_status_line)
2384 {
2385 drag_status_line = FALSE;
2386 update_mouseshape(SHAPE_IDX_STATUS);
2387 }
2388# ifdef FEAT_VERTSPLIT
2389 if (!is_drag && drag_sep_line)
2390 {
2391 drag_sep_line = FALSE;
2392 update_mouseshape(SHAPE_IDX_VSEP);
2393 }
2394# endif
2395#endif
2396
2397 /*
2398 * Ignore drag and release events if we didn't get a click.
2399 */
2400 if (is_click)
2401 got_click = TRUE;
2402 else
2403 {
2404 if (!got_click) /* didn't get click, ignore */
2405 return FALSE;
2406 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002409#ifdef FEAT_WINDOWS
2410 if (in_tab_line)
2411 {
2412 in_tab_line = FALSE;
2413 return FALSE;
2414 }
2415#endif
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /*
2420 * CTRL right mouse button does CTRL-T
2421 */
2422 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2423 {
2424 if (State & INSERT)
2425 stuffcharReadbuff(Ctrl_O);
2426 if (count > 1)
2427 stuffnumReadbuff(count);
2428 stuffcharReadbuff(Ctrl_T);
2429 got_click = FALSE; /* ignore drag&release now */
2430 return FALSE;
2431 }
2432
2433 /*
2434 * CTRL only works with left mouse button
2435 */
2436 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2437 return FALSE;
2438
2439 /*
2440 * When a modifier is down, ignore drag and release events, as well as
2441 * multiple clicks and the middle mouse button.
2442 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2443 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002444 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2445 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 && (!is_click
2447 || (mod_mask & MOD_MASK_MULTI_CLICK)
2448 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002449 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 && mouse_model_popup()
2451 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002452 && !((mod_mask & MOD_MASK_ALT)
2453 && !mouse_model_popup()
2454 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 )
2456 return FALSE;
2457
2458 /*
2459 * If the button press was used as the movement command for an operator
2460 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2461 * drag/release events.
2462 */
2463 if (!is_click && which_button == MOUSE_MIDDLE)
2464 return FALSE;
2465
2466 if (oap != NULL)
2467 regname = oap->regname;
2468 else
2469 regname = 0;
2470
2471 /*
2472 * Middle mouse button does a 'put' of the selected text
2473 */
2474 if (which_button == MOUSE_MIDDLE)
2475 {
2476 if (State == NORMAL)
2477 {
2478 /*
2479 * If an operator was pending, we don't know what the user wanted
2480 * to do. Go back to normal mode: Clear the operator and beep().
2481 */
2482 if (oap != NULL && oap->op_type != OP_NOP)
2483 {
2484 clearopbeep(oap);
2485 return FALSE;
2486 }
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /*
2489 * If visual was active, yank the highlighted text and put it
2490 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002491 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 if (VIsual_active)
2494 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002495 if (VIsual_select)
2496 {
2497 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002498 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002499 }
2500 else
2501 {
2502 stuffcharReadbuff('y');
2503 stuffcharReadbuff(K_MIDDLEMOUSE);
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 do_always = TRUE; /* ignore 'mouse' setting next time */
2506 return FALSE;
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /*
2509 * The rest is below jump_to_mouse()
2510 */
2511 }
2512
2513 else if ((State & INSERT) == 0)
2514 return FALSE;
2515
2516 /*
2517 * Middle click in insert mode doesn't move the mouse, just insert the
2518 * contents of a register. '.' register is special, can't insert that
2519 * with do_put().
2520 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2521 * happens for the GUI).
2522 */
2523 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2524 {
2525 if (regname == '.')
2526 insert_reg(regname, TRUE);
2527 else
2528 {
2529#ifdef FEAT_CLIPBOARD
2530 if (clip_star.available && regname == 0)
2531 regname = '*';
2532#endif
2533 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2534 insert_reg(regname, TRUE);
2535 else
2536 {
2537 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2538
2539 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2540 AppendCharToRedobuff(Ctrl_R);
2541 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2542 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2543 }
2544 }
2545 return FALSE;
2546 }
2547 }
2548
2549 /* When dragging or button-up stay in the same window. */
2550 if (!is_click)
2551 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2552
2553 start_visual.lnum = 0;
2554
Bram Moolenaarf740b292006-02-16 22:11:02 +00002555#ifdef FEAT_WINDOWS
2556 /* Check for clicking in the tab page line. */
2557 if (mouse_row == 0 && firstwin->w_winrow > 0)
2558 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002559 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002560 {
2561 if (in_tab_line)
2562 {
2563 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002564 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2565 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002566 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002567 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002568 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* click in a tab selects that tab page */
2571 if (is_click
2572# ifdef FEAT_CMDWIN
2573 && cmdwin_type == 0
2574# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002576 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002577 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002578 c1 = TabPageIdxs[mouse_col];
2579 if (c1 >= 0)
2580 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002581 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2582 {
2583 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002585 tabpage_new();
2586 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2587 }
2588 else
2589 {
2590 /* Go to specified tab page, or next one if not clicking
2591 * on a label. */
2592 goto_tabpage(c1);
2593
2594 /* It's like clicking on the status line of a window. */
2595 if (curwin != old_curwin)
2596 end_visual_mode();
2597 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 }
2599 else if (c1 < 0)
2600 {
2601 tabpage_T *tp;
2602
2603 /* Close the current or specified tab page. */
2604 if (c1 == -999)
2605 tp = curtab;
2606 else
2607 tp = find_tabpage(-c1);
2608 if (tp == curtab)
2609 {
2610 if (first_tabpage->tp_next != NULL)
2611 tabpage_close(FALSE);
2612 }
2613 else if (tp != NULL)
2614 tabpage_close_other(tp, FALSE);
2615 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002619 else if (is_drag && in_tab_line)
2620 {
2621 c1 = TabPageIdxs[mouse_col];
2622 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2623 return FALSE;
2624 }
2625
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /*
2629 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2630 * right button up -> pop-up menu
2631 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002632 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
2634 if (mouse_model_popup())
2635 {
2636 if (which_button == MOUSE_RIGHT
2637 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2638 {
2639 /*
2640 * NOTE: Ignore right button down and drag mouse events.
2641 * Windows only shows the popup menu on the button up event.
2642 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002643#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2644 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (!is_click)
2646 return FALSE;
2647#endif
2648#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2649 if (is_click || is_drag)
2650 return FALSE;
2651#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002652#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2654 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2655 if (gui.in_use)
2656 {
2657 jump_flags = 0;
2658 if (STRCMP(p_mousem, "popup_setpos") == 0)
2659 {
2660 /* First set the cursor position before showing the popup
2661 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if (VIsual_active)
2663 {
2664 pos_T m_pos;
2665
2666 /*
2667 * set MOUSE_MAY_STOP_VIS if we are outside the
2668 * selection or the current window (might have false
2669 * negative here)
2670 */
2671 if (mouse_row < W_WINROW(curwin)
2672 || mouse_row
2673 > (W_WINROW(curwin) + curwin->w_height))
2674 jump_flags = MOUSE_MAY_STOP_VIS;
2675 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2676 jump_flags = MOUSE_MAY_STOP_VIS;
2677 else
2678 {
2679 if ((lt(curwin->w_cursor, VIsual)
2680 && (lt(m_pos, curwin->w_cursor)
2681 || lt(VIsual, m_pos)))
2682 || (lt(VIsual, curwin->w_cursor)
2683 && (lt(m_pos, VIsual)
2684 || lt(curwin->w_cursor, m_pos))))
2685 {
2686 jump_flags = MOUSE_MAY_STOP_VIS;
2687 }
2688 else if (VIsual_mode == Ctrl_V)
2689 {
2690 getvcols(curwin, &curwin->w_cursor, &VIsual,
2691 &leftcol, &rightcol);
2692 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2693 if (m_pos.col < leftcol || m_pos.col > rightcol)
2694 jump_flags = MOUSE_MAY_STOP_VIS;
2695 }
2696 }
2697 }
2698 else
2699 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 if (jump_flags)
2702 {
2703 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002704 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 setcursor();
2706 out_flush(); /* Update before showing popup menu */
2707 }
2708# ifdef FEAT_MENU
2709 gui_show_popupmenu();
2710# endif
2711 return (jump_flags & CURSOR_MOVED) != 0;
2712 }
2713 else
2714 return FALSE;
2715#else
2716 return FALSE;
2717#endif
2718 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002719 if (which_button == MOUSE_LEFT
2720 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 which_button = MOUSE_RIGHT;
2723 mod_mask &= ~MOD_MASK_SHIFT;
2724 }
2725 }
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if ((State & (NORMAL | INSERT))
2728 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2729 {
2730 if (which_button == MOUSE_LEFT)
2731 {
2732 if (is_click)
2733 {
2734 /* stop Visual mode for a left click in a window, but not when
2735 * on a status line */
2736 if (VIsual_active)
2737 jump_flags |= MOUSE_MAY_STOP_VIS;
2738 }
2739 else if (mouse_has(MOUSE_VISUAL))
2740 jump_flags |= MOUSE_MAY_VIS;
2741 }
2742 else if (which_button == MOUSE_RIGHT)
2743 {
2744 if (is_click && VIsual_active)
2745 {
2746 /*
2747 * Remember the start and end of visual before moving the
2748 * cursor.
2749 */
2750 if (lt(curwin->w_cursor, VIsual))
2751 {
2752 start_visual = curwin->w_cursor;
2753 end_visual = VIsual;
2754 }
2755 else
2756 {
2757 start_visual = VIsual;
2758 end_visual = curwin->w_cursor;
2759 }
2760 }
2761 jump_flags |= MOUSE_FOCUS;
2762 if (mouse_has(MOUSE_VISUAL))
2763 jump_flags |= MOUSE_MAY_VIS;
2764 }
2765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
2767 /*
2768 * If an operator is pending, ignore all drags and releases until the
2769 * next mouse click.
2770 */
2771 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2772 {
2773 got_click = FALSE;
2774 oap->motion_type = MCHAR;
2775 }
2776
2777 /* When releasing the button let jump_to_mouse() know. */
2778 if (!is_click && !is_drag)
2779 jump_flags |= MOUSE_RELEASED;
2780
2781 /*
2782 * JUMP!
2783 */
2784 jump_flags = jump_to_mouse(jump_flags,
2785 oap == NULL ? NULL : &(oap->inclusive), which_button);
2786 moved = (jump_flags & CURSOR_MOVED);
2787 in_status_line = (jump_flags & IN_STATUS_LINE);
2788#ifdef FEAT_VERTSPLIT
2789 in_sep_line = (jump_flags & IN_SEP_LINE);
2790#endif
2791
2792#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002793 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2795 {
2796 int key = KEY2TERMCAP1(c);
2797
2798 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2799 || key == (int)KE_RIGHTRELEASE)
2800 netbeans_button_release(which_button);
2801 }
2802#endif
2803
2804 /* When jumping to another window, clear a pending operator. That's a bit
2805 * friendlier than beeping and not jumping to that window. */
2806 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2807 clearop(oap);
2808
2809#ifdef FEAT_FOLDING
2810 if (mod_mask == 0
2811 && !is_drag
2812 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2813 && which_button == MOUSE_LEFT)
2814 {
2815 /* open or close a fold at this line */
2816 if (jump_flags & MOUSE_FOLD_OPEN)
2817 openFold(curwin->w_cursor.lnum, 1L);
2818 else
2819 closeFold(curwin->w_cursor.lnum, 1L);
2820 /* don't move the cursor if still in the same window */
2821 if (curwin == old_curwin)
2822 curwin->w_cursor = save_cursor;
2823 }
2824#endif
2825
2826#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2827 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2828 {
2829 clip_modeless(which_button, is_click, is_drag);
2830 return FALSE;
2831 }
2832#endif
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002835 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (VIsual_active && is_drag && p_so)
2837 {
2838 /* In the very first line, allow scrolling one line */
2839 if (mouse_row == 0)
2840 mouse_dragging = 2;
2841 else
2842 mouse_dragging = 1;
2843 }
2844
2845 /* When dragging the mouse above the window, scroll down. */
2846 if (is_drag && mouse_row < 0 && !in_status_line)
2847 {
2848 scroll_redraw(FALSE, 1L);
2849 mouse_row = 0;
2850 }
2851
2852 if (start_visual.lnum) /* right click in visual mode */
2853 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002854 /* When ALT is pressed make Visual mode blockwise. */
2855 if (mod_mask & MOD_MASK_ALT)
2856 VIsual_mode = Ctrl_V;
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /*
2859 * In Visual-block mode, divide the area in four, pick up the corner
2860 * that is in the quarter that the cursor is in.
2861 */
2862 if (VIsual_mode == Ctrl_V)
2863 {
2864 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2865 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2866 end_visual.col = leftcol;
2867 else
2868 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002869 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 end_visual.lnum = start_visual.lnum;
2872
2873 /* move VIsual to the right column */
2874 start_visual = curwin->w_cursor; /* save the cursor pos */
2875 curwin->w_cursor = end_visual;
2876 coladvance(end_visual.col);
2877 VIsual = curwin->w_cursor;
2878 curwin->w_cursor = start_visual; /* restore the cursor */
2879 }
2880 else
2881 {
2882 /*
2883 * If the click is before the start of visual, change the start.
2884 * If the click is after the end of visual, change the end. If
2885 * the click is inside the visual, change the closest side.
2886 */
2887 if (lt(curwin->w_cursor, start_visual))
2888 VIsual = end_visual;
2889 else if (lt(end_visual, curwin->w_cursor))
2890 VIsual = start_visual;
2891 else
2892 {
2893 /* In the same line, compare column number */
2894 if (end_visual.lnum == start_visual.lnum)
2895 {
2896 if (curwin->w_cursor.col - start_visual.col >
2897 end_visual.col - curwin->w_cursor.col)
2898 VIsual = start_visual;
2899 else
2900 VIsual = end_visual;
2901 }
2902
2903 /* In different lines, compare line number */
2904 else
2905 {
2906 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2907 (end_visual.lnum - curwin->w_cursor.lnum);
2908
2909 if (diff > 0) /* closest to end */
2910 VIsual = start_visual;
2911 else if (diff < 0) /* closest to start */
2912 VIsual = end_visual;
2913 else /* in the middle line */
2914 {
2915 if (curwin->w_cursor.col <
2916 (start_visual.col + end_visual.col) / 2)
2917 VIsual = end_visual;
2918 else
2919 VIsual = start_visual;
2920 }
2921 }
2922 }
2923 }
2924 }
2925 /*
2926 * If Visual mode started in insert mode, execute "CTRL-O"
2927 */
2928 else if ((State & INSERT) && VIsual_active)
2929 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 /*
2932 * Middle mouse click: Put text before cursor.
2933 */
2934 if (which_button == MOUSE_MIDDLE)
2935 {
2936#ifdef FEAT_CLIPBOARD
2937 if (clip_star.available && regname == 0)
2938 regname = '*';
2939#endif
2940 if (yank_register_mline(regname))
2941 {
2942 if (mouse_past_bottom)
2943 dir = FORWARD;
2944 }
2945 else if (mouse_past_eol)
2946 dir = FORWARD;
2947
2948 if (fixindent)
2949 {
2950 c1 = (dir == BACKWARD) ? '[' : ']';
2951 c2 = 'p';
2952 }
2953 else
2954 {
2955 c1 = (dir == FORWARD) ? 'p' : 'P';
2956 c2 = NUL;
2957 }
2958 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2959
2960 /*
2961 * Remember where the paste started, so in edit() Insstart can be set
2962 * to this position
2963 */
2964 if (restart_edit != 0)
2965 where_paste_started = curwin->w_cursor;
2966 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2967 }
2968
2969#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2970 /*
2971 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2972 * error under the mouse pointer.
2973 */
2974 else if (((mod_mask & MOD_MASK_CTRL)
2975 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2976 && bt_quickfix(curbuf))
2977 {
2978 if (State & INSERT)
2979 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002980 if (curwin->w_llist_ref == NULL) /* quickfix window */
2981 stuffReadbuff((char_u *)":.cc\n");
2982 else /* location list window */
2983 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 got_click = FALSE; /* ignore drag&release now */
2985 }
2986#endif
2987
2988 /*
2989 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2990 * under the mouse pointer.
2991 */
2992 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2993 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2994 {
2995 if (State & INSERT)
2996 stuffcharReadbuff(Ctrl_O);
2997 stuffcharReadbuff(Ctrl_RSB);
2998 got_click = FALSE; /* ignore drag&release now */
2999 }
3000
3001 /*
3002 * Shift-Mouse click searches for the next occurrence of the word under
3003 * the mouse pointer
3004 */
3005 else if ((mod_mask & MOD_MASK_SHIFT))
3006 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003007 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 stuffcharReadbuff(Ctrl_O);
3009 if (which_button == MOUSE_LEFT)
3010 stuffcharReadbuff('*');
3011 else /* MOUSE_RIGHT */
3012 stuffcharReadbuff('#');
3013 }
3014
3015 /* Handle double clicks, unless on status line */
3016 else if (in_status_line)
3017 {
3018#ifdef FEAT_MOUSESHAPE
3019 if ((is_drag || is_click) && !drag_status_line)
3020 {
3021 drag_status_line = TRUE;
3022 update_mouseshape(-1);
3023 }
3024#endif
3025 }
3026#ifdef FEAT_VERTSPLIT
3027 else if (in_sep_line)
3028 {
3029# ifdef FEAT_MOUSESHAPE
3030 if ((is_drag || is_click) && !drag_sep_line)
3031 {
3032 drag_sep_line = TRUE;
3033 update_mouseshape(-1);
3034 }
3035# endif
3036 }
3037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3039 && mouse_has(MOUSE_VISUAL))
3040 {
3041 if (is_click || !VIsual_active)
3042 {
3043 if (VIsual_active)
3044 orig_cursor = VIsual;
3045 else
3046 {
3047 check_visual_highlight();
3048 VIsual = curwin->w_cursor;
3049 orig_cursor = VIsual;
3050 VIsual_active = TRUE;
3051 VIsual_reselect = TRUE;
3052 /* start Select mode if 'selectmode' contains "mouse" */
3053 may_start_select('o');
3054 setmouse();
3055 }
3056 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003057 {
3058 /* Double click with ALT pressed makes it blockwise. */
3059 if (mod_mask & MOD_MASK_ALT)
3060 VIsual_mode = Ctrl_V;
3061 else
3062 VIsual_mode = 'v';
3063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3065 VIsual_mode = 'V';
3066 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3067 VIsual_mode = Ctrl_V;
3068#ifdef FEAT_CLIPBOARD
3069 /* Make sure the clipboard gets updated. Needed because start and
3070 * end may still be the same, and the selection needs to be owned */
3071 clip_star.vmode = NUL;
3072#endif
3073 }
3074 /*
3075 * A double click selects a word or a block.
3076 */
3077 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3078 {
3079 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003080 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082 if (is_click)
3083 {
3084 /* If the character under the cursor (skipping white space) is
3085 * not a word character, try finding a match and select a (),
3086 * {}, [], #if/#endif, etc. block. */
3087 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003088 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 inc(&end_visual);
3090 if (oap != NULL)
3091 oap->motion_type = MCHAR;
3092 if (oap != NULL
3093 && VIsual_mode == 'v'
3094 && !vim_iswordc(gchar_pos(&end_visual))
3095 && equalpos(curwin->w_cursor, VIsual)
3096 && (pos = findmatch(oap, NUL)) != NULL)
3097 {
3098 curwin->w_cursor = *pos;
3099 if (oap->motion_type == MLINE)
3100 VIsual_mode = 'V';
3101 else if (*p_sel == 'e')
3102 {
3103 if (lt(curwin->w_cursor, VIsual))
3104 ++VIsual.col;
3105 else
3106 ++curwin->w_cursor.col;
3107 }
3108 }
3109 }
3110
3111 if (pos == NULL && (is_click || is_drag))
3112 {
3113 /* When not found a match or when dragging: extend to include
3114 * a word. */
3115 if (lt(curwin->w_cursor, orig_cursor))
3116 {
3117 find_start_of_word(&curwin->w_cursor);
3118 find_end_of_word(&VIsual);
3119 }
3120 else
3121 {
3122 find_start_of_word(&VIsual);
3123 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3124#ifdef FEAT_MBYTE
3125 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003126 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#else
3128 ++curwin->w_cursor.col;
3129#endif
3130 find_end_of_word(&curwin->w_cursor);
3131 }
3132 }
3133 curwin->w_set_curswant = TRUE;
3134 }
3135 if (is_click)
3136 redraw_curbuf_later(INVERTED); /* update the inversion */
3137 }
3138 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003139 {
3140 if (mod_mask & MOD_MASK_ALT)
3141 VIsual_mode = Ctrl_V;
3142 else
3143 VIsual_mode = 'v';
3144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003147 if ((!VIsual_active && old_active && mode_displayed)
3148 || (VIsual_active && p_smd && msg_silent == 0
3149 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 return moved;
3153}
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155/*
3156 * Move "pos" back to the start of the word it's in.
3157 */
3158 static void
3159find_start_of_word(pos)
3160 pos_T *pos;
3161{
3162 char_u *line;
3163 int cclass;
3164 int col;
3165
3166 line = ml_get(pos->lnum);
3167 cclass = get_mouse_class(line + pos->col);
3168
3169 while (pos->col > 0)
3170 {
3171 col = pos->col - 1;
3172#ifdef FEAT_MBYTE
3173 col -= (*mb_head_off)(line, line + col);
3174#endif
3175 if (get_mouse_class(line + col) != cclass)
3176 break;
3177 pos->col = col;
3178 }
3179}
3180
3181/*
3182 * Move "pos" forward to the end of the word it's in.
3183 * When 'selection' is "exclusive", the position is just after the word.
3184 */
3185 static void
3186find_end_of_word(pos)
3187 pos_T *pos;
3188{
3189 char_u *line;
3190 int cclass;
3191 int col;
3192
3193 line = ml_get(pos->lnum);
3194 if (*p_sel == 'e' && pos->col > 0)
3195 {
3196 --pos->col;
3197#ifdef FEAT_MBYTE
3198 pos->col -= (*mb_head_off)(line, line + pos->col);
3199#endif
3200 }
3201 cclass = get_mouse_class(line + pos->col);
3202 while (line[pos->col] != NUL)
3203 {
3204#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003205 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#else
3207 col = pos->col + 1;
3208#endif
3209 if (get_mouse_class(line + col) != cclass)
3210 {
3211 if (*p_sel == 'e')
3212 pos->col = col;
3213 break;
3214 }
3215 pos->col = col;
3216 }
3217}
3218
3219/*
3220 * Get class of a character for selection: same class means same word.
3221 * 0: blank
3222 * 1: punctuation groups
3223 * 2: normal word character
3224 * >2: multi-byte word character.
3225 */
3226 static int
3227get_mouse_class(p)
3228 char_u *p;
3229{
3230 int c;
3231
3232#ifdef FEAT_MBYTE
3233 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3234 return mb_get_class(p);
3235#endif
3236
3237 c = *p;
3238 if (c == ' ' || c == '\t')
3239 return 0;
3240
3241 if (vim_iswordc(c))
3242 return 2;
3243
3244 /*
3245 * There are a few special cases where we want certain combinations of
3246 * characters to be considered as a single word. These are things like
3247 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003248 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3251 return 1;
3252 return c;
3253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#endif /* FEAT_MOUSE */
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Check if highlighting for visual mode is possible, give a warning message
3258 * if not.
3259 */
3260 void
3261check_visual_highlight()
3262{
3263 static int did_check = FALSE;
3264
3265 if (full_screen)
3266 {
3267 if (!did_check && hl_attr(HLF_V) == 0)
3268 MSG(_("Warning: terminal cannot highlight"));
3269 did_check = TRUE;
3270 }
3271}
3272
3273/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003274 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 * This function should ALWAYS be called to end Visual mode, except from
3276 * do_pending_operator().
3277 */
3278 void
3279end_visual_mode()
3280{
3281#ifdef FEAT_CLIPBOARD
3282 /*
3283 * If we are using the clipboard, then remember what was selected in case
3284 * we need to paste it somewhere while we still own the selection.
3285 * Only do this when the clipboard is already owned. Don't want to grab
3286 * the selection when hitting ESC.
3287 */
3288 if (clip_star.available && clip_star.owned)
3289 clip_auto_select();
3290#endif
3291
3292 VIsual_active = FALSE;
3293#ifdef FEAT_MOUSE
3294 setmouse();
3295 mouse_dragging = 0;
3296#endif
3297
3298 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003299 curbuf->b_visual.vi_mode = VIsual_mode;
3300 curbuf->b_visual.vi_start = VIsual;
3301 curbuf->b_visual.vi_end = curwin->w_cursor;
3302 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#ifdef FEAT_EVAL
3304 curbuf->b_visual_mode_eval = VIsual_mode;
3305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#ifdef FEAT_VIRTUALEDIT
3307 if (!virtual_active())
3308 curwin->w_cursor.coladd = 0;
3309#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003310 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003312 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Reset VIsual_active and VIsual_reselect.
3317 */
3318 void
3319reset_VIsual_and_resel()
3320{
3321 if (VIsual_active)
3322 {
3323 end_visual_mode();
3324 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3325 }
3326 VIsual_reselect = FALSE;
3327}
3328
3329/*
3330 * Reset VIsual_active and VIsual_reselect if it's set.
3331 */
3332 void
3333reset_VIsual()
3334{
3335 if (VIsual_active)
3336 {
3337 end_visual_mode();
3338 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3339 VIsual_reselect = FALSE;
3340 }
3341}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3345
3346/*
3347 * Check for a balloon-eval special item to include when searching for an
3348 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3349 * Returns TRUE if the character at "*ptr" should be included.
3350 * "dir" is FORWARD or BACKWARD, the direction of searching.
3351 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3352 * "bnp" points to a counter for square brackets.
3353 */
3354 static int
3355find_is_eval_item(ptr, colp, bnp, dir)
3356 char_u *ptr;
3357 int *colp;
3358 int *bnp;
3359 int dir;
3360{
3361 /* Accept everything inside []. */
3362 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3363 ++*bnp;
3364 if (*bnp > 0)
3365 {
3366 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3367 --*bnp;
3368 return TRUE;
3369 }
3370
3371 /* skip over "s.var" */
3372 if (*ptr == '.')
3373 return TRUE;
3374
3375 /* two-character item: s->var */
3376 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3377 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3378 {
3379 *colp += dir;
3380 return TRUE;
3381 }
3382 return FALSE;
3383}
3384#endif
3385
3386/*
3387 * Find the identifier under or to the right of the cursor.
3388 * "find_type" can have one of three values:
3389 * FIND_IDENT: find an identifier (keyword)
3390 * FIND_STRING: find any non-white string
3391 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003392 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * There are three steps:
3395 * 1. Search forward for the start of an identifier/string. Doesn't move if
3396 * already on one.
3397 * 2. Search backward for the start of this identifier/string.
3398 * This doesn't match the real Vi but I like it a little better and it
3399 * shouldn't bother anyone.
3400 * 3. Search forward to the end of this identifier/string.
3401 * When FIND_IDENT isn't defined, we backup until a blank.
3402 *
3403 * Returns the length of the string, or zero if no string is found.
3404 * If a string is found, a pointer to the string is put in "*string". This
3405 * string is not always NUL terminated.
3406 */
3407 int
3408find_ident_under_cursor(string, find_type)
3409 char_u **string;
3410 int find_type;
3411{
3412 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3413 curwin->w_cursor.col, string, find_type);
3414}
3415
3416/*
3417 * Like find_ident_under_cursor(), but for any window and any position.
3418 * However: Uses 'iskeyword' from the current window!.
3419 */
3420 int
3421find_ident_at_pos(wp, lnum, startcol, string, find_type)
3422 win_T *wp;
3423 linenr_T lnum;
3424 colnr_T startcol;
3425 char_u **string;
3426 int find_type;
3427{
3428 char_u *ptr;
3429 int col = 0; /* init to shut up GCC */
3430 int i;
3431#ifdef FEAT_MBYTE
3432 int this_class = 0;
3433 int prev_class;
3434 int prevcol;
3435#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int bn = 0; /* bracket nesting */
3438#endif
3439
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 Moolenaar52b4b552005-03-07 23:00:57 +00003456# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* Stop at a ']' to evaluate "a[x]". */
3458 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3459 break;
3460# endif
3461 this_class = mb_get_class(ptr + col);
3462 if (this_class != 0 && (i == 1 || this_class != 1))
3463 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003464 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 }
3467 else
3468#endif
3469 while (ptr[col] != NUL
3470 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3473# endif
3474 )
3475 ++col;
3476
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* When starting on a ']' count it, so that we include the '['. */
3479 bn = ptr[col] == ']';
3480#endif
3481
3482 /*
3483 * 2. Back up to start of identifier/string.
3484 */
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 {
3488 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3491 this_class = mb_get_class((char_u *)"a");
3492 else
3493# endif
3494 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3498 prev_class = mb_get_class(ptr + prevcol);
3499 if (this_class != prev_class
3500 && (i == 0
3501 || prev_class == 0
3502 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003503# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 && (!(find_type & FIND_EVAL)
3505 || prevcol == 0
3506 || !find_is_eval_item(ptr + prevcol, &prevcol,
3507 &bn, BACKWARD))
3508# endif
3509 )
3510 break;
3511 col = prevcol;
3512 }
3513
3514 /* If we don't want just any old string, or we've found an
3515 * identifier, stop searching. */
3516 if (this_class > 2)
3517 this_class = 2;
3518 if (!(find_type & FIND_STRING) || this_class == 2)
3519 break;
3520 }
3521 else
3522#endif
3523 {
3524 while (col > 0
3525 && ((i == 0
3526 ? vim_iswordc(ptr[col - 1])
3527 : (!vim_iswhite(ptr[col - 1])
3528 && (!(find_type & FIND_IDENT)
3529 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 || ((find_type & FIND_EVAL)
3532 && col > 1
3533 && find_is_eval_item(ptr + col - 1, &col,
3534 &bn, BACKWARD))
3535#endif
3536 ))
3537 --col;
3538
3539 /* If we don't want just any old string, or we've found an
3540 * identifier, stop searching. */
3541 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3542 break;
3543 }
3544 }
3545
3546 if (ptr[col] == NUL || (i == 0 && (
3547#ifdef FEAT_MBYTE
3548 has_mbyte ? this_class != 2 :
3549#endif
3550 !vim_iswordc(ptr[col]))))
3551 {
3552 /*
3553 * didn't find an identifier or string
3554 */
3555 if (find_type & FIND_STRING)
3556 EMSG(_("E348: No string under cursor"));
3557 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return 0;
3560 }
3561 ptr += col;
3562 *string = ptr;
3563
3564 /*
3565 * 3. Find the end if the identifier/string.
3566 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 bn = 0;
3569 startcol -= col;
3570#endif
3571 col = 0;
3572#ifdef FEAT_MBYTE
3573 if (has_mbyte)
3574 {
3575 /* Search for point of changing multibyte character class. */
3576 this_class = mb_get_class(ptr);
3577 while (ptr[col] != NUL
3578 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3579 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 || ((find_type & FIND_EVAL)
3582 && col <= (int)startcol
3583 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3584# endif
3585 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003586 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 else
3589#endif
3590 while ((i == 0 ? vim_iswordc(ptr[col])
3591 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || ((find_type & FIND_EVAL)
3594 && col <= (int)startcol
3595 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3596# endif
3597 )
3598 {
3599 ++col;
3600 }
3601
3602 return col;
3603}
3604
3605/*
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003606 * Add commands to reselect Visual mode into the redo buffer.
3607 */
3608 static void
3609prep_redo_visual(cap)
3610 cmdarg_T *cap;
3611{
3612 ResetRedobuff();
3613 AppendCharToRedobuff(VIsual_mode);
3614 if (VIsual_mode == 'V' && curbuf->b_visual.vi_end.lnum
3615 != curbuf->b_visual.vi_start.lnum)
3616 {
3617 AppendNumberToRedobuff(curbuf->b_visual.vi_end.lnum
3618 - curbuf->b_visual.vi_start.lnum);
3619 AppendCharToRedobuff('j');
3620 }
3621 else if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
3622 {
3623 /* block visual mode or char visual mmode*/
3624 if (curbuf->b_visual.vi_end.lnum != curbuf->b_visual.vi_start.lnum)
3625 {
3626 AppendNumberToRedobuff(curbuf->b_visual.vi_end.lnum -
3627 curbuf->b_visual.vi_start.lnum);
3628 AppendCharToRedobuff('j');
3629 }
3630 if (curbuf->b_visual.vi_curswant == MAXCOL)
3631 AppendCharToRedobuff('$');
3632 else if (curbuf->b_visual.vi_end.col > curbuf->b_visual.vi_start.col)
3633 {
3634 AppendNumberToRedobuff(curbuf->b_visual.vi_end.col
3635 - curbuf->b_visual.vi_start.col - 1);
3636 AppendCharToRedobuff(' ');
3637 }
3638 }
3639 AppendNumberToRedobuff(cap->count1);
3640}
3641
3642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Prepare for redo of a normal command.
3644 */
3645 static void
3646prep_redo_cmd(cap)
3647 cmdarg_T *cap;
3648{
3649 prep_redo(cap->oap->regname, cap->count0,
3650 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3651}
3652
3653/*
3654 * Prepare for redo of any command.
3655 * Note that only the last argument can be a multi-byte char.
3656 */
3657 static void
3658prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3659 int regname;
3660 long num;
3661 int cmd1;
3662 int cmd2;
3663 int cmd3;
3664 int cmd4;
3665 int cmd5;
3666{
3667 ResetRedobuff();
3668 if (regname != 0) /* yank from specified buffer */
3669 {
3670 AppendCharToRedobuff('"');
3671 AppendCharToRedobuff(regname);
3672 }
3673 if (num)
3674 AppendNumberToRedobuff(num);
3675
3676 if (cmd1 != NUL)
3677 AppendCharToRedobuff(cmd1);
3678 if (cmd2 != NUL)
3679 AppendCharToRedobuff(cmd2);
3680 if (cmd3 != NUL)
3681 AppendCharToRedobuff(cmd3);
3682 if (cmd4 != NUL)
3683 AppendCharToRedobuff(cmd4);
3684 if (cmd5 != NUL)
3685 AppendCharToRedobuff(cmd5);
3686}
3687
3688/*
3689 * check for operator active and clear it
3690 *
3691 * return TRUE if operator was active
3692 */
3693 static int
3694checkclearop(oap)
3695 oparg_T *oap;
3696{
3697 if (oap->op_type == OP_NOP)
3698 return FALSE;
3699 clearopbeep(oap);
3700 return TRUE;
3701}
3702
3703/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003704 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003706 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 */
3708 static int
3709checkclearopq(oap)
3710 oparg_T *oap;
3711{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003712 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return FALSE;
3714 clearopbeep(oap);
3715 return TRUE;
3716}
3717
3718 static void
3719clearop(oap)
3720 oparg_T *oap;
3721{
3722 oap->op_type = OP_NOP;
3723 oap->regname = 0;
3724 oap->motion_force = NUL;
3725 oap->use_reg_one = FALSE;
3726}
3727
3728 static void
3729clearopbeep(oap)
3730 oparg_T *oap;
3731{
3732 clearop(oap);
3733 beep_flush();
3734}
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736/*
3737 * Remove the shift modifier from a special key.
3738 */
3739 static void
3740unshift_special(cap)
3741 cmdarg_T *cap;
3742{
3743 switch (cap->cmdchar)
3744 {
3745 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3746 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3747 case K_S_UP: cap->cmdchar = K_UP; break;
3748 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3749 case K_S_HOME: cap->cmdchar = K_HOME; break;
3750 case K_S_END: cap->cmdchar = K_END; break;
3751 }
3752 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003755/*
3756 * If the mode is currently displayed clear the command line or update the
3757 * command displayed.
3758 */
3759 static void
3760may_clear_cmdline()
3761{
3762 if (mode_displayed)
3763 clear_cmdline = TRUE; /* unshow visual mode later */
3764#ifdef FEAT_CMDL_INFO
3765 else
3766 clear_showcmd();
3767#endif
3768}
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3771/*
3772 * Routines for displaying a partly typed command
3773 */
3774
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003775#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776static char_u showcmd_buf[SHOWCMD_BUFLEN];
3777static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3778static int showcmd_is_clear = TRUE;
3779static int showcmd_visual = FALSE;
3780
3781static void display_showcmd __ARGS((void));
3782
3783 void
3784clear_showcmd()
3785{
3786 if (!p_sc)
3787 return;
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 if (VIsual_active && !char_avail())
3790 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003791 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 long lines;
3793 colnr_T leftcol, rightcol;
3794 linenr_T top, bot;
3795
3796 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003797 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
3799 top = VIsual.lnum;
3800 bot = curwin->w_cursor.lnum;
3801 }
3802 else
3803 {
3804 top = curwin->w_cursor.lnum;
3805 bot = VIsual.lnum;
3806 }
3807# ifdef FEAT_FOLDING
3808 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003809 (void)hasFolding(top, &top, NULL);
3810 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811# endif
3812 lines = bot - top + 1;
3813
3814 if (VIsual_mode == Ctrl_V)
3815 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003816# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003817 char_u *saved_sbr = p_sbr;
3818
3819 /* Make 'sbr' empty for a moment to get the correct size. */
3820 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003823# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003824 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3827 (long)(rightcol - leftcol + 1));
3828 }
3829 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3830 sprintf((char *)showcmd_buf, "%ld", lines);
3831 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003832 {
3833 char_u *s, *e;
3834 int l;
3835 int bytes = 0;
3836 int chars = 0;
3837
3838 if (cursor_bot)
3839 {
3840 s = ml_get_pos(&VIsual);
3841 e = ml_get_cursor();
3842 }
3843 else
3844 {
3845 s = ml_get_cursor();
3846 e = ml_get_pos(&VIsual);
3847 }
3848 while ((*p_sel != 'e') ? s <= e : s < e)
3849 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003850# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003851 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003852# else
3853 l = (*s == NUL) ? 0 : 1;
3854# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003855 if (l == 0)
3856 {
3857 ++bytes;
3858 ++chars;
3859 break; /* end of line */
3860 }
3861 bytes += l;
3862 ++chars;
3863 s += l;
3864 }
3865 if (bytes == chars)
3866 sprintf((char *)showcmd_buf, "%d", chars);
3867 else
3868 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3871 showcmd_visual = TRUE;
3872 }
3873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 showcmd_buf[0] = NUL;
3876 showcmd_visual = FALSE;
3877
3878 /* Don't actually display something if there is nothing to clear. */
3879 if (showcmd_is_clear)
3880 return;
3881 }
3882
3883 display_showcmd();
3884}
3885
3886/*
3887 * Add 'c' to string of shown command chars.
3888 * Return TRUE if output has been written (and setcursor() has been called).
3889 */
3890 int
3891add_to_showcmd(c)
3892 int c;
3893{
3894 char_u *p;
3895 int old_len;
3896 int extra_len;
3897 int overflow;
3898#if defined(FEAT_MOUSE)
3899 int i;
3900 static int ignore[] =
3901 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003902# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3904 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 K_IGNORE,
3907 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3908 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3909 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003910 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003912 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 0
3914 };
3915#endif
3916
Bram Moolenaar09df3122006-01-23 22:23:09 +00003917 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 return FALSE;
3919
3920 if (showcmd_visual)
3921 {
3922 showcmd_buf[0] = NUL;
3923 showcmd_visual = FALSE;
3924 }
3925
3926#if defined(FEAT_MOUSE)
3927 /* Ignore keys that are scrollbar updates and mouse clicks */
3928 if (IS_SPECIAL(c))
3929 for (i = 0; ignore[i] != 0; ++i)
3930 if (ignore[i] == c)
3931 return FALSE;
3932#endif
3933
3934 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003935 if (*p == ' ')
3936 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 old_len = (int)STRLEN(showcmd_buf);
3938 extra_len = (int)STRLEN(p);
3939 overflow = old_len + extra_len - SHOWCMD_COLS;
3940 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003941 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3942 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 STRCAT(showcmd_buf, p);
3944
3945 if (char_avail())
3946 return FALSE;
3947
3948 display_showcmd();
3949
3950 return TRUE;
3951}
3952
3953 void
3954add_to_showcmd_c(c)
3955 int c;
3956{
3957 if (!add_to_showcmd(c))
3958 setcursor();
3959}
3960
3961/*
3962 * Delete 'len' characters from the end of the shown command.
3963 */
3964 static void
3965del_from_showcmd(len)
3966 int len;
3967{
3968 int old_len;
3969
3970 if (!p_sc)
3971 return;
3972
3973 old_len = (int)STRLEN(showcmd_buf);
3974 if (len > old_len)
3975 len = old_len;
3976 showcmd_buf[old_len - len] = NUL;
3977
3978 if (!char_avail())
3979 display_showcmd();
3980}
3981
3982/*
3983 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3984 * something and there is a partial mapping.
3985 */
3986 void
3987push_showcmd()
3988{
3989 if (p_sc)
3990 STRCPY(old_showcmd_buf, showcmd_buf);
3991}
3992
3993 void
3994pop_showcmd()
3995{
3996 if (!p_sc)
3997 return;
3998
3999 STRCPY(showcmd_buf, old_showcmd_buf);
4000
4001 display_showcmd();
4002}
4003
4004 static void
4005display_showcmd()
4006{
4007 int len;
4008
4009 cursor_off();
4010
4011 len = (int)STRLEN(showcmd_buf);
4012 if (len == 0)
4013 showcmd_is_clear = TRUE;
4014 else
4015 {
4016 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4017 showcmd_is_clear = FALSE;
4018 }
4019
4020 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004021 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4022 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 */
4024 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4025
4026 setcursor(); /* put cursor back where it belongs */
4027}
4028#endif
4029
4030#ifdef FEAT_SCROLLBIND
4031/*
4032 * When "check" is FALSE, prepare for commands that scroll the window.
4033 * When "check" is TRUE, take care of scroll-binding after the window has
4034 * scrolled. Called from normal_cmd() and edit().
4035 */
4036 void
4037do_check_scrollbind(check)
4038 int check;
4039{
4040 static win_T *old_curwin = NULL;
4041 static linenr_T old_topline = 0;
4042#ifdef FEAT_DIFF
4043 static int old_topfill = 0;
4044#endif
4045 static buf_T *old_buf = NULL;
4046 static colnr_T old_leftcol = 0;
4047
4048 if (check && curwin->w_p_scb)
4049 {
4050 /* If a ":syncbind" command was just used, don't scroll, only reset
4051 * the values. */
4052 if (did_syncbind)
4053 did_syncbind = FALSE;
4054 else if (curwin == old_curwin)
4055 {
4056 /*
4057 * Synchronize other windows, as necessary according to
4058 * 'scrollbind'. Don't do this after an ":edit" command, except
4059 * when 'diff' is set.
4060 */
4061 if ((curwin->w_buffer == old_buf
4062#ifdef FEAT_DIFF
4063 || curwin->w_p_diff
4064#endif
4065 )
4066 && (curwin->w_topline != old_topline
4067#ifdef FEAT_DIFF
4068 || curwin->w_topfill != old_topfill
4069#endif
4070 || curwin->w_leftcol != old_leftcol))
4071 {
4072 check_scrollbind(curwin->w_topline - old_topline,
4073 (long)(curwin->w_leftcol - old_leftcol));
4074 }
4075 }
4076 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4077 {
4078 /*
4079 * When switching between windows, make sure that the relative
4080 * vertical offset is valid for the new window. The relative
4081 * offset is invalid whenever another 'scrollbind' window has
4082 * scrolled to a point that would force the current window to
4083 * scroll past the beginning or end of its buffer. When the
4084 * resync is performed, some of the other 'scrollbind' windows may
4085 * need to jump so that the current window's relative position is
4086 * visible on-screen.
4087 */
4088 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4089 }
4090 curwin->w_scbind_pos = curwin->w_topline;
4091 }
4092
4093 old_curwin = curwin;
4094 old_topline = curwin->w_topline;
4095#ifdef FEAT_DIFF
4096 old_topfill = curwin->w_topfill;
4097#endif
4098 old_buf = curwin->w_buffer;
4099 old_leftcol = curwin->w_leftcol;
4100}
4101
4102/*
4103 * Synchronize any windows that have "scrollbind" set, based on the
4104 * number of rows by which the current window has changed
4105 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4106 */
4107 void
4108check_scrollbind(topline_diff, leftcol_diff)
4109 linenr_T topline_diff;
4110 long leftcol_diff;
4111{
4112 int want_ver;
4113 int want_hor;
4114 win_T *old_curwin = curwin;
4115 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 int old_VIsual_select = VIsual_select;
4117 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 colnr_T tgt_leftcol = curwin->w_leftcol;
4119 long topline;
4120 long y;
4121
4122 /*
4123 * check 'scrollopt' string for vertical and horizontal scroll options
4124 */
4125 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4126#ifdef FEAT_DIFF
4127 want_ver |= old_curwin->w_p_diff;
4128#endif
4129 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4130
4131 /*
4132 * loop through the scrollbound windows and scroll accordingly
4133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4136 {
4137 curbuf = curwin->w_buffer;
4138 /* skip original window and windows with 'noscrollbind' */
4139 if (curwin != old_curwin && curwin->w_p_scb)
4140 {
4141 /*
4142 * do the vertical scroll
4143 */
4144 if (want_ver)
4145 {
4146#ifdef FEAT_DIFF
4147 if (old_curwin->w_p_diff && curwin->w_p_diff)
4148 {
4149 diff_set_topline(old_curwin, curwin);
4150 }
4151 else
4152#endif
4153 {
4154 curwin->w_scbind_pos += topline_diff;
4155 topline = curwin->w_scbind_pos;
4156 if (topline > curbuf->b_ml.ml_line_count)
4157 topline = curbuf->b_ml.ml_line_count;
4158 if (topline < 1)
4159 topline = 1;
4160
4161 y = topline - curwin->w_topline;
4162 if (y > 0)
4163 scrollup(y, FALSE);
4164 else
4165 scrolldown(-y, FALSE);
4166 }
4167
4168 redraw_later(VALID);
4169 cursor_correct();
4170#ifdef FEAT_WINDOWS
4171 curwin->w_redr_status = TRUE;
4172#endif
4173 }
4174
4175 /*
4176 * do the horizontal scroll
4177 */
4178 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4179 {
4180 curwin->w_leftcol = tgt_leftcol;
4181 leftcol_changed();
4182 }
4183 }
4184 }
4185
4186 /*
4187 * reset current-window
4188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 VIsual_select = old_VIsual_select;
4190 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 curwin = old_curwin;
4192 curbuf = old_curbuf;
4193}
4194#endif /* #ifdef FEAT_SCROLLBIND */
4195
4196/*
4197 * Command character that's ignored.
4198 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004199 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 static void
4202nv_ignore(cap)
4203 cmdarg_T *cap;
4204{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004205 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206}
4207
4208/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004209 * Command character that doesn't do anything, but unlike nv_ignore() does
4210 * start edit(). Used for "startinsert" executed while starting up.
4211 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004212 static void
4213nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004214 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004215{
4216}
4217
4218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * Command character doesn't exist.
4220 */
4221 static void
4222nv_error(cap)
4223 cmdarg_T *cap;
4224{
4225 clearopbeep(cap->oap);
4226}
4227
4228/*
4229 * <Help> and <F1> commands.
4230 */
4231 static void
4232nv_help(cap)
4233 cmdarg_T *cap;
4234{
4235 if (!checkclearopq(cap->oap))
4236 ex_help(NULL);
4237}
4238
4239/*
4240 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4241 */
4242 static void
4243nv_addsub(cap)
4244 cmdarg_T *cap;
4245{
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004246 int visual = VIsual_active;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02004247
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004248 if (cap->oap->op_type == OP_NOP
4249 && do_addsub((int)cap->cmdchar, cap->count1, cap->arg) == OK)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004250 {
4251 if (visual)
4252 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004253 prep_redo_visual(cap);
4254 if (cap->arg)
4255 AppendCharToRedobuff('g');
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004256 AppendCharToRedobuff(cap->cmdchar);
4257 }
4258 else
4259 prep_redo_cmd(cap);
4260 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004261 else
4262 clearopbeep(cap->oap);
4263 if (visual)
4264 {
4265 VIsual_active = FALSE;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004266 redo_VIsual_busy = FALSE;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02004267 may_clear_cmdline();
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004268 redraw_later(INVERTED);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270}
4271
4272/*
4273 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4274 */
4275 static void
4276nv_page(cap)
4277 cmdarg_T *cap;
4278{
4279 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004280 {
4281#ifdef FEAT_WINDOWS
4282 if (mod_mask & MOD_MASK_CTRL)
4283 {
4284 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4285 if (cap->arg == BACKWARD)
4286 goto_tabpage(-(int)cap->count1);
4287 else
4288 goto_tabpage((int)cap->count0);
4289 }
4290 else
4291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294}
4295
4296/*
4297 * Implementation of "gd" and "gD" command.
4298 */
4299 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004300nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 oparg_T *oap;
4302 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004303 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
4305 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u *ptr;
4307
Bram Moolenaard9d30582005-05-18 22:10:28 +00004308 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004309 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004311#ifdef FEAT_FOLDING
4312 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4313 foldOpenCursor();
4314#endif
4315}
4316
4317/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004318 * Search for variable declaration of "ptr[len]".
4319 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4320 * current file ("gD").
4321 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004322 * Return FAIL when not found.
4323 */
4324 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004325find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326 char_u *ptr;
4327 int len;
4328 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004329 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004330 int searchflags; /* flags passed to searchit() */
4331{
4332 char_u *pat;
4333 pos_T old_pos;
4334 pos_T par_pos;
4335 pos_T found_pos;
4336 int t;
4337 int save_p_ws;
4338 int save_p_scs;
4339 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004340 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004341
4342 if ((pat = alloc(len + 7)) == NULL)
4343 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004344
4345 /* Put "\V" before the pattern to avoid that the special meaning of "."
4346 * and "~" causes trouble. */
4347 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4348 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 old_pos = curwin->w_cursor;
4350 save_p_ws = p_ws;
4351 save_p_scs = p_scs;
4352 p_ws = FALSE; /* don't wrap around end of file now */
4353 p_scs = FALSE; /* don't switch ignorecase off now */
4354
4355 /*
4356 * With "gD" go to line 1.
4357 * With "gd" Search back for the start of the current function, then go
4358 * back until a blank line. If this fails go to line 1.
4359 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004360 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 {
4362 setpcmark(); /* Set in findpar() otherwise */
4363 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004364 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004368 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4370 --curwin->w_cursor.lnum;
4371 }
4372 curwin->w_cursor.col = 0;
4373
4374 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004375 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 for (;;)
4377 {
4378 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004379 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004380 if (curwin->w_cursor.lnum >= old_pos.lnum)
4381 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004382
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004383 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004384 {
4385 pos_T *pos;
4386
4387 /* Check that the block the match is in doesn't end before the
4388 * position where we started the search from. */
4389 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4390 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4391 && pos->lnum < old_pos.lnum)
4392 continue;
4393 }
4394
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004395 if (t == FAIL)
4396 {
4397 /* If we previously found a valid position, use it. */
4398 if (found_pos.lnum != 0)
4399 {
4400 curwin->w_cursor = found_pos;
4401 t = OK;
4402 }
4403 break;
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004406 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 {
4408 /* Ignore this line, continue at start of next line. */
4409 ++curwin->w_cursor.lnum;
4410 curwin->w_cursor.col = 0;
4411 continue;
4412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004414 if (!locally) /* global search: use first match found */
4415 break;
4416 if (curwin->w_cursor.lnum >= par_pos.lnum)
4417 {
4418 /* If we previously found a valid position, use it. */
4419 if (found_pos.lnum != 0)
4420 curwin->w_cursor = found_pos;
4421 break;
4422 }
4423
4424 /* For finding a local variable and the match is before the "{" search
4425 * to find a later match. For K&R style function declarations this
4426 * skips the function header without types. */
4427 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004429
4430 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004432 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 curwin->w_cursor = old_pos;
4434 }
4435 else
4436 {
4437 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /* "n" searches forward now */
4439 reset_search_dir();
4440 }
4441
4442 vim_free(pat);
4443 p_ws = save_p_ws;
4444 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004445
4446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449/*
4450 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4451 * lines rather than lines in the file.
4452 * 'dist' must be positive.
4453 *
4454 * Return OK if able to move cursor, FAIL otherwise.
4455 */
4456 static int
4457nv_screengo(oap, dir, dist)
4458 oparg_T *oap;
4459 int dir;
4460 long dist;
4461{
4462 int linelen = linetabsize(ml_get_curline());
4463 int retval = OK;
4464 int atend = FALSE;
4465 int n;
4466 int col_off1; /* margin offset for first screen line */
4467 int col_off2; /* margin offset for wrapped screen line */
4468 int width1; /* text width for first screen line */
4469 int width2; /* test width for wrapped screen line */
4470
4471 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004472 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 col_off1 = curwin_col_off();
4475 col_off2 = col_off1 - curwin_col_off2();
4476 width1 = W_WIDTH(curwin) - col_off1;
4477 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004478 if (width2 == 0)
4479 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481#ifdef FEAT_VERTSPLIT
4482 if (curwin->w_width != 0)
4483 {
4484#endif
4485 /*
4486 * Instead of sticking at the last character of the buffer line we
4487 * try to stick in the last column of the screen.
4488 */
4489 if (curwin->w_curswant == MAXCOL)
4490 {
4491 atend = TRUE;
4492 validate_virtcol();
4493 if (width1 <= 0)
4494 curwin->w_curswant = 0;
4495 else
4496 {
4497 curwin->w_curswant = width1 - 1;
4498 if (curwin->w_virtcol > curwin->w_curswant)
4499 curwin->w_curswant += ((curwin->w_virtcol
4500 - curwin->w_curswant - 1) / width2 + 1) * width2;
4501 }
4502 }
4503 else
4504 {
4505 if (linelen > width1)
4506 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4507 else
4508 n = width1;
4509 if (curwin->w_curswant > (colnr_T)n + 1)
4510 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4511 * width2;
4512 }
4513
4514 while (dist--)
4515 {
4516 if (dir == BACKWARD)
4517 {
4518 if ((long)curwin->w_curswant >= width2)
4519 /* move back within line */
4520 curwin->w_curswant -= width2;
4521 else
4522 {
4523 /* to previous line */
4524 if (curwin->w_cursor.lnum == 1)
4525 {
4526 retval = FAIL;
4527 break;
4528 }
4529 --curwin->w_cursor.lnum;
4530#ifdef FEAT_FOLDING
4531 /* Move to the start of a closed fold. Don't do that when
4532 * 'foldopen' contains "all": it will open in a moment. */
4533 if (!(fdo_flags & FDO_ALL))
4534 (void)hasFolding(curwin->w_cursor.lnum,
4535 &curwin->w_cursor.lnum, NULL);
4536#endif
4537 linelen = linetabsize(ml_get_curline());
4538 if (linelen > width1)
4539 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4540 + 1) * width2;
4541 }
4542 }
4543 else /* dir == FORWARD */
4544 {
4545 if (linelen > width1)
4546 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4547 else
4548 n = width1;
4549 if (curwin->w_curswant + width2 < (colnr_T)n)
4550 /* move forward within line */
4551 curwin->w_curswant += width2;
4552 else
4553 {
4554 /* to next line */
4555#ifdef FEAT_FOLDING
4556 /* Move to the end of a closed fold. */
4557 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4558 &curwin->w_cursor.lnum);
4559#endif
4560 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4561 {
4562 retval = FAIL;
4563 break;
4564 }
4565 curwin->w_cursor.lnum++;
4566 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004567 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569 }
4570 }
4571#ifdef FEAT_VERTSPLIT
4572 }
4573#endif
4574
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004575 if (virtual_active() && atend)
4576 coladvance(MAXCOL);
4577 else
4578 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
4580#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4581 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4582 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004583 colnr_T virtcol;
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 /*
4586 * Check for landing on a character that got split at the end of the
4587 * last line. We want to advance a screenline, not end up in the same
4588 * screenline or move two screenlines.
4589 */
4590 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004591 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004592# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004593 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4594 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004595# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004596
4597 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 && (curwin->w_curswant < (colnr_T)width1
4599 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4600 : ((curwin->w_curswant - width1) % width2
4601 > (colnr_T)width2 / 2)))
4602 --curwin->w_cursor.col;
4603 }
4604#endif
4605
4606 if (atend)
4607 curwin->w_curswant = MAXCOL; /* stick in the last column */
4608
4609 return retval;
4610}
4611
4612#ifdef FEAT_MOUSE
4613/*
4614 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4615 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004616 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4617 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 */
4619 static void
4620nv_mousescroll(cap)
4621 cmdarg_T *cap;
4622{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004623# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004624 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004626 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 int row, col;
4629
4630 row = mouse_row;
4631 col = mouse_col;
4632
4633 /* find the window at the pointer coordinates */
4634 curwin = mouse_find_win(&row, &col);
4635 curbuf = curwin->w_buffer;
4636 }
4637# endif
4638
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004639 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004641 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4642 {
4643 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4644 }
4645 else
4646 {
4647 cap->count1 = 3;
4648 cap->count0 = 3;
4649 nv_scroll_line(cap);
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
4654 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004655 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4656 if (!curwin->w_p_wrap)
4657 {
4658 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004659
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4661 step = W_WIDTH(curwin);
4662 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4663 if (val < 0)
4664 val = 0;
4665
4666 gui_do_horiz_scroll(val, TRUE);
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004671# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 curwin->w_redr_status = TRUE;
4673
4674 curwin = old_curwin;
4675 curbuf = curwin->w_buffer;
4676# endif
4677}
4678
4679/*
4680 * Mouse clicks and drags.
4681 */
4682 static void
4683nv_mouse(cap)
4684 cmdarg_T *cap;
4685{
4686 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4687}
4688#endif
4689
4690/*
4691 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4692 * cap->arg must be TRUE for CTRL-E.
4693 */
4694 static void
4695nv_scroll_line(cap)
4696 cmdarg_T *cap;
4697{
4698 if (!checkclearop(cap->oap))
4699 scroll_redraw(cap->arg, cap->count1);
4700}
4701
4702/*
4703 * Scroll "count" lines up or down, and redraw.
4704 */
4705 void
4706scroll_redraw(up, count)
4707 int up;
4708 long count;
4709{
4710 linenr_T prev_topline = curwin->w_topline;
4711#ifdef FEAT_DIFF
4712 int prev_topfill = curwin->w_topfill;
4713#endif
4714 linenr_T prev_lnum = curwin->w_cursor.lnum;
4715
4716 if (up)
4717 scrollup(count, TRUE);
4718 else
4719 scrolldown(count, TRUE);
4720 if (p_so)
4721 {
4722 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4723 * valid, otherwise the screen jumps back at the end of the file. */
4724 cursor_correct();
4725 check_cursor_moved(curwin);
4726 curwin->w_valid |= VALID_TOPLINE;
4727
4728 /* If moved back to where we were, at least move the cursor, otherwise
4729 * we get stuck at one position. Don't move the cursor up if the
4730 * first line of the buffer is already on the screen */
4731 while (curwin->w_topline == prev_topline
4732#ifdef FEAT_DIFF
4733 && curwin->w_topfill == prev_topfill
4734#endif
4735 )
4736 {
4737 if (up)
4738 {
4739 if (curwin->w_cursor.lnum > prev_lnum
4740 || cursor_down(1L, FALSE) == FAIL)
4741 break;
4742 }
4743 else
4744 {
4745 if (curwin->w_cursor.lnum < prev_lnum
4746 || prev_topline == 1L
4747 || cursor_up(1L, FALSE) == FAIL)
4748 break;
4749 }
4750 /* Mark w_topline as valid, otherwise the screen jumps back at the
4751 * end of the file. */
4752 check_cursor_moved(curwin);
4753 curwin->w_valid |= VALID_TOPLINE;
4754 }
4755 }
4756 if (curwin->w_cursor.lnum != prev_lnum)
4757 coladvance(curwin->w_curswant);
4758 redraw_later(VALID);
4759}
4760
4761/*
4762 * Commands that start with "z".
4763 */
4764 static void
4765nv_zet(cap)
4766 cmdarg_T *cap;
4767{
4768 long n;
4769 colnr_T col;
4770 int nchar = cap->nchar;
4771#ifdef FEAT_FOLDING
4772 long old_fdl = curwin->w_p_fdl;
4773 int old_fen = curwin->w_p_fen;
4774#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004775#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004776 int undo = FALSE;
4777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 if (VIM_ISDIGIT(nchar))
4780 {
4781 /*
4782 * "z123{nchar}": edit the count before obtaining {nchar}
4783 */
4784 if (checkclearop(cap->oap))
4785 return;
4786 n = nchar - '0';
4787 for (;;)
4788 {
4789#ifdef USE_ON_FLY_SCROLL
4790 dont_scroll = TRUE; /* disallow scrolling here */
4791#endif
4792 ++no_mapping;
4793 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004794 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 --no_mapping;
4797 --allow_keys;
4798#ifdef FEAT_CMDL_INFO
4799 (void)add_to_showcmd(nchar);
4800#endif
4801 if (nchar == K_DEL || nchar == K_KDEL)
4802 n /= 10;
4803 else if (VIM_ISDIGIT(nchar))
4804 n = n * 10 + (nchar - '0');
4805 else if (nchar == CAR)
4806 {
4807#ifdef FEAT_GUI
4808 need_mouse_correct = TRUE;
4809#endif
4810 win_setheight((int)n);
4811 break;
4812 }
4813 else if (nchar == 'l'
4814 || nchar == 'h'
4815 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004816 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 cap->count1 = n ? n * cap->count1 : cap->count1;
4819 goto dozet;
4820 }
4821 else
4822 {
4823 clearopbeep(cap->oap);
4824 break;
4825 }
4826 }
4827 cap->oap->op_type = OP_NOP;
4828 return;
4829 }
4830
4831dozet:
4832 if (
4833#ifdef FEAT_FOLDING
4834 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4835 * and "zC" only in Visual mode. "zj" and "zk" are motion
4836 * commands. */
4837 cap->nchar != 'f' && cap->nchar != 'F'
4838 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4839 && cap->nchar != 'j' && cap->nchar != 'k'
4840 &&
4841#endif
4842 checkclearop(cap->oap))
4843 return;
4844
4845 /*
4846 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4847 * If line number given, set cursor.
4848 */
4849 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4850 && cap->count0
4851 && cap->count0 != curwin->w_cursor.lnum)
4852 {
4853 setpcmark();
4854 if (cap->count0 > curbuf->b_ml.ml_line_count)
4855 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4856 else
4857 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004858 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860
4861 switch (nchar)
4862 {
4863 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4864 case '+':
4865 if (cap->count0 == 0)
4866 {
4867 /* No count given: put cursor at the line below screen */
4868 validate_botline(); /* make sure w_botline is valid */
4869 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4870 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4871 else
4872 curwin->w_cursor.lnum = curwin->w_botline;
4873 }
4874 /* FALLTHROUGH */
4875 case NL:
4876 case CAR:
4877 case K_KENTER:
4878 beginline(BL_WHITE | BL_FIX);
4879 /* FALLTHROUGH */
4880
4881 case 't': scroll_cursor_top(0, TRUE);
4882 redraw_later(VALID);
4883 break;
4884
4885 /* "z." and "zz": put cursor in middle of screen */
4886 case '.': beginline(BL_WHITE | BL_FIX);
4887 /* FALLTHROUGH */
4888
4889 case 'z': scroll_cursor_halfway(TRUE);
4890 redraw_later(VALID);
4891 break;
4892
4893 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4894 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4895 * when <count> is at bottom of window, and puts that one at
4896 * bottom of window. */
4897 if (cap->count0 != 0)
4898 {
4899 scroll_cursor_bot(0, TRUE);
4900 curwin->w_cursor.lnum = curwin->w_topline;
4901 }
4902 else if (curwin->w_topline == 1)
4903 curwin->w_cursor.lnum = 1;
4904 else
4905 curwin->w_cursor.lnum = curwin->w_topline - 1;
4906 /* FALLTHROUGH */
4907 case '-':
4908 beginline(BL_WHITE | BL_FIX);
4909 /* FALLTHROUGH */
4910
4911 case 'b': scroll_cursor_bot(0, TRUE);
4912 redraw_later(VALID);
4913 break;
4914
4915 /* "zH" - scroll screen right half-page */
4916 case 'H':
4917 cap->count1 *= W_WIDTH(curwin) / 2;
4918 /* FALLTHROUGH */
4919
4920 /* "zh" - scroll screen to the right */
4921 case 'h':
4922 case K_LEFT:
4923 if (!curwin->w_p_wrap)
4924 {
4925 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4926 curwin->w_leftcol = 0;
4927 else
4928 curwin->w_leftcol -= (colnr_T)cap->count1;
4929 leftcol_changed();
4930 }
4931 break;
4932
4933 /* "zL" - scroll screen left half-page */
4934 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4935 /* FALLTHROUGH */
4936
4937 /* "zl" - scroll screen to the left */
4938 case 'l':
4939 case K_RIGHT:
4940 if (!curwin->w_p_wrap)
4941 {
4942 /* scroll the window left */
4943 curwin->w_leftcol += (colnr_T)cap->count1;
4944 leftcol_changed();
4945 }
4946 break;
4947
4948 /* "zs" - scroll screen, cursor at the start */
4949 case 's': if (!curwin->w_p_wrap)
4950 {
4951#ifdef FEAT_FOLDING
4952 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4953 col = 0; /* like the cursor is in col 0 */
4954 else
4955#endif
4956 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4957 if ((long)col > p_siso)
4958 col -= p_siso;
4959 else
4960 col = 0;
4961 if (curwin->w_leftcol != col)
4962 {
4963 curwin->w_leftcol = col;
4964 redraw_later(NOT_VALID);
4965 }
4966 }
4967 break;
4968
4969 /* "ze" - scroll screen, cursor at the end */
4970 case 'e': if (!curwin->w_p_wrap)
4971 {
4972#ifdef FEAT_FOLDING
4973 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4974 col = 0; /* like the cursor is in col 0 */
4975 else
4976#endif
4977 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4978 n = W_WIDTH(curwin) - curwin_col_off();
4979 if ((long)col + p_siso < n)
4980 col = 0;
4981 else
4982 col = col + p_siso - n + 1;
4983 if (curwin->w_leftcol != col)
4984 {
4985 curwin->w_leftcol = col;
4986 redraw_later(NOT_VALID);
4987 }
4988 }
4989 break;
4990
4991#ifdef FEAT_FOLDING
4992 /* "zF": create fold command */
4993 /* "zf": create fold operator */
4994 case 'F':
4995 case 'f': if (foldManualAllowed(TRUE))
4996 {
4997 cap->nchar = 'f';
4998 nv_operator(cap);
4999 curwin->w_p_fen = TRUE;
5000
5001 /* "zF" is like "zfzf" */
5002 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5003 {
5004 nv_operator(cap);
5005 finish_op = TRUE;
5006 }
5007 }
5008 else
5009 clearopbeep(cap->oap);
5010 break;
5011
5012 /* "zd": delete fold at cursor */
5013 /* "zD": delete fold at cursor recursively */
5014 case 'd':
5015 case 'D': if (foldManualAllowed(FALSE))
5016 {
5017 if (VIsual_active)
5018 nv_operator(cap);
5019 else
5020 deleteFold(curwin->w_cursor.lnum,
5021 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5022 }
5023 break;
5024
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005025 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 case 'E': if (foldmethodIsManual(curwin))
5027 {
5028 clearFolding(curwin);
5029 changed_window_setting();
5030 }
5031 else if (foldmethodIsMarker(curwin))
5032 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5033 TRUE, FALSE);
5034 else
5035 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5036 break;
5037
5038 /* "zn": fold none: reset 'foldenable' */
5039 case 'n': curwin->w_p_fen = FALSE;
5040 break;
5041
5042 /* "zN": fold Normal: set 'foldenable' */
5043 case 'N': curwin->w_p_fen = TRUE;
5044 break;
5045
5046 /* "zi": invert folding: toggle 'foldenable' */
5047 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5048 break;
5049
5050 /* "za": open closed fold or close open fold at cursor */
5051 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5052 openFold(curwin->w_cursor.lnum, cap->count1);
5053 else
5054 {
5055 closeFold(curwin->w_cursor.lnum, cap->count1);
5056 curwin->w_p_fen = TRUE;
5057 }
5058 break;
5059
5060 /* "zA": open fold at cursor recursively */
5061 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5062 openFoldRecurse(curwin->w_cursor.lnum);
5063 else
5064 {
5065 closeFoldRecurse(curwin->w_cursor.lnum);
5066 curwin->w_p_fen = TRUE;
5067 }
5068 break;
5069
5070 /* "zo": open fold at cursor or Visual area */
5071 case 'o': if (VIsual_active)
5072 nv_operator(cap);
5073 else
5074 openFold(curwin->w_cursor.lnum, cap->count1);
5075 break;
5076
5077 /* "zO": open fold recursively */
5078 case 'O': if (VIsual_active)
5079 nv_operator(cap);
5080 else
5081 openFoldRecurse(curwin->w_cursor.lnum);
5082 break;
5083
5084 /* "zc": close fold at cursor or Visual area */
5085 case 'c': if (VIsual_active)
5086 nv_operator(cap);
5087 else
5088 closeFold(curwin->w_cursor.lnum, cap->count1);
5089 curwin->w_p_fen = TRUE;
5090 break;
5091
5092 /* "zC": close fold recursively */
5093 case 'C': if (VIsual_active)
5094 nv_operator(cap);
5095 else
5096 closeFoldRecurse(curwin->w_cursor.lnum);
5097 curwin->w_p_fen = TRUE;
5098 break;
5099
5100 /* "zv": open folds at the cursor */
5101 case 'v': foldOpenCursor();
5102 break;
5103
5104 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5105 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005106 curwin->w_foldinvalid = TRUE; /* recompute folds */
5107 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 foldOpenCursor();
5109 break;
5110
5111 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5112 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005113 curwin->w_foldinvalid = TRUE; /* recompute folds */
5114 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 break;
5116
5117 /* "zm": fold more */
5118 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005119 {
5120 curwin->w_p_fdl -= cap->count1;
5121 if (curwin->w_p_fdl < 0)
5122 curwin->w_p_fdl = 0;
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 old_fdl = -1; /* force an update */
5125 curwin->w_p_fen = TRUE;
5126 break;
5127
5128 /* "zM": close all folds */
5129 case 'M': curwin->w_p_fdl = 0;
5130 old_fdl = -1; /* force an update */
5131 curwin->w_p_fen = TRUE;
5132 break;
5133
5134 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005135 case 'r': curwin->w_p_fdl += cap->count1;
5136 {
5137 int d = getDeepestNesting();
5138
5139 if (curwin->w_p_fdl >= d)
5140 curwin->w_p_fdl = d;
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 break;
5143
5144 /* "zR": open all folds */
5145 case 'R': curwin->w_p_fdl = getDeepestNesting();
5146 old_fdl = -1; /* force an update */
5147 break;
5148
5149 case 'j': /* "zj" move to next fold downwards */
5150 case 'k': /* "zk" move to next fold upwards */
5151 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5152 cap->count1) == FAIL)
5153 clearopbeep(cap->oap);
5154 break;
5155
5156#endif /* FEAT_FOLDING */
5157
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005158#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005159 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5160 ++no_mapping;
5161 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005162 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005163 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005164 --no_mapping;
5165 --allow_keys;
5166#ifdef FEAT_CMDL_INFO
5167 (void)add_to_showcmd(nchar);
5168#endif
5169 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5170 {
5171 clearopbeep(cap->oap);
5172 break;
5173 }
5174 undo = TRUE;
5175 /*FALLTHROUGH*/
5176
Bram Moolenaarb765d632005-06-07 21:00:02 +00005177 case 'g': /* "zg": add good word to word list */
5178 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005179 case 'G': /* "zG": add good word to temp word list */
5180 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005181 {
5182 char_u *ptr = NULL;
5183 int len;
5184
5185 if (checkclearop(cap->oap))
5186 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005187 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5188 == FAIL)
5189 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005190 if (ptr == NULL)
5191 {
5192 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005193
Bram Moolenaar134bf072013-09-25 18:54:24 +02005194 /* Find bad word under the cursor. When 'spell' is
5195 * off this fails and find_ident_under_cursor() is
5196 * used below. */
5197 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005198 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005199 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005200 if (len != 0 && curwin->w_cursor.col <= pos.col)
5201 ptr = ml_get_pos(&curwin->w_cursor);
5202 curwin->w_cursor = pos;
5203 }
5204
Bram Moolenaarb765d632005-06-07 21:00:02 +00005205 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5206 FIND_IDENT)) == 0)
5207 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005208 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005209 (nchar == 'G' || nchar == 'W')
5210 ? 0 : (int)cap->count1,
5211 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005213 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214
Bram Moolenaar43abc522005-12-10 20:15:02 +00005215 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005216 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005217 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005219#endif
5220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 default: clearopbeep(cap->oap);
5222 }
5223
5224#ifdef FEAT_FOLDING
5225 /* Redraw when 'foldenable' changed */
5226 if (old_fen != curwin->w_p_fen)
5227 {
5228# ifdef FEAT_DIFF
5229 win_T *wp;
5230
5231 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5232 {
5233 /* Adjust 'foldenable' in diff-synced windows. */
5234 FOR_ALL_WINDOWS(wp)
5235 {
5236 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5237 {
5238 wp->w_p_fen = curwin->w_p_fen;
5239 changed_window_setting_win(wp);
5240 }
5241 }
5242 }
5243# endif
5244 changed_window_setting();
5245 }
5246
5247 /* Redraw when 'foldlevel' changed. */
5248 if (old_fdl != curwin->w_p_fdl)
5249 newFoldLevel();
5250#endif
5251}
5252
5253#ifdef FEAT_GUI
5254/*
5255 * Vertical scrollbar movement.
5256 */
5257 static void
5258nv_ver_scrollbar(cap)
5259 cmdarg_T *cap;
5260{
5261 if (cap->oap->op_type != OP_NOP)
5262 clearopbeep(cap->oap);
5263
5264 /* Even if an operator was pending, we still want to scroll */
5265 gui_do_scroll();
5266}
5267
5268/*
5269 * Horizontal scrollbar movement.
5270 */
5271 static void
5272nv_hor_scrollbar(cap)
5273 cmdarg_T *cap;
5274{
5275 if (cap->oap->op_type != OP_NOP)
5276 clearopbeep(cap->oap);
5277
5278 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005279 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280}
5281#endif
5282
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005283#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005284/*
5285 * Click in GUI tab.
5286 */
5287 static void
5288nv_tabline(cap)
5289 cmdarg_T *cap;
5290{
5291 if (cap->oap->op_type != OP_NOP)
5292 clearopbeep(cap->oap);
5293
5294 /* Even if an operator was pending, we still want to jump tabs. */
5295 goto_tabpage(current_tab);
5296}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005297
5298/*
5299 * Selected item in tab line menu.
5300 */
5301 static void
5302nv_tabmenu(cap)
5303 cmdarg_T *cap;
5304{
5305 if (cap->oap->op_type != OP_NOP)
5306 clearopbeep(cap->oap);
5307
5308 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005309 handle_tabmenu();
5310}
5311
5312/*
5313 * Handle selecting an item of the GUI tab line menu.
5314 * Used in Normal and Insert mode.
5315 */
5316 void
5317handle_tabmenu()
5318{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005319 switch (current_tabmenu)
5320 {
5321 case TABLINE_MENU_CLOSE:
5322 if (current_tab == 0)
5323 do_cmdline_cmd((char_u *)"tabclose");
5324 else
5325 {
5326 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5327 current_tab);
5328 do_cmdline_cmd(IObuff);
5329 }
5330 break;
5331
5332 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005333 if (current_tab == 0)
5334 do_cmdline_cmd((char_u *)"$tabnew");
5335 else
5336 {
5337 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5338 current_tab - 1);
5339 do_cmdline_cmd(IObuff);
5340 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005341 break;
5342
5343 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005344 if (current_tab == 0)
5345 do_cmdline_cmd((char_u *)"browse $tabnew");
5346 else
5347 {
5348 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5349 current_tab - 1);
5350 do_cmdline_cmd(IObuff);
5351 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005352 break;
5353 }
5354}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005355#endif
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357/*
5358 * "Q" command.
5359 */
5360 static void
5361nv_exmode(cap)
5362 cmdarg_T *cap;
5363{
5364 /*
5365 * Ignore 'Q' in Visual mode, just give a beep.
5366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005368 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005369 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 do_exmode(FALSE);
5371}
5372
5373/*
5374 * Handle a ":" command.
5375 */
5376 static void
5377nv_colon(cap)
5378 cmdarg_T *cap;
5379{
5380 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005381 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (VIsual_active)
5384 nv_operator(cap);
5385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 if (cap->oap->op_type != OP_NOP)
5388 {
5389 /* Using ":" as a movement is characterwise exclusive. */
5390 cap->oap->motion_type = MCHAR;
5391 cap->oap->inclusive = FALSE;
5392 }
5393 else if (cap->count0)
5394 {
5395 /* translate "count:" into ":.,.+(count - 1)" */
5396 stuffcharReadbuff('.');
5397 if (cap->count0 > 1)
5398 {
5399 stuffReadbuff((char_u *)",.+");
5400 stuffnumReadbuff((long)cap->count0 - 1L);
5401 }
5402 }
5403
5404 /* When typing, don't type below an old message */
5405 if (KeyTyped)
5406 compute_cmdrow();
5407
5408 old_p_im = p_im;
5409
5410 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005411 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5413
5414 /* If 'insertmode' changed, enter or exit Insert mode */
5415 if (p_im != old_p_im)
5416 {
5417 if (p_im)
5418 restart_edit = 'i';
5419 else
5420 restart_edit = 0;
5421 }
5422
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005423 if (cmd_result == FAIL)
5424 /* The Ex command failed, do not execute the operator. */
5425 clearop(cap->oap);
5426 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5428 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005429 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5430 || did_emsg
5431 ))
5432 /* The start of the operator has become invalid by the Ex command.
5433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 clearopbeep(cap->oap);
5435 }
5436}
5437
5438/*
5439 * Handle CTRL-G command.
5440 */
5441 static void
5442nv_ctrlg(cap)
5443 cmdarg_T *cap;
5444{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (VIsual_active) /* toggle Selection/Visual mode */
5446 {
5447 VIsual_select = !VIsual_select;
5448 showmode();
5449 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005450 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 /* print full name if count given or :cd used */
5452 fileinfo((int)cap->count0, FALSE, TRUE);
5453}
5454
5455/*
5456 * Handle CTRL-H <Backspace> command.
5457 */
5458 static void
5459nv_ctrlh(cap)
5460 cmdarg_T *cap;
5461{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 if (VIsual_active && VIsual_select)
5463 {
5464 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5465 v_visop(cap);
5466 }
5467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 nv_left(cap);
5469}
5470
5471/*
5472 * CTRL-L: clear screen and redraw.
5473 */
5474 static void
5475nv_clear(cap)
5476 cmdarg_T *cap;
5477{
5478 if (!checkclearop(cap->oap))
5479 {
5480#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5481 /*
5482 * Right now, the BeBox doesn't seem to have an easy way to detect
5483 * window resizing, so we cheat and make the user detect it
5484 * manually with CTRL-L instead
5485 */
5486 ui_get_shellsize();
5487#endif
5488#ifdef FEAT_SYN_HL
5489 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005490 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#endif
5492 redraw_later(CLEAR);
5493 }
5494}
5495
5496/*
5497 * CTRL-O: In Select mode: switch to Visual mode for one command.
5498 * Otherwise: Go to older pcmark.
5499 */
5500 static void
5501nv_ctrlo(cap)
5502 cmdarg_T *cap;
5503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (VIsual_active && VIsual_select)
5505 {
5506 VIsual_select = FALSE;
5507 showmode();
5508 restart_VIsual_select = 2; /* restart Select mode later */
5509 }
5510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 cap->count1 = -cap->count1;
5513 nv_pcmark(cap);
5514 }
5515}
5516
5517/*
5518 * CTRL-^ command, short for ":e #"
5519 */
5520 static void
5521nv_hat(cap)
5522 cmdarg_T *cap;
5523{
5524 if (!checkclearopq(cap->oap))
5525 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5526 GETF_SETMARK|GETF_ALT, FALSE);
5527}
5528
5529/*
5530 * "Z" commands.
5531 */
5532 static void
5533nv_Zet(cap)
5534 cmdarg_T *cap;
5535{
5536 if (!checkclearopq(cap->oap))
5537 {
5538 switch (cap->nchar)
5539 {
5540 /* "ZZ": equivalent to ":x". */
5541 case 'Z': do_cmdline_cmd((char_u *)"x");
5542 break;
5543
5544 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5545 case 'Q': do_cmdline_cmd((char_u *)"q!");
5546 break;
5547
5548 default: clearopbeep(cap->oap);
5549 }
5550 }
5551}
5552
5553#if defined(FEAT_WINDOWS) || defined(PROTO)
5554/*
5555 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5556 */
5557 void
5558do_nv_ident(c1, c2)
5559 int c1;
5560 int c2;
5561{
5562 oparg_T oa;
5563 cmdarg_T ca;
5564
5565 clear_oparg(&oa);
5566 vim_memset(&ca, 0, sizeof(ca));
5567 ca.oap = &oa;
5568 ca.cmdchar = c1;
5569 ca.nchar = c2;
5570 nv_ident(&ca);
5571}
5572#endif
5573
5574/*
5575 * Handle the commands that use the word under the cursor.
5576 * [g] CTRL-] :ta to current identifier
5577 * [g] 'K' run program for current identifier
5578 * [g] '*' / to current identifier or string
5579 * [g] '#' ? to current identifier or string
5580 * g ']' :tselect for current identifier
5581 */
5582 static void
5583nv_ident(cap)
5584 cmdarg_T *cap;
5585{
5586 char_u *ptr = NULL;
5587 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005588 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 char_u *p;
5590 char_u *kp; /* value of 'keywordprg' */
5591 int kp_help; /* 'keywordprg' is ":help" */
5592 int n = 0; /* init for GCC */
5593 int cmdchar;
5594 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005595 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 char_u *aux_ptr;
5597 int isman;
5598 int isman_s;
5599
5600 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5601 {
5602 cmdchar = cap->nchar;
5603 g_cmd = TRUE;
5604 }
5605 else
5606 {
5607 cmdchar = cap->cmdchar;
5608 g_cmd = FALSE;
5609 }
5610
5611 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5612 cmdchar = '#';
5613
5614 /*
5615 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5616 */
5617 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 if (checkclearopq(cap->oap))
5622 return;
5623 }
5624
5625 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5626 (cmdchar == '*' || cmdchar == '#')
5627 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5628 {
5629 clearop(cap->oap);
5630 return;
5631 }
5632
5633 /* Allocate buffer to put the command in. Inserting backslashes can
5634 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5635 * and some numbers. */
5636 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5637 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5638 || STRCMP(kp, ":help") == 0);
5639 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5640 if (buf == NULL)
5641 return;
5642 buf[0] = NUL;
5643
5644 switch (cmdchar)
5645 {
5646 case '*':
5647 case '#':
5648 /*
5649 * Put cursor at start of word, makes search skip the word
5650 * under the cursor.
5651 * Call setpcmark() first, so "*``" puts the cursor back where
5652 * it was.
5653 */
5654 setpcmark();
5655 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5656
5657 if (!g_cmd && vim_iswordp(ptr))
5658 STRCPY(buf, "\\<");
5659 no_smartcase = TRUE; /* don't use 'smartcase' now */
5660 break;
5661
5662 case 'K':
5663 if (kp_help)
5664 STRCPY(buf, "he! ");
5665 else
5666 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005667 /* An external command will probably use an argument starting
5668 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005669 while (*ptr == '-' && n > 0)
5670 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005672 --n;
5673 }
5674 if (n == 0)
5675 {
5676 EMSG(_(e_noident)); /* found dashes only */
5677 vim_free(buf);
5678 return;
5679 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 /* When a count is given, turn it into a range. Is this
5682 * really what we want? */
5683 isman = (STRCMP(kp, "man") == 0);
5684 isman_s = (STRCMP(kp, "man -s") == 0);
5685 if (cap->count0 != 0 && !(isman || isman_s))
5686 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5687
5688 STRCAT(buf, "! ");
5689 if (cap->count0 == 0 && isman_s)
5690 STRCAT(buf, "man");
5691 else
5692 STRCAT(buf, kp);
5693 STRCAT(buf, " ");
5694 if (cap->count0 != 0 && (isman || isman_s))
5695 {
5696 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5697 STRCAT(buf, " ");
5698 }
5699 }
5700 break;
5701
5702 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005703 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704#ifdef FEAT_CSCOPE
5705 if (p_cst)
5706 STRCPY(buf, "cstag ");
5707 else
5708#endif
5709 STRCPY(buf, "ts ");
5710 break;
5711
5712 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005713 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (curbuf->b_help)
5715 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005717 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005718 if (g_cmd)
5719 STRCPY(buf, "tj ");
5720 else
5721 sprintf((char *)buf, "%ldta ", cap->count0);
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724
5725 /*
5726 * Now grab the chars in the identifier
5727 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005728 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005730 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005731 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005732 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005733 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005734 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005736 vim_free(buf);
5737 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005739 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5740 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741 {
5742 vim_free(buf);
5743 vim_free(p);
5744 return;
5745 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005746 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 STRCAT(buf, p);
5748 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005750 else
5751 {
5752 if (cmdchar == '*')
5753 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5754 else if (cmdchar == '#')
5755 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005756 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005757 {
5758 if (curbuf->b_help)
5759 /* ":help" handles unescaped argument */
5760 aux_ptr = (char_u *)"";
5761 else
5762 aux_ptr = (char_u *)"\\|\"\n[";
5763 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005764 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005765 aux_ptr = (char_u *)"\\|\"\n*?[";
5766
5767 p = buf + STRLEN(buf);
5768 while (n-- > 0)
5769 {
5770 /* put a backslash before \ and some others */
5771 if (vim_strchr(aux_ptr, *ptr) != NULL)
5772 *p++ = '\\';
5773#ifdef FEAT_MBYTE
5774 /* When current byte is a part of multibyte character, copy all
5775 * bytes of that character. */
5776 if (has_mbyte)
5777 {
5778 int i;
5779 int len = (*mb_ptr2len)(ptr) - 1;
5780
5781 for (i = 0; i < len && n >= 1; ++i, --n)
5782 *p++ = *ptr++;
5783 }
5784#endif
5785 *p++ = *ptr++;
5786 }
5787 *p = NUL;
5788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790 /*
5791 * Execute the command.
5792 */
5793 if (cmdchar == '*' || cmdchar == '#')
5794 {
5795 if (!g_cmd && (
5796#ifdef FEAT_MBYTE
5797 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5798#endif
5799 vim_iswordc(ptr[-1])))
5800 STRCAT(buf, "\\>");
5801#ifdef FEAT_CMDHIST
5802 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005803 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5805#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005806 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808 else
5809 do_cmdline_cmd(buf);
5810
5811 vim_free(buf);
5812}
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814/*
5815 * Get visually selected text, within one line only.
5816 * Returns FAIL if more than one line selected.
5817 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005818 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819get_visual_text(cap, pp, lenp)
5820 cmdarg_T *cap;
5821 char_u **pp; /* return: start of selected text */
5822 int *lenp; /* return: length of selected text */
5823{
5824 if (VIsual_mode != 'V')
5825 unadjust_for_sel();
5826 if (VIsual.lnum != curwin->w_cursor.lnum)
5827 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005828 if (cap != NULL)
5829 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 return FAIL;
5831 }
5832 if (VIsual_mode == 'V')
5833 {
5834 *pp = ml_get_curline();
5835 *lenp = (int)STRLEN(*pp);
5836 }
5837 else
5838 {
5839 if (lt(curwin->w_cursor, VIsual))
5840 {
5841 *pp = ml_get_pos(&curwin->w_cursor);
5842 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5843 }
5844 else
5845 {
5846 *pp = ml_get_pos(&VIsual);
5847 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5848 }
5849#ifdef FEAT_MBYTE
5850 if (has_mbyte)
5851 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005852 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853#endif
5854 }
5855 reset_VIsual_and_resel();
5856 return OK;
5857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859/*
5860 * CTRL-T: backwards in tag stack
5861 */
5862 static void
5863nv_tagpop(cap)
5864 cmdarg_T *cap;
5865{
5866 if (!checkclearopq(cap->oap))
5867 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5868}
5869
5870/*
5871 * Handle scrolling command 'H', 'L' and 'M'.
5872 */
5873 static void
5874nv_scroll(cap)
5875 cmdarg_T *cap;
5876{
5877 int used = 0;
5878 long n;
5879#ifdef FEAT_FOLDING
5880 linenr_T lnum;
5881#endif
5882 int half;
5883
5884 cap->oap->motion_type = MLINE;
5885 setpcmark();
5886
5887 if (cap->cmdchar == 'L')
5888 {
5889 validate_botline(); /* make sure curwin->w_botline is valid */
5890 curwin->w_cursor.lnum = curwin->w_botline - 1;
5891 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5892 curwin->w_cursor.lnum = 1;
5893 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005894 {
5895#ifdef FEAT_FOLDING
5896 if (hasAnyFolding(curwin))
5897 {
5898 /* Count a fold for one screen line. */
5899 for (n = cap->count1 - 1; n > 0
5900 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5901 {
5902 (void)hasFolding(curwin->w_cursor.lnum,
5903 &curwin->w_cursor.lnum, NULL);
5904 --curwin->w_cursor.lnum;
5905 }
5906 }
5907 else
5908#endif
5909 curwin->w_cursor.lnum -= cap->count1 - 1;
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912 else
5913 {
5914 if (cap->cmdchar == 'M')
5915 {
5916#ifdef FEAT_DIFF
5917 /* Don't count filler lines above the window. */
5918 used -= diff_check_fill(curwin, curwin->w_topline)
5919 - curwin->w_topfill;
5920#endif
5921 validate_botline(); /* make sure w_empty_rows is valid */
5922 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5923 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5924 {
5925#ifdef FEAT_DIFF
5926 /* Count half he number of filler lines to be "below this
5927 * line" and half to be "above the next line". */
5928 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5929 + n) / 2 >= half)
5930 {
5931 --n;
5932 break;
5933 }
5934#endif
5935 used += plines(curwin->w_topline + n);
5936 if (used >= half)
5937 break;
5938#ifdef FEAT_FOLDING
5939 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5940 n = lnum - curwin->w_topline;
5941#endif
5942 }
5943 if (n > 0 && used > curwin->w_height)
5944 --n;
5945 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005946 else /* (cap->cmdchar == 'H') */
5947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005949#ifdef FEAT_FOLDING
5950 if (hasAnyFolding(curwin))
5951 {
5952 /* Count a fold for one screen line. */
5953 lnum = curwin->w_topline;
5954 while (n-- > 0 && lnum < curwin->w_botline - 1)
5955 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005956 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005957 ++lnum;
5958 }
5959 n = lnum - curwin->w_topline;
5960 }
5961#endif
5962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 curwin->w_cursor.lnum = curwin->w_topline + n;
5964 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5965 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5966 }
5967
5968 cursor_correct(); /* correct for 'so' */
5969 beginline(BL_SOL | BL_FIX);
5970}
5971
5972/*
5973 * Cursor right commands.
5974 */
5975 static void
5976nv_right(cap)
5977 cmdarg_T *cap;
5978{
5979 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005980 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005982 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5983 {
5984 /* <C-Right> and <S-Right> move a word or WORD right */
5985 if (mod_mask & MOD_MASK_CTRL)
5986 cap->arg = TRUE;
5987 nv_wordcmd(cap);
5988 return;
5989 }
5990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 cap->oap->motion_type = MCHAR;
5992 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005993 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005995#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005997 * In virtual edit mode, there's no such thing as "past_line", as lines
5998 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 */
6000 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006001 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#endif
6003
6004 for (n = cap->count1; n > 0; --n)
6005 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006006 if ((!past_line && oneright() == FAIL)
6007 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006008 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
6010 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006011 * <Space> wraps to next line if 'whichwrap' has 's'.
6012 * 'l' wraps to next line if 'whichwrap' has 'l'.
6013 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 */
6015 if ( ((cap->cmdchar == ' '
6016 && vim_strchr(p_ww, 's') != NULL)
6017 || (cap->cmdchar == 'l'
6018 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006019 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 && vim_strchr(p_ww, '>') != NULL))
6021 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6022 {
6023 /* When deleting we also count the NL as a character.
6024 * Set cap->oap->inclusive when last char in the line is
6025 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006026 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 && !cap->oap->inclusive
6028 && !lineempty(curwin->w_cursor.lnum))
6029 cap->oap->inclusive = TRUE;
6030 else
6031 {
6032 ++curwin->w_cursor.lnum;
6033 curwin->w_cursor.col = 0;
6034#ifdef FEAT_VIRTUALEDIT
6035 curwin->w_cursor.coladd = 0;
6036#endif
6037 curwin->w_set_curswant = TRUE;
6038 cap->oap->inclusive = FALSE;
6039 }
6040 continue;
6041 }
6042 if (cap->oap->op_type == OP_NOP)
6043 {
6044 /* Only beep and flush if not moved at all */
6045 if (n == cap->count1)
6046 beep_flush();
6047 }
6048 else
6049 {
6050 if (!lineempty(curwin->w_cursor.lnum))
6051 cap->oap->inclusive = TRUE;
6052 }
6053 break;
6054 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006055 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 {
6057 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006058#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (virtual_active())
6060 oneright();
6061 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006064#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if (has_mbyte)
6066 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006067 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 ++curwin->w_cursor.col;
6071 }
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 }
6074#ifdef FEAT_FOLDING
6075 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6076 && cap->oap->op_type == OP_NOP)
6077 foldOpenCursor();
6078#endif
6079}
6080
6081/*
6082 * Cursor left commands.
6083 *
6084 * Returns TRUE when operator end should not be adjusted.
6085 */
6086 static void
6087nv_left(cap)
6088 cmdarg_T *cap;
6089{
6090 long n;
6091
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006092 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6093 {
6094 /* <C-Left> and <S-Left> move a word or WORD left */
6095 if (mod_mask & MOD_MASK_CTRL)
6096 cap->arg = 1;
6097 nv_bck_word(cap);
6098 return;
6099 }
6100
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 cap->oap->motion_type = MCHAR;
6102 cap->oap->inclusive = FALSE;
6103 for (n = cap->count1; n > 0; --n)
6104 {
6105 if (oneleft() == FAIL)
6106 {
6107 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6108 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6109 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6110 */
6111 if ( (((cap->cmdchar == K_BS
6112 || cap->cmdchar == Ctrl_H)
6113 && vim_strchr(p_ww, 'b') != NULL)
6114 || (cap->cmdchar == 'h'
6115 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006116 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 && vim_strchr(p_ww, '<') != NULL))
6118 && curwin->w_cursor.lnum > 1)
6119 {
6120 --(curwin->w_cursor.lnum);
6121 coladvance((colnr_T)MAXCOL);
6122 curwin->w_set_curswant = TRUE;
6123
6124 /* When the NL before the first char has to be deleted we
6125 * put the cursor on the NUL after the previous line.
6126 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006127 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if ( (cap->oap->op_type == OP_DELETE
6129 || cap->oap->op_type == OP_CHANGE)
6130 && !lineempty(curwin->w_cursor.lnum))
6131 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006132 char_u *cp = ml_get_cursor();
6133
6134 if (*cp != NUL)
6135 {
6136#ifdef FEAT_MBYTE
6137 if (has_mbyte)
6138 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6139 else
6140#endif
6141 ++curwin->w_cursor.col;
6142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 cap->retval |= CA_NO_ADJ_OP_END;
6144 }
6145 continue;
6146 }
6147 /* Only beep and flush if not moved at all */
6148 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6149 beep_flush();
6150 break;
6151 }
6152 }
6153#ifdef FEAT_FOLDING
6154 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6155 && cap->oap->op_type == OP_NOP)
6156 foldOpenCursor();
6157#endif
6158}
6159
6160/*
6161 * Cursor up commands.
6162 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6163 */
6164 static void
6165nv_up(cap)
6166 cmdarg_T *cap;
6167{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006168 if (mod_mask & MOD_MASK_SHIFT)
6169 {
6170 /* <S-Up> is page up */
6171 cap->arg = BACKWARD;
6172 nv_page(cap);
6173 }
6174 else
6175 {
6176 cap->oap->motion_type = MLINE;
6177 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6178 clearopbeep(cap->oap);
6179 else if (cap->arg)
6180 beginline(BL_WHITE | BL_FIX);
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182}
6183
6184/*
6185 * Cursor down commands.
6186 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6187 */
6188 static void
6189nv_down(cap)
6190 cmdarg_T *cap;
6191{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006192 if (mod_mask & MOD_MASK_SHIFT)
6193 {
6194 /* <S-Down> is page down */
6195 cap->arg = FORWARD;
6196 nv_page(cap);
6197 }
6198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6200 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006202 if (curwin->w_llist_ref == NULL)
6203 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6204 else
6205 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 else
6207#endif
6208 {
6209#ifdef FEAT_CMDWIN
6210 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006211 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 cmdwin_result = CAR;
6213 else
6214#endif
6215 {
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
6230nv_gotofile(cap)
6231 cmdarg_T *cap;
6232{
6233 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006234 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006236 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006239 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 return;
6241 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006242#ifdef FEAT_AUTOCMD
6243 if (curbuf_locked())
6244 {
6245 clearop(cap->oap);
6246 return;
6247 }
6248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006250 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
6252 if (ptr != NULL)
6253 {
6254 /* do autowrite if necessary */
6255 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006256 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 setpcmark();
6258 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006259 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006260 if (cap->nchar == 'F' && lnum >= 0)
6261 {
6262 curwin->w_cursor.lnum = lnum;
6263 check_cursor_lnum();
6264 beginline(BL_SOL | BL_FIX);
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 vim_free(ptr);
6267 }
6268 else
6269 clearop(cap->oap);
6270}
6271#endif
6272
6273/*
6274 * <End> command: to end of current line or last line.
6275 */
6276 static void
6277nv_end(cap)
6278 cmdarg_T *cap;
6279{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006280 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006282 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 nv_goto(cap);
6284 cap->count1 = 1; /* to end of current line */
6285 }
6286 nv_dollar(cap);
6287}
6288
6289/*
6290 * Handle the "$" command.
6291 */
6292 static void
6293nv_dollar(cap)
6294 cmdarg_T *cap;
6295{
6296 cap->oap->motion_type = MCHAR;
6297 cap->oap->inclusive = TRUE;
6298#ifdef FEAT_VIRTUALEDIT
6299 /* In virtual mode when off the edge of a line and an operator
6300 * is pending (whew!) keep the cursor where it is.
6301 * Otherwise, send it to the end of the line. */
6302 if (!virtual_active() || gchar_cursor() != NUL
6303 || cap->oap->op_type == OP_NOP)
6304#endif
6305 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6306 if (cursor_down((long)(cap->count1 - 1),
6307 cap->oap->op_type == OP_NOP) == FAIL)
6308 clearopbeep(cap->oap);
6309#ifdef FEAT_FOLDING
6310 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6311 foldOpenCursor();
6312#endif
6313}
6314
6315/*
6316 * Implementation of '?' and '/' commands.
6317 * If cap->arg is TRUE don't set PC mark.
6318 */
6319 static void
6320nv_search(cap)
6321 cmdarg_T *cap;
6322{
6323 oparg_T *oap = cap->oap;
6324
6325 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6326 {
6327 /* Translate "g??" to "g?g?" */
6328 cap->cmdchar = 'g';
6329 cap->nchar = '?';
6330 nv_operator(cap);
6331 return;
6332 }
6333
6334 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6335
6336 if (cap->searchbuf == NULL)
6337 {
6338 clearop(oap);
6339 return;
6340 }
6341
Bram Moolenaar46539112015-02-17 15:43:57 +01006342 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 (cap->arg ? 0 : SEARCH_MARK));
6344}
6345
6346/*
6347 * Handle "N" and "n" commands.
6348 * cap->arg is SEARCH_REV for "N", 0 for "n".
6349 */
6350 static void
6351nv_next(cap)
6352 cmdarg_T *cap;
6353{
Bram Moolenaar46539112015-02-17 15:43:57 +01006354 pos_T old = curwin->w_cursor;
6355 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6356
6357 if (i == 1 && equalpos(old, curwin->w_cursor))
6358 {
6359 /* Avoid getting stuck on the current cursor position, which can
6360 * happen when an offset is given and the cursor is on the last char
6361 * in the buffer: Repeat with count + 1. */
6362 cap->count1 += 1;
6363 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6364 cap->count1 -= 1;
6365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366}
6367
6368/*
6369 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6370 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006371 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006373 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374normal_search(cap, dir, pat, opt)
6375 cmdarg_T *cap;
6376 int dir;
6377 char_u *pat;
6378 int opt; /* extra flags for do_search() */
6379{
6380 int i;
6381
6382 cap->oap->motion_type = MCHAR;
6383 cap->oap->inclusive = FALSE;
6384 cap->oap->use_reg_one = TRUE;
6385 curwin->w_set_curswant = TRUE;
6386
6387 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006388 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (i == 0)
6390 clearop(cap->oap);
6391 else
6392 {
6393 if (i == 2)
6394 cap->oap->motion_type = MLINE;
6395#ifdef FEAT_VIRTUALEDIT
6396 curwin->w_cursor.coladd = 0;
6397#endif
6398#ifdef FEAT_FOLDING
6399 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6400 foldOpenCursor();
6401#endif
6402 }
6403
6404 /* "/$" will put the cursor after the end of the line, may need to
6405 * correct that here */
6406 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006407 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408}
6409
6410/*
6411 * Character search commands.
6412 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6413 * ',' and FALSE for ';'.
6414 * cap->nchar is NUL for ',' and ';' (repeat the search)
6415 */
6416 static void
6417nv_csearch(cap)
6418 cmdarg_T *cap;
6419{
6420 int t_cmd;
6421
6422 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6423 t_cmd = TRUE;
6424 else
6425 t_cmd = FALSE;
6426
6427 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6429 clearopbeep(cap->oap);
6430 else
6431 {
6432 curwin->w_set_curswant = TRUE;
6433#ifdef FEAT_VIRTUALEDIT
6434 /* Include a Tab for "tx" and for "dfx". */
6435 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6436 && (t_cmd || cap->oap->op_type != OP_NOP))
6437 {
6438 colnr_T scol, ecol;
6439
6440 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6441 curwin->w_cursor.coladd = ecol - scol;
6442 }
6443 else
6444 curwin->w_cursor.coladd = 0;
6445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447#ifdef FEAT_FOLDING
6448 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6449 foldOpenCursor();
6450#endif
6451 }
6452}
6453
6454/*
6455 * "[" and "]" commands.
6456 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6457 */
6458 static void
6459nv_brackets(cap)
6460 cmdarg_T *cap;
6461{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006462 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 pos_T prev_pos;
6464 pos_T *pos = NULL; /* init for GCC */
6465 pos_T old_pos; /* cursor position before command */
6466 int flag;
6467 long n;
6468 int findc;
6469 int c;
6470
6471 cap->oap->motion_type = MCHAR;
6472 cap->oap->inclusive = FALSE;
6473 old_pos = curwin->w_cursor;
6474#ifdef FEAT_VIRTUALEDIT
6475 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6476#endif
6477
6478#ifdef FEAT_SEARCHPATH
6479 /*
6480 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6481 */
6482 if (cap->nchar == 'f')
6483 nv_gotofile(cap);
6484 else
6485#endif
6486
6487#ifdef FEAT_FIND_ID
6488 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006489 * Find the occurrence(s) of the identifier or define under cursor
6490 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 *
6492 * search list jump
6493 * fwd bwd fwd bwd fwd bwd
6494 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6495 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6496 */
6497 if (vim_strchr((char_u *)
6498#ifdef EBCDIC
6499 "iI\005dD\067",
6500#else
6501 "iI\011dD\004",
6502#endif
6503 cap->nchar) != NULL)
6504 {
6505 char_u *ptr;
6506 int len;
6507
6508 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6509 clearop(cap->oap);
6510 else
6511 {
6512 find_pattern_in_path(ptr, 0, len, TRUE,
6513 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6514 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6515 cap->count1,
6516 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6517 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6518 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6519 (linenr_T)MAXLNUM);
6520 curwin->w_set_curswant = TRUE;
6521 }
6522 }
6523 else
6524#endif
6525
6526 /*
6527 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6528 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6529 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6530 * "[m" or "]m" search for prev/next start of (Java) method.
6531 * "[M" or "]M" search for prev/next end of (Java) method.
6532 */
6533 if ( (cap->cmdchar == '['
6534 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6535 || (cap->cmdchar == ']'
6536 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6537 {
6538 if (cap->nchar == '*')
6539 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 prev_pos.lnum = 0;
6541 if (cap->nchar == 'm' || cap->nchar == 'M')
6542 {
6543 if (cap->cmdchar == '[')
6544 findc = '{';
6545 else
6546 findc = '}';
6547 n = 9999;
6548 }
6549 else
6550 {
6551 findc = cap->nchar;
6552 n = cap->count1;
6553 }
6554 for ( ; n > 0; --n)
6555 {
6556 if ((pos = findmatchlimit(cap->oap, findc,
6557 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6558 {
6559 if (new_pos.lnum == 0) /* nothing found */
6560 {
6561 if (cap->nchar != 'm' && cap->nchar != 'M')
6562 clearopbeep(cap->oap);
6563 }
6564 else
6565 pos = &new_pos; /* use last one found */
6566 break;
6567 }
6568 prev_pos = new_pos;
6569 curwin->w_cursor = *pos;
6570 new_pos = *pos;
6571 }
6572 curwin->w_cursor = old_pos;
6573
6574 /*
6575 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6576 * brought us to the match for "[m" and "]M" when inside a method.
6577 * Try finding the '{' or '}' we want to be at.
6578 * Also repeat for the given count.
6579 */
6580 if (cap->nchar == 'm' || cap->nchar == 'M')
6581 {
6582 /* norm is TRUE for "]M" and "[m" */
6583 int norm = ((findc == '{') == (cap->nchar == 'm'));
6584
6585 n = cap->count1;
6586 /* found a match: we were inside a method */
6587 if (prev_pos.lnum != 0)
6588 {
6589 pos = &prev_pos;
6590 curwin->w_cursor = prev_pos;
6591 if (norm)
6592 --n;
6593 }
6594 else
6595 pos = NULL;
6596 while (n > 0)
6597 {
6598 for (;;)
6599 {
6600 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6601 {
6602 /* if not found anything, that's an error */
6603 if (pos == NULL)
6604 clearopbeep(cap->oap);
6605 n = 0;
6606 break;
6607 }
6608 c = gchar_cursor();
6609 if (c == '{' || c == '}')
6610 {
6611 /* Must have found end/start of class: use it.
6612 * Or found the place to be at. */
6613 if ((c == findc && norm) || (n == 1 && !norm))
6614 {
6615 new_pos = curwin->w_cursor;
6616 pos = &new_pos;
6617 n = 0;
6618 }
6619 /* if no match found at all, we started outside of the
6620 * class and we're inside now. Just go on. */
6621 else if (new_pos.lnum == 0)
6622 {
6623 new_pos = curwin->w_cursor;
6624 pos = &new_pos;
6625 }
6626 /* found start/end of other method: go to match */
6627 else if ((pos = findmatchlimit(cap->oap, findc,
6628 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6629 0)) == NULL)
6630 n = 0;
6631 else
6632 curwin->w_cursor = *pos;
6633 break;
6634 }
6635 }
6636 --n;
6637 }
6638 curwin->w_cursor = old_pos;
6639 if (pos == NULL && new_pos.lnum != 0)
6640 clearopbeep(cap->oap);
6641 }
6642 if (pos != NULL)
6643 {
6644 setpcmark();
6645 curwin->w_cursor = *pos;
6646 curwin->w_set_curswant = TRUE;
6647#ifdef FEAT_FOLDING
6648 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6649 && cap->oap->op_type == OP_NOP)
6650 foldOpenCursor();
6651#endif
6652 }
6653 }
6654
6655 /*
6656 * "[[", "[]", "]]" and "][": move to start or end of function
6657 */
6658 else if (cap->nchar == '[' || cap->nchar == ']')
6659 {
6660 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6661 flag = '{';
6662 else
6663 flag = '}'; /* "][" or "[]" */
6664
6665 curwin->w_set_curswant = TRUE;
6666 /*
6667 * Imitate strange Vi behaviour: When using "]]" with an operator
6668 * we also stop at '}'.
6669 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006670 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 (cap->oap->op_type != OP_NOP
6672 && cap->arg == FORWARD && flag == '{')))
6673 clearopbeep(cap->oap);
6674 else
6675 {
6676 if (cap->oap->op_type == OP_NOP)
6677 beginline(BL_WHITE | BL_FIX);
6678#ifdef FEAT_FOLDING
6679 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6680 foldOpenCursor();
6681#endif
6682 }
6683 }
6684
6685 /*
6686 * "[p", "[P", "]P" and "]p": put with indent adjustment
6687 */
6688 else if (cap->nchar == 'p' || cap->nchar == 'P')
6689 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006690 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006692 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6693 ? FORWARD : BACKWARD;
6694 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006695 int was_visual = VIsual_active;
6696 int line_count = curbuf->b_ml.ml_line_count;
6697 pos_T start, end;
6698
6699 if (VIsual_active)
6700 {
6701 start = ltoreq(VIsual, curwin->w_cursor)
6702 ? VIsual : curwin->w_cursor;
6703 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6704 curwin->w_cursor = (dir == BACKWARD ? start : end);
6705 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006706# ifdef FEAT_CLIPBOARD
6707 adjust_clip_reg(&regname);
6708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006710
6711 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006712 if (was_visual)
6713 {
6714 VIsual = start;
6715 curwin->w_cursor = end;
6716 if (dir == BACKWARD)
6717 {
6718 /* adjust lines */
6719 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6720 curwin->w_cursor.lnum +=
6721 curbuf->b_ml.ml_line_count - line_count;
6722 }
6723
6724 VIsual_active = TRUE;
6725 if (VIsual_mode == 'V')
6726 {
6727 /* delete visually selected lines */
6728 cap->cmdchar = 'd';
6729 cap->nchar = NUL;
6730 cap->oap->regname = regname;
6731 nv_operator(cap);
6732 do_pending_operator(cap, 0, FALSE);
6733 }
6734 if (VIsual_active)
6735 {
6736 end_visual_mode();
6737 redraw_later(SOME_VALID);
6738 }
6739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 }
6742
6743 /*
6744 * "['", "[`", "]'" and "]`": jump to next mark
6745 */
6746 else if (cap->nchar == '\'' || cap->nchar == '`')
6747 {
6748 pos = &curwin->w_cursor;
6749 for (n = cap->count1; n > 0; --n)
6750 {
6751 prev_pos = *pos;
6752 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6753 cap->nchar == '\'');
6754 if (pos == NULL)
6755 break;
6756 }
6757 if (pos == NULL)
6758 pos = &prev_pos;
6759 nv_cursormark(cap, cap->nchar == '\'', pos);
6760 }
6761
6762#ifdef FEAT_MOUSE
6763 /*
6764 * [ or ] followed by a middle mouse click: put selected text with
6765 * indent adjustment. Any other button just does as usual.
6766 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006767 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 {
6769 (void)do_mouse(cap->oap, cap->nchar,
6770 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6771 cap->count1, PUT_FIXINDENT);
6772 }
6773#endif /* FEAT_MOUSE */
6774
6775#ifdef FEAT_FOLDING
6776 /*
6777 * "[z" and "]z": move to start or end of open fold.
6778 */
6779 else if (cap->nchar == 'z')
6780 {
6781 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6782 cap->count1) == FAIL)
6783 clearopbeep(cap->oap);
6784 }
6785#endif
6786
6787#ifdef FEAT_DIFF
6788 /*
6789 * "[c" and "]c": move to next or previous diff-change.
6790 */
6791 else if (cap->nchar == 'c')
6792 {
6793 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6794 cap->count1) == FAIL)
6795 clearopbeep(cap->oap);
6796 }
6797#endif
6798
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006799#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006800 /*
6801 * "[s", "[S", "]s" and "]S": move to next spell error.
6802 */
6803 else if (cap->nchar == 's' || cap->nchar == 'S')
6804 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006805 setpcmark();
6806 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006807 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6808 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006809 {
6810 clearopbeep(cap->oap);
6811 break;
6812 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006813# ifdef FEAT_FOLDING
6814 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6815 foldOpenCursor();
6816# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006817 }
6818#endif
6819
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 /* Not a valid cap->nchar. */
6821 else
6822 clearopbeep(cap->oap);
6823}
6824
6825/*
6826 * Handle Normal mode "%" command.
6827 */
6828 static void
6829nv_percent(cap)
6830 cmdarg_T *cap;
6831{
6832 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006833#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 linenr_T lnum = curwin->w_cursor.lnum;
6835#endif
6836
6837 cap->oap->inclusive = TRUE;
6838 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6839 {
6840 if (cap->count0 > 100)
6841 clearopbeep(cap->oap);
6842 else
6843 {
6844 cap->oap->motion_type = MLINE;
6845 setpcmark();
6846 /* Round up, so CTRL-G will give same value. Watch out for a
6847 * large line count, the line number must not go negative! */
6848 if (curbuf->b_ml.ml_line_count > 1000000)
6849 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6850 / 100L * cap->count0;
6851 else
6852 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6853 cap->count0 + 99L) / 100L;
6854 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6855 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6856 beginline(BL_SOL | BL_FIX);
6857 }
6858 }
6859 else /* "%" : go to matching paren */
6860 {
6861 cap->oap->motion_type = MCHAR;
6862 cap->oap->use_reg_one = TRUE;
6863 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6864 clearopbeep(cap->oap);
6865 else
6866 {
6867 setpcmark();
6868 curwin->w_cursor = *pos;
6869 curwin->w_set_curswant = TRUE;
6870#ifdef FEAT_VIRTUALEDIT
6871 curwin->w_cursor.coladd = 0;
6872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 }
6876#ifdef FEAT_FOLDING
6877 if (cap->oap->op_type == OP_NOP
6878 && lnum != curwin->w_cursor.lnum
6879 && (fdo_flags & FDO_PERCENT)
6880 && KeyTyped)
6881 foldOpenCursor();
6882#endif
6883}
6884
6885/*
6886 * Handle "(" and ")" commands.
6887 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6888 */
6889 static void
6890nv_brace(cap)
6891 cmdarg_T *cap;
6892{
6893 cap->oap->motion_type = MCHAR;
6894 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006895 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6896 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 curwin->w_set_curswant = TRUE;
6898
6899 if (findsent(cap->arg, cap->count1) == FAIL)
6900 clearopbeep(cap->oap);
6901 else
6902 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006903 /* Don't leave the cursor on the NUL past end of line. */
6904 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905#ifdef FEAT_VIRTUALEDIT
6906 curwin->w_cursor.coladd = 0;
6907#endif
6908#ifdef FEAT_FOLDING
6909 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6910 foldOpenCursor();
6911#endif
6912 }
6913}
6914
6915/*
6916 * "m" command: Mark a position.
6917 */
6918 static void
6919nv_mark(cap)
6920 cmdarg_T *cap;
6921{
6922 if (!checkclearop(cap->oap))
6923 {
6924 if (setmark(cap->nchar) == FAIL)
6925 clearopbeep(cap->oap);
6926 }
6927}
6928
6929/*
6930 * "{" and "}" commands.
6931 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6932 */
6933 static void
6934nv_findpar(cap)
6935 cmdarg_T *cap;
6936{
6937 cap->oap->motion_type = MCHAR;
6938 cap->oap->inclusive = FALSE;
6939 cap->oap->use_reg_one = TRUE;
6940 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006941 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 clearopbeep(cap->oap);
6943 else
6944 {
6945#ifdef FEAT_VIRTUALEDIT
6946 curwin->w_cursor.coladd = 0;
6947#endif
6948#ifdef FEAT_FOLDING
6949 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6950 foldOpenCursor();
6951#endif
6952 }
6953}
6954
6955/*
6956 * "u" command: Undo or make lower case.
6957 */
6958 static void
6959nv_undo(cap)
6960 cmdarg_T *cap;
6961{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006962 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
6964 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6965 cap->cmdchar = 'g';
6966 cap->nchar = 'u';
6967 nv_operator(cap);
6968 }
6969 else
6970 nv_kundo(cap);
6971}
6972
6973/*
6974 * <Undo> command.
6975 */
6976 static void
6977nv_kundo(cap)
6978 cmdarg_T *cap;
6979{
6980 if (!checkclearopq(cap->oap))
6981 {
6982 u_undo((int)cap->count1);
6983 curwin->w_set_curswant = TRUE;
6984 }
6985}
6986
6987/*
6988 * Handle the "r" command.
6989 */
6990 static void
6991nv_replace(cap)
6992 cmdarg_T *cap;
6993{
6994 char_u *ptr;
6995 int had_ctrl_v;
6996 long n;
6997
6998 if (checkclearop(cap->oap))
6999 return;
7000
7001 /* get another character */
7002 if (cap->nchar == Ctrl_V)
7003 {
7004 had_ctrl_v = Ctrl_V;
7005 cap->nchar = get_literal();
7006 /* Don't redo a multibyte character with CTRL-V. */
7007 if (cap->nchar > DEL)
7008 had_ctrl_v = NUL;
7009 }
7010 else
7011 had_ctrl_v = NUL;
7012
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007013 /* Abort if the character is a special key. */
7014 if (IS_SPECIAL(cap->nchar))
7015 {
7016 clearopbeep(cap->oap);
7017 return;
7018 }
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 /* Visual mode "r" */
7021 if (VIsual_active)
7022 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007023 if (got_int)
7024 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007025 if (had_ctrl_v)
7026 {
7027 if (cap->nchar == '\r')
7028 cap->nchar = -1;
7029 else if (cap->nchar == '\n')
7030 cap->nchar = -2;
7031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 nv_operator(cap);
7033 return;
7034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036#ifdef FEAT_VIRTUALEDIT
7037 /* Break tabs, etc. */
7038 if (virtual_active())
7039 {
7040 if (u_save_cursor() == FAIL)
7041 return;
7042 if (gchar_cursor() == NUL)
7043 {
7044 /* Add extra space and put the cursor on the first one. */
7045 coladvance_force((colnr_T)(getviscol() + cap->count1));
7046 curwin->w_cursor.col -= cap->count1;
7047 }
7048 else if (gchar_cursor() == TAB)
7049 coladvance_force(getviscol());
7050 }
7051#endif
7052
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007053 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007055 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#ifdef FEAT_MBYTE
7057 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7058#endif
7059 )
7060 {
7061 clearopbeep(cap->oap);
7062 return;
7063 }
7064
7065 /*
7066 * Replacing with a TAB is done by edit() when it is complicated because
7067 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7068 * Other characters are done below to avoid problems with things like
7069 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7070 */
7071 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7072 {
7073 stuffnumReadbuff(cap->count1);
7074 stuffcharReadbuff('R');
7075 stuffcharReadbuff('\t');
7076 stuffcharReadbuff(ESC);
7077 return;
7078 }
7079
7080 /* save line for undo */
7081 if (u_save_cursor() == FAIL)
7082 return;
7083
7084 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7085 {
7086 /*
7087 * Replace character(s) by a single newline.
7088 * Strange vi behaviour: Only one newline is inserted.
7089 * Delete the characters here.
7090 * Insert the newline with an insert command, takes care of
7091 * autoindent. The insert command depends on being on the last
7092 * character of a line or not.
7093 */
7094#ifdef FEAT_MBYTE
7095 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7096#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007097 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098#endif
7099 stuffcharReadbuff('\r');
7100 stuffcharReadbuff(ESC);
7101
7102 /* Give 'r' to edit(), to get the redo command right. */
7103 invoke_edit(cap, TRUE, 'r', FALSE);
7104 }
7105 else
7106 {
7107 prep_redo(cap->oap->regname, cap->count1,
7108 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7109
7110 curbuf->b_op_start = curwin->w_cursor;
7111#ifdef FEAT_MBYTE
7112 if (has_mbyte)
7113 {
7114 int old_State = State;
7115
7116 if (cap->ncharC1 != 0)
7117 AppendCharToRedobuff(cap->ncharC1);
7118 if (cap->ncharC2 != 0)
7119 AppendCharToRedobuff(cap->ncharC2);
7120
7121 /* This is slow, but it handles replacing a single-byte with a
7122 * multi-byte and the other way around. Also handles adding
7123 * composing characters for utf-8. */
7124 for (n = cap->count1; n > 0; --n)
7125 {
7126 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007127 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7128 {
7129 int c = ins_copychar(curwin->w_cursor.lnum
7130 + (cap->nchar == Ctrl_Y ? -1 : 1));
7131 if (c != NUL)
7132 ins_char(c);
7133 else
7134 /* will be decremented further down */
7135 ++curwin->w_cursor.col;
7136 }
7137 else
7138 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 State = old_State;
7140 if (cap->ncharC1 != 0)
7141 ins_char(cap->ncharC1);
7142 if (cap->ncharC2 != 0)
7143 ins_char(cap->ncharC2);
7144 }
7145 }
7146 else
7147#endif
7148 {
7149 /*
7150 * Replace the characters within one line.
7151 */
7152 for (n = cap->count1; n > 0; --n)
7153 {
7154 /*
7155 * Get ptr again, because u_save and/or showmatch() will have
7156 * released the line. At the same time we let know that the
7157 * line will be changed.
7158 */
7159 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007160 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7161 {
7162 int c = ins_copychar(curwin->w_cursor.lnum
7163 + (cap->nchar == Ctrl_Y ? -1 : 1));
7164 if (c != NUL)
7165 ptr[curwin->w_cursor.col] = c;
7166 }
7167 else
7168 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 if (p_sm && msg_silent == 0)
7170 showmatch(cap->nchar);
7171 ++curwin->w_cursor.col;
7172 }
7173#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007174 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007176 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar009b2592004-10-24 19:18:58 +00007178 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007179 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007181 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183#endif
7184
7185 /* mark the buffer as changed and prepare for displaying */
7186 changed_bytes(curwin->w_cursor.lnum,
7187 (colnr_T)(curwin->w_cursor.col - cap->count1));
7188 }
7189 --curwin->w_cursor.col; /* cursor on the last replaced char */
7190#ifdef FEAT_MBYTE
7191 /* if the character on the left of the current cursor is a multi-byte
7192 * character, move two characters left */
7193 if (has_mbyte)
7194 mb_adjust_cursor();
7195#endif
7196 curbuf->b_op_end = curwin->w_cursor;
7197 curwin->w_set_curswant = TRUE;
7198 set_last_insert(cap->nchar);
7199 }
7200}
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202/*
7203 * 'o': Exchange start and end of Visual area.
7204 * 'O': same, but in block mode exchange left and right corners.
7205 */
7206 static void
7207v_swap_corners(cmdchar)
7208 int cmdchar;
7209{
7210 pos_T old_cursor;
7211 colnr_T left, right;
7212
7213 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7214 {
7215 old_cursor = curwin->w_cursor;
7216 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7217 curwin->w_cursor.lnum = VIsual.lnum;
7218 coladvance(left);
7219 VIsual = curwin->w_cursor;
7220
7221 curwin->w_cursor.lnum = old_cursor.lnum;
7222 curwin->w_curswant = right;
7223 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7224 * right one column */
7225 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7226 ++curwin->w_curswant;
7227 coladvance(curwin->w_curswant);
7228 if (curwin->w_cursor.col == old_cursor.col
7229#ifdef FEAT_VIRTUALEDIT
7230 && (!virtual_active()
7231 || curwin->w_cursor.coladd == old_cursor.coladd)
7232#endif
7233 )
7234 {
7235 curwin->w_cursor.lnum = VIsual.lnum;
7236 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7237 ++right;
7238 coladvance(right);
7239 VIsual = curwin->w_cursor;
7240
7241 curwin->w_cursor.lnum = old_cursor.lnum;
7242 coladvance(left);
7243 curwin->w_curswant = left;
7244 }
7245 }
7246 else
7247 {
7248 old_cursor = curwin->w_cursor;
7249 curwin->w_cursor = VIsual;
7250 VIsual = old_cursor;
7251 curwin->w_set_curswant = TRUE;
7252 }
7253}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255/*
7256 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7257 */
7258 static void
7259nv_Replace(cap)
7260 cmdarg_T *cap;
7261{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 if (VIsual_active) /* "R" is replace lines */
7263 {
7264 cap->cmdchar = 'c';
7265 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007266 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 VIsual_mode = 'V';
7268 nv_operator(cap);
7269 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007270 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
7272 if (!curbuf->b_p_ma)
7273 EMSG(_(e_modifiable));
7274 else
7275 {
7276#ifdef FEAT_VIRTUALEDIT
7277 if (virtual_active())
7278 coladvance(getviscol());
7279#endif
7280 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7281 }
7282 }
7283}
7284
7285#ifdef FEAT_VREPLACE
7286/*
7287 * "gr".
7288 */
7289 static void
7290nv_vreplace(cap)
7291 cmdarg_T *cap;
7292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (VIsual_active)
7294 {
7295 cap->cmdchar = 'r';
7296 cap->nchar = cap->extra_char;
7297 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7298 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007299 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 if (!curbuf->b_p_ma)
7302 EMSG(_(e_modifiable));
7303 else
7304 {
7305 if (cap->extra_char == Ctrl_V) /* get another character */
7306 cap->extra_char = get_literal();
7307 stuffcharReadbuff(cap->extra_char);
7308 stuffcharReadbuff(ESC);
7309# ifdef FEAT_VIRTUALEDIT
7310 if (virtual_active())
7311 coladvance(getviscol());
7312# endif
7313 invoke_edit(cap, TRUE, 'v', FALSE);
7314 }
7315 }
7316}
7317#endif
7318
7319/*
7320 * Swap case for "~" command, when it does not work like an operator.
7321 */
7322 static void
7323n_swapchar(cap)
7324 cmdarg_T *cap;
7325{
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
7338 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7339 {
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
7419nv_cursormark(cap, flag, pos)
7420 cmdarg_T *cap;
7421 int flag;
7422 pos_T *pos;
7423{
7424 if (check_mark(pos) == FAIL)
7425 clearop(cap->oap);
7426 else
7427 {
7428 if (cap->cmdchar == '\''
7429 || cap->cmdchar == '`'
7430 || cap->cmdchar == '['
7431 || cap->cmdchar == ']')
7432 setpcmark();
7433 curwin->w_cursor = *pos;
7434 if (flag)
7435 beginline(BL_WHITE | BL_FIX);
7436 else
7437 check_cursor();
7438 }
7439 cap->oap->motion_type = flag ? MLINE : MCHAR;
7440 if (cap->cmdchar == '`')
7441 cap->oap->use_reg_one = TRUE;
7442 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7443 curwin->w_set_curswant = TRUE;
7444}
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446/*
7447 * Handle commands that are operators in Visual mode.
7448 */
7449 static void
7450v_visop(cap)
7451 cmdarg_T *cap;
7452{
7453 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7454
7455 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007456 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 if (isupper(cap->cmdchar))
7458 {
7459 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007460 {
7461 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7465 curwin->w_curswant = MAXCOL;
7466 }
7467 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7468 nv_operator(cap);
7469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471/*
7472 * "s" and "S" commands.
7473 */
7474 static void
7475nv_subst(cap)
7476 cmdarg_T *cap;
7477{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7479 {
7480 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007481 {
7482 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 cap->cmdchar = 'c';
7486 nv_operator(cap);
7487 }
7488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 nv_optrans(cap);
7490}
7491
7492/*
7493 * Abbreviated commands.
7494 */
7495 static void
7496nv_abbrev(cap)
7497 cmdarg_T *cap;
7498{
7499 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7500 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7501
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 /* in Visual mode these commands are operators */
7503 if (VIsual_active)
7504 v_visop(cap);
7505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 nv_optrans(cap);
7507}
7508
7509/*
7510 * Translate a command into another command.
7511 */
7512 static void
7513nv_optrans(cap)
7514 cmdarg_T *cap;
7515{
7516 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7517 (char_u *)"d$", (char_u *)"c$",
7518 (char_u *)"cl", (char_u *)"cc",
7519 (char_u *)"yy", (char_u *)":s\r"};
7520 static char_u *str = (char_u *)"xXDCsSY&";
7521
7522 if (!checkclearopq(cap->oap))
7523 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007524 /* In Vi "2D" doesn't delete the next line. Can't translate it
7525 * either, because "2." should also not use the count. */
7526 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7527 {
7528 cap->oap->start = curwin->w_cursor;
7529 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007530#ifdef FEAT_EVAL
7531 set_op_var(OP_DELETE);
7532#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007533 cap->count1 = 1;
7534 nv_dollar(cap);
7535 finish_op = TRUE;
7536 ResetRedobuff();
7537 AppendCharToRedobuff('D');
7538 }
7539 else
7540 {
7541 if (cap->count0)
7542 stuffnumReadbuff(cap->count0);
7543 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546 cap->opcount = 0;
7547}
7548
7549/*
7550 * "'" and "`" commands. Also for "g'" and "g`".
7551 * cap->arg is TRUE for "'" and "g'".
7552 */
7553 static void
7554nv_gomark(cap)
7555 cmdarg_T *cap;
7556{
7557 pos_T *pos;
7558 int c;
7559#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007560 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7562#endif
7563
7564 if (cap->cmdchar == 'g')
7565 c = cap->extra_char;
7566 else
7567 c = cap->nchar;
7568 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7569 if (pos == (pos_T *)-1) /* jumped to other file */
7570 {
7571 if (cap->arg)
7572 {
7573 check_cursor_lnum();
7574 beginline(BL_WHITE | BL_FIX);
7575 }
7576 else
7577 check_cursor();
7578 }
7579 else
7580 nv_cursormark(cap, cap->arg, pos);
7581
7582#ifdef FEAT_VIRTUALEDIT
7583 /* May need to clear the coladd that a mark includes. */
7584 if (!virtual_active())
7585 curwin->w_cursor.coladd = 0;
7586#endif
7587#ifdef FEAT_FOLDING
7588 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007589 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007590 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 && (fdo_flags & FDO_MARK)
7592 && old_KeyTyped)
7593 foldOpenCursor();
7594#endif
7595}
7596
7597/*
7598 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7599 */
7600 static void
7601nv_pcmark(cap)
7602 cmdarg_T *cap;
7603{
7604#ifdef FEAT_JUMPLIST
7605 pos_T *pos;
7606# ifdef FEAT_FOLDING
7607 linenr_T lnum = curwin->w_cursor.lnum;
7608 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7609# endif
7610
7611 if (!checkclearopq(cap->oap))
7612 {
7613 if (cap->cmdchar == 'g')
7614 pos = movechangelist((int)cap->count1);
7615 else
7616 pos = movemark((int)cap->count1);
7617 if (pos == (pos_T *)-1) /* jump to other file */
7618 {
7619 curwin->w_set_curswant = TRUE;
7620 check_cursor();
7621 }
7622 else if (pos != NULL) /* can jump */
7623 nv_cursormark(cap, FALSE, pos);
7624 else if (cap->cmdchar == 'g')
7625 {
7626 if (curbuf->b_changelistlen == 0)
7627 EMSG(_("E664: changelist is empty"));
7628 else if (cap->count1 < 0)
7629 EMSG(_("E662: At start of changelist"));
7630 else
7631 EMSG(_("E663: At end of changelist"));
7632 }
7633 else
7634 clearopbeep(cap->oap);
7635# ifdef FEAT_FOLDING
7636 if (cap->oap->op_type == OP_NOP
7637 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7638 && (fdo_flags & FDO_MARK)
7639 && old_KeyTyped)
7640 foldOpenCursor();
7641# endif
7642 }
7643#else
7644 clearopbeep(cap->oap);
7645#endif
7646}
7647
7648/*
7649 * Handle '"' command.
7650 */
7651 static void
7652nv_regname(cap)
7653 cmdarg_T *cap;
7654{
7655 if (checkclearop(cap->oap))
7656 return;
7657#ifdef FEAT_EVAL
7658 if (cap->nchar == '=')
7659 cap->nchar = get_expr_register();
7660#endif
7661 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7662 {
7663 cap->oap->regname = cap->nchar;
7664 cap->opcount = cap->count0; /* remember count before '"' */
7665#ifdef FEAT_EVAL
7666 set_reg_var(cap->oap->regname);
7667#endif
7668 }
7669 else
7670 clearopbeep(cap->oap);
7671}
7672
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673/*
7674 * Handle "v", "V" and "CTRL-V" commands.
7675 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7676 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007677 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 */
7679 static void
7680nv_visual(cap)
7681 cmdarg_T *cap;
7682{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007683 if (cap->cmdchar == Ctrl_Q)
7684 cap->cmdchar = Ctrl_V;
7685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7687 * characterwise, linewise, or blockwise. */
7688 if (cap->oap->op_type != OP_NOP)
7689 {
7690 cap->oap->motion_force = cap->cmdchar;
7691 finish_op = FALSE; /* operator doesn't finish now but later */
7692 return;
7693 }
7694
7695 VIsual_select = cap->arg;
7696 if (VIsual_active) /* change Visual mode */
7697 {
7698 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7699 end_visual_mode();
7700 else /* toggle char/block mode */
7701 { /* or char/line mode */
7702 VIsual_mode = cap->cmdchar;
7703 showmode();
7704 }
7705 redraw_curbuf_later(INVERTED); /* update the inversion */
7706 }
7707 else /* start Visual mode */
7708 {
7709 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007710 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007712 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 VIsual = curwin->w_cursor;
7714
7715 VIsual_active = TRUE;
7716 VIsual_reselect = TRUE;
7717 if (!cap->arg)
7718 /* start Select mode when 'selectmode' contains "cmd" */
7719 may_start_select('c');
7720#ifdef FEAT_MOUSE
7721 setmouse();
7722#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007723 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 redraw_cmdline = TRUE; /* show visual mode later */
7725 /*
7726 * For V and ^V, we multiply the number of lines even if there
7727 * was only one -- webb
7728 */
7729 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7730 {
7731 curwin->w_cursor.lnum +=
7732 resel_VIsual_line_count * cap->count0 - 1;
7733 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7734 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7735 }
7736 VIsual_mode = resel_VIsual_mode;
7737 if (VIsual_mode == 'v')
7738 {
7739 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007740 {
7741 validate_virtcol();
7742 curwin->w_curswant = curwin->w_virtcol
7743 + resel_VIsual_vcol * cap->count0 - 1;
7744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007746 curwin->w_curswant = resel_VIsual_vcol;
7747 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007749 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
7751 curwin->w_curswant = MAXCOL;
7752 coladvance((colnr_T)MAXCOL);
7753 }
7754 else if (VIsual_mode == Ctrl_V)
7755 {
7756 validate_virtcol();
7757 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007758 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 coladvance(curwin->w_curswant);
7760 }
7761 else
7762 curwin->w_set_curswant = TRUE;
7763 redraw_curbuf_later(INVERTED); /* show the inversion */
7764 }
7765 else
7766 {
7767 if (!cap->arg)
7768 /* start Select mode when 'selectmode' contains "cmd" */
7769 may_start_select('c');
7770 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007771 if (VIsual_mode != 'V' && *p_sel == 'e')
7772 ++cap->count1; /* include one more char */
7773 if (cap->count0 > 0 && --cap->count1 > 0)
7774 {
7775 /* With a count select that many characters or lines. */
7776 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7777 nv_right(cap);
7778 else if (VIsual_mode == 'V')
7779 nv_down(cap);
7780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
7782 }
7783}
7784
7785/*
7786 * Start selection for Shift-movement keys.
7787 */
7788 void
7789start_selection()
7790{
7791 /* if 'selectmode' contains "key", start Select mode */
7792 may_start_select('k');
7793 n_start_visual_mode('v');
7794}
7795
7796/*
7797 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7798 */
7799 void
7800may_start_select(c)
7801 int c;
7802{
7803 VIsual_select = (stuff_empty() && typebuf_typed()
7804 && (vim_strchr(p_slm, c) != NULL));
7805}
7806
7807/*
7808 * Start Visual mode "c".
7809 * Should set VIsual_select before calling this.
7810 */
7811 static void
7812n_start_visual_mode(c)
7813 int c;
7814{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007815#ifdef FEAT_CONCEAL
7816 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007817 conceal_check_cursur_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 Moolenaar8e469272010-07-28 19:38:16 +02007844 conceal_check_cursur_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
7869nv_window(cap)
7870 cmdarg_T *cap;
7871{
7872#ifdef FEAT_WINDOWS
7873 if (!checkclearop(cap->oap))
7874 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7875#else
7876 (void)checkclearop(cap->oap);
7877#endif
7878}
7879
7880/*
7881 * CTRL-Z: Suspend
7882 */
7883 static void
7884nv_suspend(cap)
7885 cmdarg_T *cap;
7886{
7887 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 if (VIsual_active)
7889 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 do_cmdline_cmd((char_u *)"st");
7891}
7892
7893/*
7894 * Commands starting with "g".
7895 */
7896 static void
7897nv_g_cmd(cap)
7898 cmdarg_T *cap;
7899{
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;
7924 nv_addsub(cap);
7925 }
7926 else
7927 clearopbeep(oap);
7928 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930#ifdef FEAT_VREPLACE
7931 /*
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;
7942#endif
7943
7944 case '&':
7945 do_cmdline_cmd((char_u *)"%s//~/&");
7946 break;
7947
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 /*
7949 * "gv": Reselect the previous Visual area. If Visual already active,
7950 * exchange previous and current Visual area.
7951 */
7952 case 'v':
7953 if (checkclearop(oap))
7954 break;
7955
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007956 if ( curbuf->b_visual.vi_start.lnum == 0
7957 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7958 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 beep_flush();
7960 else
7961 {
7962 /* set w_cursor to the start of the Visual area, tpos to the end */
7963 if (VIsual_active)
7964 {
7965 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007966 VIsual_mode = curbuf->b_visual.vi_mode;
7967 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968# ifdef FEAT_EVAL
7969 curbuf->b_visual_mode_eval = i;
7970# endif
7971 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007972 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7973 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007975 tpos = curbuf->b_visual.vi_end;
7976 curbuf->b_visual.vi_end = curwin->w_cursor;
7977 curwin->w_cursor = curbuf->b_visual.vi_start;
7978 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 }
7980 else
7981 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007982 VIsual_mode = curbuf->b_visual.vi_mode;
7983 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7984 tpos = curbuf->b_visual.vi_end;
7985 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987
7988 VIsual_active = TRUE;
7989 VIsual_reselect = TRUE;
7990
7991 /* Set Visual to the start and w_cursor to the end of the Visual
7992 * area. Make sure they are on an existing character. */
7993 check_cursor();
7994 VIsual = curwin->w_cursor;
7995 curwin->w_cursor = tpos;
7996 check_cursor();
7997 update_topline();
7998 /*
7999 * When called from normal "g" command: start Select mode when
8000 * 'selectmode' contains "cmd". When called for K_SELECT, always
8001 * start Select mode.
8002 */
8003 if (cap->arg)
8004 VIsual_select = TRUE;
8005 else
8006 may_start_select('c');
8007#ifdef FEAT_MOUSE
8008 setmouse();
8009#endif
8010#ifdef FEAT_CLIPBOARD
8011 /* Make sure the clipboard gets updated. Needed because start and
8012 * end are still the same, and the selection needs to be owned */
8013 clip_star.vmode = NUL;
8014#endif
8015 redraw_curbuf_later(INVERTED);
8016 showmode();
8017 }
8018 break;
8019 /*
8020 * "gV": Don't reselect the previous Visual area after a Select mode
8021 * mapping of menu.
8022 */
8023 case 'V':
8024 VIsual_reselect = FALSE;
8025 break;
8026
8027 /*
8028 * "gh": start Select mode.
8029 * "gH": start Select line mode.
8030 * "g^H": start Select block mode.
8031 */
8032 case K_BS:
8033 cap->nchar = Ctrl_H;
8034 /* FALLTHROUGH */
8035 case 'h':
8036 case 'H':
8037 case Ctrl_H:
8038# ifdef EBCDIC
8039 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8040 if (cap->nchar == Ctrl_H)
8041 cap->cmdchar = Ctrl_V;
8042 else
8043# endif
8044 cap->cmdchar = cap->nchar + ('v' - 'h');
8045 cap->arg = TRUE;
8046 nv_visual(cap);
8047 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008048
8049 /* "gn", "gN" visually select next/previous search match
8050 * "gn" selects next match
8051 * "gN" selects previous match
8052 */
8053 case 'N':
8054 case 'n':
8055 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008056 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008057 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058
8059 /*
8060 * "gj" and "gk" two new funny movement keys -- up and down
8061 * movement based on *screen* line rather than *file* line.
8062 */
8063 case 'j':
8064 case K_DOWN:
8065 /* with 'nowrap' it works just like the normal "j" command; also when
8066 * in a closed fold */
8067 if (!curwin->w_p_wrap
8068#ifdef FEAT_FOLDING
8069 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8070#endif
8071 )
8072 {
8073 oap->motion_type = MLINE;
8074 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8075 }
8076 else
8077 i = nv_screengo(oap, FORWARD, cap->count1);
8078 if (i == FAIL)
8079 clearopbeep(oap);
8080 break;
8081
8082 case 'k':
8083 case K_UP:
8084 /* with 'nowrap' it works just like the normal "k" command; also when
8085 * in a closed fold */
8086 if (!curwin->w_p_wrap
8087#ifdef FEAT_FOLDING
8088 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8089#endif
8090 )
8091 {
8092 oap->motion_type = MLINE;
8093 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8094 }
8095 else
8096 i = nv_screengo(oap, BACKWARD, cap->count1);
8097 if (i == FAIL)
8098 clearopbeep(oap);
8099 break;
8100
8101 /*
8102 * "gJ": join two lines without inserting a space.
8103 */
8104 case 'J':
8105 nv_join(cap);
8106 break;
8107
8108 /*
8109 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8110 * "gm": middle of "g0" and "g$".
8111 */
8112 case '^':
8113 flag = TRUE;
8114 /* FALLTHROUGH */
8115
8116 case '0':
8117 case 'm':
8118 case K_HOME:
8119 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 oap->motion_type = MCHAR;
8121 oap->inclusive = FALSE;
8122 if (curwin->w_p_wrap
8123#ifdef FEAT_VERTSPLIT
8124 && curwin->w_width != 0
8125#endif
8126 )
8127 {
8128 int width1 = W_WIDTH(curwin) - curwin_col_off();
8129 int width2 = width1 + curwin_col_off2();
8130
8131 validate_virtcol();
8132 i = 0;
8133 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8134 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8135 }
8136 else
8137 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008138 /* Go to the middle of the screen line. When 'number' or
8139 * 'relativenumber' is on and lines are wrapping the middle can be more
8140 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (cap->nchar == 'm')
8142 i += (W_WIDTH(curwin) - curwin_col_off()
8143 + ((curwin->w_p_wrap && i > 0)
8144 ? curwin_col_off2() : 0)) / 2;
8145 coladvance((colnr_T)i);
8146 if (flag)
8147 {
8148 do
8149 i = gchar_cursor();
8150 while (vim_iswhite(i) && oneright() == OK);
8151 }
8152 curwin->w_set_curswant = TRUE;
8153 break;
8154
8155 case '_':
8156 /* "g_": to the last non-blank character in the line or <count> lines
8157 * downward. */
8158 cap->oap->motion_type = MCHAR;
8159 cap->oap->inclusive = TRUE;
8160 curwin->w_curswant = MAXCOL;
8161 if (cursor_down((long)(cap->count1 - 1),
8162 cap->oap->op_type == OP_NOP) == FAIL)
8163 clearopbeep(cap->oap);
8164 else
8165 {
8166 char_u *ptr = ml_get_curline();
8167
8168 /* In Visual mode we may end up after the line. */
8169 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8170 --curwin->w_cursor.col;
8171
8172 /* Decrease the cursor column until it's on a non-blank. */
8173 while (curwin->w_cursor.col > 0
8174 && vim_iswhite(ptr[curwin->w_cursor.col]))
8175 --curwin->w_cursor.col;
8176 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008177 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 break;
8180
8181 case '$':
8182 case K_END:
8183 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {
8185 int col_off = curwin_col_off();
8186
8187 oap->motion_type = MCHAR;
8188 oap->inclusive = TRUE;
8189 if (curwin->w_p_wrap
8190#ifdef FEAT_VERTSPLIT
8191 && curwin->w_width != 0
8192#endif
8193 )
8194 {
8195 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8196 if (cap->count1 == 1)
8197 {
8198 int width1 = W_WIDTH(curwin) - col_off;
8199 int width2 = width1 + curwin_col_off2();
8200
8201 validate_virtcol();
8202 i = width1 - 1;
8203 if (curwin->w_virtcol >= (colnr_T)width1)
8204 i += ((curwin->w_virtcol - width1) / width2 + 1)
8205 * width2;
8206 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008207
8208 /* Make sure we stick in this column. */
8209 validate_virtcol();
8210 curwin->w_curswant = curwin->w_virtcol;
8211 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8213 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8214 {
8215 /*
8216 * Check for landing on a character that got split at
8217 * the end of the line. We do not want to advance to
8218 * the next screen line.
8219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 if (curwin->w_virtcol > (colnr_T)i)
8221 --curwin->w_cursor.col;
8222 }
8223#endif
8224 }
8225 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8226 clearopbeep(oap);
8227 }
8228 else
8229 {
8230 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8231 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008232
8233 /* Make sure we stick in this column. */
8234 validate_virtcol();
8235 curwin->w_curswant = curwin->w_virtcol;
8236 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 }
8238 }
8239 break;
8240
8241 /*
8242 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8243 */
8244 case '*':
8245 case '#':
8246#if POUND != '#'
8247 case POUND: /* pound sign (sometimes equal to '#') */
8248#endif
8249 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8250 case ']': /* :tselect for current identifier */
8251 nv_ident(cap);
8252 break;
8253
8254 /*
8255 * ge and gE: go back to end of word
8256 */
8257 case 'e':
8258 case 'E':
8259 oap->motion_type = MCHAR;
8260 curwin->w_set_curswant = TRUE;
8261 oap->inclusive = TRUE;
8262 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8263 clearopbeep(oap);
8264 break;
8265
8266 /*
8267 * "g CTRL-G": display info about cursor position
8268 */
8269 case Ctrl_G:
8270 cursor_pos_info();
8271 break;
8272
8273 /*
8274 * "gi": start Insert at the last position.
8275 */
8276 case 'i':
8277 if (curbuf->b_last_insert.lnum != 0)
8278 {
8279 curwin->w_cursor = curbuf->b_last_insert;
8280 check_cursor_lnum();
8281 i = (int)STRLEN(ml_get_curline());
8282 if (curwin->w_cursor.col > (colnr_T)i)
8283 {
8284#ifdef FEAT_VIRTUALEDIT
8285 if (virtual_active())
8286 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8287#endif
8288 curwin->w_cursor.col = i;
8289 }
8290 }
8291 cap->cmdchar = 'i';
8292 nv_edit(cap);
8293 break;
8294
8295 /*
8296 * "gI": Start insert in column 1.
8297 */
8298 case 'I':
8299 beginline(0);
8300 if (!checkclearopq(oap))
8301 invoke_edit(cap, FALSE, 'g', FALSE);
8302 break;
8303
8304#ifdef FEAT_SEARCHPATH
8305 /*
8306 * "gf": goto file, edit file under cursor
8307 * "]f" and "[f": can also be used.
8308 */
8309 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008310 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 nv_gotofile(cap);
8312 break;
8313#endif
8314
8315 /* "g'm" and "g`m": jump to mark without setting pcmark */
8316 case '\'':
8317 cap->arg = TRUE;
8318 /*FALLTHROUGH*/
8319 case '`':
8320 nv_gomark(cap);
8321 break;
8322
8323 /*
8324 * "gs": Goto sleep.
8325 */
8326 case 's':
8327 do_sleep(cap->count1 * 1000L);
8328 break;
8329
8330 /*
8331 * "ga": Display the ascii value of the character under the
8332 * cursor. It is displayed in decimal, hex, and octal. -- webb
8333 */
8334 case 'a':
8335 do_ascii(NULL);
8336 break;
8337
8338#ifdef FEAT_MBYTE
8339 /*
8340 * "g8": Display the bytes used for the UTF-8 character under the
8341 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008342 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 */
8344 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008345 if (cap->count0 == 8)
8346 utf_find_illegal();
8347 else
8348 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 break;
8350#endif
8351
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008352 case '<':
8353 show_sb_text();
8354 break;
8355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 /*
8357 * "gg": Goto the first line in file. With a count it goes to
8358 * that line number like for "G". -- webb
8359 */
8360 case 'g':
8361 cap->arg = FALSE;
8362 nv_goto(cap);
8363 break;
8364
8365 /*
8366 * Two-character operators:
8367 * "gq" Format text
8368 * "gw" Format text and keep cursor position
8369 * "g~" Toggle the case of the text.
8370 * "gu" Change text to lower case.
8371 * "gU" Change text to upper case.
8372 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008373 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 */
8375 case 'q':
8376 case 'w':
8377 oap->cursor_start = curwin->w_cursor;
8378 /*FALLTHROUGH*/
8379 case '~':
8380 case 'u':
8381 case 'U':
8382 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008383 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 nv_operator(cap);
8385 break;
8386
8387 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008388 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 * current function
8390 * "gD": idem, but in the current file.
8391 */
8392 case 'd':
8393 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008394 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 break;
8396
8397#ifdef FEAT_MOUSE
8398 /*
8399 * g<*Mouse> : <C-*mouse>
8400 */
8401 case K_MIDDLEMOUSE:
8402 case K_MIDDLEDRAG:
8403 case K_MIDDLERELEASE:
8404 case K_LEFTMOUSE:
8405 case K_LEFTDRAG:
8406 case K_LEFTRELEASE:
8407 case K_RIGHTMOUSE:
8408 case K_RIGHTDRAG:
8409 case K_RIGHTRELEASE:
8410 case K_X1MOUSE:
8411 case K_X1DRAG:
8412 case K_X1RELEASE:
8413 case K_X2MOUSE:
8414 case K_X2DRAG:
8415 case K_X2RELEASE:
8416 mod_mask = MOD_MASK_CTRL;
8417 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8418 break;
8419#endif
8420
8421 case K_IGNORE:
8422 break;
8423
8424 /*
8425 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8426 */
8427 case 'p':
8428 case 'P':
8429 nv_put(cap);
8430 break;
8431
8432#ifdef FEAT_BYTEOFF
8433 /* "go": goto byte count from start of buffer */
8434 case 'o':
8435 goto_byte(cap->count0);
8436 break;
8437#endif
8438
8439 /* "gQ": improved Ex mode */
8440 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008441 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
8443 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008444 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 break;
8446 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (!checkclearopq(oap))
8449 do_exmode(TRUE);
8450 break;
8451
8452#ifdef FEAT_JUMPLIST
8453 case ',':
8454 nv_pcmark(cap);
8455 break;
8456
8457 case ';':
8458 cap->count1 = -cap->count1;
8459 nv_pcmark(cap);
8460 break;
8461#endif
8462
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008463#ifdef FEAT_WINDOWS
8464 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008465 if (!checkclearop(oap))
8466 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008467 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008468 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008469 if (!checkclearop(oap))
8470 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008471 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008472#endif
8473
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008474 case '+':
8475 case '-': /* "g+" and "g-": undo or redo along the timeline */
8476 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008477 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008478 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008479 break;
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 default:
8482 clearopbeep(oap);
8483 break;
8484 }
8485}
8486
8487/*
8488 * Handle "o" and "O" commands.
8489 */
8490 static void
8491n_opencmd(cap)
8492 cmdarg_T *cap;
8493{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008494#ifdef FEAT_CONCEAL
8495 linenr_T oldline = curwin->w_cursor.lnum;
8496#endif
8497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 if (!checkclearopq(cap->oap))
8499 {
8500#ifdef FEAT_FOLDING
8501 if (cap->cmdchar == 'O')
8502 /* Open above the first line of a folded sequence of lines */
8503 (void)hasFolding(curwin->w_cursor.lnum,
8504 &curwin->w_cursor.lnum, NULL);
8505 else
8506 /* Open below the last line of a folded sequence of lines */
8507 (void)hasFolding(curwin->w_cursor.lnum,
8508 NULL, &curwin->w_cursor.lnum);
8509#endif
8510 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8511 (cap->cmdchar == 'O' ? 1 : 0)),
8512 (linenr_T)(curwin->w_cursor.lnum +
8513 (cap->cmdchar == 'o' ? 1 : 0))
8514 ) == OK
8515 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8516#ifdef FEAT_COMMENTS
8517 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8518#endif
8519 0, 0))
8520 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008521#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008522 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008523 update_single_line(curwin, oldline);
8524#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008525 /* When '#' is in 'cpoptions' ignore the count. */
8526 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8527 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008528#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008529 if (curwin->w_p_cul)
8530 /* force redraw of cursorline */
8531 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8534 }
8535 }
8536}
8537
8538/*
8539 * "." command: redo last change.
8540 */
8541 static void
8542nv_dot(cap)
8543 cmdarg_T *cap;
8544{
8545 if (!checkclearopq(cap->oap))
8546 {
8547 /*
8548 * If "restart_edit" is TRUE, the last but one command is repeated
8549 * instead of the last command (inserting text). This is used for
8550 * CTRL-O <.> in insert mode.
8551 */
8552 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8553 clearopbeep(cap->oap);
8554 }
8555}
8556
8557/*
8558 * CTRL-R: undo undo
8559 */
8560 static void
8561nv_redo(cap)
8562 cmdarg_T *cap;
8563{
8564 if (!checkclearopq(cap->oap))
8565 {
8566 u_redo((int)cap->count1);
8567 curwin->w_set_curswant = TRUE;
8568 }
8569}
8570
8571/*
8572 * Handle "U" command.
8573 */
8574 static void
8575nv_Undo(cap)
8576 cmdarg_T *cap;
8577{
8578 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008579 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {
8581 /* translate "gUU" to "gUgU" */
8582 cap->cmdchar = 'g';
8583 cap->nchar = 'U';
8584 nv_operator(cap);
8585 }
8586 else if (!checkclearopq(cap->oap))
8587 {
8588 u_undoline();
8589 curwin->w_set_curswant = TRUE;
8590 }
8591}
8592
8593/*
8594 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8595 * single character.
8596 */
8597 static void
8598nv_tilde(cap)
8599 cmdarg_T *cap;
8600{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008601 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 n_swapchar(cap);
8603 else
8604 nv_operator(cap);
8605}
8606
8607/*
8608 * Handle an operator command.
8609 * The actual work is done by do_pending_operator().
8610 */
8611 static void
8612nv_operator(cap)
8613 cmdarg_T *cap;
8614{
8615 int op_type;
8616
8617 op_type = get_op_type(cap->cmdchar, cap->nchar);
8618
8619 if (op_type == cap->oap->op_type) /* double operator works on lines */
8620 nv_lineop(cap);
8621 else if (!checkclearop(cap->oap))
8622 {
8623 cap->oap->start = curwin->w_cursor;
8624 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008625#ifdef FEAT_EVAL
8626 set_op_var(op_type);
8627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 }
8629}
8630
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008631#ifdef FEAT_EVAL
8632/*
8633 * Set v:operator to the characters for "optype".
8634 */
8635 static void
8636set_op_var(optype)
8637 int optype;
8638{
8639 char_u opchars[3];
8640
8641 if (optype == OP_NOP)
8642 set_vim_var_string(VV_OP, NULL, 0);
8643 else
8644 {
8645 opchars[0] = get_op_char(optype);
8646 opchars[1] = get_extra_op_char(optype);
8647 opchars[2] = NUL;
8648 set_vim_var_string(VV_OP, opchars, -1);
8649 }
8650}
8651#endif
8652
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653/*
8654 * Handle linewise operator "dd", "yy", etc.
8655 *
8656 * "_" is is a strange motion command that helps make operators more logical.
8657 * It is actually implemented, but not documented in the real Vi. This motion
8658 * command actually refers to "the current line". Commands like "dd" and "yy"
8659 * are really an alternate form of "d_" and "y_". It does accept a count, so
8660 * "d3_" works to delete 3 lines.
8661 */
8662 static void
8663nv_lineop(cap)
8664 cmdarg_T *cap;
8665{
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
8683nv_home(cap)
8684 cmdarg_T *cap;
8685{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008686 /* CTRL-HOME is like "gg" */
8687 if (mod_mask & MOD_MASK_CTRL)
8688 nv_goto(cap);
8689 else
8690 {
8691 cap->count0 = 1;
8692 nv_pipe(cap);
8693 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008694 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8695 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
8697
8698/*
8699 * "|" command.
8700 */
8701 static void
8702nv_pipe(cap)
8703 cmdarg_T *cap;
8704{
8705 cap->oap->motion_type = MCHAR;
8706 cap->oap->inclusive = FALSE;
8707 beginline(0);
8708 if (cap->count0 > 0)
8709 {
8710 coladvance((colnr_T)(cap->count0 - 1));
8711 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8712 }
8713 else
8714 curwin->w_curswant = 0;
8715 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008716 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 curwin->w_set_curswant = FALSE;
8718}
8719
8720/*
8721 * Handle back-word command "b" and "B".
8722 * cap->arg is 1 for "B"
8723 */
8724 static void
8725nv_bck_word(cap)
8726 cmdarg_T *cap;
8727{
8728 cap->oap->motion_type = MCHAR;
8729 cap->oap->inclusive = FALSE;
8730 curwin->w_set_curswant = TRUE;
8731 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8732 clearopbeep(cap->oap);
8733#ifdef FEAT_FOLDING
8734 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8735 foldOpenCursor();
8736#endif
8737}
8738
8739/*
8740 * Handle word motion commands "e", "E", "w" and "W".
8741 * cap->arg is TRUE for "E" and "W".
8742 */
8743 static void
8744nv_wordcmd(cap)
8745 cmdarg_T *cap;
8746{
8747 int n;
8748 int word_end;
8749 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008750 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751
8752 /*
8753 * Set inclusive for the "E" and "e" command.
8754 */
8755 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8756 word_end = TRUE;
8757 else
8758 word_end = FALSE;
8759 cap->oap->inclusive = word_end;
8760
8761 /*
8762 * "cw" and "cW" are a special case.
8763 */
8764 if (!word_end && cap->oap->op_type == OP_CHANGE)
8765 {
8766 n = gchar_cursor();
8767 if (n != NUL) /* not an empty line */
8768 {
8769 if (vim_iswhite(n))
8770 {
8771 /*
8772 * Reproduce a funny Vi behaviour: "cw" on a blank only
8773 * changes one character, not all blanks until the start of
8774 * the next word. Only do this when the 'w' flag is included
8775 * in 'cpoptions'.
8776 */
8777 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8778 {
8779 cap->oap->inclusive = TRUE;
8780 cap->oap->motion_type = MCHAR;
8781 return;
8782 }
8783 }
8784 else
8785 {
8786 /*
8787 * This is a little strange. To match what the real Vi does,
8788 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8789 * that we are not on a space or a TAB. This seems impolite
8790 * at first, but it's really more what we mean when we say
8791 * 'cw'.
8792 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008793 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 * will change only one character! This is done by setting
8795 * flag.
8796 */
8797 cap->oap->inclusive = TRUE;
8798 word_end = TRUE;
8799 flag = TRUE;
8800 }
8801 }
8802 }
8803
8804 cap->oap->motion_type = MCHAR;
8805 curwin->w_set_curswant = TRUE;
8806 if (word_end)
8807 n = end_word(cap->count1, cap->arg, flag, FALSE);
8808 else
8809 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8810
Bram Moolenaardfefb982008-04-01 10:06:39 +00008811 /* Don't leave the cursor on the NUL past the end of line. Unless we
8812 * didn't move it forward. */
8813 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008814 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
8816 if (n == FAIL && cap->oap->op_type == OP_NOP)
8817 clearopbeep(cap->oap);
8818 else
8819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#ifdef FEAT_FOLDING
8822 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8823 foldOpenCursor();
8824#endif
8825 }
8826}
8827
8828/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008829 * Used after a movement command: If the cursor ends up on the NUL after the
8830 * end of the line, may move it back to the last character and make the motion
8831 * inclusive.
8832 */
8833 static void
8834adjust_cursor(oap)
8835 oparg_T *oap;
8836{
8837 /* The cursor cannot remain on the NUL when:
8838 * - the column is > 0
8839 * - not in Visual mode or 'selection' is "o"
8840 * - 'virtualedit' is not "all" and not "onemore".
8841 */
8842 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008843 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008844#ifdef FEAT_VIRTUALEDIT
8845 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8846#endif
8847 )
8848 {
8849 --curwin->w_cursor.col;
8850#ifdef FEAT_MBYTE
8851 /* prevent cursor from moving on the trail byte */
8852 if (has_mbyte)
8853 mb_adjust_cursor();
8854#endif
8855 oap->inclusive = TRUE;
8856 }
8857}
8858
8859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 * "0" and "^" commands.
8861 * cap->arg is the argument for beginline().
8862 */
8863 static void
8864nv_beginline(cap)
8865 cmdarg_T *cap;
8866{
8867 cap->oap->motion_type = MCHAR;
8868 cap->oap->inclusive = FALSE;
8869 beginline(cap->arg);
8870#ifdef FEAT_FOLDING
8871 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8872 foldOpenCursor();
8873#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008874 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8875 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876}
8877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878/*
8879 * In exclusive Visual mode, may include the last character.
8880 */
8881 static void
8882adjust_for_sel(cap)
8883 cmdarg_T *cap;
8884{
8885 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8886 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8887 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008888#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (has_mbyte)
8890 inc_cursor();
8891 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 ++curwin->w_cursor.col;
8894 cap->oap->inclusive = FALSE;
8895 }
8896}
8897
8898/*
8899 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8900 * Should check VIsual_mode before calling this.
8901 * Returns TRUE when backed up to the previous line.
8902 */
8903 static int
8904unadjust_for_sel()
8905{
8906 pos_T *pp;
8907
8908 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8909 {
8910 if (lt(VIsual, curwin->w_cursor))
8911 pp = &curwin->w_cursor;
8912 else
8913 pp = &VIsual;
8914#ifdef FEAT_VIRTUALEDIT
8915 if (pp->coladd > 0)
8916 --pp->coladd;
8917 else
8918#endif
8919 if (pp->col > 0)
8920 {
8921 --pp->col;
8922#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008923 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#endif
8925 }
8926 else if (pp->lnum > 1)
8927 {
8928 --pp->lnum;
8929 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8930 return TRUE;
8931 }
8932 }
8933 return FALSE;
8934}
8935
8936/*
8937 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8938 */
8939 static void
8940nv_select(cap)
8941 cmdarg_T *cap;
8942{
8943 if (VIsual_active)
8944 VIsual_select = TRUE;
8945 else if (VIsual_reselect)
8946 {
8947 cap->nchar = 'v'; /* fake "gv" command */
8948 cap->arg = TRUE;
8949 nv_g_cmd(cap);
8950 }
8951}
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954/*
8955 * "G", "gg", CTRL-END, CTRL-HOME.
8956 * cap->arg is TRUE for "G".
8957 */
8958 static void
8959nv_goto(cap)
8960 cmdarg_T *cap;
8961{
8962 linenr_T lnum;
8963
8964 if (cap->arg)
8965 lnum = curbuf->b_ml.ml_line_count;
8966 else
8967 lnum = 1L;
8968 cap->oap->motion_type = MLINE;
8969 setpcmark();
8970
8971 /* When a count is given, use it instead of the default lnum */
8972 if (cap->count0 != 0)
8973 lnum = cap->count0;
8974 if (lnum < 1L)
8975 lnum = 1L;
8976 else if (lnum > curbuf->b_ml.ml_line_count)
8977 lnum = curbuf->b_ml.ml_line_count;
8978 curwin->w_cursor.lnum = lnum;
8979 beginline(BL_SOL | BL_FIX);
8980#ifdef FEAT_FOLDING
8981 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8982 foldOpenCursor();
8983#endif
8984}
8985
8986/*
8987 * CTRL-\ in Normal mode.
8988 */
8989 static void
8990nv_normal(cap)
8991 cmdarg_T *cap;
8992{
8993 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8994 {
8995 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008996 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 clear_cmdline = TRUE; /* unshow mode later */
8998 restart_edit = 0;
8999#ifdef FEAT_CMDWIN
9000 if (cmdwin_type != 0)
9001 cmdwin_result = Ctrl_C;
9002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 if (VIsual_active)
9004 {
9005 end_visual_mode(); /* stop Visual */
9006 redraw_curbuf_later(INVERTED);
9007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9009 if (cap->nchar == Ctrl_G && p_im)
9010 restart_edit = 'a';
9011 }
9012 else
9013 clearopbeep(cap->oap);
9014}
9015
9016/*
9017 * ESC in Normal mode: beep, but don't flush buffers.
9018 * Don't even beep if we are canceling a command.
9019 */
9020 static void
9021nv_esc(cap)
9022 cmdarg_T *cap;
9023{
9024 int no_reason;
9025
9026 no_reason = (cap->oap->op_type == OP_NOP
9027 && cap->opcount == 0
9028 && cap->count0 == 0
9029 && cap->oap->regname == 0
9030 && !p_im);
9031
9032 if (cap->arg) /* TRUE for CTRL-C */
9033 {
9034 if (restart_edit == 0
9035#ifdef FEAT_CMDWIN
9036 && cmdwin_type == 0
9037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 && no_reason)
9040 MSG(_("Type :quit<Enter> to exit Vim"));
9041
9042 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9043 * set again below when halfway a mapping. */
9044 if (!p_im)
9045 restart_edit = 0;
9046#ifdef FEAT_CMDWIN
9047 if (cmdwin_type != 0)
9048 {
9049 cmdwin_result = K_IGNORE;
9050 got_int = FALSE; /* don't stop executing autocommands et al. */
9051 return;
9052 }
9053#endif
9054 }
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 if (VIsual_active)
9057 {
9058 end_visual_mode(); /* stop Visual */
9059 check_cursor_col(); /* make sure cursor is not beyond EOL */
9060 curwin->w_set_curswant = TRUE;
9061 redraw_curbuf_later(INVERTED);
9062 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009063 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009064 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 clearop(cap->oap);
9066
9067 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9068 * set return to Insert mode afterwards. */
9069 if (restart_edit == 0 && goto_im()
9070#ifdef FEAT_EX_EXTRA
9071 && ex_normal_busy == 0
9072#endif
9073 )
9074 restart_edit = 'a';
9075}
9076
9077/*
9078 * Handle "A", "a", "I", "i" and <Insert> commands.
9079 */
9080 static void
9081nv_edit(cap)
9082 cmdarg_T *cap;
9083{
9084 /* <Insert> is equal to "i" */
9085 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9086 cap->cmdchar = 'i';
9087
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 /* in Visual mode "A" and "I" are an operator */
9089 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9090 v_visop(cap);
9091
9092 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009093 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9094 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
9096#ifdef FEAT_TEXTOBJ
9097 nv_object(cap);
9098#else
9099 clearopbeep(cap->oap);
9100#endif
9101 }
9102 else if (!curbuf->b_p_ma && !p_im)
9103 {
9104 /* Only give this error when 'insertmode' is off. */
9105 EMSG(_(e_modifiable));
9106 clearop(cap->oap);
9107 }
9108 else if (!checkclearopq(cap->oap))
9109 {
9110 switch (cap->cmdchar)
9111 {
9112 case 'A': /* "A"ppend after the line */
9113 curwin->w_set_curswant = TRUE;
9114#ifdef FEAT_VIRTUALEDIT
9115 if (ve_flags == VE_ALL)
9116 {
9117 int save_State = State;
9118
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009119 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 * character past the end of the line */
9121 State = INSERT;
9122 coladvance((colnr_T)MAXCOL);
9123 State = save_State;
9124 }
9125 else
9126#endif
9127 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9128 break;
9129
9130 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009131 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9132 beginline(BL_WHITE);
9133 else
9134 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 break;
9136
9137 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9138#ifdef FEAT_VIRTUALEDIT
9139 /* increment coladd when in virtual space, increment the
9140 * column otherwise, also to append after an unprintable char */
9141 if (virtual_active()
9142 && (curwin->w_cursor.coladd > 0
9143 || *ml_get_cursor() == NUL
9144 || *ml_get_cursor() == TAB))
9145 curwin->w_cursor.coladd++;
9146 else
9147#endif
9148 if (*ml_get_cursor() != NUL)
9149 inc_cursor();
9150 break;
9151 }
9152
9153#ifdef FEAT_VIRTUALEDIT
9154 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9155 {
9156 int save_State = State;
9157
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009158 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 * character past the end of the line */
9160 State = INSERT;
9161 coladvance(getviscol());
9162 State = save_State;
9163 }
9164#endif
9165
9166 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9167 }
9168}
9169
9170/*
9171 * Invoke edit() and take care of "restart_edit" and the return value.
9172 */
9173 static void
9174invoke_edit(cap, repl, cmd, startln)
9175 cmdarg_T *cap;
9176 int repl; /* "r" or "gr" command */
9177 int cmd;
9178 int startln;
9179{
9180 int restart_edit_save = 0;
9181
9182 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9183 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9184 * it. */
9185 if (repl || !stuff_empty())
9186 restart_edit_save = restart_edit;
9187 else
9188 restart_edit_save = 0;
9189
9190 /* Always reset "restart_edit", this is not a restarted edit. */
9191 restart_edit = 0;
9192
9193 if (edit(cmd, startln, cap->count1))
9194 cap->retval |= CA_COMMAND_BUSY;
9195
9196 if (restart_edit == 0)
9197 restart_edit = restart_edit_save;
9198}
9199
9200#ifdef FEAT_TEXTOBJ
9201/*
9202 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9203 */
9204 static void
9205nv_object(cap)
9206 cmdarg_T *cap;
9207{
9208 int flag;
9209 int include;
9210 char_u *mps_save;
9211
9212 if (cap->cmdchar == 'i')
9213 include = FALSE; /* "ix" = inner object: exclude white space */
9214 else
9215 include = TRUE; /* "ax" = an object: include white space */
9216
9217 /* Make sure (), [], {} and <> are in 'matchpairs' */
9218 mps_save = curbuf->b_p_mps;
9219 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9220
9221 switch (cap->nchar)
9222 {
9223 case 'w': /* "aw" = a word */
9224 flag = current_word(cap->oap, cap->count1, include, FALSE);
9225 break;
9226 case 'W': /* "aW" = a WORD */
9227 flag = current_word(cap->oap, cap->count1, include, TRUE);
9228 break;
9229 case 'b': /* "ab" = a braces block */
9230 case '(':
9231 case ')':
9232 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9233 break;
9234 case 'B': /* "aB" = a Brackets block */
9235 case '{':
9236 case '}':
9237 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9238 break;
9239 case '[': /* "a[" = a [] block */
9240 case ']':
9241 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9242 break;
9243 case '<': /* "a<" = a <> block */
9244 case '>':
9245 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9246 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009247 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009248 /* Do not adjust oap->end in do_pending_operator()
9249 * otherwise there are different results for 'dit'
9250 * (note leading whitespace in last line):
9251 * 1) <b> 2) <b>
9252 * foobar foobar
9253 * </b> </b>
9254 */
9255 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009256 flag = current_tagblock(cap->oap, cap->count1, include);
9257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 case 'p': /* "ap" = a paragraph */
9259 flag = current_par(cap->oap, cap->count1, include, 'p');
9260 break;
9261 case 's': /* "as" = a sentence */
9262 flag = current_sent(cap->oap, cap->count1, include);
9263 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009264 case '"': /* "a"" = a double quoted string */
9265 case '\'': /* "a'" = a single quoted string */
9266 case '`': /* "a`" = a backtick quoted string */
9267 flag = current_quote(cap->oap, cap->count1, include,
9268 cap->nchar);
9269 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270#if 0 /* TODO */
9271 case 'S': /* "aS" = a section */
9272 case 'f': /* "af" = a filename */
9273 case 'u': /* "au" = a URL */
9274#endif
9275 default:
9276 flag = FAIL;
9277 break;
9278 }
9279
9280 curbuf->b_p_mps = mps_save;
9281 if (flag == FAIL)
9282 clearopbeep(cap->oap);
9283 adjust_cursor_col();
9284 curwin->w_set_curswant = TRUE;
9285}
9286#endif
9287
9288/*
9289 * "q" command: Start/stop recording.
9290 * "q:", "q/", "q?": edit command-line in command-line window.
9291 */
9292 static void
9293nv_record(cap)
9294 cmdarg_T *cap;
9295{
9296 if (cap->oap->op_type == OP_FORMAT)
9297 {
9298 /* "gqq" is the same as "gqgq": format line */
9299 cap->cmdchar = 'g';
9300 cap->nchar = 'q';
9301 nv_operator(cap);
9302 }
9303 else if (!checkclearop(cap->oap))
9304 {
9305#ifdef FEAT_CMDWIN
9306 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9307 {
9308 stuffcharReadbuff(cap->nchar);
9309 stuffcharReadbuff(K_CMDWIN);
9310 }
9311 else
9312#endif
9313 /* (stop) recording into a named register, unless executing a
9314 * register */
9315 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9316 clearopbeep(cap->oap);
9317 }
9318}
9319
9320/*
9321 * Handle the "@r" command.
9322 */
9323 static void
9324nv_at(cap)
9325 cmdarg_T *cap;
9326{
9327 if (checkclearop(cap->oap))
9328 return;
9329#ifdef FEAT_EVAL
9330 if (cap->nchar == '=')
9331 {
9332 if (get_expr_register() == NUL)
9333 return;
9334 }
9335#endif
9336 while (cap->count1-- && !got_int)
9337 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009338 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 {
9340 clearopbeep(cap->oap);
9341 break;
9342 }
9343 line_breakcheck();
9344 }
9345}
9346
9347/*
9348 * Handle the CTRL-U and CTRL-D commands.
9349 */
9350 static void
9351nv_halfpage(cap)
9352 cmdarg_T *cap;
9353{
9354 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9355 || (cap->cmdchar == Ctrl_D
9356 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9357 clearopbeep(cap->oap);
9358 else if (!checkclearop(cap->oap))
9359 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9360}
9361
9362/*
9363 * Handle "J" or "gJ" command.
9364 */
9365 static void
9366nv_join(cap)
9367 cmdarg_T *cap;
9368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 if (VIsual_active) /* join the visual lines */
9370 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009371 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
9373 if (cap->count0 <= 1)
9374 cap->count0 = 2; /* default for join is two lines! */
9375 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9376 curbuf->b_ml.ml_line_count)
9377 clearopbeep(cap->oap); /* beyond last line */
9378 else
9379 {
9380 prep_redo(cap->oap->regname, cap->count0,
9381 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009382 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 }
9384 }
9385}
9386
9387/*
9388 * "P", "gP", "p" and "gp" commands.
9389 */
9390 static void
9391nv_put(cap)
9392 cmdarg_T *cap;
9393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 int regname = 0;
9395 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009396 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009397 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 int dir;
9399 int flags = 0;
9400
9401 if (cap->oap->op_type != OP_NOP)
9402 {
9403#ifdef FEAT_DIFF
9404 /* "dp" is ":diffput" */
9405 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9406 {
9407 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009408 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
9410 else
9411#endif
9412 clearopbeep(cap->oap);
9413 }
9414 else
9415 {
9416 dir = (cap->cmdchar == 'P'
9417 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9418 ? BACKWARD : FORWARD;
9419 prep_redo_cmd(cap);
9420 if (cap->cmdchar == 'g')
9421 flags |= PUT_CURSEND;
9422
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 if (VIsual_active)
9424 {
9425 /* Putting in Visual mode: The put text replaces the selected
9426 * text. First delete the selected text, then put the new text.
9427 * Need to save and restore the registers that the delete
9428 * overwrites if the old contents is being put.
9429 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009430 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009432#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009434#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009435 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009436 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009437#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440
9441 )
9442 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009443 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 * put, save it first. */
9445 reg1 = get_register(regname, TRUE);
9446 }
9447
9448 /* Now delete the selected text. */
9449 cap->cmdchar = 'd';
9450 cap->nchar = NUL;
9451 cap->oap->regname = NUL;
9452 nv_operator(cap);
9453 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009454 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455
9456 /* delete PUT_LINE_BACKWARD; */
9457 cap->oap->regname = regname;
9458
9459 if (reg1 != NULL)
9460 {
9461 /* Delete probably changed the register we want to put, save
9462 * it first. Then put back what was there before the delete. */
9463 reg2 = get_register(regname, FALSE);
9464 put_register(regname, reg1);
9465 }
9466
9467 /* When deleted a linewise Visual area, put the register as
9468 * lines to avoid it joined with the next line. When deletion was
9469 * characterwise, split a line when putting lines. */
9470 if (VIsual_mode == 'V')
9471 flags |= PUT_LINE;
9472 else if (VIsual_mode == 'v')
9473 flags |= PUT_LINE_SPLIT;
9474 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9475 flags |= PUT_LINE_FORWARD;
9476 dir = BACKWARD;
9477 if ((VIsual_mode != 'V'
9478 && curwin->w_cursor.col < curbuf->b_op_start.col)
9479 || (VIsual_mode == 'V'
9480 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9481 /* cursor is at the end of the line or end of file, put
9482 * forward. */
9483 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009484 /* May have been reset in do_put(). */
9485 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 do_put(cap->oap->regname, dir, cap->count1, flags);
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 /* If a register was saved, put it back now. */
9490 if (reg2 != NULL)
9491 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009492
9493 /* What to reselect with "gv"? Selecting the just put text seems to
9494 * be the most useful, since the original text was removed. */
9495 if (was_visual)
9496 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009497 curbuf->b_visual.vi_start = curbuf->b_op_start;
9498 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009499 /* need to adjust cursor position */
9500 if (*p_sel == 'e')
9501 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009502 }
9503
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009504 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009505 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009506 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009507 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009508 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009509
9510 /* If the cursor was in that line, move it to the end of the last
9511 * line. */
9512 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9513 {
9514 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9515 coladvance((colnr_T)MAXCOL);
9516 }
9517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 auto_format(FALSE, TRUE);
9519 }
9520}
9521
9522/*
9523 * "o" and "O" commands.
9524 */
9525 static void
9526nv_open(cap)
9527 cmdarg_T *cap;
9528{
9529#ifdef FEAT_DIFF
9530 /* "do" is ":diffget" */
9531 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9532 {
9533 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009534 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 }
9536 else
9537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 if (VIsual_active) /* switch start and end of visual */
9539 v_swap_corners(cap->cmdchar);
9540 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 n_opencmd(cap);
9542}
9543
9544#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 static void
9546nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009547 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 ProcessSniffRequests();
9550}
9551#endif
9552
9553#ifdef FEAT_NETBEANS_INTG
9554 static void
9555nv_nbcmd(cap)
9556 cmdarg_T *cap;
9557{
9558 netbeans_keycommand(cap->nchar);
9559}
9560#endif
9561
9562#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 static void
9564nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009565 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9568}
9569#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009570
9571#ifdef FEAT_AUTOCMD
9572/*
9573 * Trigger CursorHold event.
9574 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9575 * input buffer. "did_cursorhold" is set to avoid retriggering.
9576 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009577 static void
9578nv_cursorhold(cap)
9579 cmdarg_T *cap;
9580{
9581 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9582 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009583 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009584}
9585#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009586
9587/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009588 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009589 */
9590 static void
9591get_op_vcol(oap, redo_VIsual_vcol, initial)
9592 oparg_T *oap;
9593 colnr_T redo_VIsual_vcol;
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009594 int initial; /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009595{
9596 colnr_T start, end;
9597
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009598 if (VIsual_mode != Ctrl_V
9599 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009600 return;
9601
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009602 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009603
9604#ifdef FEAT_MBYTE
9605 /* prevent from moving onto a trail byte */
9606 if (has_mbyte)
9607 mb_adjustpos(curwin->w_buffer, &oap->end);
9608#endif
9609
9610 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009611
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009612 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009613 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009614 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9615
9616 if (start < oap->start_vcol)
9617 oap->start_vcol = start;
9618 if (end > oap->end_vcol)
9619 {
9620 if (initial && *p_sel == 'e' && start >= 1
9621 && start - 1 >= oap->end_vcol)
9622 oap->end_vcol = start - 1;
9623 else
9624 oap->end_vcol = end;
9625 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009626 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009627
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009628 /* if '$' was used, get oap->end_vcol from longest line */
9629 if (curwin->w_curswant == MAXCOL)
9630 {
9631 curwin->w_cursor.col = MAXCOL;
9632 oap->end_vcol = 0;
9633 for (curwin->w_cursor.lnum = oap->start.lnum;
9634 curwin->w_cursor.lnum <= oap->end.lnum;
9635 ++curwin->w_cursor.lnum)
9636 {
9637 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9638 if (end > oap->end_vcol)
9639 oap->end_vcol = end;
9640 }
9641 }
9642 else if (redo_VIsual_busy)
9643 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9644 /*
9645 * Correct oap->end.col and oap->start.col to be the
9646 * upper-left and lower-right corner of the block area.
9647 *
9648 * (Actually, this does convert column positions into character
9649 * positions)
9650 */
9651 curwin->w_cursor.lnum = oap->end.lnum;
9652 coladvance(oap->end_vcol);
9653 oap->end = curwin->w_cursor;
9654
9655 curwin->w_cursor = oap->start;
9656 coladvance(oap->start_vcol);
9657 oap->start = curwin->w_cursor;
9658}