blob: a46cc9d641a1ba73183c93d80c1df45ddc53e43b [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];
2564 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2565 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002566 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002567 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569 /* click in a tab selects that tab page */
2570 if (is_click
2571# ifdef FEAT_CMDWIN
2572 && cmdwin_type == 0
2573# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002574 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002576 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002577 c1 = TabPageIdxs[mouse_col];
2578 if (c1 >= 0)
2579 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002580 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2581 {
2582 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002584 tabpage_new();
2585 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2586 }
2587 else
2588 {
2589 /* Go to specified tab page, or next one if not clicking
2590 * on a label. */
2591 goto_tabpage(c1);
2592
2593 /* It's like clicking on the status line of a window. */
2594 if (curwin != old_curwin)
2595 end_visual_mode();
2596 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002597 }
2598 else if (c1 < 0)
2599 {
2600 tabpage_T *tp;
2601
2602 /* Close the current or specified tab page. */
2603 if (c1 == -999)
2604 tp = curtab;
2605 else
2606 tp = find_tabpage(-c1);
2607 if (tp == curtab)
2608 {
2609 if (first_tabpage->tp_next != NULL)
2610 tabpage_close(FALSE);
2611 }
2612 else if (tp != NULL)
2613 tabpage_close_other(tp, FALSE);
2614 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002615 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002616 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002618 else if (is_drag && in_tab_line)
2619 {
2620 c1 = TabPageIdxs[mouse_col];
2621 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2622 return FALSE;
2623 }
2624
Bram Moolenaarf740b292006-02-16 22:11:02 +00002625#endif
2626
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 /*
2628 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2629 * right button up -> pop-up menu
2630 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002631 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
2633 if (mouse_model_popup())
2634 {
2635 if (which_button == MOUSE_RIGHT
2636 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2637 {
2638 /*
2639 * NOTE: Ignore right button down and drag mouse events.
2640 * Windows only shows the popup menu on the button up event.
2641 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002642#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2643 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (!is_click)
2645 return FALSE;
2646#endif
2647#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2648 if (is_click || is_drag)
2649 return FALSE;
2650#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002651#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2653 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2654 if (gui.in_use)
2655 {
2656 jump_flags = 0;
2657 if (STRCMP(p_mousem, "popup_setpos") == 0)
2658 {
2659 /* First set the cursor position before showing the popup
2660 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (VIsual_active)
2662 {
2663 pos_T m_pos;
2664
2665 /*
2666 * set MOUSE_MAY_STOP_VIS if we are outside the
2667 * selection or the current window (might have false
2668 * negative here)
2669 */
2670 if (mouse_row < W_WINROW(curwin)
2671 || mouse_row
2672 > (W_WINROW(curwin) + curwin->w_height))
2673 jump_flags = MOUSE_MAY_STOP_VIS;
2674 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2675 jump_flags = MOUSE_MAY_STOP_VIS;
2676 else
2677 {
2678 if ((lt(curwin->w_cursor, VIsual)
2679 && (lt(m_pos, curwin->w_cursor)
2680 || lt(VIsual, m_pos)))
2681 || (lt(VIsual, curwin->w_cursor)
2682 && (lt(m_pos, VIsual)
2683 || lt(curwin->w_cursor, m_pos))))
2684 {
2685 jump_flags = MOUSE_MAY_STOP_VIS;
2686 }
2687 else if (VIsual_mode == Ctrl_V)
2688 {
2689 getvcols(curwin, &curwin->w_cursor, &VIsual,
2690 &leftcol, &rightcol);
2691 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2692 if (m_pos.col < leftcol || m_pos.col > rightcol)
2693 jump_flags = MOUSE_MAY_STOP_VIS;
2694 }
2695 }
2696 }
2697 else
2698 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 if (jump_flags)
2701 {
2702 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002703 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 setcursor();
2705 out_flush(); /* Update before showing popup menu */
2706 }
2707# ifdef FEAT_MENU
2708 gui_show_popupmenu();
2709# endif
2710 return (jump_flags & CURSOR_MOVED) != 0;
2711 }
2712 else
2713 return FALSE;
2714#else
2715 return FALSE;
2716#endif
2717 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002718 if (which_button == MOUSE_LEFT
2719 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 which_button = MOUSE_RIGHT;
2722 mod_mask &= ~MOD_MASK_SHIFT;
2723 }
2724 }
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if ((State & (NORMAL | INSERT))
2727 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2728 {
2729 if (which_button == MOUSE_LEFT)
2730 {
2731 if (is_click)
2732 {
2733 /* stop Visual mode for a left click in a window, but not when
2734 * on a status line */
2735 if (VIsual_active)
2736 jump_flags |= MOUSE_MAY_STOP_VIS;
2737 }
2738 else if (mouse_has(MOUSE_VISUAL))
2739 jump_flags |= MOUSE_MAY_VIS;
2740 }
2741 else if (which_button == MOUSE_RIGHT)
2742 {
2743 if (is_click && VIsual_active)
2744 {
2745 /*
2746 * Remember the start and end of visual before moving the
2747 * cursor.
2748 */
2749 if (lt(curwin->w_cursor, VIsual))
2750 {
2751 start_visual = curwin->w_cursor;
2752 end_visual = VIsual;
2753 }
2754 else
2755 {
2756 start_visual = VIsual;
2757 end_visual = curwin->w_cursor;
2758 }
2759 }
2760 jump_flags |= MOUSE_FOCUS;
2761 if (mouse_has(MOUSE_VISUAL))
2762 jump_flags |= MOUSE_MAY_VIS;
2763 }
2764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766 /*
2767 * If an operator is pending, ignore all drags and releases until the
2768 * next mouse click.
2769 */
2770 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2771 {
2772 got_click = FALSE;
2773 oap->motion_type = MCHAR;
2774 }
2775
2776 /* When releasing the button let jump_to_mouse() know. */
2777 if (!is_click && !is_drag)
2778 jump_flags |= MOUSE_RELEASED;
2779
2780 /*
2781 * JUMP!
2782 */
2783 jump_flags = jump_to_mouse(jump_flags,
2784 oap == NULL ? NULL : &(oap->inclusive), which_button);
2785 moved = (jump_flags & CURSOR_MOVED);
2786 in_status_line = (jump_flags & IN_STATUS_LINE);
2787#ifdef FEAT_VERTSPLIT
2788 in_sep_line = (jump_flags & IN_SEP_LINE);
2789#endif
2790
2791#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002792 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2794 {
2795 int key = KEY2TERMCAP1(c);
2796
2797 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2798 || key == (int)KE_RIGHTRELEASE)
2799 netbeans_button_release(which_button);
2800 }
2801#endif
2802
2803 /* When jumping to another window, clear a pending operator. That's a bit
2804 * friendlier than beeping and not jumping to that window. */
2805 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2806 clearop(oap);
2807
2808#ifdef FEAT_FOLDING
2809 if (mod_mask == 0
2810 && !is_drag
2811 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2812 && which_button == MOUSE_LEFT)
2813 {
2814 /* open or close a fold at this line */
2815 if (jump_flags & MOUSE_FOLD_OPEN)
2816 openFold(curwin->w_cursor.lnum, 1L);
2817 else
2818 closeFold(curwin->w_cursor.lnum, 1L);
2819 /* don't move the cursor if still in the same window */
2820 if (curwin == old_curwin)
2821 curwin->w_cursor = save_cursor;
2822 }
2823#endif
2824
2825#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2826 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2827 {
2828 clip_modeless(which_button, is_click, is_drag);
2829 return FALSE;
2830 }
2831#endif
2832
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002834 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (VIsual_active && is_drag && p_so)
2836 {
2837 /* In the very first line, allow scrolling one line */
2838 if (mouse_row == 0)
2839 mouse_dragging = 2;
2840 else
2841 mouse_dragging = 1;
2842 }
2843
2844 /* When dragging the mouse above the window, scroll down. */
2845 if (is_drag && mouse_row < 0 && !in_status_line)
2846 {
2847 scroll_redraw(FALSE, 1L);
2848 mouse_row = 0;
2849 }
2850
2851 if (start_visual.lnum) /* right click in visual mode */
2852 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002853 /* When ALT is pressed make Visual mode blockwise. */
2854 if (mod_mask & MOD_MASK_ALT)
2855 VIsual_mode = Ctrl_V;
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 /*
2858 * In Visual-block mode, divide the area in four, pick up the corner
2859 * that is in the quarter that the cursor is in.
2860 */
2861 if (VIsual_mode == Ctrl_V)
2862 {
2863 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2864 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2865 end_visual.col = leftcol;
2866 else
2867 end_visual.col = rightcol;
2868 if (curwin->w_cursor.lnum <
2869 (start_visual.lnum + end_visual.lnum) / 2)
2870 end_visual.lnum = end_visual.lnum;
2871 else
2872 end_visual.lnum = start_visual.lnum;
2873
2874 /* move VIsual to the right column */
2875 start_visual = curwin->w_cursor; /* save the cursor pos */
2876 curwin->w_cursor = end_visual;
2877 coladvance(end_visual.col);
2878 VIsual = curwin->w_cursor;
2879 curwin->w_cursor = start_visual; /* restore the cursor */
2880 }
2881 else
2882 {
2883 /*
2884 * If the click is before the start of visual, change the start.
2885 * If the click is after the end of visual, change the end. If
2886 * the click is inside the visual, change the closest side.
2887 */
2888 if (lt(curwin->w_cursor, start_visual))
2889 VIsual = end_visual;
2890 else if (lt(end_visual, curwin->w_cursor))
2891 VIsual = start_visual;
2892 else
2893 {
2894 /* In the same line, compare column number */
2895 if (end_visual.lnum == start_visual.lnum)
2896 {
2897 if (curwin->w_cursor.col - start_visual.col >
2898 end_visual.col - curwin->w_cursor.col)
2899 VIsual = start_visual;
2900 else
2901 VIsual = end_visual;
2902 }
2903
2904 /* In different lines, compare line number */
2905 else
2906 {
2907 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2908 (end_visual.lnum - curwin->w_cursor.lnum);
2909
2910 if (diff > 0) /* closest to end */
2911 VIsual = start_visual;
2912 else if (diff < 0) /* closest to start */
2913 VIsual = end_visual;
2914 else /* in the middle line */
2915 {
2916 if (curwin->w_cursor.col <
2917 (start_visual.col + end_visual.col) / 2)
2918 VIsual = end_visual;
2919 else
2920 VIsual = start_visual;
2921 }
2922 }
2923 }
2924 }
2925 }
2926 /*
2927 * If Visual mode started in insert mode, execute "CTRL-O"
2928 */
2929 else if ((State & INSERT) && VIsual_active)
2930 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 /*
2933 * Middle mouse click: Put text before cursor.
2934 */
2935 if (which_button == MOUSE_MIDDLE)
2936 {
2937#ifdef FEAT_CLIPBOARD
2938 if (clip_star.available && regname == 0)
2939 regname = '*';
2940#endif
2941 if (yank_register_mline(regname))
2942 {
2943 if (mouse_past_bottom)
2944 dir = FORWARD;
2945 }
2946 else if (mouse_past_eol)
2947 dir = FORWARD;
2948
2949 if (fixindent)
2950 {
2951 c1 = (dir == BACKWARD) ? '[' : ']';
2952 c2 = 'p';
2953 }
2954 else
2955 {
2956 c1 = (dir == FORWARD) ? 'p' : 'P';
2957 c2 = NUL;
2958 }
2959 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2960
2961 /*
2962 * Remember where the paste started, so in edit() Insstart can be set
2963 * to this position
2964 */
2965 if (restart_edit != 0)
2966 where_paste_started = curwin->w_cursor;
2967 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2968 }
2969
2970#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2971 /*
2972 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2973 * error under the mouse pointer.
2974 */
2975 else if (((mod_mask & MOD_MASK_CTRL)
2976 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2977 && bt_quickfix(curbuf))
2978 {
2979 if (State & INSERT)
2980 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002981 if (curwin->w_llist_ref == NULL) /* quickfix window */
2982 stuffReadbuff((char_u *)":.cc\n");
2983 else /* location list window */
2984 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 got_click = FALSE; /* ignore drag&release now */
2986 }
2987#endif
2988
2989 /*
2990 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2991 * under the mouse pointer.
2992 */
2993 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2994 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2995 {
2996 if (State & INSERT)
2997 stuffcharReadbuff(Ctrl_O);
2998 stuffcharReadbuff(Ctrl_RSB);
2999 got_click = FALSE; /* ignore drag&release now */
3000 }
3001
3002 /*
3003 * Shift-Mouse click searches for the next occurrence of the word under
3004 * the mouse pointer
3005 */
3006 else if ((mod_mask & MOD_MASK_SHIFT))
3007 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003008 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 stuffcharReadbuff(Ctrl_O);
3010 if (which_button == MOUSE_LEFT)
3011 stuffcharReadbuff('*');
3012 else /* MOUSE_RIGHT */
3013 stuffcharReadbuff('#');
3014 }
3015
3016 /* Handle double clicks, unless on status line */
3017 else if (in_status_line)
3018 {
3019#ifdef FEAT_MOUSESHAPE
3020 if ((is_drag || is_click) && !drag_status_line)
3021 {
3022 drag_status_line = TRUE;
3023 update_mouseshape(-1);
3024 }
3025#endif
3026 }
3027#ifdef FEAT_VERTSPLIT
3028 else if (in_sep_line)
3029 {
3030# ifdef FEAT_MOUSESHAPE
3031 if ((is_drag || is_click) && !drag_sep_line)
3032 {
3033 drag_sep_line = TRUE;
3034 update_mouseshape(-1);
3035 }
3036# endif
3037 }
3038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3040 && mouse_has(MOUSE_VISUAL))
3041 {
3042 if (is_click || !VIsual_active)
3043 {
3044 if (VIsual_active)
3045 orig_cursor = VIsual;
3046 else
3047 {
3048 check_visual_highlight();
3049 VIsual = curwin->w_cursor;
3050 orig_cursor = VIsual;
3051 VIsual_active = TRUE;
3052 VIsual_reselect = TRUE;
3053 /* start Select mode if 'selectmode' contains "mouse" */
3054 may_start_select('o');
3055 setmouse();
3056 }
3057 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003058 {
3059 /* Double click with ALT pressed makes it blockwise. */
3060 if (mod_mask & MOD_MASK_ALT)
3061 VIsual_mode = Ctrl_V;
3062 else
3063 VIsual_mode = 'v';
3064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3066 VIsual_mode = 'V';
3067 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3068 VIsual_mode = Ctrl_V;
3069#ifdef FEAT_CLIPBOARD
3070 /* Make sure the clipboard gets updated. Needed because start and
3071 * end may still be the same, and the selection needs to be owned */
3072 clip_star.vmode = NUL;
3073#endif
3074 }
3075 /*
3076 * A double click selects a word or a block.
3077 */
3078 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3079 {
3080 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003081 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 if (is_click)
3084 {
3085 /* If the character under the cursor (skipping white space) is
3086 * not a word character, try finding a match and select a (),
3087 * {}, [], #if/#endif, etc. block. */
3088 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003089 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 inc(&end_visual);
3091 if (oap != NULL)
3092 oap->motion_type = MCHAR;
3093 if (oap != NULL
3094 && VIsual_mode == 'v'
3095 && !vim_iswordc(gchar_pos(&end_visual))
3096 && equalpos(curwin->w_cursor, VIsual)
3097 && (pos = findmatch(oap, NUL)) != NULL)
3098 {
3099 curwin->w_cursor = *pos;
3100 if (oap->motion_type == MLINE)
3101 VIsual_mode = 'V';
3102 else if (*p_sel == 'e')
3103 {
3104 if (lt(curwin->w_cursor, VIsual))
3105 ++VIsual.col;
3106 else
3107 ++curwin->w_cursor.col;
3108 }
3109 }
3110 }
3111
3112 if (pos == NULL && (is_click || is_drag))
3113 {
3114 /* When not found a match or when dragging: extend to include
3115 * a word. */
3116 if (lt(curwin->w_cursor, orig_cursor))
3117 {
3118 find_start_of_word(&curwin->w_cursor);
3119 find_end_of_word(&VIsual);
3120 }
3121 else
3122 {
3123 find_start_of_word(&VIsual);
3124 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3125#ifdef FEAT_MBYTE
3126 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003127 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128#else
3129 ++curwin->w_cursor.col;
3130#endif
3131 find_end_of_word(&curwin->w_cursor);
3132 }
3133 }
3134 curwin->w_set_curswant = TRUE;
3135 }
3136 if (is_click)
3137 redraw_curbuf_later(INVERTED); /* update the inversion */
3138 }
3139 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003140 {
3141 if (mod_mask & MOD_MASK_ALT)
3142 VIsual_mode = Ctrl_V;
3143 else
3144 VIsual_mode = 'v';
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003148 if ((!VIsual_active && old_active && mode_displayed)
3149 || (VIsual_active && p_smd && msg_silent == 0
3150 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 return moved;
3154}
3155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156/*
3157 * Move "pos" back to the start of the word it's in.
3158 */
3159 static void
3160find_start_of_word(pos)
3161 pos_T *pos;
3162{
3163 char_u *line;
3164 int cclass;
3165 int col;
3166
3167 line = ml_get(pos->lnum);
3168 cclass = get_mouse_class(line + pos->col);
3169
3170 while (pos->col > 0)
3171 {
3172 col = pos->col - 1;
3173#ifdef FEAT_MBYTE
3174 col -= (*mb_head_off)(line, line + col);
3175#endif
3176 if (get_mouse_class(line + col) != cclass)
3177 break;
3178 pos->col = col;
3179 }
3180}
3181
3182/*
3183 * Move "pos" forward to the end of the word it's in.
3184 * When 'selection' is "exclusive", the position is just after the word.
3185 */
3186 static void
3187find_end_of_word(pos)
3188 pos_T *pos;
3189{
3190 char_u *line;
3191 int cclass;
3192 int col;
3193
3194 line = ml_get(pos->lnum);
3195 if (*p_sel == 'e' && pos->col > 0)
3196 {
3197 --pos->col;
3198#ifdef FEAT_MBYTE
3199 pos->col -= (*mb_head_off)(line, line + pos->col);
3200#endif
3201 }
3202 cclass = get_mouse_class(line + pos->col);
3203 while (line[pos->col] != NUL)
3204 {
3205#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003206 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#else
3208 col = pos->col + 1;
3209#endif
3210 if (get_mouse_class(line + col) != cclass)
3211 {
3212 if (*p_sel == 'e')
3213 pos->col = col;
3214 break;
3215 }
3216 pos->col = col;
3217 }
3218}
3219
3220/*
3221 * Get class of a character for selection: same class means same word.
3222 * 0: blank
3223 * 1: punctuation groups
3224 * 2: normal word character
3225 * >2: multi-byte word character.
3226 */
3227 static int
3228get_mouse_class(p)
3229 char_u *p;
3230{
3231 int c;
3232
3233#ifdef FEAT_MBYTE
3234 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3235 return mb_get_class(p);
3236#endif
3237
3238 c = *p;
3239 if (c == ' ' || c == '\t')
3240 return 0;
3241
3242 if (vim_iswordc(c))
3243 return 2;
3244
3245 /*
3246 * There are a few special cases where we want certain combinations of
3247 * characters to be considered as a single word. These are things like
3248 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003249 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 */
3251 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3252 return 1;
3253 return c;
3254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#endif /* FEAT_MOUSE */
3256
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257/*
3258 * Check if highlighting for visual mode is possible, give a warning message
3259 * if not.
3260 */
3261 void
3262check_visual_highlight()
3263{
3264 static int did_check = FALSE;
3265
3266 if (full_screen)
3267 {
3268 if (!did_check && hl_attr(HLF_V) == 0)
3269 MSG(_("Warning: terminal cannot highlight"));
3270 did_check = TRUE;
3271 }
3272}
3273
3274/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003275 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * This function should ALWAYS be called to end Visual mode, except from
3277 * do_pending_operator().
3278 */
3279 void
3280end_visual_mode()
3281{
3282#ifdef FEAT_CLIPBOARD
3283 /*
3284 * If we are using the clipboard, then remember what was selected in case
3285 * we need to paste it somewhere while we still own the selection.
3286 * Only do this when the clipboard is already owned. Don't want to grab
3287 * the selection when hitting ESC.
3288 */
3289 if (clip_star.available && clip_star.owned)
3290 clip_auto_select();
3291#endif
3292
3293 VIsual_active = FALSE;
3294#ifdef FEAT_MOUSE
3295 setmouse();
3296 mouse_dragging = 0;
3297#endif
3298
3299 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003300 curbuf->b_visual.vi_mode = VIsual_mode;
3301 curbuf->b_visual.vi_start = VIsual;
3302 curbuf->b_visual.vi_end = curwin->w_cursor;
3303 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_EVAL
3305 curbuf->b_visual_mode_eval = VIsual_mode;
3306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_VIRTUALEDIT
3308 if (!virtual_active())
3309 curwin->w_cursor.coladd = 0;
3310#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003311 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003313 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314}
3315
3316/*
3317 * Reset VIsual_active and VIsual_reselect.
3318 */
3319 void
3320reset_VIsual_and_resel()
3321{
3322 if (VIsual_active)
3323 {
3324 end_visual_mode();
3325 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3326 }
3327 VIsual_reselect = FALSE;
3328}
3329
3330/*
3331 * Reset VIsual_active and VIsual_reselect if it's set.
3332 */
3333 void
3334reset_VIsual()
3335{
3336 if (VIsual_active)
3337 {
3338 end_visual_mode();
3339 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3340 VIsual_reselect = FALSE;
3341 }
3342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003344#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3346
3347/*
3348 * Check for a balloon-eval special item to include when searching for an
3349 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3350 * Returns TRUE if the character at "*ptr" should be included.
3351 * "dir" is FORWARD or BACKWARD, the direction of searching.
3352 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3353 * "bnp" points to a counter for square brackets.
3354 */
3355 static int
3356find_is_eval_item(ptr, colp, bnp, dir)
3357 char_u *ptr;
3358 int *colp;
3359 int *bnp;
3360 int dir;
3361{
3362 /* Accept everything inside []. */
3363 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3364 ++*bnp;
3365 if (*bnp > 0)
3366 {
3367 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3368 --*bnp;
3369 return TRUE;
3370 }
3371
3372 /* skip over "s.var" */
3373 if (*ptr == '.')
3374 return TRUE;
3375
3376 /* two-character item: s->var */
3377 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3378 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3379 {
3380 *colp += dir;
3381 return TRUE;
3382 }
3383 return FALSE;
3384}
3385#endif
3386
3387/*
3388 * Find the identifier under or to the right of the cursor.
3389 * "find_type" can have one of three values:
3390 * FIND_IDENT: find an identifier (keyword)
3391 * FIND_STRING: find any non-white string
3392 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003393 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 *
3395 * There are three steps:
3396 * 1. Search forward for the start of an identifier/string. Doesn't move if
3397 * already on one.
3398 * 2. Search backward for the start of this identifier/string.
3399 * This doesn't match the real Vi but I like it a little better and it
3400 * shouldn't bother anyone.
3401 * 3. Search forward to the end of this identifier/string.
3402 * When FIND_IDENT isn't defined, we backup until a blank.
3403 *
3404 * Returns the length of the string, or zero if no string is found.
3405 * If a string is found, a pointer to the string is put in "*string". This
3406 * string is not always NUL terminated.
3407 */
3408 int
3409find_ident_under_cursor(string, find_type)
3410 char_u **string;
3411 int find_type;
3412{
3413 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3414 curwin->w_cursor.col, string, find_type);
3415}
3416
3417/*
3418 * Like find_ident_under_cursor(), but for any window and any position.
3419 * However: Uses 'iskeyword' from the current window!.
3420 */
3421 int
3422find_ident_at_pos(wp, lnum, startcol, string, find_type)
3423 win_T *wp;
3424 linenr_T lnum;
3425 colnr_T startcol;
3426 char_u **string;
3427 int find_type;
3428{
3429 char_u *ptr;
3430 int col = 0; /* init to shut up GCC */
3431 int i;
3432#ifdef FEAT_MBYTE
3433 int this_class = 0;
3434 int prev_class;
3435 int prevcol;
3436#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003437#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 int bn = 0; /* bracket nesting */
3439#endif
3440
3441 /*
3442 * if i == 0: try to find an identifier
3443 * if i == 1: try to find any non-white string
3444 */
3445 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3446 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3447 {
3448 /*
3449 * 1. skip to start of identifier/string
3450 */
3451 col = startcol;
3452#ifdef FEAT_MBYTE
3453 if (has_mbyte)
3454 {
3455 while (ptr[col] != NUL)
3456 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003457# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 /* Stop at a ']' to evaluate "a[x]". */
3459 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3460 break;
3461# endif
3462 this_class = mb_get_class(ptr + col);
3463 if (this_class != 0 && (i == 1 || this_class != 1))
3464 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003465 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 }
3468 else
3469#endif
3470 while (ptr[col] != NUL
3471 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003472# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3474# endif
3475 )
3476 ++col;
3477
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003478#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* When starting on a ']' count it, so that we include the '['. */
3480 bn = ptr[col] == ']';
3481#endif
3482
3483 /*
3484 * 2. Back up to start of identifier/string.
3485 */
3486#ifdef FEAT_MBYTE
3487 if (has_mbyte)
3488 {
3489 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3492 this_class = mb_get_class((char_u *)"a");
3493 else
3494# endif
3495 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3499 prev_class = mb_get_class(ptr + prevcol);
3500 if (this_class != prev_class
3501 && (i == 0
3502 || prev_class == 0
3503 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 && (!(find_type & FIND_EVAL)
3506 || prevcol == 0
3507 || !find_is_eval_item(ptr + prevcol, &prevcol,
3508 &bn, BACKWARD))
3509# endif
3510 )
3511 break;
3512 col = prevcol;
3513 }
3514
3515 /* If we don't want just any old string, or we've found an
3516 * identifier, stop searching. */
3517 if (this_class > 2)
3518 this_class = 2;
3519 if (!(find_type & FIND_STRING) || this_class == 2)
3520 break;
3521 }
3522 else
3523#endif
3524 {
3525 while (col > 0
3526 && ((i == 0
3527 ? vim_iswordc(ptr[col - 1])
3528 : (!vim_iswhite(ptr[col - 1])
3529 && (!(find_type & FIND_IDENT)
3530 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 || ((find_type & FIND_EVAL)
3533 && col > 1
3534 && find_is_eval_item(ptr + col - 1, &col,
3535 &bn, BACKWARD))
3536#endif
3537 ))
3538 --col;
3539
3540 /* If we don't want just any old string, or we've found an
3541 * identifier, stop searching. */
3542 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3543 break;
3544 }
3545 }
3546
3547 if (ptr[col] == NUL || (i == 0 && (
3548#ifdef FEAT_MBYTE
3549 has_mbyte ? this_class != 2 :
3550#endif
3551 !vim_iswordc(ptr[col]))))
3552 {
3553 /*
3554 * didn't find an identifier or string
3555 */
3556 if (find_type & FIND_STRING)
3557 EMSG(_("E348: No string under cursor"));
3558 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003559 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return 0;
3561 }
3562 ptr += col;
3563 *string = ptr;
3564
3565 /*
3566 * 3. Find the end if the identifier/string.
3567 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003568#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 bn = 0;
3570 startcol -= col;
3571#endif
3572 col = 0;
3573#ifdef FEAT_MBYTE
3574 if (has_mbyte)
3575 {
3576 /* Search for point of changing multibyte character class. */
3577 this_class = mb_get_class(ptr);
3578 while (ptr[col] != NUL
3579 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3580 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003581# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 || ((find_type & FIND_EVAL)
3583 && col <= (int)startcol
3584 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3585# endif
3586 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003587 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589 else
3590#endif
3591 while ((i == 0 ? vim_iswordc(ptr[col])
3592 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 || ((find_type & FIND_EVAL)
3595 && col <= (int)startcol
3596 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3597# endif
3598 )
3599 {
3600 ++col;
3601 }
3602
3603 return col;
3604}
3605
3606/*
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02003607 * Add commands to reselect Visual mode into the redo buffer.
3608 */
3609 static void
3610prep_redo_visual(cap)
3611 cmdarg_T *cap;
3612{
3613 ResetRedobuff();
3614 AppendCharToRedobuff(VIsual_mode);
3615 if (VIsual_mode == 'V' && curbuf->b_visual.vi_end.lnum
3616 != curbuf->b_visual.vi_start.lnum)
3617 {
3618 AppendNumberToRedobuff(curbuf->b_visual.vi_end.lnum
3619 - curbuf->b_visual.vi_start.lnum);
3620 AppendCharToRedobuff('j');
3621 }
3622 else if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
3623 {
3624 /* block visual mode or char visual mmode*/
3625 if (curbuf->b_visual.vi_end.lnum != curbuf->b_visual.vi_start.lnum)
3626 {
3627 AppendNumberToRedobuff(curbuf->b_visual.vi_end.lnum -
3628 curbuf->b_visual.vi_start.lnum);
3629 AppendCharToRedobuff('j');
3630 }
3631 if (curbuf->b_visual.vi_curswant == MAXCOL)
3632 AppendCharToRedobuff('$');
3633 else if (curbuf->b_visual.vi_end.col > curbuf->b_visual.vi_start.col)
3634 {
3635 AppendNumberToRedobuff(curbuf->b_visual.vi_end.col
3636 - curbuf->b_visual.vi_start.col - 1);
3637 AppendCharToRedobuff(' ');
3638 }
3639 }
3640 AppendNumberToRedobuff(cap->count1);
3641}
3642
3643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 * Prepare for redo of a normal command.
3645 */
3646 static void
3647prep_redo_cmd(cap)
3648 cmdarg_T *cap;
3649{
3650 prep_redo(cap->oap->regname, cap->count0,
3651 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3652}
3653
3654/*
3655 * Prepare for redo of any command.
3656 * Note that only the last argument can be a multi-byte char.
3657 */
3658 static void
3659prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3660 int regname;
3661 long num;
3662 int cmd1;
3663 int cmd2;
3664 int cmd3;
3665 int cmd4;
3666 int cmd5;
3667{
3668 ResetRedobuff();
3669 if (regname != 0) /* yank from specified buffer */
3670 {
3671 AppendCharToRedobuff('"');
3672 AppendCharToRedobuff(regname);
3673 }
3674 if (num)
3675 AppendNumberToRedobuff(num);
3676
3677 if (cmd1 != NUL)
3678 AppendCharToRedobuff(cmd1);
3679 if (cmd2 != NUL)
3680 AppendCharToRedobuff(cmd2);
3681 if (cmd3 != NUL)
3682 AppendCharToRedobuff(cmd3);
3683 if (cmd4 != NUL)
3684 AppendCharToRedobuff(cmd4);
3685 if (cmd5 != NUL)
3686 AppendCharToRedobuff(cmd5);
3687}
3688
3689/*
3690 * check for operator active and clear it
3691 *
3692 * return TRUE if operator was active
3693 */
3694 static int
3695checkclearop(oap)
3696 oparg_T *oap;
3697{
3698 if (oap->op_type == OP_NOP)
3699 return FALSE;
3700 clearopbeep(oap);
3701 return TRUE;
3702}
3703
3704/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003705 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003707 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
3709 static int
3710checkclearopq(oap)
3711 oparg_T *oap;
3712{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003713 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 return FALSE;
3715 clearopbeep(oap);
3716 return TRUE;
3717}
3718
3719 static void
3720clearop(oap)
3721 oparg_T *oap;
3722{
3723 oap->op_type = OP_NOP;
3724 oap->regname = 0;
3725 oap->motion_force = NUL;
3726 oap->use_reg_one = FALSE;
3727}
3728
3729 static void
3730clearopbeep(oap)
3731 oparg_T *oap;
3732{
3733 clearop(oap);
3734 beep_flush();
3735}
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737/*
3738 * Remove the shift modifier from a special key.
3739 */
3740 static void
3741unshift_special(cap)
3742 cmdarg_T *cap;
3743{
3744 switch (cap->cmdchar)
3745 {
3746 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3747 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3748 case K_S_UP: cap->cmdchar = K_UP; break;
3749 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3750 case K_S_HOME: cap->cmdchar = K_HOME; break;
3751 case K_S_END: cap->cmdchar = K_END; break;
3752 }
3753 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003756/*
3757 * If the mode is currently displayed clear the command line or update the
3758 * command displayed.
3759 */
3760 static void
3761may_clear_cmdline()
3762{
3763 if (mode_displayed)
3764 clear_cmdline = TRUE; /* unshow visual mode later */
3765#ifdef FEAT_CMDL_INFO
3766 else
3767 clear_showcmd();
3768#endif
3769}
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3772/*
3773 * Routines for displaying a partly typed command
3774 */
3775
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003776#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777static char_u showcmd_buf[SHOWCMD_BUFLEN];
3778static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3779static int showcmd_is_clear = TRUE;
3780static int showcmd_visual = FALSE;
3781
3782static void display_showcmd __ARGS((void));
3783
3784 void
3785clear_showcmd()
3786{
3787 if (!p_sc)
3788 return;
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (VIsual_active && !char_avail())
3791 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003792 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 long lines;
3794 colnr_T leftcol, rightcol;
3795 linenr_T top, bot;
3796
3797 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003798 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
3800 top = VIsual.lnum;
3801 bot = curwin->w_cursor.lnum;
3802 }
3803 else
3804 {
3805 top = curwin->w_cursor.lnum;
3806 bot = VIsual.lnum;
3807 }
3808# ifdef FEAT_FOLDING
3809 /* Include closed folds as a whole. */
3810 hasFolding(top, &top, NULL);
3811 hasFolding(bot, NULL, &bot);
3812# endif
3813 lines = bot - top + 1;
3814
3815 if (VIsual_mode == Ctrl_V)
3816 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003817# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003818 char_u *saved_sbr = p_sbr;
3819
3820 /* Make 'sbr' empty for a moment to get the correct size. */
3821 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003824# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003825 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3828 (long)(rightcol - leftcol + 1));
3829 }
3830 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3831 sprintf((char *)showcmd_buf, "%ld", lines);
3832 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003833 {
3834 char_u *s, *e;
3835 int l;
3836 int bytes = 0;
3837 int chars = 0;
3838
3839 if (cursor_bot)
3840 {
3841 s = ml_get_pos(&VIsual);
3842 e = ml_get_cursor();
3843 }
3844 else
3845 {
3846 s = ml_get_cursor();
3847 e = ml_get_pos(&VIsual);
3848 }
3849 while ((*p_sel != 'e') ? s <= e : s < e)
3850 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003851# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003852 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003853# else
3854 l = (*s == NUL) ? 0 : 1;
3855# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003856 if (l == 0)
3857 {
3858 ++bytes;
3859 ++chars;
3860 break; /* end of line */
3861 }
3862 bytes += l;
3863 ++chars;
3864 s += l;
3865 }
3866 if (bytes == chars)
3867 sprintf((char *)showcmd_buf, "%d", chars);
3868 else
3869 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3872 showcmd_visual = TRUE;
3873 }
3874 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 {
3876 showcmd_buf[0] = NUL;
3877 showcmd_visual = FALSE;
3878
3879 /* Don't actually display something if there is nothing to clear. */
3880 if (showcmd_is_clear)
3881 return;
3882 }
3883
3884 display_showcmd();
3885}
3886
3887/*
3888 * Add 'c' to string of shown command chars.
3889 * Return TRUE if output has been written (and setcursor() has been called).
3890 */
3891 int
3892add_to_showcmd(c)
3893 int c;
3894{
3895 char_u *p;
3896 int old_len;
3897 int extra_len;
3898 int overflow;
3899#if defined(FEAT_MOUSE)
3900 int i;
3901 static int ignore[] =
3902 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003903# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3905 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 K_IGNORE,
3908 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3909 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3910 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003911 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003913 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 0
3915 };
3916#endif
3917
Bram Moolenaar09df3122006-01-23 22:23:09 +00003918 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return FALSE;
3920
3921 if (showcmd_visual)
3922 {
3923 showcmd_buf[0] = NUL;
3924 showcmd_visual = FALSE;
3925 }
3926
3927#if defined(FEAT_MOUSE)
3928 /* Ignore keys that are scrollbar updates and mouse clicks */
3929 if (IS_SPECIAL(c))
3930 for (i = 0; ignore[i] != 0; ++i)
3931 if (ignore[i] == c)
3932 return FALSE;
3933#endif
3934
3935 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003936 if (*p == ' ')
3937 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 old_len = (int)STRLEN(showcmd_buf);
3939 extra_len = (int)STRLEN(p);
3940 overflow = old_len + extra_len - SHOWCMD_COLS;
3941 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003942 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3943 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 STRCAT(showcmd_buf, p);
3945
3946 if (char_avail())
3947 return FALSE;
3948
3949 display_showcmd();
3950
3951 return TRUE;
3952}
3953
3954 void
3955add_to_showcmd_c(c)
3956 int c;
3957{
3958 if (!add_to_showcmd(c))
3959 setcursor();
3960}
3961
3962/*
3963 * Delete 'len' characters from the end of the shown command.
3964 */
3965 static void
3966del_from_showcmd(len)
3967 int len;
3968{
3969 int old_len;
3970
3971 if (!p_sc)
3972 return;
3973
3974 old_len = (int)STRLEN(showcmd_buf);
3975 if (len > old_len)
3976 len = old_len;
3977 showcmd_buf[old_len - len] = NUL;
3978
3979 if (!char_avail())
3980 display_showcmd();
3981}
3982
3983/*
3984 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3985 * something and there is a partial mapping.
3986 */
3987 void
3988push_showcmd()
3989{
3990 if (p_sc)
3991 STRCPY(old_showcmd_buf, showcmd_buf);
3992}
3993
3994 void
3995pop_showcmd()
3996{
3997 if (!p_sc)
3998 return;
3999
4000 STRCPY(showcmd_buf, old_showcmd_buf);
4001
4002 display_showcmd();
4003}
4004
4005 static void
4006display_showcmd()
4007{
4008 int len;
4009
4010 cursor_off();
4011
4012 len = (int)STRLEN(showcmd_buf);
4013 if (len == 0)
4014 showcmd_is_clear = TRUE;
4015 else
4016 {
4017 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4018 showcmd_is_clear = FALSE;
4019 }
4020
4021 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004022 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4023 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
4025 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4026
4027 setcursor(); /* put cursor back where it belongs */
4028}
4029#endif
4030
4031#ifdef FEAT_SCROLLBIND
4032/*
4033 * When "check" is FALSE, prepare for commands that scroll the window.
4034 * When "check" is TRUE, take care of scroll-binding after the window has
4035 * scrolled. Called from normal_cmd() and edit().
4036 */
4037 void
4038do_check_scrollbind(check)
4039 int check;
4040{
4041 static win_T *old_curwin = NULL;
4042 static linenr_T old_topline = 0;
4043#ifdef FEAT_DIFF
4044 static int old_topfill = 0;
4045#endif
4046 static buf_T *old_buf = NULL;
4047 static colnr_T old_leftcol = 0;
4048
4049 if (check && curwin->w_p_scb)
4050 {
4051 /* If a ":syncbind" command was just used, don't scroll, only reset
4052 * the values. */
4053 if (did_syncbind)
4054 did_syncbind = FALSE;
4055 else if (curwin == old_curwin)
4056 {
4057 /*
4058 * Synchronize other windows, as necessary according to
4059 * 'scrollbind'. Don't do this after an ":edit" command, except
4060 * when 'diff' is set.
4061 */
4062 if ((curwin->w_buffer == old_buf
4063#ifdef FEAT_DIFF
4064 || curwin->w_p_diff
4065#endif
4066 )
4067 && (curwin->w_topline != old_topline
4068#ifdef FEAT_DIFF
4069 || curwin->w_topfill != old_topfill
4070#endif
4071 || curwin->w_leftcol != old_leftcol))
4072 {
4073 check_scrollbind(curwin->w_topline - old_topline,
4074 (long)(curwin->w_leftcol - old_leftcol));
4075 }
4076 }
4077 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4078 {
4079 /*
4080 * When switching between windows, make sure that the relative
4081 * vertical offset is valid for the new window. The relative
4082 * offset is invalid whenever another 'scrollbind' window has
4083 * scrolled to a point that would force the current window to
4084 * scroll past the beginning or end of its buffer. When the
4085 * resync is performed, some of the other 'scrollbind' windows may
4086 * need to jump so that the current window's relative position is
4087 * visible on-screen.
4088 */
4089 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4090 }
4091 curwin->w_scbind_pos = curwin->w_topline;
4092 }
4093
4094 old_curwin = curwin;
4095 old_topline = curwin->w_topline;
4096#ifdef FEAT_DIFF
4097 old_topfill = curwin->w_topfill;
4098#endif
4099 old_buf = curwin->w_buffer;
4100 old_leftcol = curwin->w_leftcol;
4101}
4102
4103/*
4104 * Synchronize any windows that have "scrollbind" set, based on the
4105 * number of rows by which the current window has changed
4106 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4107 */
4108 void
4109check_scrollbind(topline_diff, leftcol_diff)
4110 linenr_T topline_diff;
4111 long leftcol_diff;
4112{
4113 int want_ver;
4114 int want_hor;
4115 win_T *old_curwin = curwin;
4116 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 int old_VIsual_select = VIsual_select;
4118 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 colnr_T tgt_leftcol = curwin->w_leftcol;
4120 long topline;
4121 long y;
4122
4123 /*
4124 * check 'scrollopt' string for vertical and horizontal scroll options
4125 */
4126 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4127#ifdef FEAT_DIFF
4128 want_ver |= old_curwin->w_p_diff;
4129#endif
4130 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4131
4132 /*
4133 * loop through the scrollbound windows and scroll accordingly
4134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4137 {
4138 curbuf = curwin->w_buffer;
4139 /* skip original window and windows with 'noscrollbind' */
4140 if (curwin != old_curwin && curwin->w_p_scb)
4141 {
4142 /*
4143 * do the vertical scroll
4144 */
4145 if (want_ver)
4146 {
4147#ifdef FEAT_DIFF
4148 if (old_curwin->w_p_diff && curwin->w_p_diff)
4149 {
4150 diff_set_topline(old_curwin, curwin);
4151 }
4152 else
4153#endif
4154 {
4155 curwin->w_scbind_pos += topline_diff;
4156 topline = curwin->w_scbind_pos;
4157 if (topline > curbuf->b_ml.ml_line_count)
4158 topline = curbuf->b_ml.ml_line_count;
4159 if (topline < 1)
4160 topline = 1;
4161
4162 y = topline - curwin->w_topline;
4163 if (y > 0)
4164 scrollup(y, FALSE);
4165 else
4166 scrolldown(-y, FALSE);
4167 }
4168
4169 redraw_later(VALID);
4170 cursor_correct();
4171#ifdef FEAT_WINDOWS
4172 curwin->w_redr_status = TRUE;
4173#endif
4174 }
4175
4176 /*
4177 * do the horizontal scroll
4178 */
4179 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4180 {
4181 curwin->w_leftcol = tgt_leftcol;
4182 leftcol_changed();
4183 }
4184 }
4185 }
4186
4187 /*
4188 * reset current-window
4189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 VIsual_select = old_VIsual_select;
4191 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 curwin = old_curwin;
4193 curbuf = old_curbuf;
4194}
4195#endif /* #ifdef FEAT_SCROLLBIND */
4196
4197/*
4198 * Command character that's ignored.
4199 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004200 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 static void
4203nv_ignore(cap)
4204 cmdarg_T *cap;
4205{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004206 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
4209/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004210 * Command character that doesn't do anything, but unlike nv_ignore() does
4211 * start edit(). Used for "startinsert" executed while starting up.
4212 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004213 static void
4214nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004215 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004216{
4217}
4218
4219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * Command character doesn't exist.
4221 */
4222 static void
4223nv_error(cap)
4224 cmdarg_T *cap;
4225{
4226 clearopbeep(cap->oap);
4227}
4228
4229/*
4230 * <Help> and <F1> commands.
4231 */
4232 static void
4233nv_help(cap)
4234 cmdarg_T *cap;
4235{
4236 if (!checkclearopq(cap->oap))
4237 ex_help(NULL);
4238}
4239
4240/*
4241 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4242 */
4243 static void
4244nv_addsub(cap)
4245 cmdarg_T *cap;
4246{
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004247 int visual = VIsual_active;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02004248
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004249 if (cap->oap->op_type == OP_NOP
4250 && do_addsub((int)cap->cmdchar, cap->count1, cap->arg) == OK)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004251 {
4252 if (visual)
4253 {
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004254 prep_redo_visual(cap);
4255 if (cap->arg)
4256 AppendCharToRedobuff('g');
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004257 AppendCharToRedobuff(cap->cmdchar);
4258 }
4259 else
4260 prep_redo_cmd(cap);
4261 }
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004262 else
4263 clearopbeep(cap->oap);
4264 if (visual)
4265 {
4266 VIsual_active = FALSE;
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004267 redo_VIsual_busy = FALSE;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02004268 may_clear_cmdline();
Bram Moolenaar5d1bc782015-07-17 13:03:48 +02004269 redraw_later(INVERTED);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271}
4272
4273/*
4274 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4275 */
4276 static void
4277nv_page(cap)
4278 cmdarg_T *cap;
4279{
4280 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004281 {
4282#ifdef FEAT_WINDOWS
4283 if (mod_mask & MOD_MASK_CTRL)
4284 {
4285 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4286 if (cap->arg == BACKWARD)
4287 goto_tabpage(-(int)cap->count1);
4288 else
4289 goto_tabpage((int)cap->count0);
4290 }
4291 else
4292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295}
4296
4297/*
4298 * Implementation of "gd" and "gD" command.
4299 */
4300 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004301nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004302 oparg_T *oap;
4303 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004304 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
4306 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 char_u *ptr;
4308
Bram Moolenaard9d30582005-05-18 22:10:28 +00004309 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004310 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312#ifdef FEAT_FOLDING
4313 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4314 foldOpenCursor();
4315#endif
4316}
4317
4318/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004319 * Search for variable declaration of "ptr[len]".
4320 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4321 * current file ("gD").
4322 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004323 * Return FAIL when not found.
4324 */
4325 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004326find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004327 char_u *ptr;
4328 int len;
4329 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004330 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 int searchflags; /* flags passed to searchit() */
4332{
4333 char_u *pat;
4334 pos_T old_pos;
4335 pos_T par_pos;
4336 pos_T found_pos;
4337 int t;
4338 int save_p_ws;
4339 int save_p_scs;
4340 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004341 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342
4343 if ((pat = alloc(len + 7)) == NULL)
4344 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004345
4346 /* Put "\V" before the pattern to avoid that the special meaning of "."
4347 * and "~" causes trouble. */
4348 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4349 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 old_pos = curwin->w_cursor;
4351 save_p_ws = p_ws;
4352 save_p_scs = p_scs;
4353 p_ws = FALSE; /* don't wrap around end of file now */
4354 p_scs = FALSE; /* don't switch ignorecase off now */
4355
4356 /*
4357 * With "gD" go to line 1.
4358 * With "gd" Search back for the start of the current function, then go
4359 * back until a blank line. If this fails go to line 1.
4360 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004361 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
4363 setpcmark(); /* Set in findpar() otherwise */
4364 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004365 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else
4368 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004369 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4371 --curwin->w_cursor.lnum;
4372 }
4373 curwin->w_cursor.col = 0;
4374
4375 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004376 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 for (;;)
4378 {
4379 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004380 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004381 if (curwin->w_cursor.lnum >= old_pos.lnum)
4382 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004383
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004384 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004385 {
4386 pos_T *pos;
4387
4388 /* Check that the block the match is in doesn't end before the
4389 * position where we started the search from. */
4390 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4391 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4392 && pos->lnum < old_pos.lnum)
4393 continue;
4394 }
4395
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004396 if (t == FAIL)
4397 {
4398 /* If we previously found a valid position, use it. */
4399 if (found_pos.lnum != 0)
4400 {
4401 curwin->w_cursor = found_pos;
4402 t = OK;
4403 }
4404 break;
4405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004407 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004408 {
4409 /* Ignore this line, continue at start of next line. */
4410 ++curwin->w_cursor.lnum;
4411 curwin->w_cursor.col = 0;
4412 continue;
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004415 if (!locally) /* global search: use first match found */
4416 break;
4417 if (curwin->w_cursor.lnum >= par_pos.lnum)
4418 {
4419 /* If we previously found a valid position, use it. */
4420 if (found_pos.lnum != 0)
4421 curwin->w_cursor = found_pos;
4422 break;
4423 }
4424
4425 /* For finding a local variable and the match is before the "{" search
4426 * to find a later match. For K&R style function declarations this
4427 * skips the function header without types. */
4428 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004430
4431 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004433 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 curwin->w_cursor = old_pos;
4435 }
4436 else
4437 {
4438 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /* "n" searches forward now */
4440 reset_search_dir();
4441 }
4442
4443 vim_free(pat);
4444 p_ws = save_p_ws;
4445 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004446
4447 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449
4450/*
4451 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4452 * lines rather than lines in the file.
4453 * 'dist' must be positive.
4454 *
4455 * Return OK if able to move cursor, FAIL otherwise.
4456 */
4457 static int
4458nv_screengo(oap, dir, dist)
4459 oparg_T *oap;
4460 int dir;
4461 long dist;
4462{
4463 int linelen = linetabsize(ml_get_curline());
4464 int retval = OK;
4465 int atend = FALSE;
4466 int n;
4467 int col_off1; /* margin offset for first screen line */
4468 int col_off2; /* margin offset for wrapped screen line */
4469 int width1; /* text width for first screen line */
4470 int width2; /* test width for wrapped screen line */
4471
4472 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004473 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 col_off1 = curwin_col_off();
4476 col_off2 = col_off1 - curwin_col_off2();
4477 width1 = W_WIDTH(curwin) - col_off1;
4478 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004479 if (width2 == 0)
4480 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482#ifdef FEAT_VERTSPLIT
4483 if (curwin->w_width != 0)
4484 {
4485#endif
4486 /*
4487 * Instead of sticking at the last character of the buffer line we
4488 * try to stick in the last column of the screen.
4489 */
4490 if (curwin->w_curswant == MAXCOL)
4491 {
4492 atend = TRUE;
4493 validate_virtcol();
4494 if (width1 <= 0)
4495 curwin->w_curswant = 0;
4496 else
4497 {
4498 curwin->w_curswant = width1 - 1;
4499 if (curwin->w_virtcol > curwin->w_curswant)
4500 curwin->w_curswant += ((curwin->w_virtcol
4501 - curwin->w_curswant - 1) / width2 + 1) * width2;
4502 }
4503 }
4504 else
4505 {
4506 if (linelen > width1)
4507 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4508 else
4509 n = width1;
4510 if (curwin->w_curswant > (colnr_T)n + 1)
4511 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4512 * width2;
4513 }
4514
4515 while (dist--)
4516 {
4517 if (dir == BACKWARD)
4518 {
4519 if ((long)curwin->w_curswant >= width2)
4520 /* move back within line */
4521 curwin->w_curswant -= width2;
4522 else
4523 {
4524 /* to previous line */
4525 if (curwin->w_cursor.lnum == 1)
4526 {
4527 retval = FAIL;
4528 break;
4529 }
4530 --curwin->w_cursor.lnum;
4531#ifdef FEAT_FOLDING
4532 /* Move to the start of a closed fold. Don't do that when
4533 * 'foldopen' contains "all": it will open in a moment. */
4534 if (!(fdo_flags & FDO_ALL))
4535 (void)hasFolding(curwin->w_cursor.lnum,
4536 &curwin->w_cursor.lnum, NULL);
4537#endif
4538 linelen = linetabsize(ml_get_curline());
4539 if (linelen > width1)
4540 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4541 + 1) * width2;
4542 }
4543 }
4544 else /* dir == FORWARD */
4545 {
4546 if (linelen > width1)
4547 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4548 else
4549 n = width1;
4550 if (curwin->w_curswant + width2 < (colnr_T)n)
4551 /* move forward within line */
4552 curwin->w_curswant += width2;
4553 else
4554 {
4555 /* to next line */
4556#ifdef FEAT_FOLDING
4557 /* Move to the end of a closed fold. */
4558 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4559 &curwin->w_cursor.lnum);
4560#endif
4561 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4562 {
4563 retval = FAIL;
4564 break;
4565 }
4566 curwin->w_cursor.lnum++;
4567 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004568 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 }
4571 }
4572#ifdef FEAT_VERTSPLIT
4573 }
4574#endif
4575
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004576 if (virtual_active() && atend)
4577 coladvance(MAXCOL);
4578 else
4579 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4582 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4583 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004584 colnr_T virtcol;
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /*
4587 * Check for landing on a character that got split at the end of the
4588 * last line. We want to advance a screenline, not end up in the same
4589 * screenline or move two screenlines.
4590 */
4591 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004592 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004593# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004594 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4595 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004596# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004597
4598 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 && (curwin->w_curswant < (colnr_T)width1
4600 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4601 : ((curwin->w_curswant - width1) % width2
4602 > (colnr_T)width2 / 2)))
4603 --curwin->w_cursor.col;
4604 }
4605#endif
4606
4607 if (atend)
4608 curwin->w_curswant = MAXCOL; /* stick in the last column */
4609
4610 return retval;
4611}
4612
4613#ifdef FEAT_MOUSE
4614/*
4615 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4616 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4618 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
4620 static void
4621nv_mousescroll(cap)
4622 cmdarg_T *cap;
4623{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004624# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004625 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004627 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 int row, col;
4630
4631 row = mouse_row;
4632 col = mouse_col;
4633
4634 /* find the window at the pointer coordinates */
4635 curwin = mouse_find_win(&row, &col);
4636 curbuf = curwin->w_buffer;
4637 }
4638# endif
4639
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004640 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004642 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4643 {
4644 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4645 }
4646 else
4647 {
4648 cap->count1 = 3;
4649 cap->count0 = 3;
4650 nv_scroll_line(cap);
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 else
4655 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004656 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4657 if (!curwin->w_p_wrap)
4658 {
4659 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004660
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004661 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4662 step = W_WIDTH(curwin);
4663 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4664 if (val < 0)
4665 val = 0;
4666
4667 gui_do_horiz_scroll(val, TRUE);
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004672# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 curwin->w_redr_status = TRUE;
4674
4675 curwin = old_curwin;
4676 curbuf = curwin->w_buffer;
4677# endif
4678}
4679
4680/*
4681 * Mouse clicks and drags.
4682 */
4683 static void
4684nv_mouse(cap)
4685 cmdarg_T *cap;
4686{
4687 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4688}
4689#endif
4690
4691/*
4692 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4693 * cap->arg must be TRUE for CTRL-E.
4694 */
4695 static void
4696nv_scroll_line(cap)
4697 cmdarg_T *cap;
4698{
4699 if (!checkclearop(cap->oap))
4700 scroll_redraw(cap->arg, cap->count1);
4701}
4702
4703/*
4704 * Scroll "count" lines up or down, and redraw.
4705 */
4706 void
4707scroll_redraw(up, count)
4708 int up;
4709 long count;
4710{
4711 linenr_T prev_topline = curwin->w_topline;
4712#ifdef FEAT_DIFF
4713 int prev_topfill = curwin->w_topfill;
4714#endif
4715 linenr_T prev_lnum = curwin->w_cursor.lnum;
4716
4717 if (up)
4718 scrollup(count, TRUE);
4719 else
4720 scrolldown(count, TRUE);
4721 if (p_so)
4722 {
4723 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4724 * valid, otherwise the screen jumps back at the end of the file. */
4725 cursor_correct();
4726 check_cursor_moved(curwin);
4727 curwin->w_valid |= VALID_TOPLINE;
4728
4729 /* If moved back to where we were, at least move the cursor, otherwise
4730 * we get stuck at one position. Don't move the cursor up if the
4731 * first line of the buffer is already on the screen */
4732 while (curwin->w_topline == prev_topline
4733#ifdef FEAT_DIFF
4734 && curwin->w_topfill == prev_topfill
4735#endif
4736 )
4737 {
4738 if (up)
4739 {
4740 if (curwin->w_cursor.lnum > prev_lnum
4741 || cursor_down(1L, FALSE) == FAIL)
4742 break;
4743 }
4744 else
4745 {
4746 if (curwin->w_cursor.lnum < prev_lnum
4747 || prev_topline == 1L
4748 || cursor_up(1L, FALSE) == FAIL)
4749 break;
4750 }
4751 /* Mark w_topline as valid, otherwise the screen jumps back at the
4752 * end of the file. */
4753 check_cursor_moved(curwin);
4754 curwin->w_valid |= VALID_TOPLINE;
4755 }
4756 }
4757 if (curwin->w_cursor.lnum != prev_lnum)
4758 coladvance(curwin->w_curswant);
4759 redraw_later(VALID);
4760}
4761
4762/*
4763 * Commands that start with "z".
4764 */
4765 static void
4766nv_zet(cap)
4767 cmdarg_T *cap;
4768{
4769 long n;
4770 colnr_T col;
4771 int nchar = cap->nchar;
4772#ifdef FEAT_FOLDING
4773 long old_fdl = curwin->w_p_fdl;
4774 int old_fen = curwin->w_p_fen;
4775#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004776#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004777 int undo = FALSE;
4778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 if (VIM_ISDIGIT(nchar))
4781 {
4782 /*
4783 * "z123{nchar}": edit the count before obtaining {nchar}
4784 */
4785 if (checkclearop(cap->oap))
4786 return;
4787 n = nchar - '0';
4788 for (;;)
4789 {
4790#ifdef USE_ON_FLY_SCROLL
4791 dont_scroll = TRUE; /* disallow scrolling here */
4792#endif
4793 ++no_mapping;
4794 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004795 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 --no_mapping;
4798 --allow_keys;
4799#ifdef FEAT_CMDL_INFO
4800 (void)add_to_showcmd(nchar);
4801#endif
4802 if (nchar == K_DEL || nchar == K_KDEL)
4803 n /= 10;
4804 else if (VIM_ISDIGIT(nchar))
4805 n = n * 10 + (nchar - '0');
4806 else if (nchar == CAR)
4807 {
4808#ifdef FEAT_GUI
4809 need_mouse_correct = TRUE;
4810#endif
4811 win_setheight((int)n);
4812 break;
4813 }
4814 else if (nchar == 'l'
4815 || nchar == 'h'
4816 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004817 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 cap->count1 = n ? n * cap->count1 : cap->count1;
4820 goto dozet;
4821 }
4822 else
4823 {
4824 clearopbeep(cap->oap);
4825 break;
4826 }
4827 }
4828 cap->oap->op_type = OP_NOP;
4829 return;
4830 }
4831
4832dozet:
4833 if (
4834#ifdef FEAT_FOLDING
4835 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4836 * and "zC" only in Visual mode. "zj" and "zk" are motion
4837 * commands. */
4838 cap->nchar != 'f' && cap->nchar != 'F'
4839 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4840 && cap->nchar != 'j' && cap->nchar != 'k'
4841 &&
4842#endif
4843 checkclearop(cap->oap))
4844 return;
4845
4846 /*
4847 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4848 * If line number given, set cursor.
4849 */
4850 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4851 && cap->count0
4852 && cap->count0 != curwin->w_cursor.lnum)
4853 {
4854 setpcmark();
4855 if (cap->count0 > curbuf->b_ml.ml_line_count)
4856 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4857 else
4858 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004859 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861
4862 switch (nchar)
4863 {
4864 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4865 case '+':
4866 if (cap->count0 == 0)
4867 {
4868 /* No count given: put cursor at the line below screen */
4869 validate_botline(); /* make sure w_botline is valid */
4870 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4871 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4872 else
4873 curwin->w_cursor.lnum = curwin->w_botline;
4874 }
4875 /* FALLTHROUGH */
4876 case NL:
4877 case CAR:
4878 case K_KENTER:
4879 beginline(BL_WHITE | BL_FIX);
4880 /* FALLTHROUGH */
4881
4882 case 't': scroll_cursor_top(0, TRUE);
4883 redraw_later(VALID);
4884 break;
4885
4886 /* "z." and "zz": put cursor in middle of screen */
4887 case '.': beginline(BL_WHITE | BL_FIX);
4888 /* FALLTHROUGH */
4889
4890 case 'z': scroll_cursor_halfway(TRUE);
4891 redraw_later(VALID);
4892 break;
4893
4894 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4895 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4896 * when <count> is at bottom of window, and puts that one at
4897 * bottom of window. */
4898 if (cap->count0 != 0)
4899 {
4900 scroll_cursor_bot(0, TRUE);
4901 curwin->w_cursor.lnum = curwin->w_topline;
4902 }
4903 else if (curwin->w_topline == 1)
4904 curwin->w_cursor.lnum = 1;
4905 else
4906 curwin->w_cursor.lnum = curwin->w_topline - 1;
4907 /* FALLTHROUGH */
4908 case '-':
4909 beginline(BL_WHITE | BL_FIX);
4910 /* FALLTHROUGH */
4911
4912 case 'b': scroll_cursor_bot(0, TRUE);
4913 redraw_later(VALID);
4914 break;
4915
4916 /* "zH" - scroll screen right half-page */
4917 case 'H':
4918 cap->count1 *= W_WIDTH(curwin) / 2;
4919 /* FALLTHROUGH */
4920
4921 /* "zh" - scroll screen to the right */
4922 case 'h':
4923 case K_LEFT:
4924 if (!curwin->w_p_wrap)
4925 {
4926 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4927 curwin->w_leftcol = 0;
4928 else
4929 curwin->w_leftcol -= (colnr_T)cap->count1;
4930 leftcol_changed();
4931 }
4932 break;
4933
4934 /* "zL" - scroll screen left half-page */
4935 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4936 /* FALLTHROUGH */
4937
4938 /* "zl" - scroll screen to the left */
4939 case 'l':
4940 case K_RIGHT:
4941 if (!curwin->w_p_wrap)
4942 {
4943 /* scroll the window left */
4944 curwin->w_leftcol += (colnr_T)cap->count1;
4945 leftcol_changed();
4946 }
4947 break;
4948
4949 /* "zs" - scroll screen, cursor at the start */
4950 case 's': if (!curwin->w_p_wrap)
4951 {
4952#ifdef FEAT_FOLDING
4953 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4954 col = 0; /* like the cursor is in col 0 */
4955 else
4956#endif
4957 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4958 if ((long)col > p_siso)
4959 col -= p_siso;
4960 else
4961 col = 0;
4962 if (curwin->w_leftcol != col)
4963 {
4964 curwin->w_leftcol = col;
4965 redraw_later(NOT_VALID);
4966 }
4967 }
4968 break;
4969
4970 /* "ze" - scroll screen, cursor at the end */
4971 case 'e': if (!curwin->w_p_wrap)
4972 {
4973#ifdef FEAT_FOLDING
4974 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4975 col = 0; /* like the cursor is in col 0 */
4976 else
4977#endif
4978 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4979 n = W_WIDTH(curwin) - curwin_col_off();
4980 if ((long)col + p_siso < n)
4981 col = 0;
4982 else
4983 col = col + p_siso - n + 1;
4984 if (curwin->w_leftcol != col)
4985 {
4986 curwin->w_leftcol = col;
4987 redraw_later(NOT_VALID);
4988 }
4989 }
4990 break;
4991
4992#ifdef FEAT_FOLDING
4993 /* "zF": create fold command */
4994 /* "zf": create fold operator */
4995 case 'F':
4996 case 'f': if (foldManualAllowed(TRUE))
4997 {
4998 cap->nchar = 'f';
4999 nv_operator(cap);
5000 curwin->w_p_fen = TRUE;
5001
5002 /* "zF" is like "zfzf" */
5003 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5004 {
5005 nv_operator(cap);
5006 finish_op = TRUE;
5007 }
5008 }
5009 else
5010 clearopbeep(cap->oap);
5011 break;
5012
5013 /* "zd": delete fold at cursor */
5014 /* "zD": delete fold at cursor recursively */
5015 case 'd':
5016 case 'D': if (foldManualAllowed(FALSE))
5017 {
5018 if (VIsual_active)
5019 nv_operator(cap);
5020 else
5021 deleteFold(curwin->w_cursor.lnum,
5022 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5023 }
5024 break;
5025
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005026 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 case 'E': if (foldmethodIsManual(curwin))
5028 {
5029 clearFolding(curwin);
5030 changed_window_setting();
5031 }
5032 else if (foldmethodIsMarker(curwin))
5033 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5034 TRUE, FALSE);
5035 else
5036 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5037 break;
5038
5039 /* "zn": fold none: reset 'foldenable' */
5040 case 'n': curwin->w_p_fen = FALSE;
5041 break;
5042
5043 /* "zN": fold Normal: set 'foldenable' */
5044 case 'N': curwin->w_p_fen = TRUE;
5045 break;
5046
5047 /* "zi": invert folding: toggle 'foldenable' */
5048 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5049 break;
5050
5051 /* "za": open closed fold or close open fold at cursor */
5052 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5053 openFold(curwin->w_cursor.lnum, cap->count1);
5054 else
5055 {
5056 closeFold(curwin->w_cursor.lnum, cap->count1);
5057 curwin->w_p_fen = TRUE;
5058 }
5059 break;
5060
5061 /* "zA": open fold at cursor recursively */
5062 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5063 openFoldRecurse(curwin->w_cursor.lnum);
5064 else
5065 {
5066 closeFoldRecurse(curwin->w_cursor.lnum);
5067 curwin->w_p_fen = TRUE;
5068 }
5069 break;
5070
5071 /* "zo": open fold at cursor or Visual area */
5072 case 'o': if (VIsual_active)
5073 nv_operator(cap);
5074 else
5075 openFold(curwin->w_cursor.lnum, cap->count1);
5076 break;
5077
5078 /* "zO": open fold recursively */
5079 case 'O': if (VIsual_active)
5080 nv_operator(cap);
5081 else
5082 openFoldRecurse(curwin->w_cursor.lnum);
5083 break;
5084
5085 /* "zc": close fold at cursor or Visual area */
5086 case 'c': if (VIsual_active)
5087 nv_operator(cap);
5088 else
5089 closeFold(curwin->w_cursor.lnum, cap->count1);
5090 curwin->w_p_fen = TRUE;
5091 break;
5092
5093 /* "zC": close fold recursively */
5094 case 'C': if (VIsual_active)
5095 nv_operator(cap);
5096 else
5097 closeFoldRecurse(curwin->w_cursor.lnum);
5098 curwin->w_p_fen = TRUE;
5099 break;
5100
5101 /* "zv": open folds at the cursor */
5102 case 'v': foldOpenCursor();
5103 break;
5104
5105 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5106 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005107 curwin->w_foldinvalid = TRUE; /* recompute folds */
5108 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 foldOpenCursor();
5110 break;
5111
5112 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5113 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005114 curwin->w_foldinvalid = TRUE; /* recompute folds */
5115 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 break;
5117
5118 /* "zm": fold more */
5119 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005120 {
5121 curwin->w_p_fdl -= cap->count1;
5122 if (curwin->w_p_fdl < 0)
5123 curwin->w_p_fdl = 0;
5124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 old_fdl = -1; /* force an update */
5126 curwin->w_p_fen = TRUE;
5127 break;
5128
5129 /* "zM": close all folds */
5130 case 'M': curwin->w_p_fdl = 0;
5131 old_fdl = -1; /* force an update */
5132 curwin->w_p_fen = TRUE;
5133 break;
5134
5135 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005136 case 'r': curwin->w_p_fdl += cap->count1;
5137 {
5138 int d = getDeepestNesting();
5139
5140 if (curwin->w_p_fdl >= d)
5141 curwin->w_p_fdl = d;
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144
5145 /* "zR": open all folds */
5146 case 'R': curwin->w_p_fdl = getDeepestNesting();
5147 old_fdl = -1; /* force an update */
5148 break;
5149
5150 case 'j': /* "zj" move to next fold downwards */
5151 case 'k': /* "zk" move to next fold upwards */
5152 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5153 cap->count1) == FAIL)
5154 clearopbeep(cap->oap);
5155 break;
5156
5157#endif /* FEAT_FOLDING */
5158
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005159#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005160 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5161 ++no_mapping;
5162 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005163 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005164 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005165 --no_mapping;
5166 --allow_keys;
5167#ifdef FEAT_CMDL_INFO
5168 (void)add_to_showcmd(nchar);
5169#endif
5170 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5171 {
5172 clearopbeep(cap->oap);
5173 break;
5174 }
5175 undo = TRUE;
5176 /*FALLTHROUGH*/
5177
Bram Moolenaarb765d632005-06-07 21:00:02 +00005178 case 'g': /* "zg": add good word to word list */
5179 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005180 case 'G': /* "zG": add good word to temp word list */
5181 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005182 {
5183 char_u *ptr = NULL;
5184 int len;
5185
5186 if (checkclearop(cap->oap))
5187 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005188 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5189 == FAIL)
5190 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005191 if (ptr == NULL)
5192 {
5193 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005194
Bram Moolenaar134bf072013-09-25 18:54:24 +02005195 /* Find bad word under the cursor. When 'spell' is
5196 * off this fails and find_ident_under_cursor() is
5197 * used below. */
5198 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005199 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005200 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005201 if (len != 0 && curwin->w_cursor.col <= pos.col)
5202 ptr = ml_get_pos(&curwin->w_cursor);
5203 curwin->w_cursor = pos;
5204 }
5205
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5207 FIND_IDENT)) == 0)
5208 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005209 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005210 (nchar == 'G' || nchar == 'W')
5211 ? 0 : (int)cap->count1,
5212 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005213 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005214 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215
Bram Moolenaar43abc522005-12-10 20:15:02 +00005216 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005217 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005218 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005219 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005220#endif
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 default: clearopbeep(cap->oap);
5223 }
5224
5225#ifdef FEAT_FOLDING
5226 /* Redraw when 'foldenable' changed */
5227 if (old_fen != curwin->w_p_fen)
5228 {
5229# ifdef FEAT_DIFF
5230 win_T *wp;
5231
5232 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5233 {
5234 /* Adjust 'foldenable' in diff-synced windows. */
5235 FOR_ALL_WINDOWS(wp)
5236 {
5237 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5238 {
5239 wp->w_p_fen = curwin->w_p_fen;
5240 changed_window_setting_win(wp);
5241 }
5242 }
5243 }
5244# endif
5245 changed_window_setting();
5246 }
5247
5248 /* Redraw when 'foldlevel' changed. */
5249 if (old_fdl != curwin->w_p_fdl)
5250 newFoldLevel();
5251#endif
5252}
5253
5254#ifdef FEAT_GUI
5255/*
5256 * Vertical scrollbar movement.
5257 */
5258 static void
5259nv_ver_scrollbar(cap)
5260 cmdarg_T *cap;
5261{
5262 if (cap->oap->op_type != OP_NOP)
5263 clearopbeep(cap->oap);
5264
5265 /* Even if an operator was pending, we still want to scroll */
5266 gui_do_scroll();
5267}
5268
5269/*
5270 * Horizontal scrollbar movement.
5271 */
5272 static void
5273nv_hor_scrollbar(cap)
5274 cmdarg_T *cap;
5275{
5276 if (cap->oap->op_type != OP_NOP)
5277 clearopbeep(cap->oap);
5278
5279 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005280 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281}
5282#endif
5283
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005284#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005285/*
5286 * Click in GUI tab.
5287 */
5288 static void
5289nv_tabline(cap)
5290 cmdarg_T *cap;
5291{
5292 if (cap->oap->op_type != OP_NOP)
5293 clearopbeep(cap->oap);
5294
5295 /* Even if an operator was pending, we still want to jump tabs. */
5296 goto_tabpage(current_tab);
5297}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005298
5299/*
5300 * Selected item in tab line menu.
5301 */
5302 static void
5303nv_tabmenu(cap)
5304 cmdarg_T *cap;
5305{
5306 if (cap->oap->op_type != OP_NOP)
5307 clearopbeep(cap->oap);
5308
5309 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005310 handle_tabmenu();
5311}
5312
5313/*
5314 * Handle selecting an item of the GUI tab line menu.
5315 * Used in Normal and Insert mode.
5316 */
5317 void
5318handle_tabmenu()
5319{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005320 switch (current_tabmenu)
5321 {
5322 case TABLINE_MENU_CLOSE:
5323 if (current_tab == 0)
5324 do_cmdline_cmd((char_u *)"tabclose");
5325 else
5326 {
5327 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5328 current_tab);
5329 do_cmdline_cmd(IObuff);
5330 }
5331 break;
5332
5333 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005334 if (current_tab == 0)
5335 do_cmdline_cmd((char_u *)"$tabnew");
5336 else
5337 {
5338 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5339 current_tab - 1);
5340 do_cmdline_cmd(IObuff);
5341 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005342 break;
5343
5344 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005345 if (current_tab == 0)
5346 do_cmdline_cmd((char_u *)"browse $tabnew");
5347 else
5348 {
5349 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5350 current_tab - 1);
5351 do_cmdline_cmd(IObuff);
5352 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005353 break;
5354 }
5355}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005356#endif
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358/*
5359 * "Q" command.
5360 */
5361 static void
5362nv_exmode(cap)
5363 cmdarg_T *cap;
5364{
5365 /*
5366 * Ignore 'Q' in Visual mode, just give a beep.
5367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005369 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005370 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 do_exmode(FALSE);
5372}
5373
5374/*
5375 * Handle a ":" command.
5376 */
5377 static void
5378nv_colon(cap)
5379 cmdarg_T *cap;
5380{
5381 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005382 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (VIsual_active)
5385 nv_operator(cap);
5386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 if (cap->oap->op_type != OP_NOP)
5389 {
5390 /* Using ":" as a movement is characterwise exclusive. */
5391 cap->oap->motion_type = MCHAR;
5392 cap->oap->inclusive = FALSE;
5393 }
5394 else if (cap->count0)
5395 {
5396 /* translate "count:" into ":.,.+(count - 1)" */
5397 stuffcharReadbuff('.');
5398 if (cap->count0 > 1)
5399 {
5400 stuffReadbuff((char_u *)",.+");
5401 stuffnumReadbuff((long)cap->count0 - 1L);
5402 }
5403 }
5404
5405 /* When typing, don't type below an old message */
5406 if (KeyTyped)
5407 compute_cmdrow();
5408
5409 old_p_im = p_im;
5410
5411 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005412 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5414
5415 /* If 'insertmode' changed, enter or exit Insert mode */
5416 if (p_im != old_p_im)
5417 {
5418 if (p_im)
5419 restart_edit = 'i';
5420 else
5421 restart_edit = 0;
5422 }
5423
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005424 if (cmd_result == FAIL)
5425 /* The Ex command failed, do not execute the operator. */
5426 clearop(cap->oap);
5427 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5429 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005430 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5431 || did_emsg
5432 ))
5433 /* The start of the operator has become invalid by the Ex command.
5434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 clearopbeep(cap->oap);
5436 }
5437}
5438
5439/*
5440 * Handle CTRL-G command.
5441 */
5442 static void
5443nv_ctrlg(cap)
5444 cmdarg_T *cap;
5445{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (VIsual_active) /* toggle Selection/Visual mode */
5447 {
5448 VIsual_select = !VIsual_select;
5449 showmode();
5450 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005451 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 /* print full name if count given or :cd used */
5453 fileinfo((int)cap->count0, FALSE, TRUE);
5454}
5455
5456/*
5457 * Handle CTRL-H <Backspace> command.
5458 */
5459 static void
5460nv_ctrlh(cap)
5461 cmdarg_T *cap;
5462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (VIsual_active && VIsual_select)
5464 {
5465 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5466 v_visop(cap);
5467 }
5468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 nv_left(cap);
5470}
5471
5472/*
5473 * CTRL-L: clear screen and redraw.
5474 */
5475 static void
5476nv_clear(cap)
5477 cmdarg_T *cap;
5478{
5479 if (!checkclearop(cap->oap))
5480 {
5481#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5482 /*
5483 * Right now, the BeBox doesn't seem to have an easy way to detect
5484 * window resizing, so we cheat and make the user detect it
5485 * manually with CTRL-L instead
5486 */
5487 ui_get_shellsize();
5488#endif
5489#ifdef FEAT_SYN_HL
5490 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005491 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#endif
5493 redraw_later(CLEAR);
5494 }
5495}
5496
5497/*
5498 * CTRL-O: In Select mode: switch to Visual mode for one command.
5499 * Otherwise: Go to older pcmark.
5500 */
5501 static void
5502nv_ctrlo(cap)
5503 cmdarg_T *cap;
5504{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (VIsual_active && VIsual_select)
5506 {
5507 VIsual_select = FALSE;
5508 showmode();
5509 restart_VIsual_select = 2; /* restart Select mode later */
5510 }
5511 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
5513 cap->count1 = -cap->count1;
5514 nv_pcmark(cap);
5515 }
5516}
5517
5518/*
5519 * CTRL-^ command, short for ":e #"
5520 */
5521 static void
5522nv_hat(cap)
5523 cmdarg_T *cap;
5524{
5525 if (!checkclearopq(cap->oap))
5526 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5527 GETF_SETMARK|GETF_ALT, FALSE);
5528}
5529
5530/*
5531 * "Z" commands.
5532 */
5533 static void
5534nv_Zet(cap)
5535 cmdarg_T *cap;
5536{
5537 if (!checkclearopq(cap->oap))
5538 {
5539 switch (cap->nchar)
5540 {
5541 /* "ZZ": equivalent to ":x". */
5542 case 'Z': do_cmdline_cmd((char_u *)"x");
5543 break;
5544
5545 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5546 case 'Q': do_cmdline_cmd((char_u *)"q!");
5547 break;
5548
5549 default: clearopbeep(cap->oap);
5550 }
5551 }
5552}
5553
5554#if defined(FEAT_WINDOWS) || defined(PROTO)
5555/*
5556 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5557 */
5558 void
5559do_nv_ident(c1, c2)
5560 int c1;
5561 int c2;
5562{
5563 oparg_T oa;
5564 cmdarg_T ca;
5565
5566 clear_oparg(&oa);
5567 vim_memset(&ca, 0, sizeof(ca));
5568 ca.oap = &oa;
5569 ca.cmdchar = c1;
5570 ca.nchar = c2;
5571 nv_ident(&ca);
5572}
5573#endif
5574
5575/*
5576 * Handle the commands that use the word under the cursor.
5577 * [g] CTRL-] :ta to current identifier
5578 * [g] 'K' run program for current identifier
5579 * [g] '*' / to current identifier or string
5580 * [g] '#' ? to current identifier or string
5581 * g ']' :tselect for current identifier
5582 */
5583 static void
5584nv_ident(cap)
5585 cmdarg_T *cap;
5586{
5587 char_u *ptr = NULL;
5588 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005589 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 char_u *p;
5591 char_u *kp; /* value of 'keywordprg' */
5592 int kp_help; /* 'keywordprg' is ":help" */
5593 int n = 0; /* init for GCC */
5594 int cmdchar;
5595 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005596 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 char_u *aux_ptr;
5598 int isman;
5599 int isman_s;
5600
5601 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5602 {
5603 cmdchar = cap->nchar;
5604 g_cmd = TRUE;
5605 }
5606 else
5607 {
5608 cmdchar = cap->cmdchar;
5609 g_cmd = FALSE;
5610 }
5611
5612 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5613 cmdchar = '#';
5614
5615 /*
5616 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5617 */
5618 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5621 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 if (checkclearopq(cap->oap))
5623 return;
5624 }
5625
5626 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5627 (cmdchar == '*' || cmdchar == '#')
5628 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5629 {
5630 clearop(cap->oap);
5631 return;
5632 }
5633
5634 /* Allocate buffer to put the command in. Inserting backslashes can
5635 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5636 * and some numbers. */
5637 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5638 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5639 || STRCMP(kp, ":help") == 0);
5640 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5641 if (buf == NULL)
5642 return;
5643 buf[0] = NUL;
5644
5645 switch (cmdchar)
5646 {
5647 case '*':
5648 case '#':
5649 /*
5650 * Put cursor at start of word, makes search skip the word
5651 * under the cursor.
5652 * Call setpcmark() first, so "*``" puts the cursor back where
5653 * it was.
5654 */
5655 setpcmark();
5656 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5657
5658 if (!g_cmd && vim_iswordp(ptr))
5659 STRCPY(buf, "\\<");
5660 no_smartcase = TRUE; /* don't use 'smartcase' now */
5661 break;
5662
5663 case 'K':
5664 if (kp_help)
5665 STRCPY(buf, "he! ");
5666 else
5667 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668 /* An external command will probably use an argument starting
5669 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005670 while (*ptr == '-' && n > 0)
5671 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005672 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005673 --n;
5674 }
5675 if (n == 0)
5676 {
5677 EMSG(_(e_noident)); /* found dashes only */
5678 vim_free(buf);
5679 return;
5680 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005681
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 /* When a count is given, turn it into a range. Is this
5683 * really what we want? */
5684 isman = (STRCMP(kp, "man") == 0);
5685 isman_s = (STRCMP(kp, "man -s") == 0);
5686 if (cap->count0 != 0 && !(isman || isman_s))
5687 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5688
5689 STRCAT(buf, "! ");
5690 if (cap->count0 == 0 && isman_s)
5691 STRCAT(buf, "man");
5692 else
5693 STRCAT(buf, kp);
5694 STRCAT(buf, " ");
5695 if (cap->count0 != 0 && (isman || isman_s))
5696 {
5697 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5698 STRCAT(buf, " ");
5699 }
5700 }
5701 break;
5702
5703 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005704 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#ifdef FEAT_CSCOPE
5706 if (p_cst)
5707 STRCPY(buf, "cstag ");
5708 else
5709#endif
5710 STRCPY(buf, "ts ");
5711 break;
5712
5713 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005714 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (curbuf->b_help)
5716 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005718 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005719 if (g_cmd)
5720 STRCPY(buf, "tj ");
5721 else
5722 sprintf((char *)buf, "%ldta ", cap->count0);
5723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725
5726 /*
5727 * Now grab the chars in the identifier
5728 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005729 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005731 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005732 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005733 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005734 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005735 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005737 vim_free(buf);
5738 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005740 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5741 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 {
5743 vim_free(buf);
5744 vim_free(p);
5745 return;
5746 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005747 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005748 STRCAT(buf, p);
5749 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 else
5752 {
5753 if (cmdchar == '*')
5754 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5755 else if (cmdchar == '#')
5756 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005757 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005758 {
5759 if (curbuf->b_help)
5760 /* ":help" handles unescaped argument */
5761 aux_ptr = (char_u *)"";
5762 else
5763 aux_ptr = (char_u *)"\\|\"\n[";
5764 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005765 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005766 aux_ptr = (char_u *)"\\|\"\n*?[";
5767
5768 p = buf + STRLEN(buf);
5769 while (n-- > 0)
5770 {
5771 /* put a backslash before \ and some others */
5772 if (vim_strchr(aux_ptr, *ptr) != NULL)
5773 *p++ = '\\';
5774#ifdef FEAT_MBYTE
5775 /* When current byte is a part of multibyte character, copy all
5776 * bytes of that character. */
5777 if (has_mbyte)
5778 {
5779 int i;
5780 int len = (*mb_ptr2len)(ptr) - 1;
5781
5782 for (i = 0; i < len && n >= 1; ++i, --n)
5783 *p++ = *ptr++;
5784 }
5785#endif
5786 *p++ = *ptr++;
5787 }
5788 *p = NUL;
5789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 /*
5792 * Execute the command.
5793 */
5794 if (cmdchar == '*' || cmdchar == '#')
5795 {
5796 if (!g_cmd && (
5797#ifdef FEAT_MBYTE
5798 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5799#endif
5800 vim_iswordc(ptr[-1])))
5801 STRCAT(buf, "\\>");
5802#ifdef FEAT_CMDHIST
5803 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005804 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5806#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005807 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else
5810 do_cmdline_cmd(buf);
5811
5812 vim_free(buf);
5813}
5814
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815/*
5816 * Get visually selected text, within one line only.
5817 * Returns FAIL if more than one line selected.
5818 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005819 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820get_visual_text(cap, pp, lenp)
5821 cmdarg_T *cap;
5822 char_u **pp; /* return: start of selected text */
5823 int *lenp; /* return: length of selected text */
5824{
5825 if (VIsual_mode != 'V')
5826 unadjust_for_sel();
5827 if (VIsual.lnum != curwin->w_cursor.lnum)
5828 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005829 if (cap != NULL)
5830 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 return FAIL;
5832 }
5833 if (VIsual_mode == 'V')
5834 {
5835 *pp = ml_get_curline();
5836 *lenp = (int)STRLEN(*pp);
5837 }
5838 else
5839 {
5840 if (lt(curwin->w_cursor, VIsual))
5841 {
5842 *pp = ml_get_pos(&curwin->w_cursor);
5843 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5844 }
5845 else
5846 {
5847 *pp = ml_get_pos(&VIsual);
5848 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5849 }
5850#ifdef FEAT_MBYTE
5851 if (has_mbyte)
5852 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005853 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854#endif
5855 }
5856 reset_VIsual_and_resel();
5857 return OK;
5858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
5860/*
5861 * CTRL-T: backwards in tag stack
5862 */
5863 static void
5864nv_tagpop(cap)
5865 cmdarg_T *cap;
5866{
5867 if (!checkclearopq(cap->oap))
5868 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5869}
5870
5871/*
5872 * Handle scrolling command 'H', 'L' and 'M'.
5873 */
5874 static void
5875nv_scroll(cap)
5876 cmdarg_T *cap;
5877{
5878 int used = 0;
5879 long n;
5880#ifdef FEAT_FOLDING
5881 linenr_T lnum;
5882#endif
5883 int half;
5884
5885 cap->oap->motion_type = MLINE;
5886 setpcmark();
5887
5888 if (cap->cmdchar == 'L')
5889 {
5890 validate_botline(); /* make sure curwin->w_botline is valid */
5891 curwin->w_cursor.lnum = curwin->w_botline - 1;
5892 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5893 curwin->w_cursor.lnum = 1;
5894 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005895 {
5896#ifdef FEAT_FOLDING
5897 if (hasAnyFolding(curwin))
5898 {
5899 /* Count a fold for one screen line. */
5900 for (n = cap->count1 - 1; n > 0
5901 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5902 {
5903 (void)hasFolding(curwin->w_cursor.lnum,
5904 &curwin->w_cursor.lnum, NULL);
5905 --curwin->w_cursor.lnum;
5906 }
5907 }
5908 else
5909#endif
5910 curwin->w_cursor.lnum -= cap->count1 - 1;
5911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913 else
5914 {
5915 if (cap->cmdchar == 'M')
5916 {
5917#ifdef FEAT_DIFF
5918 /* Don't count filler lines above the window. */
5919 used -= diff_check_fill(curwin, curwin->w_topline)
5920 - curwin->w_topfill;
5921#endif
5922 validate_botline(); /* make sure w_empty_rows is valid */
5923 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5924 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5925 {
5926#ifdef FEAT_DIFF
5927 /* Count half he number of filler lines to be "below this
5928 * line" and half to be "above the next line". */
5929 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5930 + n) / 2 >= half)
5931 {
5932 --n;
5933 break;
5934 }
5935#endif
5936 used += plines(curwin->w_topline + n);
5937 if (used >= half)
5938 break;
5939#ifdef FEAT_FOLDING
5940 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5941 n = lnum - curwin->w_topline;
5942#endif
5943 }
5944 if (n > 0 && used > curwin->w_height)
5945 --n;
5946 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005947 else /* (cap->cmdchar == 'H') */
5948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005950#ifdef FEAT_FOLDING
5951 if (hasAnyFolding(curwin))
5952 {
5953 /* Count a fold for one screen line. */
5954 lnum = curwin->w_topline;
5955 while (n-- > 0 && lnum < curwin->w_botline - 1)
5956 {
5957 hasFolding(lnum, NULL, &lnum);
5958 ++lnum;
5959 }
5960 n = lnum - curwin->w_topline;
5961 }
5962#endif
5963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 curwin->w_cursor.lnum = curwin->w_topline + n;
5965 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5966 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5967 }
5968
5969 cursor_correct(); /* correct for 'so' */
5970 beginline(BL_SOL | BL_FIX);
5971}
5972
5973/*
5974 * Cursor right commands.
5975 */
5976 static void
5977nv_right(cap)
5978 cmdarg_T *cap;
5979{
5980 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005981 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005983 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5984 {
5985 /* <C-Right> and <S-Right> move a word or WORD right */
5986 if (mod_mask & MOD_MASK_CTRL)
5987 cap->arg = TRUE;
5988 nv_wordcmd(cap);
5989 return;
5990 }
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 cap->oap->motion_type = MCHAR;
5993 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005994 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005996#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005998 * In virtual edit mode, there's no such thing as "past_line", as lines
5999 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 */
6001 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006002 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003#endif
6004
6005 for (n = cap->count1; n > 0; --n)
6006 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006007 if ((!past_line && oneright() == FAIL)
6008 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006009 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
6011 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006012 * <Space> wraps to next line if 'whichwrap' has 's'.
6013 * 'l' wraps to next line if 'whichwrap' has 'l'.
6014 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 */
6016 if ( ((cap->cmdchar == ' '
6017 && vim_strchr(p_ww, 's') != NULL)
6018 || (cap->cmdchar == 'l'
6019 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006020 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 && vim_strchr(p_ww, '>') != NULL))
6022 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6023 {
6024 /* When deleting we also count the NL as a character.
6025 * Set cap->oap->inclusive when last char in the line is
6026 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 && !cap->oap->inclusive
6029 && !lineempty(curwin->w_cursor.lnum))
6030 cap->oap->inclusive = TRUE;
6031 else
6032 {
6033 ++curwin->w_cursor.lnum;
6034 curwin->w_cursor.col = 0;
6035#ifdef FEAT_VIRTUALEDIT
6036 curwin->w_cursor.coladd = 0;
6037#endif
6038 curwin->w_set_curswant = TRUE;
6039 cap->oap->inclusive = FALSE;
6040 }
6041 continue;
6042 }
6043 if (cap->oap->op_type == OP_NOP)
6044 {
6045 /* Only beep and flush if not moved at all */
6046 if (n == cap->count1)
6047 beep_flush();
6048 }
6049 else
6050 {
6051 if (!lineempty(curwin->w_cursor.lnum))
6052 cap->oap->inclusive = TRUE;
6053 }
6054 break;
6055 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006056 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 {
6058 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006059#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (virtual_active())
6061 oneright();
6062 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006065#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (has_mbyte)
6067 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006068 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 ++curwin->w_cursor.col;
6072 }
6073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 }
6075#ifdef FEAT_FOLDING
6076 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6077 && cap->oap->op_type == OP_NOP)
6078 foldOpenCursor();
6079#endif
6080}
6081
6082/*
6083 * Cursor left commands.
6084 *
6085 * Returns TRUE when operator end should not be adjusted.
6086 */
6087 static void
6088nv_left(cap)
6089 cmdarg_T *cap;
6090{
6091 long n;
6092
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006093 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6094 {
6095 /* <C-Left> and <S-Left> move a word or WORD left */
6096 if (mod_mask & MOD_MASK_CTRL)
6097 cap->arg = 1;
6098 nv_bck_word(cap);
6099 return;
6100 }
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 cap->oap->motion_type = MCHAR;
6103 cap->oap->inclusive = FALSE;
6104 for (n = cap->count1; n > 0; --n)
6105 {
6106 if (oneleft() == FAIL)
6107 {
6108 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6109 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6110 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6111 */
6112 if ( (((cap->cmdchar == K_BS
6113 || cap->cmdchar == Ctrl_H)
6114 && vim_strchr(p_ww, 'b') != NULL)
6115 || (cap->cmdchar == 'h'
6116 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006117 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 && vim_strchr(p_ww, '<') != NULL))
6119 && curwin->w_cursor.lnum > 1)
6120 {
6121 --(curwin->w_cursor.lnum);
6122 coladvance((colnr_T)MAXCOL);
6123 curwin->w_set_curswant = TRUE;
6124
6125 /* When the NL before the first char has to be deleted we
6126 * put the cursor on the NUL after the previous line.
6127 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006128 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if ( (cap->oap->op_type == OP_DELETE
6130 || cap->oap->op_type == OP_CHANGE)
6131 && !lineempty(curwin->w_cursor.lnum))
6132 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006133 char_u *cp = ml_get_cursor();
6134
6135 if (*cp != NUL)
6136 {
6137#ifdef FEAT_MBYTE
6138 if (has_mbyte)
6139 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6140 else
6141#endif
6142 ++curwin->w_cursor.col;
6143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 cap->retval |= CA_NO_ADJ_OP_END;
6145 }
6146 continue;
6147 }
6148 /* Only beep and flush if not moved at all */
6149 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6150 beep_flush();
6151 break;
6152 }
6153 }
6154#ifdef FEAT_FOLDING
6155 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6156 && cap->oap->op_type == OP_NOP)
6157 foldOpenCursor();
6158#endif
6159}
6160
6161/*
6162 * Cursor up commands.
6163 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6164 */
6165 static void
6166nv_up(cap)
6167 cmdarg_T *cap;
6168{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006169 if (mod_mask & MOD_MASK_SHIFT)
6170 {
6171 /* <S-Up> is page up */
6172 cap->arg = BACKWARD;
6173 nv_page(cap);
6174 }
6175 else
6176 {
6177 cap->oap->motion_type = MLINE;
6178 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6179 clearopbeep(cap->oap);
6180 else if (cap->arg)
6181 beginline(BL_WHITE | BL_FIX);
6182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183}
6184
6185/*
6186 * Cursor down commands.
6187 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6188 */
6189 static void
6190nv_down(cap)
6191 cmdarg_T *cap;
6192{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006193 if (mod_mask & MOD_MASK_SHIFT)
6194 {
6195 /* <S-Down> is page down */
6196 cap->arg = FORWARD;
6197 nv_page(cap);
6198 }
6199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6201 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006202 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006203 if (curwin->w_llist_ref == NULL)
6204 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6205 else
6206 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 else
6208#endif
6209 {
6210#ifdef FEAT_CMDWIN
6211 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006212 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 cmdwin_result = CAR;
6214 else
6215#endif
6216 {
6217 cap->oap->motion_type = MLINE;
6218 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6219 clearopbeep(cap->oap);
6220 else if (cap->arg)
6221 beginline(BL_WHITE | BL_FIX);
6222 }
6223 }
6224}
6225
6226#ifdef FEAT_SEARCHPATH
6227/*
6228 * Grab the file name under the cursor and edit it.
6229 */
6230 static void
6231nv_gotofile(cap)
6232 cmdarg_T *cap;
6233{
6234 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006235 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006237 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006240 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 return;
6242 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006243#ifdef FEAT_AUTOCMD
6244 if (curbuf_locked())
6245 {
6246 clearop(cap->oap);
6247 return;
6248 }
6249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006251 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 if (ptr != NULL)
6254 {
6255 /* do autowrite if necessary */
6256 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6257 autowrite(curbuf, FALSE);
6258 setpcmark();
6259 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006260 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006261 if (cap->nchar == 'F' && lnum >= 0)
6262 {
6263 curwin->w_cursor.lnum = lnum;
6264 check_cursor_lnum();
6265 beginline(BL_SOL | BL_FIX);
6266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 vim_free(ptr);
6268 }
6269 else
6270 clearop(cap->oap);
6271}
6272#endif
6273
6274/*
6275 * <End> command: to end of current line or last line.
6276 */
6277 static void
6278nv_end(cap)
6279 cmdarg_T *cap;
6280{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006281 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006283 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 nv_goto(cap);
6285 cap->count1 = 1; /* to end of current line */
6286 }
6287 nv_dollar(cap);
6288}
6289
6290/*
6291 * Handle the "$" command.
6292 */
6293 static void
6294nv_dollar(cap)
6295 cmdarg_T *cap;
6296{
6297 cap->oap->motion_type = MCHAR;
6298 cap->oap->inclusive = TRUE;
6299#ifdef FEAT_VIRTUALEDIT
6300 /* In virtual mode when off the edge of a line and an operator
6301 * is pending (whew!) keep the cursor where it is.
6302 * Otherwise, send it to the end of the line. */
6303 if (!virtual_active() || gchar_cursor() != NUL
6304 || cap->oap->op_type == OP_NOP)
6305#endif
6306 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6307 if (cursor_down((long)(cap->count1 - 1),
6308 cap->oap->op_type == OP_NOP) == FAIL)
6309 clearopbeep(cap->oap);
6310#ifdef FEAT_FOLDING
6311 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6312 foldOpenCursor();
6313#endif
6314}
6315
6316/*
6317 * Implementation of '?' and '/' commands.
6318 * If cap->arg is TRUE don't set PC mark.
6319 */
6320 static void
6321nv_search(cap)
6322 cmdarg_T *cap;
6323{
6324 oparg_T *oap = cap->oap;
6325
6326 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6327 {
6328 /* Translate "g??" to "g?g?" */
6329 cap->cmdchar = 'g';
6330 cap->nchar = '?';
6331 nv_operator(cap);
6332 return;
6333 }
6334
6335 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6336
6337 if (cap->searchbuf == NULL)
6338 {
6339 clearop(oap);
6340 return;
6341 }
6342
Bram Moolenaar46539112015-02-17 15:43:57 +01006343 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 (cap->arg ? 0 : SEARCH_MARK));
6345}
6346
6347/*
6348 * Handle "N" and "n" commands.
6349 * cap->arg is SEARCH_REV for "N", 0 for "n".
6350 */
6351 static void
6352nv_next(cap)
6353 cmdarg_T *cap;
6354{
Bram Moolenaar46539112015-02-17 15:43:57 +01006355 pos_T old = curwin->w_cursor;
6356 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6357
6358 if (i == 1 && equalpos(old, curwin->w_cursor))
6359 {
6360 /* Avoid getting stuck on the current cursor position, which can
6361 * happen when an offset is given and the cursor is on the last char
6362 * in the buffer: Repeat with count + 1. */
6363 cap->count1 += 1;
6364 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6365 cap->count1 -= 1;
6366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
6369/*
6370 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6371 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006372 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006374 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375normal_search(cap, dir, pat, opt)
6376 cmdarg_T *cap;
6377 int dir;
6378 char_u *pat;
6379 int opt; /* extra flags for do_search() */
6380{
6381 int i;
6382
6383 cap->oap->motion_type = MCHAR;
6384 cap->oap->inclusive = FALSE;
6385 cap->oap->use_reg_one = TRUE;
6386 curwin->w_set_curswant = TRUE;
6387
6388 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006389 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (i == 0)
6391 clearop(cap->oap);
6392 else
6393 {
6394 if (i == 2)
6395 cap->oap->motion_type = MLINE;
6396#ifdef FEAT_VIRTUALEDIT
6397 curwin->w_cursor.coladd = 0;
6398#endif
6399#ifdef FEAT_FOLDING
6400 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6401 foldOpenCursor();
6402#endif
6403 }
6404
6405 /* "/$" will put the cursor after the end of the line, may need to
6406 * correct that here */
6407 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006408 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410
6411/*
6412 * Character search commands.
6413 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6414 * ',' and FALSE for ';'.
6415 * cap->nchar is NUL for ',' and ';' (repeat the search)
6416 */
6417 static void
6418nv_csearch(cap)
6419 cmdarg_T *cap;
6420{
6421 int t_cmd;
6422
6423 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6424 t_cmd = TRUE;
6425 else
6426 t_cmd = FALSE;
6427
6428 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6430 clearopbeep(cap->oap);
6431 else
6432 {
6433 curwin->w_set_curswant = TRUE;
6434#ifdef FEAT_VIRTUALEDIT
6435 /* Include a Tab for "tx" and for "dfx". */
6436 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6437 && (t_cmd || cap->oap->op_type != OP_NOP))
6438 {
6439 colnr_T scol, ecol;
6440
6441 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6442 curwin->w_cursor.coladd = ecol - scol;
6443 }
6444 else
6445 curwin->w_cursor.coladd = 0;
6446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448#ifdef FEAT_FOLDING
6449 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6450 foldOpenCursor();
6451#endif
6452 }
6453}
6454
6455/*
6456 * "[" and "]" commands.
6457 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6458 */
6459 static void
6460nv_brackets(cap)
6461 cmdarg_T *cap;
6462{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006463 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 pos_T prev_pos;
6465 pos_T *pos = NULL; /* init for GCC */
6466 pos_T old_pos; /* cursor position before command */
6467 int flag;
6468 long n;
6469 int findc;
6470 int c;
6471
6472 cap->oap->motion_type = MCHAR;
6473 cap->oap->inclusive = FALSE;
6474 old_pos = curwin->w_cursor;
6475#ifdef FEAT_VIRTUALEDIT
6476 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6477#endif
6478
6479#ifdef FEAT_SEARCHPATH
6480 /*
6481 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6482 */
6483 if (cap->nchar == 'f')
6484 nv_gotofile(cap);
6485 else
6486#endif
6487
6488#ifdef FEAT_FIND_ID
6489 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006490 * Find the occurrence(s) of the identifier or define under cursor
6491 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 *
6493 * search list jump
6494 * fwd bwd fwd bwd fwd bwd
6495 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6496 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6497 */
6498 if (vim_strchr((char_u *)
6499#ifdef EBCDIC
6500 "iI\005dD\067",
6501#else
6502 "iI\011dD\004",
6503#endif
6504 cap->nchar) != NULL)
6505 {
6506 char_u *ptr;
6507 int len;
6508
6509 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6510 clearop(cap->oap);
6511 else
6512 {
6513 find_pattern_in_path(ptr, 0, len, TRUE,
6514 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6515 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6516 cap->count1,
6517 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6518 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6519 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6520 (linenr_T)MAXLNUM);
6521 curwin->w_set_curswant = TRUE;
6522 }
6523 }
6524 else
6525#endif
6526
6527 /*
6528 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6529 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6530 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6531 * "[m" or "]m" search for prev/next start of (Java) method.
6532 * "[M" or "]M" search for prev/next end of (Java) method.
6533 */
6534 if ( (cap->cmdchar == '['
6535 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6536 || (cap->cmdchar == ']'
6537 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6538 {
6539 if (cap->nchar == '*')
6540 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 prev_pos.lnum = 0;
6542 if (cap->nchar == 'm' || cap->nchar == 'M')
6543 {
6544 if (cap->cmdchar == '[')
6545 findc = '{';
6546 else
6547 findc = '}';
6548 n = 9999;
6549 }
6550 else
6551 {
6552 findc = cap->nchar;
6553 n = cap->count1;
6554 }
6555 for ( ; n > 0; --n)
6556 {
6557 if ((pos = findmatchlimit(cap->oap, findc,
6558 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6559 {
6560 if (new_pos.lnum == 0) /* nothing found */
6561 {
6562 if (cap->nchar != 'm' && cap->nchar != 'M')
6563 clearopbeep(cap->oap);
6564 }
6565 else
6566 pos = &new_pos; /* use last one found */
6567 break;
6568 }
6569 prev_pos = new_pos;
6570 curwin->w_cursor = *pos;
6571 new_pos = *pos;
6572 }
6573 curwin->w_cursor = old_pos;
6574
6575 /*
6576 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6577 * brought us to the match for "[m" and "]M" when inside a method.
6578 * Try finding the '{' or '}' we want to be at.
6579 * Also repeat for the given count.
6580 */
6581 if (cap->nchar == 'm' || cap->nchar == 'M')
6582 {
6583 /* norm is TRUE for "]M" and "[m" */
6584 int norm = ((findc == '{') == (cap->nchar == 'm'));
6585
6586 n = cap->count1;
6587 /* found a match: we were inside a method */
6588 if (prev_pos.lnum != 0)
6589 {
6590 pos = &prev_pos;
6591 curwin->w_cursor = prev_pos;
6592 if (norm)
6593 --n;
6594 }
6595 else
6596 pos = NULL;
6597 while (n > 0)
6598 {
6599 for (;;)
6600 {
6601 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6602 {
6603 /* if not found anything, that's an error */
6604 if (pos == NULL)
6605 clearopbeep(cap->oap);
6606 n = 0;
6607 break;
6608 }
6609 c = gchar_cursor();
6610 if (c == '{' || c == '}')
6611 {
6612 /* Must have found end/start of class: use it.
6613 * Or found the place to be at. */
6614 if ((c == findc && norm) || (n == 1 && !norm))
6615 {
6616 new_pos = curwin->w_cursor;
6617 pos = &new_pos;
6618 n = 0;
6619 }
6620 /* if no match found at all, we started outside of the
6621 * class and we're inside now. Just go on. */
6622 else if (new_pos.lnum == 0)
6623 {
6624 new_pos = curwin->w_cursor;
6625 pos = &new_pos;
6626 }
6627 /* found start/end of other method: go to match */
6628 else if ((pos = findmatchlimit(cap->oap, findc,
6629 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6630 0)) == NULL)
6631 n = 0;
6632 else
6633 curwin->w_cursor = *pos;
6634 break;
6635 }
6636 }
6637 --n;
6638 }
6639 curwin->w_cursor = old_pos;
6640 if (pos == NULL && new_pos.lnum != 0)
6641 clearopbeep(cap->oap);
6642 }
6643 if (pos != NULL)
6644 {
6645 setpcmark();
6646 curwin->w_cursor = *pos;
6647 curwin->w_set_curswant = TRUE;
6648#ifdef FEAT_FOLDING
6649 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6650 && cap->oap->op_type == OP_NOP)
6651 foldOpenCursor();
6652#endif
6653 }
6654 }
6655
6656 /*
6657 * "[[", "[]", "]]" and "][": move to start or end of function
6658 */
6659 else if (cap->nchar == '[' || cap->nchar == ']')
6660 {
6661 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6662 flag = '{';
6663 else
6664 flag = '}'; /* "][" or "[]" */
6665
6666 curwin->w_set_curswant = TRUE;
6667 /*
6668 * Imitate strange Vi behaviour: When using "]]" with an operator
6669 * we also stop at '}'.
6670 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006671 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 (cap->oap->op_type != OP_NOP
6673 && cap->arg == FORWARD && flag == '{')))
6674 clearopbeep(cap->oap);
6675 else
6676 {
6677 if (cap->oap->op_type == OP_NOP)
6678 beginline(BL_WHITE | BL_FIX);
6679#ifdef FEAT_FOLDING
6680 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6681 foldOpenCursor();
6682#endif
6683 }
6684 }
6685
6686 /*
6687 * "[p", "[P", "]P" and "]p": put with indent adjustment
6688 */
6689 else if (cap->nchar == 'p' || cap->nchar == 'P')
6690 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006691 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006693 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6694 ? FORWARD : BACKWARD;
6695 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006696 int was_visual = VIsual_active;
6697 int line_count = curbuf->b_ml.ml_line_count;
6698 pos_T start, end;
6699
6700 if (VIsual_active)
6701 {
6702 start = ltoreq(VIsual, curwin->w_cursor)
6703 ? VIsual : curwin->w_cursor;
6704 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6705 curwin->w_cursor = (dir == BACKWARD ? start : end);
6706 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707# ifdef FEAT_CLIPBOARD
6708 adjust_clip_reg(&regname);
6709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006711
6712 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006713 if (was_visual)
6714 {
6715 VIsual = start;
6716 curwin->w_cursor = end;
6717 if (dir == BACKWARD)
6718 {
6719 /* adjust lines */
6720 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6721 curwin->w_cursor.lnum +=
6722 curbuf->b_ml.ml_line_count - line_count;
6723 }
6724
6725 VIsual_active = TRUE;
6726 if (VIsual_mode == 'V')
6727 {
6728 /* delete visually selected lines */
6729 cap->cmdchar = 'd';
6730 cap->nchar = NUL;
6731 cap->oap->regname = regname;
6732 nv_operator(cap);
6733 do_pending_operator(cap, 0, FALSE);
6734 }
6735 if (VIsual_active)
6736 {
6737 end_visual_mode();
6738 redraw_later(SOME_VALID);
6739 }
6740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742 }
6743
6744 /*
6745 * "['", "[`", "]'" and "]`": jump to next mark
6746 */
6747 else if (cap->nchar == '\'' || cap->nchar == '`')
6748 {
6749 pos = &curwin->w_cursor;
6750 for (n = cap->count1; n > 0; --n)
6751 {
6752 prev_pos = *pos;
6753 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6754 cap->nchar == '\'');
6755 if (pos == NULL)
6756 break;
6757 }
6758 if (pos == NULL)
6759 pos = &prev_pos;
6760 nv_cursormark(cap, cap->nchar == '\'', pos);
6761 }
6762
6763#ifdef FEAT_MOUSE
6764 /*
6765 * [ or ] followed by a middle mouse click: put selected text with
6766 * indent adjustment. Any other button just does as usual.
6767 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006768 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 (void)do_mouse(cap->oap, cap->nchar,
6771 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6772 cap->count1, PUT_FIXINDENT);
6773 }
6774#endif /* FEAT_MOUSE */
6775
6776#ifdef FEAT_FOLDING
6777 /*
6778 * "[z" and "]z": move to start or end of open fold.
6779 */
6780 else if (cap->nchar == 'z')
6781 {
6782 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6783 cap->count1) == FAIL)
6784 clearopbeep(cap->oap);
6785 }
6786#endif
6787
6788#ifdef FEAT_DIFF
6789 /*
6790 * "[c" and "]c": move to next or previous diff-change.
6791 */
6792 else if (cap->nchar == 'c')
6793 {
6794 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6795 cap->count1) == FAIL)
6796 clearopbeep(cap->oap);
6797 }
6798#endif
6799
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006800#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006801 /*
6802 * "[s", "[S", "]s" and "]S": move to next spell error.
6803 */
6804 else if (cap->nchar == 's' || cap->nchar == 'S')
6805 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006806 setpcmark();
6807 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006808 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6809 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006810 {
6811 clearopbeep(cap->oap);
6812 break;
6813 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814# ifdef FEAT_FOLDING
6815 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6816 foldOpenCursor();
6817# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006818 }
6819#endif
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 /* Not a valid cap->nchar. */
6822 else
6823 clearopbeep(cap->oap);
6824}
6825
6826/*
6827 * Handle Normal mode "%" command.
6828 */
6829 static void
6830nv_percent(cap)
6831 cmdarg_T *cap;
6832{
6833 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006834#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 linenr_T lnum = curwin->w_cursor.lnum;
6836#endif
6837
6838 cap->oap->inclusive = TRUE;
6839 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6840 {
6841 if (cap->count0 > 100)
6842 clearopbeep(cap->oap);
6843 else
6844 {
6845 cap->oap->motion_type = MLINE;
6846 setpcmark();
6847 /* Round up, so CTRL-G will give same value. Watch out for a
6848 * large line count, the line number must not go negative! */
6849 if (curbuf->b_ml.ml_line_count > 1000000)
6850 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6851 / 100L * cap->count0;
6852 else
6853 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6854 cap->count0 + 99L) / 100L;
6855 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6856 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6857 beginline(BL_SOL | BL_FIX);
6858 }
6859 }
6860 else /* "%" : go to matching paren */
6861 {
6862 cap->oap->motion_type = MCHAR;
6863 cap->oap->use_reg_one = TRUE;
6864 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6865 clearopbeep(cap->oap);
6866 else
6867 {
6868 setpcmark();
6869 curwin->w_cursor = *pos;
6870 curwin->w_set_curswant = TRUE;
6871#ifdef FEAT_VIRTUALEDIT
6872 curwin->w_cursor.coladd = 0;
6873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 }
6877#ifdef FEAT_FOLDING
6878 if (cap->oap->op_type == OP_NOP
6879 && lnum != curwin->w_cursor.lnum
6880 && (fdo_flags & FDO_PERCENT)
6881 && KeyTyped)
6882 foldOpenCursor();
6883#endif
6884}
6885
6886/*
6887 * Handle "(" and ")" commands.
6888 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6889 */
6890 static void
6891nv_brace(cap)
6892 cmdarg_T *cap;
6893{
6894 cap->oap->motion_type = MCHAR;
6895 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006896 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6897 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 curwin->w_set_curswant = TRUE;
6899
6900 if (findsent(cap->arg, cap->count1) == FAIL)
6901 clearopbeep(cap->oap);
6902 else
6903 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006904 /* Don't leave the cursor on the NUL past end of line. */
6905 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906#ifdef FEAT_VIRTUALEDIT
6907 curwin->w_cursor.coladd = 0;
6908#endif
6909#ifdef FEAT_FOLDING
6910 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6911 foldOpenCursor();
6912#endif
6913 }
6914}
6915
6916/*
6917 * "m" command: Mark a position.
6918 */
6919 static void
6920nv_mark(cap)
6921 cmdarg_T *cap;
6922{
6923 if (!checkclearop(cap->oap))
6924 {
6925 if (setmark(cap->nchar) == FAIL)
6926 clearopbeep(cap->oap);
6927 }
6928}
6929
6930/*
6931 * "{" and "}" commands.
6932 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6933 */
6934 static void
6935nv_findpar(cap)
6936 cmdarg_T *cap;
6937{
6938 cap->oap->motion_type = MCHAR;
6939 cap->oap->inclusive = FALSE;
6940 cap->oap->use_reg_one = TRUE;
6941 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006942 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 clearopbeep(cap->oap);
6944 else
6945 {
6946#ifdef FEAT_VIRTUALEDIT
6947 curwin->w_cursor.coladd = 0;
6948#endif
6949#ifdef FEAT_FOLDING
6950 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6951 foldOpenCursor();
6952#endif
6953 }
6954}
6955
6956/*
6957 * "u" command: Undo or make lower case.
6958 */
6959 static void
6960nv_undo(cap)
6961 cmdarg_T *cap;
6962{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006963 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6966 cap->cmdchar = 'g';
6967 cap->nchar = 'u';
6968 nv_operator(cap);
6969 }
6970 else
6971 nv_kundo(cap);
6972}
6973
6974/*
6975 * <Undo> command.
6976 */
6977 static void
6978nv_kundo(cap)
6979 cmdarg_T *cap;
6980{
6981 if (!checkclearopq(cap->oap))
6982 {
6983 u_undo((int)cap->count1);
6984 curwin->w_set_curswant = TRUE;
6985 }
6986}
6987
6988/*
6989 * Handle the "r" command.
6990 */
6991 static void
6992nv_replace(cap)
6993 cmdarg_T *cap;
6994{
6995 char_u *ptr;
6996 int had_ctrl_v;
6997 long n;
6998
6999 if (checkclearop(cap->oap))
7000 return;
7001
7002 /* get another character */
7003 if (cap->nchar == Ctrl_V)
7004 {
7005 had_ctrl_v = Ctrl_V;
7006 cap->nchar = get_literal();
7007 /* Don't redo a multibyte character with CTRL-V. */
7008 if (cap->nchar > DEL)
7009 had_ctrl_v = NUL;
7010 }
7011 else
7012 had_ctrl_v = NUL;
7013
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007014 /* Abort if the character is a special key. */
7015 if (IS_SPECIAL(cap->nchar))
7016 {
7017 clearopbeep(cap->oap);
7018 return;
7019 }
7020
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 /* Visual mode "r" */
7022 if (VIsual_active)
7023 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007024 if (got_int)
7025 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007026 if (had_ctrl_v)
7027 {
7028 if (cap->nchar == '\r')
7029 cap->nchar = -1;
7030 else if (cap->nchar == '\n')
7031 cap->nchar = -2;
7032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 nv_operator(cap);
7034 return;
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037#ifdef FEAT_VIRTUALEDIT
7038 /* Break tabs, etc. */
7039 if (virtual_active())
7040 {
7041 if (u_save_cursor() == FAIL)
7042 return;
7043 if (gchar_cursor() == NUL)
7044 {
7045 /* Add extra space and put the cursor on the first one. */
7046 coladvance_force((colnr_T)(getviscol() + cap->count1));
7047 curwin->w_cursor.col -= cap->count1;
7048 }
7049 else if (gchar_cursor() == TAB)
7050 coladvance_force(getviscol());
7051 }
7052#endif
7053
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007054 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007056 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#ifdef FEAT_MBYTE
7058 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7059#endif
7060 )
7061 {
7062 clearopbeep(cap->oap);
7063 return;
7064 }
7065
7066 /*
7067 * Replacing with a TAB is done by edit() when it is complicated because
7068 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7069 * Other characters are done below to avoid problems with things like
7070 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7071 */
7072 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7073 {
7074 stuffnumReadbuff(cap->count1);
7075 stuffcharReadbuff('R');
7076 stuffcharReadbuff('\t');
7077 stuffcharReadbuff(ESC);
7078 return;
7079 }
7080
7081 /* save line for undo */
7082 if (u_save_cursor() == FAIL)
7083 return;
7084
7085 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7086 {
7087 /*
7088 * Replace character(s) by a single newline.
7089 * Strange vi behaviour: Only one newline is inserted.
7090 * Delete the characters here.
7091 * Insert the newline with an insert command, takes care of
7092 * autoindent. The insert command depends on being on the last
7093 * character of a line or not.
7094 */
7095#ifdef FEAT_MBYTE
7096 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7097#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007098 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#endif
7100 stuffcharReadbuff('\r');
7101 stuffcharReadbuff(ESC);
7102
7103 /* Give 'r' to edit(), to get the redo command right. */
7104 invoke_edit(cap, TRUE, 'r', FALSE);
7105 }
7106 else
7107 {
7108 prep_redo(cap->oap->regname, cap->count1,
7109 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7110
7111 curbuf->b_op_start = curwin->w_cursor;
7112#ifdef FEAT_MBYTE
7113 if (has_mbyte)
7114 {
7115 int old_State = State;
7116
7117 if (cap->ncharC1 != 0)
7118 AppendCharToRedobuff(cap->ncharC1);
7119 if (cap->ncharC2 != 0)
7120 AppendCharToRedobuff(cap->ncharC2);
7121
7122 /* This is slow, but it handles replacing a single-byte with a
7123 * multi-byte and the other way around. Also handles adding
7124 * composing characters for utf-8. */
7125 for (n = cap->count1; n > 0; --n)
7126 {
7127 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007128 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7129 {
7130 int c = ins_copychar(curwin->w_cursor.lnum
7131 + (cap->nchar == Ctrl_Y ? -1 : 1));
7132 if (c != NUL)
7133 ins_char(c);
7134 else
7135 /* will be decremented further down */
7136 ++curwin->w_cursor.col;
7137 }
7138 else
7139 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 State = old_State;
7141 if (cap->ncharC1 != 0)
7142 ins_char(cap->ncharC1);
7143 if (cap->ncharC2 != 0)
7144 ins_char(cap->ncharC2);
7145 }
7146 }
7147 else
7148#endif
7149 {
7150 /*
7151 * Replace the characters within one line.
7152 */
7153 for (n = cap->count1; n > 0; --n)
7154 {
7155 /*
7156 * Get ptr again, because u_save and/or showmatch() will have
7157 * released the line. At the same time we let know that the
7158 * line will be changed.
7159 */
7160 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007161 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7162 {
7163 int c = ins_copychar(curwin->w_cursor.lnum
7164 + (cap->nchar == Ctrl_Y ? -1 : 1));
7165 if (c != NUL)
7166 ptr[curwin->w_cursor.col] = c;
7167 }
7168 else
7169 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 if (p_sm && msg_silent == 0)
7171 showmatch(cap->nchar);
7172 ++curwin->w_cursor.col;
7173 }
7174#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007175 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007177 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaar009b2592004-10-24 19:18:58 +00007179 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007180 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007182 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184#endif
7185
7186 /* mark the buffer as changed and prepare for displaying */
7187 changed_bytes(curwin->w_cursor.lnum,
7188 (colnr_T)(curwin->w_cursor.col - cap->count1));
7189 }
7190 --curwin->w_cursor.col; /* cursor on the last replaced char */
7191#ifdef FEAT_MBYTE
7192 /* if the character on the left of the current cursor is a multi-byte
7193 * character, move two characters left */
7194 if (has_mbyte)
7195 mb_adjust_cursor();
7196#endif
7197 curbuf->b_op_end = curwin->w_cursor;
7198 curwin->w_set_curswant = TRUE;
7199 set_last_insert(cap->nchar);
7200 }
7201}
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203/*
7204 * 'o': Exchange start and end of Visual area.
7205 * 'O': same, but in block mode exchange left and right corners.
7206 */
7207 static void
7208v_swap_corners(cmdchar)
7209 int cmdchar;
7210{
7211 pos_T old_cursor;
7212 colnr_T left, right;
7213
7214 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7215 {
7216 old_cursor = curwin->w_cursor;
7217 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7218 curwin->w_cursor.lnum = VIsual.lnum;
7219 coladvance(left);
7220 VIsual = curwin->w_cursor;
7221
7222 curwin->w_cursor.lnum = old_cursor.lnum;
7223 curwin->w_curswant = right;
7224 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7225 * right one column */
7226 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7227 ++curwin->w_curswant;
7228 coladvance(curwin->w_curswant);
7229 if (curwin->w_cursor.col == old_cursor.col
7230#ifdef FEAT_VIRTUALEDIT
7231 && (!virtual_active()
7232 || curwin->w_cursor.coladd == old_cursor.coladd)
7233#endif
7234 )
7235 {
7236 curwin->w_cursor.lnum = VIsual.lnum;
7237 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7238 ++right;
7239 coladvance(right);
7240 VIsual = curwin->w_cursor;
7241
7242 curwin->w_cursor.lnum = old_cursor.lnum;
7243 coladvance(left);
7244 curwin->w_curswant = left;
7245 }
7246 }
7247 else
7248 {
7249 old_cursor = curwin->w_cursor;
7250 curwin->w_cursor = VIsual;
7251 VIsual = old_cursor;
7252 curwin->w_set_curswant = TRUE;
7253 }
7254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256/*
7257 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7258 */
7259 static void
7260nv_Replace(cap)
7261 cmdarg_T *cap;
7262{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (VIsual_active) /* "R" is replace lines */
7264 {
7265 cap->cmdchar = 'c';
7266 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007267 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 VIsual_mode = 'V';
7269 nv_operator(cap);
7270 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007271 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 if (!curbuf->b_p_ma)
7274 EMSG(_(e_modifiable));
7275 else
7276 {
7277#ifdef FEAT_VIRTUALEDIT
7278 if (virtual_active())
7279 coladvance(getviscol());
7280#endif
7281 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7282 }
7283 }
7284}
7285
7286#ifdef FEAT_VREPLACE
7287/*
7288 * "gr".
7289 */
7290 static void
7291nv_vreplace(cap)
7292 cmdarg_T *cap;
7293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (VIsual_active)
7295 {
7296 cap->cmdchar = 'r';
7297 cap->nchar = cap->extra_char;
7298 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7299 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007300 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
7302 if (!curbuf->b_p_ma)
7303 EMSG(_(e_modifiable));
7304 else
7305 {
7306 if (cap->extra_char == Ctrl_V) /* get another character */
7307 cap->extra_char = get_literal();
7308 stuffcharReadbuff(cap->extra_char);
7309 stuffcharReadbuff(ESC);
7310# ifdef FEAT_VIRTUALEDIT
7311 if (virtual_active())
7312 coladvance(getviscol());
7313# endif
7314 invoke_edit(cap, TRUE, 'v', FALSE);
7315 }
7316 }
7317}
7318#endif
7319
7320/*
7321 * Swap case for "~" command, when it does not work like an operator.
7322 */
7323 static void
7324n_swapchar(cap)
7325 cmdarg_T *cap;
7326{
7327 long n;
7328 pos_T startpos;
7329 int did_change = 0;
7330#ifdef FEAT_NETBEANS_INTG
7331 pos_T pos;
7332 char_u *ptr;
7333 int count;
7334#endif
7335
7336 if (checkclearopq(cap->oap))
7337 return;
7338
7339 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7340 {
7341 clearopbeep(cap->oap);
7342 return;
7343 }
7344
7345 prep_redo_cmd(cap);
7346
7347 if (u_save_cursor() == FAIL)
7348 return;
7349
7350 startpos = curwin->w_cursor;
7351#ifdef FEAT_NETBEANS_INTG
7352 pos = startpos;
7353#endif
7354 for (n = cap->count1; n > 0; --n)
7355 {
7356 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7357 inc_cursor();
7358 if (gchar_cursor() == NUL)
7359 {
7360 if (vim_strchr(p_ww, '~') != NULL
7361 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7362 {
7363#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007364 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
7366 if (did_change)
7367 {
7368 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007369 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007370 netbeans_removed(curbuf, pos.lnum, pos.col,
7371 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007373 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 pos.col = 0;
7376 pos.lnum++;
7377 }
7378#endif
7379 ++curwin->w_cursor.lnum;
7380 curwin->w_cursor.col = 0;
7381 if (n > 1)
7382 {
7383 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7384 break;
7385 u_clearline();
7386 }
7387 }
7388 else
7389 break;
7390 }
7391 }
7392#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007393 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
7395 ptr = ml_get(pos.lnum);
7396 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007397 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7398 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400#endif
7401
7402
7403 check_cursor();
7404 curwin->w_set_curswant = TRUE;
7405 if (did_change)
7406 {
7407 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7408 0L);
7409 curbuf->b_op_start = startpos;
7410 curbuf->b_op_end = curwin->w_cursor;
7411 if (curbuf->b_op_end.col > 0)
7412 --curbuf->b_op_end.col;
7413 }
7414}
7415
7416/*
7417 * Move cursor to mark.
7418 */
7419 static void
7420nv_cursormark(cap, flag, pos)
7421 cmdarg_T *cap;
7422 int flag;
7423 pos_T *pos;
7424{
7425 if (check_mark(pos) == FAIL)
7426 clearop(cap->oap);
7427 else
7428 {
7429 if (cap->cmdchar == '\''
7430 || cap->cmdchar == '`'
7431 || cap->cmdchar == '['
7432 || cap->cmdchar == ']')
7433 setpcmark();
7434 curwin->w_cursor = *pos;
7435 if (flag)
7436 beginline(BL_WHITE | BL_FIX);
7437 else
7438 check_cursor();
7439 }
7440 cap->oap->motion_type = flag ? MLINE : MCHAR;
7441 if (cap->cmdchar == '`')
7442 cap->oap->use_reg_one = TRUE;
7443 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7444 curwin->w_set_curswant = TRUE;
7445}
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447/*
7448 * Handle commands that are operators in Visual mode.
7449 */
7450 static void
7451v_visop(cap)
7452 cmdarg_T *cap;
7453{
7454 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7455
7456 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007457 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (isupper(cap->cmdchar))
7459 {
7460 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007461 {
7462 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7466 curwin->w_curswant = MAXCOL;
7467 }
7468 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7469 nv_operator(cap);
7470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472/*
7473 * "s" and "S" commands.
7474 */
7475 static void
7476nv_subst(cap)
7477 cmdarg_T *cap;
7478{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7480 {
7481 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007482 {
7483 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 cap->cmdchar = 'c';
7487 nv_operator(cap);
7488 }
7489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 nv_optrans(cap);
7491}
7492
7493/*
7494 * Abbreviated commands.
7495 */
7496 static void
7497nv_abbrev(cap)
7498 cmdarg_T *cap;
7499{
7500 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7501 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 /* in Visual mode these commands are operators */
7504 if (VIsual_active)
7505 v_visop(cap);
7506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 nv_optrans(cap);
7508}
7509
7510/*
7511 * Translate a command into another command.
7512 */
7513 static void
7514nv_optrans(cap)
7515 cmdarg_T *cap;
7516{
7517 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7518 (char_u *)"d$", (char_u *)"c$",
7519 (char_u *)"cl", (char_u *)"cc",
7520 (char_u *)"yy", (char_u *)":s\r"};
7521 static char_u *str = (char_u *)"xXDCsSY&";
7522
7523 if (!checkclearopq(cap->oap))
7524 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007525 /* In Vi "2D" doesn't delete the next line. Can't translate it
7526 * either, because "2." should also not use the count. */
7527 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7528 {
7529 cap->oap->start = curwin->w_cursor;
7530 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007531#ifdef FEAT_EVAL
7532 set_op_var(OP_DELETE);
7533#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007534 cap->count1 = 1;
7535 nv_dollar(cap);
7536 finish_op = TRUE;
7537 ResetRedobuff();
7538 AppendCharToRedobuff('D');
7539 }
7540 else
7541 {
7542 if (cap->count0)
7543 stuffnumReadbuff(cap->count0);
7544 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547 cap->opcount = 0;
7548}
7549
7550/*
7551 * "'" and "`" commands. Also for "g'" and "g`".
7552 * cap->arg is TRUE for "'" and "g'".
7553 */
7554 static void
7555nv_gomark(cap)
7556 cmdarg_T *cap;
7557{
7558 pos_T *pos;
7559 int c;
7560#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007561 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7563#endif
7564
7565 if (cap->cmdchar == 'g')
7566 c = cap->extra_char;
7567 else
7568 c = cap->nchar;
7569 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7570 if (pos == (pos_T *)-1) /* jumped to other file */
7571 {
7572 if (cap->arg)
7573 {
7574 check_cursor_lnum();
7575 beginline(BL_WHITE | BL_FIX);
7576 }
7577 else
7578 check_cursor();
7579 }
7580 else
7581 nv_cursormark(cap, cap->arg, pos);
7582
7583#ifdef FEAT_VIRTUALEDIT
7584 /* May need to clear the coladd that a mark includes. */
7585 if (!virtual_active())
7586 curwin->w_cursor.coladd = 0;
7587#endif
7588#ifdef FEAT_FOLDING
7589 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007590 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007591 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 && (fdo_flags & FDO_MARK)
7593 && old_KeyTyped)
7594 foldOpenCursor();
7595#endif
7596}
7597
7598/*
7599 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7600 */
7601 static void
7602nv_pcmark(cap)
7603 cmdarg_T *cap;
7604{
7605#ifdef FEAT_JUMPLIST
7606 pos_T *pos;
7607# ifdef FEAT_FOLDING
7608 linenr_T lnum = curwin->w_cursor.lnum;
7609 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7610# endif
7611
7612 if (!checkclearopq(cap->oap))
7613 {
7614 if (cap->cmdchar == 'g')
7615 pos = movechangelist((int)cap->count1);
7616 else
7617 pos = movemark((int)cap->count1);
7618 if (pos == (pos_T *)-1) /* jump to other file */
7619 {
7620 curwin->w_set_curswant = TRUE;
7621 check_cursor();
7622 }
7623 else if (pos != NULL) /* can jump */
7624 nv_cursormark(cap, FALSE, pos);
7625 else if (cap->cmdchar == 'g')
7626 {
7627 if (curbuf->b_changelistlen == 0)
7628 EMSG(_("E664: changelist is empty"));
7629 else if (cap->count1 < 0)
7630 EMSG(_("E662: At start of changelist"));
7631 else
7632 EMSG(_("E663: At end of changelist"));
7633 }
7634 else
7635 clearopbeep(cap->oap);
7636# ifdef FEAT_FOLDING
7637 if (cap->oap->op_type == OP_NOP
7638 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7639 && (fdo_flags & FDO_MARK)
7640 && old_KeyTyped)
7641 foldOpenCursor();
7642# endif
7643 }
7644#else
7645 clearopbeep(cap->oap);
7646#endif
7647}
7648
7649/*
7650 * Handle '"' command.
7651 */
7652 static void
7653nv_regname(cap)
7654 cmdarg_T *cap;
7655{
7656 if (checkclearop(cap->oap))
7657 return;
7658#ifdef FEAT_EVAL
7659 if (cap->nchar == '=')
7660 cap->nchar = get_expr_register();
7661#endif
7662 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7663 {
7664 cap->oap->regname = cap->nchar;
7665 cap->opcount = cap->count0; /* remember count before '"' */
7666#ifdef FEAT_EVAL
7667 set_reg_var(cap->oap->regname);
7668#endif
7669 }
7670 else
7671 clearopbeep(cap->oap);
7672}
7673
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674/*
7675 * Handle "v", "V" and "CTRL-V" commands.
7676 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7677 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007678 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 */
7680 static void
7681nv_visual(cap)
7682 cmdarg_T *cap;
7683{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007684 if (cap->cmdchar == Ctrl_Q)
7685 cap->cmdchar = Ctrl_V;
7686
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7688 * characterwise, linewise, or blockwise. */
7689 if (cap->oap->op_type != OP_NOP)
7690 {
7691 cap->oap->motion_force = cap->cmdchar;
7692 finish_op = FALSE; /* operator doesn't finish now but later */
7693 return;
7694 }
7695
7696 VIsual_select = cap->arg;
7697 if (VIsual_active) /* change Visual mode */
7698 {
7699 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7700 end_visual_mode();
7701 else /* toggle char/block mode */
7702 { /* or char/line mode */
7703 VIsual_mode = cap->cmdchar;
7704 showmode();
7705 }
7706 redraw_curbuf_later(INVERTED); /* update the inversion */
7707 }
7708 else /* start Visual mode */
7709 {
7710 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007711 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007713 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 VIsual = curwin->w_cursor;
7715
7716 VIsual_active = TRUE;
7717 VIsual_reselect = TRUE;
7718 if (!cap->arg)
7719 /* start Select mode when 'selectmode' contains "cmd" */
7720 may_start_select('c');
7721#ifdef FEAT_MOUSE
7722 setmouse();
7723#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007724 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 redraw_cmdline = TRUE; /* show visual mode later */
7726 /*
7727 * For V and ^V, we multiply the number of lines even if there
7728 * was only one -- webb
7729 */
7730 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7731 {
7732 curwin->w_cursor.lnum +=
7733 resel_VIsual_line_count * cap->count0 - 1;
7734 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7735 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7736 }
7737 VIsual_mode = resel_VIsual_mode;
7738 if (VIsual_mode == 'v')
7739 {
7740 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007741 {
7742 validate_virtcol();
7743 curwin->w_curswant = curwin->w_virtcol
7744 + resel_VIsual_vcol * cap->count0 - 1;
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007747 curwin->w_curswant = resel_VIsual_vcol;
7748 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007750 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 curwin->w_curswant = MAXCOL;
7753 coladvance((colnr_T)MAXCOL);
7754 }
7755 else if (VIsual_mode == Ctrl_V)
7756 {
7757 validate_virtcol();
7758 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007759 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 coladvance(curwin->w_curswant);
7761 }
7762 else
7763 curwin->w_set_curswant = TRUE;
7764 redraw_curbuf_later(INVERTED); /* show the inversion */
7765 }
7766 else
7767 {
7768 if (!cap->arg)
7769 /* start Select mode when 'selectmode' contains "cmd" */
7770 may_start_select('c');
7771 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007772 if (VIsual_mode != 'V' && *p_sel == 'e')
7773 ++cap->count1; /* include one more char */
7774 if (cap->count0 > 0 && --cap->count1 > 0)
7775 {
7776 /* With a count select that many characters or lines. */
7777 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7778 nv_right(cap);
7779 else if (VIsual_mode == 'V')
7780 nv_down(cap);
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
7783 }
7784}
7785
7786/*
7787 * Start selection for Shift-movement keys.
7788 */
7789 void
7790start_selection()
7791{
7792 /* if 'selectmode' contains "key", start Select mode */
7793 may_start_select('k');
7794 n_start_visual_mode('v');
7795}
7796
7797/*
7798 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7799 */
7800 void
7801may_start_select(c)
7802 int c;
7803{
7804 VIsual_select = (stuff_empty() && typebuf_typed()
7805 && (vim_strchr(p_slm, c) != NULL));
7806}
7807
7808/*
7809 * Start Visual mode "c".
7810 * Should set VIsual_select before calling this.
7811 */
7812 static void
7813n_start_visual_mode(c)
7814 int c;
7815{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007816#ifdef FEAT_CONCEAL
7817 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007818 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007819#endif
7820
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 VIsual_mode = c;
7822 VIsual_active = TRUE;
7823 VIsual_reselect = TRUE;
7824#ifdef FEAT_VIRTUALEDIT
7825 /* Corner case: the 0 position in a tab may change when going into
7826 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7827 */
7828 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007829 {
7830 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833#endif
7834 VIsual = curwin->w_cursor;
7835
7836#ifdef FEAT_FOLDING
7837 foldAdjustVisual();
7838#endif
7839
7840#ifdef FEAT_MOUSE
7841 setmouse();
7842#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007843#ifdef FEAT_CONCEAL
7844 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007845 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007846#endif
7847
Bram Moolenaar09df3122006-01-23 22:23:09 +00007848 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 redraw_cmdline = TRUE; /* show visual mode later */
7850#ifdef FEAT_CLIPBOARD
7851 /* Make sure the clipboard gets updated. Needed because start and
7852 * end may still be the same, and the selection needs to be owned */
7853 clip_star.vmode = NUL;
7854#endif
7855
7856 /* Only need to redraw this line, unless still need to redraw an old
7857 * Visual area (when 'lazyredraw' is set). */
7858 if (curwin->w_redr_type < INVERTED)
7859 {
7860 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7861 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7862 }
7863}
7864
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
7866/*
7867 * CTRL-W: Window commands
7868 */
7869 static void
7870nv_window(cap)
7871 cmdarg_T *cap;
7872{
7873#ifdef FEAT_WINDOWS
7874 if (!checkclearop(cap->oap))
7875 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7876#else
7877 (void)checkclearop(cap->oap);
7878#endif
7879}
7880
7881/*
7882 * CTRL-Z: Suspend
7883 */
7884 static void
7885nv_suspend(cap)
7886 cmdarg_T *cap;
7887{
7888 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (VIsual_active)
7890 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 do_cmdline_cmd((char_u *)"st");
7892}
7893
7894/*
7895 * Commands starting with "g".
7896 */
7897 static void
7898nv_g_cmd(cap)
7899 cmdarg_T *cap;
7900{
7901 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 int i;
7904 int flag = FALSE;
7905
7906 switch (cap->nchar)
7907 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007908 case Ctrl_A:
7909 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#ifdef MEM_PROFILE
7911 /*
7912 * "g^A": dump log of used memory.
7913 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007914 if (!VIsual_active && cap->nchar == Ctrl_A)
7915 vim_mem_profile_dump();
7916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007918 /*
7919 * "g^A/g^X": sequentially increment visually selected region
7920 */
7921 if (VIsual_active)
7922 {
7923 cap->arg = TRUE;
7924 cap->cmdchar = cap->nchar;
7925 nv_addsub(cap);
7926 }
7927 else
7928 clearopbeep(oap);
7929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
7931#ifdef FEAT_VREPLACE
7932 /*
7933 * "gR": Enter virtual replace mode.
7934 */
7935 case 'R':
7936 cap->arg = TRUE;
7937 nv_Replace(cap);
7938 break;
7939
7940 case 'r':
7941 nv_vreplace(cap);
7942 break;
7943#endif
7944
7945 case '&':
7946 do_cmdline_cmd((char_u *)"%s//~/&");
7947 break;
7948
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 /*
7950 * "gv": Reselect the previous Visual area. If Visual already active,
7951 * exchange previous and current Visual area.
7952 */
7953 case 'v':
7954 if (checkclearop(oap))
7955 break;
7956
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007957 if ( curbuf->b_visual.vi_start.lnum == 0
7958 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7959 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 beep_flush();
7961 else
7962 {
7963 /* set w_cursor to the start of the Visual area, tpos to the end */
7964 if (VIsual_active)
7965 {
7966 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007967 VIsual_mode = curbuf->b_visual.vi_mode;
7968 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969# ifdef FEAT_EVAL
7970 curbuf->b_visual_mode_eval = i;
7971# endif
7972 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007973 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7974 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007976 tpos = curbuf->b_visual.vi_end;
7977 curbuf->b_visual.vi_end = curwin->w_cursor;
7978 curwin->w_cursor = curbuf->b_visual.vi_start;
7979 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981 else
7982 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007983 VIsual_mode = curbuf->b_visual.vi_mode;
7984 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7985 tpos = curbuf->b_visual.vi_end;
7986 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988
7989 VIsual_active = TRUE;
7990 VIsual_reselect = TRUE;
7991
7992 /* Set Visual to the start and w_cursor to the end of the Visual
7993 * area. Make sure they are on an existing character. */
7994 check_cursor();
7995 VIsual = curwin->w_cursor;
7996 curwin->w_cursor = tpos;
7997 check_cursor();
7998 update_topline();
7999 /*
8000 * When called from normal "g" command: start Select mode when
8001 * 'selectmode' contains "cmd". When called for K_SELECT, always
8002 * start Select mode.
8003 */
8004 if (cap->arg)
8005 VIsual_select = TRUE;
8006 else
8007 may_start_select('c');
8008#ifdef FEAT_MOUSE
8009 setmouse();
8010#endif
8011#ifdef FEAT_CLIPBOARD
8012 /* Make sure the clipboard gets updated. Needed because start and
8013 * end are still the same, and the selection needs to be owned */
8014 clip_star.vmode = NUL;
8015#endif
8016 redraw_curbuf_later(INVERTED);
8017 showmode();
8018 }
8019 break;
8020 /*
8021 * "gV": Don't reselect the previous Visual area after a Select mode
8022 * mapping of menu.
8023 */
8024 case 'V':
8025 VIsual_reselect = FALSE;
8026 break;
8027
8028 /*
8029 * "gh": start Select mode.
8030 * "gH": start Select line mode.
8031 * "g^H": start Select block mode.
8032 */
8033 case K_BS:
8034 cap->nchar = Ctrl_H;
8035 /* FALLTHROUGH */
8036 case 'h':
8037 case 'H':
8038 case Ctrl_H:
8039# ifdef EBCDIC
8040 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8041 if (cap->nchar == Ctrl_H)
8042 cap->cmdchar = Ctrl_V;
8043 else
8044# endif
8045 cap->cmdchar = cap->nchar + ('v' - 'h');
8046 cap->arg = TRUE;
8047 nv_visual(cap);
8048 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008049
8050 /* "gn", "gN" visually select next/previous search match
8051 * "gn" selects next match
8052 * "gN" selects previous match
8053 */
8054 case 'N':
8055 case 'n':
8056 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008057 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
8060 /*
8061 * "gj" and "gk" two new funny movement keys -- up and down
8062 * movement based on *screen* line rather than *file* line.
8063 */
8064 case 'j':
8065 case K_DOWN:
8066 /* with 'nowrap' it works just like the normal "j" command; also when
8067 * in a closed fold */
8068 if (!curwin->w_p_wrap
8069#ifdef FEAT_FOLDING
8070 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8071#endif
8072 )
8073 {
8074 oap->motion_type = MLINE;
8075 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8076 }
8077 else
8078 i = nv_screengo(oap, FORWARD, cap->count1);
8079 if (i == FAIL)
8080 clearopbeep(oap);
8081 break;
8082
8083 case 'k':
8084 case K_UP:
8085 /* with 'nowrap' it works just like the normal "k" command; also when
8086 * in a closed fold */
8087 if (!curwin->w_p_wrap
8088#ifdef FEAT_FOLDING
8089 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8090#endif
8091 )
8092 {
8093 oap->motion_type = MLINE;
8094 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8095 }
8096 else
8097 i = nv_screengo(oap, BACKWARD, cap->count1);
8098 if (i == FAIL)
8099 clearopbeep(oap);
8100 break;
8101
8102 /*
8103 * "gJ": join two lines without inserting a space.
8104 */
8105 case 'J':
8106 nv_join(cap);
8107 break;
8108
8109 /*
8110 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8111 * "gm": middle of "g0" and "g$".
8112 */
8113 case '^':
8114 flag = TRUE;
8115 /* FALLTHROUGH */
8116
8117 case '0':
8118 case 'm':
8119 case K_HOME:
8120 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 oap->motion_type = MCHAR;
8122 oap->inclusive = FALSE;
8123 if (curwin->w_p_wrap
8124#ifdef FEAT_VERTSPLIT
8125 && curwin->w_width != 0
8126#endif
8127 )
8128 {
8129 int width1 = W_WIDTH(curwin) - curwin_col_off();
8130 int width2 = width1 + curwin_col_off2();
8131
8132 validate_virtcol();
8133 i = 0;
8134 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8135 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8136 }
8137 else
8138 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008139 /* Go to the middle of the screen line. When 'number' or
8140 * 'relativenumber' is on and lines are wrapping the middle can be more
8141 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (cap->nchar == 'm')
8143 i += (W_WIDTH(curwin) - curwin_col_off()
8144 + ((curwin->w_p_wrap && i > 0)
8145 ? curwin_col_off2() : 0)) / 2;
8146 coladvance((colnr_T)i);
8147 if (flag)
8148 {
8149 do
8150 i = gchar_cursor();
8151 while (vim_iswhite(i) && oneright() == OK);
8152 }
8153 curwin->w_set_curswant = TRUE;
8154 break;
8155
8156 case '_':
8157 /* "g_": to the last non-blank character in the line or <count> lines
8158 * downward. */
8159 cap->oap->motion_type = MCHAR;
8160 cap->oap->inclusive = TRUE;
8161 curwin->w_curswant = MAXCOL;
8162 if (cursor_down((long)(cap->count1 - 1),
8163 cap->oap->op_type == OP_NOP) == FAIL)
8164 clearopbeep(cap->oap);
8165 else
8166 {
8167 char_u *ptr = ml_get_curline();
8168
8169 /* In Visual mode we may end up after the line. */
8170 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8171 --curwin->w_cursor.col;
8172
8173 /* Decrease the cursor column until it's on a non-blank. */
8174 while (curwin->w_cursor.col > 0
8175 && vim_iswhite(ptr[curwin->w_cursor.col]))
8176 --curwin->w_cursor.col;
8177 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008178 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 break;
8181
8182 case '$':
8183 case K_END:
8184 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {
8186 int col_off = curwin_col_off();
8187
8188 oap->motion_type = MCHAR;
8189 oap->inclusive = TRUE;
8190 if (curwin->w_p_wrap
8191#ifdef FEAT_VERTSPLIT
8192 && curwin->w_width != 0
8193#endif
8194 )
8195 {
8196 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8197 if (cap->count1 == 1)
8198 {
8199 int width1 = W_WIDTH(curwin) - col_off;
8200 int width2 = width1 + curwin_col_off2();
8201
8202 validate_virtcol();
8203 i = width1 - 1;
8204 if (curwin->w_virtcol >= (colnr_T)width1)
8205 i += ((curwin->w_virtcol - width1) / width2 + 1)
8206 * width2;
8207 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008208
8209 /* Make sure we stick in this column. */
8210 validate_virtcol();
8211 curwin->w_curswant = curwin->w_virtcol;
8212 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8214 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8215 {
8216 /*
8217 * Check for landing on a character that got split at
8218 * the end of the line. We do not want to advance to
8219 * the next screen line.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (curwin->w_virtcol > (colnr_T)i)
8222 --curwin->w_cursor.col;
8223 }
8224#endif
8225 }
8226 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8227 clearopbeep(oap);
8228 }
8229 else
8230 {
8231 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8232 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008233
8234 /* Make sure we stick in this column. */
8235 validate_virtcol();
8236 curwin->w_curswant = curwin->w_virtcol;
8237 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 }
8240 break;
8241
8242 /*
8243 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8244 */
8245 case '*':
8246 case '#':
8247#if POUND != '#'
8248 case POUND: /* pound sign (sometimes equal to '#') */
8249#endif
8250 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8251 case ']': /* :tselect for current identifier */
8252 nv_ident(cap);
8253 break;
8254
8255 /*
8256 * ge and gE: go back to end of word
8257 */
8258 case 'e':
8259 case 'E':
8260 oap->motion_type = MCHAR;
8261 curwin->w_set_curswant = TRUE;
8262 oap->inclusive = TRUE;
8263 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8264 clearopbeep(oap);
8265 break;
8266
8267 /*
8268 * "g CTRL-G": display info about cursor position
8269 */
8270 case Ctrl_G:
8271 cursor_pos_info();
8272 break;
8273
8274 /*
8275 * "gi": start Insert at the last position.
8276 */
8277 case 'i':
8278 if (curbuf->b_last_insert.lnum != 0)
8279 {
8280 curwin->w_cursor = curbuf->b_last_insert;
8281 check_cursor_lnum();
8282 i = (int)STRLEN(ml_get_curline());
8283 if (curwin->w_cursor.col > (colnr_T)i)
8284 {
8285#ifdef FEAT_VIRTUALEDIT
8286 if (virtual_active())
8287 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8288#endif
8289 curwin->w_cursor.col = i;
8290 }
8291 }
8292 cap->cmdchar = 'i';
8293 nv_edit(cap);
8294 break;
8295
8296 /*
8297 * "gI": Start insert in column 1.
8298 */
8299 case 'I':
8300 beginline(0);
8301 if (!checkclearopq(oap))
8302 invoke_edit(cap, FALSE, 'g', FALSE);
8303 break;
8304
8305#ifdef FEAT_SEARCHPATH
8306 /*
8307 * "gf": goto file, edit file under cursor
8308 * "]f" and "[f": can also be used.
8309 */
8310 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008311 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 nv_gotofile(cap);
8313 break;
8314#endif
8315
8316 /* "g'm" and "g`m": jump to mark without setting pcmark */
8317 case '\'':
8318 cap->arg = TRUE;
8319 /*FALLTHROUGH*/
8320 case '`':
8321 nv_gomark(cap);
8322 break;
8323
8324 /*
8325 * "gs": Goto sleep.
8326 */
8327 case 's':
8328 do_sleep(cap->count1 * 1000L);
8329 break;
8330
8331 /*
8332 * "ga": Display the ascii value of the character under the
8333 * cursor. It is displayed in decimal, hex, and octal. -- webb
8334 */
8335 case 'a':
8336 do_ascii(NULL);
8337 break;
8338
8339#ifdef FEAT_MBYTE
8340 /*
8341 * "g8": Display the bytes used for the UTF-8 character under the
8342 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008343 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 */
8345 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008346 if (cap->count0 == 8)
8347 utf_find_illegal();
8348 else
8349 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 break;
8351#endif
8352
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008353 case '<':
8354 show_sb_text();
8355 break;
8356
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 /*
8358 * "gg": Goto the first line in file. With a count it goes to
8359 * that line number like for "G". -- webb
8360 */
8361 case 'g':
8362 cap->arg = FALSE;
8363 nv_goto(cap);
8364 break;
8365
8366 /*
8367 * Two-character operators:
8368 * "gq" Format text
8369 * "gw" Format text and keep cursor position
8370 * "g~" Toggle the case of the text.
8371 * "gu" Change text to lower case.
8372 * "gU" Change text to upper case.
8373 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008374 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 */
8376 case 'q':
8377 case 'w':
8378 oap->cursor_start = curwin->w_cursor;
8379 /*FALLTHROUGH*/
8380 case '~':
8381 case 'u':
8382 case 'U':
8383 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008384 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 nv_operator(cap);
8386 break;
8387
8388 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008389 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 * current function
8391 * "gD": idem, but in the current file.
8392 */
8393 case 'd':
8394 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008395 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 break;
8397
8398#ifdef FEAT_MOUSE
8399 /*
8400 * g<*Mouse> : <C-*mouse>
8401 */
8402 case K_MIDDLEMOUSE:
8403 case K_MIDDLEDRAG:
8404 case K_MIDDLERELEASE:
8405 case K_LEFTMOUSE:
8406 case K_LEFTDRAG:
8407 case K_LEFTRELEASE:
8408 case K_RIGHTMOUSE:
8409 case K_RIGHTDRAG:
8410 case K_RIGHTRELEASE:
8411 case K_X1MOUSE:
8412 case K_X1DRAG:
8413 case K_X1RELEASE:
8414 case K_X2MOUSE:
8415 case K_X2DRAG:
8416 case K_X2RELEASE:
8417 mod_mask = MOD_MASK_CTRL;
8418 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8419 break;
8420#endif
8421
8422 case K_IGNORE:
8423 break;
8424
8425 /*
8426 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8427 */
8428 case 'p':
8429 case 'P':
8430 nv_put(cap);
8431 break;
8432
8433#ifdef FEAT_BYTEOFF
8434 /* "go": goto byte count from start of buffer */
8435 case 'o':
8436 goto_byte(cap->count0);
8437 break;
8438#endif
8439
8440 /* "gQ": improved Ex mode */
8441 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008442 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
8444 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008445 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 break;
8447 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 if (!checkclearopq(oap))
8450 do_exmode(TRUE);
8451 break;
8452
8453#ifdef FEAT_JUMPLIST
8454 case ',':
8455 nv_pcmark(cap);
8456 break;
8457
8458 case ';':
8459 cap->count1 = -cap->count1;
8460 nv_pcmark(cap);
8461 break;
8462#endif
8463
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008464#ifdef FEAT_WINDOWS
8465 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008466 if (!checkclearop(oap))
8467 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008468 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008469 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008470 if (!checkclearop(oap))
8471 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008472 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008473#endif
8474
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008475 case '+':
8476 case '-': /* "g+" and "g-": undo or redo along the timeline */
8477 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008478 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008479 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008480 break;
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 default:
8483 clearopbeep(oap);
8484 break;
8485 }
8486}
8487
8488/*
8489 * Handle "o" and "O" commands.
8490 */
8491 static void
8492n_opencmd(cap)
8493 cmdarg_T *cap;
8494{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008495#ifdef FEAT_CONCEAL
8496 linenr_T oldline = curwin->w_cursor.lnum;
8497#endif
8498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (!checkclearopq(cap->oap))
8500 {
8501#ifdef FEAT_FOLDING
8502 if (cap->cmdchar == 'O')
8503 /* Open above the first line of a folded sequence of lines */
8504 (void)hasFolding(curwin->w_cursor.lnum,
8505 &curwin->w_cursor.lnum, NULL);
8506 else
8507 /* Open below the last line of a folded sequence of lines */
8508 (void)hasFolding(curwin->w_cursor.lnum,
8509 NULL, &curwin->w_cursor.lnum);
8510#endif
8511 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8512 (cap->cmdchar == 'O' ? 1 : 0)),
8513 (linenr_T)(curwin->w_cursor.lnum +
8514 (cap->cmdchar == 'o' ? 1 : 0))
8515 ) == OK
8516 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8517#ifdef FEAT_COMMENTS
8518 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8519#endif
8520 0, 0))
8521 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008522#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008523 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008524 update_single_line(curwin, oldline);
8525#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008526 /* When '#' is in 'cpoptions' ignore the count. */
8527 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8528 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008529#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008530 if (curwin->w_p_cul)
8531 /* force redraw of cursorline */
8532 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8535 }
8536 }
8537}
8538
8539/*
8540 * "." command: redo last change.
8541 */
8542 static void
8543nv_dot(cap)
8544 cmdarg_T *cap;
8545{
8546 if (!checkclearopq(cap->oap))
8547 {
8548 /*
8549 * If "restart_edit" is TRUE, the last but one command is repeated
8550 * instead of the last command (inserting text). This is used for
8551 * CTRL-O <.> in insert mode.
8552 */
8553 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8554 clearopbeep(cap->oap);
8555 }
8556}
8557
8558/*
8559 * CTRL-R: undo undo
8560 */
8561 static void
8562nv_redo(cap)
8563 cmdarg_T *cap;
8564{
8565 if (!checkclearopq(cap->oap))
8566 {
8567 u_redo((int)cap->count1);
8568 curwin->w_set_curswant = TRUE;
8569 }
8570}
8571
8572/*
8573 * Handle "U" command.
8574 */
8575 static void
8576nv_Undo(cap)
8577 cmdarg_T *cap;
8578{
8579 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008580 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 {
8582 /* translate "gUU" to "gUgU" */
8583 cap->cmdchar = 'g';
8584 cap->nchar = 'U';
8585 nv_operator(cap);
8586 }
8587 else if (!checkclearopq(cap->oap))
8588 {
8589 u_undoline();
8590 curwin->w_set_curswant = TRUE;
8591 }
8592}
8593
8594/*
8595 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8596 * single character.
8597 */
8598 static void
8599nv_tilde(cap)
8600 cmdarg_T *cap;
8601{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008602 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 n_swapchar(cap);
8604 else
8605 nv_operator(cap);
8606}
8607
8608/*
8609 * Handle an operator command.
8610 * The actual work is done by do_pending_operator().
8611 */
8612 static void
8613nv_operator(cap)
8614 cmdarg_T *cap;
8615{
8616 int op_type;
8617
8618 op_type = get_op_type(cap->cmdchar, cap->nchar);
8619
8620 if (op_type == cap->oap->op_type) /* double operator works on lines */
8621 nv_lineop(cap);
8622 else if (!checkclearop(cap->oap))
8623 {
8624 cap->oap->start = curwin->w_cursor;
8625 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008626#ifdef FEAT_EVAL
8627 set_op_var(op_type);
8628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 }
8630}
8631
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008632#ifdef FEAT_EVAL
8633/*
8634 * Set v:operator to the characters for "optype".
8635 */
8636 static void
8637set_op_var(optype)
8638 int optype;
8639{
8640 char_u opchars[3];
8641
8642 if (optype == OP_NOP)
8643 set_vim_var_string(VV_OP, NULL, 0);
8644 else
8645 {
8646 opchars[0] = get_op_char(optype);
8647 opchars[1] = get_extra_op_char(optype);
8648 opchars[2] = NUL;
8649 set_vim_var_string(VV_OP, opchars, -1);
8650 }
8651}
8652#endif
8653
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654/*
8655 * Handle linewise operator "dd", "yy", etc.
8656 *
8657 * "_" is is a strange motion command that helps make operators more logical.
8658 * It is actually implemented, but not documented in the real Vi. This motion
8659 * command actually refers to "the current line". Commands like "dd" and "yy"
8660 * are really an alternate form of "d_" and "y_". It does accept a count, so
8661 * "d3_" works to delete 3 lines.
8662 */
8663 static void
8664nv_lineop(cap)
8665 cmdarg_T *cap;
8666{
8667 cap->oap->motion_type = MLINE;
8668 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8669 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008670 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8671 && cap->oap->motion_force != 'v'
8672 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 || cap->oap->op_type == OP_LSHIFT
8674 || cap->oap->op_type == OP_RSHIFT)
8675 beginline(BL_SOL | BL_FIX);
8676 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8677 beginline(BL_WHITE | BL_FIX);
8678}
8679
8680/*
8681 * <Home> command.
8682 */
8683 static void
8684nv_home(cap)
8685 cmdarg_T *cap;
8686{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008687 /* CTRL-HOME is like "gg" */
8688 if (mod_mask & MOD_MASK_CTRL)
8689 nv_goto(cap);
8690 else
8691 {
8692 cap->count0 = 1;
8693 nv_pipe(cap);
8694 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008695 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8696 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699/*
8700 * "|" command.
8701 */
8702 static void
8703nv_pipe(cap)
8704 cmdarg_T *cap;
8705{
8706 cap->oap->motion_type = MCHAR;
8707 cap->oap->inclusive = FALSE;
8708 beginline(0);
8709 if (cap->count0 > 0)
8710 {
8711 coladvance((colnr_T)(cap->count0 - 1));
8712 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8713 }
8714 else
8715 curwin->w_curswant = 0;
8716 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008717 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 curwin->w_set_curswant = FALSE;
8719}
8720
8721/*
8722 * Handle back-word command "b" and "B".
8723 * cap->arg is 1 for "B"
8724 */
8725 static void
8726nv_bck_word(cap)
8727 cmdarg_T *cap;
8728{
8729 cap->oap->motion_type = MCHAR;
8730 cap->oap->inclusive = FALSE;
8731 curwin->w_set_curswant = TRUE;
8732 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8733 clearopbeep(cap->oap);
8734#ifdef FEAT_FOLDING
8735 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8736 foldOpenCursor();
8737#endif
8738}
8739
8740/*
8741 * Handle word motion commands "e", "E", "w" and "W".
8742 * cap->arg is TRUE for "E" and "W".
8743 */
8744 static void
8745nv_wordcmd(cap)
8746 cmdarg_T *cap;
8747{
8748 int n;
8749 int word_end;
8750 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008751 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 /*
8754 * Set inclusive for the "E" and "e" command.
8755 */
8756 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8757 word_end = TRUE;
8758 else
8759 word_end = FALSE;
8760 cap->oap->inclusive = word_end;
8761
8762 /*
8763 * "cw" and "cW" are a special case.
8764 */
8765 if (!word_end && cap->oap->op_type == OP_CHANGE)
8766 {
8767 n = gchar_cursor();
8768 if (n != NUL) /* not an empty line */
8769 {
8770 if (vim_iswhite(n))
8771 {
8772 /*
8773 * Reproduce a funny Vi behaviour: "cw" on a blank only
8774 * changes one character, not all blanks until the start of
8775 * the next word. Only do this when the 'w' flag is included
8776 * in 'cpoptions'.
8777 */
8778 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8779 {
8780 cap->oap->inclusive = TRUE;
8781 cap->oap->motion_type = MCHAR;
8782 return;
8783 }
8784 }
8785 else
8786 {
8787 /*
8788 * This is a little strange. To match what the real Vi does,
8789 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8790 * that we are not on a space or a TAB. This seems impolite
8791 * at first, but it's really more what we mean when we say
8792 * 'cw'.
8793 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008794 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * will change only one character! This is done by setting
8796 * flag.
8797 */
8798 cap->oap->inclusive = TRUE;
8799 word_end = TRUE;
8800 flag = TRUE;
8801 }
8802 }
8803 }
8804
8805 cap->oap->motion_type = MCHAR;
8806 curwin->w_set_curswant = TRUE;
8807 if (word_end)
8808 n = end_word(cap->count1, cap->arg, flag, FALSE);
8809 else
8810 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8811
Bram Moolenaardfefb982008-04-01 10:06:39 +00008812 /* Don't leave the cursor on the NUL past the end of line. Unless we
8813 * didn't move it forward. */
8814 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008815 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816
8817 if (n == FAIL && cap->oap->op_type == OP_NOP)
8818 clearopbeep(cap->oap);
8819 else
8820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822#ifdef FEAT_FOLDING
8823 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8824 foldOpenCursor();
8825#endif
8826 }
8827}
8828
8829/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008830 * Used after a movement command: If the cursor ends up on the NUL after the
8831 * end of the line, may move it back to the last character and make the motion
8832 * inclusive.
8833 */
8834 static void
8835adjust_cursor(oap)
8836 oparg_T *oap;
8837{
8838 /* The cursor cannot remain on the NUL when:
8839 * - the column is > 0
8840 * - not in Visual mode or 'selection' is "o"
8841 * - 'virtualedit' is not "all" and not "onemore".
8842 */
8843 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008844 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008845#ifdef FEAT_VIRTUALEDIT
8846 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8847#endif
8848 )
8849 {
8850 --curwin->w_cursor.col;
8851#ifdef FEAT_MBYTE
8852 /* prevent cursor from moving on the trail byte */
8853 if (has_mbyte)
8854 mb_adjust_cursor();
8855#endif
8856 oap->inclusive = TRUE;
8857 }
8858}
8859
8860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 * "0" and "^" commands.
8862 * cap->arg is the argument for beginline().
8863 */
8864 static void
8865nv_beginline(cap)
8866 cmdarg_T *cap;
8867{
8868 cap->oap->motion_type = MCHAR;
8869 cap->oap->inclusive = FALSE;
8870 beginline(cap->arg);
8871#ifdef FEAT_FOLDING
8872 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8873 foldOpenCursor();
8874#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008875 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8876 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877}
8878
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879/*
8880 * In exclusive Visual mode, may include the last character.
8881 */
8882 static void
8883adjust_for_sel(cap)
8884 cmdarg_T *cap;
8885{
8886 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8887 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8888 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008889#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (has_mbyte)
8891 inc_cursor();
8892 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 ++curwin->w_cursor.col;
8895 cap->oap->inclusive = FALSE;
8896 }
8897}
8898
8899/*
8900 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8901 * Should check VIsual_mode before calling this.
8902 * Returns TRUE when backed up to the previous line.
8903 */
8904 static int
8905unadjust_for_sel()
8906{
8907 pos_T *pp;
8908
8909 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8910 {
8911 if (lt(VIsual, curwin->w_cursor))
8912 pp = &curwin->w_cursor;
8913 else
8914 pp = &VIsual;
8915#ifdef FEAT_VIRTUALEDIT
8916 if (pp->coladd > 0)
8917 --pp->coladd;
8918 else
8919#endif
8920 if (pp->col > 0)
8921 {
8922 --pp->col;
8923#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008924 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
8926 }
8927 else if (pp->lnum > 1)
8928 {
8929 --pp->lnum;
8930 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8931 return TRUE;
8932 }
8933 }
8934 return FALSE;
8935}
8936
8937/*
8938 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8939 */
8940 static void
8941nv_select(cap)
8942 cmdarg_T *cap;
8943{
8944 if (VIsual_active)
8945 VIsual_select = TRUE;
8946 else if (VIsual_reselect)
8947 {
8948 cap->nchar = 'v'; /* fake "gv" command */
8949 cap->arg = TRUE;
8950 nv_g_cmd(cap);
8951 }
8952}
8953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
8955/*
8956 * "G", "gg", CTRL-END, CTRL-HOME.
8957 * cap->arg is TRUE for "G".
8958 */
8959 static void
8960nv_goto(cap)
8961 cmdarg_T *cap;
8962{
8963 linenr_T lnum;
8964
8965 if (cap->arg)
8966 lnum = curbuf->b_ml.ml_line_count;
8967 else
8968 lnum = 1L;
8969 cap->oap->motion_type = MLINE;
8970 setpcmark();
8971
8972 /* When a count is given, use it instead of the default lnum */
8973 if (cap->count0 != 0)
8974 lnum = cap->count0;
8975 if (lnum < 1L)
8976 lnum = 1L;
8977 else if (lnum > curbuf->b_ml.ml_line_count)
8978 lnum = curbuf->b_ml.ml_line_count;
8979 curwin->w_cursor.lnum = lnum;
8980 beginline(BL_SOL | BL_FIX);
8981#ifdef FEAT_FOLDING
8982 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8983 foldOpenCursor();
8984#endif
8985}
8986
8987/*
8988 * CTRL-\ in Normal mode.
8989 */
8990 static void
8991nv_normal(cap)
8992 cmdarg_T *cap;
8993{
8994 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8995 {
8996 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008997 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 clear_cmdline = TRUE; /* unshow mode later */
8999 restart_edit = 0;
9000#ifdef FEAT_CMDWIN
9001 if (cmdwin_type != 0)
9002 cmdwin_result = Ctrl_C;
9003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 if (VIsual_active)
9005 {
9006 end_visual_mode(); /* stop Visual */
9007 redraw_curbuf_later(INVERTED);
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9010 if (cap->nchar == Ctrl_G && p_im)
9011 restart_edit = 'a';
9012 }
9013 else
9014 clearopbeep(cap->oap);
9015}
9016
9017/*
9018 * ESC in Normal mode: beep, but don't flush buffers.
9019 * Don't even beep if we are canceling a command.
9020 */
9021 static void
9022nv_esc(cap)
9023 cmdarg_T *cap;
9024{
9025 int no_reason;
9026
9027 no_reason = (cap->oap->op_type == OP_NOP
9028 && cap->opcount == 0
9029 && cap->count0 == 0
9030 && cap->oap->regname == 0
9031 && !p_im);
9032
9033 if (cap->arg) /* TRUE for CTRL-C */
9034 {
9035 if (restart_edit == 0
9036#ifdef FEAT_CMDWIN
9037 && cmdwin_type == 0
9038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 && no_reason)
9041 MSG(_("Type :quit<Enter> to exit Vim"));
9042
9043 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9044 * set again below when halfway a mapping. */
9045 if (!p_im)
9046 restart_edit = 0;
9047#ifdef FEAT_CMDWIN
9048 if (cmdwin_type != 0)
9049 {
9050 cmdwin_result = K_IGNORE;
9051 got_int = FALSE; /* don't stop executing autocommands et al. */
9052 return;
9053 }
9054#endif
9055 }
9056
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 if (VIsual_active)
9058 {
9059 end_visual_mode(); /* stop Visual */
9060 check_cursor_col(); /* make sure cursor is not beyond EOL */
9061 curwin->w_set_curswant = TRUE;
9062 redraw_curbuf_later(INVERTED);
9063 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009064 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009065 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 clearop(cap->oap);
9067
9068 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9069 * set return to Insert mode afterwards. */
9070 if (restart_edit == 0 && goto_im()
9071#ifdef FEAT_EX_EXTRA
9072 && ex_normal_busy == 0
9073#endif
9074 )
9075 restart_edit = 'a';
9076}
9077
9078/*
9079 * Handle "A", "a", "I", "i" and <Insert> commands.
9080 */
9081 static void
9082nv_edit(cap)
9083 cmdarg_T *cap;
9084{
9085 /* <Insert> is equal to "i" */
9086 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9087 cap->cmdchar = 'i';
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 /* in Visual mode "A" and "I" are an operator */
9090 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9091 v_visop(cap);
9092
9093 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009094 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9095 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 {
9097#ifdef FEAT_TEXTOBJ
9098 nv_object(cap);
9099#else
9100 clearopbeep(cap->oap);
9101#endif
9102 }
9103 else if (!curbuf->b_p_ma && !p_im)
9104 {
9105 /* Only give this error when 'insertmode' is off. */
9106 EMSG(_(e_modifiable));
9107 clearop(cap->oap);
9108 }
9109 else if (!checkclearopq(cap->oap))
9110 {
9111 switch (cap->cmdchar)
9112 {
9113 case 'A': /* "A"ppend after the line */
9114 curwin->w_set_curswant = TRUE;
9115#ifdef FEAT_VIRTUALEDIT
9116 if (ve_flags == VE_ALL)
9117 {
9118 int save_State = State;
9119
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009120 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 * character past the end of the line */
9122 State = INSERT;
9123 coladvance((colnr_T)MAXCOL);
9124 State = save_State;
9125 }
9126 else
9127#endif
9128 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9129 break;
9130
9131 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009132 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9133 beginline(BL_WHITE);
9134 else
9135 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 break;
9137
9138 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9139#ifdef FEAT_VIRTUALEDIT
9140 /* increment coladd when in virtual space, increment the
9141 * column otherwise, also to append after an unprintable char */
9142 if (virtual_active()
9143 && (curwin->w_cursor.coladd > 0
9144 || *ml_get_cursor() == NUL
9145 || *ml_get_cursor() == TAB))
9146 curwin->w_cursor.coladd++;
9147 else
9148#endif
9149 if (*ml_get_cursor() != NUL)
9150 inc_cursor();
9151 break;
9152 }
9153
9154#ifdef FEAT_VIRTUALEDIT
9155 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9156 {
9157 int save_State = State;
9158
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009159 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 * character past the end of the line */
9161 State = INSERT;
9162 coladvance(getviscol());
9163 State = save_State;
9164 }
9165#endif
9166
9167 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9168 }
9169}
9170
9171/*
9172 * Invoke edit() and take care of "restart_edit" and the return value.
9173 */
9174 static void
9175invoke_edit(cap, repl, cmd, startln)
9176 cmdarg_T *cap;
9177 int repl; /* "r" or "gr" command */
9178 int cmd;
9179 int startln;
9180{
9181 int restart_edit_save = 0;
9182
9183 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9184 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9185 * it. */
9186 if (repl || !stuff_empty())
9187 restart_edit_save = restart_edit;
9188 else
9189 restart_edit_save = 0;
9190
9191 /* Always reset "restart_edit", this is not a restarted edit. */
9192 restart_edit = 0;
9193
9194 if (edit(cmd, startln, cap->count1))
9195 cap->retval |= CA_COMMAND_BUSY;
9196
9197 if (restart_edit == 0)
9198 restart_edit = restart_edit_save;
9199}
9200
9201#ifdef FEAT_TEXTOBJ
9202/*
9203 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9204 */
9205 static void
9206nv_object(cap)
9207 cmdarg_T *cap;
9208{
9209 int flag;
9210 int include;
9211 char_u *mps_save;
9212
9213 if (cap->cmdchar == 'i')
9214 include = FALSE; /* "ix" = inner object: exclude white space */
9215 else
9216 include = TRUE; /* "ax" = an object: include white space */
9217
9218 /* Make sure (), [], {} and <> are in 'matchpairs' */
9219 mps_save = curbuf->b_p_mps;
9220 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9221
9222 switch (cap->nchar)
9223 {
9224 case 'w': /* "aw" = a word */
9225 flag = current_word(cap->oap, cap->count1, include, FALSE);
9226 break;
9227 case 'W': /* "aW" = a WORD */
9228 flag = current_word(cap->oap, cap->count1, include, TRUE);
9229 break;
9230 case 'b': /* "ab" = a braces block */
9231 case '(':
9232 case ')':
9233 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9234 break;
9235 case 'B': /* "aB" = a Brackets block */
9236 case '{':
9237 case '}':
9238 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9239 break;
9240 case '[': /* "a[" = a [] block */
9241 case ']':
9242 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9243 break;
9244 case '<': /* "a<" = a <> block */
9245 case '>':
9246 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9247 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009248 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009249 /* Do not adjust oap->end in do_pending_operator()
9250 * otherwise there are different results for 'dit'
9251 * (note leading whitespace in last line):
9252 * 1) <b> 2) <b>
9253 * foobar foobar
9254 * </b> </b>
9255 */
9256 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009257 flag = current_tagblock(cap->oap, cap->count1, include);
9258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 case 'p': /* "ap" = a paragraph */
9260 flag = current_par(cap->oap, cap->count1, include, 'p');
9261 break;
9262 case 's': /* "as" = a sentence */
9263 flag = current_sent(cap->oap, cap->count1, include);
9264 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009265 case '"': /* "a"" = a double quoted string */
9266 case '\'': /* "a'" = a single quoted string */
9267 case '`': /* "a`" = a backtick quoted string */
9268 flag = current_quote(cap->oap, cap->count1, include,
9269 cap->nchar);
9270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271#if 0 /* TODO */
9272 case 'S': /* "aS" = a section */
9273 case 'f': /* "af" = a filename */
9274 case 'u': /* "au" = a URL */
9275#endif
9276 default:
9277 flag = FAIL;
9278 break;
9279 }
9280
9281 curbuf->b_p_mps = mps_save;
9282 if (flag == FAIL)
9283 clearopbeep(cap->oap);
9284 adjust_cursor_col();
9285 curwin->w_set_curswant = TRUE;
9286}
9287#endif
9288
9289/*
9290 * "q" command: Start/stop recording.
9291 * "q:", "q/", "q?": edit command-line in command-line window.
9292 */
9293 static void
9294nv_record(cap)
9295 cmdarg_T *cap;
9296{
9297 if (cap->oap->op_type == OP_FORMAT)
9298 {
9299 /* "gqq" is the same as "gqgq": format line */
9300 cap->cmdchar = 'g';
9301 cap->nchar = 'q';
9302 nv_operator(cap);
9303 }
9304 else if (!checkclearop(cap->oap))
9305 {
9306#ifdef FEAT_CMDWIN
9307 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9308 {
9309 stuffcharReadbuff(cap->nchar);
9310 stuffcharReadbuff(K_CMDWIN);
9311 }
9312 else
9313#endif
9314 /* (stop) recording into a named register, unless executing a
9315 * register */
9316 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9317 clearopbeep(cap->oap);
9318 }
9319}
9320
9321/*
9322 * Handle the "@r" command.
9323 */
9324 static void
9325nv_at(cap)
9326 cmdarg_T *cap;
9327{
9328 if (checkclearop(cap->oap))
9329 return;
9330#ifdef FEAT_EVAL
9331 if (cap->nchar == '=')
9332 {
9333 if (get_expr_register() == NUL)
9334 return;
9335 }
9336#endif
9337 while (cap->count1-- && !got_int)
9338 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009339 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 {
9341 clearopbeep(cap->oap);
9342 break;
9343 }
9344 line_breakcheck();
9345 }
9346}
9347
9348/*
9349 * Handle the CTRL-U and CTRL-D commands.
9350 */
9351 static void
9352nv_halfpage(cap)
9353 cmdarg_T *cap;
9354{
9355 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9356 || (cap->cmdchar == Ctrl_D
9357 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9358 clearopbeep(cap->oap);
9359 else if (!checkclearop(cap->oap))
9360 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9361}
9362
9363/*
9364 * Handle "J" or "gJ" command.
9365 */
9366 static void
9367nv_join(cap)
9368 cmdarg_T *cap;
9369{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (VIsual_active) /* join the visual lines */
9371 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009372 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 {
9374 if (cap->count0 <= 1)
9375 cap->count0 = 2; /* default for join is two lines! */
9376 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9377 curbuf->b_ml.ml_line_count)
9378 clearopbeep(cap->oap); /* beyond last line */
9379 else
9380 {
9381 prep_redo(cap->oap->regname, cap->count0,
9382 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009383 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 }
9385 }
9386}
9387
9388/*
9389 * "P", "gP", "p" and "gp" commands.
9390 */
9391 static void
9392nv_put(cap)
9393 cmdarg_T *cap;
9394{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 int regname = 0;
9396 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009397 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009398 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 int dir;
9400 int flags = 0;
9401
9402 if (cap->oap->op_type != OP_NOP)
9403 {
9404#ifdef FEAT_DIFF
9405 /* "dp" is ":diffput" */
9406 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9407 {
9408 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009409 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 else
9412#endif
9413 clearopbeep(cap->oap);
9414 }
9415 else
9416 {
9417 dir = (cap->cmdchar == 'P'
9418 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9419 ? BACKWARD : FORWARD;
9420 prep_redo_cmd(cap);
9421 if (cap->cmdchar == 'g')
9422 flags |= PUT_CURSEND;
9423
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 if (VIsual_active)
9425 {
9426 /* Putting in Visual mode: The put text replaces the selected
9427 * text. First delete the selected text, then put the new text.
9428 * Need to save and restore the registers that the delete
9429 * overwrites if the old contents is being put.
9430 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009431 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009433#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009435#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009436 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009437 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009438#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442 )
9443 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009444 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * put, save it first. */
9446 reg1 = get_register(regname, TRUE);
9447 }
9448
9449 /* Now delete the selected text. */
9450 cap->cmdchar = 'd';
9451 cap->nchar = NUL;
9452 cap->oap->regname = NUL;
9453 nv_operator(cap);
9454 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009455 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456
9457 /* delete PUT_LINE_BACKWARD; */
9458 cap->oap->regname = regname;
9459
9460 if (reg1 != NULL)
9461 {
9462 /* Delete probably changed the register we want to put, save
9463 * it first. Then put back what was there before the delete. */
9464 reg2 = get_register(regname, FALSE);
9465 put_register(regname, reg1);
9466 }
9467
9468 /* When deleted a linewise Visual area, put the register as
9469 * lines to avoid it joined with the next line. When deletion was
9470 * characterwise, split a line when putting lines. */
9471 if (VIsual_mode == 'V')
9472 flags |= PUT_LINE;
9473 else if (VIsual_mode == 'v')
9474 flags |= PUT_LINE_SPLIT;
9475 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9476 flags |= PUT_LINE_FORWARD;
9477 dir = BACKWARD;
9478 if ((VIsual_mode != 'V'
9479 && curwin->w_cursor.col < curbuf->b_op_start.col)
9480 || (VIsual_mode == 'V'
9481 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9482 /* cursor is at the end of the line or end of file, put
9483 * forward. */
9484 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009485 /* May have been reset in do_put(). */
9486 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 do_put(cap->oap->regname, dir, cap->count1, flags);
9489
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 /* If a register was saved, put it back now. */
9491 if (reg2 != NULL)
9492 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009493
9494 /* What to reselect with "gv"? Selecting the just put text seems to
9495 * be the most useful, since the original text was removed. */
9496 if (was_visual)
9497 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009498 curbuf->b_visual.vi_start = curbuf->b_op_start;
9499 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009500 }
9501
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009502 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009503 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009504 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009505 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009506 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009507
9508 /* If the cursor was in that line, move it to the end of the last
9509 * line. */
9510 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9511 {
9512 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9513 coladvance((colnr_T)MAXCOL);
9514 }
9515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 auto_format(FALSE, TRUE);
9517 }
9518}
9519
9520/*
9521 * "o" and "O" commands.
9522 */
9523 static void
9524nv_open(cap)
9525 cmdarg_T *cap;
9526{
9527#ifdef FEAT_DIFF
9528 /* "do" is ":diffget" */
9529 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9530 {
9531 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009532 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 }
9534 else
9535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 if (VIsual_active) /* switch start and end of visual */
9537 v_swap_corners(cap->cmdchar);
9538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 n_opencmd(cap);
9540}
9541
9542#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 static void
9544nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009545 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547 ProcessSniffRequests();
9548}
9549#endif
9550
9551#ifdef FEAT_NETBEANS_INTG
9552 static void
9553nv_nbcmd(cap)
9554 cmdarg_T *cap;
9555{
9556 netbeans_keycommand(cap->nchar);
9557}
9558#endif
9559
9560#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 static void
9562nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009563 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9566}
9567#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009568
9569#ifdef FEAT_AUTOCMD
9570/*
9571 * Trigger CursorHold event.
9572 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9573 * input buffer. "did_cursorhold" is set to avoid retriggering.
9574 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009575 static void
9576nv_cursorhold(cap)
9577 cmdarg_T *cap;
9578{
9579 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9580 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009581 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009582}
9583#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009584
9585/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009586 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009587 */
9588 static void
9589get_op_vcol(oap, redo_VIsual_vcol, initial)
9590 oparg_T *oap;
9591 colnr_T redo_VIsual_vcol;
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009592 int initial; /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009593{
9594 colnr_T start, end;
9595
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009596 if (VIsual_mode != Ctrl_V
9597 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009598 return;
9599
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009600 oap->block_mode = VIsual_active;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009601
9602#ifdef FEAT_MBYTE
9603 /* prevent from moving onto a trail byte */
9604 if (has_mbyte)
9605 mb_adjustpos(curwin->w_buffer, &oap->end);
9606#endif
9607
9608 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009609
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009610 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009611 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009612 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9613
9614 if (start < oap->start_vcol)
9615 oap->start_vcol = start;
9616 if (end > oap->end_vcol)
9617 {
9618 if (initial && *p_sel == 'e' && start >= 1
9619 && start - 1 >= oap->end_vcol)
9620 oap->end_vcol = start - 1;
9621 else
9622 oap->end_vcol = end;
9623 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009624 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009625
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009626 /* if '$' was used, get oap->end_vcol from longest line */
9627 if (curwin->w_curswant == MAXCOL)
9628 {
9629 curwin->w_cursor.col = MAXCOL;
9630 oap->end_vcol = 0;
9631 for (curwin->w_cursor.lnum = oap->start.lnum;
9632 curwin->w_cursor.lnum <= oap->end.lnum;
9633 ++curwin->w_cursor.lnum)
9634 {
9635 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9636 if (end > oap->end_vcol)
9637 oap->end_vcol = end;
9638 }
9639 }
9640 else if (redo_VIsual_busy)
9641 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9642 /*
9643 * Correct oap->end.col and oap->start.col to be the
9644 * upper-left and lower-right corner of the block area.
9645 *
9646 * (Actually, this does convert column positions into character
9647 * positions)
9648 */
9649 curwin->w_cursor.lnum = oap->end.lnum;
9650 coladvance(oap->end_vcol);
9651 oap->end = curwin->w_cursor;
9652
9653 curwin->w_cursor = oap->start;
9654 coladvance(oap->start_vcol);
9655 oap->start = curwin->w_cursor;
9656}