blob: 8d8be9b0cb18f4ee6e2d08217cdb9b924b461f97 [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 Moolenaar071d4272004-06-13 20:20:40 +000050#ifdef FEAT_CMDL_INFO
51static void del_from_showcmd __ARGS((int));
52#endif
53
54/*
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
58 */
59static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000060static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void nv_error __ARGS((cmdarg_T *cap));
62static void nv_help __ARGS((cmdarg_T *cap));
63static void nv_addsub __ARGS((cmdarg_T *cap));
64static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000065static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67#ifdef FEAT_MOUSE
68static void nv_mousescroll __ARGS((cmdarg_T *cap));
69static void nv_mouse __ARGS((cmdarg_T *cap));
70#endif
71static void nv_scroll_line __ARGS((cmdarg_T *cap));
72static void nv_zet __ARGS((cmdarg_T *cap));
73#ifdef FEAT_GUI
74static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000077#ifdef FEAT_GUI_TABLINE
78static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000079static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static void nv_exmode __ARGS((cmdarg_T *cap));
82static void nv_colon __ARGS((cmdarg_T *cap));
83static void nv_ctrlg __ARGS((cmdarg_T *cap));
84static void nv_ctrlh __ARGS((cmdarg_T *cap));
85static void nv_clear __ARGS((cmdarg_T *cap));
86static void nv_ctrlo __ARGS((cmdarg_T *cap));
87static void nv_hat __ARGS((cmdarg_T *cap));
88static void nv_Zet __ARGS((cmdarg_T *cap));
89static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void nv_tagpop __ARGS((cmdarg_T *cap));
91static void nv_scroll __ARGS((cmdarg_T *cap));
92static void nv_right __ARGS((cmdarg_T *cap));
93static void nv_left __ARGS((cmdarg_T *cap));
94static void nv_up __ARGS((cmdarg_T *cap));
95static void nv_down __ARGS((cmdarg_T *cap));
96#ifdef FEAT_SEARCHPATH
97static void nv_gotofile __ARGS((cmdarg_T *cap));
98#endif
99static void nv_end __ARGS((cmdarg_T *cap));
100static void nv_dollar __ARGS((cmdarg_T *cap));
101static void nv_search __ARGS((cmdarg_T *cap));
102static void nv_next __ARGS((cmdarg_T *cap));
Bram Moolenaar46539112015-02-17 15:43:57 +0100103static int normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static void nv_csearch __ARGS((cmdarg_T *cap));
105static void nv_brackets __ARGS((cmdarg_T *cap));
106static void nv_percent __ARGS((cmdarg_T *cap));
107static void nv_brace __ARGS((cmdarg_T *cap));
108static void nv_mark __ARGS((cmdarg_T *cap));
109static void nv_findpar __ARGS((cmdarg_T *cap));
110static void nv_undo __ARGS((cmdarg_T *cap));
111static void nv_kundo __ARGS((cmdarg_T *cap));
112static void nv_Replace __ARGS((cmdarg_T *cap));
113#ifdef FEAT_VREPLACE
114static void nv_vreplace __ARGS((cmdarg_T *cap));
115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void v_swap_corners __ARGS((int cmdchar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static void nv_replace __ARGS((cmdarg_T *cap));
118static void n_swapchar __ARGS((cmdarg_T *cap));
119static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static void v_visop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121static void nv_subst __ARGS((cmdarg_T *cap));
122static void nv_abbrev __ARGS((cmdarg_T *cap));
123static void nv_optrans __ARGS((cmdarg_T *cap));
124static void nv_gomark __ARGS((cmdarg_T *cap));
125static void nv_pcmark __ARGS((cmdarg_T *cap));
126static void nv_regname __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static void nv_visual __ARGS((cmdarg_T *cap));
128static void n_start_visual_mode __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void nv_window __ARGS((cmdarg_T *cap));
130static void nv_suspend __ARGS((cmdarg_T *cap));
131static void nv_g_cmd __ARGS((cmdarg_T *cap));
132static void n_opencmd __ARGS((cmdarg_T *cap));
133static void nv_dot __ARGS((cmdarg_T *cap));
134static void nv_redo __ARGS((cmdarg_T *cap));
135static void nv_Undo __ARGS((cmdarg_T *cap));
136static void nv_tilde __ARGS((cmdarg_T *cap));
137static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000138#ifdef FEAT_EVAL
139static void set_op_var __ARGS((int optype));
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static void nv_lineop __ARGS((cmdarg_T *cap));
142static void nv_home __ARGS((cmdarg_T *cap));
143static void nv_pipe __ARGS((cmdarg_T *cap));
144static void nv_bck_word __ARGS((cmdarg_T *cap));
145static void nv_wordcmd __ARGS((cmdarg_T *cap));
146static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000147static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void adjust_for_sel __ARGS((cmdarg_T *cap));
149static int unadjust_for_sel __ARGS((void));
150static void nv_select __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void nv_goto __ARGS((cmdarg_T *cap));
152static void nv_normal __ARGS((cmdarg_T *cap));
153static void nv_esc __ARGS((cmdarg_T *oap));
154static void nv_edit __ARGS((cmdarg_T *cap));
155static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
156#ifdef FEAT_TEXTOBJ
157static void nv_object __ARGS((cmdarg_T *cap));
158#endif
159static void nv_record __ARGS((cmdarg_T *cap));
160static void nv_at __ARGS((cmdarg_T *cap));
161static void nv_halfpage __ARGS((cmdarg_T *cap));
162static void nv_join __ARGS((cmdarg_T *cap));
163static void nv_put __ARGS((cmdarg_T *cap));
164static void nv_open __ARGS((cmdarg_T *cap));
165#ifdef FEAT_SNIFF
166static void nv_sniff __ARGS((cmdarg_T *cap));
167#endif
168#ifdef FEAT_NETBEANS_INTG
169static void nv_nbcmd __ARGS((cmdarg_T *cap));
170#endif
171#ifdef FEAT_DND
172static void nv_drop __ARGS((cmdarg_T *cap));
173#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#ifdef FEAT_AUTOCMD
175static void nv_cursorhold __ARGS((cmdarg_T *cap));
176#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +0200177static void get_op_vcol __ARGS((oparg_T *oap, colnr_T col, int initial));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000179static char *e_noident = N_("E349: No identifier under cursor");
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181/*
182 * Function to be called for a Normal or Visual mode command.
183 * The argument is a cmdarg_T.
184 */
185typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
186
187/* Values for cmd_flags. */
188#define NV_NCH 0x01 /* may need to get a second char */
189#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
190#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
191#define NV_LANG 0x08 /* second char needs language adjustment */
192
193#define NV_SS 0x10 /* may start selection */
194#define NV_SSS 0x20 /* may start selection with shift modifier */
195#define NV_STS 0x40 /* may stop selection without shift modif. */
196#define NV_RL 0x80 /* 'rightleft' modifies command */
197#define NV_KEEPREG 0x100 /* don't clear regname */
198#define NV_NCW 0x200 /* not allowed in command-line window */
199
200/*
201 * Generally speaking, every Normal mode command should either clear any
202 * pending operator (with *clearop*()), or set the motion type variable
203 * oap->motion_type.
204 *
205 * When a cursor motion command is made, it is marked as being a character or
206 * line oriented motion. Then, if an operator is in effect, the operation
207 * becomes character or line oriented accordingly.
208 */
209
210/*
211 * This table contains one entry for every Normal or Visual mode command.
212 * The order doesn't matter, init_normal_cmds() will create a sorted index.
213 * It is faster when all keys from zero to '~' are present.
214 */
215static const struct nv_cmd
216{
217 int cmd_char; /* (first) command character */
218 nv_func_T cmd_func; /* function for this command */
219 short_u cmd_flags; /* NV_ flags */
220 short cmd_arg; /* value for ca.arg */
221} nv_cmds[] =
222{
223 {NUL, nv_error, 0, 0},
224 {Ctrl_A, nv_addsub, 0, 0},
225 {Ctrl_B, nv_page, NV_STS, BACKWARD},
226 {Ctrl_C, nv_esc, 0, TRUE},
227 {Ctrl_D, nv_halfpage, 0, 0},
228 {Ctrl_E, nv_scroll_line, 0, TRUE},
229 {Ctrl_F, nv_page, NV_STS, FORWARD},
230 {Ctrl_G, nv_ctrlg, 0, 0},
231 {Ctrl_H, nv_ctrlh, 0, 0},
232 {Ctrl_I, nv_pcmark, 0, 0},
233 {NL, nv_down, 0, FALSE},
234 {Ctrl_K, nv_error, 0, 0},
235 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000236 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {Ctrl_N, nv_down, NV_STS, FALSE},
238 {Ctrl_O, nv_ctrlo, 0, 0},
239 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000240 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {Ctrl_R, nv_redo, 0, 0},
242 {Ctrl_S, nv_ignore, 0, 0},
243 {Ctrl_T, nv_tagpop, NV_NCW, 0},
244 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 {Ctrl_V, nv_visual, 0, FALSE},
246 {'V', nv_visual, 0, FALSE},
247 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {Ctrl_W, nv_window, 0, 0},
249 {Ctrl_X, nv_addsub, 0, 0},
250 {Ctrl_Y, nv_scroll_line, 0, FALSE},
251 {Ctrl_Z, nv_suspend, 0, 0},
252 {ESC, nv_esc, 0, FALSE},
253 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
254 {Ctrl_RSB, nv_ident, NV_NCW, 0},
255 {Ctrl_HAT, nv_hat, NV_NCW, 0},
256 {Ctrl__, nv_error, 0, 0},
257 {' ', nv_right, 0, 0},
258 {'!', nv_operator, 0, 0},
259 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
260 {'#', nv_ident, 0, 0},
261 {'$', nv_dollar, 0, 0},
262 {'%', nv_percent, 0, 0},
263 {'&', nv_optrans, 0, 0},
264 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
265 {'(', nv_brace, 0, BACKWARD},
266 {')', nv_brace, 0, FORWARD},
267 {'*', nv_ident, 0, 0},
268 {'+', nv_down, 0, TRUE},
269 {',', nv_csearch, 0, TRUE},
270 {'-', nv_up, 0, TRUE},
271 {'.', nv_dot, NV_KEEPREG, 0},
272 {'/', nv_search, 0, FALSE},
273 {'0', nv_beginline, 0, 0},
274 {'1', nv_ignore, 0, 0},
275 {'2', nv_ignore, 0, 0},
276 {'3', nv_ignore, 0, 0},
277 {'4', nv_ignore, 0, 0},
278 {'5', nv_ignore, 0, 0},
279 {'6', nv_ignore, 0, 0},
280 {'7', nv_ignore, 0, 0},
281 {'8', nv_ignore, 0, 0},
282 {'9', nv_ignore, 0, 0},
283 {':', nv_colon, 0, 0},
284 {';', nv_csearch, 0, FALSE},
285 {'<', nv_operator, NV_RL, 0},
286 {'=', nv_operator, 0, 0},
287 {'>', nv_operator, NV_RL, 0},
288 {'?', nv_search, 0, FALSE},
289 {'@', nv_at, NV_NCH_NOP, FALSE},
290 {'A', nv_edit, 0, 0},
291 {'B', nv_bck_word, 0, 1},
292 {'C', nv_abbrev, NV_KEEPREG, 0},
293 {'D', nv_abbrev, NV_KEEPREG, 0},
294 {'E', nv_wordcmd, 0, TRUE},
295 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
296 {'G', nv_goto, 0, TRUE},
297 {'H', nv_scroll, 0, 0},
298 {'I', nv_edit, 0, 0},
299 {'J', nv_join, 0, 0},
300 {'K', nv_ident, 0, 0},
301 {'L', nv_scroll, 0, 0},
302 {'M', nv_scroll, 0, 0},
303 {'N', nv_next, 0, SEARCH_REV},
304 {'O', nv_open, 0, 0},
305 {'P', nv_put, 0, 0},
306 {'Q', nv_exmode, NV_NCW, 0},
307 {'R', nv_Replace, 0, FALSE},
308 {'S', nv_subst, NV_KEEPREG, 0},
309 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
310 {'U', nv_Undo, 0, 0},
311 {'W', nv_wordcmd, 0, TRUE},
312 {'X', nv_abbrev, NV_KEEPREG, 0},
313 {'Y', nv_abbrev, NV_KEEPREG, 0},
314 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
315 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
316 {'\\', nv_error, 0, 0},
317 {']', nv_brackets, NV_NCH_ALW, FORWARD},
318 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
319 {'_', nv_lineop, 0, 0},
320 {'`', nv_gomark, NV_NCH_ALW, FALSE},
321 {'a', nv_edit, NV_NCH, 0},
322 {'b', nv_bck_word, 0, 0},
323 {'c', nv_operator, 0, 0},
324 {'d', nv_operator, 0, 0},
325 {'e', nv_wordcmd, 0, FALSE},
326 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
327 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
328 {'h', nv_left, NV_RL, 0},
329 {'i', nv_edit, NV_NCH, 0},
330 {'j', nv_down, 0, FALSE},
331 {'k', nv_up, 0, FALSE},
332 {'l', nv_right, NV_RL, 0},
333 {'m', nv_mark, NV_NCH_NOP, 0},
334 {'n', nv_next, 0, 0},
335 {'o', nv_open, 0, 0},
336 {'p', nv_put, 0, 0},
337 {'q', nv_record, NV_NCH, 0},
338 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
339 {'s', nv_subst, NV_KEEPREG, 0},
340 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
341 {'u', nv_undo, 0, 0},
342 {'w', nv_wordcmd, 0, FALSE},
343 {'x', nv_abbrev, NV_KEEPREG, 0},
344 {'y', nv_operator, 0, 0},
345 {'z', nv_zet, NV_NCH_ALW, 0},
346 {'{', nv_findpar, 0, BACKWARD},
347 {'|', nv_pipe, 0, 0},
348 {'}', nv_findpar, 0, FORWARD},
349 {'~', nv_tilde, 0, 0},
350
351 /* pound sign */
352 {POUND, nv_ident, 0, 0},
353#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200354 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
355 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
356 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
357 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {K_LEFTMOUSE, nv_mouse, 0, 0},
359 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
360 {K_LEFTDRAG, nv_mouse, 0, 0},
361 {K_LEFTRELEASE, nv_mouse, 0, 0},
362 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
363 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
364 {K_MIDDLEDRAG, nv_mouse, 0, 0},
365 {K_MIDDLERELEASE, nv_mouse, 0, 0},
366 {K_RIGHTMOUSE, nv_mouse, 0, 0},
367 {K_RIGHTDRAG, nv_mouse, 0, 0},
368 {K_RIGHTRELEASE, nv_mouse, 0, 0},
369 {K_X1MOUSE, nv_mouse, 0, 0},
370 {K_X1DRAG, nv_mouse, 0, 0},
371 {K_X1RELEASE, nv_mouse, 0, 0},
372 {K_X2MOUSE, nv_mouse, 0, 0},
373 {K_X2DRAG, nv_mouse, 0, 0},
374 {K_X2RELEASE, nv_mouse, 0, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000377 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {K_INS, nv_edit, 0, 0},
379 {K_KINS, nv_edit, 0, 0},
380 {K_BS, nv_ctrlh, 0, 0},
381 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
382 {K_S_UP, nv_page, NV_SS, BACKWARD},
383 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
384 {K_S_DOWN, nv_page, NV_SS, FORWARD},
385 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
386 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
387 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
388 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
389 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
390 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
391 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
393 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
395 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
396 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {K_S_END, nv_end, NV_SS, FALSE},
398 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
399 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
400 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {K_S_HOME, nv_home, NV_SS, 0},
402 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
403 {K_DEL, nv_abbrev, 0, 0},
404 {K_KDEL, nv_abbrev, 0, 0},
405 {K_UNDO, nv_kundo, 0, 0},
406 {K_HELP, nv_help, NV_NCW, 0},
407 {K_F1, nv_help, NV_NCW, 0},
408 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#ifdef FEAT_GUI
411 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
412 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
413#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000414#ifdef FEAT_GUI_TABLINE
415 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000416 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#ifdef FEAT_FKMAP
419 {K_F8, farsi_fkey, 0, 0},
420 {K_F9, farsi_fkey, 0, 0},
421#endif
422#ifdef FEAT_SNIFF
423 {K_SNIFF, nv_sniff, 0, 0},
424#endif
425#ifdef FEAT_NETBEANS_INTG
426 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
427#endif
428#ifdef FEAT_DND
429 {K_DROP, nv_drop, NV_STS, 0},
430#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000431#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000432 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434};
435
436/* Number of commands in nv_cmds[]. */
437#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
438
439/* Sorted index of commands in nv_cmds[]. */
440static short nv_cmd_idx[NV_CMDS_SIZE];
441
442/* The highest index for which
443 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
444static int nv_max_linear;
445
446/*
447 * Compare functions for qsort() below, that checks the command character
448 * through the index in nv_cmd_idx[].
449 */
450 static int
451#ifdef __BORLANDC__
452_RTLENTRYF
453#endif
454nv_compare(s1, s2)
455 const void *s1;
456 const void *s2;
457{
458 int c1, c2;
459
460 /* The commands are sorted on absolute value. */
461 c1 = nv_cmds[*(const short *)s1].cmd_char;
462 c2 = nv_cmds[*(const short *)s2].cmd_char;
463 if (c1 < 0)
464 c1 = -c1;
465 if (c2 < 0)
466 c2 = -c2;
467 return c1 - c2;
468}
469
470/*
471 * Initialize the nv_cmd_idx[] table.
472 */
473 void
474init_normal_cmds()
475{
476 int i;
477
478 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000479 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 nv_cmd_idx[i] = i;
481
482 /* Sort the commands by the command character. */
483 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
484
485 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000486 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
488 break;
489 nv_max_linear = i - 1;
490}
491
492/*
493 * Search for a command in the commands table.
494 * Returns -1 for invalid command.
495 */
496 static int
497find_command(cmdchar)
498 int cmdchar;
499{
500 int i;
501 int idx;
502 int top, bot;
503 int c;
504
505#ifdef FEAT_MBYTE
506 /* A multi-byte character is never a command. */
507 if (cmdchar >= 0x100)
508 return -1;
509#endif
510
511 /* We use the absolute value of the character. Special keys have a
512 * negative value, but are sorted on their absolute value. */
513 if (cmdchar < 0)
514 cmdchar = -cmdchar;
515
516 /* If the character is in the first part: The character is the index into
517 * nv_cmd_idx[]. */
518 if (cmdchar <= nv_max_linear)
519 return nv_cmd_idx[cmdchar];
520
521 /* Perform a binary search. */
522 bot = nv_max_linear + 1;
523 top = NV_CMDS_SIZE - 1;
524 idx = -1;
525 while (bot <= top)
526 {
527 i = (top + bot) / 2;
528 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
529 if (c < 0)
530 c = -c;
531 if (cmdchar == c)
532 {
533 idx = nv_cmd_idx[i];
534 break;
535 }
536 if (cmdchar > c)
537 bot = i + 1;
538 else
539 top = i - 1;
540 }
541 return idx;
542}
543
544/*
545 * Execute a command in Normal mode.
546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 void
548normal_cmd(oap, toplevel)
549 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000550 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 cmdarg_T ca; /* command arguments */
553 int c;
554 int ctrl_w = FALSE; /* got CTRL-W command */
555 int old_col = curwin->w_curswant;
556#ifdef FEAT_CMDL_INFO
557 int need_flushbuf; /* need to call out_flush() */
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 pos_T old_pos; /* cursor position before command */
560 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 static int old_mapped_len = 0;
562 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000563#ifdef FEAT_EVAL
564 int set_prevcount = FALSE;
565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
568 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000569
570 /* Use a count remembered from before entering an operator. After typing
571 * "3d" we return from normal_cmd() and come back here, the "3" is
572 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 ca.opcount = opcount;
574
575#ifdef FEAT_SNIFF
576 want_sniff_request = sniff_connected;
577#endif
578
579 /*
580 * If there is an operator pending, then the command we take this time
581 * will terminate it. Finish_op tells us to finish the operation before
582 * returning this time (unless the operation was cancelled).
583 */
584#ifdef CURSOR_SHAPE
585 c = finish_op;
586#endif
587 finish_op = (oap->op_type != OP_NOP);
588#ifdef CURSOR_SHAPE
589 if (finish_op != c)
590 {
591 ui_cursor_shape(); /* may show different cursor shape */
592# ifdef FEAT_MOUSESHAPE
593 update_mouseshape(-1);
594# endif
595 }
596#endif
597
Bram Moolenaara983fe92008-07-31 20:04:27 +0000598 /* When not finishing an operator and no register name typed, reset the
599 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000603#ifdef FEAT_EVAL
604 set_prevcount = TRUE;
605#endif
606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaara983fe92008-07-31 20:04:27 +0000608#ifdef FEAT_AUTOCMD
609 /* Restore counts from before receiving K_CURSORHOLD. This means after
610 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
611 * "3 * 2". */
612 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
613 {
614 ca.opcount = oap->prev_opcount;
615 ca.count0 = oap->prev_count0;
616 oap->prev_opcount = 0;
617 oap->prev_count0 = 0;
618 }
619#endif
620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 State = NORMAL_BUSY;
624#ifdef USE_ON_FLY_SCROLL
625 dont_scroll = FALSE; /* allow scrolling here */
626#endif
627
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100628#ifdef FEAT_EVAL
629 /* Set v:count here, when called from main() and not a stuffed
630 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100631 * when there is no count. Do set it for redo. */
632 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100633 set_vcount_ca(&ca, &set_prevcount);
634#endif
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /*
637 * Get the command character from the user.
638 */
639 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
642 /*
643 * If a mapping was started in Visual or Select mode, remember the length
644 * of the mapping. This is used below to not return to Insert mode for as
645 * long as the mapping is being executed.
646 */
647 if (restart_edit == 0)
648 old_mapped_len = 0;
649 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 old_mapped_len = typebuf_maplen();
652
653 if (c == NUL)
654 c = K_ZERO;
655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 /*
657 * In Select mode, typed text replaces the selection.
658 */
659 if (VIsual_active
660 && VIsual_select
661 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
662 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000663 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
664 * 'insertmode' is set) fake a "d"elete command, Insert mode will
665 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000666 * Insert the typed character in the typeahead buffer, so that it can
667 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000668 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000669 if (restart_edit != 0)
670 c = 'd';
671 else
672 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000673 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200674 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676
677#ifdef FEAT_CMDL_INFO
678 need_flushbuf = add_to_showcmd(c);
679#endif
680
681getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
684 /*
685 * Handle a count before a command and compute ca.count0.
686 * Note that '0' is a command and not the start of a count, but it's
687 * part of a count after other digits.
688 */
689 while ( (c >= '1' && c <= '9')
690 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
691 {
692 if (c == K_DEL || c == K_KDEL)
693 {
694 ca.count0 /= 10;
695#ifdef FEAT_CMDL_INFO
696 del_from_showcmd(4); /* delete the digit and ~@% */
697#endif
698 }
699 else
700 ca.count0 = ca.count0 * 10 + (c - '0');
701 if (ca.count0 < 0) /* got too large! */
702 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000703#ifdef FEAT_EVAL
704 /* Set v:count here, when called from main() and not a stuffed
705 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100706 * right after the count. Do set it for redo. */
707 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100708 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 if (ctrl_w)
711 {
712 ++no_mapping;
713 ++allow_keys; /* no mapping for nchar, but keys */
714 }
715 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000716 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 --no_zero_mapping;
719 if (ctrl_w)
720 {
721 --no_mapping;
722 --allow_keys;
723 }
724#ifdef FEAT_CMDL_INFO
725 need_flushbuf |= add_to_showcmd(c);
726#endif
727 }
728
729 /*
730 * If we got CTRL-W there may be a/another count
731 */
732 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
733 {
734 ctrl_w = TRUE;
735 ca.opcount = ca.count0; /* remember first count */
736 ca.count0 = 0;
737 ++no_mapping;
738 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000739 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 --no_mapping;
742 --allow_keys;
743#ifdef FEAT_CMDL_INFO
744 need_flushbuf |= add_to_showcmd(c);
745#endif
746 goto getcount; /* jump back */
747 }
748 }
749
Bram Moolenaara983fe92008-07-31 20:04:27 +0000750#ifdef FEAT_AUTOCMD
751 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000753 /* Save the count values so that ca.opcount and ca.count0 are exactly
754 * the same when coming back here after handling K_CURSORHOLD. */
755 oap->prev_opcount = ca.opcount;
756 oap->prev_count0 = ca.count0;
757 }
758 else
759#endif
760 if (ca.opcount != 0)
761 {
762 /*
763 * If we're in the middle of an operator (including after entering a
764 * yank buffer with '"') AND we had a count before the operator, then
765 * that count overrides the current value of ca.count0.
766 * What this means effectively, is that commands like "3dw" get turned
767 * into "d3w" which makes things fall into place pretty neatly.
768 * If you give a count before AND after the operator, they are
769 * multiplied.
770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (ca.count0)
772 ca.count0 *= ca.opcount;
773 else
774 ca.count0 = ca.opcount;
775 }
776
777 /*
778 * Always remember the count. It will be set to zero (on the next call,
779 * above) when there is no pending operator.
780 * When called from main(), save the count for use by the "count" built-in
781 * variable.
782 */
783 ca.opcount = ca.count0;
784 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
785
786#ifdef FEAT_EVAL
787 /*
788 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100789 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100791 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000792 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793#endif
794
795 /*
796 * Find the command character in the table of commands.
797 * For CTRL-W we already got nchar when looking for a count.
798 */
799 if (ctrl_w)
800 {
801 ca.nchar = c;
802 ca.cmdchar = Ctrl_W;
803 }
804 else
805 ca.cmdchar = c;
806 idx = find_command(ca.cmdchar);
807 if (idx < 0)
808 {
809 /* Not a known command: beep. */
810 clearopbeep(oap);
811 goto normal_end;
812 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000813
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000814 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200816 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000818 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 goto normal_end;
820 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000821#ifdef FEAT_AUTOCMD
822 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
823 goto normal_end;
824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 /*
827 * In Visual/Select mode, a few keys are handled in a special way.
828 */
829 if (VIsual_active)
830 {
831 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
832 if (km_stopsel
833 && (nv_cmds[idx].cmd_flags & NV_STS)
834 && !(mod_mask & MOD_MASK_SHIFT))
835 {
836 end_visual_mode();
837 redraw_curbuf_later(INVERTED);
838 }
839
840 /* Keys that work different when 'keymodel' contains "startsel" */
841 if (km_startsel)
842 {
843 if (nv_cmds[idx].cmd_flags & NV_SS)
844 {
845 unshift_special(&ca);
846 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 if (idx < 0)
848 {
849 /* Just in case */
850 clearopbeep(oap);
851 goto normal_end;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
855 && (mod_mask & MOD_MASK_SHIFT))
856 {
857 mod_mask &= ~MOD_MASK_SHIFT;
858 }
859 }
860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862#ifdef FEAT_RIGHTLEFT
863 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
864 && (nv_cmds[idx].cmd_flags & NV_RL))
865 {
866 /* Invert horizontal movements and operations. Only when typed by the
867 * user directly, not when the result of a mapping or "x" translated
868 * to "dl". */
869 switch (ca.cmdchar)
870 {
871 case 'l': ca.cmdchar = 'h'; break;
872 case K_RIGHT: ca.cmdchar = K_LEFT; break;
873 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
874 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
875 case 'h': ca.cmdchar = 'l'; break;
876 case K_LEFT: ca.cmdchar = K_RIGHT; break;
877 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
878 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
879 case '>': ca.cmdchar = '<'; break;
880 case '<': ca.cmdchar = '>'; break;
881 }
882 idx = find_command(ca.cmdchar);
883 }
884#endif
885
886 /*
887 * Get an additional character if we need one.
888 */
889 if ((nv_cmds[idx].cmd_flags & NV_NCH)
890 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
891 && oap->op_type == OP_NOP)
892 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
893 || (ca.cmdchar == 'q'
894 && oap->op_type == OP_NOP
895 && !Recording
896 && !Exec_reg)
897 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100898 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
900 int *cp;
901 int repl = FALSE; /* get character for replace mode */
902 int lit = FALSE; /* get extra character literally */
903 int langmap_active = FALSE; /* using :lmap mappings */
904 int lang; /* getting a text character */
905#ifdef USE_IM_CONTROL
906 int save_smd; /* saved value of p_smd */
907#endif
908
909 ++no_mapping;
910 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000911#ifdef FEAT_AUTOCMD
912 /* Don't generate a CursorHold event here, most commands can't handle
913 * it, e.g., nv_replace(), nv_csearch(). */
914 did_cursorhold = TRUE;
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 if (ca.cmdchar == 'g')
917 {
918 /*
919 * For 'g' get the next character now, so that we can check for
920 * "gr", "g'" and "g`".
921 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000922 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef FEAT_CMDL_INFO
925 need_flushbuf |= add_to_showcmd(ca.nchar);
926#endif
927 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100928 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 cp = &ca.extra_char; /* need to get a third character */
931 if (ca.nchar != 'r')
932 lit = TRUE; /* get it literally */
933 else
934 repl = TRUE; /* get it in replace mode */
935 }
936 else
937 cp = NULL; /* no third character needed */
938 }
939 else
940 {
941 if (ca.cmdchar == 'r') /* get it in replace mode */
942 repl = TRUE;
943 cp = &ca.nchar;
944 }
945 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
946
947 /*
948 * Get a second or third character.
949 */
950 if (cp != NULL)
951 {
952#ifdef CURSOR_SHAPE
953 if (repl)
954 {
955 State = REPLACE; /* pretend Replace mode */
956 ui_cursor_shape(); /* show different cursor shape */
957 }
958#endif
959 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
960 {
961 /* Allow mappings defined with ":lmap". */
962 --no_mapping;
963 --allow_keys;
964 if (repl)
965 State = LREPLACE;
966 else
967 State = LANGMAP;
968 langmap_active = TRUE;
969 }
970#ifdef USE_IM_CONTROL
971 save_smd = p_smd;
972 p_smd = FALSE; /* Don't let the IM code show the mode here */
973 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
974 im_set_active(TRUE);
975#endif
976
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000977 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
979 if (langmap_active)
980 {
981 /* Undo the decrement done above */
982 ++no_mapping;
983 ++allow_keys;
984 State = NORMAL_BUSY;
985 }
986#ifdef USE_IM_CONTROL
987 if (lang)
988 {
989 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
990 im_save_status(&curbuf->b_p_iminsert);
991 im_set_active(FALSE);
992 }
993 p_smd = save_smd;
994#endif
995#ifdef CURSOR_SHAPE
996 State = NORMAL_BUSY;
997#endif
998#ifdef FEAT_CMDL_INFO
999 need_flushbuf |= add_to_showcmd(*cp);
1000#endif
1001
1002 if (!lit)
1003 {
1004#ifdef FEAT_DIGRAPHS
1005 /* Typing CTRL-K gets a digraph. */
1006 if (*cp == Ctrl_K
1007 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1008 || cp == &ca.extra_char)
1009 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1010 {
1011 c = get_digraph(FALSE);
1012 if (c > 0)
1013 {
1014 *cp = c;
1015# ifdef FEAT_CMDL_INFO
1016 /* Guessing how to update showcmd here... */
1017 del_from_showcmd(3);
1018 need_flushbuf |= add_to_showcmd(*cp);
1019# endif
1020 }
1021 }
1022#endif
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 /* adjust chars > 127, except after "tTfFr" commands */
1025 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#ifdef FEAT_RIGHTLEFT
1027 /* adjust Hebrew mapped char */
1028 if (p_hkmap && lang && KeyTyped)
1029 *cp = hkmap(*cp);
1030# ifdef FEAT_FKMAP
1031 /* adjust Farsi mapped char */
1032 if (p_fkmap && lang && KeyTyped)
1033 *cp = fkmap(*cp);
1034# endif
1035#endif
1036 }
1037
1038 /*
1039 * When the next character is CTRL-\ a following CTRL-N means the
1040 * command is aborted and we go to Normal mode.
1041 */
1042 if (cp == &ca.extra_char
1043 && ca.nchar == Ctrl_BSL
1044 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1045 {
1046 ca.cmdchar = Ctrl_BSL;
1047 ca.nchar = ca.extra_char;
1048 idx = find_command(ca.cmdchar);
1049 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001050 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001051 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 else if (*cp == Ctrl_BSL)
1053 {
1054 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1055
1056 /* There is a busy wait here when typing "f<C-\>" and then
1057 * something different from CTRL-N. Can't be avoided. */
1058 while ((c = vpeekc()) <= 0 && towait > 0L)
1059 {
1060 do_sleep(towait > 50L ? 50L : towait);
1061 towait -= 50L;
1062 }
1063 if (c > 0)
1064 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001065 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (c != Ctrl_N && c != Ctrl_G)
1067 vungetc(c);
1068 else
1069 {
1070 ca.cmdchar = Ctrl_BSL;
1071 ca.nchar = c;
1072 idx = find_command(ca.cmdchar);
1073 }
1074 }
1075 }
1076
1077#ifdef FEAT_MBYTE
1078 /* When getting a text character and the next character is a
1079 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001080 * However, don't wait for it to arrive. Also, do enable mapping,
1081 * because if it's put back with vungetc() it's too late to apply
1082 * mapping. */
1083 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 while (enc_utf8 && lang && (c = vpeekc()) > 0
1085 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1086 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001087 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (!utf_iscomposing(c))
1089 {
1090 vungetc(c); /* it wasn't, put it back */
1091 break;
1092 }
1093 else if (ca.ncharC1 == 0)
1094 ca.ncharC1 = c;
1095 else
1096 ca.ncharC2 = c;
1097 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001098 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099#endif
1100 }
1101 --no_mapping;
1102 --allow_keys;
1103 }
1104
1105#ifdef FEAT_CMDL_INFO
1106 /*
1107 * Flush the showcmd characters onto the screen so we can see them while
1108 * the command is being executed. Only do this when the shown command was
1109 * actually displayed, otherwise this will slow down a lot when executing
1110 * mappings.
1111 */
1112 if (need_flushbuf)
1113 out_flush();
1114#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001115#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001116 if (ca.cmdchar != K_IGNORE)
1117 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120 State = NORMAL;
1121
1122 if (ca.nchar == ESC)
1123 {
1124 clearop(oap);
1125 if (restart_edit == 0 && goto_im())
1126 restart_edit = 'a';
1127 goto normal_end;
1128 }
1129
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001130 if (ca.cmdchar != K_IGNORE)
1131 {
1132 msg_didout = FALSE; /* don't scroll screen up for normal command */
1133 msg_col = 0;
1134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 old_pos = curwin->w_cursor; /* remember where cursor was */
1137
1138 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1139 * mode. */
1140 if (!VIsual_active && km_startsel)
1141 {
1142 if (nv_cmds[idx].cmd_flags & NV_SS)
1143 {
1144 start_selection();
1145 unshift_special(&ca);
1146 idx = find_command(ca.cmdchar);
1147 }
1148 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1149 && (mod_mask & MOD_MASK_SHIFT))
1150 {
1151 start_selection();
1152 mod_mask &= ~MOD_MASK_SHIFT;
1153 }
1154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156 /*
1157 * Execute the command!
1158 * Call the command function found in the commands table.
1159 */
1160 ca.arg = nv_cmds[idx].cmd_arg;
1161 (nv_cmds[idx].cmd_func)(&ca);
1162
1163 /*
1164 * If we didn't start or finish an operator, reset oap->regname, unless we
1165 * need it later.
1166 */
1167 if (!finish_op
1168 && !oap->op_type
1169 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1170 {
1171 clearop(oap);
1172#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001173 {
1174 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001175
Bram Moolenaar536681b2011-05-10 16:12:45 +02001176 /* Adjust the register according to 'clipboard', so that when
1177 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001178# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001179 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001180# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001181 set_reg_var(regname);
1182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#endif
1184 }
1185
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001186 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001187 * character or "z333<cr>". */
1188 if (old_mapped_len > 0)
1189 old_mapped_len = typebuf_maplen();
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 /*
1192 * If an operation is pending, handle it...
1193 */
1194 do_pending_operator(&ca, old_col, FALSE);
1195
1196 /*
1197 * Wait for a moment when a message is displayed that will be overwritten
1198 * by the mode message.
1199 * In Visual mode and with "^O" in Insert mode, a short message will be
1200 * overwritten by the mode message. Wait a bit, until a key is hit.
1201 * In Visual mode, it's more important to keep the Visual area updated
1202 * than keeping a message (e.g. from a /pat search).
1203 * Only do this if the command was typed, not from a mapping.
1204 * Don't wait when emsg_silent is non-zero.
1205 * Also wait a bit after an error message, e.g. for "^O:".
1206 * Don't redraw the screen, it would remove the message.
1207 */
1208 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001209 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 || (VIsual_active
1212 && old_pos.lnum == curwin->w_cursor.lnum
1213 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 )
1215 && (clear_cmdline
1216 || redraw_cmdline)
1217 && (msg_didout || (msg_didany && msg_scroll))
1218 && !msg_nowait
1219 && KeyTyped)
1220 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 && (msg_scroll
1223 || emsg_on_display)))
1224 && oap->regname == 0
1225 && !(ca.retval & CA_COMMAND_BUSY)
1226 && stuff_empty()
1227 && typebuf_typed()
1228 && emsg_silent == 0
1229 && !did_wait_return
1230 && oap->op_type == OP_NOP)
1231 {
1232 int save_State = State;
1233
1234 /* Draw the cursor with the right shape here */
1235 if (restart_edit != 0)
1236 State = INSERT;
1237
1238 /* If need to redraw, and there is a "keep_msg", redraw before the
1239 * delay */
1240 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1241 {
1242 char_u *kmsg;
1243
1244 kmsg = keep_msg;
1245 keep_msg = NULL;
1246 /* showmode() will clear keep_msg, but we want to use it anyway */
1247 update_screen(0);
1248 /* now reset it, otherwise it's put in the history again */
1249 keep_msg = kmsg;
1250 msg_attr(kmsg, keep_msg_attr);
1251 vim_free(kmsg);
1252 }
1253 setcursor();
1254 cursor_on();
1255 out_flush();
1256 if (msg_scroll || emsg_on_display)
1257 ui_delay(1000L, TRUE); /* wait at least one second */
1258 ui_delay(3000L, FALSE); /* wait up to three seconds */
1259 State = save_State;
1260
1261 msg_scroll = FALSE;
1262 emsg_on_display = FALSE;
1263 }
1264
1265 /*
1266 * Finish up after executing a Normal mode command.
1267 */
1268normal_end:
1269
1270 msg_nowait = FALSE;
1271
1272 /* Reset finish_op, in case it was set */
1273#ifdef CURSOR_SHAPE
1274 c = finish_op;
1275#endif
1276 finish_op = FALSE;
1277#ifdef CURSOR_SHAPE
1278 /* Redraw the cursor with another shape, if we were in Operator-pending
1279 * mode or did a replace command. */
1280 if (c || ca.cmdchar == 'r')
1281 {
1282 ui_cursor_shape(); /* may show different cursor shape */
1283# ifdef FEAT_MOUSESHAPE
1284 update_mouseshape(-1);
1285# endif
1286 }
1287#endif
1288
1289#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001290 if (oap->op_type == OP_NOP && oap->regname == 0
1291# ifdef FEAT_AUTOCMD
1292 && ca.cmdchar != K_CURSORHOLD
1293# endif
1294 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 clear_showcmd();
1296#endif
1297
1298 checkpcmark(); /* check if we moved since setting pcmark */
1299 vim_free(ca.searchbuf);
1300
1301#ifdef FEAT_MBYTE
1302 if (has_mbyte)
1303 mb_adjust_cursor();
1304#endif
1305
1306#ifdef FEAT_SCROLLBIND
1307 if (curwin->w_p_scb && toplevel)
1308 {
1309 validate_cursor(); /* may need to update w_leftcol */
1310 do_check_scrollbind(TRUE);
1311 }
1312#endif
1313
Bram Moolenaar860cae12010-06-05 23:22:07 +02001314#ifdef FEAT_CURSORBIND
1315 if (curwin->w_p_crb && toplevel)
1316 {
1317 validate_cursor(); /* may need to update w_leftcol */
1318 do_check_cursorbind();
1319 }
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 /*
1323 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1324 * if still inside a mapping that started in Visual mode).
1325 * May switch from Visual to Select mode after CTRL-O command.
1326 */
1327 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1329 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && !(ca.retval & CA_COMMAND_BUSY)
1331 && stuff_empty()
1332 && oap->regname == 0)
1333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (restart_VIsual_select == 1)
1335 {
1336 VIsual_select = TRUE;
1337 showmode();
1338 restart_VIsual_select = 0;
1339 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001340 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 (void)edit(restart_edit, FALSE, 1L);
1342 }
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (restart_VIsual_select == 2)
1345 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 /* Save count before an operator for next time. */
1348 opcount = ca.opcount;
1349}
1350
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001351#ifdef FEAT_EVAL
1352/*
1353 * Set v:count and v:count1 according to "cap".
1354 * Set v:prevcount only when "set_prevcount" is TRUE.
1355 */
1356 static void
1357set_vcount_ca(cap, set_prevcount)
1358 cmdarg_T *cap;
1359 int *set_prevcount;
1360{
1361 long count = cap->count0;
1362
1363 /* multiply with cap->opcount the same way as above */
1364 if (cap->opcount != 0)
1365 count = cap->opcount * (count == 0 ? 1 : count);
1366 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1367 *set_prevcount = FALSE; /* only set v:prevcount once */
1368}
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * Handle an operator after visual mode or when the movement is finished
1373 */
1374 void
1375do_pending_operator(cap, old_col, gui_yank)
1376 cmdarg_T *cap;
1377 int old_col;
1378 int gui_yank;
1379{
1380 oparg_T *oap = cap->oap;
1381 pos_T old_cursor;
1382 int empty_region_error;
1383 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001384#ifdef FEAT_LINEBREAK
1385 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /* The visual area is remembered for redo */
1389 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1390 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001391 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001393#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
1397#if defined(FEAT_CLIPBOARD)
1398 /*
1399 * Yank the visual area into the GUI selection register before we operate
1400 * on it and lose it forever.
1401 * Don't do it if a specific register was specified, so that ""x"*P works.
1402 * This could call do_pending_operator() recursively, but that's OK
1403 * because gui_yank will be TRUE for the nested call.
1404 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001405 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 && oap->op_type != OP_NOP
1407 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && VIsual_active
1409 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && oap->regname == 0)
1411 clip_auto_select();
1412#endif
1413 old_cursor = curwin->w_cursor;
1414
1415 /*
1416 * If an operation is pending, handle it...
1417 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001418 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001420#ifdef FEAT_LINEBREAK
1421 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001422 if (curwin->w_p_lbr)
1423 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001424 curwin->w_p_lbr = FALSE;
1425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 oap->is_VIsual = VIsual_active;
1427 if (oap->motion_force == 'V')
1428 oap->motion_type = MLINE;
1429 else if (oap->motion_force == 'v')
1430 {
1431 /* If the motion was linewise, "inclusive" will not have been set.
1432 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1433 if (oap->motion_type == MLINE)
1434 oap->inclusive = FALSE;
1435 /* If the motion already was characterwise, toggle "inclusive" */
1436 else if (oap->motion_type == MCHAR)
1437 oap->inclusive = !oap->inclusive;
1438 oap->motion_type = MCHAR;
1439 }
1440 else if (oap->motion_force == Ctrl_V)
1441 {
1442 /* Change line- or characterwise motion into Visual block mode. */
1443 VIsual_active = TRUE;
1444 VIsual = oap->start;
1445 VIsual_mode = Ctrl_V;
1446 VIsual_select = FALSE;
1447 VIsual_reselect = FALSE;
1448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
Bram Moolenaar7afea822013-04-24 18:34:45 +02001450 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1451 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001453 && ((!VIsual_active || oap->motion_force)
1454 /* Also redo Operator-pending Visual mode mappings */
1455 || (VIsual_active && cap->cmdchar == ':'
1456 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001457 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458#ifdef FEAT_FOLDING
1459 && oap->op_type != OP_FOLD
1460 && oap->op_type != OP_FOLDOPEN
1461 && oap->op_type != OP_FOLDOPENREC
1462 && oap->op_type != OP_FOLDCLOSE
1463 && oap->op_type != OP_FOLDCLOSEREC
1464 && oap->op_type != OP_FOLDDEL
1465 && oap->op_type != OP_FOLDDELREC
1466#endif
1467 )
1468 {
1469 prep_redo(oap->regname, cap->count0,
1470 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1471 oap->motion_force, cap->cmdchar, cap->nchar);
1472 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1473 {
1474 /*
1475 * If 'cpoptions' does not contain 'r', insert the search
1476 * pattern to really repeat the same command.
1477 */
1478 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001479 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 AppendToRedobuff(NL_STR);
1481 }
1482 else if (cap->cmdchar == ':')
1483 {
1484 /* do_cmdline() has stored the first typed line in
1485 * "repeat_cmdline". When several lines are typed repeating
1486 * won't be possible. */
1487 if (repeat_cmdline == NULL)
1488 ResetRedobuff();
1489 else
1490 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001491 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 AppendToRedobuff(NL_STR);
1493 vim_free(repeat_cmdline);
1494 repeat_cmdline = NULL;
1495 }
1496 }
1497 }
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (redo_VIsual_busy)
1500 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001501 /* Redo of an operation on a Visual area. Use the same size from
1502 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 oap->start = curwin->w_cursor;
1504 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1505 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1506 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1507 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001508 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001510 if (VIsual_mode == 'v')
1511 {
1512 if (redo_VIsual_line_count <= 1)
1513 {
1514 validate_virtcol();
1515 curwin->w_curswant =
1516 curwin->w_virtcol + redo_VIsual_vcol - 1;
1517 }
1518 else
1519 curwin->w_curswant = redo_VIsual_vcol;
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001522 {
1523 curwin->w_curswant = MAXCOL;
1524 }
1525 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
1527 cap->count0 = redo_VIsual_count;
1528 if (redo_VIsual_count != 0)
1529 cap->count1 = redo_VIsual_count;
1530 else
1531 cap->count1 = 1;
1532 }
1533 else if (VIsual_active)
1534 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001535 if (!gui_yank)
1536 {
1537 /* Save the current VIsual area for '< and '> marks, and "gv" */
1538 curbuf->b_visual.vi_start = VIsual;
1539 curbuf->b_visual.vi_end = curwin->w_cursor;
1540 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001541 if (VIsual_mode_orig != NUL)
1542 {
1543 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1544 VIsual_mode_orig = NUL;
1545 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001546 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001548 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001553 * characterwise selection.
1554 * Special case: gH<Del> deletes the last line. */
1555 if (VIsual_select && VIsual_mode == 'V'
1556 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 if (lt(VIsual, curwin->w_cursor))
1559 {
1560 VIsual.col = 0;
1561 curwin->w_cursor.col =
1562 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1563 }
1564 else
1565 {
1566 curwin->w_cursor.col = 0;
1567 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1568 }
1569 VIsual_mode = 'v';
1570 }
1571 /* If 'selection' is "exclusive", backup one character for
1572 * charwise selections. */
1573 else if (VIsual_mode == 'v')
1574 {
1575# ifdef FEAT_VIRTUALEDIT
1576 include_line_break =
1577# endif
1578 unadjust_for_sel();
1579 }
1580
1581 oap->start = VIsual;
1582 if (VIsual_mode == 'V')
1583 oap->start.col = 0;
1584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 /*
1587 * Set oap->start to the first position of the operated text, oap->end
1588 * to the end of the operated text. w_cursor is equal to oap->start.
1589 */
1590 if (lt(oap->start, curwin->w_cursor))
1591 {
1592#ifdef FEAT_FOLDING
1593 /* Include folded lines completely. */
1594 if (!VIsual_active)
1595 {
1596 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1597 oap->start.col = 0;
1598 if (hasFolding(curwin->w_cursor.lnum, NULL,
1599 &curwin->w_cursor.lnum))
1600 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1601 }
1602#endif
1603 oap->end = curwin->w_cursor;
1604 curwin->w_cursor = oap->start;
1605
1606 /* w_virtcol may have been updated; if the cursor goes back to its
1607 * previous position w_virtcol becomes invalid and isn't updated
1608 * automatically. */
1609 curwin->w_valid &= ~VALID_VIRTCOL;
1610 }
1611 else
1612 {
1613#ifdef FEAT_FOLDING
1614 /* Include folded lines completely. */
1615 if (!VIsual_active && oap->motion_type == MLINE)
1616 {
1617 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1618 NULL))
1619 curwin->w_cursor.col = 0;
1620 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1621 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1622 }
1623#endif
1624 oap->end = oap->start;
1625 oap->start = curwin->w_cursor;
1626 }
1627
1628 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1629
1630#ifdef FEAT_VIRTUALEDIT
1631 /* Set "virtual_op" before resetting VIsual_active. */
1632 virtual_op = virtual_active();
1633#endif
1634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (VIsual_active || redo_VIsual_busy)
1636 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001637 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 if (!redo_VIsual_busy && !gui_yank)
1640 {
1641 /*
1642 * Prepare to reselect and redo Visual: this is based on the
1643 * size of the Visual text
1644 */
1645 resel_VIsual_mode = VIsual_mode;
1646 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001647 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001649 {
1650 if (VIsual_mode != Ctrl_V)
1651 getvvcol(curwin, &(oap->end),
1652 NULL, NULL, &oap->end_vcol);
1653 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1654 {
1655 if (VIsual_mode != Ctrl_V)
1656 getvvcol(curwin, &(oap->start),
1657 &oap->start_vcol, NULL, NULL);
1658 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1659 }
1660 else
1661 resel_VIsual_vcol = oap->end_vcol;
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 resel_VIsual_line_count = oap->line_count;
1664 }
1665
1666 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1667 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1668 && oap->op_type != OP_COLON
1669#ifdef FEAT_FOLDING
1670 && oap->op_type != OP_FOLD
1671 && oap->op_type != OP_FOLDOPEN
1672 && oap->op_type != OP_FOLDOPENREC
1673 && oap->op_type != OP_FOLDCLOSE
1674 && oap->op_type != OP_FOLDCLOSEREC
1675 && oap->op_type != OP_FOLDDEL
1676 && oap->op_type != OP_FOLDDELREC
1677#endif
1678 && oap->motion_force == NUL
1679 )
1680 {
1681 /* Prepare for redoing. Only use the nchar field for "r",
1682 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001683 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1684 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001685 prep_redo(oap->regname, cap->count0,
1686 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1687 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001688 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001689 prep_redo(oap->regname, 0L, NUL, 'v',
1690 get_op_char(oap->op_type),
1691 get_extra_op_char(oap->op_type),
1692 oap->op_type == OP_REPLACE
1693 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (!redo_VIsual_busy)
1695 {
1696 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001697 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 redo_VIsual_line_count = resel_VIsual_line_count;
1699 redo_VIsual_count = cap->count0;
1700 }
1701 }
1702
1703 /*
1704 * oap->inclusive defaults to TRUE.
1705 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1706 * FALSE. This makes "d}P" and "v}dP" work the same.
1707 */
1708 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1709 oap->inclusive = TRUE;
1710 if (VIsual_mode == 'V')
1711 oap->motion_type = MLINE;
1712 else
1713 {
1714 oap->motion_type = MCHAR;
1715 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001716#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 )
1720 {
1721 oap->inclusive = FALSE;
1722 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001723 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001724 if (*p_sel != 'o'
1725 && !op_on_lines(oap->op_type)
1726 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001728 ++oap->end.lnum;
1729 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001730#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001731 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001732#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001733 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 }
1736 }
1737
1738 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /*
1741 * Switch Visual off now, so screen updating does
1742 * not show inverted text when the screen is redrawn.
1743 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1744 * no screen redraw, so it is done here to remove the inverted
1745 * part.
1746 */
1747 if (!gui_yank)
1748 {
1749 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001750#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 setmouse();
1752 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001753#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001754 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 clear_cmdline = TRUE; /* unshow visual mode later */
1756#ifdef FEAT_CMDL_INFO
1757 else
1758 clear_showcmd();
1759#endif
1760 if ((oap->op_type == OP_YANK
1761 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001762 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 || oap->op_type == OP_FILTER)
1764 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001765 {
1766#ifdef FEAT_LINEBREAK
1767 /* make sure redrawing is correct */
1768 curwin->w_p_lbr = lbr_saved;
1769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775#ifdef FEAT_MBYTE
1776 /* Include the trailing byte of a multi-byte char. */
1777 if (has_mbyte && oap->inclusive)
1778 {
1779 int l;
1780
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001781 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (l > 1)
1783 oap->end.col += l - 1;
1784 }
1785#endif
1786 curwin->w_set_curswant = TRUE;
1787
1788 /*
1789 * oap->empty is set when start and end are the same. The inclusive
1790 * flag affects this too, unless yanking and the end is on a NUL.
1791 */
1792 oap->empty = (oap->motion_type == MCHAR
1793 && (!oap->inclusive
1794 || (oap->op_type == OP_YANK
1795 && gchar_pos(&oap->end) == NUL))
1796 && equalpos(oap->start, oap->end)
1797#ifdef FEAT_VIRTUALEDIT
1798 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1799#endif
1800 );
1801 /*
1802 * For delete, change and yank, it's an error to operate on an
1803 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1804 */
1805 empty_region_error = (oap->empty
1806 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 /* Force a redraw when operating on an empty Visual region, when
1809 * 'modifiable is off or creating a fold. */
1810 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001811#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001815 {
1816#ifdef FEAT_LINEBREAK
1817 curwin->w_p_lbr = lbr_saved;
1818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 /*
1823 * If the end of an operator is in column one while oap->motion_type
1824 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1825 * character in the previous line. If op_start is on or before the
1826 * first non-blank in the line, the operator becomes linewise
1827 * (strange, but that's the way vi does it).
1828 */
1829 if ( oap->motion_type == MCHAR
1830 && oap->inclusive == FALSE
1831 && !(cap->retval & CA_NO_ADJ_OP_END)
1832 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001834 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 && oap->line_count > 1)
1836 {
1837 oap->end_adjusted = TRUE; /* remember that we did this */
1838 --oap->line_count;
1839 --oap->end.lnum;
1840 if (inindent(0))
1841 oap->motion_type = MLINE;
1842 else
1843 {
1844 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1845 if (oap->end.col)
1846 {
1847 --oap->end.col;
1848 oap->inclusive = TRUE;
1849 }
1850 }
1851 }
1852 else
1853 oap->end_adjusted = FALSE;
1854
1855 switch (oap->op_type)
1856 {
1857 case OP_LSHIFT:
1858 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001859 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 auto_format(FALSE, TRUE);
1861 break;
1862
1863 case OP_JOIN_NS:
1864 case OP_JOIN:
1865 if (oap->line_count < 2)
1866 oap->line_count = 2;
1867 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1868 curbuf->b_ml.ml_line_count)
1869 beep_flush();
1870 else
1871 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001872 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001873 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 auto_format(FALSE, TRUE);
1875 }
1876 break;
1877
1878 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001883 CancelRedo();
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 else
1886 {
1887 (void)op_delete(oap);
1888 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1889 u_save_cursor(); /* cursor line wasn't saved yet */
1890 auto_format(FALSE, TRUE);
1891 }
1892 break;
1893
1894 case OP_YANK:
1895 if (empty_region_error)
1896 {
1897 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001900 CancelRedo();
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001904 {
1905#ifdef FEAT_LINEBREAK
1906 curwin->w_p_lbr = lbr_saved;
1907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 check_cursor_col();
1911 break;
1912
1913 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001918 CancelRedo();
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
1921 {
1922 /* This is a new edit command, not a restart. Need to
1923 * remember it to make 'insertmode' work with mappings for
1924 * Visual mode. But do this only once and not when typed and
1925 * 'insertmode' isn't set. */
1926 if (p_im || !KeyTyped)
1927 restart_edit_save = restart_edit;
1928 else
1929 restart_edit_save = 0;
1930 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001931#ifdef FEAT_LINEBREAK
1932 /* Restore linebreak, so that when the user edits it looks as
1933 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001934 if (curwin->w_p_lbr != lbr_saved)
1935 {
1936 curwin->w_p_lbr = lbr_saved;
1937 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1938 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 /* Reset finish_op now, don't want it set inside edit(). */
1941 finish_op = FALSE;
1942 if (op_change(oap)) /* will call edit() */
1943 cap->retval |= CA_COMMAND_BUSY;
1944 if (restart_edit == 0)
1945 restart_edit = restart_edit_save;
1946 }
1947 break;
1948
1949 case OP_FILTER:
1950 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1951 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1952 else
1953 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1954
1955 case OP_INDENT:
1956 case OP_COLON:
1957
1958#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1959 /*
1960 * If 'equalprg' is empty, do the indenting internally.
1961 */
1962 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1963 {
1964# ifdef FEAT_LISP
1965 if (curbuf->b_p_lisp)
1966 {
1967 op_reindent(oap, get_lisp_indent);
1968 break;
1969 }
1970# endif
1971# ifdef FEAT_CINDENT
1972 op_reindent(oap,
1973# ifdef FEAT_EVAL
1974 *curbuf->b_p_inde != NUL ? get_expr_indent :
1975# endif
1976 get_c_indent);
1977 break;
1978# endif
1979 }
1980#endif
1981
1982 op_colon(oap);
1983 break;
1984
1985 case OP_TILDE:
1986 case OP_UPPER:
1987 case OP_LOWER:
1988 case OP_ROT13:
1989 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001992 CancelRedo();
1993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 else
1995 op_tilde(oap);
1996 check_cursor_col();
1997 break;
1998
1999 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002000#if defined(FEAT_EVAL)
2001 if (*curbuf->b_p_fex != NUL)
2002 op_formatexpr(oap); /* use expression */
2003 else
2004#endif
2005 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 op_colon(oap); /* use external command */
2007 else
2008 op_format(oap, FALSE); /* use internal function */
2009 break;
2010
2011 case OP_FORMAT2:
2012 op_format(oap, TRUE); /* use internal function */
2013 break;
2014
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002015 case OP_FUNCTION:
2016 op_function(oap); /* call 'operatorfunc' */
2017 break;
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 case OP_INSERT:
2020 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#ifdef FEAT_VISUALEXTRA
2023 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002026 CancelRedo();
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 else
2029 {
2030 /* This is a new edit command, not a restart. Need to
2031 * remember it to make 'insertmode' work with mappings for
2032 * Visual mode. But do this only once. */
2033 restart_edit_save = restart_edit;
2034 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002035#ifdef FEAT_LINEBREAK
2036 /* Restore linebreak, so that when the user edits it looks as
2037 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002038 if (curwin->w_p_lbr != lbr_saved)
2039 {
2040 curwin->w_p_lbr = lbr_saved;
2041 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2042 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002045#ifdef FEAT_LINEBREAK
2046 /* Reset linebreak, so that formatting works correctly. */
2047 curwin->w_p_lbr = FALSE;
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /* TODO: when inserting in several lines, should format all
2051 * the lines. */
2052 auto_format(FALSE, TRUE);
2053
2054 if (restart_edit == 0)
2055 restart_edit = restart_edit_save;
2056 }
2057#else
2058 vim_beep();
2059#endif
2060 break;
2061
2062 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064#ifdef FEAT_VISUALEXTRA
2065 if (empty_region_error)
2066#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002069 CancelRedo();
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#ifdef FEAT_VISUALEXTRA
2072 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002073 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002074# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002075 /* Restore linebreak, so that when the user edits it looks as
2076 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002077 if (curwin->w_p_lbr != lbr_saved)
2078 {
2079 curwin->w_p_lbr = lbr_saved;
2080 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2081 }
2082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085#endif
2086 break;
2087
2088#ifdef FEAT_FOLDING
2089 case OP_FOLD:
2090 VIsual_reselect = FALSE; /* don't reselect now */
2091 foldCreate(oap->start.lnum, oap->end.lnum);
2092 break;
2093
2094 case OP_FOLDOPEN:
2095 case OP_FOLDOPENREC:
2096 case OP_FOLDCLOSE:
2097 case OP_FOLDCLOSEREC:
2098 VIsual_reselect = FALSE; /* don't reselect now */
2099 opFoldRange(oap->start.lnum, oap->end.lnum,
2100 oap->op_type == OP_FOLDOPEN
2101 || oap->op_type == OP_FOLDOPENREC,
2102 oap->op_type == OP_FOLDOPENREC
2103 || oap->op_type == OP_FOLDCLOSEREC,
2104 oap->is_VIsual);
2105 break;
2106
2107 case OP_FOLDDEL:
2108 case OP_FOLDDELREC:
2109 VIsual_reselect = FALSE; /* don't reselect now */
2110 deleteFold(oap->start.lnum, oap->end.lnum,
2111 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2112 break;
2113#endif
2114 default:
2115 clearopbeep(oap);
2116 }
2117#ifdef FEAT_VIRTUALEDIT
2118 virtual_op = MAYBE;
2119#endif
2120 if (!gui_yank)
2121 {
2122 /*
2123 * if 'sol' not set, go back to old column for some commands
2124 */
2125 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2126 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2127 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002128 {
2129#ifdef FEAT_LINEBREAK
2130 curwin->w_p_lbr = FALSE;
2131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 else
2136 {
2137 curwin->w_cursor = old_cursor;
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 clearop(oap);
2141 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002142#ifdef FEAT_LINEBREAK
2143 curwin->w_p_lbr = lbr_saved;
2144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145}
2146
2147/*
2148 * Handle indent and format operators and visual mode ":".
2149 */
2150 static void
2151op_colon(oap)
2152 oparg_T *oap;
2153{
2154 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 if (oap->is_VIsual)
2156 stuffReadbuff((char_u *)"'<,'>");
2157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
2159 /*
2160 * Make the range look nice, so it can be repeated.
2161 */
2162 if (oap->start.lnum == curwin->w_cursor.lnum)
2163 stuffcharReadbuff('.');
2164 else
2165 stuffnumReadbuff((long)oap->start.lnum);
2166 if (oap->end.lnum != oap->start.lnum)
2167 {
2168 stuffcharReadbuff(',');
2169 if (oap->end.lnum == curwin->w_cursor.lnum)
2170 stuffcharReadbuff('.');
2171 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2172 stuffcharReadbuff('$');
2173 else if (oap->start.lnum == curwin->w_cursor.lnum)
2174 {
2175 stuffReadbuff((char_u *)".+");
2176 stuffnumReadbuff((long)oap->line_count - 1);
2177 }
2178 else
2179 stuffnumReadbuff((long)oap->end.lnum);
2180 }
2181 }
2182 if (oap->op_type != OP_COLON)
2183 stuffReadbuff((char_u *)"!");
2184 if (oap->op_type == OP_INDENT)
2185 {
2186#ifndef FEAT_CINDENT
2187 if (*get_equalprg() == NUL)
2188 stuffReadbuff((char_u *)"indent");
2189 else
2190#endif
2191 stuffReadbuff(get_equalprg());
2192 stuffReadbuff((char_u *)"\n");
2193 }
2194 else if (oap->op_type == OP_FORMAT)
2195 {
2196 if (*p_fp == NUL)
2197 stuffReadbuff((char_u *)"fmt");
2198 else
2199 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002200 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 }
2202
2203 /*
2204 * do_cmdline() does the rest
2205 */
2206}
2207
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002208/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002209 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002210 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002211 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002212op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002213 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002214{
2215#ifdef FEAT_EVAL
2216 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002217# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002218 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002219# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002220
2221 if (*p_opfunc == NUL)
2222 EMSG(_("E774: 'operatorfunc' is empty"));
2223 else
2224 {
2225 /* Set '[ and '] marks to text to be operated on. */
2226 curbuf->b_op_start = oap->start;
2227 curbuf->b_op_end = oap->end;
2228 if (oap->motion_type != MLINE && !oap->inclusive)
2229 /* Exclude the end position. */
2230 decl(&curbuf->b_op_end);
2231
2232 if (oap->block_mode)
2233 argv[0] = (char_u *)"block";
2234 else if (oap->motion_type == MLINE)
2235 argv[0] = (char_u *)"line";
2236 else
2237 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002238
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002239# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002240 /* Reset virtual_op so that 'virtualedit' can be changed in the
2241 * function. */
2242 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002243# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002244
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002245 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002246
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002247# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002248 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002249# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002250 }
2251#else
2252 EMSG(_("E775: Eval feature not available"));
2253#endif
2254}
2255
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256#if defined(FEAT_MOUSE) || defined(PROTO)
2257/*
2258 * Do the appropriate action for the current mouse click in the current mode.
2259 * Not used for Command-line mode.
2260 *
2261 * Normal Mode:
2262 * event modi- position visual change action
2263 * fier cursor window
2264 * left press - yes end yes
2265 * left press C yes end yes "^]" (2)
2266 * left press S yes end yes "*" (2)
2267 * left drag - yes start if moved no
2268 * left relse - yes start if moved no
2269 * middle press - yes if not active no put register
2270 * middle press - yes if active no yank and put
2271 * right press - yes start or extend yes
2272 * right press S yes no change yes "#" (2)
2273 * right drag - yes extend no
2274 * right relse - yes extend no
2275 *
2276 * Insert or Replace Mode:
2277 * event modi- position visual change action
2278 * fier cursor window
2279 * left press - yes (cannot be active) yes
2280 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2281 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2282 * left drag - yes start or extend (1) no CTRL-O (1)
2283 * left relse - yes start or extend (1) no CTRL-O (1)
2284 * middle press - no (cannot be active) no put register
2285 * right press - yes start or extend yes CTRL-O
2286 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2287 *
2288 * (1) only if mouse pointer moved since press
2289 * (2) only if click is in same buffer
2290 *
2291 * Return TRUE if start_arrow() should be called for edit mode.
2292 */
2293 int
2294do_mouse(oap, c, dir, count, fixindent)
2295 oparg_T *oap; /* operator argument, can be NULL */
2296 int c; /* K_LEFTMOUSE, etc */
2297 int dir; /* Direction to 'put' if necessary */
2298 long count;
2299 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2300{
2301 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2302 static int got_click = FALSE; /* got a click some time back */
2303
2304 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2305 int is_click; /* If FALSE it's a drag or release event */
2306 int is_drag; /* If TRUE it's a drag event */
2307 int jump_flags = 0; /* flags for jump_to_mouse() */
2308 pos_T start_visual;
2309 int moved; /* Has cursor moved? */
2310 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002311#ifdef FEAT_WINDOWS
2312 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#ifdef FEAT_VERTSPLIT
2315 int in_sep_line; /* mouse in vertical separator line */
2316#endif
2317 int c1, c2;
2318#if defined(FEAT_FOLDING)
2319 pos_T save_cursor;
2320#endif
2321 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 static pos_T orig_cursor;
2323 colnr_T leftcol, rightcol;
2324 pos_T end_visual;
2325 int diff;
2326 int old_active = VIsual_active;
2327 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 int regname;
2329
2330#if defined(FEAT_FOLDING)
2331 save_cursor = curwin->w_cursor;
2332#endif
2333
2334 /*
2335 * When GUI is active, always recognize mouse events, otherwise:
2336 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2337 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2338 * - For command line and insert mode 'mouse' is checked before calling
2339 * do_mouse().
2340 */
2341 if (do_always)
2342 do_always = FALSE;
2343 else
2344#ifdef FEAT_GUI
2345 if (!gui.in_use)
2346#endif
2347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (VIsual_active)
2349 {
2350 if (!mouse_has(MOUSE_VISUAL))
2351 return FALSE;
2352 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002353 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 return FALSE;
2355 }
2356
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002357 for (;;)
2358 {
2359 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2360 if (is_drag)
2361 {
2362 /* If the next character is the same mouse event then use that
2363 * one. Speeds up dragging the status line. */
2364 if (vpeekc() != NUL)
2365 {
2366 int nc;
2367 int save_mouse_row = mouse_row;
2368 int save_mouse_col = mouse_col;
2369
2370 /* Need to get the character, peeking doesn't get the actual
2371 * one. */
2372 nc = safe_vgetc();
2373 if (c == nc)
2374 continue;
2375 vungetc(nc);
2376 mouse_row = save_mouse_row;
2377 mouse_col = save_mouse_col;
2378 }
2379 }
2380 break;
2381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383#ifdef FEAT_MOUSESHAPE
2384 /* May have stopped dragging the status or separator line. The pointer is
2385 * most likely still on the status or separator line. */
2386 if (!is_drag && drag_status_line)
2387 {
2388 drag_status_line = FALSE;
2389 update_mouseshape(SHAPE_IDX_STATUS);
2390 }
2391# ifdef FEAT_VERTSPLIT
2392 if (!is_drag && drag_sep_line)
2393 {
2394 drag_sep_line = FALSE;
2395 update_mouseshape(SHAPE_IDX_VSEP);
2396 }
2397# endif
2398#endif
2399
2400 /*
2401 * Ignore drag and release events if we didn't get a click.
2402 */
2403 if (is_click)
2404 got_click = TRUE;
2405 else
2406 {
2407 if (!got_click) /* didn't get click, ignore */
2408 return FALSE;
2409 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002412#ifdef FEAT_WINDOWS
2413 if (in_tab_line)
2414 {
2415 in_tab_line = FALSE;
2416 return FALSE;
2417 }
2418#endif
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 /*
2423 * CTRL right mouse button does CTRL-T
2424 */
2425 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2426 {
2427 if (State & INSERT)
2428 stuffcharReadbuff(Ctrl_O);
2429 if (count > 1)
2430 stuffnumReadbuff(count);
2431 stuffcharReadbuff(Ctrl_T);
2432 got_click = FALSE; /* ignore drag&release now */
2433 return FALSE;
2434 }
2435
2436 /*
2437 * CTRL only works with left mouse button
2438 */
2439 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2440 return FALSE;
2441
2442 /*
2443 * When a modifier is down, ignore drag and release events, as well as
2444 * multiple clicks and the middle mouse button.
2445 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2446 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002447 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2448 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 && (!is_click
2450 || (mod_mask & MOD_MASK_MULTI_CLICK)
2451 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002452 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 && mouse_model_popup()
2454 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002455 && !((mod_mask & MOD_MASK_ALT)
2456 && !mouse_model_popup()
2457 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 )
2459 return FALSE;
2460
2461 /*
2462 * If the button press was used as the movement command for an operator
2463 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2464 * drag/release events.
2465 */
2466 if (!is_click && which_button == MOUSE_MIDDLE)
2467 return FALSE;
2468
2469 if (oap != NULL)
2470 regname = oap->regname;
2471 else
2472 regname = 0;
2473
2474 /*
2475 * Middle mouse button does a 'put' of the selected text
2476 */
2477 if (which_button == MOUSE_MIDDLE)
2478 {
2479 if (State == NORMAL)
2480 {
2481 /*
2482 * If an operator was pending, we don't know what the user wanted
2483 * to do. Go back to normal mode: Clear the operator and beep().
2484 */
2485 if (oap != NULL && oap->op_type != OP_NOP)
2486 {
2487 clearopbeep(oap);
2488 return FALSE;
2489 }
2490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 /*
2492 * If visual was active, yank the highlighted text and put it
2493 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002494 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 if (VIsual_active)
2497 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002498 if (VIsual_select)
2499 {
2500 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002501 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002502 }
2503 else
2504 {
2505 stuffcharReadbuff('y');
2506 stuffcharReadbuff(K_MIDDLEMOUSE);
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 do_always = TRUE; /* ignore 'mouse' setting next time */
2509 return FALSE;
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 /*
2512 * The rest is below jump_to_mouse()
2513 */
2514 }
2515
2516 else if ((State & INSERT) == 0)
2517 return FALSE;
2518
2519 /*
2520 * Middle click in insert mode doesn't move the mouse, just insert the
2521 * contents of a register. '.' register is special, can't insert that
2522 * with do_put().
2523 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2524 * happens for the GUI).
2525 */
2526 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2527 {
2528 if (regname == '.')
2529 insert_reg(regname, TRUE);
2530 else
2531 {
2532#ifdef FEAT_CLIPBOARD
2533 if (clip_star.available && regname == 0)
2534 regname = '*';
2535#endif
2536 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2537 insert_reg(regname, TRUE);
2538 else
2539 {
2540 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2541
2542 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2543 AppendCharToRedobuff(Ctrl_R);
2544 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2545 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2546 }
2547 }
2548 return FALSE;
2549 }
2550 }
2551
2552 /* When dragging or button-up stay in the same window. */
2553 if (!is_click)
2554 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2555
2556 start_visual.lnum = 0;
2557
Bram Moolenaarf740b292006-02-16 22:11:02 +00002558#ifdef FEAT_WINDOWS
2559 /* Check for clicking in the tab page line. */
2560 if (mouse_row == 0 && firstwin->w_winrow > 0)
2561 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002562 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002563 {
2564 if (in_tab_line)
2565 {
2566 c1 = TabPageIdxs[mouse_col];
2567 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2568 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002569 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002570 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002571
Bram Moolenaarf740b292006-02-16 22:11:02 +00002572 /* click in a tab selects that tab page */
2573 if (is_click
2574# ifdef FEAT_CMDWIN
2575 && cmdwin_type == 0
2576# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002577 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002578 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002579 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 c1 = TabPageIdxs[mouse_col];
2581 if (c1 >= 0)
2582 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002583 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2584 {
2585 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002587 tabpage_new();
2588 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2589 }
2590 else
2591 {
2592 /* Go to specified tab page, or next one if not clicking
2593 * on a label. */
2594 goto_tabpage(c1);
2595
2596 /* It's like clicking on the status line of a window. */
2597 if (curwin != old_curwin)
2598 end_visual_mode();
2599 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002600 }
2601 else if (c1 < 0)
2602 {
2603 tabpage_T *tp;
2604
2605 /* Close the current or specified tab page. */
2606 if (c1 == -999)
2607 tp = curtab;
2608 else
2609 tp = find_tabpage(-c1);
2610 if (tp == curtab)
2611 {
2612 if (first_tabpage->tp_next != NULL)
2613 tabpage_close(FALSE);
2614 }
2615 else if (tp != NULL)
2616 tabpage_close_other(tp, FALSE);
2617 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002620 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002621 else if (is_drag && in_tab_line)
2622 {
2623 c1 = TabPageIdxs[mouse_col];
2624 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2625 return FALSE;
2626 }
2627
Bram Moolenaarf740b292006-02-16 22:11:02 +00002628#endif
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /*
2631 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2632 * right button up -> pop-up menu
2633 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002634 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 */
2636 if (mouse_model_popup())
2637 {
2638 if (which_button == MOUSE_RIGHT
2639 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2640 {
2641 /*
2642 * NOTE: Ignore right button down and drag mouse events.
2643 * Windows only shows the popup menu on the button up event.
2644 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002645#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2646 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (!is_click)
2648 return FALSE;
2649#endif
2650#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2651 if (is_click || is_drag)
2652 return FALSE;
2653#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002654#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2656 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2657 if (gui.in_use)
2658 {
2659 jump_flags = 0;
2660 if (STRCMP(p_mousem, "popup_setpos") == 0)
2661 {
2662 /* First set the cursor position before showing the popup
2663 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (VIsual_active)
2665 {
2666 pos_T m_pos;
2667
2668 /*
2669 * set MOUSE_MAY_STOP_VIS if we are outside the
2670 * selection or the current window (might have false
2671 * negative here)
2672 */
2673 if (mouse_row < W_WINROW(curwin)
2674 || mouse_row
2675 > (W_WINROW(curwin) + curwin->w_height))
2676 jump_flags = MOUSE_MAY_STOP_VIS;
2677 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2678 jump_flags = MOUSE_MAY_STOP_VIS;
2679 else
2680 {
2681 if ((lt(curwin->w_cursor, VIsual)
2682 && (lt(m_pos, curwin->w_cursor)
2683 || lt(VIsual, m_pos)))
2684 || (lt(VIsual, curwin->w_cursor)
2685 && (lt(m_pos, VIsual)
2686 || lt(curwin->w_cursor, m_pos))))
2687 {
2688 jump_flags = MOUSE_MAY_STOP_VIS;
2689 }
2690 else if (VIsual_mode == Ctrl_V)
2691 {
2692 getvcols(curwin, &curwin->w_cursor, &VIsual,
2693 &leftcol, &rightcol);
2694 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2695 if (m_pos.col < leftcol || m_pos.col > rightcol)
2696 jump_flags = MOUSE_MAY_STOP_VIS;
2697 }
2698 }
2699 }
2700 else
2701 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703 if (jump_flags)
2704 {
2705 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002706 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 setcursor();
2708 out_flush(); /* Update before showing popup menu */
2709 }
2710# ifdef FEAT_MENU
2711 gui_show_popupmenu();
2712# endif
2713 return (jump_flags & CURSOR_MOVED) != 0;
2714 }
2715 else
2716 return FALSE;
2717#else
2718 return FALSE;
2719#endif
2720 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002721 if (which_button == MOUSE_LEFT
2722 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 which_button = MOUSE_RIGHT;
2725 mod_mask &= ~MOD_MASK_SHIFT;
2726 }
2727 }
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if ((State & (NORMAL | INSERT))
2730 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2731 {
2732 if (which_button == MOUSE_LEFT)
2733 {
2734 if (is_click)
2735 {
2736 /* stop Visual mode for a left click in a window, but not when
2737 * on a status line */
2738 if (VIsual_active)
2739 jump_flags |= MOUSE_MAY_STOP_VIS;
2740 }
2741 else if (mouse_has(MOUSE_VISUAL))
2742 jump_flags |= MOUSE_MAY_VIS;
2743 }
2744 else if (which_button == MOUSE_RIGHT)
2745 {
2746 if (is_click && VIsual_active)
2747 {
2748 /*
2749 * Remember the start and end of visual before moving the
2750 * cursor.
2751 */
2752 if (lt(curwin->w_cursor, VIsual))
2753 {
2754 start_visual = curwin->w_cursor;
2755 end_visual = VIsual;
2756 }
2757 else
2758 {
2759 start_visual = VIsual;
2760 end_visual = curwin->w_cursor;
2761 }
2762 }
2763 jump_flags |= MOUSE_FOCUS;
2764 if (mouse_has(MOUSE_VISUAL))
2765 jump_flags |= MOUSE_MAY_VIS;
2766 }
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * If an operator is pending, ignore all drags and releases until the
2771 * next mouse click.
2772 */
2773 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2774 {
2775 got_click = FALSE;
2776 oap->motion_type = MCHAR;
2777 }
2778
2779 /* When releasing the button let jump_to_mouse() know. */
2780 if (!is_click && !is_drag)
2781 jump_flags |= MOUSE_RELEASED;
2782
2783 /*
2784 * JUMP!
2785 */
2786 jump_flags = jump_to_mouse(jump_flags,
2787 oap == NULL ? NULL : &(oap->inclusive), which_button);
2788 moved = (jump_flags & CURSOR_MOVED);
2789 in_status_line = (jump_flags & IN_STATUS_LINE);
2790#ifdef FEAT_VERTSPLIT
2791 in_sep_line = (jump_flags & IN_SEP_LINE);
2792#endif
2793
2794#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002795 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2797 {
2798 int key = KEY2TERMCAP1(c);
2799
2800 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2801 || key == (int)KE_RIGHTRELEASE)
2802 netbeans_button_release(which_button);
2803 }
2804#endif
2805
2806 /* When jumping to another window, clear a pending operator. That's a bit
2807 * friendlier than beeping and not jumping to that window. */
2808 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2809 clearop(oap);
2810
2811#ifdef FEAT_FOLDING
2812 if (mod_mask == 0
2813 && !is_drag
2814 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2815 && which_button == MOUSE_LEFT)
2816 {
2817 /* open or close a fold at this line */
2818 if (jump_flags & MOUSE_FOLD_OPEN)
2819 openFold(curwin->w_cursor.lnum, 1L);
2820 else
2821 closeFold(curwin->w_cursor.lnum, 1L);
2822 /* don't move the cursor if still in the same window */
2823 if (curwin == old_curwin)
2824 curwin->w_cursor = save_cursor;
2825 }
2826#endif
2827
2828#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2829 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2830 {
2831 clip_modeless(which_button, is_click, is_drag);
2832 return FALSE;
2833 }
2834#endif
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002837 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if (VIsual_active && is_drag && p_so)
2839 {
2840 /* In the very first line, allow scrolling one line */
2841 if (mouse_row == 0)
2842 mouse_dragging = 2;
2843 else
2844 mouse_dragging = 1;
2845 }
2846
2847 /* When dragging the mouse above the window, scroll down. */
2848 if (is_drag && mouse_row < 0 && !in_status_line)
2849 {
2850 scroll_redraw(FALSE, 1L);
2851 mouse_row = 0;
2852 }
2853
2854 if (start_visual.lnum) /* right click in visual mode */
2855 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002856 /* When ALT is pressed make Visual mode blockwise. */
2857 if (mod_mask & MOD_MASK_ALT)
2858 VIsual_mode = Ctrl_V;
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 /*
2861 * In Visual-block mode, divide the area in four, pick up the corner
2862 * that is in the quarter that the cursor is in.
2863 */
2864 if (VIsual_mode == Ctrl_V)
2865 {
2866 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2867 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2868 end_visual.col = leftcol;
2869 else
2870 end_visual.col = rightcol;
2871 if (curwin->w_cursor.lnum <
2872 (start_visual.lnum + end_visual.lnum) / 2)
2873 end_visual.lnum = end_visual.lnum;
2874 else
2875 end_visual.lnum = start_visual.lnum;
2876
2877 /* move VIsual to the right column */
2878 start_visual = curwin->w_cursor; /* save the cursor pos */
2879 curwin->w_cursor = end_visual;
2880 coladvance(end_visual.col);
2881 VIsual = curwin->w_cursor;
2882 curwin->w_cursor = start_visual; /* restore the cursor */
2883 }
2884 else
2885 {
2886 /*
2887 * If the click is before the start of visual, change the start.
2888 * If the click is after the end of visual, change the end. If
2889 * the click is inside the visual, change the closest side.
2890 */
2891 if (lt(curwin->w_cursor, start_visual))
2892 VIsual = end_visual;
2893 else if (lt(end_visual, curwin->w_cursor))
2894 VIsual = start_visual;
2895 else
2896 {
2897 /* In the same line, compare column number */
2898 if (end_visual.lnum == start_visual.lnum)
2899 {
2900 if (curwin->w_cursor.col - start_visual.col >
2901 end_visual.col - curwin->w_cursor.col)
2902 VIsual = start_visual;
2903 else
2904 VIsual = end_visual;
2905 }
2906
2907 /* In different lines, compare line number */
2908 else
2909 {
2910 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2911 (end_visual.lnum - curwin->w_cursor.lnum);
2912
2913 if (diff > 0) /* closest to end */
2914 VIsual = start_visual;
2915 else if (diff < 0) /* closest to start */
2916 VIsual = end_visual;
2917 else /* in the middle line */
2918 {
2919 if (curwin->w_cursor.col <
2920 (start_visual.col + end_visual.col) / 2)
2921 VIsual = end_visual;
2922 else
2923 VIsual = start_visual;
2924 }
2925 }
2926 }
2927 }
2928 }
2929 /*
2930 * If Visual mode started in insert mode, execute "CTRL-O"
2931 */
2932 else if ((State & INSERT) && VIsual_active)
2933 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 /*
2936 * Middle mouse click: Put text before cursor.
2937 */
2938 if (which_button == MOUSE_MIDDLE)
2939 {
2940#ifdef FEAT_CLIPBOARD
2941 if (clip_star.available && regname == 0)
2942 regname = '*';
2943#endif
2944 if (yank_register_mline(regname))
2945 {
2946 if (mouse_past_bottom)
2947 dir = FORWARD;
2948 }
2949 else if (mouse_past_eol)
2950 dir = FORWARD;
2951
2952 if (fixindent)
2953 {
2954 c1 = (dir == BACKWARD) ? '[' : ']';
2955 c2 = 'p';
2956 }
2957 else
2958 {
2959 c1 = (dir == FORWARD) ? 'p' : 'P';
2960 c2 = NUL;
2961 }
2962 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2963
2964 /*
2965 * Remember where the paste started, so in edit() Insstart can be set
2966 * to this position
2967 */
2968 if (restart_edit != 0)
2969 where_paste_started = curwin->w_cursor;
2970 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2971 }
2972
2973#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2974 /*
2975 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2976 * error under the mouse pointer.
2977 */
2978 else if (((mod_mask & MOD_MASK_CTRL)
2979 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2980 && bt_quickfix(curbuf))
2981 {
2982 if (State & INSERT)
2983 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002984 if (curwin->w_llist_ref == NULL) /* quickfix window */
2985 stuffReadbuff((char_u *)":.cc\n");
2986 else /* location list window */
2987 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 got_click = FALSE; /* ignore drag&release now */
2989 }
2990#endif
2991
2992 /*
2993 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2994 * under the mouse pointer.
2995 */
2996 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2997 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2998 {
2999 if (State & INSERT)
3000 stuffcharReadbuff(Ctrl_O);
3001 stuffcharReadbuff(Ctrl_RSB);
3002 got_click = FALSE; /* ignore drag&release now */
3003 }
3004
3005 /*
3006 * Shift-Mouse click searches for the next occurrence of the word under
3007 * the mouse pointer
3008 */
3009 else if ((mod_mask & MOD_MASK_SHIFT))
3010 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003011 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 stuffcharReadbuff(Ctrl_O);
3013 if (which_button == MOUSE_LEFT)
3014 stuffcharReadbuff('*');
3015 else /* MOUSE_RIGHT */
3016 stuffcharReadbuff('#');
3017 }
3018
3019 /* Handle double clicks, unless on status line */
3020 else if (in_status_line)
3021 {
3022#ifdef FEAT_MOUSESHAPE
3023 if ((is_drag || is_click) && !drag_status_line)
3024 {
3025 drag_status_line = TRUE;
3026 update_mouseshape(-1);
3027 }
3028#endif
3029 }
3030#ifdef FEAT_VERTSPLIT
3031 else if (in_sep_line)
3032 {
3033# ifdef FEAT_MOUSESHAPE
3034 if ((is_drag || is_click) && !drag_sep_line)
3035 {
3036 drag_sep_line = TRUE;
3037 update_mouseshape(-1);
3038 }
3039# endif
3040 }
3041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3043 && mouse_has(MOUSE_VISUAL))
3044 {
3045 if (is_click || !VIsual_active)
3046 {
3047 if (VIsual_active)
3048 orig_cursor = VIsual;
3049 else
3050 {
3051 check_visual_highlight();
3052 VIsual = curwin->w_cursor;
3053 orig_cursor = VIsual;
3054 VIsual_active = TRUE;
3055 VIsual_reselect = TRUE;
3056 /* start Select mode if 'selectmode' contains "mouse" */
3057 may_start_select('o');
3058 setmouse();
3059 }
3060 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003061 {
3062 /* Double click with ALT pressed makes it blockwise. */
3063 if (mod_mask & MOD_MASK_ALT)
3064 VIsual_mode = Ctrl_V;
3065 else
3066 VIsual_mode = 'v';
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3069 VIsual_mode = 'V';
3070 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3071 VIsual_mode = Ctrl_V;
3072#ifdef FEAT_CLIPBOARD
3073 /* Make sure the clipboard gets updated. Needed because start and
3074 * end may still be the same, and the selection needs to be owned */
3075 clip_star.vmode = NUL;
3076#endif
3077 }
3078 /*
3079 * A double click selects a word or a block.
3080 */
3081 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3082 {
3083 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003084 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085
3086 if (is_click)
3087 {
3088 /* If the character under the cursor (skipping white space) is
3089 * not a word character, try finding a match and select a (),
3090 * {}, [], #if/#endif, etc. block. */
3091 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003092 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 inc(&end_visual);
3094 if (oap != NULL)
3095 oap->motion_type = MCHAR;
3096 if (oap != NULL
3097 && VIsual_mode == 'v'
3098 && !vim_iswordc(gchar_pos(&end_visual))
3099 && equalpos(curwin->w_cursor, VIsual)
3100 && (pos = findmatch(oap, NUL)) != NULL)
3101 {
3102 curwin->w_cursor = *pos;
3103 if (oap->motion_type == MLINE)
3104 VIsual_mode = 'V';
3105 else if (*p_sel == 'e')
3106 {
3107 if (lt(curwin->w_cursor, VIsual))
3108 ++VIsual.col;
3109 else
3110 ++curwin->w_cursor.col;
3111 }
3112 }
3113 }
3114
3115 if (pos == NULL && (is_click || is_drag))
3116 {
3117 /* When not found a match or when dragging: extend to include
3118 * a word. */
3119 if (lt(curwin->w_cursor, orig_cursor))
3120 {
3121 find_start_of_word(&curwin->w_cursor);
3122 find_end_of_word(&VIsual);
3123 }
3124 else
3125 {
3126 find_start_of_word(&VIsual);
3127 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3128#ifdef FEAT_MBYTE
3129 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003130 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#else
3132 ++curwin->w_cursor.col;
3133#endif
3134 find_end_of_word(&curwin->w_cursor);
3135 }
3136 }
3137 curwin->w_set_curswant = TRUE;
3138 }
3139 if (is_click)
3140 redraw_curbuf_later(INVERTED); /* update the inversion */
3141 }
3142 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003143 {
3144 if (mod_mask & MOD_MASK_ALT)
3145 VIsual_mode = Ctrl_V;
3146 else
3147 VIsual_mode = 'v';
3148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003151 if ((!VIsual_active && old_active && mode_displayed)
3152 || (VIsual_active && p_smd && msg_silent == 0
3153 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
3156 return moved;
3157}
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159/*
3160 * Move "pos" back to the start of the word it's in.
3161 */
3162 static void
3163find_start_of_word(pos)
3164 pos_T *pos;
3165{
3166 char_u *line;
3167 int cclass;
3168 int col;
3169
3170 line = ml_get(pos->lnum);
3171 cclass = get_mouse_class(line + pos->col);
3172
3173 while (pos->col > 0)
3174 {
3175 col = pos->col - 1;
3176#ifdef FEAT_MBYTE
3177 col -= (*mb_head_off)(line, line + col);
3178#endif
3179 if (get_mouse_class(line + col) != cclass)
3180 break;
3181 pos->col = col;
3182 }
3183}
3184
3185/*
3186 * Move "pos" forward to the end of the word it's in.
3187 * When 'selection' is "exclusive", the position is just after the word.
3188 */
3189 static void
3190find_end_of_word(pos)
3191 pos_T *pos;
3192{
3193 char_u *line;
3194 int cclass;
3195 int col;
3196
3197 line = ml_get(pos->lnum);
3198 if (*p_sel == 'e' && pos->col > 0)
3199 {
3200 --pos->col;
3201#ifdef FEAT_MBYTE
3202 pos->col -= (*mb_head_off)(line, line + pos->col);
3203#endif
3204 }
3205 cclass = get_mouse_class(line + pos->col);
3206 while (line[pos->col] != NUL)
3207 {
3208#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003209 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#else
3211 col = pos->col + 1;
3212#endif
3213 if (get_mouse_class(line + col) != cclass)
3214 {
3215 if (*p_sel == 'e')
3216 pos->col = col;
3217 break;
3218 }
3219 pos->col = col;
3220 }
3221}
3222
3223/*
3224 * Get class of a character for selection: same class means same word.
3225 * 0: blank
3226 * 1: punctuation groups
3227 * 2: normal word character
3228 * >2: multi-byte word character.
3229 */
3230 static int
3231get_mouse_class(p)
3232 char_u *p;
3233{
3234 int c;
3235
3236#ifdef FEAT_MBYTE
3237 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3238 return mb_get_class(p);
3239#endif
3240
3241 c = *p;
3242 if (c == ' ' || c == '\t')
3243 return 0;
3244
3245 if (vim_iswordc(c))
3246 return 2;
3247
3248 /*
3249 * There are a few special cases where we want certain combinations of
3250 * characters to be considered as a single word. These are things like
3251 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003252 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
3254 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3255 return 1;
3256 return c;
3257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#endif /* FEAT_MOUSE */
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260/*
3261 * Check if highlighting for visual mode is possible, give a warning message
3262 * if not.
3263 */
3264 void
3265check_visual_highlight()
3266{
3267 static int did_check = FALSE;
3268
3269 if (full_screen)
3270 {
3271 if (!did_check && hl_attr(HLF_V) == 0)
3272 MSG(_("Warning: terminal cannot highlight"));
3273 did_check = TRUE;
3274 }
3275}
3276
3277/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003278 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 * This function should ALWAYS be called to end Visual mode, except from
3280 * do_pending_operator().
3281 */
3282 void
3283end_visual_mode()
3284{
3285#ifdef FEAT_CLIPBOARD
3286 /*
3287 * If we are using the clipboard, then remember what was selected in case
3288 * we need to paste it somewhere while we still own the selection.
3289 * Only do this when the clipboard is already owned. Don't want to grab
3290 * the selection when hitting ESC.
3291 */
3292 if (clip_star.available && clip_star.owned)
3293 clip_auto_select();
3294#endif
3295
3296 VIsual_active = FALSE;
3297#ifdef FEAT_MOUSE
3298 setmouse();
3299 mouse_dragging = 0;
3300#endif
3301
3302 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003303 curbuf->b_visual.vi_mode = VIsual_mode;
3304 curbuf->b_visual.vi_start = VIsual;
3305 curbuf->b_visual.vi_end = curwin->w_cursor;
3306 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_EVAL
3308 curbuf->b_visual_mode_eval = VIsual_mode;
3309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#ifdef FEAT_VIRTUALEDIT
3311 if (!virtual_active())
3312 curwin->w_cursor.coladd = 0;
3313#endif
3314
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003315 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 clear_cmdline = TRUE; /* unshow visual mode later */
3317#ifdef FEAT_CMDL_INFO
3318 else
3319 clear_showcmd();
3320#endif
3321
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003322 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323}
3324
3325/*
3326 * Reset VIsual_active and VIsual_reselect.
3327 */
3328 void
3329reset_VIsual_and_resel()
3330{
3331 if (VIsual_active)
3332 {
3333 end_visual_mode();
3334 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3335 }
3336 VIsual_reselect = FALSE;
3337}
3338
3339/*
3340 * Reset VIsual_active and VIsual_reselect if it's set.
3341 */
3342 void
3343reset_VIsual()
3344{
3345 if (VIsual_active)
3346 {
3347 end_visual_mode();
3348 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3349 VIsual_reselect = FALSE;
3350 }
3351}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003353#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3355
3356/*
3357 * Check for a balloon-eval special item to include when searching for an
3358 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3359 * Returns TRUE if the character at "*ptr" should be included.
3360 * "dir" is FORWARD or BACKWARD, the direction of searching.
3361 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3362 * "bnp" points to a counter for square brackets.
3363 */
3364 static int
3365find_is_eval_item(ptr, colp, bnp, dir)
3366 char_u *ptr;
3367 int *colp;
3368 int *bnp;
3369 int dir;
3370{
3371 /* Accept everything inside []. */
3372 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3373 ++*bnp;
3374 if (*bnp > 0)
3375 {
3376 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3377 --*bnp;
3378 return TRUE;
3379 }
3380
3381 /* skip over "s.var" */
3382 if (*ptr == '.')
3383 return TRUE;
3384
3385 /* two-character item: s->var */
3386 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3387 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3388 {
3389 *colp += dir;
3390 return TRUE;
3391 }
3392 return FALSE;
3393}
3394#endif
3395
3396/*
3397 * Find the identifier under or to the right of the cursor.
3398 * "find_type" can have one of three values:
3399 * FIND_IDENT: find an identifier (keyword)
3400 * FIND_STRING: find any non-white string
3401 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 *
3404 * There are three steps:
3405 * 1. Search forward for the start of an identifier/string. Doesn't move if
3406 * already on one.
3407 * 2. Search backward for the start of this identifier/string.
3408 * This doesn't match the real Vi but I like it a little better and it
3409 * shouldn't bother anyone.
3410 * 3. Search forward to the end of this identifier/string.
3411 * When FIND_IDENT isn't defined, we backup until a blank.
3412 *
3413 * Returns the length of the string, or zero if no string is found.
3414 * If a string is found, a pointer to the string is put in "*string". This
3415 * string is not always NUL terminated.
3416 */
3417 int
3418find_ident_under_cursor(string, find_type)
3419 char_u **string;
3420 int find_type;
3421{
3422 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3423 curwin->w_cursor.col, string, find_type);
3424}
3425
3426/*
3427 * Like find_ident_under_cursor(), but for any window and any position.
3428 * However: Uses 'iskeyword' from the current window!.
3429 */
3430 int
3431find_ident_at_pos(wp, lnum, startcol, string, find_type)
3432 win_T *wp;
3433 linenr_T lnum;
3434 colnr_T startcol;
3435 char_u **string;
3436 int find_type;
3437{
3438 char_u *ptr;
3439 int col = 0; /* init to shut up GCC */
3440 int i;
3441#ifdef FEAT_MBYTE
3442 int this_class = 0;
3443 int prev_class;
3444 int prevcol;
3445#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int bn = 0; /* bracket nesting */
3448#endif
3449
3450 /*
3451 * if i == 0: try to find an identifier
3452 * if i == 1: try to find any non-white string
3453 */
3454 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3455 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3456 {
3457 /*
3458 * 1. skip to start of identifier/string
3459 */
3460 col = startcol;
3461#ifdef FEAT_MBYTE
3462 if (has_mbyte)
3463 {
3464 while (ptr[col] != NUL)
3465 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 /* Stop at a ']' to evaluate "a[x]". */
3468 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3469 break;
3470# endif
3471 this_class = mb_get_class(ptr + col);
3472 if (this_class != 0 && (i == 1 || this_class != 1))
3473 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003474 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 }
3476 }
3477 else
3478#endif
3479 while (ptr[col] != NUL
3480 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003481# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3483# endif
3484 )
3485 ++col;
3486
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003487#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 /* When starting on a ']' count it, so that we include the '['. */
3489 bn = ptr[col] == ']';
3490#endif
3491
3492 /*
3493 * 2. Back up to start of identifier/string.
3494 */
3495#ifdef FEAT_MBYTE
3496 if (has_mbyte)
3497 {
3498 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3501 this_class = mb_get_class((char_u *)"a");
3502 else
3503# endif
3504 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3508 prev_class = mb_get_class(ptr + prevcol);
3509 if (this_class != prev_class
3510 && (i == 0
3511 || prev_class == 0
3512 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003513# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 && (!(find_type & FIND_EVAL)
3515 || prevcol == 0
3516 || !find_is_eval_item(ptr + prevcol, &prevcol,
3517 &bn, BACKWARD))
3518# endif
3519 )
3520 break;
3521 col = prevcol;
3522 }
3523
3524 /* If we don't want just any old string, or we've found an
3525 * identifier, stop searching. */
3526 if (this_class > 2)
3527 this_class = 2;
3528 if (!(find_type & FIND_STRING) || this_class == 2)
3529 break;
3530 }
3531 else
3532#endif
3533 {
3534 while (col > 0
3535 && ((i == 0
3536 ? vim_iswordc(ptr[col - 1])
3537 : (!vim_iswhite(ptr[col - 1])
3538 && (!(find_type & FIND_IDENT)
3539 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 || ((find_type & FIND_EVAL)
3542 && col > 1
3543 && find_is_eval_item(ptr + col - 1, &col,
3544 &bn, BACKWARD))
3545#endif
3546 ))
3547 --col;
3548
3549 /* If we don't want just any old string, or we've found an
3550 * identifier, stop searching. */
3551 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3552 break;
3553 }
3554 }
3555
3556 if (ptr[col] == NUL || (i == 0 && (
3557#ifdef FEAT_MBYTE
3558 has_mbyte ? this_class != 2 :
3559#endif
3560 !vim_iswordc(ptr[col]))))
3561 {
3562 /*
3563 * didn't find an identifier or string
3564 */
3565 if (find_type & FIND_STRING)
3566 EMSG(_("E348: No string under cursor"));
3567 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003568 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return 0;
3570 }
3571 ptr += col;
3572 *string = ptr;
3573
3574 /*
3575 * 3. Find the end if the identifier/string.
3576 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 bn = 0;
3579 startcol -= col;
3580#endif
3581 col = 0;
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
3585 /* Search for point of changing multibyte character class. */
3586 this_class = mb_get_class(ptr);
3587 while (ptr[col] != NUL
3588 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3589 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003590# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 || ((find_type & FIND_EVAL)
3592 && col <= (int)startcol
3593 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3594# endif
3595 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003596 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 else
3599#endif
3600 while ((i == 0 ? vim_iswordc(ptr[col])
3601 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 || ((find_type & FIND_EVAL)
3604 && col <= (int)startcol
3605 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3606# endif
3607 )
3608 {
3609 ++col;
3610 }
3611
3612 return col;
3613}
3614
3615/*
3616 * Prepare for redo of a normal command.
3617 */
3618 static void
3619prep_redo_cmd(cap)
3620 cmdarg_T *cap;
3621{
3622 prep_redo(cap->oap->regname, cap->count0,
3623 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3624}
3625
3626/*
3627 * Prepare for redo of any command.
3628 * Note that only the last argument can be a multi-byte char.
3629 */
3630 static void
3631prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3632 int regname;
3633 long num;
3634 int cmd1;
3635 int cmd2;
3636 int cmd3;
3637 int cmd4;
3638 int cmd5;
3639{
3640 ResetRedobuff();
3641 if (regname != 0) /* yank from specified buffer */
3642 {
3643 AppendCharToRedobuff('"');
3644 AppendCharToRedobuff(regname);
3645 }
3646 if (num)
3647 AppendNumberToRedobuff(num);
3648
3649 if (cmd1 != NUL)
3650 AppendCharToRedobuff(cmd1);
3651 if (cmd2 != NUL)
3652 AppendCharToRedobuff(cmd2);
3653 if (cmd3 != NUL)
3654 AppendCharToRedobuff(cmd3);
3655 if (cmd4 != NUL)
3656 AppendCharToRedobuff(cmd4);
3657 if (cmd5 != NUL)
3658 AppendCharToRedobuff(cmd5);
3659}
3660
3661/*
3662 * check for operator active and clear it
3663 *
3664 * return TRUE if operator was active
3665 */
3666 static int
3667checkclearop(oap)
3668 oparg_T *oap;
3669{
3670 if (oap->op_type == OP_NOP)
3671 return FALSE;
3672 clearopbeep(oap);
3673 return TRUE;
3674}
3675
3676/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003677 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003679 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 */
3681 static int
3682checkclearopq(oap)
3683 oparg_T *oap;
3684{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003685 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FALSE;
3687 clearopbeep(oap);
3688 return TRUE;
3689}
3690
3691 static void
3692clearop(oap)
3693 oparg_T *oap;
3694{
3695 oap->op_type = OP_NOP;
3696 oap->regname = 0;
3697 oap->motion_force = NUL;
3698 oap->use_reg_one = FALSE;
3699}
3700
3701 static void
3702clearopbeep(oap)
3703 oparg_T *oap;
3704{
3705 clearop(oap);
3706 beep_flush();
3707}
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709/*
3710 * Remove the shift modifier from a special key.
3711 */
3712 static void
3713unshift_special(cap)
3714 cmdarg_T *cap;
3715{
3716 switch (cap->cmdchar)
3717 {
3718 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3719 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3720 case K_S_UP: cap->cmdchar = K_UP; break;
3721 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3722 case K_S_HOME: cap->cmdchar = K_HOME; break;
3723 case K_S_END: cap->cmdchar = K_END; break;
3724 }
3725 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3726}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727
3728#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3729/*
3730 * Routines for displaying a partly typed command
3731 */
3732
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003733#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734static char_u showcmd_buf[SHOWCMD_BUFLEN];
3735static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3736static int showcmd_is_clear = TRUE;
3737static int showcmd_visual = FALSE;
3738
3739static void display_showcmd __ARGS((void));
3740
3741 void
3742clear_showcmd()
3743{
3744 if (!p_sc)
3745 return;
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 if (VIsual_active && !char_avail())
3748 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003749 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 long lines;
3751 colnr_T leftcol, rightcol;
3752 linenr_T top, bot;
3753
3754 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003755 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 top = VIsual.lnum;
3758 bot = curwin->w_cursor.lnum;
3759 }
3760 else
3761 {
3762 top = curwin->w_cursor.lnum;
3763 bot = VIsual.lnum;
3764 }
3765# ifdef FEAT_FOLDING
3766 /* Include closed folds as a whole. */
3767 hasFolding(top, &top, NULL);
3768 hasFolding(bot, NULL, &bot);
3769# endif
3770 lines = bot - top + 1;
3771
3772 if (VIsual_mode == Ctrl_V)
3773 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003774# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003775 char_u *saved_sbr = p_sbr;
3776
3777 /* Make 'sbr' empty for a moment to get the correct size. */
3778 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003781# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003782 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3785 (long)(rightcol - leftcol + 1));
3786 }
3787 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3788 sprintf((char *)showcmd_buf, "%ld", lines);
3789 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003790 {
3791 char_u *s, *e;
3792 int l;
3793 int bytes = 0;
3794 int chars = 0;
3795
3796 if (cursor_bot)
3797 {
3798 s = ml_get_pos(&VIsual);
3799 e = ml_get_cursor();
3800 }
3801 else
3802 {
3803 s = ml_get_cursor();
3804 e = ml_get_pos(&VIsual);
3805 }
3806 while ((*p_sel != 'e') ? s <= e : s < e)
3807 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003808# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003809 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003810# else
3811 l = (*s == NUL) ? 0 : 1;
3812# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003813 if (l == 0)
3814 {
3815 ++bytes;
3816 ++chars;
3817 break; /* end of line */
3818 }
3819 bytes += l;
3820 ++chars;
3821 s += l;
3822 }
3823 if (bytes == chars)
3824 sprintf((char *)showcmd_buf, "%d", chars);
3825 else
3826 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3829 showcmd_visual = TRUE;
3830 }
3831 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
3833 showcmd_buf[0] = NUL;
3834 showcmd_visual = FALSE;
3835
3836 /* Don't actually display something if there is nothing to clear. */
3837 if (showcmd_is_clear)
3838 return;
3839 }
3840
3841 display_showcmd();
3842}
3843
3844/*
3845 * Add 'c' to string of shown command chars.
3846 * Return TRUE if output has been written (and setcursor() has been called).
3847 */
3848 int
3849add_to_showcmd(c)
3850 int c;
3851{
3852 char_u *p;
3853 int old_len;
3854 int extra_len;
3855 int overflow;
3856#if defined(FEAT_MOUSE)
3857 int i;
3858 static int ignore[] =
3859 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003860# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3862 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 K_IGNORE,
3865 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3866 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3867 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003868 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003870 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 0
3872 };
3873#endif
3874
Bram Moolenaar09df3122006-01-23 22:23:09 +00003875 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 return FALSE;
3877
3878 if (showcmd_visual)
3879 {
3880 showcmd_buf[0] = NUL;
3881 showcmd_visual = FALSE;
3882 }
3883
3884#if defined(FEAT_MOUSE)
3885 /* Ignore keys that are scrollbar updates and mouse clicks */
3886 if (IS_SPECIAL(c))
3887 for (i = 0; ignore[i] != 0; ++i)
3888 if (ignore[i] == c)
3889 return FALSE;
3890#endif
3891
3892 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003893 if (*p == ' ')
3894 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 old_len = (int)STRLEN(showcmd_buf);
3896 extra_len = (int)STRLEN(p);
3897 overflow = old_len + extra_len - SHOWCMD_COLS;
3898 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003899 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3900 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 STRCAT(showcmd_buf, p);
3902
3903 if (char_avail())
3904 return FALSE;
3905
3906 display_showcmd();
3907
3908 return TRUE;
3909}
3910
3911 void
3912add_to_showcmd_c(c)
3913 int c;
3914{
3915 if (!add_to_showcmd(c))
3916 setcursor();
3917}
3918
3919/*
3920 * Delete 'len' characters from the end of the shown command.
3921 */
3922 static void
3923del_from_showcmd(len)
3924 int len;
3925{
3926 int old_len;
3927
3928 if (!p_sc)
3929 return;
3930
3931 old_len = (int)STRLEN(showcmd_buf);
3932 if (len > old_len)
3933 len = old_len;
3934 showcmd_buf[old_len - len] = NUL;
3935
3936 if (!char_avail())
3937 display_showcmd();
3938}
3939
3940/*
3941 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3942 * something and there is a partial mapping.
3943 */
3944 void
3945push_showcmd()
3946{
3947 if (p_sc)
3948 STRCPY(old_showcmd_buf, showcmd_buf);
3949}
3950
3951 void
3952pop_showcmd()
3953{
3954 if (!p_sc)
3955 return;
3956
3957 STRCPY(showcmd_buf, old_showcmd_buf);
3958
3959 display_showcmd();
3960}
3961
3962 static void
3963display_showcmd()
3964{
3965 int len;
3966
3967 cursor_off();
3968
3969 len = (int)STRLEN(showcmd_buf);
3970 if (len == 0)
3971 showcmd_is_clear = TRUE;
3972 else
3973 {
3974 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3975 showcmd_is_clear = FALSE;
3976 }
3977
3978 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003979 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3980 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 */
3982 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3983
3984 setcursor(); /* put cursor back where it belongs */
3985}
3986#endif
3987
3988#ifdef FEAT_SCROLLBIND
3989/*
3990 * When "check" is FALSE, prepare for commands that scroll the window.
3991 * When "check" is TRUE, take care of scroll-binding after the window has
3992 * scrolled. Called from normal_cmd() and edit().
3993 */
3994 void
3995do_check_scrollbind(check)
3996 int check;
3997{
3998 static win_T *old_curwin = NULL;
3999 static linenr_T old_topline = 0;
4000#ifdef FEAT_DIFF
4001 static int old_topfill = 0;
4002#endif
4003 static buf_T *old_buf = NULL;
4004 static colnr_T old_leftcol = 0;
4005
4006 if (check && curwin->w_p_scb)
4007 {
4008 /* If a ":syncbind" command was just used, don't scroll, only reset
4009 * the values. */
4010 if (did_syncbind)
4011 did_syncbind = FALSE;
4012 else if (curwin == old_curwin)
4013 {
4014 /*
4015 * Synchronize other windows, as necessary according to
4016 * 'scrollbind'. Don't do this after an ":edit" command, except
4017 * when 'diff' is set.
4018 */
4019 if ((curwin->w_buffer == old_buf
4020#ifdef FEAT_DIFF
4021 || curwin->w_p_diff
4022#endif
4023 )
4024 && (curwin->w_topline != old_topline
4025#ifdef FEAT_DIFF
4026 || curwin->w_topfill != old_topfill
4027#endif
4028 || curwin->w_leftcol != old_leftcol))
4029 {
4030 check_scrollbind(curwin->w_topline - old_topline,
4031 (long)(curwin->w_leftcol - old_leftcol));
4032 }
4033 }
4034 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4035 {
4036 /*
4037 * When switching between windows, make sure that the relative
4038 * vertical offset is valid for the new window. The relative
4039 * offset is invalid whenever another 'scrollbind' window has
4040 * scrolled to a point that would force the current window to
4041 * scroll past the beginning or end of its buffer. When the
4042 * resync is performed, some of the other 'scrollbind' windows may
4043 * need to jump so that the current window's relative position is
4044 * visible on-screen.
4045 */
4046 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4047 }
4048 curwin->w_scbind_pos = curwin->w_topline;
4049 }
4050
4051 old_curwin = curwin;
4052 old_topline = curwin->w_topline;
4053#ifdef FEAT_DIFF
4054 old_topfill = curwin->w_topfill;
4055#endif
4056 old_buf = curwin->w_buffer;
4057 old_leftcol = curwin->w_leftcol;
4058}
4059
4060/*
4061 * Synchronize any windows that have "scrollbind" set, based on the
4062 * number of rows by which the current window has changed
4063 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4064 */
4065 void
4066check_scrollbind(topline_diff, leftcol_diff)
4067 linenr_T topline_diff;
4068 long leftcol_diff;
4069{
4070 int want_ver;
4071 int want_hor;
4072 win_T *old_curwin = curwin;
4073 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 int old_VIsual_select = VIsual_select;
4075 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 colnr_T tgt_leftcol = curwin->w_leftcol;
4077 long topline;
4078 long y;
4079
4080 /*
4081 * check 'scrollopt' string for vertical and horizontal scroll options
4082 */
4083 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4084#ifdef FEAT_DIFF
4085 want_ver |= old_curwin->w_p_diff;
4086#endif
4087 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4088
4089 /*
4090 * loop through the scrollbound windows and scroll accordingly
4091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4094 {
4095 curbuf = curwin->w_buffer;
4096 /* skip original window and windows with 'noscrollbind' */
4097 if (curwin != old_curwin && curwin->w_p_scb)
4098 {
4099 /*
4100 * do the vertical scroll
4101 */
4102 if (want_ver)
4103 {
4104#ifdef FEAT_DIFF
4105 if (old_curwin->w_p_diff && curwin->w_p_diff)
4106 {
4107 diff_set_topline(old_curwin, curwin);
4108 }
4109 else
4110#endif
4111 {
4112 curwin->w_scbind_pos += topline_diff;
4113 topline = curwin->w_scbind_pos;
4114 if (topline > curbuf->b_ml.ml_line_count)
4115 topline = curbuf->b_ml.ml_line_count;
4116 if (topline < 1)
4117 topline = 1;
4118
4119 y = topline - curwin->w_topline;
4120 if (y > 0)
4121 scrollup(y, FALSE);
4122 else
4123 scrolldown(-y, FALSE);
4124 }
4125
4126 redraw_later(VALID);
4127 cursor_correct();
4128#ifdef FEAT_WINDOWS
4129 curwin->w_redr_status = TRUE;
4130#endif
4131 }
4132
4133 /*
4134 * do the horizontal scroll
4135 */
4136 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4137 {
4138 curwin->w_leftcol = tgt_leftcol;
4139 leftcol_changed();
4140 }
4141 }
4142 }
4143
4144 /*
4145 * reset current-window
4146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 VIsual_select = old_VIsual_select;
4148 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 curwin = old_curwin;
4150 curbuf = old_curbuf;
4151}
4152#endif /* #ifdef FEAT_SCROLLBIND */
4153
4154/*
4155 * Command character that's ignored.
4156 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004157 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 static void
4160nv_ignore(cap)
4161 cmdarg_T *cap;
4162{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004163 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164}
4165
4166/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004167 * Command character that doesn't do anything, but unlike nv_ignore() does
4168 * start edit(). Used for "startinsert" executed while starting up.
4169 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004170 static void
4171nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004172 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004173{
4174}
4175
4176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * Command character doesn't exist.
4178 */
4179 static void
4180nv_error(cap)
4181 cmdarg_T *cap;
4182{
4183 clearopbeep(cap->oap);
4184}
4185
4186/*
4187 * <Help> and <F1> commands.
4188 */
4189 static void
4190nv_help(cap)
4191 cmdarg_T *cap;
4192{
4193 if (!checkclearopq(cap->oap))
4194 ex_help(NULL);
4195}
4196
4197/*
4198 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4199 */
4200 static void
4201nv_addsub(cap)
4202 cmdarg_T *cap;
4203{
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004204 int visual = VIsual_active;
4205 if (cap->oap->op_type == OP_NOP
4206 && do_addsub((int)cap->cmdchar, cap->count1, cap->arg) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 prep_redo_cmd(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004208 else
4209 clearopbeep(cap->oap);
4210 if (visual)
4211 {
4212 VIsual_active = FALSE;
4213 redraw_later(CLEAR);
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215}
4216
4217/*
4218 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4219 */
4220 static void
4221nv_page(cap)
4222 cmdarg_T *cap;
4223{
4224 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004225 {
4226#ifdef FEAT_WINDOWS
4227 if (mod_mask & MOD_MASK_CTRL)
4228 {
4229 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4230 if (cap->arg == BACKWARD)
4231 goto_tabpage(-(int)cap->count1);
4232 else
4233 goto_tabpage((int)cap->count0);
4234 }
4235 else
4236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
4242 * Implementation of "gd" and "gD" command.
4243 */
4244 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004245nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004246 oparg_T *oap;
4247 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004248 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 char_u *ptr;
4252
Bram Moolenaard9d30582005-05-18 22:10:28 +00004253 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004254 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004256#ifdef FEAT_FOLDING
4257 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4258 foldOpenCursor();
4259#endif
4260}
4261
4262/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004263 * Search for variable declaration of "ptr[len]".
4264 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4265 * current file ("gD").
4266 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 * Return FAIL when not found.
4268 */
4269 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004270find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 char_u *ptr;
4272 int len;
4273 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004274 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004275 int searchflags; /* flags passed to searchit() */
4276{
4277 char_u *pat;
4278 pos_T old_pos;
4279 pos_T par_pos;
4280 pos_T found_pos;
4281 int t;
4282 int save_p_ws;
4283 int save_p_scs;
4284 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004285 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004286
4287 if ((pat = alloc(len + 7)) == NULL)
4288 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004289
4290 /* Put "\V" before the pattern to avoid that the special meaning of "."
4291 * and "~" causes trouble. */
4292 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4293 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 old_pos = curwin->w_cursor;
4295 save_p_ws = p_ws;
4296 save_p_scs = p_scs;
4297 p_ws = FALSE; /* don't wrap around end of file now */
4298 p_scs = FALSE; /* don't switch ignorecase off now */
4299
4300 /*
4301 * With "gD" go to line 1.
4302 * With "gd" Search back for the start of the current function, then go
4303 * back until a blank line. If this fails go to line 1.
4304 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004305 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 setpcmark(); /* Set in findpar() otherwise */
4308 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004309 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else
4312 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004313 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4315 --curwin->w_cursor.lnum;
4316 }
4317 curwin->w_cursor.col = 0;
4318
4319 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004320 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004321 for (;;)
4322 {
4323 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004324 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004325 if (curwin->w_cursor.lnum >= old_pos.lnum)
4326 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004327
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004328 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004329 {
4330 pos_T *pos;
4331
4332 /* Check that the block the match is in doesn't end before the
4333 * position where we started the search from. */
4334 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4335 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4336 && pos->lnum < old_pos.lnum)
4337 continue;
4338 }
4339
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004340 if (t == FAIL)
4341 {
4342 /* If we previously found a valid position, use it. */
4343 if (found_pos.lnum != 0)
4344 {
4345 curwin->w_cursor = found_pos;
4346 t = OK;
4347 }
4348 break;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004351 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004352 {
4353 /* Ignore this line, continue at start of next line. */
4354 ++curwin->w_cursor.lnum;
4355 curwin->w_cursor.col = 0;
4356 continue;
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004359 if (!locally) /* global search: use first match found */
4360 break;
4361 if (curwin->w_cursor.lnum >= par_pos.lnum)
4362 {
4363 /* If we previously found a valid position, use it. */
4364 if (found_pos.lnum != 0)
4365 curwin->w_cursor = found_pos;
4366 break;
4367 }
4368
4369 /* For finding a local variable and the match is before the "{" search
4370 * to find a later match. For K&R style function declarations this
4371 * skips the function header without types. */
4372 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004374
4375 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 curwin->w_cursor = old_pos;
4379 }
4380 else
4381 {
4382 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /* "n" searches forward now */
4384 reset_search_dir();
4385 }
4386
4387 vim_free(pat);
4388 p_ws = save_p_ws;
4389 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004390
4391 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392}
4393
4394/*
4395 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4396 * lines rather than lines in the file.
4397 * 'dist' must be positive.
4398 *
4399 * Return OK if able to move cursor, FAIL otherwise.
4400 */
4401 static int
4402nv_screengo(oap, dir, dist)
4403 oparg_T *oap;
4404 int dir;
4405 long dist;
4406{
4407 int linelen = linetabsize(ml_get_curline());
4408 int retval = OK;
4409 int atend = FALSE;
4410 int n;
4411 int col_off1; /* margin offset for first screen line */
4412 int col_off2; /* margin offset for wrapped screen line */
4413 int width1; /* text width for first screen line */
4414 int width2; /* test width for wrapped screen line */
4415
4416 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004417 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
4419 col_off1 = curwin_col_off();
4420 col_off2 = col_off1 - curwin_col_off2();
4421 width1 = W_WIDTH(curwin) - col_off1;
4422 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004423 if (width2 == 0)
4424 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426#ifdef FEAT_VERTSPLIT
4427 if (curwin->w_width != 0)
4428 {
4429#endif
4430 /*
4431 * Instead of sticking at the last character of the buffer line we
4432 * try to stick in the last column of the screen.
4433 */
4434 if (curwin->w_curswant == MAXCOL)
4435 {
4436 atend = TRUE;
4437 validate_virtcol();
4438 if (width1 <= 0)
4439 curwin->w_curswant = 0;
4440 else
4441 {
4442 curwin->w_curswant = width1 - 1;
4443 if (curwin->w_virtcol > curwin->w_curswant)
4444 curwin->w_curswant += ((curwin->w_virtcol
4445 - curwin->w_curswant - 1) / width2 + 1) * width2;
4446 }
4447 }
4448 else
4449 {
4450 if (linelen > width1)
4451 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4452 else
4453 n = width1;
4454 if (curwin->w_curswant > (colnr_T)n + 1)
4455 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4456 * width2;
4457 }
4458
4459 while (dist--)
4460 {
4461 if (dir == BACKWARD)
4462 {
4463 if ((long)curwin->w_curswant >= width2)
4464 /* move back within line */
4465 curwin->w_curswant -= width2;
4466 else
4467 {
4468 /* to previous line */
4469 if (curwin->w_cursor.lnum == 1)
4470 {
4471 retval = FAIL;
4472 break;
4473 }
4474 --curwin->w_cursor.lnum;
4475#ifdef FEAT_FOLDING
4476 /* Move to the start of a closed fold. Don't do that when
4477 * 'foldopen' contains "all": it will open in a moment. */
4478 if (!(fdo_flags & FDO_ALL))
4479 (void)hasFolding(curwin->w_cursor.lnum,
4480 &curwin->w_cursor.lnum, NULL);
4481#endif
4482 linelen = linetabsize(ml_get_curline());
4483 if (linelen > width1)
4484 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4485 + 1) * width2;
4486 }
4487 }
4488 else /* dir == FORWARD */
4489 {
4490 if (linelen > width1)
4491 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4492 else
4493 n = width1;
4494 if (curwin->w_curswant + width2 < (colnr_T)n)
4495 /* move forward within line */
4496 curwin->w_curswant += width2;
4497 else
4498 {
4499 /* to next line */
4500#ifdef FEAT_FOLDING
4501 /* Move to the end of a closed fold. */
4502 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4503 &curwin->w_cursor.lnum);
4504#endif
4505 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4506 {
4507 retval = FAIL;
4508 break;
4509 }
4510 curwin->w_cursor.lnum++;
4511 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004512 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 }
4515 }
4516#ifdef FEAT_VERTSPLIT
4517 }
4518#endif
4519
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004520 if (virtual_active() && atend)
4521 coladvance(MAXCOL);
4522 else
4523 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524
4525#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4526 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4527 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004528 colnr_T virtcol;
4529
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 /*
4531 * Check for landing on a character that got split at the end of the
4532 * last line. We want to advance a screenline, not end up in the same
4533 * screenline or move two screenlines.
4534 */
4535 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004536 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004537# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004538 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4539 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004540# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004541
4542 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 && (curwin->w_curswant < (colnr_T)width1
4544 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4545 : ((curwin->w_curswant - width1) % width2
4546 > (colnr_T)width2 / 2)))
4547 --curwin->w_cursor.col;
4548 }
4549#endif
4550
4551 if (atend)
4552 curwin->w_curswant = MAXCOL; /* stick in the last column */
4553
4554 return retval;
4555}
4556
4557#ifdef FEAT_MOUSE
4558/*
4559 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4560 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004561 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4562 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
4564 static void
4565nv_mousescroll(cap)
4566 cmdarg_T *cap;
4567{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004568# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004569 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004571 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 int row, col;
4574
4575 row = mouse_row;
4576 col = mouse_col;
4577
4578 /* find the window at the pointer coordinates */
4579 curwin = mouse_find_win(&row, &col);
4580 curbuf = curwin->w_buffer;
4581 }
4582# endif
4583
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004584 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004586 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4587 {
4588 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4589 }
4590 else
4591 {
4592 cap->count1 = 3;
4593 cap->count0 = 3;
4594 nv_scroll_line(cap);
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004597# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004600 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4601 if (!curwin->w_p_wrap)
4602 {
4603 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004604
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004605 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4606 step = W_WIDTH(curwin);
4607 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4608 if (val < 0)
4609 val = 0;
4610
4611 gui_do_horiz_scroll(val, TRUE);
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004616# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 curwin->w_redr_status = TRUE;
4618
4619 curwin = old_curwin;
4620 curbuf = curwin->w_buffer;
4621# endif
4622}
4623
4624/*
4625 * Mouse clicks and drags.
4626 */
4627 static void
4628nv_mouse(cap)
4629 cmdarg_T *cap;
4630{
4631 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4632}
4633#endif
4634
4635/*
4636 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4637 * cap->arg must be TRUE for CTRL-E.
4638 */
4639 static void
4640nv_scroll_line(cap)
4641 cmdarg_T *cap;
4642{
4643 if (!checkclearop(cap->oap))
4644 scroll_redraw(cap->arg, cap->count1);
4645}
4646
4647/*
4648 * Scroll "count" lines up or down, and redraw.
4649 */
4650 void
4651scroll_redraw(up, count)
4652 int up;
4653 long count;
4654{
4655 linenr_T prev_topline = curwin->w_topline;
4656#ifdef FEAT_DIFF
4657 int prev_topfill = curwin->w_topfill;
4658#endif
4659 linenr_T prev_lnum = curwin->w_cursor.lnum;
4660
4661 if (up)
4662 scrollup(count, TRUE);
4663 else
4664 scrolldown(count, TRUE);
4665 if (p_so)
4666 {
4667 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4668 * valid, otherwise the screen jumps back at the end of the file. */
4669 cursor_correct();
4670 check_cursor_moved(curwin);
4671 curwin->w_valid |= VALID_TOPLINE;
4672
4673 /* If moved back to where we were, at least move the cursor, otherwise
4674 * we get stuck at one position. Don't move the cursor up if the
4675 * first line of the buffer is already on the screen */
4676 while (curwin->w_topline == prev_topline
4677#ifdef FEAT_DIFF
4678 && curwin->w_topfill == prev_topfill
4679#endif
4680 )
4681 {
4682 if (up)
4683 {
4684 if (curwin->w_cursor.lnum > prev_lnum
4685 || cursor_down(1L, FALSE) == FAIL)
4686 break;
4687 }
4688 else
4689 {
4690 if (curwin->w_cursor.lnum < prev_lnum
4691 || prev_topline == 1L
4692 || cursor_up(1L, FALSE) == FAIL)
4693 break;
4694 }
4695 /* Mark w_topline as valid, otherwise the screen jumps back at the
4696 * end of the file. */
4697 check_cursor_moved(curwin);
4698 curwin->w_valid |= VALID_TOPLINE;
4699 }
4700 }
4701 if (curwin->w_cursor.lnum != prev_lnum)
4702 coladvance(curwin->w_curswant);
4703 redraw_later(VALID);
4704}
4705
4706/*
4707 * Commands that start with "z".
4708 */
4709 static void
4710nv_zet(cap)
4711 cmdarg_T *cap;
4712{
4713 long n;
4714 colnr_T col;
4715 int nchar = cap->nchar;
4716#ifdef FEAT_FOLDING
4717 long old_fdl = curwin->w_p_fdl;
4718 int old_fen = curwin->w_p_fen;
4719#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004720#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004721 int undo = FALSE;
4722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
4724 if (VIM_ISDIGIT(nchar))
4725 {
4726 /*
4727 * "z123{nchar}": edit the count before obtaining {nchar}
4728 */
4729 if (checkclearop(cap->oap))
4730 return;
4731 n = nchar - '0';
4732 for (;;)
4733 {
4734#ifdef USE_ON_FLY_SCROLL
4735 dont_scroll = TRUE; /* disallow scrolling here */
4736#endif
4737 ++no_mapping;
4738 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004739 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 --no_mapping;
4742 --allow_keys;
4743#ifdef FEAT_CMDL_INFO
4744 (void)add_to_showcmd(nchar);
4745#endif
4746 if (nchar == K_DEL || nchar == K_KDEL)
4747 n /= 10;
4748 else if (VIM_ISDIGIT(nchar))
4749 n = n * 10 + (nchar - '0');
4750 else if (nchar == CAR)
4751 {
4752#ifdef FEAT_GUI
4753 need_mouse_correct = TRUE;
4754#endif
4755 win_setheight((int)n);
4756 break;
4757 }
4758 else if (nchar == 'l'
4759 || nchar == 'h'
4760 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004761 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 cap->count1 = n ? n * cap->count1 : cap->count1;
4764 goto dozet;
4765 }
4766 else
4767 {
4768 clearopbeep(cap->oap);
4769 break;
4770 }
4771 }
4772 cap->oap->op_type = OP_NOP;
4773 return;
4774 }
4775
4776dozet:
4777 if (
4778#ifdef FEAT_FOLDING
4779 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4780 * and "zC" only in Visual mode. "zj" and "zk" are motion
4781 * commands. */
4782 cap->nchar != 'f' && cap->nchar != 'F'
4783 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4784 && cap->nchar != 'j' && cap->nchar != 'k'
4785 &&
4786#endif
4787 checkclearop(cap->oap))
4788 return;
4789
4790 /*
4791 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4792 * If line number given, set cursor.
4793 */
4794 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4795 && cap->count0
4796 && cap->count0 != curwin->w_cursor.lnum)
4797 {
4798 setpcmark();
4799 if (cap->count0 > curbuf->b_ml.ml_line_count)
4800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4801 else
4802 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004803 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805
4806 switch (nchar)
4807 {
4808 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4809 case '+':
4810 if (cap->count0 == 0)
4811 {
4812 /* No count given: put cursor at the line below screen */
4813 validate_botline(); /* make sure w_botline is valid */
4814 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4815 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4816 else
4817 curwin->w_cursor.lnum = curwin->w_botline;
4818 }
4819 /* FALLTHROUGH */
4820 case NL:
4821 case CAR:
4822 case K_KENTER:
4823 beginline(BL_WHITE | BL_FIX);
4824 /* FALLTHROUGH */
4825
4826 case 't': scroll_cursor_top(0, TRUE);
4827 redraw_later(VALID);
4828 break;
4829
4830 /* "z." and "zz": put cursor in middle of screen */
4831 case '.': beginline(BL_WHITE | BL_FIX);
4832 /* FALLTHROUGH */
4833
4834 case 'z': scroll_cursor_halfway(TRUE);
4835 redraw_later(VALID);
4836 break;
4837
4838 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4839 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4840 * when <count> is at bottom of window, and puts that one at
4841 * bottom of window. */
4842 if (cap->count0 != 0)
4843 {
4844 scroll_cursor_bot(0, TRUE);
4845 curwin->w_cursor.lnum = curwin->w_topline;
4846 }
4847 else if (curwin->w_topline == 1)
4848 curwin->w_cursor.lnum = 1;
4849 else
4850 curwin->w_cursor.lnum = curwin->w_topline - 1;
4851 /* FALLTHROUGH */
4852 case '-':
4853 beginline(BL_WHITE | BL_FIX);
4854 /* FALLTHROUGH */
4855
4856 case 'b': scroll_cursor_bot(0, TRUE);
4857 redraw_later(VALID);
4858 break;
4859
4860 /* "zH" - scroll screen right half-page */
4861 case 'H':
4862 cap->count1 *= W_WIDTH(curwin) / 2;
4863 /* FALLTHROUGH */
4864
4865 /* "zh" - scroll screen to the right */
4866 case 'h':
4867 case K_LEFT:
4868 if (!curwin->w_p_wrap)
4869 {
4870 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4871 curwin->w_leftcol = 0;
4872 else
4873 curwin->w_leftcol -= (colnr_T)cap->count1;
4874 leftcol_changed();
4875 }
4876 break;
4877
4878 /* "zL" - scroll screen left half-page */
4879 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4880 /* FALLTHROUGH */
4881
4882 /* "zl" - scroll screen to the left */
4883 case 'l':
4884 case K_RIGHT:
4885 if (!curwin->w_p_wrap)
4886 {
4887 /* scroll the window left */
4888 curwin->w_leftcol += (colnr_T)cap->count1;
4889 leftcol_changed();
4890 }
4891 break;
4892
4893 /* "zs" - scroll screen, cursor at the start */
4894 case 's': if (!curwin->w_p_wrap)
4895 {
4896#ifdef FEAT_FOLDING
4897 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4898 col = 0; /* like the cursor is in col 0 */
4899 else
4900#endif
4901 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4902 if ((long)col > p_siso)
4903 col -= p_siso;
4904 else
4905 col = 0;
4906 if (curwin->w_leftcol != col)
4907 {
4908 curwin->w_leftcol = col;
4909 redraw_later(NOT_VALID);
4910 }
4911 }
4912 break;
4913
4914 /* "ze" - scroll screen, cursor at the end */
4915 case 'e': if (!curwin->w_p_wrap)
4916 {
4917#ifdef FEAT_FOLDING
4918 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4919 col = 0; /* like the cursor is in col 0 */
4920 else
4921#endif
4922 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4923 n = W_WIDTH(curwin) - curwin_col_off();
4924 if ((long)col + p_siso < n)
4925 col = 0;
4926 else
4927 col = col + p_siso - n + 1;
4928 if (curwin->w_leftcol != col)
4929 {
4930 curwin->w_leftcol = col;
4931 redraw_later(NOT_VALID);
4932 }
4933 }
4934 break;
4935
4936#ifdef FEAT_FOLDING
4937 /* "zF": create fold command */
4938 /* "zf": create fold operator */
4939 case 'F':
4940 case 'f': if (foldManualAllowed(TRUE))
4941 {
4942 cap->nchar = 'f';
4943 nv_operator(cap);
4944 curwin->w_p_fen = TRUE;
4945
4946 /* "zF" is like "zfzf" */
4947 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4948 {
4949 nv_operator(cap);
4950 finish_op = TRUE;
4951 }
4952 }
4953 else
4954 clearopbeep(cap->oap);
4955 break;
4956
4957 /* "zd": delete fold at cursor */
4958 /* "zD": delete fold at cursor recursively */
4959 case 'd':
4960 case 'D': if (foldManualAllowed(FALSE))
4961 {
4962 if (VIsual_active)
4963 nv_operator(cap);
4964 else
4965 deleteFold(curwin->w_cursor.lnum,
4966 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4967 }
4968 break;
4969
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004970 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 case 'E': if (foldmethodIsManual(curwin))
4972 {
4973 clearFolding(curwin);
4974 changed_window_setting();
4975 }
4976 else if (foldmethodIsMarker(curwin))
4977 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4978 TRUE, FALSE);
4979 else
4980 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4981 break;
4982
4983 /* "zn": fold none: reset 'foldenable' */
4984 case 'n': curwin->w_p_fen = FALSE;
4985 break;
4986
4987 /* "zN": fold Normal: set 'foldenable' */
4988 case 'N': curwin->w_p_fen = TRUE;
4989 break;
4990
4991 /* "zi": invert folding: toggle 'foldenable' */
4992 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4993 break;
4994
4995 /* "za": open closed fold or close open fold at cursor */
4996 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4997 openFold(curwin->w_cursor.lnum, cap->count1);
4998 else
4999 {
5000 closeFold(curwin->w_cursor.lnum, cap->count1);
5001 curwin->w_p_fen = TRUE;
5002 }
5003 break;
5004
5005 /* "zA": open fold at cursor recursively */
5006 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5007 openFoldRecurse(curwin->w_cursor.lnum);
5008 else
5009 {
5010 closeFoldRecurse(curwin->w_cursor.lnum);
5011 curwin->w_p_fen = TRUE;
5012 }
5013 break;
5014
5015 /* "zo": open fold at cursor or Visual area */
5016 case 'o': if (VIsual_active)
5017 nv_operator(cap);
5018 else
5019 openFold(curwin->w_cursor.lnum, cap->count1);
5020 break;
5021
5022 /* "zO": open fold recursively */
5023 case 'O': if (VIsual_active)
5024 nv_operator(cap);
5025 else
5026 openFoldRecurse(curwin->w_cursor.lnum);
5027 break;
5028
5029 /* "zc": close fold at cursor or Visual area */
5030 case 'c': if (VIsual_active)
5031 nv_operator(cap);
5032 else
5033 closeFold(curwin->w_cursor.lnum, cap->count1);
5034 curwin->w_p_fen = TRUE;
5035 break;
5036
5037 /* "zC": close fold recursively */
5038 case 'C': if (VIsual_active)
5039 nv_operator(cap);
5040 else
5041 closeFoldRecurse(curwin->w_cursor.lnum);
5042 curwin->w_p_fen = TRUE;
5043 break;
5044
5045 /* "zv": open folds at the cursor */
5046 case 'v': foldOpenCursor();
5047 break;
5048
5049 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5050 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005051 curwin->w_foldinvalid = TRUE; /* recompute folds */
5052 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 foldOpenCursor();
5054 break;
5055
5056 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5057 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005058 curwin->w_foldinvalid = TRUE; /* recompute folds */
5059 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061
5062 /* "zm": fold more */
5063 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005064 {
5065 curwin->w_p_fdl -= cap->count1;
5066 if (curwin->w_p_fdl < 0)
5067 curwin->w_p_fdl = 0;
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 old_fdl = -1; /* force an update */
5070 curwin->w_p_fen = TRUE;
5071 break;
5072
5073 /* "zM": close all folds */
5074 case 'M': curwin->w_p_fdl = 0;
5075 old_fdl = -1; /* force an update */
5076 curwin->w_p_fen = TRUE;
5077 break;
5078
5079 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005080 case 'r': curwin->w_p_fdl += cap->count1;
5081 {
5082 int d = getDeepestNesting();
5083
5084 if (curwin->w_p_fdl >= d)
5085 curwin->w_p_fdl = d;
5086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 break;
5088
5089 /* "zR": open all folds */
5090 case 'R': curwin->w_p_fdl = getDeepestNesting();
5091 old_fdl = -1; /* force an update */
5092 break;
5093
5094 case 'j': /* "zj" move to next fold downwards */
5095 case 'k': /* "zk" move to next fold upwards */
5096 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5097 cap->count1) == FAIL)
5098 clearopbeep(cap->oap);
5099 break;
5100
5101#endif /* FEAT_FOLDING */
5102
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005103#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005104 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5105 ++no_mapping;
5106 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005107 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005108 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005109 --no_mapping;
5110 --allow_keys;
5111#ifdef FEAT_CMDL_INFO
5112 (void)add_to_showcmd(nchar);
5113#endif
5114 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5115 {
5116 clearopbeep(cap->oap);
5117 break;
5118 }
5119 undo = TRUE;
5120 /*FALLTHROUGH*/
5121
Bram Moolenaarb765d632005-06-07 21:00:02 +00005122 case 'g': /* "zg": add good word to word list */
5123 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005124 case 'G': /* "zG": add good word to temp word list */
5125 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126 {
5127 char_u *ptr = NULL;
5128 int len;
5129
5130 if (checkclearop(cap->oap))
5131 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005132 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5133 == FAIL)
5134 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005135 if (ptr == NULL)
5136 {
5137 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005138
Bram Moolenaar134bf072013-09-25 18:54:24 +02005139 /* Find bad word under the cursor. When 'spell' is
5140 * off this fails and find_ident_under_cursor() is
5141 * used below. */
5142 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005143 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005144 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005145 if (len != 0 && curwin->w_cursor.col <= pos.col)
5146 ptr = ml_get_pos(&curwin->w_cursor);
5147 curwin->w_cursor = pos;
5148 }
5149
Bram Moolenaarb765d632005-06-07 21:00:02 +00005150 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5151 FIND_IDENT)) == 0)
5152 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005153 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005154 (nchar == 'G' || nchar == 'W')
5155 ? 0 : (int)cap->count1,
5156 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005157 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005158 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005159
Bram Moolenaar43abc522005-12-10 20:15:02 +00005160 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005161 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005162 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005163 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005164#endif
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 default: clearopbeep(cap->oap);
5167 }
5168
5169#ifdef FEAT_FOLDING
5170 /* Redraw when 'foldenable' changed */
5171 if (old_fen != curwin->w_p_fen)
5172 {
5173# ifdef FEAT_DIFF
5174 win_T *wp;
5175
5176 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5177 {
5178 /* Adjust 'foldenable' in diff-synced windows. */
5179 FOR_ALL_WINDOWS(wp)
5180 {
5181 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5182 {
5183 wp->w_p_fen = curwin->w_p_fen;
5184 changed_window_setting_win(wp);
5185 }
5186 }
5187 }
5188# endif
5189 changed_window_setting();
5190 }
5191
5192 /* Redraw when 'foldlevel' changed. */
5193 if (old_fdl != curwin->w_p_fdl)
5194 newFoldLevel();
5195#endif
5196}
5197
5198#ifdef FEAT_GUI
5199/*
5200 * Vertical scrollbar movement.
5201 */
5202 static void
5203nv_ver_scrollbar(cap)
5204 cmdarg_T *cap;
5205{
5206 if (cap->oap->op_type != OP_NOP)
5207 clearopbeep(cap->oap);
5208
5209 /* Even if an operator was pending, we still want to scroll */
5210 gui_do_scroll();
5211}
5212
5213/*
5214 * Horizontal scrollbar movement.
5215 */
5216 static void
5217nv_hor_scrollbar(cap)
5218 cmdarg_T *cap;
5219{
5220 if (cap->oap->op_type != OP_NOP)
5221 clearopbeep(cap->oap);
5222
5223 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005224 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225}
5226#endif
5227
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005228#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005229/*
5230 * Click in GUI tab.
5231 */
5232 static void
5233nv_tabline(cap)
5234 cmdarg_T *cap;
5235{
5236 if (cap->oap->op_type != OP_NOP)
5237 clearopbeep(cap->oap);
5238
5239 /* Even if an operator was pending, we still want to jump tabs. */
5240 goto_tabpage(current_tab);
5241}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005242
5243/*
5244 * Selected item in tab line menu.
5245 */
5246 static void
5247nv_tabmenu(cap)
5248 cmdarg_T *cap;
5249{
5250 if (cap->oap->op_type != OP_NOP)
5251 clearopbeep(cap->oap);
5252
5253 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005254 handle_tabmenu();
5255}
5256
5257/*
5258 * Handle selecting an item of the GUI tab line menu.
5259 * Used in Normal and Insert mode.
5260 */
5261 void
5262handle_tabmenu()
5263{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005264 switch (current_tabmenu)
5265 {
5266 case TABLINE_MENU_CLOSE:
5267 if (current_tab == 0)
5268 do_cmdline_cmd((char_u *)"tabclose");
5269 else
5270 {
5271 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5272 current_tab);
5273 do_cmdline_cmd(IObuff);
5274 }
5275 break;
5276
5277 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005278 if (current_tab == 0)
5279 do_cmdline_cmd((char_u *)"$tabnew");
5280 else
5281 {
5282 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5283 current_tab - 1);
5284 do_cmdline_cmd(IObuff);
5285 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005286 break;
5287
5288 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005289 if (current_tab == 0)
5290 do_cmdline_cmd((char_u *)"browse $tabnew");
5291 else
5292 {
5293 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5294 current_tab - 1);
5295 do_cmdline_cmd(IObuff);
5296 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005297 break;
5298 }
5299}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005300#endif
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302/*
5303 * "Q" command.
5304 */
5305 static void
5306nv_exmode(cap)
5307 cmdarg_T *cap;
5308{
5309 /*
5310 * Ignore 'Q' in Visual mode, just give a beep.
5311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 if (VIsual_active)
5313 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005314 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 do_exmode(FALSE);
5316}
5317
5318/*
5319 * Handle a ":" command.
5320 */
5321 static void
5322nv_colon(cap)
5323 cmdarg_T *cap;
5324{
5325 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005326 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (VIsual_active)
5329 nv_operator(cap);
5330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 if (cap->oap->op_type != OP_NOP)
5333 {
5334 /* Using ":" as a movement is characterwise exclusive. */
5335 cap->oap->motion_type = MCHAR;
5336 cap->oap->inclusive = FALSE;
5337 }
5338 else if (cap->count0)
5339 {
5340 /* translate "count:" into ":.,.+(count - 1)" */
5341 stuffcharReadbuff('.');
5342 if (cap->count0 > 1)
5343 {
5344 stuffReadbuff((char_u *)",.+");
5345 stuffnumReadbuff((long)cap->count0 - 1L);
5346 }
5347 }
5348
5349 /* When typing, don't type below an old message */
5350 if (KeyTyped)
5351 compute_cmdrow();
5352
5353 old_p_im = p_im;
5354
5355 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005356 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5358
5359 /* If 'insertmode' changed, enter or exit Insert mode */
5360 if (p_im != old_p_im)
5361 {
5362 if (p_im)
5363 restart_edit = 'i';
5364 else
5365 restart_edit = 0;
5366 }
5367
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005368 if (cmd_result == FAIL)
5369 /* The Ex command failed, do not execute the operator. */
5370 clearop(cap->oap);
5371 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5373 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005374 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5375 || did_emsg
5376 ))
5377 /* The start of the operator has become invalid by the Ex command.
5378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 clearopbeep(cap->oap);
5380 }
5381}
5382
5383/*
5384 * Handle CTRL-G command.
5385 */
5386 static void
5387nv_ctrlg(cap)
5388 cmdarg_T *cap;
5389{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 if (VIsual_active) /* toggle Selection/Visual mode */
5391 {
5392 VIsual_select = !VIsual_select;
5393 showmode();
5394 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005395 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 /* print full name if count given or :cd used */
5397 fileinfo((int)cap->count0, FALSE, TRUE);
5398}
5399
5400/*
5401 * Handle CTRL-H <Backspace> command.
5402 */
5403 static void
5404nv_ctrlh(cap)
5405 cmdarg_T *cap;
5406{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (VIsual_active && VIsual_select)
5408 {
5409 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5410 v_visop(cap);
5411 }
5412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 nv_left(cap);
5414}
5415
5416/*
5417 * CTRL-L: clear screen and redraw.
5418 */
5419 static void
5420nv_clear(cap)
5421 cmdarg_T *cap;
5422{
5423 if (!checkclearop(cap->oap))
5424 {
5425#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5426 /*
5427 * Right now, the BeBox doesn't seem to have an easy way to detect
5428 * window resizing, so we cheat and make the user detect it
5429 * manually with CTRL-L instead
5430 */
5431 ui_get_shellsize();
5432#endif
5433#ifdef FEAT_SYN_HL
5434 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005435 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437 redraw_later(CLEAR);
5438 }
5439}
5440
5441/*
5442 * CTRL-O: In Select mode: switch to Visual mode for one command.
5443 * Otherwise: Go to older pcmark.
5444 */
5445 static void
5446nv_ctrlo(cap)
5447 cmdarg_T *cap;
5448{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 if (VIsual_active && VIsual_select)
5450 {
5451 VIsual_select = FALSE;
5452 showmode();
5453 restart_VIsual_select = 2; /* restart Select mode later */
5454 }
5455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 cap->count1 = -cap->count1;
5458 nv_pcmark(cap);
5459 }
5460}
5461
5462/*
5463 * CTRL-^ command, short for ":e #"
5464 */
5465 static void
5466nv_hat(cap)
5467 cmdarg_T *cap;
5468{
5469 if (!checkclearopq(cap->oap))
5470 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5471 GETF_SETMARK|GETF_ALT, FALSE);
5472}
5473
5474/*
5475 * "Z" commands.
5476 */
5477 static void
5478nv_Zet(cap)
5479 cmdarg_T *cap;
5480{
5481 if (!checkclearopq(cap->oap))
5482 {
5483 switch (cap->nchar)
5484 {
5485 /* "ZZ": equivalent to ":x". */
5486 case 'Z': do_cmdline_cmd((char_u *)"x");
5487 break;
5488
5489 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5490 case 'Q': do_cmdline_cmd((char_u *)"q!");
5491 break;
5492
5493 default: clearopbeep(cap->oap);
5494 }
5495 }
5496}
5497
5498#if defined(FEAT_WINDOWS) || defined(PROTO)
5499/*
5500 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5501 */
5502 void
5503do_nv_ident(c1, c2)
5504 int c1;
5505 int c2;
5506{
5507 oparg_T oa;
5508 cmdarg_T ca;
5509
5510 clear_oparg(&oa);
5511 vim_memset(&ca, 0, sizeof(ca));
5512 ca.oap = &oa;
5513 ca.cmdchar = c1;
5514 ca.nchar = c2;
5515 nv_ident(&ca);
5516}
5517#endif
5518
5519/*
5520 * Handle the commands that use the word under the cursor.
5521 * [g] CTRL-] :ta to current identifier
5522 * [g] 'K' run program for current identifier
5523 * [g] '*' / to current identifier or string
5524 * [g] '#' ? to current identifier or string
5525 * g ']' :tselect for current identifier
5526 */
5527 static void
5528nv_ident(cap)
5529 cmdarg_T *cap;
5530{
5531 char_u *ptr = NULL;
5532 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005533 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u *p;
5535 char_u *kp; /* value of 'keywordprg' */
5536 int kp_help; /* 'keywordprg' is ":help" */
5537 int n = 0; /* init for GCC */
5538 int cmdchar;
5539 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005540 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 char_u *aux_ptr;
5542 int isman;
5543 int isman_s;
5544
5545 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5546 {
5547 cmdchar = cap->nchar;
5548 g_cmd = TRUE;
5549 }
5550 else
5551 {
5552 cmdchar = cap->cmdchar;
5553 g_cmd = FALSE;
5554 }
5555
5556 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5557 cmdchar = '#';
5558
5559 /*
5560 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5561 */
5562 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5565 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (checkclearopq(cap->oap))
5567 return;
5568 }
5569
5570 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5571 (cmdchar == '*' || cmdchar == '#')
5572 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5573 {
5574 clearop(cap->oap);
5575 return;
5576 }
5577
5578 /* Allocate buffer to put the command in. Inserting backslashes can
5579 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5580 * and some numbers. */
5581 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5582 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5583 || STRCMP(kp, ":help") == 0);
5584 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5585 if (buf == NULL)
5586 return;
5587 buf[0] = NUL;
5588
5589 switch (cmdchar)
5590 {
5591 case '*':
5592 case '#':
5593 /*
5594 * Put cursor at start of word, makes search skip the word
5595 * under the cursor.
5596 * Call setpcmark() first, so "*``" puts the cursor back where
5597 * it was.
5598 */
5599 setpcmark();
5600 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5601
5602 if (!g_cmd && vim_iswordp(ptr))
5603 STRCPY(buf, "\\<");
5604 no_smartcase = TRUE; /* don't use 'smartcase' now */
5605 break;
5606
5607 case 'K':
5608 if (kp_help)
5609 STRCPY(buf, "he! ");
5610 else
5611 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005612 /* An external command will probably use an argument starting
5613 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005614 while (*ptr == '-' && n > 0)
5615 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005616 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005617 --n;
5618 }
5619 if (n == 0)
5620 {
5621 EMSG(_(e_noident)); /* found dashes only */
5622 vim_free(buf);
5623 return;
5624 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /* When a count is given, turn it into a range. Is this
5627 * really what we want? */
5628 isman = (STRCMP(kp, "man") == 0);
5629 isman_s = (STRCMP(kp, "man -s") == 0);
5630 if (cap->count0 != 0 && !(isman || isman_s))
5631 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5632
5633 STRCAT(buf, "! ");
5634 if (cap->count0 == 0 && isman_s)
5635 STRCAT(buf, "man");
5636 else
5637 STRCAT(buf, kp);
5638 STRCAT(buf, " ");
5639 if (cap->count0 != 0 && (isman || isman_s))
5640 {
5641 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5642 STRCAT(buf, " ");
5643 }
5644 }
5645 break;
5646
5647 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005648 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#ifdef FEAT_CSCOPE
5650 if (p_cst)
5651 STRCPY(buf, "cstag ");
5652 else
5653#endif
5654 STRCPY(buf, "ts ");
5655 break;
5656
5657 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005658 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (curbuf->b_help)
5660 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005662 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005663 if (g_cmd)
5664 STRCPY(buf, "tj ");
5665 else
5666 sprintf((char *)buf, "%ldta ", cap->count0);
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
5670 /*
5671 * Now grab the chars in the identifier
5672 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005675 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005676 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005677 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005678 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005679 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005681 vim_free(buf);
5682 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005684 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5685 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005686 {
5687 vim_free(buf);
5688 vim_free(p);
5689 return;
5690 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005691 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005692 STRCAT(buf, p);
5693 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695 else
5696 {
5697 if (cmdchar == '*')
5698 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5699 else if (cmdchar == '#')
5700 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005701 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005702 {
5703 if (curbuf->b_help)
5704 /* ":help" handles unescaped argument */
5705 aux_ptr = (char_u *)"";
5706 else
5707 aux_ptr = (char_u *)"\\|\"\n[";
5708 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005709 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005710 aux_ptr = (char_u *)"\\|\"\n*?[";
5711
5712 p = buf + STRLEN(buf);
5713 while (n-- > 0)
5714 {
5715 /* put a backslash before \ and some others */
5716 if (vim_strchr(aux_ptr, *ptr) != NULL)
5717 *p++ = '\\';
5718#ifdef FEAT_MBYTE
5719 /* When current byte is a part of multibyte character, copy all
5720 * bytes of that character. */
5721 if (has_mbyte)
5722 {
5723 int i;
5724 int len = (*mb_ptr2len)(ptr) - 1;
5725
5726 for (i = 0; i < len && n >= 1; ++i, --n)
5727 *p++ = *ptr++;
5728 }
5729#endif
5730 *p++ = *ptr++;
5731 }
5732 *p = NUL;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 /*
5736 * Execute the command.
5737 */
5738 if (cmdchar == '*' || cmdchar == '#')
5739 {
5740 if (!g_cmd && (
5741#ifdef FEAT_MBYTE
5742 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5743#endif
5744 vim_iswordc(ptr[-1])))
5745 STRCAT(buf, "\\>");
5746#ifdef FEAT_CMDHIST
5747 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005748 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5750#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005751 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
5753 else
5754 do_cmdline_cmd(buf);
5755
5756 vim_free(buf);
5757}
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759/*
5760 * Get visually selected text, within one line only.
5761 * Returns FAIL if more than one line selected.
5762 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005763 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764get_visual_text(cap, pp, lenp)
5765 cmdarg_T *cap;
5766 char_u **pp; /* return: start of selected text */
5767 int *lenp; /* return: length of selected text */
5768{
5769 if (VIsual_mode != 'V')
5770 unadjust_for_sel();
5771 if (VIsual.lnum != curwin->w_cursor.lnum)
5772 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005773 if (cap != NULL)
5774 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 return FAIL;
5776 }
5777 if (VIsual_mode == 'V')
5778 {
5779 *pp = ml_get_curline();
5780 *lenp = (int)STRLEN(*pp);
5781 }
5782 else
5783 {
5784 if (lt(curwin->w_cursor, VIsual))
5785 {
5786 *pp = ml_get_pos(&curwin->w_cursor);
5787 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5788 }
5789 else
5790 {
5791 *pp = ml_get_pos(&VIsual);
5792 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5793 }
5794#ifdef FEAT_MBYTE
5795 if (has_mbyte)
5796 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#endif
5799 }
5800 reset_VIsual_and_resel();
5801 return OK;
5802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804/*
5805 * CTRL-T: backwards in tag stack
5806 */
5807 static void
5808nv_tagpop(cap)
5809 cmdarg_T *cap;
5810{
5811 if (!checkclearopq(cap->oap))
5812 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5813}
5814
5815/*
5816 * Handle scrolling command 'H', 'L' and 'M'.
5817 */
5818 static void
5819nv_scroll(cap)
5820 cmdarg_T *cap;
5821{
5822 int used = 0;
5823 long n;
5824#ifdef FEAT_FOLDING
5825 linenr_T lnum;
5826#endif
5827 int half;
5828
5829 cap->oap->motion_type = MLINE;
5830 setpcmark();
5831
5832 if (cap->cmdchar == 'L')
5833 {
5834 validate_botline(); /* make sure curwin->w_botline is valid */
5835 curwin->w_cursor.lnum = curwin->w_botline - 1;
5836 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5837 curwin->w_cursor.lnum = 1;
5838 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005839 {
5840#ifdef FEAT_FOLDING
5841 if (hasAnyFolding(curwin))
5842 {
5843 /* Count a fold for one screen line. */
5844 for (n = cap->count1 - 1; n > 0
5845 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5846 {
5847 (void)hasFolding(curwin->w_cursor.lnum,
5848 &curwin->w_cursor.lnum, NULL);
5849 --curwin->w_cursor.lnum;
5850 }
5851 }
5852 else
5853#endif
5854 curwin->w_cursor.lnum -= cap->count1 - 1;
5855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857 else
5858 {
5859 if (cap->cmdchar == 'M')
5860 {
5861#ifdef FEAT_DIFF
5862 /* Don't count filler lines above the window. */
5863 used -= diff_check_fill(curwin, curwin->w_topline)
5864 - curwin->w_topfill;
5865#endif
5866 validate_botline(); /* make sure w_empty_rows is valid */
5867 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5868 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5869 {
5870#ifdef FEAT_DIFF
5871 /* Count half he number of filler lines to be "below this
5872 * line" and half to be "above the next line". */
5873 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5874 + n) / 2 >= half)
5875 {
5876 --n;
5877 break;
5878 }
5879#endif
5880 used += plines(curwin->w_topline + n);
5881 if (used >= half)
5882 break;
5883#ifdef FEAT_FOLDING
5884 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5885 n = lnum - curwin->w_topline;
5886#endif
5887 }
5888 if (n > 0 && used > curwin->w_height)
5889 --n;
5890 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005891 else /* (cap->cmdchar == 'H') */
5892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005894#ifdef FEAT_FOLDING
5895 if (hasAnyFolding(curwin))
5896 {
5897 /* Count a fold for one screen line. */
5898 lnum = curwin->w_topline;
5899 while (n-- > 0 && lnum < curwin->w_botline - 1)
5900 {
5901 hasFolding(lnum, NULL, &lnum);
5902 ++lnum;
5903 }
5904 n = lnum - curwin->w_topline;
5905 }
5906#endif
5907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 curwin->w_cursor.lnum = curwin->w_topline + n;
5909 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5910 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5911 }
5912
5913 cursor_correct(); /* correct for 'so' */
5914 beginline(BL_SOL | BL_FIX);
5915}
5916
5917/*
5918 * Cursor right commands.
5919 */
5920 static void
5921nv_right(cap)
5922 cmdarg_T *cap;
5923{
5924 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005925 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005927 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5928 {
5929 /* <C-Right> and <S-Right> move a word or WORD right */
5930 if (mod_mask & MOD_MASK_CTRL)
5931 cap->arg = TRUE;
5932 nv_wordcmd(cap);
5933 return;
5934 }
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 cap->oap->motion_type = MCHAR;
5937 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005938 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005940#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005942 * In virtual edit mode, there's no such thing as "past_line", as lines
5943 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 */
5945 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005946 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947#endif
5948
5949 for (n = cap->count1; n > 0; --n)
5950 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005951 if ((!past_line && oneright() == FAIL)
5952 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005953 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 {
5955 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005956 * <Space> wraps to next line if 'whichwrap' has 's'.
5957 * 'l' wraps to next line if 'whichwrap' has 'l'.
5958 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 */
5960 if ( ((cap->cmdchar == ' '
5961 && vim_strchr(p_ww, 's') != NULL)
5962 || (cap->cmdchar == 'l'
5963 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005964 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 && vim_strchr(p_ww, '>') != NULL))
5966 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5967 {
5968 /* When deleting we also count the NL as a character.
5969 * Set cap->oap->inclusive when last char in the line is
5970 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005971 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 && !cap->oap->inclusive
5973 && !lineempty(curwin->w_cursor.lnum))
5974 cap->oap->inclusive = TRUE;
5975 else
5976 {
5977 ++curwin->w_cursor.lnum;
5978 curwin->w_cursor.col = 0;
5979#ifdef FEAT_VIRTUALEDIT
5980 curwin->w_cursor.coladd = 0;
5981#endif
5982 curwin->w_set_curswant = TRUE;
5983 cap->oap->inclusive = FALSE;
5984 }
5985 continue;
5986 }
5987 if (cap->oap->op_type == OP_NOP)
5988 {
5989 /* Only beep and flush if not moved at all */
5990 if (n == cap->count1)
5991 beep_flush();
5992 }
5993 else
5994 {
5995 if (!lineempty(curwin->w_cursor.lnum))
5996 cap->oap->inclusive = TRUE;
5997 }
5998 break;
5999 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006000 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 {
6002 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006003#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 if (virtual_active())
6005 oneright();
6006 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006009#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 if (has_mbyte)
6011 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006012 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 ++curwin->w_cursor.col;
6016 }
6017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
6019#ifdef FEAT_FOLDING
6020 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6021 && cap->oap->op_type == OP_NOP)
6022 foldOpenCursor();
6023#endif
6024}
6025
6026/*
6027 * Cursor left commands.
6028 *
6029 * Returns TRUE when operator end should not be adjusted.
6030 */
6031 static void
6032nv_left(cap)
6033 cmdarg_T *cap;
6034{
6035 long n;
6036
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006037 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6038 {
6039 /* <C-Left> and <S-Left> move a word or WORD left */
6040 if (mod_mask & MOD_MASK_CTRL)
6041 cap->arg = 1;
6042 nv_bck_word(cap);
6043 return;
6044 }
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 cap->oap->motion_type = MCHAR;
6047 cap->oap->inclusive = FALSE;
6048 for (n = cap->count1; n > 0; --n)
6049 {
6050 if (oneleft() == FAIL)
6051 {
6052 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6053 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6054 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6055 */
6056 if ( (((cap->cmdchar == K_BS
6057 || cap->cmdchar == Ctrl_H)
6058 && vim_strchr(p_ww, 'b') != NULL)
6059 || (cap->cmdchar == 'h'
6060 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006061 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 && vim_strchr(p_ww, '<') != NULL))
6063 && curwin->w_cursor.lnum > 1)
6064 {
6065 --(curwin->w_cursor.lnum);
6066 coladvance((colnr_T)MAXCOL);
6067 curwin->w_set_curswant = TRUE;
6068
6069 /* When the NL before the first char has to be deleted we
6070 * put the cursor on the NUL after the previous line.
6071 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006072 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if ( (cap->oap->op_type == OP_DELETE
6074 || cap->oap->op_type == OP_CHANGE)
6075 && !lineempty(curwin->w_cursor.lnum))
6076 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006077 char_u *cp = ml_get_cursor();
6078
6079 if (*cp != NUL)
6080 {
6081#ifdef FEAT_MBYTE
6082 if (has_mbyte)
6083 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6084 else
6085#endif
6086 ++curwin->w_cursor.col;
6087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 cap->retval |= CA_NO_ADJ_OP_END;
6089 }
6090 continue;
6091 }
6092 /* Only beep and flush if not moved at all */
6093 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6094 beep_flush();
6095 break;
6096 }
6097 }
6098#ifdef FEAT_FOLDING
6099 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6100 && cap->oap->op_type == OP_NOP)
6101 foldOpenCursor();
6102#endif
6103}
6104
6105/*
6106 * Cursor up commands.
6107 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6108 */
6109 static void
6110nv_up(cap)
6111 cmdarg_T *cap;
6112{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006113 if (mod_mask & MOD_MASK_SHIFT)
6114 {
6115 /* <S-Up> is page up */
6116 cap->arg = BACKWARD;
6117 nv_page(cap);
6118 }
6119 else
6120 {
6121 cap->oap->motion_type = MLINE;
6122 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6123 clearopbeep(cap->oap);
6124 else if (cap->arg)
6125 beginline(BL_WHITE | BL_FIX);
6126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127}
6128
6129/*
6130 * Cursor down commands.
6131 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6132 */
6133 static void
6134nv_down(cap)
6135 cmdarg_T *cap;
6136{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006137 if (mod_mask & MOD_MASK_SHIFT)
6138 {
6139 /* <S-Down> is page down */
6140 cap->arg = FORWARD;
6141 nv_page(cap);
6142 }
6143 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6145 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006146 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006147 if (curwin->w_llist_ref == NULL)
6148 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6149 else
6150 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 else
6152#endif
6153 {
6154#ifdef FEAT_CMDWIN
6155 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006156 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 cmdwin_result = CAR;
6158 else
6159#endif
6160 {
6161 cap->oap->motion_type = MLINE;
6162 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6163 clearopbeep(cap->oap);
6164 else if (cap->arg)
6165 beginline(BL_WHITE | BL_FIX);
6166 }
6167 }
6168}
6169
6170#ifdef FEAT_SEARCHPATH
6171/*
6172 * Grab the file name under the cursor and edit it.
6173 */
6174 static void
6175nv_gotofile(cap)
6176 cmdarg_T *cap;
6177{
6178 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006179 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006181 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 {
6183 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006184 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 return;
6186 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006187#ifdef FEAT_AUTOCMD
6188 if (curbuf_locked())
6189 {
6190 clearop(cap->oap);
6191 return;
6192 }
6193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006195 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 if (ptr != NULL)
6198 {
6199 /* do autowrite if necessary */
6200 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6201 autowrite(curbuf, FALSE);
6202 setpcmark();
6203 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006204 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006205 if (cap->nchar == 'F' && lnum >= 0)
6206 {
6207 curwin->w_cursor.lnum = lnum;
6208 check_cursor_lnum();
6209 beginline(BL_SOL | BL_FIX);
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 vim_free(ptr);
6212 }
6213 else
6214 clearop(cap->oap);
6215}
6216#endif
6217
6218/*
6219 * <End> command: to end of current line or last line.
6220 */
6221 static void
6222nv_end(cap)
6223 cmdarg_T *cap;
6224{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006225 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006227 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 nv_goto(cap);
6229 cap->count1 = 1; /* to end of current line */
6230 }
6231 nv_dollar(cap);
6232}
6233
6234/*
6235 * Handle the "$" command.
6236 */
6237 static void
6238nv_dollar(cap)
6239 cmdarg_T *cap;
6240{
6241 cap->oap->motion_type = MCHAR;
6242 cap->oap->inclusive = TRUE;
6243#ifdef FEAT_VIRTUALEDIT
6244 /* In virtual mode when off the edge of a line and an operator
6245 * is pending (whew!) keep the cursor where it is.
6246 * Otherwise, send it to the end of the line. */
6247 if (!virtual_active() || gchar_cursor() != NUL
6248 || cap->oap->op_type == OP_NOP)
6249#endif
6250 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6251 if (cursor_down((long)(cap->count1 - 1),
6252 cap->oap->op_type == OP_NOP) == FAIL)
6253 clearopbeep(cap->oap);
6254#ifdef FEAT_FOLDING
6255 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6256 foldOpenCursor();
6257#endif
6258}
6259
6260/*
6261 * Implementation of '?' and '/' commands.
6262 * If cap->arg is TRUE don't set PC mark.
6263 */
6264 static void
6265nv_search(cap)
6266 cmdarg_T *cap;
6267{
6268 oparg_T *oap = cap->oap;
6269
6270 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6271 {
6272 /* Translate "g??" to "g?g?" */
6273 cap->cmdchar = 'g';
6274 cap->nchar = '?';
6275 nv_operator(cap);
6276 return;
6277 }
6278
6279 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6280
6281 if (cap->searchbuf == NULL)
6282 {
6283 clearop(oap);
6284 return;
6285 }
6286
Bram Moolenaar46539112015-02-17 15:43:57 +01006287 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 (cap->arg ? 0 : SEARCH_MARK));
6289}
6290
6291/*
6292 * Handle "N" and "n" commands.
6293 * cap->arg is SEARCH_REV for "N", 0 for "n".
6294 */
6295 static void
6296nv_next(cap)
6297 cmdarg_T *cap;
6298{
Bram Moolenaar46539112015-02-17 15:43:57 +01006299 pos_T old = curwin->w_cursor;
6300 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6301
6302 if (i == 1 && equalpos(old, curwin->w_cursor))
6303 {
6304 /* Avoid getting stuck on the current cursor position, which can
6305 * happen when an offset is given and the cursor is on the last char
6306 * in the buffer: Repeat with count + 1. */
6307 cap->count1 += 1;
6308 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6309 cap->count1 -= 1;
6310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311}
6312
6313/*
6314 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6315 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006316 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006318 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319normal_search(cap, dir, pat, opt)
6320 cmdarg_T *cap;
6321 int dir;
6322 char_u *pat;
6323 int opt; /* extra flags for do_search() */
6324{
6325 int i;
6326
6327 cap->oap->motion_type = MCHAR;
6328 cap->oap->inclusive = FALSE;
6329 cap->oap->use_reg_one = TRUE;
6330 curwin->w_set_curswant = TRUE;
6331
6332 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006333 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (i == 0)
6335 clearop(cap->oap);
6336 else
6337 {
6338 if (i == 2)
6339 cap->oap->motion_type = MLINE;
6340#ifdef FEAT_VIRTUALEDIT
6341 curwin->w_cursor.coladd = 0;
6342#endif
6343#ifdef FEAT_FOLDING
6344 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6345 foldOpenCursor();
6346#endif
6347 }
6348
6349 /* "/$" will put the cursor after the end of the line, may need to
6350 * correct that here */
6351 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006352 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353}
6354
6355/*
6356 * Character search commands.
6357 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6358 * ',' and FALSE for ';'.
6359 * cap->nchar is NUL for ',' and ';' (repeat the search)
6360 */
6361 static void
6362nv_csearch(cap)
6363 cmdarg_T *cap;
6364{
6365 int t_cmd;
6366
6367 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6368 t_cmd = TRUE;
6369 else
6370 t_cmd = FALSE;
6371
6372 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6374 clearopbeep(cap->oap);
6375 else
6376 {
6377 curwin->w_set_curswant = TRUE;
6378#ifdef FEAT_VIRTUALEDIT
6379 /* Include a Tab for "tx" and for "dfx". */
6380 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6381 && (t_cmd || cap->oap->op_type != OP_NOP))
6382 {
6383 colnr_T scol, ecol;
6384
6385 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6386 curwin->w_cursor.coladd = ecol - scol;
6387 }
6388 else
6389 curwin->w_cursor.coladd = 0;
6390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392#ifdef FEAT_FOLDING
6393 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6394 foldOpenCursor();
6395#endif
6396 }
6397}
6398
6399/*
6400 * "[" and "]" commands.
6401 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6402 */
6403 static void
6404nv_brackets(cap)
6405 cmdarg_T *cap;
6406{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006407 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 pos_T prev_pos;
6409 pos_T *pos = NULL; /* init for GCC */
6410 pos_T old_pos; /* cursor position before command */
6411 int flag;
6412 long n;
6413 int findc;
6414 int c;
6415
6416 cap->oap->motion_type = MCHAR;
6417 cap->oap->inclusive = FALSE;
6418 old_pos = curwin->w_cursor;
6419#ifdef FEAT_VIRTUALEDIT
6420 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6421#endif
6422
6423#ifdef FEAT_SEARCHPATH
6424 /*
6425 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6426 */
6427 if (cap->nchar == 'f')
6428 nv_gotofile(cap);
6429 else
6430#endif
6431
6432#ifdef FEAT_FIND_ID
6433 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006434 * Find the occurrence(s) of the identifier or define under cursor
6435 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 *
6437 * search list jump
6438 * fwd bwd fwd bwd fwd bwd
6439 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6440 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6441 */
6442 if (vim_strchr((char_u *)
6443#ifdef EBCDIC
6444 "iI\005dD\067",
6445#else
6446 "iI\011dD\004",
6447#endif
6448 cap->nchar) != NULL)
6449 {
6450 char_u *ptr;
6451 int len;
6452
6453 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6454 clearop(cap->oap);
6455 else
6456 {
6457 find_pattern_in_path(ptr, 0, len, TRUE,
6458 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6459 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6460 cap->count1,
6461 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6462 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6463 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6464 (linenr_T)MAXLNUM);
6465 curwin->w_set_curswant = TRUE;
6466 }
6467 }
6468 else
6469#endif
6470
6471 /*
6472 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6473 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6474 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6475 * "[m" or "]m" search for prev/next start of (Java) method.
6476 * "[M" or "]M" search for prev/next end of (Java) method.
6477 */
6478 if ( (cap->cmdchar == '['
6479 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6480 || (cap->cmdchar == ']'
6481 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6482 {
6483 if (cap->nchar == '*')
6484 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 prev_pos.lnum = 0;
6486 if (cap->nchar == 'm' || cap->nchar == 'M')
6487 {
6488 if (cap->cmdchar == '[')
6489 findc = '{';
6490 else
6491 findc = '}';
6492 n = 9999;
6493 }
6494 else
6495 {
6496 findc = cap->nchar;
6497 n = cap->count1;
6498 }
6499 for ( ; n > 0; --n)
6500 {
6501 if ((pos = findmatchlimit(cap->oap, findc,
6502 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6503 {
6504 if (new_pos.lnum == 0) /* nothing found */
6505 {
6506 if (cap->nchar != 'm' && cap->nchar != 'M')
6507 clearopbeep(cap->oap);
6508 }
6509 else
6510 pos = &new_pos; /* use last one found */
6511 break;
6512 }
6513 prev_pos = new_pos;
6514 curwin->w_cursor = *pos;
6515 new_pos = *pos;
6516 }
6517 curwin->w_cursor = old_pos;
6518
6519 /*
6520 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6521 * brought us to the match for "[m" and "]M" when inside a method.
6522 * Try finding the '{' or '}' we want to be at.
6523 * Also repeat for the given count.
6524 */
6525 if (cap->nchar == 'm' || cap->nchar == 'M')
6526 {
6527 /* norm is TRUE for "]M" and "[m" */
6528 int norm = ((findc == '{') == (cap->nchar == 'm'));
6529
6530 n = cap->count1;
6531 /* found a match: we were inside a method */
6532 if (prev_pos.lnum != 0)
6533 {
6534 pos = &prev_pos;
6535 curwin->w_cursor = prev_pos;
6536 if (norm)
6537 --n;
6538 }
6539 else
6540 pos = NULL;
6541 while (n > 0)
6542 {
6543 for (;;)
6544 {
6545 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6546 {
6547 /* if not found anything, that's an error */
6548 if (pos == NULL)
6549 clearopbeep(cap->oap);
6550 n = 0;
6551 break;
6552 }
6553 c = gchar_cursor();
6554 if (c == '{' || c == '}')
6555 {
6556 /* Must have found end/start of class: use it.
6557 * Or found the place to be at. */
6558 if ((c == findc && norm) || (n == 1 && !norm))
6559 {
6560 new_pos = curwin->w_cursor;
6561 pos = &new_pos;
6562 n = 0;
6563 }
6564 /* if no match found at all, we started outside of the
6565 * class and we're inside now. Just go on. */
6566 else if (new_pos.lnum == 0)
6567 {
6568 new_pos = curwin->w_cursor;
6569 pos = &new_pos;
6570 }
6571 /* found start/end of other method: go to match */
6572 else if ((pos = findmatchlimit(cap->oap, findc,
6573 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6574 0)) == NULL)
6575 n = 0;
6576 else
6577 curwin->w_cursor = *pos;
6578 break;
6579 }
6580 }
6581 --n;
6582 }
6583 curwin->w_cursor = old_pos;
6584 if (pos == NULL && new_pos.lnum != 0)
6585 clearopbeep(cap->oap);
6586 }
6587 if (pos != NULL)
6588 {
6589 setpcmark();
6590 curwin->w_cursor = *pos;
6591 curwin->w_set_curswant = TRUE;
6592#ifdef FEAT_FOLDING
6593 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6594 && cap->oap->op_type == OP_NOP)
6595 foldOpenCursor();
6596#endif
6597 }
6598 }
6599
6600 /*
6601 * "[[", "[]", "]]" and "][": move to start or end of function
6602 */
6603 else if (cap->nchar == '[' || cap->nchar == ']')
6604 {
6605 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6606 flag = '{';
6607 else
6608 flag = '}'; /* "][" or "[]" */
6609
6610 curwin->w_set_curswant = TRUE;
6611 /*
6612 * Imitate strange Vi behaviour: When using "]]" with an operator
6613 * we also stop at '}'.
6614 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006615 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 (cap->oap->op_type != OP_NOP
6617 && cap->arg == FORWARD && flag == '{')))
6618 clearopbeep(cap->oap);
6619 else
6620 {
6621 if (cap->oap->op_type == OP_NOP)
6622 beginline(BL_WHITE | BL_FIX);
6623#ifdef FEAT_FOLDING
6624 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6625 foldOpenCursor();
6626#endif
6627 }
6628 }
6629
6630 /*
6631 * "[p", "[P", "]P" and "]p": put with indent adjustment
6632 */
6633 else if (cap->nchar == 'p' || cap->nchar == 'P')
6634 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006635 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006637 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6638 ? FORWARD : BACKWARD;
6639 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006640 int was_visual = VIsual_active;
6641 int line_count = curbuf->b_ml.ml_line_count;
6642 pos_T start, end;
6643
6644 if (VIsual_active)
6645 {
6646 start = ltoreq(VIsual, curwin->w_cursor)
6647 ? VIsual : curwin->w_cursor;
6648 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6649 curwin->w_cursor = (dir == BACKWARD ? start : end);
6650 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006651# ifdef FEAT_CLIPBOARD
6652 adjust_clip_reg(&regname);
6653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006655
6656 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006657 if (was_visual)
6658 {
6659 VIsual = start;
6660 curwin->w_cursor = end;
6661 if (dir == BACKWARD)
6662 {
6663 /* adjust lines */
6664 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6665 curwin->w_cursor.lnum +=
6666 curbuf->b_ml.ml_line_count - line_count;
6667 }
6668
6669 VIsual_active = TRUE;
6670 if (VIsual_mode == 'V')
6671 {
6672 /* delete visually selected lines */
6673 cap->cmdchar = 'd';
6674 cap->nchar = NUL;
6675 cap->oap->regname = regname;
6676 nv_operator(cap);
6677 do_pending_operator(cap, 0, FALSE);
6678 }
6679 if (VIsual_active)
6680 {
6681 end_visual_mode();
6682 redraw_later(SOME_VALID);
6683 }
6684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 }
6686 }
6687
6688 /*
6689 * "['", "[`", "]'" and "]`": jump to next mark
6690 */
6691 else if (cap->nchar == '\'' || cap->nchar == '`')
6692 {
6693 pos = &curwin->w_cursor;
6694 for (n = cap->count1; n > 0; --n)
6695 {
6696 prev_pos = *pos;
6697 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6698 cap->nchar == '\'');
6699 if (pos == NULL)
6700 break;
6701 }
6702 if (pos == NULL)
6703 pos = &prev_pos;
6704 nv_cursormark(cap, cap->nchar == '\'', pos);
6705 }
6706
6707#ifdef FEAT_MOUSE
6708 /*
6709 * [ or ] followed by a middle mouse click: put selected text with
6710 * indent adjustment. Any other button just does as usual.
6711 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006712 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
6714 (void)do_mouse(cap->oap, cap->nchar,
6715 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6716 cap->count1, PUT_FIXINDENT);
6717 }
6718#endif /* FEAT_MOUSE */
6719
6720#ifdef FEAT_FOLDING
6721 /*
6722 * "[z" and "]z": move to start or end of open fold.
6723 */
6724 else if (cap->nchar == 'z')
6725 {
6726 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6727 cap->count1) == FAIL)
6728 clearopbeep(cap->oap);
6729 }
6730#endif
6731
6732#ifdef FEAT_DIFF
6733 /*
6734 * "[c" and "]c": move to next or previous diff-change.
6735 */
6736 else if (cap->nchar == 'c')
6737 {
6738 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6739 cap->count1) == FAIL)
6740 clearopbeep(cap->oap);
6741 }
6742#endif
6743
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006744#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006745 /*
6746 * "[s", "[S", "]s" and "]S": move to next spell error.
6747 */
6748 else if (cap->nchar == 's' || cap->nchar == 'S')
6749 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006750 setpcmark();
6751 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006752 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6753 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006754 {
6755 clearopbeep(cap->oap);
6756 break;
6757 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006758# ifdef FEAT_FOLDING
6759 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6760 foldOpenCursor();
6761# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006762 }
6763#endif
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 /* Not a valid cap->nchar. */
6766 else
6767 clearopbeep(cap->oap);
6768}
6769
6770/*
6771 * Handle Normal mode "%" command.
6772 */
6773 static void
6774nv_percent(cap)
6775 cmdarg_T *cap;
6776{
6777 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006778#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 linenr_T lnum = curwin->w_cursor.lnum;
6780#endif
6781
6782 cap->oap->inclusive = TRUE;
6783 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6784 {
6785 if (cap->count0 > 100)
6786 clearopbeep(cap->oap);
6787 else
6788 {
6789 cap->oap->motion_type = MLINE;
6790 setpcmark();
6791 /* Round up, so CTRL-G will give same value. Watch out for a
6792 * large line count, the line number must not go negative! */
6793 if (curbuf->b_ml.ml_line_count > 1000000)
6794 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6795 / 100L * cap->count0;
6796 else
6797 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6798 cap->count0 + 99L) / 100L;
6799 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6801 beginline(BL_SOL | BL_FIX);
6802 }
6803 }
6804 else /* "%" : go to matching paren */
6805 {
6806 cap->oap->motion_type = MCHAR;
6807 cap->oap->use_reg_one = TRUE;
6808 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6809 clearopbeep(cap->oap);
6810 else
6811 {
6812 setpcmark();
6813 curwin->w_cursor = *pos;
6814 curwin->w_set_curswant = TRUE;
6815#ifdef FEAT_VIRTUALEDIT
6816 curwin->w_cursor.coladd = 0;
6817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820 }
6821#ifdef FEAT_FOLDING
6822 if (cap->oap->op_type == OP_NOP
6823 && lnum != curwin->w_cursor.lnum
6824 && (fdo_flags & FDO_PERCENT)
6825 && KeyTyped)
6826 foldOpenCursor();
6827#endif
6828}
6829
6830/*
6831 * Handle "(" and ")" commands.
6832 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6833 */
6834 static void
6835nv_brace(cap)
6836 cmdarg_T *cap;
6837{
6838 cap->oap->motion_type = MCHAR;
6839 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006840 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6841 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 curwin->w_set_curswant = TRUE;
6843
6844 if (findsent(cap->arg, cap->count1) == FAIL)
6845 clearopbeep(cap->oap);
6846 else
6847 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006848 /* Don't leave the cursor on the NUL past end of line. */
6849 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850#ifdef FEAT_VIRTUALEDIT
6851 curwin->w_cursor.coladd = 0;
6852#endif
6853#ifdef FEAT_FOLDING
6854 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6855 foldOpenCursor();
6856#endif
6857 }
6858}
6859
6860/*
6861 * "m" command: Mark a position.
6862 */
6863 static void
6864nv_mark(cap)
6865 cmdarg_T *cap;
6866{
6867 if (!checkclearop(cap->oap))
6868 {
6869 if (setmark(cap->nchar) == FAIL)
6870 clearopbeep(cap->oap);
6871 }
6872}
6873
6874/*
6875 * "{" and "}" commands.
6876 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6877 */
6878 static void
6879nv_findpar(cap)
6880 cmdarg_T *cap;
6881{
6882 cap->oap->motion_type = MCHAR;
6883 cap->oap->inclusive = FALSE;
6884 cap->oap->use_reg_one = TRUE;
6885 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006886 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 clearopbeep(cap->oap);
6888 else
6889 {
6890#ifdef FEAT_VIRTUALEDIT
6891 curwin->w_cursor.coladd = 0;
6892#endif
6893#ifdef FEAT_FOLDING
6894 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6895 foldOpenCursor();
6896#endif
6897 }
6898}
6899
6900/*
6901 * "u" command: Undo or make lower case.
6902 */
6903 static void
6904nv_undo(cap)
6905 cmdarg_T *cap;
6906{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006907 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
6909 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6910 cap->cmdchar = 'g';
6911 cap->nchar = 'u';
6912 nv_operator(cap);
6913 }
6914 else
6915 nv_kundo(cap);
6916}
6917
6918/*
6919 * <Undo> command.
6920 */
6921 static void
6922nv_kundo(cap)
6923 cmdarg_T *cap;
6924{
6925 if (!checkclearopq(cap->oap))
6926 {
6927 u_undo((int)cap->count1);
6928 curwin->w_set_curswant = TRUE;
6929 }
6930}
6931
6932/*
6933 * Handle the "r" command.
6934 */
6935 static void
6936nv_replace(cap)
6937 cmdarg_T *cap;
6938{
6939 char_u *ptr;
6940 int had_ctrl_v;
6941 long n;
6942
6943 if (checkclearop(cap->oap))
6944 return;
6945
6946 /* get another character */
6947 if (cap->nchar == Ctrl_V)
6948 {
6949 had_ctrl_v = Ctrl_V;
6950 cap->nchar = get_literal();
6951 /* Don't redo a multibyte character with CTRL-V. */
6952 if (cap->nchar > DEL)
6953 had_ctrl_v = NUL;
6954 }
6955 else
6956 had_ctrl_v = NUL;
6957
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006958 /* Abort if the character is a special key. */
6959 if (IS_SPECIAL(cap->nchar))
6960 {
6961 clearopbeep(cap->oap);
6962 return;
6963 }
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /* Visual mode "r" */
6966 if (VIsual_active)
6967 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006968 if (got_int)
6969 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006970 if (had_ctrl_v)
6971 {
6972 if (cap->nchar == '\r')
6973 cap->nchar = -1;
6974 else if (cap->nchar == '\n')
6975 cap->nchar = -2;
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 nv_operator(cap);
6978 return;
6979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981#ifdef FEAT_VIRTUALEDIT
6982 /* Break tabs, etc. */
6983 if (virtual_active())
6984 {
6985 if (u_save_cursor() == FAIL)
6986 return;
6987 if (gchar_cursor() == NUL)
6988 {
6989 /* Add extra space and put the cursor on the first one. */
6990 coladvance_force((colnr_T)(getviscol() + cap->count1));
6991 curwin->w_cursor.col -= cap->count1;
6992 }
6993 else if (gchar_cursor() == TAB)
6994 coladvance_force(getviscol());
6995 }
6996#endif
6997
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006998 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007000 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001#ifdef FEAT_MBYTE
7002 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7003#endif
7004 )
7005 {
7006 clearopbeep(cap->oap);
7007 return;
7008 }
7009
7010 /*
7011 * Replacing with a TAB is done by edit() when it is complicated because
7012 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7013 * Other characters are done below to avoid problems with things like
7014 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7015 */
7016 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7017 {
7018 stuffnumReadbuff(cap->count1);
7019 stuffcharReadbuff('R');
7020 stuffcharReadbuff('\t');
7021 stuffcharReadbuff(ESC);
7022 return;
7023 }
7024
7025 /* save line for undo */
7026 if (u_save_cursor() == FAIL)
7027 return;
7028
7029 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7030 {
7031 /*
7032 * Replace character(s) by a single newline.
7033 * Strange vi behaviour: Only one newline is inserted.
7034 * Delete the characters here.
7035 * Insert the newline with an insert command, takes care of
7036 * autoindent. The insert command depends on being on the last
7037 * character of a line or not.
7038 */
7039#ifdef FEAT_MBYTE
7040 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7041#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007042 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043#endif
7044 stuffcharReadbuff('\r');
7045 stuffcharReadbuff(ESC);
7046
7047 /* Give 'r' to edit(), to get the redo command right. */
7048 invoke_edit(cap, TRUE, 'r', FALSE);
7049 }
7050 else
7051 {
7052 prep_redo(cap->oap->regname, cap->count1,
7053 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7054
7055 curbuf->b_op_start = curwin->w_cursor;
7056#ifdef FEAT_MBYTE
7057 if (has_mbyte)
7058 {
7059 int old_State = State;
7060
7061 if (cap->ncharC1 != 0)
7062 AppendCharToRedobuff(cap->ncharC1);
7063 if (cap->ncharC2 != 0)
7064 AppendCharToRedobuff(cap->ncharC2);
7065
7066 /* This is slow, but it handles replacing a single-byte with a
7067 * multi-byte and the other way around. Also handles adding
7068 * composing characters for utf-8. */
7069 for (n = cap->count1; n > 0; --n)
7070 {
7071 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007072 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7073 {
7074 int c = ins_copychar(curwin->w_cursor.lnum
7075 + (cap->nchar == Ctrl_Y ? -1 : 1));
7076 if (c != NUL)
7077 ins_char(c);
7078 else
7079 /* will be decremented further down */
7080 ++curwin->w_cursor.col;
7081 }
7082 else
7083 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 State = old_State;
7085 if (cap->ncharC1 != 0)
7086 ins_char(cap->ncharC1);
7087 if (cap->ncharC2 != 0)
7088 ins_char(cap->ncharC2);
7089 }
7090 }
7091 else
7092#endif
7093 {
7094 /*
7095 * Replace the characters within one line.
7096 */
7097 for (n = cap->count1; n > 0; --n)
7098 {
7099 /*
7100 * Get ptr again, because u_save and/or showmatch() will have
7101 * released the line. At the same time we let know that the
7102 * line will be changed.
7103 */
7104 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007105 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7106 {
7107 int c = ins_copychar(curwin->w_cursor.lnum
7108 + (cap->nchar == Ctrl_Y ? -1 : 1));
7109 if (c != NUL)
7110 ptr[curwin->w_cursor.col] = c;
7111 }
7112 else
7113 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (p_sm && msg_silent == 0)
7115 showmatch(cap->nchar);
7116 ++curwin->w_cursor.col;
7117 }
7118#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007119 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007121 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
Bram Moolenaar009b2592004-10-24 19:18:58 +00007123 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007124 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007126 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 }
7128#endif
7129
7130 /* mark the buffer as changed and prepare for displaying */
7131 changed_bytes(curwin->w_cursor.lnum,
7132 (colnr_T)(curwin->w_cursor.col - cap->count1));
7133 }
7134 --curwin->w_cursor.col; /* cursor on the last replaced char */
7135#ifdef FEAT_MBYTE
7136 /* if the character on the left of the current cursor is a multi-byte
7137 * character, move two characters left */
7138 if (has_mbyte)
7139 mb_adjust_cursor();
7140#endif
7141 curbuf->b_op_end = curwin->w_cursor;
7142 curwin->w_set_curswant = TRUE;
7143 set_last_insert(cap->nchar);
7144 }
7145}
7146
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147/*
7148 * 'o': Exchange start and end of Visual area.
7149 * 'O': same, but in block mode exchange left and right corners.
7150 */
7151 static void
7152v_swap_corners(cmdchar)
7153 int cmdchar;
7154{
7155 pos_T old_cursor;
7156 colnr_T left, right;
7157
7158 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7159 {
7160 old_cursor = curwin->w_cursor;
7161 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7162 curwin->w_cursor.lnum = VIsual.lnum;
7163 coladvance(left);
7164 VIsual = curwin->w_cursor;
7165
7166 curwin->w_cursor.lnum = old_cursor.lnum;
7167 curwin->w_curswant = right;
7168 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7169 * right one column */
7170 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7171 ++curwin->w_curswant;
7172 coladvance(curwin->w_curswant);
7173 if (curwin->w_cursor.col == old_cursor.col
7174#ifdef FEAT_VIRTUALEDIT
7175 && (!virtual_active()
7176 || curwin->w_cursor.coladd == old_cursor.coladd)
7177#endif
7178 )
7179 {
7180 curwin->w_cursor.lnum = VIsual.lnum;
7181 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7182 ++right;
7183 coladvance(right);
7184 VIsual = curwin->w_cursor;
7185
7186 curwin->w_cursor.lnum = old_cursor.lnum;
7187 coladvance(left);
7188 curwin->w_curswant = left;
7189 }
7190 }
7191 else
7192 {
7193 old_cursor = curwin->w_cursor;
7194 curwin->w_cursor = VIsual;
7195 VIsual = old_cursor;
7196 curwin->w_set_curswant = TRUE;
7197 }
7198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200/*
7201 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7202 */
7203 static void
7204nv_Replace(cap)
7205 cmdarg_T *cap;
7206{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (VIsual_active) /* "R" is replace lines */
7208 {
7209 cap->cmdchar = 'c';
7210 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007211 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 VIsual_mode = 'V';
7213 nv_operator(cap);
7214 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007215 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 if (!curbuf->b_p_ma)
7218 EMSG(_(e_modifiable));
7219 else
7220 {
7221#ifdef FEAT_VIRTUALEDIT
7222 if (virtual_active())
7223 coladvance(getviscol());
7224#endif
7225 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7226 }
7227 }
7228}
7229
7230#ifdef FEAT_VREPLACE
7231/*
7232 * "gr".
7233 */
7234 static void
7235nv_vreplace(cap)
7236 cmdarg_T *cap;
7237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (VIsual_active)
7239 {
7240 cap->cmdchar = 'r';
7241 cap->nchar = cap->extra_char;
7242 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7243 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007244 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
7246 if (!curbuf->b_p_ma)
7247 EMSG(_(e_modifiable));
7248 else
7249 {
7250 if (cap->extra_char == Ctrl_V) /* get another character */
7251 cap->extra_char = get_literal();
7252 stuffcharReadbuff(cap->extra_char);
7253 stuffcharReadbuff(ESC);
7254# ifdef FEAT_VIRTUALEDIT
7255 if (virtual_active())
7256 coladvance(getviscol());
7257# endif
7258 invoke_edit(cap, TRUE, 'v', FALSE);
7259 }
7260 }
7261}
7262#endif
7263
7264/*
7265 * Swap case for "~" command, when it does not work like an operator.
7266 */
7267 static void
7268n_swapchar(cap)
7269 cmdarg_T *cap;
7270{
7271 long n;
7272 pos_T startpos;
7273 int did_change = 0;
7274#ifdef FEAT_NETBEANS_INTG
7275 pos_T pos;
7276 char_u *ptr;
7277 int count;
7278#endif
7279
7280 if (checkclearopq(cap->oap))
7281 return;
7282
7283 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7284 {
7285 clearopbeep(cap->oap);
7286 return;
7287 }
7288
7289 prep_redo_cmd(cap);
7290
7291 if (u_save_cursor() == FAIL)
7292 return;
7293
7294 startpos = curwin->w_cursor;
7295#ifdef FEAT_NETBEANS_INTG
7296 pos = startpos;
7297#endif
7298 for (n = cap->count1; n > 0; --n)
7299 {
7300 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7301 inc_cursor();
7302 if (gchar_cursor() == NUL)
7303 {
7304 if (vim_strchr(p_ww, '~') != NULL
7305 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7306 {
7307#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007308 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 {
7310 if (did_change)
7311 {
7312 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007313 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007314 netbeans_removed(curbuf, pos.lnum, pos.col,
7315 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007317 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
7319 pos.col = 0;
7320 pos.lnum++;
7321 }
7322#endif
7323 ++curwin->w_cursor.lnum;
7324 curwin->w_cursor.col = 0;
7325 if (n > 1)
7326 {
7327 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7328 break;
7329 u_clearline();
7330 }
7331 }
7332 else
7333 break;
7334 }
7335 }
7336#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007337 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
7339 ptr = ml_get(pos.lnum);
7340 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007341 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7342 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 }
7344#endif
7345
7346
7347 check_cursor();
7348 curwin->w_set_curswant = TRUE;
7349 if (did_change)
7350 {
7351 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7352 0L);
7353 curbuf->b_op_start = startpos;
7354 curbuf->b_op_end = curwin->w_cursor;
7355 if (curbuf->b_op_end.col > 0)
7356 --curbuf->b_op_end.col;
7357 }
7358}
7359
7360/*
7361 * Move cursor to mark.
7362 */
7363 static void
7364nv_cursormark(cap, flag, pos)
7365 cmdarg_T *cap;
7366 int flag;
7367 pos_T *pos;
7368{
7369 if (check_mark(pos) == FAIL)
7370 clearop(cap->oap);
7371 else
7372 {
7373 if (cap->cmdchar == '\''
7374 || cap->cmdchar == '`'
7375 || cap->cmdchar == '['
7376 || cap->cmdchar == ']')
7377 setpcmark();
7378 curwin->w_cursor = *pos;
7379 if (flag)
7380 beginline(BL_WHITE | BL_FIX);
7381 else
7382 check_cursor();
7383 }
7384 cap->oap->motion_type = flag ? MLINE : MCHAR;
7385 if (cap->cmdchar == '`')
7386 cap->oap->use_reg_one = TRUE;
7387 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7388 curwin->w_set_curswant = TRUE;
7389}
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391/*
7392 * Handle commands that are operators in Visual mode.
7393 */
7394 static void
7395v_visop(cap)
7396 cmdarg_T *cap;
7397{
7398 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7399
7400 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007401 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (isupper(cap->cmdchar))
7403 {
7404 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007405 {
7406 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7410 curwin->w_curswant = MAXCOL;
7411 }
7412 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7413 nv_operator(cap);
7414}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416/*
7417 * "s" and "S" commands.
7418 */
7419 static void
7420nv_subst(cap)
7421 cmdarg_T *cap;
7422{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7424 {
7425 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007426 {
7427 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 cap->cmdchar = 'c';
7431 nv_operator(cap);
7432 }
7433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 nv_optrans(cap);
7435}
7436
7437/*
7438 * Abbreviated commands.
7439 */
7440 static void
7441nv_abbrev(cap)
7442 cmdarg_T *cap;
7443{
7444 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7445 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 /* in Visual mode these commands are operators */
7448 if (VIsual_active)
7449 v_visop(cap);
7450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 nv_optrans(cap);
7452}
7453
7454/*
7455 * Translate a command into another command.
7456 */
7457 static void
7458nv_optrans(cap)
7459 cmdarg_T *cap;
7460{
7461 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7462 (char_u *)"d$", (char_u *)"c$",
7463 (char_u *)"cl", (char_u *)"cc",
7464 (char_u *)"yy", (char_u *)":s\r"};
7465 static char_u *str = (char_u *)"xXDCsSY&";
7466
7467 if (!checkclearopq(cap->oap))
7468 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007469 /* In Vi "2D" doesn't delete the next line. Can't translate it
7470 * either, because "2." should also not use the count. */
7471 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7472 {
7473 cap->oap->start = curwin->w_cursor;
7474 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007475#ifdef FEAT_EVAL
7476 set_op_var(OP_DELETE);
7477#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007478 cap->count1 = 1;
7479 nv_dollar(cap);
7480 finish_op = TRUE;
7481 ResetRedobuff();
7482 AppendCharToRedobuff('D');
7483 }
7484 else
7485 {
7486 if (cap->count0)
7487 stuffnumReadbuff(cap->count0);
7488 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 }
7491 cap->opcount = 0;
7492}
7493
7494/*
7495 * "'" and "`" commands. Also for "g'" and "g`".
7496 * cap->arg is TRUE for "'" and "g'".
7497 */
7498 static void
7499nv_gomark(cap)
7500 cmdarg_T *cap;
7501{
7502 pos_T *pos;
7503 int c;
7504#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007505 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7507#endif
7508
7509 if (cap->cmdchar == 'g')
7510 c = cap->extra_char;
7511 else
7512 c = cap->nchar;
7513 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7514 if (pos == (pos_T *)-1) /* jumped to other file */
7515 {
7516 if (cap->arg)
7517 {
7518 check_cursor_lnum();
7519 beginline(BL_WHITE | BL_FIX);
7520 }
7521 else
7522 check_cursor();
7523 }
7524 else
7525 nv_cursormark(cap, cap->arg, pos);
7526
7527#ifdef FEAT_VIRTUALEDIT
7528 /* May need to clear the coladd that a mark includes. */
7529 if (!virtual_active())
7530 curwin->w_cursor.coladd = 0;
7531#endif
7532#ifdef FEAT_FOLDING
7533 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007534 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007535 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 && (fdo_flags & FDO_MARK)
7537 && old_KeyTyped)
7538 foldOpenCursor();
7539#endif
7540}
7541
7542/*
7543 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7544 */
7545 static void
7546nv_pcmark(cap)
7547 cmdarg_T *cap;
7548{
7549#ifdef FEAT_JUMPLIST
7550 pos_T *pos;
7551# ifdef FEAT_FOLDING
7552 linenr_T lnum = curwin->w_cursor.lnum;
7553 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7554# endif
7555
7556 if (!checkclearopq(cap->oap))
7557 {
7558 if (cap->cmdchar == 'g')
7559 pos = movechangelist((int)cap->count1);
7560 else
7561 pos = movemark((int)cap->count1);
7562 if (pos == (pos_T *)-1) /* jump to other file */
7563 {
7564 curwin->w_set_curswant = TRUE;
7565 check_cursor();
7566 }
7567 else if (pos != NULL) /* can jump */
7568 nv_cursormark(cap, FALSE, pos);
7569 else if (cap->cmdchar == 'g')
7570 {
7571 if (curbuf->b_changelistlen == 0)
7572 EMSG(_("E664: changelist is empty"));
7573 else if (cap->count1 < 0)
7574 EMSG(_("E662: At start of changelist"));
7575 else
7576 EMSG(_("E663: At end of changelist"));
7577 }
7578 else
7579 clearopbeep(cap->oap);
7580# ifdef FEAT_FOLDING
7581 if (cap->oap->op_type == OP_NOP
7582 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7583 && (fdo_flags & FDO_MARK)
7584 && old_KeyTyped)
7585 foldOpenCursor();
7586# endif
7587 }
7588#else
7589 clearopbeep(cap->oap);
7590#endif
7591}
7592
7593/*
7594 * Handle '"' command.
7595 */
7596 static void
7597nv_regname(cap)
7598 cmdarg_T *cap;
7599{
7600 if (checkclearop(cap->oap))
7601 return;
7602#ifdef FEAT_EVAL
7603 if (cap->nchar == '=')
7604 cap->nchar = get_expr_register();
7605#endif
7606 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7607 {
7608 cap->oap->regname = cap->nchar;
7609 cap->opcount = cap->count0; /* remember count before '"' */
7610#ifdef FEAT_EVAL
7611 set_reg_var(cap->oap->regname);
7612#endif
7613 }
7614 else
7615 clearopbeep(cap->oap);
7616}
7617
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618/*
7619 * Handle "v", "V" and "CTRL-V" commands.
7620 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7621 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007622 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 */
7624 static void
7625nv_visual(cap)
7626 cmdarg_T *cap;
7627{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007628 if (cap->cmdchar == Ctrl_Q)
7629 cap->cmdchar = Ctrl_V;
7630
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7632 * characterwise, linewise, or blockwise. */
7633 if (cap->oap->op_type != OP_NOP)
7634 {
7635 cap->oap->motion_force = cap->cmdchar;
7636 finish_op = FALSE; /* operator doesn't finish now but later */
7637 return;
7638 }
7639
7640 VIsual_select = cap->arg;
7641 if (VIsual_active) /* change Visual mode */
7642 {
7643 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7644 end_visual_mode();
7645 else /* toggle char/block mode */
7646 { /* or char/line mode */
7647 VIsual_mode = cap->cmdchar;
7648 showmode();
7649 }
7650 redraw_curbuf_later(INVERTED); /* update the inversion */
7651 }
7652 else /* start Visual mode */
7653 {
7654 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007655 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007657 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 VIsual = curwin->w_cursor;
7659
7660 VIsual_active = TRUE;
7661 VIsual_reselect = TRUE;
7662 if (!cap->arg)
7663 /* start Select mode when 'selectmode' contains "cmd" */
7664 may_start_select('c');
7665#ifdef FEAT_MOUSE
7666 setmouse();
7667#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007668 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 redraw_cmdline = TRUE; /* show visual mode later */
7670 /*
7671 * For V and ^V, we multiply the number of lines even if there
7672 * was only one -- webb
7673 */
7674 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7675 {
7676 curwin->w_cursor.lnum +=
7677 resel_VIsual_line_count * cap->count0 - 1;
7678 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7679 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7680 }
7681 VIsual_mode = resel_VIsual_mode;
7682 if (VIsual_mode == 'v')
7683 {
7684 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007685 {
7686 validate_virtcol();
7687 curwin->w_curswant = curwin->w_virtcol
7688 + resel_VIsual_vcol * cap->count0 - 1;
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007691 curwin->w_curswant = resel_VIsual_vcol;
7692 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007694 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {
7696 curwin->w_curswant = MAXCOL;
7697 coladvance((colnr_T)MAXCOL);
7698 }
7699 else if (VIsual_mode == Ctrl_V)
7700 {
7701 validate_virtcol();
7702 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007703 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 coladvance(curwin->w_curswant);
7705 }
7706 else
7707 curwin->w_set_curswant = TRUE;
7708 redraw_curbuf_later(INVERTED); /* show the inversion */
7709 }
7710 else
7711 {
7712 if (!cap->arg)
7713 /* start Select mode when 'selectmode' contains "cmd" */
7714 may_start_select('c');
7715 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007716 if (VIsual_mode != 'V' && *p_sel == 'e')
7717 ++cap->count1; /* include one more char */
7718 if (cap->count0 > 0 && --cap->count1 > 0)
7719 {
7720 /* With a count select that many characters or lines. */
7721 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7722 nv_right(cap);
7723 else if (VIsual_mode == 'V')
7724 nv_down(cap);
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727 }
7728}
7729
7730/*
7731 * Start selection for Shift-movement keys.
7732 */
7733 void
7734start_selection()
7735{
7736 /* if 'selectmode' contains "key", start Select mode */
7737 may_start_select('k');
7738 n_start_visual_mode('v');
7739}
7740
7741/*
7742 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7743 */
7744 void
7745may_start_select(c)
7746 int c;
7747{
7748 VIsual_select = (stuff_empty() && typebuf_typed()
7749 && (vim_strchr(p_slm, c) != NULL));
7750}
7751
7752/*
7753 * Start Visual mode "c".
7754 * Should set VIsual_select before calling this.
7755 */
7756 static void
7757n_start_visual_mode(c)
7758 int c;
7759{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007760#ifdef FEAT_CONCEAL
7761 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007762 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007763#endif
7764
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 VIsual_mode = c;
7766 VIsual_active = TRUE;
7767 VIsual_reselect = TRUE;
7768#ifdef FEAT_VIRTUALEDIT
7769 /* Corner case: the 0 position in a tab may change when going into
7770 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7771 */
7772 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007773 {
7774 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777#endif
7778 VIsual = curwin->w_cursor;
7779
7780#ifdef FEAT_FOLDING
7781 foldAdjustVisual();
7782#endif
7783
7784#ifdef FEAT_MOUSE
7785 setmouse();
7786#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007787#ifdef FEAT_CONCEAL
7788 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007789 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007790#endif
7791
Bram Moolenaar09df3122006-01-23 22:23:09 +00007792 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 redraw_cmdline = TRUE; /* show visual mode later */
7794#ifdef FEAT_CLIPBOARD
7795 /* Make sure the clipboard gets updated. Needed because start and
7796 * end may still be the same, and the selection needs to be owned */
7797 clip_star.vmode = NUL;
7798#endif
7799
7800 /* Only need to redraw this line, unless still need to redraw an old
7801 * Visual area (when 'lazyredraw' is set). */
7802 if (curwin->w_redr_type < INVERTED)
7803 {
7804 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7805 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7806 }
7807}
7808
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
7810/*
7811 * CTRL-W: Window commands
7812 */
7813 static void
7814nv_window(cap)
7815 cmdarg_T *cap;
7816{
7817#ifdef FEAT_WINDOWS
7818 if (!checkclearop(cap->oap))
7819 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7820#else
7821 (void)checkclearop(cap->oap);
7822#endif
7823}
7824
7825/*
7826 * CTRL-Z: Suspend
7827 */
7828 static void
7829nv_suspend(cap)
7830 cmdarg_T *cap;
7831{
7832 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 if (VIsual_active)
7834 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 do_cmdline_cmd((char_u *)"st");
7836}
7837
7838/*
7839 * Commands starting with "g".
7840 */
7841 static void
7842nv_g_cmd(cap)
7843 cmdarg_T *cap;
7844{
7845 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 int i;
7848 int flag = FALSE;
7849
7850 switch (cap->nchar)
7851 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007852 case Ctrl_A:
7853 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854#ifdef MEM_PROFILE
7855 /*
7856 * "g^A": dump log of used memory.
7857 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007858 if (!VIsual_active && cap->nchar == Ctrl_A)
7859 vim_mem_profile_dump();
7860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007862 /*
7863 * "g^A/g^X": sequentially increment visually selected region
7864 */
7865 if (VIsual_active)
7866 {
7867 cap->arg = TRUE;
7868 cap->cmdchar = cap->nchar;
7869 nv_addsub(cap);
7870 }
7871 else
7872 clearopbeep(oap);
7873 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875#ifdef FEAT_VREPLACE
7876 /*
7877 * "gR": Enter virtual replace mode.
7878 */
7879 case 'R':
7880 cap->arg = TRUE;
7881 nv_Replace(cap);
7882 break;
7883
7884 case 'r':
7885 nv_vreplace(cap);
7886 break;
7887#endif
7888
7889 case '&':
7890 do_cmdline_cmd((char_u *)"%s//~/&");
7891 break;
7892
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 /*
7894 * "gv": Reselect the previous Visual area. If Visual already active,
7895 * exchange previous and current Visual area.
7896 */
7897 case 'v':
7898 if (checkclearop(oap))
7899 break;
7900
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007901 if ( curbuf->b_visual.vi_start.lnum == 0
7902 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7903 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 beep_flush();
7905 else
7906 {
7907 /* set w_cursor to the start of the Visual area, tpos to the end */
7908 if (VIsual_active)
7909 {
7910 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007911 VIsual_mode = curbuf->b_visual.vi_mode;
7912 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913# ifdef FEAT_EVAL
7914 curbuf->b_visual_mode_eval = i;
7915# endif
7916 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007917 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7918 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007920 tpos = curbuf->b_visual.vi_end;
7921 curbuf->b_visual.vi_end = curwin->w_cursor;
7922 curwin->w_cursor = curbuf->b_visual.vi_start;
7923 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925 else
7926 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007927 VIsual_mode = curbuf->b_visual.vi_mode;
7928 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7929 tpos = curbuf->b_visual.vi_end;
7930 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 }
7932
7933 VIsual_active = TRUE;
7934 VIsual_reselect = TRUE;
7935
7936 /* Set Visual to the start and w_cursor to the end of the Visual
7937 * area. Make sure they are on an existing character. */
7938 check_cursor();
7939 VIsual = curwin->w_cursor;
7940 curwin->w_cursor = tpos;
7941 check_cursor();
7942 update_topline();
7943 /*
7944 * When called from normal "g" command: start Select mode when
7945 * 'selectmode' contains "cmd". When called for K_SELECT, always
7946 * start Select mode.
7947 */
7948 if (cap->arg)
7949 VIsual_select = TRUE;
7950 else
7951 may_start_select('c');
7952#ifdef FEAT_MOUSE
7953 setmouse();
7954#endif
7955#ifdef FEAT_CLIPBOARD
7956 /* Make sure the clipboard gets updated. Needed because start and
7957 * end are still the same, and the selection needs to be owned */
7958 clip_star.vmode = NUL;
7959#endif
7960 redraw_curbuf_later(INVERTED);
7961 showmode();
7962 }
7963 break;
7964 /*
7965 * "gV": Don't reselect the previous Visual area after a Select mode
7966 * mapping of menu.
7967 */
7968 case 'V':
7969 VIsual_reselect = FALSE;
7970 break;
7971
7972 /*
7973 * "gh": start Select mode.
7974 * "gH": start Select line mode.
7975 * "g^H": start Select block mode.
7976 */
7977 case K_BS:
7978 cap->nchar = Ctrl_H;
7979 /* FALLTHROUGH */
7980 case 'h':
7981 case 'H':
7982 case Ctrl_H:
7983# ifdef EBCDIC
7984 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7985 if (cap->nchar == Ctrl_H)
7986 cap->cmdchar = Ctrl_V;
7987 else
7988# endif
7989 cap->cmdchar = cap->nchar + ('v' - 'h');
7990 cap->arg = TRUE;
7991 nv_visual(cap);
7992 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007993
7994 /* "gn", "gN" visually select next/previous search match
7995 * "gn" selects next match
7996 * "gN" selects previous match
7997 */
7998 case 'N':
7999 case 'n':
8000 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008001 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008002 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 /*
8005 * "gj" and "gk" two new funny movement keys -- up and down
8006 * movement based on *screen* line rather than *file* line.
8007 */
8008 case 'j':
8009 case K_DOWN:
8010 /* with 'nowrap' it works just like the normal "j" command; also when
8011 * in a closed fold */
8012 if (!curwin->w_p_wrap
8013#ifdef FEAT_FOLDING
8014 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8015#endif
8016 )
8017 {
8018 oap->motion_type = MLINE;
8019 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8020 }
8021 else
8022 i = nv_screengo(oap, FORWARD, cap->count1);
8023 if (i == FAIL)
8024 clearopbeep(oap);
8025 break;
8026
8027 case 'k':
8028 case K_UP:
8029 /* with 'nowrap' it works just like the normal "k" command; also when
8030 * in a closed fold */
8031 if (!curwin->w_p_wrap
8032#ifdef FEAT_FOLDING
8033 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8034#endif
8035 )
8036 {
8037 oap->motion_type = MLINE;
8038 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8039 }
8040 else
8041 i = nv_screengo(oap, BACKWARD, cap->count1);
8042 if (i == FAIL)
8043 clearopbeep(oap);
8044 break;
8045
8046 /*
8047 * "gJ": join two lines without inserting a space.
8048 */
8049 case 'J':
8050 nv_join(cap);
8051 break;
8052
8053 /*
8054 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8055 * "gm": middle of "g0" and "g$".
8056 */
8057 case '^':
8058 flag = TRUE;
8059 /* FALLTHROUGH */
8060
8061 case '0':
8062 case 'm':
8063 case K_HOME:
8064 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 oap->motion_type = MCHAR;
8066 oap->inclusive = FALSE;
8067 if (curwin->w_p_wrap
8068#ifdef FEAT_VERTSPLIT
8069 && curwin->w_width != 0
8070#endif
8071 )
8072 {
8073 int width1 = W_WIDTH(curwin) - curwin_col_off();
8074 int width2 = width1 + curwin_col_off2();
8075
8076 validate_virtcol();
8077 i = 0;
8078 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8079 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8080 }
8081 else
8082 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008083 /* Go to the middle of the screen line. When 'number' or
8084 * 'relativenumber' is on and lines are wrapping the middle can be more
8085 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 if (cap->nchar == 'm')
8087 i += (W_WIDTH(curwin) - curwin_col_off()
8088 + ((curwin->w_p_wrap && i > 0)
8089 ? curwin_col_off2() : 0)) / 2;
8090 coladvance((colnr_T)i);
8091 if (flag)
8092 {
8093 do
8094 i = gchar_cursor();
8095 while (vim_iswhite(i) && oneright() == OK);
8096 }
8097 curwin->w_set_curswant = TRUE;
8098 break;
8099
8100 case '_':
8101 /* "g_": to the last non-blank character in the line or <count> lines
8102 * downward. */
8103 cap->oap->motion_type = MCHAR;
8104 cap->oap->inclusive = TRUE;
8105 curwin->w_curswant = MAXCOL;
8106 if (cursor_down((long)(cap->count1 - 1),
8107 cap->oap->op_type == OP_NOP) == FAIL)
8108 clearopbeep(cap->oap);
8109 else
8110 {
8111 char_u *ptr = ml_get_curline();
8112
8113 /* In Visual mode we may end up after the line. */
8114 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8115 --curwin->w_cursor.col;
8116
8117 /* Decrease the cursor column until it's on a non-blank. */
8118 while (curwin->w_cursor.col > 0
8119 && vim_iswhite(ptr[curwin->w_cursor.col]))
8120 --curwin->w_cursor.col;
8121 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008122 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124 break;
8125
8126 case '$':
8127 case K_END:
8128 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
8130 int col_off = curwin_col_off();
8131
8132 oap->motion_type = MCHAR;
8133 oap->inclusive = TRUE;
8134 if (curwin->w_p_wrap
8135#ifdef FEAT_VERTSPLIT
8136 && curwin->w_width != 0
8137#endif
8138 )
8139 {
8140 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8141 if (cap->count1 == 1)
8142 {
8143 int width1 = W_WIDTH(curwin) - col_off;
8144 int width2 = width1 + curwin_col_off2();
8145
8146 validate_virtcol();
8147 i = width1 - 1;
8148 if (curwin->w_virtcol >= (colnr_T)width1)
8149 i += ((curwin->w_virtcol - width1) / width2 + 1)
8150 * width2;
8151 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008152
8153 /* Make sure we stick in this column. */
8154 validate_virtcol();
8155 curwin->w_curswant = curwin->w_virtcol;
8156 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8158 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8159 {
8160 /*
8161 * Check for landing on a character that got split at
8162 * the end of the line. We do not want to advance to
8163 * the next screen line.
8164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 if (curwin->w_virtcol > (colnr_T)i)
8166 --curwin->w_cursor.col;
8167 }
8168#endif
8169 }
8170 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8171 clearopbeep(oap);
8172 }
8173 else
8174 {
8175 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8176 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008177
8178 /* Make sure we stick in this column. */
8179 validate_virtcol();
8180 curwin->w_curswant = curwin->w_virtcol;
8181 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 }
8183 }
8184 break;
8185
8186 /*
8187 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8188 */
8189 case '*':
8190 case '#':
8191#if POUND != '#'
8192 case POUND: /* pound sign (sometimes equal to '#') */
8193#endif
8194 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8195 case ']': /* :tselect for current identifier */
8196 nv_ident(cap);
8197 break;
8198
8199 /*
8200 * ge and gE: go back to end of word
8201 */
8202 case 'e':
8203 case 'E':
8204 oap->motion_type = MCHAR;
8205 curwin->w_set_curswant = TRUE;
8206 oap->inclusive = TRUE;
8207 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8208 clearopbeep(oap);
8209 break;
8210
8211 /*
8212 * "g CTRL-G": display info about cursor position
8213 */
8214 case Ctrl_G:
8215 cursor_pos_info();
8216 break;
8217
8218 /*
8219 * "gi": start Insert at the last position.
8220 */
8221 case 'i':
8222 if (curbuf->b_last_insert.lnum != 0)
8223 {
8224 curwin->w_cursor = curbuf->b_last_insert;
8225 check_cursor_lnum();
8226 i = (int)STRLEN(ml_get_curline());
8227 if (curwin->w_cursor.col > (colnr_T)i)
8228 {
8229#ifdef FEAT_VIRTUALEDIT
8230 if (virtual_active())
8231 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8232#endif
8233 curwin->w_cursor.col = i;
8234 }
8235 }
8236 cap->cmdchar = 'i';
8237 nv_edit(cap);
8238 break;
8239
8240 /*
8241 * "gI": Start insert in column 1.
8242 */
8243 case 'I':
8244 beginline(0);
8245 if (!checkclearopq(oap))
8246 invoke_edit(cap, FALSE, 'g', FALSE);
8247 break;
8248
8249#ifdef FEAT_SEARCHPATH
8250 /*
8251 * "gf": goto file, edit file under cursor
8252 * "]f" and "[f": can also be used.
8253 */
8254 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008255 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 nv_gotofile(cap);
8257 break;
8258#endif
8259
8260 /* "g'm" and "g`m": jump to mark without setting pcmark */
8261 case '\'':
8262 cap->arg = TRUE;
8263 /*FALLTHROUGH*/
8264 case '`':
8265 nv_gomark(cap);
8266 break;
8267
8268 /*
8269 * "gs": Goto sleep.
8270 */
8271 case 's':
8272 do_sleep(cap->count1 * 1000L);
8273 break;
8274
8275 /*
8276 * "ga": Display the ascii value of the character under the
8277 * cursor. It is displayed in decimal, hex, and octal. -- webb
8278 */
8279 case 'a':
8280 do_ascii(NULL);
8281 break;
8282
8283#ifdef FEAT_MBYTE
8284 /*
8285 * "g8": Display the bytes used for the UTF-8 character under the
8286 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008287 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 */
8289 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008290 if (cap->count0 == 8)
8291 utf_find_illegal();
8292 else
8293 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 break;
8295#endif
8296
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008297 case '<':
8298 show_sb_text();
8299 break;
8300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 /*
8302 * "gg": Goto the first line in file. With a count it goes to
8303 * that line number like for "G". -- webb
8304 */
8305 case 'g':
8306 cap->arg = FALSE;
8307 nv_goto(cap);
8308 break;
8309
8310 /*
8311 * Two-character operators:
8312 * "gq" Format text
8313 * "gw" Format text and keep cursor position
8314 * "g~" Toggle the case of the text.
8315 * "gu" Change text to lower case.
8316 * "gU" Change text to upper case.
8317 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008318 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 */
8320 case 'q':
8321 case 'w':
8322 oap->cursor_start = curwin->w_cursor;
8323 /*FALLTHROUGH*/
8324 case '~':
8325 case 'u':
8326 case 'U':
8327 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008328 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 nv_operator(cap);
8330 break;
8331
8332 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008333 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 * current function
8335 * "gD": idem, but in the current file.
8336 */
8337 case 'd':
8338 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008339 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 break;
8341
8342#ifdef FEAT_MOUSE
8343 /*
8344 * g<*Mouse> : <C-*mouse>
8345 */
8346 case K_MIDDLEMOUSE:
8347 case K_MIDDLEDRAG:
8348 case K_MIDDLERELEASE:
8349 case K_LEFTMOUSE:
8350 case K_LEFTDRAG:
8351 case K_LEFTRELEASE:
8352 case K_RIGHTMOUSE:
8353 case K_RIGHTDRAG:
8354 case K_RIGHTRELEASE:
8355 case K_X1MOUSE:
8356 case K_X1DRAG:
8357 case K_X1RELEASE:
8358 case K_X2MOUSE:
8359 case K_X2DRAG:
8360 case K_X2RELEASE:
8361 mod_mask = MOD_MASK_CTRL;
8362 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8363 break;
8364#endif
8365
8366 case K_IGNORE:
8367 break;
8368
8369 /*
8370 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8371 */
8372 case 'p':
8373 case 'P':
8374 nv_put(cap);
8375 break;
8376
8377#ifdef FEAT_BYTEOFF
8378 /* "go": goto byte count from start of buffer */
8379 case 'o':
8380 goto_byte(cap->count0);
8381 break;
8382#endif
8383
8384 /* "gQ": improved Ex mode */
8385 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008386 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {
8388 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008389 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 break;
8391 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008392
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 if (!checkclearopq(oap))
8394 do_exmode(TRUE);
8395 break;
8396
8397#ifdef FEAT_JUMPLIST
8398 case ',':
8399 nv_pcmark(cap);
8400 break;
8401
8402 case ';':
8403 cap->count1 = -cap->count1;
8404 nv_pcmark(cap);
8405 break;
8406#endif
8407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408#ifdef FEAT_WINDOWS
8409 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008410 if (!checkclearop(oap))
8411 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008412 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008413 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008414 if (!checkclearop(oap))
8415 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008416 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008417#endif
8418
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008419 case '+':
8420 case '-': /* "g+" and "g-": undo or redo along the timeline */
8421 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008422 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008423 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008424 break;
8425
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 default:
8427 clearopbeep(oap);
8428 break;
8429 }
8430}
8431
8432/*
8433 * Handle "o" and "O" commands.
8434 */
8435 static void
8436n_opencmd(cap)
8437 cmdarg_T *cap;
8438{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008439#ifdef FEAT_CONCEAL
8440 linenr_T oldline = curwin->w_cursor.lnum;
8441#endif
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 if (!checkclearopq(cap->oap))
8444 {
8445#ifdef FEAT_FOLDING
8446 if (cap->cmdchar == 'O')
8447 /* Open above the first line of a folded sequence of lines */
8448 (void)hasFolding(curwin->w_cursor.lnum,
8449 &curwin->w_cursor.lnum, NULL);
8450 else
8451 /* Open below the last line of a folded sequence of lines */
8452 (void)hasFolding(curwin->w_cursor.lnum,
8453 NULL, &curwin->w_cursor.lnum);
8454#endif
8455 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8456 (cap->cmdchar == 'O' ? 1 : 0)),
8457 (linenr_T)(curwin->w_cursor.lnum +
8458 (cap->cmdchar == 'o' ? 1 : 0))
8459 ) == OK
8460 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8461#ifdef FEAT_COMMENTS
8462 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8463#endif
8464 0, 0))
8465 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008466#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008467 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008468 update_single_line(curwin, oldline);
8469#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008470 /* When '#' is in 'cpoptions' ignore the count. */
8471 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8472 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008473#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008474 if (curwin->w_p_cul)
8475 /* force redraw of cursorline */
8476 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8479 }
8480 }
8481}
8482
8483/*
8484 * "." command: redo last change.
8485 */
8486 static void
8487nv_dot(cap)
8488 cmdarg_T *cap;
8489{
8490 if (!checkclearopq(cap->oap))
8491 {
8492 /*
8493 * If "restart_edit" is TRUE, the last but one command is repeated
8494 * instead of the last command (inserting text). This is used for
8495 * CTRL-O <.> in insert mode.
8496 */
8497 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8498 clearopbeep(cap->oap);
8499 }
8500}
8501
8502/*
8503 * CTRL-R: undo undo
8504 */
8505 static void
8506nv_redo(cap)
8507 cmdarg_T *cap;
8508{
8509 if (!checkclearopq(cap->oap))
8510 {
8511 u_redo((int)cap->count1);
8512 curwin->w_set_curswant = TRUE;
8513 }
8514}
8515
8516/*
8517 * Handle "U" command.
8518 */
8519 static void
8520nv_Undo(cap)
8521 cmdarg_T *cap;
8522{
8523 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008524 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
8526 /* translate "gUU" to "gUgU" */
8527 cap->cmdchar = 'g';
8528 cap->nchar = 'U';
8529 nv_operator(cap);
8530 }
8531 else if (!checkclearopq(cap->oap))
8532 {
8533 u_undoline();
8534 curwin->w_set_curswant = TRUE;
8535 }
8536}
8537
8538/*
8539 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8540 * single character.
8541 */
8542 static void
8543nv_tilde(cap)
8544 cmdarg_T *cap;
8545{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008546 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 n_swapchar(cap);
8548 else
8549 nv_operator(cap);
8550}
8551
8552/*
8553 * Handle an operator command.
8554 * The actual work is done by do_pending_operator().
8555 */
8556 static void
8557nv_operator(cap)
8558 cmdarg_T *cap;
8559{
8560 int op_type;
8561
8562 op_type = get_op_type(cap->cmdchar, cap->nchar);
8563
8564 if (op_type == cap->oap->op_type) /* double operator works on lines */
8565 nv_lineop(cap);
8566 else if (!checkclearop(cap->oap))
8567 {
8568 cap->oap->start = curwin->w_cursor;
8569 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008570#ifdef FEAT_EVAL
8571 set_op_var(op_type);
8572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 }
8574}
8575
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008576#ifdef FEAT_EVAL
8577/*
8578 * Set v:operator to the characters for "optype".
8579 */
8580 static void
8581set_op_var(optype)
8582 int optype;
8583{
8584 char_u opchars[3];
8585
8586 if (optype == OP_NOP)
8587 set_vim_var_string(VV_OP, NULL, 0);
8588 else
8589 {
8590 opchars[0] = get_op_char(optype);
8591 opchars[1] = get_extra_op_char(optype);
8592 opchars[2] = NUL;
8593 set_vim_var_string(VV_OP, opchars, -1);
8594 }
8595}
8596#endif
8597
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598/*
8599 * Handle linewise operator "dd", "yy", etc.
8600 *
8601 * "_" is is a strange motion command that helps make operators more logical.
8602 * It is actually implemented, but not documented in the real Vi. This motion
8603 * command actually refers to "the current line". Commands like "dd" and "yy"
8604 * are really an alternate form of "d_" and "y_". It does accept a count, so
8605 * "d3_" works to delete 3 lines.
8606 */
8607 static void
8608nv_lineop(cap)
8609 cmdarg_T *cap;
8610{
8611 cap->oap->motion_type = MLINE;
8612 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8613 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008614 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8615 && cap->oap->motion_force != 'v'
8616 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 || cap->oap->op_type == OP_LSHIFT
8618 || cap->oap->op_type == OP_RSHIFT)
8619 beginline(BL_SOL | BL_FIX);
8620 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8621 beginline(BL_WHITE | BL_FIX);
8622}
8623
8624/*
8625 * <Home> command.
8626 */
8627 static void
8628nv_home(cap)
8629 cmdarg_T *cap;
8630{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008631 /* CTRL-HOME is like "gg" */
8632 if (mod_mask & MOD_MASK_CTRL)
8633 nv_goto(cap);
8634 else
8635 {
8636 cap->count0 = 1;
8637 nv_pipe(cap);
8638 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008639 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8640 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641}
8642
8643/*
8644 * "|" command.
8645 */
8646 static void
8647nv_pipe(cap)
8648 cmdarg_T *cap;
8649{
8650 cap->oap->motion_type = MCHAR;
8651 cap->oap->inclusive = FALSE;
8652 beginline(0);
8653 if (cap->count0 > 0)
8654 {
8655 coladvance((colnr_T)(cap->count0 - 1));
8656 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8657 }
8658 else
8659 curwin->w_curswant = 0;
8660 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008661 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 curwin->w_set_curswant = FALSE;
8663}
8664
8665/*
8666 * Handle back-word command "b" and "B".
8667 * cap->arg is 1 for "B"
8668 */
8669 static void
8670nv_bck_word(cap)
8671 cmdarg_T *cap;
8672{
8673 cap->oap->motion_type = MCHAR;
8674 cap->oap->inclusive = FALSE;
8675 curwin->w_set_curswant = TRUE;
8676 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8677 clearopbeep(cap->oap);
8678#ifdef FEAT_FOLDING
8679 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8680 foldOpenCursor();
8681#endif
8682}
8683
8684/*
8685 * Handle word motion commands "e", "E", "w" and "W".
8686 * cap->arg is TRUE for "E" and "W".
8687 */
8688 static void
8689nv_wordcmd(cap)
8690 cmdarg_T *cap;
8691{
8692 int n;
8693 int word_end;
8694 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008695 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
8697 /*
8698 * Set inclusive for the "E" and "e" command.
8699 */
8700 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8701 word_end = TRUE;
8702 else
8703 word_end = FALSE;
8704 cap->oap->inclusive = word_end;
8705
8706 /*
8707 * "cw" and "cW" are a special case.
8708 */
8709 if (!word_end && cap->oap->op_type == OP_CHANGE)
8710 {
8711 n = gchar_cursor();
8712 if (n != NUL) /* not an empty line */
8713 {
8714 if (vim_iswhite(n))
8715 {
8716 /*
8717 * Reproduce a funny Vi behaviour: "cw" on a blank only
8718 * changes one character, not all blanks until the start of
8719 * the next word. Only do this when the 'w' flag is included
8720 * in 'cpoptions'.
8721 */
8722 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8723 {
8724 cap->oap->inclusive = TRUE;
8725 cap->oap->motion_type = MCHAR;
8726 return;
8727 }
8728 }
8729 else
8730 {
8731 /*
8732 * This is a little strange. To match what the real Vi does,
8733 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8734 * that we are not on a space or a TAB. This seems impolite
8735 * at first, but it's really more what we mean when we say
8736 * 'cw'.
8737 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008738 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 * will change only one character! This is done by setting
8740 * flag.
8741 */
8742 cap->oap->inclusive = TRUE;
8743 word_end = TRUE;
8744 flag = TRUE;
8745 }
8746 }
8747 }
8748
8749 cap->oap->motion_type = MCHAR;
8750 curwin->w_set_curswant = TRUE;
8751 if (word_end)
8752 n = end_word(cap->count1, cap->arg, flag, FALSE);
8753 else
8754 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8755
Bram Moolenaardfefb982008-04-01 10:06:39 +00008756 /* Don't leave the cursor on the NUL past the end of line. Unless we
8757 * didn't move it forward. */
8758 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008759 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
8761 if (n == FAIL && cap->oap->op_type == OP_NOP)
8762 clearopbeep(cap->oap);
8763 else
8764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766#ifdef FEAT_FOLDING
8767 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8768 foldOpenCursor();
8769#endif
8770 }
8771}
8772
8773/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008774 * Used after a movement command: If the cursor ends up on the NUL after the
8775 * end of the line, may move it back to the last character and make the motion
8776 * inclusive.
8777 */
8778 static void
8779adjust_cursor(oap)
8780 oparg_T *oap;
8781{
8782 /* The cursor cannot remain on the NUL when:
8783 * - the column is > 0
8784 * - not in Visual mode or 'selection' is "o"
8785 * - 'virtualedit' is not "all" and not "onemore".
8786 */
8787 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008788 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008789#ifdef FEAT_VIRTUALEDIT
8790 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8791#endif
8792 )
8793 {
8794 --curwin->w_cursor.col;
8795#ifdef FEAT_MBYTE
8796 /* prevent cursor from moving on the trail byte */
8797 if (has_mbyte)
8798 mb_adjust_cursor();
8799#endif
8800 oap->inclusive = TRUE;
8801 }
8802}
8803
8804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 * "0" and "^" commands.
8806 * cap->arg is the argument for beginline().
8807 */
8808 static void
8809nv_beginline(cap)
8810 cmdarg_T *cap;
8811{
8812 cap->oap->motion_type = MCHAR;
8813 cap->oap->inclusive = FALSE;
8814 beginline(cap->arg);
8815#ifdef FEAT_FOLDING
8816 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8817 foldOpenCursor();
8818#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008819 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8820 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821}
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * In exclusive Visual mode, may include the last character.
8825 */
8826 static void
8827adjust_for_sel(cap)
8828 cmdarg_T *cap;
8829{
8830 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8831 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8832 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008833#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (has_mbyte)
8835 inc_cursor();
8836 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 ++curwin->w_cursor.col;
8839 cap->oap->inclusive = FALSE;
8840 }
8841}
8842
8843/*
8844 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8845 * Should check VIsual_mode before calling this.
8846 * Returns TRUE when backed up to the previous line.
8847 */
8848 static int
8849unadjust_for_sel()
8850{
8851 pos_T *pp;
8852
8853 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8854 {
8855 if (lt(VIsual, curwin->w_cursor))
8856 pp = &curwin->w_cursor;
8857 else
8858 pp = &VIsual;
8859#ifdef FEAT_VIRTUALEDIT
8860 if (pp->coladd > 0)
8861 --pp->coladd;
8862 else
8863#endif
8864 if (pp->col > 0)
8865 {
8866 --pp->col;
8867#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008868 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869#endif
8870 }
8871 else if (pp->lnum > 1)
8872 {
8873 --pp->lnum;
8874 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8875 return TRUE;
8876 }
8877 }
8878 return FALSE;
8879}
8880
8881/*
8882 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8883 */
8884 static void
8885nv_select(cap)
8886 cmdarg_T *cap;
8887{
8888 if (VIsual_active)
8889 VIsual_select = TRUE;
8890 else if (VIsual_reselect)
8891 {
8892 cap->nchar = 'v'; /* fake "gv" command */
8893 cap->arg = TRUE;
8894 nv_g_cmd(cap);
8895 }
8896}
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
8899/*
8900 * "G", "gg", CTRL-END, CTRL-HOME.
8901 * cap->arg is TRUE for "G".
8902 */
8903 static void
8904nv_goto(cap)
8905 cmdarg_T *cap;
8906{
8907 linenr_T lnum;
8908
8909 if (cap->arg)
8910 lnum = curbuf->b_ml.ml_line_count;
8911 else
8912 lnum = 1L;
8913 cap->oap->motion_type = MLINE;
8914 setpcmark();
8915
8916 /* When a count is given, use it instead of the default lnum */
8917 if (cap->count0 != 0)
8918 lnum = cap->count0;
8919 if (lnum < 1L)
8920 lnum = 1L;
8921 else if (lnum > curbuf->b_ml.ml_line_count)
8922 lnum = curbuf->b_ml.ml_line_count;
8923 curwin->w_cursor.lnum = lnum;
8924 beginline(BL_SOL | BL_FIX);
8925#ifdef FEAT_FOLDING
8926 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8927 foldOpenCursor();
8928#endif
8929}
8930
8931/*
8932 * CTRL-\ in Normal mode.
8933 */
8934 static void
8935nv_normal(cap)
8936 cmdarg_T *cap;
8937{
8938 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8939 {
8940 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008941 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 clear_cmdline = TRUE; /* unshow mode later */
8943 restart_edit = 0;
8944#ifdef FEAT_CMDWIN
8945 if (cmdwin_type != 0)
8946 cmdwin_result = Ctrl_C;
8947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 if (VIsual_active)
8949 {
8950 end_visual_mode(); /* stop Visual */
8951 redraw_curbuf_later(INVERTED);
8952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8954 if (cap->nchar == Ctrl_G && p_im)
8955 restart_edit = 'a';
8956 }
8957 else
8958 clearopbeep(cap->oap);
8959}
8960
8961/*
8962 * ESC in Normal mode: beep, but don't flush buffers.
8963 * Don't even beep if we are canceling a command.
8964 */
8965 static void
8966nv_esc(cap)
8967 cmdarg_T *cap;
8968{
8969 int no_reason;
8970
8971 no_reason = (cap->oap->op_type == OP_NOP
8972 && cap->opcount == 0
8973 && cap->count0 == 0
8974 && cap->oap->regname == 0
8975 && !p_im);
8976
8977 if (cap->arg) /* TRUE for CTRL-C */
8978 {
8979 if (restart_edit == 0
8980#ifdef FEAT_CMDWIN
8981 && cmdwin_type == 0
8982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 && no_reason)
8985 MSG(_("Type :quit<Enter> to exit Vim"));
8986
8987 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8988 * set again below when halfway a mapping. */
8989 if (!p_im)
8990 restart_edit = 0;
8991#ifdef FEAT_CMDWIN
8992 if (cmdwin_type != 0)
8993 {
8994 cmdwin_result = K_IGNORE;
8995 got_int = FALSE; /* don't stop executing autocommands et al. */
8996 return;
8997 }
8998#endif
8999 }
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 if (VIsual_active)
9002 {
9003 end_visual_mode(); /* stop Visual */
9004 check_cursor_col(); /* make sure cursor is not beyond EOL */
9005 curwin->w_set_curswant = TRUE;
9006 redraw_curbuf_later(INVERTED);
9007 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009008 else if (no_reason)
9009 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 clearop(cap->oap);
9011
9012 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9013 * set return to Insert mode afterwards. */
9014 if (restart_edit == 0 && goto_im()
9015#ifdef FEAT_EX_EXTRA
9016 && ex_normal_busy == 0
9017#endif
9018 )
9019 restart_edit = 'a';
9020}
9021
9022/*
9023 * Handle "A", "a", "I", "i" and <Insert> commands.
9024 */
9025 static void
9026nv_edit(cap)
9027 cmdarg_T *cap;
9028{
9029 /* <Insert> is equal to "i" */
9030 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9031 cap->cmdchar = 'i';
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 /* in Visual mode "A" and "I" are an operator */
9034 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9035 v_visop(cap);
9036
9037 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009038 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9039 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 {
9041#ifdef FEAT_TEXTOBJ
9042 nv_object(cap);
9043#else
9044 clearopbeep(cap->oap);
9045#endif
9046 }
9047 else if (!curbuf->b_p_ma && !p_im)
9048 {
9049 /* Only give this error when 'insertmode' is off. */
9050 EMSG(_(e_modifiable));
9051 clearop(cap->oap);
9052 }
9053 else if (!checkclearopq(cap->oap))
9054 {
9055 switch (cap->cmdchar)
9056 {
9057 case 'A': /* "A"ppend after the line */
9058 curwin->w_set_curswant = TRUE;
9059#ifdef FEAT_VIRTUALEDIT
9060 if (ve_flags == VE_ALL)
9061 {
9062 int save_State = State;
9063
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009064 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 * character past the end of the line */
9066 State = INSERT;
9067 coladvance((colnr_T)MAXCOL);
9068 State = save_State;
9069 }
9070 else
9071#endif
9072 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9073 break;
9074
9075 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009076 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9077 beginline(BL_WHITE);
9078 else
9079 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 break;
9081
9082 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9083#ifdef FEAT_VIRTUALEDIT
9084 /* increment coladd when in virtual space, increment the
9085 * column otherwise, also to append after an unprintable char */
9086 if (virtual_active()
9087 && (curwin->w_cursor.coladd > 0
9088 || *ml_get_cursor() == NUL
9089 || *ml_get_cursor() == TAB))
9090 curwin->w_cursor.coladd++;
9091 else
9092#endif
9093 if (*ml_get_cursor() != NUL)
9094 inc_cursor();
9095 break;
9096 }
9097
9098#ifdef FEAT_VIRTUALEDIT
9099 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9100 {
9101 int save_State = State;
9102
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009103 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 * character past the end of the line */
9105 State = INSERT;
9106 coladvance(getviscol());
9107 State = save_State;
9108 }
9109#endif
9110
9111 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9112 }
9113}
9114
9115/*
9116 * Invoke edit() and take care of "restart_edit" and the return value.
9117 */
9118 static void
9119invoke_edit(cap, repl, cmd, startln)
9120 cmdarg_T *cap;
9121 int repl; /* "r" or "gr" command */
9122 int cmd;
9123 int startln;
9124{
9125 int restart_edit_save = 0;
9126
9127 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9128 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9129 * it. */
9130 if (repl || !stuff_empty())
9131 restart_edit_save = restart_edit;
9132 else
9133 restart_edit_save = 0;
9134
9135 /* Always reset "restart_edit", this is not a restarted edit. */
9136 restart_edit = 0;
9137
9138 if (edit(cmd, startln, cap->count1))
9139 cap->retval |= CA_COMMAND_BUSY;
9140
9141 if (restart_edit == 0)
9142 restart_edit = restart_edit_save;
9143}
9144
9145#ifdef FEAT_TEXTOBJ
9146/*
9147 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9148 */
9149 static void
9150nv_object(cap)
9151 cmdarg_T *cap;
9152{
9153 int flag;
9154 int include;
9155 char_u *mps_save;
9156
9157 if (cap->cmdchar == 'i')
9158 include = FALSE; /* "ix" = inner object: exclude white space */
9159 else
9160 include = TRUE; /* "ax" = an object: include white space */
9161
9162 /* Make sure (), [], {} and <> are in 'matchpairs' */
9163 mps_save = curbuf->b_p_mps;
9164 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9165
9166 switch (cap->nchar)
9167 {
9168 case 'w': /* "aw" = a word */
9169 flag = current_word(cap->oap, cap->count1, include, FALSE);
9170 break;
9171 case 'W': /* "aW" = a WORD */
9172 flag = current_word(cap->oap, cap->count1, include, TRUE);
9173 break;
9174 case 'b': /* "ab" = a braces block */
9175 case '(':
9176 case ')':
9177 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9178 break;
9179 case 'B': /* "aB" = a Brackets block */
9180 case '{':
9181 case '}':
9182 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9183 break;
9184 case '[': /* "a[" = a [] block */
9185 case ']':
9186 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9187 break;
9188 case '<': /* "a<" = a <> block */
9189 case '>':
9190 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9191 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009192 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009193 /* Do not adjust oap->end in do_pending_operator()
9194 * otherwise there are different results for 'dit'
9195 * (note leading whitespace in last line):
9196 * 1) <b> 2) <b>
9197 * foobar foobar
9198 * </b> </b>
9199 */
9200 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009201 flag = current_tagblock(cap->oap, cap->count1, include);
9202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 case 'p': /* "ap" = a paragraph */
9204 flag = current_par(cap->oap, cap->count1, include, 'p');
9205 break;
9206 case 's': /* "as" = a sentence */
9207 flag = current_sent(cap->oap, cap->count1, include);
9208 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009209 case '"': /* "a"" = a double quoted string */
9210 case '\'': /* "a'" = a single quoted string */
9211 case '`': /* "a`" = a backtick quoted string */
9212 flag = current_quote(cap->oap, cap->count1, include,
9213 cap->nchar);
9214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215#if 0 /* TODO */
9216 case 'S': /* "aS" = a section */
9217 case 'f': /* "af" = a filename */
9218 case 'u': /* "au" = a URL */
9219#endif
9220 default:
9221 flag = FAIL;
9222 break;
9223 }
9224
9225 curbuf->b_p_mps = mps_save;
9226 if (flag == FAIL)
9227 clearopbeep(cap->oap);
9228 adjust_cursor_col();
9229 curwin->w_set_curswant = TRUE;
9230}
9231#endif
9232
9233/*
9234 * "q" command: Start/stop recording.
9235 * "q:", "q/", "q?": edit command-line in command-line window.
9236 */
9237 static void
9238nv_record(cap)
9239 cmdarg_T *cap;
9240{
9241 if (cap->oap->op_type == OP_FORMAT)
9242 {
9243 /* "gqq" is the same as "gqgq": format line */
9244 cap->cmdchar = 'g';
9245 cap->nchar = 'q';
9246 nv_operator(cap);
9247 }
9248 else if (!checkclearop(cap->oap))
9249 {
9250#ifdef FEAT_CMDWIN
9251 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9252 {
9253 stuffcharReadbuff(cap->nchar);
9254 stuffcharReadbuff(K_CMDWIN);
9255 }
9256 else
9257#endif
9258 /* (stop) recording into a named register, unless executing a
9259 * register */
9260 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9261 clearopbeep(cap->oap);
9262 }
9263}
9264
9265/*
9266 * Handle the "@r" command.
9267 */
9268 static void
9269nv_at(cap)
9270 cmdarg_T *cap;
9271{
9272 if (checkclearop(cap->oap))
9273 return;
9274#ifdef FEAT_EVAL
9275 if (cap->nchar == '=')
9276 {
9277 if (get_expr_register() == NUL)
9278 return;
9279 }
9280#endif
9281 while (cap->count1-- && !got_int)
9282 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009283 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 {
9285 clearopbeep(cap->oap);
9286 break;
9287 }
9288 line_breakcheck();
9289 }
9290}
9291
9292/*
9293 * Handle the CTRL-U and CTRL-D commands.
9294 */
9295 static void
9296nv_halfpage(cap)
9297 cmdarg_T *cap;
9298{
9299 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9300 || (cap->cmdchar == Ctrl_D
9301 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9302 clearopbeep(cap->oap);
9303 else if (!checkclearop(cap->oap))
9304 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9305}
9306
9307/*
9308 * Handle "J" or "gJ" command.
9309 */
9310 static void
9311nv_join(cap)
9312 cmdarg_T *cap;
9313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 if (VIsual_active) /* join the visual lines */
9315 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009316 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 {
9318 if (cap->count0 <= 1)
9319 cap->count0 = 2; /* default for join is two lines! */
9320 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9321 curbuf->b_ml.ml_line_count)
9322 clearopbeep(cap->oap); /* beyond last line */
9323 else
9324 {
9325 prep_redo(cap->oap->regname, cap->count0,
9326 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009327 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 }
9329 }
9330}
9331
9332/*
9333 * "P", "gP", "p" and "gp" commands.
9334 */
9335 static void
9336nv_put(cap)
9337 cmdarg_T *cap;
9338{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 int regname = 0;
9340 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009341 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009342 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 int dir;
9344 int flags = 0;
9345
9346 if (cap->oap->op_type != OP_NOP)
9347 {
9348#ifdef FEAT_DIFF
9349 /* "dp" is ":diffput" */
9350 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9351 {
9352 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009353 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 }
9355 else
9356#endif
9357 clearopbeep(cap->oap);
9358 }
9359 else
9360 {
9361 dir = (cap->cmdchar == 'P'
9362 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9363 ? BACKWARD : FORWARD;
9364 prep_redo_cmd(cap);
9365 if (cap->cmdchar == 'g')
9366 flags |= PUT_CURSEND;
9367
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 if (VIsual_active)
9369 {
9370 /* Putting in Visual mode: The put text replaces the selected
9371 * text. First delete the selected text, then put the new text.
9372 * Need to save and restore the registers that the delete
9373 * overwrites if the old contents is being put.
9374 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009375 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009377#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009379#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009380 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009381 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009382#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385
9386 )
9387 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009388 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 * put, save it first. */
9390 reg1 = get_register(regname, TRUE);
9391 }
9392
9393 /* Now delete the selected text. */
9394 cap->cmdchar = 'd';
9395 cap->nchar = NUL;
9396 cap->oap->regname = NUL;
9397 nv_operator(cap);
9398 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009399 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400
9401 /* delete PUT_LINE_BACKWARD; */
9402 cap->oap->regname = regname;
9403
9404 if (reg1 != NULL)
9405 {
9406 /* Delete probably changed the register we want to put, save
9407 * it first. Then put back what was there before the delete. */
9408 reg2 = get_register(regname, FALSE);
9409 put_register(regname, reg1);
9410 }
9411
9412 /* When deleted a linewise Visual area, put the register as
9413 * lines to avoid it joined with the next line. When deletion was
9414 * characterwise, split a line when putting lines. */
9415 if (VIsual_mode == 'V')
9416 flags |= PUT_LINE;
9417 else if (VIsual_mode == 'v')
9418 flags |= PUT_LINE_SPLIT;
9419 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9420 flags |= PUT_LINE_FORWARD;
9421 dir = BACKWARD;
9422 if ((VIsual_mode != 'V'
9423 && curwin->w_cursor.col < curbuf->b_op_start.col)
9424 || (VIsual_mode == 'V'
9425 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9426 /* cursor is at the end of the line or end of file, put
9427 * forward. */
9428 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009429 /* May have been reset in do_put(). */
9430 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 do_put(cap->oap->regname, dir, cap->count1, flags);
9433
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 /* If a register was saved, put it back now. */
9435 if (reg2 != NULL)
9436 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009437
9438 /* What to reselect with "gv"? Selecting the just put text seems to
9439 * be the most useful, since the original text was removed. */
9440 if (was_visual)
9441 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009442 curbuf->b_visual.vi_start = curbuf->b_op_start;
9443 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009444 }
9445
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009446 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009447 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009448 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009449 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009450 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009451
9452 /* If the cursor was in that line, move it to the end of the last
9453 * line. */
9454 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9455 {
9456 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9457 coladvance((colnr_T)MAXCOL);
9458 }
9459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 auto_format(FALSE, TRUE);
9461 }
9462}
9463
9464/*
9465 * "o" and "O" commands.
9466 */
9467 static void
9468nv_open(cap)
9469 cmdarg_T *cap;
9470{
9471#ifdef FEAT_DIFF
9472 /* "do" is ":diffget" */
9473 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9474 {
9475 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009476 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 }
9478 else
9479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (VIsual_active) /* switch start and end of visual */
9481 v_swap_corners(cap->cmdchar);
9482 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 n_opencmd(cap);
9484}
9485
9486#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 static void
9488nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009489 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 ProcessSniffRequests();
9492}
9493#endif
9494
9495#ifdef FEAT_NETBEANS_INTG
9496 static void
9497nv_nbcmd(cap)
9498 cmdarg_T *cap;
9499{
9500 netbeans_keycommand(cap->nchar);
9501}
9502#endif
9503
9504#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 static void
9506nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009507 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9510}
9511#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009512
9513#ifdef FEAT_AUTOCMD
9514/*
9515 * Trigger CursorHold event.
9516 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9517 * input buffer. "did_cursorhold" is set to avoid retriggering.
9518 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009519 static void
9520nv_cursorhold(cap)
9521 cmdarg_T *cap;
9522{
9523 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9524 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009525 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009526}
9527#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009528
9529/*
9530 * calculate start/end virtual columns for operating in block mode
9531 */
9532 static void
9533get_op_vcol(oap, redo_VIsual_vcol, initial)
9534 oparg_T *oap;
9535 colnr_T redo_VIsual_vcol;
9536 int initial; /* when true: adjust position for 'selectmode' */
9537{
9538 colnr_T start, end;
9539
9540 if (VIsual_mode != Ctrl_V)
9541 return;
9542
9543 oap->block_mode = TRUE;
9544
9545#ifdef FEAT_MBYTE
9546 /* prevent from moving onto a trail byte */
9547 if (has_mbyte)
9548 mb_adjustpos(curwin->w_buffer, &oap->end);
9549#endif
9550
9551 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
9552 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9553
9554 if (start < oap->start_vcol)
9555 oap->start_vcol = start;
9556 if (end > oap->end_vcol)
9557 {
9558 if (initial && *p_sel == 'e' && start >= 1
9559 && start - 1 >= oap->end_vcol)
9560 oap->end_vcol = start - 1;
9561 else
9562 oap->end_vcol = end;
9563 }
9564 /* if '$' was used, get oap->end_vcol from longest line */
9565 if (curwin->w_curswant == MAXCOL)
9566 {
9567 curwin->w_cursor.col = MAXCOL;
9568 oap->end_vcol = 0;
9569 for (curwin->w_cursor.lnum = oap->start.lnum;
9570 curwin->w_cursor.lnum <= oap->end.lnum;
9571 ++curwin->w_cursor.lnum)
9572 {
9573 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9574 if (end > oap->end_vcol)
9575 oap->end_vcol = end;
9576 }
9577 }
9578 else if (redo_VIsual_busy)
9579 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9580 /*
9581 * Correct oap->end.col and oap->start.col to be the
9582 * upper-left and lower-right corner of the block area.
9583 *
9584 * (Actually, this does convert column positions into character
9585 * positions)
9586 */
9587 curwin->w_cursor.lnum = oap->end.lnum;
9588 coladvance(oap->end_vcol);
9589 oap->end = curwin->w_cursor;
9590
9591 curwin->w_cursor = oap->start;
9592 coladvance(oap->start_vcol);
9593 oap->start = curwin->w_cursor;
9594}