blob: 9c9537f4b04172ef7cec047c1c8e78e26e93402b [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{
4204 if (!checkclearopq(cap->oap)
4205 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4206 prep_redo_cmd(cap);
4207}
4208
4209/*
4210 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4211 */
4212 static void
4213nv_page(cap)
4214 cmdarg_T *cap;
4215{
4216 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004217 {
4218#ifdef FEAT_WINDOWS
4219 if (mod_mask & MOD_MASK_CTRL)
4220 {
4221 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4222 if (cap->arg == BACKWARD)
4223 goto_tabpage(-(int)cap->count1);
4224 else
4225 goto_tabpage((int)cap->count0);
4226 }
4227 else
4228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
4233/*
4234 * Implementation of "gd" and "gD" command.
4235 */
4236 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004237nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238 oparg_T *oap;
4239 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004240 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 char_u *ptr;
4244
Bram Moolenaard9d30582005-05-18 22:10:28 +00004245 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004246 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004248#ifdef FEAT_FOLDING
4249 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4250 foldOpenCursor();
4251#endif
4252}
4253
4254/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004255 * Search for variable declaration of "ptr[len]".
4256 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4257 * current file ("gD").
4258 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004259 * Return FAIL when not found.
4260 */
4261 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004262find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263 char_u *ptr;
4264 int len;
4265 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004266 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 int searchflags; /* flags passed to searchit() */
4268{
4269 char_u *pat;
4270 pos_T old_pos;
4271 pos_T par_pos;
4272 pos_T found_pos;
4273 int t;
4274 int save_p_ws;
4275 int save_p_scs;
4276 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004277 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004278
4279 if ((pat = alloc(len + 7)) == NULL)
4280 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004281
4282 /* Put "\V" before the pattern to avoid that the special meaning of "."
4283 * and "~" causes trouble. */
4284 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4285 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 old_pos = curwin->w_cursor;
4287 save_p_ws = p_ws;
4288 save_p_scs = p_scs;
4289 p_ws = FALSE; /* don't wrap around end of file now */
4290 p_scs = FALSE; /* don't switch ignorecase off now */
4291
4292 /*
4293 * With "gD" go to line 1.
4294 * With "gd" Search back for the start of the current function, then go
4295 * back until a blank line. If this fails go to line 1.
4296 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004297 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
4299 setpcmark(); /* Set in findpar() otherwise */
4300 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004301 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 else
4304 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004305 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4307 --curwin->w_cursor.lnum;
4308 }
4309 curwin->w_cursor.col = 0;
4310
4311 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004312 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004313 for (;;)
4314 {
4315 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004316 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317 if (curwin->w_cursor.lnum >= old_pos.lnum)
4318 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004319
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004320 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004321 {
4322 pos_T *pos;
4323
4324 /* Check that the block the match is in doesn't end before the
4325 * position where we started the search from. */
4326 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4327 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4328 && pos->lnum < old_pos.lnum)
4329 continue;
4330 }
4331
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 if (t == FAIL)
4333 {
4334 /* If we previously found a valid position, use it. */
4335 if (found_pos.lnum != 0)
4336 {
4337 curwin->w_cursor = found_pos;
4338 t = OK;
4339 }
4340 break;
4341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004343 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344 {
4345 /* Ignore this line, continue at start of next line. */
4346 ++curwin->w_cursor.lnum;
4347 curwin->w_cursor.col = 0;
4348 continue;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351 if (!locally) /* global search: use first match found */
4352 break;
4353 if (curwin->w_cursor.lnum >= par_pos.lnum)
4354 {
4355 /* If we previously found a valid position, use it. */
4356 if (found_pos.lnum != 0)
4357 curwin->w_cursor = found_pos;
4358 break;
4359 }
4360
4361 /* For finding a local variable and the match is before the "{" search
4362 * to find a later match. For K&R style function declarations this
4363 * skips the function header without types. */
4364 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004366
4367 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004369 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 curwin->w_cursor = old_pos;
4371 }
4372 else
4373 {
4374 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 /* "n" searches forward now */
4376 reset_search_dir();
4377 }
4378
4379 vim_free(pat);
4380 p_ws = save_p_ws;
4381 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004382
4383 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384}
4385
4386/*
4387 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4388 * lines rather than lines in the file.
4389 * 'dist' must be positive.
4390 *
4391 * Return OK if able to move cursor, FAIL otherwise.
4392 */
4393 static int
4394nv_screengo(oap, dir, dist)
4395 oparg_T *oap;
4396 int dir;
4397 long dist;
4398{
4399 int linelen = linetabsize(ml_get_curline());
4400 int retval = OK;
4401 int atend = FALSE;
4402 int n;
4403 int col_off1; /* margin offset for first screen line */
4404 int col_off2; /* margin offset for wrapped screen line */
4405 int width1; /* text width for first screen line */
4406 int width2; /* test width for wrapped screen line */
4407
4408 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004409 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411 col_off1 = curwin_col_off();
4412 col_off2 = col_off1 - curwin_col_off2();
4413 width1 = W_WIDTH(curwin) - col_off1;
4414 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004415 if (width2 == 0)
4416 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418#ifdef FEAT_VERTSPLIT
4419 if (curwin->w_width != 0)
4420 {
4421#endif
4422 /*
4423 * Instead of sticking at the last character of the buffer line we
4424 * try to stick in the last column of the screen.
4425 */
4426 if (curwin->w_curswant == MAXCOL)
4427 {
4428 atend = TRUE;
4429 validate_virtcol();
4430 if (width1 <= 0)
4431 curwin->w_curswant = 0;
4432 else
4433 {
4434 curwin->w_curswant = width1 - 1;
4435 if (curwin->w_virtcol > curwin->w_curswant)
4436 curwin->w_curswant += ((curwin->w_virtcol
4437 - curwin->w_curswant - 1) / width2 + 1) * width2;
4438 }
4439 }
4440 else
4441 {
4442 if (linelen > width1)
4443 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4444 else
4445 n = width1;
4446 if (curwin->w_curswant > (colnr_T)n + 1)
4447 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4448 * width2;
4449 }
4450
4451 while (dist--)
4452 {
4453 if (dir == BACKWARD)
4454 {
4455 if ((long)curwin->w_curswant >= width2)
4456 /* move back within line */
4457 curwin->w_curswant -= width2;
4458 else
4459 {
4460 /* to previous line */
4461 if (curwin->w_cursor.lnum == 1)
4462 {
4463 retval = FAIL;
4464 break;
4465 }
4466 --curwin->w_cursor.lnum;
4467#ifdef FEAT_FOLDING
4468 /* Move to the start of a closed fold. Don't do that when
4469 * 'foldopen' contains "all": it will open in a moment. */
4470 if (!(fdo_flags & FDO_ALL))
4471 (void)hasFolding(curwin->w_cursor.lnum,
4472 &curwin->w_cursor.lnum, NULL);
4473#endif
4474 linelen = linetabsize(ml_get_curline());
4475 if (linelen > width1)
4476 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4477 + 1) * width2;
4478 }
4479 }
4480 else /* dir == FORWARD */
4481 {
4482 if (linelen > width1)
4483 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4484 else
4485 n = width1;
4486 if (curwin->w_curswant + width2 < (colnr_T)n)
4487 /* move forward within line */
4488 curwin->w_curswant += width2;
4489 else
4490 {
4491 /* to next line */
4492#ifdef FEAT_FOLDING
4493 /* Move to the end of a closed fold. */
4494 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4495 &curwin->w_cursor.lnum);
4496#endif
4497 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4498 {
4499 retval = FAIL;
4500 break;
4501 }
4502 curwin->w_cursor.lnum++;
4503 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004504 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 }
4507 }
4508#ifdef FEAT_VERTSPLIT
4509 }
4510#endif
4511
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004512 if (virtual_active() && atend)
4513 coladvance(MAXCOL);
4514 else
4515 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4518 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4519 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004520 colnr_T virtcol;
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /*
4523 * Check for landing on a character that got split at the end of the
4524 * last line. We want to advance a screenline, not end up in the same
4525 * screenline or move two screenlines.
4526 */
4527 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004528 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004529# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004530 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4531 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004532# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004533
4534 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 && (curwin->w_curswant < (colnr_T)width1
4536 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4537 : ((curwin->w_curswant - width1) % width2
4538 > (colnr_T)width2 / 2)))
4539 --curwin->w_cursor.col;
4540 }
4541#endif
4542
4543 if (atend)
4544 curwin->w_curswant = MAXCOL; /* stick in the last column */
4545
4546 return retval;
4547}
4548
4549#ifdef FEAT_MOUSE
4550/*
4551 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4552 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004553 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4554 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 */
4556 static void
4557nv_mousescroll(cap)
4558 cmdarg_T *cap;
4559{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004560# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004561 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004563 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 int row, col;
4566
4567 row = mouse_row;
4568 col = mouse_col;
4569
4570 /* find the window at the pointer coordinates */
4571 curwin = mouse_find_win(&row, &col);
4572 curbuf = curwin->w_buffer;
4573 }
4574# endif
4575
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004576 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004578 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4579 {
4580 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4581 }
4582 else
4583 {
4584 cap->count1 = 3;
4585 cap->count0 = 3;
4586 nv_scroll_line(cap);
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 else
4591 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004592 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4593 if (!curwin->w_p_wrap)
4594 {
4595 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004596
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004597 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4598 step = W_WIDTH(curwin);
4599 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4600 if (val < 0)
4601 val = 0;
4602
4603 gui_do_horiz_scroll(val, TRUE);
4604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004608# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 curwin->w_redr_status = TRUE;
4610
4611 curwin = old_curwin;
4612 curbuf = curwin->w_buffer;
4613# endif
4614}
4615
4616/*
4617 * Mouse clicks and drags.
4618 */
4619 static void
4620nv_mouse(cap)
4621 cmdarg_T *cap;
4622{
4623 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4624}
4625#endif
4626
4627/*
4628 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4629 * cap->arg must be TRUE for CTRL-E.
4630 */
4631 static void
4632nv_scroll_line(cap)
4633 cmdarg_T *cap;
4634{
4635 if (!checkclearop(cap->oap))
4636 scroll_redraw(cap->arg, cap->count1);
4637}
4638
4639/*
4640 * Scroll "count" lines up or down, and redraw.
4641 */
4642 void
4643scroll_redraw(up, count)
4644 int up;
4645 long count;
4646{
4647 linenr_T prev_topline = curwin->w_topline;
4648#ifdef FEAT_DIFF
4649 int prev_topfill = curwin->w_topfill;
4650#endif
4651 linenr_T prev_lnum = curwin->w_cursor.lnum;
4652
4653 if (up)
4654 scrollup(count, TRUE);
4655 else
4656 scrolldown(count, TRUE);
4657 if (p_so)
4658 {
4659 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4660 * valid, otherwise the screen jumps back at the end of the file. */
4661 cursor_correct();
4662 check_cursor_moved(curwin);
4663 curwin->w_valid |= VALID_TOPLINE;
4664
4665 /* If moved back to where we were, at least move the cursor, otherwise
4666 * we get stuck at one position. Don't move the cursor up if the
4667 * first line of the buffer is already on the screen */
4668 while (curwin->w_topline == prev_topline
4669#ifdef FEAT_DIFF
4670 && curwin->w_topfill == prev_topfill
4671#endif
4672 )
4673 {
4674 if (up)
4675 {
4676 if (curwin->w_cursor.lnum > prev_lnum
4677 || cursor_down(1L, FALSE) == FAIL)
4678 break;
4679 }
4680 else
4681 {
4682 if (curwin->w_cursor.lnum < prev_lnum
4683 || prev_topline == 1L
4684 || cursor_up(1L, FALSE) == FAIL)
4685 break;
4686 }
4687 /* Mark w_topline as valid, otherwise the screen jumps back at the
4688 * end of the file. */
4689 check_cursor_moved(curwin);
4690 curwin->w_valid |= VALID_TOPLINE;
4691 }
4692 }
4693 if (curwin->w_cursor.lnum != prev_lnum)
4694 coladvance(curwin->w_curswant);
4695 redraw_later(VALID);
4696}
4697
4698/*
4699 * Commands that start with "z".
4700 */
4701 static void
4702nv_zet(cap)
4703 cmdarg_T *cap;
4704{
4705 long n;
4706 colnr_T col;
4707 int nchar = cap->nchar;
4708#ifdef FEAT_FOLDING
4709 long old_fdl = curwin->w_p_fdl;
4710 int old_fen = curwin->w_p_fen;
4711#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004712#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004713 int undo = FALSE;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 if (VIM_ISDIGIT(nchar))
4717 {
4718 /*
4719 * "z123{nchar}": edit the count before obtaining {nchar}
4720 */
4721 if (checkclearop(cap->oap))
4722 return;
4723 n = nchar - '0';
4724 for (;;)
4725 {
4726#ifdef USE_ON_FLY_SCROLL
4727 dont_scroll = TRUE; /* disallow scrolling here */
4728#endif
4729 ++no_mapping;
4730 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004731 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 --no_mapping;
4734 --allow_keys;
4735#ifdef FEAT_CMDL_INFO
4736 (void)add_to_showcmd(nchar);
4737#endif
4738 if (nchar == K_DEL || nchar == K_KDEL)
4739 n /= 10;
4740 else if (VIM_ISDIGIT(nchar))
4741 n = n * 10 + (nchar - '0');
4742 else if (nchar == CAR)
4743 {
4744#ifdef FEAT_GUI
4745 need_mouse_correct = TRUE;
4746#endif
4747 win_setheight((int)n);
4748 break;
4749 }
4750 else if (nchar == 'l'
4751 || nchar == 'h'
4752 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004753 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 cap->count1 = n ? n * cap->count1 : cap->count1;
4756 goto dozet;
4757 }
4758 else
4759 {
4760 clearopbeep(cap->oap);
4761 break;
4762 }
4763 }
4764 cap->oap->op_type = OP_NOP;
4765 return;
4766 }
4767
4768dozet:
4769 if (
4770#ifdef FEAT_FOLDING
4771 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4772 * and "zC" only in Visual mode. "zj" and "zk" are motion
4773 * commands. */
4774 cap->nchar != 'f' && cap->nchar != 'F'
4775 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4776 && cap->nchar != 'j' && cap->nchar != 'k'
4777 &&
4778#endif
4779 checkclearop(cap->oap))
4780 return;
4781
4782 /*
4783 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4784 * If line number given, set cursor.
4785 */
4786 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4787 && cap->count0
4788 && cap->count0 != curwin->w_cursor.lnum)
4789 {
4790 setpcmark();
4791 if (cap->count0 > curbuf->b_ml.ml_line_count)
4792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4793 else
4794 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004795 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797
4798 switch (nchar)
4799 {
4800 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4801 case '+':
4802 if (cap->count0 == 0)
4803 {
4804 /* No count given: put cursor at the line below screen */
4805 validate_botline(); /* make sure w_botline is valid */
4806 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4807 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4808 else
4809 curwin->w_cursor.lnum = curwin->w_botline;
4810 }
4811 /* FALLTHROUGH */
4812 case NL:
4813 case CAR:
4814 case K_KENTER:
4815 beginline(BL_WHITE | BL_FIX);
4816 /* FALLTHROUGH */
4817
4818 case 't': scroll_cursor_top(0, TRUE);
4819 redraw_later(VALID);
4820 break;
4821
4822 /* "z." and "zz": put cursor in middle of screen */
4823 case '.': beginline(BL_WHITE | BL_FIX);
4824 /* FALLTHROUGH */
4825
4826 case 'z': scroll_cursor_halfway(TRUE);
4827 redraw_later(VALID);
4828 break;
4829
4830 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4831 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4832 * when <count> is at bottom of window, and puts that one at
4833 * bottom of window. */
4834 if (cap->count0 != 0)
4835 {
4836 scroll_cursor_bot(0, TRUE);
4837 curwin->w_cursor.lnum = curwin->w_topline;
4838 }
4839 else if (curwin->w_topline == 1)
4840 curwin->w_cursor.lnum = 1;
4841 else
4842 curwin->w_cursor.lnum = curwin->w_topline - 1;
4843 /* FALLTHROUGH */
4844 case '-':
4845 beginline(BL_WHITE | BL_FIX);
4846 /* FALLTHROUGH */
4847
4848 case 'b': scroll_cursor_bot(0, TRUE);
4849 redraw_later(VALID);
4850 break;
4851
4852 /* "zH" - scroll screen right half-page */
4853 case 'H':
4854 cap->count1 *= W_WIDTH(curwin) / 2;
4855 /* FALLTHROUGH */
4856
4857 /* "zh" - scroll screen to the right */
4858 case 'h':
4859 case K_LEFT:
4860 if (!curwin->w_p_wrap)
4861 {
4862 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4863 curwin->w_leftcol = 0;
4864 else
4865 curwin->w_leftcol -= (colnr_T)cap->count1;
4866 leftcol_changed();
4867 }
4868 break;
4869
4870 /* "zL" - scroll screen left half-page */
4871 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4872 /* FALLTHROUGH */
4873
4874 /* "zl" - scroll screen to the left */
4875 case 'l':
4876 case K_RIGHT:
4877 if (!curwin->w_p_wrap)
4878 {
4879 /* scroll the window left */
4880 curwin->w_leftcol += (colnr_T)cap->count1;
4881 leftcol_changed();
4882 }
4883 break;
4884
4885 /* "zs" - scroll screen, cursor at the start */
4886 case 's': if (!curwin->w_p_wrap)
4887 {
4888#ifdef FEAT_FOLDING
4889 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4890 col = 0; /* like the cursor is in col 0 */
4891 else
4892#endif
4893 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4894 if ((long)col > p_siso)
4895 col -= p_siso;
4896 else
4897 col = 0;
4898 if (curwin->w_leftcol != col)
4899 {
4900 curwin->w_leftcol = col;
4901 redraw_later(NOT_VALID);
4902 }
4903 }
4904 break;
4905
4906 /* "ze" - scroll screen, cursor at the end */
4907 case 'e': if (!curwin->w_p_wrap)
4908 {
4909#ifdef FEAT_FOLDING
4910 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4911 col = 0; /* like the cursor is in col 0 */
4912 else
4913#endif
4914 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4915 n = W_WIDTH(curwin) - curwin_col_off();
4916 if ((long)col + p_siso < n)
4917 col = 0;
4918 else
4919 col = col + p_siso - n + 1;
4920 if (curwin->w_leftcol != col)
4921 {
4922 curwin->w_leftcol = col;
4923 redraw_later(NOT_VALID);
4924 }
4925 }
4926 break;
4927
4928#ifdef FEAT_FOLDING
4929 /* "zF": create fold command */
4930 /* "zf": create fold operator */
4931 case 'F':
4932 case 'f': if (foldManualAllowed(TRUE))
4933 {
4934 cap->nchar = 'f';
4935 nv_operator(cap);
4936 curwin->w_p_fen = TRUE;
4937
4938 /* "zF" is like "zfzf" */
4939 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4940 {
4941 nv_operator(cap);
4942 finish_op = TRUE;
4943 }
4944 }
4945 else
4946 clearopbeep(cap->oap);
4947 break;
4948
4949 /* "zd": delete fold at cursor */
4950 /* "zD": delete fold at cursor recursively */
4951 case 'd':
4952 case 'D': if (foldManualAllowed(FALSE))
4953 {
4954 if (VIsual_active)
4955 nv_operator(cap);
4956 else
4957 deleteFold(curwin->w_cursor.lnum,
4958 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4959 }
4960 break;
4961
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004962 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 case 'E': if (foldmethodIsManual(curwin))
4964 {
4965 clearFolding(curwin);
4966 changed_window_setting();
4967 }
4968 else if (foldmethodIsMarker(curwin))
4969 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4970 TRUE, FALSE);
4971 else
4972 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4973 break;
4974
4975 /* "zn": fold none: reset 'foldenable' */
4976 case 'n': curwin->w_p_fen = FALSE;
4977 break;
4978
4979 /* "zN": fold Normal: set 'foldenable' */
4980 case 'N': curwin->w_p_fen = TRUE;
4981 break;
4982
4983 /* "zi": invert folding: toggle 'foldenable' */
4984 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4985 break;
4986
4987 /* "za": open closed fold or close open fold at cursor */
4988 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4989 openFold(curwin->w_cursor.lnum, cap->count1);
4990 else
4991 {
4992 closeFold(curwin->w_cursor.lnum, cap->count1);
4993 curwin->w_p_fen = TRUE;
4994 }
4995 break;
4996
4997 /* "zA": open fold at cursor recursively */
4998 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4999 openFoldRecurse(curwin->w_cursor.lnum);
5000 else
5001 {
5002 closeFoldRecurse(curwin->w_cursor.lnum);
5003 curwin->w_p_fen = TRUE;
5004 }
5005 break;
5006
5007 /* "zo": open fold at cursor or Visual area */
5008 case 'o': if (VIsual_active)
5009 nv_operator(cap);
5010 else
5011 openFold(curwin->w_cursor.lnum, cap->count1);
5012 break;
5013
5014 /* "zO": open fold recursively */
5015 case 'O': if (VIsual_active)
5016 nv_operator(cap);
5017 else
5018 openFoldRecurse(curwin->w_cursor.lnum);
5019 break;
5020
5021 /* "zc": close fold at cursor or Visual area */
5022 case 'c': if (VIsual_active)
5023 nv_operator(cap);
5024 else
5025 closeFold(curwin->w_cursor.lnum, cap->count1);
5026 curwin->w_p_fen = TRUE;
5027 break;
5028
5029 /* "zC": close fold recursively */
5030 case 'C': if (VIsual_active)
5031 nv_operator(cap);
5032 else
5033 closeFoldRecurse(curwin->w_cursor.lnum);
5034 curwin->w_p_fen = TRUE;
5035 break;
5036
5037 /* "zv": open folds at the cursor */
5038 case 'v': foldOpenCursor();
5039 break;
5040
5041 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5042 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005043 curwin->w_foldinvalid = TRUE; /* recompute folds */
5044 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 foldOpenCursor();
5046 break;
5047
5048 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5049 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005050 curwin->w_foldinvalid = TRUE; /* recompute folds */
5051 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 break;
5053
5054 /* "zm": fold more */
5055 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005056 {
5057 curwin->w_p_fdl -= cap->count1;
5058 if (curwin->w_p_fdl < 0)
5059 curwin->w_p_fdl = 0;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 old_fdl = -1; /* force an update */
5062 curwin->w_p_fen = TRUE;
5063 break;
5064
5065 /* "zM": close all folds */
5066 case 'M': curwin->w_p_fdl = 0;
5067 old_fdl = -1; /* force an update */
5068 curwin->w_p_fen = TRUE;
5069 break;
5070
5071 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005072 case 'r': curwin->w_p_fdl += cap->count1;
5073 {
5074 int d = getDeepestNesting();
5075
5076 if (curwin->w_p_fdl >= d)
5077 curwin->w_p_fdl = d;
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /* "zR": open all folds */
5082 case 'R': curwin->w_p_fdl = getDeepestNesting();
5083 old_fdl = -1; /* force an update */
5084 break;
5085
5086 case 'j': /* "zj" move to next fold downwards */
5087 case 'k': /* "zk" move to next fold upwards */
5088 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5089 cap->count1) == FAIL)
5090 clearopbeep(cap->oap);
5091 break;
5092
5093#endif /* FEAT_FOLDING */
5094
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005095#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005096 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5097 ++no_mapping;
5098 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005099 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005100 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005101 --no_mapping;
5102 --allow_keys;
5103#ifdef FEAT_CMDL_INFO
5104 (void)add_to_showcmd(nchar);
5105#endif
5106 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5107 {
5108 clearopbeep(cap->oap);
5109 break;
5110 }
5111 undo = TRUE;
5112 /*FALLTHROUGH*/
5113
Bram Moolenaarb765d632005-06-07 21:00:02 +00005114 case 'g': /* "zg": add good word to word list */
5115 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005116 case 'G': /* "zG": add good word to temp word list */
5117 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005118 {
5119 char_u *ptr = NULL;
5120 int len;
5121
5122 if (checkclearop(cap->oap))
5123 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005124 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5125 == FAIL)
5126 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005127 if (ptr == NULL)
5128 {
5129 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005130
Bram Moolenaar134bf072013-09-25 18:54:24 +02005131 /* Find bad word under the cursor. When 'spell' is
5132 * off this fails and find_ident_under_cursor() is
5133 * used below. */
5134 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005135 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005136 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005137 if (len != 0 && curwin->w_cursor.col <= pos.col)
5138 ptr = ml_get_pos(&curwin->w_cursor);
5139 curwin->w_cursor = pos;
5140 }
5141
Bram Moolenaarb765d632005-06-07 21:00:02 +00005142 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5143 FIND_IDENT)) == 0)
5144 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005145 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005146 (nchar == 'G' || nchar == 'W')
5147 ? 0 : (int)cap->count1,
5148 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005149 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005150 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005151
Bram Moolenaar43abc522005-12-10 20:15:02 +00005152 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005153 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005154 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005156#endif
5157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 default: clearopbeep(cap->oap);
5159 }
5160
5161#ifdef FEAT_FOLDING
5162 /* Redraw when 'foldenable' changed */
5163 if (old_fen != curwin->w_p_fen)
5164 {
5165# ifdef FEAT_DIFF
5166 win_T *wp;
5167
5168 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5169 {
5170 /* Adjust 'foldenable' in diff-synced windows. */
5171 FOR_ALL_WINDOWS(wp)
5172 {
5173 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5174 {
5175 wp->w_p_fen = curwin->w_p_fen;
5176 changed_window_setting_win(wp);
5177 }
5178 }
5179 }
5180# endif
5181 changed_window_setting();
5182 }
5183
5184 /* Redraw when 'foldlevel' changed. */
5185 if (old_fdl != curwin->w_p_fdl)
5186 newFoldLevel();
5187#endif
5188}
5189
5190#ifdef FEAT_GUI
5191/*
5192 * Vertical scrollbar movement.
5193 */
5194 static void
5195nv_ver_scrollbar(cap)
5196 cmdarg_T *cap;
5197{
5198 if (cap->oap->op_type != OP_NOP)
5199 clearopbeep(cap->oap);
5200
5201 /* Even if an operator was pending, we still want to scroll */
5202 gui_do_scroll();
5203}
5204
5205/*
5206 * Horizontal scrollbar movement.
5207 */
5208 static void
5209nv_hor_scrollbar(cap)
5210 cmdarg_T *cap;
5211{
5212 if (cap->oap->op_type != OP_NOP)
5213 clearopbeep(cap->oap);
5214
5215 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005216 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217}
5218#endif
5219
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005220#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005221/*
5222 * Click in GUI tab.
5223 */
5224 static void
5225nv_tabline(cap)
5226 cmdarg_T *cap;
5227{
5228 if (cap->oap->op_type != OP_NOP)
5229 clearopbeep(cap->oap);
5230
5231 /* Even if an operator was pending, we still want to jump tabs. */
5232 goto_tabpage(current_tab);
5233}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005234
5235/*
5236 * Selected item in tab line menu.
5237 */
5238 static void
5239nv_tabmenu(cap)
5240 cmdarg_T *cap;
5241{
5242 if (cap->oap->op_type != OP_NOP)
5243 clearopbeep(cap->oap);
5244
5245 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005246 handle_tabmenu();
5247}
5248
5249/*
5250 * Handle selecting an item of the GUI tab line menu.
5251 * Used in Normal and Insert mode.
5252 */
5253 void
5254handle_tabmenu()
5255{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005256 switch (current_tabmenu)
5257 {
5258 case TABLINE_MENU_CLOSE:
5259 if (current_tab == 0)
5260 do_cmdline_cmd((char_u *)"tabclose");
5261 else
5262 {
5263 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5264 current_tab);
5265 do_cmdline_cmd(IObuff);
5266 }
5267 break;
5268
5269 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005270 if (current_tab == 0)
5271 do_cmdline_cmd((char_u *)"$tabnew");
5272 else
5273 {
5274 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5275 current_tab - 1);
5276 do_cmdline_cmd(IObuff);
5277 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005278 break;
5279
5280 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005281 if (current_tab == 0)
5282 do_cmdline_cmd((char_u *)"browse $tabnew");
5283 else
5284 {
5285 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5286 current_tab - 1);
5287 do_cmdline_cmd(IObuff);
5288 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005289 break;
5290 }
5291}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005292#endif
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294/*
5295 * "Q" command.
5296 */
5297 static void
5298nv_exmode(cap)
5299 cmdarg_T *cap;
5300{
5301 /*
5302 * Ignore 'Q' in Visual mode, just give a beep.
5303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (VIsual_active)
5305 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005306 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 do_exmode(FALSE);
5308}
5309
5310/*
5311 * Handle a ":" command.
5312 */
5313 static void
5314nv_colon(cap)
5315 cmdarg_T *cap;
5316{
5317 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005318 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (VIsual_active)
5321 nv_operator(cap);
5322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
5324 if (cap->oap->op_type != OP_NOP)
5325 {
5326 /* Using ":" as a movement is characterwise exclusive. */
5327 cap->oap->motion_type = MCHAR;
5328 cap->oap->inclusive = FALSE;
5329 }
5330 else if (cap->count0)
5331 {
5332 /* translate "count:" into ":.,.+(count - 1)" */
5333 stuffcharReadbuff('.');
5334 if (cap->count0 > 1)
5335 {
5336 stuffReadbuff((char_u *)",.+");
5337 stuffnumReadbuff((long)cap->count0 - 1L);
5338 }
5339 }
5340
5341 /* When typing, don't type below an old message */
5342 if (KeyTyped)
5343 compute_cmdrow();
5344
5345 old_p_im = p_im;
5346
5347 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005348 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5350
5351 /* If 'insertmode' changed, enter or exit Insert mode */
5352 if (p_im != old_p_im)
5353 {
5354 if (p_im)
5355 restart_edit = 'i';
5356 else
5357 restart_edit = 0;
5358 }
5359
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005360 if (cmd_result == FAIL)
5361 /* The Ex command failed, do not execute the operator. */
5362 clearop(cap->oap);
5363 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5365 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005366 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5367 || did_emsg
5368 ))
5369 /* The start of the operator has become invalid by the Ex command.
5370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 clearopbeep(cap->oap);
5372 }
5373}
5374
5375/*
5376 * Handle CTRL-G command.
5377 */
5378 static void
5379nv_ctrlg(cap)
5380 cmdarg_T *cap;
5381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 if (VIsual_active) /* toggle Selection/Visual mode */
5383 {
5384 VIsual_select = !VIsual_select;
5385 showmode();
5386 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005387 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 /* print full name if count given or :cd used */
5389 fileinfo((int)cap->count0, FALSE, TRUE);
5390}
5391
5392/*
5393 * Handle CTRL-H <Backspace> command.
5394 */
5395 static void
5396nv_ctrlh(cap)
5397 cmdarg_T *cap;
5398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (VIsual_active && VIsual_select)
5400 {
5401 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5402 v_visop(cap);
5403 }
5404 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 nv_left(cap);
5406}
5407
5408/*
5409 * CTRL-L: clear screen and redraw.
5410 */
5411 static void
5412nv_clear(cap)
5413 cmdarg_T *cap;
5414{
5415 if (!checkclearop(cap->oap))
5416 {
5417#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5418 /*
5419 * Right now, the BeBox doesn't seem to have an easy way to detect
5420 * window resizing, so we cheat and make the user detect it
5421 * manually with CTRL-L instead
5422 */
5423 ui_get_shellsize();
5424#endif
5425#ifdef FEAT_SYN_HL
5426 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005427 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#endif
5429 redraw_later(CLEAR);
5430 }
5431}
5432
5433/*
5434 * CTRL-O: In Select mode: switch to Visual mode for one command.
5435 * Otherwise: Go to older pcmark.
5436 */
5437 static void
5438nv_ctrlo(cap)
5439 cmdarg_T *cap;
5440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 if (VIsual_active && VIsual_select)
5442 {
5443 VIsual_select = FALSE;
5444 showmode();
5445 restart_VIsual_select = 2; /* restart Select mode later */
5446 }
5447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
5449 cap->count1 = -cap->count1;
5450 nv_pcmark(cap);
5451 }
5452}
5453
5454/*
5455 * CTRL-^ command, short for ":e #"
5456 */
5457 static void
5458nv_hat(cap)
5459 cmdarg_T *cap;
5460{
5461 if (!checkclearopq(cap->oap))
5462 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5463 GETF_SETMARK|GETF_ALT, FALSE);
5464}
5465
5466/*
5467 * "Z" commands.
5468 */
5469 static void
5470nv_Zet(cap)
5471 cmdarg_T *cap;
5472{
5473 if (!checkclearopq(cap->oap))
5474 {
5475 switch (cap->nchar)
5476 {
5477 /* "ZZ": equivalent to ":x". */
5478 case 'Z': do_cmdline_cmd((char_u *)"x");
5479 break;
5480
5481 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5482 case 'Q': do_cmdline_cmd((char_u *)"q!");
5483 break;
5484
5485 default: clearopbeep(cap->oap);
5486 }
5487 }
5488}
5489
5490#if defined(FEAT_WINDOWS) || defined(PROTO)
5491/*
5492 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5493 */
5494 void
5495do_nv_ident(c1, c2)
5496 int c1;
5497 int c2;
5498{
5499 oparg_T oa;
5500 cmdarg_T ca;
5501
5502 clear_oparg(&oa);
5503 vim_memset(&ca, 0, sizeof(ca));
5504 ca.oap = &oa;
5505 ca.cmdchar = c1;
5506 ca.nchar = c2;
5507 nv_ident(&ca);
5508}
5509#endif
5510
5511/*
5512 * Handle the commands that use the word under the cursor.
5513 * [g] CTRL-] :ta to current identifier
5514 * [g] 'K' run program for current identifier
5515 * [g] '*' / to current identifier or string
5516 * [g] '#' ? to current identifier or string
5517 * g ']' :tselect for current identifier
5518 */
5519 static void
5520nv_ident(cap)
5521 cmdarg_T *cap;
5522{
5523 char_u *ptr = NULL;
5524 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005525 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 char_u *p;
5527 char_u *kp; /* value of 'keywordprg' */
5528 int kp_help; /* 'keywordprg' is ":help" */
5529 int n = 0; /* init for GCC */
5530 int cmdchar;
5531 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005532 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 char_u *aux_ptr;
5534 int isman;
5535 int isman_s;
5536
5537 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5538 {
5539 cmdchar = cap->nchar;
5540 g_cmd = TRUE;
5541 }
5542 else
5543 {
5544 cmdchar = cap->cmdchar;
5545 g_cmd = FALSE;
5546 }
5547
5548 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5549 cmdchar = '#';
5550
5551 /*
5552 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5553 */
5554 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (checkclearopq(cap->oap))
5559 return;
5560 }
5561
5562 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5563 (cmdchar == '*' || cmdchar == '#')
5564 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5565 {
5566 clearop(cap->oap);
5567 return;
5568 }
5569
5570 /* Allocate buffer to put the command in. Inserting backslashes can
5571 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5572 * and some numbers. */
5573 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5574 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5575 || STRCMP(kp, ":help") == 0);
5576 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5577 if (buf == NULL)
5578 return;
5579 buf[0] = NUL;
5580
5581 switch (cmdchar)
5582 {
5583 case '*':
5584 case '#':
5585 /*
5586 * Put cursor at start of word, makes search skip the word
5587 * under the cursor.
5588 * Call setpcmark() first, so "*``" puts the cursor back where
5589 * it was.
5590 */
5591 setpcmark();
5592 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5593
5594 if (!g_cmd && vim_iswordp(ptr))
5595 STRCPY(buf, "\\<");
5596 no_smartcase = TRUE; /* don't use 'smartcase' now */
5597 break;
5598
5599 case 'K':
5600 if (kp_help)
5601 STRCPY(buf, "he! ");
5602 else
5603 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005604 /* An external command will probably use an argument starting
5605 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005606 while (*ptr == '-' && n > 0)
5607 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005608 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005609 --n;
5610 }
5611 if (n == 0)
5612 {
5613 EMSG(_(e_noident)); /* found dashes only */
5614 vim_free(buf);
5615 return;
5616 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 /* When a count is given, turn it into a range. Is this
5619 * really what we want? */
5620 isman = (STRCMP(kp, "man") == 0);
5621 isman_s = (STRCMP(kp, "man -s") == 0);
5622 if (cap->count0 != 0 && !(isman || isman_s))
5623 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5624
5625 STRCAT(buf, "! ");
5626 if (cap->count0 == 0 && isman_s)
5627 STRCAT(buf, "man");
5628 else
5629 STRCAT(buf, kp);
5630 STRCAT(buf, " ");
5631 if (cap->count0 != 0 && (isman || isman_s))
5632 {
5633 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5634 STRCAT(buf, " ");
5635 }
5636 }
5637 break;
5638
5639 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005640 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641#ifdef FEAT_CSCOPE
5642 if (p_cst)
5643 STRCPY(buf, "cstag ");
5644 else
5645#endif
5646 STRCPY(buf, "ts ");
5647 break;
5648
5649 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005650 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 if (curbuf->b_help)
5652 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005654 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005655 if (g_cmd)
5656 STRCPY(buf, "tj ");
5657 else
5658 sprintf((char *)buf, "%ldta ", cap->count0);
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661
5662 /*
5663 * Now grab the chars in the identifier
5664 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005665 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005667 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005668 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005669 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005670 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 vim_free(buf);
5674 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005676 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5677 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005678 {
5679 vim_free(buf);
5680 vim_free(p);
5681 return;
5682 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005683 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005684 STRCAT(buf, p);
5685 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005687 else
5688 {
5689 if (cmdchar == '*')
5690 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5691 else if (cmdchar == '#')
5692 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005693 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005694 {
5695 if (curbuf->b_help)
5696 /* ":help" handles unescaped argument */
5697 aux_ptr = (char_u *)"";
5698 else
5699 aux_ptr = (char_u *)"\\|\"\n[";
5700 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005701 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005702 aux_ptr = (char_u *)"\\|\"\n*?[";
5703
5704 p = buf + STRLEN(buf);
5705 while (n-- > 0)
5706 {
5707 /* put a backslash before \ and some others */
5708 if (vim_strchr(aux_ptr, *ptr) != NULL)
5709 *p++ = '\\';
5710#ifdef FEAT_MBYTE
5711 /* When current byte is a part of multibyte character, copy all
5712 * bytes of that character. */
5713 if (has_mbyte)
5714 {
5715 int i;
5716 int len = (*mb_ptr2len)(ptr) - 1;
5717
5718 for (i = 0; i < len && n >= 1; ++i, --n)
5719 *p++ = *ptr++;
5720 }
5721#endif
5722 *p++ = *ptr++;
5723 }
5724 *p = NUL;
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727 /*
5728 * Execute the command.
5729 */
5730 if (cmdchar == '*' || cmdchar == '#')
5731 {
5732 if (!g_cmd && (
5733#ifdef FEAT_MBYTE
5734 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5735#endif
5736 vim_iswordc(ptr[-1])))
5737 STRCAT(buf, "\\>");
5738#ifdef FEAT_CMDHIST
5739 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005740 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5742#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005743 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745 else
5746 do_cmdline_cmd(buf);
5747
5748 vim_free(buf);
5749}
5750
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751/*
5752 * Get visually selected text, within one line only.
5753 * Returns FAIL if more than one line selected.
5754 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005755 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756get_visual_text(cap, pp, lenp)
5757 cmdarg_T *cap;
5758 char_u **pp; /* return: start of selected text */
5759 int *lenp; /* return: length of selected text */
5760{
5761 if (VIsual_mode != 'V')
5762 unadjust_for_sel();
5763 if (VIsual.lnum != curwin->w_cursor.lnum)
5764 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005765 if (cap != NULL)
5766 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 return FAIL;
5768 }
5769 if (VIsual_mode == 'V')
5770 {
5771 *pp = ml_get_curline();
5772 *lenp = (int)STRLEN(*pp);
5773 }
5774 else
5775 {
5776 if (lt(curwin->w_cursor, VIsual))
5777 {
5778 *pp = ml_get_pos(&curwin->w_cursor);
5779 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5780 }
5781 else
5782 {
5783 *pp = ml_get_pos(&VIsual);
5784 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5785 }
5786#ifdef FEAT_MBYTE
5787 if (has_mbyte)
5788 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005789 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#endif
5791 }
5792 reset_VIsual_and_resel();
5793 return OK;
5794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796/*
5797 * CTRL-T: backwards in tag stack
5798 */
5799 static void
5800nv_tagpop(cap)
5801 cmdarg_T *cap;
5802{
5803 if (!checkclearopq(cap->oap))
5804 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5805}
5806
5807/*
5808 * Handle scrolling command 'H', 'L' and 'M'.
5809 */
5810 static void
5811nv_scroll(cap)
5812 cmdarg_T *cap;
5813{
5814 int used = 0;
5815 long n;
5816#ifdef FEAT_FOLDING
5817 linenr_T lnum;
5818#endif
5819 int half;
5820
5821 cap->oap->motion_type = MLINE;
5822 setpcmark();
5823
5824 if (cap->cmdchar == 'L')
5825 {
5826 validate_botline(); /* make sure curwin->w_botline is valid */
5827 curwin->w_cursor.lnum = curwin->w_botline - 1;
5828 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5829 curwin->w_cursor.lnum = 1;
5830 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005831 {
5832#ifdef FEAT_FOLDING
5833 if (hasAnyFolding(curwin))
5834 {
5835 /* Count a fold for one screen line. */
5836 for (n = cap->count1 - 1; n > 0
5837 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5838 {
5839 (void)hasFolding(curwin->w_cursor.lnum,
5840 &curwin->w_cursor.lnum, NULL);
5841 --curwin->w_cursor.lnum;
5842 }
5843 }
5844 else
5845#endif
5846 curwin->w_cursor.lnum -= cap->count1 - 1;
5847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
5849 else
5850 {
5851 if (cap->cmdchar == 'M')
5852 {
5853#ifdef FEAT_DIFF
5854 /* Don't count filler lines above the window. */
5855 used -= diff_check_fill(curwin, curwin->w_topline)
5856 - curwin->w_topfill;
5857#endif
5858 validate_botline(); /* make sure w_empty_rows is valid */
5859 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5860 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5861 {
5862#ifdef FEAT_DIFF
5863 /* Count half he number of filler lines to be "below this
5864 * line" and half to be "above the next line". */
5865 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5866 + n) / 2 >= half)
5867 {
5868 --n;
5869 break;
5870 }
5871#endif
5872 used += plines(curwin->w_topline + n);
5873 if (used >= half)
5874 break;
5875#ifdef FEAT_FOLDING
5876 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5877 n = lnum - curwin->w_topline;
5878#endif
5879 }
5880 if (n > 0 && used > curwin->w_height)
5881 --n;
5882 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005883 else /* (cap->cmdchar == 'H') */
5884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005886#ifdef FEAT_FOLDING
5887 if (hasAnyFolding(curwin))
5888 {
5889 /* Count a fold for one screen line. */
5890 lnum = curwin->w_topline;
5891 while (n-- > 0 && lnum < curwin->w_botline - 1)
5892 {
5893 hasFolding(lnum, NULL, &lnum);
5894 ++lnum;
5895 }
5896 n = lnum - curwin->w_topline;
5897 }
5898#endif
5899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 curwin->w_cursor.lnum = curwin->w_topline + n;
5901 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5902 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5903 }
5904
5905 cursor_correct(); /* correct for 'so' */
5906 beginline(BL_SOL | BL_FIX);
5907}
5908
5909/*
5910 * Cursor right commands.
5911 */
5912 static void
5913nv_right(cap)
5914 cmdarg_T *cap;
5915{
5916 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005917 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005919 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5920 {
5921 /* <C-Right> and <S-Right> move a word or WORD right */
5922 if (mod_mask & MOD_MASK_CTRL)
5923 cap->arg = TRUE;
5924 nv_wordcmd(cap);
5925 return;
5926 }
5927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 cap->oap->motion_type = MCHAR;
5929 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005930 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005932#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005934 * In virtual edit mode, there's no such thing as "past_line", as lines
5935 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 */
5937 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005938 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939#endif
5940
5941 for (n = cap->count1; n > 0; --n)
5942 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005943 if ((!past_line && oneright() == FAIL)
5944 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005945 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 {
5947 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005948 * <Space> wraps to next line if 'whichwrap' has 's'.
5949 * 'l' wraps to next line if 'whichwrap' has 'l'.
5950 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 */
5952 if ( ((cap->cmdchar == ' '
5953 && vim_strchr(p_ww, 's') != NULL)
5954 || (cap->cmdchar == 'l'
5955 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005956 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 && vim_strchr(p_ww, '>') != NULL))
5958 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5959 {
5960 /* When deleting we also count the NL as a character.
5961 * Set cap->oap->inclusive when last char in the line is
5962 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005963 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 && !cap->oap->inclusive
5965 && !lineempty(curwin->w_cursor.lnum))
5966 cap->oap->inclusive = TRUE;
5967 else
5968 {
5969 ++curwin->w_cursor.lnum;
5970 curwin->w_cursor.col = 0;
5971#ifdef FEAT_VIRTUALEDIT
5972 curwin->w_cursor.coladd = 0;
5973#endif
5974 curwin->w_set_curswant = TRUE;
5975 cap->oap->inclusive = FALSE;
5976 }
5977 continue;
5978 }
5979 if (cap->oap->op_type == OP_NOP)
5980 {
5981 /* Only beep and flush if not moved at all */
5982 if (n == cap->count1)
5983 beep_flush();
5984 }
5985 else
5986 {
5987 if (!lineempty(curwin->w_cursor.lnum))
5988 cap->oap->inclusive = TRUE;
5989 }
5990 break;
5991 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005992 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005995#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 if (virtual_active())
5997 oneright();
5998 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006001#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (has_mbyte)
6003 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006004 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 ++curwin->w_cursor.col;
6008 }
6009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
6011#ifdef FEAT_FOLDING
6012 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6013 && cap->oap->op_type == OP_NOP)
6014 foldOpenCursor();
6015#endif
6016}
6017
6018/*
6019 * Cursor left commands.
6020 *
6021 * Returns TRUE when operator end should not be adjusted.
6022 */
6023 static void
6024nv_left(cap)
6025 cmdarg_T *cap;
6026{
6027 long n;
6028
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006029 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6030 {
6031 /* <C-Left> and <S-Left> move a word or WORD left */
6032 if (mod_mask & MOD_MASK_CTRL)
6033 cap->arg = 1;
6034 nv_bck_word(cap);
6035 return;
6036 }
6037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 cap->oap->motion_type = MCHAR;
6039 cap->oap->inclusive = FALSE;
6040 for (n = cap->count1; n > 0; --n)
6041 {
6042 if (oneleft() == FAIL)
6043 {
6044 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6045 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6046 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6047 */
6048 if ( (((cap->cmdchar == K_BS
6049 || cap->cmdchar == Ctrl_H)
6050 && vim_strchr(p_ww, 'b') != NULL)
6051 || (cap->cmdchar == 'h'
6052 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006053 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 && vim_strchr(p_ww, '<') != NULL))
6055 && curwin->w_cursor.lnum > 1)
6056 {
6057 --(curwin->w_cursor.lnum);
6058 coladvance((colnr_T)MAXCOL);
6059 curwin->w_set_curswant = TRUE;
6060
6061 /* When the NL before the first char has to be deleted we
6062 * put the cursor on the NUL after the previous line.
6063 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006064 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if ( (cap->oap->op_type == OP_DELETE
6066 || cap->oap->op_type == OP_CHANGE)
6067 && !lineempty(curwin->w_cursor.lnum))
6068 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006069 char_u *cp = ml_get_cursor();
6070
6071 if (*cp != NUL)
6072 {
6073#ifdef FEAT_MBYTE
6074 if (has_mbyte)
6075 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6076 else
6077#endif
6078 ++curwin->w_cursor.col;
6079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 cap->retval |= CA_NO_ADJ_OP_END;
6081 }
6082 continue;
6083 }
6084 /* Only beep and flush if not moved at all */
6085 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6086 beep_flush();
6087 break;
6088 }
6089 }
6090#ifdef FEAT_FOLDING
6091 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6092 && cap->oap->op_type == OP_NOP)
6093 foldOpenCursor();
6094#endif
6095}
6096
6097/*
6098 * Cursor up commands.
6099 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6100 */
6101 static void
6102nv_up(cap)
6103 cmdarg_T *cap;
6104{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006105 if (mod_mask & MOD_MASK_SHIFT)
6106 {
6107 /* <S-Up> is page up */
6108 cap->arg = BACKWARD;
6109 nv_page(cap);
6110 }
6111 else
6112 {
6113 cap->oap->motion_type = MLINE;
6114 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6115 clearopbeep(cap->oap);
6116 else if (cap->arg)
6117 beginline(BL_WHITE | BL_FIX);
6118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119}
6120
6121/*
6122 * Cursor down commands.
6123 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6124 */
6125 static void
6126nv_down(cap)
6127 cmdarg_T *cap;
6128{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006129 if (mod_mask & MOD_MASK_SHIFT)
6130 {
6131 /* <S-Down> is page down */
6132 cap->arg = FORWARD;
6133 nv_page(cap);
6134 }
6135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6137 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006138 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006139 if (curwin->w_llist_ref == NULL)
6140 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6141 else
6142 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 else
6144#endif
6145 {
6146#ifdef FEAT_CMDWIN
6147 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006148 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 cmdwin_result = CAR;
6150 else
6151#endif
6152 {
6153 cap->oap->motion_type = MLINE;
6154 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6155 clearopbeep(cap->oap);
6156 else if (cap->arg)
6157 beginline(BL_WHITE | BL_FIX);
6158 }
6159 }
6160}
6161
6162#ifdef FEAT_SEARCHPATH
6163/*
6164 * Grab the file name under the cursor and edit it.
6165 */
6166 static void
6167nv_gotofile(cap)
6168 cmdarg_T *cap;
6169{
6170 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006171 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006173 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006176 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 return;
6178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006179#ifdef FEAT_AUTOCMD
6180 if (curbuf_locked())
6181 {
6182 clearop(cap->oap);
6183 return;
6184 }
6185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006187 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
6189 if (ptr != NULL)
6190 {
6191 /* do autowrite if necessary */
6192 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6193 autowrite(curbuf, FALSE);
6194 setpcmark();
6195 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006196 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006197 if (cap->nchar == 'F' && lnum >= 0)
6198 {
6199 curwin->w_cursor.lnum = lnum;
6200 check_cursor_lnum();
6201 beginline(BL_SOL | BL_FIX);
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 vim_free(ptr);
6204 }
6205 else
6206 clearop(cap->oap);
6207}
6208#endif
6209
6210/*
6211 * <End> command: to end of current line or last line.
6212 */
6213 static void
6214nv_end(cap)
6215 cmdarg_T *cap;
6216{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006217 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006219 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 nv_goto(cap);
6221 cap->count1 = 1; /* to end of current line */
6222 }
6223 nv_dollar(cap);
6224}
6225
6226/*
6227 * Handle the "$" command.
6228 */
6229 static void
6230nv_dollar(cap)
6231 cmdarg_T *cap;
6232{
6233 cap->oap->motion_type = MCHAR;
6234 cap->oap->inclusive = TRUE;
6235#ifdef FEAT_VIRTUALEDIT
6236 /* In virtual mode when off the edge of a line and an operator
6237 * is pending (whew!) keep the cursor where it is.
6238 * Otherwise, send it to the end of the line. */
6239 if (!virtual_active() || gchar_cursor() != NUL
6240 || cap->oap->op_type == OP_NOP)
6241#endif
6242 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6243 if (cursor_down((long)(cap->count1 - 1),
6244 cap->oap->op_type == OP_NOP) == FAIL)
6245 clearopbeep(cap->oap);
6246#ifdef FEAT_FOLDING
6247 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6248 foldOpenCursor();
6249#endif
6250}
6251
6252/*
6253 * Implementation of '?' and '/' commands.
6254 * If cap->arg is TRUE don't set PC mark.
6255 */
6256 static void
6257nv_search(cap)
6258 cmdarg_T *cap;
6259{
6260 oparg_T *oap = cap->oap;
6261
6262 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6263 {
6264 /* Translate "g??" to "g?g?" */
6265 cap->cmdchar = 'g';
6266 cap->nchar = '?';
6267 nv_operator(cap);
6268 return;
6269 }
6270
6271 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6272
6273 if (cap->searchbuf == NULL)
6274 {
6275 clearop(oap);
6276 return;
6277 }
6278
Bram Moolenaar46539112015-02-17 15:43:57 +01006279 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 (cap->arg ? 0 : SEARCH_MARK));
6281}
6282
6283/*
6284 * Handle "N" and "n" commands.
6285 * cap->arg is SEARCH_REV for "N", 0 for "n".
6286 */
6287 static void
6288nv_next(cap)
6289 cmdarg_T *cap;
6290{
Bram Moolenaar46539112015-02-17 15:43:57 +01006291 pos_T old = curwin->w_cursor;
6292 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6293
6294 if (i == 1 && equalpos(old, curwin->w_cursor))
6295 {
6296 /* Avoid getting stuck on the current cursor position, which can
6297 * happen when an offset is given and the cursor is on the last char
6298 * in the buffer: Repeat with count + 1. */
6299 cap->count1 += 1;
6300 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6301 cap->count1 -= 1;
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303}
6304
6305/*
6306 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6307 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006308 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006310 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311normal_search(cap, dir, pat, opt)
6312 cmdarg_T *cap;
6313 int dir;
6314 char_u *pat;
6315 int opt; /* extra flags for do_search() */
6316{
6317 int i;
6318
6319 cap->oap->motion_type = MCHAR;
6320 cap->oap->inclusive = FALSE;
6321 cap->oap->use_reg_one = TRUE;
6322 curwin->w_set_curswant = TRUE;
6323
6324 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006325 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (i == 0)
6327 clearop(cap->oap);
6328 else
6329 {
6330 if (i == 2)
6331 cap->oap->motion_type = MLINE;
6332#ifdef FEAT_VIRTUALEDIT
6333 curwin->w_cursor.coladd = 0;
6334#endif
6335#ifdef FEAT_FOLDING
6336 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6337 foldOpenCursor();
6338#endif
6339 }
6340
6341 /* "/$" will put the cursor after the end of the line, may need to
6342 * correct that here */
6343 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006344 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345}
6346
6347/*
6348 * Character search commands.
6349 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6350 * ',' and FALSE for ';'.
6351 * cap->nchar is NUL for ',' and ';' (repeat the search)
6352 */
6353 static void
6354nv_csearch(cap)
6355 cmdarg_T *cap;
6356{
6357 int t_cmd;
6358
6359 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6360 t_cmd = TRUE;
6361 else
6362 t_cmd = FALSE;
6363
6364 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6366 clearopbeep(cap->oap);
6367 else
6368 {
6369 curwin->w_set_curswant = TRUE;
6370#ifdef FEAT_VIRTUALEDIT
6371 /* Include a Tab for "tx" and for "dfx". */
6372 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6373 && (t_cmd || cap->oap->op_type != OP_NOP))
6374 {
6375 colnr_T scol, ecol;
6376
6377 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6378 curwin->w_cursor.coladd = ecol - scol;
6379 }
6380 else
6381 curwin->w_cursor.coladd = 0;
6382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384#ifdef FEAT_FOLDING
6385 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6386 foldOpenCursor();
6387#endif
6388 }
6389}
6390
6391/*
6392 * "[" and "]" commands.
6393 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6394 */
6395 static void
6396nv_brackets(cap)
6397 cmdarg_T *cap;
6398{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006399 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 pos_T prev_pos;
6401 pos_T *pos = NULL; /* init for GCC */
6402 pos_T old_pos; /* cursor position before command */
6403 int flag;
6404 long n;
6405 int findc;
6406 int c;
6407
6408 cap->oap->motion_type = MCHAR;
6409 cap->oap->inclusive = FALSE;
6410 old_pos = curwin->w_cursor;
6411#ifdef FEAT_VIRTUALEDIT
6412 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6413#endif
6414
6415#ifdef FEAT_SEARCHPATH
6416 /*
6417 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6418 */
6419 if (cap->nchar == 'f')
6420 nv_gotofile(cap);
6421 else
6422#endif
6423
6424#ifdef FEAT_FIND_ID
6425 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006426 * Find the occurrence(s) of the identifier or define under cursor
6427 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 *
6429 * search list jump
6430 * fwd bwd fwd bwd fwd bwd
6431 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6432 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6433 */
6434 if (vim_strchr((char_u *)
6435#ifdef EBCDIC
6436 "iI\005dD\067",
6437#else
6438 "iI\011dD\004",
6439#endif
6440 cap->nchar) != NULL)
6441 {
6442 char_u *ptr;
6443 int len;
6444
6445 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6446 clearop(cap->oap);
6447 else
6448 {
6449 find_pattern_in_path(ptr, 0, len, TRUE,
6450 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6451 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6452 cap->count1,
6453 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6454 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6455 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6456 (linenr_T)MAXLNUM);
6457 curwin->w_set_curswant = TRUE;
6458 }
6459 }
6460 else
6461#endif
6462
6463 /*
6464 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6465 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6466 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6467 * "[m" or "]m" search for prev/next start of (Java) method.
6468 * "[M" or "]M" search for prev/next end of (Java) method.
6469 */
6470 if ( (cap->cmdchar == '['
6471 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6472 || (cap->cmdchar == ']'
6473 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6474 {
6475 if (cap->nchar == '*')
6476 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 prev_pos.lnum = 0;
6478 if (cap->nchar == 'm' || cap->nchar == 'M')
6479 {
6480 if (cap->cmdchar == '[')
6481 findc = '{';
6482 else
6483 findc = '}';
6484 n = 9999;
6485 }
6486 else
6487 {
6488 findc = cap->nchar;
6489 n = cap->count1;
6490 }
6491 for ( ; n > 0; --n)
6492 {
6493 if ((pos = findmatchlimit(cap->oap, findc,
6494 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6495 {
6496 if (new_pos.lnum == 0) /* nothing found */
6497 {
6498 if (cap->nchar != 'm' && cap->nchar != 'M')
6499 clearopbeep(cap->oap);
6500 }
6501 else
6502 pos = &new_pos; /* use last one found */
6503 break;
6504 }
6505 prev_pos = new_pos;
6506 curwin->w_cursor = *pos;
6507 new_pos = *pos;
6508 }
6509 curwin->w_cursor = old_pos;
6510
6511 /*
6512 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6513 * brought us to the match for "[m" and "]M" when inside a method.
6514 * Try finding the '{' or '}' we want to be at.
6515 * Also repeat for the given count.
6516 */
6517 if (cap->nchar == 'm' || cap->nchar == 'M')
6518 {
6519 /* norm is TRUE for "]M" and "[m" */
6520 int norm = ((findc == '{') == (cap->nchar == 'm'));
6521
6522 n = cap->count1;
6523 /* found a match: we were inside a method */
6524 if (prev_pos.lnum != 0)
6525 {
6526 pos = &prev_pos;
6527 curwin->w_cursor = prev_pos;
6528 if (norm)
6529 --n;
6530 }
6531 else
6532 pos = NULL;
6533 while (n > 0)
6534 {
6535 for (;;)
6536 {
6537 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6538 {
6539 /* if not found anything, that's an error */
6540 if (pos == NULL)
6541 clearopbeep(cap->oap);
6542 n = 0;
6543 break;
6544 }
6545 c = gchar_cursor();
6546 if (c == '{' || c == '}')
6547 {
6548 /* Must have found end/start of class: use it.
6549 * Or found the place to be at. */
6550 if ((c == findc && norm) || (n == 1 && !norm))
6551 {
6552 new_pos = curwin->w_cursor;
6553 pos = &new_pos;
6554 n = 0;
6555 }
6556 /* if no match found at all, we started outside of the
6557 * class and we're inside now. Just go on. */
6558 else if (new_pos.lnum == 0)
6559 {
6560 new_pos = curwin->w_cursor;
6561 pos = &new_pos;
6562 }
6563 /* found start/end of other method: go to match */
6564 else if ((pos = findmatchlimit(cap->oap, findc,
6565 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6566 0)) == NULL)
6567 n = 0;
6568 else
6569 curwin->w_cursor = *pos;
6570 break;
6571 }
6572 }
6573 --n;
6574 }
6575 curwin->w_cursor = old_pos;
6576 if (pos == NULL && new_pos.lnum != 0)
6577 clearopbeep(cap->oap);
6578 }
6579 if (pos != NULL)
6580 {
6581 setpcmark();
6582 curwin->w_cursor = *pos;
6583 curwin->w_set_curswant = TRUE;
6584#ifdef FEAT_FOLDING
6585 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6586 && cap->oap->op_type == OP_NOP)
6587 foldOpenCursor();
6588#endif
6589 }
6590 }
6591
6592 /*
6593 * "[[", "[]", "]]" and "][": move to start or end of function
6594 */
6595 else if (cap->nchar == '[' || cap->nchar == ']')
6596 {
6597 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6598 flag = '{';
6599 else
6600 flag = '}'; /* "][" or "[]" */
6601
6602 curwin->w_set_curswant = TRUE;
6603 /*
6604 * Imitate strange Vi behaviour: When using "]]" with an operator
6605 * we also stop at '}'.
6606 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006607 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 (cap->oap->op_type != OP_NOP
6609 && cap->arg == FORWARD && flag == '{')))
6610 clearopbeep(cap->oap);
6611 else
6612 {
6613 if (cap->oap->op_type == OP_NOP)
6614 beginline(BL_WHITE | BL_FIX);
6615#ifdef FEAT_FOLDING
6616 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6617 foldOpenCursor();
6618#endif
6619 }
6620 }
6621
6622 /*
6623 * "[p", "[P", "]P" and "]p": put with indent adjustment
6624 */
6625 else if (cap->nchar == 'p' || cap->nchar == 'P')
6626 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006627 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006629 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6630 ? FORWARD : BACKWARD;
6631 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006632 int was_visual = VIsual_active;
6633 int line_count = curbuf->b_ml.ml_line_count;
6634 pos_T start, end;
6635
6636 if (VIsual_active)
6637 {
6638 start = ltoreq(VIsual, curwin->w_cursor)
6639 ? VIsual : curwin->w_cursor;
6640 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6641 curwin->w_cursor = (dir == BACKWARD ? start : end);
6642 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006643# ifdef FEAT_CLIPBOARD
6644 adjust_clip_reg(&regname);
6645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006647
6648 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006649 if (was_visual)
6650 {
6651 VIsual = start;
6652 curwin->w_cursor = end;
6653 if (dir == BACKWARD)
6654 {
6655 /* adjust lines */
6656 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6657 curwin->w_cursor.lnum +=
6658 curbuf->b_ml.ml_line_count - line_count;
6659 }
6660
6661 VIsual_active = TRUE;
6662 if (VIsual_mode == 'V')
6663 {
6664 /* delete visually selected lines */
6665 cap->cmdchar = 'd';
6666 cap->nchar = NUL;
6667 cap->oap->regname = regname;
6668 nv_operator(cap);
6669 do_pending_operator(cap, 0, FALSE);
6670 }
6671 if (VIsual_active)
6672 {
6673 end_visual_mode();
6674 redraw_later(SOME_VALID);
6675 }
6676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 }
6678 }
6679
6680 /*
6681 * "['", "[`", "]'" and "]`": jump to next mark
6682 */
6683 else if (cap->nchar == '\'' || cap->nchar == '`')
6684 {
6685 pos = &curwin->w_cursor;
6686 for (n = cap->count1; n > 0; --n)
6687 {
6688 prev_pos = *pos;
6689 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6690 cap->nchar == '\'');
6691 if (pos == NULL)
6692 break;
6693 }
6694 if (pos == NULL)
6695 pos = &prev_pos;
6696 nv_cursormark(cap, cap->nchar == '\'', pos);
6697 }
6698
6699#ifdef FEAT_MOUSE
6700 /*
6701 * [ or ] followed by a middle mouse click: put selected text with
6702 * indent adjustment. Any other button just does as usual.
6703 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006704 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 (void)do_mouse(cap->oap, cap->nchar,
6707 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6708 cap->count1, PUT_FIXINDENT);
6709 }
6710#endif /* FEAT_MOUSE */
6711
6712#ifdef FEAT_FOLDING
6713 /*
6714 * "[z" and "]z": move to start or end of open fold.
6715 */
6716 else if (cap->nchar == 'z')
6717 {
6718 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6719 cap->count1) == FAIL)
6720 clearopbeep(cap->oap);
6721 }
6722#endif
6723
6724#ifdef FEAT_DIFF
6725 /*
6726 * "[c" and "]c": move to next or previous diff-change.
6727 */
6728 else if (cap->nchar == 'c')
6729 {
6730 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6731 cap->count1) == FAIL)
6732 clearopbeep(cap->oap);
6733 }
6734#endif
6735
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006736#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006737 /*
6738 * "[s", "[S", "]s" and "]S": move to next spell error.
6739 */
6740 else if (cap->nchar == 's' || cap->nchar == 'S')
6741 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006742 setpcmark();
6743 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006744 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6745 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006746 {
6747 clearopbeep(cap->oap);
6748 break;
6749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750# ifdef FEAT_FOLDING
6751 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6752 foldOpenCursor();
6753# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006754 }
6755#endif
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 /* Not a valid cap->nchar. */
6758 else
6759 clearopbeep(cap->oap);
6760}
6761
6762/*
6763 * Handle Normal mode "%" command.
6764 */
6765 static void
6766nv_percent(cap)
6767 cmdarg_T *cap;
6768{
6769 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006770#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 linenr_T lnum = curwin->w_cursor.lnum;
6772#endif
6773
6774 cap->oap->inclusive = TRUE;
6775 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6776 {
6777 if (cap->count0 > 100)
6778 clearopbeep(cap->oap);
6779 else
6780 {
6781 cap->oap->motion_type = MLINE;
6782 setpcmark();
6783 /* Round up, so CTRL-G will give same value. Watch out for a
6784 * large line count, the line number must not go negative! */
6785 if (curbuf->b_ml.ml_line_count > 1000000)
6786 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6787 / 100L * cap->count0;
6788 else
6789 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6790 cap->count0 + 99L) / 100L;
6791 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6793 beginline(BL_SOL | BL_FIX);
6794 }
6795 }
6796 else /* "%" : go to matching paren */
6797 {
6798 cap->oap->motion_type = MCHAR;
6799 cap->oap->use_reg_one = TRUE;
6800 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6801 clearopbeep(cap->oap);
6802 else
6803 {
6804 setpcmark();
6805 curwin->w_cursor = *pos;
6806 curwin->w_set_curswant = TRUE;
6807#ifdef FEAT_VIRTUALEDIT
6808 curwin->w_cursor.coladd = 0;
6809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812 }
6813#ifdef FEAT_FOLDING
6814 if (cap->oap->op_type == OP_NOP
6815 && lnum != curwin->w_cursor.lnum
6816 && (fdo_flags & FDO_PERCENT)
6817 && KeyTyped)
6818 foldOpenCursor();
6819#endif
6820}
6821
6822/*
6823 * Handle "(" and ")" commands.
6824 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6825 */
6826 static void
6827nv_brace(cap)
6828 cmdarg_T *cap;
6829{
6830 cap->oap->motion_type = MCHAR;
6831 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006832 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6833 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 curwin->w_set_curswant = TRUE;
6835
6836 if (findsent(cap->arg, cap->count1) == FAIL)
6837 clearopbeep(cap->oap);
6838 else
6839 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006840 /* Don't leave the cursor on the NUL past end of line. */
6841 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842#ifdef FEAT_VIRTUALEDIT
6843 curwin->w_cursor.coladd = 0;
6844#endif
6845#ifdef FEAT_FOLDING
6846 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6847 foldOpenCursor();
6848#endif
6849 }
6850}
6851
6852/*
6853 * "m" command: Mark a position.
6854 */
6855 static void
6856nv_mark(cap)
6857 cmdarg_T *cap;
6858{
6859 if (!checkclearop(cap->oap))
6860 {
6861 if (setmark(cap->nchar) == FAIL)
6862 clearopbeep(cap->oap);
6863 }
6864}
6865
6866/*
6867 * "{" and "}" commands.
6868 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6869 */
6870 static void
6871nv_findpar(cap)
6872 cmdarg_T *cap;
6873{
6874 cap->oap->motion_type = MCHAR;
6875 cap->oap->inclusive = FALSE;
6876 cap->oap->use_reg_one = TRUE;
6877 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006878 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 clearopbeep(cap->oap);
6880 else
6881 {
6882#ifdef FEAT_VIRTUALEDIT
6883 curwin->w_cursor.coladd = 0;
6884#endif
6885#ifdef FEAT_FOLDING
6886 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6887 foldOpenCursor();
6888#endif
6889 }
6890}
6891
6892/*
6893 * "u" command: Undo or make lower case.
6894 */
6895 static void
6896nv_undo(cap)
6897 cmdarg_T *cap;
6898{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006899 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6902 cap->cmdchar = 'g';
6903 cap->nchar = 'u';
6904 nv_operator(cap);
6905 }
6906 else
6907 nv_kundo(cap);
6908}
6909
6910/*
6911 * <Undo> command.
6912 */
6913 static void
6914nv_kundo(cap)
6915 cmdarg_T *cap;
6916{
6917 if (!checkclearopq(cap->oap))
6918 {
6919 u_undo((int)cap->count1);
6920 curwin->w_set_curswant = TRUE;
6921 }
6922}
6923
6924/*
6925 * Handle the "r" command.
6926 */
6927 static void
6928nv_replace(cap)
6929 cmdarg_T *cap;
6930{
6931 char_u *ptr;
6932 int had_ctrl_v;
6933 long n;
6934
6935 if (checkclearop(cap->oap))
6936 return;
6937
6938 /* get another character */
6939 if (cap->nchar == Ctrl_V)
6940 {
6941 had_ctrl_v = Ctrl_V;
6942 cap->nchar = get_literal();
6943 /* Don't redo a multibyte character with CTRL-V. */
6944 if (cap->nchar > DEL)
6945 had_ctrl_v = NUL;
6946 }
6947 else
6948 had_ctrl_v = NUL;
6949
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006950 /* Abort if the character is a special key. */
6951 if (IS_SPECIAL(cap->nchar))
6952 {
6953 clearopbeep(cap->oap);
6954 return;
6955 }
6956
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 /* Visual mode "r" */
6958 if (VIsual_active)
6959 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006960 if (got_int)
6961 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006962 if (had_ctrl_v)
6963 {
6964 if (cap->nchar == '\r')
6965 cap->nchar = -1;
6966 else if (cap->nchar == '\n')
6967 cap->nchar = -2;
6968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 nv_operator(cap);
6970 return;
6971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973#ifdef FEAT_VIRTUALEDIT
6974 /* Break tabs, etc. */
6975 if (virtual_active())
6976 {
6977 if (u_save_cursor() == FAIL)
6978 return;
6979 if (gchar_cursor() == NUL)
6980 {
6981 /* Add extra space and put the cursor on the first one. */
6982 coladvance_force((colnr_T)(getviscol() + cap->count1));
6983 curwin->w_cursor.col -= cap->count1;
6984 }
6985 else if (gchar_cursor() == TAB)
6986 coladvance_force(getviscol());
6987 }
6988#endif
6989
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006990 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006992 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993#ifdef FEAT_MBYTE
6994 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6995#endif
6996 )
6997 {
6998 clearopbeep(cap->oap);
6999 return;
7000 }
7001
7002 /*
7003 * Replacing with a TAB is done by edit() when it is complicated because
7004 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7005 * Other characters are done below to avoid problems with things like
7006 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7007 */
7008 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7009 {
7010 stuffnumReadbuff(cap->count1);
7011 stuffcharReadbuff('R');
7012 stuffcharReadbuff('\t');
7013 stuffcharReadbuff(ESC);
7014 return;
7015 }
7016
7017 /* save line for undo */
7018 if (u_save_cursor() == FAIL)
7019 return;
7020
7021 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7022 {
7023 /*
7024 * Replace character(s) by a single newline.
7025 * Strange vi behaviour: Only one newline is inserted.
7026 * Delete the characters here.
7027 * Insert the newline with an insert command, takes care of
7028 * autoindent. The insert command depends on being on the last
7029 * character of a line or not.
7030 */
7031#ifdef FEAT_MBYTE
7032 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7033#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007034 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035#endif
7036 stuffcharReadbuff('\r');
7037 stuffcharReadbuff(ESC);
7038
7039 /* Give 'r' to edit(), to get the redo command right. */
7040 invoke_edit(cap, TRUE, 'r', FALSE);
7041 }
7042 else
7043 {
7044 prep_redo(cap->oap->regname, cap->count1,
7045 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7046
7047 curbuf->b_op_start = curwin->w_cursor;
7048#ifdef FEAT_MBYTE
7049 if (has_mbyte)
7050 {
7051 int old_State = State;
7052
7053 if (cap->ncharC1 != 0)
7054 AppendCharToRedobuff(cap->ncharC1);
7055 if (cap->ncharC2 != 0)
7056 AppendCharToRedobuff(cap->ncharC2);
7057
7058 /* This is slow, but it handles replacing a single-byte with a
7059 * multi-byte and the other way around. Also handles adding
7060 * composing characters for utf-8. */
7061 for (n = cap->count1; n > 0; --n)
7062 {
7063 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007064 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7065 {
7066 int c = ins_copychar(curwin->w_cursor.lnum
7067 + (cap->nchar == Ctrl_Y ? -1 : 1));
7068 if (c != NUL)
7069 ins_char(c);
7070 else
7071 /* will be decremented further down */
7072 ++curwin->w_cursor.col;
7073 }
7074 else
7075 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 State = old_State;
7077 if (cap->ncharC1 != 0)
7078 ins_char(cap->ncharC1);
7079 if (cap->ncharC2 != 0)
7080 ins_char(cap->ncharC2);
7081 }
7082 }
7083 else
7084#endif
7085 {
7086 /*
7087 * Replace the characters within one line.
7088 */
7089 for (n = cap->count1; n > 0; --n)
7090 {
7091 /*
7092 * Get ptr again, because u_save and/or showmatch() will have
7093 * released the line. At the same time we let know that the
7094 * line will be changed.
7095 */
7096 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007097 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7098 {
7099 int c = ins_copychar(curwin->w_cursor.lnum
7100 + (cap->nchar == Ctrl_Y ? -1 : 1));
7101 if (c != NUL)
7102 ptr[curwin->w_cursor.col] = c;
7103 }
7104 else
7105 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (p_sm && msg_silent == 0)
7107 showmatch(cap->nchar);
7108 ++curwin->w_cursor.col;
7109 }
7110#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007111 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007113 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
Bram Moolenaar009b2592004-10-24 19:18:58 +00007115 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007116 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007118 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 }
7120#endif
7121
7122 /* mark the buffer as changed and prepare for displaying */
7123 changed_bytes(curwin->w_cursor.lnum,
7124 (colnr_T)(curwin->w_cursor.col - cap->count1));
7125 }
7126 --curwin->w_cursor.col; /* cursor on the last replaced char */
7127#ifdef FEAT_MBYTE
7128 /* if the character on the left of the current cursor is a multi-byte
7129 * character, move two characters left */
7130 if (has_mbyte)
7131 mb_adjust_cursor();
7132#endif
7133 curbuf->b_op_end = curwin->w_cursor;
7134 curwin->w_set_curswant = TRUE;
7135 set_last_insert(cap->nchar);
7136 }
7137}
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139/*
7140 * 'o': Exchange start and end of Visual area.
7141 * 'O': same, but in block mode exchange left and right corners.
7142 */
7143 static void
7144v_swap_corners(cmdchar)
7145 int cmdchar;
7146{
7147 pos_T old_cursor;
7148 colnr_T left, right;
7149
7150 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7151 {
7152 old_cursor = curwin->w_cursor;
7153 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7154 curwin->w_cursor.lnum = VIsual.lnum;
7155 coladvance(left);
7156 VIsual = curwin->w_cursor;
7157
7158 curwin->w_cursor.lnum = old_cursor.lnum;
7159 curwin->w_curswant = right;
7160 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7161 * right one column */
7162 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7163 ++curwin->w_curswant;
7164 coladvance(curwin->w_curswant);
7165 if (curwin->w_cursor.col == old_cursor.col
7166#ifdef FEAT_VIRTUALEDIT
7167 && (!virtual_active()
7168 || curwin->w_cursor.coladd == old_cursor.coladd)
7169#endif
7170 )
7171 {
7172 curwin->w_cursor.lnum = VIsual.lnum;
7173 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7174 ++right;
7175 coladvance(right);
7176 VIsual = curwin->w_cursor;
7177
7178 curwin->w_cursor.lnum = old_cursor.lnum;
7179 coladvance(left);
7180 curwin->w_curswant = left;
7181 }
7182 }
7183 else
7184 {
7185 old_cursor = curwin->w_cursor;
7186 curwin->w_cursor = VIsual;
7187 VIsual = old_cursor;
7188 curwin->w_set_curswant = TRUE;
7189 }
7190}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
7192/*
7193 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7194 */
7195 static void
7196nv_Replace(cap)
7197 cmdarg_T *cap;
7198{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (VIsual_active) /* "R" is replace lines */
7200 {
7201 cap->cmdchar = 'c';
7202 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007203 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 VIsual_mode = 'V';
7205 nv_operator(cap);
7206 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007207 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
7209 if (!curbuf->b_p_ma)
7210 EMSG(_(e_modifiable));
7211 else
7212 {
7213#ifdef FEAT_VIRTUALEDIT
7214 if (virtual_active())
7215 coladvance(getviscol());
7216#endif
7217 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7218 }
7219 }
7220}
7221
7222#ifdef FEAT_VREPLACE
7223/*
7224 * "gr".
7225 */
7226 static void
7227nv_vreplace(cap)
7228 cmdarg_T *cap;
7229{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (VIsual_active)
7231 {
7232 cap->cmdchar = 'r';
7233 cap->nchar = cap->extra_char;
7234 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7235 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007236 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238 if (!curbuf->b_p_ma)
7239 EMSG(_(e_modifiable));
7240 else
7241 {
7242 if (cap->extra_char == Ctrl_V) /* get another character */
7243 cap->extra_char = get_literal();
7244 stuffcharReadbuff(cap->extra_char);
7245 stuffcharReadbuff(ESC);
7246# ifdef FEAT_VIRTUALEDIT
7247 if (virtual_active())
7248 coladvance(getviscol());
7249# endif
7250 invoke_edit(cap, TRUE, 'v', FALSE);
7251 }
7252 }
7253}
7254#endif
7255
7256/*
7257 * Swap case for "~" command, when it does not work like an operator.
7258 */
7259 static void
7260n_swapchar(cap)
7261 cmdarg_T *cap;
7262{
7263 long n;
7264 pos_T startpos;
7265 int did_change = 0;
7266#ifdef FEAT_NETBEANS_INTG
7267 pos_T pos;
7268 char_u *ptr;
7269 int count;
7270#endif
7271
7272 if (checkclearopq(cap->oap))
7273 return;
7274
7275 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7276 {
7277 clearopbeep(cap->oap);
7278 return;
7279 }
7280
7281 prep_redo_cmd(cap);
7282
7283 if (u_save_cursor() == FAIL)
7284 return;
7285
7286 startpos = curwin->w_cursor;
7287#ifdef FEAT_NETBEANS_INTG
7288 pos = startpos;
7289#endif
7290 for (n = cap->count1; n > 0; --n)
7291 {
7292 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7293 inc_cursor();
7294 if (gchar_cursor() == NUL)
7295 {
7296 if (vim_strchr(p_ww, '~') != NULL
7297 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7298 {
7299#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007300 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
7302 if (did_change)
7303 {
7304 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007305 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007306 netbeans_removed(curbuf, pos.lnum, pos.col,
7307 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007309 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 }
7311 pos.col = 0;
7312 pos.lnum++;
7313 }
7314#endif
7315 ++curwin->w_cursor.lnum;
7316 curwin->w_cursor.col = 0;
7317 if (n > 1)
7318 {
7319 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7320 break;
7321 u_clearline();
7322 }
7323 }
7324 else
7325 break;
7326 }
7327 }
7328#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007329 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
7331 ptr = ml_get(pos.lnum);
7332 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007333 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7334 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 }
7336#endif
7337
7338
7339 check_cursor();
7340 curwin->w_set_curswant = TRUE;
7341 if (did_change)
7342 {
7343 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7344 0L);
7345 curbuf->b_op_start = startpos;
7346 curbuf->b_op_end = curwin->w_cursor;
7347 if (curbuf->b_op_end.col > 0)
7348 --curbuf->b_op_end.col;
7349 }
7350}
7351
7352/*
7353 * Move cursor to mark.
7354 */
7355 static void
7356nv_cursormark(cap, flag, pos)
7357 cmdarg_T *cap;
7358 int flag;
7359 pos_T *pos;
7360{
7361 if (check_mark(pos) == FAIL)
7362 clearop(cap->oap);
7363 else
7364 {
7365 if (cap->cmdchar == '\''
7366 || cap->cmdchar == '`'
7367 || cap->cmdchar == '['
7368 || cap->cmdchar == ']')
7369 setpcmark();
7370 curwin->w_cursor = *pos;
7371 if (flag)
7372 beginline(BL_WHITE | BL_FIX);
7373 else
7374 check_cursor();
7375 }
7376 cap->oap->motion_type = flag ? MLINE : MCHAR;
7377 if (cap->cmdchar == '`')
7378 cap->oap->use_reg_one = TRUE;
7379 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7380 curwin->w_set_curswant = TRUE;
7381}
7382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383/*
7384 * Handle commands that are operators in Visual mode.
7385 */
7386 static void
7387v_visop(cap)
7388 cmdarg_T *cap;
7389{
7390 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7391
7392 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007393 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (isupper(cap->cmdchar))
7395 {
7396 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007397 {
7398 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7402 curwin->w_curswant = MAXCOL;
7403 }
7404 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7405 nv_operator(cap);
7406}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408/*
7409 * "s" and "S" commands.
7410 */
7411 static void
7412nv_subst(cap)
7413 cmdarg_T *cap;
7414{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7416 {
7417 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007418 {
7419 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 cap->cmdchar = 'c';
7423 nv_operator(cap);
7424 }
7425 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 nv_optrans(cap);
7427}
7428
7429/*
7430 * Abbreviated commands.
7431 */
7432 static void
7433nv_abbrev(cap)
7434 cmdarg_T *cap;
7435{
7436 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7437 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 /* in Visual mode these commands are operators */
7440 if (VIsual_active)
7441 v_visop(cap);
7442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 nv_optrans(cap);
7444}
7445
7446/*
7447 * Translate a command into another command.
7448 */
7449 static void
7450nv_optrans(cap)
7451 cmdarg_T *cap;
7452{
7453 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7454 (char_u *)"d$", (char_u *)"c$",
7455 (char_u *)"cl", (char_u *)"cc",
7456 (char_u *)"yy", (char_u *)":s\r"};
7457 static char_u *str = (char_u *)"xXDCsSY&";
7458
7459 if (!checkclearopq(cap->oap))
7460 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007461 /* In Vi "2D" doesn't delete the next line. Can't translate it
7462 * either, because "2." should also not use the count. */
7463 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7464 {
7465 cap->oap->start = curwin->w_cursor;
7466 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007467#ifdef FEAT_EVAL
7468 set_op_var(OP_DELETE);
7469#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007470 cap->count1 = 1;
7471 nv_dollar(cap);
7472 finish_op = TRUE;
7473 ResetRedobuff();
7474 AppendCharToRedobuff('D');
7475 }
7476 else
7477 {
7478 if (cap->count0)
7479 stuffnumReadbuff(cap->count0);
7480 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483 cap->opcount = 0;
7484}
7485
7486/*
7487 * "'" and "`" commands. Also for "g'" and "g`".
7488 * cap->arg is TRUE for "'" and "g'".
7489 */
7490 static void
7491nv_gomark(cap)
7492 cmdarg_T *cap;
7493{
7494 pos_T *pos;
7495 int c;
7496#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007497 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7499#endif
7500
7501 if (cap->cmdchar == 'g')
7502 c = cap->extra_char;
7503 else
7504 c = cap->nchar;
7505 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7506 if (pos == (pos_T *)-1) /* jumped to other file */
7507 {
7508 if (cap->arg)
7509 {
7510 check_cursor_lnum();
7511 beginline(BL_WHITE | BL_FIX);
7512 }
7513 else
7514 check_cursor();
7515 }
7516 else
7517 nv_cursormark(cap, cap->arg, pos);
7518
7519#ifdef FEAT_VIRTUALEDIT
7520 /* May need to clear the coladd that a mark includes. */
7521 if (!virtual_active())
7522 curwin->w_cursor.coladd = 0;
7523#endif
7524#ifdef FEAT_FOLDING
7525 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007526 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007527 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 && (fdo_flags & FDO_MARK)
7529 && old_KeyTyped)
7530 foldOpenCursor();
7531#endif
7532}
7533
7534/*
7535 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7536 */
7537 static void
7538nv_pcmark(cap)
7539 cmdarg_T *cap;
7540{
7541#ifdef FEAT_JUMPLIST
7542 pos_T *pos;
7543# ifdef FEAT_FOLDING
7544 linenr_T lnum = curwin->w_cursor.lnum;
7545 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7546# endif
7547
7548 if (!checkclearopq(cap->oap))
7549 {
7550 if (cap->cmdchar == 'g')
7551 pos = movechangelist((int)cap->count1);
7552 else
7553 pos = movemark((int)cap->count1);
7554 if (pos == (pos_T *)-1) /* jump to other file */
7555 {
7556 curwin->w_set_curswant = TRUE;
7557 check_cursor();
7558 }
7559 else if (pos != NULL) /* can jump */
7560 nv_cursormark(cap, FALSE, pos);
7561 else if (cap->cmdchar == 'g')
7562 {
7563 if (curbuf->b_changelistlen == 0)
7564 EMSG(_("E664: changelist is empty"));
7565 else if (cap->count1 < 0)
7566 EMSG(_("E662: At start of changelist"));
7567 else
7568 EMSG(_("E663: At end of changelist"));
7569 }
7570 else
7571 clearopbeep(cap->oap);
7572# ifdef FEAT_FOLDING
7573 if (cap->oap->op_type == OP_NOP
7574 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7575 && (fdo_flags & FDO_MARK)
7576 && old_KeyTyped)
7577 foldOpenCursor();
7578# endif
7579 }
7580#else
7581 clearopbeep(cap->oap);
7582#endif
7583}
7584
7585/*
7586 * Handle '"' command.
7587 */
7588 static void
7589nv_regname(cap)
7590 cmdarg_T *cap;
7591{
7592 if (checkclearop(cap->oap))
7593 return;
7594#ifdef FEAT_EVAL
7595 if (cap->nchar == '=')
7596 cap->nchar = get_expr_register();
7597#endif
7598 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7599 {
7600 cap->oap->regname = cap->nchar;
7601 cap->opcount = cap->count0; /* remember count before '"' */
7602#ifdef FEAT_EVAL
7603 set_reg_var(cap->oap->regname);
7604#endif
7605 }
7606 else
7607 clearopbeep(cap->oap);
7608}
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610/*
7611 * Handle "v", "V" and "CTRL-V" commands.
7612 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7613 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 */
7616 static void
7617nv_visual(cap)
7618 cmdarg_T *cap;
7619{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007620 if (cap->cmdchar == Ctrl_Q)
7621 cap->cmdchar = Ctrl_V;
7622
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7624 * characterwise, linewise, or blockwise. */
7625 if (cap->oap->op_type != OP_NOP)
7626 {
7627 cap->oap->motion_force = cap->cmdchar;
7628 finish_op = FALSE; /* operator doesn't finish now but later */
7629 return;
7630 }
7631
7632 VIsual_select = cap->arg;
7633 if (VIsual_active) /* change Visual mode */
7634 {
7635 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7636 end_visual_mode();
7637 else /* toggle char/block mode */
7638 { /* or char/line mode */
7639 VIsual_mode = cap->cmdchar;
7640 showmode();
7641 }
7642 redraw_curbuf_later(INVERTED); /* update the inversion */
7643 }
7644 else /* start Visual mode */
7645 {
7646 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007647 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007649 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 VIsual = curwin->w_cursor;
7651
7652 VIsual_active = TRUE;
7653 VIsual_reselect = TRUE;
7654 if (!cap->arg)
7655 /* start Select mode when 'selectmode' contains "cmd" */
7656 may_start_select('c');
7657#ifdef FEAT_MOUSE
7658 setmouse();
7659#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007660 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 redraw_cmdline = TRUE; /* show visual mode later */
7662 /*
7663 * For V and ^V, we multiply the number of lines even if there
7664 * was only one -- webb
7665 */
7666 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7667 {
7668 curwin->w_cursor.lnum +=
7669 resel_VIsual_line_count * cap->count0 - 1;
7670 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7671 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7672 }
7673 VIsual_mode = resel_VIsual_mode;
7674 if (VIsual_mode == 'v')
7675 {
7676 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007677 {
7678 validate_virtcol();
7679 curwin->w_curswant = curwin->w_virtcol
7680 + resel_VIsual_vcol * cap->count0 - 1;
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007683 curwin->w_curswant = resel_VIsual_vcol;
7684 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007686 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 curwin->w_curswant = MAXCOL;
7689 coladvance((colnr_T)MAXCOL);
7690 }
7691 else if (VIsual_mode == Ctrl_V)
7692 {
7693 validate_virtcol();
7694 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007695 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 coladvance(curwin->w_curswant);
7697 }
7698 else
7699 curwin->w_set_curswant = TRUE;
7700 redraw_curbuf_later(INVERTED); /* show the inversion */
7701 }
7702 else
7703 {
7704 if (!cap->arg)
7705 /* start Select mode when 'selectmode' contains "cmd" */
7706 may_start_select('c');
7707 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007708 if (VIsual_mode != 'V' && *p_sel == 'e')
7709 ++cap->count1; /* include one more char */
7710 if (cap->count0 > 0 && --cap->count1 > 0)
7711 {
7712 /* With a count select that many characters or lines. */
7713 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7714 nv_right(cap);
7715 else if (VIsual_mode == 'V')
7716 nv_down(cap);
7717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719 }
7720}
7721
7722/*
7723 * Start selection for Shift-movement keys.
7724 */
7725 void
7726start_selection()
7727{
7728 /* if 'selectmode' contains "key", start Select mode */
7729 may_start_select('k');
7730 n_start_visual_mode('v');
7731}
7732
7733/*
7734 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7735 */
7736 void
7737may_start_select(c)
7738 int c;
7739{
7740 VIsual_select = (stuff_empty() && typebuf_typed()
7741 && (vim_strchr(p_slm, c) != NULL));
7742}
7743
7744/*
7745 * Start Visual mode "c".
7746 * Should set VIsual_select before calling this.
7747 */
7748 static void
7749n_start_visual_mode(c)
7750 int c;
7751{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007752#ifdef FEAT_CONCEAL
7753 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007754 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007755#endif
7756
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 VIsual_mode = c;
7758 VIsual_active = TRUE;
7759 VIsual_reselect = TRUE;
7760#ifdef FEAT_VIRTUALEDIT
7761 /* Corner case: the 0 position in a tab may change when going into
7762 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7763 */
7764 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007765 {
7766 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769#endif
7770 VIsual = curwin->w_cursor;
7771
7772#ifdef FEAT_FOLDING
7773 foldAdjustVisual();
7774#endif
7775
7776#ifdef FEAT_MOUSE
7777 setmouse();
7778#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007779#ifdef FEAT_CONCEAL
7780 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007781 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007782#endif
7783
Bram Moolenaar09df3122006-01-23 22:23:09 +00007784 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 redraw_cmdline = TRUE; /* show visual mode later */
7786#ifdef FEAT_CLIPBOARD
7787 /* Make sure the clipboard gets updated. Needed because start and
7788 * end may still be the same, and the selection needs to be owned */
7789 clip_star.vmode = NUL;
7790#endif
7791
7792 /* Only need to redraw this line, unless still need to redraw an old
7793 * Visual area (when 'lazyredraw' is set). */
7794 if (curwin->w_redr_type < INVERTED)
7795 {
7796 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7797 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7798 }
7799}
7800
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
7802/*
7803 * CTRL-W: Window commands
7804 */
7805 static void
7806nv_window(cap)
7807 cmdarg_T *cap;
7808{
7809#ifdef FEAT_WINDOWS
7810 if (!checkclearop(cap->oap))
7811 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7812#else
7813 (void)checkclearop(cap->oap);
7814#endif
7815}
7816
7817/*
7818 * CTRL-Z: Suspend
7819 */
7820 static void
7821nv_suspend(cap)
7822 cmdarg_T *cap;
7823{
7824 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (VIsual_active)
7826 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 do_cmdline_cmd((char_u *)"st");
7828}
7829
7830/*
7831 * Commands starting with "g".
7832 */
7833 static void
7834nv_g_cmd(cap)
7835 cmdarg_T *cap;
7836{
7837 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 int i;
7840 int flag = FALSE;
7841
7842 switch (cap->nchar)
7843 {
7844#ifdef MEM_PROFILE
7845 /*
7846 * "g^A": dump log of used memory.
7847 */
7848 case Ctrl_A:
7849 vim_mem_profile_dump();
7850 break;
7851#endif
7852
7853#ifdef FEAT_VREPLACE
7854 /*
7855 * "gR": Enter virtual replace mode.
7856 */
7857 case 'R':
7858 cap->arg = TRUE;
7859 nv_Replace(cap);
7860 break;
7861
7862 case 'r':
7863 nv_vreplace(cap);
7864 break;
7865#endif
7866
7867 case '&':
7868 do_cmdline_cmd((char_u *)"%s//~/&");
7869 break;
7870
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 /*
7872 * "gv": Reselect the previous Visual area. If Visual already active,
7873 * exchange previous and current Visual area.
7874 */
7875 case 'v':
7876 if (checkclearop(oap))
7877 break;
7878
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007879 if ( curbuf->b_visual.vi_start.lnum == 0
7880 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7881 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 beep_flush();
7883 else
7884 {
7885 /* set w_cursor to the start of the Visual area, tpos to the end */
7886 if (VIsual_active)
7887 {
7888 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007889 VIsual_mode = curbuf->b_visual.vi_mode;
7890 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891# ifdef FEAT_EVAL
7892 curbuf->b_visual_mode_eval = i;
7893# endif
7894 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007895 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7896 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007898 tpos = curbuf->b_visual.vi_end;
7899 curbuf->b_visual.vi_end = curwin->w_cursor;
7900 curwin->w_cursor = curbuf->b_visual.vi_start;
7901 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 }
7903 else
7904 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007905 VIsual_mode = curbuf->b_visual.vi_mode;
7906 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7907 tpos = curbuf->b_visual.vi_end;
7908 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910
7911 VIsual_active = TRUE;
7912 VIsual_reselect = TRUE;
7913
7914 /* Set Visual to the start and w_cursor to the end of the Visual
7915 * area. Make sure they are on an existing character. */
7916 check_cursor();
7917 VIsual = curwin->w_cursor;
7918 curwin->w_cursor = tpos;
7919 check_cursor();
7920 update_topline();
7921 /*
7922 * When called from normal "g" command: start Select mode when
7923 * 'selectmode' contains "cmd". When called for K_SELECT, always
7924 * start Select mode.
7925 */
7926 if (cap->arg)
7927 VIsual_select = TRUE;
7928 else
7929 may_start_select('c');
7930#ifdef FEAT_MOUSE
7931 setmouse();
7932#endif
7933#ifdef FEAT_CLIPBOARD
7934 /* Make sure the clipboard gets updated. Needed because start and
7935 * end are still the same, and the selection needs to be owned */
7936 clip_star.vmode = NUL;
7937#endif
7938 redraw_curbuf_later(INVERTED);
7939 showmode();
7940 }
7941 break;
7942 /*
7943 * "gV": Don't reselect the previous Visual area after a Select mode
7944 * mapping of menu.
7945 */
7946 case 'V':
7947 VIsual_reselect = FALSE;
7948 break;
7949
7950 /*
7951 * "gh": start Select mode.
7952 * "gH": start Select line mode.
7953 * "g^H": start Select block mode.
7954 */
7955 case K_BS:
7956 cap->nchar = Ctrl_H;
7957 /* FALLTHROUGH */
7958 case 'h':
7959 case 'H':
7960 case Ctrl_H:
7961# ifdef EBCDIC
7962 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7963 if (cap->nchar == Ctrl_H)
7964 cap->cmdchar = Ctrl_V;
7965 else
7966# endif
7967 cap->cmdchar = cap->nchar + ('v' - 'h');
7968 cap->arg = TRUE;
7969 nv_visual(cap);
7970 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007971
7972 /* "gn", "gN" visually select next/previous search match
7973 * "gn" selects next match
7974 * "gN" selects previous match
7975 */
7976 case 'N':
7977 case 'n':
7978 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007979 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
7982 /*
7983 * "gj" and "gk" two new funny movement keys -- up and down
7984 * movement based on *screen* line rather than *file* line.
7985 */
7986 case 'j':
7987 case K_DOWN:
7988 /* with 'nowrap' it works just like the normal "j" command; also when
7989 * in a closed fold */
7990 if (!curwin->w_p_wrap
7991#ifdef FEAT_FOLDING
7992 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7993#endif
7994 )
7995 {
7996 oap->motion_type = MLINE;
7997 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7998 }
7999 else
8000 i = nv_screengo(oap, FORWARD, cap->count1);
8001 if (i == FAIL)
8002 clearopbeep(oap);
8003 break;
8004
8005 case 'k':
8006 case K_UP:
8007 /* with 'nowrap' it works just like the normal "k" command; also when
8008 * in a closed fold */
8009 if (!curwin->w_p_wrap
8010#ifdef FEAT_FOLDING
8011 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8012#endif
8013 )
8014 {
8015 oap->motion_type = MLINE;
8016 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8017 }
8018 else
8019 i = nv_screengo(oap, BACKWARD, cap->count1);
8020 if (i == FAIL)
8021 clearopbeep(oap);
8022 break;
8023
8024 /*
8025 * "gJ": join two lines without inserting a space.
8026 */
8027 case 'J':
8028 nv_join(cap);
8029 break;
8030
8031 /*
8032 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8033 * "gm": middle of "g0" and "g$".
8034 */
8035 case '^':
8036 flag = TRUE;
8037 /* FALLTHROUGH */
8038
8039 case '0':
8040 case 'm':
8041 case K_HOME:
8042 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 oap->motion_type = MCHAR;
8044 oap->inclusive = FALSE;
8045 if (curwin->w_p_wrap
8046#ifdef FEAT_VERTSPLIT
8047 && curwin->w_width != 0
8048#endif
8049 )
8050 {
8051 int width1 = W_WIDTH(curwin) - curwin_col_off();
8052 int width2 = width1 + curwin_col_off2();
8053
8054 validate_virtcol();
8055 i = 0;
8056 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8057 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8058 }
8059 else
8060 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008061 /* Go to the middle of the screen line. When 'number' or
8062 * 'relativenumber' is on and lines are wrapping the middle can be more
8063 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (cap->nchar == 'm')
8065 i += (W_WIDTH(curwin) - curwin_col_off()
8066 + ((curwin->w_p_wrap && i > 0)
8067 ? curwin_col_off2() : 0)) / 2;
8068 coladvance((colnr_T)i);
8069 if (flag)
8070 {
8071 do
8072 i = gchar_cursor();
8073 while (vim_iswhite(i) && oneright() == OK);
8074 }
8075 curwin->w_set_curswant = TRUE;
8076 break;
8077
8078 case '_':
8079 /* "g_": to the last non-blank character in the line or <count> lines
8080 * downward. */
8081 cap->oap->motion_type = MCHAR;
8082 cap->oap->inclusive = TRUE;
8083 curwin->w_curswant = MAXCOL;
8084 if (cursor_down((long)(cap->count1 - 1),
8085 cap->oap->op_type == OP_NOP) == FAIL)
8086 clearopbeep(cap->oap);
8087 else
8088 {
8089 char_u *ptr = ml_get_curline();
8090
8091 /* In Visual mode we may end up after the line. */
8092 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8093 --curwin->w_cursor.col;
8094
8095 /* Decrease the cursor column until it's on a non-blank. */
8096 while (curwin->w_cursor.col > 0
8097 && vim_iswhite(ptr[curwin->w_cursor.col]))
8098 --curwin->w_cursor.col;
8099 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008100 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102 break;
8103
8104 case '$':
8105 case K_END:
8106 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {
8108 int col_off = curwin_col_off();
8109
8110 oap->motion_type = MCHAR;
8111 oap->inclusive = TRUE;
8112 if (curwin->w_p_wrap
8113#ifdef FEAT_VERTSPLIT
8114 && curwin->w_width != 0
8115#endif
8116 )
8117 {
8118 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8119 if (cap->count1 == 1)
8120 {
8121 int width1 = W_WIDTH(curwin) - col_off;
8122 int width2 = width1 + curwin_col_off2();
8123
8124 validate_virtcol();
8125 i = width1 - 1;
8126 if (curwin->w_virtcol >= (colnr_T)width1)
8127 i += ((curwin->w_virtcol - width1) / width2 + 1)
8128 * width2;
8129 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008130
8131 /* Make sure we stick in this column. */
8132 validate_virtcol();
8133 curwin->w_curswant = curwin->w_virtcol;
8134 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8136 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8137 {
8138 /*
8139 * Check for landing on a character that got split at
8140 * the end of the line. We do not want to advance to
8141 * the next screen line.
8142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (curwin->w_virtcol > (colnr_T)i)
8144 --curwin->w_cursor.col;
8145 }
8146#endif
8147 }
8148 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8149 clearopbeep(oap);
8150 }
8151 else
8152 {
8153 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8154 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008155
8156 /* Make sure we stick in this column. */
8157 validate_virtcol();
8158 curwin->w_curswant = curwin->w_virtcol;
8159 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 }
8161 }
8162 break;
8163
8164 /*
8165 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8166 */
8167 case '*':
8168 case '#':
8169#if POUND != '#'
8170 case POUND: /* pound sign (sometimes equal to '#') */
8171#endif
8172 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8173 case ']': /* :tselect for current identifier */
8174 nv_ident(cap);
8175 break;
8176
8177 /*
8178 * ge and gE: go back to end of word
8179 */
8180 case 'e':
8181 case 'E':
8182 oap->motion_type = MCHAR;
8183 curwin->w_set_curswant = TRUE;
8184 oap->inclusive = TRUE;
8185 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8186 clearopbeep(oap);
8187 break;
8188
8189 /*
8190 * "g CTRL-G": display info about cursor position
8191 */
8192 case Ctrl_G:
8193 cursor_pos_info();
8194 break;
8195
8196 /*
8197 * "gi": start Insert at the last position.
8198 */
8199 case 'i':
8200 if (curbuf->b_last_insert.lnum != 0)
8201 {
8202 curwin->w_cursor = curbuf->b_last_insert;
8203 check_cursor_lnum();
8204 i = (int)STRLEN(ml_get_curline());
8205 if (curwin->w_cursor.col > (colnr_T)i)
8206 {
8207#ifdef FEAT_VIRTUALEDIT
8208 if (virtual_active())
8209 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8210#endif
8211 curwin->w_cursor.col = i;
8212 }
8213 }
8214 cap->cmdchar = 'i';
8215 nv_edit(cap);
8216 break;
8217
8218 /*
8219 * "gI": Start insert in column 1.
8220 */
8221 case 'I':
8222 beginline(0);
8223 if (!checkclearopq(oap))
8224 invoke_edit(cap, FALSE, 'g', FALSE);
8225 break;
8226
8227#ifdef FEAT_SEARCHPATH
8228 /*
8229 * "gf": goto file, edit file under cursor
8230 * "]f" and "[f": can also be used.
8231 */
8232 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008233 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 nv_gotofile(cap);
8235 break;
8236#endif
8237
8238 /* "g'm" and "g`m": jump to mark without setting pcmark */
8239 case '\'':
8240 cap->arg = TRUE;
8241 /*FALLTHROUGH*/
8242 case '`':
8243 nv_gomark(cap);
8244 break;
8245
8246 /*
8247 * "gs": Goto sleep.
8248 */
8249 case 's':
8250 do_sleep(cap->count1 * 1000L);
8251 break;
8252
8253 /*
8254 * "ga": Display the ascii value of the character under the
8255 * cursor. It is displayed in decimal, hex, and octal. -- webb
8256 */
8257 case 'a':
8258 do_ascii(NULL);
8259 break;
8260
8261#ifdef FEAT_MBYTE
8262 /*
8263 * "g8": Display the bytes used for the UTF-8 character under the
8264 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008265 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 */
8267 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008268 if (cap->count0 == 8)
8269 utf_find_illegal();
8270 else
8271 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 break;
8273#endif
8274
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008275 case '<':
8276 show_sb_text();
8277 break;
8278
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 /*
8280 * "gg": Goto the first line in file. With a count it goes to
8281 * that line number like for "G". -- webb
8282 */
8283 case 'g':
8284 cap->arg = FALSE;
8285 nv_goto(cap);
8286 break;
8287
8288 /*
8289 * Two-character operators:
8290 * "gq" Format text
8291 * "gw" Format text and keep cursor position
8292 * "g~" Toggle the case of the text.
8293 * "gu" Change text to lower case.
8294 * "gU" Change text to upper case.
8295 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008296 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 */
8298 case 'q':
8299 case 'w':
8300 oap->cursor_start = curwin->w_cursor;
8301 /*FALLTHROUGH*/
8302 case '~':
8303 case 'u':
8304 case 'U':
8305 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008306 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 nv_operator(cap);
8308 break;
8309
8310 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008311 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 * current function
8313 * "gD": idem, but in the current file.
8314 */
8315 case 'd':
8316 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008317 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 break;
8319
8320#ifdef FEAT_MOUSE
8321 /*
8322 * g<*Mouse> : <C-*mouse>
8323 */
8324 case K_MIDDLEMOUSE:
8325 case K_MIDDLEDRAG:
8326 case K_MIDDLERELEASE:
8327 case K_LEFTMOUSE:
8328 case K_LEFTDRAG:
8329 case K_LEFTRELEASE:
8330 case K_RIGHTMOUSE:
8331 case K_RIGHTDRAG:
8332 case K_RIGHTRELEASE:
8333 case K_X1MOUSE:
8334 case K_X1DRAG:
8335 case K_X1RELEASE:
8336 case K_X2MOUSE:
8337 case K_X2DRAG:
8338 case K_X2RELEASE:
8339 mod_mask = MOD_MASK_CTRL;
8340 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8341 break;
8342#endif
8343
8344 case K_IGNORE:
8345 break;
8346
8347 /*
8348 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8349 */
8350 case 'p':
8351 case 'P':
8352 nv_put(cap);
8353 break;
8354
8355#ifdef FEAT_BYTEOFF
8356 /* "go": goto byte count from start of buffer */
8357 case 'o':
8358 goto_byte(cap->count0);
8359 break;
8360#endif
8361
8362 /* "gQ": improved Ex mode */
8363 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008364 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
8366 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008367 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 break;
8369 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008370
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (!checkclearopq(oap))
8372 do_exmode(TRUE);
8373 break;
8374
8375#ifdef FEAT_JUMPLIST
8376 case ',':
8377 nv_pcmark(cap);
8378 break;
8379
8380 case ';':
8381 cap->count1 = -cap->count1;
8382 nv_pcmark(cap);
8383 break;
8384#endif
8385
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008386#ifdef FEAT_WINDOWS
8387 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008388 if (!checkclearop(oap))
8389 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008390 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008391 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008392 if (!checkclearop(oap))
8393 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008394 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008395#endif
8396
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008397 case '+':
8398 case '-': /* "g+" and "g-": undo or redo along the timeline */
8399 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008400 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008401 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008402 break;
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 default:
8405 clearopbeep(oap);
8406 break;
8407 }
8408}
8409
8410/*
8411 * Handle "o" and "O" commands.
8412 */
8413 static void
8414n_opencmd(cap)
8415 cmdarg_T *cap;
8416{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008417#ifdef FEAT_CONCEAL
8418 linenr_T oldline = curwin->w_cursor.lnum;
8419#endif
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 if (!checkclearopq(cap->oap))
8422 {
8423#ifdef FEAT_FOLDING
8424 if (cap->cmdchar == 'O')
8425 /* Open above the first line of a folded sequence of lines */
8426 (void)hasFolding(curwin->w_cursor.lnum,
8427 &curwin->w_cursor.lnum, NULL);
8428 else
8429 /* Open below the last line of a folded sequence of lines */
8430 (void)hasFolding(curwin->w_cursor.lnum,
8431 NULL, &curwin->w_cursor.lnum);
8432#endif
8433 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8434 (cap->cmdchar == 'O' ? 1 : 0)),
8435 (linenr_T)(curwin->w_cursor.lnum +
8436 (cap->cmdchar == 'o' ? 1 : 0))
8437 ) == OK
8438 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8439#ifdef FEAT_COMMENTS
8440 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8441#endif
8442 0, 0))
8443 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008444#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008445 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008446 update_single_line(curwin, oldline);
8447#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008448 /* When '#' is in 'cpoptions' ignore the count. */
8449 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8450 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008451#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008452 if (curwin->w_p_cul)
8453 /* force redraw of cursorline */
8454 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8457 }
8458 }
8459}
8460
8461/*
8462 * "." command: redo last change.
8463 */
8464 static void
8465nv_dot(cap)
8466 cmdarg_T *cap;
8467{
8468 if (!checkclearopq(cap->oap))
8469 {
8470 /*
8471 * If "restart_edit" is TRUE, the last but one command is repeated
8472 * instead of the last command (inserting text). This is used for
8473 * CTRL-O <.> in insert mode.
8474 */
8475 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8476 clearopbeep(cap->oap);
8477 }
8478}
8479
8480/*
8481 * CTRL-R: undo undo
8482 */
8483 static void
8484nv_redo(cap)
8485 cmdarg_T *cap;
8486{
8487 if (!checkclearopq(cap->oap))
8488 {
8489 u_redo((int)cap->count1);
8490 curwin->w_set_curswant = TRUE;
8491 }
8492}
8493
8494/*
8495 * Handle "U" command.
8496 */
8497 static void
8498nv_Undo(cap)
8499 cmdarg_T *cap;
8500{
8501 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008502 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {
8504 /* translate "gUU" to "gUgU" */
8505 cap->cmdchar = 'g';
8506 cap->nchar = 'U';
8507 nv_operator(cap);
8508 }
8509 else if (!checkclearopq(cap->oap))
8510 {
8511 u_undoline();
8512 curwin->w_set_curswant = TRUE;
8513 }
8514}
8515
8516/*
8517 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8518 * single character.
8519 */
8520 static void
8521nv_tilde(cap)
8522 cmdarg_T *cap;
8523{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008524 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 n_swapchar(cap);
8526 else
8527 nv_operator(cap);
8528}
8529
8530/*
8531 * Handle an operator command.
8532 * The actual work is done by do_pending_operator().
8533 */
8534 static void
8535nv_operator(cap)
8536 cmdarg_T *cap;
8537{
8538 int op_type;
8539
8540 op_type = get_op_type(cap->cmdchar, cap->nchar);
8541
8542 if (op_type == cap->oap->op_type) /* double operator works on lines */
8543 nv_lineop(cap);
8544 else if (!checkclearop(cap->oap))
8545 {
8546 cap->oap->start = curwin->w_cursor;
8547 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008548#ifdef FEAT_EVAL
8549 set_op_var(op_type);
8550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552}
8553
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008554#ifdef FEAT_EVAL
8555/*
8556 * Set v:operator to the characters for "optype".
8557 */
8558 static void
8559set_op_var(optype)
8560 int optype;
8561{
8562 char_u opchars[3];
8563
8564 if (optype == OP_NOP)
8565 set_vim_var_string(VV_OP, NULL, 0);
8566 else
8567 {
8568 opchars[0] = get_op_char(optype);
8569 opchars[1] = get_extra_op_char(optype);
8570 opchars[2] = NUL;
8571 set_vim_var_string(VV_OP, opchars, -1);
8572 }
8573}
8574#endif
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576/*
8577 * Handle linewise operator "dd", "yy", etc.
8578 *
8579 * "_" is is a strange motion command that helps make operators more logical.
8580 * It is actually implemented, but not documented in the real Vi. This motion
8581 * command actually refers to "the current line". Commands like "dd" and "yy"
8582 * are really an alternate form of "d_" and "y_". It does accept a count, so
8583 * "d3_" works to delete 3 lines.
8584 */
8585 static void
8586nv_lineop(cap)
8587 cmdarg_T *cap;
8588{
8589 cap->oap->motion_type = MLINE;
8590 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8591 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008592 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8593 && cap->oap->motion_force != 'v'
8594 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 || cap->oap->op_type == OP_LSHIFT
8596 || cap->oap->op_type == OP_RSHIFT)
8597 beginline(BL_SOL | BL_FIX);
8598 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8599 beginline(BL_WHITE | BL_FIX);
8600}
8601
8602/*
8603 * <Home> command.
8604 */
8605 static void
8606nv_home(cap)
8607 cmdarg_T *cap;
8608{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008609 /* CTRL-HOME is like "gg" */
8610 if (mod_mask & MOD_MASK_CTRL)
8611 nv_goto(cap);
8612 else
8613 {
8614 cap->count0 = 1;
8615 nv_pipe(cap);
8616 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008617 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8618 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619}
8620
8621/*
8622 * "|" command.
8623 */
8624 static void
8625nv_pipe(cap)
8626 cmdarg_T *cap;
8627{
8628 cap->oap->motion_type = MCHAR;
8629 cap->oap->inclusive = FALSE;
8630 beginline(0);
8631 if (cap->count0 > 0)
8632 {
8633 coladvance((colnr_T)(cap->count0 - 1));
8634 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8635 }
8636 else
8637 curwin->w_curswant = 0;
8638 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008639 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 curwin->w_set_curswant = FALSE;
8641}
8642
8643/*
8644 * Handle back-word command "b" and "B".
8645 * cap->arg is 1 for "B"
8646 */
8647 static void
8648nv_bck_word(cap)
8649 cmdarg_T *cap;
8650{
8651 cap->oap->motion_type = MCHAR;
8652 cap->oap->inclusive = FALSE;
8653 curwin->w_set_curswant = TRUE;
8654 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8655 clearopbeep(cap->oap);
8656#ifdef FEAT_FOLDING
8657 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8658 foldOpenCursor();
8659#endif
8660}
8661
8662/*
8663 * Handle word motion commands "e", "E", "w" and "W".
8664 * cap->arg is TRUE for "E" and "W".
8665 */
8666 static void
8667nv_wordcmd(cap)
8668 cmdarg_T *cap;
8669{
8670 int n;
8671 int word_end;
8672 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008673 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
8675 /*
8676 * Set inclusive for the "E" and "e" command.
8677 */
8678 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8679 word_end = TRUE;
8680 else
8681 word_end = FALSE;
8682 cap->oap->inclusive = word_end;
8683
8684 /*
8685 * "cw" and "cW" are a special case.
8686 */
8687 if (!word_end && cap->oap->op_type == OP_CHANGE)
8688 {
8689 n = gchar_cursor();
8690 if (n != NUL) /* not an empty line */
8691 {
8692 if (vim_iswhite(n))
8693 {
8694 /*
8695 * Reproduce a funny Vi behaviour: "cw" on a blank only
8696 * changes one character, not all blanks until the start of
8697 * the next word. Only do this when the 'w' flag is included
8698 * in 'cpoptions'.
8699 */
8700 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8701 {
8702 cap->oap->inclusive = TRUE;
8703 cap->oap->motion_type = MCHAR;
8704 return;
8705 }
8706 }
8707 else
8708 {
8709 /*
8710 * This is a little strange. To match what the real Vi does,
8711 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8712 * that we are not on a space or a TAB. This seems impolite
8713 * at first, but it's really more what we mean when we say
8714 * 'cw'.
8715 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008716 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 * will change only one character! This is done by setting
8718 * flag.
8719 */
8720 cap->oap->inclusive = TRUE;
8721 word_end = TRUE;
8722 flag = TRUE;
8723 }
8724 }
8725 }
8726
8727 cap->oap->motion_type = MCHAR;
8728 curwin->w_set_curswant = TRUE;
8729 if (word_end)
8730 n = end_word(cap->count1, cap->arg, flag, FALSE);
8731 else
8732 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8733
Bram Moolenaardfefb982008-04-01 10:06:39 +00008734 /* Don't leave the cursor on the NUL past the end of line. Unless we
8735 * didn't move it forward. */
8736 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008737 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 if (n == FAIL && cap->oap->op_type == OP_NOP)
8740 clearopbeep(cap->oap);
8741 else
8742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744#ifdef FEAT_FOLDING
8745 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8746 foldOpenCursor();
8747#endif
8748 }
8749}
8750
8751/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008752 * Used after a movement command: If the cursor ends up on the NUL after the
8753 * end of the line, may move it back to the last character and make the motion
8754 * inclusive.
8755 */
8756 static void
8757adjust_cursor(oap)
8758 oparg_T *oap;
8759{
8760 /* The cursor cannot remain on the NUL when:
8761 * - the column is > 0
8762 * - not in Visual mode or 'selection' is "o"
8763 * - 'virtualedit' is not "all" and not "onemore".
8764 */
8765 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008766 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008767#ifdef FEAT_VIRTUALEDIT
8768 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8769#endif
8770 )
8771 {
8772 --curwin->w_cursor.col;
8773#ifdef FEAT_MBYTE
8774 /* prevent cursor from moving on the trail byte */
8775 if (has_mbyte)
8776 mb_adjust_cursor();
8777#endif
8778 oap->inclusive = TRUE;
8779 }
8780}
8781
8782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 * "0" and "^" commands.
8784 * cap->arg is the argument for beginline().
8785 */
8786 static void
8787nv_beginline(cap)
8788 cmdarg_T *cap;
8789{
8790 cap->oap->motion_type = MCHAR;
8791 cap->oap->inclusive = FALSE;
8792 beginline(cap->arg);
8793#ifdef FEAT_FOLDING
8794 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8795 foldOpenCursor();
8796#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008797 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8798 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801/*
8802 * In exclusive Visual mode, may include the last character.
8803 */
8804 static void
8805adjust_for_sel(cap)
8806 cmdarg_T *cap;
8807{
8808 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8809 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8810 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008811#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (has_mbyte)
8813 inc_cursor();
8814 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 ++curwin->w_cursor.col;
8817 cap->oap->inclusive = FALSE;
8818 }
8819}
8820
8821/*
8822 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8823 * Should check VIsual_mode before calling this.
8824 * Returns TRUE when backed up to the previous line.
8825 */
8826 static int
8827unadjust_for_sel()
8828{
8829 pos_T *pp;
8830
8831 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8832 {
8833 if (lt(VIsual, curwin->w_cursor))
8834 pp = &curwin->w_cursor;
8835 else
8836 pp = &VIsual;
8837#ifdef FEAT_VIRTUALEDIT
8838 if (pp->coladd > 0)
8839 --pp->coladd;
8840 else
8841#endif
8842 if (pp->col > 0)
8843 {
8844 --pp->col;
8845#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008846 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847#endif
8848 }
8849 else if (pp->lnum > 1)
8850 {
8851 --pp->lnum;
8852 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8853 return TRUE;
8854 }
8855 }
8856 return FALSE;
8857}
8858
8859/*
8860 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8861 */
8862 static void
8863nv_select(cap)
8864 cmdarg_T *cap;
8865{
8866 if (VIsual_active)
8867 VIsual_select = TRUE;
8868 else if (VIsual_reselect)
8869 {
8870 cap->nchar = 'v'; /* fake "gv" command */
8871 cap->arg = TRUE;
8872 nv_g_cmd(cap);
8873 }
8874}
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
8877/*
8878 * "G", "gg", CTRL-END, CTRL-HOME.
8879 * cap->arg is TRUE for "G".
8880 */
8881 static void
8882nv_goto(cap)
8883 cmdarg_T *cap;
8884{
8885 linenr_T lnum;
8886
8887 if (cap->arg)
8888 lnum = curbuf->b_ml.ml_line_count;
8889 else
8890 lnum = 1L;
8891 cap->oap->motion_type = MLINE;
8892 setpcmark();
8893
8894 /* When a count is given, use it instead of the default lnum */
8895 if (cap->count0 != 0)
8896 lnum = cap->count0;
8897 if (lnum < 1L)
8898 lnum = 1L;
8899 else if (lnum > curbuf->b_ml.ml_line_count)
8900 lnum = curbuf->b_ml.ml_line_count;
8901 curwin->w_cursor.lnum = lnum;
8902 beginline(BL_SOL | BL_FIX);
8903#ifdef FEAT_FOLDING
8904 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8905 foldOpenCursor();
8906#endif
8907}
8908
8909/*
8910 * CTRL-\ in Normal mode.
8911 */
8912 static void
8913nv_normal(cap)
8914 cmdarg_T *cap;
8915{
8916 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8917 {
8918 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008919 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 clear_cmdline = TRUE; /* unshow mode later */
8921 restart_edit = 0;
8922#ifdef FEAT_CMDWIN
8923 if (cmdwin_type != 0)
8924 cmdwin_result = Ctrl_C;
8925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (VIsual_active)
8927 {
8928 end_visual_mode(); /* stop Visual */
8929 redraw_curbuf_later(INVERTED);
8930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8932 if (cap->nchar == Ctrl_G && p_im)
8933 restart_edit = 'a';
8934 }
8935 else
8936 clearopbeep(cap->oap);
8937}
8938
8939/*
8940 * ESC in Normal mode: beep, but don't flush buffers.
8941 * Don't even beep if we are canceling a command.
8942 */
8943 static void
8944nv_esc(cap)
8945 cmdarg_T *cap;
8946{
8947 int no_reason;
8948
8949 no_reason = (cap->oap->op_type == OP_NOP
8950 && cap->opcount == 0
8951 && cap->count0 == 0
8952 && cap->oap->regname == 0
8953 && !p_im);
8954
8955 if (cap->arg) /* TRUE for CTRL-C */
8956 {
8957 if (restart_edit == 0
8958#ifdef FEAT_CMDWIN
8959 && cmdwin_type == 0
8960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 && no_reason)
8963 MSG(_("Type :quit<Enter> to exit Vim"));
8964
8965 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8966 * set again below when halfway a mapping. */
8967 if (!p_im)
8968 restart_edit = 0;
8969#ifdef FEAT_CMDWIN
8970 if (cmdwin_type != 0)
8971 {
8972 cmdwin_result = K_IGNORE;
8973 got_int = FALSE; /* don't stop executing autocommands et al. */
8974 return;
8975 }
8976#endif
8977 }
8978
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (VIsual_active)
8980 {
8981 end_visual_mode(); /* stop Visual */
8982 check_cursor_col(); /* make sure cursor is not beyond EOL */
8983 curwin->w_set_curswant = TRUE;
8984 redraw_curbuf_later(INVERTED);
8985 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008986 else if (no_reason)
8987 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 clearop(cap->oap);
8989
8990 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8991 * set return to Insert mode afterwards. */
8992 if (restart_edit == 0 && goto_im()
8993#ifdef FEAT_EX_EXTRA
8994 && ex_normal_busy == 0
8995#endif
8996 )
8997 restart_edit = 'a';
8998}
8999
9000/*
9001 * Handle "A", "a", "I", "i" and <Insert> commands.
9002 */
9003 static void
9004nv_edit(cap)
9005 cmdarg_T *cap;
9006{
9007 /* <Insert> is equal to "i" */
9008 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9009 cap->cmdchar = 'i';
9010
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 /* in Visual mode "A" and "I" are an operator */
9012 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9013 v_visop(cap);
9014
9015 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009016 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9017 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
9019#ifdef FEAT_TEXTOBJ
9020 nv_object(cap);
9021#else
9022 clearopbeep(cap->oap);
9023#endif
9024 }
9025 else if (!curbuf->b_p_ma && !p_im)
9026 {
9027 /* Only give this error when 'insertmode' is off. */
9028 EMSG(_(e_modifiable));
9029 clearop(cap->oap);
9030 }
9031 else if (!checkclearopq(cap->oap))
9032 {
9033 switch (cap->cmdchar)
9034 {
9035 case 'A': /* "A"ppend after the line */
9036 curwin->w_set_curswant = TRUE;
9037#ifdef FEAT_VIRTUALEDIT
9038 if (ve_flags == VE_ALL)
9039 {
9040 int save_State = State;
9041
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009042 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 * character past the end of the line */
9044 State = INSERT;
9045 coladvance((colnr_T)MAXCOL);
9046 State = save_State;
9047 }
9048 else
9049#endif
9050 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9051 break;
9052
9053 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009054 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9055 beginline(BL_WHITE);
9056 else
9057 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 break;
9059
9060 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9061#ifdef FEAT_VIRTUALEDIT
9062 /* increment coladd when in virtual space, increment the
9063 * column otherwise, also to append after an unprintable char */
9064 if (virtual_active()
9065 && (curwin->w_cursor.coladd > 0
9066 || *ml_get_cursor() == NUL
9067 || *ml_get_cursor() == TAB))
9068 curwin->w_cursor.coladd++;
9069 else
9070#endif
9071 if (*ml_get_cursor() != NUL)
9072 inc_cursor();
9073 break;
9074 }
9075
9076#ifdef FEAT_VIRTUALEDIT
9077 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9078 {
9079 int save_State = State;
9080
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009081 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 * character past the end of the line */
9083 State = INSERT;
9084 coladvance(getviscol());
9085 State = save_State;
9086 }
9087#endif
9088
9089 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9090 }
9091}
9092
9093/*
9094 * Invoke edit() and take care of "restart_edit" and the return value.
9095 */
9096 static void
9097invoke_edit(cap, repl, cmd, startln)
9098 cmdarg_T *cap;
9099 int repl; /* "r" or "gr" command */
9100 int cmd;
9101 int startln;
9102{
9103 int restart_edit_save = 0;
9104
9105 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9106 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9107 * it. */
9108 if (repl || !stuff_empty())
9109 restart_edit_save = restart_edit;
9110 else
9111 restart_edit_save = 0;
9112
9113 /* Always reset "restart_edit", this is not a restarted edit. */
9114 restart_edit = 0;
9115
9116 if (edit(cmd, startln, cap->count1))
9117 cap->retval |= CA_COMMAND_BUSY;
9118
9119 if (restart_edit == 0)
9120 restart_edit = restart_edit_save;
9121}
9122
9123#ifdef FEAT_TEXTOBJ
9124/*
9125 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9126 */
9127 static void
9128nv_object(cap)
9129 cmdarg_T *cap;
9130{
9131 int flag;
9132 int include;
9133 char_u *mps_save;
9134
9135 if (cap->cmdchar == 'i')
9136 include = FALSE; /* "ix" = inner object: exclude white space */
9137 else
9138 include = TRUE; /* "ax" = an object: include white space */
9139
9140 /* Make sure (), [], {} and <> are in 'matchpairs' */
9141 mps_save = curbuf->b_p_mps;
9142 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9143
9144 switch (cap->nchar)
9145 {
9146 case 'w': /* "aw" = a word */
9147 flag = current_word(cap->oap, cap->count1, include, FALSE);
9148 break;
9149 case 'W': /* "aW" = a WORD */
9150 flag = current_word(cap->oap, cap->count1, include, TRUE);
9151 break;
9152 case 'b': /* "ab" = a braces block */
9153 case '(':
9154 case ')':
9155 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9156 break;
9157 case 'B': /* "aB" = a Brackets block */
9158 case '{':
9159 case '}':
9160 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9161 break;
9162 case '[': /* "a[" = a [] block */
9163 case ']':
9164 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9165 break;
9166 case '<': /* "a<" = a <> block */
9167 case '>':
9168 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9169 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009170 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009171 /* Do not adjust oap->end in do_pending_operator()
9172 * otherwise there are different results for 'dit'
9173 * (note leading whitespace in last line):
9174 * 1) <b> 2) <b>
9175 * foobar foobar
9176 * </b> </b>
9177 */
9178 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009179 flag = current_tagblock(cap->oap, cap->count1, include);
9180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 case 'p': /* "ap" = a paragraph */
9182 flag = current_par(cap->oap, cap->count1, include, 'p');
9183 break;
9184 case 's': /* "as" = a sentence */
9185 flag = current_sent(cap->oap, cap->count1, include);
9186 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009187 case '"': /* "a"" = a double quoted string */
9188 case '\'': /* "a'" = a single quoted string */
9189 case '`': /* "a`" = a backtick quoted string */
9190 flag = current_quote(cap->oap, cap->count1, include,
9191 cap->nchar);
9192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#if 0 /* TODO */
9194 case 'S': /* "aS" = a section */
9195 case 'f': /* "af" = a filename */
9196 case 'u': /* "au" = a URL */
9197#endif
9198 default:
9199 flag = FAIL;
9200 break;
9201 }
9202
9203 curbuf->b_p_mps = mps_save;
9204 if (flag == FAIL)
9205 clearopbeep(cap->oap);
9206 adjust_cursor_col();
9207 curwin->w_set_curswant = TRUE;
9208}
9209#endif
9210
9211/*
9212 * "q" command: Start/stop recording.
9213 * "q:", "q/", "q?": edit command-line in command-line window.
9214 */
9215 static void
9216nv_record(cap)
9217 cmdarg_T *cap;
9218{
9219 if (cap->oap->op_type == OP_FORMAT)
9220 {
9221 /* "gqq" is the same as "gqgq": format line */
9222 cap->cmdchar = 'g';
9223 cap->nchar = 'q';
9224 nv_operator(cap);
9225 }
9226 else if (!checkclearop(cap->oap))
9227 {
9228#ifdef FEAT_CMDWIN
9229 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9230 {
9231 stuffcharReadbuff(cap->nchar);
9232 stuffcharReadbuff(K_CMDWIN);
9233 }
9234 else
9235#endif
9236 /* (stop) recording into a named register, unless executing a
9237 * register */
9238 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9239 clearopbeep(cap->oap);
9240 }
9241}
9242
9243/*
9244 * Handle the "@r" command.
9245 */
9246 static void
9247nv_at(cap)
9248 cmdarg_T *cap;
9249{
9250 if (checkclearop(cap->oap))
9251 return;
9252#ifdef FEAT_EVAL
9253 if (cap->nchar == '=')
9254 {
9255 if (get_expr_register() == NUL)
9256 return;
9257 }
9258#endif
9259 while (cap->count1-- && !got_int)
9260 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009261 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
9263 clearopbeep(cap->oap);
9264 break;
9265 }
9266 line_breakcheck();
9267 }
9268}
9269
9270/*
9271 * Handle the CTRL-U and CTRL-D commands.
9272 */
9273 static void
9274nv_halfpage(cap)
9275 cmdarg_T *cap;
9276{
9277 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9278 || (cap->cmdchar == Ctrl_D
9279 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9280 clearopbeep(cap->oap);
9281 else if (!checkclearop(cap->oap))
9282 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9283}
9284
9285/*
9286 * Handle "J" or "gJ" command.
9287 */
9288 static void
9289nv_join(cap)
9290 cmdarg_T *cap;
9291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 if (VIsual_active) /* join the visual lines */
9293 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009294 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 {
9296 if (cap->count0 <= 1)
9297 cap->count0 = 2; /* default for join is two lines! */
9298 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9299 curbuf->b_ml.ml_line_count)
9300 clearopbeep(cap->oap); /* beyond last line */
9301 else
9302 {
9303 prep_redo(cap->oap->regname, cap->count0,
9304 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009305 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 }
9307 }
9308}
9309
9310/*
9311 * "P", "gP", "p" and "gp" commands.
9312 */
9313 static void
9314nv_put(cap)
9315 cmdarg_T *cap;
9316{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 int regname = 0;
9318 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009319 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009320 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 int dir;
9322 int flags = 0;
9323
9324 if (cap->oap->op_type != OP_NOP)
9325 {
9326#ifdef FEAT_DIFF
9327 /* "dp" is ":diffput" */
9328 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9329 {
9330 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009331 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 }
9333 else
9334#endif
9335 clearopbeep(cap->oap);
9336 }
9337 else
9338 {
9339 dir = (cap->cmdchar == 'P'
9340 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9341 ? BACKWARD : FORWARD;
9342 prep_redo_cmd(cap);
9343 if (cap->cmdchar == 'g')
9344 flags |= PUT_CURSEND;
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 if (VIsual_active)
9347 {
9348 /* Putting in Visual mode: The put text replaces the selected
9349 * text. First delete the selected text, then put the new text.
9350 * Need to save and restore the registers that the delete
9351 * overwrites if the old contents is being put.
9352 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009353 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009355#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009357#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009358 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009359 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009360#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
9364 )
9365 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009366 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 * put, save it first. */
9368 reg1 = get_register(regname, TRUE);
9369 }
9370
9371 /* Now delete the selected text. */
9372 cap->cmdchar = 'd';
9373 cap->nchar = NUL;
9374 cap->oap->regname = NUL;
9375 nv_operator(cap);
9376 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009377 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378
9379 /* delete PUT_LINE_BACKWARD; */
9380 cap->oap->regname = regname;
9381
9382 if (reg1 != NULL)
9383 {
9384 /* Delete probably changed the register we want to put, save
9385 * it first. Then put back what was there before the delete. */
9386 reg2 = get_register(regname, FALSE);
9387 put_register(regname, reg1);
9388 }
9389
9390 /* When deleted a linewise Visual area, put the register as
9391 * lines to avoid it joined with the next line. When deletion was
9392 * characterwise, split a line when putting lines. */
9393 if (VIsual_mode == 'V')
9394 flags |= PUT_LINE;
9395 else if (VIsual_mode == 'v')
9396 flags |= PUT_LINE_SPLIT;
9397 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9398 flags |= PUT_LINE_FORWARD;
9399 dir = BACKWARD;
9400 if ((VIsual_mode != 'V'
9401 && curwin->w_cursor.col < curbuf->b_op_start.col)
9402 || (VIsual_mode == 'V'
9403 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9404 /* cursor is at the end of the line or end of file, put
9405 * forward. */
9406 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009407 /* May have been reset in do_put(). */
9408 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 do_put(cap->oap->regname, dir, cap->count1, flags);
9411
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 /* If a register was saved, put it back now. */
9413 if (reg2 != NULL)
9414 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009415
9416 /* What to reselect with "gv"? Selecting the just put text seems to
9417 * be the most useful, since the original text was removed. */
9418 if (was_visual)
9419 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009420 curbuf->b_visual.vi_start = curbuf->b_op_start;
9421 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009422 }
9423
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009424 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009425 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009426 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009427 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009428 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009429
9430 /* If the cursor was in that line, move it to the end of the last
9431 * line. */
9432 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9433 {
9434 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9435 coladvance((colnr_T)MAXCOL);
9436 }
9437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 auto_format(FALSE, TRUE);
9439 }
9440}
9441
9442/*
9443 * "o" and "O" commands.
9444 */
9445 static void
9446nv_open(cap)
9447 cmdarg_T *cap;
9448{
9449#ifdef FEAT_DIFF
9450 /* "do" is ":diffget" */
9451 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9452 {
9453 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009454 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 }
9456 else
9457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (VIsual_active) /* switch start and end of visual */
9459 v_swap_corners(cap->cmdchar);
9460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 n_opencmd(cap);
9462}
9463
9464#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
9466nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009467 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 ProcessSniffRequests();
9470}
9471#endif
9472
9473#ifdef FEAT_NETBEANS_INTG
9474 static void
9475nv_nbcmd(cap)
9476 cmdarg_T *cap;
9477{
9478 netbeans_keycommand(cap->nchar);
9479}
9480#endif
9481
9482#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 static void
9484nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009485 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486{
9487 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9488}
9489#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009490
9491#ifdef FEAT_AUTOCMD
9492/*
9493 * Trigger CursorHold event.
9494 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9495 * input buffer. "did_cursorhold" is set to avoid retriggering.
9496 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009497 static void
9498nv_cursorhold(cap)
9499 cmdarg_T *cap;
9500{
9501 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9502 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009503 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009504}
9505#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009506
9507/*
9508 * calculate start/end virtual columns for operating in block mode
9509 */
9510 static void
9511get_op_vcol(oap, redo_VIsual_vcol, initial)
9512 oparg_T *oap;
9513 colnr_T redo_VIsual_vcol;
9514 int initial; /* when true: adjust position for 'selectmode' */
9515{
9516 colnr_T start, end;
9517
9518 if (VIsual_mode != Ctrl_V)
9519 return;
9520
9521 oap->block_mode = TRUE;
9522
9523#ifdef FEAT_MBYTE
9524 /* prevent from moving onto a trail byte */
9525 if (has_mbyte)
9526 mb_adjustpos(curwin->w_buffer, &oap->end);
9527#endif
9528
9529 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
9530 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9531
9532 if (start < oap->start_vcol)
9533 oap->start_vcol = start;
9534 if (end > oap->end_vcol)
9535 {
9536 if (initial && *p_sel == 'e' && start >= 1
9537 && start - 1 >= oap->end_vcol)
9538 oap->end_vcol = start - 1;
9539 else
9540 oap->end_vcol = end;
9541 }
9542 /* if '$' was used, get oap->end_vcol from longest line */
9543 if (curwin->w_curswant == MAXCOL)
9544 {
9545 curwin->w_cursor.col = MAXCOL;
9546 oap->end_vcol = 0;
9547 for (curwin->w_cursor.lnum = oap->start.lnum;
9548 curwin->w_cursor.lnum <= oap->end.lnum;
9549 ++curwin->w_cursor.lnum)
9550 {
9551 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9552 if (end > oap->end_vcol)
9553 oap->end_vcol = end;
9554 }
9555 }
9556 else if (redo_VIsual_busy)
9557 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9558 /*
9559 * Correct oap->end.col and oap->start.col to be the
9560 * upper-left and lower-right corner of the block area.
9561 *
9562 * (Actually, this does convert column positions into character
9563 * positions)
9564 */
9565 curwin->w_cursor.lnum = oap->end.lnum;
9566 coladvance(oap->end_vcol);
9567 oap->end = curwin->w_cursor;
9568
9569 curwin->w_cursor = oap->start;
9570 coladvance(oap->start_vcol);
9571 oap->start = curwin->w_cursor;
9572}