blob: f9050db6250dd2260a0dfb39d378fdecedb1e2b7 [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
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020023static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
26#endif
27
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010028#ifdef FEAT_EVAL
29static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
30#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020032#ifdef __BORLANDC__
33 _RTLENTRYF
34#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 nv_compare __ARGS((const void *s1, const void *s2));
36static int find_command __ARGS((int cmdchar));
37static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000038static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
40static void find_start_of_word __ARGS((pos_T *));
41static void find_end_of_word __ARGS((pos_T *));
42static int get_mouse_class __ARGS((char_u *p));
43#endif
44static void prep_redo_cmd __ARGS((cmdarg_T *cap));
45static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
46static int checkclearop __ARGS((oparg_T *oap));
47static int checkclearopq __ARGS((oparg_T *oap));
48static void clearop __ARGS((oparg_T *oap));
49static void clearopbeep __ARGS((oparg_T *oap));
50#ifdef FEAT_VISUAL
51static void unshift_special __ARGS((cmdarg_T *cap));
52#endif
53#ifdef FEAT_CMDL_INFO
54static void del_from_showcmd __ARGS((int));
55#endif
56
57/*
58 * nv_*(): functions called to handle Normal and Visual mode commands.
59 * n_*(): functions called to handle Normal mode commands.
60 * v_*(): functions called to handle Visual mode commands.
61 */
62static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000063static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void nv_error __ARGS((cmdarg_T *cap));
65static void nv_help __ARGS((cmdarg_T *cap));
66static void nv_addsub __ARGS((cmdarg_T *cap));
67static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000068static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
70#ifdef FEAT_MOUSE
71static void nv_mousescroll __ARGS((cmdarg_T *cap));
72static void nv_mouse __ARGS((cmdarg_T *cap));
73#endif
74static void nv_scroll_line __ARGS((cmdarg_T *cap));
75static void nv_zet __ARGS((cmdarg_T *cap));
76#ifdef FEAT_GUI
77static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
78static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
79#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#ifdef FEAT_GUI_TABLINE
81static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000082static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000084static void nv_exmode __ARGS((cmdarg_T *cap));
85static void nv_colon __ARGS((cmdarg_T *cap));
86static void nv_ctrlg __ARGS((cmdarg_T *cap));
87static void nv_ctrlh __ARGS((cmdarg_T *cap));
88static void nv_clear __ARGS((cmdarg_T *cap));
89static void nv_ctrlo __ARGS((cmdarg_T *cap));
90static void nv_hat __ARGS((cmdarg_T *cap));
91static void nv_Zet __ARGS((cmdarg_T *cap));
92static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000093static void nv_tagpop __ARGS((cmdarg_T *cap));
94static void nv_scroll __ARGS((cmdarg_T *cap));
95static void nv_right __ARGS((cmdarg_T *cap));
96static void nv_left __ARGS((cmdarg_T *cap));
97static void nv_up __ARGS((cmdarg_T *cap));
98static void nv_down __ARGS((cmdarg_T *cap));
99#ifdef FEAT_SEARCHPATH
100static void nv_gotofile __ARGS((cmdarg_T *cap));
101#endif
102static void nv_end __ARGS((cmdarg_T *cap));
103static void nv_dollar __ARGS((cmdarg_T *cap));
104static void nv_search __ARGS((cmdarg_T *cap));
105static void nv_next __ARGS((cmdarg_T *cap));
106static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
107static void nv_csearch __ARGS((cmdarg_T *cap));
108static void nv_brackets __ARGS((cmdarg_T *cap));
109static void nv_percent __ARGS((cmdarg_T *cap));
110static void nv_brace __ARGS((cmdarg_T *cap));
111static void nv_mark __ARGS((cmdarg_T *cap));
112static void nv_findpar __ARGS((cmdarg_T *cap));
113static void nv_undo __ARGS((cmdarg_T *cap));
114static void nv_kundo __ARGS((cmdarg_T *cap));
115static void nv_Replace __ARGS((cmdarg_T *cap));
116#ifdef FEAT_VREPLACE
117static void nv_vreplace __ARGS((cmdarg_T *cap));
118#endif
119#ifdef FEAT_VISUAL
120static void v_swap_corners __ARGS((int cmdchar));
121#endif
122static void nv_replace __ARGS((cmdarg_T *cap));
123static void n_swapchar __ARGS((cmdarg_T *cap));
124static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
125#ifdef FEAT_VISUAL
126static void v_visop __ARGS((cmdarg_T *cap));
127#endif
128static void nv_subst __ARGS((cmdarg_T *cap));
129static void nv_abbrev __ARGS((cmdarg_T *cap));
130static void nv_optrans __ARGS((cmdarg_T *cap));
131static void nv_gomark __ARGS((cmdarg_T *cap));
132static void nv_pcmark __ARGS((cmdarg_T *cap));
133static void nv_regname __ARGS((cmdarg_T *cap));
134#ifdef FEAT_VISUAL
135static void nv_visual __ARGS((cmdarg_T *cap));
136static void n_start_visual_mode __ARGS((int c));
137#endif
138static void nv_window __ARGS((cmdarg_T *cap));
139static void nv_suspend __ARGS((cmdarg_T *cap));
140static void nv_g_cmd __ARGS((cmdarg_T *cap));
141static void n_opencmd __ARGS((cmdarg_T *cap));
142static void nv_dot __ARGS((cmdarg_T *cap));
143static void nv_redo __ARGS((cmdarg_T *cap));
144static void nv_Undo __ARGS((cmdarg_T *cap));
145static void nv_tilde __ARGS((cmdarg_T *cap));
146static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000147#ifdef FEAT_EVAL
148static void set_op_var __ARGS((int optype));
149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void nv_lineop __ARGS((cmdarg_T *cap));
151static void nv_home __ARGS((cmdarg_T *cap));
152static void nv_pipe __ARGS((cmdarg_T *cap));
153static void nv_bck_word __ARGS((cmdarg_T *cap));
154static void nv_wordcmd __ARGS((cmdarg_T *cap));
155static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000156static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_VISUAL
158static void adjust_for_sel __ARGS((cmdarg_T *cap));
159static int unadjust_for_sel __ARGS((void));
160static void nv_select __ARGS((cmdarg_T *cap));
161#endif
162static void nv_goto __ARGS((cmdarg_T *cap));
163static void nv_normal __ARGS((cmdarg_T *cap));
164static void nv_esc __ARGS((cmdarg_T *oap));
165static void nv_edit __ARGS((cmdarg_T *cap));
166static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
167#ifdef FEAT_TEXTOBJ
168static void nv_object __ARGS((cmdarg_T *cap));
169#endif
170static void nv_record __ARGS((cmdarg_T *cap));
171static void nv_at __ARGS((cmdarg_T *cap));
172static void nv_halfpage __ARGS((cmdarg_T *cap));
173static void nv_join __ARGS((cmdarg_T *cap));
174static void nv_put __ARGS((cmdarg_T *cap));
175static void nv_open __ARGS((cmdarg_T *cap));
176#ifdef FEAT_SNIFF
177static void nv_sniff __ARGS((cmdarg_T *cap));
178#endif
179#ifdef FEAT_NETBEANS_INTG
180static void nv_nbcmd __ARGS((cmdarg_T *cap));
181#endif
182#ifdef FEAT_DND
183static void nv_drop __ARGS((cmdarg_T *cap));
184#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000185#ifdef FEAT_AUTOCMD
186static void nv_cursorhold __ARGS((cmdarg_T *cap));
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000189static char *e_noident = N_("E349: No identifier under cursor");
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Function to be called for a Normal or Visual mode command.
193 * The argument is a cmdarg_T.
194 */
195typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
196
197/* Values for cmd_flags. */
198#define NV_NCH 0x01 /* may need to get a second char */
199#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
200#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
201#define NV_LANG 0x08 /* second char needs language adjustment */
202
203#define NV_SS 0x10 /* may start selection */
204#define NV_SSS 0x20 /* may start selection with shift modifier */
205#define NV_STS 0x40 /* may stop selection without shift modif. */
206#define NV_RL 0x80 /* 'rightleft' modifies command */
207#define NV_KEEPREG 0x100 /* don't clear regname */
208#define NV_NCW 0x200 /* not allowed in command-line window */
209
210/*
211 * Generally speaking, every Normal mode command should either clear any
212 * pending operator (with *clearop*()), or set the motion type variable
213 * oap->motion_type.
214 *
215 * When a cursor motion command is made, it is marked as being a character or
216 * line oriented motion. Then, if an operator is in effect, the operation
217 * becomes character or line oriented accordingly.
218 */
219
220/*
221 * This table contains one entry for every Normal or Visual mode command.
222 * The order doesn't matter, init_normal_cmds() will create a sorted index.
223 * It is faster when all keys from zero to '~' are present.
224 */
225static const struct nv_cmd
226{
227 int cmd_char; /* (first) command character */
228 nv_func_T cmd_func; /* function for this command */
229 short_u cmd_flags; /* NV_ flags */
230 short cmd_arg; /* value for ca.arg */
231} nv_cmds[] =
232{
233 {NUL, nv_error, 0, 0},
234 {Ctrl_A, nv_addsub, 0, 0},
235 {Ctrl_B, nv_page, NV_STS, BACKWARD},
236 {Ctrl_C, nv_esc, 0, TRUE},
237 {Ctrl_D, nv_halfpage, 0, 0},
238 {Ctrl_E, nv_scroll_line, 0, TRUE},
239 {Ctrl_F, nv_page, NV_STS, FORWARD},
240 {Ctrl_G, nv_ctrlg, 0, 0},
241 {Ctrl_H, nv_ctrlh, 0, 0},
242 {Ctrl_I, nv_pcmark, 0, 0},
243 {NL, nv_down, 0, FALSE},
244 {Ctrl_K, nv_error, 0, 0},
245 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000246 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_N, nv_down, NV_STS, FALSE},
248 {Ctrl_O, nv_ctrlo, 0, 0},
249 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000250#ifdef FEAT_VISUAL
251 {Ctrl_Q, nv_visual, 0, FALSE},
252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {Ctrl_R, nv_redo, 0, 0},
256 {Ctrl_S, nv_ignore, 0, 0},
257 {Ctrl_T, nv_tagpop, NV_NCW, 0},
258 {Ctrl_U, nv_halfpage, 0, 0},
259#ifdef FEAT_VISUAL
260 {Ctrl_V, nv_visual, 0, FALSE},
261 {'V', nv_visual, 0, FALSE},
262 {'v', nv_visual, 0, FALSE},
263#else
264 {Ctrl_V, nv_error, 0, 0},
265 {'V', nv_error, 0, 0},
266 {'v', nv_error, 0, 0},
267#endif
268 {Ctrl_W, nv_window, 0, 0},
269 {Ctrl_X, nv_addsub, 0, 0},
270 {Ctrl_Y, nv_scroll_line, 0, FALSE},
271 {Ctrl_Z, nv_suspend, 0, 0},
272 {ESC, nv_esc, 0, FALSE},
273 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
274 {Ctrl_RSB, nv_ident, NV_NCW, 0},
275 {Ctrl_HAT, nv_hat, NV_NCW, 0},
276 {Ctrl__, nv_error, 0, 0},
277 {' ', nv_right, 0, 0},
278 {'!', nv_operator, 0, 0},
279 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
280 {'#', nv_ident, 0, 0},
281 {'$', nv_dollar, 0, 0},
282 {'%', nv_percent, 0, 0},
283 {'&', nv_optrans, 0, 0},
284 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
285 {'(', nv_brace, 0, BACKWARD},
286 {')', nv_brace, 0, FORWARD},
287 {'*', nv_ident, 0, 0},
288 {'+', nv_down, 0, TRUE},
289 {',', nv_csearch, 0, TRUE},
290 {'-', nv_up, 0, TRUE},
291 {'.', nv_dot, NV_KEEPREG, 0},
292 {'/', nv_search, 0, FALSE},
293 {'0', nv_beginline, 0, 0},
294 {'1', nv_ignore, 0, 0},
295 {'2', nv_ignore, 0, 0},
296 {'3', nv_ignore, 0, 0},
297 {'4', nv_ignore, 0, 0},
298 {'5', nv_ignore, 0, 0},
299 {'6', nv_ignore, 0, 0},
300 {'7', nv_ignore, 0, 0},
301 {'8', nv_ignore, 0, 0},
302 {'9', nv_ignore, 0, 0},
303 {':', nv_colon, 0, 0},
304 {';', nv_csearch, 0, FALSE},
305 {'<', nv_operator, NV_RL, 0},
306 {'=', nv_operator, 0, 0},
307 {'>', nv_operator, NV_RL, 0},
308 {'?', nv_search, 0, FALSE},
309 {'@', nv_at, NV_NCH_NOP, FALSE},
310 {'A', nv_edit, 0, 0},
311 {'B', nv_bck_word, 0, 1},
312 {'C', nv_abbrev, NV_KEEPREG, 0},
313 {'D', nv_abbrev, NV_KEEPREG, 0},
314 {'E', nv_wordcmd, 0, TRUE},
315 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
316 {'G', nv_goto, 0, TRUE},
317 {'H', nv_scroll, 0, 0},
318 {'I', nv_edit, 0, 0},
319 {'J', nv_join, 0, 0},
320 {'K', nv_ident, 0, 0},
321 {'L', nv_scroll, 0, 0},
322 {'M', nv_scroll, 0, 0},
323 {'N', nv_next, 0, SEARCH_REV},
324 {'O', nv_open, 0, 0},
325 {'P', nv_put, 0, 0},
326 {'Q', nv_exmode, NV_NCW, 0},
327 {'R', nv_Replace, 0, FALSE},
328 {'S', nv_subst, NV_KEEPREG, 0},
329 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
330 {'U', nv_Undo, 0, 0},
331 {'W', nv_wordcmd, 0, TRUE},
332 {'X', nv_abbrev, NV_KEEPREG, 0},
333 {'Y', nv_abbrev, NV_KEEPREG, 0},
334 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
335 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
336 {'\\', nv_error, 0, 0},
337 {']', nv_brackets, NV_NCH_ALW, FORWARD},
338 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
339 {'_', nv_lineop, 0, 0},
340 {'`', nv_gomark, NV_NCH_ALW, FALSE},
341 {'a', nv_edit, NV_NCH, 0},
342 {'b', nv_bck_word, 0, 0},
343 {'c', nv_operator, 0, 0},
344 {'d', nv_operator, 0, 0},
345 {'e', nv_wordcmd, 0, FALSE},
346 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
347 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
348 {'h', nv_left, NV_RL, 0},
349 {'i', nv_edit, NV_NCH, 0},
350 {'j', nv_down, 0, FALSE},
351 {'k', nv_up, 0, FALSE},
352 {'l', nv_right, NV_RL, 0},
353 {'m', nv_mark, NV_NCH_NOP, 0},
354 {'n', nv_next, 0, 0},
355 {'o', nv_open, 0, 0},
356 {'p', nv_put, 0, 0},
357 {'q', nv_record, NV_NCH, 0},
358 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
359 {'s', nv_subst, NV_KEEPREG, 0},
360 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
361 {'u', nv_undo, 0, 0},
362 {'w', nv_wordcmd, 0, FALSE},
363 {'x', nv_abbrev, NV_KEEPREG, 0},
364 {'y', nv_operator, 0, 0},
365 {'z', nv_zet, NV_NCH_ALW, 0},
366 {'{', nv_findpar, 0, BACKWARD},
367 {'|', nv_pipe, 0, 0},
368 {'}', nv_findpar, 0, FORWARD},
369 {'~', nv_tilde, 0, 0},
370
371 /* pound sign */
372 {POUND, nv_ident, 0, 0},
373#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200374 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
375 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
376 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
377 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {K_LEFTMOUSE, nv_mouse, 0, 0},
379 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
380 {K_LEFTDRAG, nv_mouse, 0, 0},
381 {K_LEFTRELEASE, nv_mouse, 0, 0},
382 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
383 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
384 {K_MIDDLEDRAG, nv_mouse, 0, 0},
385 {K_MIDDLERELEASE, nv_mouse, 0, 0},
386 {K_RIGHTMOUSE, nv_mouse, 0, 0},
387 {K_RIGHTDRAG, nv_mouse, 0, 0},
388 {K_RIGHTRELEASE, nv_mouse, 0, 0},
389 {K_X1MOUSE, nv_mouse, 0, 0},
390 {K_X1DRAG, nv_mouse, 0, 0},
391 {K_X1RELEASE, nv_mouse, 0, 0},
392 {K_X2MOUSE, nv_mouse, 0, 0},
393 {K_X2DRAG, nv_mouse, 0, 0},
394 {K_X2RELEASE, nv_mouse, 0, 0},
395#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000397 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_INS, nv_edit, 0, 0},
399 {K_KINS, nv_edit, 0, 0},
400 {K_BS, nv_ctrlh, 0, 0},
401 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
402 {K_S_UP, nv_page, NV_SS, BACKWARD},
403 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
404 {K_S_DOWN, nv_page, NV_SS, FORWARD},
405 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
406 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
407 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
408 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
409 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
410 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
411 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
412 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
413 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
414 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
415 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
416 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {K_S_END, nv_end, NV_SS, FALSE},
418 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
419 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
420 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {K_S_HOME, nv_home, NV_SS, 0},
422 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
423 {K_DEL, nv_abbrev, 0, 0},
424 {K_KDEL, nv_abbrev, 0, 0},
425 {K_UNDO, nv_kundo, 0, 0},
426 {K_HELP, nv_help, NV_NCW, 0},
427 {K_F1, nv_help, NV_NCW, 0},
428 {K_XF1, nv_help, NV_NCW, 0},
429#ifdef FEAT_VISUAL
430 {K_SELECT, nv_select, 0, 0},
431#endif
432#ifdef FEAT_GUI
433 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
434 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
435#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000436#ifdef FEAT_GUI_TABLINE
437 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000438 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#ifdef FEAT_FKMAP
441 {K_F8, farsi_fkey, 0, 0},
442 {K_F9, farsi_fkey, 0, 0},
443#endif
444#ifdef FEAT_SNIFF
445 {K_SNIFF, nv_sniff, 0, 0},
446#endif
447#ifdef FEAT_NETBEANS_INTG
448 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
449#endif
450#ifdef FEAT_DND
451 {K_DROP, nv_drop, NV_STS, 0},
452#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000453#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000454 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456};
457
458/* Number of commands in nv_cmds[]. */
459#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
460
461/* Sorted index of commands in nv_cmds[]. */
462static short nv_cmd_idx[NV_CMDS_SIZE];
463
464/* The highest index for which
465 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
466static int nv_max_linear;
467
468/*
469 * Compare functions for qsort() below, that checks the command character
470 * through the index in nv_cmd_idx[].
471 */
472 static int
473#ifdef __BORLANDC__
474_RTLENTRYF
475#endif
476nv_compare(s1, s2)
477 const void *s1;
478 const void *s2;
479{
480 int c1, c2;
481
482 /* The commands are sorted on absolute value. */
483 c1 = nv_cmds[*(const short *)s1].cmd_char;
484 c2 = nv_cmds[*(const short *)s2].cmd_char;
485 if (c1 < 0)
486 c1 = -c1;
487 if (c2 < 0)
488 c2 = -c2;
489 return c1 - c2;
490}
491
492/*
493 * Initialize the nv_cmd_idx[] table.
494 */
495 void
496init_normal_cmds()
497{
498 int i;
499
500 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000501 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 nv_cmd_idx[i] = i;
503
504 /* Sort the commands by the command character. */
505 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
506
507 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000508 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
510 break;
511 nv_max_linear = i - 1;
512}
513
514/*
515 * Search for a command in the commands table.
516 * Returns -1 for invalid command.
517 */
518 static int
519find_command(cmdchar)
520 int cmdchar;
521{
522 int i;
523 int idx;
524 int top, bot;
525 int c;
526
527#ifdef FEAT_MBYTE
528 /* A multi-byte character is never a command. */
529 if (cmdchar >= 0x100)
530 return -1;
531#endif
532
533 /* We use the absolute value of the character. Special keys have a
534 * negative value, but are sorted on their absolute value. */
535 if (cmdchar < 0)
536 cmdchar = -cmdchar;
537
538 /* If the character is in the first part: The character is the index into
539 * nv_cmd_idx[]. */
540 if (cmdchar <= nv_max_linear)
541 return nv_cmd_idx[cmdchar];
542
543 /* Perform a binary search. */
544 bot = nv_max_linear + 1;
545 top = NV_CMDS_SIZE - 1;
546 idx = -1;
547 while (bot <= top)
548 {
549 i = (top + bot) / 2;
550 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
551 if (c < 0)
552 c = -c;
553 if (cmdchar == c)
554 {
555 idx = nv_cmd_idx[i];
556 break;
557 }
558 if (cmdchar > c)
559 bot = i + 1;
560 else
561 top = i - 1;
562 }
563 return idx;
564}
565
566/*
567 * Execute a command in Normal mode.
568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 void
570normal_cmd(oap, toplevel)
571 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000572 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 cmdarg_T ca; /* command arguments */
575 int c;
576 int ctrl_w = FALSE; /* got CTRL-W command */
577 int old_col = curwin->w_curswant;
578#ifdef FEAT_CMDL_INFO
579 int need_flushbuf; /* need to call out_flush() */
580#endif
581#ifdef FEAT_VISUAL
582 pos_T old_pos; /* cursor position before command */
583 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000587#ifdef FEAT_EVAL
588 int set_prevcount = FALSE;
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
592 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000593
594 /* Use a count remembered from before entering an operator. After typing
595 * "3d" we return from normal_cmd() and come back here, the "3" is
596 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ca.opcount = opcount;
598
599#ifdef FEAT_SNIFF
600 want_sniff_request = sniff_connected;
601#endif
602
603 /*
604 * If there is an operator pending, then the command we take this time
605 * will terminate it. Finish_op tells us to finish the operation before
606 * returning this time (unless the operation was cancelled).
607 */
608#ifdef CURSOR_SHAPE
609 c = finish_op;
610#endif
611 finish_op = (oap->op_type != OP_NOP);
612#ifdef CURSOR_SHAPE
613 if (finish_op != c)
614 {
615 ui_cursor_shape(); /* may show different cursor shape */
616# ifdef FEAT_MOUSESHAPE
617 update_mouseshape(-1);
618# endif
619 }
620#endif
621
Bram Moolenaara983fe92008-07-31 20:04:27 +0000622 /* When not finishing an operator and no register name typed, reset the
623 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000627#ifdef FEAT_EVAL
628 set_prevcount = TRUE;
629#endif
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaara983fe92008-07-31 20:04:27 +0000632#ifdef FEAT_AUTOCMD
633 /* Restore counts from before receiving K_CURSORHOLD. This means after
634 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
635 * "3 * 2". */
636 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
637 {
638 ca.opcount = oap->prev_opcount;
639 ca.count0 = oap->prev_count0;
640 oap->prev_opcount = 0;
641 oap->prev_count0 = 0;
642 }
643#endif
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_VISUAL
646 mapped_len = typebuf_maplen();
647#endif
648
649 State = NORMAL_BUSY;
650#ifdef USE_ON_FLY_SCROLL
651 dont_scroll = FALSE; /* allow scrolling here */
652#endif
653
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100654#ifdef FEAT_EVAL
655 /* Set v:count here, when called from main() and not a stuffed
656 * command, so that v:count can be used in an expression mapping
657 * when there is no count. */
658 if (toplevel && stuff_empty())
659 set_vcount_ca(&ca, &set_prevcount);
660#endif
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 /*
663 * Get the command character from the user.
664 */
665 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000668#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /*
670 * If a mapping was started in Visual or Select mode, remember the length
671 * of the mapping. This is used below to not return to Insert mode for as
672 * long as the mapping is being executed.
673 */
674 if (restart_edit == 0)
675 old_mapped_len = 0;
676 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000677 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 if (c == NUL)
682 c = K_ZERO;
683
684#ifdef FEAT_VISUAL
685 /*
686 * In Select mode, typed text replaces the selection.
687 */
688 if (VIsual_active
689 && VIsual_select
690 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
691 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000692 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
693 * 'insertmode' is set) fake a "d"elete command, Insert mode will
694 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000695 * Insert the typed character in the typeahead buffer, so that it can
696 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000697 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000698 if (restart_edit != 0)
699 c = 'd';
700 else
701 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000702 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704#endif
705
706#ifdef FEAT_CMDL_INFO
707 need_flushbuf = add_to_showcmd(c);
708#endif
709
710getcount:
711#ifdef FEAT_VISUAL
712 if (!(VIsual_active && VIsual_select))
713#endif
714 {
715 /*
716 * Handle a count before a command and compute ca.count0.
717 * Note that '0' is a command and not the start of a count, but it's
718 * part of a count after other digits.
719 */
720 while ( (c >= '1' && c <= '9')
721 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
722 {
723 if (c == K_DEL || c == K_KDEL)
724 {
725 ca.count0 /= 10;
726#ifdef FEAT_CMDL_INFO
727 del_from_showcmd(4); /* delete the digit and ~@% */
728#endif
729 }
730 else
731 ca.count0 = ca.count0 * 10 + (c - '0');
732 if (ca.count0 < 0) /* got too large! */
733 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000734#ifdef FEAT_EVAL
735 /* Set v:count here, when called from main() and not a stuffed
736 * command, so that v:count can be used in an expression mapping
737 * right after the count. */
738 if (toplevel && stuff_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (ctrl_w)
742 {
743 ++no_mapping;
744 ++allow_keys; /* no mapping for nchar, but keys */
745 }
746 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000747 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 --no_zero_mapping;
750 if (ctrl_w)
751 {
752 --no_mapping;
753 --allow_keys;
754 }
755#ifdef FEAT_CMDL_INFO
756 need_flushbuf |= add_to_showcmd(c);
757#endif
758 }
759
760 /*
761 * If we got CTRL-W there may be a/another count
762 */
763 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
764 {
765 ctrl_w = TRUE;
766 ca.opcount = ca.count0; /* remember first count */
767 ca.count0 = 0;
768 ++no_mapping;
769 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000770 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 --no_mapping;
773 --allow_keys;
774#ifdef FEAT_CMDL_INFO
775 need_flushbuf |= add_to_showcmd(c);
776#endif
777 goto getcount; /* jump back */
778 }
779 }
780
Bram Moolenaara983fe92008-07-31 20:04:27 +0000781#ifdef FEAT_AUTOCMD
782 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000784 /* Save the count values so that ca.opcount and ca.count0 are exactly
785 * the same when coming back here after handling K_CURSORHOLD. */
786 oap->prev_opcount = ca.opcount;
787 oap->prev_count0 = ca.count0;
788 }
789 else
790#endif
791 if (ca.opcount != 0)
792 {
793 /*
794 * If we're in the middle of an operator (including after entering a
795 * yank buffer with '"') AND we had a count before the operator, then
796 * that count overrides the current value of ca.count0.
797 * What this means effectively, is that commands like "3dw" get turned
798 * into "d3w" which makes things fall into place pretty neatly.
799 * If you give a count before AND after the operator, they are
800 * multiplied.
801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ca.count0)
803 ca.count0 *= ca.opcount;
804 else
805 ca.count0 = ca.opcount;
806 }
807
808 /*
809 * Always remember the count. It will be set to zero (on the next call,
810 * above) when there is no pending operator.
811 * When called from main(), save the count for use by the "count" built-in
812 * variable.
813 */
814 ca.opcount = ca.count0;
815 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
816
817#ifdef FEAT_EVAL
818 /*
819 * Only set v:count when called from main() and not a stuffed command.
820 */
821 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000822 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824
825 /*
826 * Find the command character in the table of commands.
827 * For CTRL-W we already got nchar when looking for a count.
828 */
829 if (ctrl_w)
830 {
831 ca.nchar = c;
832 ca.cmdchar = Ctrl_W;
833 }
834 else
835 ca.cmdchar = c;
836 idx = find_command(ca.cmdchar);
837 if (idx < 0)
838 {
839 /* Not a known command: beep. */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000843
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000844 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000846 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000848 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 goto normal_end;
850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000851#ifdef FEAT_AUTOCMD
852 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
853 goto normal_end;
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_VISUAL
857 /*
858 * In Visual/Select mode, a few keys are handled in a special way.
859 */
860 if (VIsual_active)
861 {
862 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
863 if (km_stopsel
864 && (nv_cmds[idx].cmd_flags & NV_STS)
865 && !(mod_mask & MOD_MASK_SHIFT))
866 {
867 end_visual_mode();
868 redraw_curbuf_later(INVERTED);
869 }
870
871 /* Keys that work different when 'keymodel' contains "startsel" */
872 if (km_startsel)
873 {
874 if (nv_cmds[idx].cmd_flags & NV_SS)
875 {
876 unshift_special(&ca);
877 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000878 if (idx < 0)
879 {
880 /* Just in case */
881 clearopbeep(oap);
882 goto normal_end;
883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
886 && (mod_mask & MOD_MASK_SHIFT))
887 {
888 mod_mask &= ~MOD_MASK_SHIFT;
889 }
890 }
891 }
892#endif
893
894#ifdef FEAT_RIGHTLEFT
895 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
896 && (nv_cmds[idx].cmd_flags & NV_RL))
897 {
898 /* Invert horizontal movements and operations. Only when typed by the
899 * user directly, not when the result of a mapping or "x" translated
900 * to "dl". */
901 switch (ca.cmdchar)
902 {
903 case 'l': ca.cmdchar = 'h'; break;
904 case K_RIGHT: ca.cmdchar = K_LEFT; break;
905 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
906 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
907 case 'h': ca.cmdchar = 'l'; break;
908 case K_LEFT: ca.cmdchar = K_RIGHT; break;
909 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
910 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
911 case '>': ca.cmdchar = '<'; break;
912 case '<': ca.cmdchar = '>'; break;
913 }
914 idx = find_command(ca.cmdchar);
915 }
916#endif
917
918 /*
919 * Get an additional character if we need one.
920 */
921 if ((nv_cmds[idx].cmd_flags & NV_NCH)
922 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
923 && oap->op_type == OP_NOP)
924 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
925 || (ca.cmdchar == 'q'
926 && oap->op_type == OP_NOP
927 && !Recording
928 && !Exec_reg)
929 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
930 && (oap->op_type != OP_NOP
931#ifdef FEAT_VISUAL
932 || VIsual_active
933#endif
934 ))))
935 {
936 int *cp;
937 int repl = FALSE; /* get character for replace mode */
938 int lit = FALSE; /* get extra character literally */
939 int langmap_active = FALSE; /* using :lmap mappings */
940 int lang; /* getting a text character */
941#ifdef USE_IM_CONTROL
942 int save_smd; /* saved value of p_smd */
943#endif
944
945 ++no_mapping;
946 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000947#ifdef FEAT_AUTOCMD
948 /* Don't generate a CursorHold event here, most commands can't handle
949 * it, e.g., nv_replace(), nv_csearch(). */
950 did_cursorhold = TRUE;
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (ca.cmdchar == 'g')
953 {
954 /*
955 * For 'g' get the next character now, so that we can check for
956 * "gr", "g'" and "g`".
957 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000958 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_CMDL_INFO
961 need_flushbuf |= add_to_showcmd(ca.nchar);
962#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200963 /* For "gn" from redo, need to get one more char to determine the
964 * operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200966 || ca.nchar == Ctrl_BSL
967 || ((ca.nchar == 'n' || ca.nchar == 'N') && !stuff_empty()))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 cp = &ca.extra_char; /* need to get a third character */
970 if (ca.nchar != 'r')
971 lit = TRUE; /* get it literally */
972 else
973 repl = TRUE; /* get it in replace mode */
974 }
975 else
976 cp = NULL; /* no third character needed */
977 }
978 else
979 {
980 if (ca.cmdchar == 'r') /* get it in replace mode */
981 repl = TRUE;
982 cp = &ca.nchar;
983 }
984 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
985
986 /*
987 * Get a second or third character.
988 */
989 if (cp != NULL)
990 {
991#ifdef CURSOR_SHAPE
992 if (repl)
993 {
994 State = REPLACE; /* pretend Replace mode */
995 ui_cursor_shape(); /* show different cursor shape */
996 }
997#endif
998 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
999 {
1000 /* Allow mappings defined with ":lmap". */
1001 --no_mapping;
1002 --allow_keys;
1003 if (repl)
1004 State = LREPLACE;
1005 else
1006 State = LANGMAP;
1007 langmap_active = TRUE;
1008 }
1009#ifdef USE_IM_CONTROL
1010 save_smd = p_smd;
1011 p_smd = FALSE; /* Don't let the IM code show the mode here */
1012 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1013 im_set_active(TRUE);
1014#endif
1015
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001016 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (langmap_active)
1019 {
1020 /* Undo the decrement done above */
1021 ++no_mapping;
1022 ++allow_keys;
1023 State = NORMAL_BUSY;
1024 }
1025#ifdef USE_IM_CONTROL
1026 if (lang)
1027 {
1028 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1029 im_save_status(&curbuf->b_p_iminsert);
1030 im_set_active(FALSE);
1031 }
1032 p_smd = save_smd;
1033#endif
1034#ifdef CURSOR_SHAPE
1035 State = NORMAL_BUSY;
1036#endif
1037#ifdef FEAT_CMDL_INFO
1038 need_flushbuf |= add_to_showcmd(*cp);
1039#endif
1040
1041 if (!lit)
1042 {
1043#ifdef FEAT_DIGRAPHS
1044 /* Typing CTRL-K gets a digraph. */
1045 if (*cp == Ctrl_K
1046 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1047 || cp == &ca.extra_char)
1048 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1049 {
1050 c = get_digraph(FALSE);
1051 if (c > 0)
1052 {
1053 *cp = c;
1054# ifdef FEAT_CMDL_INFO
1055 /* Guessing how to update showcmd here... */
1056 del_from_showcmd(3);
1057 need_flushbuf |= add_to_showcmd(*cp);
1058# endif
1059 }
1060 }
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 /* adjust chars > 127, except after "tTfFr" commands */
1064 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_RIGHTLEFT
1066 /* adjust Hebrew mapped char */
1067 if (p_hkmap && lang && KeyTyped)
1068 *cp = hkmap(*cp);
1069# ifdef FEAT_FKMAP
1070 /* adjust Farsi mapped char */
1071 if (p_fkmap && lang && KeyTyped)
1072 *cp = fkmap(*cp);
1073# endif
1074#endif
1075 }
1076
1077 /*
1078 * When the next character is CTRL-\ a following CTRL-N means the
1079 * command is aborted and we go to Normal mode.
1080 */
1081 if (cp == &ca.extra_char
1082 && ca.nchar == Ctrl_BSL
1083 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1084 {
1085 ca.cmdchar = Ctrl_BSL;
1086 ca.nchar = ca.extra_char;
1087 idx = find_command(ca.cmdchar);
1088 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001089 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001090 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else if (*cp == Ctrl_BSL)
1092 {
1093 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1094
1095 /* There is a busy wait here when typing "f<C-\>" and then
1096 * something different from CTRL-N. Can't be avoided. */
1097 while ((c = vpeekc()) <= 0 && towait > 0L)
1098 {
1099 do_sleep(towait > 50L ? 50L : towait);
1100 towait -= 50L;
1101 }
1102 if (c > 0)
1103 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001104 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (c != Ctrl_N && c != Ctrl_G)
1106 vungetc(c);
1107 else
1108 {
1109 ca.cmdchar = Ctrl_BSL;
1110 ca.nchar = c;
1111 idx = find_command(ca.cmdchar);
1112 }
1113 }
1114 }
1115
1116#ifdef FEAT_MBYTE
1117 /* When getting a text character and the next character is a
1118 * multi-byte character, it could be a composing character.
1119 * However, don't wait for it to arrive. */
1120 while (enc_utf8 && lang && (c = vpeekc()) > 0
1121 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1122 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!utf_iscomposing(c))
1125 {
1126 vungetc(c); /* it wasn't, put it back */
1127 break;
1128 }
1129 else if (ca.ncharC1 == 0)
1130 ca.ncharC1 = c;
1131 else
1132 ca.ncharC2 = c;
1133 }
1134#endif
1135 }
1136 --no_mapping;
1137 --allow_keys;
1138 }
1139
1140#ifdef FEAT_CMDL_INFO
1141 /*
1142 * Flush the showcmd characters onto the screen so we can see them while
1143 * the command is being executed. Only do this when the shown command was
1144 * actually displayed, otherwise this will slow down a lot when executing
1145 * mappings.
1146 */
1147 if (need_flushbuf)
1148 out_flush();
1149#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001150#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001151 if (ca.cmdchar != K_IGNORE)
1152 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 State = NORMAL;
1156
1157 if (ca.nchar == ESC)
1158 {
1159 clearop(oap);
1160 if (restart_edit == 0 && goto_im())
1161 restart_edit = 'a';
1162 goto normal_end;
1163 }
1164
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001165 if (ca.cmdchar != K_IGNORE)
1166 {
1167 msg_didout = FALSE; /* don't scroll screen up for normal command */
1168 msg_col = 0;
1169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171#ifdef FEAT_VISUAL
1172 old_pos = curwin->w_cursor; /* remember where cursor was */
1173
1174 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1175 * mode. */
1176 if (!VIsual_active && km_startsel)
1177 {
1178 if (nv_cmds[idx].cmd_flags & NV_SS)
1179 {
1180 start_selection();
1181 unshift_special(&ca);
1182 idx = find_command(ca.cmdchar);
1183 }
1184 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1185 && (mod_mask & MOD_MASK_SHIFT))
1186 {
1187 start_selection();
1188 mod_mask &= ~MOD_MASK_SHIFT;
1189 }
1190 }
1191#endif
1192
1193 /*
1194 * Execute the command!
1195 * Call the command function found in the commands table.
1196 */
1197 ca.arg = nv_cmds[idx].cmd_arg;
1198 (nv_cmds[idx].cmd_func)(&ca);
1199
1200 /*
1201 * If we didn't start or finish an operator, reset oap->regname, unless we
1202 * need it later.
1203 */
1204 if (!finish_op
1205 && !oap->op_type
1206 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1207 {
1208 clearop(oap);
1209#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001210 {
1211 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 /* Adjust the register according to 'clipboard', so that when
1214 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001215# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001216 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001217# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001218 set_reg_var(regname);
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 }
1222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001224 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * character or "z333<cr>". */
1226 if (old_mapped_len > 0)
1227 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001228#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 /*
1231 * If an operation is pending, handle it...
1232 */
1233 do_pending_operator(&ca, old_col, FALSE);
1234
1235 /*
1236 * Wait for a moment when a message is displayed that will be overwritten
1237 * by the mode message.
1238 * In Visual mode and with "^O" in Insert mode, a short message will be
1239 * overwritten by the mode message. Wait a bit, until a key is hit.
1240 * In Visual mode, it's more important to keep the Visual area updated
1241 * than keeping a message (e.g. from a /pat search).
1242 * Only do this if the command was typed, not from a mapping.
1243 * Don't wait when emsg_silent is non-zero.
1244 * Also wait a bit after an error message, e.g. for "^O:".
1245 * Don't redraw the screen, it would remove the message.
1246 */
1247 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001248 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 && (restart_edit != 0
1250#ifdef FEAT_VISUAL
1251 || (VIsual_active
1252 && old_pos.lnum == curwin->w_cursor.lnum
1253 && old_pos.col == curwin->w_cursor.col)
1254#endif
1255 )
1256 && (clear_cmdline
1257 || redraw_cmdline)
1258 && (msg_didout || (msg_didany && msg_scroll))
1259 && !msg_nowait
1260 && KeyTyped)
1261 || (restart_edit != 0
1262#ifdef FEAT_VISUAL
1263 && !VIsual_active
1264#endif
1265 && (msg_scroll
1266 || emsg_on_display)))
1267 && oap->regname == 0
1268 && !(ca.retval & CA_COMMAND_BUSY)
1269 && stuff_empty()
1270 && typebuf_typed()
1271 && emsg_silent == 0
1272 && !did_wait_return
1273 && oap->op_type == OP_NOP)
1274 {
1275 int save_State = State;
1276
1277 /* Draw the cursor with the right shape here */
1278 if (restart_edit != 0)
1279 State = INSERT;
1280
1281 /* If need to redraw, and there is a "keep_msg", redraw before the
1282 * delay */
1283 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1284 {
1285 char_u *kmsg;
1286
1287 kmsg = keep_msg;
1288 keep_msg = NULL;
1289 /* showmode() will clear keep_msg, but we want to use it anyway */
1290 update_screen(0);
1291 /* now reset it, otherwise it's put in the history again */
1292 keep_msg = kmsg;
1293 msg_attr(kmsg, keep_msg_attr);
1294 vim_free(kmsg);
1295 }
1296 setcursor();
1297 cursor_on();
1298 out_flush();
1299 if (msg_scroll || emsg_on_display)
1300 ui_delay(1000L, TRUE); /* wait at least one second */
1301 ui_delay(3000L, FALSE); /* wait up to three seconds */
1302 State = save_State;
1303
1304 msg_scroll = FALSE;
1305 emsg_on_display = FALSE;
1306 }
1307
1308 /*
1309 * Finish up after executing a Normal mode command.
1310 */
1311normal_end:
1312
1313 msg_nowait = FALSE;
1314
1315 /* Reset finish_op, in case it was set */
1316#ifdef CURSOR_SHAPE
1317 c = finish_op;
1318#endif
1319 finish_op = FALSE;
1320#ifdef CURSOR_SHAPE
1321 /* Redraw the cursor with another shape, if we were in Operator-pending
1322 * mode or did a replace command. */
1323 if (c || ca.cmdchar == 'r')
1324 {
1325 ui_cursor_shape(); /* may show different cursor shape */
1326# ifdef FEAT_MOUSESHAPE
1327 update_mouseshape(-1);
1328# endif
1329 }
1330#endif
1331
1332#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001333 if (oap->op_type == OP_NOP && oap->regname == 0
1334# ifdef FEAT_AUTOCMD
1335 && ca.cmdchar != K_CURSORHOLD
1336# endif
1337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 clear_showcmd();
1339#endif
1340
1341 checkpcmark(); /* check if we moved since setting pcmark */
1342 vim_free(ca.searchbuf);
1343
1344#ifdef FEAT_MBYTE
1345 if (has_mbyte)
1346 mb_adjust_cursor();
1347#endif
1348
1349#ifdef FEAT_SCROLLBIND
1350 if (curwin->w_p_scb && toplevel)
1351 {
1352 validate_cursor(); /* may need to update w_leftcol */
1353 do_check_scrollbind(TRUE);
1354 }
1355#endif
1356
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357#ifdef FEAT_CURSORBIND
1358 if (curwin->w_p_crb && toplevel)
1359 {
1360 validate_cursor(); /* may need to update w_leftcol */
1361 do_check_cursorbind();
1362 }
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1367 * if still inside a mapping that started in Visual mode).
1368 * May switch from Visual to Select mode after CTRL-O command.
1369 */
1370 if ( oap->op_type == OP_NOP
1371#ifdef FEAT_VISUAL
1372 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1373 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001374#else
1375 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#endif
1377 && !(ca.retval & CA_COMMAND_BUSY)
1378 && stuff_empty()
1379 && oap->regname == 0)
1380 {
1381#ifdef FEAT_VISUAL
1382 if (restart_VIsual_select == 1)
1383 {
1384 VIsual_select = TRUE;
1385 showmode();
1386 restart_VIsual_select = 0;
1387 }
1388#endif
1389 if (restart_edit != 0
1390#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001391 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001393 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 (void)edit(restart_edit, FALSE, 1L);
1395 }
1396
1397#ifdef FEAT_VISUAL
1398 if (restart_VIsual_select == 2)
1399 restart_VIsual_select = 1;
1400#endif
1401
1402 /* Save count before an operator for next time. */
1403 opcount = ca.opcount;
1404}
1405
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001406#ifdef FEAT_EVAL
1407/*
1408 * Set v:count and v:count1 according to "cap".
1409 * Set v:prevcount only when "set_prevcount" is TRUE.
1410 */
1411 static void
1412set_vcount_ca(cap, set_prevcount)
1413 cmdarg_T *cap;
1414 int *set_prevcount;
1415{
1416 long count = cap->count0;
1417
1418 /* multiply with cap->opcount the same way as above */
1419 if (cap->opcount != 0)
1420 count = cap->opcount * (count == 0 ? 1 : count);
1421 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1422 *set_prevcount = FALSE; /* only set v:prevcount once */
1423}
1424#endif
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/*
1427 * Handle an operator after visual mode or when the movement is finished
1428 */
1429 void
1430do_pending_operator(cap, old_col, gui_yank)
1431 cmdarg_T *cap;
1432 int old_col;
1433 int gui_yank;
1434{
1435 oparg_T *oap = cap->oap;
1436 pos_T old_cursor;
1437 int empty_region_error;
1438 int restart_edit_save;
1439
1440#ifdef FEAT_VISUAL
1441 /* The visual area is remembered for redo */
1442 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1443 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001444 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 static long redo_VIsual_count; /* count for Visual operator */
1446# ifdef FEAT_VIRTUALEDIT
1447 int include_line_break = FALSE;
1448# endif
1449#endif
1450
1451#if defined(FEAT_CLIPBOARD)
1452 /*
1453 * Yank the visual area into the GUI selection register before we operate
1454 * on it and lose it forever.
1455 * Don't do it if a specific register was specified, so that ""x"*P works.
1456 * This could call do_pending_operator() recursively, but that's OK
1457 * because gui_yank will be TRUE for the nested call.
1458 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001459 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 && oap->op_type != OP_NOP
1461 && !gui_yank
1462# ifdef FEAT_VISUAL
1463 && VIsual_active
1464 && !redo_VIsual_busy
1465# endif
1466 && oap->regname == 0)
1467 clip_auto_select();
1468#endif
1469 old_cursor = curwin->w_cursor;
1470
1471 /*
1472 * If an operation is pending, handle it...
1473 */
1474 if ((finish_op
1475#ifdef FEAT_VISUAL
1476 || VIsual_active
1477#endif
1478 ) && oap->op_type != OP_NOP)
1479 {
1480#ifdef FEAT_VISUAL
1481 oap->is_VIsual = VIsual_active;
1482 if (oap->motion_force == 'V')
1483 oap->motion_type = MLINE;
1484 else if (oap->motion_force == 'v')
1485 {
1486 /* If the motion was linewise, "inclusive" will not have been set.
1487 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1488 if (oap->motion_type == MLINE)
1489 oap->inclusive = FALSE;
1490 /* If the motion already was characterwise, toggle "inclusive" */
1491 else if (oap->motion_type == MCHAR)
1492 oap->inclusive = !oap->inclusive;
1493 oap->motion_type = MCHAR;
1494 }
1495 else if (oap->motion_force == Ctrl_V)
1496 {
1497 /* Change line- or characterwise motion into Visual block mode. */
1498 VIsual_active = TRUE;
1499 VIsual = oap->start;
1500 VIsual_mode = Ctrl_V;
1501 VIsual_select = FALSE;
1502 VIsual_reselect = FALSE;
1503 }
1504#endif
1505
1506 /* only redo yank when 'y' flag is in 'cpoptions' */
1507 /* never redo "zf" (define fold) */
1508 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1509#ifdef FEAT_VISUAL
1510 && (!VIsual_active || oap->motion_force)
1511#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001512 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#ifdef FEAT_FOLDING
1514 && oap->op_type != OP_FOLD
1515 && oap->op_type != OP_FOLDOPEN
1516 && oap->op_type != OP_FOLDOPENREC
1517 && oap->op_type != OP_FOLDCLOSE
1518 && oap->op_type != OP_FOLDCLOSEREC
1519 && oap->op_type != OP_FOLDDEL
1520 && oap->op_type != OP_FOLDDELREC
1521#endif
1522 )
1523 {
1524 prep_redo(oap->regname, cap->count0,
1525 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1526 oap->motion_force, cap->cmdchar, cap->nchar);
1527 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1528 {
1529 /*
1530 * If 'cpoptions' does not contain 'r', insert the search
1531 * pattern to really repeat the same command.
1532 */
1533 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001534 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 AppendToRedobuff(NL_STR);
1536 }
1537 else if (cap->cmdchar == ':')
1538 {
1539 /* do_cmdline() has stored the first typed line in
1540 * "repeat_cmdline". When several lines are typed repeating
1541 * won't be possible. */
1542 if (repeat_cmdline == NULL)
1543 ResetRedobuff();
1544 else
1545 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001546 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 AppendToRedobuff(NL_STR);
1548 vim_free(repeat_cmdline);
1549 repeat_cmdline = NULL;
1550 }
1551 }
1552 }
1553
1554#ifdef FEAT_VISUAL
1555 if (redo_VIsual_busy)
1556 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001557 /* Redo of an operation on a Visual area. Use the same size from
1558 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 oap->start = curwin->w_cursor;
1560 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1561 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1562 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1563 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001564 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001566 if (VIsual_mode == 'v')
1567 {
1568 if (redo_VIsual_line_count <= 1)
1569 {
1570 validate_virtcol();
1571 curwin->w_curswant =
1572 curwin->w_virtcol + redo_VIsual_vcol - 1;
1573 }
1574 else
1575 curwin->w_curswant = redo_VIsual_vcol;
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001578 {
1579 curwin->w_curswant = MAXCOL;
1580 }
1581 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 cap->count0 = redo_VIsual_count;
1584 if (redo_VIsual_count != 0)
1585 cap->count1 = redo_VIsual_count;
1586 else
1587 cap->count1 = 1;
1588 }
1589 else if (VIsual_active)
1590 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001591 if (!gui_yank)
1592 {
1593 /* Save the current VIsual area for '< and '> marks, and "gv" */
1594 curbuf->b_visual.vi_start = VIsual;
1595 curbuf->b_visual.vi_end = curwin->w_cursor;
1596 curbuf->b_visual.vi_mode = VIsual_mode;
1597 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001599 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 /* In Select mode, a linewise selection is operated upon like a
1604 * characterwise selection. */
1605 if (VIsual_select && VIsual_mode == 'V')
1606 {
1607 if (lt(VIsual, curwin->w_cursor))
1608 {
1609 VIsual.col = 0;
1610 curwin->w_cursor.col =
1611 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1612 }
1613 else
1614 {
1615 curwin->w_cursor.col = 0;
1616 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1617 }
1618 VIsual_mode = 'v';
1619 }
1620 /* If 'selection' is "exclusive", backup one character for
1621 * charwise selections. */
1622 else if (VIsual_mode == 'v')
1623 {
1624# ifdef FEAT_VIRTUALEDIT
1625 include_line_break =
1626# endif
1627 unadjust_for_sel();
1628 }
1629
1630 oap->start = VIsual;
1631 if (VIsual_mode == 'V')
1632 oap->start.col = 0;
1633 }
1634#endif /* FEAT_VISUAL */
1635
1636 /*
1637 * Set oap->start to the first position of the operated text, oap->end
1638 * to the end of the operated text. w_cursor is equal to oap->start.
1639 */
1640 if (lt(oap->start, curwin->w_cursor))
1641 {
1642#ifdef FEAT_FOLDING
1643 /* Include folded lines completely. */
1644 if (!VIsual_active)
1645 {
1646 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1647 oap->start.col = 0;
1648 if (hasFolding(curwin->w_cursor.lnum, NULL,
1649 &curwin->w_cursor.lnum))
1650 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1651 }
1652#endif
1653 oap->end = curwin->w_cursor;
1654 curwin->w_cursor = oap->start;
1655
1656 /* w_virtcol may have been updated; if the cursor goes back to its
1657 * previous position w_virtcol becomes invalid and isn't updated
1658 * automatically. */
1659 curwin->w_valid &= ~VALID_VIRTCOL;
1660 }
1661 else
1662 {
1663#ifdef FEAT_FOLDING
1664 /* Include folded lines completely. */
1665 if (!VIsual_active && oap->motion_type == MLINE)
1666 {
1667 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1668 NULL))
1669 curwin->w_cursor.col = 0;
1670 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1671 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1672 }
1673#endif
1674 oap->end = oap->start;
1675 oap->start = curwin->w_cursor;
1676 }
1677
1678 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1679
1680#ifdef FEAT_VIRTUALEDIT
1681 /* Set "virtual_op" before resetting VIsual_active. */
1682 virtual_op = virtual_active();
1683#endif
1684
1685#ifdef FEAT_VISUAL
1686 if (VIsual_active || redo_VIsual_busy)
1687 {
1688 if (VIsual_mode == Ctrl_V) /* block mode */
1689 {
1690 colnr_T start, end;
1691
1692 oap->block_mode = TRUE;
1693
1694 getvvcol(curwin, &(oap->start),
1695 &oap->start_vcol, NULL, &oap->end_vcol);
1696 if (!redo_VIsual_busy)
1697 {
1698 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1699
1700 if (start < oap->start_vcol)
1701 oap->start_vcol = start;
1702 if (end > oap->end_vcol)
1703 {
1704 if (*p_sel == 'e' && start >= 1
1705 && start - 1 >= oap->end_vcol)
1706 oap->end_vcol = start - 1;
1707 else
1708 oap->end_vcol = end;
1709 }
1710 }
1711
1712 /* if '$' was used, get oap->end_vcol from longest line */
1713 if (curwin->w_curswant == MAXCOL)
1714 {
1715 curwin->w_cursor.col = MAXCOL;
1716 oap->end_vcol = 0;
1717 for (curwin->w_cursor.lnum = oap->start.lnum;
1718 curwin->w_cursor.lnum <= oap->end.lnum;
1719 ++curwin->w_cursor.lnum)
1720 {
1721 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1722 if (end > oap->end_vcol)
1723 oap->end_vcol = end;
1724 }
1725 }
1726 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001727 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 /*
1729 * Correct oap->end.col and oap->start.col to be the
1730 * upper-left and lower-right corner of the block area.
1731 *
1732 * (Actually, this does convert column positions into character
1733 * positions)
1734 */
1735 curwin->w_cursor.lnum = oap->end.lnum;
1736 coladvance(oap->end_vcol);
1737 oap->end = curwin->w_cursor;
1738
1739 curwin->w_cursor = oap->start;
1740 coladvance(oap->start_vcol);
1741 oap->start = curwin->w_cursor;
1742 }
1743
1744 if (!redo_VIsual_busy && !gui_yank)
1745 {
1746 /*
1747 * Prepare to reselect and redo Visual: this is based on the
1748 * size of the Visual text
1749 */
1750 resel_VIsual_mode = VIsual_mode;
1751 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001752 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001754 {
1755 if (VIsual_mode != Ctrl_V)
1756 getvvcol(curwin, &(oap->end),
1757 NULL, NULL, &oap->end_vcol);
1758 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1759 {
1760 if (VIsual_mode != Ctrl_V)
1761 getvvcol(curwin, &(oap->start),
1762 &oap->start_vcol, NULL, NULL);
1763 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1764 }
1765 else
1766 resel_VIsual_vcol = oap->end_vcol;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 resel_VIsual_line_count = oap->line_count;
1769 }
1770
1771 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1772 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1773 && oap->op_type != OP_COLON
1774#ifdef FEAT_FOLDING
1775 && oap->op_type != OP_FOLD
1776 && oap->op_type != OP_FOLDOPEN
1777 && oap->op_type != OP_FOLDOPENREC
1778 && oap->op_type != OP_FOLDCLOSE
1779 && oap->op_type != OP_FOLDCLOSEREC
1780 && oap->op_type != OP_FOLDDEL
1781 && oap->op_type != OP_FOLDDELREC
1782#endif
1783 && oap->motion_force == NUL
1784 )
1785 {
1786 /* Prepare for redoing. Only use the nchar field for "r",
1787 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001788 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1789 || cap->nchar == 'N'))
1790 /* "gn" and "gN" are a bit different */
1791 prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
1792 get_op_char(oap->op_type),
1793 get_extra_op_char(oap->op_type));
1794 else
1795 prep_redo(oap->regname, 0L, NUL, 'v',
1796 get_op_char(oap->op_type),
1797 get_extra_op_char(oap->op_type),
1798 oap->op_type == OP_REPLACE
1799 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (!redo_VIsual_busy)
1801 {
1802 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001803 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 redo_VIsual_line_count = resel_VIsual_line_count;
1805 redo_VIsual_count = cap->count0;
1806 }
1807 }
1808
1809 /*
1810 * oap->inclusive defaults to TRUE.
1811 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1812 * FALSE. This makes "d}P" and "v}dP" work the same.
1813 */
1814 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1815 oap->inclusive = TRUE;
1816 if (VIsual_mode == 'V')
1817 oap->motion_type = MLINE;
1818 else
1819 {
1820 oap->motion_type = MCHAR;
1821 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1822# ifdef FEAT_VIRTUALEDIT
1823 && (include_line_break || !virtual_op)
1824# endif
1825 )
1826 {
1827 oap->inclusive = FALSE;
1828 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001829 * that works on lines only. */
1830 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001832 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1833 {
1834 ++oap->end.lnum;
1835 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001837 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001839 ++oap->line_count;
1840 }
1841 else
1842 {
1843 /* Cannot move below the last line, make the op
1844 * inclusive to tell the operation to include the
1845 * line break. */
1846 oap->inclusive = TRUE;
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 }
1850 }
1851
1852 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 /*
1855 * Switch Visual off now, so screen updating does
1856 * not show inverted text when the screen is redrawn.
1857 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1858 * no screen redraw, so it is done here to remove the inverted
1859 * part.
1860 */
1861 if (!gui_yank)
1862 {
1863 VIsual_active = FALSE;
1864# ifdef FEAT_MOUSE
1865 setmouse();
1866 mouse_dragging = 0;
1867# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001868 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 clear_cmdline = TRUE; /* unshow visual mode later */
1870#ifdef FEAT_CMDL_INFO
1871 else
1872 clear_showcmd();
1873#endif
1874 if ((oap->op_type == OP_YANK
1875 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001876 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 || oap->op_type == OP_FILTER)
1878 && oap->motion_force == NUL)
1879 redraw_curbuf_later(INVERTED);
1880 }
1881 }
1882#endif
1883
1884#ifdef FEAT_MBYTE
1885 /* Include the trailing byte of a multi-byte char. */
1886 if (has_mbyte && oap->inclusive)
1887 {
1888 int l;
1889
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001890 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (l > 1)
1892 oap->end.col += l - 1;
1893 }
1894#endif
1895 curwin->w_set_curswant = TRUE;
1896
1897 /*
1898 * oap->empty is set when start and end are the same. The inclusive
1899 * flag affects this too, unless yanking and the end is on a NUL.
1900 */
1901 oap->empty = (oap->motion_type == MCHAR
1902 && (!oap->inclusive
1903 || (oap->op_type == OP_YANK
1904 && gchar_pos(&oap->end) == NUL))
1905 && equalpos(oap->start, oap->end)
1906#ifdef FEAT_VIRTUALEDIT
1907 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1908#endif
1909 );
1910 /*
1911 * For delete, change and yank, it's an error to operate on an
1912 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1913 */
1914 empty_region_error = (oap->empty
1915 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1916
1917#ifdef FEAT_VISUAL
1918 /* Force a redraw when operating on an empty Visual region, when
1919 * 'modifiable is off or creating a fold. */
1920 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1921# ifdef FEAT_FOLDING
1922 || oap->op_type == OP_FOLD
1923# endif
1924 ))
1925 redraw_curbuf_later(INVERTED);
1926#endif
1927
1928 /*
1929 * If the end of an operator is in column one while oap->motion_type
1930 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1931 * character in the previous line. If op_start is on or before the
1932 * first non-blank in the line, the operator becomes linewise
1933 * (strange, but that's the way vi does it).
1934 */
1935 if ( oap->motion_type == MCHAR
1936 && oap->inclusive == FALSE
1937 && !(cap->retval & CA_NO_ADJ_OP_END)
1938 && oap->end.col == 0
1939#ifdef FEAT_VISUAL
1940 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001941 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942#endif
1943 && oap->line_count > 1)
1944 {
1945 oap->end_adjusted = TRUE; /* remember that we did this */
1946 --oap->line_count;
1947 --oap->end.lnum;
1948 if (inindent(0))
1949 oap->motion_type = MLINE;
1950 else
1951 {
1952 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1953 if (oap->end.col)
1954 {
1955 --oap->end.col;
1956 oap->inclusive = TRUE;
1957 }
1958 }
1959 }
1960 else
1961 oap->end_adjusted = FALSE;
1962
1963 switch (oap->op_type)
1964 {
1965 case OP_LSHIFT:
1966 case OP_RSHIFT:
1967 op_shift(oap, TRUE,
1968#ifdef FEAT_VISUAL
1969 oap->is_VIsual ? (int)cap->count1 :
1970#endif
1971 1);
1972 auto_format(FALSE, TRUE);
1973 break;
1974
1975 case OP_JOIN_NS:
1976 case OP_JOIN:
1977 if (oap->line_count < 2)
1978 oap->line_count = 2;
1979 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1980 curbuf->b_ml.ml_line_count)
1981 beep_flush();
1982 else
1983 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001984 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 auto_format(FALSE, TRUE);
1986 }
1987 break;
1988
1989 case OP_DELETE:
1990#ifdef FEAT_VISUAL
1991 VIsual_reselect = FALSE; /* don't reselect now */
1992#endif
1993 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001996 CancelRedo();
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 {
2000 (void)op_delete(oap);
2001 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
2002 u_save_cursor(); /* cursor line wasn't saved yet */
2003 auto_format(FALSE, TRUE);
2004 }
2005 break;
2006
2007 case OP_YANK:
2008 if (empty_region_error)
2009 {
2010 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002013 CancelRedo();
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 else
2017 (void)op_yank(oap, FALSE, !gui_yank);
2018 check_cursor_col();
2019 break;
2020
2021 case OP_CHANGE:
2022#ifdef FEAT_VISUAL
2023 VIsual_reselect = FALSE; /* don't reselect now */
2024#endif
2025 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002028 CancelRedo();
2029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 else
2031 {
2032 /* This is a new edit command, not a restart. Need to
2033 * remember it to make 'insertmode' work with mappings for
2034 * Visual mode. But do this only once and not when typed and
2035 * 'insertmode' isn't set. */
2036 if (p_im || !KeyTyped)
2037 restart_edit_save = restart_edit;
2038 else
2039 restart_edit_save = 0;
2040 restart_edit = 0;
2041 /* Reset finish_op now, don't want it set inside edit(). */
2042 finish_op = FALSE;
2043 if (op_change(oap)) /* will call edit() */
2044 cap->retval |= CA_COMMAND_BUSY;
2045 if (restart_edit == 0)
2046 restart_edit = restart_edit_save;
2047 }
2048 break;
2049
2050 case OP_FILTER:
2051 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2052 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2053 else
2054 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2055
2056 case OP_INDENT:
2057 case OP_COLON:
2058
2059#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2060 /*
2061 * If 'equalprg' is empty, do the indenting internally.
2062 */
2063 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2064 {
2065# ifdef FEAT_LISP
2066 if (curbuf->b_p_lisp)
2067 {
2068 op_reindent(oap, get_lisp_indent);
2069 break;
2070 }
2071# endif
2072# ifdef FEAT_CINDENT
2073 op_reindent(oap,
2074# ifdef FEAT_EVAL
2075 *curbuf->b_p_inde != NUL ? get_expr_indent :
2076# endif
2077 get_c_indent);
2078 break;
2079# endif
2080 }
2081#endif
2082
2083 op_colon(oap);
2084 break;
2085
2086 case OP_TILDE:
2087 case OP_UPPER:
2088 case OP_LOWER:
2089 case OP_ROT13:
2090 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002093 CancelRedo();
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else
2096 op_tilde(oap);
2097 check_cursor_col();
2098 break;
2099
2100 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002101#if defined(FEAT_EVAL)
2102 if (*curbuf->b_p_fex != NUL)
2103 op_formatexpr(oap); /* use expression */
2104 else
2105#endif
2106 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 op_colon(oap); /* use external command */
2108 else
2109 op_format(oap, FALSE); /* use internal function */
2110 break;
2111
2112 case OP_FORMAT2:
2113 op_format(oap, TRUE); /* use internal function */
2114 break;
2115
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002116 case OP_FUNCTION:
2117 op_function(oap); /* call 'operatorfunc' */
2118 break;
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 case OP_INSERT:
2121 case OP_APPEND:
2122#ifdef FEAT_VISUAL
2123 VIsual_reselect = FALSE; /* don't reselect now */
2124#endif
2125#ifdef FEAT_VISUALEXTRA
2126 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002129 CancelRedo();
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
2133 /* This is a new edit command, not a restart. Need to
2134 * remember it to make 'insertmode' work with mappings for
2135 * Visual mode. But do this only once. */
2136 restart_edit_save = restart_edit;
2137 restart_edit = 0;
2138
2139 op_insert(oap, cap->count1);
2140
2141 /* TODO: when inserting in several lines, should format all
2142 * the lines. */
2143 auto_format(FALSE, TRUE);
2144
2145 if (restart_edit == 0)
2146 restart_edit = restart_edit_save;
2147 }
2148#else
2149 vim_beep();
2150#endif
2151 break;
2152
2153 case OP_REPLACE:
2154#ifdef FEAT_VISUAL
2155 VIsual_reselect = FALSE; /* don't reselect now */
2156#endif
2157#ifdef FEAT_VISUALEXTRA
2158 if (empty_region_error)
2159#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002162 CancelRedo();
2163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef FEAT_VISUALEXTRA
2165 else
2166 op_replace(oap, cap->nchar);
2167#endif
2168 break;
2169
2170#ifdef FEAT_FOLDING
2171 case OP_FOLD:
2172 VIsual_reselect = FALSE; /* don't reselect now */
2173 foldCreate(oap->start.lnum, oap->end.lnum);
2174 break;
2175
2176 case OP_FOLDOPEN:
2177 case OP_FOLDOPENREC:
2178 case OP_FOLDCLOSE:
2179 case OP_FOLDCLOSEREC:
2180 VIsual_reselect = FALSE; /* don't reselect now */
2181 opFoldRange(oap->start.lnum, oap->end.lnum,
2182 oap->op_type == OP_FOLDOPEN
2183 || oap->op_type == OP_FOLDOPENREC,
2184 oap->op_type == OP_FOLDOPENREC
2185 || oap->op_type == OP_FOLDCLOSEREC,
2186 oap->is_VIsual);
2187 break;
2188
2189 case OP_FOLDDEL:
2190 case OP_FOLDDELREC:
2191 VIsual_reselect = FALSE; /* don't reselect now */
2192 deleteFold(oap->start.lnum, oap->end.lnum,
2193 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2194 break;
2195#endif
2196 default:
2197 clearopbeep(oap);
2198 }
2199#ifdef FEAT_VIRTUALEDIT
2200 virtual_op = MAYBE;
2201#endif
2202 if (!gui_yank)
2203 {
2204 /*
2205 * if 'sol' not set, go back to old column for some commands
2206 */
2207 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2208 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2209 || oap->op_type == OP_DELETE))
2210 coladvance(curwin->w_curswant = old_col);
2211 }
2212 else
2213 {
2214 curwin->w_cursor = old_cursor;
2215 }
2216#ifdef FEAT_VISUAL
2217 oap->block_mode = FALSE;
2218#endif
2219 clearop(oap);
2220 }
2221}
2222
2223/*
2224 * Handle indent and format operators and visual mode ":".
2225 */
2226 static void
2227op_colon(oap)
2228 oparg_T *oap;
2229{
2230 stuffcharReadbuff(':');
2231#ifdef FEAT_VISUAL
2232 if (oap->is_VIsual)
2233 stuffReadbuff((char_u *)"'<,'>");
2234 else
2235#endif
2236 {
2237 /*
2238 * Make the range look nice, so it can be repeated.
2239 */
2240 if (oap->start.lnum == curwin->w_cursor.lnum)
2241 stuffcharReadbuff('.');
2242 else
2243 stuffnumReadbuff((long)oap->start.lnum);
2244 if (oap->end.lnum != oap->start.lnum)
2245 {
2246 stuffcharReadbuff(',');
2247 if (oap->end.lnum == curwin->w_cursor.lnum)
2248 stuffcharReadbuff('.');
2249 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2250 stuffcharReadbuff('$');
2251 else if (oap->start.lnum == curwin->w_cursor.lnum)
2252 {
2253 stuffReadbuff((char_u *)".+");
2254 stuffnumReadbuff((long)oap->line_count - 1);
2255 }
2256 else
2257 stuffnumReadbuff((long)oap->end.lnum);
2258 }
2259 }
2260 if (oap->op_type != OP_COLON)
2261 stuffReadbuff((char_u *)"!");
2262 if (oap->op_type == OP_INDENT)
2263 {
2264#ifndef FEAT_CINDENT
2265 if (*get_equalprg() == NUL)
2266 stuffReadbuff((char_u *)"indent");
2267 else
2268#endif
2269 stuffReadbuff(get_equalprg());
2270 stuffReadbuff((char_u *)"\n");
2271 }
2272 else if (oap->op_type == OP_FORMAT)
2273 {
2274 if (*p_fp == NUL)
2275 stuffReadbuff((char_u *)"fmt");
2276 else
2277 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002278 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /*
2282 * do_cmdline() does the rest
2283 */
2284}
2285
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002286/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002287 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002288 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002289 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002290op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002291 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002292{
2293#ifdef FEAT_EVAL
2294 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002295# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002296 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002297# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002298
2299 if (*p_opfunc == NUL)
2300 EMSG(_("E774: 'operatorfunc' is empty"));
2301 else
2302 {
2303 /* Set '[ and '] marks to text to be operated on. */
2304 curbuf->b_op_start = oap->start;
2305 curbuf->b_op_end = oap->end;
2306 if (oap->motion_type != MLINE && !oap->inclusive)
2307 /* Exclude the end position. */
2308 decl(&curbuf->b_op_end);
2309
2310 if (oap->block_mode)
2311 argv[0] = (char_u *)"block";
2312 else if (oap->motion_type == MLINE)
2313 argv[0] = (char_u *)"line";
2314 else
2315 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002316
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002317# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002318 /* Reset virtual_op so that 'virtualedit' can be changed in the
2319 * function. */
2320 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002321# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002322
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002323 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002324
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002325# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002326 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002327# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002328 }
2329#else
2330 EMSG(_("E775: Eval feature not available"));
2331#endif
2332}
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#if defined(FEAT_MOUSE) || defined(PROTO)
2335/*
2336 * Do the appropriate action for the current mouse click in the current mode.
2337 * Not used for Command-line mode.
2338 *
2339 * Normal Mode:
2340 * event modi- position visual change action
2341 * fier cursor window
2342 * left press - yes end yes
2343 * left press C yes end yes "^]" (2)
2344 * left press S yes end yes "*" (2)
2345 * left drag - yes start if moved no
2346 * left relse - yes start if moved no
2347 * middle press - yes if not active no put register
2348 * middle press - yes if active no yank and put
2349 * right press - yes start or extend yes
2350 * right press S yes no change yes "#" (2)
2351 * right drag - yes extend no
2352 * right relse - yes extend no
2353 *
2354 * Insert or Replace Mode:
2355 * event modi- position visual change action
2356 * fier cursor window
2357 * left press - yes (cannot be active) yes
2358 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2359 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2360 * left drag - yes start or extend (1) no CTRL-O (1)
2361 * left relse - yes start or extend (1) no CTRL-O (1)
2362 * middle press - no (cannot be active) no put register
2363 * right press - yes start or extend yes CTRL-O
2364 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2365 *
2366 * (1) only if mouse pointer moved since press
2367 * (2) only if click is in same buffer
2368 *
2369 * Return TRUE if start_arrow() should be called for edit mode.
2370 */
2371 int
2372do_mouse(oap, c, dir, count, fixindent)
2373 oparg_T *oap; /* operator argument, can be NULL */
2374 int c; /* K_LEFTMOUSE, etc */
2375 int dir; /* Direction to 'put' if necessary */
2376 long count;
2377 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2378{
2379 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2380 static int got_click = FALSE; /* got a click some time back */
2381
2382 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2383 int is_click; /* If FALSE it's a drag or release event */
2384 int is_drag; /* If TRUE it's a drag event */
2385 int jump_flags = 0; /* flags for jump_to_mouse() */
2386 pos_T start_visual;
2387 int moved; /* Has cursor moved? */
2388 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002389#ifdef FEAT_WINDOWS
2390 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_VERTSPLIT
2393 int in_sep_line; /* mouse in vertical separator line */
2394#endif
2395 int c1, c2;
2396#if defined(FEAT_FOLDING)
2397 pos_T save_cursor;
2398#endif
2399 win_T *old_curwin = curwin;
2400#ifdef FEAT_VISUAL
2401 static pos_T orig_cursor;
2402 colnr_T leftcol, rightcol;
2403 pos_T end_visual;
2404 int diff;
2405 int old_active = VIsual_active;
2406 int old_mode = VIsual_mode;
2407#endif
2408 int regname;
2409
2410#if defined(FEAT_FOLDING)
2411 save_cursor = curwin->w_cursor;
2412#endif
2413
2414 /*
2415 * When GUI is active, always recognize mouse events, otherwise:
2416 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2417 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2418 * - For command line and insert mode 'mouse' is checked before calling
2419 * do_mouse().
2420 */
2421 if (do_always)
2422 do_always = FALSE;
2423 else
2424#ifdef FEAT_GUI
2425 if (!gui.in_use)
2426#endif
2427 {
2428#ifdef FEAT_VISUAL
2429 if (VIsual_active)
2430 {
2431 if (!mouse_has(MOUSE_VISUAL))
2432 return FALSE;
2433 }
2434 else
2435#endif
2436 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2437 return FALSE;
2438 }
2439
2440 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2441
2442#ifdef FEAT_MOUSESHAPE
2443 /* May have stopped dragging the status or separator line. The pointer is
2444 * most likely still on the status or separator line. */
2445 if (!is_drag && drag_status_line)
2446 {
2447 drag_status_line = FALSE;
2448 update_mouseshape(SHAPE_IDX_STATUS);
2449 }
2450# ifdef FEAT_VERTSPLIT
2451 if (!is_drag && drag_sep_line)
2452 {
2453 drag_sep_line = FALSE;
2454 update_mouseshape(SHAPE_IDX_VSEP);
2455 }
2456# endif
2457#endif
2458
2459 /*
2460 * Ignore drag and release events if we didn't get a click.
2461 */
2462 if (is_click)
2463 got_click = TRUE;
2464 else
2465 {
2466 if (!got_click) /* didn't get click, ignore */
2467 return FALSE;
2468 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002471#ifdef FEAT_WINDOWS
2472 if (in_tab_line)
2473 {
2474 in_tab_line = FALSE;
2475 return FALSE;
2476 }
2477#endif
2478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
Bram Moolenaar64969662005-12-14 21:59:55 +00002481#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002483 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
2485 if ((mod_mask & MOD_MASK_ALT))
2486 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
2490 * CTRL right mouse button does CTRL-T
2491 */
2492 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2493 {
2494 if (State & INSERT)
2495 stuffcharReadbuff(Ctrl_O);
2496 if (count > 1)
2497 stuffnumReadbuff(count);
2498 stuffcharReadbuff(Ctrl_T);
2499 got_click = FALSE; /* ignore drag&release now */
2500 return FALSE;
2501 }
2502
2503 /*
2504 * CTRL only works with left mouse button
2505 */
2506 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2507 return FALSE;
2508
2509 /*
2510 * When a modifier is down, ignore drag and release events, as well as
2511 * multiple clicks and the middle mouse button.
2512 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2513 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002514 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2515 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && (!is_click
2517 || (mod_mask & MOD_MASK_MULTI_CLICK)
2518 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002519 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && mouse_model_popup()
2521 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002522 && !((mod_mask & MOD_MASK_ALT)
2523 && !mouse_model_popup()
2524 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 )
2526 return FALSE;
2527
2528 /*
2529 * If the button press was used as the movement command for an operator
2530 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2531 * drag/release events.
2532 */
2533 if (!is_click && which_button == MOUSE_MIDDLE)
2534 return FALSE;
2535
2536 if (oap != NULL)
2537 regname = oap->regname;
2538 else
2539 regname = 0;
2540
2541 /*
2542 * Middle mouse button does a 'put' of the selected text
2543 */
2544 if (which_button == MOUSE_MIDDLE)
2545 {
2546 if (State == NORMAL)
2547 {
2548 /*
2549 * If an operator was pending, we don't know what the user wanted
2550 * to do. Go back to normal mode: Clear the operator and beep().
2551 */
2552 if (oap != NULL && oap->op_type != OP_NOP)
2553 {
2554 clearopbeep(oap);
2555 return FALSE;
2556 }
2557
2558#ifdef FEAT_VISUAL
2559 /*
2560 * If visual was active, yank the highlighted text and put it
2561 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002562 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 */
2564 if (VIsual_active)
2565 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002566 if (VIsual_select)
2567 {
2568 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002569 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002570 }
2571 else
2572 {
2573 stuffcharReadbuff('y');
2574 stuffcharReadbuff(K_MIDDLEMOUSE);
2575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 do_always = TRUE; /* ignore 'mouse' setting next time */
2577 return FALSE;
2578 }
2579#endif
2580 /*
2581 * The rest is below jump_to_mouse()
2582 */
2583 }
2584
2585 else if ((State & INSERT) == 0)
2586 return FALSE;
2587
2588 /*
2589 * Middle click in insert mode doesn't move the mouse, just insert the
2590 * contents of a register. '.' register is special, can't insert that
2591 * with do_put().
2592 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2593 * happens for the GUI).
2594 */
2595 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2596 {
2597 if (regname == '.')
2598 insert_reg(regname, TRUE);
2599 else
2600 {
2601#ifdef FEAT_CLIPBOARD
2602 if (clip_star.available && regname == 0)
2603 regname = '*';
2604#endif
2605 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2606 insert_reg(regname, TRUE);
2607 else
2608 {
2609 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2610
2611 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2612 AppendCharToRedobuff(Ctrl_R);
2613 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2614 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2615 }
2616 }
2617 return FALSE;
2618 }
2619 }
2620
2621 /* When dragging or button-up stay in the same window. */
2622 if (!is_click)
2623 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2624
2625 start_visual.lnum = 0;
2626
Bram Moolenaarf740b292006-02-16 22:11:02 +00002627#ifdef FEAT_WINDOWS
2628 /* Check for clicking in the tab page line. */
2629 if (mouse_row == 0 && firstwin->w_winrow > 0)
2630 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002631 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002632 {
2633 if (in_tab_line)
2634 {
2635 c1 = TabPageIdxs[mouse_col];
2636 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2637 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002638 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002639 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002640
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 /* click in a tab selects that tab page */
2642 if (is_click
2643# ifdef FEAT_CMDWIN
2644 && cmdwin_type == 0
2645# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002648 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002649 c1 = TabPageIdxs[mouse_col];
2650 if (c1 >= 0)
2651 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002652 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2653 {
2654 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002655 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002656 tabpage_new();
2657 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2658 }
2659 else
2660 {
2661 /* Go to specified tab page, or next one if not clicking
2662 * on a label. */
2663 goto_tabpage(c1);
2664
2665 /* It's like clicking on the status line of a window. */
2666 if (curwin != old_curwin)
2667 end_visual_mode();
2668 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 }
2670 else if (c1 < 0)
2671 {
2672 tabpage_T *tp;
2673
2674 /* Close the current or specified tab page. */
2675 if (c1 == -999)
2676 tp = curtab;
2677 else
2678 tp = find_tabpage(-c1);
2679 if (tp == curtab)
2680 {
2681 if (first_tabpage->tp_next != NULL)
2682 tabpage_close(FALSE);
2683 }
2684 else if (tp != NULL)
2685 tabpage_close_other(tp, FALSE);
2686 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002687 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002688 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002689 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002690 else if (is_drag && in_tab_line)
2691 {
2692 c1 = TabPageIdxs[mouse_col];
2693 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2694 return FALSE;
2695 }
2696
Bram Moolenaarf740b292006-02-16 22:11:02 +00002697#endif
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /*
2700 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2701 * right button up -> pop-up menu
2702 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002703 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
2705 if (mouse_model_popup())
2706 {
2707 if (which_button == MOUSE_RIGHT
2708 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2709 {
2710 /*
2711 * NOTE: Ignore right button down and drag mouse events.
2712 * Windows only shows the popup menu on the button up event.
2713 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002714#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2715 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (!is_click)
2717 return FALSE;
2718#endif
2719#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2720 if (is_click || is_drag)
2721 return FALSE;
2722#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002723#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2725 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2726 if (gui.in_use)
2727 {
2728 jump_flags = 0;
2729 if (STRCMP(p_mousem, "popup_setpos") == 0)
2730 {
2731 /* First set the cursor position before showing the popup
2732 * menu. */
2733#ifdef FEAT_VISUAL
2734 if (VIsual_active)
2735 {
2736 pos_T m_pos;
2737
2738 /*
2739 * set MOUSE_MAY_STOP_VIS if we are outside the
2740 * selection or the current window (might have false
2741 * negative here)
2742 */
2743 if (mouse_row < W_WINROW(curwin)
2744 || mouse_row
2745 > (W_WINROW(curwin) + curwin->w_height))
2746 jump_flags = MOUSE_MAY_STOP_VIS;
2747 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2748 jump_flags = MOUSE_MAY_STOP_VIS;
2749 else
2750 {
2751 if ((lt(curwin->w_cursor, VIsual)
2752 && (lt(m_pos, curwin->w_cursor)
2753 || lt(VIsual, m_pos)))
2754 || (lt(VIsual, curwin->w_cursor)
2755 && (lt(m_pos, VIsual)
2756 || lt(curwin->w_cursor, m_pos))))
2757 {
2758 jump_flags = MOUSE_MAY_STOP_VIS;
2759 }
2760 else if (VIsual_mode == Ctrl_V)
2761 {
2762 getvcols(curwin, &curwin->w_cursor, &VIsual,
2763 &leftcol, &rightcol);
2764 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2765 if (m_pos.col < leftcol || m_pos.col > rightcol)
2766 jump_flags = MOUSE_MAY_STOP_VIS;
2767 }
2768 }
2769 }
2770 else
2771 jump_flags = MOUSE_MAY_STOP_VIS;
2772#endif
2773 }
2774 if (jump_flags)
2775 {
2776 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2777 update_curbuf(
2778#ifdef FEAT_VISUAL
2779 VIsual_active ? INVERTED :
2780#endif
2781 VALID);
2782 setcursor();
2783 out_flush(); /* Update before showing popup menu */
2784 }
2785# ifdef FEAT_MENU
2786 gui_show_popupmenu();
2787# endif
2788 return (jump_flags & CURSOR_MOVED) != 0;
2789 }
2790 else
2791 return FALSE;
2792#else
2793 return FALSE;
2794#endif
2795 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002796 if (which_button == MOUSE_LEFT
2797 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 which_button = MOUSE_RIGHT;
2800 mod_mask &= ~MOD_MASK_SHIFT;
2801 }
2802 }
2803
2804#ifdef FEAT_VISUAL
2805 if ((State & (NORMAL | INSERT))
2806 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2807 {
2808 if (which_button == MOUSE_LEFT)
2809 {
2810 if (is_click)
2811 {
2812 /* stop Visual mode for a left click in a window, but not when
2813 * on a status line */
2814 if (VIsual_active)
2815 jump_flags |= MOUSE_MAY_STOP_VIS;
2816 }
2817 else if (mouse_has(MOUSE_VISUAL))
2818 jump_flags |= MOUSE_MAY_VIS;
2819 }
2820 else if (which_button == MOUSE_RIGHT)
2821 {
2822 if (is_click && VIsual_active)
2823 {
2824 /*
2825 * Remember the start and end of visual before moving the
2826 * cursor.
2827 */
2828 if (lt(curwin->w_cursor, VIsual))
2829 {
2830 start_visual = curwin->w_cursor;
2831 end_visual = VIsual;
2832 }
2833 else
2834 {
2835 start_visual = VIsual;
2836 end_visual = curwin->w_cursor;
2837 }
2838 }
2839 jump_flags |= MOUSE_FOCUS;
2840 if (mouse_has(MOUSE_VISUAL))
2841 jump_flags |= MOUSE_MAY_VIS;
2842 }
2843 }
2844#endif
2845
2846 /*
2847 * If an operator is pending, ignore all drags and releases until the
2848 * next mouse click.
2849 */
2850 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2851 {
2852 got_click = FALSE;
2853 oap->motion_type = MCHAR;
2854 }
2855
2856 /* When releasing the button let jump_to_mouse() know. */
2857 if (!is_click && !is_drag)
2858 jump_flags |= MOUSE_RELEASED;
2859
2860 /*
2861 * JUMP!
2862 */
2863 jump_flags = jump_to_mouse(jump_flags,
2864 oap == NULL ? NULL : &(oap->inclusive), which_button);
2865 moved = (jump_flags & CURSOR_MOVED);
2866 in_status_line = (jump_flags & IN_STATUS_LINE);
2867#ifdef FEAT_VERTSPLIT
2868 in_sep_line = (jump_flags & IN_SEP_LINE);
2869#endif
2870
2871#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002872 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2874 {
2875 int key = KEY2TERMCAP1(c);
2876
2877 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2878 || key == (int)KE_RIGHTRELEASE)
2879 netbeans_button_release(which_button);
2880 }
2881#endif
2882
2883 /* When jumping to another window, clear a pending operator. That's a bit
2884 * friendlier than beeping and not jumping to that window. */
2885 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2886 clearop(oap);
2887
2888#ifdef FEAT_FOLDING
2889 if (mod_mask == 0
2890 && !is_drag
2891 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2892 && which_button == MOUSE_LEFT)
2893 {
2894 /* open or close a fold at this line */
2895 if (jump_flags & MOUSE_FOLD_OPEN)
2896 openFold(curwin->w_cursor.lnum, 1L);
2897 else
2898 closeFold(curwin->w_cursor.lnum, 1L);
2899 /* don't move the cursor if still in the same window */
2900 if (curwin == old_curwin)
2901 curwin->w_cursor = save_cursor;
2902 }
2903#endif
2904
2905#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2906 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2907 {
2908 clip_modeless(which_button, is_click, is_drag);
2909 return FALSE;
2910 }
2911#endif
2912
2913#ifdef FEAT_VISUAL
2914 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002915 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (VIsual_active && is_drag && p_so)
2917 {
2918 /* In the very first line, allow scrolling one line */
2919 if (mouse_row == 0)
2920 mouse_dragging = 2;
2921 else
2922 mouse_dragging = 1;
2923 }
2924
2925 /* When dragging the mouse above the window, scroll down. */
2926 if (is_drag && mouse_row < 0 && !in_status_line)
2927 {
2928 scroll_redraw(FALSE, 1L);
2929 mouse_row = 0;
2930 }
2931
2932 if (start_visual.lnum) /* right click in visual mode */
2933 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002934 /* When ALT is pressed make Visual mode blockwise. */
2935 if (mod_mask & MOD_MASK_ALT)
2936 VIsual_mode = Ctrl_V;
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 /*
2939 * In Visual-block mode, divide the area in four, pick up the corner
2940 * that is in the quarter that the cursor is in.
2941 */
2942 if (VIsual_mode == Ctrl_V)
2943 {
2944 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2945 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2946 end_visual.col = leftcol;
2947 else
2948 end_visual.col = rightcol;
2949 if (curwin->w_cursor.lnum <
2950 (start_visual.lnum + end_visual.lnum) / 2)
2951 end_visual.lnum = end_visual.lnum;
2952 else
2953 end_visual.lnum = start_visual.lnum;
2954
2955 /* move VIsual to the right column */
2956 start_visual = curwin->w_cursor; /* save the cursor pos */
2957 curwin->w_cursor = end_visual;
2958 coladvance(end_visual.col);
2959 VIsual = curwin->w_cursor;
2960 curwin->w_cursor = start_visual; /* restore the cursor */
2961 }
2962 else
2963 {
2964 /*
2965 * If the click is before the start of visual, change the start.
2966 * If the click is after the end of visual, change the end. If
2967 * the click is inside the visual, change the closest side.
2968 */
2969 if (lt(curwin->w_cursor, start_visual))
2970 VIsual = end_visual;
2971 else if (lt(end_visual, curwin->w_cursor))
2972 VIsual = start_visual;
2973 else
2974 {
2975 /* In the same line, compare column number */
2976 if (end_visual.lnum == start_visual.lnum)
2977 {
2978 if (curwin->w_cursor.col - start_visual.col >
2979 end_visual.col - curwin->w_cursor.col)
2980 VIsual = start_visual;
2981 else
2982 VIsual = end_visual;
2983 }
2984
2985 /* In different lines, compare line number */
2986 else
2987 {
2988 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2989 (end_visual.lnum - curwin->w_cursor.lnum);
2990
2991 if (diff > 0) /* closest to end */
2992 VIsual = start_visual;
2993 else if (diff < 0) /* closest to start */
2994 VIsual = end_visual;
2995 else /* in the middle line */
2996 {
2997 if (curwin->w_cursor.col <
2998 (start_visual.col + end_visual.col) / 2)
2999 VIsual = end_visual;
3000 else
3001 VIsual = start_visual;
3002 }
3003 }
3004 }
3005 }
3006 }
3007 /*
3008 * If Visual mode started in insert mode, execute "CTRL-O"
3009 */
3010 else if ((State & INSERT) && VIsual_active)
3011 stuffcharReadbuff(Ctrl_O);
3012#endif
3013
3014 /*
3015 * Middle mouse click: Put text before cursor.
3016 */
3017 if (which_button == MOUSE_MIDDLE)
3018 {
3019#ifdef FEAT_CLIPBOARD
3020 if (clip_star.available && regname == 0)
3021 regname = '*';
3022#endif
3023 if (yank_register_mline(regname))
3024 {
3025 if (mouse_past_bottom)
3026 dir = FORWARD;
3027 }
3028 else if (mouse_past_eol)
3029 dir = FORWARD;
3030
3031 if (fixindent)
3032 {
3033 c1 = (dir == BACKWARD) ? '[' : ']';
3034 c2 = 'p';
3035 }
3036 else
3037 {
3038 c1 = (dir == FORWARD) ? 'p' : 'P';
3039 c2 = NUL;
3040 }
3041 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3042
3043 /*
3044 * Remember where the paste started, so in edit() Insstart can be set
3045 * to this position
3046 */
3047 if (restart_edit != 0)
3048 where_paste_started = curwin->w_cursor;
3049 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3050 }
3051
3052#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3053 /*
3054 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3055 * error under the mouse pointer.
3056 */
3057 else if (((mod_mask & MOD_MASK_CTRL)
3058 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3059 && bt_quickfix(curbuf))
3060 {
3061 if (State & INSERT)
3062 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003063 if (curwin->w_llist_ref == NULL) /* quickfix window */
3064 stuffReadbuff((char_u *)":.cc\n");
3065 else /* location list window */
3066 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 got_click = FALSE; /* ignore drag&release now */
3068 }
3069#endif
3070
3071 /*
3072 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3073 * under the mouse pointer.
3074 */
3075 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3076 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3077 {
3078 if (State & INSERT)
3079 stuffcharReadbuff(Ctrl_O);
3080 stuffcharReadbuff(Ctrl_RSB);
3081 got_click = FALSE; /* ignore drag&release now */
3082 }
3083
3084 /*
3085 * Shift-Mouse click searches for the next occurrence of the word under
3086 * the mouse pointer
3087 */
3088 else if ((mod_mask & MOD_MASK_SHIFT))
3089 {
3090 if (State & INSERT
3091#ifdef FEAT_VISUAL
3092 || (VIsual_active && VIsual_select)
3093#endif
3094 )
3095 stuffcharReadbuff(Ctrl_O);
3096 if (which_button == MOUSE_LEFT)
3097 stuffcharReadbuff('*');
3098 else /* MOUSE_RIGHT */
3099 stuffcharReadbuff('#');
3100 }
3101
3102 /* Handle double clicks, unless on status line */
3103 else if (in_status_line)
3104 {
3105#ifdef FEAT_MOUSESHAPE
3106 if ((is_drag || is_click) && !drag_status_line)
3107 {
3108 drag_status_line = TRUE;
3109 update_mouseshape(-1);
3110 }
3111#endif
3112 }
3113#ifdef FEAT_VERTSPLIT
3114 else if (in_sep_line)
3115 {
3116# ifdef FEAT_MOUSESHAPE
3117 if ((is_drag || is_click) && !drag_sep_line)
3118 {
3119 drag_sep_line = TRUE;
3120 update_mouseshape(-1);
3121 }
3122# endif
3123 }
3124#endif
3125#ifdef FEAT_VISUAL
3126 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3127 && mouse_has(MOUSE_VISUAL))
3128 {
3129 if (is_click || !VIsual_active)
3130 {
3131 if (VIsual_active)
3132 orig_cursor = VIsual;
3133 else
3134 {
3135 check_visual_highlight();
3136 VIsual = curwin->w_cursor;
3137 orig_cursor = VIsual;
3138 VIsual_active = TRUE;
3139 VIsual_reselect = TRUE;
3140 /* start Select mode if 'selectmode' contains "mouse" */
3141 may_start_select('o');
3142 setmouse();
3143 }
3144 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003145 {
3146 /* Double click with ALT pressed makes it blockwise. */
3147 if (mod_mask & MOD_MASK_ALT)
3148 VIsual_mode = Ctrl_V;
3149 else
3150 VIsual_mode = 'v';
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3153 VIsual_mode = 'V';
3154 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3155 VIsual_mode = Ctrl_V;
3156#ifdef FEAT_CLIPBOARD
3157 /* Make sure the clipboard gets updated. Needed because start and
3158 * end may still be the same, and the selection needs to be owned */
3159 clip_star.vmode = NUL;
3160#endif
3161 }
3162 /*
3163 * A double click selects a word or a block.
3164 */
3165 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3166 {
3167 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003168 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 if (is_click)
3171 {
3172 /* If the character under the cursor (skipping white space) is
3173 * not a word character, try finding a match and select a (),
3174 * {}, [], #if/#endif, etc. block. */
3175 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003176 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 inc(&end_visual);
3178 if (oap != NULL)
3179 oap->motion_type = MCHAR;
3180 if (oap != NULL
3181 && VIsual_mode == 'v'
3182 && !vim_iswordc(gchar_pos(&end_visual))
3183 && equalpos(curwin->w_cursor, VIsual)
3184 && (pos = findmatch(oap, NUL)) != NULL)
3185 {
3186 curwin->w_cursor = *pos;
3187 if (oap->motion_type == MLINE)
3188 VIsual_mode = 'V';
3189 else if (*p_sel == 'e')
3190 {
3191 if (lt(curwin->w_cursor, VIsual))
3192 ++VIsual.col;
3193 else
3194 ++curwin->w_cursor.col;
3195 }
3196 }
3197 }
3198
3199 if (pos == NULL && (is_click || is_drag))
3200 {
3201 /* When not found a match or when dragging: extend to include
3202 * a word. */
3203 if (lt(curwin->w_cursor, orig_cursor))
3204 {
3205 find_start_of_word(&curwin->w_cursor);
3206 find_end_of_word(&VIsual);
3207 }
3208 else
3209 {
3210 find_start_of_word(&VIsual);
3211 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3212#ifdef FEAT_MBYTE
3213 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003214 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
3216 ++curwin->w_cursor.col;
3217#endif
3218 find_end_of_word(&curwin->w_cursor);
3219 }
3220 }
3221 curwin->w_set_curswant = TRUE;
3222 }
3223 if (is_click)
3224 redraw_curbuf_later(INVERTED); /* update the inversion */
3225 }
3226 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003227 {
3228 if (mod_mask & MOD_MASK_ALT)
3229 VIsual_mode = Ctrl_V;
3230 else
3231 VIsual_mode = 'v';
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003235 if ((!VIsual_active && old_active && mode_displayed)
3236 || (VIsual_active && p_smd && msg_silent == 0
3237 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 redraw_cmdline = TRUE;
3239#endif
3240
3241 return moved;
3242}
3243
3244#ifdef FEAT_VISUAL
3245/*
3246 * Move "pos" back to the start of the word it's in.
3247 */
3248 static void
3249find_start_of_word(pos)
3250 pos_T *pos;
3251{
3252 char_u *line;
3253 int cclass;
3254 int col;
3255
3256 line = ml_get(pos->lnum);
3257 cclass = get_mouse_class(line + pos->col);
3258
3259 while (pos->col > 0)
3260 {
3261 col = pos->col - 1;
3262#ifdef FEAT_MBYTE
3263 col -= (*mb_head_off)(line, line + col);
3264#endif
3265 if (get_mouse_class(line + col) != cclass)
3266 break;
3267 pos->col = col;
3268 }
3269}
3270
3271/*
3272 * Move "pos" forward to the end of the word it's in.
3273 * When 'selection' is "exclusive", the position is just after the word.
3274 */
3275 static void
3276find_end_of_word(pos)
3277 pos_T *pos;
3278{
3279 char_u *line;
3280 int cclass;
3281 int col;
3282
3283 line = ml_get(pos->lnum);
3284 if (*p_sel == 'e' && pos->col > 0)
3285 {
3286 --pos->col;
3287#ifdef FEAT_MBYTE
3288 pos->col -= (*mb_head_off)(line, line + pos->col);
3289#endif
3290 }
3291 cclass = get_mouse_class(line + pos->col);
3292 while (line[pos->col] != NUL)
3293 {
3294#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003295 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296#else
3297 col = pos->col + 1;
3298#endif
3299 if (get_mouse_class(line + col) != cclass)
3300 {
3301 if (*p_sel == 'e')
3302 pos->col = col;
3303 break;
3304 }
3305 pos->col = col;
3306 }
3307}
3308
3309/*
3310 * Get class of a character for selection: same class means same word.
3311 * 0: blank
3312 * 1: punctuation groups
3313 * 2: normal word character
3314 * >2: multi-byte word character.
3315 */
3316 static int
3317get_mouse_class(p)
3318 char_u *p;
3319{
3320 int c;
3321
3322#ifdef FEAT_MBYTE
3323 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3324 return mb_get_class(p);
3325#endif
3326
3327 c = *p;
3328 if (c == ' ' || c == '\t')
3329 return 0;
3330
3331 if (vim_iswordc(c))
3332 return 2;
3333
3334 /*
3335 * There are a few special cases where we want certain combinations of
3336 * characters to be considered as a single word. These are things like
3337 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003338 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
3340 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3341 return 1;
3342 return c;
3343}
3344#endif /* FEAT_VISUAL */
3345#endif /* FEAT_MOUSE */
3346
3347#if defined(FEAT_VISUAL) || defined(PROTO)
3348/*
3349 * Check if highlighting for visual mode is possible, give a warning message
3350 * if not.
3351 */
3352 void
3353check_visual_highlight()
3354{
3355 static int did_check = FALSE;
3356
3357 if (full_screen)
3358 {
3359 if (!did_check && hl_attr(HLF_V) == 0)
3360 MSG(_("Warning: terminal cannot highlight"));
3361 did_check = TRUE;
3362 }
3363}
3364
3365/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003366 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 * This function should ALWAYS be called to end Visual mode, except from
3368 * do_pending_operator().
3369 */
3370 void
3371end_visual_mode()
3372{
3373#ifdef FEAT_CLIPBOARD
3374 /*
3375 * If we are using the clipboard, then remember what was selected in case
3376 * we need to paste it somewhere while we still own the selection.
3377 * Only do this when the clipboard is already owned. Don't want to grab
3378 * the selection when hitting ESC.
3379 */
3380 if (clip_star.available && clip_star.owned)
3381 clip_auto_select();
3382#endif
3383
3384 VIsual_active = FALSE;
3385#ifdef FEAT_MOUSE
3386 setmouse();
3387 mouse_dragging = 0;
3388#endif
3389
3390 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003391 curbuf->b_visual.vi_mode = VIsual_mode;
3392 curbuf->b_visual.vi_start = VIsual;
3393 curbuf->b_visual.vi_end = curwin->w_cursor;
3394 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef FEAT_EVAL
3396 curbuf->b_visual_mode_eval = VIsual_mode;
3397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#ifdef FEAT_VIRTUALEDIT
3399 if (!virtual_active())
3400 curwin->w_cursor.coladd = 0;
3401#endif
3402
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003403 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 clear_cmdline = TRUE; /* unshow visual mode later */
3405#ifdef FEAT_CMDL_INFO
3406 else
3407 clear_showcmd();
3408#endif
3409
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003410 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
3414 * Reset VIsual_active and VIsual_reselect.
3415 */
3416 void
3417reset_VIsual_and_resel()
3418{
3419 if (VIsual_active)
3420 {
3421 end_visual_mode();
3422 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3423 }
3424 VIsual_reselect = FALSE;
3425}
3426
3427/*
3428 * Reset VIsual_active and VIsual_reselect if it's set.
3429 */
3430 void
3431reset_VIsual()
3432{
3433 if (VIsual_active)
3434 {
3435 end_visual_mode();
3436 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3437 VIsual_reselect = FALSE;
3438 }
3439}
3440#endif /* FEAT_VISUAL */
3441
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3444
3445/*
3446 * Check for a balloon-eval special item to include when searching for an
3447 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3448 * Returns TRUE if the character at "*ptr" should be included.
3449 * "dir" is FORWARD or BACKWARD, the direction of searching.
3450 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3451 * "bnp" points to a counter for square brackets.
3452 */
3453 static int
3454find_is_eval_item(ptr, colp, bnp, dir)
3455 char_u *ptr;
3456 int *colp;
3457 int *bnp;
3458 int dir;
3459{
3460 /* Accept everything inside []. */
3461 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3462 ++*bnp;
3463 if (*bnp > 0)
3464 {
3465 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3466 --*bnp;
3467 return TRUE;
3468 }
3469
3470 /* skip over "s.var" */
3471 if (*ptr == '.')
3472 return TRUE;
3473
3474 /* two-character item: s->var */
3475 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3476 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3477 {
3478 *colp += dir;
3479 return TRUE;
3480 }
3481 return FALSE;
3482}
3483#endif
3484
3485/*
3486 * Find the identifier under or to the right of the cursor.
3487 * "find_type" can have one of three values:
3488 * FIND_IDENT: find an identifier (keyword)
3489 * FIND_STRING: find any non-white string
3490 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 *
3493 * There are three steps:
3494 * 1. Search forward for the start of an identifier/string. Doesn't move if
3495 * already on one.
3496 * 2. Search backward for the start of this identifier/string.
3497 * This doesn't match the real Vi but I like it a little better and it
3498 * shouldn't bother anyone.
3499 * 3. Search forward to the end of this identifier/string.
3500 * When FIND_IDENT isn't defined, we backup until a blank.
3501 *
3502 * Returns the length of the string, or zero if no string is found.
3503 * If a string is found, a pointer to the string is put in "*string". This
3504 * string is not always NUL terminated.
3505 */
3506 int
3507find_ident_under_cursor(string, find_type)
3508 char_u **string;
3509 int find_type;
3510{
3511 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3512 curwin->w_cursor.col, string, find_type);
3513}
3514
3515/*
3516 * Like find_ident_under_cursor(), but for any window and any position.
3517 * However: Uses 'iskeyword' from the current window!.
3518 */
3519 int
3520find_ident_at_pos(wp, lnum, startcol, string, find_type)
3521 win_T *wp;
3522 linenr_T lnum;
3523 colnr_T startcol;
3524 char_u **string;
3525 int find_type;
3526{
3527 char_u *ptr;
3528 int col = 0; /* init to shut up GCC */
3529 int i;
3530#ifdef FEAT_MBYTE
3531 int this_class = 0;
3532 int prev_class;
3533 int prevcol;
3534#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 int bn = 0; /* bracket nesting */
3537#endif
3538
3539 /*
3540 * if i == 0: try to find an identifier
3541 * if i == 1: try to find any non-white string
3542 */
3543 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3544 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3545 {
3546 /*
3547 * 1. skip to start of identifier/string
3548 */
3549 col = startcol;
3550#ifdef FEAT_MBYTE
3551 if (has_mbyte)
3552 {
3553 while (ptr[col] != NUL)
3554 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 /* Stop at a ']' to evaluate "a[x]". */
3557 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3558 break;
3559# endif
3560 this_class = mb_get_class(ptr + col);
3561 if (this_class != 0 && (i == 1 || this_class != 1))
3562 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003563 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566 else
3567#endif
3568 while (ptr[col] != NUL
3569 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3572# endif
3573 )
3574 ++col;
3575
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 /* When starting on a ']' count it, so that we include the '['. */
3578 bn = ptr[col] == ']';
3579#endif
3580
3581 /*
3582 * 2. Back up to start of identifier/string.
3583 */
3584#ifdef FEAT_MBYTE
3585 if (has_mbyte)
3586 {
3587 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003588# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3590 this_class = mb_get_class((char_u *)"a");
3591 else
3592# endif
3593 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 {
3596 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3597 prev_class = mb_get_class(ptr + prevcol);
3598 if (this_class != prev_class
3599 && (i == 0
3600 || prev_class == 0
3601 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 && (!(find_type & FIND_EVAL)
3604 || prevcol == 0
3605 || !find_is_eval_item(ptr + prevcol, &prevcol,
3606 &bn, BACKWARD))
3607# endif
3608 )
3609 break;
3610 col = prevcol;
3611 }
3612
3613 /* If we don't want just any old string, or we've found an
3614 * identifier, stop searching. */
3615 if (this_class > 2)
3616 this_class = 2;
3617 if (!(find_type & FIND_STRING) || this_class == 2)
3618 break;
3619 }
3620 else
3621#endif
3622 {
3623 while (col > 0
3624 && ((i == 0
3625 ? vim_iswordc(ptr[col - 1])
3626 : (!vim_iswhite(ptr[col - 1])
3627 && (!(find_type & FIND_IDENT)
3628 || !vim_iswordc(ptr[col - 1]))))
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 > 1
3632 && find_is_eval_item(ptr + col - 1, &col,
3633 &bn, BACKWARD))
3634#endif
3635 ))
3636 --col;
3637
3638 /* If we don't want just any old string, or we've found an
3639 * identifier, stop searching. */
3640 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3641 break;
3642 }
3643 }
3644
3645 if (ptr[col] == NUL || (i == 0 && (
3646#ifdef FEAT_MBYTE
3647 has_mbyte ? this_class != 2 :
3648#endif
3649 !vim_iswordc(ptr[col]))))
3650 {
3651 /*
3652 * didn't find an identifier or string
3653 */
3654 if (find_type & FIND_STRING)
3655 EMSG(_("E348: No string under cursor"));
3656 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003657 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return 0;
3659 }
3660 ptr += col;
3661 *string = ptr;
3662
3663 /*
3664 * 3. Find the end if the identifier/string.
3665 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003666#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 bn = 0;
3668 startcol -= col;
3669#endif
3670 col = 0;
3671#ifdef FEAT_MBYTE
3672 if (has_mbyte)
3673 {
3674 /* Search for point of changing multibyte character class. */
3675 this_class = mb_get_class(ptr);
3676 while (ptr[col] != NUL
3677 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3678 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003679# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 || ((find_type & FIND_EVAL)
3681 && col <= (int)startcol
3682 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3683# endif
3684 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003685 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
3687 else
3688#endif
3689 while ((i == 0 ? vim_iswordc(ptr[col])
3690 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003691# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 || ((find_type & FIND_EVAL)
3693 && col <= (int)startcol
3694 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3695# endif
3696 )
3697 {
3698 ++col;
3699 }
3700
3701 return col;
3702}
3703
3704/*
3705 * Prepare for redo of a normal command.
3706 */
3707 static void
3708prep_redo_cmd(cap)
3709 cmdarg_T *cap;
3710{
3711 prep_redo(cap->oap->regname, cap->count0,
3712 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3713}
3714
3715/*
3716 * Prepare for redo of any command.
3717 * Note that only the last argument can be a multi-byte char.
3718 */
3719 static void
3720prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3721 int regname;
3722 long num;
3723 int cmd1;
3724 int cmd2;
3725 int cmd3;
3726 int cmd4;
3727 int cmd5;
3728{
3729 ResetRedobuff();
3730 if (regname != 0) /* yank from specified buffer */
3731 {
3732 AppendCharToRedobuff('"');
3733 AppendCharToRedobuff(regname);
3734 }
3735 if (num)
3736 AppendNumberToRedobuff(num);
3737
3738 if (cmd1 != NUL)
3739 AppendCharToRedobuff(cmd1);
3740 if (cmd2 != NUL)
3741 AppendCharToRedobuff(cmd2);
3742 if (cmd3 != NUL)
3743 AppendCharToRedobuff(cmd3);
3744 if (cmd4 != NUL)
3745 AppendCharToRedobuff(cmd4);
3746 if (cmd5 != NUL)
3747 AppendCharToRedobuff(cmd5);
3748}
3749
3750/*
3751 * check for operator active and clear it
3752 *
3753 * return TRUE if operator was active
3754 */
3755 static int
3756checkclearop(oap)
3757 oparg_T *oap;
3758{
3759 if (oap->op_type == OP_NOP)
3760 return FALSE;
3761 clearopbeep(oap);
3762 return TRUE;
3763}
3764
3765/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003766 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003768 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 static int
3771checkclearopq(oap)
3772 oparg_T *oap;
3773{
3774 if (oap->op_type == OP_NOP
3775#ifdef FEAT_VISUAL
3776 && !VIsual_active
3777#endif
3778 )
3779 return FALSE;
3780 clearopbeep(oap);
3781 return TRUE;
3782}
3783
3784 static void
3785clearop(oap)
3786 oparg_T *oap;
3787{
3788 oap->op_type = OP_NOP;
3789 oap->regname = 0;
3790 oap->motion_force = NUL;
3791 oap->use_reg_one = FALSE;
3792}
3793
3794 static void
3795clearopbeep(oap)
3796 oparg_T *oap;
3797{
3798 clearop(oap);
3799 beep_flush();
3800}
3801
3802#ifdef FEAT_VISUAL
3803/*
3804 * Remove the shift modifier from a special key.
3805 */
3806 static void
3807unshift_special(cap)
3808 cmdarg_T *cap;
3809{
3810 switch (cap->cmdchar)
3811 {
3812 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3813 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3814 case K_S_UP: cap->cmdchar = K_UP; break;
3815 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3816 case K_S_HOME: cap->cmdchar = K_HOME; break;
3817 case K_S_END: cap->cmdchar = K_END; break;
3818 }
3819 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3820}
3821#endif
3822
3823#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3824/*
3825 * Routines for displaying a partly typed command
3826 */
3827
3828#ifdef FEAT_VISUAL /* need room for size of Visual area */
3829# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3830#else
3831# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3832#endif
3833static char_u showcmd_buf[SHOWCMD_BUFLEN];
3834static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3835static int showcmd_is_clear = TRUE;
3836static int showcmd_visual = FALSE;
3837
3838static void display_showcmd __ARGS((void));
3839
3840 void
3841clear_showcmd()
3842{
3843 if (!p_sc)
3844 return;
3845
3846#ifdef FEAT_VISUAL
3847 if (VIsual_active && !char_avail())
3848 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003849 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 long lines;
3851 colnr_T leftcol, rightcol;
3852 linenr_T top, bot;
3853
3854 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003855 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
3857 top = VIsual.lnum;
3858 bot = curwin->w_cursor.lnum;
3859 }
3860 else
3861 {
3862 top = curwin->w_cursor.lnum;
3863 bot = VIsual.lnum;
3864 }
3865# ifdef FEAT_FOLDING
3866 /* Include closed folds as a whole. */
3867 hasFolding(top, &top, NULL);
3868 hasFolding(bot, NULL, &bot);
3869# endif
3870 lines = bot - top + 1;
3871
3872 if (VIsual_mode == Ctrl_V)
3873 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003874# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003875 char_u *saved_sbr = p_sbr;
3876
3877 /* Make 'sbr' empty for a moment to get the correct size. */
3878 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003881# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003882 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3885 (long)(rightcol - leftcol + 1));
3886 }
3887 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3888 sprintf((char *)showcmd_buf, "%ld", lines);
3889 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003890 {
3891 char_u *s, *e;
3892 int l;
3893 int bytes = 0;
3894 int chars = 0;
3895
3896 if (cursor_bot)
3897 {
3898 s = ml_get_pos(&VIsual);
3899 e = ml_get_cursor();
3900 }
3901 else
3902 {
3903 s = ml_get_cursor();
3904 e = ml_get_pos(&VIsual);
3905 }
3906 while ((*p_sel != 'e') ? s <= e : s < e)
3907 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003908# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003909 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003910# else
3911 l = (*s == NUL) ? 0 : 1;
3912# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003913 if (l == 0)
3914 {
3915 ++bytes;
3916 ++chars;
3917 break; /* end of line */
3918 }
3919 bytes += l;
3920 ++chars;
3921 s += l;
3922 }
3923 if (bytes == chars)
3924 sprintf((char *)showcmd_buf, "%d", chars);
3925 else
3926 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3929 showcmd_visual = TRUE;
3930 }
3931 else
3932#endif
3933 {
3934 showcmd_buf[0] = NUL;
3935 showcmd_visual = FALSE;
3936
3937 /* Don't actually display something if there is nothing to clear. */
3938 if (showcmd_is_clear)
3939 return;
3940 }
3941
3942 display_showcmd();
3943}
3944
3945/*
3946 * Add 'c' to string of shown command chars.
3947 * Return TRUE if output has been written (and setcursor() has been called).
3948 */
3949 int
3950add_to_showcmd(c)
3951 int c;
3952{
3953 char_u *p;
3954 int old_len;
3955 int extra_len;
3956 int overflow;
3957#if defined(FEAT_MOUSE)
3958 int i;
3959 static int ignore[] =
3960 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003961# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3963 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 K_IGNORE,
3966 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3967 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3968 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003969 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003971 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 0
3973 };
3974#endif
3975
Bram Moolenaar09df3122006-01-23 22:23:09 +00003976 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 return FALSE;
3978
3979 if (showcmd_visual)
3980 {
3981 showcmd_buf[0] = NUL;
3982 showcmd_visual = FALSE;
3983 }
3984
3985#if defined(FEAT_MOUSE)
3986 /* Ignore keys that are scrollbar updates and mouse clicks */
3987 if (IS_SPECIAL(c))
3988 for (i = 0; ignore[i] != 0; ++i)
3989 if (ignore[i] == c)
3990 return FALSE;
3991#endif
3992
3993 p = transchar(c);
3994 old_len = (int)STRLEN(showcmd_buf);
3995 extra_len = (int)STRLEN(p);
3996 overflow = old_len + extra_len - SHOWCMD_COLS;
3997 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003998 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3999 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 STRCAT(showcmd_buf, p);
4001
4002 if (char_avail())
4003 return FALSE;
4004
4005 display_showcmd();
4006
4007 return TRUE;
4008}
4009
4010 void
4011add_to_showcmd_c(c)
4012 int c;
4013{
4014 if (!add_to_showcmd(c))
4015 setcursor();
4016}
4017
4018/*
4019 * Delete 'len' characters from the end of the shown command.
4020 */
4021 static void
4022del_from_showcmd(len)
4023 int len;
4024{
4025 int old_len;
4026
4027 if (!p_sc)
4028 return;
4029
4030 old_len = (int)STRLEN(showcmd_buf);
4031 if (len > old_len)
4032 len = old_len;
4033 showcmd_buf[old_len - len] = NUL;
4034
4035 if (!char_avail())
4036 display_showcmd();
4037}
4038
4039/*
4040 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4041 * something and there is a partial mapping.
4042 */
4043 void
4044push_showcmd()
4045{
4046 if (p_sc)
4047 STRCPY(old_showcmd_buf, showcmd_buf);
4048}
4049
4050 void
4051pop_showcmd()
4052{
4053 if (!p_sc)
4054 return;
4055
4056 STRCPY(showcmd_buf, old_showcmd_buf);
4057
4058 display_showcmd();
4059}
4060
4061 static void
4062display_showcmd()
4063{
4064 int len;
4065
4066 cursor_off();
4067
4068 len = (int)STRLEN(showcmd_buf);
4069 if (len == 0)
4070 showcmd_is_clear = TRUE;
4071 else
4072 {
4073 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4074 showcmd_is_clear = FALSE;
4075 }
4076
4077 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004078 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4079 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 */
4081 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4082
4083 setcursor(); /* put cursor back where it belongs */
4084}
4085#endif
4086
4087#ifdef FEAT_SCROLLBIND
4088/*
4089 * When "check" is FALSE, prepare for commands that scroll the window.
4090 * When "check" is TRUE, take care of scroll-binding after the window has
4091 * scrolled. Called from normal_cmd() and edit().
4092 */
4093 void
4094do_check_scrollbind(check)
4095 int check;
4096{
4097 static win_T *old_curwin = NULL;
4098 static linenr_T old_topline = 0;
4099#ifdef FEAT_DIFF
4100 static int old_topfill = 0;
4101#endif
4102 static buf_T *old_buf = NULL;
4103 static colnr_T old_leftcol = 0;
4104
4105 if (check && curwin->w_p_scb)
4106 {
4107 /* If a ":syncbind" command was just used, don't scroll, only reset
4108 * the values. */
4109 if (did_syncbind)
4110 did_syncbind = FALSE;
4111 else if (curwin == old_curwin)
4112 {
4113 /*
4114 * Synchronize other windows, as necessary according to
4115 * 'scrollbind'. Don't do this after an ":edit" command, except
4116 * when 'diff' is set.
4117 */
4118 if ((curwin->w_buffer == old_buf
4119#ifdef FEAT_DIFF
4120 || curwin->w_p_diff
4121#endif
4122 )
4123 && (curwin->w_topline != old_topline
4124#ifdef FEAT_DIFF
4125 || curwin->w_topfill != old_topfill
4126#endif
4127 || curwin->w_leftcol != old_leftcol))
4128 {
4129 check_scrollbind(curwin->w_topline - old_topline,
4130 (long)(curwin->w_leftcol - old_leftcol));
4131 }
4132 }
4133 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4134 {
4135 /*
4136 * When switching between windows, make sure that the relative
4137 * vertical offset is valid for the new window. The relative
4138 * offset is invalid whenever another 'scrollbind' window has
4139 * scrolled to a point that would force the current window to
4140 * scroll past the beginning or end of its buffer. When the
4141 * resync is performed, some of the other 'scrollbind' windows may
4142 * need to jump so that the current window's relative position is
4143 * visible on-screen.
4144 */
4145 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4146 }
4147 curwin->w_scbind_pos = curwin->w_topline;
4148 }
4149
4150 old_curwin = curwin;
4151 old_topline = curwin->w_topline;
4152#ifdef FEAT_DIFF
4153 old_topfill = curwin->w_topfill;
4154#endif
4155 old_buf = curwin->w_buffer;
4156 old_leftcol = curwin->w_leftcol;
4157}
4158
4159/*
4160 * Synchronize any windows that have "scrollbind" set, based on the
4161 * number of rows by which the current window has changed
4162 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4163 */
4164 void
4165check_scrollbind(topline_diff, leftcol_diff)
4166 linenr_T topline_diff;
4167 long leftcol_diff;
4168{
4169 int want_ver;
4170 int want_hor;
4171 win_T *old_curwin = curwin;
4172 buf_T *old_curbuf = curbuf;
4173#ifdef FEAT_VISUAL
4174 int old_VIsual_select = VIsual_select;
4175 int old_VIsual_active = VIsual_active;
4176#endif
4177 colnr_T tgt_leftcol = curwin->w_leftcol;
4178 long topline;
4179 long y;
4180
4181 /*
4182 * check 'scrollopt' string for vertical and horizontal scroll options
4183 */
4184 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4185#ifdef FEAT_DIFF
4186 want_ver |= old_curwin->w_p_diff;
4187#endif
4188 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4189
4190 /*
4191 * loop through the scrollbound windows and scroll accordingly
4192 */
4193#ifdef FEAT_VISUAL
4194 VIsual_select = VIsual_active = 0;
4195#endif
4196 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4197 {
4198 curbuf = curwin->w_buffer;
4199 /* skip original window and windows with 'noscrollbind' */
4200 if (curwin != old_curwin && curwin->w_p_scb)
4201 {
4202 /*
4203 * do the vertical scroll
4204 */
4205 if (want_ver)
4206 {
4207#ifdef FEAT_DIFF
4208 if (old_curwin->w_p_diff && curwin->w_p_diff)
4209 {
4210 diff_set_topline(old_curwin, curwin);
4211 }
4212 else
4213#endif
4214 {
4215 curwin->w_scbind_pos += topline_diff;
4216 topline = curwin->w_scbind_pos;
4217 if (topline > curbuf->b_ml.ml_line_count)
4218 topline = curbuf->b_ml.ml_line_count;
4219 if (topline < 1)
4220 topline = 1;
4221
4222 y = topline - curwin->w_topline;
4223 if (y > 0)
4224 scrollup(y, FALSE);
4225 else
4226 scrolldown(-y, FALSE);
4227 }
4228
4229 redraw_later(VALID);
4230 cursor_correct();
4231#ifdef FEAT_WINDOWS
4232 curwin->w_redr_status = TRUE;
4233#endif
4234 }
4235
4236 /*
4237 * do the horizontal scroll
4238 */
4239 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4240 {
4241 curwin->w_leftcol = tgt_leftcol;
4242 leftcol_changed();
4243 }
4244 }
4245 }
4246
4247 /*
4248 * reset current-window
4249 */
4250#ifdef FEAT_VISUAL
4251 VIsual_select = old_VIsual_select;
4252 VIsual_active = old_VIsual_active;
4253#endif
4254 curwin = old_curwin;
4255 curbuf = old_curbuf;
4256}
4257#endif /* #ifdef FEAT_SCROLLBIND */
4258
4259/*
4260 * Command character that's ignored.
4261 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004262 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 static void
4265nv_ignore(cap)
4266 cmdarg_T *cap;
4267{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004268 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269}
4270
4271/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004272 * Command character that doesn't do anything, but unlike nv_ignore() does
4273 * start edit(). Used for "startinsert" executed while starting up.
4274 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004275 static void
4276nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004277 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004278{
4279}
4280
4281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 * Command character doesn't exist.
4283 */
4284 static void
4285nv_error(cap)
4286 cmdarg_T *cap;
4287{
4288 clearopbeep(cap->oap);
4289}
4290
4291/*
4292 * <Help> and <F1> commands.
4293 */
4294 static void
4295nv_help(cap)
4296 cmdarg_T *cap;
4297{
4298 if (!checkclearopq(cap->oap))
4299 ex_help(NULL);
4300}
4301
4302/*
4303 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4304 */
4305 static void
4306nv_addsub(cap)
4307 cmdarg_T *cap;
4308{
4309 if (!checkclearopq(cap->oap)
4310 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4311 prep_redo_cmd(cap);
4312}
4313
4314/*
4315 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4316 */
4317 static void
4318nv_page(cap)
4319 cmdarg_T *cap;
4320{
4321 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004322 {
4323#ifdef FEAT_WINDOWS
4324 if (mod_mask & MOD_MASK_CTRL)
4325 {
4326 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4327 if (cap->arg == BACKWARD)
4328 goto_tabpage(-(int)cap->count1);
4329 else
4330 goto_tabpage((int)cap->count0);
4331 }
4332 else
4333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336}
4337
4338/*
4339 * Implementation of "gd" and "gD" command.
4340 */
4341 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004342nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004343 oparg_T *oap;
4344 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004345 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
4347 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 char_u *ptr;
4349
Bram Moolenaard9d30582005-05-18 22:10:28 +00004350 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004351 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004353#ifdef FEAT_FOLDING
4354 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4355 foldOpenCursor();
4356#endif
4357}
4358
4359/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 * Search for variable declaration of "ptr[len]".
4361 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4362 * current file ("gD").
4363 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004364 * Return FAIL when not found.
4365 */
4366 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004367find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004368 char_u *ptr;
4369 int len;
4370 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004371 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 int searchflags; /* flags passed to searchit() */
4373{
4374 char_u *pat;
4375 pos_T old_pos;
4376 pos_T par_pos;
4377 pos_T found_pos;
4378 int t;
4379 int save_p_ws;
4380 int save_p_scs;
4381 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004382 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004383
4384 if ((pat = alloc(len + 7)) == NULL)
4385 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004386
4387 /* Put "\V" before the pattern to avoid that the special meaning of "."
4388 * and "~" causes trouble. */
4389 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4390 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 old_pos = curwin->w_cursor;
4392 save_p_ws = p_ws;
4393 save_p_scs = p_scs;
4394 p_ws = FALSE; /* don't wrap around end of file now */
4395 p_scs = FALSE; /* don't switch ignorecase off now */
4396
4397 /*
4398 * With "gD" go to line 1.
4399 * With "gd" Search back for the start of the current function, then go
4400 * back until a blank line. If this fails go to line 1.
4401 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004402 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
4404 setpcmark(); /* Set in findpar() otherwise */
4405 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004406 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
4408 else
4409 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004410 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4412 --curwin->w_cursor.lnum;
4413 }
4414 curwin->w_cursor.col = 0;
4415
4416 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004417 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004418 for (;;)
4419 {
4420 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004421 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004422 if (curwin->w_cursor.lnum >= old_pos.lnum)
4423 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004424
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004425 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004426 {
4427 pos_T *pos;
4428
4429 /* Check that the block the match is in doesn't end before the
4430 * position where we started the search from. */
4431 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4432 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4433 && pos->lnum < old_pos.lnum)
4434 continue;
4435 }
4436
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437 if (t == FAIL)
4438 {
4439 /* If we previously found a valid position, use it. */
4440 if (found_pos.lnum != 0)
4441 {
4442 curwin->w_cursor = found_pos;
4443 t = OK;
4444 }
4445 break;
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004448 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004449 {
4450 /* Ignore this line, continue at start of next line. */
4451 ++curwin->w_cursor.lnum;
4452 curwin->w_cursor.col = 0;
4453 continue;
4454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004456 if (!locally) /* global search: use first match found */
4457 break;
4458 if (curwin->w_cursor.lnum >= par_pos.lnum)
4459 {
4460 /* If we previously found a valid position, use it. */
4461 if (found_pos.lnum != 0)
4462 curwin->w_cursor = found_pos;
4463 break;
4464 }
4465
4466 /* For finding a local variable and the match is before the "{" search
4467 * to find a later match. For K&R style function declarations this
4468 * skips the function header without types. */
4469 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004471
4472 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004474 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 curwin->w_cursor = old_pos;
4476 }
4477 else
4478 {
4479 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /* "n" searches forward now */
4481 reset_search_dir();
4482 }
4483
4484 vim_free(pat);
4485 p_ws = save_p_ws;
4486 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004487
4488 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489}
4490
4491/*
4492 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4493 * lines rather than lines in the file.
4494 * 'dist' must be positive.
4495 *
4496 * Return OK if able to move cursor, FAIL otherwise.
4497 */
4498 static int
4499nv_screengo(oap, dir, dist)
4500 oparg_T *oap;
4501 int dir;
4502 long dist;
4503{
4504 int linelen = linetabsize(ml_get_curline());
4505 int retval = OK;
4506 int atend = FALSE;
4507 int n;
4508 int col_off1; /* margin offset for first screen line */
4509 int col_off2; /* margin offset for wrapped screen line */
4510 int width1; /* text width for first screen line */
4511 int width2; /* test width for wrapped screen line */
4512
4513 oap->motion_type = MCHAR;
4514 oap->inclusive = FALSE;
4515
4516 col_off1 = curwin_col_off();
4517 col_off2 = col_off1 - curwin_col_off2();
4518 width1 = W_WIDTH(curwin) - col_off1;
4519 width2 = W_WIDTH(curwin) - col_off2;
4520
4521#ifdef FEAT_VERTSPLIT
4522 if (curwin->w_width != 0)
4523 {
4524#endif
4525 /*
4526 * Instead of sticking at the last character of the buffer line we
4527 * try to stick in the last column of the screen.
4528 */
4529 if (curwin->w_curswant == MAXCOL)
4530 {
4531 atend = TRUE;
4532 validate_virtcol();
4533 if (width1 <= 0)
4534 curwin->w_curswant = 0;
4535 else
4536 {
4537 curwin->w_curswant = width1 - 1;
4538 if (curwin->w_virtcol > curwin->w_curswant)
4539 curwin->w_curswant += ((curwin->w_virtcol
4540 - curwin->w_curswant - 1) / width2 + 1) * width2;
4541 }
4542 }
4543 else
4544 {
4545 if (linelen > width1)
4546 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4547 else
4548 n = width1;
4549 if (curwin->w_curswant > (colnr_T)n + 1)
4550 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4551 * width2;
4552 }
4553
4554 while (dist--)
4555 {
4556 if (dir == BACKWARD)
4557 {
4558 if ((long)curwin->w_curswant >= width2)
4559 /* move back within line */
4560 curwin->w_curswant -= width2;
4561 else
4562 {
4563 /* to previous line */
4564 if (curwin->w_cursor.lnum == 1)
4565 {
4566 retval = FAIL;
4567 break;
4568 }
4569 --curwin->w_cursor.lnum;
4570#ifdef FEAT_FOLDING
4571 /* Move to the start of a closed fold. Don't do that when
4572 * 'foldopen' contains "all": it will open in a moment. */
4573 if (!(fdo_flags & FDO_ALL))
4574 (void)hasFolding(curwin->w_cursor.lnum,
4575 &curwin->w_cursor.lnum, NULL);
4576#endif
4577 linelen = linetabsize(ml_get_curline());
4578 if (linelen > width1)
4579 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4580 + 1) * width2;
4581 }
4582 }
4583 else /* dir == FORWARD */
4584 {
4585 if (linelen > width1)
4586 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4587 else
4588 n = width1;
4589 if (curwin->w_curswant + width2 < (colnr_T)n)
4590 /* move forward within line */
4591 curwin->w_curswant += width2;
4592 else
4593 {
4594 /* to next line */
4595#ifdef FEAT_FOLDING
4596 /* Move to the end of a closed fold. */
4597 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4598 &curwin->w_cursor.lnum);
4599#endif
4600 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4601 {
4602 retval = FAIL;
4603 break;
4604 }
4605 curwin->w_cursor.lnum++;
4606 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004607 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 }
4610 }
4611#ifdef FEAT_VERTSPLIT
4612 }
4613#endif
4614
4615 coladvance(curwin->w_curswant);
4616
4617#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4618 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4619 {
4620 /*
4621 * Check for landing on a character that got split at the end of the
4622 * last line. We want to advance a screenline, not end up in the same
4623 * screenline or move two screenlines.
4624 */
4625 validate_virtcol();
4626 if (curwin->w_virtcol > curwin->w_curswant
4627 && (curwin->w_curswant < (colnr_T)width1
4628 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4629 : ((curwin->w_curswant - width1) % width2
4630 > (colnr_T)width2 / 2)))
4631 --curwin->w_cursor.col;
4632 }
4633#endif
4634
4635 if (atend)
4636 curwin->w_curswant = MAXCOL; /* stick in the last column */
4637
4638 return retval;
4639}
4640
4641#ifdef FEAT_MOUSE
4642/*
4643 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4644 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004645 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4646 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 */
4648 static void
4649nv_mousescroll(cap)
4650 cmdarg_T *cap;
4651{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004652# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004653 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004655 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
4657 int row, col;
4658
4659 row = mouse_row;
4660 col = mouse_col;
4661
4662 /* find the window at the pointer coordinates */
4663 curwin = mouse_find_win(&row, &col);
4664 curbuf = curwin->w_buffer;
4665 }
4666# endif
4667
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004668 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004670 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4671 {
4672 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4673 }
4674 else
4675 {
4676 cap->count1 = 3;
4677 cap->count0 = 3;
4678 nv_scroll_line(cap);
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004681# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 else
4683 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004684 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4685 if (!curwin->w_p_wrap)
4686 {
4687 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004688
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004689 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4690 step = W_WIDTH(curwin);
4691 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4692 if (val < 0)
4693 val = 0;
4694
4695 gui_do_horiz_scroll(val, TRUE);
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004700# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 curwin->w_redr_status = TRUE;
4702
4703 curwin = old_curwin;
4704 curbuf = curwin->w_buffer;
4705# endif
4706}
4707
4708/*
4709 * Mouse clicks and drags.
4710 */
4711 static void
4712nv_mouse(cap)
4713 cmdarg_T *cap;
4714{
4715 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4716}
4717#endif
4718
4719/*
4720 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4721 * cap->arg must be TRUE for CTRL-E.
4722 */
4723 static void
4724nv_scroll_line(cap)
4725 cmdarg_T *cap;
4726{
4727 if (!checkclearop(cap->oap))
4728 scroll_redraw(cap->arg, cap->count1);
4729}
4730
4731/*
4732 * Scroll "count" lines up or down, and redraw.
4733 */
4734 void
4735scroll_redraw(up, count)
4736 int up;
4737 long count;
4738{
4739 linenr_T prev_topline = curwin->w_topline;
4740#ifdef FEAT_DIFF
4741 int prev_topfill = curwin->w_topfill;
4742#endif
4743 linenr_T prev_lnum = curwin->w_cursor.lnum;
4744
4745 if (up)
4746 scrollup(count, TRUE);
4747 else
4748 scrolldown(count, TRUE);
4749 if (p_so)
4750 {
4751 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4752 * valid, otherwise the screen jumps back at the end of the file. */
4753 cursor_correct();
4754 check_cursor_moved(curwin);
4755 curwin->w_valid |= VALID_TOPLINE;
4756
4757 /* If moved back to where we were, at least move the cursor, otherwise
4758 * we get stuck at one position. Don't move the cursor up if the
4759 * first line of the buffer is already on the screen */
4760 while (curwin->w_topline == prev_topline
4761#ifdef FEAT_DIFF
4762 && curwin->w_topfill == prev_topfill
4763#endif
4764 )
4765 {
4766 if (up)
4767 {
4768 if (curwin->w_cursor.lnum > prev_lnum
4769 || cursor_down(1L, FALSE) == FAIL)
4770 break;
4771 }
4772 else
4773 {
4774 if (curwin->w_cursor.lnum < prev_lnum
4775 || prev_topline == 1L
4776 || cursor_up(1L, FALSE) == FAIL)
4777 break;
4778 }
4779 /* Mark w_topline as valid, otherwise the screen jumps back at the
4780 * end of the file. */
4781 check_cursor_moved(curwin);
4782 curwin->w_valid |= VALID_TOPLINE;
4783 }
4784 }
4785 if (curwin->w_cursor.lnum != prev_lnum)
4786 coladvance(curwin->w_curswant);
4787 redraw_later(VALID);
4788}
4789
4790/*
4791 * Commands that start with "z".
4792 */
4793 static void
4794nv_zet(cap)
4795 cmdarg_T *cap;
4796{
4797 long n;
4798 colnr_T col;
4799 int nchar = cap->nchar;
4800#ifdef FEAT_FOLDING
4801 long old_fdl = curwin->w_p_fdl;
4802 int old_fen = curwin->w_p_fen;
4803#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004804#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004805 int undo = FALSE;
4806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 if (VIM_ISDIGIT(nchar))
4809 {
4810 /*
4811 * "z123{nchar}": edit the count before obtaining {nchar}
4812 */
4813 if (checkclearop(cap->oap))
4814 return;
4815 n = nchar - '0';
4816 for (;;)
4817 {
4818#ifdef USE_ON_FLY_SCROLL
4819 dont_scroll = TRUE; /* disallow scrolling here */
4820#endif
4821 ++no_mapping;
4822 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004823 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 --no_mapping;
4826 --allow_keys;
4827#ifdef FEAT_CMDL_INFO
4828 (void)add_to_showcmd(nchar);
4829#endif
4830 if (nchar == K_DEL || nchar == K_KDEL)
4831 n /= 10;
4832 else if (VIM_ISDIGIT(nchar))
4833 n = n * 10 + (nchar - '0');
4834 else if (nchar == CAR)
4835 {
4836#ifdef FEAT_GUI
4837 need_mouse_correct = TRUE;
4838#endif
4839 win_setheight((int)n);
4840 break;
4841 }
4842 else if (nchar == 'l'
4843 || nchar == 'h'
4844 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004845 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
4847 cap->count1 = n ? n * cap->count1 : cap->count1;
4848 goto dozet;
4849 }
4850 else
4851 {
4852 clearopbeep(cap->oap);
4853 break;
4854 }
4855 }
4856 cap->oap->op_type = OP_NOP;
4857 return;
4858 }
4859
4860dozet:
4861 if (
4862#ifdef FEAT_FOLDING
4863 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4864 * and "zC" only in Visual mode. "zj" and "zk" are motion
4865 * commands. */
4866 cap->nchar != 'f' && cap->nchar != 'F'
4867 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4868 && cap->nchar != 'j' && cap->nchar != 'k'
4869 &&
4870#endif
4871 checkclearop(cap->oap))
4872 return;
4873
4874 /*
4875 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4876 * If line number given, set cursor.
4877 */
4878 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4879 && cap->count0
4880 && cap->count0 != curwin->w_cursor.lnum)
4881 {
4882 setpcmark();
4883 if (cap->count0 > curbuf->b_ml.ml_line_count)
4884 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4885 else
4886 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004887 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889
4890 switch (nchar)
4891 {
4892 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4893 case '+':
4894 if (cap->count0 == 0)
4895 {
4896 /* No count given: put cursor at the line below screen */
4897 validate_botline(); /* make sure w_botline is valid */
4898 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4899 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4900 else
4901 curwin->w_cursor.lnum = curwin->w_botline;
4902 }
4903 /* FALLTHROUGH */
4904 case NL:
4905 case CAR:
4906 case K_KENTER:
4907 beginline(BL_WHITE | BL_FIX);
4908 /* FALLTHROUGH */
4909
4910 case 't': scroll_cursor_top(0, TRUE);
4911 redraw_later(VALID);
4912 break;
4913
4914 /* "z." and "zz": put cursor in middle of screen */
4915 case '.': beginline(BL_WHITE | BL_FIX);
4916 /* FALLTHROUGH */
4917
4918 case 'z': scroll_cursor_halfway(TRUE);
4919 redraw_later(VALID);
4920 break;
4921
4922 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4923 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4924 * when <count> is at bottom of window, and puts that one at
4925 * bottom of window. */
4926 if (cap->count0 != 0)
4927 {
4928 scroll_cursor_bot(0, TRUE);
4929 curwin->w_cursor.lnum = curwin->w_topline;
4930 }
4931 else if (curwin->w_topline == 1)
4932 curwin->w_cursor.lnum = 1;
4933 else
4934 curwin->w_cursor.lnum = curwin->w_topline - 1;
4935 /* FALLTHROUGH */
4936 case '-':
4937 beginline(BL_WHITE | BL_FIX);
4938 /* FALLTHROUGH */
4939
4940 case 'b': scroll_cursor_bot(0, TRUE);
4941 redraw_later(VALID);
4942 break;
4943
4944 /* "zH" - scroll screen right half-page */
4945 case 'H':
4946 cap->count1 *= W_WIDTH(curwin) / 2;
4947 /* FALLTHROUGH */
4948
4949 /* "zh" - scroll screen to the right */
4950 case 'h':
4951 case K_LEFT:
4952 if (!curwin->w_p_wrap)
4953 {
4954 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4955 curwin->w_leftcol = 0;
4956 else
4957 curwin->w_leftcol -= (colnr_T)cap->count1;
4958 leftcol_changed();
4959 }
4960 break;
4961
4962 /* "zL" - scroll screen left half-page */
4963 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4964 /* FALLTHROUGH */
4965
4966 /* "zl" - scroll screen to the left */
4967 case 'l':
4968 case K_RIGHT:
4969 if (!curwin->w_p_wrap)
4970 {
4971 /* scroll the window left */
4972 curwin->w_leftcol += (colnr_T)cap->count1;
4973 leftcol_changed();
4974 }
4975 break;
4976
4977 /* "zs" - scroll screen, cursor at the start */
4978 case 's': if (!curwin->w_p_wrap)
4979 {
4980#ifdef FEAT_FOLDING
4981 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4982 col = 0; /* like the cursor is in col 0 */
4983 else
4984#endif
4985 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4986 if ((long)col > p_siso)
4987 col -= p_siso;
4988 else
4989 col = 0;
4990 if (curwin->w_leftcol != col)
4991 {
4992 curwin->w_leftcol = col;
4993 redraw_later(NOT_VALID);
4994 }
4995 }
4996 break;
4997
4998 /* "ze" - scroll screen, cursor at the end */
4999 case 'e': if (!curwin->w_p_wrap)
5000 {
5001#ifdef FEAT_FOLDING
5002 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5003 col = 0; /* like the cursor is in col 0 */
5004 else
5005#endif
5006 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5007 n = W_WIDTH(curwin) - curwin_col_off();
5008 if ((long)col + p_siso < n)
5009 col = 0;
5010 else
5011 col = col + p_siso - n + 1;
5012 if (curwin->w_leftcol != col)
5013 {
5014 curwin->w_leftcol = col;
5015 redraw_later(NOT_VALID);
5016 }
5017 }
5018 break;
5019
5020#ifdef FEAT_FOLDING
5021 /* "zF": create fold command */
5022 /* "zf": create fold operator */
5023 case 'F':
5024 case 'f': if (foldManualAllowed(TRUE))
5025 {
5026 cap->nchar = 'f';
5027 nv_operator(cap);
5028 curwin->w_p_fen = TRUE;
5029
5030 /* "zF" is like "zfzf" */
5031 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5032 {
5033 nv_operator(cap);
5034 finish_op = TRUE;
5035 }
5036 }
5037 else
5038 clearopbeep(cap->oap);
5039 break;
5040
5041 /* "zd": delete fold at cursor */
5042 /* "zD": delete fold at cursor recursively */
5043 case 'd':
5044 case 'D': if (foldManualAllowed(FALSE))
5045 {
5046 if (VIsual_active)
5047 nv_operator(cap);
5048 else
5049 deleteFold(curwin->w_cursor.lnum,
5050 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5051 }
5052 break;
5053
5054 /* "zE": erease all folds */
5055 case 'E': if (foldmethodIsManual(curwin))
5056 {
5057 clearFolding(curwin);
5058 changed_window_setting();
5059 }
5060 else if (foldmethodIsMarker(curwin))
5061 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5062 TRUE, FALSE);
5063 else
5064 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5065 break;
5066
5067 /* "zn": fold none: reset 'foldenable' */
5068 case 'n': curwin->w_p_fen = FALSE;
5069 break;
5070
5071 /* "zN": fold Normal: set 'foldenable' */
5072 case 'N': curwin->w_p_fen = TRUE;
5073 break;
5074
5075 /* "zi": invert folding: toggle 'foldenable' */
5076 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5077 break;
5078
5079 /* "za": open closed fold or close open fold at cursor */
5080 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5081 openFold(curwin->w_cursor.lnum, cap->count1);
5082 else
5083 {
5084 closeFold(curwin->w_cursor.lnum, cap->count1);
5085 curwin->w_p_fen = TRUE;
5086 }
5087 break;
5088
5089 /* "zA": open fold at cursor recursively */
5090 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5091 openFoldRecurse(curwin->w_cursor.lnum);
5092 else
5093 {
5094 closeFoldRecurse(curwin->w_cursor.lnum);
5095 curwin->w_p_fen = TRUE;
5096 }
5097 break;
5098
5099 /* "zo": open fold at cursor or Visual area */
5100 case 'o': if (VIsual_active)
5101 nv_operator(cap);
5102 else
5103 openFold(curwin->w_cursor.lnum, cap->count1);
5104 break;
5105
5106 /* "zO": open fold recursively */
5107 case 'O': if (VIsual_active)
5108 nv_operator(cap);
5109 else
5110 openFoldRecurse(curwin->w_cursor.lnum);
5111 break;
5112
5113 /* "zc": close fold at cursor or Visual area */
5114 case 'c': if (VIsual_active)
5115 nv_operator(cap);
5116 else
5117 closeFold(curwin->w_cursor.lnum, cap->count1);
5118 curwin->w_p_fen = TRUE;
5119 break;
5120
5121 /* "zC": close fold recursively */
5122 case 'C': if (VIsual_active)
5123 nv_operator(cap);
5124 else
5125 closeFoldRecurse(curwin->w_cursor.lnum);
5126 curwin->w_p_fen = TRUE;
5127 break;
5128
5129 /* "zv": open folds at the cursor */
5130 case 'v': foldOpenCursor();
5131 break;
5132
5133 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5134 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005135 curwin->w_foldinvalid = TRUE; /* recompute folds */
5136 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 foldOpenCursor();
5138 break;
5139
5140 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5141 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005142 curwin->w_foldinvalid = TRUE; /* recompute folds */
5143 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 break;
5145
5146 /* "zm": fold more */
5147 case 'm': if (curwin->w_p_fdl > 0)
5148 --curwin->w_p_fdl;
5149 old_fdl = -1; /* force an update */
5150 curwin->w_p_fen = TRUE;
5151 break;
5152
5153 /* "zM": close all folds */
5154 case 'M': curwin->w_p_fdl = 0;
5155 old_fdl = -1; /* force an update */
5156 curwin->w_p_fen = TRUE;
5157 break;
5158
5159 /* "zr": reduce folding */
5160 case 'r': ++curwin->w_p_fdl;
5161 break;
5162
5163 /* "zR": open all folds */
5164 case 'R': curwin->w_p_fdl = getDeepestNesting();
5165 old_fdl = -1; /* force an update */
5166 break;
5167
5168 case 'j': /* "zj" move to next fold downwards */
5169 case 'k': /* "zk" move to next fold upwards */
5170 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5171 cap->count1) == FAIL)
5172 clearopbeep(cap->oap);
5173 break;
5174
5175#endif /* FEAT_FOLDING */
5176
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005177#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005178 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5179 ++no_mapping;
5180 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005181 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005182 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005183 --no_mapping;
5184 --allow_keys;
5185#ifdef FEAT_CMDL_INFO
5186 (void)add_to_showcmd(nchar);
5187#endif
5188 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5189 {
5190 clearopbeep(cap->oap);
5191 break;
5192 }
5193 undo = TRUE;
5194 /*FALLTHROUGH*/
5195
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196 case 'g': /* "zg": add good word to word list */
5197 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005198 case 'G': /* "zG": add good word to temp word list */
5199 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005200 {
5201 char_u *ptr = NULL;
5202 int len;
5203
5204 if (checkclearop(cap->oap))
5205 break;
5206# ifdef FEAT_VISUAL
5207 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5208 == FAIL)
5209 return;
5210# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005211 if (ptr == NULL)
5212 {
5213 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005214
5215 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005216 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005217 if (len != 0 && curwin->w_cursor.col <= pos.col)
5218 ptr = ml_get_pos(&curwin->w_cursor);
5219 curwin->w_cursor = pos;
5220 }
5221
Bram Moolenaarb765d632005-06-07 21:00:02 +00005222 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5223 FIND_IDENT)) == 0)
5224 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005225 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005226 (nchar == 'G' || nchar == 'W')
5227 ? 0 : (int)cap->count1,
5228 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005229 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005230 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231
Bram Moolenaar43abc522005-12-10 20:15:02 +00005232 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005233 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005234 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005236#endif
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 default: clearopbeep(cap->oap);
5239 }
5240
5241#ifdef FEAT_FOLDING
5242 /* Redraw when 'foldenable' changed */
5243 if (old_fen != curwin->w_p_fen)
5244 {
5245# ifdef FEAT_DIFF
5246 win_T *wp;
5247
5248 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5249 {
5250 /* Adjust 'foldenable' in diff-synced windows. */
5251 FOR_ALL_WINDOWS(wp)
5252 {
5253 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5254 {
5255 wp->w_p_fen = curwin->w_p_fen;
5256 changed_window_setting_win(wp);
5257 }
5258 }
5259 }
5260# endif
5261 changed_window_setting();
5262 }
5263
5264 /* Redraw when 'foldlevel' changed. */
5265 if (old_fdl != curwin->w_p_fdl)
5266 newFoldLevel();
5267#endif
5268}
5269
5270#ifdef FEAT_GUI
5271/*
5272 * Vertical scrollbar movement.
5273 */
5274 static void
5275nv_ver_scrollbar(cap)
5276 cmdarg_T *cap;
5277{
5278 if (cap->oap->op_type != OP_NOP)
5279 clearopbeep(cap->oap);
5280
5281 /* Even if an operator was pending, we still want to scroll */
5282 gui_do_scroll();
5283}
5284
5285/*
5286 * Horizontal scrollbar movement.
5287 */
5288 static void
5289nv_hor_scrollbar(cap)
5290 cmdarg_T *cap;
5291{
5292 if (cap->oap->op_type != OP_NOP)
5293 clearopbeep(cap->oap);
5294
5295 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005296 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297}
5298#endif
5299
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005300#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005301/*
5302 * Click in GUI tab.
5303 */
5304 static void
5305nv_tabline(cap)
5306 cmdarg_T *cap;
5307{
5308 if (cap->oap->op_type != OP_NOP)
5309 clearopbeep(cap->oap);
5310
5311 /* Even if an operator was pending, we still want to jump tabs. */
5312 goto_tabpage(current_tab);
5313}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005314
5315/*
5316 * Selected item in tab line menu.
5317 */
5318 static void
5319nv_tabmenu(cap)
5320 cmdarg_T *cap;
5321{
5322 if (cap->oap->op_type != OP_NOP)
5323 clearopbeep(cap->oap);
5324
5325 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005326 handle_tabmenu();
5327}
5328
5329/*
5330 * Handle selecting an item of the GUI tab line menu.
5331 * Used in Normal and Insert mode.
5332 */
5333 void
5334handle_tabmenu()
5335{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005336 switch (current_tabmenu)
5337 {
5338 case TABLINE_MENU_CLOSE:
5339 if (current_tab == 0)
5340 do_cmdline_cmd((char_u *)"tabclose");
5341 else
5342 {
5343 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5344 current_tab);
5345 do_cmdline_cmd(IObuff);
5346 }
5347 break;
5348
5349 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005350 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5351 current_tab > 0 ? current_tab - 1 : 999);
5352 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005353 break;
5354
5355 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005356 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5357 current_tab > 0 ? current_tab - 1 : 999);
5358 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005359 break;
5360 }
5361}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005362#endif
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364/*
5365 * "Q" command.
5366 */
5367 static void
5368nv_exmode(cap)
5369 cmdarg_T *cap;
5370{
5371 /*
5372 * Ignore 'Q' in Visual mode, just give a beep.
5373 */
5374#ifdef FEAT_VISUAL
5375 if (VIsual_active)
5376 vim_beep();
5377 else
5378#endif
5379 if (!checkclearop(cap->oap))
5380 do_exmode(FALSE);
5381}
5382
5383/*
5384 * Handle a ":" command.
5385 */
5386 static void
5387nv_colon(cap)
5388 cmdarg_T *cap;
5389{
5390 int old_p_im;
5391
5392#ifdef FEAT_VISUAL
5393 if (VIsual_active)
5394 nv_operator(cap);
5395 else
5396#endif
5397 {
5398 if (cap->oap->op_type != OP_NOP)
5399 {
5400 /* Using ":" as a movement is characterwise exclusive. */
5401 cap->oap->motion_type = MCHAR;
5402 cap->oap->inclusive = FALSE;
5403 }
5404 else if (cap->count0)
5405 {
5406 /* translate "count:" into ":.,.+(count - 1)" */
5407 stuffcharReadbuff('.');
5408 if (cap->count0 > 1)
5409 {
5410 stuffReadbuff((char_u *)",.+");
5411 stuffnumReadbuff((long)cap->count0 - 1L);
5412 }
5413 }
5414
5415 /* When typing, don't type below an old message */
5416 if (KeyTyped)
5417 compute_cmdrow();
5418
5419 old_p_im = p_im;
5420
5421 /* get a command line and execute it */
5422 do_cmdline(NULL, getexline, NULL,
5423 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5424
5425 /* If 'insertmode' changed, enter or exit Insert mode */
5426 if (p_im != old_p_im)
5427 {
5428 if (p_im)
5429 restart_edit = 'i';
5430 else
5431 restart_edit = 0;
5432 }
5433
5434 /* The start of the operator may have become invalid by the Ex
5435 * command. */
5436 if (cap->oap->op_type != OP_NOP
5437 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5438 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005439 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 clearopbeep(cap->oap);
5441 }
5442}
5443
5444/*
5445 * Handle CTRL-G command.
5446 */
5447 static void
5448nv_ctrlg(cap)
5449 cmdarg_T *cap;
5450{
5451#ifdef FEAT_VISUAL
5452 if (VIsual_active) /* toggle Selection/Visual mode */
5453 {
5454 VIsual_select = !VIsual_select;
5455 showmode();
5456 }
5457 else
5458#endif
5459 if (!checkclearop(cap->oap))
5460 /* print full name if count given or :cd used */
5461 fileinfo((int)cap->count0, FALSE, TRUE);
5462}
5463
5464/*
5465 * Handle CTRL-H <Backspace> command.
5466 */
5467 static void
5468nv_ctrlh(cap)
5469 cmdarg_T *cap;
5470{
5471#ifdef FEAT_VISUAL
5472 if (VIsual_active && VIsual_select)
5473 {
5474 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5475 v_visop(cap);
5476 }
5477 else
5478#endif
5479 nv_left(cap);
5480}
5481
5482/*
5483 * CTRL-L: clear screen and redraw.
5484 */
5485 static void
5486nv_clear(cap)
5487 cmdarg_T *cap;
5488{
5489 if (!checkclearop(cap->oap))
5490 {
5491#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5492 /*
5493 * Right now, the BeBox doesn't seem to have an easy way to detect
5494 * window resizing, so we cheat and make the user detect it
5495 * manually with CTRL-L instead
5496 */
5497 ui_get_shellsize();
5498#endif
5499#ifdef FEAT_SYN_HL
5500 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005501 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#endif
5503 redraw_later(CLEAR);
5504 }
5505}
5506
5507/*
5508 * CTRL-O: In Select mode: switch to Visual mode for one command.
5509 * Otherwise: Go to older pcmark.
5510 */
5511 static void
5512nv_ctrlo(cap)
5513 cmdarg_T *cap;
5514{
5515#ifdef FEAT_VISUAL
5516 if (VIsual_active && VIsual_select)
5517 {
5518 VIsual_select = FALSE;
5519 showmode();
5520 restart_VIsual_select = 2; /* restart Select mode later */
5521 }
5522 else
5523#endif
5524 {
5525 cap->count1 = -cap->count1;
5526 nv_pcmark(cap);
5527 }
5528}
5529
5530/*
5531 * CTRL-^ command, short for ":e #"
5532 */
5533 static void
5534nv_hat(cap)
5535 cmdarg_T *cap;
5536{
5537 if (!checkclearopq(cap->oap))
5538 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5539 GETF_SETMARK|GETF_ALT, FALSE);
5540}
5541
5542/*
5543 * "Z" commands.
5544 */
5545 static void
5546nv_Zet(cap)
5547 cmdarg_T *cap;
5548{
5549 if (!checkclearopq(cap->oap))
5550 {
5551 switch (cap->nchar)
5552 {
5553 /* "ZZ": equivalent to ":x". */
5554 case 'Z': do_cmdline_cmd((char_u *)"x");
5555 break;
5556
5557 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5558 case 'Q': do_cmdline_cmd((char_u *)"q!");
5559 break;
5560
5561 default: clearopbeep(cap->oap);
5562 }
5563 }
5564}
5565
5566#if defined(FEAT_WINDOWS) || defined(PROTO)
5567/*
5568 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5569 */
5570 void
5571do_nv_ident(c1, c2)
5572 int c1;
5573 int c2;
5574{
5575 oparg_T oa;
5576 cmdarg_T ca;
5577
5578 clear_oparg(&oa);
5579 vim_memset(&ca, 0, sizeof(ca));
5580 ca.oap = &oa;
5581 ca.cmdchar = c1;
5582 ca.nchar = c2;
5583 nv_ident(&ca);
5584}
5585#endif
5586
5587/*
5588 * Handle the commands that use the word under the cursor.
5589 * [g] CTRL-] :ta to current identifier
5590 * [g] 'K' run program for current identifier
5591 * [g] '*' / to current identifier or string
5592 * [g] '#' ? to current identifier or string
5593 * g ']' :tselect for current identifier
5594 */
5595 static void
5596nv_ident(cap)
5597 cmdarg_T *cap;
5598{
5599 char_u *ptr = NULL;
5600 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005601 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 char_u *p;
5603 char_u *kp; /* value of 'keywordprg' */
5604 int kp_help; /* 'keywordprg' is ":help" */
5605 int n = 0; /* init for GCC */
5606 int cmdchar;
5607 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005608 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 char_u *aux_ptr;
5610 int isman;
5611 int isman_s;
5612
5613 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5614 {
5615 cmdchar = cap->nchar;
5616 g_cmd = TRUE;
5617 }
5618 else
5619 {
5620 cmdchar = cap->cmdchar;
5621 g_cmd = FALSE;
5622 }
5623
5624 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5625 cmdchar = '#';
5626
5627 /*
5628 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5629 */
5630 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5631 {
5632#ifdef FEAT_VISUAL
5633 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5634 return;
5635#endif
5636 if (checkclearopq(cap->oap))
5637 return;
5638 }
5639
5640 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5641 (cmdchar == '*' || cmdchar == '#')
5642 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5643 {
5644 clearop(cap->oap);
5645 return;
5646 }
5647
5648 /* Allocate buffer to put the command in. Inserting backslashes can
5649 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5650 * and some numbers. */
5651 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5652 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5653 || STRCMP(kp, ":help") == 0);
5654 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5655 if (buf == NULL)
5656 return;
5657 buf[0] = NUL;
5658
5659 switch (cmdchar)
5660 {
5661 case '*':
5662 case '#':
5663 /*
5664 * Put cursor at start of word, makes search skip the word
5665 * under the cursor.
5666 * Call setpcmark() first, so "*``" puts the cursor back where
5667 * it was.
5668 */
5669 setpcmark();
5670 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5671
5672 if (!g_cmd && vim_iswordp(ptr))
5673 STRCPY(buf, "\\<");
5674 no_smartcase = TRUE; /* don't use 'smartcase' now */
5675 break;
5676
5677 case 'K':
5678 if (kp_help)
5679 STRCPY(buf, "he! ");
5680 else
5681 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005682 /* An external command will probably use an argument starting
5683 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005684 while (*ptr == '-' && n > 0)
5685 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005686 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005687 --n;
5688 }
5689 if (n == 0)
5690 {
5691 EMSG(_(e_noident)); /* found dashes only */
5692 vim_free(buf);
5693 return;
5694 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* When a count is given, turn it into a range. Is this
5697 * really what we want? */
5698 isman = (STRCMP(kp, "man") == 0);
5699 isman_s = (STRCMP(kp, "man -s") == 0);
5700 if (cap->count0 != 0 && !(isman || isman_s))
5701 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5702
5703 STRCAT(buf, "! ");
5704 if (cap->count0 == 0 && isman_s)
5705 STRCAT(buf, "man");
5706 else
5707 STRCAT(buf, kp);
5708 STRCAT(buf, " ");
5709 if (cap->count0 != 0 && (isman || isman_s))
5710 {
5711 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5712 STRCAT(buf, " ");
5713 }
5714 }
5715 break;
5716
5717 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005718 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#ifdef FEAT_CSCOPE
5720 if (p_cst)
5721 STRCPY(buf, "cstag ");
5722 else
5723#endif
5724 STRCPY(buf, "ts ");
5725 break;
5726
5727 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005728 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (curbuf->b_help)
5730 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005732 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005733 if (g_cmd)
5734 STRCPY(buf, "tj ");
5735 else
5736 sprintf((char *)buf, "%ldta ", cap->count0);
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739
5740 /*
5741 * Now grab the chars in the identifier
5742 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005743 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005745 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005746 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005748 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005749 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 vim_free(buf);
5752 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005754 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5755 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005756 {
5757 vim_free(buf);
5758 vim_free(p);
5759 return;
5760 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005761 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005762 STRCAT(buf, p);
5763 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005765 else
5766 {
5767 if (cmdchar == '*')
5768 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5769 else if (cmdchar == '#')
5770 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005771 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005772 {
5773 if (curbuf->b_help)
5774 /* ":help" handles unescaped argument */
5775 aux_ptr = (char_u *)"";
5776 else
5777 aux_ptr = (char_u *)"\\|\"\n[";
5778 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005779 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005780 aux_ptr = (char_u *)"\\|\"\n*?[";
5781
5782 p = buf + STRLEN(buf);
5783 while (n-- > 0)
5784 {
5785 /* put a backslash before \ and some others */
5786 if (vim_strchr(aux_ptr, *ptr) != NULL)
5787 *p++ = '\\';
5788#ifdef FEAT_MBYTE
5789 /* When current byte is a part of multibyte character, copy all
5790 * bytes of that character. */
5791 if (has_mbyte)
5792 {
5793 int i;
5794 int len = (*mb_ptr2len)(ptr) - 1;
5795
5796 for (i = 0; i < len && n >= 1; ++i, --n)
5797 *p++ = *ptr++;
5798 }
5799#endif
5800 *p++ = *ptr++;
5801 }
5802 *p = NUL;
5803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 /*
5806 * Execute the command.
5807 */
5808 if (cmdchar == '*' || cmdchar == '#')
5809 {
5810 if (!g_cmd && (
5811#ifdef FEAT_MBYTE
5812 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5813#endif
5814 vim_iswordc(ptr[-1])))
5815 STRCAT(buf, "\\>");
5816#ifdef FEAT_CMDHIST
5817 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005818 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5820#endif
5821 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5822 }
5823 else
5824 do_cmdline_cmd(buf);
5825
5826 vim_free(buf);
5827}
5828
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005829#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830/*
5831 * Get visually selected text, within one line only.
5832 * Returns FAIL if more than one line selected.
5833 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005834 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835get_visual_text(cap, pp, lenp)
5836 cmdarg_T *cap;
5837 char_u **pp; /* return: start of selected text */
5838 int *lenp; /* return: length of selected text */
5839{
5840 if (VIsual_mode != 'V')
5841 unadjust_for_sel();
5842 if (VIsual.lnum != curwin->w_cursor.lnum)
5843 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005844 if (cap != NULL)
5845 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 return FAIL;
5847 }
5848 if (VIsual_mode == 'V')
5849 {
5850 *pp = ml_get_curline();
5851 *lenp = (int)STRLEN(*pp);
5852 }
5853 else
5854 {
5855 if (lt(curwin->w_cursor, VIsual))
5856 {
5857 *pp = ml_get_pos(&curwin->w_cursor);
5858 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5859 }
5860 else
5861 {
5862 *pp = ml_get_pos(&VIsual);
5863 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5864 }
5865#ifdef FEAT_MBYTE
5866 if (has_mbyte)
5867 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005868 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#endif
5870 }
5871 reset_VIsual_and_resel();
5872 return OK;
5873}
5874#endif
5875
5876/*
5877 * CTRL-T: backwards in tag stack
5878 */
5879 static void
5880nv_tagpop(cap)
5881 cmdarg_T *cap;
5882{
5883 if (!checkclearopq(cap->oap))
5884 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5885}
5886
5887/*
5888 * Handle scrolling command 'H', 'L' and 'M'.
5889 */
5890 static void
5891nv_scroll(cap)
5892 cmdarg_T *cap;
5893{
5894 int used = 0;
5895 long n;
5896#ifdef FEAT_FOLDING
5897 linenr_T lnum;
5898#endif
5899 int half;
5900
5901 cap->oap->motion_type = MLINE;
5902 setpcmark();
5903
5904 if (cap->cmdchar == 'L')
5905 {
5906 validate_botline(); /* make sure curwin->w_botline is valid */
5907 curwin->w_cursor.lnum = curwin->w_botline - 1;
5908 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5909 curwin->w_cursor.lnum = 1;
5910 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005911 {
5912#ifdef FEAT_FOLDING
5913 if (hasAnyFolding(curwin))
5914 {
5915 /* Count a fold for one screen line. */
5916 for (n = cap->count1 - 1; n > 0
5917 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5918 {
5919 (void)hasFolding(curwin->w_cursor.lnum,
5920 &curwin->w_cursor.lnum, NULL);
5921 --curwin->w_cursor.lnum;
5922 }
5923 }
5924 else
5925#endif
5926 curwin->w_cursor.lnum -= cap->count1 - 1;
5927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 }
5929 else
5930 {
5931 if (cap->cmdchar == 'M')
5932 {
5933#ifdef FEAT_DIFF
5934 /* Don't count filler lines above the window. */
5935 used -= diff_check_fill(curwin, curwin->w_topline)
5936 - curwin->w_topfill;
5937#endif
5938 validate_botline(); /* make sure w_empty_rows is valid */
5939 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5940 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5941 {
5942#ifdef FEAT_DIFF
5943 /* Count half he number of filler lines to be "below this
5944 * line" and half to be "above the next line". */
5945 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5946 + n) / 2 >= half)
5947 {
5948 --n;
5949 break;
5950 }
5951#endif
5952 used += plines(curwin->w_topline + n);
5953 if (used >= half)
5954 break;
5955#ifdef FEAT_FOLDING
5956 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5957 n = lnum - curwin->w_topline;
5958#endif
5959 }
5960 if (n > 0 && used > curwin->w_height)
5961 --n;
5962 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005963 else /* (cap->cmdchar == 'H') */
5964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005966#ifdef FEAT_FOLDING
5967 if (hasAnyFolding(curwin))
5968 {
5969 /* Count a fold for one screen line. */
5970 lnum = curwin->w_topline;
5971 while (n-- > 0 && lnum < curwin->w_botline - 1)
5972 {
5973 hasFolding(lnum, NULL, &lnum);
5974 ++lnum;
5975 }
5976 n = lnum - curwin->w_topline;
5977 }
5978#endif
5979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 curwin->w_cursor.lnum = curwin->w_topline + n;
5981 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5982 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5983 }
5984
5985 cursor_correct(); /* correct for 'so' */
5986 beginline(BL_SOL | BL_FIX);
5987}
5988
5989/*
5990 * Cursor right commands.
5991 */
5992 static void
5993nv_right(cap)
5994 cmdarg_T *cap;
5995{
5996 long n;
5997#ifdef FEAT_VISUAL
5998 int PAST_LINE;
5999#else
6000# define PAST_LINE 0
6001#endif
6002
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006003 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6004 {
6005 /* <C-Right> and <S-Right> move a word or WORD right */
6006 if (mod_mask & MOD_MASK_CTRL)
6007 cap->arg = TRUE;
6008 nv_wordcmd(cap);
6009 return;
6010 }
6011
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 cap->oap->motion_type = MCHAR;
6013 cap->oap->inclusive = FALSE;
6014#ifdef FEAT_VISUAL
6015 PAST_LINE = (VIsual_active && *p_sel != 'o');
6016
6017# ifdef FEAT_VIRTUALEDIT
6018 /*
6019 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006020 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 */
6022 if (virtual_active())
6023 PAST_LINE = 0;
6024# endif
6025#endif
6026
6027 for (n = cap->count1; n > 0; --n)
6028 {
6029 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006030#ifdef FEAT_VISUAL
6031 || (PAST_LINE && *ml_get_cursor() == NUL)
6032#endif
6033 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
6035 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006036 * <Space> wraps to next line if 'whichwrap' has 's'.
6037 * 'l' wraps to next line if 'whichwrap' has 'l'.
6038 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 */
6040 if ( ((cap->cmdchar == ' '
6041 && vim_strchr(p_ww, 's') != NULL)
6042 || (cap->cmdchar == 'l'
6043 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006044 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 && vim_strchr(p_ww, '>') != NULL))
6046 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6047 {
6048 /* When deleting we also count the NL as a character.
6049 * Set cap->oap->inclusive when last char in the line is
6050 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006051 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 && !cap->oap->inclusive
6053 && !lineempty(curwin->w_cursor.lnum))
6054 cap->oap->inclusive = TRUE;
6055 else
6056 {
6057 ++curwin->w_cursor.lnum;
6058 curwin->w_cursor.col = 0;
6059#ifdef FEAT_VIRTUALEDIT
6060 curwin->w_cursor.coladd = 0;
6061#endif
6062 curwin->w_set_curswant = TRUE;
6063 cap->oap->inclusive = FALSE;
6064 }
6065 continue;
6066 }
6067 if (cap->oap->op_type == OP_NOP)
6068 {
6069 /* Only beep and flush if not moved at all */
6070 if (n == cap->count1)
6071 beep_flush();
6072 }
6073 else
6074 {
6075 if (!lineempty(curwin->w_cursor.lnum))
6076 cap->oap->inclusive = TRUE;
6077 }
6078 break;
6079 }
6080#ifdef FEAT_VISUAL
6081 else if (PAST_LINE)
6082 {
6083 curwin->w_set_curswant = TRUE;
6084# ifdef FEAT_VIRTUALEDIT
6085 if (virtual_active())
6086 oneright();
6087 else
6088# endif
6089 {
6090# ifdef FEAT_MBYTE
6091 if (has_mbyte)
6092 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006093 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 else
6095# endif
6096 ++curwin->w_cursor.col;
6097 }
6098 }
6099#endif
6100 }
6101#ifdef FEAT_FOLDING
6102 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6103 && cap->oap->op_type == OP_NOP)
6104 foldOpenCursor();
6105#endif
6106}
6107
6108/*
6109 * Cursor left commands.
6110 *
6111 * Returns TRUE when operator end should not be adjusted.
6112 */
6113 static void
6114nv_left(cap)
6115 cmdarg_T *cap;
6116{
6117 long n;
6118
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006119 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6120 {
6121 /* <C-Left> and <S-Left> move a word or WORD left */
6122 if (mod_mask & MOD_MASK_CTRL)
6123 cap->arg = 1;
6124 nv_bck_word(cap);
6125 return;
6126 }
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 cap->oap->motion_type = MCHAR;
6129 cap->oap->inclusive = FALSE;
6130 for (n = cap->count1; n > 0; --n)
6131 {
6132 if (oneleft() == FAIL)
6133 {
6134 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6135 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6136 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6137 */
6138 if ( (((cap->cmdchar == K_BS
6139 || cap->cmdchar == Ctrl_H)
6140 && vim_strchr(p_ww, 'b') != NULL)
6141 || (cap->cmdchar == 'h'
6142 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006143 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 && vim_strchr(p_ww, '<') != NULL))
6145 && curwin->w_cursor.lnum > 1)
6146 {
6147 --(curwin->w_cursor.lnum);
6148 coladvance((colnr_T)MAXCOL);
6149 curwin->w_set_curswant = TRUE;
6150
6151 /* When the NL before the first char has to be deleted we
6152 * put the cursor on the NUL after the previous line.
6153 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006154 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if ( (cap->oap->op_type == OP_DELETE
6156 || cap->oap->op_type == OP_CHANGE)
6157 && !lineempty(curwin->w_cursor.lnum))
6158 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006159 if (*ml_get_cursor() != NUL)
6160 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 cap->retval |= CA_NO_ADJ_OP_END;
6162 }
6163 continue;
6164 }
6165 /* Only beep and flush if not moved at all */
6166 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6167 beep_flush();
6168 break;
6169 }
6170 }
6171#ifdef FEAT_FOLDING
6172 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6173 && cap->oap->op_type == OP_NOP)
6174 foldOpenCursor();
6175#endif
6176}
6177
6178/*
6179 * Cursor up commands.
6180 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6181 */
6182 static void
6183nv_up(cap)
6184 cmdarg_T *cap;
6185{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006186 if (mod_mask & MOD_MASK_SHIFT)
6187 {
6188 /* <S-Up> is page up */
6189 cap->arg = BACKWARD;
6190 nv_page(cap);
6191 }
6192 else
6193 {
6194 cap->oap->motion_type = MLINE;
6195 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6196 clearopbeep(cap->oap);
6197 else if (cap->arg)
6198 beginline(BL_WHITE | BL_FIX);
6199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200}
6201
6202/*
6203 * Cursor down commands.
6204 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6205 */
6206 static void
6207nv_down(cap)
6208 cmdarg_T *cap;
6209{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006210 if (mod_mask & MOD_MASK_SHIFT)
6211 {
6212 /* <S-Down> is page down */
6213 cap->arg = FORWARD;
6214 nv_page(cap);
6215 }
6216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6218 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006219 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006220 if (curwin->w_llist_ref == NULL)
6221 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6222 else
6223 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 else
6225#endif
6226 {
6227#ifdef FEAT_CMDWIN
6228 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006229 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 cmdwin_result = CAR;
6231 else
6232#endif
6233 {
6234 cap->oap->motion_type = MLINE;
6235 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6236 clearopbeep(cap->oap);
6237 else if (cap->arg)
6238 beginline(BL_WHITE | BL_FIX);
6239 }
6240 }
6241}
6242
6243#ifdef FEAT_SEARCHPATH
6244/*
6245 * Grab the file name under the cursor and edit it.
6246 */
6247 static void
6248nv_gotofile(cap)
6249 cmdarg_T *cap;
6250{
6251 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006252 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006254 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
6256 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006257 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 return;
6259 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006260#ifdef FEAT_AUTOCMD
6261 if (curbuf_locked())
6262 {
6263 clearop(cap->oap);
6264 return;
6265 }
6266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006268 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 if (ptr != NULL)
6271 {
6272 /* do autowrite if necessary */
6273 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6274 autowrite(curbuf, FALSE);
6275 setpcmark();
6276 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006277 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006278 if (cap->nchar == 'F' && lnum >= 0)
6279 {
6280 curwin->w_cursor.lnum = lnum;
6281 check_cursor_lnum();
6282 beginline(BL_SOL | BL_FIX);
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 vim_free(ptr);
6285 }
6286 else
6287 clearop(cap->oap);
6288}
6289#endif
6290
6291/*
6292 * <End> command: to end of current line or last line.
6293 */
6294 static void
6295nv_end(cap)
6296 cmdarg_T *cap;
6297{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006298 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006300 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 nv_goto(cap);
6302 cap->count1 = 1; /* to end of current line */
6303 }
6304 nv_dollar(cap);
6305}
6306
6307/*
6308 * Handle the "$" command.
6309 */
6310 static void
6311nv_dollar(cap)
6312 cmdarg_T *cap;
6313{
6314 cap->oap->motion_type = MCHAR;
6315 cap->oap->inclusive = TRUE;
6316#ifdef FEAT_VIRTUALEDIT
6317 /* In virtual mode when off the edge of a line and an operator
6318 * is pending (whew!) keep the cursor where it is.
6319 * Otherwise, send it to the end of the line. */
6320 if (!virtual_active() || gchar_cursor() != NUL
6321 || cap->oap->op_type == OP_NOP)
6322#endif
6323 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6324 if (cursor_down((long)(cap->count1 - 1),
6325 cap->oap->op_type == OP_NOP) == FAIL)
6326 clearopbeep(cap->oap);
6327#ifdef FEAT_FOLDING
6328 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6329 foldOpenCursor();
6330#endif
6331}
6332
6333/*
6334 * Implementation of '?' and '/' commands.
6335 * If cap->arg is TRUE don't set PC mark.
6336 */
6337 static void
6338nv_search(cap)
6339 cmdarg_T *cap;
6340{
6341 oparg_T *oap = cap->oap;
6342
6343 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6344 {
6345 /* Translate "g??" to "g?g?" */
6346 cap->cmdchar = 'g';
6347 cap->nchar = '?';
6348 nv_operator(cap);
6349 return;
6350 }
6351
6352 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6353
6354 if (cap->searchbuf == NULL)
6355 {
6356 clearop(oap);
6357 return;
6358 }
6359
6360 normal_search(cap, cap->cmdchar, cap->searchbuf,
6361 (cap->arg ? 0 : SEARCH_MARK));
6362}
6363
6364/*
6365 * Handle "N" and "n" commands.
6366 * cap->arg is SEARCH_REV for "N", 0 for "n".
6367 */
6368 static void
6369nv_next(cap)
6370 cmdarg_T *cap;
6371{
6372 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6373}
6374
6375/*
6376 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6377 * Uses only cap->count1 and cap->oap from "cap".
6378 */
6379 static void
6380normal_search(cap, dir, pat, opt)
6381 cmdarg_T *cap;
6382 int dir;
6383 char_u *pat;
6384 int opt; /* extra flags for do_search() */
6385{
6386 int i;
6387
6388 cap->oap->motion_type = MCHAR;
6389 cap->oap->inclusive = FALSE;
6390 cap->oap->use_reg_one = TRUE;
6391 curwin->w_set_curswant = TRUE;
6392
6393 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006394 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (i == 0)
6396 clearop(cap->oap);
6397 else
6398 {
6399 if (i == 2)
6400 cap->oap->motion_type = MLINE;
6401#ifdef FEAT_VIRTUALEDIT
6402 curwin->w_cursor.coladd = 0;
6403#endif
6404#ifdef FEAT_FOLDING
6405 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6406 foldOpenCursor();
6407#endif
6408 }
6409
6410 /* "/$" will put the cursor after the end of the line, may need to
6411 * correct that here */
6412 check_cursor();
6413}
6414
6415/*
6416 * Character search commands.
6417 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6418 * ',' and FALSE for ';'.
6419 * cap->nchar is NUL for ',' and ';' (repeat the search)
6420 */
6421 static void
6422nv_csearch(cap)
6423 cmdarg_T *cap;
6424{
6425 int t_cmd;
6426
6427 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6428 t_cmd = TRUE;
6429 else
6430 t_cmd = FALSE;
6431
6432 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6434 clearopbeep(cap->oap);
6435 else
6436 {
6437 curwin->w_set_curswant = TRUE;
6438#ifdef FEAT_VIRTUALEDIT
6439 /* Include a Tab for "tx" and for "dfx". */
6440 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6441 && (t_cmd || cap->oap->op_type != OP_NOP))
6442 {
6443 colnr_T scol, ecol;
6444
6445 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6446 curwin->w_cursor.coladd = ecol - scol;
6447 }
6448 else
6449 curwin->w_cursor.coladd = 0;
6450#endif
6451#ifdef FEAT_VISUAL
6452 adjust_for_sel(cap);
6453#endif
6454#ifdef FEAT_FOLDING
6455 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6456 foldOpenCursor();
6457#endif
6458 }
6459}
6460
6461/*
6462 * "[" and "]" commands.
6463 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6464 */
6465 static void
6466nv_brackets(cap)
6467 cmdarg_T *cap;
6468{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006469 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 pos_T prev_pos;
6471 pos_T *pos = NULL; /* init for GCC */
6472 pos_T old_pos; /* cursor position before command */
6473 int flag;
6474 long n;
6475 int findc;
6476 int c;
6477
6478 cap->oap->motion_type = MCHAR;
6479 cap->oap->inclusive = FALSE;
6480 old_pos = curwin->w_cursor;
6481#ifdef FEAT_VIRTUALEDIT
6482 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6483#endif
6484
6485#ifdef FEAT_SEARCHPATH
6486 /*
6487 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6488 */
6489 if (cap->nchar == 'f')
6490 nv_gotofile(cap);
6491 else
6492#endif
6493
6494#ifdef FEAT_FIND_ID
6495 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006496 * Find the occurrence(s) of the identifier or define under cursor
6497 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 *
6499 * search list jump
6500 * fwd bwd fwd bwd fwd bwd
6501 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6502 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6503 */
6504 if (vim_strchr((char_u *)
6505#ifdef EBCDIC
6506 "iI\005dD\067",
6507#else
6508 "iI\011dD\004",
6509#endif
6510 cap->nchar) != NULL)
6511 {
6512 char_u *ptr;
6513 int len;
6514
6515 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6516 clearop(cap->oap);
6517 else
6518 {
6519 find_pattern_in_path(ptr, 0, len, TRUE,
6520 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6521 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6522 cap->count1,
6523 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6524 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6525 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6526 (linenr_T)MAXLNUM);
6527 curwin->w_set_curswant = TRUE;
6528 }
6529 }
6530 else
6531#endif
6532
6533 /*
6534 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6535 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6536 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6537 * "[m" or "]m" search for prev/next start of (Java) method.
6538 * "[M" or "]M" search for prev/next end of (Java) method.
6539 */
6540 if ( (cap->cmdchar == '['
6541 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6542 || (cap->cmdchar == ']'
6543 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6544 {
6545 if (cap->nchar == '*')
6546 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 prev_pos.lnum = 0;
6548 if (cap->nchar == 'm' || cap->nchar == 'M')
6549 {
6550 if (cap->cmdchar == '[')
6551 findc = '{';
6552 else
6553 findc = '}';
6554 n = 9999;
6555 }
6556 else
6557 {
6558 findc = cap->nchar;
6559 n = cap->count1;
6560 }
6561 for ( ; n > 0; --n)
6562 {
6563 if ((pos = findmatchlimit(cap->oap, findc,
6564 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6565 {
6566 if (new_pos.lnum == 0) /* nothing found */
6567 {
6568 if (cap->nchar != 'm' && cap->nchar != 'M')
6569 clearopbeep(cap->oap);
6570 }
6571 else
6572 pos = &new_pos; /* use last one found */
6573 break;
6574 }
6575 prev_pos = new_pos;
6576 curwin->w_cursor = *pos;
6577 new_pos = *pos;
6578 }
6579 curwin->w_cursor = old_pos;
6580
6581 /*
6582 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6583 * brought us to the match for "[m" and "]M" when inside a method.
6584 * Try finding the '{' or '}' we want to be at.
6585 * Also repeat for the given count.
6586 */
6587 if (cap->nchar == 'm' || cap->nchar == 'M')
6588 {
6589 /* norm is TRUE for "]M" and "[m" */
6590 int norm = ((findc == '{') == (cap->nchar == 'm'));
6591
6592 n = cap->count1;
6593 /* found a match: we were inside a method */
6594 if (prev_pos.lnum != 0)
6595 {
6596 pos = &prev_pos;
6597 curwin->w_cursor = prev_pos;
6598 if (norm)
6599 --n;
6600 }
6601 else
6602 pos = NULL;
6603 while (n > 0)
6604 {
6605 for (;;)
6606 {
6607 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6608 {
6609 /* if not found anything, that's an error */
6610 if (pos == NULL)
6611 clearopbeep(cap->oap);
6612 n = 0;
6613 break;
6614 }
6615 c = gchar_cursor();
6616 if (c == '{' || c == '}')
6617 {
6618 /* Must have found end/start of class: use it.
6619 * Or found the place to be at. */
6620 if ((c == findc && norm) || (n == 1 && !norm))
6621 {
6622 new_pos = curwin->w_cursor;
6623 pos = &new_pos;
6624 n = 0;
6625 }
6626 /* if no match found at all, we started outside of the
6627 * class and we're inside now. Just go on. */
6628 else if (new_pos.lnum == 0)
6629 {
6630 new_pos = curwin->w_cursor;
6631 pos = &new_pos;
6632 }
6633 /* found start/end of other method: go to match */
6634 else if ((pos = findmatchlimit(cap->oap, findc,
6635 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6636 0)) == NULL)
6637 n = 0;
6638 else
6639 curwin->w_cursor = *pos;
6640 break;
6641 }
6642 }
6643 --n;
6644 }
6645 curwin->w_cursor = old_pos;
6646 if (pos == NULL && new_pos.lnum != 0)
6647 clearopbeep(cap->oap);
6648 }
6649 if (pos != NULL)
6650 {
6651 setpcmark();
6652 curwin->w_cursor = *pos;
6653 curwin->w_set_curswant = TRUE;
6654#ifdef FEAT_FOLDING
6655 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6656 && cap->oap->op_type == OP_NOP)
6657 foldOpenCursor();
6658#endif
6659 }
6660 }
6661
6662 /*
6663 * "[[", "[]", "]]" and "][": move to start or end of function
6664 */
6665 else if (cap->nchar == '[' || cap->nchar == ']')
6666 {
6667 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6668 flag = '{';
6669 else
6670 flag = '}'; /* "][" or "[]" */
6671
6672 curwin->w_set_curswant = TRUE;
6673 /*
6674 * Imitate strange Vi behaviour: When using "]]" with an operator
6675 * we also stop at '}'.
6676 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006677 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 (cap->oap->op_type != OP_NOP
6679 && cap->arg == FORWARD && flag == '{')))
6680 clearopbeep(cap->oap);
6681 else
6682 {
6683 if (cap->oap->op_type == OP_NOP)
6684 beginline(BL_WHITE | BL_FIX);
6685#ifdef FEAT_FOLDING
6686 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6687 foldOpenCursor();
6688#endif
6689 }
6690 }
6691
6692 /*
6693 * "[p", "[P", "]P" and "]p": put with indent adjustment
6694 */
6695 else if (cap->nchar == 'p' || cap->nchar == 'P')
6696 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006697 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 prep_redo_cmd(cap);
6700 do_put(cap->oap->regname,
6701 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6702 cap->count1, PUT_FIXINDENT);
6703 }
6704 }
6705
6706 /*
6707 * "['", "[`", "]'" and "]`": jump to next mark
6708 */
6709 else if (cap->nchar == '\'' || cap->nchar == '`')
6710 {
6711 pos = &curwin->w_cursor;
6712 for (n = cap->count1; n > 0; --n)
6713 {
6714 prev_pos = *pos;
6715 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6716 cap->nchar == '\'');
6717 if (pos == NULL)
6718 break;
6719 }
6720 if (pos == NULL)
6721 pos = &prev_pos;
6722 nv_cursormark(cap, cap->nchar == '\'', pos);
6723 }
6724
6725#ifdef FEAT_MOUSE
6726 /*
6727 * [ or ] followed by a middle mouse click: put selected text with
6728 * indent adjustment. Any other button just does as usual.
6729 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006730 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
6732 (void)do_mouse(cap->oap, cap->nchar,
6733 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6734 cap->count1, PUT_FIXINDENT);
6735 }
6736#endif /* FEAT_MOUSE */
6737
6738#ifdef FEAT_FOLDING
6739 /*
6740 * "[z" and "]z": move to start or end of open fold.
6741 */
6742 else if (cap->nchar == 'z')
6743 {
6744 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6745 cap->count1) == FAIL)
6746 clearopbeep(cap->oap);
6747 }
6748#endif
6749
6750#ifdef FEAT_DIFF
6751 /*
6752 * "[c" and "]c": move to next or previous diff-change.
6753 */
6754 else if (cap->nchar == 'c')
6755 {
6756 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6757 cap->count1) == FAIL)
6758 clearopbeep(cap->oap);
6759 }
6760#endif
6761
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006762#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006763 /*
6764 * "[s", "[S", "]s" and "]S": move to next spell error.
6765 */
6766 else if (cap->nchar == 's' || cap->nchar == 'S')
6767 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006768 setpcmark();
6769 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006770 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6771 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006772 {
6773 clearopbeep(cap->oap);
6774 break;
6775 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006776# ifdef FEAT_FOLDING
6777 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6778 foldOpenCursor();
6779# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006780 }
6781#endif
6782
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 /* Not a valid cap->nchar. */
6784 else
6785 clearopbeep(cap->oap);
6786}
6787
6788/*
6789 * Handle Normal mode "%" command.
6790 */
6791 static void
6792nv_percent(cap)
6793 cmdarg_T *cap;
6794{
6795 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006796#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 linenr_T lnum = curwin->w_cursor.lnum;
6798#endif
6799
6800 cap->oap->inclusive = TRUE;
6801 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6802 {
6803 if (cap->count0 > 100)
6804 clearopbeep(cap->oap);
6805 else
6806 {
6807 cap->oap->motion_type = MLINE;
6808 setpcmark();
6809 /* Round up, so CTRL-G will give same value. Watch out for a
6810 * large line count, the line number must not go negative! */
6811 if (curbuf->b_ml.ml_line_count > 1000000)
6812 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6813 / 100L * cap->count0;
6814 else
6815 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6816 cap->count0 + 99L) / 100L;
6817 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6818 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6819 beginline(BL_SOL | BL_FIX);
6820 }
6821 }
6822 else /* "%" : go to matching paren */
6823 {
6824 cap->oap->motion_type = MCHAR;
6825 cap->oap->use_reg_one = TRUE;
6826 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6827 clearopbeep(cap->oap);
6828 else
6829 {
6830 setpcmark();
6831 curwin->w_cursor = *pos;
6832 curwin->w_set_curswant = TRUE;
6833#ifdef FEAT_VIRTUALEDIT
6834 curwin->w_cursor.coladd = 0;
6835#endif
6836#ifdef FEAT_VISUAL
6837 adjust_for_sel(cap);
6838#endif
6839 }
6840 }
6841#ifdef FEAT_FOLDING
6842 if (cap->oap->op_type == OP_NOP
6843 && lnum != curwin->w_cursor.lnum
6844 && (fdo_flags & FDO_PERCENT)
6845 && KeyTyped)
6846 foldOpenCursor();
6847#endif
6848}
6849
6850/*
6851 * Handle "(" and ")" commands.
6852 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6853 */
6854 static void
6855nv_brace(cap)
6856 cmdarg_T *cap;
6857{
6858 cap->oap->motion_type = MCHAR;
6859 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006860 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6861 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 curwin->w_set_curswant = TRUE;
6863
6864 if (findsent(cap->arg, cap->count1) == FAIL)
6865 clearopbeep(cap->oap);
6866 else
6867 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006868 /* Don't leave the cursor on the NUL past end of line. */
6869 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870#ifdef FEAT_VIRTUALEDIT
6871 curwin->w_cursor.coladd = 0;
6872#endif
6873#ifdef FEAT_FOLDING
6874 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6875 foldOpenCursor();
6876#endif
6877 }
6878}
6879
6880/*
6881 * "m" command: Mark a position.
6882 */
6883 static void
6884nv_mark(cap)
6885 cmdarg_T *cap;
6886{
6887 if (!checkclearop(cap->oap))
6888 {
6889 if (setmark(cap->nchar) == FAIL)
6890 clearopbeep(cap->oap);
6891 }
6892}
6893
6894/*
6895 * "{" and "}" commands.
6896 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6897 */
6898 static void
6899nv_findpar(cap)
6900 cmdarg_T *cap;
6901{
6902 cap->oap->motion_type = MCHAR;
6903 cap->oap->inclusive = FALSE;
6904 cap->oap->use_reg_one = TRUE;
6905 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006906 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 clearopbeep(cap->oap);
6908 else
6909 {
6910#ifdef FEAT_VIRTUALEDIT
6911 curwin->w_cursor.coladd = 0;
6912#endif
6913#ifdef FEAT_FOLDING
6914 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6915 foldOpenCursor();
6916#endif
6917 }
6918}
6919
6920/*
6921 * "u" command: Undo or make lower case.
6922 */
6923 static void
6924nv_undo(cap)
6925 cmdarg_T *cap;
6926{
6927 if (cap->oap->op_type == OP_LOWER
6928#ifdef FEAT_VISUAL
6929 || VIsual_active
6930#endif
6931 )
6932 {
6933 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6934 cap->cmdchar = 'g';
6935 cap->nchar = 'u';
6936 nv_operator(cap);
6937 }
6938 else
6939 nv_kundo(cap);
6940}
6941
6942/*
6943 * <Undo> command.
6944 */
6945 static void
6946nv_kundo(cap)
6947 cmdarg_T *cap;
6948{
6949 if (!checkclearopq(cap->oap))
6950 {
6951 u_undo((int)cap->count1);
6952 curwin->w_set_curswant = TRUE;
6953 }
6954}
6955
6956/*
6957 * Handle the "r" command.
6958 */
6959 static void
6960nv_replace(cap)
6961 cmdarg_T *cap;
6962{
6963 char_u *ptr;
6964 int had_ctrl_v;
6965 long n;
6966
6967 if (checkclearop(cap->oap))
6968 return;
6969
6970 /* get another character */
6971 if (cap->nchar == Ctrl_V)
6972 {
6973 had_ctrl_v = Ctrl_V;
6974 cap->nchar = get_literal();
6975 /* Don't redo a multibyte character with CTRL-V. */
6976 if (cap->nchar > DEL)
6977 had_ctrl_v = NUL;
6978 }
6979 else
6980 had_ctrl_v = NUL;
6981
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006982 /* Abort if the character is a special key. */
6983 if (IS_SPECIAL(cap->nchar))
6984 {
6985 clearopbeep(cap->oap);
6986 return;
6987 }
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989#ifdef FEAT_VISUAL
6990 /* Visual mode "r" */
6991 if (VIsual_active)
6992 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006993 if (got_int)
6994 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 nv_operator(cap);
6996 return;
6997 }
6998#endif
6999
7000#ifdef FEAT_VIRTUALEDIT
7001 /* Break tabs, etc. */
7002 if (virtual_active())
7003 {
7004 if (u_save_cursor() == FAIL)
7005 return;
7006 if (gchar_cursor() == NUL)
7007 {
7008 /* Add extra space and put the cursor on the first one. */
7009 coladvance_force((colnr_T)(getviscol() + cap->count1));
7010 curwin->w_cursor.col -= cap->count1;
7011 }
7012 else if (gchar_cursor() == TAB)
7013 coladvance_force(getviscol());
7014 }
7015#endif
7016
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007017 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007019 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#ifdef FEAT_MBYTE
7021 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7022#endif
7023 )
7024 {
7025 clearopbeep(cap->oap);
7026 return;
7027 }
7028
7029 /*
7030 * Replacing with a TAB is done by edit() when it is complicated because
7031 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7032 * Other characters are done below to avoid problems with things like
7033 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7034 */
7035 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7036 {
7037 stuffnumReadbuff(cap->count1);
7038 stuffcharReadbuff('R');
7039 stuffcharReadbuff('\t');
7040 stuffcharReadbuff(ESC);
7041 return;
7042 }
7043
7044 /* save line for undo */
7045 if (u_save_cursor() == FAIL)
7046 return;
7047
7048 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7049 {
7050 /*
7051 * Replace character(s) by a single newline.
7052 * Strange vi behaviour: Only one newline is inserted.
7053 * Delete the characters here.
7054 * Insert the newline with an insert command, takes care of
7055 * autoindent. The insert command depends on being on the last
7056 * character of a line or not.
7057 */
7058#ifdef FEAT_MBYTE
7059 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7060#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007061 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#endif
7063 stuffcharReadbuff('\r');
7064 stuffcharReadbuff(ESC);
7065
7066 /* Give 'r' to edit(), to get the redo command right. */
7067 invoke_edit(cap, TRUE, 'r', FALSE);
7068 }
7069 else
7070 {
7071 prep_redo(cap->oap->regname, cap->count1,
7072 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7073
7074 curbuf->b_op_start = curwin->w_cursor;
7075#ifdef FEAT_MBYTE
7076 if (has_mbyte)
7077 {
7078 int old_State = State;
7079
7080 if (cap->ncharC1 != 0)
7081 AppendCharToRedobuff(cap->ncharC1);
7082 if (cap->ncharC2 != 0)
7083 AppendCharToRedobuff(cap->ncharC2);
7084
7085 /* This is slow, but it handles replacing a single-byte with a
7086 * multi-byte and the other way around. Also handles adding
7087 * composing characters for utf-8. */
7088 for (n = cap->count1; n > 0; --n)
7089 {
7090 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007091 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7092 {
7093 int c = ins_copychar(curwin->w_cursor.lnum
7094 + (cap->nchar == Ctrl_Y ? -1 : 1));
7095 if (c != NUL)
7096 ins_char(c);
7097 else
7098 /* will be decremented further down */
7099 ++curwin->w_cursor.col;
7100 }
7101 else
7102 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 State = old_State;
7104 if (cap->ncharC1 != 0)
7105 ins_char(cap->ncharC1);
7106 if (cap->ncharC2 != 0)
7107 ins_char(cap->ncharC2);
7108 }
7109 }
7110 else
7111#endif
7112 {
7113 /*
7114 * Replace the characters within one line.
7115 */
7116 for (n = cap->count1; n > 0; --n)
7117 {
7118 /*
7119 * Get ptr again, because u_save and/or showmatch() will have
7120 * released the line. At the same time we let know that the
7121 * line will be changed.
7122 */
7123 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007124 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7125 {
7126 int c = ins_copychar(curwin->w_cursor.lnum
7127 + (cap->nchar == Ctrl_Y ? -1 : 1));
7128 if (c != NUL)
7129 ptr[curwin->w_cursor.col] = c;
7130 }
7131 else
7132 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (p_sm && msg_silent == 0)
7134 showmatch(cap->nchar);
7135 ++curwin->w_cursor.col;
7136 }
7137#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007138 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007140 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141
Bram Moolenaar009b2592004-10-24 19:18:58 +00007142 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007143 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007145 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
7147#endif
7148
7149 /* mark the buffer as changed and prepare for displaying */
7150 changed_bytes(curwin->w_cursor.lnum,
7151 (colnr_T)(curwin->w_cursor.col - cap->count1));
7152 }
7153 --curwin->w_cursor.col; /* cursor on the last replaced char */
7154#ifdef FEAT_MBYTE
7155 /* if the character on the left of the current cursor is a multi-byte
7156 * character, move two characters left */
7157 if (has_mbyte)
7158 mb_adjust_cursor();
7159#endif
7160 curbuf->b_op_end = curwin->w_cursor;
7161 curwin->w_set_curswant = TRUE;
7162 set_last_insert(cap->nchar);
7163 }
7164}
7165
7166#ifdef FEAT_VISUAL
7167/*
7168 * 'o': Exchange start and end of Visual area.
7169 * 'O': same, but in block mode exchange left and right corners.
7170 */
7171 static void
7172v_swap_corners(cmdchar)
7173 int cmdchar;
7174{
7175 pos_T old_cursor;
7176 colnr_T left, right;
7177
7178 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7179 {
7180 old_cursor = curwin->w_cursor;
7181 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7182 curwin->w_cursor.lnum = VIsual.lnum;
7183 coladvance(left);
7184 VIsual = curwin->w_cursor;
7185
7186 curwin->w_cursor.lnum = old_cursor.lnum;
7187 curwin->w_curswant = right;
7188 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7189 * right one column */
7190 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7191 ++curwin->w_curswant;
7192 coladvance(curwin->w_curswant);
7193 if (curwin->w_cursor.col == old_cursor.col
7194#ifdef FEAT_VIRTUALEDIT
7195 && (!virtual_active()
7196 || curwin->w_cursor.coladd == old_cursor.coladd)
7197#endif
7198 )
7199 {
7200 curwin->w_cursor.lnum = VIsual.lnum;
7201 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7202 ++right;
7203 coladvance(right);
7204 VIsual = curwin->w_cursor;
7205
7206 curwin->w_cursor.lnum = old_cursor.lnum;
7207 coladvance(left);
7208 curwin->w_curswant = left;
7209 }
7210 }
7211 else
7212 {
7213 old_cursor = curwin->w_cursor;
7214 curwin->w_cursor = VIsual;
7215 VIsual = old_cursor;
7216 curwin->w_set_curswant = TRUE;
7217 }
7218}
7219#endif /* FEAT_VISUAL */
7220
7221/*
7222 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7223 */
7224 static void
7225nv_Replace(cap)
7226 cmdarg_T *cap;
7227{
7228#ifdef FEAT_VISUAL
7229 if (VIsual_active) /* "R" is replace lines */
7230 {
7231 cap->cmdchar = 'c';
7232 cap->nchar = NUL;
7233 VIsual_mode = 'V';
7234 nv_operator(cap);
7235 }
7236 else
7237#endif
7238 if (!checkclearopq(cap->oap))
7239 {
7240 if (!curbuf->b_p_ma)
7241 EMSG(_(e_modifiable));
7242 else
7243 {
7244#ifdef FEAT_VIRTUALEDIT
7245 if (virtual_active())
7246 coladvance(getviscol());
7247#endif
7248 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7249 }
7250 }
7251}
7252
7253#ifdef FEAT_VREPLACE
7254/*
7255 * "gr".
7256 */
7257 static void
7258nv_vreplace(cap)
7259 cmdarg_T *cap;
7260{
7261# ifdef FEAT_VISUAL
7262 if (VIsual_active)
7263 {
7264 cap->cmdchar = 'r';
7265 cap->nchar = cap->extra_char;
7266 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7267 }
7268 else
7269# endif
7270 if (!checkclearopq(cap->oap))
7271 {
7272 if (!curbuf->b_p_ma)
7273 EMSG(_(e_modifiable));
7274 else
7275 {
7276 if (cap->extra_char == Ctrl_V) /* get another character */
7277 cap->extra_char = get_literal();
7278 stuffcharReadbuff(cap->extra_char);
7279 stuffcharReadbuff(ESC);
7280# ifdef FEAT_VIRTUALEDIT
7281 if (virtual_active())
7282 coladvance(getviscol());
7283# endif
7284 invoke_edit(cap, TRUE, 'v', FALSE);
7285 }
7286 }
7287}
7288#endif
7289
7290/*
7291 * Swap case for "~" command, when it does not work like an operator.
7292 */
7293 static void
7294n_swapchar(cap)
7295 cmdarg_T *cap;
7296{
7297 long n;
7298 pos_T startpos;
7299 int did_change = 0;
7300#ifdef FEAT_NETBEANS_INTG
7301 pos_T pos;
7302 char_u *ptr;
7303 int count;
7304#endif
7305
7306 if (checkclearopq(cap->oap))
7307 return;
7308
7309 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7310 {
7311 clearopbeep(cap->oap);
7312 return;
7313 }
7314
7315 prep_redo_cmd(cap);
7316
7317 if (u_save_cursor() == FAIL)
7318 return;
7319
7320 startpos = curwin->w_cursor;
7321#ifdef FEAT_NETBEANS_INTG
7322 pos = startpos;
7323#endif
7324 for (n = cap->count1; n > 0; --n)
7325 {
7326 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7327 inc_cursor();
7328 if (gchar_cursor() == NUL)
7329 {
7330 if (vim_strchr(p_ww, '~') != NULL
7331 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7332 {
7333#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007334 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 if (did_change)
7337 {
7338 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007339 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007340 netbeans_removed(curbuf, pos.lnum, pos.col,
7341 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007343 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345 pos.col = 0;
7346 pos.lnum++;
7347 }
7348#endif
7349 ++curwin->w_cursor.lnum;
7350 curwin->w_cursor.col = 0;
7351 if (n > 1)
7352 {
7353 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7354 break;
7355 u_clearline();
7356 }
7357 }
7358 else
7359 break;
7360 }
7361 }
7362#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007363 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
7365 ptr = ml_get(pos.lnum);
7366 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007367 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7368 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370#endif
7371
7372
7373 check_cursor();
7374 curwin->w_set_curswant = TRUE;
7375 if (did_change)
7376 {
7377 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7378 0L);
7379 curbuf->b_op_start = startpos;
7380 curbuf->b_op_end = curwin->w_cursor;
7381 if (curbuf->b_op_end.col > 0)
7382 --curbuf->b_op_end.col;
7383 }
7384}
7385
7386/*
7387 * Move cursor to mark.
7388 */
7389 static void
7390nv_cursormark(cap, flag, pos)
7391 cmdarg_T *cap;
7392 int flag;
7393 pos_T *pos;
7394{
7395 if (check_mark(pos) == FAIL)
7396 clearop(cap->oap);
7397 else
7398 {
7399 if (cap->cmdchar == '\''
7400 || cap->cmdchar == '`'
7401 || cap->cmdchar == '['
7402 || cap->cmdchar == ']')
7403 setpcmark();
7404 curwin->w_cursor = *pos;
7405 if (flag)
7406 beginline(BL_WHITE | BL_FIX);
7407 else
7408 check_cursor();
7409 }
7410 cap->oap->motion_type = flag ? MLINE : MCHAR;
7411 if (cap->cmdchar == '`')
7412 cap->oap->use_reg_one = TRUE;
7413 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7414 curwin->w_set_curswant = TRUE;
7415}
7416
7417#ifdef FEAT_VISUAL
7418/*
7419 * Handle commands that are operators in Visual mode.
7420 */
7421 static void
7422v_visop(cap)
7423 cmdarg_T *cap;
7424{
7425 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7426
7427 /* Uppercase means linewise, except in block mode, then "D" deletes till
7428 * the end of the line, and "C" replaces til EOL */
7429 if (isupper(cap->cmdchar))
7430 {
7431 if (VIsual_mode != Ctrl_V)
7432 VIsual_mode = 'V';
7433 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7434 curwin->w_curswant = MAXCOL;
7435 }
7436 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7437 nv_operator(cap);
7438}
7439#endif
7440
7441/*
7442 * "s" and "S" commands.
7443 */
7444 static void
7445nv_subst(cap)
7446 cmdarg_T *cap;
7447{
7448#ifdef FEAT_VISUAL
7449 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7450 {
7451 if (cap->cmdchar == 'S')
7452 VIsual_mode = 'V';
7453 cap->cmdchar = 'c';
7454 nv_operator(cap);
7455 }
7456 else
7457#endif
7458 nv_optrans(cap);
7459}
7460
7461/*
7462 * Abbreviated commands.
7463 */
7464 static void
7465nv_abbrev(cap)
7466 cmdarg_T *cap;
7467{
7468 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7469 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7470
7471#ifdef FEAT_VISUAL
7472 /* in Visual mode these commands are operators */
7473 if (VIsual_active)
7474 v_visop(cap);
7475 else
7476#endif
7477 nv_optrans(cap);
7478}
7479
7480/*
7481 * Translate a command into another command.
7482 */
7483 static void
7484nv_optrans(cap)
7485 cmdarg_T *cap;
7486{
7487 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7488 (char_u *)"d$", (char_u *)"c$",
7489 (char_u *)"cl", (char_u *)"cc",
7490 (char_u *)"yy", (char_u *)":s\r"};
7491 static char_u *str = (char_u *)"xXDCsSY&";
7492
7493 if (!checkclearopq(cap->oap))
7494 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007495 /* In Vi "2D" doesn't delete the next line. Can't translate it
7496 * either, because "2." should also not use the count. */
7497 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7498 {
7499 cap->oap->start = curwin->w_cursor;
7500 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007501#ifdef FEAT_EVAL
7502 set_op_var(OP_DELETE);
7503#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007504 cap->count1 = 1;
7505 nv_dollar(cap);
7506 finish_op = TRUE;
7507 ResetRedobuff();
7508 AppendCharToRedobuff('D');
7509 }
7510 else
7511 {
7512 if (cap->count0)
7513 stuffnumReadbuff(cap->count0);
7514 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 cap->opcount = 0;
7518}
7519
7520/*
7521 * "'" and "`" commands. Also for "g'" and "g`".
7522 * cap->arg is TRUE for "'" and "g'".
7523 */
7524 static void
7525nv_gomark(cap)
7526 cmdarg_T *cap;
7527{
7528 pos_T *pos;
7529 int c;
7530#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007531 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7533#endif
7534
7535 if (cap->cmdchar == 'g')
7536 c = cap->extra_char;
7537 else
7538 c = cap->nchar;
7539 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7540 if (pos == (pos_T *)-1) /* jumped to other file */
7541 {
7542 if (cap->arg)
7543 {
7544 check_cursor_lnum();
7545 beginline(BL_WHITE | BL_FIX);
7546 }
7547 else
7548 check_cursor();
7549 }
7550 else
7551 nv_cursormark(cap, cap->arg, pos);
7552
7553#ifdef FEAT_VIRTUALEDIT
7554 /* May need to clear the coladd that a mark includes. */
7555 if (!virtual_active())
7556 curwin->w_cursor.coladd = 0;
7557#endif
7558#ifdef FEAT_FOLDING
7559 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007560 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007561 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 && (fdo_flags & FDO_MARK)
7563 && old_KeyTyped)
7564 foldOpenCursor();
7565#endif
7566}
7567
7568/*
7569 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7570 */
7571 static void
7572nv_pcmark(cap)
7573 cmdarg_T *cap;
7574{
7575#ifdef FEAT_JUMPLIST
7576 pos_T *pos;
7577# ifdef FEAT_FOLDING
7578 linenr_T lnum = curwin->w_cursor.lnum;
7579 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7580# endif
7581
7582 if (!checkclearopq(cap->oap))
7583 {
7584 if (cap->cmdchar == 'g')
7585 pos = movechangelist((int)cap->count1);
7586 else
7587 pos = movemark((int)cap->count1);
7588 if (pos == (pos_T *)-1) /* jump to other file */
7589 {
7590 curwin->w_set_curswant = TRUE;
7591 check_cursor();
7592 }
7593 else if (pos != NULL) /* can jump */
7594 nv_cursormark(cap, FALSE, pos);
7595 else if (cap->cmdchar == 'g')
7596 {
7597 if (curbuf->b_changelistlen == 0)
7598 EMSG(_("E664: changelist is empty"));
7599 else if (cap->count1 < 0)
7600 EMSG(_("E662: At start of changelist"));
7601 else
7602 EMSG(_("E663: At end of changelist"));
7603 }
7604 else
7605 clearopbeep(cap->oap);
7606# ifdef FEAT_FOLDING
7607 if (cap->oap->op_type == OP_NOP
7608 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7609 && (fdo_flags & FDO_MARK)
7610 && old_KeyTyped)
7611 foldOpenCursor();
7612# endif
7613 }
7614#else
7615 clearopbeep(cap->oap);
7616#endif
7617}
7618
7619/*
7620 * Handle '"' command.
7621 */
7622 static void
7623nv_regname(cap)
7624 cmdarg_T *cap;
7625{
7626 if (checkclearop(cap->oap))
7627 return;
7628#ifdef FEAT_EVAL
7629 if (cap->nchar == '=')
7630 cap->nchar = get_expr_register();
7631#endif
7632 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7633 {
7634 cap->oap->regname = cap->nchar;
7635 cap->opcount = cap->count0; /* remember count before '"' */
7636#ifdef FEAT_EVAL
7637 set_reg_var(cap->oap->regname);
7638#endif
7639 }
7640 else
7641 clearopbeep(cap->oap);
7642}
7643
7644#ifdef FEAT_VISUAL
7645/*
7646 * Handle "v", "V" and "CTRL-V" commands.
7647 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7648 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 */
7651 static void
7652nv_visual(cap)
7653 cmdarg_T *cap;
7654{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007655 if (cap->cmdchar == Ctrl_Q)
7656 cap->cmdchar = Ctrl_V;
7657
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7659 * characterwise, linewise, or blockwise. */
7660 if (cap->oap->op_type != OP_NOP)
7661 {
7662 cap->oap->motion_force = cap->cmdchar;
7663 finish_op = FALSE; /* operator doesn't finish now but later */
7664 return;
7665 }
7666
7667 VIsual_select = cap->arg;
7668 if (VIsual_active) /* change Visual mode */
7669 {
7670 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7671 end_visual_mode();
7672 else /* toggle char/block mode */
7673 { /* or char/line mode */
7674 VIsual_mode = cap->cmdchar;
7675 showmode();
7676 }
7677 redraw_curbuf_later(INVERTED); /* update the inversion */
7678 }
7679 else /* start Visual mode */
7680 {
7681 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007682 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007684 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 VIsual = curwin->w_cursor;
7686
7687 VIsual_active = TRUE;
7688 VIsual_reselect = TRUE;
7689 if (!cap->arg)
7690 /* start Select mode when 'selectmode' contains "cmd" */
7691 may_start_select('c');
7692#ifdef FEAT_MOUSE
7693 setmouse();
7694#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007695 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 redraw_cmdline = TRUE; /* show visual mode later */
7697 /*
7698 * For V and ^V, we multiply the number of lines even if there
7699 * was only one -- webb
7700 */
7701 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7702 {
7703 curwin->w_cursor.lnum +=
7704 resel_VIsual_line_count * cap->count0 - 1;
7705 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7706 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7707 }
7708 VIsual_mode = resel_VIsual_mode;
7709 if (VIsual_mode == 'v')
7710 {
7711 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007712 {
7713 validate_virtcol();
7714 curwin->w_curswant = curwin->w_virtcol
7715 + resel_VIsual_vcol * cap->count0 - 1;
7716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007718 curwin->w_curswant = resel_VIsual_vcol;
7719 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007721 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
7723 curwin->w_curswant = MAXCOL;
7724 coladvance((colnr_T)MAXCOL);
7725 }
7726 else if (VIsual_mode == Ctrl_V)
7727 {
7728 validate_virtcol();
7729 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007730 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 coladvance(curwin->w_curswant);
7732 }
7733 else
7734 curwin->w_set_curswant = TRUE;
7735 redraw_curbuf_later(INVERTED); /* show the inversion */
7736 }
7737 else
7738 {
7739 if (!cap->arg)
7740 /* start Select mode when 'selectmode' contains "cmd" */
7741 may_start_select('c');
7742 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007743 if (VIsual_mode != 'V' && *p_sel == 'e')
7744 ++cap->count1; /* include one more char */
7745 if (cap->count0 > 0 && --cap->count1 > 0)
7746 {
7747 /* With a count select that many characters or lines. */
7748 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7749 nv_right(cap);
7750 else if (VIsual_mode == 'V')
7751 nv_down(cap);
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 }
7755}
7756
7757/*
7758 * Start selection for Shift-movement keys.
7759 */
7760 void
7761start_selection()
7762{
7763 /* if 'selectmode' contains "key", start Select mode */
7764 may_start_select('k');
7765 n_start_visual_mode('v');
7766}
7767
7768/*
7769 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7770 */
7771 void
7772may_start_select(c)
7773 int c;
7774{
7775 VIsual_select = (stuff_empty() && typebuf_typed()
7776 && (vim_strchr(p_slm, c) != NULL));
7777}
7778
7779/*
7780 * Start Visual mode "c".
7781 * Should set VIsual_select before calling this.
7782 */
7783 static void
7784n_start_visual_mode(c)
7785 int c;
7786{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007787#ifdef FEAT_CONCEAL
7788 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007789 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007790#endif
7791
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 VIsual_mode = c;
7793 VIsual_active = TRUE;
7794 VIsual_reselect = TRUE;
7795#ifdef FEAT_VIRTUALEDIT
7796 /* Corner case: the 0 position in a tab may change when going into
7797 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7798 */
7799 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007800 {
7801 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804#endif
7805 VIsual = curwin->w_cursor;
7806
7807#ifdef FEAT_FOLDING
7808 foldAdjustVisual();
7809#endif
7810
7811#ifdef FEAT_MOUSE
7812 setmouse();
7813#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007814#ifdef FEAT_CONCEAL
7815 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007816 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007817#endif
7818
Bram Moolenaar09df3122006-01-23 22:23:09 +00007819 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 redraw_cmdline = TRUE; /* show visual mode later */
7821#ifdef FEAT_CLIPBOARD
7822 /* Make sure the clipboard gets updated. Needed because start and
7823 * end may still be the same, and the selection needs to be owned */
7824 clip_star.vmode = NUL;
7825#endif
7826
7827 /* Only need to redraw this line, unless still need to redraw an old
7828 * Visual area (when 'lazyredraw' is set). */
7829 if (curwin->w_redr_type < INVERTED)
7830 {
7831 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7832 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7833 }
7834}
7835
7836#endif /* FEAT_VISUAL */
7837
7838/*
7839 * CTRL-W: Window commands
7840 */
7841 static void
7842nv_window(cap)
7843 cmdarg_T *cap;
7844{
7845#ifdef FEAT_WINDOWS
7846 if (!checkclearop(cap->oap))
7847 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7848#else
7849 (void)checkclearop(cap->oap);
7850#endif
7851}
7852
7853/*
7854 * CTRL-Z: Suspend
7855 */
7856 static void
7857nv_suspend(cap)
7858 cmdarg_T *cap;
7859{
7860 clearop(cap->oap);
7861#ifdef FEAT_VISUAL
7862 if (VIsual_active)
7863 end_visual_mode(); /* stop Visual mode */
7864#endif
7865 do_cmdline_cmd((char_u *)"st");
7866}
7867
7868/*
7869 * Commands starting with "g".
7870 */
7871 static void
7872nv_g_cmd(cap)
7873 cmdarg_T *cap;
7874{
7875 oparg_T *oap = cap->oap;
7876#ifdef FEAT_VISUAL
7877 pos_T tpos;
7878#endif
7879 int i;
7880 int flag = FALSE;
7881
7882 switch (cap->nchar)
7883 {
7884#ifdef MEM_PROFILE
7885 /*
7886 * "g^A": dump log of used memory.
7887 */
7888 case Ctrl_A:
7889 vim_mem_profile_dump();
7890 break;
7891#endif
7892
7893#ifdef FEAT_VREPLACE
7894 /*
7895 * "gR": Enter virtual replace mode.
7896 */
7897 case 'R':
7898 cap->arg = TRUE;
7899 nv_Replace(cap);
7900 break;
7901
7902 case 'r':
7903 nv_vreplace(cap);
7904 break;
7905#endif
7906
7907 case '&':
7908 do_cmdline_cmd((char_u *)"%s//~/&");
7909 break;
7910
7911#ifdef FEAT_VISUAL
7912 /*
7913 * "gv": Reselect the previous Visual area. If Visual already active,
7914 * exchange previous and current Visual area.
7915 */
7916 case 'v':
7917 if (checkclearop(oap))
7918 break;
7919
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007920 if ( curbuf->b_visual.vi_start.lnum == 0
7921 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7922 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 beep_flush();
7924 else
7925 {
7926 /* set w_cursor to the start of the Visual area, tpos to the end */
7927 if (VIsual_active)
7928 {
7929 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007930 VIsual_mode = curbuf->b_visual.vi_mode;
7931 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932# ifdef FEAT_EVAL
7933 curbuf->b_visual_mode_eval = i;
7934# endif
7935 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007936 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7937 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007939 tpos = curbuf->b_visual.vi_end;
7940 curbuf->b_visual.vi_end = curwin->w_cursor;
7941 curwin->w_cursor = curbuf->b_visual.vi_start;
7942 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 }
7944 else
7945 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007946 VIsual_mode = curbuf->b_visual.vi_mode;
7947 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7948 tpos = curbuf->b_visual.vi_end;
7949 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 }
7951
7952 VIsual_active = TRUE;
7953 VIsual_reselect = TRUE;
7954
7955 /* Set Visual to the start and w_cursor to the end of the Visual
7956 * area. Make sure they are on an existing character. */
7957 check_cursor();
7958 VIsual = curwin->w_cursor;
7959 curwin->w_cursor = tpos;
7960 check_cursor();
7961 update_topline();
7962 /*
7963 * When called from normal "g" command: start Select mode when
7964 * 'selectmode' contains "cmd". When called for K_SELECT, always
7965 * start Select mode.
7966 */
7967 if (cap->arg)
7968 VIsual_select = TRUE;
7969 else
7970 may_start_select('c');
7971#ifdef FEAT_MOUSE
7972 setmouse();
7973#endif
7974#ifdef FEAT_CLIPBOARD
7975 /* Make sure the clipboard gets updated. Needed because start and
7976 * end are still the same, and the selection needs to be owned */
7977 clip_star.vmode = NUL;
7978#endif
7979 redraw_curbuf_later(INVERTED);
7980 showmode();
7981 }
7982 break;
7983 /*
7984 * "gV": Don't reselect the previous Visual area after a Select mode
7985 * mapping of menu.
7986 */
7987 case 'V':
7988 VIsual_reselect = FALSE;
7989 break;
7990
7991 /*
7992 * "gh": start Select mode.
7993 * "gH": start Select line mode.
7994 * "g^H": start Select block mode.
7995 */
7996 case K_BS:
7997 cap->nchar = Ctrl_H;
7998 /* FALLTHROUGH */
7999 case 'h':
8000 case 'H':
8001 case Ctrl_H:
8002# ifdef EBCDIC
8003 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8004 if (cap->nchar == Ctrl_H)
8005 cap->cmdchar = Ctrl_V;
8006 else
8007# endif
8008 cap->cmdchar = cap->nchar + ('v' - 'h');
8009 cap->arg = TRUE;
8010 nv_visual(cap);
8011 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008012#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008013
8014 /* "gn", "gN" visually select next/previous search match
8015 * "gn" selects next match
8016 * "gN" selects previous match
8017 */
8018 case 'N':
8019 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008020#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008021 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008022#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008023 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
8026 /*
8027 * "gj" and "gk" two new funny movement keys -- up and down
8028 * movement based on *screen* line rather than *file* line.
8029 */
8030 case 'j':
8031 case K_DOWN:
8032 /* with 'nowrap' it works just like the normal "j" command; also when
8033 * in a closed fold */
8034 if (!curwin->w_p_wrap
8035#ifdef FEAT_FOLDING
8036 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8037#endif
8038 )
8039 {
8040 oap->motion_type = MLINE;
8041 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8042 }
8043 else
8044 i = nv_screengo(oap, FORWARD, cap->count1);
8045 if (i == FAIL)
8046 clearopbeep(oap);
8047 break;
8048
8049 case 'k':
8050 case K_UP:
8051 /* with 'nowrap' it works just like the normal "k" command; also when
8052 * in a closed fold */
8053 if (!curwin->w_p_wrap
8054#ifdef FEAT_FOLDING
8055 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8056#endif
8057 )
8058 {
8059 oap->motion_type = MLINE;
8060 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8061 }
8062 else
8063 i = nv_screengo(oap, BACKWARD, cap->count1);
8064 if (i == FAIL)
8065 clearopbeep(oap);
8066 break;
8067
8068 /*
8069 * "gJ": join two lines without inserting a space.
8070 */
8071 case 'J':
8072 nv_join(cap);
8073 break;
8074
8075 /*
8076 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8077 * "gm": middle of "g0" and "g$".
8078 */
8079 case '^':
8080 flag = TRUE;
8081 /* FALLTHROUGH */
8082
8083 case '0':
8084 case 'm':
8085 case K_HOME:
8086 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 oap->motion_type = MCHAR;
8088 oap->inclusive = FALSE;
8089 if (curwin->w_p_wrap
8090#ifdef FEAT_VERTSPLIT
8091 && curwin->w_width != 0
8092#endif
8093 )
8094 {
8095 int width1 = W_WIDTH(curwin) - curwin_col_off();
8096 int width2 = width1 + curwin_col_off2();
8097
8098 validate_virtcol();
8099 i = 0;
8100 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8101 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8102 }
8103 else
8104 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008105 /* Go to the middle of the screen line. When 'number' or
8106 * 'relativenumber' is on and lines are wrapping the middle can be more
8107 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (cap->nchar == 'm')
8109 i += (W_WIDTH(curwin) - curwin_col_off()
8110 + ((curwin->w_p_wrap && i > 0)
8111 ? curwin_col_off2() : 0)) / 2;
8112 coladvance((colnr_T)i);
8113 if (flag)
8114 {
8115 do
8116 i = gchar_cursor();
8117 while (vim_iswhite(i) && oneright() == OK);
8118 }
8119 curwin->w_set_curswant = TRUE;
8120 break;
8121
8122 case '_':
8123 /* "g_": to the last non-blank character in the line or <count> lines
8124 * downward. */
8125 cap->oap->motion_type = MCHAR;
8126 cap->oap->inclusive = TRUE;
8127 curwin->w_curswant = MAXCOL;
8128 if (cursor_down((long)(cap->count1 - 1),
8129 cap->oap->op_type == OP_NOP) == FAIL)
8130 clearopbeep(cap->oap);
8131 else
8132 {
8133 char_u *ptr = ml_get_curline();
8134
8135 /* In Visual mode we may end up after the line. */
8136 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8137 --curwin->w_cursor.col;
8138
8139 /* Decrease the cursor column until it's on a non-blank. */
8140 while (curwin->w_cursor.col > 0
8141 && vim_iswhite(ptr[curwin->w_cursor.col]))
8142 --curwin->w_cursor.col;
8143 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008144#ifdef FEAT_VISUAL
8145 adjust_for_sel(cap);
8146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 break;
8149
8150 case '$':
8151 case K_END:
8152 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
8154 int col_off = curwin_col_off();
8155
8156 oap->motion_type = MCHAR;
8157 oap->inclusive = TRUE;
8158 if (curwin->w_p_wrap
8159#ifdef FEAT_VERTSPLIT
8160 && curwin->w_width != 0
8161#endif
8162 )
8163 {
8164 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8165 if (cap->count1 == 1)
8166 {
8167 int width1 = W_WIDTH(curwin) - col_off;
8168 int width2 = width1 + curwin_col_off2();
8169
8170 validate_virtcol();
8171 i = width1 - 1;
8172 if (curwin->w_virtcol >= (colnr_T)width1)
8173 i += ((curwin->w_virtcol - width1) / width2 + 1)
8174 * width2;
8175 coladvance((colnr_T)i);
8176#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8177 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8178 {
8179 /*
8180 * Check for landing on a character that got split at
8181 * the end of the line. We do not want to advance to
8182 * the next screen line.
8183 */
8184 validate_virtcol();
8185 if (curwin->w_virtcol > (colnr_T)i)
8186 --curwin->w_cursor.col;
8187 }
8188#endif
8189 }
8190 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8191 clearopbeep(oap);
8192 }
8193 else
8194 {
8195 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8196 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008197
8198 /* Make sure we stick in this column. */
8199 validate_virtcol();
8200 curwin->w_curswant = curwin->w_virtcol;
8201 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 }
8203 }
8204 break;
8205
8206 /*
8207 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8208 */
8209 case '*':
8210 case '#':
8211#if POUND != '#'
8212 case POUND: /* pound sign (sometimes equal to '#') */
8213#endif
8214 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8215 case ']': /* :tselect for current identifier */
8216 nv_ident(cap);
8217 break;
8218
8219 /*
8220 * ge and gE: go back to end of word
8221 */
8222 case 'e':
8223 case 'E':
8224 oap->motion_type = MCHAR;
8225 curwin->w_set_curswant = TRUE;
8226 oap->inclusive = TRUE;
8227 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8228 clearopbeep(oap);
8229 break;
8230
8231 /*
8232 * "g CTRL-G": display info about cursor position
8233 */
8234 case Ctrl_G:
8235 cursor_pos_info();
8236 break;
8237
8238 /*
8239 * "gi": start Insert at the last position.
8240 */
8241 case 'i':
8242 if (curbuf->b_last_insert.lnum != 0)
8243 {
8244 curwin->w_cursor = curbuf->b_last_insert;
8245 check_cursor_lnum();
8246 i = (int)STRLEN(ml_get_curline());
8247 if (curwin->w_cursor.col > (colnr_T)i)
8248 {
8249#ifdef FEAT_VIRTUALEDIT
8250 if (virtual_active())
8251 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8252#endif
8253 curwin->w_cursor.col = i;
8254 }
8255 }
8256 cap->cmdchar = 'i';
8257 nv_edit(cap);
8258 break;
8259
8260 /*
8261 * "gI": Start insert in column 1.
8262 */
8263 case 'I':
8264 beginline(0);
8265 if (!checkclearopq(oap))
8266 invoke_edit(cap, FALSE, 'g', FALSE);
8267 break;
8268
8269#ifdef FEAT_SEARCHPATH
8270 /*
8271 * "gf": goto file, edit file under cursor
8272 * "]f" and "[f": can also be used.
8273 */
8274 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008275 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 nv_gotofile(cap);
8277 break;
8278#endif
8279
8280 /* "g'm" and "g`m": jump to mark without setting pcmark */
8281 case '\'':
8282 cap->arg = TRUE;
8283 /*FALLTHROUGH*/
8284 case '`':
8285 nv_gomark(cap);
8286 break;
8287
8288 /*
8289 * "gs": Goto sleep.
8290 */
8291 case 's':
8292 do_sleep(cap->count1 * 1000L);
8293 break;
8294
8295 /*
8296 * "ga": Display the ascii value of the character under the
8297 * cursor. It is displayed in decimal, hex, and octal. -- webb
8298 */
8299 case 'a':
8300 do_ascii(NULL);
8301 break;
8302
8303#ifdef FEAT_MBYTE
8304 /*
8305 * "g8": Display the bytes used for the UTF-8 character under the
8306 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008307 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 */
8309 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008310 if (cap->count0 == 8)
8311 utf_find_illegal();
8312 else
8313 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 break;
8315#endif
8316
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008317 case '<':
8318 show_sb_text();
8319 break;
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 /*
8322 * "gg": Goto the first line in file. With a count it goes to
8323 * that line number like for "G". -- webb
8324 */
8325 case 'g':
8326 cap->arg = FALSE;
8327 nv_goto(cap);
8328 break;
8329
8330 /*
8331 * Two-character operators:
8332 * "gq" Format text
8333 * "gw" Format text and keep cursor position
8334 * "g~" Toggle the case of the text.
8335 * "gu" Change text to lower case.
8336 * "gU" Change text to upper case.
8337 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008338 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 */
8340 case 'q':
8341 case 'w':
8342 oap->cursor_start = curwin->w_cursor;
8343 /*FALLTHROUGH*/
8344 case '~':
8345 case 'u':
8346 case 'U':
8347 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008348 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 nv_operator(cap);
8350 break;
8351
8352 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008353 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 * current function
8355 * "gD": idem, but in the current file.
8356 */
8357 case 'd':
8358 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008359 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 break;
8361
8362#ifdef FEAT_MOUSE
8363 /*
8364 * g<*Mouse> : <C-*mouse>
8365 */
8366 case K_MIDDLEMOUSE:
8367 case K_MIDDLEDRAG:
8368 case K_MIDDLERELEASE:
8369 case K_LEFTMOUSE:
8370 case K_LEFTDRAG:
8371 case K_LEFTRELEASE:
8372 case K_RIGHTMOUSE:
8373 case K_RIGHTDRAG:
8374 case K_RIGHTRELEASE:
8375 case K_X1MOUSE:
8376 case K_X1DRAG:
8377 case K_X1RELEASE:
8378 case K_X2MOUSE:
8379 case K_X2DRAG:
8380 case K_X2RELEASE:
8381 mod_mask = MOD_MASK_CTRL;
8382 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8383 break;
8384#endif
8385
8386 case K_IGNORE:
8387 break;
8388
8389 /*
8390 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8391 */
8392 case 'p':
8393 case 'P':
8394 nv_put(cap);
8395 break;
8396
8397#ifdef FEAT_BYTEOFF
8398 /* "go": goto byte count from start of buffer */
8399 case 'o':
8400 goto_byte(cap->count0);
8401 break;
8402#endif
8403
8404 /* "gQ": improved Ex mode */
8405 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008406 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {
8408 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008409 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 break;
8411 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008412
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 if (!checkclearopq(oap))
8414 do_exmode(TRUE);
8415 break;
8416
8417#ifdef FEAT_JUMPLIST
8418 case ',':
8419 nv_pcmark(cap);
8420 break;
8421
8422 case ';':
8423 cap->count1 = -cap->count1;
8424 nv_pcmark(cap);
8425 break;
8426#endif
8427
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008428#ifdef FEAT_WINDOWS
8429 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008430 if (!checkclearop(oap))
8431 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008432 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008433 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008434 if (!checkclearop(oap))
8435 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008436 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008437#endif
8438
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008439 case '+':
8440 case '-': /* "g+" and "g-": undo or redo along the timeline */
8441 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008442 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008443 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008444 break;
8445
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 default:
8447 clearopbeep(oap);
8448 break;
8449 }
8450}
8451
8452/*
8453 * Handle "o" and "O" commands.
8454 */
8455 static void
8456n_opencmd(cap)
8457 cmdarg_T *cap;
8458{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008459#ifdef FEAT_CONCEAL
8460 linenr_T oldline = curwin->w_cursor.lnum;
8461#endif
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 if (!checkclearopq(cap->oap))
8464 {
8465#ifdef FEAT_FOLDING
8466 if (cap->cmdchar == 'O')
8467 /* Open above the first line of a folded sequence of lines */
8468 (void)hasFolding(curwin->w_cursor.lnum,
8469 &curwin->w_cursor.lnum, NULL);
8470 else
8471 /* Open below the last line of a folded sequence of lines */
8472 (void)hasFolding(curwin->w_cursor.lnum,
8473 NULL, &curwin->w_cursor.lnum);
8474#endif
8475 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8476 (cap->cmdchar == 'O' ? 1 : 0)),
8477 (linenr_T)(curwin->w_cursor.lnum +
8478 (cap->cmdchar == 'o' ? 1 : 0))
8479 ) == OK
8480 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8481#ifdef FEAT_COMMENTS
8482 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8483#endif
8484 0, 0))
8485 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008486#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008487 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008488 update_single_line(curwin, oldline);
8489#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008490 /* When '#' is in 'cpoptions' ignore the count. */
8491 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8492 cap->count1 = 1;
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 */
8539 if (cap->oap->op_type == OP_UPPER
8540#ifdef FEAT_VISUAL
8541 || VIsual_active
8542#endif
8543 )
8544 {
8545 /* translate "gUU" to "gUgU" */
8546 cap->cmdchar = 'g';
8547 cap->nchar = 'U';
8548 nv_operator(cap);
8549 }
8550 else if (!checkclearopq(cap->oap))
8551 {
8552 u_undoline();
8553 curwin->w_set_curswant = TRUE;
8554 }
8555}
8556
8557/*
8558 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8559 * single character.
8560 */
8561 static void
8562nv_tilde(cap)
8563 cmdarg_T *cap;
8564{
8565 if (!p_to
8566#ifdef FEAT_VISUAL
8567 && !VIsual_active
8568#endif
8569 && cap->oap->op_type != OP_TILDE)
8570 n_swapchar(cap);
8571 else
8572 nv_operator(cap);
8573}
8574
8575/*
8576 * Handle an operator command.
8577 * The actual work is done by do_pending_operator().
8578 */
8579 static void
8580nv_operator(cap)
8581 cmdarg_T *cap;
8582{
8583 int op_type;
8584
8585 op_type = get_op_type(cap->cmdchar, cap->nchar);
8586
8587 if (op_type == cap->oap->op_type) /* double operator works on lines */
8588 nv_lineop(cap);
8589 else if (!checkclearop(cap->oap))
8590 {
8591 cap->oap->start = curwin->w_cursor;
8592 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008593#ifdef FEAT_EVAL
8594 set_op_var(op_type);
8595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597}
8598
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008599#ifdef FEAT_EVAL
8600/*
8601 * Set v:operator to the characters for "optype".
8602 */
8603 static void
8604set_op_var(optype)
8605 int optype;
8606{
8607 char_u opchars[3];
8608
8609 if (optype == OP_NOP)
8610 set_vim_var_string(VV_OP, NULL, 0);
8611 else
8612 {
8613 opchars[0] = get_op_char(optype);
8614 opchars[1] = get_extra_op_char(optype);
8615 opchars[2] = NUL;
8616 set_vim_var_string(VV_OP, opchars, -1);
8617 }
8618}
8619#endif
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*
8622 * Handle linewise operator "dd", "yy", etc.
8623 *
8624 * "_" is is a strange motion command that helps make operators more logical.
8625 * It is actually implemented, but not documented in the real Vi. This motion
8626 * command actually refers to "the current line". Commands like "dd" and "yy"
8627 * are really an alternate form of "d_" and "y_". It does accept a count, so
8628 * "d3_" works to delete 3 lines.
8629 */
8630 static void
8631nv_lineop(cap)
8632 cmdarg_T *cap;
8633{
8634 cap->oap->motion_type = MLINE;
8635 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8636 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008637 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8638 && cap->oap->motion_force != 'v'
8639 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 || cap->oap->op_type == OP_LSHIFT
8641 || cap->oap->op_type == OP_RSHIFT)
8642 beginline(BL_SOL | BL_FIX);
8643 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8644 beginline(BL_WHITE | BL_FIX);
8645}
8646
8647/*
8648 * <Home> command.
8649 */
8650 static void
8651nv_home(cap)
8652 cmdarg_T *cap;
8653{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008654 /* CTRL-HOME is like "gg" */
8655 if (mod_mask & MOD_MASK_CTRL)
8656 nv_goto(cap);
8657 else
8658 {
8659 cap->count0 = 1;
8660 nv_pipe(cap);
8661 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008662 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8663 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664}
8665
8666/*
8667 * "|" command.
8668 */
8669 static void
8670nv_pipe(cap)
8671 cmdarg_T *cap;
8672{
8673 cap->oap->motion_type = MCHAR;
8674 cap->oap->inclusive = FALSE;
8675 beginline(0);
8676 if (cap->count0 > 0)
8677 {
8678 coladvance((colnr_T)(cap->count0 - 1));
8679 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8680 }
8681 else
8682 curwin->w_curswant = 0;
8683 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008684 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 curwin->w_set_curswant = FALSE;
8686}
8687
8688/*
8689 * Handle back-word command "b" and "B".
8690 * cap->arg is 1 for "B"
8691 */
8692 static void
8693nv_bck_word(cap)
8694 cmdarg_T *cap;
8695{
8696 cap->oap->motion_type = MCHAR;
8697 cap->oap->inclusive = FALSE;
8698 curwin->w_set_curswant = TRUE;
8699 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8700 clearopbeep(cap->oap);
8701#ifdef FEAT_FOLDING
8702 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8703 foldOpenCursor();
8704#endif
8705}
8706
8707/*
8708 * Handle word motion commands "e", "E", "w" and "W".
8709 * cap->arg is TRUE for "E" and "W".
8710 */
8711 static void
8712nv_wordcmd(cap)
8713 cmdarg_T *cap;
8714{
8715 int n;
8716 int word_end;
8717 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008718 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 /*
8721 * Set inclusive for the "E" and "e" command.
8722 */
8723 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8724 word_end = TRUE;
8725 else
8726 word_end = FALSE;
8727 cap->oap->inclusive = word_end;
8728
8729 /*
8730 * "cw" and "cW" are a special case.
8731 */
8732 if (!word_end && cap->oap->op_type == OP_CHANGE)
8733 {
8734 n = gchar_cursor();
8735 if (n != NUL) /* not an empty line */
8736 {
8737 if (vim_iswhite(n))
8738 {
8739 /*
8740 * Reproduce a funny Vi behaviour: "cw" on a blank only
8741 * changes one character, not all blanks until the start of
8742 * the next word. Only do this when the 'w' flag is included
8743 * in 'cpoptions'.
8744 */
8745 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8746 {
8747 cap->oap->inclusive = TRUE;
8748 cap->oap->motion_type = MCHAR;
8749 return;
8750 }
8751 }
8752 else
8753 {
8754 /*
8755 * This is a little strange. To match what the real Vi does,
8756 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8757 * that we are not on a space or a TAB. This seems impolite
8758 * at first, but it's really more what we mean when we say
8759 * 'cw'.
8760 * Another strangeness: When standing on the end of a word
8761 * "ce" will change until the end of the next wordt, but "cw"
8762 * will change only one character! This is done by setting
8763 * flag.
8764 */
8765 cap->oap->inclusive = TRUE;
8766 word_end = TRUE;
8767 flag = TRUE;
8768 }
8769 }
8770 }
8771
8772 cap->oap->motion_type = MCHAR;
8773 curwin->w_set_curswant = TRUE;
8774 if (word_end)
8775 n = end_word(cap->count1, cap->arg, flag, FALSE);
8776 else
8777 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8778
Bram Moolenaardfefb982008-04-01 10:06:39 +00008779 /* Don't leave the cursor on the NUL past the end of line. Unless we
8780 * didn't move it forward. */
8781 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008782 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 if (n == FAIL && cap->oap->op_type == OP_NOP)
8785 clearopbeep(cap->oap);
8786 else
8787 {
8788#ifdef FEAT_VISUAL
8789 adjust_for_sel(cap);
8790#endif
8791#ifdef FEAT_FOLDING
8792 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8793 foldOpenCursor();
8794#endif
8795 }
8796}
8797
8798/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008799 * Used after a movement command: If the cursor ends up on the NUL after the
8800 * end of the line, may move it back to the last character and make the motion
8801 * inclusive.
8802 */
8803 static void
8804adjust_cursor(oap)
8805 oparg_T *oap;
8806{
8807 /* The cursor cannot remain on the NUL when:
8808 * - the column is > 0
8809 * - not in Visual mode or 'selection' is "o"
8810 * - 'virtualedit' is not "all" and not "onemore".
8811 */
8812 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8813#ifdef FEAT_VISUAL
8814 && (!VIsual_active || *p_sel == 'o')
8815#endif
8816#ifdef FEAT_VIRTUALEDIT
8817 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8818#endif
8819 )
8820 {
8821 --curwin->w_cursor.col;
8822#ifdef FEAT_MBYTE
8823 /* prevent cursor from moving on the trail byte */
8824 if (has_mbyte)
8825 mb_adjust_cursor();
8826#endif
8827 oap->inclusive = TRUE;
8828 }
8829}
8830
8831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 * "0" and "^" commands.
8833 * cap->arg is the argument for beginline().
8834 */
8835 static void
8836nv_beginline(cap)
8837 cmdarg_T *cap;
8838{
8839 cap->oap->motion_type = MCHAR;
8840 cap->oap->inclusive = FALSE;
8841 beginline(cap->arg);
8842#ifdef FEAT_FOLDING
8843 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8844 foldOpenCursor();
8845#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008846 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8847 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
8850#ifdef FEAT_VISUAL
8851/*
8852 * In exclusive Visual mode, may include the last character.
8853 */
8854 static void
8855adjust_for_sel(cap)
8856 cmdarg_T *cap;
8857{
8858 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8859 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8860 {
8861# ifdef FEAT_MBYTE
8862 if (has_mbyte)
8863 inc_cursor();
8864 else
8865# endif
8866 ++curwin->w_cursor.col;
8867 cap->oap->inclusive = FALSE;
8868 }
8869}
8870
8871/*
8872 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8873 * Should check VIsual_mode before calling this.
8874 * Returns TRUE when backed up to the previous line.
8875 */
8876 static int
8877unadjust_for_sel()
8878{
8879 pos_T *pp;
8880
8881 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8882 {
8883 if (lt(VIsual, curwin->w_cursor))
8884 pp = &curwin->w_cursor;
8885 else
8886 pp = &VIsual;
8887#ifdef FEAT_VIRTUALEDIT
8888 if (pp->coladd > 0)
8889 --pp->coladd;
8890 else
8891#endif
8892 if (pp->col > 0)
8893 {
8894 --pp->col;
8895#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008896 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897#endif
8898 }
8899 else if (pp->lnum > 1)
8900 {
8901 --pp->lnum;
8902 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8903 return TRUE;
8904 }
8905 }
8906 return FALSE;
8907}
8908
8909/*
8910 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8911 */
8912 static void
8913nv_select(cap)
8914 cmdarg_T *cap;
8915{
8916 if (VIsual_active)
8917 VIsual_select = TRUE;
8918 else if (VIsual_reselect)
8919 {
8920 cap->nchar = 'v'; /* fake "gv" command */
8921 cap->arg = TRUE;
8922 nv_g_cmd(cap);
8923 }
8924}
8925
8926#endif
8927
8928/*
8929 * "G", "gg", CTRL-END, CTRL-HOME.
8930 * cap->arg is TRUE for "G".
8931 */
8932 static void
8933nv_goto(cap)
8934 cmdarg_T *cap;
8935{
8936 linenr_T lnum;
8937
8938 if (cap->arg)
8939 lnum = curbuf->b_ml.ml_line_count;
8940 else
8941 lnum = 1L;
8942 cap->oap->motion_type = MLINE;
8943 setpcmark();
8944
8945 /* When a count is given, use it instead of the default lnum */
8946 if (cap->count0 != 0)
8947 lnum = cap->count0;
8948 if (lnum < 1L)
8949 lnum = 1L;
8950 else if (lnum > curbuf->b_ml.ml_line_count)
8951 lnum = curbuf->b_ml.ml_line_count;
8952 curwin->w_cursor.lnum = lnum;
8953 beginline(BL_SOL | BL_FIX);
8954#ifdef FEAT_FOLDING
8955 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8956 foldOpenCursor();
8957#endif
8958}
8959
8960/*
8961 * CTRL-\ in Normal mode.
8962 */
8963 static void
8964nv_normal(cap)
8965 cmdarg_T *cap;
8966{
8967 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8968 {
8969 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008970 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 clear_cmdline = TRUE; /* unshow mode later */
8972 restart_edit = 0;
8973#ifdef FEAT_CMDWIN
8974 if (cmdwin_type != 0)
8975 cmdwin_result = Ctrl_C;
8976#endif
8977#ifdef FEAT_VISUAL
8978 if (VIsual_active)
8979 {
8980 end_visual_mode(); /* stop Visual */
8981 redraw_curbuf_later(INVERTED);
8982 }
8983#endif
8984 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8985 if (cap->nchar == Ctrl_G && p_im)
8986 restart_edit = 'a';
8987 }
8988 else
8989 clearopbeep(cap->oap);
8990}
8991
8992/*
8993 * ESC in Normal mode: beep, but don't flush buffers.
8994 * Don't even beep if we are canceling a command.
8995 */
8996 static void
8997nv_esc(cap)
8998 cmdarg_T *cap;
8999{
9000 int no_reason;
9001
9002 no_reason = (cap->oap->op_type == OP_NOP
9003 && cap->opcount == 0
9004 && cap->count0 == 0
9005 && cap->oap->regname == 0
9006 && !p_im);
9007
9008 if (cap->arg) /* TRUE for CTRL-C */
9009 {
9010 if (restart_edit == 0
9011#ifdef FEAT_CMDWIN
9012 && cmdwin_type == 0
9013#endif
9014#ifdef FEAT_VISUAL
9015 && !VIsual_active
9016#endif
9017 && no_reason)
9018 MSG(_("Type :quit<Enter> to exit Vim"));
9019
9020 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9021 * set again below when halfway a mapping. */
9022 if (!p_im)
9023 restart_edit = 0;
9024#ifdef FEAT_CMDWIN
9025 if (cmdwin_type != 0)
9026 {
9027 cmdwin_result = K_IGNORE;
9028 got_int = FALSE; /* don't stop executing autocommands et al. */
9029 return;
9030 }
9031#endif
9032 }
9033
9034#ifdef FEAT_VISUAL
9035 if (VIsual_active)
9036 {
9037 end_visual_mode(); /* stop Visual */
9038 check_cursor_col(); /* make sure cursor is not beyond EOL */
9039 curwin->w_set_curswant = TRUE;
9040 redraw_curbuf_later(INVERTED);
9041 }
9042 else
9043#endif
9044 if (no_reason)
9045 vim_beep();
9046 clearop(cap->oap);
9047
9048 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9049 * set return to Insert mode afterwards. */
9050 if (restart_edit == 0 && goto_im()
9051#ifdef FEAT_EX_EXTRA
9052 && ex_normal_busy == 0
9053#endif
9054 )
9055 restart_edit = 'a';
9056}
9057
9058/*
9059 * Handle "A", "a", "I", "i" and <Insert> commands.
9060 */
9061 static void
9062nv_edit(cap)
9063 cmdarg_T *cap;
9064{
9065 /* <Insert> is equal to "i" */
9066 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9067 cap->cmdchar = 'i';
9068
9069#ifdef FEAT_VISUAL
9070 /* in Visual mode "A" and "I" are an operator */
9071 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9072 v_visop(cap);
9073
9074 /* in Visual mode and after an operator "a" and "i" are for text objects */
9075 else
9076#endif
9077 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9078 && (cap->oap->op_type != OP_NOP
9079#ifdef FEAT_VISUAL
9080 || VIsual_active
9081#endif
9082 ))
9083 {
9084#ifdef FEAT_TEXTOBJ
9085 nv_object(cap);
9086#else
9087 clearopbeep(cap->oap);
9088#endif
9089 }
9090 else if (!curbuf->b_p_ma && !p_im)
9091 {
9092 /* Only give this error when 'insertmode' is off. */
9093 EMSG(_(e_modifiable));
9094 clearop(cap->oap);
9095 }
9096 else if (!checkclearopq(cap->oap))
9097 {
9098 switch (cap->cmdchar)
9099 {
9100 case 'A': /* "A"ppend after the line */
9101 curwin->w_set_curswant = TRUE;
9102#ifdef FEAT_VIRTUALEDIT
9103 if (ve_flags == VE_ALL)
9104 {
9105 int save_State = State;
9106
9107 /* Pretent Insert mode here to allow the cursor on the
9108 * character past the end of the line */
9109 State = INSERT;
9110 coladvance((colnr_T)MAXCOL);
9111 State = save_State;
9112 }
9113 else
9114#endif
9115 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9116 break;
9117
9118 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009119 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9120 beginline(BL_WHITE);
9121 else
9122 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 break;
9124
9125 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9126#ifdef FEAT_VIRTUALEDIT
9127 /* increment coladd when in virtual space, increment the
9128 * column otherwise, also to append after an unprintable char */
9129 if (virtual_active()
9130 && (curwin->w_cursor.coladd > 0
9131 || *ml_get_cursor() == NUL
9132 || *ml_get_cursor() == TAB))
9133 curwin->w_cursor.coladd++;
9134 else
9135#endif
9136 if (*ml_get_cursor() != NUL)
9137 inc_cursor();
9138 break;
9139 }
9140
9141#ifdef FEAT_VIRTUALEDIT
9142 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9143 {
9144 int save_State = State;
9145
9146 /* Pretent Insert mode here to allow the cursor on the
9147 * character past the end of the line */
9148 State = INSERT;
9149 coladvance(getviscol());
9150 State = save_State;
9151 }
9152#endif
9153
9154 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9155 }
9156}
9157
9158/*
9159 * Invoke edit() and take care of "restart_edit" and the return value.
9160 */
9161 static void
9162invoke_edit(cap, repl, cmd, startln)
9163 cmdarg_T *cap;
9164 int repl; /* "r" or "gr" command */
9165 int cmd;
9166 int startln;
9167{
9168 int restart_edit_save = 0;
9169
9170 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9171 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9172 * it. */
9173 if (repl || !stuff_empty())
9174 restart_edit_save = restart_edit;
9175 else
9176 restart_edit_save = 0;
9177
9178 /* Always reset "restart_edit", this is not a restarted edit. */
9179 restart_edit = 0;
9180
9181 if (edit(cmd, startln, cap->count1))
9182 cap->retval |= CA_COMMAND_BUSY;
9183
9184 if (restart_edit == 0)
9185 restart_edit = restart_edit_save;
9186}
9187
9188#ifdef FEAT_TEXTOBJ
9189/*
9190 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9191 */
9192 static void
9193nv_object(cap)
9194 cmdarg_T *cap;
9195{
9196 int flag;
9197 int include;
9198 char_u *mps_save;
9199
9200 if (cap->cmdchar == 'i')
9201 include = FALSE; /* "ix" = inner object: exclude white space */
9202 else
9203 include = TRUE; /* "ax" = an object: include white space */
9204
9205 /* Make sure (), [], {} and <> are in 'matchpairs' */
9206 mps_save = curbuf->b_p_mps;
9207 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9208
9209 switch (cap->nchar)
9210 {
9211 case 'w': /* "aw" = a word */
9212 flag = current_word(cap->oap, cap->count1, include, FALSE);
9213 break;
9214 case 'W': /* "aW" = a WORD */
9215 flag = current_word(cap->oap, cap->count1, include, TRUE);
9216 break;
9217 case 'b': /* "ab" = a braces block */
9218 case '(':
9219 case ')':
9220 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9221 break;
9222 case 'B': /* "aB" = a Brackets block */
9223 case '{':
9224 case '}':
9225 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9226 break;
9227 case '[': /* "a[" = a [] block */
9228 case ']':
9229 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9230 break;
9231 case '<': /* "a<" = a <> block */
9232 case '>':
9233 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9234 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009235 case 't': /* "at" = a tag block (xml and html) */
9236 flag = current_tagblock(cap->oap, cap->count1, include);
9237 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 case 'p': /* "ap" = a paragraph */
9239 flag = current_par(cap->oap, cap->count1, include, 'p');
9240 break;
9241 case 's': /* "as" = a sentence */
9242 flag = current_sent(cap->oap, cap->count1, include);
9243 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009244 case '"': /* "a"" = a double quoted string */
9245 case '\'': /* "a'" = a single quoted string */
9246 case '`': /* "a`" = a backtick quoted string */
9247 flag = current_quote(cap->oap, cap->count1, include,
9248 cap->nchar);
9249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#if 0 /* TODO */
9251 case 'S': /* "aS" = a section */
9252 case 'f': /* "af" = a filename */
9253 case 'u': /* "au" = a URL */
9254#endif
9255 default:
9256 flag = FAIL;
9257 break;
9258 }
9259
9260 curbuf->b_p_mps = mps_save;
9261 if (flag == FAIL)
9262 clearopbeep(cap->oap);
9263 adjust_cursor_col();
9264 curwin->w_set_curswant = TRUE;
9265}
9266#endif
9267
9268/*
9269 * "q" command: Start/stop recording.
9270 * "q:", "q/", "q?": edit command-line in command-line window.
9271 */
9272 static void
9273nv_record(cap)
9274 cmdarg_T *cap;
9275{
9276 if (cap->oap->op_type == OP_FORMAT)
9277 {
9278 /* "gqq" is the same as "gqgq": format line */
9279 cap->cmdchar = 'g';
9280 cap->nchar = 'q';
9281 nv_operator(cap);
9282 }
9283 else if (!checkclearop(cap->oap))
9284 {
9285#ifdef FEAT_CMDWIN
9286 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9287 {
9288 stuffcharReadbuff(cap->nchar);
9289 stuffcharReadbuff(K_CMDWIN);
9290 }
9291 else
9292#endif
9293 /* (stop) recording into a named register, unless executing a
9294 * register */
9295 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9296 clearopbeep(cap->oap);
9297 }
9298}
9299
9300/*
9301 * Handle the "@r" command.
9302 */
9303 static void
9304nv_at(cap)
9305 cmdarg_T *cap;
9306{
9307 if (checkclearop(cap->oap))
9308 return;
9309#ifdef FEAT_EVAL
9310 if (cap->nchar == '=')
9311 {
9312 if (get_expr_register() == NUL)
9313 return;
9314 }
9315#endif
9316 while (cap->count1-- && !got_int)
9317 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009318 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 {
9320 clearopbeep(cap->oap);
9321 break;
9322 }
9323 line_breakcheck();
9324 }
9325}
9326
9327/*
9328 * Handle the CTRL-U and CTRL-D commands.
9329 */
9330 static void
9331nv_halfpage(cap)
9332 cmdarg_T *cap;
9333{
9334 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9335 || (cap->cmdchar == Ctrl_D
9336 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9337 clearopbeep(cap->oap);
9338 else if (!checkclearop(cap->oap))
9339 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9340}
9341
9342/*
9343 * Handle "J" or "gJ" command.
9344 */
9345 static void
9346nv_join(cap)
9347 cmdarg_T *cap;
9348{
9349#ifdef FEAT_VISUAL
9350 if (VIsual_active) /* join the visual lines */
9351 nv_operator(cap);
9352 else
9353#endif
9354 if (!checkclearop(cap->oap))
9355 {
9356 if (cap->count0 <= 1)
9357 cap->count0 = 2; /* default for join is two lines! */
9358 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9359 curbuf->b_ml.ml_line_count)
9360 clearopbeep(cap->oap); /* beyond last line */
9361 else
9362 {
9363 prep_redo(cap->oap->regname, cap->count0,
9364 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009365 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 }
9367 }
9368}
9369
9370/*
9371 * "P", "gP", "p" and "gp" commands.
9372 */
9373 static void
9374nv_put(cap)
9375 cmdarg_T *cap;
9376{
9377#ifdef FEAT_VISUAL
9378 int regname = 0;
9379 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009380 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009381 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382#endif
9383 int dir;
9384 int flags = 0;
9385
9386 if (cap->oap->op_type != OP_NOP)
9387 {
9388#ifdef FEAT_DIFF
9389 /* "dp" is ":diffput" */
9390 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9391 {
9392 clearop(cap->oap);
9393 nv_diffgetput(TRUE);
9394 }
9395 else
9396#endif
9397 clearopbeep(cap->oap);
9398 }
9399 else
9400 {
9401 dir = (cap->cmdchar == 'P'
9402 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9403 ? BACKWARD : FORWARD;
9404 prep_redo_cmd(cap);
9405 if (cap->cmdchar == 'g')
9406 flags |= PUT_CURSEND;
9407
9408#ifdef FEAT_VISUAL
9409 if (VIsual_active)
9410 {
9411 /* Putting in Visual mode: The put text replaces the selected
9412 * text. First delete the selected text, then put the new text.
9413 * Need to save and restore the registers that the delete
9414 * overwrites if the old contents is being put.
9415 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009416 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 regname = cap->oap->regname;
9418# ifdef FEAT_CLIPBOARD
9419 adjust_clip_reg(&regname);
9420# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009421 if (regname == 0 || regname == '"'
9422 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423# ifdef FEAT_CLIPBOARD
9424 || (clip_unnamed && (regname == '*' || regname == '+'))
9425# endif
9426
9427 )
9428 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009429 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 * put, save it first. */
9431 reg1 = get_register(regname, TRUE);
9432 }
9433
9434 /* Now delete the selected text. */
9435 cap->cmdchar = 'd';
9436 cap->nchar = NUL;
9437 cap->oap->regname = NUL;
9438 nv_operator(cap);
9439 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009440 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441
9442 /* delete PUT_LINE_BACKWARD; */
9443 cap->oap->regname = regname;
9444
9445 if (reg1 != NULL)
9446 {
9447 /* Delete probably changed the register we want to put, save
9448 * it first. Then put back what was there before the delete. */
9449 reg2 = get_register(regname, FALSE);
9450 put_register(regname, reg1);
9451 }
9452
9453 /* When deleted a linewise Visual area, put the register as
9454 * lines to avoid it joined with the next line. When deletion was
9455 * characterwise, split a line when putting lines. */
9456 if (VIsual_mode == 'V')
9457 flags |= PUT_LINE;
9458 else if (VIsual_mode == 'v')
9459 flags |= PUT_LINE_SPLIT;
9460 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9461 flags |= PUT_LINE_FORWARD;
9462 dir = BACKWARD;
9463 if ((VIsual_mode != 'V'
9464 && curwin->w_cursor.col < curbuf->b_op_start.col)
9465 || (VIsual_mode == 'V'
9466 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9467 /* cursor is at the end of the line or end of file, put
9468 * forward. */
9469 dir = FORWARD;
9470 }
9471#endif
9472 do_put(cap->oap->regname, dir, cap->count1, flags);
9473
9474#ifdef FEAT_VISUAL
9475 /* If a register was saved, put it back now. */
9476 if (reg2 != NULL)
9477 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009478
9479 /* What to reselect with "gv"? Selecting the just put text seems to
9480 * be the most useful, since the original text was removed. */
9481 if (was_visual)
9482 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009483 curbuf->b_visual.vi_start = curbuf->b_op_start;
9484 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009485 }
9486
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009487 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009488 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009489 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009490 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009491 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009492
9493 /* If the cursor was in that line, move it to the end of the last
9494 * line. */
9495 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9496 {
9497 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9498 coladvance((colnr_T)MAXCOL);
9499 }
9500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#endif
9502 auto_format(FALSE, TRUE);
9503 }
9504}
9505
9506/*
9507 * "o" and "O" commands.
9508 */
9509 static void
9510nv_open(cap)
9511 cmdarg_T *cap;
9512{
9513#ifdef FEAT_DIFF
9514 /* "do" is ":diffget" */
9515 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9516 {
9517 clearop(cap->oap);
9518 nv_diffgetput(FALSE);
9519 }
9520 else
9521#endif
9522#ifdef FEAT_VISUAL
9523 if (VIsual_active) /* switch start and end of visual */
9524 v_swap_corners(cap->cmdchar);
9525 else
9526#endif
9527 n_opencmd(cap);
9528}
9529
9530#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 static void
9532nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009533 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 ProcessSniffRequests();
9536}
9537#endif
9538
9539#ifdef FEAT_NETBEANS_INTG
9540 static void
9541nv_nbcmd(cap)
9542 cmdarg_T *cap;
9543{
9544 netbeans_keycommand(cap->nchar);
9545}
9546#endif
9547
9548#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 static void
9550nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009551 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9554}
9555#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009556
9557#ifdef FEAT_AUTOCMD
9558/*
9559 * Trigger CursorHold event.
9560 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9561 * input buffer. "did_cursorhold" is set to avoid retriggering.
9562 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009563 static void
9564nv_cursorhold(cap)
9565 cmdarg_T *cap;
9566{
9567 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9568 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009569 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009570}
9571#endif