blob: 38a9f81b4ec24f845e80e3d82ab86ecdd3da145a [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 Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000178static char *e_noident = N_("E349: No identifier under cursor");
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * Function to be called for a Normal or Visual mode command.
182 * The argument is a cmdarg_T.
183 */
184typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
185
186/* Values for cmd_flags. */
187#define NV_NCH 0x01 /* may need to get a second char */
188#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
189#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
190#define NV_LANG 0x08 /* second char needs language adjustment */
191
192#define NV_SS 0x10 /* may start selection */
193#define NV_SSS 0x20 /* may start selection with shift modifier */
194#define NV_STS 0x40 /* may stop selection without shift modif. */
195#define NV_RL 0x80 /* 'rightleft' modifies command */
196#define NV_KEEPREG 0x100 /* don't clear regname */
197#define NV_NCW 0x200 /* not allowed in command-line window */
198
199/*
200 * Generally speaking, every Normal mode command should either clear any
201 * pending operator (with *clearop*()), or set the motion type variable
202 * oap->motion_type.
203 *
204 * When a cursor motion command is made, it is marked as being a character or
205 * line oriented motion. Then, if an operator is in effect, the operation
206 * becomes character or line oriented accordingly.
207 */
208
209/*
210 * This table contains one entry for every Normal or Visual mode command.
211 * The order doesn't matter, init_normal_cmds() will create a sorted index.
212 * It is faster when all keys from zero to '~' are present.
213 */
214static const struct nv_cmd
215{
216 int cmd_char; /* (first) command character */
217 nv_func_T cmd_func; /* function for this command */
218 short_u cmd_flags; /* NV_ flags */
219 short cmd_arg; /* value for ca.arg */
220} nv_cmds[] =
221{
222 {NUL, nv_error, 0, 0},
223 {Ctrl_A, nv_addsub, 0, 0},
224 {Ctrl_B, nv_page, NV_STS, BACKWARD},
225 {Ctrl_C, nv_esc, 0, TRUE},
226 {Ctrl_D, nv_halfpage, 0, 0},
227 {Ctrl_E, nv_scroll_line, 0, TRUE},
228 {Ctrl_F, nv_page, NV_STS, FORWARD},
229 {Ctrl_G, nv_ctrlg, 0, 0},
230 {Ctrl_H, nv_ctrlh, 0, 0},
231 {Ctrl_I, nv_pcmark, 0, 0},
232 {NL, nv_down, 0, FALSE},
233 {Ctrl_K, nv_error, 0, 0},
234 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000235 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {Ctrl_N, nv_down, NV_STS, FALSE},
237 {Ctrl_O, nv_ctrlo, 0, 0},
238 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000239 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {Ctrl_R, nv_redo, 0, 0},
241 {Ctrl_S, nv_ignore, 0, 0},
242 {Ctrl_T, nv_tagpop, NV_NCW, 0},
243 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_V, nv_visual, 0, FALSE},
245 {'V', nv_visual, 0, FALSE},
246 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_W, nv_window, 0, 0},
248 {Ctrl_X, nv_addsub, 0, 0},
249 {Ctrl_Y, nv_scroll_line, 0, FALSE},
250 {Ctrl_Z, nv_suspend, 0, 0},
251 {ESC, nv_esc, 0, FALSE},
252 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
253 {Ctrl_RSB, nv_ident, NV_NCW, 0},
254 {Ctrl_HAT, nv_hat, NV_NCW, 0},
255 {Ctrl__, nv_error, 0, 0},
256 {' ', nv_right, 0, 0},
257 {'!', nv_operator, 0, 0},
258 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
259 {'#', nv_ident, 0, 0},
260 {'$', nv_dollar, 0, 0},
261 {'%', nv_percent, 0, 0},
262 {'&', nv_optrans, 0, 0},
263 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
264 {'(', nv_brace, 0, BACKWARD},
265 {')', nv_brace, 0, FORWARD},
266 {'*', nv_ident, 0, 0},
267 {'+', nv_down, 0, TRUE},
268 {',', nv_csearch, 0, TRUE},
269 {'-', nv_up, 0, TRUE},
270 {'.', nv_dot, NV_KEEPREG, 0},
271 {'/', nv_search, 0, FALSE},
272 {'0', nv_beginline, 0, 0},
273 {'1', nv_ignore, 0, 0},
274 {'2', nv_ignore, 0, 0},
275 {'3', nv_ignore, 0, 0},
276 {'4', nv_ignore, 0, 0},
277 {'5', nv_ignore, 0, 0},
278 {'6', nv_ignore, 0, 0},
279 {'7', nv_ignore, 0, 0},
280 {'8', nv_ignore, 0, 0},
281 {'9', nv_ignore, 0, 0},
282 {':', nv_colon, 0, 0},
283 {';', nv_csearch, 0, FALSE},
284 {'<', nv_operator, NV_RL, 0},
285 {'=', nv_operator, 0, 0},
286 {'>', nv_operator, NV_RL, 0},
287 {'?', nv_search, 0, FALSE},
288 {'@', nv_at, NV_NCH_NOP, FALSE},
289 {'A', nv_edit, 0, 0},
290 {'B', nv_bck_word, 0, 1},
291 {'C', nv_abbrev, NV_KEEPREG, 0},
292 {'D', nv_abbrev, NV_KEEPREG, 0},
293 {'E', nv_wordcmd, 0, TRUE},
294 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
295 {'G', nv_goto, 0, TRUE},
296 {'H', nv_scroll, 0, 0},
297 {'I', nv_edit, 0, 0},
298 {'J', nv_join, 0, 0},
299 {'K', nv_ident, 0, 0},
300 {'L', nv_scroll, 0, 0},
301 {'M', nv_scroll, 0, 0},
302 {'N', nv_next, 0, SEARCH_REV},
303 {'O', nv_open, 0, 0},
304 {'P', nv_put, 0, 0},
305 {'Q', nv_exmode, NV_NCW, 0},
306 {'R', nv_Replace, 0, FALSE},
307 {'S', nv_subst, NV_KEEPREG, 0},
308 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
309 {'U', nv_Undo, 0, 0},
310 {'W', nv_wordcmd, 0, TRUE},
311 {'X', nv_abbrev, NV_KEEPREG, 0},
312 {'Y', nv_abbrev, NV_KEEPREG, 0},
313 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
314 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
315 {'\\', nv_error, 0, 0},
316 {']', nv_brackets, NV_NCH_ALW, FORWARD},
317 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
318 {'_', nv_lineop, 0, 0},
319 {'`', nv_gomark, NV_NCH_ALW, FALSE},
320 {'a', nv_edit, NV_NCH, 0},
321 {'b', nv_bck_word, 0, 0},
322 {'c', nv_operator, 0, 0},
323 {'d', nv_operator, 0, 0},
324 {'e', nv_wordcmd, 0, FALSE},
325 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
326 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
327 {'h', nv_left, NV_RL, 0},
328 {'i', nv_edit, NV_NCH, 0},
329 {'j', nv_down, 0, FALSE},
330 {'k', nv_up, 0, FALSE},
331 {'l', nv_right, NV_RL, 0},
332 {'m', nv_mark, NV_NCH_NOP, 0},
333 {'n', nv_next, 0, 0},
334 {'o', nv_open, 0, 0},
335 {'p', nv_put, 0, 0},
336 {'q', nv_record, NV_NCH, 0},
337 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
338 {'s', nv_subst, NV_KEEPREG, 0},
339 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
340 {'u', nv_undo, 0, 0},
341 {'w', nv_wordcmd, 0, FALSE},
342 {'x', nv_abbrev, NV_KEEPREG, 0},
343 {'y', nv_operator, 0, 0},
344 {'z', nv_zet, NV_NCH_ALW, 0},
345 {'{', nv_findpar, 0, BACKWARD},
346 {'|', nv_pipe, 0, 0},
347 {'}', nv_findpar, 0, FORWARD},
348 {'~', nv_tilde, 0, 0},
349
350 /* pound sign */
351 {POUND, nv_ident, 0, 0},
352#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200353 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
354 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
355 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
356 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {K_LEFTMOUSE, nv_mouse, 0, 0},
358 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
359 {K_LEFTDRAG, nv_mouse, 0, 0},
360 {K_LEFTRELEASE, nv_mouse, 0, 0},
361 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
418 {K_F8, farsi_fkey, 0, 0},
419 {K_F9, farsi_fkey, 0, 0},
420#endif
421#ifdef FEAT_SNIFF
422 {K_SNIFF, nv_sniff, 0, 0},
423#endif
424#ifdef FEAT_NETBEANS_INTG
425 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
426#endif
427#ifdef FEAT_DND
428 {K_DROP, nv_drop, NV_STS, 0},
429#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000430#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000431 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433};
434
435/* Number of commands in nv_cmds[]. */
436#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
437
438/* Sorted index of commands in nv_cmds[]. */
439static short nv_cmd_idx[NV_CMDS_SIZE];
440
441/* The highest index for which
442 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
443static int nv_max_linear;
444
445/*
446 * Compare functions for qsort() below, that checks the command character
447 * through the index in nv_cmd_idx[].
448 */
449 static int
450#ifdef __BORLANDC__
451_RTLENTRYF
452#endif
453nv_compare(s1, s2)
454 const void *s1;
455 const void *s2;
456{
457 int c1, c2;
458
459 /* The commands are sorted on absolute value. */
460 c1 = nv_cmds[*(const short *)s1].cmd_char;
461 c2 = nv_cmds[*(const short *)s2].cmd_char;
462 if (c1 < 0)
463 c1 = -c1;
464 if (c2 < 0)
465 c2 = -c2;
466 return c1 - c2;
467}
468
469/*
470 * Initialize the nv_cmd_idx[] table.
471 */
472 void
473init_normal_cmds()
474{
475 int i;
476
477 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000478 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 nv_cmd_idx[i] = i;
480
481 /* Sort the commands by the command character. */
482 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
483
484 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000485 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
487 break;
488 nv_max_linear = i - 1;
489}
490
491/*
492 * Search for a command in the commands table.
493 * Returns -1 for invalid command.
494 */
495 static int
496find_command(cmdchar)
497 int cmdchar;
498{
499 int i;
500 int idx;
501 int top, bot;
502 int c;
503
504#ifdef FEAT_MBYTE
505 /* A multi-byte character is never a command. */
506 if (cmdchar >= 0x100)
507 return -1;
508#endif
509
510 /* We use the absolute value of the character. Special keys have a
511 * negative value, but are sorted on their absolute value. */
512 if (cmdchar < 0)
513 cmdchar = -cmdchar;
514
515 /* If the character is in the first part: The character is the index into
516 * nv_cmd_idx[]. */
517 if (cmdchar <= nv_max_linear)
518 return nv_cmd_idx[cmdchar];
519
520 /* Perform a binary search. */
521 bot = nv_max_linear + 1;
522 top = NV_CMDS_SIZE - 1;
523 idx = -1;
524 while (bot <= top)
525 {
526 i = (top + bot) / 2;
527 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
528 if (c < 0)
529 c = -c;
530 if (cmdchar == c)
531 {
532 idx = nv_cmd_idx[i];
533 break;
534 }
535 if (cmdchar > c)
536 bot = i + 1;
537 else
538 top = i - 1;
539 }
540 return idx;
541}
542
543/*
544 * Execute a command in Normal mode.
545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 void
547normal_cmd(oap, toplevel)
548 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000549 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 cmdarg_T ca; /* command arguments */
552 int c;
553 int ctrl_w = FALSE; /* got CTRL-W command */
554 int old_col = curwin->w_curswant;
555#ifdef FEAT_CMDL_INFO
556 int need_flushbuf; /* need to call out_flush() */
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 pos_T old_pos; /* cursor position before command */
559 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 static int old_mapped_len = 0;
561 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000562#ifdef FEAT_EVAL
563 int set_prevcount = FALSE;
564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
567 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000568
569 /* Use a count remembered from before entering an operator. After typing
570 * "3d" we return from normal_cmd() and come back here, the "3" is
571 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ca.opcount = opcount;
573
574#ifdef FEAT_SNIFF
575 want_sniff_request = sniff_connected;
576#endif
577
578 /*
579 * If there is an operator pending, then the command we take this time
580 * will terminate it. Finish_op tells us to finish the operation before
581 * returning this time (unless the operation was cancelled).
582 */
583#ifdef CURSOR_SHAPE
584 c = finish_op;
585#endif
586 finish_op = (oap->op_type != OP_NOP);
587#ifdef CURSOR_SHAPE
588 if (finish_op != c)
589 {
590 ui_cursor_shape(); /* may show different cursor shape */
591# ifdef FEAT_MOUSESHAPE
592 update_mouseshape(-1);
593# endif
594 }
595#endif
596
Bram Moolenaara983fe92008-07-31 20:04:27 +0000597 /* When not finishing an operator and no register name typed, reset the
598 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000602#ifdef FEAT_EVAL
603 set_prevcount = TRUE;
604#endif
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaara983fe92008-07-31 20:04:27 +0000607#ifdef FEAT_AUTOCMD
608 /* Restore counts from before receiving K_CURSORHOLD. This means after
609 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
610 * "3 * 2". */
611 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
612 {
613 ca.opcount = oap->prev_opcount;
614 ca.count0 = oap->prev_count0;
615 oap->prev_opcount = 0;
616 oap->prev_count0 = 0;
617 }
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 State = NORMAL_BUSY;
623#ifdef USE_ON_FLY_SCROLL
624 dont_scroll = FALSE; /* allow scrolling here */
625#endif
626
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100627#ifdef FEAT_EVAL
628 /* Set v:count here, when called from main() and not a stuffed
629 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100630 * when there is no count. Do set it for redo. */
631 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100632 set_vcount_ca(&ca, &set_prevcount);
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 /*
636 * Get the command character from the user.
637 */
638 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 /*
642 * If a mapping was started in Visual or Select mode, remember the length
643 * of the mapping. This is used below to not return to Insert mode for as
644 * long as the mapping is being executed.
645 */
646 if (restart_edit == 0)
647 old_mapped_len = 0;
648 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 old_mapped_len = typebuf_maplen();
651
652 if (c == NUL)
653 c = K_ZERO;
654
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 /*
656 * In Select mode, typed text replaces the selection.
657 */
658 if (VIsual_active
659 && VIsual_select
660 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
661 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000662 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
663 * 'insertmode' is set) fake a "d"elete command, Insert mode will
664 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000665 * Insert the typed character in the typeahead buffer, so that it can
666 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000667 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000668 if (restart_edit != 0)
669 c = 'd';
670 else
671 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000672 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200673 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676#ifdef FEAT_CMDL_INFO
677 need_flushbuf = add_to_showcmd(c);
678#endif
679
680getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 /*
684 * Handle a count before a command and compute ca.count0.
685 * Note that '0' is a command and not the start of a count, but it's
686 * part of a count after other digits.
687 */
688 while ( (c >= '1' && c <= '9')
689 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
690 {
691 if (c == K_DEL || c == K_KDEL)
692 {
693 ca.count0 /= 10;
694#ifdef FEAT_CMDL_INFO
695 del_from_showcmd(4); /* delete the digit and ~@% */
696#endif
697 }
698 else
699 ca.count0 = ca.count0 * 10 + (c - '0');
700 if (ca.count0 < 0) /* got too large! */
701 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000702#ifdef FEAT_EVAL
703 /* Set v:count here, when called from main() and not a stuffed
704 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100705 * right after the count. Do set it for redo. */
706 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100707 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (ctrl_w)
710 {
711 ++no_mapping;
712 ++allow_keys; /* no mapping for nchar, but keys */
713 }
714 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000715 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 --no_zero_mapping;
718 if (ctrl_w)
719 {
720 --no_mapping;
721 --allow_keys;
722 }
723#ifdef FEAT_CMDL_INFO
724 need_flushbuf |= add_to_showcmd(c);
725#endif
726 }
727
728 /*
729 * If we got CTRL-W there may be a/another count
730 */
731 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
732 {
733 ctrl_w = TRUE;
734 ca.opcount = ca.count0; /* remember first count */
735 ca.count0 = 0;
736 ++no_mapping;
737 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000738 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 --no_mapping;
741 --allow_keys;
742#ifdef FEAT_CMDL_INFO
743 need_flushbuf |= add_to_showcmd(c);
744#endif
745 goto getcount; /* jump back */
746 }
747 }
748
Bram Moolenaara983fe92008-07-31 20:04:27 +0000749#ifdef FEAT_AUTOCMD
750 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000752 /* Save the count values so that ca.opcount and ca.count0 are exactly
753 * the same when coming back here after handling K_CURSORHOLD. */
754 oap->prev_opcount = ca.opcount;
755 oap->prev_count0 = ca.count0;
756 }
757 else
758#endif
759 if (ca.opcount != 0)
760 {
761 /*
762 * If we're in the middle of an operator (including after entering a
763 * yank buffer with '"') AND we had a count before the operator, then
764 * that count overrides the current value of ca.count0.
765 * What this means effectively, is that commands like "3dw" get turned
766 * into "d3w" which makes things fall into place pretty neatly.
767 * If you give a count before AND after the operator, they are
768 * multiplied.
769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (ca.count0)
771 ca.count0 *= ca.opcount;
772 else
773 ca.count0 = ca.opcount;
774 }
775
776 /*
777 * Always remember the count. It will be set to zero (on the next call,
778 * above) when there is no pending operator.
779 * When called from main(), save the count for use by the "count" built-in
780 * variable.
781 */
782 ca.opcount = ca.count0;
783 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
784
785#ifdef FEAT_EVAL
786 /*
787 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100788 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100790 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000791 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#endif
793
794 /*
795 * Find the command character in the table of commands.
796 * For CTRL-W we already got nchar when looking for a count.
797 */
798 if (ctrl_w)
799 {
800 ca.nchar = c;
801 ca.cmdchar = Ctrl_W;
802 }
803 else
804 ca.cmdchar = c;
805 idx = find_command(ca.cmdchar);
806 if (idx < 0)
807 {
808 /* Not a known command: beep. */
809 clearopbeep(oap);
810 goto normal_end;
811 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000812
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000813 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200815 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000817 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 goto normal_end;
819 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000820#ifdef FEAT_AUTOCMD
821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
822 goto normal_end;
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
826 * In Visual/Select mode, a few keys are handled in a special way.
827 */
828 if (VIsual_active)
829 {
830 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
831 if (km_stopsel
832 && (nv_cmds[idx].cmd_flags & NV_STS)
833 && !(mod_mask & MOD_MASK_SHIFT))
834 {
835 end_visual_mode();
836 redraw_curbuf_later(INVERTED);
837 }
838
839 /* Keys that work different when 'keymodel' contains "startsel" */
840 if (km_startsel)
841 {
842 if (nv_cmds[idx].cmd_flags & NV_SS)
843 {
844 unshift_special(&ca);
845 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 if (idx < 0)
847 {
848 /* Just in case */
849 clearopbeep(oap);
850 goto normal_end;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
854 && (mod_mask & MOD_MASK_SHIFT))
855 {
856 mod_mask &= ~MOD_MASK_SHIFT;
857 }
858 }
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
863 && (nv_cmds[idx].cmd_flags & NV_RL))
864 {
865 /* Invert horizontal movements and operations. Only when typed by the
866 * user directly, not when the result of a mapping or "x" translated
867 * to "dl". */
868 switch (ca.cmdchar)
869 {
870 case 'l': ca.cmdchar = 'h'; break;
871 case K_RIGHT: ca.cmdchar = K_LEFT; break;
872 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
873 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
874 case 'h': ca.cmdchar = 'l'; break;
875 case K_LEFT: ca.cmdchar = K_RIGHT; break;
876 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
877 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
878 case '>': ca.cmdchar = '<'; break;
879 case '<': ca.cmdchar = '>'; break;
880 }
881 idx = find_command(ca.cmdchar);
882 }
883#endif
884
885 /*
886 * Get an additional character if we need one.
887 */
888 if ((nv_cmds[idx].cmd_flags & NV_NCH)
889 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
890 && oap->op_type == OP_NOP)
891 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
892 || (ca.cmdchar == 'q'
893 && oap->op_type == OP_NOP
894 && !Recording
895 && !Exec_reg)
896 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 int *cp;
900 int repl = FALSE; /* get character for replace mode */
901 int lit = FALSE; /* get extra character literally */
902 int langmap_active = FALSE; /* using :lmap mappings */
903 int lang; /* getting a text character */
904#ifdef USE_IM_CONTROL
905 int save_smd; /* saved value of p_smd */
906#endif
907
908 ++no_mapping;
909 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000910#ifdef FEAT_AUTOCMD
911 /* Don't generate a CursorHold event here, most commands can't handle
912 * it, e.g., nv_replace(), nv_csearch(). */
913 did_cursorhold = TRUE;
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (ca.cmdchar == 'g')
916 {
917 /*
918 * For 'g' get the next character now, so that we can check for
919 * "gr", "g'" and "g`".
920 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_CMDL_INFO
924 need_flushbuf |= add_to_showcmd(ca.nchar);
925#endif
926 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100927 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 cp = &ca.extra_char; /* need to get a third character */
930 if (ca.nchar != 'r')
931 lit = TRUE; /* get it literally */
932 else
933 repl = TRUE; /* get it in replace mode */
934 }
935 else
936 cp = NULL; /* no third character needed */
937 }
938 else
939 {
940 if (ca.cmdchar == 'r') /* get it in replace mode */
941 repl = TRUE;
942 cp = &ca.nchar;
943 }
944 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
945
946 /*
947 * Get a second or third character.
948 */
949 if (cp != NULL)
950 {
951#ifdef CURSOR_SHAPE
952 if (repl)
953 {
954 State = REPLACE; /* pretend Replace mode */
955 ui_cursor_shape(); /* show different cursor shape */
956 }
957#endif
958 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
959 {
960 /* Allow mappings defined with ":lmap". */
961 --no_mapping;
962 --allow_keys;
963 if (repl)
964 State = LREPLACE;
965 else
966 State = LANGMAP;
967 langmap_active = TRUE;
968 }
969#ifdef USE_IM_CONTROL
970 save_smd = p_smd;
971 p_smd = FALSE; /* Don't let the IM code show the mode here */
972 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
973 im_set_active(TRUE);
974#endif
975
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000976 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 if (langmap_active)
979 {
980 /* Undo the decrement done above */
981 ++no_mapping;
982 ++allow_keys;
983 State = NORMAL_BUSY;
984 }
985#ifdef USE_IM_CONTROL
986 if (lang)
987 {
988 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
989 im_save_status(&curbuf->b_p_iminsert);
990 im_set_active(FALSE);
991 }
992 p_smd = save_smd;
993#endif
994#ifdef CURSOR_SHAPE
995 State = NORMAL_BUSY;
996#endif
997#ifdef FEAT_CMDL_INFO
998 need_flushbuf |= add_to_showcmd(*cp);
999#endif
1000
1001 if (!lit)
1002 {
1003#ifdef FEAT_DIGRAPHS
1004 /* Typing CTRL-K gets a digraph. */
1005 if (*cp == Ctrl_K
1006 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1007 || cp == &ca.extra_char)
1008 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1009 {
1010 c = get_digraph(FALSE);
1011 if (c > 0)
1012 {
1013 *cp = c;
1014# ifdef FEAT_CMDL_INFO
1015 /* Guessing how to update showcmd here... */
1016 del_from_showcmd(3);
1017 need_flushbuf |= add_to_showcmd(*cp);
1018# endif
1019 }
1020 }
1021#endif
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /* adjust chars > 127, except after "tTfFr" commands */
1024 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_RIGHTLEFT
1026 /* adjust Hebrew mapped char */
1027 if (p_hkmap && lang && KeyTyped)
1028 *cp = hkmap(*cp);
1029# ifdef FEAT_FKMAP
1030 /* adjust Farsi mapped char */
1031 if (p_fkmap && lang && KeyTyped)
1032 *cp = fkmap(*cp);
1033# endif
1034#endif
1035 }
1036
1037 /*
1038 * When the next character is CTRL-\ a following CTRL-N means the
1039 * command is aborted and we go to Normal mode.
1040 */
1041 if (cp == &ca.extra_char
1042 && ca.nchar == Ctrl_BSL
1043 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1044 {
1045 ca.cmdchar = Ctrl_BSL;
1046 ca.nchar = ca.extra_char;
1047 idx = find_command(ca.cmdchar);
1048 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001049 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001050 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else if (*cp == Ctrl_BSL)
1052 {
1053 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1054
1055 /* There is a busy wait here when typing "f<C-\>" and then
1056 * something different from CTRL-N. Can't be avoided. */
1057 while ((c = vpeekc()) <= 0 && towait > 0L)
1058 {
1059 do_sleep(towait > 50L ? 50L : towait);
1060 towait -= 50L;
1061 }
1062 if (c > 0)
1063 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001064 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (c != Ctrl_N && c != Ctrl_G)
1066 vungetc(c);
1067 else
1068 {
1069 ca.cmdchar = Ctrl_BSL;
1070 ca.nchar = c;
1071 idx = find_command(ca.cmdchar);
1072 }
1073 }
1074 }
1075
1076#ifdef FEAT_MBYTE
1077 /* When getting a text character and the next character is a
1078 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001079 * However, don't wait for it to arrive. Also, do enable mapping,
1080 * because if it's put back with vungetc() it's too late to apply
1081 * mapping. */
1082 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 while (enc_utf8 && lang && (c = vpeekc()) > 0
1084 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1085 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001086 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (!utf_iscomposing(c))
1088 {
1089 vungetc(c); /* it wasn't, put it back */
1090 break;
1091 }
1092 else if (ca.ncharC1 == 0)
1093 ca.ncharC1 = c;
1094 else
1095 ca.ncharC2 = c;
1096 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001097 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099 }
1100 --no_mapping;
1101 --allow_keys;
1102 }
1103
1104#ifdef FEAT_CMDL_INFO
1105 /*
1106 * Flush the showcmd characters onto the screen so we can see them while
1107 * the command is being executed. Only do this when the shown command was
1108 * actually displayed, otherwise this will slow down a lot when executing
1109 * mappings.
1110 */
1111 if (need_flushbuf)
1112 out_flush();
1113#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001114#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001115 if (ca.cmdchar != K_IGNORE)
1116 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 State = NORMAL;
1120
1121 if (ca.nchar == ESC)
1122 {
1123 clearop(oap);
1124 if (restart_edit == 0 && goto_im())
1125 restart_edit = 'a';
1126 goto normal_end;
1127 }
1128
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001129 if (ca.cmdchar != K_IGNORE)
1130 {
1131 msg_didout = FALSE; /* don't scroll screen up for normal command */
1132 msg_col = 0;
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 old_pos = curwin->w_cursor; /* remember where cursor was */
1136
1137 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1138 * mode. */
1139 if (!VIsual_active && km_startsel)
1140 {
1141 if (nv_cmds[idx].cmd_flags & NV_SS)
1142 {
1143 start_selection();
1144 unshift_special(&ca);
1145 idx = find_command(ca.cmdchar);
1146 }
1147 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1148 && (mod_mask & MOD_MASK_SHIFT))
1149 {
1150 start_selection();
1151 mod_mask &= ~MOD_MASK_SHIFT;
1152 }
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 /*
1156 * Execute the command!
1157 * Call the command function found in the commands table.
1158 */
1159 ca.arg = nv_cmds[idx].cmd_arg;
1160 (nv_cmds[idx].cmd_func)(&ca);
1161
1162 /*
1163 * If we didn't start or finish an operator, reset oap->regname, unless we
1164 * need it later.
1165 */
1166 if (!finish_op
1167 && !oap->op_type
1168 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1169 {
1170 clearop(oap);
1171#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001172 {
1173 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001174
Bram Moolenaar536681b2011-05-10 16:12:45 +02001175 /* Adjust the register according to 'clipboard', so that when
1176 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001177# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001178 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001179# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001180 set_reg_var(regname);
1181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183 }
1184
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001185 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001186 * character or "z333<cr>". */
1187 if (old_mapped_len > 0)
1188 old_mapped_len = typebuf_maplen();
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 /*
1191 * If an operation is pending, handle it...
1192 */
1193 do_pending_operator(&ca, old_col, FALSE);
1194
1195 /*
1196 * Wait for a moment when a message is displayed that will be overwritten
1197 * by the mode message.
1198 * In Visual mode and with "^O" in Insert mode, a short message will be
1199 * overwritten by the mode message. Wait a bit, until a key is hit.
1200 * In Visual mode, it's more important to keep the Visual area updated
1201 * than keeping a message (e.g. from a /pat search).
1202 * Only do this if the command was typed, not from a mapping.
1203 * Don't wait when emsg_silent is non-zero.
1204 * Also wait a bit after an error message, e.g. for "^O:".
1205 * Don't redraw the screen, it would remove the message.
1206 */
1207 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001208 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 || (VIsual_active
1211 && old_pos.lnum == curwin->w_cursor.lnum
1212 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 )
1214 && (clear_cmdline
1215 || redraw_cmdline)
1216 && (msg_didout || (msg_didany && msg_scroll))
1217 && !msg_nowait
1218 && KeyTyped)
1219 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && (msg_scroll
1222 || emsg_on_display)))
1223 && oap->regname == 0
1224 && !(ca.retval & CA_COMMAND_BUSY)
1225 && stuff_empty()
1226 && typebuf_typed()
1227 && emsg_silent == 0
1228 && !did_wait_return
1229 && oap->op_type == OP_NOP)
1230 {
1231 int save_State = State;
1232
1233 /* Draw the cursor with the right shape here */
1234 if (restart_edit != 0)
1235 State = INSERT;
1236
1237 /* If need to redraw, and there is a "keep_msg", redraw before the
1238 * delay */
1239 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1240 {
1241 char_u *kmsg;
1242
1243 kmsg = keep_msg;
1244 keep_msg = NULL;
1245 /* showmode() will clear keep_msg, but we want to use it anyway */
1246 update_screen(0);
1247 /* now reset it, otherwise it's put in the history again */
1248 keep_msg = kmsg;
1249 msg_attr(kmsg, keep_msg_attr);
1250 vim_free(kmsg);
1251 }
1252 setcursor();
1253 cursor_on();
1254 out_flush();
1255 if (msg_scroll || emsg_on_display)
1256 ui_delay(1000L, TRUE); /* wait at least one second */
1257 ui_delay(3000L, FALSE); /* wait up to three seconds */
1258 State = save_State;
1259
1260 msg_scroll = FALSE;
1261 emsg_on_display = FALSE;
1262 }
1263
1264 /*
1265 * Finish up after executing a Normal mode command.
1266 */
1267normal_end:
1268
1269 msg_nowait = FALSE;
1270
1271 /* Reset finish_op, in case it was set */
1272#ifdef CURSOR_SHAPE
1273 c = finish_op;
1274#endif
1275 finish_op = FALSE;
1276#ifdef CURSOR_SHAPE
1277 /* Redraw the cursor with another shape, if we were in Operator-pending
1278 * mode or did a replace command. */
1279 if (c || ca.cmdchar == 'r')
1280 {
1281 ui_cursor_shape(); /* may show different cursor shape */
1282# ifdef FEAT_MOUSESHAPE
1283 update_mouseshape(-1);
1284# endif
1285 }
1286#endif
1287
1288#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001289 if (oap->op_type == OP_NOP && oap->regname == 0
1290# ifdef FEAT_AUTOCMD
1291 && ca.cmdchar != K_CURSORHOLD
1292# endif
1293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 clear_showcmd();
1295#endif
1296
1297 checkpcmark(); /* check if we moved since setting pcmark */
1298 vim_free(ca.searchbuf);
1299
1300#ifdef FEAT_MBYTE
1301 if (has_mbyte)
1302 mb_adjust_cursor();
1303#endif
1304
1305#ifdef FEAT_SCROLLBIND
1306 if (curwin->w_p_scb && toplevel)
1307 {
1308 validate_cursor(); /* may need to update w_leftcol */
1309 do_check_scrollbind(TRUE);
1310 }
1311#endif
1312
Bram Moolenaar860cae12010-06-05 23:22:07 +02001313#ifdef FEAT_CURSORBIND
1314 if (curwin->w_p_crb && toplevel)
1315 {
1316 validate_cursor(); /* may need to update w_leftcol */
1317 do_check_cursorbind();
1318 }
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 /*
1322 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1323 * if still inside a mapping that started in Visual mode).
1324 * May switch from Visual to Select mode after CTRL-O command.
1325 */
1326 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1328 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && !(ca.retval & CA_COMMAND_BUSY)
1330 && stuff_empty()
1331 && oap->regname == 0)
1332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 1)
1334 {
1335 VIsual_select = TRUE;
1336 showmode();
1337 restart_VIsual_select = 0;
1338 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001339 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 (void)edit(restart_edit, FALSE, 1L);
1341 }
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (restart_VIsual_select == 2)
1344 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /* Save count before an operator for next time. */
1347 opcount = ca.opcount;
1348}
1349
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001350#ifdef FEAT_EVAL
1351/*
1352 * Set v:count and v:count1 according to "cap".
1353 * Set v:prevcount only when "set_prevcount" is TRUE.
1354 */
1355 static void
1356set_vcount_ca(cap, set_prevcount)
1357 cmdarg_T *cap;
1358 int *set_prevcount;
1359{
1360 long count = cap->count0;
1361
1362 /* multiply with cap->opcount the same way as above */
1363 if (cap->opcount != 0)
1364 count = cap->opcount * (count == 0 ? 1 : count);
1365 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1366 *set_prevcount = FALSE; /* only set v:prevcount once */
1367}
1368#endif
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * Handle an operator after visual mode or when the movement is finished
1372 */
1373 void
1374do_pending_operator(cap, old_col, gui_yank)
1375 cmdarg_T *cap;
1376 int old_col;
1377 int gui_yank;
1378{
1379 oparg_T *oap = cap->oap;
1380 pos_T old_cursor;
1381 int empty_region_error;
1382 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001383#ifdef FEAT_LINEBREAK
1384 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /* The visual area is remembered for redo */
1388 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1389 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001390 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001392#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#endif
1395
1396#if defined(FEAT_CLIPBOARD)
1397 /*
1398 * Yank the visual area into the GUI selection register before we operate
1399 * on it and lose it forever.
1400 * Don't do it if a specific register was specified, so that ""x"*P works.
1401 * This could call do_pending_operator() recursively, but that's OK
1402 * because gui_yank will be TRUE for the nested call.
1403 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001404 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 && oap->op_type != OP_NOP
1406 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 && VIsual_active
1408 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 && oap->regname == 0)
1410 clip_auto_select();
1411#endif
1412 old_cursor = curwin->w_cursor;
1413
1414 /*
1415 * If an operation is pending, handle it...
1416 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001417 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001419#ifdef FEAT_LINEBREAK
1420 /* Avoid a problem with unwanted linebreaks in block mode. */
1421 curwin->w_p_lbr = FALSE;
1422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 oap->is_VIsual = VIsual_active;
1424 if (oap->motion_force == 'V')
1425 oap->motion_type = MLINE;
1426 else if (oap->motion_force == 'v')
1427 {
1428 /* If the motion was linewise, "inclusive" will not have been set.
1429 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1430 if (oap->motion_type == MLINE)
1431 oap->inclusive = FALSE;
1432 /* If the motion already was characterwise, toggle "inclusive" */
1433 else if (oap->motion_type == MCHAR)
1434 oap->inclusive = !oap->inclusive;
1435 oap->motion_type = MCHAR;
1436 }
1437 else if (oap->motion_force == Ctrl_V)
1438 {
1439 /* Change line- or characterwise motion into Visual block mode. */
1440 VIsual_active = TRUE;
1441 VIsual = oap->start;
1442 VIsual_mode = Ctrl_V;
1443 VIsual_select = FALSE;
1444 VIsual_reselect = FALSE;
1445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar7afea822013-04-24 18:34:45 +02001447 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1448 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001450 && ((!VIsual_active || oap->motion_force)
1451 /* Also redo Operator-pending Visual mode mappings */
1452 || (VIsual_active && cap->cmdchar == ':'
1453 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001454 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef FEAT_FOLDING
1456 && oap->op_type != OP_FOLD
1457 && oap->op_type != OP_FOLDOPEN
1458 && oap->op_type != OP_FOLDOPENREC
1459 && oap->op_type != OP_FOLDCLOSE
1460 && oap->op_type != OP_FOLDCLOSEREC
1461 && oap->op_type != OP_FOLDDEL
1462 && oap->op_type != OP_FOLDDELREC
1463#endif
1464 )
1465 {
1466 prep_redo(oap->regname, cap->count0,
1467 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1468 oap->motion_force, cap->cmdchar, cap->nchar);
1469 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1470 {
1471 /*
1472 * If 'cpoptions' does not contain 'r', insert the search
1473 * pattern to really repeat the same command.
1474 */
1475 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001476 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 AppendToRedobuff(NL_STR);
1478 }
1479 else if (cap->cmdchar == ':')
1480 {
1481 /* do_cmdline() has stored the first typed line in
1482 * "repeat_cmdline". When several lines are typed repeating
1483 * won't be possible. */
1484 if (repeat_cmdline == NULL)
1485 ResetRedobuff();
1486 else
1487 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001488 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 AppendToRedobuff(NL_STR);
1490 vim_free(repeat_cmdline);
1491 repeat_cmdline = NULL;
1492 }
1493 }
1494 }
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (redo_VIsual_busy)
1497 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001498 /* Redo of an operation on a Visual area. Use the same size from
1499 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 oap->start = curwin->w_cursor;
1501 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1502 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1503 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1504 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001505 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001507 if (VIsual_mode == 'v')
1508 {
1509 if (redo_VIsual_line_count <= 1)
1510 {
1511 validate_virtcol();
1512 curwin->w_curswant =
1513 curwin->w_virtcol + redo_VIsual_vcol - 1;
1514 }
1515 else
1516 curwin->w_curswant = redo_VIsual_vcol;
1517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001519 {
1520 curwin->w_curswant = MAXCOL;
1521 }
1522 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 cap->count0 = redo_VIsual_count;
1525 if (redo_VIsual_count != 0)
1526 cap->count1 = redo_VIsual_count;
1527 else
1528 cap->count1 = 1;
1529 }
1530 else if (VIsual_active)
1531 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001532 if (!gui_yank)
1533 {
1534 /* Save the current VIsual area for '< and '> marks, and "gv" */
1535 curbuf->b_visual.vi_start = VIsual;
1536 curbuf->b_visual.vi_end = curwin->w_cursor;
1537 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001538 if (VIsual_mode_orig != NUL)
1539 {
1540 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1541 VIsual_mode_orig = NUL;
1542 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001543 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001545 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001550 * characterwise selection.
1551 * Special case: gH<Del> deletes the last line. */
1552 if (VIsual_select && VIsual_mode == 'V'
1553 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 if (lt(VIsual, curwin->w_cursor))
1556 {
1557 VIsual.col = 0;
1558 curwin->w_cursor.col =
1559 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1560 }
1561 else
1562 {
1563 curwin->w_cursor.col = 0;
1564 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1565 }
1566 VIsual_mode = 'v';
1567 }
1568 /* If 'selection' is "exclusive", backup one character for
1569 * charwise selections. */
1570 else if (VIsual_mode == 'v')
1571 {
1572# ifdef FEAT_VIRTUALEDIT
1573 include_line_break =
1574# endif
1575 unadjust_for_sel();
1576 }
1577
1578 oap->start = VIsual;
1579 if (VIsual_mode == 'V')
1580 oap->start.col = 0;
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 /*
1584 * Set oap->start to the first position of the operated text, oap->end
1585 * to the end of the operated text. w_cursor is equal to oap->start.
1586 */
1587 if (lt(oap->start, curwin->w_cursor))
1588 {
1589#ifdef FEAT_FOLDING
1590 /* Include folded lines completely. */
1591 if (!VIsual_active)
1592 {
1593 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1594 oap->start.col = 0;
1595 if (hasFolding(curwin->w_cursor.lnum, NULL,
1596 &curwin->w_cursor.lnum))
1597 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1598 }
1599#endif
1600 oap->end = curwin->w_cursor;
1601 curwin->w_cursor = oap->start;
1602
1603 /* w_virtcol may have been updated; if the cursor goes back to its
1604 * previous position w_virtcol becomes invalid and isn't updated
1605 * automatically. */
1606 curwin->w_valid &= ~VALID_VIRTCOL;
1607 }
1608 else
1609 {
1610#ifdef FEAT_FOLDING
1611 /* Include folded lines completely. */
1612 if (!VIsual_active && oap->motion_type == MLINE)
1613 {
1614 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1615 NULL))
1616 curwin->w_cursor.col = 0;
1617 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1618 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1619 }
1620#endif
1621 oap->end = oap->start;
1622 oap->start = curwin->w_cursor;
1623 }
1624
1625 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1626
1627#ifdef FEAT_VIRTUALEDIT
1628 /* Set "virtual_op" before resetting VIsual_active. */
1629 virtual_op = virtual_active();
1630#endif
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 if (VIsual_active || redo_VIsual_busy)
1633 {
1634 if (VIsual_mode == Ctrl_V) /* block mode */
1635 {
1636 colnr_T start, end;
1637
1638 oap->block_mode = TRUE;
1639
1640 getvvcol(curwin, &(oap->start),
1641 &oap->start_vcol, NULL, &oap->end_vcol);
1642 if (!redo_VIsual_busy)
1643 {
1644 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1645
1646 if (start < oap->start_vcol)
1647 oap->start_vcol = start;
1648 if (end > oap->end_vcol)
1649 {
1650 if (*p_sel == 'e' && start >= 1
1651 && start - 1 >= oap->end_vcol)
1652 oap->end_vcol = start - 1;
1653 else
1654 oap->end_vcol = end;
1655 }
1656 }
1657
1658 /* if '$' was used, get oap->end_vcol from longest line */
1659 if (curwin->w_curswant == MAXCOL)
1660 {
1661 curwin->w_cursor.col = MAXCOL;
1662 oap->end_vcol = 0;
1663 for (curwin->w_cursor.lnum = oap->start.lnum;
1664 curwin->w_cursor.lnum <= oap->end.lnum;
1665 ++curwin->w_cursor.lnum)
1666 {
1667 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1668 if (end > oap->end_vcol)
1669 oap->end_vcol = end;
1670 }
1671 }
1672 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001673 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 /*
1675 * Correct oap->end.col and oap->start.col to be the
1676 * upper-left and lower-right corner of the block area.
1677 *
1678 * (Actually, this does convert column positions into character
1679 * positions)
1680 */
1681 curwin->w_cursor.lnum = oap->end.lnum;
1682 coladvance(oap->end_vcol);
1683 oap->end = curwin->w_cursor;
1684
1685 curwin->w_cursor = oap->start;
1686 coladvance(oap->start_vcol);
1687 oap->start = curwin->w_cursor;
1688 }
1689
1690 if (!redo_VIsual_busy && !gui_yank)
1691 {
1692 /*
1693 * Prepare to reselect and redo Visual: this is based on the
1694 * size of the Visual text
1695 */
1696 resel_VIsual_mode = VIsual_mode;
1697 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001698 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001700 {
1701 if (VIsual_mode != Ctrl_V)
1702 getvvcol(curwin, &(oap->end),
1703 NULL, NULL, &oap->end_vcol);
1704 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1705 {
1706 if (VIsual_mode != Ctrl_V)
1707 getvvcol(curwin, &(oap->start),
1708 &oap->start_vcol, NULL, NULL);
1709 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1710 }
1711 else
1712 resel_VIsual_vcol = oap->end_vcol;
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 resel_VIsual_line_count = oap->line_count;
1715 }
1716
1717 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1718 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1719 && oap->op_type != OP_COLON
1720#ifdef FEAT_FOLDING
1721 && oap->op_type != OP_FOLD
1722 && oap->op_type != OP_FOLDOPEN
1723 && oap->op_type != OP_FOLDOPENREC
1724 && oap->op_type != OP_FOLDCLOSE
1725 && oap->op_type != OP_FOLDCLOSEREC
1726 && oap->op_type != OP_FOLDDEL
1727 && oap->op_type != OP_FOLDDELREC
1728#endif
1729 && oap->motion_force == NUL
1730 )
1731 {
1732 /* Prepare for redoing. Only use the nchar field for "r",
1733 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001734 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1735 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001736 prep_redo(oap->regname, cap->count0,
1737 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1738 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001739 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001740 prep_redo(oap->regname, 0L, NUL, 'v',
1741 get_op_char(oap->op_type),
1742 get_extra_op_char(oap->op_type),
1743 oap->op_type == OP_REPLACE
1744 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (!redo_VIsual_busy)
1746 {
1747 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001748 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 redo_VIsual_line_count = resel_VIsual_line_count;
1750 redo_VIsual_count = cap->count0;
1751 }
1752 }
1753
1754 /*
1755 * oap->inclusive defaults to TRUE.
1756 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1757 * FALSE. This makes "d}P" and "v}dP" work the same.
1758 */
1759 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1760 oap->inclusive = TRUE;
1761 if (VIsual_mode == 'V')
1762 oap->motion_type = MLINE;
1763 else
1764 {
1765 oap->motion_type = MCHAR;
1766 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001767#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 )
1771 {
1772 oap->inclusive = FALSE;
1773 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001774 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001775 if (*p_sel != 'o'
1776 && !op_on_lines(oap->op_type)
1777 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001779 ++oap->end.lnum;
1780 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001781#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001782 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001783#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001784 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 }
1786 }
1787 }
1788
1789 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 /*
1792 * Switch Visual off now, so screen updating does
1793 * not show inverted text when the screen is redrawn.
1794 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1795 * no screen redraw, so it is done here to remove the inverted
1796 * part.
1797 */
1798 if (!gui_yank)
1799 {
1800 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001801#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 setmouse();
1803 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001804#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001805 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 clear_cmdline = TRUE; /* unshow visual mode later */
1807#ifdef FEAT_CMDL_INFO
1808 else
1809 clear_showcmd();
1810#endif
1811 if ((oap->op_type == OP_YANK
1812 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001813 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 || oap->op_type == OP_FILTER)
1815 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001816 {
1817#ifdef FEAT_LINEBREAK
1818 /* make sure redrawing is correct */
1819 curwin->w_p_lbr = lbr_saved;
1820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 }
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826#ifdef FEAT_MBYTE
1827 /* Include the trailing byte of a multi-byte char. */
1828 if (has_mbyte && oap->inclusive)
1829 {
1830 int l;
1831
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001832 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 if (l > 1)
1834 oap->end.col += l - 1;
1835 }
1836#endif
1837 curwin->w_set_curswant = TRUE;
1838
1839 /*
1840 * oap->empty is set when start and end are the same. The inclusive
1841 * flag affects this too, unless yanking and the end is on a NUL.
1842 */
1843 oap->empty = (oap->motion_type == MCHAR
1844 && (!oap->inclusive
1845 || (oap->op_type == OP_YANK
1846 && gchar_pos(&oap->end) == NUL))
1847 && equalpos(oap->start, oap->end)
1848#ifdef FEAT_VIRTUALEDIT
1849 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1850#endif
1851 );
1852 /*
1853 * For delete, change and yank, it's an error to operate on an
1854 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1855 */
1856 empty_region_error = (oap->empty
1857 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 /* Force a redraw when operating on an empty Visual region, when
1860 * 'modifiable is off or creating a fold. */
1861 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001862#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001866 {
1867#ifdef FEAT_LINEBREAK
1868 curwin->w_p_lbr = lbr_saved;
1869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 /*
1874 * If the end of an operator is in column one while oap->motion_type
1875 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1876 * character in the previous line. If op_start is on or before the
1877 * first non-blank in the line, the operator becomes linewise
1878 * (strange, but that's the way vi does it).
1879 */
1880 if ( oap->motion_type == MCHAR
1881 && oap->inclusive == FALSE
1882 && !(cap->retval & CA_NO_ADJ_OP_END)
1883 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001885 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 && oap->line_count > 1)
1887 {
1888 oap->end_adjusted = TRUE; /* remember that we did this */
1889 --oap->line_count;
1890 --oap->end.lnum;
1891 if (inindent(0))
1892 oap->motion_type = MLINE;
1893 else
1894 {
1895 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1896 if (oap->end.col)
1897 {
1898 --oap->end.col;
1899 oap->inclusive = TRUE;
1900 }
1901 }
1902 }
1903 else
1904 oap->end_adjusted = FALSE;
1905
1906 switch (oap->op_type)
1907 {
1908 case OP_LSHIFT:
1909 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001910 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 auto_format(FALSE, TRUE);
1912 break;
1913
1914 case OP_JOIN_NS:
1915 case OP_JOIN:
1916 if (oap->line_count < 2)
1917 oap->line_count = 2;
1918 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1919 curbuf->b_ml.ml_line_count)
1920 beep_flush();
1921 else
1922 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001923 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001924 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 auto_format(FALSE, TRUE);
1926 }
1927 break;
1928
1929 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001934 CancelRedo();
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 else
1937 {
1938 (void)op_delete(oap);
1939 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1940 u_save_cursor(); /* cursor line wasn't saved yet */
1941 auto_format(FALSE, TRUE);
1942 }
1943 break;
1944
1945 case OP_YANK:
1946 if (empty_region_error)
1947 {
1948 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001951 CancelRedo();
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001955 {
1956#ifdef FEAT_LINEBREAK
1957 curwin->w_p_lbr = lbr_saved;
1958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 check_cursor_col();
1962 break;
1963
1964 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001969 CancelRedo();
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 else
1972 {
1973 /* This is a new edit command, not a restart. Need to
1974 * remember it to make 'insertmode' work with mappings for
1975 * Visual mode. But do this only once and not when typed and
1976 * 'insertmode' isn't set. */
1977 if (p_im || !KeyTyped)
1978 restart_edit_save = restart_edit;
1979 else
1980 restart_edit_save = 0;
1981 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001982#ifdef FEAT_LINEBREAK
1983 /* Restore linebreak, so that when the user edits it looks as
1984 * before. */
1985 curwin->w_p_lbr = lbr_saved;
1986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 /* Reset finish_op now, don't want it set inside edit(). */
1988 finish_op = FALSE;
1989 if (op_change(oap)) /* will call edit() */
1990 cap->retval |= CA_COMMAND_BUSY;
1991 if (restart_edit == 0)
1992 restart_edit = restart_edit_save;
1993 }
1994 break;
1995
1996 case OP_FILTER:
1997 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1998 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1999 else
2000 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2001
2002 case OP_INDENT:
2003 case OP_COLON:
2004
2005#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2006 /*
2007 * If 'equalprg' is empty, do the indenting internally.
2008 */
2009 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2010 {
2011# ifdef FEAT_LISP
2012 if (curbuf->b_p_lisp)
2013 {
2014 op_reindent(oap, get_lisp_indent);
2015 break;
2016 }
2017# endif
2018# ifdef FEAT_CINDENT
2019 op_reindent(oap,
2020# ifdef FEAT_EVAL
2021 *curbuf->b_p_inde != NUL ? get_expr_indent :
2022# endif
2023 get_c_indent);
2024 break;
2025# endif
2026 }
2027#endif
2028
2029 op_colon(oap);
2030 break;
2031
2032 case OP_TILDE:
2033 case OP_UPPER:
2034 case OP_LOWER:
2035 case OP_ROT13:
2036 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002039 CancelRedo();
2040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 else
2042 op_tilde(oap);
2043 check_cursor_col();
2044 break;
2045
2046 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002047#if defined(FEAT_EVAL)
2048 if (*curbuf->b_p_fex != NUL)
2049 op_formatexpr(oap); /* use expression */
2050 else
2051#endif
2052 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 op_colon(oap); /* use external command */
2054 else
2055 op_format(oap, FALSE); /* use internal function */
2056 break;
2057
2058 case OP_FORMAT2:
2059 op_format(oap, TRUE); /* use internal function */
2060 break;
2061
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002062 case OP_FUNCTION:
2063 op_function(oap); /* call 'operatorfunc' */
2064 break;
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 case OP_INSERT:
2067 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#ifdef FEAT_VISUALEXTRA
2070 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002073 CancelRedo();
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else
2076 {
2077 /* This is a new edit command, not a restart. Need to
2078 * remember it to make 'insertmode' work with mappings for
2079 * Visual mode. But do this only once. */
2080 restart_edit_save = restart_edit;
2081 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002082#ifdef FEAT_LINEBREAK
2083 /* Restore linebreak, so that when the user edits it looks as
2084 * before. */
2085 curwin->w_p_lbr = lbr_saved;
2086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002088#ifdef FEAT_LINEBREAK
2089 /* Reset linebreak, so that formatting works correctly. */
2090 curwin->w_p_lbr = FALSE;
2091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093 /* TODO: when inserting in several lines, should format all
2094 * the lines. */
2095 auto_format(FALSE, TRUE);
2096
2097 if (restart_edit == 0)
2098 restart_edit = restart_edit_save;
2099 }
2100#else
2101 vim_beep();
2102#endif
2103 break;
2104
2105 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107#ifdef FEAT_VISUALEXTRA
2108 if (empty_region_error)
2109#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002112 CancelRedo();
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef FEAT_VISUALEXTRA
2115 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002116 {
2117#ifdef FEAT_LINEBREAK
2118 /* Restore linebreak, so that when the user edits it looks as
2119 * before. */
2120 curwin->w_p_lbr = lbr_saved;
2121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#endif
2125 break;
2126
2127#ifdef FEAT_FOLDING
2128 case OP_FOLD:
2129 VIsual_reselect = FALSE; /* don't reselect now */
2130 foldCreate(oap->start.lnum, oap->end.lnum);
2131 break;
2132
2133 case OP_FOLDOPEN:
2134 case OP_FOLDOPENREC:
2135 case OP_FOLDCLOSE:
2136 case OP_FOLDCLOSEREC:
2137 VIsual_reselect = FALSE; /* don't reselect now */
2138 opFoldRange(oap->start.lnum, oap->end.lnum,
2139 oap->op_type == OP_FOLDOPEN
2140 || oap->op_type == OP_FOLDOPENREC,
2141 oap->op_type == OP_FOLDOPENREC
2142 || oap->op_type == OP_FOLDCLOSEREC,
2143 oap->is_VIsual);
2144 break;
2145
2146 case OP_FOLDDEL:
2147 case OP_FOLDDELREC:
2148 VIsual_reselect = FALSE; /* don't reselect now */
2149 deleteFold(oap->start.lnum, oap->end.lnum,
2150 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2151 break;
2152#endif
2153 default:
2154 clearopbeep(oap);
2155 }
2156#ifdef FEAT_VIRTUALEDIT
2157 virtual_op = MAYBE;
2158#endif
2159 if (!gui_yank)
2160 {
2161 /*
2162 * if 'sol' not set, go back to old column for some commands
2163 */
2164 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2165 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2166 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002167 {
2168#ifdef FEAT_LINEBREAK
2169 curwin->w_p_lbr = FALSE;
2170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
2174 else
2175 {
2176 curwin->w_cursor = old_cursor;
2177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 clearop(oap);
2180 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002181#ifdef FEAT_LINEBREAK
2182 curwin->w_p_lbr = lbr_saved;
2183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184}
2185
2186/*
2187 * Handle indent and format operators and visual mode ":".
2188 */
2189 static void
2190op_colon(oap)
2191 oparg_T *oap;
2192{
2193 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (oap->is_VIsual)
2195 stuffReadbuff((char_u *)"'<,'>");
2196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
2198 /*
2199 * Make the range look nice, so it can be repeated.
2200 */
2201 if (oap->start.lnum == curwin->w_cursor.lnum)
2202 stuffcharReadbuff('.');
2203 else
2204 stuffnumReadbuff((long)oap->start.lnum);
2205 if (oap->end.lnum != oap->start.lnum)
2206 {
2207 stuffcharReadbuff(',');
2208 if (oap->end.lnum == curwin->w_cursor.lnum)
2209 stuffcharReadbuff('.');
2210 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2211 stuffcharReadbuff('$');
2212 else if (oap->start.lnum == curwin->w_cursor.lnum)
2213 {
2214 stuffReadbuff((char_u *)".+");
2215 stuffnumReadbuff((long)oap->line_count - 1);
2216 }
2217 else
2218 stuffnumReadbuff((long)oap->end.lnum);
2219 }
2220 }
2221 if (oap->op_type != OP_COLON)
2222 stuffReadbuff((char_u *)"!");
2223 if (oap->op_type == OP_INDENT)
2224 {
2225#ifndef FEAT_CINDENT
2226 if (*get_equalprg() == NUL)
2227 stuffReadbuff((char_u *)"indent");
2228 else
2229#endif
2230 stuffReadbuff(get_equalprg());
2231 stuffReadbuff((char_u *)"\n");
2232 }
2233 else if (oap->op_type == OP_FORMAT)
2234 {
2235 if (*p_fp == NUL)
2236 stuffReadbuff((char_u *)"fmt");
2237 else
2238 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002239 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241
2242 /*
2243 * do_cmdline() does the rest
2244 */
2245}
2246
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002247/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002248 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002249 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002250 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002251op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002252 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002253{
2254#ifdef FEAT_EVAL
2255 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002256# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002257 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002258# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002259
2260 if (*p_opfunc == NUL)
2261 EMSG(_("E774: 'operatorfunc' is empty"));
2262 else
2263 {
2264 /* Set '[ and '] marks to text to be operated on. */
2265 curbuf->b_op_start = oap->start;
2266 curbuf->b_op_end = oap->end;
2267 if (oap->motion_type != MLINE && !oap->inclusive)
2268 /* Exclude the end position. */
2269 decl(&curbuf->b_op_end);
2270
2271 if (oap->block_mode)
2272 argv[0] = (char_u *)"block";
2273 else if (oap->motion_type == MLINE)
2274 argv[0] = (char_u *)"line";
2275 else
2276 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002277
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002278# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002279 /* Reset virtual_op so that 'virtualedit' can be changed in the
2280 * function. */
2281 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002282# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002283
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002284 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002285
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002286# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002287 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002288# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002289 }
2290#else
2291 EMSG(_("E775: Eval feature not available"));
2292#endif
2293}
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295#if defined(FEAT_MOUSE) || defined(PROTO)
2296/*
2297 * Do the appropriate action for the current mouse click in the current mode.
2298 * Not used for Command-line mode.
2299 *
2300 * Normal Mode:
2301 * event modi- position visual change action
2302 * fier cursor window
2303 * left press - yes end yes
2304 * left press C yes end yes "^]" (2)
2305 * left press S yes end yes "*" (2)
2306 * left drag - yes start if moved no
2307 * left relse - yes start if moved no
2308 * middle press - yes if not active no put register
2309 * middle press - yes if active no yank and put
2310 * right press - yes start or extend yes
2311 * right press S yes no change yes "#" (2)
2312 * right drag - yes extend no
2313 * right relse - yes extend no
2314 *
2315 * Insert or Replace Mode:
2316 * event modi- position visual change action
2317 * fier cursor window
2318 * left press - yes (cannot be active) yes
2319 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2320 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2321 * left drag - yes start or extend (1) no CTRL-O (1)
2322 * left relse - yes start or extend (1) no CTRL-O (1)
2323 * middle press - no (cannot be active) no put register
2324 * right press - yes start or extend yes CTRL-O
2325 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2326 *
2327 * (1) only if mouse pointer moved since press
2328 * (2) only if click is in same buffer
2329 *
2330 * Return TRUE if start_arrow() should be called for edit mode.
2331 */
2332 int
2333do_mouse(oap, c, dir, count, fixindent)
2334 oparg_T *oap; /* operator argument, can be NULL */
2335 int c; /* K_LEFTMOUSE, etc */
2336 int dir; /* Direction to 'put' if necessary */
2337 long count;
2338 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2339{
2340 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2341 static int got_click = FALSE; /* got a click some time back */
2342
2343 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2344 int is_click; /* If FALSE it's a drag or release event */
2345 int is_drag; /* If TRUE it's a drag event */
2346 int jump_flags = 0; /* flags for jump_to_mouse() */
2347 pos_T start_visual;
2348 int moved; /* Has cursor moved? */
2349 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002350#ifdef FEAT_WINDOWS
2351 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_VERTSPLIT
2354 int in_sep_line; /* mouse in vertical separator line */
2355#endif
2356 int c1, c2;
2357#if defined(FEAT_FOLDING)
2358 pos_T save_cursor;
2359#endif
2360 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 static pos_T orig_cursor;
2362 colnr_T leftcol, rightcol;
2363 pos_T end_visual;
2364 int diff;
2365 int old_active = VIsual_active;
2366 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 int regname;
2368
2369#if defined(FEAT_FOLDING)
2370 save_cursor = curwin->w_cursor;
2371#endif
2372
2373 /*
2374 * When GUI is active, always recognize mouse events, otherwise:
2375 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2376 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2377 * - For command line and insert mode 'mouse' is checked before calling
2378 * do_mouse().
2379 */
2380 if (do_always)
2381 do_always = FALSE;
2382 else
2383#ifdef FEAT_GUI
2384 if (!gui.in_use)
2385#endif
2386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (VIsual_active)
2388 {
2389 if (!mouse_has(MOUSE_VISUAL))
2390 return FALSE;
2391 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002392 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 return FALSE;
2394 }
2395
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002396 for (;;)
2397 {
2398 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2399 if (is_drag)
2400 {
2401 /* If the next character is the same mouse event then use that
2402 * one. Speeds up dragging the status line. */
2403 if (vpeekc() != NUL)
2404 {
2405 int nc;
2406 int save_mouse_row = mouse_row;
2407 int save_mouse_col = mouse_col;
2408
2409 /* Need to get the character, peeking doesn't get the actual
2410 * one. */
2411 nc = safe_vgetc();
2412 if (c == nc)
2413 continue;
2414 vungetc(nc);
2415 mouse_row = save_mouse_row;
2416 mouse_col = save_mouse_col;
2417 }
2418 }
2419 break;
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421
2422#ifdef FEAT_MOUSESHAPE
2423 /* May have stopped dragging the status or separator line. The pointer is
2424 * most likely still on the status or separator line. */
2425 if (!is_drag && drag_status_line)
2426 {
2427 drag_status_line = FALSE;
2428 update_mouseshape(SHAPE_IDX_STATUS);
2429 }
2430# ifdef FEAT_VERTSPLIT
2431 if (!is_drag && drag_sep_line)
2432 {
2433 drag_sep_line = FALSE;
2434 update_mouseshape(SHAPE_IDX_VSEP);
2435 }
2436# endif
2437#endif
2438
2439 /*
2440 * Ignore drag and release events if we didn't get a click.
2441 */
2442 if (is_click)
2443 got_click = TRUE;
2444 else
2445 {
2446 if (!got_click) /* didn't get click, ignore */
2447 return FALSE;
2448 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002451#ifdef FEAT_WINDOWS
2452 if (in_tab_line)
2453 {
2454 in_tab_line = FALSE;
2455 return FALSE;
2456 }
2457#endif
2458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 /*
2462 * CTRL right mouse button does CTRL-T
2463 */
2464 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2465 {
2466 if (State & INSERT)
2467 stuffcharReadbuff(Ctrl_O);
2468 if (count > 1)
2469 stuffnumReadbuff(count);
2470 stuffcharReadbuff(Ctrl_T);
2471 got_click = FALSE; /* ignore drag&release now */
2472 return FALSE;
2473 }
2474
2475 /*
2476 * CTRL only works with left mouse button
2477 */
2478 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2479 return FALSE;
2480
2481 /*
2482 * When a modifier is down, ignore drag and release events, as well as
2483 * multiple clicks and the middle mouse button.
2484 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2485 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002486 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2487 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 && (!is_click
2489 || (mod_mask & MOD_MASK_MULTI_CLICK)
2490 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002491 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 && mouse_model_popup()
2493 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002494 && !((mod_mask & MOD_MASK_ALT)
2495 && !mouse_model_popup()
2496 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 )
2498 return FALSE;
2499
2500 /*
2501 * If the button press was used as the movement command for an operator
2502 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2503 * drag/release events.
2504 */
2505 if (!is_click && which_button == MOUSE_MIDDLE)
2506 return FALSE;
2507
2508 if (oap != NULL)
2509 regname = oap->regname;
2510 else
2511 regname = 0;
2512
2513 /*
2514 * Middle mouse button does a 'put' of the selected text
2515 */
2516 if (which_button == MOUSE_MIDDLE)
2517 {
2518 if (State == NORMAL)
2519 {
2520 /*
2521 * If an operator was pending, we don't know what the user wanted
2522 * to do. Go back to normal mode: Clear the operator and beep().
2523 */
2524 if (oap != NULL && oap->op_type != OP_NOP)
2525 {
2526 clearopbeep(oap);
2527 return FALSE;
2528 }
2529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 /*
2531 * If visual was active, yank the highlighted text and put it
2532 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002533 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 */
2535 if (VIsual_active)
2536 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002537 if (VIsual_select)
2538 {
2539 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002540 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002541 }
2542 else
2543 {
2544 stuffcharReadbuff('y');
2545 stuffcharReadbuff(K_MIDDLEMOUSE);
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 do_always = TRUE; /* ignore 'mouse' setting next time */
2548 return FALSE;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 /*
2551 * The rest is below jump_to_mouse()
2552 */
2553 }
2554
2555 else if ((State & INSERT) == 0)
2556 return FALSE;
2557
2558 /*
2559 * Middle click in insert mode doesn't move the mouse, just insert the
2560 * contents of a register. '.' register is special, can't insert that
2561 * with do_put().
2562 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2563 * happens for the GUI).
2564 */
2565 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2566 {
2567 if (regname == '.')
2568 insert_reg(regname, TRUE);
2569 else
2570 {
2571#ifdef FEAT_CLIPBOARD
2572 if (clip_star.available && regname == 0)
2573 regname = '*';
2574#endif
2575 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2576 insert_reg(regname, TRUE);
2577 else
2578 {
2579 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2580
2581 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2582 AppendCharToRedobuff(Ctrl_R);
2583 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2584 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2585 }
2586 }
2587 return FALSE;
2588 }
2589 }
2590
2591 /* When dragging or button-up stay in the same window. */
2592 if (!is_click)
2593 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2594
2595 start_visual.lnum = 0;
2596
Bram Moolenaarf740b292006-02-16 22:11:02 +00002597#ifdef FEAT_WINDOWS
2598 /* Check for clicking in the tab page line. */
2599 if (mouse_row == 0 && firstwin->w_winrow > 0)
2600 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002601 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002602 {
2603 if (in_tab_line)
2604 {
2605 c1 = TabPageIdxs[mouse_col];
2606 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2607 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002608 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002609 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002610
Bram Moolenaarf740b292006-02-16 22:11:02 +00002611 /* click in a tab selects that tab page */
2612 if (is_click
2613# ifdef FEAT_CMDWIN
2614 && cmdwin_type == 0
2615# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002616 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002618 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619 c1 = TabPageIdxs[mouse_col];
2620 if (c1 >= 0)
2621 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002622 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2623 {
2624 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002626 tabpage_new();
2627 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2628 }
2629 else
2630 {
2631 /* Go to specified tab page, or next one if not clicking
2632 * on a label. */
2633 goto_tabpage(c1);
2634
2635 /* It's like clicking on the status line of a window. */
2636 if (curwin != old_curwin)
2637 end_visual_mode();
2638 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002639 }
2640 else if (c1 < 0)
2641 {
2642 tabpage_T *tp;
2643
2644 /* Close the current or specified tab page. */
2645 if (c1 == -999)
2646 tp = curtab;
2647 else
2648 tp = find_tabpage(-c1);
2649 if (tp == curtab)
2650 {
2651 if (first_tabpage->tp_next != NULL)
2652 tabpage_close(FALSE);
2653 }
2654 else if (tp != NULL)
2655 tabpage_close_other(tp, FALSE);
2656 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002657 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002659 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002660 else if (is_drag && in_tab_line)
2661 {
2662 c1 = TabPageIdxs[mouse_col];
2663 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2664 return FALSE;
2665 }
2666
Bram Moolenaarf740b292006-02-16 22:11:02 +00002667#endif
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 /*
2670 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2671 * right button up -> pop-up menu
2672 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002673 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 */
2675 if (mouse_model_popup())
2676 {
2677 if (which_button == MOUSE_RIGHT
2678 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2679 {
2680 /*
2681 * NOTE: Ignore right button down and drag mouse events.
2682 * Windows only shows the popup menu on the button up event.
2683 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002684#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2685 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (!is_click)
2687 return FALSE;
2688#endif
2689#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2690 if (is_click || is_drag)
2691 return FALSE;
2692#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002693#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2695 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2696 if (gui.in_use)
2697 {
2698 jump_flags = 0;
2699 if (STRCMP(p_mousem, "popup_setpos") == 0)
2700 {
2701 /* First set the cursor position before showing the popup
2702 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (VIsual_active)
2704 {
2705 pos_T m_pos;
2706
2707 /*
2708 * set MOUSE_MAY_STOP_VIS if we are outside the
2709 * selection or the current window (might have false
2710 * negative here)
2711 */
2712 if (mouse_row < W_WINROW(curwin)
2713 || mouse_row
2714 > (W_WINROW(curwin) + curwin->w_height))
2715 jump_flags = MOUSE_MAY_STOP_VIS;
2716 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2717 jump_flags = MOUSE_MAY_STOP_VIS;
2718 else
2719 {
2720 if ((lt(curwin->w_cursor, VIsual)
2721 && (lt(m_pos, curwin->w_cursor)
2722 || lt(VIsual, m_pos)))
2723 || (lt(VIsual, curwin->w_cursor)
2724 && (lt(m_pos, VIsual)
2725 || lt(curwin->w_cursor, m_pos))))
2726 {
2727 jump_flags = MOUSE_MAY_STOP_VIS;
2728 }
2729 else if (VIsual_mode == Ctrl_V)
2730 {
2731 getvcols(curwin, &curwin->w_cursor, &VIsual,
2732 &leftcol, &rightcol);
2733 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2734 if (m_pos.col < leftcol || m_pos.col > rightcol)
2735 jump_flags = MOUSE_MAY_STOP_VIS;
2736 }
2737 }
2738 }
2739 else
2740 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742 if (jump_flags)
2743 {
2744 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002745 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 setcursor();
2747 out_flush(); /* Update before showing popup menu */
2748 }
2749# ifdef FEAT_MENU
2750 gui_show_popupmenu();
2751# endif
2752 return (jump_flags & CURSOR_MOVED) != 0;
2753 }
2754 else
2755 return FALSE;
2756#else
2757 return FALSE;
2758#endif
2759 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002760 if (which_button == MOUSE_LEFT
2761 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
2763 which_button = MOUSE_RIGHT;
2764 mod_mask &= ~MOD_MASK_SHIFT;
2765 }
2766 }
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if ((State & (NORMAL | INSERT))
2769 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2770 {
2771 if (which_button == MOUSE_LEFT)
2772 {
2773 if (is_click)
2774 {
2775 /* stop Visual mode for a left click in a window, but not when
2776 * on a status line */
2777 if (VIsual_active)
2778 jump_flags |= MOUSE_MAY_STOP_VIS;
2779 }
2780 else if (mouse_has(MOUSE_VISUAL))
2781 jump_flags |= MOUSE_MAY_VIS;
2782 }
2783 else if (which_button == MOUSE_RIGHT)
2784 {
2785 if (is_click && VIsual_active)
2786 {
2787 /*
2788 * Remember the start and end of visual before moving the
2789 * cursor.
2790 */
2791 if (lt(curwin->w_cursor, VIsual))
2792 {
2793 start_visual = curwin->w_cursor;
2794 end_visual = VIsual;
2795 }
2796 else
2797 {
2798 start_visual = VIsual;
2799 end_visual = curwin->w_cursor;
2800 }
2801 }
2802 jump_flags |= MOUSE_FOCUS;
2803 if (mouse_has(MOUSE_VISUAL))
2804 jump_flags |= MOUSE_MAY_VIS;
2805 }
2806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 /*
2809 * If an operator is pending, ignore all drags and releases until the
2810 * next mouse click.
2811 */
2812 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2813 {
2814 got_click = FALSE;
2815 oap->motion_type = MCHAR;
2816 }
2817
2818 /* When releasing the button let jump_to_mouse() know. */
2819 if (!is_click && !is_drag)
2820 jump_flags |= MOUSE_RELEASED;
2821
2822 /*
2823 * JUMP!
2824 */
2825 jump_flags = jump_to_mouse(jump_flags,
2826 oap == NULL ? NULL : &(oap->inclusive), which_button);
2827 moved = (jump_flags & CURSOR_MOVED);
2828 in_status_line = (jump_flags & IN_STATUS_LINE);
2829#ifdef FEAT_VERTSPLIT
2830 in_sep_line = (jump_flags & IN_SEP_LINE);
2831#endif
2832
2833#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002834 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2836 {
2837 int key = KEY2TERMCAP1(c);
2838
2839 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2840 || key == (int)KE_RIGHTRELEASE)
2841 netbeans_button_release(which_button);
2842 }
2843#endif
2844
2845 /* When jumping to another window, clear a pending operator. That's a bit
2846 * friendlier than beeping and not jumping to that window. */
2847 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2848 clearop(oap);
2849
2850#ifdef FEAT_FOLDING
2851 if (mod_mask == 0
2852 && !is_drag
2853 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2854 && which_button == MOUSE_LEFT)
2855 {
2856 /* open or close a fold at this line */
2857 if (jump_flags & MOUSE_FOLD_OPEN)
2858 openFold(curwin->w_cursor.lnum, 1L);
2859 else
2860 closeFold(curwin->w_cursor.lnum, 1L);
2861 /* don't move the cursor if still in the same window */
2862 if (curwin == old_curwin)
2863 curwin->w_cursor = save_cursor;
2864 }
2865#endif
2866
2867#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2868 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2869 {
2870 clip_modeless(which_button, is_click, is_drag);
2871 return FALSE;
2872 }
2873#endif
2874
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002876 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (VIsual_active && is_drag && p_so)
2878 {
2879 /* In the very first line, allow scrolling one line */
2880 if (mouse_row == 0)
2881 mouse_dragging = 2;
2882 else
2883 mouse_dragging = 1;
2884 }
2885
2886 /* When dragging the mouse above the window, scroll down. */
2887 if (is_drag && mouse_row < 0 && !in_status_line)
2888 {
2889 scroll_redraw(FALSE, 1L);
2890 mouse_row = 0;
2891 }
2892
2893 if (start_visual.lnum) /* right click in visual mode */
2894 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002895 /* When ALT is pressed make Visual mode blockwise. */
2896 if (mod_mask & MOD_MASK_ALT)
2897 VIsual_mode = Ctrl_V;
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /*
2900 * In Visual-block mode, divide the area in four, pick up the corner
2901 * that is in the quarter that the cursor is in.
2902 */
2903 if (VIsual_mode == Ctrl_V)
2904 {
2905 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2906 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2907 end_visual.col = leftcol;
2908 else
2909 end_visual.col = rightcol;
2910 if (curwin->w_cursor.lnum <
2911 (start_visual.lnum + end_visual.lnum) / 2)
2912 end_visual.lnum = end_visual.lnum;
2913 else
2914 end_visual.lnum = start_visual.lnum;
2915
2916 /* move VIsual to the right column */
2917 start_visual = curwin->w_cursor; /* save the cursor pos */
2918 curwin->w_cursor = end_visual;
2919 coladvance(end_visual.col);
2920 VIsual = curwin->w_cursor;
2921 curwin->w_cursor = start_visual; /* restore the cursor */
2922 }
2923 else
2924 {
2925 /*
2926 * If the click is before the start of visual, change the start.
2927 * If the click is after the end of visual, change the end. If
2928 * the click is inside the visual, change the closest side.
2929 */
2930 if (lt(curwin->w_cursor, start_visual))
2931 VIsual = end_visual;
2932 else if (lt(end_visual, curwin->w_cursor))
2933 VIsual = start_visual;
2934 else
2935 {
2936 /* In the same line, compare column number */
2937 if (end_visual.lnum == start_visual.lnum)
2938 {
2939 if (curwin->w_cursor.col - start_visual.col >
2940 end_visual.col - curwin->w_cursor.col)
2941 VIsual = start_visual;
2942 else
2943 VIsual = end_visual;
2944 }
2945
2946 /* In different lines, compare line number */
2947 else
2948 {
2949 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2950 (end_visual.lnum - curwin->w_cursor.lnum);
2951
2952 if (diff > 0) /* closest to end */
2953 VIsual = start_visual;
2954 else if (diff < 0) /* closest to start */
2955 VIsual = end_visual;
2956 else /* in the middle line */
2957 {
2958 if (curwin->w_cursor.col <
2959 (start_visual.col + end_visual.col) / 2)
2960 VIsual = end_visual;
2961 else
2962 VIsual = start_visual;
2963 }
2964 }
2965 }
2966 }
2967 }
2968 /*
2969 * If Visual mode started in insert mode, execute "CTRL-O"
2970 */
2971 else if ((State & INSERT) && VIsual_active)
2972 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 /*
2975 * Middle mouse click: Put text before cursor.
2976 */
2977 if (which_button == MOUSE_MIDDLE)
2978 {
2979#ifdef FEAT_CLIPBOARD
2980 if (clip_star.available && regname == 0)
2981 regname = '*';
2982#endif
2983 if (yank_register_mline(regname))
2984 {
2985 if (mouse_past_bottom)
2986 dir = FORWARD;
2987 }
2988 else if (mouse_past_eol)
2989 dir = FORWARD;
2990
2991 if (fixindent)
2992 {
2993 c1 = (dir == BACKWARD) ? '[' : ']';
2994 c2 = 'p';
2995 }
2996 else
2997 {
2998 c1 = (dir == FORWARD) ? 'p' : 'P';
2999 c2 = NUL;
3000 }
3001 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3002
3003 /*
3004 * Remember where the paste started, so in edit() Insstart can be set
3005 * to this position
3006 */
3007 if (restart_edit != 0)
3008 where_paste_started = curwin->w_cursor;
3009 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3010 }
3011
3012#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3013 /*
3014 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3015 * error under the mouse pointer.
3016 */
3017 else if (((mod_mask & MOD_MASK_CTRL)
3018 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3019 && bt_quickfix(curbuf))
3020 {
3021 if (State & INSERT)
3022 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003023 if (curwin->w_llist_ref == NULL) /* quickfix window */
3024 stuffReadbuff((char_u *)":.cc\n");
3025 else /* location list window */
3026 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 got_click = FALSE; /* ignore drag&release now */
3028 }
3029#endif
3030
3031 /*
3032 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3033 * under the mouse pointer.
3034 */
3035 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3036 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3037 {
3038 if (State & INSERT)
3039 stuffcharReadbuff(Ctrl_O);
3040 stuffcharReadbuff(Ctrl_RSB);
3041 got_click = FALSE; /* ignore drag&release now */
3042 }
3043
3044 /*
3045 * Shift-Mouse click searches for the next occurrence of the word under
3046 * the mouse pointer
3047 */
3048 else if ((mod_mask & MOD_MASK_SHIFT))
3049 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003050 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 stuffcharReadbuff(Ctrl_O);
3052 if (which_button == MOUSE_LEFT)
3053 stuffcharReadbuff('*');
3054 else /* MOUSE_RIGHT */
3055 stuffcharReadbuff('#');
3056 }
3057
3058 /* Handle double clicks, unless on status line */
3059 else if (in_status_line)
3060 {
3061#ifdef FEAT_MOUSESHAPE
3062 if ((is_drag || is_click) && !drag_status_line)
3063 {
3064 drag_status_line = TRUE;
3065 update_mouseshape(-1);
3066 }
3067#endif
3068 }
3069#ifdef FEAT_VERTSPLIT
3070 else if (in_sep_line)
3071 {
3072# ifdef FEAT_MOUSESHAPE
3073 if ((is_drag || is_click) && !drag_sep_line)
3074 {
3075 drag_sep_line = TRUE;
3076 update_mouseshape(-1);
3077 }
3078# endif
3079 }
3080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3082 && mouse_has(MOUSE_VISUAL))
3083 {
3084 if (is_click || !VIsual_active)
3085 {
3086 if (VIsual_active)
3087 orig_cursor = VIsual;
3088 else
3089 {
3090 check_visual_highlight();
3091 VIsual = curwin->w_cursor;
3092 orig_cursor = VIsual;
3093 VIsual_active = TRUE;
3094 VIsual_reselect = TRUE;
3095 /* start Select mode if 'selectmode' contains "mouse" */
3096 may_start_select('o');
3097 setmouse();
3098 }
3099 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003100 {
3101 /* Double click with ALT pressed makes it blockwise. */
3102 if (mod_mask & MOD_MASK_ALT)
3103 VIsual_mode = Ctrl_V;
3104 else
3105 VIsual_mode = 'v';
3106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3108 VIsual_mode = 'V';
3109 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3110 VIsual_mode = Ctrl_V;
3111#ifdef FEAT_CLIPBOARD
3112 /* Make sure the clipboard gets updated. Needed because start and
3113 * end may still be the same, and the selection needs to be owned */
3114 clip_star.vmode = NUL;
3115#endif
3116 }
3117 /*
3118 * A double click selects a word or a block.
3119 */
3120 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3121 {
3122 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003123 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 if (is_click)
3126 {
3127 /* If the character under the cursor (skipping white space) is
3128 * not a word character, try finding a match and select a (),
3129 * {}, [], #if/#endif, etc. block. */
3130 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003131 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 inc(&end_visual);
3133 if (oap != NULL)
3134 oap->motion_type = MCHAR;
3135 if (oap != NULL
3136 && VIsual_mode == 'v'
3137 && !vim_iswordc(gchar_pos(&end_visual))
3138 && equalpos(curwin->w_cursor, VIsual)
3139 && (pos = findmatch(oap, NUL)) != NULL)
3140 {
3141 curwin->w_cursor = *pos;
3142 if (oap->motion_type == MLINE)
3143 VIsual_mode = 'V';
3144 else if (*p_sel == 'e')
3145 {
3146 if (lt(curwin->w_cursor, VIsual))
3147 ++VIsual.col;
3148 else
3149 ++curwin->w_cursor.col;
3150 }
3151 }
3152 }
3153
3154 if (pos == NULL && (is_click || is_drag))
3155 {
3156 /* When not found a match or when dragging: extend to include
3157 * a word. */
3158 if (lt(curwin->w_cursor, orig_cursor))
3159 {
3160 find_start_of_word(&curwin->w_cursor);
3161 find_end_of_word(&VIsual);
3162 }
3163 else
3164 {
3165 find_start_of_word(&VIsual);
3166 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3167#ifdef FEAT_MBYTE
3168 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003169 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#else
3171 ++curwin->w_cursor.col;
3172#endif
3173 find_end_of_word(&curwin->w_cursor);
3174 }
3175 }
3176 curwin->w_set_curswant = TRUE;
3177 }
3178 if (is_click)
3179 redraw_curbuf_later(INVERTED); /* update the inversion */
3180 }
3181 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003182 {
3183 if (mod_mask & MOD_MASK_ALT)
3184 VIsual_mode = Ctrl_V;
3185 else
3186 VIsual_mode = 'v';
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003190 if ((!VIsual_active && old_active && mode_displayed)
3191 || (VIsual_active && p_smd && msg_silent == 0
3192 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 return moved;
3196}
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198/*
3199 * Move "pos" back to the start of the word it's in.
3200 */
3201 static void
3202find_start_of_word(pos)
3203 pos_T *pos;
3204{
3205 char_u *line;
3206 int cclass;
3207 int col;
3208
3209 line = ml_get(pos->lnum);
3210 cclass = get_mouse_class(line + pos->col);
3211
3212 while (pos->col > 0)
3213 {
3214 col = pos->col - 1;
3215#ifdef FEAT_MBYTE
3216 col -= (*mb_head_off)(line, line + col);
3217#endif
3218 if (get_mouse_class(line + col) != cclass)
3219 break;
3220 pos->col = col;
3221 }
3222}
3223
3224/*
3225 * Move "pos" forward to the end of the word it's in.
3226 * When 'selection' is "exclusive", the position is just after the word.
3227 */
3228 static void
3229find_end_of_word(pos)
3230 pos_T *pos;
3231{
3232 char_u *line;
3233 int cclass;
3234 int col;
3235
3236 line = ml_get(pos->lnum);
3237 if (*p_sel == 'e' && pos->col > 0)
3238 {
3239 --pos->col;
3240#ifdef FEAT_MBYTE
3241 pos->col -= (*mb_head_off)(line, line + pos->col);
3242#endif
3243 }
3244 cclass = get_mouse_class(line + pos->col);
3245 while (line[pos->col] != NUL)
3246 {
3247#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003248 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249#else
3250 col = pos->col + 1;
3251#endif
3252 if (get_mouse_class(line + col) != cclass)
3253 {
3254 if (*p_sel == 'e')
3255 pos->col = col;
3256 break;
3257 }
3258 pos->col = col;
3259 }
3260}
3261
3262/*
3263 * Get class of a character for selection: same class means same word.
3264 * 0: blank
3265 * 1: punctuation groups
3266 * 2: normal word character
3267 * >2: multi-byte word character.
3268 */
3269 static int
3270get_mouse_class(p)
3271 char_u *p;
3272{
3273 int c;
3274
3275#ifdef FEAT_MBYTE
3276 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3277 return mb_get_class(p);
3278#endif
3279
3280 c = *p;
3281 if (c == ' ' || c == '\t')
3282 return 0;
3283
3284 if (vim_iswordc(c))
3285 return 2;
3286
3287 /*
3288 * There are a few special cases where we want certain combinations of
3289 * characters to be considered as a single word. These are things like
3290 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003291 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3294 return 1;
3295 return c;
3296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif /* FEAT_MOUSE */
3298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299/*
3300 * Check if highlighting for visual mode is possible, give a warning message
3301 * if not.
3302 */
3303 void
3304check_visual_highlight()
3305{
3306 static int did_check = FALSE;
3307
3308 if (full_screen)
3309 {
3310 if (!did_check && hl_attr(HLF_V) == 0)
3311 MSG(_("Warning: terminal cannot highlight"));
3312 did_check = TRUE;
3313 }
3314}
3315
3316/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003317 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * This function should ALWAYS be called to end Visual mode, except from
3319 * do_pending_operator().
3320 */
3321 void
3322end_visual_mode()
3323{
3324#ifdef FEAT_CLIPBOARD
3325 /*
3326 * If we are using the clipboard, then remember what was selected in case
3327 * we need to paste it somewhere while we still own the selection.
3328 * Only do this when the clipboard is already owned. Don't want to grab
3329 * the selection when hitting ESC.
3330 */
3331 if (clip_star.available && clip_star.owned)
3332 clip_auto_select();
3333#endif
3334
3335 VIsual_active = FALSE;
3336#ifdef FEAT_MOUSE
3337 setmouse();
3338 mouse_dragging = 0;
3339#endif
3340
3341 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003342 curbuf->b_visual.vi_mode = VIsual_mode;
3343 curbuf->b_visual.vi_start = VIsual;
3344 curbuf->b_visual.vi_end = curwin->w_cursor;
3345 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#ifdef FEAT_EVAL
3347 curbuf->b_visual_mode_eval = VIsual_mode;
3348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#ifdef FEAT_VIRTUALEDIT
3350 if (!virtual_active())
3351 curwin->w_cursor.coladd = 0;
3352#endif
3353
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003354 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 clear_cmdline = TRUE; /* unshow visual mode later */
3356#ifdef FEAT_CMDL_INFO
3357 else
3358 clear_showcmd();
3359#endif
3360
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003361 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362}
3363
3364/*
3365 * Reset VIsual_active and VIsual_reselect.
3366 */
3367 void
3368reset_VIsual_and_resel()
3369{
3370 if (VIsual_active)
3371 {
3372 end_visual_mode();
3373 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3374 }
3375 VIsual_reselect = FALSE;
3376}
3377
3378/*
3379 * Reset VIsual_active and VIsual_reselect if it's set.
3380 */
3381 void
3382reset_VIsual()
3383{
3384 if (VIsual_active)
3385 {
3386 end_visual_mode();
3387 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3388 VIsual_reselect = FALSE;
3389 }
3390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003392#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3394
3395/*
3396 * Check for a balloon-eval special item to include when searching for an
3397 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3398 * Returns TRUE if the character at "*ptr" should be included.
3399 * "dir" is FORWARD or BACKWARD, the direction of searching.
3400 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3401 * "bnp" points to a counter for square brackets.
3402 */
3403 static int
3404find_is_eval_item(ptr, colp, bnp, dir)
3405 char_u *ptr;
3406 int *colp;
3407 int *bnp;
3408 int dir;
3409{
3410 /* Accept everything inside []. */
3411 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3412 ++*bnp;
3413 if (*bnp > 0)
3414 {
3415 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3416 --*bnp;
3417 return TRUE;
3418 }
3419
3420 /* skip over "s.var" */
3421 if (*ptr == '.')
3422 return TRUE;
3423
3424 /* two-character item: s->var */
3425 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3426 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3427 {
3428 *colp += dir;
3429 return TRUE;
3430 }
3431 return FALSE;
3432}
3433#endif
3434
3435/*
3436 * Find the identifier under or to the right of the cursor.
3437 * "find_type" can have one of three values:
3438 * FIND_IDENT: find an identifier (keyword)
3439 * FIND_STRING: find any non-white string
3440 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003441 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 *
3443 * There are three steps:
3444 * 1. Search forward for the start of an identifier/string. Doesn't move if
3445 * already on one.
3446 * 2. Search backward for the start of this identifier/string.
3447 * This doesn't match the real Vi but I like it a little better and it
3448 * shouldn't bother anyone.
3449 * 3. Search forward to the end of this identifier/string.
3450 * When FIND_IDENT isn't defined, we backup until a blank.
3451 *
3452 * Returns the length of the string, or zero if no string is found.
3453 * If a string is found, a pointer to the string is put in "*string". This
3454 * string is not always NUL terminated.
3455 */
3456 int
3457find_ident_under_cursor(string, find_type)
3458 char_u **string;
3459 int find_type;
3460{
3461 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3462 curwin->w_cursor.col, string, find_type);
3463}
3464
3465/*
3466 * Like find_ident_under_cursor(), but for any window and any position.
3467 * However: Uses 'iskeyword' from the current window!.
3468 */
3469 int
3470find_ident_at_pos(wp, lnum, startcol, string, find_type)
3471 win_T *wp;
3472 linenr_T lnum;
3473 colnr_T startcol;
3474 char_u **string;
3475 int find_type;
3476{
3477 char_u *ptr;
3478 int col = 0; /* init to shut up GCC */
3479 int i;
3480#ifdef FEAT_MBYTE
3481 int this_class = 0;
3482 int prev_class;
3483 int prevcol;
3484#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int bn = 0; /* bracket nesting */
3487#endif
3488
3489 /*
3490 * if i == 0: try to find an identifier
3491 * if i == 1: try to find any non-white string
3492 */
3493 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3494 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3495 {
3496 /*
3497 * 1. skip to start of identifier/string
3498 */
3499 col = startcol;
3500#ifdef FEAT_MBYTE
3501 if (has_mbyte)
3502 {
3503 while (ptr[col] != NUL)
3504 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* Stop at a ']' to evaluate "a[x]". */
3507 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3508 break;
3509# endif
3510 this_class = mb_get_class(ptr + col);
3511 if (this_class != 0 && (i == 1 || this_class != 1))
3512 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003513 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 }
3516 else
3517#endif
3518 while (ptr[col] != NUL
3519 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003520# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3522# endif
3523 )
3524 ++col;
3525
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /* When starting on a ']' count it, so that we include the '['. */
3528 bn = ptr[col] == ']';
3529#endif
3530
3531 /*
3532 * 2. Back up to start of identifier/string.
3533 */
3534#ifdef FEAT_MBYTE
3535 if (has_mbyte)
3536 {
3537 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003538# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3540 this_class = mb_get_class((char_u *)"a");
3541 else
3542# endif
3543 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003544 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 {
3546 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3547 prev_class = mb_get_class(ptr + prevcol);
3548 if (this_class != prev_class
3549 && (i == 0
3550 || prev_class == 0
3551 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 && (!(find_type & FIND_EVAL)
3554 || prevcol == 0
3555 || !find_is_eval_item(ptr + prevcol, &prevcol,
3556 &bn, BACKWARD))
3557# endif
3558 )
3559 break;
3560 col = prevcol;
3561 }
3562
3563 /* If we don't want just any old string, or we've found an
3564 * identifier, stop searching. */
3565 if (this_class > 2)
3566 this_class = 2;
3567 if (!(find_type & FIND_STRING) || this_class == 2)
3568 break;
3569 }
3570 else
3571#endif
3572 {
3573 while (col > 0
3574 && ((i == 0
3575 ? vim_iswordc(ptr[col - 1])
3576 : (!vim_iswhite(ptr[col - 1])
3577 && (!(find_type & FIND_IDENT)
3578 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003579#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 || ((find_type & FIND_EVAL)
3581 && col > 1
3582 && find_is_eval_item(ptr + col - 1, &col,
3583 &bn, BACKWARD))
3584#endif
3585 ))
3586 --col;
3587
3588 /* If we don't want just any old string, or we've found an
3589 * identifier, stop searching. */
3590 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3591 break;
3592 }
3593 }
3594
3595 if (ptr[col] == NUL || (i == 0 && (
3596#ifdef FEAT_MBYTE
3597 has_mbyte ? this_class != 2 :
3598#endif
3599 !vim_iswordc(ptr[col]))))
3600 {
3601 /*
3602 * didn't find an identifier or string
3603 */
3604 if (find_type & FIND_STRING)
3605 EMSG(_("E348: No string under cursor"));
3606 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003607 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return 0;
3609 }
3610 ptr += col;
3611 *string = ptr;
3612
3613 /*
3614 * 3. Find the end if the identifier/string.
3615 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003616#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 bn = 0;
3618 startcol -= col;
3619#endif
3620 col = 0;
3621#ifdef FEAT_MBYTE
3622 if (has_mbyte)
3623 {
3624 /* Search for point of changing multibyte character class. */
3625 this_class = mb_get_class(ptr);
3626 while (ptr[col] != NUL
3627 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3628 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003629# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 || ((find_type & FIND_EVAL)
3631 && col <= (int)startcol
3632 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3633# endif
3634 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003635 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638#endif
3639 while ((i == 0 ? vim_iswordc(ptr[col])
3640 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003641# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 || ((find_type & FIND_EVAL)
3643 && col <= (int)startcol
3644 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3645# endif
3646 )
3647 {
3648 ++col;
3649 }
3650
3651 return col;
3652}
3653
3654/*
3655 * Prepare for redo of a normal command.
3656 */
3657 static void
3658prep_redo_cmd(cap)
3659 cmdarg_T *cap;
3660{
3661 prep_redo(cap->oap->regname, cap->count0,
3662 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3663}
3664
3665/*
3666 * Prepare for redo of any command.
3667 * Note that only the last argument can be a multi-byte char.
3668 */
3669 static void
3670prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3671 int regname;
3672 long num;
3673 int cmd1;
3674 int cmd2;
3675 int cmd3;
3676 int cmd4;
3677 int cmd5;
3678{
3679 ResetRedobuff();
3680 if (regname != 0) /* yank from specified buffer */
3681 {
3682 AppendCharToRedobuff('"');
3683 AppendCharToRedobuff(regname);
3684 }
3685 if (num)
3686 AppendNumberToRedobuff(num);
3687
3688 if (cmd1 != NUL)
3689 AppendCharToRedobuff(cmd1);
3690 if (cmd2 != NUL)
3691 AppendCharToRedobuff(cmd2);
3692 if (cmd3 != NUL)
3693 AppendCharToRedobuff(cmd3);
3694 if (cmd4 != NUL)
3695 AppendCharToRedobuff(cmd4);
3696 if (cmd5 != NUL)
3697 AppendCharToRedobuff(cmd5);
3698}
3699
3700/*
3701 * check for operator active and clear it
3702 *
3703 * return TRUE if operator was active
3704 */
3705 static int
3706checkclearop(oap)
3707 oparg_T *oap;
3708{
3709 if (oap->op_type == OP_NOP)
3710 return FALSE;
3711 clearopbeep(oap);
3712 return TRUE;
3713}
3714
3715/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003716 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003718 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
3720 static int
3721checkclearopq(oap)
3722 oparg_T *oap;
3723{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003724 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 return FALSE;
3726 clearopbeep(oap);
3727 return TRUE;
3728}
3729
3730 static void
3731clearop(oap)
3732 oparg_T *oap;
3733{
3734 oap->op_type = OP_NOP;
3735 oap->regname = 0;
3736 oap->motion_force = NUL;
3737 oap->use_reg_one = FALSE;
3738}
3739
3740 static void
3741clearopbeep(oap)
3742 oparg_T *oap;
3743{
3744 clearop(oap);
3745 beep_flush();
3746}
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748/*
3749 * Remove the shift modifier from a special key.
3750 */
3751 static void
3752unshift_special(cap)
3753 cmdarg_T *cap;
3754{
3755 switch (cap->cmdchar)
3756 {
3757 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3758 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3759 case K_S_UP: cap->cmdchar = K_UP; break;
3760 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3761 case K_S_HOME: cap->cmdchar = K_HOME; break;
3762 case K_S_END: cap->cmdchar = K_END; break;
3763 }
3764 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3765}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3768/*
3769 * Routines for displaying a partly typed command
3770 */
3771
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003772#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773static char_u showcmd_buf[SHOWCMD_BUFLEN];
3774static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3775static int showcmd_is_clear = TRUE;
3776static int showcmd_visual = FALSE;
3777
3778static void display_showcmd __ARGS((void));
3779
3780 void
3781clear_showcmd()
3782{
3783 if (!p_sc)
3784 return;
3785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 if (VIsual_active && !char_avail())
3787 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003788 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 long lines;
3790 colnr_T leftcol, rightcol;
3791 linenr_T top, bot;
3792
3793 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003794 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
3796 top = VIsual.lnum;
3797 bot = curwin->w_cursor.lnum;
3798 }
3799 else
3800 {
3801 top = curwin->w_cursor.lnum;
3802 bot = VIsual.lnum;
3803 }
3804# ifdef FEAT_FOLDING
3805 /* Include closed folds as a whole. */
3806 hasFolding(top, &top, NULL);
3807 hasFolding(bot, NULL, &bot);
3808# endif
3809 lines = bot - top + 1;
3810
3811 if (VIsual_mode == Ctrl_V)
3812 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003813# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003814 char_u *saved_sbr = p_sbr;
3815
3816 /* Make 'sbr' empty for a moment to get the correct size. */
3817 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003820# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003821 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3824 (long)(rightcol - leftcol + 1));
3825 }
3826 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3827 sprintf((char *)showcmd_buf, "%ld", lines);
3828 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003829 {
3830 char_u *s, *e;
3831 int l;
3832 int bytes = 0;
3833 int chars = 0;
3834
3835 if (cursor_bot)
3836 {
3837 s = ml_get_pos(&VIsual);
3838 e = ml_get_cursor();
3839 }
3840 else
3841 {
3842 s = ml_get_cursor();
3843 e = ml_get_pos(&VIsual);
3844 }
3845 while ((*p_sel != 'e') ? s <= e : s < e)
3846 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003847# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003848 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003849# else
3850 l = (*s == NUL) ? 0 : 1;
3851# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003852 if (l == 0)
3853 {
3854 ++bytes;
3855 ++chars;
3856 break; /* end of line */
3857 }
3858 bytes += l;
3859 ++chars;
3860 s += l;
3861 }
3862 if (bytes == chars)
3863 sprintf((char *)showcmd_buf, "%d", chars);
3864 else
3865 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3868 showcmd_visual = TRUE;
3869 }
3870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
3872 showcmd_buf[0] = NUL;
3873 showcmd_visual = FALSE;
3874
3875 /* Don't actually display something if there is nothing to clear. */
3876 if (showcmd_is_clear)
3877 return;
3878 }
3879
3880 display_showcmd();
3881}
3882
3883/*
3884 * Add 'c' to string of shown command chars.
3885 * Return TRUE if output has been written (and setcursor() has been called).
3886 */
3887 int
3888add_to_showcmd(c)
3889 int c;
3890{
3891 char_u *p;
3892 int old_len;
3893 int extra_len;
3894 int overflow;
3895#if defined(FEAT_MOUSE)
3896 int i;
3897 static int ignore[] =
3898 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003899# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3901 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 K_IGNORE,
3904 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3905 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3906 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003907 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003909 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 0
3911 };
3912#endif
3913
Bram Moolenaar09df3122006-01-23 22:23:09 +00003914 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return FALSE;
3916
3917 if (showcmd_visual)
3918 {
3919 showcmd_buf[0] = NUL;
3920 showcmd_visual = FALSE;
3921 }
3922
3923#if defined(FEAT_MOUSE)
3924 /* Ignore keys that are scrollbar updates and mouse clicks */
3925 if (IS_SPECIAL(c))
3926 for (i = 0; ignore[i] != 0; ++i)
3927 if (ignore[i] == c)
3928 return FALSE;
3929#endif
3930
3931 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003932 if (*p == ' ')
3933 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 old_len = (int)STRLEN(showcmd_buf);
3935 extra_len = (int)STRLEN(p);
3936 overflow = old_len + extra_len - SHOWCMD_COLS;
3937 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003938 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3939 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 STRCAT(showcmd_buf, p);
3941
3942 if (char_avail())
3943 return FALSE;
3944
3945 display_showcmd();
3946
3947 return TRUE;
3948}
3949
3950 void
3951add_to_showcmd_c(c)
3952 int c;
3953{
3954 if (!add_to_showcmd(c))
3955 setcursor();
3956}
3957
3958/*
3959 * Delete 'len' characters from the end of the shown command.
3960 */
3961 static void
3962del_from_showcmd(len)
3963 int len;
3964{
3965 int old_len;
3966
3967 if (!p_sc)
3968 return;
3969
3970 old_len = (int)STRLEN(showcmd_buf);
3971 if (len > old_len)
3972 len = old_len;
3973 showcmd_buf[old_len - len] = NUL;
3974
3975 if (!char_avail())
3976 display_showcmd();
3977}
3978
3979/*
3980 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3981 * something and there is a partial mapping.
3982 */
3983 void
3984push_showcmd()
3985{
3986 if (p_sc)
3987 STRCPY(old_showcmd_buf, showcmd_buf);
3988}
3989
3990 void
3991pop_showcmd()
3992{
3993 if (!p_sc)
3994 return;
3995
3996 STRCPY(showcmd_buf, old_showcmd_buf);
3997
3998 display_showcmd();
3999}
4000
4001 static void
4002display_showcmd()
4003{
4004 int len;
4005
4006 cursor_off();
4007
4008 len = (int)STRLEN(showcmd_buf);
4009 if (len == 0)
4010 showcmd_is_clear = TRUE;
4011 else
4012 {
4013 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4014 showcmd_is_clear = FALSE;
4015 }
4016
4017 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004018 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4019 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 */
4021 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4022
4023 setcursor(); /* put cursor back where it belongs */
4024}
4025#endif
4026
4027#ifdef FEAT_SCROLLBIND
4028/*
4029 * When "check" is FALSE, prepare for commands that scroll the window.
4030 * When "check" is TRUE, take care of scroll-binding after the window has
4031 * scrolled. Called from normal_cmd() and edit().
4032 */
4033 void
4034do_check_scrollbind(check)
4035 int check;
4036{
4037 static win_T *old_curwin = NULL;
4038 static linenr_T old_topline = 0;
4039#ifdef FEAT_DIFF
4040 static int old_topfill = 0;
4041#endif
4042 static buf_T *old_buf = NULL;
4043 static colnr_T old_leftcol = 0;
4044
4045 if (check && curwin->w_p_scb)
4046 {
4047 /* If a ":syncbind" command was just used, don't scroll, only reset
4048 * the values. */
4049 if (did_syncbind)
4050 did_syncbind = FALSE;
4051 else if (curwin == old_curwin)
4052 {
4053 /*
4054 * Synchronize other windows, as necessary according to
4055 * 'scrollbind'. Don't do this after an ":edit" command, except
4056 * when 'diff' is set.
4057 */
4058 if ((curwin->w_buffer == old_buf
4059#ifdef FEAT_DIFF
4060 || curwin->w_p_diff
4061#endif
4062 )
4063 && (curwin->w_topline != old_topline
4064#ifdef FEAT_DIFF
4065 || curwin->w_topfill != old_topfill
4066#endif
4067 || curwin->w_leftcol != old_leftcol))
4068 {
4069 check_scrollbind(curwin->w_topline - old_topline,
4070 (long)(curwin->w_leftcol - old_leftcol));
4071 }
4072 }
4073 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4074 {
4075 /*
4076 * When switching between windows, make sure that the relative
4077 * vertical offset is valid for the new window. The relative
4078 * offset is invalid whenever another 'scrollbind' window has
4079 * scrolled to a point that would force the current window to
4080 * scroll past the beginning or end of its buffer. When the
4081 * resync is performed, some of the other 'scrollbind' windows may
4082 * need to jump so that the current window's relative position is
4083 * visible on-screen.
4084 */
4085 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4086 }
4087 curwin->w_scbind_pos = curwin->w_topline;
4088 }
4089
4090 old_curwin = curwin;
4091 old_topline = curwin->w_topline;
4092#ifdef FEAT_DIFF
4093 old_topfill = curwin->w_topfill;
4094#endif
4095 old_buf = curwin->w_buffer;
4096 old_leftcol = curwin->w_leftcol;
4097}
4098
4099/*
4100 * Synchronize any windows that have "scrollbind" set, based on the
4101 * number of rows by which the current window has changed
4102 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4103 */
4104 void
4105check_scrollbind(topline_diff, leftcol_diff)
4106 linenr_T topline_diff;
4107 long leftcol_diff;
4108{
4109 int want_ver;
4110 int want_hor;
4111 win_T *old_curwin = curwin;
4112 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 int old_VIsual_select = VIsual_select;
4114 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 colnr_T tgt_leftcol = curwin->w_leftcol;
4116 long topline;
4117 long y;
4118
4119 /*
4120 * check 'scrollopt' string for vertical and horizontal scroll options
4121 */
4122 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4123#ifdef FEAT_DIFF
4124 want_ver |= old_curwin->w_p_diff;
4125#endif
4126 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4127
4128 /*
4129 * loop through the scrollbound windows and scroll accordingly
4130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4133 {
4134 curbuf = curwin->w_buffer;
4135 /* skip original window and windows with 'noscrollbind' */
4136 if (curwin != old_curwin && curwin->w_p_scb)
4137 {
4138 /*
4139 * do the vertical scroll
4140 */
4141 if (want_ver)
4142 {
4143#ifdef FEAT_DIFF
4144 if (old_curwin->w_p_diff && curwin->w_p_diff)
4145 {
4146 diff_set_topline(old_curwin, curwin);
4147 }
4148 else
4149#endif
4150 {
4151 curwin->w_scbind_pos += topline_diff;
4152 topline = curwin->w_scbind_pos;
4153 if (topline > curbuf->b_ml.ml_line_count)
4154 topline = curbuf->b_ml.ml_line_count;
4155 if (topline < 1)
4156 topline = 1;
4157
4158 y = topline - curwin->w_topline;
4159 if (y > 0)
4160 scrollup(y, FALSE);
4161 else
4162 scrolldown(-y, FALSE);
4163 }
4164
4165 redraw_later(VALID);
4166 cursor_correct();
4167#ifdef FEAT_WINDOWS
4168 curwin->w_redr_status = TRUE;
4169#endif
4170 }
4171
4172 /*
4173 * do the horizontal scroll
4174 */
4175 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4176 {
4177 curwin->w_leftcol = tgt_leftcol;
4178 leftcol_changed();
4179 }
4180 }
4181 }
4182
4183 /*
4184 * reset current-window
4185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 VIsual_select = old_VIsual_select;
4187 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 curwin = old_curwin;
4189 curbuf = old_curbuf;
4190}
4191#endif /* #ifdef FEAT_SCROLLBIND */
4192
4193/*
4194 * Command character that's ignored.
4195 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004196 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 static void
4199nv_ignore(cap)
4200 cmdarg_T *cap;
4201{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004202 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203}
4204
4205/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004206 * Command character that doesn't do anything, but unlike nv_ignore() does
4207 * start edit(). Used for "startinsert" executed while starting up.
4208 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004209 static void
4210nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004211 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004212{
4213}
4214
4215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Command character doesn't exist.
4217 */
4218 static void
4219nv_error(cap)
4220 cmdarg_T *cap;
4221{
4222 clearopbeep(cap->oap);
4223}
4224
4225/*
4226 * <Help> and <F1> commands.
4227 */
4228 static void
4229nv_help(cap)
4230 cmdarg_T *cap;
4231{
4232 if (!checkclearopq(cap->oap))
4233 ex_help(NULL);
4234}
4235
4236/*
4237 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4238 */
4239 static void
4240nv_addsub(cap)
4241 cmdarg_T *cap;
4242{
4243 if (!checkclearopq(cap->oap)
4244 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4245 prep_redo_cmd(cap);
4246}
4247
4248/*
4249 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4250 */
4251 static void
4252nv_page(cap)
4253 cmdarg_T *cap;
4254{
4255 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004256 {
4257#ifdef FEAT_WINDOWS
4258 if (mod_mask & MOD_MASK_CTRL)
4259 {
4260 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4261 if (cap->arg == BACKWARD)
4262 goto_tabpage(-(int)cap->count1);
4263 else
4264 goto_tabpage((int)cap->count0);
4265 }
4266 else
4267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270}
4271
4272/*
4273 * Implementation of "gd" and "gD" command.
4274 */
4275 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004276nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004277 oparg_T *oap;
4278 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004279 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280{
4281 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 char_u *ptr;
4283
Bram Moolenaard9d30582005-05-18 22:10:28 +00004284 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004285 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004287#ifdef FEAT_FOLDING
4288 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4289 foldOpenCursor();
4290#endif
4291}
4292
4293/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004294 * Search for variable declaration of "ptr[len]".
4295 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4296 * current file ("gD").
4297 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 * Return FAIL when not found.
4299 */
4300 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004301find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004302 char_u *ptr;
4303 int len;
4304 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004305 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004306 int searchflags; /* flags passed to searchit() */
4307{
4308 char_u *pat;
4309 pos_T old_pos;
4310 pos_T par_pos;
4311 pos_T found_pos;
4312 int t;
4313 int save_p_ws;
4314 int save_p_scs;
4315 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004316 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317
4318 if ((pat = alloc(len + 7)) == NULL)
4319 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004320
4321 /* Put "\V" before the pattern to avoid that the special meaning of "."
4322 * and "~" causes trouble. */
4323 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4324 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 old_pos = curwin->w_cursor;
4326 save_p_ws = p_ws;
4327 save_p_scs = p_scs;
4328 p_ws = FALSE; /* don't wrap around end of file now */
4329 p_scs = FALSE; /* don't switch ignorecase off now */
4330
4331 /*
4332 * With "gD" go to line 1.
4333 * With "gd" Search back for the start of the current function, then go
4334 * back until a blank line. If this fails go to line 1.
4335 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004336 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 setpcmark(); /* Set in findpar() otherwise */
4339 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004340 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 else
4343 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004344 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4346 --curwin->w_cursor.lnum;
4347 }
4348 curwin->w_cursor.col = 0;
4349
4350 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004351 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004352 for (;;)
4353 {
4354 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004355 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356 if (curwin->w_cursor.lnum >= old_pos.lnum)
4357 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004358
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004359 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 {
4361 pos_T *pos;
4362
4363 /* Check that the block the match is in doesn't end before the
4364 * position where we started the search from. */
4365 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4366 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4367 && pos->lnum < old_pos.lnum)
4368 continue;
4369 }
4370
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004371 if (t == FAIL)
4372 {
4373 /* If we previously found a valid position, use it. */
4374 if (found_pos.lnum != 0)
4375 {
4376 curwin->w_cursor = found_pos;
4377 t = OK;
4378 }
4379 break;
4380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004382 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004383 {
4384 /* Ignore this line, continue at start of next line. */
4385 ++curwin->w_cursor.lnum;
4386 curwin->w_cursor.col = 0;
4387 continue;
4388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004390 if (!locally) /* global search: use first match found */
4391 break;
4392 if (curwin->w_cursor.lnum >= par_pos.lnum)
4393 {
4394 /* If we previously found a valid position, use it. */
4395 if (found_pos.lnum != 0)
4396 curwin->w_cursor = found_pos;
4397 break;
4398 }
4399
4400 /* For finding a local variable and the match is before the "{" search
4401 * to find a later match. For K&R style function declarations this
4402 * skips the function header without types. */
4403 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004405
4406 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004408 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 curwin->w_cursor = old_pos;
4410 }
4411 else
4412 {
4413 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 /* "n" searches forward now */
4415 reset_search_dir();
4416 }
4417
4418 vim_free(pat);
4419 p_ws = save_p_ws;
4420 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004421
4422 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423}
4424
4425/*
4426 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4427 * lines rather than lines in the file.
4428 * 'dist' must be positive.
4429 *
4430 * Return OK if able to move cursor, FAIL otherwise.
4431 */
4432 static int
4433nv_screengo(oap, dir, dist)
4434 oparg_T *oap;
4435 int dir;
4436 long dist;
4437{
4438 int linelen = linetabsize(ml_get_curline());
4439 int retval = OK;
4440 int atend = FALSE;
4441 int n;
4442 int col_off1; /* margin offset for first screen line */
4443 int col_off2; /* margin offset for wrapped screen line */
4444 int width1; /* text width for first screen line */
4445 int width2; /* test width for wrapped screen line */
4446
4447 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004448 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 col_off1 = curwin_col_off();
4451 col_off2 = col_off1 - curwin_col_off2();
4452 width1 = W_WIDTH(curwin) - col_off1;
4453 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004454 if (width2 == 0)
4455 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457#ifdef FEAT_VERTSPLIT
4458 if (curwin->w_width != 0)
4459 {
4460#endif
4461 /*
4462 * Instead of sticking at the last character of the buffer line we
4463 * try to stick in the last column of the screen.
4464 */
4465 if (curwin->w_curswant == MAXCOL)
4466 {
4467 atend = TRUE;
4468 validate_virtcol();
4469 if (width1 <= 0)
4470 curwin->w_curswant = 0;
4471 else
4472 {
4473 curwin->w_curswant = width1 - 1;
4474 if (curwin->w_virtcol > curwin->w_curswant)
4475 curwin->w_curswant += ((curwin->w_virtcol
4476 - curwin->w_curswant - 1) / width2 + 1) * width2;
4477 }
4478 }
4479 else
4480 {
4481 if (linelen > width1)
4482 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4483 else
4484 n = width1;
4485 if (curwin->w_curswant > (colnr_T)n + 1)
4486 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4487 * width2;
4488 }
4489
4490 while (dist--)
4491 {
4492 if (dir == BACKWARD)
4493 {
4494 if ((long)curwin->w_curswant >= width2)
4495 /* move back within line */
4496 curwin->w_curswant -= width2;
4497 else
4498 {
4499 /* to previous line */
4500 if (curwin->w_cursor.lnum == 1)
4501 {
4502 retval = FAIL;
4503 break;
4504 }
4505 --curwin->w_cursor.lnum;
4506#ifdef FEAT_FOLDING
4507 /* Move to the start of a closed fold. Don't do that when
4508 * 'foldopen' contains "all": it will open in a moment. */
4509 if (!(fdo_flags & FDO_ALL))
4510 (void)hasFolding(curwin->w_cursor.lnum,
4511 &curwin->w_cursor.lnum, NULL);
4512#endif
4513 linelen = linetabsize(ml_get_curline());
4514 if (linelen > width1)
4515 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4516 + 1) * width2;
4517 }
4518 }
4519 else /* dir == FORWARD */
4520 {
4521 if (linelen > width1)
4522 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4523 else
4524 n = width1;
4525 if (curwin->w_curswant + width2 < (colnr_T)n)
4526 /* move forward within line */
4527 curwin->w_curswant += width2;
4528 else
4529 {
4530 /* to next line */
4531#ifdef FEAT_FOLDING
4532 /* Move to the end of a closed fold. */
4533 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4534 &curwin->w_cursor.lnum);
4535#endif
4536 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4537 {
4538 retval = FAIL;
4539 break;
4540 }
4541 curwin->w_cursor.lnum++;
4542 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004543 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 }
4546 }
4547#ifdef FEAT_VERTSPLIT
4548 }
4549#endif
4550
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004551 if (virtual_active() && atend)
4552 coladvance(MAXCOL);
4553 else
4554 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4557 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4558 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004559 colnr_T virtcol;
4560
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 /*
4562 * Check for landing on a character that got split at the end of the
4563 * last line. We want to advance a screenline, not end up in the same
4564 * screenline or move two screenlines.
4565 */
4566 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004567 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004568# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004569 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4570 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004571# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004572
4573 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 && (curwin->w_curswant < (colnr_T)width1
4575 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4576 : ((curwin->w_curswant - width1) % width2
4577 > (colnr_T)width2 / 2)))
4578 --curwin->w_cursor.col;
4579 }
4580#endif
4581
4582 if (atend)
4583 curwin->w_curswant = MAXCOL; /* stick in the last column */
4584
4585 return retval;
4586}
4587
4588#ifdef FEAT_MOUSE
4589/*
4590 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4591 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004592 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4593 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
4595 static void
4596nv_mousescroll(cap)
4597 cmdarg_T *cap;
4598{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004599# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004600 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004602 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 int row, col;
4605
4606 row = mouse_row;
4607 col = mouse_col;
4608
4609 /* find the window at the pointer coordinates */
4610 curwin = mouse_find_win(&row, &col);
4611 curbuf = curwin->w_buffer;
4612 }
4613# endif
4614
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004615 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4618 {
4619 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4620 }
4621 else
4622 {
4623 cap->count1 = 3;
4624 cap->count0 = 3;
4625 nv_scroll_line(cap);
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 else
4630 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004631 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4632 if (!curwin->w_p_wrap)
4633 {
4634 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004635
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004636 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4637 step = W_WIDTH(curwin);
4638 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4639 if (val < 0)
4640 val = 0;
4641
4642 gui_do_horiz_scroll(val, TRUE);
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004647# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 curwin->w_redr_status = TRUE;
4649
4650 curwin = old_curwin;
4651 curbuf = curwin->w_buffer;
4652# endif
4653}
4654
4655/*
4656 * Mouse clicks and drags.
4657 */
4658 static void
4659nv_mouse(cap)
4660 cmdarg_T *cap;
4661{
4662 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4663}
4664#endif
4665
4666/*
4667 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4668 * cap->arg must be TRUE for CTRL-E.
4669 */
4670 static void
4671nv_scroll_line(cap)
4672 cmdarg_T *cap;
4673{
4674 if (!checkclearop(cap->oap))
4675 scroll_redraw(cap->arg, cap->count1);
4676}
4677
4678/*
4679 * Scroll "count" lines up or down, and redraw.
4680 */
4681 void
4682scroll_redraw(up, count)
4683 int up;
4684 long count;
4685{
4686 linenr_T prev_topline = curwin->w_topline;
4687#ifdef FEAT_DIFF
4688 int prev_topfill = curwin->w_topfill;
4689#endif
4690 linenr_T prev_lnum = curwin->w_cursor.lnum;
4691
4692 if (up)
4693 scrollup(count, TRUE);
4694 else
4695 scrolldown(count, TRUE);
4696 if (p_so)
4697 {
4698 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4699 * valid, otherwise the screen jumps back at the end of the file. */
4700 cursor_correct();
4701 check_cursor_moved(curwin);
4702 curwin->w_valid |= VALID_TOPLINE;
4703
4704 /* If moved back to where we were, at least move the cursor, otherwise
4705 * we get stuck at one position. Don't move the cursor up if the
4706 * first line of the buffer is already on the screen */
4707 while (curwin->w_topline == prev_topline
4708#ifdef FEAT_DIFF
4709 && curwin->w_topfill == prev_topfill
4710#endif
4711 )
4712 {
4713 if (up)
4714 {
4715 if (curwin->w_cursor.lnum > prev_lnum
4716 || cursor_down(1L, FALSE) == FAIL)
4717 break;
4718 }
4719 else
4720 {
4721 if (curwin->w_cursor.lnum < prev_lnum
4722 || prev_topline == 1L
4723 || cursor_up(1L, FALSE) == FAIL)
4724 break;
4725 }
4726 /* Mark w_topline as valid, otherwise the screen jumps back at the
4727 * end of the file. */
4728 check_cursor_moved(curwin);
4729 curwin->w_valid |= VALID_TOPLINE;
4730 }
4731 }
4732 if (curwin->w_cursor.lnum != prev_lnum)
4733 coladvance(curwin->w_curswant);
4734 redraw_later(VALID);
4735}
4736
4737/*
4738 * Commands that start with "z".
4739 */
4740 static void
4741nv_zet(cap)
4742 cmdarg_T *cap;
4743{
4744 long n;
4745 colnr_T col;
4746 int nchar = cap->nchar;
4747#ifdef FEAT_FOLDING
4748 long old_fdl = curwin->w_p_fdl;
4749 int old_fen = curwin->w_p_fen;
4750#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004751#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004752 int undo = FALSE;
4753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 if (VIM_ISDIGIT(nchar))
4756 {
4757 /*
4758 * "z123{nchar}": edit the count before obtaining {nchar}
4759 */
4760 if (checkclearop(cap->oap))
4761 return;
4762 n = nchar - '0';
4763 for (;;)
4764 {
4765#ifdef USE_ON_FLY_SCROLL
4766 dont_scroll = TRUE; /* disallow scrolling here */
4767#endif
4768 ++no_mapping;
4769 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004770 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 --no_mapping;
4773 --allow_keys;
4774#ifdef FEAT_CMDL_INFO
4775 (void)add_to_showcmd(nchar);
4776#endif
4777 if (nchar == K_DEL || nchar == K_KDEL)
4778 n /= 10;
4779 else if (VIM_ISDIGIT(nchar))
4780 n = n * 10 + (nchar - '0');
4781 else if (nchar == CAR)
4782 {
4783#ifdef FEAT_GUI
4784 need_mouse_correct = TRUE;
4785#endif
4786 win_setheight((int)n);
4787 break;
4788 }
4789 else if (nchar == 'l'
4790 || nchar == 'h'
4791 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004792 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 cap->count1 = n ? n * cap->count1 : cap->count1;
4795 goto dozet;
4796 }
4797 else
4798 {
4799 clearopbeep(cap->oap);
4800 break;
4801 }
4802 }
4803 cap->oap->op_type = OP_NOP;
4804 return;
4805 }
4806
4807dozet:
4808 if (
4809#ifdef FEAT_FOLDING
4810 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4811 * and "zC" only in Visual mode. "zj" and "zk" are motion
4812 * commands. */
4813 cap->nchar != 'f' && cap->nchar != 'F'
4814 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4815 && cap->nchar != 'j' && cap->nchar != 'k'
4816 &&
4817#endif
4818 checkclearop(cap->oap))
4819 return;
4820
4821 /*
4822 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4823 * If line number given, set cursor.
4824 */
4825 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4826 && cap->count0
4827 && cap->count0 != curwin->w_cursor.lnum)
4828 {
4829 setpcmark();
4830 if (cap->count0 > curbuf->b_ml.ml_line_count)
4831 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4832 else
4833 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004834 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836
4837 switch (nchar)
4838 {
4839 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4840 case '+':
4841 if (cap->count0 == 0)
4842 {
4843 /* No count given: put cursor at the line below screen */
4844 validate_botline(); /* make sure w_botline is valid */
4845 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4846 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4847 else
4848 curwin->w_cursor.lnum = curwin->w_botline;
4849 }
4850 /* FALLTHROUGH */
4851 case NL:
4852 case CAR:
4853 case K_KENTER:
4854 beginline(BL_WHITE | BL_FIX);
4855 /* FALLTHROUGH */
4856
4857 case 't': scroll_cursor_top(0, TRUE);
4858 redraw_later(VALID);
4859 break;
4860
4861 /* "z." and "zz": put cursor in middle of screen */
4862 case '.': beginline(BL_WHITE | BL_FIX);
4863 /* FALLTHROUGH */
4864
4865 case 'z': scroll_cursor_halfway(TRUE);
4866 redraw_later(VALID);
4867 break;
4868
4869 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4870 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4871 * when <count> is at bottom of window, and puts that one at
4872 * bottom of window. */
4873 if (cap->count0 != 0)
4874 {
4875 scroll_cursor_bot(0, TRUE);
4876 curwin->w_cursor.lnum = curwin->w_topline;
4877 }
4878 else if (curwin->w_topline == 1)
4879 curwin->w_cursor.lnum = 1;
4880 else
4881 curwin->w_cursor.lnum = curwin->w_topline - 1;
4882 /* FALLTHROUGH */
4883 case '-':
4884 beginline(BL_WHITE | BL_FIX);
4885 /* FALLTHROUGH */
4886
4887 case 'b': scroll_cursor_bot(0, TRUE);
4888 redraw_later(VALID);
4889 break;
4890
4891 /* "zH" - scroll screen right half-page */
4892 case 'H':
4893 cap->count1 *= W_WIDTH(curwin) / 2;
4894 /* FALLTHROUGH */
4895
4896 /* "zh" - scroll screen to the right */
4897 case 'h':
4898 case K_LEFT:
4899 if (!curwin->w_p_wrap)
4900 {
4901 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4902 curwin->w_leftcol = 0;
4903 else
4904 curwin->w_leftcol -= (colnr_T)cap->count1;
4905 leftcol_changed();
4906 }
4907 break;
4908
4909 /* "zL" - scroll screen left half-page */
4910 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4911 /* FALLTHROUGH */
4912
4913 /* "zl" - scroll screen to the left */
4914 case 'l':
4915 case K_RIGHT:
4916 if (!curwin->w_p_wrap)
4917 {
4918 /* scroll the window left */
4919 curwin->w_leftcol += (colnr_T)cap->count1;
4920 leftcol_changed();
4921 }
4922 break;
4923
4924 /* "zs" - scroll screen, cursor at the start */
4925 case 's': if (!curwin->w_p_wrap)
4926 {
4927#ifdef FEAT_FOLDING
4928 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4929 col = 0; /* like the cursor is in col 0 */
4930 else
4931#endif
4932 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4933 if ((long)col > p_siso)
4934 col -= p_siso;
4935 else
4936 col = 0;
4937 if (curwin->w_leftcol != col)
4938 {
4939 curwin->w_leftcol = col;
4940 redraw_later(NOT_VALID);
4941 }
4942 }
4943 break;
4944
4945 /* "ze" - scroll screen, cursor at the end */
4946 case 'e': if (!curwin->w_p_wrap)
4947 {
4948#ifdef FEAT_FOLDING
4949 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4950 col = 0; /* like the cursor is in col 0 */
4951 else
4952#endif
4953 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4954 n = W_WIDTH(curwin) - curwin_col_off();
4955 if ((long)col + p_siso < n)
4956 col = 0;
4957 else
4958 col = col + p_siso - n + 1;
4959 if (curwin->w_leftcol != col)
4960 {
4961 curwin->w_leftcol = col;
4962 redraw_later(NOT_VALID);
4963 }
4964 }
4965 break;
4966
4967#ifdef FEAT_FOLDING
4968 /* "zF": create fold command */
4969 /* "zf": create fold operator */
4970 case 'F':
4971 case 'f': if (foldManualAllowed(TRUE))
4972 {
4973 cap->nchar = 'f';
4974 nv_operator(cap);
4975 curwin->w_p_fen = TRUE;
4976
4977 /* "zF" is like "zfzf" */
4978 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4979 {
4980 nv_operator(cap);
4981 finish_op = TRUE;
4982 }
4983 }
4984 else
4985 clearopbeep(cap->oap);
4986 break;
4987
4988 /* "zd": delete fold at cursor */
4989 /* "zD": delete fold at cursor recursively */
4990 case 'd':
4991 case 'D': if (foldManualAllowed(FALSE))
4992 {
4993 if (VIsual_active)
4994 nv_operator(cap);
4995 else
4996 deleteFold(curwin->w_cursor.lnum,
4997 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4998 }
4999 break;
5000
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005001 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 case 'E': if (foldmethodIsManual(curwin))
5003 {
5004 clearFolding(curwin);
5005 changed_window_setting();
5006 }
5007 else if (foldmethodIsMarker(curwin))
5008 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5009 TRUE, FALSE);
5010 else
5011 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5012 break;
5013
5014 /* "zn": fold none: reset 'foldenable' */
5015 case 'n': curwin->w_p_fen = FALSE;
5016 break;
5017
5018 /* "zN": fold Normal: set 'foldenable' */
5019 case 'N': curwin->w_p_fen = TRUE;
5020 break;
5021
5022 /* "zi": invert folding: toggle 'foldenable' */
5023 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5024 break;
5025
5026 /* "za": open closed fold or close open fold at cursor */
5027 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5028 openFold(curwin->w_cursor.lnum, cap->count1);
5029 else
5030 {
5031 closeFold(curwin->w_cursor.lnum, cap->count1);
5032 curwin->w_p_fen = TRUE;
5033 }
5034 break;
5035
5036 /* "zA": open fold at cursor recursively */
5037 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5038 openFoldRecurse(curwin->w_cursor.lnum);
5039 else
5040 {
5041 closeFoldRecurse(curwin->w_cursor.lnum);
5042 curwin->w_p_fen = TRUE;
5043 }
5044 break;
5045
5046 /* "zo": open fold at cursor or Visual area */
5047 case 'o': if (VIsual_active)
5048 nv_operator(cap);
5049 else
5050 openFold(curwin->w_cursor.lnum, cap->count1);
5051 break;
5052
5053 /* "zO": open fold recursively */
5054 case 'O': if (VIsual_active)
5055 nv_operator(cap);
5056 else
5057 openFoldRecurse(curwin->w_cursor.lnum);
5058 break;
5059
5060 /* "zc": close fold at cursor or Visual area */
5061 case 'c': if (VIsual_active)
5062 nv_operator(cap);
5063 else
5064 closeFold(curwin->w_cursor.lnum, cap->count1);
5065 curwin->w_p_fen = TRUE;
5066 break;
5067
5068 /* "zC": close fold recursively */
5069 case 'C': if (VIsual_active)
5070 nv_operator(cap);
5071 else
5072 closeFoldRecurse(curwin->w_cursor.lnum);
5073 curwin->w_p_fen = TRUE;
5074 break;
5075
5076 /* "zv": open folds at the cursor */
5077 case 'v': foldOpenCursor();
5078 break;
5079
5080 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5081 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005082 curwin->w_foldinvalid = TRUE; /* recompute folds */
5083 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 foldOpenCursor();
5085 break;
5086
5087 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5088 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005089 curwin->w_foldinvalid = TRUE; /* recompute folds */
5090 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /* "zm": fold more */
5094 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005095 {
5096 curwin->w_p_fdl -= cap->count1;
5097 if (curwin->w_p_fdl < 0)
5098 curwin->w_p_fdl = 0;
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 old_fdl = -1; /* force an update */
5101 curwin->w_p_fen = TRUE;
5102 break;
5103
5104 /* "zM": close all folds */
5105 case 'M': curwin->w_p_fdl = 0;
5106 old_fdl = -1; /* force an update */
5107 curwin->w_p_fen = TRUE;
5108 break;
5109
5110 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005111 case 'r': curwin->w_p_fdl += cap->count1;
5112 {
5113 int d = getDeepestNesting();
5114
5115 if (curwin->w_p_fdl >= d)
5116 curwin->w_p_fdl = d;
5117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /* "zR": open all folds */
5121 case 'R': curwin->w_p_fdl = getDeepestNesting();
5122 old_fdl = -1; /* force an update */
5123 break;
5124
5125 case 'j': /* "zj" move to next fold downwards */
5126 case 'k': /* "zk" move to next fold upwards */
5127 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5128 cap->count1) == FAIL)
5129 clearopbeep(cap->oap);
5130 break;
5131
5132#endif /* FEAT_FOLDING */
5133
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005134#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005135 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5136 ++no_mapping;
5137 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005138 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005139 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005140 --no_mapping;
5141 --allow_keys;
5142#ifdef FEAT_CMDL_INFO
5143 (void)add_to_showcmd(nchar);
5144#endif
5145 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5146 {
5147 clearopbeep(cap->oap);
5148 break;
5149 }
5150 undo = TRUE;
5151 /*FALLTHROUGH*/
5152
Bram Moolenaarb765d632005-06-07 21:00:02 +00005153 case 'g': /* "zg": add good word to word list */
5154 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005155 case 'G': /* "zG": add good word to temp word list */
5156 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005157 {
5158 char_u *ptr = NULL;
5159 int len;
5160
5161 if (checkclearop(cap->oap))
5162 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005163 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5164 == FAIL)
5165 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005166 if (ptr == NULL)
5167 {
5168 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005169
Bram Moolenaar134bf072013-09-25 18:54:24 +02005170 /* Find bad word under the cursor. When 'spell' is
5171 * off this fails and find_ident_under_cursor() is
5172 * used below. */
5173 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005174 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005175 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005176 if (len != 0 && curwin->w_cursor.col <= pos.col)
5177 ptr = ml_get_pos(&curwin->w_cursor);
5178 curwin->w_cursor = pos;
5179 }
5180
Bram Moolenaarb765d632005-06-07 21:00:02 +00005181 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5182 FIND_IDENT)) == 0)
5183 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005184 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005185 (nchar == 'G' || nchar == 'W')
5186 ? 0 : (int)cap->count1,
5187 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005188 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005189 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005190
Bram Moolenaar43abc522005-12-10 20:15:02 +00005191 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005192 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005193 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005194 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195#endif
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 default: clearopbeep(cap->oap);
5198 }
5199
5200#ifdef FEAT_FOLDING
5201 /* Redraw when 'foldenable' changed */
5202 if (old_fen != curwin->w_p_fen)
5203 {
5204# ifdef FEAT_DIFF
5205 win_T *wp;
5206
5207 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5208 {
5209 /* Adjust 'foldenable' in diff-synced windows. */
5210 FOR_ALL_WINDOWS(wp)
5211 {
5212 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5213 {
5214 wp->w_p_fen = curwin->w_p_fen;
5215 changed_window_setting_win(wp);
5216 }
5217 }
5218 }
5219# endif
5220 changed_window_setting();
5221 }
5222
5223 /* Redraw when 'foldlevel' changed. */
5224 if (old_fdl != curwin->w_p_fdl)
5225 newFoldLevel();
5226#endif
5227}
5228
5229#ifdef FEAT_GUI
5230/*
5231 * Vertical scrollbar movement.
5232 */
5233 static void
5234nv_ver_scrollbar(cap)
5235 cmdarg_T *cap;
5236{
5237 if (cap->oap->op_type != OP_NOP)
5238 clearopbeep(cap->oap);
5239
5240 /* Even if an operator was pending, we still want to scroll */
5241 gui_do_scroll();
5242}
5243
5244/*
5245 * Horizontal scrollbar movement.
5246 */
5247 static void
5248nv_hor_scrollbar(cap)
5249 cmdarg_T *cap;
5250{
5251 if (cap->oap->op_type != OP_NOP)
5252 clearopbeep(cap->oap);
5253
5254 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005255 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256}
5257#endif
5258
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005259#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005260/*
5261 * Click in GUI tab.
5262 */
5263 static void
5264nv_tabline(cap)
5265 cmdarg_T *cap;
5266{
5267 if (cap->oap->op_type != OP_NOP)
5268 clearopbeep(cap->oap);
5269
5270 /* Even if an operator was pending, we still want to jump tabs. */
5271 goto_tabpage(current_tab);
5272}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005273
5274/*
5275 * Selected item in tab line menu.
5276 */
5277 static void
5278nv_tabmenu(cap)
5279 cmdarg_T *cap;
5280{
5281 if (cap->oap->op_type != OP_NOP)
5282 clearopbeep(cap->oap);
5283
5284 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005285 handle_tabmenu();
5286}
5287
5288/*
5289 * Handle selecting an item of the GUI tab line menu.
5290 * Used in Normal and Insert mode.
5291 */
5292 void
5293handle_tabmenu()
5294{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005295 switch (current_tabmenu)
5296 {
5297 case TABLINE_MENU_CLOSE:
5298 if (current_tab == 0)
5299 do_cmdline_cmd((char_u *)"tabclose");
5300 else
5301 {
5302 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5303 current_tab);
5304 do_cmdline_cmd(IObuff);
5305 }
5306 break;
5307
5308 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005309 if (current_tab == 0)
5310 do_cmdline_cmd((char_u *)"$tabnew");
5311 else
5312 {
5313 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5314 current_tab - 1);
5315 do_cmdline_cmd(IObuff);
5316 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005317 break;
5318
5319 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005320 if (current_tab == 0)
5321 do_cmdline_cmd((char_u *)"browse $tabnew");
5322 else
5323 {
5324 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5325 current_tab - 1);
5326 do_cmdline_cmd(IObuff);
5327 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005328 break;
5329 }
5330}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005331#endif
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333/*
5334 * "Q" command.
5335 */
5336 static void
5337nv_exmode(cap)
5338 cmdarg_T *cap;
5339{
5340 /*
5341 * Ignore 'Q' in Visual mode, just give a beep.
5342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (VIsual_active)
5344 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005345 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 do_exmode(FALSE);
5347}
5348
5349/*
5350 * Handle a ":" command.
5351 */
5352 static void
5353nv_colon(cap)
5354 cmdarg_T *cap;
5355{
5356 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005357 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (VIsual_active)
5360 nv_operator(cap);
5361 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 if (cap->oap->op_type != OP_NOP)
5364 {
5365 /* Using ":" as a movement is characterwise exclusive. */
5366 cap->oap->motion_type = MCHAR;
5367 cap->oap->inclusive = FALSE;
5368 }
5369 else if (cap->count0)
5370 {
5371 /* translate "count:" into ":.,.+(count - 1)" */
5372 stuffcharReadbuff('.');
5373 if (cap->count0 > 1)
5374 {
5375 stuffReadbuff((char_u *)",.+");
5376 stuffnumReadbuff((long)cap->count0 - 1L);
5377 }
5378 }
5379
5380 /* When typing, don't type below an old message */
5381 if (KeyTyped)
5382 compute_cmdrow();
5383
5384 old_p_im = p_im;
5385
5386 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005387 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5389
5390 /* If 'insertmode' changed, enter or exit Insert mode */
5391 if (p_im != old_p_im)
5392 {
5393 if (p_im)
5394 restart_edit = 'i';
5395 else
5396 restart_edit = 0;
5397 }
5398
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005399 if (cmd_result == FAIL)
5400 /* The Ex command failed, do not execute the operator. */
5401 clearop(cap->oap);
5402 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5404 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005405 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5406 || did_emsg
5407 ))
5408 /* The start of the operator has become invalid by the Ex command.
5409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 clearopbeep(cap->oap);
5411 }
5412}
5413
5414/*
5415 * Handle CTRL-G command.
5416 */
5417 static void
5418nv_ctrlg(cap)
5419 cmdarg_T *cap;
5420{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 if (VIsual_active) /* toggle Selection/Visual mode */
5422 {
5423 VIsual_select = !VIsual_select;
5424 showmode();
5425 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005426 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 /* print full name if count given or :cd used */
5428 fileinfo((int)cap->count0, FALSE, TRUE);
5429}
5430
5431/*
5432 * Handle CTRL-H <Backspace> command.
5433 */
5434 static void
5435nv_ctrlh(cap)
5436 cmdarg_T *cap;
5437{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (VIsual_active && VIsual_select)
5439 {
5440 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5441 v_visop(cap);
5442 }
5443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 nv_left(cap);
5445}
5446
5447/*
5448 * CTRL-L: clear screen and redraw.
5449 */
5450 static void
5451nv_clear(cap)
5452 cmdarg_T *cap;
5453{
5454 if (!checkclearop(cap->oap))
5455 {
5456#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5457 /*
5458 * Right now, the BeBox doesn't seem to have an easy way to detect
5459 * window resizing, so we cheat and make the user detect it
5460 * manually with CTRL-L instead
5461 */
5462 ui_get_shellsize();
5463#endif
5464#ifdef FEAT_SYN_HL
5465 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005466 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#endif
5468 redraw_later(CLEAR);
5469 }
5470}
5471
5472/*
5473 * CTRL-O: In Select mode: switch to Visual mode for one command.
5474 * Otherwise: Go to older pcmark.
5475 */
5476 static void
5477nv_ctrlo(cap)
5478 cmdarg_T *cap;
5479{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 if (VIsual_active && VIsual_select)
5481 {
5482 VIsual_select = FALSE;
5483 showmode();
5484 restart_VIsual_select = 2; /* restart Select mode later */
5485 }
5486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 cap->count1 = -cap->count1;
5489 nv_pcmark(cap);
5490 }
5491}
5492
5493/*
5494 * CTRL-^ command, short for ":e #"
5495 */
5496 static void
5497nv_hat(cap)
5498 cmdarg_T *cap;
5499{
5500 if (!checkclearopq(cap->oap))
5501 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5502 GETF_SETMARK|GETF_ALT, FALSE);
5503}
5504
5505/*
5506 * "Z" commands.
5507 */
5508 static void
5509nv_Zet(cap)
5510 cmdarg_T *cap;
5511{
5512 if (!checkclearopq(cap->oap))
5513 {
5514 switch (cap->nchar)
5515 {
5516 /* "ZZ": equivalent to ":x". */
5517 case 'Z': do_cmdline_cmd((char_u *)"x");
5518 break;
5519
5520 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5521 case 'Q': do_cmdline_cmd((char_u *)"q!");
5522 break;
5523
5524 default: clearopbeep(cap->oap);
5525 }
5526 }
5527}
5528
5529#if defined(FEAT_WINDOWS) || defined(PROTO)
5530/*
5531 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5532 */
5533 void
5534do_nv_ident(c1, c2)
5535 int c1;
5536 int c2;
5537{
5538 oparg_T oa;
5539 cmdarg_T ca;
5540
5541 clear_oparg(&oa);
5542 vim_memset(&ca, 0, sizeof(ca));
5543 ca.oap = &oa;
5544 ca.cmdchar = c1;
5545 ca.nchar = c2;
5546 nv_ident(&ca);
5547}
5548#endif
5549
5550/*
5551 * Handle the commands that use the word under the cursor.
5552 * [g] CTRL-] :ta to current identifier
5553 * [g] 'K' run program for current identifier
5554 * [g] '*' / to current identifier or string
5555 * [g] '#' ? to current identifier or string
5556 * g ']' :tselect for current identifier
5557 */
5558 static void
5559nv_ident(cap)
5560 cmdarg_T *cap;
5561{
5562 char_u *ptr = NULL;
5563 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005564 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 char_u *p;
5566 char_u *kp; /* value of 'keywordprg' */
5567 int kp_help; /* 'keywordprg' is ":help" */
5568 int n = 0; /* init for GCC */
5569 int cmdchar;
5570 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005571 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 char_u *aux_ptr;
5573 int isman;
5574 int isman_s;
5575
5576 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5577 {
5578 cmdchar = cap->nchar;
5579 g_cmd = TRUE;
5580 }
5581 else
5582 {
5583 cmdchar = cap->cmdchar;
5584 g_cmd = FALSE;
5585 }
5586
5587 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5588 cmdchar = '#';
5589
5590 /*
5591 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5592 */
5593 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (checkclearopq(cap->oap))
5598 return;
5599 }
5600
5601 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5602 (cmdchar == '*' || cmdchar == '#')
5603 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5604 {
5605 clearop(cap->oap);
5606 return;
5607 }
5608
5609 /* Allocate buffer to put the command in. Inserting backslashes can
5610 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5611 * and some numbers. */
5612 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5613 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5614 || STRCMP(kp, ":help") == 0);
5615 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5616 if (buf == NULL)
5617 return;
5618 buf[0] = NUL;
5619
5620 switch (cmdchar)
5621 {
5622 case '*':
5623 case '#':
5624 /*
5625 * Put cursor at start of word, makes search skip the word
5626 * under the cursor.
5627 * Call setpcmark() first, so "*``" puts the cursor back where
5628 * it was.
5629 */
5630 setpcmark();
5631 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5632
5633 if (!g_cmd && vim_iswordp(ptr))
5634 STRCPY(buf, "\\<");
5635 no_smartcase = TRUE; /* don't use 'smartcase' now */
5636 break;
5637
5638 case 'K':
5639 if (kp_help)
5640 STRCPY(buf, "he! ");
5641 else
5642 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005643 /* An external command will probably use an argument starting
5644 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005645 while (*ptr == '-' && n > 0)
5646 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005647 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005648 --n;
5649 }
5650 if (n == 0)
5651 {
5652 EMSG(_(e_noident)); /* found dashes only */
5653 vim_free(buf);
5654 return;
5655 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 /* When a count is given, turn it into a range. Is this
5658 * really what we want? */
5659 isman = (STRCMP(kp, "man") == 0);
5660 isman_s = (STRCMP(kp, "man -s") == 0);
5661 if (cap->count0 != 0 && !(isman || isman_s))
5662 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5663
5664 STRCAT(buf, "! ");
5665 if (cap->count0 == 0 && isman_s)
5666 STRCAT(buf, "man");
5667 else
5668 STRCAT(buf, kp);
5669 STRCAT(buf, " ");
5670 if (cap->count0 != 0 && (isman || isman_s))
5671 {
5672 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5673 STRCAT(buf, " ");
5674 }
5675 }
5676 break;
5677
5678 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005679 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_CSCOPE
5681 if (p_cst)
5682 STRCPY(buf, "cstag ");
5683 else
5684#endif
5685 STRCPY(buf, "ts ");
5686 break;
5687
5688 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005689 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (curbuf->b_help)
5691 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005693 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005694 if (g_cmd)
5695 STRCPY(buf, "tj ");
5696 else
5697 sprintf((char *)buf, "%ldta ", cap->count0);
5698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
5701 /*
5702 * Now grab the chars in the identifier
5703 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005704 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005706 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005707 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005708 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005709 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005710 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005712 vim_free(buf);
5713 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005715 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5716 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005717 {
5718 vim_free(buf);
5719 vim_free(p);
5720 return;
5721 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005722 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005723 STRCAT(buf, p);
5724 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005726 else
5727 {
5728 if (cmdchar == '*')
5729 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5730 else if (cmdchar == '#')
5731 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005732 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005733 {
5734 if (curbuf->b_help)
5735 /* ":help" handles unescaped argument */
5736 aux_ptr = (char_u *)"";
5737 else
5738 aux_ptr = (char_u *)"\\|\"\n[";
5739 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005740 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741 aux_ptr = (char_u *)"\\|\"\n*?[";
5742
5743 p = buf + STRLEN(buf);
5744 while (n-- > 0)
5745 {
5746 /* put a backslash before \ and some others */
5747 if (vim_strchr(aux_ptr, *ptr) != NULL)
5748 *p++ = '\\';
5749#ifdef FEAT_MBYTE
5750 /* When current byte is a part of multibyte character, copy all
5751 * bytes of that character. */
5752 if (has_mbyte)
5753 {
5754 int i;
5755 int len = (*mb_ptr2len)(ptr) - 1;
5756
5757 for (i = 0; i < len && n >= 1; ++i, --n)
5758 *p++ = *ptr++;
5759 }
5760#endif
5761 *p++ = *ptr++;
5762 }
5763 *p = NUL;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 /*
5767 * Execute the command.
5768 */
5769 if (cmdchar == '*' || cmdchar == '#')
5770 {
5771 if (!g_cmd && (
5772#ifdef FEAT_MBYTE
5773 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5774#endif
5775 vim_iswordc(ptr[-1])))
5776 STRCAT(buf, "\\>");
5777#ifdef FEAT_CMDHIST
5778 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005779 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5781#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005782 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 else
5785 do_cmdline_cmd(buf);
5786
5787 vim_free(buf);
5788}
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790/*
5791 * Get visually selected text, within one line only.
5792 * Returns FAIL if more than one line selected.
5793 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005794 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795get_visual_text(cap, pp, lenp)
5796 cmdarg_T *cap;
5797 char_u **pp; /* return: start of selected text */
5798 int *lenp; /* return: length of selected text */
5799{
5800 if (VIsual_mode != 'V')
5801 unadjust_for_sel();
5802 if (VIsual.lnum != curwin->w_cursor.lnum)
5803 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005804 if (cap != NULL)
5805 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 return FAIL;
5807 }
5808 if (VIsual_mode == 'V')
5809 {
5810 *pp = ml_get_curline();
5811 *lenp = (int)STRLEN(*pp);
5812 }
5813 else
5814 {
5815 if (lt(curwin->w_cursor, VIsual))
5816 {
5817 *pp = ml_get_pos(&curwin->w_cursor);
5818 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5819 }
5820 else
5821 {
5822 *pp = ml_get_pos(&VIsual);
5823 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5824 }
5825#ifdef FEAT_MBYTE
5826 if (has_mbyte)
5827 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005828 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#endif
5830 }
5831 reset_VIsual_and_resel();
5832 return OK;
5833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835/*
5836 * CTRL-T: backwards in tag stack
5837 */
5838 static void
5839nv_tagpop(cap)
5840 cmdarg_T *cap;
5841{
5842 if (!checkclearopq(cap->oap))
5843 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5844}
5845
5846/*
5847 * Handle scrolling command 'H', 'L' and 'M'.
5848 */
5849 static void
5850nv_scroll(cap)
5851 cmdarg_T *cap;
5852{
5853 int used = 0;
5854 long n;
5855#ifdef FEAT_FOLDING
5856 linenr_T lnum;
5857#endif
5858 int half;
5859
5860 cap->oap->motion_type = MLINE;
5861 setpcmark();
5862
5863 if (cap->cmdchar == 'L')
5864 {
5865 validate_botline(); /* make sure curwin->w_botline is valid */
5866 curwin->w_cursor.lnum = curwin->w_botline - 1;
5867 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5868 curwin->w_cursor.lnum = 1;
5869 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005870 {
5871#ifdef FEAT_FOLDING
5872 if (hasAnyFolding(curwin))
5873 {
5874 /* Count a fold for one screen line. */
5875 for (n = cap->count1 - 1; n > 0
5876 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5877 {
5878 (void)hasFolding(curwin->w_cursor.lnum,
5879 &curwin->w_cursor.lnum, NULL);
5880 --curwin->w_cursor.lnum;
5881 }
5882 }
5883 else
5884#endif
5885 curwin->w_cursor.lnum -= cap->count1 - 1;
5886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
5888 else
5889 {
5890 if (cap->cmdchar == 'M')
5891 {
5892#ifdef FEAT_DIFF
5893 /* Don't count filler lines above the window. */
5894 used -= diff_check_fill(curwin, curwin->w_topline)
5895 - curwin->w_topfill;
5896#endif
5897 validate_botline(); /* make sure w_empty_rows is valid */
5898 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5899 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5900 {
5901#ifdef FEAT_DIFF
5902 /* Count half he number of filler lines to be "below this
5903 * line" and half to be "above the next line". */
5904 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5905 + n) / 2 >= half)
5906 {
5907 --n;
5908 break;
5909 }
5910#endif
5911 used += plines(curwin->w_topline + n);
5912 if (used >= half)
5913 break;
5914#ifdef FEAT_FOLDING
5915 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5916 n = lnum - curwin->w_topline;
5917#endif
5918 }
5919 if (n > 0 && used > curwin->w_height)
5920 --n;
5921 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005922 else /* (cap->cmdchar == 'H') */
5923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005925#ifdef FEAT_FOLDING
5926 if (hasAnyFolding(curwin))
5927 {
5928 /* Count a fold for one screen line. */
5929 lnum = curwin->w_topline;
5930 while (n-- > 0 && lnum < curwin->w_botline - 1)
5931 {
5932 hasFolding(lnum, NULL, &lnum);
5933 ++lnum;
5934 }
5935 n = lnum - curwin->w_topline;
5936 }
5937#endif
5938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 curwin->w_cursor.lnum = curwin->w_topline + n;
5940 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5941 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5942 }
5943
5944 cursor_correct(); /* correct for 'so' */
5945 beginline(BL_SOL | BL_FIX);
5946}
5947
5948/*
5949 * Cursor right commands.
5950 */
5951 static void
5952nv_right(cap)
5953 cmdarg_T *cap;
5954{
5955 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005956 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005958 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5959 {
5960 /* <C-Right> and <S-Right> move a word or WORD right */
5961 if (mod_mask & MOD_MASK_CTRL)
5962 cap->arg = TRUE;
5963 nv_wordcmd(cap);
5964 return;
5965 }
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cap->oap->motion_type = MCHAR;
5968 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005969 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005971#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005973 * In virtual edit mode, there's no such thing as "past_line", as lines
5974 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 */
5976 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005977 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#endif
5979
5980 for (n = cap->count1; n > 0; --n)
5981 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005982 if ((!past_line && oneright() == FAIL)
5983 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005984 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 {
5986 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005987 * <Space> wraps to next line if 'whichwrap' has 's'.
5988 * 'l' wraps to next line if 'whichwrap' has 'l'.
5989 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 if ( ((cap->cmdchar == ' '
5992 && vim_strchr(p_ww, 's') != NULL)
5993 || (cap->cmdchar == 'l'
5994 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005995 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 && vim_strchr(p_ww, '>') != NULL))
5997 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5998 {
5999 /* When deleting we also count the NL as a character.
6000 * Set cap->oap->inclusive when last char in the line is
6001 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006002 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 && !cap->oap->inclusive
6004 && !lineempty(curwin->w_cursor.lnum))
6005 cap->oap->inclusive = TRUE;
6006 else
6007 {
6008 ++curwin->w_cursor.lnum;
6009 curwin->w_cursor.col = 0;
6010#ifdef FEAT_VIRTUALEDIT
6011 curwin->w_cursor.coladd = 0;
6012#endif
6013 curwin->w_set_curswant = TRUE;
6014 cap->oap->inclusive = FALSE;
6015 }
6016 continue;
6017 }
6018 if (cap->oap->op_type == OP_NOP)
6019 {
6020 /* Only beep and flush if not moved at all */
6021 if (n == cap->count1)
6022 beep_flush();
6023 }
6024 else
6025 {
6026 if (!lineempty(curwin->w_cursor.lnum))
6027 cap->oap->inclusive = TRUE;
6028 }
6029 break;
6030 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006031 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 {
6033 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006034#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (virtual_active())
6036 oneright();
6037 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006040#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (has_mbyte)
6042 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006043 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 ++curwin->w_cursor.col;
6047 }
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050#ifdef FEAT_FOLDING
6051 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6052 && cap->oap->op_type == OP_NOP)
6053 foldOpenCursor();
6054#endif
6055}
6056
6057/*
6058 * Cursor left commands.
6059 *
6060 * Returns TRUE when operator end should not be adjusted.
6061 */
6062 static void
6063nv_left(cap)
6064 cmdarg_T *cap;
6065{
6066 long n;
6067
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006068 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6069 {
6070 /* <C-Left> and <S-Left> move a word or WORD left */
6071 if (mod_mask & MOD_MASK_CTRL)
6072 cap->arg = 1;
6073 nv_bck_word(cap);
6074 return;
6075 }
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 cap->oap->motion_type = MCHAR;
6078 cap->oap->inclusive = FALSE;
6079 for (n = cap->count1; n > 0; --n)
6080 {
6081 if (oneleft() == FAIL)
6082 {
6083 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6084 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6085 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6086 */
6087 if ( (((cap->cmdchar == K_BS
6088 || cap->cmdchar == Ctrl_H)
6089 && vim_strchr(p_ww, 'b') != NULL)
6090 || (cap->cmdchar == 'h'
6091 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006092 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 && vim_strchr(p_ww, '<') != NULL))
6094 && curwin->w_cursor.lnum > 1)
6095 {
6096 --(curwin->w_cursor.lnum);
6097 coladvance((colnr_T)MAXCOL);
6098 curwin->w_set_curswant = TRUE;
6099
6100 /* When the NL before the first char has to be deleted we
6101 * put the cursor on the NUL after the previous line.
6102 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006103 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if ( (cap->oap->op_type == OP_DELETE
6105 || cap->oap->op_type == OP_CHANGE)
6106 && !lineempty(curwin->w_cursor.lnum))
6107 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006108 char_u *cp = ml_get_cursor();
6109
6110 if (*cp != NUL)
6111 {
6112#ifdef FEAT_MBYTE
6113 if (has_mbyte)
6114 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6115 else
6116#endif
6117 ++curwin->w_cursor.col;
6118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 cap->retval |= CA_NO_ADJ_OP_END;
6120 }
6121 continue;
6122 }
6123 /* Only beep and flush if not moved at all */
6124 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6125 beep_flush();
6126 break;
6127 }
6128 }
6129#ifdef FEAT_FOLDING
6130 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6131 && cap->oap->op_type == OP_NOP)
6132 foldOpenCursor();
6133#endif
6134}
6135
6136/*
6137 * Cursor up commands.
6138 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6139 */
6140 static void
6141nv_up(cap)
6142 cmdarg_T *cap;
6143{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006144 if (mod_mask & MOD_MASK_SHIFT)
6145 {
6146 /* <S-Up> is page up */
6147 cap->arg = BACKWARD;
6148 nv_page(cap);
6149 }
6150 else
6151 {
6152 cap->oap->motion_type = MLINE;
6153 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6154 clearopbeep(cap->oap);
6155 else if (cap->arg)
6156 beginline(BL_WHITE | BL_FIX);
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158}
6159
6160/*
6161 * Cursor down commands.
6162 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6163 */
6164 static void
6165nv_down(cap)
6166 cmdarg_T *cap;
6167{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006168 if (mod_mask & MOD_MASK_SHIFT)
6169 {
6170 /* <S-Down> is page down */
6171 cap->arg = FORWARD;
6172 nv_page(cap);
6173 }
6174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6176 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006177 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006178 if (curwin->w_llist_ref == NULL)
6179 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6180 else
6181 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 else
6183#endif
6184 {
6185#ifdef FEAT_CMDWIN
6186 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006187 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 cmdwin_result = CAR;
6189 else
6190#endif
6191 {
6192 cap->oap->motion_type = MLINE;
6193 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6194 clearopbeep(cap->oap);
6195 else if (cap->arg)
6196 beginline(BL_WHITE | BL_FIX);
6197 }
6198 }
6199}
6200
6201#ifdef FEAT_SEARCHPATH
6202/*
6203 * Grab the file name under the cursor and edit it.
6204 */
6205 static void
6206nv_gotofile(cap)
6207 cmdarg_T *cap;
6208{
6209 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006210 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006212 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
6214 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006215 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 return;
6217 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006218#ifdef FEAT_AUTOCMD
6219 if (curbuf_locked())
6220 {
6221 clearop(cap->oap);
6222 return;
6223 }
6224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006226 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
6228 if (ptr != NULL)
6229 {
6230 /* do autowrite if necessary */
6231 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6232 autowrite(curbuf, FALSE);
6233 setpcmark();
6234 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006235 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006236 if (cap->nchar == 'F' && lnum >= 0)
6237 {
6238 curwin->w_cursor.lnum = lnum;
6239 check_cursor_lnum();
6240 beginline(BL_SOL | BL_FIX);
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 vim_free(ptr);
6243 }
6244 else
6245 clearop(cap->oap);
6246}
6247#endif
6248
6249/*
6250 * <End> command: to end of current line or last line.
6251 */
6252 static void
6253nv_end(cap)
6254 cmdarg_T *cap;
6255{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006256 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006258 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 nv_goto(cap);
6260 cap->count1 = 1; /* to end of current line */
6261 }
6262 nv_dollar(cap);
6263}
6264
6265/*
6266 * Handle the "$" command.
6267 */
6268 static void
6269nv_dollar(cap)
6270 cmdarg_T *cap;
6271{
6272 cap->oap->motion_type = MCHAR;
6273 cap->oap->inclusive = TRUE;
6274#ifdef FEAT_VIRTUALEDIT
6275 /* In virtual mode when off the edge of a line and an operator
6276 * is pending (whew!) keep the cursor where it is.
6277 * Otherwise, send it to the end of the line. */
6278 if (!virtual_active() || gchar_cursor() != NUL
6279 || cap->oap->op_type == OP_NOP)
6280#endif
6281 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6282 if (cursor_down((long)(cap->count1 - 1),
6283 cap->oap->op_type == OP_NOP) == FAIL)
6284 clearopbeep(cap->oap);
6285#ifdef FEAT_FOLDING
6286 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6287 foldOpenCursor();
6288#endif
6289}
6290
6291/*
6292 * Implementation of '?' and '/' commands.
6293 * If cap->arg is TRUE don't set PC mark.
6294 */
6295 static void
6296nv_search(cap)
6297 cmdarg_T *cap;
6298{
6299 oparg_T *oap = cap->oap;
6300
6301 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6302 {
6303 /* Translate "g??" to "g?g?" */
6304 cap->cmdchar = 'g';
6305 cap->nchar = '?';
6306 nv_operator(cap);
6307 return;
6308 }
6309
6310 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6311
6312 if (cap->searchbuf == NULL)
6313 {
6314 clearop(oap);
6315 return;
6316 }
6317
Bram Moolenaar46539112015-02-17 15:43:57 +01006318 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 (cap->arg ? 0 : SEARCH_MARK));
6320}
6321
6322/*
6323 * Handle "N" and "n" commands.
6324 * cap->arg is SEARCH_REV for "N", 0 for "n".
6325 */
6326 static void
6327nv_next(cap)
6328 cmdarg_T *cap;
6329{
Bram Moolenaar46539112015-02-17 15:43:57 +01006330 pos_T old = curwin->w_cursor;
6331 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6332
6333 if (i == 1 && equalpos(old, curwin->w_cursor))
6334 {
6335 /* Avoid getting stuck on the current cursor position, which can
6336 * happen when an offset is given and the cursor is on the last char
6337 * in the buffer: Repeat with count + 1. */
6338 cap->count1 += 1;
6339 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6340 cap->count1 -= 1;
6341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342}
6343
6344/*
6345 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6346 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006347 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006349 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350normal_search(cap, dir, pat, opt)
6351 cmdarg_T *cap;
6352 int dir;
6353 char_u *pat;
6354 int opt; /* extra flags for do_search() */
6355{
6356 int i;
6357
6358 cap->oap->motion_type = MCHAR;
6359 cap->oap->inclusive = FALSE;
6360 cap->oap->use_reg_one = TRUE;
6361 curwin->w_set_curswant = TRUE;
6362
6363 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006364 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (i == 0)
6366 clearop(cap->oap);
6367 else
6368 {
6369 if (i == 2)
6370 cap->oap->motion_type = MLINE;
6371#ifdef FEAT_VIRTUALEDIT
6372 curwin->w_cursor.coladd = 0;
6373#endif
6374#ifdef FEAT_FOLDING
6375 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6376 foldOpenCursor();
6377#endif
6378 }
6379
6380 /* "/$" will put the cursor after the end of the line, may need to
6381 * correct that here */
6382 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006383 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384}
6385
6386/*
6387 * Character search commands.
6388 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6389 * ',' and FALSE for ';'.
6390 * cap->nchar is NUL for ',' and ';' (repeat the search)
6391 */
6392 static void
6393nv_csearch(cap)
6394 cmdarg_T *cap;
6395{
6396 int t_cmd;
6397
6398 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6399 t_cmd = TRUE;
6400 else
6401 t_cmd = FALSE;
6402
6403 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6405 clearopbeep(cap->oap);
6406 else
6407 {
6408 curwin->w_set_curswant = TRUE;
6409#ifdef FEAT_VIRTUALEDIT
6410 /* Include a Tab for "tx" and for "dfx". */
6411 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6412 && (t_cmd || cap->oap->op_type != OP_NOP))
6413 {
6414 colnr_T scol, ecol;
6415
6416 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6417 curwin->w_cursor.coladd = ecol - scol;
6418 }
6419 else
6420 curwin->w_cursor.coladd = 0;
6421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423#ifdef FEAT_FOLDING
6424 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6425 foldOpenCursor();
6426#endif
6427 }
6428}
6429
6430/*
6431 * "[" and "]" commands.
6432 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6433 */
6434 static void
6435nv_brackets(cap)
6436 cmdarg_T *cap;
6437{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006438 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 pos_T prev_pos;
6440 pos_T *pos = NULL; /* init for GCC */
6441 pos_T old_pos; /* cursor position before command */
6442 int flag;
6443 long n;
6444 int findc;
6445 int c;
6446
6447 cap->oap->motion_type = MCHAR;
6448 cap->oap->inclusive = FALSE;
6449 old_pos = curwin->w_cursor;
6450#ifdef FEAT_VIRTUALEDIT
6451 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6452#endif
6453
6454#ifdef FEAT_SEARCHPATH
6455 /*
6456 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6457 */
6458 if (cap->nchar == 'f')
6459 nv_gotofile(cap);
6460 else
6461#endif
6462
6463#ifdef FEAT_FIND_ID
6464 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006465 * Find the occurrence(s) of the identifier or define under cursor
6466 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 *
6468 * search list jump
6469 * fwd bwd fwd bwd fwd bwd
6470 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6471 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6472 */
6473 if (vim_strchr((char_u *)
6474#ifdef EBCDIC
6475 "iI\005dD\067",
6476#else
6477 "iI\011dD\004",
6478#endif
6479 cap->nchar) != NULL)
6480 {
6481 char_u *ptr;
6482 int len;
6483
6484 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6485 clearop(cap->oap);
6486 else
6487 {
6488 find_pattern_in_path(ptr, 0, len, TRUE,
6489 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6490 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6491 cap->count1,
6492 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6493 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6494 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6495 (linenr_T)MAXLNUM);
6496 curwin->w_set_curswant = TRUE;
6497 }
6498 }
6499 else
6500#endif
6501
6502 /*
6503 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6504 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6505 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6506 * "[m" or "]m" search for prev/next start of (Java) method.
6507 * "[M" or "]M" search for prev/next end of (Java) method.
6508 */
6509 if ( (cap->cmdchar == '['
6510 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6511 || (cap->cmdchar == ']'
6512 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6513 {
6514 if (cap->nchar == '*')
6515 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 prev_pos.lnum = 0;
6517 if (cap->nchar == 'm' || cap->nchar == 'M')
6518 {
6519 if (cap->cmdchar == '[')
6520 findc = '{';
6521 else
6522 findc = '}';
6523 n = 9999;
6524 }
6525 else
6526 {
6527 findc = cap->nchar;
6528 n = cap->count1;
6529 }
6530 for ( ; n > 0; --n)
6531 {
6532 if ((pos = findmatchlimit(cap->oap, findc,
6533 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6534 {
6535 if (new_pos.lnum == 0) /* nothing found */
6536 {
6537 if (cap->nchar != 'm' && cap->nchar != 'M')
6538 clearopbeep(cap->oap);
6539 }
6540 else
6541 pos = &new_pos; /* use last one found */
6542 break;
6543 }
6544 prev_pos = new_pos;
6545 curwin->w_cursor = *pos;
6546 new_pos = *pos;
6547 }
6548 curwin->w_cursor = old_pos;
6549
6550 /*
6551 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6552 * brought us to the match for "[m" and "]M" when inside a method.
6553 * Try finding the '{' or '}' we want to be at.
6554 * Also repeat for the given count.
6555 */
6556 if (cap->nchar == 'm' || cap->nchar == 'M')
6557 {
6558 /* norm is TRUE for "]M" and "[m" */
6559 int norm = ((findc == '{') == (cap->nchar == 'm'));
6560
6561 n = cap->count1;
6562 /* found a match: we were inside a method */
6563 if (prev_pos.lnum != 0)
6564 {
6565 pos = &prev_pos;
6566 curwin->w_cursor = prev_pos;
6567 if (norm)
6568 --n;
6569 }
6570 else
6571 pos = NULL;
6572 while (n > 0)
6573 {
6574 for (;;)
6575 {
6576 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6577 {
6578 /* if not found anything, that's an error */
6579 if (pos == NULL)
6580 clearopbeep(cap->oap);
6581 n = 0;
6582 break;
6583 }
6584 c = gchar_cursor();
6585 if (c == '{' || c == '}')
6586 {
6587 /* Must have found end/start of class: use it.
6588 * Or found the place to be at. */
6589 if ((c == findc && norm) || (n == 1 && !norm))
6590 {
6591 new_pos = curwin->w_cursor;
6592 pos = &new_pos;
6593 n = 0;
6594 }
6595 /* if no match found at all, we started outside of the
6596 * class and we're inside now. Just go on. */
6597 else if (new_pos.lnum == 0)
6598 {
6599 new_pos = curwin->w_cursor;
6600 pos = &new_pos;
6601 }
6602 /* found start/end of other method: go to match */
6603 else if ((pos = findmatchlimit(cap->oap, findc,
6604 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6605 0)) == NULL)
6606 n = 0;
6607 else
6608 curwin->w_cursor = *pos;
6609 break;
6610 }
6611 }
6612 --n;
6613 }
6614 curwin->w_cursor = old_pos;
6615 if (pos == NULL && new_pos.lnum != 0)
6616 clearopbeep(cap->oap);
6617 }
6618 if (pos != NULL)
6619 {
6620 setpcmark();
6621 curwin->w_cursor = *pos;
6622 curwin->w_set_curswant = TRUE;
6623#ifdef FEAT_FOLDING
6624 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6625 && cap->oap->op_type == OP_NOP)
6626 foldOpenCursor();
6627#endif
6628 }
6629 }
6630
6631 /*
6632 * "[[", "[]", "]]" and "][": move to start or end of function
6633 */
6634 else if (cap->nchar == '[' || cap->nchar == ']')
6635 {
6636 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6637 flag = '{';
6638 else
6639 flag = '}'; /* "][" or "[]" */
6640
6641 curwin->w_set_curswant = TRUE;
6642 /*
6643 * Imitate strange Vi behaviour: When using "]]" with an operator
6644 * we also stop at '}'.
6645 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006646 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 (cap->oap->op_type != OP_NOP
6648 && cap->arg == FORWARD && flag == '{')))
6649 clearopbeep(cap->oap);
6650 else
6651 {
6652 if (cap->oap->op_type == OP_NOP)
6653 beginline(BL_WHITE | BL_FIX);
6654#ifdef FEAT_FOLDING
6655 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6656 foldOpenCursor();
6657#endif
6658 }
6659 }
6660
6661 /*
6662 * "[p", "[P", "]P" and "]p": put with indent adjustment
6663 */
6664 else if (cap->nchar == 'p' || cap->nchar == 'P')
6665 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006666 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006668 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6669 ? FORWARD : BACKWARD;
6670 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006671 int was_visual = VIsual_active;
6672 int line_count = curbuf->b_ml.ml_line_count;
6673 pos_T start, end;
6674
6675 if (VIsual_active)
6676 {
6677 start = ltoreq(VIsual, curwin->w_cursor)
6678 ? VIsual : curwin->w_cursor;
6679 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6680 curwin->w_cursor = (dir == BACKWARD ? start : end);
6681 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006682# ifdef FEAT_CLIPBOARD
6683 adjust_clip_reg(&regname);
6684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006686
6687 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006688 if (was_visual)
6689 {
6690 VIsual = start;
6691 curwin->w_cursor = end;
6692 if (dir == BACKWARD)
6693 {
6694 /* adjust lines */
6695 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6696 curwin->w_cursor.lnum +=
6697 curbuf->b_ml.ml_line_count - line_count;
6698 }
6699
6700 VIsual_active = TRUE;
6701 if (VIsual_mode == 'V')
6702 {
6703 /* delete visually selected lines */
6704 cap->cmdchar = 'd';
6705 cap->nchar = NUL;
6706 cap->oap->regname = regname;
6707 nv_operator(cap);
6708 do_pending_operator(cap, 0, FALSE);
6709 }
6710 if (VIsual_active)
6711 {
6712 end_visual_mode();
6713 redraw_later(SOME_VALID);
6714 }
6715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
6717 }
6718
6719 /*
6720 * "['", "[`", "]'" and "]`": jump to next mark
6721 */
6722 else if (cap->nchar == '\'' || cap->nchar == '`')
6723 {
6724 pos = &curwin->w_cursor;
6725 for (n = cap->count1; n > 0; --n)
6726 {
6727 prev_pos = *pos;
6728 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6729 cap->nchar == '\'');
6730 if (pos == NULL)
6731 break;
6732 }
6733 if (pos == NULL)
6734 pos = &prev_pos;
6735 nv_cursormark(cap, cap->nchar == '\'', pos);
6736 }
6737
6738#ifdef FEAT_MOUSE
6739 /*
6740 * [ or ] followed by a middle mouse click: put selected text with
6741 * indent adjustment. Any other button just does as usual.
6742 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006743 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {
6745 (void)do_mouse(cap->oap, cap->nchar,
6746 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6747 cap->count1, PUT_FIXINDENT);
6748 }
6749#endif /* FEAT_MOUSE */
6750
6751#ifdef FEAT_FOLDING
6752 /*
6753 * "[z" and "]z": move to start or end of open fold.
6754 */
6755 else if (cap->nchar == 'z')
6756 {
6757 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6758 cap->count1) == FAIL)
6759 clearopbeep(cap->oap);
6760 }
6761#endif
6762
6763#ifdef FEAT_DIFF
6764 /*
6765 * "[c" and "]c": move to next or previous diff-change.
6766 */
6767 else if (cap->nchar == 'c')
6768 {
6769 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6770 cap->count1) == FAIL)
6771 clearopbeep(cap->oap);
6772 }
6773#endif
6774
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006775#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006776 /*
6777 * "[s", "[S", "]s" and "]S": move to next spell error.
6778 */
6779 else if (cap->nchar == 's' || cap->nchar == 'S')
6780 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006781 setpcmark();
6782 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006783 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6784 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006785 {
6786 clearopbeep(cap->oap);
6787 break;
6788 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006789# ifdef FEAT_FOLDING
6790 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6791 foldOpenCursor();
6792# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006793 }
6794#endif
6795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 /* Not a valid cap->nchar. */
6797 else
6798 clearopbeep(cap->oap);
6799}
6800
6801/*
6802 * Handle Normal mode "%" command.
6803 */
6804 static void
6805nv_percent(cap)
6806 cmdarg_T *cap;
6807{
6808 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006809#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 linenr_T lnum = curwin->w_cursor.lnum;
6811#endif
6812
6813 cap->oap->inclusive = TRUE;
6814 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6815 {
6816 if (cap->count0 > 100)
6817 clearopbeep(cap->oap);
6818 else
6819 {
6820 cap->oap->motion_type = MLINE;
6821 setpcmark();
6822 /* Round up, so CTRL-G will give same value. Watch out for a
6823 * large line count, the line number must not go negative! */
6824 if (curbuf->b_ml.ml_line_count > 1000000)
6825 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6826 / 100L * cap->count0;
6827 else
6828 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6829 cap->count0 + 99L) / 100L;
6830 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6831 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6832 beginline(BL_SOL | BL_FIX);
6833 }
6834 }
6835 else /* "%" : go to matching paren */
6836 {
6837 cap->oap->motion_type = MCHAR;
6838 cap->oap->use_reg_one = TRUE;
6839 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6840 clearopbeep(cap->oap);
6841 else
6842 {
6843 setpcmark();
6844 curwin->w_cursor = *pos;
6845 curwin->w_set_curswant = TRUE;
6846#ifdef FEAT_VIRTUALEDIT
6847 curwin->w_cursor.coladd = 0;
6848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 }
6852#ifdef FEAT_FOLDING
6853 if (cap->oap->op_type == OP_NOP
6854 && lnum != curwin->w_cursor.lnum
6855 && (fdo_flags & FDO_PERCENT)
6856 && KeyTyped)
6857 foldOpenCursor();
6858#endif
6859}
6860
6861/*
6862 * Handle "(" and ")" commands.
6863 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6864 */
6865 static void
6866nv_brace(cap)
6867 cmdarg_T *cap;
6868{
6869 cap->oap->motion_type = MCHAR;
6870 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006871 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6872 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 curwin->w_set_curswant = TRUE;
6874
6875 if (findsent(cap->arg, cap->count1) == FAIL)
6876 clearopbeep(cap->oap);
6877 else
6878 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006879 /* Don't leave the cursor on the NUL past end of line. */
6880 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881#ifdef FEAT_VIRTUALEDIT
6882 curwin->w_cursor.coladd = 0;
6883#endif
6884#ifdef FEAT_FOLDING
6885 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6886 foldOpenCursor();
6887#endif
6888 }
6889}
6890
6891/*
6892 * "m" command: Mark a position.
6893 */
6894 static void
6895nv_mark(cap)
6896 cmdarg_T *cap;
6897{
6898 if (!checkclearop(cap->oap))
6899 {
6900 if (setmark(cap->nchar) == FAIL)
6901 clearopbeep(cap->oap);
6902 }
6903}
6904
6905/*
6906 * "{" and "}" commands.
6907 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6908 */
6909 static void
6910nv_findpar(cap)
6911 cmdarg_T *cap;
6912{
6913 cap->oap->motion_type = MCHAR;
6914 cap->oap->inclusive = FALSE;
6915 cap->oap->use_reg_one = TRUE;
6916 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006917 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 clearopbeep(cap->oap);
6919 else
6920 {
6921#ifdef FEAT_VIRTUALEDIT
6922 curwin->w_cursor.coladd = 0;
6923#endif
6924#ifdef FEAT_FOLDING
6925 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6926 foldOpenCursor();
6927#endif
6928 }
6929}
6930
6931/*
6932 * "u" command: Undo or make lower case.
6933 */
6934 static void
6935nv_undo(cap)
6936 cmdarg_T *cap;
6937{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006938 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6941 cap->cmdchar = 'g';
6942 cap->nchar = 'u';
6943 nv_operator(cap);
6944 }
6945 else
6946 nv_kundo(cap);
6947}
6948
6949/*
6950 * <Undo> command.
6951 */
6952 static void
6953nv_kundo(cap)
6954 cmdarg_T *cap;
6955{
6956 if (!checkclearopq(cap->oap))
6957 {
6958 u_undo((int)cap->count1);
6959 curwin->w_set_curswant = TRUE;
6960 }
6961}
6962
6963/*
6964 * Handle the "r" command.
6965 */
6966 static void
6967nv_replace(cap)
6968 cmdarg_T *cap;
6969{
6970 char_u *ptr;
6971 int had_ctrl_v;
6972 long n;
6973
6974 if (checkclearop(cap->oap))
6975 return;
6976
6977 /* get another character */
6978 if (cap->nchar == Ctrl_V)
6979 {
6980 had_ctrl_v = Ctrl_V;
6981 cap->nchar = get_literal();
6982 /* Don't redo a multibyte character with CTRL-V. */
6983 if (cap->nchar > DEL)
6984 had_ctrl_v = NUL;
6985 }
6986 else
6987 had_ctrl_v = NUL;
6988
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006989 /* Abort if the character is a special key. */
6990 if (IS_SPECIAL(cap->nchar))
6991 {
6992 clearopbeep(cap->oap);
6993 return;
6994 }
6995
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 /* Visual mode "r" */
6997 if (VIsual_active)
6998 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006999 if (got_int)
7000 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007001 if (had_ctrl_v)
7002 {
7003 if (cap->nchar == '\r')
7004 cap->nchar = -1;
7005 else if (cap->nchar == '\n')
7006 cap->nchar = -2;
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 nv_operator(cap);
7009 return;
7010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012#ifdef FEAT_VIRTUALEDIT
7013 /* Break tabs, etc. */
7014 if (virtual_active())
7015 {
7016 if (u_save_cursor() == FAIL)
7017 return;
7018 if (gchar_cursor() == NUL)
7019 {
7020 /* Add extra space and put the cursor on the first one. */
7021 coladvance_force((colnr_T)(getviscol() + cap->count1));
7022 curwin->w_cursor.col -= cap->count1;
7023 }
7024 else if (gchar_cursor() == TAB)
7025 coladvance_force(getviscol());
7026 }
7027#endif
7028
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007029 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007031 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032#ifdef FEAT_MBYTE
7033 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7034#endif
7035 )
7036 {
7037 clearopbeep(cap->oap);
7038 return;
7039 }
7040
7041 /*
7042 * Replacing with a TAB is done by edit() when it is complicated because
7043 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7044 * Other characters are done below to avoid problems with things like
7045 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7046 */
7047 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7048 {
7049 stuffnumReadbuff(cap->count1);
7050 stuffcharReadbuff('R');
7051 stuffcharReadbuff('\t');
7052 stuffcharReadbuff(ESC);
7053 return;
7054 }
7055
7056 /* save line for undo */
7057 if (u_save_cursor() == FAIL)
7058 return;
7059
7060 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7061 {
7062 /*
7063 * Replace character(s) by a single newline.
7064 * Strange vi behaviour: Only one newline is inserted.
7065 * Delete the characters here.
7066 * Insert the newline with an insert command, takes care of
7067 * autoindent. The insert command depends on being on the last
7068 * character of a line or not.
7069 */
7070#ifdef FEAT_MBYTE
7071 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7072#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007073 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#endif
7075 stuffcharReadbuff('\r');
7076 stuffcharReadbuff(ESC);
7077
7078 /* Give 'r' to edit(), to get the redo command right. */
7079 invoke_edit(cap, TRUE, 'r', FALSE);
7080 }
7081 else
7082 {
7083 prep_redo(cap->oap->regname, cap->count1,
7084 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7085
7086 curbuf->b_op_start = curwin->w_cursor;
7087#ifdef FEAT_MBYTE
7088 if (has_mbyte)
7089 {
7090 int old_State = State;
7091
7092 if (cap->ncharC1 != 0)
7093 AppendCharToRedobuff(cap->ncharC1);
7094 if (cap->ncharC2 != 0)
7095 AppendCharToRedobuff(cap->ncharC2);
7096
7097 /* This is slow, but it handles replacing a single-byte with a
7098 * multi-byte and the other way around. Also handles adding
7099 * composing characters for utf-8. */
7100 for (n = cap->count1; n > 0; --n)
7101 {
7102 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007103 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7104 {
7105 int c = ins_copychar(curwin->w_cursor.lnum
7106 + (cap->nchar == Ctrl_Y ? -1 : 1));
7107 if (c != NUL)
7108 ins_char(c);
7109 else
7110 /* will be decremented further down */
7111 ++curwin->w_cursor.col;
7112 }
7113 else
7114 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 State = old_State;
7116 if (cap->ncharC1 != 0)
7117 ins_char(cap->ncharC1);
7118 if (cap->ncharC2 != 0)
7119 ins_char(cap->ncharC2);
7120 }
7121 }
7122 else
7123#endif
7124 {
7125 /*
7126 * Replace the characters within one line.
7127 */
7128 for (n = cap->count1; n > 0; --n)
7129 {
7130 /*
7131 * Get ptr again, because u_save and/or showmatch() will have
7132 * released the line. At the same time we let know that the
7133 * line will be changed.
7134 */
7135 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007136 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7137 {
7138 int c = ins_copychar(curwin->w_cursor.lnum
7139 + (cap->nchar == Ctrl_Y ? -1 : 1));
7140 if (c != NUL)
7141 ptr[curwin->w_cursor.col] = c;
7142 }
7143 else
7144 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (p_sm && msg_silent == 0)
7146 showmatch(cap->nchar);
7147 ++curwin->w_cursor.col;
7148 }
7149#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007150 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007152 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
Bram Moolenaar009b2592004-10-24 19:18:58 +00007154 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007155 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007157 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159#endif
7160
7161 /* mark the buffer as changed and prepare for displaying */
7162 changed_bytes(curwin->w_cursor.lnum,
7163 (colnr_T)(curwin->w_cursor.col - cap->count1));
7164 }
7165 --curwin->w_cursor.col; /* cursor on the last replaced char */
7166#ifdef FEAT_MBYTE
7167 /* if the character on the left of the current cursor is a multi-byte
7168 * character, move two characters left */
7169 if (has_mbyte)
7170 mb_adjust_cursor();
7171#endif
7172 curbuf->b_op_end = curwin->w_cursor;
7173 curwin->w_set_curswant = TRUE;
7174 set_last_insert(cap->nchar);
7175 }
7176}
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * 'o': Exchange start and end of Visual area.
7180 * 'O': same, but in block mode exchange left and right corners.
7181 */
7182 static void
7183v_swap_corners(cmdchar)
7184 int cmdchar;
7185{
7186 pos_T old_cursor;
7187 colnr_T left, right;
7188
7189 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7190 {
7191 old_cursor = curwin->w_cursor;
7192 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7193 curwin->w_cursor.lnum = VIsual.lnum;
7194 coladvance(left);
7195 VIsual = curwin->w_cursor;
7196
7197 curwin->w_cursor.lnum = old_cursor.lnum;
7198 curwin->w_curswant = right;
7199 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7200 * right one column */
7201 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7202 ++curwin->w_curswant;
7203 coladvance(curwin->w_curswant);
7204 if (curwin->w_cursor.col == old_cursor.col
7205#ifdef FEAT_VIRTUALEDIT
7206 && (!virtual_active()
7207 || curwin->w_cursor.coladd == old_cursor.coladd)
7208#endif
7209 )
7210 {
7211 curwin->w_cursor.lnum = VIsual.lnum;
7212 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7213 ++right;
7214 coladvance(right);
7215 VIsual = curwin->w_cursor;
7216
7217 curwin->w_cursor.lnum = old_cursor.lnum;
7218 coladvance(left);
7219 curwin->w_curswant = left;
7220 }
7221 }
7222 else
7223 {
7224 old_cursor = curwin->w_cursor;
7225 curwin->w_cursor = VIsual;
7226 VIsual = old_cursor;
7227 curwin->w_set_curswant = TRUE;
7228 }
7229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
7231/*
7232 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7233 */
7234 static void
7235nv_Replace(cap)
7236 cmdarg_T *cap;
7237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (VIsual_active) /* "R" is replace lines */
7239 {
7240 cap->cmdchar = 'c';
7241 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007242 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 VIsual_mode = 'V';
7244 nv_operator(cap);
7245 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007246 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
7248 if (!curbuf->b_p_ma)
7249 EMSG(_(e_modifiable));
7250 else
7251 {
7252#ifdef FEAT_VIRTUALEDIT
7253 if (virtual_active())
7254 coladvance(getviscol());
7255#endif
7256 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7257 }
7258 }
7259}
7260
7261#ifdef FEAT_VREPLACE
7262/*
7263 * "gr".
7264 */
7265 static void
7266nv_vreplace(cap)
7267 cmdarg_T *cap;
7268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (VIsual_active)
7270 {
7271 cap->cmdchar = 'r';
7272 cap->nchar = cap->extra_char;
7273 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7274 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007275 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {
7277 if (!curbuf->b_p_ma)
7278 EMSG(_(e_modifiable));
7279 else
7280 {
7281 if (cap->extra_char == Ctrl_V) /* get another character */
7282 cap->extra_char = get_literal();
7283 stuffcharReadbuff(cap->extra_char);
7284 stuffcharReadbuff(ESC);
7285# ifdef FEAT_VIRTUALEDIT
7286 if (virtual_active())
7287 coladvance(getviscol());
7288# endif
7289 invoke_edit(cap, TRUE, 'v', FALSE);
7290 }
7291 }
7292}
7293#endif
7294
7295/*
7296 * Swap case for "~" command, when it does not work like an operator.
7297 */
7298 static void
7299n_swapchar(cap)
7300 cmdarg_T *cap;
7301{
7302 long n;
7303 pos_T startpos;
7304 int did_change = 0;
7305#ifdef FEAT_NETBEANS_INTG
7306 pos_T pos;
7307 char_u *ptr;
7308 int count;
7309#endif
7310
7311 if (checkclearopq(cap->oap))
7312 return;
7313
7314 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7315 {
7316 clearopbeep(cap->oap);
7317 return;
7318 }
7319
7320 prep_redo_cmd(cap);
7321
7322 if (u_save_cursor() == FAIL)
7323 return;
7324
7325 startpos = curwin->w_cursor;
7326#ifdef FEAT_NETBEANS_INTG
7327 pos = startpos;
7328#endif
7329 for (n = cap->count1; n > 0; --n)
7330 {
7331 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7332 inc_cursor();
7333 if (gchar_cursor() == NUL)
7334 {
7335 if (vim_strchr(p_ww, '~') != NULL
7336 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7337 {
7338#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007339 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
7341 if (did_change)
7342 {
7343 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007344 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007345 netbeans_removed(curbuf, pos.lnum, pos.col,
7346 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007348 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 }
7350 pos.col = 0;
7351 pos.lnum++;
7352 }
7353#endif
7354 ++curwin->w_cursor.lnum;
7355 curwin->w_cursor.col = 0;
7356 if (n > 1)
7357 {
7358 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7359 break;
7360 u_clearline();
7361 }
7362 }
7363 else
7364 break;
7365 }
7366 }
7367#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007368 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 ptr = ml_get(pos.lnum);
7371 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007372 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7373 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375#endif
7376
7377
7378 check_cursor();
7379 curwin->w_set_curswant = TRUE;
7380 if (did_change)
7381 {
7382 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7383 0L);
7384 curbuf->b_op_start = startpos;
7385 curbuf->b_op_end = curwin->w_cursor;
7386 if (curbuf->b_op_end.col > 0)
7387 --curbuf->b_op_end.col;
7388 }
7389}
7390
7391/*
7392 * Move cursor to mark.
7393 */
7394 static void
7395nv_cursormark(cap, flag, pos)
7396 cmdarg_T *cap;
7397 int flag;
7398 pos_T *pos;
7399{
7400 if (check_mark(pos) == FAIL)
7401 clearop(cap->oap);
7402 else
7403 {
7404 if (cap->cmdchar == '\''
7405 || cap->cmdchar == '`'
7406 || cap->cmdchar == '['
7407 || cap->cmdchar == ']')
7408 setpcmark();
7409 curwin->w_cursor = *pos;
7410 if (flag)
7411 beginline(BL_WHITE | BL_FIX);
7412 else
7413 check_cursor();
7414 }
7415 cap->oap->motion_type = flag ? MLINE : MCHAR;
7416 if (cap->cmdchar == '`')
7417 cap->oap->use_reg_one = TRUE;
7418 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7419 curwin->w_set_curswant = TRUE;
7420}
7421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422/*
7423 * Handle commands that are operators in Visual mode.
7424 */
7425 static void
7426v_visop(cap)
7427 cmdarg_T *cap;
7428{
7429 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7430
7431 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007432 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (isupper(cap->cmdchar))
7434 {
7435 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007436 {
7437 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7441 curwin->w_curswant = MAXCOL;
7442 }
7443 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7444 nv_operator(cap);
7445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
7447/*
7448 * "s" and "S" commands.
7449 */
7450 static void
7451nv_subst(cap)
7452 cmdarg_T *cap;
7453{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7455 {
7456 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007457 {
7458 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 cap->cmdchar = 'c';
7462 nv_operator(cap);
7463 }
7464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 nv_optrans(cap);
7466}
7467
7468/*
7469 * Abbreviated commands.
7470 */
7471 static void
7472nv_abbrev(cap)
7473 cmdarg_T *cap;
7474{
7475 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7476 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7477
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 /* in Visual mode these commands are operators */
7479 if (VIsual_active)
7480 v_visop(cap);
7481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 nv_optrans(cap);
7483}
7484
7485/*
7486 * Translate a command into another command.
7487 */
7488 static void
7489nv_optrans(cap)
7490 cmdarg_T *cap;
7491{
7492 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7493 (char_u *)"d$", (char_u *)"c$",
7494 (char_u *)"cl", (char_u *)"cc",
7495 (char_u *)"yy", (char_u *)":s\r"};
7496 static char_u *str = (char_u *)"xXDCsSY&";
7497
7498 if (!checkclearopq(cap->oap))
7499 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007500 /* In Vi "2D" doesn't delete the next line. Can't translate it
7501 * either, because "2." should also not use the count. */
7502 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7503 {
7504 cap->oap->start = curwin->w_cursor;
7505 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007506#ifdef FEAT_EVAL
7507 set_op_var(OP_DELETE);
7508#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007509 cap->count1 = 1;
7510 nv_dollar(cap);
7511 finish_op = TRUE;
7512 ResetRedobuff();
7513 AppendCharToRedobuff('D');
7514 }
7515 else
7516 {
7517 if (cap->count0)
7518 stuffnumReadbuff(cap->count0);
7519 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522 cap->opcount = 0;
7523}
7524
7525/*
7526 * "'" and "`" commands. Also for "g'" and "g`".
7527 * cap->arg is TRUE for "'" and "g'".
7528 */
7529 static void
7530nv_gomark(cap)
7531 cmdarg_T *cap;
7532{
7533 pos_T *pos;
7534 int c;
7535#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007536 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7538#endif
7539
7540 if (cap->cmdchar == 'g')
7541 c = cap->extra_char;
7542 else
7543 c = cap->nchar;
7544 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7545 if (pos == (pos_T *)-1) /* jumped to other file */
7546 {
7547 if (cap->arg)
7548 {
7549 check_cursor_lnum();
7550 beginline(BL_WHITE | BL_FIX);
7551 }
7552 else
7553 check_cursor();
7554 }
7555 else
7556 nv_cursormark(cap, cap->arg, pos);
7557
7558#ifdef FEAT_VIRTUALEDIT
7559 /* May need to clear the coladd that a mark includes. */
7560 if (!virtual_active())
7561 curwin->w_cursor.coladd = 0;
7562#endif
7563#ifdef FEAT_FOLDING
7564 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007565 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007566 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 && (fdo_flags & FDO_MARK)
7568 && old_KeyTyped)
7569 foldOpenCursor();
7570#endif
7571}
7572
7573/*
7574 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7575 */
7576 static void
7577nv_pcmark(cap)
7578 cmdarg_T *cap;
7579{
7580#ifdef FEAT_JUMPLIST
7581 pos_T *pos;
7582# ifdef FEAT_FOLDING
7583 linenr_T lnum = curwin->w_cursor.lnum;
7584 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7585# endif
7586
7587 if (!checkclearopq(cap->oap))
7588 {
7589 if (cap->cmdchar == 'g')
7590 pos = movechangelist((int)cap->count1);
7591 else
7592 pos = movemark((int)cap->count1);
7593 if (pos == (pos_T *)-1) /* jump to other file */
7594 {
7595 curwin->w_set_curswant = TRUE;
7596 check_cursor();
7597 }
7598 else if (pos != NULL) /* can jump */
7599 nv_cursormark(cap, FALSE, pos);
7600 else if (cap->cmdchar == 'g')
7601 {
7602 if (curbuf->b_changelistlen == 0)
7603 EMSG(_("E664: changelist is empty"));
7604 else if (cap->count1 < 0)
7605 EMSG(_("E662: At start of changelist"));
7606 else
7607 EMSG(_("E663: At end of changelist"));
7608 }
7609 else
7610 clearopbeep(cap->oap);
7611# ifdef FEAT_FOLDING
7612 if (cap->oap->op_type == OP_NOP
7613 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7614 && (fdo_flags & FDO_MARK)
7615 && old_KeyTyped)
7616 foldOpenCursor();
7617# endif
7618 }
7619#else
7620 clearopbeep(cap->oap);
7621#endif
7622}
7623
7624/*
7625 * Handle '"' command.
7626 */
7627 static void
7628nv_regname(cap)
7629 cmdarg_T *cap;
7630{
7631 if (checkclearop(cap->oap))
7632 return;
7633#ifdef FEAT_EVAL
7634 if (cap->nchar == '=')
7635 cap->nchar = get_expr_register();
7636#endif
7637 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7638 {
7639 cap->oap->regname = cap->nchar;
7640 cap->opcount = cap->count0; /* remember count before '"' */
7641#ifdef FEAT_EVAL
7642 set_reg_var(cap->oap->regname);
7643#endif
7644 }
7645 else
7646 clearopbeep(cap->oap);
7647}
7648
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649/*
7650 * Handle "v", "V" and "CTRL-V" commands.
7651 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7652 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007653 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 */
7655 static void
7656nv_visual(cap)
7657 cmdarg_T *cap;
7658{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 if (cap->cmdchar == Ctrl_Q)
7660 cap->cmdchar = Ctrl_V;
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7663 * characterwise, linewise, or blockwise. */
7664 if (cap->oap->op_type != OP_NOP)
7665 {
7666 cap->oap->motion_force = cap->cmdchar;
7667 finish_op = FALSE; /* operator doesn't finish now but later */
7668 return;
7669 }
7670
7671 VIsual_select = cap->arg;
7672 if (VIsual_active) /* change Visual mode */
7673 {
7674 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7675 end_visual_mode();
7676 else /* toggle char/block mode */
7677 { /* or char/line mode */
7678 VIsual_mode = cap->cmdchar;
7679 showmode();
7680 }
7681 redraw_curbuf_later(INVERTED); /* update the inversion */
7682 }
7683 else /* start Visual mode */
7684 {
7685 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007686 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007688 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 VIsual = curwin->w_cursor;
7690
7691 VIsual_active = TRUE;
7692 VIsual_reselect = TRUE;
7693 if (!cap->arg)
7694 /* start Select mode when 'selectmode' contains "cmd" */
7695 may_start_select('c');
7696#ifdef FEAT_MOUSE
7697 setmouse();
7698#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007699 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 redraw_cmdline = TRUE; /* show visual mode later */
7701 /*
7702 * For V and ^V, we multiply the number of lines even if there
7703 * was only one -- webb
7704 */
7705 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7706 {
7707 curwin->w_cursor.lnum +=
7708 resel_VIsual_line_count * cap->count0 - 1;
7709 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7711 }
7712 VIsual_mode = resel_VIsual_mode;
7713 if (VIsual_mode == 'v')
7714 {
7715 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007716 {
7717 validate_virtcol();
7718 curwin->w_curswant = curwin->w_virtcol
7719 + resel_VIsual_vcol * cap->count0 - 1;
7720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007722 curwin->w_curswant = resel_VIsual_vcol;
7723 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007725 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {
7727 curwin->w_curswant = MAXCOL;
7728 coladvance((colnr_T)MAXCOL);
7729 }
7730 else if (VIsual_mode == Ctrl_V)
7731 {
7732 validate_virtcol();
7733 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007734 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 coladvance(curwin->w_curswant);
7736 }
7737 else
7738 curwin->w_set_curswant = TRUE;
7739 redraw_curbuf_later(INVERTED); /* show the inversion */
7740 }
7741 else
7742 {
7743 if (!cap->arg)
7744 /* start Select mode when 'selectmode' contains "cmd" */
7745 may_start_select('c');
7746 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007747 if (VIsual_mode != 'V' && *p_sel == 'e')
7748 ++cap->count1; /* include one more char */
7749 if (cap->count0 > 0 && --cap->count1 > 0)
7750 {
7751 /* With a count select that many characters or lines. */
7752 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7753 nv_right(cap);
7754 else if (VIsual_mode == 'V')
7755 nv_down(cap);
7756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758 }
7759}
7760
7761/*
7762 * Start selection for Shift-movement keys.
7763 */
7764 void
7765start_selection()
7766{
7767 /* if 'selectmode' contains "key", start Select mode */
7768 may_start_select('k');
7769 n_start_visual_mode('v');
7770}
7771
7772/*
7773 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7774 */
7775 void
7776may_start_select(c)
7777 int c;
7778{
7779 VIsual_select = (stuff_empty() && typebuf_typed()
7780 && (vim_strchr(p_slm, c) != NULL));
7781}
7782
7783/*
7784 * Start Visual mode "c".
7785 * Should set VIsual_select before calling this.
7786 */
7787 static void
7788n_start_visual_mode(c)
7789 int c;
7790{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007791#ifdef FEAT_CONCEAL
7792 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007793 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007794#endif
7795
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 VIsual_mode = c;
7797 VIsual_active = TRUE;
7798 VIsual_reselect = TRUE;
7799#ifdef FEAT_VIRTUALEDIT
7800 /* Corner case: the 0 position in a tab may change when going into
7801 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7802 */
7803 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007804 {
7805 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808#endif
7809 VIsual = curwin->w_cursor;
7810
7811#ifdef FEAT_FOLDING
7812 foldAdjustVisual();
7813#endif
7814
7815#ifdef FEAT_MOUSE
7816 setmouse();
7817#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007818#ifdef FEAT_CONCEAL
7819 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007820 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007821#endif
7822
Bram Moolenaar09df3122006-01-23 22:23:09 +00007823 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 redraw_cmdline = TRUE; /* show visual mode later */
7825#ifdef FEAT_CLIPBOARD
7826 /* Make sure the clipboard gets updated. Needed because start and
7827 * end may still be the same, and the selection needs to be owned */
7828 clip_star.vmode = NUL;
7829#endif
7830
7831 /* Only need to redraw this line, unless still need to redraw an old
7832 * Visual area (when 'lazyredraw' is set). */
7833 if (curwin->w_redr_type < INVERTED)
7834 {
7835 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7836 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7837 }
7838}
7839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
7841/*
7842 * CTRL-W: Window commands
7843 */
7844 static void
7845nv_window(cap)
7846 cmdarg_T *cap;
7847{
7848#ifdef FEAT_WINDOWS
7849 if (!checkclearop(cap->oap))
7850 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7851#else
7852 (void)checkclearop(cap->oap);
7853#endif
7854}
7855
7856/*
7857 * CTRL-Z: Suspend
7858 */
7859 static void
7860nv_suspend(cap)
7861 cmdarg_T *cap;
7862{
7863 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (VIsual_active)
7865 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 do_cmdline_cmd((char_u *)"st");
7867}
7868
7869/*
7870 * Commands starting with "g".
7871 */
7872 static void
7873nv_g_cmd(cap)
7874 cmdarg_T *cap;
7875{
7876 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 int i;
7879 int flag = FALSE;
7880
7881 switch (cap->nchar)
7882 {
7883#ifdef MEM_PROFILE
7884 /*
7885 * "g^A": dump log of used memory.
7886 */
7887 case Ctrl_A:
7888 vim_mem_profile_dump();
7889 break;
7890#endif
7891
7892#ifdef FEAT_VREPLACE
7893 /*
7894 * "gR": Enter virtual replace mode.
7895 */
7896 case 'R':
7897 cap->arg = TRUE;
7898 nv_Replace(cap);
7899 break;
7900
7901 case 'r':
7902 nv_vreplace(cap);
7903 break;
7904#endif
7905
7906 case '&':
7907 do_cmdline_cmd((char_u *)"%s//~/&");
7908 break;
7909
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 /*
7911 * "gv": Reselect the previous Visual area. If Visual already active,
7912 * exchange previous and current Visual area.
7913 */
7914 case 'v':
7915 if (checkclearop(oap))
7916 break;
7917
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007918 if ( curbuf->b_visual.vi_start.lnum == 0
7919 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7920 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 beep_flush();
7922 else
7923 {
7924 /* set w_cursor to the start of the Visual area, tpos to the end */
7925 if (VIsual_active)
7926 {
7927 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007928 VIsual_mode = curbuf->b_visual.vi_mode;
7929 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930# ifdef FEAT_EVAL
7931 curbuf->b_visual_mode_eval = i;
7932# endif
7933 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007934 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7935 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007937 tpos = curbuf->b_visual.vi_end;
7938 curbuf->b_visual.vi_end = curwin->w_cursor;
7939 curwin->w_cursor = curbuf->b_visual.vi_start;
7940 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 }
7942 else
7943 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007944 VIsual_mode = curbuf->b_visual.vi_mode;
7945 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7946 tpos = curbuf->b_visual.vi_end;
7947 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949
7950 VIsual_active = TRUE;
7951 VIsual_reselect = TRUE;
7952
7953 /* Set Visual to the start and w_cursor to the end of the Visual
7954 * area. Make sure they are on an existing character. */
7955 check_cursor();
7956 VIsual = curwin->w_cursor;
7957 curwin->w_cursor = tpos;
7958 check_cursor();
7959 update_topline();
7960 /*
7961 * When called from normal "g" command: start Select mode when
7962 * 'selectmode' contains "cmd". When called for K_SELECT, always
7963 * start Select mode.
7964 */
7965 if (cap->arg)
7966 VIsual_select = TRUE;
7967 else
7968 may_start_select('c');
7969#ifdef FEAT_MOUSE
7970 setmouse();
7971#endif
7972#ifdef FEAT_CLIPBOARD
7973 /* Make sure the clipboard gets updated. Needed because start and
7974 * end are still the same, and the selection needs to be owned */
7975 clip_star.vmode = NUL;
7976#endif
7977 redraw_curbuf_later(INVERTED);
7978 showmode();
7979 }
7980 break;
7981 /*
7982 * "gV": Don't reselect the previous Visual area after a Select mode
7983 * mapping of menu.
7984 */
7985 case 'V':
7986 VIsual_reselect = FALSE;
7987 break;
7988
7989 /*
7990 * "gh": start Select mode.
7991 * "gH": start Select line mode.
7992 * "g^H": start Select block mode.
7993 */
7994 case K_BS:
7995 cap->nchar = Ctrl_H;
7996 /* FALLTHROUGH */
7997 case 'h':
7998 case 'H':
7999 case Ctrl_H:
8000# ifdef EBCDIC
8001 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8002 if (cap->nchar == Ctrl_H)
8003 cap->cmdchar = Ctrl_V;
8004 else
8005# endif
8006 cap->cmdchar = cap->nchar + ('v' - 'h');
8007 cap->arg = TRUE;
8008 nv_visual(cap);
8009 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008010
8011 /* "gn", "gN" visually select next/previous search match
8012 * "gn" selects next match
8013 * "gN" selects previous match
8014 */
8015 case 'N':
8016 case 'n':
8017 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008018 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
8021 /*
8022 * "gj" and "gk" two new funny movement keys -- up and down
8023 * movement based on *screen* line rather than *file* line.
8024 */
8025 case 'j':
8026 case K_DOWN:
8027 /* with 'nowrap' it works just like the normal "j" command; also when
8028 * in a closed fold */
8029 if (!curwin->w_p_wrap
8030#ifdef FEAT_FOLDING
8031 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8032#endif
8033 )
8034 {
8035 oap->motion_type = MLINE;
8036 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8037 }
8038 else
8039 i = nv_screengo(oap, FORWARD, cap->count1);
8040 if (i == FAIL)
8041 clearopbeep(oap);
8042 break;
8043
8044 case 'k':
8045 case K_UP:
8046 /* with 'nowrap' it works just like the normal "k" command; also when
8047 * in a closed fold */
8048 if (!curwin->w_p_wrap
8049#ifdef FEAT_FOLDING
8050 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8051#endif
8052 )
8053 {
8054 oap->motion_type = MLINE;
8055 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8056 }
8057 else
8058 i = nv_screengo(oap, BACKWARD, cap->count1);
8059 if (i == FAIL)
8060 clearopbeep(oap);
8061 break;
8062
8063 /*
8064 * "gJ": join two lines without inserting a space.
8065 */
8066 case 'J':
8067 nv_join(cap);
8068 break;
8069
8070 /*
8071 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8072 * "gm": middle of "g0" and "g$".
8073 */
8074 case '^':
8075 flag = TRUE;
8076 /* FALLTHROUGH */
8077
8078 case '0':
8079 case 'm':
8080 case K_HOME:
8081 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 oap->motion_type = MCHAR;
8083 oap->inclusive = FALSE;
8084 if (curwin->w_p_wrap
8085#ifdef FEAT_VERTSPLIT
8086 && curwin->w_width != 0
8087#endif
8088 )
8089 {
8090 int width1 = W_WIDTH(curwin) - curwin_col_off();
8091 int width2 = width1 + curwin_col_off2();
8092
8093 validate_virtcol();
8094 i = 0;
8095 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8096 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8097 }
8098 else
8099 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008100 /* Go to the middle of the screen line. When 'number' or
8101 * 'relativenumber' is on and lines are wrapping the middle can be more
8102 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 if (cap->nchar == 'm')
8104 i += (W_WIDTH(curwin) - curwin_col_off()
8105 + ((curwin->w_p_wrap && i > 0)
8106 ? curwin_col_off2() : 0)) / 2;
8107 coladvance((colnr_T)i);
8108 if (flag)
8109 {
8110 do
8111 i = gchar_cursor();
8112 while (vim_iswhite(i) && oneright() == OK);
8113 }
8114 curwin->w_set_curswant = TRUE;
8115 break;
8116
8117 case '_':
8118 /* "g_": to the last non-blank character in the line or <count> lines
8119 * downward. */
8120 cap->oap->motion_type = MCHAR;
8121 cap->oap->inclusive = TRUE;
8122 curwin->w_curswant = MAXCOL;
8123 if (cursor_down((long)(cap->count1 - 1),
8124 cap->oap->op_type == OP_NOP) == FAIL)
8125 clearopbeep(cap->oap);
8126 else
8127 {
8128 char_u *ptr = ml_get_curline();
8129
8130 /* In Visual mode we may end up after the line. */
8131 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8132 --curwin->w_cursor.col;
8133
8134 /* Decrease the cursor column until it's on a non-blank. */
8135 while (curwin->w_cursor.col > 0
8136 && vim_iswhite(ptr[curwin->w_cursor.col]))
8137 --curwin->w_cursor.col;
8138 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008139 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 }
8141 break;
8142
8143 case '$':
8144 case K_END:
8145 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
8147 int col_off = curwin_col_off();
8148
8149 oap->motion_type = MCHAR;
8150 oap->inclusive = TRUE;
8151 if (curwin->w_p_wrap
8152#ifdef FEAT_VERTSPLIT
8153 && curwin->w_width != 0
8154#endif
8155 )
8156 {
8157 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8158 if (cap->count1 == 1)
8159 {
8160 int width1 = W_WIDTH(curwin) - col_off;
8161 int width2 = width1 + curwin_col_off2();
8162
8163 validate_virtcol();
8164 i = width1 - 1;
8165 if (curwin->w_virtcol >= (colnr_T)width1)
8166 i += ((curwin->w_virtcol - width1) / width2 + 1)
8167 * width2;
8168 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008169
8170 /* Make sure we stick in this column. */
8171 validate_virtcol();
8172 curwin->w_curswant = curwin->w_virtcol;
8173 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8175 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8176 {
8177 /*
8178 * Check for landing on a character that got split at
8179 * the end of the line. We do not want to advance to
8180 * the next screen line.
8181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 if (curwin->w_virtcol > (colnr_T)i)
8183 --curwin->w_cursor.col;
8184 }
8185#endif
8186 }
8187 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8188 clearopbeep(oap);
8189 }
8190 else
8191 {
8192 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8193 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008194
8195 /* Make sure we stick in this column. */
8196 validate_virtcol();
8197 curwin->w_curswant = curwin->w_virtcol;
8198 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200 }
8201 break;
8202
8203 /*
8204 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8205 */
8206 case '*':
8207 case '#':
8208#if POUND != '#'
8209 case POUND: /* pound sign (sometimes equal to '#') */
8210#endif
8211 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8212 case ']': /* :tselect for current identifier */
8213 nv_ident(cap);
8214 break;
8215
8216 /*
8217 * ge and gE: go back to end of word
8218 */
8219 case 'e':
8220 case 'E':
8221 oap->motion_type = MCHAR;
8222 curwin->w_set_curswant = TRUE;
8223 oap->inclusive = TRUE;
8224 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8225 clearopbeep(oap);
8226 break;
8227
8228 /*
8229 * "g CTRL-G": display info about cursor position
8230 */
8231 case Ctrl_G:
8232 cursor_pos_info();
8233 break;
8234
8235 /*
8236 * "gi": start Insert at the last position.
8237 */
8238 case 'i':
8239 if (curbuf->b_last_insert.lnum != 0)
8240 {
8241 curwin->w_cursor = curbuf->b_last_insert;
8242 check_cursor_lnum();
8243 i = (int)STRLEN(ml_get_curline());
8244 if (curwin->w_cursor.col > (colnr_T)i)
8245 {
8246#ifdef FEAT_VIRTUALEDIT
8247 if (virtual_active())
8248 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8249#endif
8250 curwin->w_cursor.col = i;
8251 }
8252 }
8253 cap->cmdchar = 'i';
8254 nv_edit(cap);
8255 break;
8256
8257 /*
8258 * "gI": Start insert in column 1.
8259 */
8260 case 'I':
8261 beginline(0);
8262 if (!checkclearopq(oap))
8263 invoke_edit(cap, FALSE, 'g', FALSE);
8264 break;
8265
8266#ifdef FEAT_SEARCHPATH
8267 /*
8268 * "gf": goto file, edit file under cursor
8269 * "]f" and "[f": can also be used.
8270 */
8271 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008272 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 nv_gotofile(cap);
8274 break;
8275#endif
8276
8277 /* "g'm" and "g`m": jump to mark without setting pcmark */
8278 case '\'':
8279 cap->arg = TRUE;
8280 /*FALLTHROUGH*/
8281 case '`':
8282 nv_gomark(cap);
8283 break;
8284
8285 /*
8286 * "gs": Goto sleep.
8287 */
8288 case 's':
8289 do_sleep(cap->count1 * 1000L);
8290 break;
8291
8292 /*
8293 * "ga": Display the ascii value of the character under the
8294 * cursor. It is displayed in decimal, hex, and octal. -- webb
8295 */
8296 case 'a':
8297 do_ascii(NULL);
8298 break;
8299
8300#ifdef FEAT_MBYTE
8301 /*
8302 * "g8": Display the bytes used for the UTF-8 character under the
8303 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008304 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 */
8306 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008307 if (cap->count0 == 8)
8308 utf_find_illegal();
8309 else
8310 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
8312#endif
8313
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008314 case '<':
8315 show_sb_text();
8316 break;
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 /*
8319 * "gg": Goto the first line in file. With a count it goes to
8320 * that line number like for "G". -- webb
8321 */
8322 case 'g':
8323 cap->arg = FALSE;
8324 nv_goto(cap);
8325 break;
8326
8327 /*
8328 * Two-character operators:
8329 * "gq" Format text
8330 * "gw" Format text and keep cursor position
8331 * "g~" Toggle the case of the text.
8332 * "gu" Change text to lower case.
8333 * "gU" Change text to upper case.
8334 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008335 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 */
8337 case 'q':
8338 case 'w':
8339 oap->cursor_start = curwin->w_cursor;
8340 /*FALLTHROUGH*/
8341 case '~':
8342 case 'u':
8343 case 'U':
8344 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008345 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 nv_operator(cap);
8347 break;
8348
8349 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008350 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 * current function
8352 * "gD": idem, but in the current file.
8353 */
8354 case 'd':
8355 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008356 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 break;
8358
8359#ifdef FEAT_MOUSE
8360 /*
8361 * g<*Mouse> : <C-*mouse>
8362 */
8363 case K_MIDDLEMOUSE:
8364 case K_MIDDLEDRAG:
8365 case K_MIDDLERELEASE:
8366 case K_LEFTMOUSE:
8367 case K_LEFTDRAG:
8368 case K_LEFTRELEASE:
8369 case K_RIGHTMOUSE:
8370 case K_RIGHTDRAG:
8371 case K_RIGHTRELEASE:
8372 case K_X1MOUSE:
8373 case K_X1DRAG:
8374 case K_X1RELEASE:
8375 case K_X2MOUSE:
8376 case K_X2DRAG:
8377 case K_X2RELEASE:
8378 mod_mask = MOD_MASK_CTRL;
8379 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8380 break;
8381#endif
8382
8383 case K_IGNORE:
8384 break;
8385
8386 /*
8387 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8388 */
8389 case 'p':
8390 case 'P':
8391 nv_put(cap);
8392 break;
8393
8394#ifdef FEAT_BYTEOFF
8395 /* "go": goto byte count from start of buffer */
8396 case 'o':
8397 goto_byte(cap->count0);
8398 break;
8399#endif
8400
8401 /* "gQ": improved Ex mode */
8402 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008403 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {
8405 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008406 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 break;
8408 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (!checkclearopq(oap))
8411 do_exmode(TRUE);
8412 break;
8413
8414#ifdef FEAT_JUMPLIST
8415 case ',':
8416 nv_pcmark(cap);
8417 break;
8418
8419 case ';':
8420 cap->count1 = -cap->count1;
8421 nv_pcmark(cap);
8422 break;
8423#endif
8424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008425#ifdef FEAT_WINDOWS
8426 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008427 if (!checkclearop(oap))
8428 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008429 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008430 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008431 if (!checkclearop(oap))
8432 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008433 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008434#endif
8435
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008436 case '+':
8437 case '-': /* "g+" and "g-": undo or redo along the timeline */
8438 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008439 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008440 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008441 break;
8442
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 default:
8444 clearopbeep(oap);
8445 break;
8446 }
8447}
8448
8449/*
8450 * Handle "o" and "O" commands.
8451 */
8452 static void
8453n_opencmd(cap)
8454 cmdarg_T *cap;
8455{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008456#ifdef FEAT_CONCEAL
8457 linenr_T oldline = curwin->w_cursor.lnum;
8458#endif
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (!checkclearopq(cap->oap))
8461 {
8462#ifdef FEAT_FOLDING
8463 if (cap->cmdchar == 'O')
8464 /* Open above the first line of a folded sequence of lines */
8465 (void)hasFolding(curwin->w_cursor.lnum,
8466 &curwin->w_cursor.lnum, NULL);
8467 else
8468 /* Open below the last line of a folded sequence of lines */
8469 (void)hasFolding(curwin->w_cursor.lnum,
8470 NULL, &curwin->w_cursor.lnum);
8471#endif
8472 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8473 (cap->cmdchar == 'O' ? 1 : 0)),
8474 (linenr_T)(curwin->w_cursor.lnum +
8475 (cap->cmdchar == 'o' ? 1 : 0))
8476 ) == OK
8477 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8478#ifdef FEAT_COMMENTS
8479 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8480#endif
8481 0, 0))
8482 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008483#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008484 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008485 update_single_line(curwin, oldline);
8486#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008487 /* When '#' is in 'cpoptions' ignore the count. */
8488 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8489 cap->count1 = 1;
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008490 if (curwin->w_p_cul)
8491 /* force redraw of cursorline */
8492 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8494 }
8495 }
8496}
8497
8498/*
8499 * "." command: redo last change.
8500 */
8501 static void
8502nv_dot(cap)
8503 cmdarg_T *cap;
8504{
8505 if (!checkclearopq(cap->oap))
8506 {
8507 /*
8508 * If "restart_edit" is TRUE, the last but one command is repeated
8509 * instead of the last command (inserting text). This is used for
8510 * CTRL-O <.> in insert mode.
8511 */
8512 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8513 clearopbeep(cap->oap);
8514 }
8515}
8516
8517/*
8518 * CTRL-R: undo undo
8519 */
8520 static void
8521nv_redo(cap)
8522 cmdarg_T *cap;
8523{
8524 if (!checkclearopq(cap->oap))
8525 {
8526 u_redo((int)cap->count1);
8527 curwin->w_set_curswant = TRUE;
8528 }
8529}
8530
8531/*
8532 * Handle "U" command.
8533 */
8534 static void
8535nv_Undo(cap)
8536 cmdarg_T *cap;
8537{
8538 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008539 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 /* translate "gUU" to "gUgU" */
8542 cap->cmdchar = 'g';
8543 cap->nchar = 'U';
8544 nv_operator(cap);
8545 }
8546 else if (!checkclearopq(cap->oap))
8547 {
8548 u_undoline();
8549 curwin->w_set_curswant = TRUE;
8550 }
8551}
8552
8553/*
8554 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8555 * single character.
8556 */
8557 static void
8558nv_tilde(cap)
8559 cmdarg_T *cap;
8560{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008561 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 n_swapchar(cap);
8563 else
8564 nv_operator(cap);
8565}
8566
8567/*
8568 * Handle an operator command.
8569 * The actual work is done by do_pending_operator().
8570 */
8571 static void
8572nv_operator(cap)
8573 cmdarg_T *cap;
8574{
8575 int op_type;
8576
8577 op_type = get_op_type(cap->cmdchar, cap->nchar);
8578
8579 if (op_type == cap->oap->op_type) /* double operator works on lines */
8580 nv_lineop(cap);
8581 else if (!checkclearop(cap->oap))
8582 {
8583 cap->oap->start = curwin->w_cursor;
8584 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008585#ifdef FEAT_EVAL
8586 set_op_var(op_type);
8587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589}
8590
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008591#ifdef FEAT_EVAL
8592/*
8593 * Set v:operator to the characters for "optype".
8594 */
8595 static void
8596set_op_var(optype)
8597 int optype;
8598{
8599 char_u opchars[3];
8600
8601 if (optype == OP_NOP)
8602 set_vim_var_string(VV_OP, NULL, 0);
8603 else
8604 {
8605 opchars[0] = get_op_char(optype);
8606 opchars[1] = get_extra_op_char(optype);
8607 opchars[2] = NUL;
8608 set_vim_var_string(VV_OP, opchars, -1);
8609 }
8610}
8611#endif
8612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613/*
8614 * Handle linewise operator "dd", "yy", etc.
8615 *
8616 * "_" is is a strange motion command that helps make operators more logical.
8617 * It is actually implemented, but not documented in the real Vi. This motion
8618 * command actually refers to "the current line". Commands like "dd" and "yy"
8619 * are really an alternate form of "d_" and "y_". It does accept a count, so
8620 * "d3_" works to delete 3 lines.
8621 */
8622 static void
8623nv_lineop(cap)
8624 cmdarg_T *cap;
8625{
8626 cap->oap->motion_type = MLINE;
8627 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8628 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008629 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8630 && cap->oap->motion_force != 'v'
8631 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 || cap->oap->op_type == OP_LSHIFT
8633 || cap->oap->op_type == OP_RSHIFT)
8634 beginline(BL_SOL | BL_FIX);
8635 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8636 beginline(BL_WHITE | BL_FIX);
8637}
8638
8639/*
8640 * <Home> command.
8641 */
8642 static void
8643nv_home(cap)
8644 cmdarg_T *cap;
8645{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008646 /* CTRL-HOME is like "gg" */
8647 if (mod_mask & MOD_MASK_CTRL)
8648 nv_goto(cap);
8649 else
8650 {
8651 cap->count0 = 1;
8652 nv_pipe(cap);
8653 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008654 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8655 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
8657
8658/*
8659 * "|" command.
8660 */
8661 static void
8662nv_pipe(cap)
8663 cmdarg_T *cap;
8664{
8665 cap->oap->motion_type = MCHAR;
8666 cap->oap->inclusive = FALSE;
8667 beginline(0);
8668 if (cap->count0 > 0)
8669 {
8670 coladvance((colnr_T)(cap->count0 - 1));
8671 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8672 }
8673 else
8674 curwin->w_curswant = 0;
8675 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008676 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 curwin->w_set_curswant = FALSE;
8678}
8679
8680/*
8681 * Handle back-word command "b" and "B".
8682 * cap->arg is 1 for "B"
8683 */
8684 static void
8685nv_bck_word(cap)
8686 cmdarg_T *cap;
8687{
8688 cap->oap->motion_type = MCHAR;
8689 cap->oap->inclusive = FALSE;
8690 curwin->w_set_curswant = TRUE;
8691 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8692 clearopbeep(cap->oap);
8693#ifdef FEAT_FOLDING
8694 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8695 foldOpenCursor();
8696#endif
8697}
8698
8699/*
8700 * Handle word motion commands "e", "E", "w" and "W".
8701 * cap->arg is TRUE for "E" and "W".
8702 */
8703 static void
8704nv_wordcmd(cap)
8705 cmdarg_T *cap;
8706{
8707 int n;
8708 int word_end;
8709 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008710 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
8712 /*
8713 * Set inclusive for the "E" and "e" command.
8714 */
8715 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8716 word_end = TRUE;
8717 else
8718 word_end = FALSE;
8719 cap->oap->inclusive = word_end;
8720
8721 /*
8722 * "cw" and "cW" are a special case.
8723 */
8724 if (!word_end && cap->oap->op_type == OP_CHANGE)
8725 {
8726 n = gchar_cursor();
8727 if (n != NUL) /* not an empty line */
8728 {
8729 if (vim_iswhite(n))
8730 {
8731 /*
8732 * Reproduce a funny Vi behaviour: "cw" on a blank only
8733 * changes one character, not all blanks until the start of
8734 * the next word. Only do this when the 'w' flag is included
8735 * in 'cpoptions'.
8736 */
8737 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8738 {
8739 cap->oap->inclusive = TRUE;
8740 cap->oap->motion_type = MCHAR;
8741 return;
8742 }
8743 }
8744 else
8745 {
8746 /*
8747 * This is a little strange. To match what the real Vi does,
8748 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8749 * that we are not on a space or a TAB. This seems impolite
8750 * at first, but it's really more what we mean when we say
8751 * 'cw'.
8752 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008753 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 * will change only one character! This is done by setting
8755 * flag.
8756 */
8757 cap->oap->inclusive = TRUE;
8758 word_end = TRUE;
8759 flag = TRUE;
8760 }
8761 }
8762 }
8763
8764 cap->oap->motion_type = MCHAR;
8765 curwin->w_set_curswant = TRUE;
8766 if (word_end)
8767 n = end_word(cap->count1, cap->arg, flag, FALSE);
8768 else
8769 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8770
Bram Moolenaardfefb982008-04-01 10:06:39 +00008771 /* Don't leave the cursor on the NUL past the end of line. Unless we
8772 * didn't move it forward. */
8773 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008774 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
8776 if (n == FAIL && cap->oap->op_type == OP_NOP)
8777 clearopbeep(cap->oap);
8778 else
8779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#ifdef FEAT_FOLDING
8782 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8783 foldOpenCursor();
8784#endif
8785 }
8786}
8787
8788/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008789 * Used after a movement command: If the cursor ends up on the NUL after the
8790 * end of the line, may move it back to the last character and make the motion
8791 * inclusive.
8792 */
8793 static void
8794adjust_cursor(oap)
8795 oparg_T *oap;
8796{
8797 /* The cursor cannot remain on the NUL when:
8798 * - the column is > 0
8799 * - not in Visual mode or 'selection' is "o"
8800 * - 'virtualedit' is not "all" and not "onemore".
8801 */
8802 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008803 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008804#ifdef FEAT_VIRTUALEDIT
8805 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8806#endif
8807 )
8808 {
8809 --curwin->w_cursor.col;
8810#ifdef FEAT_MBYTE
8811 /* prevent cursor from moving on the trail byte */
8812 if (has_mbyte)
8813 mb_adjust_cursor();
8814#endif
8815 oap->inclusive = TRUE;
8816 }
8817}
8818
8819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * "0" and "^" commands.
8821 * cap->arg is the argument for beginline().
8822 */
8823 static void
8824nv_beginline(cap)
8825 cmdarg_T *cap;
8826{
8827 cap->oap->motion_type = MCHAR;
8828 cap->oap->inclusive = FALSE;
8829 beginline(cap->arg);
8830#ifdef FEAT_FOLDING
8831 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8832 foldOpenCursor();
8833#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008834 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8835 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838/*
8839 * In exclusive Visual mode, may include the last character.
8840 */
8841 static void
8842adjust_for_sel(cap)
8843 cmdarg_T *cap;
8844{
8845 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8846 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8847 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008848#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (has_mbyte)
8850 inc_cursor();
8851 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 ++curwin->w_cursor.col;
8854 cap->oap->inclusive = FALSE;
8855 }
8856}
8857
8858/*
8859 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8860 * Should check VIsual_mode before calling this.
8861 * Returns TRUE when backed up to the previous line.
8862 */
8863 static int
8864unadjust_for_sel()
8865{
8866 pos_T *pp;
8867
8868 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8869 {
8870 if (lt(VIsual, curwin->w_cursor))
8871 pp = &curwin->w_cursor;
8872 else
8873 pp = &VIsual;
8874#ifdef FEAT_VIRTUALEDIT
8875 if (pp->coladd > 0)
8876 --pp->coladd;
8877 else
8878#endif
8879 if (pp->col > 0)
8880 {
8881 --pp->col;
8882#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008883 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884#endif
8885 }
8886 else if (pp->lnum > 1)
8887 {
8888 --pp->lnum;
8889 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8890 return TRUE;
8891 }
8892 }
8893 return FALSE;
8894}
8895
8896/*
8897 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8898 */
8899 static void
8900nv_select(cap)
8901 cmdarg_T *cap;
8902{
8903 if (VIsual_active)
8904 VIsual_select = TRUE;
8905 else if (VIsual_reselect)
8906 {
8907 cap->nchar = 'v'; /* fake "gv" command */
8908 cap->arg = TRUE;
8909 nv_g_cmd(cap);
8910 }
8911}
8912
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
8914/*
8915 * "G", "gg", CTRL-END, CTRL-HOME.
8916 * cap->arg is TRUE for "G".
8917 */
8918 static void
8919nv_goto(cap)
8920 cmdarg_T *cap;
8921{
8922 linenr_T lnum;
8923
8924 if (cap->arg)
8925 lnum = curbuf->b_ml.ml_line_count;
8926 else
8927 lnum = 1L;
8928 cap->oap->motion_type = MLINE;
8929 setpcmark();
8930
8931 /* When a count is given, use it instead of the default lnum */
8932 if (cap->count0 != 0)
8933 lnum = cap->count0;
8934 if (lnum < 1L)
8935 lnum = 1L;
8936 else if (lnum > curbuf->b_ml.ml_line_count)
8937 lnum = curbuf->b_ml.ml_line_count;
8938 curwin->w_cursor.lnum = lnum;
8939 beginline(BL_SOL | BL_FIX);
8940#ifdef FEAT_FOLDING
8941 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8942 foldOpenCursor();
8943#endif
8944}
8945
8946/*
8947 * CTRL-\ in Normal mode.
8948 */
8949 static void
8950nv_normal(cap)
8951 cmdarg_T *cap;
8952{
8953 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8954 {
8955 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008956 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 clear_cmdline = TRUE; /* unshow mode later */
8958 restart_edit = 0;
8959#ifdef FEAT_CMDWIN
8960 if (cmdwin_type != 0)
8961 cmdwin_result = Ctrl_C;
8962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 if (VIsual_active)
8964 {
8965 end_visual_mode(); /* stop Visual */
8966 redraw_curbuf_later(INVERTED);
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8969 if (cap->nchar == Ctrl_G && p_im)
8970 restart_edit = 'a';
8971 }
8972 else
8973 clearopbeep(cap->oap);
8974}
8975
8976/*
8977 * ESC in Normal mode: beep, but don't flush buffers.
8978 * Don't even beep if we are canceling a command.
8979 */
8980 static void
8981nv_esc(cap)
8982 cmdarg_T *cap;
8983{
8984 int no_reason;
8985
8986 no_reason = (cap->oap->op_type == OP_NOP
8987 && cap->opcount == 0
8988 && cap->count0 == 0
8989 && cap->oap->regname == 0
8990 && !p_im);
8991
8992 if (cap->arg) /* TRUE for CTRL-C */
8993 {
8994 if (restart_edit == 0
8995#ifdef FEAT_CMDWIN
8996 && cmdwin_type == 0
8997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 && no_reason)
9000 MSG(_("Type :quit<Enter> to exit Vim"));
9001
9002 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9003 * set again below when halfway a mapping. */
9004 if (!p_im)
9005 restart_edit = 0;
9006#ifdef FEAT_CMDWIN
9007 if (cmdwin_type != 0)
9008 {
9009 cmdwin_result = K_IGNORE;
9010 got_int = FALSE; /* don't stop executing autocommands et al. */
9011 return;
9012 }
9013#endif
9014 }
9015
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 if (VIsual_active)
9017 {
9018 end_visual_mode(); /* stop Visual */
9019 check_cursor_col(); /* make sure cursor is not beyond EOL */
9020 curwin->w_set_curswant = TRUE;
9021 redraw_curbuf_later(INVERTED);
9022 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009023 else if (no_reason)
9024 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 clearop(cap->oap);
9026
9027 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9028 * set return to Insert mode afterwards. */
9029 if (restart_edit == 0 && goto_im()
9030#ifdef FEAT_EX_EXTRA
9031 && ex_normal_busy == 0
9032#endif
9033 )
9034 restart_edit = 'a';
9035}
9036
9037/*
9038 * Handle "A", "a", "I", "i" and <Insert> commands.
9039 */
9040 static void
9041nv_edit(cap)
9042 cmdarg_T *cap;
9043{
9044 /* <Insert> is equal to "i" */
9045 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9046 cap->cmdchar = 'i';
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 /* in Visual mode "A" and "I" are an operator */
9049 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9050 v_visop(cap);
9051
9052 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009053 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9054 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
9056#ifdef FEAT_TEXTOBJ
9057 nv_object(cap);
9058#else
9059 clearopbeep(cap->oap);
9060#endif
9061 }
9062 else if (!curbuf->b_p_ma && !p_im)
9063 {
9064 /* Only give this error when 'insertmode' is off. */
9065 EMSG(_(e_modifiable));
9066 clearop(cap->oap);
9067 }
9068 else if (!checkclearopq(cap->oap))
9069 {
9070 switch (cap->cmdchar)
9071 {
9072 case 'A': /* "A"ppend after the line */
9073 curwin->w_set_curswant = TRUE;
9074#ifdef FEAT_VIRTUALEDIT
9075 if (ve_flags == VE_ALL)
9076 {
9077 int save_State = State;
9078
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009079 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 * character past the end of the line */
9081 State = INSERT;
9082 coladvance((colnr_T)MAXCOL);
9083 State = save_State;
9084 }
9085 else
9086#endif
9087 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9088 break;
9089
9090 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009091 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9092 beginline(BL_WHITE);
9093 else
9094 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 break;
9096
9097 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9098#ifdef FEAT_VIRTUALEDIT
9099 /* increment coladd when in virtual space, increment the
9100 * column otherwise, also to append after an unprintable char */
9101 if (virtual_active()
9102 && (curwin->w_cursor.coladd > 0
9103 || *ml_get_cursor() == NUL
9104 || *ml_get_cursor() == TAB))
9105 curwin->w_cursor.coladd++;
9106 else
9107#endif
9108 if (*ml_get_cursor() != NUL)
9109 inc_cursor();
9110 break;
9111 }
9112
9113#ifdef FEAT_VIRTUALEDIT
9114 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9115 {
9116 int save_State = State;
9117
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009118 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 * character past the end of the line */
9120 State = INSERT;
9121 coladvance(getviscol());
9122 State = save_State;
9123 }
9124#endif
9125
9126 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9127 }
9128}
9129
9130/*
9131 * Invoke edit() and take care of "restart_edit" and the return value.
9132 */
9133 static void
9134invoke_edit(cap, repl, cmd, startln)
9135 cmdarg_T *cap;
9136 int repl; /* "r" or "gr" command */
9137 int cmd;
9138 int startln;
9139{
9140 int restart_edit_save = 0;
9141
9142 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9143 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9144 * it. */
9145 if (repl || !stuff_empty())
9146 restart_edit_save = restart_edit;
9147 else
9148 restart_edit_save = 0;
9149
9150 /* Always reset "restart_edit", this is not a restarted edit. */
9151 restart_edit = 0;
9152
9153 if (edit(cmd, startln, cap->count1))
9154 cap->retval |= CA_COMMAND_BUSY;
9155
9156 if (restart_edit == 0)
9157 restart_edit = restart_edit_save;
9158}
9159
9160#ifdef FEAT_TEXTOBJ
9161/*
9162 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9163 */
9164 static void
9165nv_object(cap)
9166 cmdarg_T *cap;
9167{
9168 int flag;
9169 int include;
9170 char_u *mps_save;
9171
9172 if (cap->cmdchar == 'i')
9173 include = FALSE; /* "ix" = inner object: exclude white space */
9174 else
9175 include = TRUE; /* "ax" = an object: include white space */
9176
9177 /* Make sure (), [], {} and <> are in 'matchpairs' */
9178 mps_save = curbuf->b_p_mps;
9179 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9180
9181 switch (cap->nchar)
9182 {
9183 case 'w': /* "aw" = a word */
9184 flag = current_word(cap->oap, cap->count1, include, FALSE);
9185 break;
9186 case 'W': /* "aW" = a WORD */
9187 flag = current_word(cap->oap, cap->count1, include, TRUE);
9188 break;
9189 case 'b': /* "ab" = a braces block */
9190 case '(':
9191 case ')':
9192 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9193 break;
9194 case 'B': /* "aB" = a Brackets block */
9195 case '{':
9196 case '}':
9197 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9198 break;
9199 case '[': /* "a[" = a [] block */
9200 case ']':
9201 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9202 break;
9203 case '<': /* "a<" = a <> block */
9204 case '>':
9205 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9206 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009207 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009208 /* Do not adjust oap->end in do_pending_operator()
9209 * otherwise there are different results for 'dit'
9210 * (note leading whitespace in last line):
9211 * 1) <b> 2) <b>
9212 * foobar foobar
9213 * </b> </b>
9214 */
9215 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009216 flag = current_tagblock(cap->oap, cap->count1, include);
9217 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 case 'p': /* "ap" = a paragraph */
9219 flag = current_par(cap->oap, cap->count1, include, 'p');
9220 break;
9221 case 's': /* "as" = a sentence */
9222 flag = current_sent(cap->oap, cap->count1, include);
9223 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009224 case '"': /* "a"" = a double quoted string */
9225 case '\'': /* "a'" = a single quoted string */
9226 case '`': /* "a`" = a backtick quoted string */
9227 flag = current_quote(cap->oap, cap->count1, include,
9228 cap->nchar);
9229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230#if 0 /* TODO */
9231 case 'S': /* "aS" = a section */
9232 case 'f': /* "af" = a filename */
9233 case 'u': /* "au" = a URL */
9234#endif
9235 default:
9236 flag = FAIL;
9237 break;
9238 }
9239
9240 curbuf->b_p_mps = mps_save;
9241 if (flag == FAIL)
9242 clearopbeep(cap->oap);
9243 adjust_cursor_col();
9244 curwin->w_set_curswant = TRUE;
9245}
9246#endif
9247
9248/*
9249 * "q" command: Start/stop recording.
9250 * "q:", "q/", "q?": edit command-line in command-line window.
9251 */
9252 static void
9253nv_record(cap)
9254 cmdarg_T *cap;
9255{
9256 if (cap->oap->op_type == OP_FORMAT)
9257 {
9258 /* "gqq" is the same as "gqgq": format line */
9259 cap->cmdchar = 'g';
9260 cap->nchar = 'q';
9261 nv_operator(cap);
9262 }
9263 else if (!checkclearop(cap->oap))
9264 {
9265#ifdef FEAT_CMDWIN
9266 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9267 {
9268 stuffcharReadbuff(cap->nchar);
9269 stuffcharReadbuff(K_CMDWIN);
9270 }
9271 else
9272#endif
9273 /* (stop) recording into a named register, unless executing a
9274 * register */
9275 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9276 clearopbeep(cap->oap);
9277 }
9278}
9279
9280/*
9281 * Handle the "@r" command.
9282 */
9283 static void
9284nv_at(cap)
9285 cmdarg_T *cap;
9286{
9287 if (checkclearop(cap->oap))
9288 return;
9289#ifdef FEAT_EVAL
9290 if (cap->nchar == '=')
9291 {
9292 if (get_expr_register() == NUL)
9293 return;
9294 }
9295#endif
9296 while (cap->count1-- && !got_int)
9297 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009298 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 {
9300 clearopbeep(cap->oap);
9301 break;
9302 }
9303 line_breakcheck();
9304 }
9305}
9306
9307/*
9308 * Handle the CTRL-U and CTRL-D commands.
9309 */
9310 static void
9311nv_halfpage(cap)
9312 cmdarg_T *cap;
9313{
9314 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9315 || (cap->cmdchar == Ctrl_D
9316 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9317 clearopbeep(cap->oap);
9318 else if (!checkclearop(cap->oap))
9319 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9320}
9321
9322/*
9323 * Handle "J" or "gJ" command.
9324 */
9325 static void
9326nv_join(cap)
9327 cmdarg_T *cap;
9328{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 if (VIsual_active) /* join the visual lines */
9330 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009331 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 {
9333 if (cap->count0 <= 1)
9334 cap->count0 = 2; /* default for join is two lines! */
9335 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9336 curbuf->b_ml.ml_line_count)
9337 clearopbeep(cap->oap); /* beyond last line */
9338 else
9339 {
9340 prep_redo(cap->oap->regname, cap->count0,
9341 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009342 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 }
9344 }
9345}
9346
9347/*
9348 * "P", "gP", "p" and "gp" commands.
9349 */
9350 static void
9351nv_put(cap)
9352 cmdarg_T *cap;
9353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 int regname = 0;
9355 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009356 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009357 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 int dir;
9359 int flags = 0;
9360
9361 if (cap->oap->op_type != OP_NOP)
9362 {
9363#ifdef FEAT_DIFF
9364 /* "dp" is ":diffput" */
9365 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9366 {
9367 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009368 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 }
9370 else
9371#endif
9372 clearopbeep(cap->oap);
9373 }
9374 else
9375 {
9376 dir = (cap->cmdchar == 'P'
9377 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9378 ? BACKWARD : FORWARD;
9379 prep_redo_cmd(cap);
9380 if (cap->cmdchar == 'g')
9381 flags |= PUT_CURSEND;
9382
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 if (VIsual_active)
9384 {
9385 /* Putting in Visual mode: The put text replaces the selected
9386 * text. First delete the selected text, then put the new text.
9387 * Need to save and restore the registers that the delete
9388 * overwrites if the old contents is being put.
9389 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009390 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009392#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009394#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009395 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009396 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009397#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400
9401 )
9402 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009403 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 * put, save it first. */
9405 reg1 = get_register(regname, TRUE);
9406 }
9407
9408 /* Now delete the selected text. */
9409 cap->cmdchar = 'd';
9410 cap->nchar = NUL;
9411 cap->oap->regname = NUL;
9412 nv_operator(cap);
9413 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009414 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415
9416 /* delete PUT_LINE_BACKWARD; */
9417 cap->oap->regname = regname;
9418
9419 if (reg1 != NULL)
9420 {
9421 /* Delete probably changed the register we want to put, save
9422 * it first. Then put back what was there before the delete. */
9423 reg2 = get_register(regname, FALSE);
9424 put_register(regname, reg1);
9425 }
9426
9427 /* When deleted a linewise Visual area, put the register as
9428 * lines to avoid it joined with the next line. When deletion was
9429 * characterwise, split a line when putting lines. */
9430 if (VIsual_mode == 'V')
9431 flags |= PUT_LINE;
9432 else if (VIsual_mode == 'v')
9433 flags |= PUT_LINE_SPLIT;
9434 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9435 flags |= PUT_LINE_FORWARD;
9436 dir = BACKWARD;
9437 if ((VIsual_mode != 'V'
9438 && curwin->w_cursor.col < curbuf->b_op_start.col)
9439 || (VIsual_mode == 'V'
9440 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9441 /* cursor is at the end of the line or end of file, put
9442 * forward. */
9443 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009444 /* May have been reset in do_put(). */
9445 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 do_put(cap->oap->regname, dir, cap->count1, flags);
9448
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 /* If a register was saved, put it back now. */
9450 if (reg2 != NULL)
9451 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009452
9453 /* What to reselect with "gv"? Selecting the just put text seems to
9454 * be the most useful, since the original text was removed. */
9455 if (was_visual)
9456 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009457 curbuf->b_visual.vi_start = curbuf->b_op_start;
9458 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009459 }
9460
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009461 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009462 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009463 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009464 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009465 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009466
9467 /* If the cursor was in that line, move it to the end of the last
9468 * line. */
9469 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9470 {
9471 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9472 coladvance((colnr_T)MAXCOL);
9473 }
9474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 auto_format(FALSE, TRUE);
9476 }
9477}
9478
9479/*
9480 * "o" and "O" commands.
9481 */
9482 static void
9483nv_open(cap)
9484 cmdarg_T *cap;
9485{
9486#ifdef FEAT_DIFF
9487 /* "do" is ":diffget" */
9488 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9489 {
9490 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009491 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 }
9493 else
9494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 if (VIsual_active) /* switch start and end of visual */
9496 v_swap_corners(cap->cmdchar);
9497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 n_opencmd(cap);
9499}
9500
9501#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 static void
9503nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009504 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 ProcessSniffRequests();
9507}
9508#endif
9509
9510#ifdef FEAT_NETBEANS_INTG
9511 static void
9512nv_nbcmd(cap)
9513 cmdarg_T *cap;
9514{
9515 netbeans_keycommand(cap->nchar);
9516}
9517#endif
9518
9519#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 static void
9521nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009522 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9525}
9526#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009527
9528#ifdef FEAT_AUTOCMD
9529/*
9530 * Trigger CursorHold event.
9531 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9532 * input buffer. "did_cursorhold" is set to avoid retriggering.
9533 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009534 static void
9535nv_cursorhold(cap)
9536 cmdarg_T *cap;
9537{
9538 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9539 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009540 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009541}
9542#endif