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