blob: 810aabfa00733ed0cfa6bdb72f01ba9471183f7c [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
43static void prep_redo_cmd __ARGS((cmdarg_T *cap));
44static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
45static int checkclearop __ARGS((oparg_T *oap));
46static int checkclearopq __ARGS((oparg_T *oap));
47static void clearop __ARGS((oparg_T *oap));
48static void clearopbeep __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void unshift_special __ARGS((cmdarg_T *cap));
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +020050static void may_clear_cmdline __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
52static void del_from_showcmd __ARGS((int));
53#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
60static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000061static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static void nv_error __ARGS((cmdarg_T *cap));
63static void nv_help __ARGS((cmdarg_T *cap));
64static void nv_addsub __ARGS((cmdarg_T *cap));
65static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000066static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
68#ifdef FEAT_MOUSE
69static void nv_mousescroll __ARGS((cmdarg_T *cap));
70static void nv_mouse __ARGS((cmdarg_T *cap));
71#endif
72static void nv_scroll_line __ARGS((cmdarg_T *cap));
73static void nv_zet __ARGS((cmdarg_T *cap));
74#ifdef FEAT_GUI
75static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
76static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
77#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
79static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000080static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000082static void nv_exmode __ARGS((cmdarg_T *cap));
83static void nv_colon __ARGS((cmdarg_T *cap));
84static void nv_ctrlg __ARGS((cmdarg_T *cap));
85static void nv_ctrlh __ARGS((cmdarg_T *cap));
86static void nv_clear __ARGS((cmdarg_T *cap));
87static void nv_ctrlo __ARGS((cmdarg_T *cap));
88static void nv_hat __ARGS((cmdarg_T *cap));
89static void nv_Zet __ARGS((cmdarg_T *cap));
90static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000091static void nv_tagpop __ARGS((cmdarg_T *cap));
92static void nv_scroll __ARGS((cmdarg_T *cap));
93static void nv_right __ARGS((cmdarg_T *cap));
94static void nv_left __ARGS((cmdarg_T *cap));
95static void nv_up __ARGS((cmdarg_T *cap));
96static void nv_down __ARGS((cmdarg_T *cap));
97#ifdef FEAT_SEARCHPATH
98static void nv_gotofile __ARGS((cmdarg_T *cap));
99#endif
100static void nv_end __ARGS((cmdarg_T *cap));
101static void nv_dollar __ARGS((cmdarg_T *cap));
102static void nv_search __ARGS((cmdarg_T *cap));
103static void nv_next __ARGS((cmdarg_T *cap));
Bram Moolenaar46539112015-02-17 15:43:57 +0100104static int normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105static void nv_csearch __ARGS((cmdarg_T *cap));
106static void nv_brackets __ARGS((cmdarg_T *cap));
107static void nv_percent __ARGS((cmdarg_T *cap));
108static void nv_brace __ARGS((cmdarg_T *cap));
109static void nv_mark __ARGS((cmdarg_T *cap));
110static void nv_findpar __ARGS((cmdarg_T *cap));
111static void nv_undo __ARGS((cmdarg_T *cap));
112static void nv_kundo __ARGS((cmdarg_T *cap));
113static void nv_Replace __ARGS((cmdarg_T *cap));
114#ifdef FEAT_VREPLACE
115static void nv_vreplace __ARGS((cmdarg_T *cap));
116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static void v_swap_corners __ARGS((int cmdchar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118static void nv_replace __ARGS((cmdarg_T *cap));
119static void n_swapchar __ARGS((cmdarg_T *cap));
120static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121static void v_visop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122static void nv_subst __ARGS((cmdarg_T *cap));
123static void nv_abbrev __ARGS((cmdarg_T *cap));
124static void nv_optrans __ARGS((cmdarg_T *cap));
125static void nv_gomark __ARGS((cmdarg_T *cap));
126static void nv_pcmark __ARGS((cmdarg_T *cap));
127static void nv_regname __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128static void nv_visual __ARGS((cmdarg_T *cap));
129static void n_start_visual_mode __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130static void nv_window __ARGS((cmdarg_T *cap));
131static void nv_suspend __ARGS((cmdarg_T *cap));
132static void nv_g_cmd __ARGS((cmdarg_T *cap));
133static void n_opencmd __ARGS((cmdarg_T *cap));
134static void nv_dot __ARGS((cmdarg_T *cap));
135static void nv_redo __ARGS((cmdarg_T *cap));
136static void nv_Undo __ARGS((cmdarg_T *cap));
137static void nv_tilde __ARGS((cmdarg_T *cap));
138static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
140static void set_op_var __ARGS((int optype));
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142static void nv_lineop __ARGS((cmdarg_T *cap));
143static void nv_home __ARGS((cmdarg_T *cap));
144static void nv_pipe __ARGS((cmdarg_T *cap));
145static void nv_bck_word __ARGS((cmdarg_T *cap));
146static void nv_wordcmd __ARGS((cmdarg_T *cap));
147static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000148static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void adjust_for_sel __ARGS((cmdarg_T *cap));
150static int unadjust_for_sel __ARGS((void));
151static void nv_select __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152static void nv_goto __ARGS((cmdarg_T *cap));
153static void nv_normal __ARGS((cmdarg_T *cap));
154static void nv_esc __ARGS((cmdarg_T *oap));
155static void nv_edit __ARGS((cmdarg_T *cap));
156static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
157#ifdef FEAT_TEXTOBJ
158static void nv_object __ARGS((cmdarg_T *cap));
159#endif
160static void nv_record __ARGS((cmdarg_T *cap));
161static void nv_at __ARGS((cmdarg_T *cap));
162static void nv_halfpage __ARGS((cmdarg_T *cap));
163static void nv_join __ARGS((cmdarg_T *cap));
164static void nv_put __ARGS((cmdarg_T *cap));
165static void nv_open __ARGS((cmdarg_T *cap));
166#ifdef FEAT_SNIFF
167static void nv_sniff __ARGS((cmdarg_T *cap));
168#endif
169#ifdef FEAT_NETBEANS_INTG
170static void nv_nbcmd __ARGS((cmdarg_T *cap));
171#endif
172#ifdef FEAT_DND
173static void nv_drop __ARGS((cmdarg_T *cap));
174#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000175#ifdef FEAT_AUTOCMD
176static void nv_cursorhold __ARGS((cmdarg_T *cap));
177#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +0200178static void get_op_vcol __ARGS((oparg_T *oap, colnr_T col, int initial));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000180static char *e_noident = N_("E349: No identifier under cursor");
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/*
183 * Function to be called for a Normal or Visual mode command.
184 * The argument is a cmdarg_T.
185 */
186typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
187
188/* Values for cmd_flags. */
189#define NV_NCH 0x01 /* may need to get a second char */
190#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
191#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
192#define NV_LANG 0x08 /* second char needs language adjustment */
193
194#define NV_SS 0x10 /* may start selection */
195#define NV_SSS 0x20 /* may start selection with shift modifier */
196#define NV_STS 0x40 /* may stop selection without shift modif. */
197#define NV_RL 0x80 /* 'rightleft' modifies command */
198#define NV_KEEPREG 0x100 /* don't clear regname */
199#define NV_NCW 0x200 /* not allowed in command-line window */
200
201/*
202 * Generally speaking, every Normal mode command should either clear any
203 * pending operator (with *clearop*()), or set the motion type variable
204 * oap->motion_type.
205 *
206 * When a cursor motion command is made, it is marked as being a character or
207 * line oriented motion. Then, if an operator is in effect, the operation
208 * becomes character or line oriented accordingly.
209 */
210
211/*
212 * This table contains one entry for every Normal or Visual mode command.
213 * The order doesn't matter, init_normal_cmds() will create a sorted index.
214 * It is faster when all keys from zero to '~' are present.
215 */
216static const struct nv_cmd
217{
218 int cmd_char; /* (first) command character */
219 nv_func_T cmd_func; /* function for this command */
220 short_u cmd_flags; /* NV_ flags */
221 short cmd_arg; /* value for ca.arg */
222} nv_cmds[] =
223{
224 {NUL, nv_error, 0, 0},
225 {Ctrl_A, nv_addsub, 0, 0},
226 {Ctrl_B, nv_page, NV_STS, BACKWARD},
227 {Ctrl_C, nv_esc, 0, TRUE},
228 {Ctrl_D, nv_halfpage, 0, 0},
229 {Ctrl_E, nv_scroll_line, 0, TRUE},
230 {Ctrl_F, nv_page, NV_STS, FORWARD},
231 {Ctrl_G, nv_ctrlg, 0, 0},
232 {Ctrl_H, nv_ctrlh, 0, 0},
233 {Ctrl_I, nv_pcmark, 0, 0},
234 {NL, nv_down, 0, FALSE},
235 {Ctrl_K, nv_error, 0, 0},
236 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000237 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 {Ctrl_N, nv_down, NV_STS, FALSE},
239 {Ctrl_O, nv_ctrlo, 0, 0},
240 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000241 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {Ctrl_R, nv_redo, 0, 0},
243 {Ctrl_S, nv_ignore, 0, 0},
244 {Ctrl_T, nv_tagpop, NV_NCW, 0},
245 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {Ctrl_V, nv_visual, 0, FALSE},
247 {'V', nv_visual, 0, FALSE},
248 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {Ctrl_W, nv_window, 0, 0},
250 {Ctrl_X, nv_addsub, 0, 0},
251 {Ctrl_Y, nv_scroll_line, 0, FALSE},
252 {Ctrl_Z, nv_suspend, 0, 0},
253 {ESC, nv_esc, 0, FALSE},
254 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
255 {Ctrl_RSB, nv_ident, NV_NCW, 0},
256 {Ctrl_HAT, nv_hat, NV_NCW, 0},
257 {Ctrl__, nv_error, 0, 0},
258 {' ', nv_right, 0, 0},
259 {'!', nv_operator, 0, 0},
260 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
261 {'#', nv_ident, 0, 0},
262 {'$', nv_dollar, 0, 0},
263 {'%', nv_percent, 0, 0},
264 {'&', nv_optrans, 0, 0},
265 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
266 {'(', nv_brace, 0, BACKWARD},
267 {')', nv_brace, 0, FORWARD},
268 {'*', nv_ident, 0, 0},
269 {'+', nv_down, 0, TRUE},
270 {',', nv_csearch, 0, TRUE},
271 {'-', nv_up, 0, TRUE},
272 {'.', nv_dot, NV_KEEPREG, 0},
273 {'/', nv_search, 0, FALSE},
274 {'0', nv_beginline, 0, 0},
275 {'1', nv_ignore, 0, 0},
276 {'2', nv_ignore, 0, 0},
277 {'3', nv_ignore, 0, 0},
278 {'4', nv_ignore, 0, 0},
279 {'5', nv_ignore, 0, 0},
280 {'6', nv_ignore, 0, 0},
281 {'7', nv_ignore, 0, 0},
282 {'8', nv_ignore, 0, 0},
283 {'9', nv_ignore, 0, 0},
284 {':', nv_colon, 0, 0},
285 {';', nv_csearch, 0, FALSE},
286 {'<', nv_operator, NV_RL, 0},
287 {'=', nv_operator, 0, 0},
288 {'>', nv_operator, NV_RL, 0},
289 {'?', nv_search, 0, FALSE},
290 {'@', nv_at, NV_NCH_NOP, FALSE},
291 {'A', nv_edit, 0, 0},
292 {'B', nv_bck_word, 0, 1},
293 {'C', nv_abbrev, NV_KEEPREG, 0},
294 {'D', nv_abbrev, NV_KEEPREG, 0},
295 {'E', nv_wordcmd, 0, TRUE},
296 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
297 {'G', nv_goto, 0, TRUE},
298 {'H', nv_scroll, 0, 0},
299 {'I', nv_edit, 0, 0},
300 {'J', nv_join, 0, 0},
301 {'K', nv_ident, 0, 0},
302 {'L', nv_scroll, 0, 0},
303 {'M', nv_scroll, 0, 0},
304 {'N', nv_next, 0, SEARCH_REV},
305 {'O', nv_open, 0, 0},
306 {'P', nv_put, 0, 0},
307 {'Q', nv_exmode, NV_NCW, 0},
308 {'R', nv_Replace, 0, FALSE},
309 {'S', nv_subst, NV_KEEPREG, 0},
310 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
311 {'U', nv_Undo, 0, 0},
312 {'W', nv_wordcmd, 0, TRUE},
313 {'X', nv_abbrev, NV_KEEPREG, 0},
314 {'Y', nv_abbrev, NV_KEEPREG, 0},
315 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
316 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
317 {'\\', nv_error, 0, 0},
318 {']', nv_brackets, NV_NCH_ALW, FORWARD},
319 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
320 {'_', nv_lineop, 0, 0},
321 {'`', nv_gomark, NV_NCH_ALW, FALSE},
322 {'a', nv_edit, NV_NCH, 0},
323 {'b', nv_bck_word, 0, 0},
324 {'c', nv_operator, 0, 0},
325 {'d', nv_operator, 0, 0},
326 {'e', nv_wordcmd, 0, FALSE},
327 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
328 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
329 {'h', nv_left, NV_RL, 0},
330 {'i', nv_edit, NV_NCH, 0},
331 {'j', nv_down, 0, FALSE},
332 {'k', nv_up, 0, FALSE},
333 {'l', nv_right, NV_RL, 0},
334 {'m', nv_mark, NV_NCH_NOP, 0},
335 {'n', nv_next, 0, 0},
336 {'o', nv_open, 0, 0},
337 {'p', nv_put, 0, 0},
338 {'q', nv_record, NV_NCH, 0},
339 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
340 {'s', nv_subst, NV_KEEPREG, 0},
341 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
342 {'u', nv_undo, 0, 0},
343 {'w', nv_wordcmd, 0, FALSE},
344 {'x', nv_abbrev, NV_KEEPREG, 0},
345 {'y', nv_operator, 0, 0},
346 {'z', nv_zet, NV_NCH_ALW, 0},
347 {'{', nv_findpar, 0, BACKWARD},
348 {'|', nv_pipe, 0, 0},
349 {'}', nv_findpar, 0, FORWARD},
350 {'~', nv_tilde, 0, 0},
351
352 /* pound sign */
353 {POUND, nv_ident, 0, 0},
354#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200355 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
356 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
357 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
358 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 {K_LEFTMOUSE, nv_mouse, 0, 0},
360 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
361 {K_LEFTDRAG, nv_mouse, 0, 0},
362 {K_LEFTRELEASE, nv_mouse, 0, 0},
363 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
364 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
365 {K_MIDDLEDRAG, nv_mouse, 0, 0},
366 {K_MIDDLERELEASE, nv_mouse, 0, 0},
367 {K_RIGHTMOUSE, nv_mouse, 0, 0},
368 {K_RIGHTDRAG, nv_mouse, 0, 0},
369 {K_RIGHTRELEASE, nv_mouse, 0, 0},
370 {K_X1MOUSE, nv_mouse, 0, 0},
371 {K_X1DRAG, nv_mouse, 0, 0},
372 {K_X1RELEASE, nv_mouse, 0, 0},
373 {K_X2MOUSE, nv_mouse, 0, 0},
374 {K_X2DRAG, nv_mouse, 0, 0},
375 {K_X2RELEASE, nv_mouse, 0, 0},
376#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000377 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000378 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_INS, nv_edit, 0, 0},
380 {K_KINS, nv_edit, 0, 0},
381 {K_BS, nv_ctrlh, 0, 0},
382 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
383 {K_S_UP, nv_page, NV_SS, BACKWARD},
384 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
385 {K_S_DOWN, nv_page, NV_SS, FORWARD},
386 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
387 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
388 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
389 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
390 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
391 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
392 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
393 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
394 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
395 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
396 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
397 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_S_END, nv_end, NV_SS, FALSE},
399 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
400 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
401 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {K_S_HOME, nv_home, NV_SS, 0},
403 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
404 {K_DEL, nv_abbrev, 0, 0},
405 {K_KDEL, nv_abbrev, 0, 0},
406 {K_UNDO, nv_kundo, 0, 0},
407 {K_HELP, nv_help, NV_NCW, 0},
408 {K_F1, nv_help, NV_NCW, 0},
409 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411#ifdef FEAT_GUI
412 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
413 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
414#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000415#ifdef FEAT_GUI_TABLINE
416 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000417 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_FKMAP
420 {K_F8, farsi_fkey, 0, 0},
421 {K_F9, farsi_fkey, 0, 0},
422#endif
423#ifdef FEAT_SNIFF
424 {K_SNIFF, nv_sniff, 0, 0},
425#endif
426#ifdef FEAT_NETBEANS_INTG
427 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
428#endif
429#ifdef FEAT_DND
430 {K_DROP, nv_drop, NV_STS, 0},
431#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000432#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000433 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435};
436
437/* Number of commands in nv_cmds[]. */
438#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
439
440/* Sorted index of commands in nv_cmds[]. */
441static short nv_cmd_idx[NV_CMDS_SIZE];
442
443/* The highest index for which
444 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
445static int nv_max_linear;
446
447/*
448 * Compare functions for qsort() below, that checks the command character
449 * through the index in nv_cmd_idx[].
450 */
451 static int
452#ifdef __BORLANDC__
453_RTLENTRYF
454#endif
455nv_compare(s1, s2)
456 const void *s1;
457 const void *s2;
458{
459 int c1, c2;
460
461 /* The commands are sorted on absolute value. */
462 c1 = nv_cmds[*(const short *)s1].cmd_char;
463 c2 = nv_cmds[*(const short *)s2].cmd_char;
464 if (c1 < 0)
465 c1 = -c1;
466 if (c2 < 0)
467 c2 = -c2;
468 return c1 - c2;
469}
470
471/*
472 * Initialize the nv_cmd_idx[] table.
473 */
474 void
475init_normal_cmds()
476{
477 int i;
478
479 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000480 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 nv_cmd_idx[i] = i;
482
483 /* Sort the commands by the command character. */
484 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
485
486 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000487 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
489 break;
490 nv_max_linear = i - 1;
491}
492
493/*
494 * Search for a command in the commands table.
495 * Returns -1 for invalid command.
496 */
497 static int
498find_command(cmdchar)
499 int cmdchar;
500{
501 int i;
502 int idx;
503 int top, bot;
504 int c;
505
506#ifdef FEAT_MBYTE
507 /* A multi-byte character is never a command. */
508 if (cmdchar >= 0x100)
509 return -1;
510#endif
511
512 /* We use the absolute value of the character. Special keys have a
513 * negative value, but are sorted on their absolute value. */
514 if (cmdchar < 0)
515 cmdchar = -cmdchar;
516
517 /* If the character is in the first part: The character is the index into
518 * nv_cmd_idx[]. */
519 if (cmdchar <= nv_max_linear)
520 return nv_cmd_idx[cmdchar];
521
522 /* Perform a binary search. */
523 bot = nv_max_linear + 1;
524 top = NV_CMDS_SIZE - 1;
525 idx = -1;
526 while (bot <= top)
527 {
528 i = (top + bot) / 2;
529 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
530 if (c < 0)
531 c = -c;
532 if (cmdchar == c)
533 {
534 idx = nv_cmd_idx[i];
535 break;
536 }
537 if (cmdchar > c)
538 bot = i + 1;
539 else
540 top = i - 1;
541 }
542 return idx;
543}
544
545/*
546 * Execute a command in Normal mode.
547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 void
549normal_cmd(oap, toplevel)
550 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000551 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 cmdarg_T ca; /* command arguments */
554 int c;
555 int ctrl_w = FALSE; /* got CTRL-W command */
556 int old_col = curwin->w_curswant;
557#ifdef FEAT_CMDL_INFO
558 int need_flushbuf; /* need to call out_flush() */
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 pos_T old_pos; /* cursor position before command */
561 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 static int old_mapped_len = 0;
563 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000564#ifdef FEAT_EVAL
565 int set_prevcount = FALSE;
566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
569 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000570
571 /* Use a count remembered from before entering an operator. After typing
572 * "3d" we return from normal_cmd() and come back here, the "3" is
573 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 ca.opcount = opcount;
575
576#ifdef FEAT_SNIFF
577 want_sniff_request = sniff_connected;
578#endif
579
580 /*
581 * If there is an operator pending, then the command we take this time
582 * will terminate it. Finish_op tells us to finish the operation before
583 * returning this time (unless the operation was cancelled).
584 */
585#ifdef CURSOR_SHAPE
586 c = finish_op;
587#endif
588 finish_op = (oap->op_type != OP_NOP);
589#ifdef CURSOR_SHAPE
590 if (finish_op != c)
591 {
592 ui_cursor_shape(); /* may show different cursor shape */
593# ifdef FEAT_MOUSESHAPE
594 update_mouseshape(-1);
595# endif
596 }
597#endif
598
Bram Moolenaara983fe92008-07-31 20:04:27 +0000599 /* When not finishing an operator and no register name typed, reset the
600 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000604#ifdef FEAT_EVAL
605 set_prevcount = TRUE;
606#endif
607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaara983fe92008-07-31 20:04:27 +0000609#ifdef FEAT_AUTOCMD
610 /* Restore counts from before receiving K_CURSORHOLD. This means after
611 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
612 * "3 * 2". */
613 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
614 {
615 ca.opcount = oap->prev_opcount;
616 ca.count0 = oap->prev_count0;
617 oap->prev_opcount = 0;
618 oap->prev_count0 = 0;
619 }
620#endif
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 State = NORMAL_BUSY;
625#ifdef USE_ON_FLY_SCROLL
626 dont_scroll = FALSE; /* allow scrolling here */
627#endif
628
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100629#ifdef FEAT_EVAL
630 /* Set v:count here, when called from main() and not a stuffed
631 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100632 * when there is no count. Do set it for redo. */
633 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100634 set_vcount_ca(&ca, &set_prevcount);
635#endif
636
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 /*
638 * Get the command character from the user.
639 */
640 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
643 /*
644 * If a mapping was started in Visual or Select mode, remember the length
645 * of the mapping. This is used below to not return to Insert mode for as
646 * long as the mapping is being executed.
647 */
648 if (restart_edit == 0)
649 old_mapped_len = 0;
650 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000651 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 old_mapped_len = typebuf_maplen();
653
654 if (c == NUL)
655 c = K_ZERO;
656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 /*
658 * In Select mode, typed text replaces the selection.
659 */
660 if (VIsual_active
661 && VIsual_select
662 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
663 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000664 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
665 * 'insertmode' is set) fake a "d"elete command, Insert mode will
666 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000667 * Insert the typed character in the typeahead buffer, so that it can
668 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000669 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000670 if (restart_edit != 0)
671 c = 'd';
672 else
673 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000674 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200675 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678#ifdef FEAT_CMDL_INFO
679 need_flushbuf = add_to_showcmd(c);
680#endif
681
682getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 /*
686 * Handle a count before a command and compute ca.count0.
687 * Note that '0' is a command and not the start of a count, but it's
688 * part of a count after other digits.
689 */
690 while ( (c >= '1' && c <= '9')
691 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
692 {
693 if (c == K_DEL || c == K_KDEL)
694 {
695 ca.count0 /= 10;
696#ifdef FEAT_CMDL_INFO
697 del_from_showcmd(4); /* delete the digit and ~@% */
698#endif
699 }
700 else
701 ca.count0 = ca.count0 * 10 + (c - '0');
702 if (ca.count0 < 0) /* got too large! */
703 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000704#ifdef FEAT_EVAL
705 /* Set v:count here, when called from main() and not a stuffed
706 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100707 * right after the count. Do set it for redo. */
708 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100709 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (ctrl_w)
712 {
713 ++no_mapping;
714 ++allow_keys; /* no mapping for nchar, but keys */
715 }
716 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000717 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 --no_zero_mapping;
720 if (ctrl_w)
721 {
722 --no_mapping;
723 --allow_keys;
724 }
725#ifdef FEAT_CMDL_INFO
726 need_flushbuf |= add_to_showcmd(c);
727#endif
728 }
729
730 /*
731 * If we got CTRL-W there may be a/another count
732 */
733 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
734 {
735 ctrl_w = TRUE;
736 ca.opcount = ca.count0; /* remember first count */
737 ca.count0 = 0;
738 ++no_mapping;
739 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000740 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 --no_mapping;
743 --allow_keys;
744#ifdef FEAT_CMDL_INFO
745 need_flushbuf |= add_to_showcmd(c);
746#endif
747 goto getcount; /* jump back */
748 }
749 }
750
Bram Moolenaara983fe92008-07-31 20:04:27 +0000751#ifdef FEAT_AUTOCMD
752 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000754 /* Save the count values so that ca.opcount and ca.count0 are exactly
755 * the same when coming back here after handling K_CURSORHOLD. */
756 oap->prev_opcount = ca.opcount;
757 oap->prev_count0 = ca.count0;
758 }
759 else
760#endif
761 if (ca.opcount != 0)
762 {
763 /*
764 * If we're in the middle of an operator (including after entering a
765 * yank buffer with '"') AND we had a count before the operator, then
766 * that count overrides the current value of ca.count0.
767 * What this means effectively, is that commands like "3dw" get turned
768 * into "d3w" which makes things fall into place pretty neatly.
769 * If you give a count before AND after the operator, they are
770 * multiplied.
771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (ca.count0)
773 ca.count0 *= ca.opcount;
774 else
775 ca.count0 = ca.opcount;
776 }
777
778 /*
779 * Always remember the count. It will be set to zero (on the next call,
780 * above) when there is no pending operator.
781 * When called from main(), save the count for use by the "count" built-in
782 * variable.
783 */
784 ca.opcount = ca.count0;
785 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
786
787#ifdef FEAT_EVAL
788 /*
789 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100790 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100792 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000793 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794#endif
795
796 /*
797 * Find the command character in the table of commands.
798 * For CTRL-W we already got nchar when looking for a count.
799 */
800 if (ctrl_w)
801 {
802 ca.nchar = c;
803 ca.cmdchar = Ctrl_W;
804 }
805 else
806 ca.cmdchar = c;
807 idx = find_command(ca.cmdchar);
808 if (idx < 0)
809 {
810 /* Not a known command: beep. */
811 clearopbeep(oap);
812 goto normal_end;
813 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000814
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000815 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200817 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000819 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 goto normal_end;
821 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000822#ifdef FEAT_AUTOCMD
823 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
824 goto normal_end;
825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /*
828 * In Visual/Select mode, a few keys are handled in a special way.
829 */
830 if (VIsual_active)
831 {
832 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
833 if (km_stopsel
834 && (nv_cmds[idx].cmd_flags & NV_STS)
835 && !(mod_mask & MOD_MASK_SHIFT))
836 {
837 end_visual_mode();
838 redraw_curbuf_later(INVERTED);
839 }
840
841 /* Keys that work different when 'keymodel' contains "startsel" */
842 if (km_startsel)
843 {
844 if (nv_cmds[idx].cmd_flags & NV_SS)
845 {
846 unshift_special(&ca);
847 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000848 if (idx < 0)
849 {
850 /* Just in case */
851 clearopbeep(oap);
852 goto normal_end;
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
856 && (mod_mask & MOD_MASK_SHIFT))
857 {
858 mod_mask &= ~MOD_MASK_SHIFT;
859 }
860 }
861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863#ifdef FEAT_RIGHTLEFT
864 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
865 && (nv_cmds[idx].cmd_flags & NV_RL))
866 {
867 /* Invert horizontal movements and operations. Only when typed by the
868 * user directly, not when the result of a mapping or "x" translated
869 * to "dl". */
870 switch (ca.cmdchar)
871 {
872 case 'l': ca.cmdchar = 'h'; break;
873 case K_RIGHT: ca.cmdchar = K_LEFT; break;
874 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
875 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
876 case 'h': ca.cmdchar = 'l'; break;
877 case K_LEFT: ca.cmdchar = K_RIGHT; break;
878 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
879 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
880 case '>': ca.cmdchar = '<'; break;
881 case '<': ca.cmdchar = '>'; break;
882 }
883 idx = find_command(ca.cmdchar);
884 }
885#endif
886
887 /*
888 * Get an additional character if we need one.
889 */
890 if ((nv_cmds[idx].cmd_flags & NV_NCH)
891 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
892 && oap->op_type == OP_NOP)
893 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
894 || (ca.cmdchar == 'q'
895 && oap->op_type == OP_NOP
896 && !Recording
897 && !Exec_reg)
898 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100899 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
901 int *cp;
902 int repl = FALSE; /* get character for replace mode */
903 int lit = FALSE; /* get extra character literally */
904 int langmap_active = FALSE; /* using :lmap mappings */
905 int lang; /* getting a text character */
906#ifdef USE_IM_CONTROL
907 int save_smd; /* saved value of p_smd */
908#endif
909
910 ++no_mapping;
911 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000912#ifdef FEAT_AUTOCMD
913 /* Don't generate a CursorHold event here, most commands can't handle
914 * it, e.g., nv_replace(), nv_csearch(). */
915 did_cursorhold = TRUE;
916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (ca.cmdchar == 'g')
918 {
919 /*
920 * For 'g' get the next character now, so that we can check for
921 * "gr", "g'" and "g`".
922 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000923 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#ifdef FEAT_CMDL_INFO
926 need_flushbuf |= add_to_showcmd(ca.nchar);
927#endif
928 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100929 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 {
931 cp = &ca.extra_char; /* need to get a third character */
932 if (ca.nchar != 'r')
933 lit = TRUE; /* get it literally */
934 else
935 repl = TRUE; /* get it in replace mode */
936 }
937 else
938 cp = NULL; /* no third character needed */
939 }
940 else
941 {
942 if (ca.cmdchar == 'r') /* get it in replace mode */
943 repl = TRUE;
944 cp = &ca.nchar;
945 }
946 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
947
948 /*
949 * Get a second or third character.
950 */
951 if (cp != NULL)
952 {
953#ifdef CURSOR_SHAPE
954 if (repl)
955 {
956 State = REPLACE; /* pretend Replace mode */
957 ui_cursor_shape(); /* show different cursor shape */
958 }
959#endif
960 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
961 {
962 /* Allow mappings defined with ":lmap". */
963 --no_mapping;
964 --allow_keys;
965 if (repl)
966 State = LREPLACE;
967 else
968 State = LANGMAP;
969 langmap_active = TRUE;
970 }
971#ifdef USE_IM_CONTROL
972 save_smd = p_smd;
973 p_smd = FALSE; /* Don't let the IM code show the mode here */
974 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
975 im_set_active(TRUE);
976#endif
977
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000978 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 if (langmap_active)
981 {
982 /* Undo the decrement done above */
983 ++no_mapping;
984 ++allow_keys;
985 State = NORMAL_BUSY;
986 }
987#ifdef USE_IM_CONTROL
988 if (lang)
989 {
990 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
991 im_save_status(&curbuf->b_p_iminsert);
992 im_set_active(FALSE);
993 }
994 p_smd = save_smd;
995#endif
996#ifdef CURSOR_SHAPE
997 State = NORMAL_BUSY;
998#endif
999#ifdef FEAT_CMDL_INFO
1000 need_flushbuf |= add_to_showcmd(*cp);
1001#endif
1002
1003 if (!lit)
1004 {
1005#ifdef FEAT_DIGRAPHS
1006 /* Typing CTRL-K gets a digraph. */
1007 if (*cp == Ctrl_K
1008 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1009 || cp == &ca.extra_char)
1010 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1011 {
1012 c = get_digraph(FALSE);
1013 if (c > 0)
1014 {
1015 *cp = c;
1016# ifdef FEAT_CMDL_INFO
1017 /* Guessing how to update showcmd here... */
1018 del_from_showcmd(3);
1019 need_flushbuf |= add_to_showcmd(*cp);
1020# endif
1021 }
1022 }
1023#endif
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /* adjust chars > 127, except after "tTfFr" commands */
1026 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027#ifdef FEAT_RIGHTLEFT
1028 /* adjust Hebrew mapped char */
1029 if (p_hkmap && lang && KeyTyped)
1030 *cp = hkmap(*cp);
1031# ifdef FEAT_FKMAP
1032 /* adjust Farsi mapped char */
1033 if (p_fkmap && lang && KeyTyped)
1034 *cp = fkmap(*cp);
1035# endif
1036#endif
1037 }
1038
1039 /*
1040 * When the next character is CTRL-\ a following CTRL-N means the
1041 * command is aborted and we go to Normal mode.
1042 */
1043 if (cp == &ca.extra_char
1044 && ca.nchar == Ctrl_BSL
1045 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1046 {
1047 ca.cmdchar = Ctrl_BSL;
1048 ca.nchar = ca.extra_char;
1049 idx = find_command(ca.cmdchar);
1050 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001051 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001052 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 else if (*cp == Ctrl_BSL)
1054 {
1055 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1056
1057 /* There is a busy wait here when typing "f<C-\>" and then
1058 * something different from CTRL-N. Can't be avoided. */
1059 while ((c = vpeekc()) <= 0 && towait > 0L)
1060 {
1061 do_sleep(towait > 50L ? 50L : towait);
1062 towait -= 50L;
1063 }
1064 if (c > 0)
1065 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001066 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (c != Ctrl_N && c != Ctrl_G)
1068 vungetc(c);
1069 else
1070 {
1071 ca.cmdchar = Ctrl_BSL;
1072 ca.nchar = c;
1073 idx = find_command(ca.cmdchar);
1074 }
1075 }
1076 }
1077
1078#ifdef FEAT_MBYTE
1079 /* When getting a text character and the next character is a
1080 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001081 * However, don't wait for it to arrive. Also, do enable mapping,
1082 * because if it's put back with vungetc() it's too late to apply
1083 * mapping. */
1084 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 while (enc_utf8 && lang && (c = vpeekc()) > 0
1086 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1087 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001088 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (!utf_iscomposing(c))
1090 {
1091 vungetc(c); /* it wasn't, put it back */
1092 break;
1093 }
1094 else if (ca.ncharC1 == 0)
1095 ca.ncharC1 = c;
1096 else
1097 ca.ncharC2 = c;
1098 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001099 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100#endif
1101 }
1102 --no_mapping;
1103 --allow_keys;
1104 }
1105
1106#ifdef FEAT_CMDL_INFO
1107 /*
1108 * Flush the showcmd characters onto the screen so we can see them while
1109 * the command is being executed. Only do this when the shown command was
1110 * actually displayed, otherwise this will slow down a lot when executing
1111 * mappings.
1112 */
1113 if (need_flushbuf)
1114 out_flush();
1115#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001116#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001117 if (ca.cmdchar != K_IGNORE)
1118 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121 State = NORMAL;
1122
1123 if (ca.nchar == ESC)
1124 {
1125 clearop(oap);
1126 if (restart_edit == 0 && goto_im())
1127 restart_edit = 'a';
1128 goto normal_end;
1129 }
1130
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001131 if (ca.cmdchar != K_IGNORE)
1132 {
1133 msg_didout = FALSE; /* don't scroll screen up for normal command */
1134 msg_col = 0;
1135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 old_pos = curwin->w_cursor; /* remember where cursor was */
1138
1139 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1140 * mode. */
1141 if (!VIsual_active && km_startsel)
1142 {
1143 if (nv_cmds[idx].cmd_flags & NV_SS)
1144 {
1145 start_selection();
1146 unshift_special(&ca);
1147 idx = find_command(ca.cmdchar);
1148 }
1149 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1150 && (mod_mask & MOD_MASK_SHIFT))
1151 {
1152 start_selection();
1153 mod_mask &= ~MOD_MASK_SHIFT;
1154 }
1155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 /*
1158 * Execute the command!
1159 * Call the command function found in the commands table.
1160 */
1161 ca.arg = nv_cmds[idx].cmd_arg;
1162 (nv_cmds[idx].cmd_func)(&ca);
1163
1164 /*
1165 * If we didn't start or finish an operator, reset oap->regname, unless we
1166 * need it later.
1167 */
1168 if (!finish_op
1169 && !oap->op_type
1170 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1171 {
1172 clearop(oap);
1173#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001174 {
1175 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001176
Bram Moolenaar536681b2011-05-10 16:12:45 +02001177 /* Adjust the register according to 'clipboard', so that when
1178 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001179# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001180 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001181# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001182 set_reg_var(regname);
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184#endif
1185 }
1186
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001187 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001188 * character or "z333<cr>". */
1189 if (old_mapped_len > 0)
1190 old_mapped_len = typebuf_maplen();
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 /*
1193 * If an operation is pending, handle it...
1194 */
1195 do_pending_operator(&ca, old_col, FALSE);
1196
1197 /*
1198 * Wait for a moment when a message is displayed that will be overwritten
1199 * by the mode message.
1200 * In Visual mode and with "^O" in Insert mode, a short message will be
1201 * overwritten by the mode message. Wait a bit, until a key is hit.
1202 * In Visual mode, it's more important to keep the Visual area updated
1203 * than keeping a message (e.g. from a /pat search).
1204 * Only do this if the command was typed, not from a mapping.
1205 * Don't wait when emsg_silent is non-zero.
1206 * Also wait a bit after an error message, e.g. for "^O:".
1207 * Don't redraw the screen, it would remove the message.
1208 */
1209 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001210 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 || (VIsual_active
1213 && old_pos.lnum == curwin->w_cursor.lnum
1214 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 )
1216 && (clear_cmdline
1217 || redraw_cmdline)
1218 && (msg_didout || (msg_didany && msg_scroll))
1219 && !msg_nowait
1220 && KeyTyped)
1221 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && (msg_scroll
1224 || emsg_on_display)))
1225 && oap->regname == 0
1226 && !(ca.retval & CA_COMMAND_BUSY)
1227 && stuff_empty()
1228 && typebuf_typed()
1229 && emsg_silent == 0
1230 && !did_wait_return
1231 && oap->op_type == OP_NOP)
1232 {
1233 int save_State = State;
1234
1235 /* Draw the cursor with the right shape here */
1236 if (restart_edit != 0)
1237 State = INSERT;
1238
1239 /* If need to redraw, and there is a "keep_msg", redraw before the
1240 * delay */
1241 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1242 {
1243 char_u *kmsg;
1244
1245 kmsg = keep_msg;
1246 keep_msg = NULL;
1247 /* showmode() will clear keep_msg, but we want to use it anyway */
1248 update_screen(0);
1249 /* now reset it, otherwise it's put in the history again */
1250 keep_msg = kmsg;
1251 msg_attr(kmsg, keep_msg_attr);
1252 vim_free(kmsg);
1253 }
1254 setcursor();
1255 cursor_on();
1256 out_flush();
1257 if (msg_scroll || emsg_on_display)
1258 ui_delay(1000L, TRUE); /* wait at least one second */
1259 ui_delay(3000L, FALSE); /* wait up to three seconds */
1260 State = save_State;
1261
1262 msg_scroll = FALSE;
1263 emsg_on_display = FALSE;
1264 }
1265
1266 /*
1267 * Finish up after executing a Normal mode command.
1268 */
1269normal_end:
1270
1271 msg_nowait = FALSE;
1272
1273 /* Reset finish_op, in case it was set */
1274#ifdef CURSOR_SHAPE
1275 c = finish_op;
1276#endif
1277 finish_op = FALSE;
1278#ifdef CURSOR_SHAPE
1279 /* Redraw the cursor with another shape, if we were in Operator-pending
1280 * mode or did a replace command. */
1281 if (c || ca.cmdchar == 'r')
1282 {
1283 ui_cursor_shape(); /* may show different cursor shape */
1284# ifdef FEAT_MOUSESHAPE
1285 update_mouseshape(-1);
1286# endif
1287 }
1288#endif
1289
1290#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001291 if (oap->op_type == OP_NOP && oap->regname == 0
1292# ifdef FEAT_AUTOCMD
1293 && ca.cmdchar != K_CURSORHOLD
1294# endif
1295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 clear_showcmd();
1297#endif
1298
1299 checkpcmark(); /* check if we moved since setting pcmark */
1300 vim_free(ca.searchbuf);
1301
1302#ifdef FEAT_MBYTE
1303 if (has_mbyte)
1304 mb_adjust_cursor();
1305#endif
1306
1307#ifdef FEAT_SCROLLBIND
1308 if (curwin->w_p_scb && toplevel)
1309 {
1310 validate_cursor(); /* may need to update w_leftcol */
1311 do_check_scrollbind(TRUE);
1312 }
1313#endif
1314
Bram Moolenaar860cae12010-06-05 23:22:07 +02001315#ifdef FEAT_CURSORBIND
1316 if (curwin->w_p_crb && toplevel)
1317 {
1318 validate_cursor(); /* may need to update w_leftcol */
1319 do_check_cursorbind();
1320 }
1321#endif
1322
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 /*
1324 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1325 * if still inside a mapping that started in Visual mode).
1326 * May switch from Visual to Select mode after CTRL-O command.
1327 */
1328 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1330 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && !(ca.retval & CA_COMMAND_BUSY)
1332 && stuff_empty()
1333 && oap->regname == 0)
1334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (restart_VIsual_select == 1)
1336 {
1337 VIsual_select = TRUE;
1338 showmode();
1339 restart_VIsual_select = 0;
1340 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001341 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 (void)edit(restart_edit, FALSE, 1L);
1343 }
1344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (restart_VIsual_select == 2)
1346 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 /* Save count before an operator for next time. */
1349 opcount = ca.opcount;
1350}
1351
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001352#ifdef FEAT_EVAL
1353/*
1354 * Set v:count and v:count1 according to "cap".
1355 * Set v:prevcount only when "set_prevcount" is TRUE.
1356 */
1357 static void
1358set_vcount_ca(cap, set_prevcount)
1359 cmdarg_T *cap;
1360 int *set_prevcount;
1361{
1362 long count = cap->count0;
1363
1364 /* multiply with cap->opcount the same way as above */
1365 if (cap->opcount != 0)
1366 count = cap->opcount * (count == 0 ? 1 : count);
1367 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1368 *set_prevcount = FALSE; /* only set v:prevcount once */
1369}
1370#endif
1371
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372/*
1373 * Handle an operator after visual mode or when the movement is finished
1374 */
1375 void
1376do_pending_operator(cap, old_col, gui_yank)
1377 cmdarg_T *cap;
1378 int old_col;
1379 int gui_yank;
1380{
1381 oparg_T *oap = cap->oap;
1382 pos_T old_cursor;
1383 int empty_region_error;
1384 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001385#ifdef FEAT_LINEBREAK
1386 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 /* The visual area is remembered for redo */
1390 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1391 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001392 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001394 static int redo_VIsual_arg; /* extra argument */
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;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001702 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 }
1705
1706 /*
1707 * oap->inclusive defaults to TRUE.
1708 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1709 * FALSE. This makes "d}P" and "v}dP" work the same.
1710 */
1711 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1712 oap->inclusive = TRUE;
1713 if (VIsual_mode == 'V')
1714 oap->motion_type = MLINE;
1715 else
1716 {
1717 oap->motion_type = MCHAR;
1718 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001719#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 )
1723 {
1724 oap->inclusive = FALSE;
1725 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001726 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001727 if (*p_sel != 'o'
1728 && !op_on_lines(oap->op_type)
1729 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001731 ++oap->end.lnum;
1732 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001733#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001734 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001735#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001736 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 }
1739 }
1740
1741 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /*
1744 * Switch Visual off now, so screen updating does
1745 * not show inverted text when the screen is redrawn.
1746 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1747 * no screen redraw, so it is done here to remove the inverted
1748 * part.
1749 */
1750 if (!gui_yank)
1751 {
1752 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001753#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 setmouse();
1755 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001756#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001757 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if ((oap->op_type == OP_YANK
1759 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001760 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 || oap->op_type == OP_FILTER)
1762 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001763 {
1764#ifdef FEAT_LINEBREAK
1765 /* make sure redrawing is correct */
1766 curwin->w_p_lbr = lbr_saved;
1767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 }
1771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773#ifdef FEAT_MBYTE
1774 /* Include the trailing byte of a multi-byte char. */
1775 if (has_mbyte && oap->inclusive)
1776 {
1777 int l;
1778
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001779 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (l > 1)
1781 oap->end.col += l - 1;
1782 }
1783#endif
1784 curwin->w_set_curswant = TRUE;
1785
1786 /*
1787 * oap->empty is set when start and end are the same. The inclusive
1788 * flag affects this too, unless yanking and the end is on a NUL.
1789 */
1790 oap->empty = (oap->motion_type == MCHAR
1791 && (!oap->inclusive
1792 || (oap->op_type == OP_YANK
1793 && gchar_pos(&oap->end) == NUL))
1794 && equalpos(oap->start, oap->end)
1795#ifdef FEAT_VIRTUALEDIT
1796 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1797#endif
1798 );
1799 /*
1800 * For delete, change and yank, it's an error to operate on an
1801 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1802 */
1803 empty_region_error = (oap->empty
1804 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 /* Force a redraw when operating on an empty Visual region, when
1807 * 'modifiable is off or creating a fold. */
1808 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001809#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001813 {
1814#ifdef FEAT_LINEBREAK
1815 curwin->w_p_lbr = lbr_saved;
1816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820 /*
1821 * If the end of an operator is in column one while oap->motion_type
1822 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1823 * character in the previous line. If op_start is on or before the
1824 * first non-blank in the line, the operator becomes linewise
1825 * (strange, but that's the way vi does it).
1826 */
1827 if ( oap->motion_type == MCHAR
1828 && oap->inclusive == FALSE
1829 && !(cap->retval & CA_NO_ADJ_OP_END)
1830 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001832 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && oap->line_count > 1)
1834 {
1835 oap->end_adjusted = TRUE; /* remember that we did this */
1836 --oap->line_count;
1837 --oap->end.lnum;
1838 if (inindent(0))
1839 oap->motion_type = MLINE;
1840 else
1841 {
1842 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1843 if (oap->end.col)
1844 {
1845 --oap->end.col;
1846 oap->inclusive = TRUE;
1847 }
1848 }
1849 }
1850 else
1851 oap->end_adjusted = FALSE;
1852
1853 switch (oap->op_type)
1854 {
1855 case OP_LSHIFT:
1856 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001857 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 auto_format(FALSE, TRUE);
1859 break;
1860
1861 case OP_JOIN_NS:
1862 case OP_JOIN:
1863 if (oap->line_count < 2)
1864 oap->line_count = 2;
1865 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1866 curbuf->b_ml.ml_line_count)
1867 beep_flush();
1868 else
1869 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001870 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001871 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 auto_format(FALSE, TRUE);
1873 }
1874 break;
1875
1876 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001879 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001880 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001881 CancelRedo();
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 else
1884 {
1885 (void)op_delete(oap);
1886 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1887 u_save_cursor(); /* cursor line wasn't saved yet */
1888 auto_format(FALSE, TRUE);
1889 }
1890 break;
1891
1892 case OP_YANK:
1893 if (empty_region_error)
1894 {
1895 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001896 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001897 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001898 CancelRedo();
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 }
1901 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001902 {
1903#ifdef FEAT_LINEBREAK
1904 curwin->w_p_lbr = lbr_saved;
1905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 check_cursor_col();
1909 break;
1910
1911 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001914 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001915 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001916 CancelRedo();
1917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 else
1919 {
1920 /* This is a new edit command, not a restart. Need to
1921 * remember it to make 'insertmode' work with mappings for
1922 * Visual mode. But do this only once and not when typed and
1923 * 'insertmode' isn't set. */
1924 if (p_im || !KeyTyped)
1925 restart_edit_save = restart_edit;
1926 else
1927 restart_edit_save = 0;
1928 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001929#ifdef FEAT_LINEBREAK
1930 /* Restore linebreak, so that when the user edits it looks as
1931 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001932 if (curwin->w_p_lbr != lbr_saved)
1933 {
1934 curwin->w_p_lbr = lbr_saved;
1935 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1936 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 /* Reset finish_op now, don't want it set inside edit(). */
1939 finish_op = FALSE;
1940 if (op_change(oap)) /* will call edit() */
1941 cap->retval |= CA_COMMAND_BUSY;
1942 if (restart_edit == 0)
1943 restart_edit = restart_edit_save;
1944 }
1945 break;
1946
1947 case OP_FILTER:
1948 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1949 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1950 else
1951 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1952
1953 case OP_INDENT:
1954 case OP_COLON:
1955
1956#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1957 /*
1958 * If 'equalprg' is empty, do the indenting internally.
1959 */
1960 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1961 {
1962# ifdef FEAT_LISP
1963 if (curbuf->b_p_lisp)
1964 {
1965 op_reindent(oap, get_lisp_indent);
1966 break;
1967 }
1968# endif
1969# ifdef FEAT_CINDENT
1970 op_reindent(oap,
1971# ifdef FEAT_EVAL
1972 *curbuf->b_p_inde != NUL ? get_expr_indent :
1973# endif
1974 get_c_indent);
1975 break;
1976# endif
1977 }
1978#endif
1979
1980 op_colon(oap);
1981 break;
1982
1983 case OP_TILDE:
1984 case OP_UPPER:
1985 case OP_LOWER:
1986 case OP_ROT13:
1987 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001988 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001989 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001990 CancelRedo();
1991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 else
1993 op_tilde(oap);
1994 check_cursor_col();
1995 break;
1996
1997 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001998#if defined(FEAT_EVAL)
1999 if (*curbuf->b_p_fex != NUL)
2000 op_formatexpr(oap); /* use expression */
2001 else
2002#endif
2003 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 op_colon(oap); /* use external command */
2005 else
2006 op_format(oap, FALSE); /* use internal function */
2007 break;
2008
2009 case OP_FORMAT2:
2010 op_format(oap, TRUE); /* use internal function */
2011 break;
2012
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002013 case OP_FUNCTION:
2014 op_function(oap); /* call 'operatorfunc' */
2015 break;
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 case OP_INSERT:
2018 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020#ifdef FEAT_VISUALEXTRA
2021 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002022 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002023 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002024 CancelRedo();
2025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 else
2027 {
2028 /* This is a new edit command, not a restart. Need to
2029 * remember it to make 'insertmode' work with mappings for
2030 * Visual mode. But do this only once. */
2031 restart_edit_save = restart_edit;
2032 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002033#ifdef FEAT_LINEBREAK
2034 /* Restore linebreak, so that when the user edits it looks as
2035 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002036 if (curwin->w_p_lbr != lbr_saved)
2037 {
2038 curwin->w_p_lbr = lbr_saved;
2039 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2040 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002043#ifdef FEAT_LINEBREAK
2044 /* Reset linebreak, so that formatting works correctly. */
2045 curwin->w_p_lbr = FALSE;
2046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 /* TODO: when inserting in several lines, should format all
2049 * the lines. */
2050 auto_format(FALSE, TRUE);
2051
2052 if (restart_edit == 0)
2053 restart_edit = restart_edit_save;
2054 }
2055#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002056 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#endif
2058 break;
2059
2060 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062#ifdef FEAT_VISUALEXTRA
2063 if (empty_region_error)
2064#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002065 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002066 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002067 CancelRedo();
2068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#ifdef FEAT_VISUALEXTRA
2070 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002071 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002072# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002073 /* Restore linebreak, so that when the user edits it looks as
2074 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002075 if (curwin->w_p_lbr != lbr_saved)
2076 {
2077 curwin->w_p_lbr = lbr_saved;
2078 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2079 }
2080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#endif
2084 break;
2085
2086#ifdef FEAT_FOLDING
2087 case OP_FOLD:
2088 VIsual_reselect = FALSE; /* don't reselect now */
2089 foldCreate(oap->start.lnum, oap->end.lnum);
2090 break;
2091
2092 case OP_FOLDOPEN:
2093 case OP_FOLDOPENREC:
2094 case OP_FOLDCLOSE:
2095 case OP_FOLDCLOSEREC:
2096 VIsual_reselect = FALSE; /* don't reselect now */
2097 opFoldRange(oap->start.lnum, oap->end.lnum,
2098 oap->op_type == OP_FOLDOPEN
2099 || oap->op_type == OP_FOLDOPENREC,
2100 oap->op_type == OP_FOLDOPENREC
2101 || oap->op_type == OP_FOLDCLOSEREC,
2102 oap->is_VIsual);
2103 break;
2104
2105 case OP_FOLDDEL:
2106 case OP_FOLDDELREC:
2107 VIsual_reselect = FALSE; /* don't reselect now */
2108 deleteFold(oap->start.lnum, oap->end.lnum,
2109 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2110 break;
2111#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002112 case OP_NR_ADD:
2113 case OP_NR_SUB:
2114 if (empty_region_error)
2115 {
2116 vim_beep(BO_OPER);
2117 CancelRedo();
2118 }
2119 else
2120 {
2121 VIsual_active = TRUE;
2122#ifdef FEAT_LINEBREAK
2123 curwin->w_p_lbr = lbr_saved;
2124#endif
2125 op_addsub(oap, cap->count1, redo_VIsual_arg);
2126 VIsual_active = FALSE;
2127 }
2128 check_cursor_col();
2129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 default:
2131 clearopbeep(oap);
2132 }
2133#ifdef FEAT_VIRTUALEDIT
2134 virtual_op = MAYBE;
2135#endif
2136 if (!gui_yank)
2137 {
2138 /*
2139 * if 'sol' not set, go back to old column for some commands
2140 */
2141 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2142 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2143 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002144 {
2145#ifdef FEAT_LINEBREAK
2146 curwin->w_p_lbr = FALSE;
2147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 else
2152 {
2153 curwin->w_cursor = old_cursor;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 clearop(oap);
2157 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002158#ifdef FEAT_LINEBREAK
2159 curwin->w_p_lbr = lbr_saved;
2160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161}
2162
2163/*
2164 * Handle indent and format operators and visual mode ":".
2165 */
2166 static void
2167op_colon(oap)
2168 oparg_T *oap;
2169{
2170 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (oap->is_VIsual)
2172 stuffReadbuff((char_u *)"'<,'>");
2173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
2175 /*
2176 * Make the range look nice, so it can be repeated.
2177 */
2178 if (oap->start.lnum == curwin->w_cursor.lnum)
2179 stuffcharReadbuff('.');
2180 else
2181 stuffnumReadbuff((long)oap->start.lnum);
2182 if (oap->end.lnum != oap->start.lnum)
2183 {
2184 stuffcharReadbuff(',');
2185 if (oap->end.lnum == curwin->w_cursor.lnum)
2186 stuffcharReadbuff('.');
2187 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2188 stuffcharReadbuff('$');
2189 else if (oap->start.lnum == curwin->w_cursor.lnum)
2190 {
2191 stuffReadbuff((char_u *)".+");
2192 stuffnumReadbuff((long)oap->line_count - 1);
2193 }
2194 else
2195 stuffnumReadbuff((long)oap->end.lnum);
2196 }
2197 }
2198 if (oap->op_type != OP_COLON)
2199 stuffReadbuff((char_u *)"!");
2200 if (oap->op_type == OP_INDENT)
2201 {
2202#ifndef FEAT_CINDENT
2203 if (*get_equalprg() == NUL)
2204 stuffReadbuff((char_u *)"indent");
2205 else
2206#endif
2207 stuffReadbuff(get_equalprg());
2208 stuffReadbuff((char_u *)"\n");
2209 }
2210 else if (oap->op_type == OP_FORMAT)
2211 {
2212 if (*p_fp == NUL)
2213 stuffReadbuff((char_u *)"fmt");
2214 else
2215 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002216 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218
2219 /*
2220 * do_cmdline() does the rest
2221 */
2222}
2223
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002224/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002225 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002226 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002227 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002228op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002229 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002230{
2231#ifdef FEAT_EVAL
2232 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002233# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002234 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002235# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002236
2237 if (*p_opfunc == NUL)
2238 EMSG(_("E774: 'operatorfunc' is empty"));
2239 else
2240 {
2241 /* Set '[ and '] marks to text to be operated on. */
2242 curbuf->b_op_start = oap->start;
2243 curbuf->b_op_end = oap->end;
2244 if (oap->motion_type != MLINE && !oap->inclusive)
2245 /* Exclude the end position. */
2246 decl(&curbuf->b_op_end);
2247
2248 if (oap->block_mode)
2249 argv[0] = (char_u *)"block";
2250 else if (oap->motion_type == MLINE)
2251 argv[0] = (char_u *)"line";
2252 else
2253 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002254
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002255# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002256 /* Reset virtual_op so that 'virtualedit' can be changed in the
2257 * function. */
2258 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002259# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002260
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002261 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002262
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002263# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002264 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002265# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002266 }
2267#else
2268 EMSG(_("E775: Eval feature not available"));
2269#endif
2270}
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#if defined(FEAT_MOUSE) || defined(PROTO)
2273/*
2274 * Do the appropriate action for the current mouse click in the current mode.
2275 * Not used for Command-line mode.
2276 *
2277 * Normal Mode:
2278 * event modi- position visual change action
2279 * fier cursor window
2280 * left press - yes end yes
2281 * left press C yes end yes "^]" (2)
2282 * left press S yes end yes "*" (2)
2283 * left drag - yes start if moved no
2284 * left relse - yes start if moved no
2285 * middle press - yes if not active no put register
2286 * middle press - yes if active no yank and put
2287 * right press - yes start or extend yes
2288 * right press S yes no change yes "#" (2)
2289 * right drag - yes extend no
2290 * right relse - yes extend no
2291 *
2292 * Insert or Replace Mode:
2293 * event modi- position visual change action
2294 * fier cursor window
2295 * left press - yes (cannot be active) yes
2296 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2297 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2298 * left drag - yes start or extend (1) no CTRL-O (1)
2299 * left relse - yes start or extend (1) no CTRL-O (1)
2300 * middle press - no (cannot be active) no put register
2301 * right press - yes start or extend yes CTRL-O
2302 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2303 *
2304 * (1) only if mouse pointer moved since press
2305 * (2) only if click is in same buffer
2306 *
2307 * Return TRUE if start_arrow() should be called for edit mode.
2308 */
2309 int
2310do_mouse(oap, c, dir, count, fixindent)
2311 oparg_T *oap; /* operator argument, can be NULL */
2312 int c; /* K_LEFTMOUSE, etc */
2313 int dir; /* Direction to 'put' if necessary */
2314 long count;
2315 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2316{
2317 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2318 static int got_click = FALSE; /* got a click some time back */
2319
2320 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2321 int is_click; /* If FALSE it's a drag or release event */
2322 int is_drag; /* If TRUE it's a drag event */
2323 int jump_flags = 0; /* flags for jump_to_mouse() */
2324 pos_T start_visual;
2325 int moved; /* Has cursor moved? */
2326 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002327#ifdef FEAT_WINDOWS
2328 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#ifdef FEAT_VERTSPLIT
2331 int in_sep_line; /* mouse in vertical separator line */
2332#endif
2333 int c1, c2;
2334#if defined(FEAT_FOLDING)
2335 pos_T save_cursor;
2336#endif
2337 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 static pos_T orig_cursor;
2339 colnr_T leftcol, rightcol;
2340 pos_T end_visual;
2341 int diff;
2342 int old_active = VIsual_active;
2343 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 int regname;
2345
2346#if defined(FEAT_FOLDING)
2347 save_cursor = curwin->w_cursor;
2348#endif
2349
2350 /*
2351 * When GUI is active, always recognize mouse events, otherwise:
2352 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2353 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2354 * - For command line and insert mode 'mouse' is checked before calling
2355 * do_mouse().
2356 */
2357 if (do_always)
2358 do_always = FALSE;
2359 else
2360#ifdef FEAT_GUI
2361 if (!gui.in_use)
2362#endif
2363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (VIsual_active)
2365 {
2366 if (!mouse_has(MOUSE_VISUAL))
2367 return FALSE;
2368 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002369 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 return FALSE;
2371 }
2372
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002373 for (;;)
2374 {
2375 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2376 if (is_drag)
2377 {
2378 /* If the next character is the same mouse event then use that
2379 * one. Speeds up dragging the status line. */
2380 if (vpeekc() != NUL)
2381 {
2382 int nc;
2383 int save_mouse_row = mouse_row;
2384 int save_mouse_col = mouse_col;
2385
2386 /* Need to get the character, peeking doesn't get the actual
2387 * one. */
2388 nc = safe_vgetc();
2389 if (c == nc)
2390 continue;
2391 vungetc(nc);
2392 mouse_row = save_mouse_row;
2393 mouse_col = save_mouse_col;
2394 }
2395 }
2396 break;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399#ifdef FEAT_MOUSESHAPE
2400 /* May have stopped dragging the status or separator line. The pointer is
2401 * most likely still on the status or separator line. */
2402 if (!is_drag && drag_status_line)
2403 {
2404 drag_status_line = FALSE;
2405 update_mouseshape(SHAPE_IDX_STATUS);
2406 }
2407# ifdef FEAT_VERTSPLIT
2408 if (!is_drag && drag_sep_line)
2409 {
2410 drag_sep_line = FALSE;
2411 update_mouseshape(SHAPE_IDX_VSEP);
2412 }
2413# endif
2414#endif
2415
2416 /*
2417 * Ignore drag and release events if we didn't get a click.
2418 */
2419 if (is_click)
2420 got_click = TRUE;
2421 else
2422 {
2423 if (!got_click) /* didn't get click, ignore */
2424 return FALSE;
2425 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002426 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002428#ifdef FEAT_WINDOWS
2429 if (in_tab_line)
2430 {
2431 in_tab_line = FALSE;
2432 return FALSE;
2433 }
2434#endif
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /*
2439 * CTRL right mouse button does CTRL-T
2440 */
2441 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2442 {
2443 if (State & INSERT)
2444 stuffcharReadbuff(Ctrl_O);
2445 if (count > 1)
2446 stuffnumReadbuff(count);
2447 stuffcharReadbuff(Ctrl_T);
2448 got_click = FALSE; /* ignore drag&release now */
2449 return FALSE;
2450 }
2451
2452 /*
2453 * CTRL only works with left mouse button
2454 */
2455 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2456 return FALSE;
2457
2458 /*
2459 * When a modifier is down, ignore drag and release events, as well as
2460 * multiple clicks and the middle mouse button.
2461 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2462 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002463 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2464 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && (!is_click
2466 || (mod_mask & MOD_MASK_MULTI_CLICK)
2467 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002468 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 && mouse_model_popup()
2470 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002471 && !((mod_mask & MOD_MASK_ALT)
2472 && !mouse_model_popup()
2473 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 )
2475 return FALSE;
2476
2477 /*
2478 * If the button press was used as the movement command for an operator
2479 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2480 * drag/release events.
2481 */
2482 if (!is_click && which_button == MOUSE_MIDDLE)
2483 return FALSE;
2484
2485 if (oap != NULL)
2486 regname = oap->regname;
2487 else
2488 regname = 0;
2489
2490 /*
2491 * Middle mouse button does a 'put' of the selected text
2492 */
2493 if (which_button == MOUSE_MIDDLE)
2494 {
2495 if (State == NORMAL)
2496 {
2497 /*
2498 * If an operator was pending, we don't know what the user wanted
2499 * to do. Go back to normal mode: Clear the operator and beep().
2500 */
2501 if (oap != NULL && oap->op_type != OP_NOP)
2502 {
2503 clearopbeep(oap);
2504 return FALSE;
2505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
2508 * If visual was active, yank the highlighted text and put it
2509 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002510 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 */
2512 if (VIsual_active)
2513 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002514 if (VIsual_select)
2515 {
2516 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002517 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002518 }
2519 else
2520 {
2521 stuffcharReadbuff('y');
2522 stuffcharReadbuff(K_MIDDLEMOUSE);
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 do_always = TRUE; /* ignore 'mouse' setting next time */
2525 return FALSE;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * The rest is below jump_to_mouse()
2529 */
2530 }
2531
2532 else if ((State & INSERT) == 0)
2533 return FALSE;
2534
2535 /*
2536 * Middle click in insert mode doesn't move the mouse, just insert the
2537 * contents of a register. '.' register is special, can't insert that
2538 * with do_put().
2539 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2540 * happens for the GUI).
2541 */
2542 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2543 {
2544 if (regname == '.')
2545 insert_reg(regname, TRUE);
2546 else
2547 {
2548#ifdef FEAT_CLIPBOARD
2549 if (clip_star.available && regname == 0)
2550 regname = '*';
2551#endif
2552 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2553 insert_reg(regname, TRUE);
2554 else
2555 {
2556 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2557
2558 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2559 AppendCharToRedobuff(Ctrl_R);
2560 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2561 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2562 }
2563 }
2564 return FALSE;
2565 }
2566 }
2567
2568 /* When dragging or button-up stay in the same window. */
2569 if (!is_click)
2570 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2571
2572 start_visual.lnum = 0;
2573
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574#ifdef FEAT_WINDOWS
2575 /* Check for clicking in the tab page line. */
2576 if (mouse_row == 0 && firstwin->w_winrow > 0)
2577 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002578 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002579 {
2580 if (in_tab_line)
2581 {
2582 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002583 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2584 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002585 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002586 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002587 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588
Bram Moolenaarf740b292006-02-16 22:11:02 +00002589 /* click in a tab selects that tab page */
2590 if (is_click
2591# ifdef FEAT_CMDWIN
2592 && cmdwin_type == 0
2593# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002595 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002596 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002597 c1 = TabPageIdxs[mouse_col];
2598 if (c1 >= 0)
2599 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002600 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2601 {
2602 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002603 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002604 tabpage_new();
2605 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2606 }
2607 else
2608 {
2609 /* Go to specified tab page, or next one if not clicking
2610 * on a label. */
2611 goto_tabpage(c1);
2612
2613 /* It's like clicking on the status line of a window. */
2614 if (curwin != old_curwin)
2615 end_visual_mode();
2616 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617 }
2618 else if (c1 < 0)
2619 {
2620 tabpage_T *tp;
2621
2622 /* Close the current or specified tab page. */
2623 if (c1 == -999)
2624 tp = curtab;
2625 else
2626 tp = find_tabpage(-c1);
2627 if (tp == curtab)
2628 {
2629 if (first_tabpage->tp_next != NULL)
2630 tabpage_close(FALSE);
2631 }
2632 else if (tp != NULL)
2633 tabpage_close_other(tp, FALSE);
2634 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002635 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002636 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002637 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002638 else if (is_drag && in_tab_line)
2639 {
2640 c1 = TabPageIdxs[mouse_col];
2641 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2642 return FALSE;
2643 }
2644
Bram Moolenaarf740b292006-02-16 22:11:02 +00002645#endif
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 /*
2648 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2649 * right button up -> pop-up menu
2650 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002651 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 */
2653 if (mouse_model_popup())
2654 {
2655 if (which_button == MOUSE_RIGHT
2656 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2657 {
2658 /*
2659 * NOTE: Ignore right button down and drag mouse events.
2660 * Windows only shows the popup menu on the button up event.
2661 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002662#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2663 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (!is_click)
2665 return FALSE;
2666#endif
2667#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2668 if (is_click || is_drag)
2669 return FALSE;
2670#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002671#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2673 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2674 if (gui.in_use)
2675 {
2676 jump_flags = 0;
2677 if (STRCMP(p_mousem, "popup_setpos") == 0)
2678 {
2679 /* First set the cursor position before showing the popup
2680 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (VIsual_active)
2682 {
2683 pos_T m_pos;
2684
2685 /*
2686 * set MOUSE_MAY_STOP_VIS if we are outside the
2687 * selection or the current window (might have false
2688 * negative here)
2689 */
2690 if (mouse_row < W_WINROW(curwin)
2691 || mouse_row
2692 > (W_WINROW(curwin) + curwin->w_height))
2693 jump_flags = MOUSE_MAY_STOP_VIS;
2694 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2695 jump_flags = MOUSE_MAY_STOP_VIS;
2696 else
2697 {
2698 if ((lt(curwin->w_cursor, VIsual)
2699 && (lt(m_pos, curwin->w_cursor)
2700 || lt(VIsual, m_pos)))
2701 || (lt(VIsual, curwin->w_cursor)
2702 && (lt(m_pos, VIsual)
2703 || lt(curwin->w_cursor, m_pos))))
2704 {
2705 jump_flags = MOUSE_MAY_STOP_VIS;
2706 }
2707 else if (VIsual_mode == Ctrl_V)
2708 {
2709 getvcols(curwin, &curwin->w_cursor, &VIsual,
2710 &leftcol, &rightcol);
2711 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2712 if (m_pos.col < leftcol || m_pos.col > rightcol)
2713 jump_flags = MOUSE_MAY_STOP_VIS;
2714 }
2715 }
2716 }
2717 else
2718 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 if (jump_flags)
2721 {
2722 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002723 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 setcursor();
2725 out_flush(); /* Update before showing popup menu */
2726 }
2727# ifdef FEAT_MENU
2728 gui_show_popupmenu();
2729# endif
2730 return (jump_flags & CURSOR_MOVED) != 0;
2731 }
2732 else
2733 return FALSE;
2734#else
2735 return FALSE;
2736#endif
2737 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002738 if (which_button == MOUSE_LEFT
2739 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
2741 which_button = MOUSE_RIGHT;
2742 mod_mask &= ~MOD_MASK_SHIFT;
2743 }
2744 }
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if ((State & (NORMAL | INSERT))
2747 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2748 {
2749 if (which_button == MOUSE_LEFT)
2750 {
2751 if (is_click)
2752 {
2753 /* stop Visual mode for a left click in a window, but not when
2754 * on a status line */
2755 if (VIsual_active)
2756 jump_flags |= MOUSE_MAY_STOP_VIS;
2757 }
2758 else if (mouse_has(MOUSE_VISUAL))
2759 jump_flags |= MOUSE_MAY_VIS;
2760 }
2761 else if (which_button == MOUSE_RIGHT)
2762 {
2763 if (is_click && VIsual_active)
2764 {
2765 /*
2766 * Remember the start and end of visual before moving the
2767 * cursor.
2768 */
2769 if (lt(curwin->w_cursor, VIsual))
2770 {
2771 start_visual = curwin->w_cursor;
2772 end_visual = VIsual;
2773 }
2774 else
2775 {
2776 start_visual = VIsual;
2777 end_visual = curwin->w_cursor;
2778 }
2779 }
2780 jump_flags |= MOUSE_FOCUS;
2781 if (mouse_has(MOUSE_VISUAL))
2782 jump_flags |= MOUSE_MAY_VIS;
2783 }
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 /*
2787 * If an operator is pending, ignore all drags and releases until the
2788 * next mouse click.
2789 */
2790 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2791 {
2792 got_click = FALSE;
2793 oap->motion_type = MCHAR;
2794 }
2795
2796 /* When releasing the button let jump_to_mouse() know. */
2797 if (!is_click && !is_drag)
2798 jump_flags |= MOUSE_RELEASED;
2799
2800 /*
2801 * JUMP!
2802 */
2803 jump_flags = jump_to_mouse(jump_flags,
2804 oap == NULL ? NULL : &(oap->inclusive), which_button);
2805 moved = (jump_flags & CURSOR_MOVED);
2806 in_status_line = (jump_flags & IN_STATUS_LINE);
2807#ifdef FEAT_VERTSPLIT
2808 in_sep_line = (jump_flags & IN_SEP_LINE);
2809#endif
2810
2811#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002812 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2814 {
2815 int key = KEY2TERMCAP1(c);
2816
2817 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2818 || key == (int)KE_RIGHTRELEASE)
2819 netbeans_button_release(which_button);
2820 }
2821#endif
2822
2823 /* When jumping to another window, clear a pending operator. That's a bit
2824 * friendlier than beeping and not jumping to that window. */
2825 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2826 clearop(oap);
2827
2828#ifdef FEAT_FOLDING
2829 if (mod_mask == 0
2830 && !is_drag
2831 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2832 && which_button == MOUSE_LEFT)
2833 {
2834 /* open or close a fold at this line */
2835 if (jump_flags & MOUSE_FOLD_OPEN)
2836 openFold(curwin->w_cursor.lnum, 1L);
2837 else
2838 closeFold(curwin->w_cursor.lnum, 1L);
2839 /* don't move the cursor if still in the same window */
2840 if (curwin == old_curwin)
2841 curwin->w_cursor = save_cursor;
2842 }
2843#endif
2844
2845#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2846 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2847 {
2848 clip_modeless(which_button, is_click, is_drag);
2849 return FALSE;
2850 }
2851#endif
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002854 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (VIsual_active && is_drag && p_so)
2856 {
2857 /* In the very first line, allow scrolling one line */
2858 if (mouse_row == 0)
2859 mouse_dragging = 2;
2860 else
2861 mouse_dragging = 1;
2862 }
2863
2864 /* When dragging the mouse above the window, scroll down. */
2865 if (is_drag && mouse_row < 0 && !in_status_line)
2866 {
2867 scroll_redraw(FALSE, 1L);
2868 mouse_row = 0;
2869 }
2870
2871 if (start_visual.lnum) /* right click in visual mode */
2872 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002873 /* When ALT is pressed make Visual mode blockwise. */
2874 if (mod_mask & MOD_MASK_ALT)
2875 VIsual_mode = Ctrl_V;
2876
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 /*
2878 * In Visual-block mode, divide the area in four, pick up the corner
2879 * that is in the quarter that the cursor is in.
2880 */
2881 if (VIsual_mode == Ctrl_V)
2882 {
2883 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2884 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2885 end_visual.col = leftcol;
2886 else
2887 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002888 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 end_visual.lnum = start_visual.lnum;
2891
2892 /* move VIsual to the right column */
2893 start_visual = curwin->w_cursor; /* save the cursor pos */
2894 curwin->w_cursor = end_visual;
2895 coladvance(end_visual.col);
2896 VIsual = curwin->w_cursor;
2897 curwin->w_cursor = start_visual; /* restore the cursor */
2898 }
2899 else
2900 {
2901 /*
2902 * If the click is before the start of visual, change the start.
2903 * If the click is after the end of visual, change the end. If
2904 * the click is inside the visual, change the closest side.
2905 */
2906 if (lt(curwin->w_cursor, start_visual))
2907 VIsual = end_visual;
2908 else if (lt(end_visual, curwin->w_cursor))
2909 VIsual = start_visual;
2910 else
2911 {
2912 /* In the same line, compare column number */
2913 if (end_visual.lnum == start_visual.lnum)
2914 {
2915 if (curwin->w_cursor.col - start_visual.col >
2916 end_visual.col - curwin->w_cursor.col)
2917 VIsual = start_visual;
2918 else
2919 VIsual = end_visual;
2920 }
2921
2922 /* In different lines, compare line number */
2923 else
2924 {
2925 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2926 (end_visual.lnum - curwin->w_cursor.lnum);
2927
2928 if (diff > 0) /* closest to end */
2929 VIsual = start_visual;
2930 else if (diff < 0) /* closest to start */
2931 VIsual = end_visual;
2932 else /* in the middle line */
2933 {
2934 if (curwin->w_cursor.col <
2935 (start_visual.col + end_visual.col) / 2)
2936 VIsual = end_visual;
2937 else
2938 VIsual = start_visual;
2939 }
2940 }
2941 }
2942 }
2943 }
2944 /*
2945 * If Visual mode started in insert mode, execute "CTRL-O"
2946 */
2947 else if ((State & INSERT) && VIsual_active)
2948 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /*
2951 * Middle mouse click: Put text before cursor.
2952 */
2953 if (which_button == MOUSE_MIDDLE)
2954 {
2955#ifdef FEAT_CLIPBOARD
2956 if (clip_star.available && regname == 0)
2957 regname = '*';
2958#endif
2959 if (yank_register_mline(regname))
2960 {
2961 if (mouse_past_bottom)
2962 dir = FORWARD;
2963 }
2964 else if (mouse_past_eol)
2965 dir = FORWARD;
2966
2967 if (fixindent)
2968 {
2969 c1 = (dir == BACKWARD) ? '[' : ']';
2970 c2 = 'p';
2971 }
2972 else
2973 {
2974 c1 = (dir == FORWARD) ? 'p' : 'P';
2975 c2 = NUL;
2976 }
2977 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2978
2979 /*
2980 * Remember where the paste started, so in edit() Insstart can be set
2981 * to this position
2982 */
2983 if (restart_edit != 0)
2984 where_paste_started = curwin->w_cursor;
2985 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2986 }
2987
2988#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2989 /*
2990 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2991 * error under the mouse pointer.
2992 */
2993 else if (((mod_mask & MOD_MASK_CTRL)
2994 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2995 && bt_quickfix(curbuf))
2996 {
2997 if (State & INSERT)
2998 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002999 if (curwin->w_llist_ref == NULL) /* quickfix window */
3000 stuffReadbuff((char_u *)":.cc\n");
3001 else /* location list window */
3002 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 got_click = FALSE; /* ignore drag&release now */
3004 }
3005#endif
3006
3007 /*
3008 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3009 * under the mouse pointer.
3010 */
3011 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3012 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3013 {
3014 if (State & INSERT)
3015 stuffcharReadbuff(Ctrl_O);
3016 stuffcharReadbuff(Ctrl_RSB);
3017 got_click = FALSE; /* ignore drag&release now */
3018 }
3019
3020 /*
3021 * Shift-Mouse click searches for the next occurrence of the word under
3022 * the mouse pointer
3023 */
3024 else if ((mod_mask & MOD_MASK_SHIFT))
3025 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003026 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 stuffcharReadbuff(Ctrl_O);
3028 if (which_button == MOUSE_LEFT)
3029 stuffcharReadbuff('*');
3030 else /* MOUSE_RIGHT */
3031 stuffcharReadbuff('#');
3032 }
3033
3034 /* Handle double clicks, unless on status line */
3035 else if (in_status_line)
3036 {
3037#ifdef FEAT_MOUSESHAPE
3038 if ((is_drag || is_click) && !drag_status_line)
3039 {
3040 drag_status_line = TRUE;
3041 update_mouseshape(-1);
3042 }
3043#endif
3044 }
3045#ifdef FEAT_VERTSPLIT
3046 else if (in_sep_line)
3047 {
3048# ifdef FEAT_MOUSESHAPE
3049 if ((is_drag || is_click) && !drag_sep_line)
3050 {
3051 drag_sep_line = TRUE;
3052 update_mouseshape(-1);
3053 }
3054# endif
3055 }
3056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3058 && mouse_has(MOUSE_VISUAL))
3059 {
3060 if (is_click || !VIsual_active)
3061 {
3062 if (VIsual_active)
3063 orig_cursor = VIsual;
3064 else
3065 {
3066 check_visual_highlight();
3067 VIsual = curwin->w_cursor;
3068 orig_cursor = VIsual;
3069 VIsual_active = TRUE;
3070 VIsual_reselect = TRUE;
3071 /* start Select mode if 'selectmode' contains "mouse" */
3072 may_start_select('o');
3073 setmouse();
3074 }
3075 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003076 {
3077 /* Double click with ALT pressed makes it blockwise. */
3078 if (mod_mask & MOD_MASK_ALT)
3079 VIsual_mode = Ctrl_V;
3080 else
3081 VIsual_mode = 'v';
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3084 VIsual_mode = 'V';
3085 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3086 VIsual_mode = Ctrl_V;
3087#ifdef FEAT_CLIPBOARD
3088 /* Make sure the clipboard gets updated. Needed because start and
3089 * end may still be the same, and the selection needs to be owned */
3090 clip_star.vmode = NUL;
3091#endif
3092 }
3093 /*
3094 * A double click selects a word or a block.
3095 */
3096 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3097 {
3098 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003099 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 if (is_click)
3102 {
3103 /* If the character under the cursor (skipping white space) is
3104 * not a word character, try finding a match and select a (),
3105 * {}, [], #if/#endif, etc. block. */
3106 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003107 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 inc(&end_visual);
3109 if (oap != NULL)
3110 oap->motion_type = MCHAR;
3111 if (oap != NULL
3112 && VIsual_mode == 'v'
3113 && !vim_iswordc(gchar_pos(&end_visual))
3114 && equalpos(curwin->w_cursor, VIsual)
3115 && (pos = findmatch(oap, NUL)) != NULL)
3116 {
3117 curwin->w_cursor = *pos;
3118 if (oap->motion_type == MLINE)
3119 VIsual_mode = 'V';
3120 else if (*p_sel == 'e')
3121 {
3122 if (lt(curwin->w_cursor, VIsual))
3123 ++VIsual.col;
3124 else
3125 ++curwin->w_cursor.col;
3126 }
3127 }
3128 }
3129
3130 if (pos == NULL && (is_click || is_drag))
3131 {
3132 /* When not found a match or when dragging: extend to include
3133 * a word. */
3134 if (lt(curwin->w_cursor, orig_cursor))
3135 {
3136 find_start_of_word(&curwin->w_cursor);
3137 find_end_of_word(&VIsual);
3138 }
3139 else
3140 {
3141 find_start_of_word(&VIsual);
3142 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3143#ifdef FEAT_MBYTE
3144 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003145 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#else
3147 ++curwin->w_cursor.col;
3148#endif
3149 find_end_of_word(&curwin->w_cursor);
3150 }
3151 }
3152 curwin->w_set_curswant = TRUE;
3153 }
3154 if (is_click)
3155 redraw_curbuf_later(INVERTED); /* update the inversion */
3156 }
3157 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003158 {
3159 if (mod_mask & MOD_MASK_ALT)
3160 VIsual_mode = Ctrl_V;
3161 else
3162 VIsual_mode = 'v';
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003166 if ((!VIsual_active && old_active && mode_displayed)
3167 || (VIsual_active && p_smd && msg_silent == 0
3168 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 return moved;
3172}
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174/*
3175 * Move "pos" back to the start of the word it's in.
3176 */
3177 static void
3178find_start_of_word(pos)
3179 pos_T *pos;
3180{
3181 char_u *line;
3182 int cclass;
3183 int col;
3184
3185 line = ml_get(pos->lnum);
3186 cclass = get_mouse_class(line + pos->col);
3187
3188 while (pos->col > 0)
3189 {
3190 col = pos->col - 1;
3191#ifdef FEAT_MBYTE
3192 col -= (*mb_head_off)(line, line + col);
3193#endif
3194 if (get_mouse_class(line + col) != cclass)
3195 break;
3196 pos->col = col;
3197 }
3198}
3199
3200/*
3201 * Move "pos" forward to the end of the word it's in.
3202 * When 'selection' is "exclusive", the position is just after the word.
3203 */
3204 static void
3205find_end_of_word(pos)
3206 pos_T *pos;
3207{
3208 char_u *line;
3209 int cclass;
3210 int col;
3211
3212 line = ml_get(pos->lnum);
3213 if (*p_sel == 'e' && pos->col > 0)
3214 {
3215 --pos->col;
3216#ifdef FEAT_MBYTE
3217 pos->col -= (*mb_head_off)(line, line + pos->col);
3218#endif
3219 }
3220 cclass = get_mouse_class(line + pos->col);
3221 while (line[pos->col] != NUL)
3222 {
3223#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003224 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#else
3226 col = pos->col + 1;
3227#endif
3228 if (get_mouse_class(line + col) != cclass)
3229 {
3230 if (*p_sel == 'e')
3231 pos->col = col;
3232 break;
3233 }
3234 pos->col = col;
3235 }
3236}
3237
3238/*
3239 * Get class of a character for selection: same class means same word.
3240 * 0: blank
3241 * 1: punctuation groups
3242 * 2: normal word character
3243 * >2: multi-byte word character.
3244 */
3245 static int
3246get_mouse_class(p)
3247 char_u *p;
3248{
3249 int c;
3250
3251#ifdef FEAT_MBYTE
3252 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3253 return mb_get_class(p);
3254#endif
3255
3256 c = *p;
3257 if (c == ' ' || c == '\t')
3258 return 0;
3259
3260 if (vim_iswordc(c))
3261 return 2;
3262
3263 /*
3264 * There are a few special cases where we want certain combinations of
3265 * characters to be considered as a single word. These are things like
3266 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003267 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3270 return 1;
3271 return c;
3272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273#endif /* FEAT_MOUSE */
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
3276 * Check if highlighting for visual mode is possible, give a warning message
3277 * if not.
3278 */
3279 void
3280check_visual_highlight()
3281{
3282 static int did_check = FALSE;
3283
3284 if (full_screen)
3285 {
3286 if (!did_check && hl_attr(HLF_V) == 0)
3287 MSG(_("Warning: terminal cannot highlight"));
3288 did_check = TRUE;
3289 }
3290}
3291
3292/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003293 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 * This function should ALWAYS be called to end Visual mode, except from
3295 * do_pending_operator().
3296 */
3297 void
3298end_visual_mode()
3299{
3300#ifdef FEAT_CLIPBOARD
3301 /*
3302 * If we are using the clipboard, then remember what was selected in case
3303 * we need to paste it somewhere while we still own the selection.
3304 * Only do this when the clipboard is already owned. Don't want to grab
3305 * the selection when hitting ESC.
3306 */
3307 if (clip_star.available && clip_star.owned)
3308 clip_auto_select();
3309#endif
3310
3311 VIsual_active = FALSE;
3312#ifdef FEAT_MOUSE
3313 setmouse();
3314 mouse_dragging = 0;
3315#endif
3316
3317 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003318 curbuf->b_visual.vi_mode = VIsual_mode;
3319 curbuf->b_visual.vi_start = VIsual;
3320 curbuf->b_visual.vi_end = curwin->w_cursor;
3321 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#ifdef FEAT_EVAL
3323 curbuf->b_visual_mode_eval = VIsual_mode;
3324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#ifdef FEAT_VIRTUALEDIT
3326 if (!virtual_active())
3327 curwin->w_cursor.coladd = 0;
3328#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003329 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003331 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332}
3333
3334/*
3335 * Reset VIsual_active and VIsual_reselect.
3336 */
3337 void
3338reset_VIsual_and_resel()
3339{
3340 if (VIsual_active)
3341 {
3342 end_visual_mode();
3343 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3344 }
3345 VIsual_reselect = FALSE;
3346}
3347
3348/*
3349 * Reset VIsual_active and VIsual_reselect if it's set.
3350 */
3351 void
3352reset_VIsual()
3353{
3354 if (VIsual_active)
3355 {
3356 end_visual_mode();
3357 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3358 VIsual_reselect = FALSE;
3359 }
3360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003362#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3364
3365/*
3366 * Check for a balloon-eval special item to include when searching for an
3367 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3368 * Returns TRUE if the character at "*ptr" should be included.
3369 * "dir" is FORWARD or BACKWARD, the direction of searching.
3370 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3371 * "bnp" points to a counter for square brackets.
3372 */
3373 static int
3374find_is_eval_item(ptr, colp, bnp, dir)
3375 char_u *ptr;
3376 int *colp;
3377 int *bnp;
3378 int dir;
3379{
3380 /* Accept everything inside []. */
3381 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3382 ++*bnp;
3383 if (*bnp > 0)
3384 {
3385 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3386 --*bnp;
3387 return TRUE;
3388 }
3389
3390 /* skip over "s.var" */
3391 if (*ptr == '.')
3392 return TRUE;
3393
3394 /* two-character item: s->var */
3395 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3396 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3397 {
3398 *colp += dir;
3399 return TRUE;
3400 }
3401 return FALSE;
3402}
3403#endif
3404
3405/*
3406 * Find the identifier under or to the right of the cursor.
3407 * "find_type" can have one of three values:
3408 * FIND_IDENT: find an identifier (keyword)
3409 * FIND_STRING: find any non-white string
3410 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 *
3413 * There are three steps:
3414 * 1. Search forward for the start of an identifier/string. Doesn't move if
3415 * already on one.
3416 * 2. Search backward for the start of this identifier/string.
3417 * This doesn't match the real Vi but I like it a little better and it
3418 * shouldn't bother anyone.
3419 * 3. Search forward to the end of this identifier/string.
3420 * When FIND_IDENT isn't defined, we backup until a blank.
3421 *
3422 * Returns the length of the string, or zero if no string is found.
3423 * If a string is found, a pointer to the string is put in "*string". This
3424 * string is not always NUL terminated.
3425 */
3426 int
3427find_ident_under_cursor(string, find_type)
3428 char_u **string;
3429 int find_type;
3430{
3431 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3432 curwin->w_cursor.col, string, find_type);
3433}
3434
3435/*
3436 * Like find_ident_under_cursor(), but for any window and any position.
3437 * However: Uses 'iskeyword' from the current window!.
3438 */
3439 int
3440find_ident_at_pos(wp, lnum, startcol, string, find_type)
3441 win_T *wp;
3442 linenr_T lnum;
3443 colnr_T startcol;
3444 char_u **string;
3445 int find_type;
3446{
3447 char_u *ptr;
3448 int col = 0; /* init to shut up GCC */
3449 int i;
3450#ifdef FEAT_MBYTE
3451 int this_class = 0;
3452 int prev_class;
3453 int prevcol;
3454#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003455#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int bn = 0; /* bracket nesting */
3457#endif
3458
3459 /*
3460 * if i == 0: try to find an identifier
3461 * if i == 1: try to find any non-white string
3462 */
3463 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3464 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3465 {
3466 /*
3467 * 1. skip to start of identifier/string
3468 */
3469 col = startcol;
3470#ifdef FEAT_MBYTE
3471 if (has_mbyte)
3472 {
3473 while (ptr[col] != NUL)
3474 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Stop at a ']' to evaluate "a[x]". */
3477 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3478 break;
3479# endif
3480 this_class = mb_get_class(ptr + col);
3481 if (this_class != 0 && (i == 1 || this_class != 1))
3482 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003483 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
3485 }
3486 else
3487#endif
3488 while (ptr[col] != NUL
3489 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3492# endif
3493 )
3494 ++col;
3495
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* When starting on a ']' count it, so that we include the '['. */
3498 bn = ptr[col] == ']';
3499#endif
3500
3501 /*
3502 * 2. Back up to start of identifier/string.
3503 */
3504#ifdef FEAT_MBYTE
3505 if (has_mbyte)
3506 {
3507 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003508# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3510 this_class = mb_get_class((char_u *)"a");
3511 else
3512# endif
3513 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003514 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3517 prev_class = mb_get_class(ptr + prevcol);
3518 if (this_class != prev_class
3519 && (i == 0
3520 || prev_class == 0
3521 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 && (!(find_type & FIND_EVAL)
3524 || prevcol == 0
3525 || !find_is_eval_item(ptr + prevcol, &prevcol,
3526 &bn, BACKWARD))
3527# endif
3528 )
3529 break;
3530 col = prevcol;
3531 }
3532
3533 /* If we don't want just any old string, or we've found an
3534 * identifier, stop searching. */
3535 if (this_class > 2)
3536 this_class = 2;
3537 if (!(find_type & FIND_STRING) || this_class == 2)
3538 break;
3539 }
3540 else
3541#endif
3542 {
3543 while (col > 0
3544 && ((i == 0
3545 ? vim_iswordc(ptr[col - 1])
3546 : (!vim_iswhite(ptr[col - 1])
3547 && (!(find_type & FIND_IDENT)
3548 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 || ((find_type & FIND_EVAL)
3551 && col > 1
3552 && find_is_eval_item(ptr + col - 1, &col,
3553 &bn, BACKWARD))
3554#endif
3555 ))
3556 --col;
3557
3558 /* If we don't want just any old string, or we've found an
3559 * identifier, stop searching. */
3560 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3561 break;
3562 }
3563 }
3564
3565 if (ptr[col] == NUL || (i == 0 && (
3566#ifdef FEAT_MBYTE
3567 has_mbyte ? this_class != 2 :
3568#endif
3569 !vim_iswordc(ptr[col]))))
3570 {
3571 /*
3572 * didn't find an identifier or string
3573 */
3574 if (find_type & FIND_STRING)
3575 EMSG(_("E348: No string under cursor"));
3576 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003577 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 return 0;
3579 }
3580 ptr += col;
3581 *string = ptr;
3582
3583 /*
3584 * 3. Find the end if the identifier/string.
3585 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 bn = 0;
3588 startcol -= col;
3589#endif
3590 col = 0;
3591#ifdef FEAT_MBYTE
3592 if (has_mbyte)
3593 {
3594 /* Search for point of changing multibyte character class. */
3595 this_class = mb_get_class(ptr);
3596 while (ptr[col] != NUL
3597 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3598 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 || ((find_type & FIND_EVAL)
3601 && col <= (int)startcol
3602 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3603# endif
3604 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003605 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 else
3608#endif
3609 while ((i == 0 ? vim_iswordc(ptr[col])
3610 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003611# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 || ((find_type & FIND_EVAL)
3613 && col <= (int)startcol
3614 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3615# endif
3616 )
3617 {
3618 ++col;
3619 }
3620
3621 return col;
3622}
3623
3624/*
3625 * Prepare for redo of a normal command.
3626 */
3627 static void
3628prep_redo_cmd(cap)
3629 cmdarg_T *cap;
3630{
3631 prep_redo(cap->oap->regname, cap->count0,
3632 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3633}
3634
3635/*
3636 * Prepare for redo of any command.
3637 * Note that only the last argument can be a multi-byte char.
3638 */
3639 static void
3640prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3641 int regname;
3642 long num;
3643 int cmd1;
3644 int cmd2;
3645 int cmd3;
3646 int cmd4;
3647 int cmd5;
3648{
3649 ResetRedobuff();
3650 if (regname != 0) /* yank from specified buffer */
3651 {
3652 AppendCharToRedobuff('"');
3653 AppendCharToRedobuff(regname);
3654 }
3655 if (num)
3656 AppendNumberToRedobuff(num);
3657
3658 if (cmd1 != NUL)
3659 AppendCharToRedobuff(cmd1);
3660 if (cmd2 != NUL)
3661 AppendCharToRedobuff(cmd2);
3662 if (cmd3 != NUL)
3663 AppendCharToRedobuff(cmd3);
3664 if (cmd4 != NUL)
3665 AppendCharToRedobuff(cmd4);
3666 if (cmd5 != NUL)
3667 AppendCharToRedobuff(cmd5);
3668}
3669
3670/*
3671 * check for operator active and clear it
3672 *
3673 * return TRUE if operator was active
3674 */
3675 static int
3676checkclearop(oap)
3677 oparg_T *oap;
3678{
3679 if (oap->op_type == OP_NOP)
3680 return FALSE;
3681 clearopbeep(oap);
3682 return TRUE;
3683}
3684
3685/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003686 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003688 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 */
3690 static int
3691checkclearopq(oap)
3692 oparg_T *oap;
3693{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003694 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 return FALSE;
3696 clearopbeep(oap);
3697 return TRUE;
3698}
3699
3700 static void
3701clearop(oap)
3702 oparg_T *oap;
3703{
3704 oap->op_type = OP_NOP;
3705 oap->regname = 0;
3706 oap->motion_force = NUL;
3707 oap->use_reg_one = FALSE;
3708}
3709
3710 static void
3711clearopbeep(oap)
3712 oparg_T *oap;
3713{
3714 clearop(oap);
3715 beep_flush();
3716}
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * Remove the shift modifier from a special key.
3720 */
3721 static void
3722unshift_special(cap)
3723 cmdarg_T *cap;
3724{
3725 switch (cap->cmdchar)
3726 {
3727 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3728 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3729 case K_S_UP: cap->cmdchar = K_UP; break;
3730 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3731 case K_S_HOME: cap->cmdchar = K_HOME; break;
3732 case K_S_END: cap->cmdchar = K_END; break;
3733 }
3734 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003737/*
3738 * If the mode is currently displayed clear the command line or update the
3739 * command displayed.
3740 */
3741 static void
3742may_clear_cmdline()
3743{
3744 if (mode_displayed)
3745 clear_cmdline = TRUE; /* unshow visual mode later */
3746#ifdef FEAT_CMDL_INFO
3747 else
3748 clear_showcmd();
3749#endif
3750}
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3753/*
3754 * Routines for displaying a partly typed command
3755 */
3756
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003757#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758static char_u showcmd_buf[SHOWCMD_BUFLEN];
3759static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3760static int showcmd_is_clear = TRUE;
3761static int showcmd_visual = FALSE;
3762
3763static void display_showcmd __ARGS((void));
3764
3765 void
3766clear_showcmd()
3767{
3768 if (!p_sc)
3769 return;
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (VIsual_active && !char_avail())
3772 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003773 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 long lines;
3775 colnr_T leftcol, rightcol;
3776 linenr_T top, bot;
3777
3778 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003779 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
3781 top = VIsual.lnum;
3782 bot = curwin->w_cursor.lnum;
3783 }
3784 else
3785 {
3786 top = curwin->w_cursor.lnum;
3787 bot = VIsual.lnum;
3788 }
3789# ifdef FEAT_FOLDING
3790 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003791 (void)hasFolding(top, &top, NULL);
3792 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793# endif
3794 lines = bot - top + 1;
3795
3796 if (VIsual_mode == Ctrl_V)
3797 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003798# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003799 char_u *saved_sbr = p_sbr;
3800
3801 /* Make 'sbr' empty for a moment to get the correct size. */
3802 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003805# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003806 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3809 (long)(rightcol - leftcol + 1));
3810 }
3811 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3812 sprintf((char *)showcmd_buf, "%ld", lines);
3813 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003814 {
3815 char_u *s, *e;
3816 int l;
3817 int bytes = 0;
3818 int chars = 0;
3819
3820 if (cursor_bot)
3821 {
3822 s = ml_get_pos(&VIsual);
3823 e = ml_get_cursor();
3824 }
3825 else
3826 {
3827 s = ml_get_cursor();
3828 e = ml_get_pos(&VIsual);
3829 }
3830 while ((*p_sel != 'e') ? s <= e : s < e)
3831 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003832# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003833 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003834# else
3835 l = (*s == NUL) ? 0 : 1;
3836# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003837 if (l == 0)
3838 {
3839 ++bytes;
3840 ++chars;
3841 break; /* end of line */
3842 }
3843 bytes += l;
3844 ++chars;
3845 s += l;
3846 }
3847 if (bytes == chars)
3848 sprintf((char *)showcmd_buf, "%d", chars);
3849 else
3850 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3853 showcmd_visual = TRUE;
3854 }
3855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
3857 showcmd_buf[0] = NUL;
3858 showcmd_visual = FALSE;
3859
3860 /* Don't actually display something if there is nothing to clear. */
3861 if (showcmd_is_clear)
3862 return;
3863 }
3864
3865 display_showcmd();
3866}
3867
3868/*
3869 * Add 'c' to string of shown command chars.
3870 * Return TRUE if output has been written (and setcursor() has been called).
3871 */
3872 int
3873add_to_showcmd(c)
3874 int c;
3875{
3876 char_u *p;
3877 int old_len;
3878 int extra_len;
3879 int overflow;
3880#if defined(FEAT_MOUSE)
3881 int i;
3882 static int ignore[] =
3883 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003884# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3886 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 K_IGNORE,
3889 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3890 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3891 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003892 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003894 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 0
3896 };
3897#endif
3898
Bram Moolenaar09df3122006-01-23 22:23:09 +00003899 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 return FALSE;
3901
3902 if (showcmd_visual)
3903 {
3904 showcmd_buf[0] = NUL;
3905 showcmd_visual = FALSE;
3906 }
3907
3908#if defined(FEAT_MOUSE)
3909 /* Ignore keys that are scrollbar updates and mouse clicks */
3910 if (IS_SPECIAL(c))
3911 for (i = 0; ignore[i] != 0; ++i)
3912 if (ignore[i] == c)
3913 return FALSE;
3914#endif
3915
3916 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003917 if (*p == ' ')
3918 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 old_len = (int)STRLEN(showcmd_buf);
3920 extra_len = (int)STRLEN(p);
3921 overflow = old_len + extra_len - SHOWCMD_COLS;
3922 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003923 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3924 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 STRCAT(showcmd_buf, p);
3926
3927 if (char_avail())
3928 return FALSE;
3929
3930 display_showcmd();
3931
3932 return TRUE;
3933}
3934
3935 void
3936add_to_showcmd_c(c)
3937 int c;
3938{
3939 if (!add_to_showcmd(c))
3940 setcursor();
3941}
3942
3943/*
3944 * Delete 'len' characters from the end of the shown command.
3945 */
3946 static void
3947del_from_showcmd(len)
3948 int len;
3949{
3950 int old_len;
3951
3952 if (!p_sc)
3953 return;
3954
3955 old_len = (int)STRLEN(showcmd_buf);
3956 if (len > old_len)
3957 len = old_len;
3958 showcmd_buf[old_len - len] = NUL;
3959
3960 if (!char_avail())
3961 display_showcmd();
3962}
3963
3964/*
3965 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3966 * something and there is a partial mapping.
3967 */
3968 void
3969push_showcmd()
3970{
3971 if (p_sc)
3972 STRCPY(old_showcmd_buf, showcmd_buf);
3973}
3974
3975 void
3976pop_showcmd()
3977{
3978 if (!p_sc)
3979 return;
3980
3981 STRCPY(showcmd_buf, old_showcmd_buf);
3982
3983 display_showcmd();
3984}
3985
3986 static void
3987display_showcmd()
3988{
3989 int len;
3990
3991 cursor_off();
3992
3993 len = (int)STRLEN(showcmd_buf);
3994 if (len == 0)
3995 showcmd_is_clear = TRUE;
3996 else
3997 {
3998 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3999 showcmd_is_clear = FALSE;
4000 }
4001
4002 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004003 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4004 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 */
4006 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4007
4008 setcursor(); /* put cursor back where it belongs */
4009}
4010#endif
4011
4012#ifdef FEAT_SCROLLBIND
4013/*
4014 * When "check" is FALSE, prepare for commands that scroll the window.
4015 * When "check" is TRUE, take care of scroll-binding after the window has
4016 * scrolled. Called from normal_cmd() and edit().
4017 */
4018 void
4019do_check_scrollbind(check)
4020 int check;
4021{
4022 static win_T *old_curwin = NULL;
4023 static linenr_T old_topline = 0;
4024#ifdef FEAT_DIFF
4025 static int old_topfill = 0;
4026#endif
4027 static buf_T *old_buf = NULL;
4028 static colnr_T old_leftcol = 0;
4029
4030 if (check && curwin->w_p_scb)
4031 {
4032 /* If a ":syncbind" command was just used, don't scroll, only reset
4033 * the values. */
4034 if (did_syncbind)
4035 did_syncbind = FALSE;
4036 else if (curwin == old_curwin)
4037 {
4038 /*
4039 * Synchronize other windows, as necessary according to
4040 * 'scrollbind'. Don't do this after an ":edit" command, except
4041 * when 'diff' is set.
4042 */
4043 if ((curwin->w_buffer == old_buf
4044#ifdef FEAT_DIFF
4045 || curwin->w_p_diff
4046#endif
4047 )
4048 && (curwin->w_topline != old_topline
4049#ifdef FEAT_DIFF
4050 || curwin->w_topfill != old_topfill
4051#endif
4052 || curwin->w_leftcol != old_leftcol))
4053 {
4054 check_scrollbind(curwin->w_topline - old_topline,
4055 (long)(curwin->w_leftcol - old_leftcol));
4056 }
4057 }
4058 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4059 {
4060 /*
4061 * When switching between windows, make sure that the relative
4062 * vertical offset is valid for the new window. The relative
4063 * offset is invalid whenever another 'scrollbind' window has
4064 * scrolled to a point that would force the current window to
4065 * scroll past the beginning or end of its buffer. When the
4066 * resync is performed, some of the other 'scrollbind' windows may
4067 * need to jump so that the current window's relative position is
4068 * visible on-screen.
4069 */
4070 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4071 }
4072 curwin->w_scbind_pos = curwin->w_topline;
4073 }
4074
4075 old_curwin = curwin;
4076 old_topline = curwin->w_topline;
4077#ifdef FEAT_DIFF
4078 old_topfill = curwin->w_topfill;
4079#endif
4080 old_buf = curwin->w_buffer;
4081 old_leftcol = curwin->w_leftcol;
4082}
4083
4084/*
4085 * Synchronize any windows that have "scrollbind" set, based on the
4086 * number of rows by which the current window has changed
4087 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4088 */
4089 void
4090check_scrollbind(topline_diff, leftcol_diff)
4091 linenr_T topline_diff;
4092 long leftcol_diff;
4093{
4094 int want_ver;
4095 int want_hor;
4096 win_T *old_curwin = curwin;
4097 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 int old_VIsual_select = VIsual_select;
4099 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 colnr_T tgt_leftcol = curwin->w_leftcol;
4101 long topline;
4102 long y;
4103
4104 /*
4105 * check 'scrollopt' string for vertical and horizontal scroll options
4106 */
4107 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4108#ifdef FEAT_DIFF
4109 want_ver |= old_curwin->w_p_diff;
4110#endif
4111 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4112
4113 /*
4114 * loop through the scrollbound windows and scroll accordingly
4115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4118 {
4119 curbuf = curwin->w_buffer;
4120 /* skip original window and windows with 'noscrollbind' */
4121 if (curwin != old_curwin && curwin->w_p_scb)
4122 {
4123 /*
4124 * do the vertical scroll
4125 */
4126 if (want_ver)
4127 {
4128#ifdef FEAT_DIFF
4129 if (old_curwin->w_p_diff && curwin->w_p_diff)
4130 {
4131 diff_set_topline(old_curwin, curwin);
4132 }
4133 else
4134#endif
4135 {
4136 curwin->w_scbind_pos += topline_diff;
4137 topline = curwin->w_scbind_pos;
4138 if (topline > curbuf->b_ml.ml_line_count)
4139 topline = curbuf->b_ml.ml_line_count;
4140 if (topline < 1)
4141 topline = 1;
4142
4143 y = topline - curwin->w_topline;
4144 if (y > 0)
4145 scrollup(y, FALSE);
4146 else
4147 scrolldown(-y, FALSE);
4148 }
4149
4150 redraw_later(VALID);
4151 cursor_correct();
4152#ifdef FEAT_WINDOWS
4153 curwin->w_redr_status = TRUE;
4154#endif
4155 }
4156
4157 /*
4158 * do the horizontal scroll
4159 */
4160 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4161 {
4162 curwin->w_leftcol = tgt_leftcol;
4163 leftcol_changed();
4164 }
4165 }
4166 }
4167
4168 /*
4169 * reset current-window
4170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 VIsual_select = old_VIsual_select;
4172 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 curwin = old_curwin;
4174 curbuf = old_curbuf;
4175}
4176#endif /* #ifdef FEAT_SCROLLBIND */
4177
4178/*
4179 * Command character that's ignored.
4180 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004181 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 static void
4184nv_ignore(cap)
4185 cmdarg_T *cap;
4186{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004187 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188}
4189
4190/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004191 * Command character that doesn't do anything, but unlike nv_ignore() does
4192 * start edit(). Used for "startinsert" executed while starting up.
4193 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004194 static void
4195nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004196 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004197{
4198}
4199
4200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 * Command character doesn't exist.
4202 */
4203 static void
4204nv_error(cap)
4205 cmdarg_T *cap;
4206{
4207 clearopbeep(cap->oap);
4208}
4209
4210/*
4211 * <Help> and <F1> commands.
4212 */
4213 static void
4214nv_help(cap)
4215 cmdarg_T *cap;
4216{
4217 if (!checkclearopq(cap->oap))
4218 ex_help(NULL);
4219}
4220
4221/*
4222 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4223 */
4224 static void
4225nv_addsub(cap)
4226 cmdarg_T *cap;
4227{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004228 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004229 {
Bram Moolenaard79e5502016-01-10 22:13:02 +01004230 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4231 op_addsub(cap->oap, cap->count1, cap->arg);
4232 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004233 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004234 else if (VIsual_active)
4235 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004236 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004237 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238}
4239
4240/*
4241 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4242 */
4243 static void
4244nv_page(cap)
4245 cmdarg_T *cap;
4246{
4247 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004248 {
4249#ifdef FEAT_WINDOWS
4250 if (mod_mask & MOD_MASK_CTRL)
4251 {
4252 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4253 if (cap->arg == BACKWARD)
4254 goto_tabpage(-(int)cap->count1);
4255 else
4256 goto_tabpage((int)cap->count0);
4257 }
4258 else
4259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262}
4263
4264/*
4265 * Implementation of "gd" and "gD" command.
4266 */
4267 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004268nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004269 oparg_T *oap;
4270 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u *ptr;
4275
Bram Moolenaard9d30582005-05-18 22:10:28 +00004276 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004277 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004279#ifdef FEAT_FOLDING
4280 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4281 foldOpenCursor();
4282#endif
4283}
4284
4285/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004286 * Search for variable declaration of "ptr[len]".
4287 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4288 * current file ("gD").
4289 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004290 * Return FAIL when not found.
4291 */
4292 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004293find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004294 char_u *ptr;
4295 int len;
4296 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004297 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 int searchflags; /* flags passed to searchit() */
4299{
4300 char_u *pat;
4301 pos_T old_pos;
4302 pos_T par_pos;
4303 pos_T found_pos;
4304 int t;
4305 int save_p_ws;
4306 int save_p_scs;
4307 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004308 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004309
4310 if ((pat = alloc(len + 7)) == NULL)
4311 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004312
4313 /* Put "\V" before the pattern to avoid that the special meaning of "."
4314 * and "~" causes trouble. */
4315 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4316 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 old_pos = curwin->w_cursor;
4318 save_p_ws = p_ws;
4319 save_p_scs = p_scs;
4320 p_ws = FALSE; /* don't wrap around end of file now */
4321 p_scs = FALSE; /* don't switch ignorecase off now */
4322
4323 /*
4324 * With "gD" go to line 1.
4325 * With "gd" Search back for the start of the current function, then go
4326 * back until a blank line. If this fails go to line 1.
4327 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004328 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 setpcmark(); /* Set in findpar() otherwise */
4331 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004332 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 else
4335 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004336 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4338 --curwin->w_cursor.lnum;
4339 }
4340 curwin->w_cursor.col = 0;
4341
4342 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004343 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344 for (;;)
4345 {
4346 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004347 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004348 if (curwin->w_cursor.lnum >= old_pos.lnum)
4349 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004350
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004351 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004352 {
4353 pos_T *pos;
4354
4355 /* Check that the block the match is in doesn't end before the
4356 * position where we started the search from. */
4357 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4358 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4359 && pos->lnum < old_pos.lnum)
4360 continue;
4361 }
4362
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004363 if (t == FAIL)
4364 {
4365 /* If we previously found a valid position, use it. */
4366 if (found_pos.lnum != 0)
4367 {
4368 curwin->w_cursor = found_pos;
4369 t = OK;
4370 }
4371 break;
4372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004374 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 {
4376 /* Ignore this line, continue at start of next line. */
4377 ++curwin->w_cursor.lnum;
4378 curwin->w_cursor.col = 0;
4379 continue;
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004382 if (!locally) /* global search: use first match found */
4383 break;
4384 if (curwin->w_cursor.lnum >= par_pos.lnum)
4385 {
4386 /* If we previously found a valid position, use it. */
4387 if (found_pos.lnum != 0)
4388 curwin->w_cursor = found_pos;
4389 break;
4390 }
4391
4392 /* For finding a local variable and the match is before the "{" search
4393 * to find a later match. For K&R style function declarations this
4394 * skips the function header without types. */
4395 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004397
4398 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 curwin->w_cursor = old_pos;
4402 }
4403 else
4404 {
4405 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 /* "n" searches forward now */
4407 reset_search_dir();
4408 }
4409
4410 vim_free(pat);
4411 p_ws = save_p_ws;
4412 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004413
4414 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
4418 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4419 * lines rather than lines in the file.
4420 * 'dist' must be positive.
4421 *
4422 * Return OK if able to move cursor, FAIL otherwise.
4423 */
4424 static int
4425nv_screengo(oap, dir, dist)
4426 oparg_T *oap;
4427 int dir;
4428 long dist;
4429{
4430 int linelen = linetabsize(ml_get_curline());
4431 int retval = OK;
4432 int atend = FALSE;
4433 int n;
4434 int col_off1; /* margin offset for first screen line */
4435 int col_off2; /* margin offset for wrapped screen line */
4436 int width1; /* text width for first screen line */
4437 int width2; /* test width for wrapped screen line */
4438
4439 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004440 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 col_off1 = curwin_col_off();
4443 col_off2 = col_off1 - curwin_col_off2();
4444 width1 = W_WIDTH(curwin) - col_off1;
4445 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004446 if (width2 == 0)
4447 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449#ifdef FEAT_VERTSPLIT
4450 if (curwin->w_width != 0)
4451 {
4452#endif
4453 /*
4454 * Instead of sticking at the last character of the buffer line we
4455 * try to stick in the last column of the screen.
4456 */
4457 if (curwin->w_curswant == MAXCOL)
4458 {
4459 atend = TRUE;
4460 validate_virtcol();
4461 if (width1 <= 0)
4462 curwin->w_curswant = 0;
4463 else
4464 {
4465 curwin->w_curswant = width1 - 1;
4466 if (curwin->w_virtcol > curwin->w_curswant)
4467 curwin->w_curswant += ((curwin->w_virtcol
4468 - curwin->w_curswant - 1) / width2 + 1) * width2;
4469 }
4470 }
4471 else
4472 {
4473 if (linelen > width1)
4474 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4475 else
4476 n = width1;
4477 if (curwin->w_curswant > (colnr_T)n + 1)
4478 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4479 * width2;
4480 }
4481
4482 while (dist--)
4483 {
4484 if (dir == BACKWARD)
4485 {
4486 if ((long)curwin->w_curswant >= width2)
4487 /* move back within line */
4488 curwin->w_curswant -= width2;
4489 else
4490 {
4491 /* to previous line */
4492 if (curwin->w_cursor.lnum == 1)
4493 {
4494 retval = FAIL;
4495 break;
4496 }
4497 --curwin->w_cursor.lnum;
4498#ifdef FEAT_FOLDING
4499 /* Move to the start of a closed fold. Don't do that when
4500 * 'foldopen' contains "all": it will open in a moment. */
4501 if (!(fdo_flags & FDO_ALL))
4502 (void)hasFolding(curwin->w_cursor.lnum,
4503 &curwin->w_cursor.lnum, NULL);
4504#endif
4505 linelen = linetabsize(ml_get_curline());
4506 if (linelen > width1)
4507 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4508 + 1) * width2;
4509 }
4510 }
4511 else /* dir == FORWARD */
4512 {
4513 if (linelen > width1)
4514 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4515 else
4516 n = width1;
4517 if (curwin->w_curswant + width2 < (colnr_T)n)
4518 /* move forward within line */
4519 curwin->w_curswant += width2;
4520 else
4521 {
4522 /* to next line */
4523#ifdef FEAT_FOLDING
4524 /* Move to the end of a closed fold. */
4525 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4526 &curwin->w_cursor.lnum);
4527#endif
4528 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4529 {
4530 retval = FAIL;
4531 break;
4532 }
4533 curwin->w_cursor.lnum++;
4534 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004535 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537 }
4538 }
4539#ifdef FEAT_VERTSPLIT
4540 }
4541#endif
4542
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004543 if (virtual_active() && atend)
4544 coladvance(MAXCOL);
4545 else
4546 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4549 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4550 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004551 colnr_T virtcol;
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 /*
4554 * Check for landing on a character that got split at the end of the
4555 * last line. We want to advance a screenline, not end up in the same
4556 * screenline or move two screenlines.
4557 */
4558 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004559 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004560# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004561 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4562 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004563# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004564
4565 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 && (curwin->w_curswant < (colnr_T)width1
4567 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4568 : ((curwin->w_curswant - width1) % width2
4569 > (colnr_T)width2 / 2)))
4570 --curwin->w_cursor.col;
4571 }
4572#endif
4573
4574 if (atend)
4575 curwin->w_curswant = MAXCOL; /* stick in the last column */
4576
4577 return retval;
4578}
4579
4580#ifdef FEAT_MOUSE
4581/*
4582 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4583 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004584 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4585 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
4587 static void
4588nv_mousescroll(cap)
4589 cmdarg_T *cap;
4590{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004591# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004592 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004594 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 int row, col;
4597
4598 row = mouse_row;
4599 col = mouse_col;
4600
4601 /* find the window at the pointer coordinates */
4602 curwin = mouse_find_win(&row, &col);
4603 curbuf = curwin->w_buffer;
4604 }
4605# endif
4606
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004607 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4610 {
4611 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4612 }
4613 else
4614 {
4615 cap->count1 = 3;
4616 cap->count0 = 3;
4617 nv_scroll_line(cap);
4618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004620# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else
4622 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004623 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4624 if (!curwin->w_p_wrap)
4625 {
4626 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004627
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4629 step = W_WIDTH(curwin);
4630 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4631 if (val < 0)
4632 val = 0;
4633
4634 gui_do_horiz_scroll(val, TRUE);
4635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004639# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 curwin->w_redr_status = TRUE;
4641
4642 curwin = old_curwin;
4643 curbuf = curwin->w_buffer;
4644# endif
4645}
4646
4647/*
4648 * Mouse clicks and drags.
4649 */
4650 static void
4651nv_mouse(cap)
4652 cmdarg_T *cap;
4653{
4654 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4655}
4656#endif
4657
4658/*
4659 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4660 * cap->arg must be TRUE for CTRL-E.
4661 */
4662 static void
4663nv_scroll_line(cap)
4664 cmdarg_T *cap;
4665{
4666 if (!checkclearop(cap->oap))
4667 scroll_redraw(cap->arg, cap->count1);
4668}
4669
4670/*
4671 * Scroll "count" lines up or down, and redraw.
4672 */
4673 void
4674scroll_redraw(up, count)
4675 int up;
4676 long count;
4677{
4678 linenr_T prev_topline = curwin->w_topline;
4679#ifdef FEAT_DIFF
4680 int prev_topfill = curwin->w_topfill;
4681#endif
4682 linenr_T prev_lnum = curwin->w_cursor.lnum;
4683
4684 if (up)
4685 scrollup(count, TRUE);
4686 else
4687 scrolldown(count, TRUE);
4688 if (p_so)
4689 {
4690 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4691 * valid, otherwise the screen jumps back at the end of the file. */
4692 cursor_correct();
4693 check_cursor_moved(curwin);
4694 curwin->w_valid |= VALID_TOPLINE;
4695
4696 /* If moved back to where we were, at least move the cursor, otherwise
4697 * we get stuck at one position. Don't move the cursor up if the
4698 * first line of the buffer is already on the screen */
4699 while (curwin->w_topline == prev_topline
4700#ifdef FEAT_DIFF
4701 && curwin->w_topfill == prev_topfill
4702#endif
4703 )
4704 {
4705 if (up)
4706 {
4707 if (curwin->w_cursor.lnum > prev_lnum
4708 || cursor_down(1L, FALSE) == FAIL)
4709 break;
4710 }
4711 else
4712 {
4713 if (curwin->w_cursor.lnum < prev_lnum
4714 || prev_topline == 1L
4715 || cursor_up(1L, FALSE) == FAIL)
4716 break;
4717 }
4718 /* Mark w_topline as valid, otherwise the screen jumps back at the
4719 * end of the file. */
4720 check_cursor_moved(curwin);
4721 curwin->w_valid |= VALID_TOPLINE;
4722 }
4723 }
4724 if (curwin->w_cursor.lnum != prev_lnum)
4725 coladvance(curwin->w_curswant);
4726 redraw_later(VALID);
4727}
4728
4729/*
4730 * Commands that start with "z".
4731 */
4732 static void
4733nv_zet(cap)
4734 cmdarg_T *cap;
4735{
4736 long n;
4737 colnr_T col;
4738 int nchar = cap->nchar;
4739#ifdef FEAT_FOLDING
4740 long old_fdl = curwin->w_p_fdl;
4741 int old_fen = curwin->w_p_fen;
4742#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004743#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004744 int undo = FALSE;
4745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 if (VIM_ISDIGIT(nchar))
4748 {
4749 /*
4750 * "z123{nchar}": edit the count before obtaining {nchar}
4751 */
4752 if (checkclearop(cap->oap))
4753 return;
4754 n = nchar - '0';
4755 for (;;)
4756 {
4757#ifdef USE_ON_FLY_SCROLL
4758 dont_scroll = TRUE; /* disallow scrolling here */
4759#endif
4760 ++no_mapping;
4761 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004762 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 --no_mapping;
4765 --allow_keys;
4766#ifdef FEAT_CMDL_INFO
4767 (void)add_to_showcmd(nchar);
4768#endif
4769 if (nchar == K_DEL || nchar == K_KDEL)
4770 n /= 10;
4771 else if (VIM_ISDIGIT(nchar))
4772 n = n * 10 + (nchar - '0');
4773 else if (nchar == CAR)
4774 {
4775#ifdef FEAT_GUI
4776 need_mouse_correct = TRUE;
4777#endif
4778 win_setheight((int)n);
4779 break;
4780 }
4781 else if (nchar == 'l'
4782 || nchar == 'h'
4783 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004784 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 cap->count1 = n ? n * cap->count1 : cap->count1;
4787 goto dozet;
4788 }
4789 else
4790 {
4791 clearopbeep(cap->oap);
4792 break;
4793 }
4794 }
4795 cap->oap->op_type = OP_NOP;
4796 return;
4797 }
4798
4799dozet:
4800 if (
4801#ifdef FEAT_FOLDING
4802 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4803 * and "zC" only in Visual mode. "zj" and "zk" are motion
4804 * commands. */
4805 cap->nchar != 'f' && cap->nchar != 'F'
4806 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4807 && cap->nchar != 'j' && cap->nchar != 'k'
4808 &&
4809#endif
4810 checkclearop(cap->oap))
4811 return;
4812
4813 /*
4814 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4815 * If line number given, set cursor.
4816 */
4817 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4818 && cap->count0
4819 && cap->count0 != curwin->w_cursor.lnum)
4820 {
4821 setpcmark();
4822 if (cap->count0 > curbuf->b_ml.ml_line_count)
4823 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4824 else
4825 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004826 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828
4829 switch (nchar)
4830 {
4831 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4832 case '+':
4833 if (cap->count0 == 0)
4834 {
4835 /* No count given: put cursor at the line below screen */
4836 validate_botline(); /* make sure w_botline is valid */
4837 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4838 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4839 else
4840 curwin->w_cursor.lnum = curwin->w_botline;
4841 }
4842 /* FALLTHROUGH */
4843 case NL:
4844 case CAR:
4845 case K_KENTER:
4846 beginline(BL_WHITE | BL_FIX);
4847 /* FALLTHROUGH */
4848
4849 case 't': scroll_cursor_top(0, TRUE);
4850 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004851 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 break;
4853
4854 /* "z." and "zz": put cursor in middle of screen */
4855 case '.': beginline(BL_WHITE | BL_FIX);
4856 /* FALLTHROUGH */
4857
4858 case 'z': scroll_cursor_halfway(TRUE);
4859 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004860 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 break;
4862
4863 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4864 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4865 * when <count> is at bottom of window, and puts that one at
4866 * bottom of window. */
4867 if (cap->count0 != 0)
4868 {
4869 scroll_cursor_bot(0, TRUE);
4870 curwin->w_cursor.lnum = curwin->w_topline;
4871 }
4872 else if (curwin->w_topline == 1)
4873 curwin->w_cursor.lnum = 1;
4874 else
4875 curwin->w_cursor.lnum = curwin->w_topline - 1;
4876 /* FALLTHROUGH */
4877 case '-':
4878 beginline(BL_WHITE | BL_FIX);
4879 /* FALLTHROUGH */
4880
4881 case 'b': scroll_cursor_bot(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 /* "zH" - scroll screen right half-page */
4887 case 'H':
4888 cap->count1 *= W_WIDTH(curwin) / 2;
4889 /* FALLTHROUGH */
4890
4891 /* "zh" - scroll screen to the right */
4892 case 'h':
4893 case K_LEFT:
4894 if (!curwin->w_p_wrap)
4895 {
4896 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4897 curwin->w_leftcol = 0;
4898 else
4899 curwin->w_leftcol -= (colnr_T)cap->count1;
4900 leftcol_changed();
4901 }
4902 break;
4903
4904 /* "zL" - scroll screen left half-page */
4905 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4906 /* FALLTHROUGH */
4907
4908 /* "zl" - scroll screen to the left */
4909 case 'l':
4910 case K_RIGHT:
4911 if (!curwin->w_p_wrap)
4912 {
4913 /* scroll the window left */
4914 curwin->w_leftcol += (colnr_T)cap->count1;
4915 leftcol_changed();
4916 }
4917 break;
4918
4919 /* "zs" - scroll screen, cursor at the start */
4920 case 's': if (!curwin->w_p_wrap)
4921 {
4922#ifdef FEAT_FOLDING
4923 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4924 col = 0; /* like the cursor is in col 0 */
4925 else
4926#endif
4927 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4928 if ((long)col > p_siso)
4929 col -= p_siso;
4930 else
4931 col = 0;
4932 if (curwin->w_leftcol != col)
4933 {
4934 curwin->w_leftcol = col;
4935 redraw_later(NOT_VALID);
4936 }
4937 }
4938 break;
4939
4940 /* "ze" - scroll screen, cursor at the end */
4941 case 'e': if (!curwin->w_p_wrap)
4942 {
4943#ifdef FEAT_FOLDING
4944 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4945 col = 0; /* like the cursor is in col 0 */
4946 else
4947#endif
4948 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4949 n = W_WIDTH(curwin) - curwin_col_off();
4950 if ((long)col + p_siso < n)
4951 col = 0;
4952 else
4953 col = col + p_siso - n + 1;
4954 if (curwin->w_leftcol != col)
4955 {
4956 curwin->w_leftcol = col;
4957 redraw_later(NOT_VALID);
4958 }
4959 }
4960 break;
4961
4962#ifdef FEAT_FOLDING
4963 /* "zF": create fold command */
4964 /* "zf": create fold operator */
4965 case 'F':
4966 case 'f': if (foldManualAllowed(TRUE))
4967 {
4968 cap->nchar = 'f';
4969 nv_operator(cap);
4970 curwin->w_p_fen = TRUE;
4971
4972 /* "zF" is like "zfzf" */
4973 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4974 {
4975 nv_operator(cap);
4976 finish_op = TRUE;
4977 }
4978 }
4979 else
4980 clearopbeep(cap->oap);
4981 break;
4982
4983 /* "zd": delete fold at cursor */
4984 /* "zD": delete fold at cursor recursively */
4985 case 'd':
4986 case 'D': if (foldManualAllowed(FALSE))
4987 {
4988 if (VIsual_active)
4989 nv_operator(cap);
4990 else
4991 deleteFold(curwin->w_cursor.lnum,
4992 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4993 }
4994 break;
4995
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004996 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 case 'E': if (foldmethodIsManual(curwin))
4998 {
4999 clearFolding(curwin);
5000 changed_window_setting();
5001 }
5002 else if (foldmethodIsMarker(curwin))
5003 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5004 TRUE, FALSE);
5005 else
5006 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5007 break;
5008
5009 /* "zn": fold none: reset 'foldenable' */
5010 case 'n': curwin->w_p_fen = FALSE;
5011 break;
5012
5013 /* "zN": fold Normal: set 'foldenable' */
5014 case 'N': curwin->w_p_fen = TRUE;
5015 break;
5016
5017 /* "zi": invert folding: toggle 'foldenable' */
5018 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5019 break;
5020
5021 /* "za": open closed fold or close open fold at cursor */
5022 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5023 openFold(curwin->w_cursor.lnum, cap->count1);
5024 else
5025 {
5026 closeFold(curwin->w_cursor.lnum, cap->count1);
5027 curwin->w_p_fen = TRUE;
5028 }
5029 break;
5030
5031 /* "zA": open fold at cursor recursively */
5032 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5033 openFoldRecurse(curwin->w_cursor.lnum);
5034 else
5035 {
5036 closeFoldRecurse(curwin->w_cursor.lnum);
5037 curwin->w_p_fen = TRUE;
5038 }
5039 break;
5040
5041 /* "zo": open fold at cursor or Visual area */
5042 case 'o': if (VIsual_active)
5043 nv_operator(cap);
5044 else
5045 openFold(curwin->w_cursor.lnum, cap->count1);
5046 break;
5047
5048 /* "zO": open fold recursively */
5049 case 'O': if (VIsual_active)
5050 nv_operator(cap);
5051 else
5052 openFoldRecurse(curwin->w_cursor.lnum);
5053 break;
5054
5055 /* "zc": close fold at cursor or Visual area */
5056 case 'c': if (VIsual_active)
5057 nv_operator(cap);
5058 else
5059 closeFold(curwin->w_cursor.lnum, cap->count1);
5060 curwin->w_p_fen = TRUE;
5061 break;
5062
5063 /* "zC": close fold recursively */
5064 case 'C': if (VIsual_active)
5065 nv_operator(cap);
5066 else
5067 closeFoldRecurse(curwin->w_cursor.lnum);
5068 curwin->w_p_fen = TRUE;
5069 break;
5070
5071 /* "zv": open folds at the cursor */
5072 case 'v': foldOpenCursor();
5073 break;
5074
5075 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5076 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005077 curwin->w_foldinvalid = TRUE; /* recompute folds */
5078 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 foldOpenCursor();
5080 break;
5081
5082 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5083 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005084 curwin->w_foldinvalid = TRUE; /* recompute folds */
5085 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087
5088 /* "zm": fold more */
5089 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005090 {
5091 curwin->w_p_fdl -= cap->count1;
5092 if (curwin->w_p_fdl < 0)
5093 curwin->w_p_fdl = 0;
5094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 old_fdl = -1; /* force an update */
5096 curwin->w_p_fen = TRUE;
5097 break;
5098
5099 /* "zM": close all folds */
5100 case 'M': curwin->w_p_fdl = 0;
5101 old_fdl = -1; /* force an update */
5102 curwin->w_p_fen = TRUE;
5103 break;
5104
5105 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005106 case 'r': curwin->w_p_fdl += cap->count1;
5107 {
5108 int d = getDeepestNesting();
5109
5110 if (curwin->w_p_fdl >= d)
5111 curwin->w_p_fdl = d;
5112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 break;
5114
5115 /* "zR": open all folds */
5116 case 'R': curwin->w_p_fdl = getDeepestNesting();
5117 old_fdl = -1; /* force an update */
5118 break;
5119
5120 case 'j': /* "zj" move to next fold downwards */
5121 case 'k': /* "zk" move to next fold upwards */
5122 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5123 cap->count1) == FAIL)
5124 clearopbeep(cap->oap);
5125 break;
5126
5127#endif /* FEAT_FOLDING */
5128
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005129#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005130 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5131 ++no_mapping;
5132 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005133 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005134 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005135 --no_mapping;
5136 --allow_keys;
5137#ifdef FEAT_CMDL_INFO
5138 (void)add_to_showcmd(nchar);
5139#endif
5140 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5141 {
5142 clearopbeep(cap->oap);
5143 break;
5144 }
5145 undo = TRUE;
5146 /*FALLTHROUGH*/
5147
Bram Moolenaarb765d632005-06-07 21:00:02 +00005148 case 'g': /* "zg": add good word to word list */
5149 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005150 case 'G': /* "zG": add good word to temp word list */
5151 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005152 {
5153 char_u *ptr = NULL;
5154 int len;
5155
5156 if (checkclearop(cap->oap))
5157 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005158 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5159 == FAIL)
5160 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005161 if (ptr == NULL)
5162 {
5163 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005164
Bram Moolenaar134bf072013-09-25 18:54:24 +02005165 /* Find bad word under the cursor. When 'spell' is
5166 * off this fails and find_ident_under_cursor() is
5167 * used below. */
5168 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005169 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005170 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005171 if (len != 0 && curwin->w_cursor.col <= pos.col)
5172 ptr = ml_get_pos(&curwin->w_cursor);
5173 curwin->w_cursor = pos;
5174 }
5175
Bram Moolenaarb765d632005-06-07 21:00:02 +00005176 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5177 FIND_IDENT)) == 0)
5178 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005179 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005180 (nchar == 'G' || nchar == 'W')
5181 ? 0 : (int)cap->count1,
5182 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005183 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005184 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005185
Bram Moolenaar43abc522005-12-10 20:15:02 +00005186 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005187 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005188 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005189 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005190#endif
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 default: clearopbeep(cap->oap);
5193 }
5194
5195#ifdef FEAT_FOLDING
5196 /* Redraw when 'foldenable' changed */
5197 if (old_fen != curwin->w_p_fen)
5198 {
5199# ifdef FEAT_DIFF
5200 win_T *wp;
5201
5202 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5203 {
5204 /* Adjust 'foldenable' in diff-synced windows. */
5205 FOR_ALL_WINDOWS(wp)
5206 {
5207 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5208 {
5209 wp->w_p_fen = curwin->w_p_fen;
5210 changed_window_setting_win(wp);
5211 }
5212 }
5213 }
5214# endif
5215 changed_window_setting();
5216 }
5217
5218 /* Redraw when 'foldlevel' changed. */
5219 if (old_fdl != curwin->w_p_fdl)
5220 newFoldLevel();
5221#endif
5222}
5223
5224#ifdef FEAT_GUI
5225/*
5226 * Vertical scrollbar movement.
5227 */
5228 static void
5229nv_ver_scrollbar(cap)
5230 cmdarg_T *cap;
5231{
5232 if (cap->oap->op_type != OP_NOP)
5233 clearopbeep(cap->oap);
5234
5235 /* Even if an operator was pending, we still want to scroll */
5236 gui_do_scroll();
5237}
5238
5239/*
5240 * Horizontal scrollbar movement.
5241 */
5242 static void
5243nv_hor_scrollbar(cap)
5244 cmdarg_T *cap;
5245{
5246 if (cap->oap->op_type != OP_NOP)
5247 clearopbeep(cap->oap);
5248
5249 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005250 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251}
5252#endif
5253
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005254#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005255/*
5256 * Click in GUI tab.
5257 */
5258 static void
5259nv_tabline(cap)
5260 cmdarg_T *cap;
5261{
5262 if (cap->oap->op_type != OP_NOP)
5263 clearopbeep(cap->oap);
5264
5265 /* Even if an operator was pending, we still want to jump tabs. */
5266 goto_tabpage(current_tab);
5267}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005268
5269/*
5270 * Selected item in tab line menu.
5271 */
5272 static void
5273nv_tabmenu(cap)
5274 cmdarg_T *cap;
5275{
5276 if (cap->oap->op_type != OP_NOP)
5277 clearopbeep(cap->oap);
5278
5279 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005280 handle_tabmenu();
5281}
5282
5283/*
5284 * Handle selecting an item of the GUI tab line menu.
5285 * Used in Normal and Insert mode.
5286 */
5287 void
5288handle_tabmenu()
5289{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005290 switch (current_tabmenu)
5291 {
5292 case TABLINE_MENU_CLOSE:
5293 if (current_tab == 0)
5294 do_cmdline_cmd((char_u *)"tabclose");
5295 else
5296 {
5297 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5298 current_tab);
5299 do_cmdline_cmd(IObuff);
5300 }
5301 break;
5302
5303 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005304 if (current_tab == 0)
5305 do_cmdline_cmd((char_u *)"$tabnew");
5306 else
5307 {
5308 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5309 current_tab - 1);
5310 do_cmdline_cmd(IObuff);
5311 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005312 break;
5313
5314 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005315 if (current_tab == 0)
5316 do_cmdline_cmd((char_u *)"browse $tabnew");
5317 else
5318 {
5319 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5320 current_tab - 1);
5321 do_cmdline_cmd(IObuff);
5322 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005323 break;
5324 }
5325}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328/*
5329 * "Q" command.
5330 */
5331 static void
5332nv_exmode(cap)
5333 cmdarg_T *cap;
5334{
5335 /*
5336 * Ignore 'Q' in Visual mode, just give a beep.
5337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005339 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005340 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 do_exmode(FALSE);
5342}
5343
5344/*
5345 * Handle a ":" command.
5346 */
5347 static void
5348nv_colon(cap)
5349 cmdarg_T *cap;
5350{
5351 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005352 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (VIsual_active)
5355 nv_operator(cap);
5356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 {
5358 if (cap->oap->op_type != OP_NOP)
5359 {
5360 /* Using ":" as a movement is characterwise exclusive. */
5361 cap->oap->motion_type = MCHAR;
5362 cap->oap->inclusive = FALSE;
5363 }
5364 else if (cap->count0)
5365 {
5366 /* translate "count:" into ":.,.+(count - 1)" */
5367 stuffcharReadbuff('.');
5368 if (cap->count0 > 1)
5369 {
5370 stuffReadbuff((char_u *)",.+");
5371 stuffnumReadbuff((long)cap->count0 - 1L);
5372 }
5373 }
5374
5375 /* When typing, don't type below an old message */
5376 if (KeyTyped)
5377 compute_cmdrow();
5378
5379 old_p_im = p_im;
5380
5381 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005382 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5384
5385 /* If 'insertmode' changed, enter or exit Insert mode */
5386 if (p_im != old_p_im)
5387 {
5388 if (p_im)
5389 restart_edit = 'i';
5390 else
5391 restart_edit = 0;
5392 }
5393
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005394 if (cmd_result == FAIL)
5395 /* The Ex command failed, do not execute the operator. */
5396 clearop(cap->oap);
5397 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5399 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005400 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5401 || did_emsg
5402 ))
5403 /* The start of the operator has become invalid by the Ex command.
5404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 clearopbeep(cap->oap);
5406 }
5407}
5408
5409/*
5410 * Handle CTRL-G command.
5411 */
5412 static void
5413nv_ctrlg(cap)
5414 cmdarg_T *cap;
5415{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 if (VIsual_active) /* toggle Selection/Visual mode */
5417 {
5418 VIsual_select = !VIsual_select;
5419 showmode();
5420 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005421 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 /* print full name if count given or :cd used */
5423 fileinfo((int)cap->count0, FALSE, TRUE);
5424}
5425
5426/*
5427 * Handle CTRL-H <Backspace> command.
5428 */
5429 static void
5430nv_ctrlh(cap)
5431 cmdarg_T *cap;
5432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (VIsual_active && VIsual_select)
5434 {
5435 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5436 v_visop(cap);
5437 }
5438 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 nv_left(cap);
5440}
5441
5442/*
5443 * CTRL-L: clear screen and redraw.
5444 */
5445 static void
5446nv_clear(cap)
5447 cmdarg_T *cap;
5448{
5449 if (!checkclearop(cap->oap))
5450 {
5451#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5452 /*
5453 * Right now, the BeBox doesn't seem to have an easy way to detect
5454 * window resizing, so we cheat and make the user detect it
5455 * manually with CTRL-L instead
5456 */
5457 ui_get_shellsize();
5458#endif
5459#ifdef FEAT_SYN_HL
5460 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005461 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#endif
5463 redraw_later(CLEAR);
5464 }
5465}
5466
5467/*
5468 * CTRL-O: In Select mode: switch to Visual mode for one command.
5469 * Otherwise: Go to older pcmark.
5470 */
5471 static void
5472nv_ctrlo(cap)
5473 cmdarg_T *cap;
5474{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (VIsual_active && VIsual_select)
5476 {
5477 VIsual_select = FALSE;
5478 showmode();
5479 restart_VIsual_select = 2; /* restart Select mode later */
5480 }
5481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 cap->count1 = -cap->count1;
5484 nv_pcmark(cap);
5485 }
5486}
5487
5488/*
5489 * CTRL-^ command, short for ":e #"
5490 */
5491 static void
5492nv_hat(cap)
5493 cmdarg_T *cap;
5494{
5495 if (!checkclearopq(cap->oap))
5496 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5497 GETF_SETMARK|GETF_ALT, FALSE);
5498}
5499
5500/*
5501 * "Z" commands.
5502 */
5503 static void
5504nv_Zet(cap)
5505 cmdarg_T *cap;
5506{
5507 if (!checkclearopq(cap->oap))
5508 {
5509 switch (cap->nchar)
5510 {
5511 /* "ZZ": equivalent to ":x". */
5512 case 'Z': do_cmdline_cmd((char_u *)"x");
5513 break;
5514
5515 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5516 case 'Q': do_cmdline_cmd((char_u *)"q!");
5517 break;
5518
5519 default: clearopbeep(cap->oap);
5520 }
5521 }
5522}
5523
5524#if defined(FEAT_WINDOWS) || defined(PROTO)
5525/*
5526 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5527 */
5528 void
5529do_nv_ident(c1, c2)
5530 int c1;
5531 int c2;
5532{
5533 oparg_T oa;
5534 cmdarg_T ca;
5535
5536 clear_oparg(&oa);
5537 vim_memset(&ca, 0, sizeof(ca));
5538 ca.oap = &oa;
5539 ca.cmdchar = c1;
5540 ca.nchar = c2;
5541 nv_ident(&ca);
5542}
5543#endif
5544
5545/*
5546 * Handle the commands that use the word under the cursor.
5547 * [g] CTRL-] :ta to current identifier
5548 * [g] 'K' run program for current identifier
5549 * [g] '*' / to current identifier or string
5550 * [g] '#' ? to current identifier or string
5551 * g ']' :tselect for current identifier
5552 */
5553 static void
5554nv_ident(cap)
5555 cmdarg_T *cap;
5556{
5557 char_u *ptr = NULL;
5558 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005559 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 char_u *p;
5561 char_u *kp; /* value of 'keywordprg' */
5562 int kp_help; /* 'keywordprg' is ":help" */
5563 int n = 0; /* init for GCC */
5564 int cmdchar;
5565 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005566 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 char_u *aux_ptr;
5568 int isman;
5569 int isman_s;
5570
5571 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5572 {
5573 cmdchar = cap->nchar;
5574 g_cmd = TRUE;
5575 }
5576 else
5577 {
5578 cmdchar = cap->cmdchar;
5579 g_cmd = FALSE;
5580 }
5581
5582 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5583 cmdchar = '#';
5584
5585 /*
5586 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5587 */
5588 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5591 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 if (checkclearopq(cap->oap))
5593 return;
5594 }
5595
5596 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5597 (cmdchar == '*' || cmdchar == '#')
5598 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5599 {
5600 clearop(cap->oap);
5601 return;
5602 }
5603
5604 /* Allocate buffer to put the command in. Inserting backslashes can
5605 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5606 * and some numbers. */
5607 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5608 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5609 || STRCMP(kp, ":help") == 0);
5610 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5611 if (buf == NULL)
5612 return;
5613 buf[0] = NUL;
5614
5615 switch (cmdchar)
5616 {
5617 case '*':
5618 case '#':
5619 /*
5620 * Put cursor at start of word, makes search skip the word
5621 * under the cursor.
5622 * Call setpcmark() first, so "*``" puts the cursor back where
5623 * it was.
5624 */
5625 setpcmark();
5626 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5627
5628 if (!g_cmd && vim_iswordp(ptr))
5629 STRCPY(buf, "\\<");
5630 no_smartcase = TRUE; /* don't use 'smartcase' now */
5631 break;
5632
5633 case 'K':
5634 if (kp_help)
5635 STRCPY(buf, "he! ");
5636 else
5637 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005638 /* An external command will probably use an argument starting
5639 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005640 while (*ptr == '-' && n > 0)
5641 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005642 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005643 --n;
5644 }
5645 if (n == 0)
5646 {
5647 EMSG(_(e_noident)); /* found dashes only */
5648 vim_free(buf);
5649 return;
5650 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 /* When a count is given, turn it into a range. Is this
5653 * really what we want? */
5654 isman = (STRCMP(kp, "man") == 0);
5655 isman_s = (STRCMP(kp, "man -s") == 0);
5656 if (cap->count0 != 0 && !(isman || isman_s))
5657 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5658
5659 STRCAT(buf, "! ");
5660 if (cap->count0 == 0 && isman_s)
5661 STRCAT(buf, "man");
5662 else
5663 STRCAT(buf, kp);
5664 STRCAT(buf, " ");
5665 if (cap->count0 != 0 && (isman || isman_s))
5666 {
5667 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5668 STRCAT(buf, " ");
5669 }
5670 }
5671 break;
5672
5673 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005674 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#ifdef FEAT_CSCOPE
5676 if (p_cst)
5677 STRCPY(buf, "cstag ");
5678 else
5679#endif
5680 STRCPY(buf, "ts ");
5681 break;
5682
5683 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005684 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (curbuf->b_help)
5686 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005688 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005689 if (g_cmd)
5690 STRCPY(buf, "tj ");
5691 else
5692 sprintf((char *)buf, "%ldta ", cap->count0);
5693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695
5696 /*
5697 * Now grab the chars in the identifier
5698 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005699 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005701 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005702 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005703 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005704 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005705 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005707 vim_free(buf);
5708 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005710 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5711 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005712 {
5713 vim_free(buf);
5714 vim_free(p);
5715 return;
5716 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005717 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005718 STRCAT(buf, p);
5719 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005721 else
5722 {
5723 if (cmdchar == '*')
5724 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5725 else if (cmdchar == '#')
5726 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005727 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005728 {
5729 if (curbuf->b_help)
5730 /* ":help" handles unescaped argument */
5731 aux_ptr = (char_u *)"";
5732 else
5733 aux_ptr = (char_u *)"\\|\"\n[";
5734 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005735 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005736 aux_ptr = (char_u *)"\\|\"\n*?[";
5737
5738 p = buf + STRLEN(buf);
5739 while (n-- > 0)
5740 {
5741 /* put a backslash before \ and some others */
5742 if (vim_strchr(aux_ptr, *ptr) != NULL)
5743 *p++ = '\\';
5744#ifdef FEAT_MBYTE
5745 /* When current byte is a part of multibyte character, copy all
5746 * bytes of that character. */
5747 if (has_mbyte)
5748 {
5749 int i;
5750 int len = (*mb_ptr2len)(ptr) - 1;
5751
5752 for (i = 0; i < len && n >= 1; ++i, --n)
5753 *p++ = *ptr++;
5754 }
5755#endif
5756 *p++ = *ptr++;
5757 }
5758 *p = NUL;
5759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 /*
5762 * Execute the command.
5763 */
5764 if (cmdchar == '*' || cmdchar == '#')
5765 {
5766 if (!g_cmd && (
5767#ifdef FEAT_MBYTE
5768 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5769#endif
5770 vim_iswordc(ptr[-1])))
5771 STRCAT(buf, "\\>");
5772#ifdef FEAT_CMDHIST
5773 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005774 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5776#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005777 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 else
5780 do_cmdline_cmd(buf);
5781
5782 vim_free(buf);
5783}
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785/*
5786 * Get visually selected text, within one line only.
5787 * Returns FAIL if more than one line selected.
5788 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005789 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790get_visual_text(cap, pp, lenp)
5791 cmdarg_T *cap;
5792 char_u **pp; /* return: start of selected text */
5793 int *lenp; /* return: length of selected text */
5794{
5795 if (VIsual_mode != 'V')
5796 unadjust_for_sel();
5797 if (VIsual.lnum != curwin->w_cursor.lnum)
5798 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005799 if (cap != NULL)
5800 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 return FAIL;
5802 }
5803 if (VIsual_mode == 'V')
5804 {
5805 *pp = ml_get_curline();
5806 *lenp = (int)STRLEN(*pp);
5807 }
5808 else
5809 {
5810 if (lt(curwin->w_cursor, VIsual))
5811 {
5812 *pp = ml_get_pos(&curwin->w_cursor);
5813 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5814 }
5815 else
5816 {
5817 *pp = ml_get_pos(&VIsual);
5818 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5819 }
5820#ifdef FEAT_MBYTE
5821 if (has_mbyte)
5822 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005823 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#endif
5825 }
5826 reset_VIsual_and_resel();
5827 return OK;
5828}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
5830/*
5831 * CTRL-T: backwards in tag stack
5832 */
5833 static void
5834nv_tagpop(cap)
5835 cmdarg_T *cap;
5836{
5837 if (!checkclearopq(cap->oap))
5838 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5839}
5840
5841/*
5842 * Handle scrolling command 'H', 'L' and 'M'.
5843 */
5844 static void
5845nv_scroll(cap)
5846 cmdarg_T *cap;
5847{
5848 int used = 0;
5849 long n;
5850#ifdef FEAT_FOLDING
5851 linenr_T lnum;
5852#endif
5853 int half;
5854
5855 cap->oap->motion_type = MLINE;
5856 setpcmark();
5857
5858 if (cap->cmdchar == 'L')
5859 {
5860 validate_botline(); /* make sure curwin->w_botline is valid */
5861 curwin->w_cursor.lnum = curwin->w_botline - 1;
5862 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5863 curwin->w_cursor.lnum = 1;
5864 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005865 {
5866#ifdef FEAT_FOLDING
5867 if (hasAnyFolding(curwin))
5868 {
5869 /* Count a fold for one screen line. */
5870 for (n = cap->count1 - 1; n > 0
5871 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5872 {
5873 (void)hasFolding(curwin->w_cursor.lnum,
5874 &curwin->w_cursor.lnum, NULL);
5875 --curwin->w_cursor.lnum;
5876 }
5877 }
5878 else
5879#endif
5880 curwin->w_cursor.lnum -= cap->count1 - 1;
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 else
5884 {
5885 if (cap->cmdchar == 'M')
5886 {
5887#ifdef FEAT_DIFF
5888 /* Don't count filler lines above the window. */
5889 used -= diff_check_fill(curwin, curwin->w_topline)
5890 - curwin->w_topfill;
5891#endif
5892 validate_botline(); /* make sure w_empty_rows is valid */
5893 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5894 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5895 {
5896#ifdef FEAT_DIFF
5897 /* Count half he number of filler lines to be "below this
5898 * line" and half to be "above the next line". */
5899 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5900 + n) / 2 >= half)
5901 {
5902 --n;
5903 break;
5904 }
5905#endif
5906 used += plines(curwin->w_topline + n);
5907 if (used >= half)
5908 break;
5909#ifdef FEAT_FOLDING
5910 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5911 n = lnum - curwin->w_topline;
5912#endif
5913 }
5914 if (n > 0 && used > curwin->w_height)
5915 --n;
5916 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005917 else /* (cap->cmdchar == 'H') */
5918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005920#ifdef FEAT_FOLDING
5921 if (hasAnyFolding(curwin))
5922 {
5923 /* Count a fold for one screen line. */
5924 lnum = curwin->w_topline;
5925 while (n-- > 0 && lnum < curwin->w_botline - 1)
5926 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005927 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005928 ++lnum;
5929 }
5930 n = lnum - curwin->w_topline;
5931 }
5932#endif
5933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 curwin->w_cursor.lnum = curwin->w_topline + n;
5935 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5936 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5937 }
5938
5939 cursor_correct(); /* correct for 'so' */
5940 beginline(BL_SOL | BL_FIX);
5941}
5942
5943/*
5944 * Cursor right commands.
5945 */
5946 static void
5947nv_right(cap)
5948 cmdarg_T *cap;
5949{
5950 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005951 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005953 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5954 {
5955 /* <C-Right> and <S-Right> move a word or WORD right */
5956 if (mod_mask & MOD_MASK_CTRL)
5957 cap->arg = TRUE;
5958 nv_wordcmd(cap);
5959 return;
5960 }
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 cap->oap->motion_type = MCHAR;
5963 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005964 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005966#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005968 * In virtual edit mode, there's no such thing as "past_line", as lines
5969 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 */
5971 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005972 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#endif
5974
5975 for (n = cap->count1; n > 0; --n)
5976 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005977 if ((!past_line && oneright() == FAIL)
5978 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005979 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
5981 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005982 * <Space> wraps to next line if 'whichwrap' has 's'.
5983 * 'l' wraps to next line if 'whichwrap' has 'l'.
5984 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 */
5986 if ( ((cap->cmdchar == ' '
5987 && vim_strchr(p_ww, 's') != NULL)
5988 || (cap->cmdchar == 'l'
5989 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005990 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 && vim_strchr(p_ww, '>') != NULL))
5992 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5993 {
5994 /* When deleting we also count the NL as a character.
5995 * Set cap->oap->inclusive when last char in the line is
5996 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005997 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 && !cap->oap->inclusive
5999 && !lineempty(curwin->w_cursor.lnum))
6000 cap->oap->inclusive = TRUE;
6001 else
6002 {
6003 ++curwin->w_cursor.lnum;
6004 curwin->w_cursor.col = 0;
6005#ifdef FEAT_VIRTUALEDIT
6006 curwin->w_cursor.coladd = 0;
6007#endif
6008 curwin->w_set_curswant = TRUE;
6009 cap->oap->inclusive = FALSE;
6010 }
6011 continue;
6012 }
6013 if (cap->oap->op_type == OP_NOP)
6014 {
6015 /* Only beep and flush if not moved at all */
6016 if (n == cap->count1)
6017 beep_flush();
6018 }
6019 else
6020 {
6021 if (!lineempty(curwin->w_cursor.lnum))
6022 cap->oap->inclusive = TRUE;
6023 }
6024 break;
6025 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006026 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
6028 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006029#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 if (virtual_active())
6031 oneright();
6032 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006035#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 if (has_mbyte)
6037 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006038 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 ++curwin->w_cursor.col;
6042 }
6043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045#ifdef FEAT_FOLDING
6046 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6047 && cap->oap->op_type == OP_NOP)
6048 foldOpenCursor();
6049#endif
6050}
6051
6052/*
6053 * Cursor left commands.
6054 *
6055 * Returns TRUE when operator end should not be adjusted.
6056 */
6057 static void
6058nv_left(cap)
6059 cmdarg_T *cap;
6060{
6061 long n;
6062
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006063 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6064 {
6065 /* <C-Left> and <S-Left> move a word or WORD left */
6066 if (mod_mask & MOD_MASK_CTRL)
6067 cap->arg = 1;
6068 nv_bck_word(cap);
6069 return;
6070 }
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 cap->oap->motion_type = MCHAR;
6073 cap->oap->inclusive = FALSE;
6074 for (n = cap->count1; n > 0; --n)
6075 {
6076 if (oneleft() == FAIL)
6077 {
6078 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6079 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6080 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6081 */
6082 if ( (((cap->cmdchar == K_BS
6083 || cap->cmdchar == Ctrl_H)
6084 && vim_strchr(p_ww, 'b') != NULL)
6085 || (cap->cmdchar == 'h'
6086 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006087 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 && vim_strchr(p_ww, '<') != NULL))
6089 && curwin->w_cursor.lnum > 1)
6090 {
6091 --(curwin->w_cursor.lnum);
6092 coladvance((colnr_T)MAXCOL);
6093 curwin->w_set_curswant = TRUE;
6094
6095 /* When the NL before the first char has to be deleted we
6096 * put the cursor on the NUL after the previous line.
6097 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006098 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if ( (cap->oap->op_type == OP_DELETE
6100 || cap->oap->op_type == OP_CHANGE)
6101 && !lineempty(curwin->w_cursor.lnum))
6102 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006103 char_u *cp = ml_get_cursor();
6104
6105 if (*cp != NUL)
6106 {
6107#ifdef FEAT_MBYTE
6108 if (has_mbyte)
6109 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6110 else
6111#endif
6112 ++curwin->w_cursor.col;
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 cap->retval |= CA_NO_ADJ_OP_END;
6115 }
6116 continue;
6117 }
6118 /* Only beep and flush if not moved at all */
6119 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6120 beep_flush();
6121 break;
6122 }
6123 }
6124#ifdef FEAT_FOLDING
6125 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6126 && cap->oap->op_type == OP_NOP)
6127 foldOpenCursor();
6128#endif
6129}
6130
6131/*
6132 * Cursor up commands.
6133 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6134 */
6135 static void
6136nv_up(cap)
6137 cmdarg_T *cap;
6138{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006139 if (mod_mask & MOD_MASK_SHIFT)
6140 {
6141 /* <S-Up> is page up */
6142 cap->arg = BACKWARD;
6143 nv_page(cap);
6144 }
6145 else
6146 {
6147 cap->oap->motion_type = MLINE;
6148 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6149 clearopbeep(cap->oap);
6150 else if (cap->arg)
6151 beginline(BL_WHITE | BL_FIX);
6152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153}
6154
6155/*
6156 * Cursor down commands.
6157 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6158 */
6159 static void
6160nv_down(cap)
6161 cmdarg_T *cap;
6162{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006163 if (mod_mask & MOD_MASK_SHIFT)
6164 {
6165 /* <S-Down> is page down */
6166 cap->arg = FORWARD;
6167 nv_page(cap);
6168 }
6169 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6171 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006172 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006173 if (curwin->w_llist_ref == NULL)
6174 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6175 else
6176 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 else
6178#endif
6179 {
6180#ifdef FEAT_CMDWIN
6181 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006182 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 cmdwin_result = CAR;
6184 else
6185#endif
6186 {
6187 cap->oap->motion_type = MLINE;
6188 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6189 clearopbeep(cap->oap);
6190 else if (cap->arg)
6191 beginline(BL_WHITE | BL_FIX);
6192 }
6193 }
6194}
6195
6196#ifdef FEAT_SEARCHPATH
6197/*
6198 * Grab the file name under the cursor and edit it.
6199 */
6200 static void
6201nv_gotofile(cap)
6202 cmdarg_T *cap;
6203{
6204 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006205 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006207 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006210 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 return;
6212 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006213#ifdef FEAT_AUTOCMD
6214 if (curbuf_locked())
6215 {
6216 clearop(cap->oap);
6217 return;
6218 }
6219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006221 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 if (ptr != NULL)
6224 {
6225 /* do autowrite if necessary */
6226 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006227 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 setpcmark();
6229 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006230 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006231 if (cap->nchar == 'F' && lnum >= 0)
6232 {
6233 curwin->w_cursor.lnum = lnum;
6234 check_cursor_lnum();
6235 beginline(BL_SOL | BL_FIX);
6236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 vim_free(ptr);
6238 }
6239 else
6240 clearop(cap->oap);
6241}
6242#endif
6243
6244/*
6245 * <End> command: to end of current line or last line.
6246 */
6247 static void
6248nv_end(cap)
6249 cmdarg_T *cap;
6250{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006251 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006253 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 nv_goto(cap);
6255 cap->count1 = 1; /* to end of current line */
6256 }
6257 nv_dollar(cap);
6258}
6259
6260/*
6261 * Handle the "$" command.
6262 */
6263 static void
6264nv_dollar(cap)
6265 cmdarg_T *cap;
6266{
6267 cap->oap->motion_type = MCHAR;
6268 cap->oap->inclusive = TRUE;
6269#ifdef FEAT_VIRTUALEDIT
6270 /* In virtual mode when off the edge of a line and an operator
6271 * is pending (whew!) keep the cursor where it is.
6272 * Otherwise, send it to the end of the line. */
6273 if (!virtual_active() || gchar_cursor() != NUL
6274 || cap->oap->op_type == OP_NOP)
6275#endif
6276 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6277 if (cursor_down((long)(cap->count1 - 1),
6278 cap->oap->op_type == OP_NOP) == FAIL)
6279 clearopbeep(cap->oap);
6280#ifdef FEAT_FOLDING
6281 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6282 foldOpenCursor();
6283#endif
6284}
6285
6286/*
6287 * Implementation of '?' and '/' commands.
6288 * If cap->arg is TRUE don't set PC mark.
6289 */
6290 static void
6291nv_search(cap)
6292 cmdarg_T *cap;
6293{
6294 oparg_T *oap = cap->oap;
6295
6296 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6297 {
6298 /* Translate "g??" to "g?g?" */
6299 cap->cmdchar = 'g';
6300 cap->nchar = '?';
6301 nv_operator(cap);
6302 return;
6303 }
6304
6305 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6306
6307 if (cap->searchbuf == NULL)
6308 {
6309 clearop(oap);
6310 return;
6311 }
6312
Bram Moolenaar46539112015-02-17 15:43:57 +01006313 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 (cap->arg ? 0 : SEARCH_MARK));
6315}
6316
6317/*
6318 * Handle "N" and "n" commands.
6319 * cap->arg is SEARCH_REV for "N", 0 for "n".
6320 */
6321 static void
6322nv_next(cap)
6323 cmdarg_T *cap;
6324{
Bram Moolenaar46539112015-02-17 15:43:57 +01006325 pos_T old = curwin->w_cursor;
6326 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6327
6328 if (i == 1 && equalpos(old, curwin->w_cursor))
6329 {
6330 /* Avoid getting stuck on the current cursor position, which can
6331 * happen when an offset is given and the cursor is on the last char
6332 * in the buffer: Repeat with count + 1. */
6333 cap->count1 += 1;
6334 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6335 cap->count1 -= 1;
6336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339/*
6340 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6341 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006342 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006344 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345normal_search(cap, dir, pat, opt)
6346 cmdarg_T *cap;
6347 int dir;
6348 char_u *pat;
6349 int opt; /* extra flags for do_search() */
6350{
6351 int i;
6352
6353 cap->oap->motion_type = MCHAR;
6354 cap->oap->inclusive = FALSE;
6355 cap->oap->use_reg_one = TRUE;
6356 curwin->w_set_curswant = TRUE;
6357
6358 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006359 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 if (i == 0)
6361 clearop(cap->oap);
6362 else
6363 {
6364 if (i == 2)
6365 cap->oap->motion_type = MLINE;
6366#ifdef FEAT_VIRTUALEDIT
6367 curwin->w_cursor.coladd = 0;
6368#endif
6369#ifdef FEAT_FOLDING
6370 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6371 foldOpenCursor();
6372#endif
6373 }
6374
6375 /* "/$" will put the cursor after the end of the line, may need to
6376 * correct that here */
6377 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006378 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379}
6380
6381/*
6382 * Character search commands.
6383 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6384 * ',' and FALSE for ';'.
6385 * cap->nchar is NUL for ',' and ';' (repeat the search)
6386 */
6387 static void
6388nv_csearch(cap)
6389 cmdarg_T *cap;
6390{
6391 int t_cmd;
6392
6393 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6394 t_cmd = TRUE;
6395 else
6396 t_cmd = FALSE;
6397
6398 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6400 clearopbeep(cap->oap);
6401 else
6402 {
6403 curwin->w_set_curswant = TRUE;
6404#ifdef FEAT_VIRTUALEDIT
6405 /* Include a Tab for "tx" and for "dfx". */
6406 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6407 && (t_cmd || cap->oap->op_type != OP_NOP))
6408 {
6409 colnr_T scol, ecol;
6410
6411 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6412 curwin->w_cursor.coladd = ecol - scol;
6413 }
6414 else
6415 curwin->w_cursor.coladd = 0;
6416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418#ifdef FEAT_FOLDING
6419 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6420 foldOpenCursor();
6421#endif
6422 }
6423}
6424
6425/*
6426 * "[" and "]" commands.
6427 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6428 */
6429 static void
6430nv_brackets(cap)
6431 cmdarg_T *cap;
6432{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006433 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 pos_T prev_pos;
6435 pos_T *pos = NULL; /* init for GCC */
6436 pos_T old_pos; /* cursor position before command */
6437 int flag;
6438 long n;
6439 int findc;
6440 int c;
6441
6442 cap->oap->motion_type = MCHAR;
6443 cap->oap->inclusive = FALSE;
6444 old_pos = curwin->w_cursor;
6445#ifdef FEAT_VIRTUALEDIT
6446 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6447#endif
6448
6449#ifdef FEAT_SEARCHPATH
6450 /*
6451 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6452 */
6453 if (cap->nchar == 'f')
6454 nv_gotofile(cap);
6455 else
6456#endif
6457
6458#ifdef FEAT_FIND_ID
6459 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006460 * Find the occurrence(s) of the identifier or define under cursor
6461 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 *
6463 * search list jump
6464 * fwd bwd fwd bwd fwd bwd
6465 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6466 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6467 */
6468 if (vim_strchr((char_u *)
6469#ifdef EBCDIC
6470 "iI\005dD\067",
6471#else
6472 "iI\011dD\004",
6473#endif
6474 cap->nchar) != NULL)
6475 {
6476 char_u *ptr;
6477 int len;
6478
6479 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6480 clearop(cap->oap);
6481 else
6482 {
6483 find_pattern_in_path(ptr, 0, len, TRUE,
6484 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6485 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6486 cap->count1,
6487 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6488 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6489 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6490 (linenr_T)MAXLNUM);
6491 curwin->w_set_curswant = TRUE;
6492 }
6493 }
6494 else
6495#endif
6496
6497 /*
6498 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6499 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6500 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6501 * "[m" or "]m" search for prev/next start of (Java) method.
6502 * "[M" or "]M" search for prev/next end of (Java) method.
6503 */
6504 if ( (cap->cmdchar == '['
6505 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6506 || (cap->cmdchar == ']'
6507 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6508 {
6509 if (cap->nchar == '*')
6510 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 prev_pos.lnum = 0;
6512 if (cap->nchar == 'm' || cap->nchar == 'M')
6513 {
6514 if (cap->cmdchar == '[')
6515 findc = '{';
6516 else
6517 findc = '}';
6518 n = 9999;
6519 }
6520 else
6521 {
6522 findc = cap->nchar;
6523 n = cap->count1;
6524 }
6525 for ( ; n > 0; --n)
6526 {
6527 if ((pos = findmatchlimit(cap->oap, findc,
6528 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6529 {
6530 if (new_pos.lnum == 0) /* nothing found */
6531 {
6532 if (cap->nchar != 'm' && cap->nchar != 'M')
6533 clearopbeep(cap->oap);
6534 }
6535 else
6536 pos = &new_pos; /* use last one found */
6537 break;
6538 }
6539 prev_pos = new_pos;
6540 curwin->w_cursor = *pos;
6541 new_pos = *pos;
6542 }
6543 curwin->w_cursor = old_pos;
6544
6545 /*
6546 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6547 * brought us to the match for "[m" and "]M" when inside a method.
6548 * Try finding the '{' or '}' we want to be at.
6549 * Also repeat for the given count.
6550 */
6551 if (cap->nchar == 'm' || cap->nchar == 'M')
6552 {
6553 /* norm is TRUE for "]M" and "[m" */
6554 int norm = ((findc == '{') == (cap->nchar == 'm'));
6555
6556 n = cap->count1;
6557 /* found a match: we were inside a method */
6558 if (prev_pos.lnum != 0)
6559 {
6560 pos = &prev_pos;
6561 curwin->w_cursor = prev_pos;
6562 if (norm)
6563 --n;
6564 }
6565 else
6566 pos = NULL;
6567 while (n > 0)
6568 {
6569 for (;;)
6570 {
6571 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6572 {
6573 /* if not found anything, that's an error */
6574 if (pos == NULL)
6575 clearopbeep(cap->oap);
6576 n = 0;
6577 break;
6578 }
6579 c = gchar_cursor();
6580 if (c == '{' || c == '}')
6581 {
6582 /* Must have found end/start of class: use it.
6583 * Or found the place to be at. */
6584 if ((c == findc && norm) || (n == 1 && !norm))
6585 {
6586 new_pos = curwin->w_cursor;
6587 pos = &new_pos;
6588 n = 0;
6589 }
6590 /* if no match found at all, we started outside of the
6591 * class and we're inside now. Just go on. */
6592 else if (new_pos.lnum == 0)
6593 {
6594 new_pos = curwin->w_cursor;
6595 pos = &new_pos;
6596 }
6597 /* found start/end of other method: go to match */
6598 else if ((pos = findmatchlimit(cap->oap, findc,
6599 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6600 0)) == NULL)
6601 n = 0;
6602 else
6603 curwin->w_cursor = *pos;
6604 break;
6605 }
6606 }
6607 --n;
6608 }
6609 curwin->w_cursor = old_pos;
6610 if (pos == NULL && new_pos.lnum != 0)
6611 clearopbeep(cap->oap);
6612 }
6613 if (pos != NULL)
6614 {
6615 setpcmark();
6616 curwin->w_cursor = *pos;
6617 curwin->w_set_curswant = TRUE;
6618#ifdef FEAT_FOLDING
6619 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6620 && cap->oap->op_type == OP_NOP)
6621 foldOpenCursor();
6622#endif
6623 }
6624 }
6625
6626 /*
6627 * "[[", "[]", "]]" and "][": move to start or end of function
6628 */
6629 else if (cap->nchar == '[' || cap->nchar == ']')
6630 {
6631 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6632 flag = '{';
6633 else
6634 flag = '}'; /* "][" or "[]" */
6635
6636 curwin->w_set_curswant = TRUE;
6637 /*
6638 * Imitate strange Vi behaviour: When using "]]" with an operator
6639 * we also stop at '}'.
6640 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006641 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 (cap->oap->op_type != OP_NOP
6643 && cap->arg == FORWARD && flag == '{')))
6644 clearopbeep(cap->oap);
6645 else
6646 {
6647 if (cap->oap->op_type == OP_NOP)
6648 beginline(BL_WHITE | BL_FIX);
6649#ifdef FEAT_FOLDING
6650 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6651 foldOpenCursor();
6652#endif
6653 }
6654 }
6655
6656 /*
6657 * "[p", "[P", "]P" and "]p": put with indent adjustment
6658 */
6659 else if (cap->nchar == 'p' || cap->nchar == 'P')
6660 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006661 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006663 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6664 ? FORWARD : BACKWARD;
6665 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006666 int was_visual = VIsual_active;
6667 int line_count = curbuf->b_ml.ml_line_count;
6668 pos_T start, end;
6669
6670 if (VIsual_active)
6671 {
6672 start = ltoreq(VIsual, curwin->w_cursor)
6673 ? VIsual : curwin->w_cursor;
6674 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6675 curwin->w_cursor = (dir == BACKWARD ? start : end);
6676 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006677# ifdef FEAT_CLIPBOARD
6678 adjust_clip_reg(&regname);
6679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006681
6682 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006683 if (was_visual)
6684 {
6685 VIsual = start;
6686 curwin->w_cursor = end;
6687 if (dir == BACKWARD)
6688 {
6689 /* adjust lines */
6690 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6691 curwin->w_cursor.lnum +=
6692 curbuf->b_ml.ml_line_count - line_count;
6693 }
6694
6695 VIsual_active = TRUE;
6696 if (VIsual_mode == 'V')
6697 {
6698 /* delete visually selected lines */
6699 cap->cmdchar = 'd';
6700 cap->nchar = NUL;
6701 cap->oap->regname = regname;
6702 nv_operator(cap);
6703 do_pending_operator(cap, 0, FALSE);
6704 }
6705 if (VIsual_active)
6706 {
6707 end_visual_mode();
6708 redraw_later(SOME_VALID);
6709 }
6710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712 }
6713
6714 /*
6715 * "['", "[`", "]'" and "]`": jump to next mark
6716 */
6717 else if (cap->nchar == '\'' || cap->nchar == '`')
6718 {
6719 pos = &curwin->w_cursor;
6720 for (n = cap->count1; n > 0; --n)
6721 {
6722 prev_pos = *pos;
6723 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6724 cap->nchar == '\'');
6725 if (pos == NULL)
6726 break;
6727 }
6728 if (pos == NULL)
6729 pos = &prev_pos;
6730 nv_cursormark(cap, cap->nchar == '\'', pos);
6731 }
6732
6733#ifdef FEAT_MOUSE
6734 /*
6735 * [ or ] followed by a middle mouse click: put selected text with
6736 * indent adjustment. Any other button just does as usual.
6737 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006738 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 {
6740 (void)do_mouse(cap->oap, cap->nchar,
6741 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6742 cap->count1, PUT_FIXINDENT);
6743 }
6744#endif /* FEAT_MOUSE */
6745
6746#ifdef FEAT_FOLDING
6747 /*
6748 * "[z" and "]z": move to start or end of open fold.
6749 */
6750 else if (cap->nchar == 'z')
6751 {
6752 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6753 cap->count1) == FAIL)
6754 clearopbeep(cap->oap);
6755 }
6756#endif
6757
6758#ifdef FEAT_DIFF
6759 /*
6760 * "[c" and "]c": move to next or previous diff-change.
6761 */
6762 else if (cap->nchar == 'c')
6763 {
6764 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6765 cap->count1) == FAIL)
6766 clearopbeep(cap->oap);
6767 }
6768#endif
6769
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006770#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006771 /*
6772 * "[s", "[S", "]s" and "]S": move to next spell error.
6773 */
6774 else if (cap->nchar == 's' || cap->nchar == 'S')
6775 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006776 setpcmark();
6777 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006778 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6779 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006780 {
6781 clearopbeep(cap->oap);
6782 break;
6783 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006784# ifdef FEAT_FOLDING
6785 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6786 foldOpenCursor();
6787# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006788 }
6789#endif
6790
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 /* Not a valid cap->nchar. */
6792 else
6793 clearopbeep(cap->oap);
6794}
6795
6796/*
6797 * Handle Normal mode "%" command.
6798 */
6799 static void
6800nv_percent(cap)
6801 cmdarg_T *cap;
6802{
6803 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006804#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 linenr_T lnum = curwin->w_cursor.lnum;
6806#endif
6807
6808 cap->oap->inclusive = TRUE;
6809 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6810 {
6811 if (cap->count0 > 100)
6812 clearopbeep(cap->oap);
6813 else
6814 {
6815 cap->oap->motion_type = MLINE;
6816 setpcmark();
6817 /* Round up, so CTRL-G will give same value. Watch out for a
6818 * large line count, the line number must not go negative! */
6819 if (curbuf->b_ml.ml_line_count > 1000000)
6820 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6821 / 100L * cap->count0;
6822 else
6823 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6824 cap->count0 + 99L) / 100L;
6825 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6826 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6827 beginline(BL_SOL | BL_FIX);
6828 }
6829 }
6830 else /* "%" : go to matching paren */
6831 {
6832 cap->oap->motion_type = MCHAR;
6833 cap->oap->use_reg_one = TRUE;
6834 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6835 clearopbeep(cap->oap);
6836 else
6837 {
6838 setpcmark();
6839 curwin->w_cursor = *pos;
6840 curwin->w_set_curswant = TRUE;
6841#ifdef FEAT_VIRTUALEDIT
6842 curwin->w_cursor.coladd = 0;
6843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846 }
6847#ifdef FEAT_FOLDING
6848 if (cap->oap->op_type == OP_NOP
6849 && lnum != curwin->w_cursor.lnum
6850 && (fdo_flags & FDO_PERCENT)
6851 && KeyTyped)
6852 foldOpenCursor();
6853#endif
6854}
6855
6856/*
6857 * Handle "(" and ")" commands.
6858 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6859 */
6860 static void
6861nv_brace(cap)
6862 cmdarg_T *cap;
6863{
6864 cap->oap->motion_type = MCHAR;
6865 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006866 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6867 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 curwin->w_set_curswant = TRUE;
6869
6870 if (findsent(cap->arg, cap->count1) == FAIL)
6871 clearopbeep(cap->oap);
6872 else
6873 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006874 /* Don't leave the cursor on the NUL past end of line. */
6875 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#ifdef FEAT_VIRTUALEDIT
6877 curwin->w_cursor.coladd = 0;
6878#endif
6879#ifdef FEAT_FOLDING
6880 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6881 foldOpenCursor();
6882#endif
6883 }
6884}
6885
6886/*
6887 * "m" command: Mark a position.
6888 */
6889 static void
6890nv_mark(cap)
6891 cmdarg_T *cap;
6892{
6893 if (!checkclearop(cap->oap))
6894 {
6895 if (setmark(cap->nchar) == FAIL)
6896 clearopbeep(cap->oap);
6897 }
6898}
6899
6900/*
6901 * "{" and "}" commands.
6902 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6903 */
6904 static void
6905nv_findpar(cap)
6906 cmdarg_T *cap;
6907{
6908 cap->oap->motion_type = MCHAR;
6909 cap->oap->inclusive = FALSE;
6910 cap->oap->use_reg_one = TRUE;
6911 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006912 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 clearopbeep(cap->oap);
6914 else
6915 {
6916#ifdef FEAT_VIRTUALEDIT
6917 curwin->w_cursor.coladd = 0;
6918#endif
6919#ifdef FEAT_FOLDING
6920 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6921 foldOpenCursor();
6922#endif
6923 }
6924}
6925
6926/*
6927 * "u" command: Undo or make lower case.
6928 */
6929 static void
6930nv_undo(cap)
6931 cmdarg_T *cap;
6932{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006933 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 {
6935 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6936 cap->cmdchar = 'g';
6937 cap->nchar = 'u';
6938 nv_operator(cap);
6939 }
6940 else
6941 nv_kundo(cap);
6942}
6943
6944/*
6945 * <Undo> command.
6946 */
6947 static void
6948nv_kundo(cap)
6949 cmdarg_T *cap;
6950{
6951 if (!checkclearopq(cap->oap))
6952 {
6953 u_undo((int)cap->count1);
6954 curwin->w_set_curswant = TRUE;
6955 }
6956}
6957
6958/*
6959 * Handle the "r" command.
6960 */
6961 static void
6962nv_replace(cap)
6963 cmdarg_T *cap;
6964{
6965 char_u *ptr;
6966 int had_ctrl_v;
6967 long n;
6968
6969 if (checkclearop(cap->oap))
6970 return;
6971
6972 /* get another character */
6973 if (cap->nchar == Ctrl_V)
6974 {
6975 had_ctrl_v = Ctrl_V;
6976 cap->nchar = get_literal();
6977 /* Don't redo a multibyte character with CTRL-V. */
6978 if (cap->nchar > DEL)
6979 had_ctrl_v = NUL;
6980 }
6981 else
6982 had_ctrl_v = NUL;
6983
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006984 /* Abort if the character is a special key. */
6985 if (IS_SPECIAL(cap->nchar))
6986 {
6987 clearopbeep(cap->oap);
6988 return;
6989 }
6990
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 /* Visual mode "r" */
6992 if (VIsual_active)
6993 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006994 if (got_int)
6995 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006996 if (had_ctrl_v)
6997 {
6998 if (cap->nchar == '\r')
6999 cap->nchar = -1;
7000 else if (cap->nchar == '\n')
7001 cap->nchar = -2;
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 nv_operator(cap);
7004 return;
7005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007#ifdef FEAT_VIRTUALEDIT
7008 /* Break tabs, etc. */
7009 if (virtual_active())
7010 {
7011 if (u_save_cursor() == FAIL)
7012 return;
7013 if (gchar_cursor() == NUL)
7014 {
7015 /* Add extra space and put the cursor on the first one. */
7016 coladvance_force((colnr_T)(getviscol() + cap->count1));
7017 curwin->w_cursor.col -= cap->count1;
7018 }
7019 else if (gchar_cursor() == TAB)
7020 coladvance_force(getviscol());
7021 }
7022#endif
7023
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007024 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007026 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027#ifdef FEAT_MBYTE
7028 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7029#endif
7030 )
7031 {
7032 clearopbeep(cap->oap);
7033 return;
7034 }
7035
7036 /*
7037 * Replacing with a TAB is done by edit() when it is complicated because
7038 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7039 * Other characters are done below to avoid problems with things like
7040 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7041 */
7042 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7043 {
7044 stuffnumReadbuff(cap->count1);
7045 stuffcharReadbuff('R');
7046 stuffcharReadbuff('\t');
7047 stuffcharReadbuff(ESC);
7048 return;
7049 }
7050
7051 /* save line for undo */
7052 if (u_save_cursor() == FAIL)
7053 return;
7054
7055 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7056 {
7057 /*
7058 * Replace character(s) by a single newline.
7059 * Strange vi behaviour: Only one newline is inserted.
7060 * Delete the characters here.
7061 * Insert the newline with an insert command, takes care of
7062 * autoindent. The insert command depends on being on the last
7063 * character of a line or not.
7064 */
7065#ifdef FEAT_MBYTE
7066 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7067#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007068 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#endif
7070 stuffcharReadbuff('\r');
7071 stuffcharReadbuff(ESC);
7072
7073 /* Give 'r' to edit(), to get the redo command right. */
7074 invoke_edit(cap, TRUE, 'r', FALSE);
7075 }
7076 else
7077 {
7078 prep_redo(cap->oap->regname, cap->count1,
7079 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7080
7081 curbuf->b_op_start = curwin->w_cursor;
7082#ifdef FEAT_MBYTE
7083 if (has_mbyte)
7084 {
7085 int old_State = State;
7086
7087 if (cap->ncharC1 != 0)
7088 AppendCharToRedobuff(cap->ncharC1);
7089 if (cap->ncharC2 != 0)
7090 AppendCharToRedobuff(cap->ncharC2);
7091
7092 /* This is slow, but it handles replacing a single-byte with a
7093 * multi-byte and the other way around. Also handles adding
7094 * composing characters for utf-8. */
7095 for (n = cap->count1; n > 0; --n)
7096 {
7097 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007098 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7099 {
7100 int c = ins_copychar(curwin->w_cursor.lnum
7101 + (cap->nchar == Ctrl_Y ? -1 : 1));
7102 if (c != NUL)
7103 ins_char(c);
7104 else
7105 /* will be decremented further down */
7106 ++curwin->w_cursor.col;
7107 }
7108 else
7109 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 State = old_State;
7111 if (cap->ncharC1 != 0)
7112 ins_char(cap->ncharC1);
7113 if (cap->ncharC2 != 0)
7114 ins_char(cap->ncharC2);
7115 }
7116 }
7117 else
7118#endif
7119 {
7120 /*
7121 * Replace the characters within one line.
7122 */
7123 for (n = cap->count1; n > 0; --n)
7124 {
7125 /*
7126 * Get ptr again, because u_save and/or showmatch() will have
7127 * released the line. At the same time we let know that the
7128 * line will be changed.
7129 */
7130 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007131 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7132 {
7133 int c = ins_copychar(curwin->w_cursor.lnum
7134 + (cap->nchar == Ctrl_Y ? -1 : 1));
7135 if (c != NUL)
7136 ptr[curwin->w_cursor.col] = c;
7137 }
7138 else
7139 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (p_sm && msg_silent == 0)
7141 showmatch(cap->nchar);
7142 ++curwin->w_cursor.col;
7143 }
7144#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007145 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007147 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
Bram Moolenaar009b2592004-10-24 19:18:58 +00007149 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007150 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007152 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
7154#endif
7155
7156 /* mark the buffer as changed and prepare for displaying */
7157 changed_bytes(curwin->w_cursor.lnum,
7158 (colnr_T)(curwin->w_cursor.col - cap->count1));
7159 }
7160 --curwin->w_cursor.col; /* cursor on the last replaced char */
7161#ifdef FEAT_MBYTE
7162 /* if the character on the left of the current cursor is a multi-byte
7163 * character, move two characters left */
7164 if (has_mbyte)
7165 mb_adjust_cursor();
7166#endif
7167 curbuf->b_op_end = curwin->w_cursor;
7168 curwin->w_set_curswant = TRUE;
7169 set_last_insert(cap->nchar);
7170 }
7171}
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * 'o': Exchange start and end of Visual area.
7175 * 'O': same, but in block mode exchange left and right corners.
7176 */
7177 static void
7178v_swap_corners(cmdchar)
7179 int cmdchar;
7180{
7181 pos_T old_cursor;
7182 colnr_T left, right;
7183
7184 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7185 {
7186 old_cursor = curwin->w_cursor;
7187 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7188 curwin->w_cursor.lnum = VIsual.lnum;
7189 coladvance(left);
7190 VIsual = curwin->w_cursor;
7191
7192 curwin->w_cursor.lnum = old_cursor.lnum;
7193 curwin->w_curswant = right;
7194 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7195 * right one column */
7196 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7197 ++curwin->w_curswant;
7198 coladvance(curwin->w_curswant);
7199 if (curwin->w_cursor.col == old_cursor.col
7200#ifdef FEAT_VIRTUALEDIT
7201 && (!virtual_active()
7202 || curwin->w_cursor.coladd == old_cursor.coladd)
7203#endif
7204 )
7205 {
7206 curwin->w_cursor.lnum = VIsual.lnum;
7207 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7208 ++right;
7209 coladvance(right);
7210 VIsual = curwin->w_cursor;
7211
7212 curwin->w_cursor.lnum = old_cursor.lnum;
7213 coladvance(left);
7214 curwin->w_curswant = left;
7215 }
7216 }
7217 else
7218 {
7219 old_cursor = curwin->w_cursor;
7220 curwin->w_cursor = VIsual;
7221 VIsual = old_cursor;
7222 curwin->w_set_curswant = TRUE;
7223 }
7224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226/*
7227 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7228 */
7229 static void
7230nv_Replace(cap)
7231 cmdarg_T *cap;
7232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 if (VIsual_active) /* "R" is replace lines */
7234 {
7235 cap->cmdchar = 'c';
7236 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007237 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 VIsual_mode = 'V';
7239 nv_operator(cap);
7240 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007241 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 if (!curbuf->b_p_ma)
7244 EMSG(_(e_modifiable));
7245 else
7246 {
7247#ifdef FEAT_VIRTUALEDIT
7248 if (virtual_active())
7249 coladvance(getviscol());
7250#endif
7251 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7252 }
7253 }
7254}
7255
7256#ifdef FEAT_VREPLACE
7257/*
7258 * "gr".
7259 */
7260 static void
7261nv_vreplace(cap)
7262 cmdarg_T *cap;
7263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 if (VIsual_active)
7265 {
7266 cap->cmdchar = 'r';
7267 cap->nchar = cap->extra_char;
7268 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7269 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007270 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
7272 if (!curbuf->b_p_ma)
7273 EMSG(_(e_modifiable));
7274 else
7275 {
7276 if (cap->extra_char == Ctrl_V) /* get another character */
7277 cap->extra_char = get_literal();
7278 stuffcharReadbuff(cap->extra_char);
7279 stuffcharReadbuff(ESC);
7280# ifdef FEAT_VIRTUALEDIT
7281 if (virtual_active())
7282 coladvance(getviscol());
7283# endif
7284 invoke_edit(cap, TRUE, 'v', FALSE);
7285 }
7286 }
7287}
7288#endif
7289
7290/*
7291 * Swap case for "~" command, when it does not work like an operator.
7292 */
7293 static void
7294n_swapchar(cap)
7295 cmdarg_T *cap;
7296{
7297 long n;
7298 pos_T startpos;
7299 int did_change = 0;
7300#ifdef FEAT_NETBEANS_INTG
7301 pos_T pos;
7302 char_u *ptr;
7303 int count;
7304#endif
7305
7306 if (checkclearopq(cap->oap))
7307 return;
7308
7309 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7310 {
7311 clearopbeep(cap->oap);
7312 return;
7313 }
7314
7315 prep_redo_cmd(cap);
7316
7317 if (u_save_cursor() == FAIL)
7318 return;
7319
7320 startpos = curwin->w_cursor;
7321#ifdef FEAT_NETBEANS_INTG
7322 pos = startpos;
7323#endif
7324 for (n = cap->count1; n > 0; --n)
7325 {
7326 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7327 inc_cursor();
7328 if (gchar_cursor() == NUL)
7329 {
7330 if (vim_strchr(p_ww, '~') != NULL
7331 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7332 {
7333#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007334 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 if (did_change)
7337 {
7338 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007339 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007340 netbeans_removed(curbuf, pos.lnum, pos.col,
7341 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007343 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345 pos.col = 0;
7346 pos.lnum++;
7347 }
7348#endif
7349 ++curwin->w_cursor.lnum;
7350 curwin->w_cursor.col = 0;
7351 if (n > 1)
7352 {
7353 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7354 break;
7355 u_clearline();
7356 }
7357 }
7358 else
7359 break;
7360 }
7361 }
7362#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007363 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
7365 ptr = ml_get(pos.lnum);
7366 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007367 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7368 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370#endif
7371
7372
7373 check_cursor();
7374 curwin->w_set_curswant = TRUE;
7375 if (did_change)
7376 {
7377 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7378 0L);
7379 curbuf->b_op_start = startpos;
7380 curbuf->b_op_end = curwin->w_cursor;
7381 if (curbuf->b_op_end.col > 0)
7382 --curbuf->b_op_end.col;
7383 }
7384}
7385
7386/*
7387 * Move cursor to mark.
7388 */
7389 static void
7390nv_cursormark(cap, flag, pos)
7391 cmdarg_T *cap;
7392 int flag;
7393 pos_T *pos;
7394{
7395 if (check_mark(pos) == FAIL)
7396 clearop(cap->oap);
7397 else
7398 {
7399 if (cap->cmdchar == '\''
7400 || cap->cmdchar == '`'
7401 || cap->cmdchar == '['
7402 || cap->cmdchar == ']')
7403 setpcmark();
7404 curwin->w_cursor = *pos;
7405 if (flag)
7406 beginline(BL_WHITE | BL_FIX);
7407 else
7408 check_cursor();
7409 }
7410 cap->oap->motion_type = flag ? MLINE : MCHAR;
7411 if (cap->cmdchar == '`')
7412 cap->oap->use_reg_one = TRUE;
7413 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7414 curwin->w_set_curswant = TRUE;
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*
7418 * Handle commands that are operators in Visual mode.
7419 */
7420 static void
7421v_visop(cap)
7422 cmdarg_T *cap;
7423{
7424 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7425
7426 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007427 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (isupper(cap->cmdchar))
7429 {
7430 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007431 {
7432 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7436 curwin->w_curswant = MAXCOL;
7437 }
7438 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7439 nv_operator(cap);
7440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441
7442/*
7443 * "s" and "S" commands.
7444 */
7445 static void
7446nv_subst(cap)
7447 cmdarg_T *cap;
7448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7450 {
7451 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007452 {
7453 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 cap->cmdchar = 'c';
7457 nv_operator(cap);
7458 }
7459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 nv_optrans(cap);
7461}
7462
7463/*
7464 * Abbreviated commands.
7465 */
7466 static void
7467nv_abbrev(cap)
7468 cmdarg_T *cap;
7469{
7470 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7471 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 /* in Visual mode these commands are operators */
7474 if (VIsual_active)
7475 v_visop(cap);
7476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 nv_optrans(cap);
7478}
7479
7480/*
7481 * Translate a command into another command.
7482 */
7483 static void
7484nv_optrans(cap)
7485 cmdarg_T *cap;
7486{
7487 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7488 (char_u *)"d$", (char_u *)"c$",
7489 (char_u *)"cl", (char_u *)"cc",
7490 (char_u *)"yy", (char_u *)":s\r"};
7491 static char_u *str = (char_u *)"xXDCsSY&";
7492
7493 if (!checkclearopq(cap->oap))
7494 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007495 /* In Vi "2D" doesn't delete the next line. Can't translate it
7496 * either, because "2." should also not use the count. */
7497 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7498 {
7499 cap->oap->start = curwin->w_cursor;
7500 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007501#ifdef FEAT_EVAL
7502 set_op_var(OP_DELETE);
7503#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007504 cap->count1 = 1;
7505 nv_dollar(cap);
7506 finish_op = TRUE;
7507 ResetRedobuff();
7508 AppendCharToRedobuff('D');
7509 }
7510 else
7511 {
7512 if (cap->count0)
7513 stuffnumReadbuff(cap->count0);
7514 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 cap->opcount = 0;
7518}
7519
7520/*
7521 * "'" and "`" commands. Also for "g'" and "g`".
7522 * cap->arg is TRUE for "'" and "g'".
7523 */
7524 static void
7525nv_gomark(cap)
7526 cmdarg_T *cap;
7527{
7528 pos_T *pos;
7529 int c;
7530#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007531 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7533#endif
7534
7535 if (cap->cmdchar == 'g')
7536 c = cap->extra_char;
7537 else
7538 c = cap->nchar;
7539 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7540 if (pos == (pos_T *)-1) /* jumped to other file */
7541 {
7542 if (cap->arg)
7543 {
7544 check_cursor_lnum();
7545 beginline(BL_WHITE | BL_FIX);
7546 }
7547 else
7548 check_cursor();
7549 }
7550 else
7551 nv_cursormark(cap, cap->arg, pos);
7552
7553#ifdef FEAT_VIRTUALEDIT
7554 /* May need to clear the coladd that a mark includes. */
7555 if (!virtual_active())
7556 curwin->w_cursor.coladd = 0;
7557#endif
7558#ifdef FEAT_FOLDING
7559 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007560 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007561 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 && (fdo_flags & FDO_MARK)
7563 && old_KeyTyped)
7564 foldOpenCursor();
7565#endif
7566}
7567
7568/*
7569 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7570 */
7571 static void
7572nv_pcmark(cap)
7573 cmdarg_T *cap;
7574{
7575#ifdef FEAT_JUMPLIST
7576 pos_T *pos;
7577# ifdef FEAT_FOLDING
7578 linenr_T lnum = curwin->w_cursor.lnum;
7579 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7580# endif
7581
7582 if (!checkclearopq(cap->oap))
7583 {
7584 if (cap->cmdchar == 'g')
7585 pos = movechangelist((int)cap->count1);
7586 else
7587 pos = movemark((int)cap->count1);
7588 if (pos == (pos_T *)-1) /* jump to other file */
7589 {
7590 curwin->w_set_curswant = TRUE;
7591 check_cursor();
7592 }
7593 else if (pos != NULL) /* can jump */
7594 nv_cursormark(cap, FALSE, pos);
7595 else if (cap->cmdchar == 'g')
7596 {
7597 if (curbuf->b_changelistlen == 0)
7598 EMSG(_("E664: changelist is empty"));
7599 else if (cap->count1 < 0)
7600 EMSG(_("E662: At start of changelist"));
7601 else
7602 EMSG(_("E663: At end of changelist"));
7603 }
7604 else
7605 clearopbeep(cap->oap);
7606# ifdef FEAT_FOLDING
7607 if (cap->oap->op_type == OP_NOP
7608 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7609 && (fdo_flags & FDO_MARK)
7610 && old_KeyTyped)
7611 foldOpenCursor();
7612# endif
7613 }
7614#else
7615 clearopbeep(cap->oap);
7616#endif
7617}
7618
7619/*
7620 * Handle '"' command.
7621 */
7622 static void
7623nv_regname(cap)
7624 cmdarg_T *cap;
7625{
7626 if (checkclearop(cap->oap))
7627 return;
7628#ifdef FEAT_EVAL
7629 if (cap->nchar == '=')
7630 cap->nchar = get_expr_register();
7631#endif
7632 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7633 {
7634 cap->oap->regname = cap->nchar;
7635 cap->opcount = cap->count0; /* remember count before '"' */
7636#ifdef FEAT_EVAL
7637 set_reg_var(cap->oap->regname);
7638#endif
7639 }
7640 else
7641 clearopbeep(cap->oap);
7642}
7643
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644/*
7645 * Handle "v", "V" and "CTRL-V" commands.
7646 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7647 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007648 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 */
7650 static void
7651nv_visual(cap)
7652 cmdarg_T *cap;
7653{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007654 if (cap->cmdchar == Ctrl_Q)
7655 cap->cmdchar = Ctrl_V;
7656
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7658 * characterwise, linewise, or blockwise. */
7659 if (cap->oap->op_type != OP_NOP)
7660 {
7661 cap->oap->motion_force = cap->cmdchar;
7662 finish_op = FALSE; /* operator doesn't finish now but later */
7663 return;
7664 }
7665
7666 VIsual_select = cap->arg;
7667 if (VIsual_active) /* change Visual mode */
7668 {
7669 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7670 end_visual_mode();
7671 else /* toggle char/block mode */
7672 { /* or char/line mode */
7673 VIsual_mode = cap->cmdchar;
7674 showmode();
7675 }
7676 redraw_curbuf_later(INVERTED); /* update the inversion */
7677 }
7678 else /* start Visual mode */
7679 {
7680 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007681 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007683 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 VIsual = curwin->w_cursor;
7685
7686 VIsual_active = TRUE;
7687 VIsual_reselect = TRUE;
7688 if (!cap->arg)
7689 /* start Select mode when 'selectmode' contains "cmd" */
7690 may_start_select('c');
7691#ifdef FEAT_MOUSE
7692 setmouse();
7693#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007694 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 redraw_cmdline = TRUE; /* show visual mode later */
7696 /*
7697 * For V and ^V, we multiply the number of lines even if there
7698 * was only one -- webb
7699 */
7700 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7701 {
7702 curwin->w_cursor.lnum +=
7703 resel_VIsual_line_count * cap->count0 - 1;
7704 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7705 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7706 }
7707 VIsual_mode = resel_VIsual_mode;
7708 if (VIsual_mode == 'v')
7709 {
7710 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007711 {
7712 validate_virtcol();
7713 curwin->w_curswant = curwin->w_virtcol
7714 + resel_VIsual_vcol * cap->count0 - 1;
7715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007717 curwin->w_curswant = resel_VIsual_vcol;
7718 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007720 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
7722 curwin->w_curswant = MAXCOL;
7723 coladvance((colnr_T)MAXCOL);
7724 }
7725 else if (VIsual_mode == Ctrl_V)
7726 {
7727 validate_virtcol();
7728 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007729 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 coladvance(curwin->w_curswant);
7731 }
7732 else
7733 curwin->w_set_curswant = TRUE;
7734 redraw_curbuf_later(INVERTED); /* show the inversion */
7735 }
7736 else
7737 {
7738 if (!cap->arg)
7739 /* start Select mode when 'selectmode' contains "cmd" */
7740 may_start_select('c');
7741 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007742 if (VIsual_mode != 'V' && *p_sel == 'e')
7743 ++cap->count1; /* include one more char */
7744 if (cap->count0 > 0 && --cap->count1 > 0)
7745 {
7746 /* With a count select that many characters or lines. */
7747 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7748 nv_right(cap);
7749 else if (VIsual_mode == 'V')
7750 nv_down(cap);
7751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 }
7754}
7755
7756/*
7757 * Start selection for Shift-movement keys.
7758 */
7759 void
7760start_selection()
7761{
7762 /* if 'selectmode' contains "key", start Select mode */
7763 may_start_select('k');
7764 n_start_visual_mode('v');
7765}
7766
7767/*
7768 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7769 */
7770 void
7771may_start_select(c)
7772 int c;
7773{
7774 VIsual_select = (stuff_empty() && typebuf_typed()
7775 && (vim_strchr(p_slm, c) != NULL));
7776}
7777
7778/*
7779 * Start Visual mode "c".
7780 * Should set VIsual_select before calling this.
7781 */
7782 static void
7783n_start_visual_mode(c)
7784 int c;
7785{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007786#ifdef FEAT_CONCEAL
7787 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007788 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007789#endif
7790
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 VIsual_mode = c;
7792 VIsual_active = TRUE;
7793 VIsual_reselect = TRUE;
7794#ifdef FEAT_VIRTUALEDIT
7795 /* Corner case: the 0 position in a tab may change when going into
7796 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7797 */
7798 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007799 {
7800 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803#endif
7804 VIsual = curwin->w_cursor;
7805
7806#ifdef FEAT_FOLDING
7807 foldAdjustVisual();
7808#endif
7809
7810#ifdef FEAT_MOUSE
7811 setmouse();
7812#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007813#ifdef FEAT_CONCEAL
7814 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007815 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007816#endif
7817
Bram Moolenaar09df3122006-01-23 22:23:09 +00007818 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 redraw_cmdline = TRUE; /* show visual mode later */
7820#ifdef FEAT_CLIPBOARD
7821 /* Make sure the clipboard gets updated. Needed because start and
7822 * end may still be the same, and the selection needs to be owned */
7823 clip_star.vmode = NUL;
7824#endif
7825
7826 /* Only need to redraw this line, unless still need to redraw an old
7827 * Visual area (when 'lazyredraw' is set). */
7828 if (curwin->w_redr_type < INVERTED)
7829 {
7830 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7831 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7832 }
7833}
7834
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836/*
7837 * CTRL-W: Window commands
7838 */
7839 static void
7840nv_window(cap)
7841 cmdarg_T *cap;
7842{
7843#ifdef FEAT_WINDOWS
7844 if (!checkclearop(cap->oap))
7845 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7846#else
7847 (void)checkclearop(cap->oap);
7848#endif
7849}
7850
7851/*
7852 * CTRL-Z: Suspend
7853 */
7854 static void
7855nv_suspend(cap)
7856 cmdarg_T *cap;
7857{
7858 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (VIsual_active)
7860 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 do_cmdline_cmd((char_u *)"st");
7862}
7863
7864/*
7865 * Commands starting with "g".
7866 */
7867 static void
7868nv_g_cmd(cap)
7869 cmdarg_T *cap;
7870{
7871 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 int i;
7874 int flag = FALSE;
7875
7876 switch (cap->nchar)
7877 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007878 case Ctrl_A:
7879 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#ifdef MEM_PROFILE
7881 /*
7882 * "g^A": dump log of used memory.
7883 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007884 if (!VIsual_active && cap->nchar == Ctrl_A)
7885 vim_mem_profile_dump();
7886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007888 /*
7889 * "g^A/g^X": sequentially increment visually selected region
7890 */
7891 if (VIsual_active)
7892 {
7893 cap->arg = TRUE;
7894 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007895 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007896 nv_addsub(cap);
7897 }
7898 else
7899 clearopbeep(oap);
7900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901
7902#ifdef FEAT_VREPLACE
7903 /*
7904 * "gR": Enter virtual replace mode.
7905 */
7906 case 'R':
7907 cap->arg = TRUE;
7908 nv_Replace(cap);
7909 break;
7910
7911 case 'r':
7912 nv_vreplace(cap);
7913 break;
7914#endif
7915
7916 case '&':
7917 do_cmdline_cmd((char_u *)"%s//~/&");
7918 break;
7919
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 /*
7921 * "gv": Reselect the previous Visual area. If Visual already active,
7922 * exchange previous and current Visual area.
7923 */
7924 case 'v':
7925 if (checkclearop(oap))
7926 break;
7927
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007928 if ( curbuf->b_visual.vi_start.lnum == 0
7929 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7930 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 beep_flush();
7932 else
7933 {
7934 /* set w_cursor to the start of the Visual area, tpos to the end */
7935 if (VIsual_active)
7936 {
7937 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007938 VIsual_mode = curbuf->b_visual.vi_mode;
7939 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940# ifdef FEAT_EVAL
7941 curbuf->b_visual_mode_eval = i;
7942# endif
7943 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007944 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7945 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007947 tpos = curbuf->b_visual.vi_end;
7948 curbuf->b_visual.vi_end = curwin->w_cursor;
7949 curwin->w_cursor = curbuf->b_visual.vi_start;
7950 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 }
7952 else
7953 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007954 VIsual_mode = curbuf->b_visual.vi_mode;
7955 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7956 tpos = curbuf->b_visual.vi_end;
7957 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959
7960 VIsual_active = TRUE;
7961 VIsual_reselect = TRUE;
7962
7963 /* Set Visual to the start and w_cursor to the end of the Visual
7964 * area. Make sure they are on an existing character. */
7965 check_cursor();
7966 VIsual = curwin->w_cursor;
7967 curwin->w_cursor = tpos;
7968 check_cursor();
7969 update_topline();
7970 /*
7971 * When called from normal "g" command: start Select mode when
7972 * 'selectmode' contains "cmd". When called for K_SELECT, always
7973 * start Select mode.
7974 */
7975 if (cap->arg)
7976 VIsual_select = TRUE;
7977 else
7978 may_start_select('c');
7979#ifdef FEAT_MOUSE
7980 setmouse();
7981#endif
7982#ifdef FEAT_CLIPBOARD
7983 /* Make sure the clipboard gets updated. Needed because start and
7984 * end are still the same, and the selection needs to be owned */
7985 clip_star.vmode = NUL;
7986#endif
7987 redraw_curbuf_later(INVERTED);
7988 showmode();
7989 }
7990 break;
7991 /*
7992 * "gV": Don't reselect the previous Visual area after a Select mode
7993 * mapping of menu.
7994 */
7995 case 'V':
7996 VIsual_reselect = FALSE;
7997 break;
7998
7999 /*
8000 * "gh": start Select mode.
8001 * "gH": start Select line mode.
8002 * "g^H": start Select block mode.
8003 */
8004 case K_BS:
8005 cap->nchar = Ctrl_H;
8006 /* FALLTHROUGH */
8007 case 'h':
8008 case 'H':
8009 case Ctrl_H:
8010# ifdef EBCDIC
8011 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8012 if (cap->nchar == Ctrl_H)
8013 cap->cmdchar = Ctrl_V;
8014 else
8015# endif
8016 cap->cmdchar = cap->nchar + ('v' - 'h');
8017 cap->arg = TRUE;
8018 nv_visual(cap);
8019 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008020
8021 /* "gn", "gN" visually select next/previous search match
8022 * "gn" selects next match
8023 * "gN" selects previous match
8024 */
8025 case 'N':
8026 case 'n':
8027 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008028 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 /*
8032 * "gj" and "gk" two new funny movement keys -- up and down
8033 * movement based on *screen* line rather than *file* line.
8034 */
8035 case 'j':
8036 case K_DOWN:
8037 /* with 'nowrap' it works just like the normal "j" command; also when
8038 * in a closed fold */
8039 if (!curwin->w_p_wrap
8040#ifdef FEAT_FOLDING
8041 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8042#endif
8043 )
8044 {
8045 oap->motion_type = MLINE;
8046 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8047 }
8048 else
8049 i = nv_screengo(oap, FORWARD, cap->count1);
8050 if (i == FAIL)
8051 clearopbeep(oap);
8052 break;
8053
8054 case 'k':
8055 case K_UP:
8056 /* with 'nowrap' it works just like the normal "k" command; also when
8057 * in a closed fold */
8058 if (!curwin->w_p_wrap
8059#ifdef FEAT_FOLDING
8060 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8061#endif
8062 )
8063 {
8064 oap->motion_type = MLINE;
8065 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8066 }
8067 else
8068 i = nv_screengo(oap, BACKWARD, cap->count1);
8069 if (i == FAIL)
8070 clearopbeep(oap);
8071 break;
8072
8073 /*
8074 * "gJ": join two lines without inserting a space.
8075 */
8076 case 'J':
8077 nv_join(cap);
8078 break;
8079
8080 /*
8081 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8082 * "gm": middle of "g0" and "g$".
8083 */
8084 case '^':
8085 flag = TRUE;
8086 /* FALLTHROUGH */
8087
8088 case '0':
8089 case 'm':
8090 case K_HOME:
8091 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 oap->motion_type = MCHAR;
8093 oap->inclusive = FALSE;
8094 if (curwin->w_p_wrap
8095#ifdef FEAT_VERTSPLIT
8096 && curwin->w_width != 0
8097#endif
8098 )
8099 {
8100 int width1 = W_WIDTH(curwin) - curwin_col_off();
8101 int width2 = width1 + curwin_col_off2();
8102
8103 validate_virtcol();
8104 i = 0;
8105 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8106 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8107 }
8108 else
8109 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008110 /* Go to the middle of the screen line. When 'number' or
8111 * 'relativenumber' is on and lines are wrapping the middle can be more
8112 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (cap->nchar == 'm')
8114 i += (W_WIDTH(curwin) - curwin_col_off()
8115 + ((curwin->w_p_wrap && i > 0)
8116 ? curwin_col_off2() : 0)) / 2;
8117 coladvance((colnr_T)i);
8118 if (flag)
8119 {
8120 do
8121 i = gchar_cursor();
8122 while (vim_iswhite(i) && oneright() == OK);
8123 }
8124 curwin->w_set_curswant = TRUE;
8125 break;
8126
8127 case '_':
8128 /* "g_": to the last non-blank character in the line or <count> lines
8129 * downward. */
8130 cap->oap->motion_type = MCHAR;
8131 cap->oap->inclusive = TRUE;
8132 curwin->w_curswant = MAXCOL;
8133 if (cursor_down((long)(cap->count1 - 1),
8134 cap->oap->op_type == OP_NOP) == FAIL)
8135 clearopbeep(cap->oap);
8136 else
8137 {
8138 char_u *ptr = ml_get_curline();
8139
8140 /* In Visual mode we may end up after the line. */
8141 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8142 --curwin->w_cursor.col;
8143
8144 /* Decrease the cursor column until it's on a non-blank. */
8145 while (curwin->w_cursor.col > 0
8146 && vim_iswhite(ptr[curwin->w_cursor.col]))
8147 --curwin->w_cursor.col;
8148 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008149 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 }
8151 break;
8152
8153 case '$':
8154 case K_END:
8155 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
8157 int col_off = curwin_col_off();
8158
8159 oap->motion_type = MCHAR;
8160 oap->inclusive = TRUE;
8161 if (curwin->w_p_wrap
8162#ifdef FEAT_VERTSPLIT
8163 && curwin->w_width != 0
8164#endif
8165 )
8166 {
8167 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8168 if (cap->count1 == 1)
8169 {
8170 int width1 = W_WIDTH(curwin) - col_off;
8171 int width2 = width1 + curwin_col_off2();
8172
8173 validate_virtcol();
8174 i = width1 - 1;
8175 if (curwin->w_virtcol >= (colnr_T)width1)
8176 i += ((curwin->w_virtcol - width1) / width2 + 1)
8177 * width2;
8178 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008179
8180 /* Make sure we stick in this column. */
8181 validate_virtcol();
8182 curwin->w_curswant = curwin->w_virtcol;
8183 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8185 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8186 {
8187 /*
8188 * Check for landing on a character that got split at
8189 * the end of the line. We do not want to advance to
8190 * the next screen line.
8191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (curwin->w_virtcol > (colnr_T)i)
8193 --curwin->w_cursor.col;
8194 }
8195#endif
8196 }
8197 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8198 clearopbeep(oap);
8199 }
8200 else
8201 {
8202 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8203 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008204
8205 /* Make sure we stick in this column. */
8206 validate_virtcol();
8207 curwin->w_curswant = curwin->w_virtcol;
8208 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 }
8210 }
8211 break;
8212
8213 /*
8214 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8215 */
8216 case '*':
8217 case '#':
8218#if POUND != '#'
8219 case POUND: /* pound sign (sometimes equal to '#') */
8220#endif
8221 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8222 case ']': /* :tselect for current identifier */
8223 nv_ident(cap);
8224 break;
8225
8226 /*
8227 * ge and gE: go back to end of word
8228 */
8229 case 'e':
8230 case 'E':
8231 oap->motion_type = MCHAR;
8232 curwin->w_set_curswant = TRUE;
8233 oap->inclusive = TRUE;
8234 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8235 clearopbeep(oap);
8236 break;
8237
8238 /*
8239 * "g CTRL-G": display info about cursor position
8240 */
8241 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008242 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 break;
8244
8245 /*
8246 * "gi": start Insert at the last position.
8247 */
8248 case 'i':
8249 if (curbuf->b_last_insert.lnum != 0)
8250 {
8251 curwin->w_cursor = curbuf->b_last_insert;
8252 check_cursor_lnum();
8253 i = (int)STRLEN(ml_get_curline());
8254 if (curwin->w_cursor.col > (colnr_T)i)
8255 {
8256#ifdef FEAT_VIRTUALEDIT
8257 if (virtual_active())
8258 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8259#endif
8260 curwin->w_cursor.col = i;
8261 }
8262 }
8263 cap->cmdchar = 'i';
8264 nv_edit(cap);
8265 break;
8266
8267 /*
8268 * "gI": Start insert in column 1.
8269 */
8270 case 'I':
8271 beginline(0);
8272 if (!checkclearopq(oap))
8273 invoke_edit(cap, FALSE, 'g', FALSE);
8274 break;
8275
8276#ifdef FEAT_SEARCHPATH
8277 /*
8278 * "gf": goto file, edit file under cursor
8279 * "]f" and "[f": can also be used.
8280 */
8281 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008282 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 nv_gotofile(cap);
8284 break;
8285#endif
8286
8287 /* "g'm" and "g`m": jump to mark without setting pcmark */
8288 case '\'':
8289 cap->arg = TRUE;
8290 /*FALLTHROUGH*/
8291 case '`':
8292 nv_gomark(cap);
8293 break;
8294
8295 /*
8296 * "gs": Goto sleep.
8297 */
8298 case 's':
8299 do_sleep(cap->count1 * 1000L);
8300 break;
8301
8302 /*
8303 * "ga": Display the ascii value of the character under the
8304 * cursor. It is displayed in decimal, hex, and octal. -- webb
8305 */
8306 case 'a':
8307 do_ascii(NULL);
8308 break;
8309
8310#ifdef FEAT_MBYTE
8311 /*
8312 * "g8": Display the bytes used for the UTF-8 character under the
8313 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008314 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 */
8316 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008317 if (cap->count0 == 8)
8318 utf_find_illegal();
8319 else
8320 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 break;
8322#endif
8323
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008324 case '<':
8325 show_sb_text();
8326 break;
8327
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 /*
8329 * "gg": Goto the first line in file. With a count it goes to
8330 * that line number like for "G". -- webb
8331 */
8332 case 'g':
8333 cap->arg = FALSE;
8334 nv_goto(cap);
8335 break;
8336
8337 /*
8338 * Two-character operators:
8339 * "gq" Format text
8340 * "gw" Format text and keep cursor position
8341 * "g~" Toggle the case of the text.
8342 * "gu" Change text to lower case.
8343 * "gU" Change text to upper case.
8344 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008345 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 */
8347 case 'q':
8348 case 'w':
8349 oap->cursor_start = curwin->w_cursor;
8350 /*FALLTHROUGH*/
8351 case '~':
8352 case 'u':
8353 case 'U':
8354 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008355 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 nv_operator(cap);
8357 break;
8358
8359 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008360 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 * current function
8362 * "gD": idem, but in the current file.
8363 */
8364 case 'd':
8365 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008366 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 break;
8368
8369#ifdef FEAT_MOUSE
8370 /*
8371 * g<*Mouse> : <C-*mouse>
8372 */
8373 case K_MIDDLEMOUSE:
8374 case K_MIDDLEDRAG:
8375 case K_MIDDLERELEASE:
8376 case K_LEFTMOUSE:
8377 case K_LEFTDRAG:
8378 case K_LEFTRELEASE:
8379 case K_RIGHTMOUSE:
8380 case K_RIGHTDRAG:
8381 case K_RIGHTRELEASE:
8382 case K_X1MOUSE:
8383 case K_X1DRAG:
8384 case K_X1RELEASE:
8385 case K_X2MOUSE:
8386 case K_X2DRAG:
8387 case K_X2RELEASE:
8388 mod_mask = MOD_MASK_CTRL;
8389 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8390 break;
8391#endif
8392
8393 case K_IGNORE:
8394 break;
8395
8396 /*
8397 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8398 */
8399 case 'p':
8400 case 'P':
8401 nv_put(cap);
8402 break;
8403
8404#ifdef FEAT_BYTEOFF
8405 /* "go": goto byte count from start of buffer */
8406 case 'o':
8407 goto_byte(cap->count0);
8408 break;
8409#endif
8410
8411 /* "gQ": improved Ex mode */
8412 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008413 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {
8415 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008416 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 break;
8418 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 if (!checkclearopq(oap))
8421 do_exmode(TRUE);
8422 break;
8423
8424#ifdef FEAT_JUMPLIST
8425 case ',':
8426 nv_pcmark(cap);
8427 break;
8428
8429 case ';':
8430 cap->count1 = -cap->count1;
8431 nv_pcmark(cap);
8432 break;
8433#endif
8434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008435#ifdef FEAT_WINDOWS
8436 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008437 if (!checkclearop(oap))
8438 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008439 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008440 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008441 if (!checkclearop(oap))
8442 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008443 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008444#endif
8445
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008446 case '+':
8447 case '-': /* "g+" and "g-": undo or redo along the timeline */
8448 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008449 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008450 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008451 break;
8452
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 default:
8454 clearopbeep(oap);
8455 break;
8456 }
8457}
8458
8459/*
8460 * Handle "o" and "O" commands.
8461 */
8462 static void
8463n_opencmd(cap)
8464 cmdarg_T *cap;
8465{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008466#ifdef FEAT_CONCEAL
8467 linenr_T oldline = curwin->w_cursor.lnum;
8468#endif
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (!checkclearopq(cap->oap))
8471 {
8472#ifdef FEAT_FOLDING
8473 if (cap->cmdchar == 'O')
8474 /* Open above the first line of a folded sequence of lines */
8475 (void)hasFolding(curwin->w_cursor.lnum,
8476 &curwin->w_cursor.lnum, NULL);
8477 else
8478 /* Open below the last line of a folded sequence of lines */
8479 (void)hasFolding(curwin->w_cursor.lnum,
8480 NULL, &curwin->w_cursor.lnum);
8481#endif
8482 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8483 (cap->cmdchar == 'O' ? 1 : 0)),
8484 (linenr_T)(curwin->w_cursor.lnum +
8485 (cap->cmdchar == 'o' ? 1 : 0))
8486 ) == OK
8487 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8488#ifdef FEAT_COMMENTS
8489 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8490#endif
8491 0, 0))
8492 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008493#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008494 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008495 update_single_line(curwin, oldline);
8496#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008497 /* When '#' is in 'cpoptions' ignore the count. */
8498 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8499 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008500#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008501 if (curwin->w_p_cul)
8502 /* force redraw of cursorline */
8503 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8506 }
8507 }
8508}
8509
8510/*
8511 * "." command: redo last change.
8512 */
8513 static void
8514nv_dot(cap)
8515 cmdarg_T *cap;
8516{
8517 if (!checkclearopq(cap->oap))
8518 {
8519 /*
8520 * If "restart_edit" is TRUE, the last but one command is repeated
8521 * instead of the last command (inserting text). This is used for
8522 * CTRL-O <.> in insert mode.
8523 */
8524 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8525 clearopbeep(cap->oap);
8526 }
8527}
8528
8529/*
8530 * CTRL-R: undo undo
8531 */
8532 static void
8533nv_redo(cap)
8534 cmdarg_T *cap;
8535{
8536 if (!checkclearopq(cap->oap))
8537 {
8538 u_redo((int)cap->count1);
8539 curwin->w_set_curswant = TRUE;
8540 }
8541}
8542
8543/*
8544 * Handle "U" command.
8545 */
8546 static void
8547nv_Undo(cap)
8548 cmdarg_T *cap;
8549{
8550 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008551 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
8553 /* translate "gUU" to "gUgU" */
8554 cap->cmdchar = 'g';
8555 cap->nchar = 'U';
8556 nv_operator(cap);
8557 }
8558 else if (!checkclearopq(cap->oap))
8559 {
8560 u_undoline();
8561 curwin->w_set_curswant = TRUE;
8562 }
8563}
8564
8565/*
8566 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8567 * single character.
8568 */
8569 static void
8570nv_tilde(cap)
8571 cmdarg_T *cap;
8572{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008573 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 n_swapchar(cap);
8575 else
8576 nv_operator(cap);
8577}
8578
8579/*
8580 * Handle an operator command.
8581 * The actual work is done by do_pending_operator().
8582 */
8583 static void
8584nv_operator(cap)
8585 cmdarg_T *cap;
8586{
8587 int op_type;
8588
8589 op_type = get_op_type(cap->cmdchar, cap->nchar);
8590
8591 if (op_type == cap->oap->op_type) /* double operator works on lines */
8592 nv_lineop(cap);
8593 else if (!checkclearop(cap->oap))
8594 {
8595 cap->oap->start = curwin->w_cursor;
8596 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008597#ifdef FEAT_EVAL
8598 set_op_var(op_type);
8599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 }
8601}
8602
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008603#ifdef FEAT_EVAL
8604/*
8605 * Set v:operator to the characters for "optype".
8606 */
8607 static void
8608set_op_var(optype)
8609 int optype;
8610{
8611 char_u opchars[3];
8612
8613 if (optype == OP_NOP)
8614 set_vim_var_string(VV_OP, NULL, 0);
8615 else
8616 {
8617 opchars[0] = get_op_char(optype);
8618 opchars[1] = get_extra_op_char(optype);
8619 opchars[2] = NUL;
8620 set_vim_var_string(VV_OP, opchars, -1);
8621 }
8622}
8623#endif
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625/*
8626 * Handle linewise operator "dd", "yy", etc.
8627 *
8628 * "_" is is a strange motion command that helps make operators more logical.
8629 * It is actually implemented, but not documented in the real Vi. This motion
8630 * command actually refers to "the current line". Commands like "dd" and "yy"
8631 * are really an alternate form of "d_" and "y_". It does accept a count, so
8632 * "d3_" works to delete 3 lines.
8633 */
8634 static void
8635nv_lineop(cap)
8636 cmdarg_T *cap;
8637{
8638 cap->oap->motion_type = MLINE;
8639 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8640 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008641 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8642 && cap->oap->motion_force != 'v'
8643 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 || cap->oap->op_type == OP_LSHIFT
8645 || cap->oap->op_type == OP_RSHIFT)
8646 beginline(BL_SOL | BL_FIX);
8647 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8648 beginline(BL_WHITE | BL_FIX);
8649}
8650
8651/*
8652 * <Home> command.
8653 */
8654 static void
8655nv_home(cap)
8656 cmdarg_T *cap;
8657{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008658 /* CTRL-HOME is like "gg" */
8659 if (mod_mask & MOD_MASK_CTRL)
8660 nv_goto(cap);
8661 else
8662 {
8663 cap->count0 = 1;
8664 nv_pipe(cap);
8665 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008666 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8667 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
8670/*
8671 * "|" command.
8672 */
8673 static void
8674nv_pipe(cap)
8675 cmdarg_T *cap;
8676{
8677 cap->oap->motion_type = MCHAR;
8678 cap->oap->inclusive = FALSE;
8679 beginline(0);
8680 if (cap->count0 > 0)
8681 {
8682 coladvance((colnr_T)(cap->count0 - 1));
8683 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8684 }
8685 else
8686 curwin->w_curswant = 0;
8687 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008688 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 curwin->w_set_curswant = FALSE;
8690}
8691
8692/*
8693 * Handle back-word command "b" and "B".
8694 * cap->arg is 1 for "B"
8695 */
8696 static void
8697nv_bck_word(cap)
8698 cmdarg_T *cap;
8699{
8700 cap->oap->motion_type = MCHAR;
8701 cap->oap->inclusive = FALSE;
8702 curwin->w_set_curswant = TRUE;
8703 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8704 clearopbeep(cap->oap);
8705#ifdef FEAT_FOLDING
8706 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8707 foldOpenCursor();
8708#endif
8709}
8710
8711/*
8712 * Handle word motion commands "e", "E", "w" and "W".
8713 * cap->arg is TRUE for "E" and "W".
8714 */
8715 static void
8716nv_wordcmd(cap)
8717 cmdarg_T *cap;
8718{
8719 int n;
8720 int word_end;
8721 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008722 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 /*
8725 * Set inclusive for the "E" and "e" command.
8726 */
8727 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8728 word_end = TRUE;
8729 else
8730 word_end = FALSE;
8731 cap->oap->inclusive = word_end;
8732
8733 /*
8734 * "cw" and "cW" are a special case.
8735 */
8736 if (!word_end && cap->oap->op_type == OP_CHANGE)
8737 {
8738 n = gchar_cursor();
8739 if (n != NUL) /* not an empty line */
8740 {
8741 if (vim_iswhite(n))
8742 {
8743 /*
8744 * Reproduce a funny Vi behaviour: "cw" on a blank only
8745 * changes one character, not all blanks until the start of
8746 * the next word. Only do this when the 'w' flag is included
8747 * in 'cpoptions'.
8748 */
8749 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8750 {
8751 cap->oap->inclusive = TRUE;
8752 cap->oap->motion_type = MCHAR;
8753 return;
8754 }
8755 }
8756 else
8757 {
8758 /*
8759 * This is a little strange. To match what the real Vi does,
8760 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8761 * that we are not on a space or a TAB. This seems impolite
8762 * at first, but it's really more what we mean when we say
8763 * 'cw'.
8764 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008765 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 * will change only one character! This is done by setting
8767 * flag.
8768 */
8769 cap->oap->inclusive = TRUE;
8770 word_end = TRUE;
8771 flag = TRUE;
8772 }
8773 }
8774 }
8775
8776 cap->oap->motion_type = MCHAR;
8777 curwin->w_set_curswant = TRUE;
8778 if (word_end)
8779 n = end_word(cap->count1, cap->arg, flag, FALSE);
8780 else
8781 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8782
Bram Moolenaardfefb982008-04-01 10:06:39 +00008783 /* Don't leave the cursor on the NUL past the end of line. Unless we
8784 * didn't move it forward. */
8785 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008786 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
8788 if (n == FAIL && cap->oap->op_type == OP_NOP)
8789 clearopbeep(cap->oap);
8790 else
8791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#ifdef FEAT_FOLDING
8794 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8795 foldOpenCursor();
8796#endif
8797 }
8798}
8799
8800/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008801 * Used after a movement command: If the cursor ends up on the NUL after the
8802 * end of the line, may move it back to the last character and make the motion
8803 * inclusive.
8804 */
8805 static void
8806adjust_cursor(oap)
8807 oparg_T *oap;
8808{
8809 /* The cursor cannot remain on the NUL when:
8810 * - the column is > 0
8811 * - not in Visual mode or 'selection' is "o"
8812 * - 'virtualedit' is not "all" and not "onemore".
8813 */
8814 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008815 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008816#ifdef FEAT_VIRTUALEDIT
8817 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8818#endif
8819 )
8820 {
8821 --curwin->w_cursor.col;
8822#ifdef FEAT_MBYTE
8823 /* prevent cursor from moving on the trail byte */
8824 if (has_mbyte)
8825 mb_adjust_cursor();
8826#endif
8827 oap->inclusive = TRUE;
8828 }
8829}
8830
8831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 * "0" and "^" commands.
8833 * cap->arg is the argument for beginline().
8834 */
8835 static void
8836nv_beginline(cap)
8837 cmdarg_T *cap;
8838{
8839 cap->oap->motion_type = MCHAR;
8840 cap->oap->inclusive = FALSE;
8841 beginline(cap->arg);
8842#ifdef FEAT_FOLDING
8843 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8844 foldOpenCursor();
8845#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008846 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8847 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * In exclusive Visual mode, may include the last character.
8852 */
8853 static void
8854adjust_for_sel(cap)
8855 cmdarg_T *cap;
8856{
8857 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8858 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8859 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008860#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (has_mbyte)
8862 inc_cursor();
8863 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 ++curwin->w_cursor.col;
8866 cap->oap->inclusive = FALSE;
8867 }
8868}
8869
8870/*
8871 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8872 * Should check VIsual_mode before calling this.
8873 * Returns TRUE when backed up to the previous line.
8874 */
8875 static int
8876unadjust_for_sel()
8877{
8878 pos_T *pp;
8879
8880 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8881 {
8882 if (lt(VIsual, curwin->w_cursor))
8883 pp = &curwin->w_cursor;
8884 else
8885 pp = &VIsual;
8886#ifdef FEAT_VIRTUALEDIT
8887 if (pp->coladd > 0)
8888 --pp->coladd;
8889 else
8890#endif
8891 if (pp->col > 0)
8892 {
8893 --pp->col;
8894#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008895 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896#endif
8897 }
8898 else if (pp->lnum > 1)
8899 {
8900 --pp->lnum;
8901 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8902 return TRUE;
8903 }
8904 }
8905 return FALSE;
8906}
8907
8908/*
8909 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8910 */
8911 static void
8912nv_select(cap)
8913 cmdarg_T *cap;
8914{
8915 if (VIsual_active)
8916 VIsual_select = TRUE;
8917 else if (VIsual_reselect)
8918 {
8919 cap->nchar = 'v'; /* fake "gv" command */
8920 cap->arg = TRUE;
8921 nv_g_cmd(cap);
8922 }
8923}
8924
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925
8926/*
8927 * "G", "gg", CTRL-END, CTRL-HOME.
8928 * cap->arg is TRUE for "G".
8929 */
8930 static void
8931nv_goto(cap)
8932 cmdarg_T *cap;
8933{
8934 linenr_T lnum;
8935
8936 if (cap->arg)
8937 lnum = curbuf->b_ml.ml_line_count;
8938 else
8939 lnum = 1L;
8940 cap->oap->motion_type = MLINE;
8941 setpcmark();
8942
8943 /* When a count is given, use it instead of the default lnum */
8944 if (cap->count0 != 0)
8945 lnum = cap->count0;
8946 if (lnum < 1L)
8947 lnum = 1L;
8948 else if (lnum > curbuf->b_ml.ml_line_count)
8949 lnum = curbuf->b_ml.ml_line_count;
8950 curwin->w_cursor.lnum = lnum;
8951 beginline(BL_SOL | BL_FIX);
8952#ifdef FEAT_FOLDING
8953 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8954 foldOpenCursor();
8955#endif
8956}
8957
8958/*
8959 * CTRL-\ in Normal mode.
8960 */
8961 static void
8962nv_normal(cap)
8963 cmdarg_T *cap;
8964{
8965 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8966 {
8967 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008968 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 clear_cmdline = TRUE; /* unshow mode later */
8970 restart_edit = 0;
8971#ifdef FEAT_CMDWIN
8972 if (cmdwin_type != 0)
8973 cmdwin_result = Ctrl_C;
8974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (VIsual_active)
8976 {
8977 end_visual_mode(); /* stop Visual */
8978 redraw_curbuf_later(INVERTED);
8979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8981 if (cap->nchar == Ctrl_G && p_im)
8982 restart_edit = 'a';
8983 }
8984 else
8985 clearopbeep(cap->oap);
8986}
8987
8988/*
8989 * ESC in Normal mode: beep, but don't flush buffers.
8990 * Don't even beep if we are canceling a command.
8991 */
8992 static void
8993nv_esc(cap)
8994 cmdarg_T *cap;
8995{
8996 int no_reason;
8997
8998 no_reason = (cap->oap->op_type == OP_NOP
8999 && cap->opcount == 0
9000 && cap->count0 == 0
9001 && cap->oap->regname == 0
9002 && !p_im);
9003
9004 if (cap->arg) /* TRUE for CTRL-C */
9005 {
9006 if (restart_edit == 0
9007#ifdef FEAT_CMDWIN
9008 && cmdwin_type == 0
9009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 && no_reason)
9012 MSG(_("Type :quit<Enter> to exit Vim"));
9013
9014 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9015 * set again below when halfway a mapping. */
9016 if (!p_im)
9017 restart_edit = 0;
9018#ifdef FEAT_CMDWIN
9019 if (cmdwin_type != 0)
9020 {
9021 cmdwin_result = K_IGNORE;
9022 got_int = FALSE; /* don't stop executing autocommands et al. */
9023 return;
9024 }
9025#endif
9026 }
9027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 if (VIsual_active)
9029 {
9030 end_visual_mode(); /* stop Visual */
9031 check_cursor_col(); /* make sure cursor is not beyond EOL */
9032 curwin->w_set_curswant = TRUE;
9033 redraw_curbuf_later(INVERTED);
9034 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009035 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009036 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 clearop(cap->oap);
9038
9039 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9040 * set return to Insert mode afterwards. */
9041 if (restart_edit == 0 && goto_im()
9042#ifdef FEAT_EX_EXTRA
9043 && ex_normal_busy == 0
9044#endif
9045 )
9046 restart_edit = 'a';
9047}
9048
9049/*
9050 * Handle "A", "a", "I", "i" and <Insert> commands.
9051 */
9052 static void
9053nv_edit(cap)
9054 cmdarg_T *cap;
9055{
9056 /* <Insert> is equal to "i" */
9057 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9058 cap->cmdchar = 'i';
9059
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 /* in Visual mode "A" and "I" are an operator */
9061 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9062 v_visop(cap);
9063
9064 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009065 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9066 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 {
9068#ifdef FEAT_TEXTOBJ
9069 nv_object(cap);
9070#else
9071 clearopbeep(cap->oap);
9072#endif
9073 }
9074 else if (!curbuf->b_p_ma && !p_im)
9075 {
9076 /* Only give this error when 'insertmode' is off. */
9077 EMSG(_(e_modifiable));
9078 clearop(cap->oap);
9079 }
9080 else if (!checkclearopq(cap->oap))
9081 {
9082 switch (cap->cmdchar)
9083 {
9084 case 'A': /* "A"ppend after the line */
9085 curwin->w_set_curswant = TRUE;
9086#ifdef FEAT_VIRTUALEDIT
9087 if (ve_flags == VE_ALL)
9088 {
9089 int save_State = State;
9090
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009091 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 * character past the end of the line */
9093 State = INSERT;
9094 coladvance((colnr_T)MAXCOL);
9095 State = save_State;
9096 }
9097 else
9098#endif
9099 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9100 break;
9101
9102 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009103 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9104 beginline(BL_WHITE);
9105 else
9106 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 break;
9108
9109 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9110#ifdef FEAT_VIRTUALEDIT
9111 /* increment coladd when in virtual space, increment the
9112 * column otherwise, also to append after an unprintable char */
9113 if (virtual_active()
9114 && (curwin->w_cursor.coladd > 0
9115 || *ml_get_cursor() == NUL
9116 || *ml_get_cursor() == TAB))
9117 curwin->w_cursor.coladd++;
9118 else
9119#endif
9120 if (*ml_get_cursor() != NUL)
9121 inc_cursor();
9122 break;
9123 }
9124
9125#ifdef FEAT_VIRTUALEDIT
9126 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9127 {
9128 int save_State = State;
9129
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009130 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 * character past the end of the line */
9132 State = INSERT;
9133 coladvance(getviscol());
9134 State = save_State;
9135 }
9136#endif
9137
9138 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9139 }
9140}
9141
9142/*
9143 * Invoke edit() and take care of "restart_edit" and the return value.
9144 */
9145 static void
9146invoke_edit(cap, repl, cmd, startln)
9147 cmdarg_T *cap;
9148 int repl; /* "r" or "gr" command */
9149 int cmd;
9150 int startln;
9151{
9152 int restart_edit_save = 0;
9153
9154 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9155 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9156 * it. */
9157 if (repl || !stuff_empty())
9158 restart_edit_save = restart_edit;
9159 else
9160 restart_edit_save = 0;
9161
9162 /* Always reset "restart_edit", this is not a restarted edit. */
9163 restart_edit = 0;
9164
9165 if (edit(cmd, startln, cap->count1))
9166 cap->retval |= CA_COMMAND_BUSY;
9167
9168 if (restart_edit == 0)
9169 restart_edit = restart_edit_save;
9170}
9171
9172#ifdef FEAT_TEXTOBJ
9173/*
9174 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9175 */
9176 static void
9177nv_object(cap)
9178 cmdarg_T *cap;
9179{
9180 int flag;
9181 int include;
9182 char_u *mps_save;
9183
9184 if (cap->cmdchar == 'i')
9185 include = FALSE; /* "ix" = inner object: exclude white space */
9186 else
9187 include = TRUE; /* "ax" = an object: include white space */
9188
9189 /* Make sure (), [], {} and <> are in 'matchpairs' */
9190 mps_save = curbuf->b_p_mps;
9191 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9192
9193 switch (cap->nchar)
9194 {
9195 case 'w': /* "aw" = a word */
9196 flag = current_word(cap->oap, cap->count1, include, FALSE);
9197 break;
9198 case 'W': /* "aW" = a WORD */
9199 flag = current_word(cap->oap, cap->count1, include, TRUE);
9200 break;
9201 case 'b': /* "ab" = a braces block */
9202 case '(':
9203 case ')':
9204 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9205 break;
9206 case 'B': /* "aB" = a Brackets block */
9207 case '{':
9208 case '}':
9209 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9210 break;
9211 case '[': /* "a[" = a [] block */
9212 case ']':
9213 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9214 break;
9215 case '<': /* "a<" = a <> block */
9216 case '>':
9217 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9218 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009219 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009220 /* Do not adjust oap->end in do_pending_operator()
9221 * otherwise there are different results for 'dit'
9222 * (note leading whitespace in last line):
9223 * 1) <b> 2) <b>
9224 * foobar foobar
9225 * </b> </b>
9226 */
9227 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009228 flag = current_tagblock(cap->oap, cap->count1, include);
9229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 case 'p': /* "ap" = a paragraph */
9231 flag = current_par(cap->oap, cap->count1, include, 'p');
9232 break;
9233 case 's': /* "as" = a sentence */
9234 flag = current_sent(cap->oap, cap->count1, include);
9235 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009236 case '"': /* "a"" = a double quoted string */
9237 case '\'': /* "a'" = a single quoted string */
9238 case '`': /* "a`" = a backtick quoted string */
9239 flag = current_quote(cap->oap, cap->count1, include,
9240 cap->nchar);
9241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242#if 0 /* TODO */
9243 case 'S': /* "aS" = a section */
9244 case 'f': /* "af" = a filename */
9245 case 'u': /* "au" = a URL */
9246#endif
9247 default:
9248 flag = FAIL;
9249 break;
9250 }
9251
9252 curbuf->b_p_mps = mps_save;
9253 if (flag == FAIL)
9254 clearopbeep(cap->oap);
9255 adjust_cursor_col();
9256 curwin->w_set_curswant = TRUE;
9257}
9258#endif
9259
9260/*
9261 * "q" command: Start/stop recording.
9262 * "q:", "q/", "q?": edit command-line in command-line window.
9263 */
9264 static void
9265nv_record(cap)
9266 cmdarg_T *cap;
9267{
9268 if (cap->oap->op_type == OP_FORMAT)
9269 {
9270 /* "gqq" is the same as "gqgq": format line */
9271 cap->cmdchar = 'g';
9272 cap->nchar = 'q';
9273 nv_operator(cap);
9274 }
9275 else if (!checkclearop(cap->oap))
9276 {
9277#ifdef FEAT_CMDWIN
9278 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9279 {
9280 stuffcharReadbuff(cap->nchar);
9281 stuffcharReadbuff(K_CMDWIN);
9282 }
9283 else
9284#endif
9285 /* (stop) recording into a named register, unless executing a
9286 * register */
9287 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9288 clearopbeep(cap->oap);
9289 }
9290}
9291
9292/*
9293 * Handle the "@r" command.
9294 */
9295 static void
9296nv_at(cap)
9297 cmdarg_T *cap;
9298{
9299 if (checkclearop(cap->oap))
9300 return;
9301#ifdef FEAT_EVAL
9302 if (cap->nchar == '=')
9303 {
9304 if (get_expr_register() == NUL)
9305 return;
9306 }
9307#endif
9308 while (cap->count1-- && !got_int)
9309 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009310 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 {
9312 clearopbeep(cap->oap);
9313 break;
9314 }
9315 line_breakcheck();
9316 }
9317}
9318
9319/*
9320 * Handle the CTRL-U and CTRL-D commands.
9321 */
9322 static void
9323nv_halfpage(cap)
9324 cmdarg_T *cap;
9325{
9326 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9327 || (cap->cmdchar == Ctrl_D
9328 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9329 clearopbeep(cap->oap);
9330 else if (!checkclearop(cap->oap))
9331 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9332}
9333
9334/*
9335 * Handle "J" or "gJ" command.
9336 */
9337 static void
9338nv_join(cap)
9339 cmdarg_T *cap;
9340{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 if (VIsual_active) /* join the visual lines */
9342 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009343 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
9345 if (cap->count0 <= 1)
9346 cap->count0 = 2; /* default for join is two lines! */
9347 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9348 curbuf->b_ml.ml_line_count)
9349 clearopbeep(cap->oap); /* beyond last line */
9350 else
9351 {
9352 prep_redo(cap->oap->regname, cap->count0,
9353 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009354 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 }
9356 }
9357}
9358
9359/*
9360 * "P", "gP", "p" and "gp" commands.
9361 */
9362 static void
9363nv_put(cap)
9364 cmdarg_T *cap;
9365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 int regname = 0;
9367 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009368 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009369 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 int dir;
9371 int flags = 0;
9372
9373 if (cap->oap->op_type != OP_NOP)
9374 {
9375#ifdef FEAT_DIFF
9376 /* "dp" is ":diffput" */
9377 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9378 {
9379 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009380 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 }
9382 else
9383#endif
9384 clearopbeep(cap->oap);
9385 }
9386 else
9387 {
9388 dir = (cap->cmdchar == 'P'
9389 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9390 ? BACKWARD : FORWARD;
9391 prep_redo_cmd(cap);
9392 if (cap->cmdchar == 'g')
9393 flags |= PUT_CURSEND;
9394
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 if (VIsual_active)
9396 {
9397 /* Putting in Visual mode: The put text replaces the selected
9398 * text. First delete the selected text, then put the new text.
9399 * Need to save and restore the registers that the delete
9400 * overwrites if the old contents is being put.
9401 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009402 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009404#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009406#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009407 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009408 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009409#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412
9413 )
9414 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009415 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 * put, save it first. */
9417 reg1 = get_register(regname, TRUE);
9418 }
9419
9420 /* Now delete the selected text. */
9421 cap->cmdchar = 'd';
9422 cap->nchar = NUL;
9423 cap->oap->regname = NUL;
9424 nv_operator(cap);
9425 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009426 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
9428 /* delete PUT_LINE_BACKWARD; */
9429 cap->oap->regname = regname;
9430
9431 if (reg1 != NULL)
9432 {
9433 /* Delete probably changed the register we want to put, save
9434 * it first. Then put back what was there before the delete. */
9435 reg2 = get_register(regname, FALSE);
9436 put_register(regname, reg1);
9437 }
9438
9439 /* When deleted a linewise Visual area, put the register as
9440 * lines to avoid it joined with the next line. When deletion was
9441 * characterwise, split a line when putting lines. */
9442 if (VIsual_mode == 'V')
9443 flags |= PUT_LINE;
9444 else if (VIsual_mode == 'v')
9445 flags |= PUT_LINE_SPLIT;
9446 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9447 flags |= PUT_LINE_FORWARD;
9448 dir = BACKWARD;
9449 if ((VIsual_mode != 'V'
9450 && curwin->w_cursor.col < curbuf->b_op_start.col)
9451 || (VIsual_mode == 'V'
9452 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9453 /* cursor is at the end of the line or end of file, put
9454 * forward. */
9455 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009456 /* May have been reset in do_put(). */
9457 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 do_put(cap->oap->regname, dir, cap->count1, flags);
9460
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 /* If a register was saved, put it back now. */
9462 if (reg2 != NULL)
9463 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009464
9465 /* What to reselect with "gv"? Selecting the just put text seems to
9466 * be the most useful, since the original text was removed. */
9467 if (was_visual)
9468 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009469 curbuf->b_visual.vi_start = curbuf->b_op_start;
9470 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009471 /* need to adjust cursor position */
9472 if (*p_sel == 'e')
9473 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009474 }
9475
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009476 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009477 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009478 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009479 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009480 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009481
9482 /* If the cursor was in that line, move it to the end of the last
9483 * line. */
9484 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9485 {
9486 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9487 coladvance((colnr_T)MAXCOL);
9488 }
9489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 auto_format(FALSE, TRUE);
9491 }
9492}
9493
9494/*
9495 * "o" and "O" commands.
9496 */
9497 static void
9498nv_open(cap)
9499 cmdarg_T *cap;
9500{
9501#ifdef FEAT_DIFF
9502 /* "do" is ":diffget" */
9503 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9504 {
9505 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009506 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 }
9508 else
9509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 if (VIsual_active) /* switch start and end of visual */
9511 v_swap_corners(cap->cmdchar);
9512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 n_opencmd(cap);
9514}
9515
9516#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 static void
9518nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009519 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 ProcessSniffRequests();
9522}
9523#endif
9524
9525#ifdef FEAT_NETBEANS_INTG
9526 static void
9527nv_nbcmd(cap)
9528 cmdarg_T *cap;
9529{
9530 netbeans_keycommand(cap->nchar);
9531}
9532#endif
9533
9534#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 static void
9536nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009537 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9540}
9541#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009542
9543#ifdef FEAT_AUTOCMD
9544/*
9545 * Trigger CursorHold event.
9546 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9547 * input buffer. "did_cursorhold" is set to avoid retriggering.
9548 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009549 static void
9550nv_cursorhold(cap)
9551 cmdarg_T *cap;
9552{
9553 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9554 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009555 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009556}
9557#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009558
9559/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009560 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009561 */
9562 static void
9563get_op_vcol(oap, redo_VIsual_vcol, initial)
9564 oparg_T *oap;
9565 colnr_T redo_VIsual_vcol;
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009566 int initial; /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009567{
9568 colnr_T start, end;
9569
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009570 if (VIsual_mode != Ctrl_V
9571 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009572 return;
9573
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009574 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009575
9576#ifdef FEAT_MBYTE
9577 /* prevent from moving onto a trail byte */
9578 if (has_mbyte)
9579 mb_adjustpos(curwin->w_buffer, &oap->end);
9580#endif
9581
9582 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009583
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009584 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009585 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009586 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9587
9588 if (start < oap->start_vcol)
9589 oap->start_vcol = start;
9590 if (end > oap->end_vcol)
9591 {
9592 if (initial && *p_sel == 'e' && start >= 1
9593 && start - 1 >= oap->end_vcol)
9594 oap->end_vcol = start - 1;
9595 else
9596 oap->end_vcol = end;
9597 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009598 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009599
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009600 /* if '$' was used, get oap->end_vcol from longest line */
9601 if (curwin->w_curswant == MAXCOL)
9602 {
9603 curwin->w_cursor.col = MAXCOL;
9604 oap->end_vcol = 0;
9605 for (curwin->w_cursor.lnum = oap->start.lnum;
9606 curwin->w_cursor.lnum <= oap->end.lnum;
9607 ++curwin->w_cursor.lnum)
9608 {
9609 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9610 if (end > oap->end_vcol)
9611 oap->end_vcol = end;
9612 }
9613 }
9614 else if (redo_VIsual_busy)
9615 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9616 /*
9617 * Correct oap->end.col and oap->start.col to be the
9618 * upper-left and lower-right corner of the block area.
9619 *
9620 * (Actually, this does convert column positions into character
9621 * positions)
9622 */
9623 curwin->w_cursor.lnum = oap->end.lnum;
9624 coladvance(oap->end_vcol);
9625 oap->end = curwin->w_cursor;
9626
9627 curwin->w_cursor = oap->start;
9628 coladvance(oap->start_vcol);
9629 oap->start = curwin->w_cursor;
9630}