blob: 869ada37af8eb6209048d5e804d833e5a2a2fd5f [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 Moolenaar61d281a2012-03-28 12:59:57 +02002295 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002296
2297 if (*p_opfunc == NUL)
2298 EMSG(_("E774: 'operatorfunc' is empty"));
2299 else
2300 {
2301 /* Set '[ and '] marks to text to be operated on. */
2302 curbuf->b_op_start = oap->start;
2303 curbuf->b_op_end = oap->end;
2304 if (oap->motion_type != MLINE && !oap->inclusive)
2305 /* Exclude the end position. */
2306 decl(&curbuf->b_op_end);
2307
2308 if (oap->block_mode)
2309 argv[0] = (char_u *)"block";
2310 else if (oap->motion_type == MLINE)
2311 argv[0] = (char_u *)"line";
2312 else
2313 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002314
2315 /* Reset virtual_op so that 'virtualedit' can be changed in the
2316 * function. */
2317 virtual_op = MAYBE;
2318
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002319 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002320
2321 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002322 }
2323#else
2324 EMSG(_("E775: Eval feature not available"));
2325#endif
2326}
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328#if defined(FEAT_MOUSE) || defined(PROTO)
2329/*
2330 * Do the appropriate action for the current mouse click in the current mode.
2331 * Not used for Command-line mode.
2332 *
2333 * Normal Mode:
2334 * event modi- position visual change action
2335 * fier cursor window
2336 * left press - yes end yes
2337 * left press C yes end yes "^]" (2)
2338 * left press S yes end yes "*" (2)
2339 * left drag - yes start if moved no
2340 * left relse - yes start if moved no
2341 * middle press - yes if not active no put register
2342 * middle press - yes if active no yank and put
2343 * right press - yes start or extend yes
2344 * right press S yes no change yes "#" (2)
2345 * right drag - yes extend no
2346 * right relse - yes extend no
2347 *
2348 * Insert or Replace Mode:
2349 * event modi- position visual change action
2350 * fier cursor window
2351 * left press - yes (cannot be active) yes
2352 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2353 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2354 * left drag - yes start or extend (1) no CTRL-O (1)
2355 * left relse - yes start or extend (1) no CTRL-O (1)
2356 * middle press - no (cannot be active) no put register
2357 * right press - yes start or extend yes CTRL-O
2358 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2359 *
2360 * (1) only if mouse pointer moved since press
2361 * (2) only if click is in same buffer
2362 *
2363 * Return TRUE if start_arrow() should be called for edit mode.
2364 */
2365 int
2366do_mouse(oap, c, dir, count, fixindent)
2367 oparg_T *oap; /* operator argument, can be NULL */
2368 int c; /* K_LEFTMOUSE, etc */
2369 int dir; /* Direction to 'put' if necessary */
2370 long count;
2371 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2372{
2373 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2374 static int got_click = FALSE; /* got a click some time back */
2375
2376 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2377 int is_click; /* If FALSE it's a drag or release event */
2378 int is_drag; /* If TRUE it's a drag event */
2379 int jump_flags = 0; /* flags for jump_to_mouse() */
2380 pos_T start_visual;
2381 int moved; /* Has cursor moved? */
2382 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002383#ifdef FEAT_WINDOWS
2384 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#ifdef FEAT_VERTSPLIT
2387 int in_sep_line; /* mouse in vertical separator line */
2388#endif
2389 int c1, c2;
2390#if defined(FEAT_FOLDING)
2391 pos_T save_cursor;
2392#endif
2393 win_T *old_curwin = curwin;
2394#ifdef FEAT_VISUAL
2395 static pos_T orig_cursor;
2396 colnr_T leftcol, rightcol;
2397 pos_T end_visual;
2398 int diff;
2399 int old_active = VIsual_active;
2400 int old_mode = VIsual_mode;
2401#endif
2402 int regname;
2403
2404#if defined(FEAT_FOLDING)
2405 save_cursor = curwin->w_cursor;
2406#endif
2407
2408 /*
2409 * When GUI is active, always recognize mouse events, otherwise:
2410 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2411 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2412 * - For command line and insert mode 'mouse' is checked before calling
2413 * do_mouse().
2414 */
2415 if (do_always)
2416 do_always = FALSE;
2417 else
2418#ifdef FEAT_GUI
2419 if (!gui.in_use)
2420#endif
2421 {
2422#ifdef FEAT_VISUAL
2423 if (VIsual_active)
2424 {
2425 if (!mouse_has(MOUSE_VISUAL))
2426 return FALSE;
2427 }
2428 else
2429#endif
2430 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2431 return FALSE;
2432 }
2433
2434 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2435
2436#ifdef FEAT_MOUSESHAPE
2437 /* May have stopped dragging the status or separator line. The pointer is
2438 * most likely still on the status or separator line. */
2439 if (!is_drag && drag_status_line)
2440 {
2441 drag_status_line = FALSE;
2442 update_mouseshape(SHAPE_IDX_STATUS);
2443 }
2444# ifdef FEAT_VERTSPLIT
2445 if (!is_drag && drag_sep_line)
2446 {
2447 drag_sep_line = FALSE;
2448 update_mouseshape(SHAPE_IDX_VSEP);
2449 }
2450# endif
2451#endif
2452
2453 /*
2454 * Ignore drag and release events if we didn't get a click.
2455 */
2456 if (is_click)
2457 got_click = TRUE;
2458 else
2459 {
2460 if (!got_click) /* didn't get click, ignore */
2461 return FALSE;
2462 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002465#ifdef FEAT_WINDOWS
2466 if (in_tab_line)
2467 {
2468 in_tab_line = FALSE;
2469 return FALSE;
2470 }
2471#endif
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474
Bram Moolenaar64969662005-12-14 21:59:55 +00002475#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002477 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 if ((mod_mask & MOD_MASK_ALT))
2480 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483 /*
2484 * CTRL right mouse button does CTRL-T
2485 */
2486 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2487 {
2488 if (State & INSERT)
2489 stuffcharReadbuff(Ctrl_O);
2490 if (count > 1)
2491 stuffnumReadbuff(count);
2492 stuffcharReadbuff(Ctrl_T);
2493 got_click = FALSE; /* ignore drag&release now */
2494 return FALSE;
2495 }
2496
2497 /*
2498 * CTRL only works with left mouse button
2499 */
2500 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2501 return FALSE;
2502
2503 /*
2504 * When a modifier is down, ignore drag and release events, as well as
2505 * multiple clicks and the middle mouse button.
2506 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2507 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002508 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2509 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 && (!is_click
2511 || (mod_mask & MOD_MASK_MULTI_CLICK)
2512 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002513 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 && mouse_model_popup()
2515 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002516 && !((mod_mask & MOD_MASK_ALT)
2517 && !mouse_model_popup()
2518 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 )
2520 return FALSE;
2521
2522 /*
2523 * If the button press was used as the movement command for an operator
2524 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2525 * drag/release events.
2526 */
2527 if (!is_click && which_button == MOUSE_MIDDLE)
2528 return FALSE;
2529
2530 if (oap != NULL)
2531 regname = oap->regname;
2532 else
2533 regname = 0;
2534
2535 /*
2536 * Middle mouse button does a 'put' of the selected text
2537 */
2538 if (which_button == MOUSE_MIDDLE)
2539 {
2540 if (State == NORMAL)
2541 {
2542 /*
2543 * If an operator was pending, we don't know what the user wanted
2544 * to do. Go back to normal mode: Clear the operator and beep().
2545 */
2546 if (oap != NULL && oap->op_type != OP_NOP)
2547 {
2548 clearopbeep(oap);
2549 return FALSE;
2550 }
2551
2552#ifdef FEAT_VISUAL
2553 /*
2554 * If visual was active, yank the highlighted text and put it
2555 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002556 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 */
2558 if (VIsual_active)
2559 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002560 if (VIsual_select)
2561 {
2562 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002563 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002564 }
2565 else
2566 {
2567 stuffcharReadbuff('y');
2568 stuffcharReadbuff(K_MIDDLEMOUSE);
2569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 do_always = TRUE; /* ignore 'mouse' setting next time */
2571 return FALSE;
2572 }
2573#endif
2574 /*
2575 * The rest is below jump_to_mouse()
2576 */
2577 }
2578
2579 else if ((State & INSERT) == 0)
2580 return FALSE;
2581
2582 /*
2583 * Middle click in insert mode doesn't move the mouse, just insert the
2584 * contents of a register. '.' register is special, can't insert that
2585 * with do_put().
2586 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2587 * happens for the GUI).
2588 */
2589 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2590 {
2591 if (regname == '.')
2592 insert_reg(regname, TRUE);
2593 else
2594 {
2595#ifdef FEAT_CLIPBOARD
2596 if (clip_star.available && regname == 0)
2597 regname = '*';
2598#endif
2599 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2600 insert_reg(regname, TRUE);
2601 else
2602 {
2603 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2604
2605 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2606 AppendCharToRedobuff(Ctrl_R);
2607 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2608 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2609 }
2610 }
2611 return FALSE;
2612 }
2613 }
2614
2615 /* When dragging or button-up stay in the same window. */
2616 if (!is_click)
2617 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2618
2619 start_visual.lnum = 0;
2620
Bram Moolenaarf740b292006-02-16 22:11:02 +00002621#ifdef FEAT_WINDOWS
2622 /* Check for clicking in the tab page line. */
2623 if (mouse_row == 0 && firstwin->w_winrow > 0)
2624 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002625 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002626 {
2627 if (in_tab_line)
2628 {
2629 c1 = TabPageIdxs[mouse_col];
2630 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2631 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002632 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002633 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002634
Bram Moolenaarf740b292006-02-16 22:11:02 +00002635 /* click in a tab selects that tab page */
2636 if (is_click
2637# ifdef FEAT_CMDWIN
2638 && cmdwin_type == 0
2639# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002640 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002642 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002643 c1 = TabPageIdxs[mouse_col];
2644 if (c1 >= 0)
2645 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002646 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2647 {
2648 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002649 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002650 tabpage_new();
2651 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2652 }
2653 else
2654 {
2655 /* Go to specified tab page, or next one if not clicking
2656 * on a label. */
2657 goto_tabpage(c1);
2658
2659 /* It's like clicking on the status line of a window. */
2660 if (curwin != old_curwin)
2661 end_visual_mode();
2662 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 }
2664 else if (c1 < 0)
2665 {
2666 tabpage_T *tp;
2667
2668 /* Close the current or specified tab page. */
2669 if (c1 == -999)
2670 tp = curtab;
2671 else
2672 tp = find_tabpage(-c1);
2673 if (tp == curtab)
2674 {
2675 if (first_tabpage->tp_next != NULL)
2676 tabpage_close(FALSE);
2677 }
2678 else if (tp != NULL)
2679 tabpage_close_other(tp, FALSE);
2680 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002681 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002682 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002683 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002684 else if (is_drag && in_tab_line)
2685 {
2686 c1 = TabPageIdxs[mouse_col];
2687 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2688 return FALSE;
2689 }
2690
Bram Moolenaarf740b292006-02-16 22:11:02 +00002691#endif
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 /*
2694 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2695 * right button up -> pop-up menu
2696 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002697 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
2699 if (mouse_model_popup())
2700 {
2701 if (which_button == MOUSE_RIGHT
2702 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2703 {
2704 /*
2705 * NOTE: Ignore right button down and drag mouse events.
2706 * Windows only shows the popup menu on the button up event.
2707 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002708#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2709 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (!is_click)
2711 return FALSE;
2712#endif
2713#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2714 if (is_click || is_drag)
2715 return FALSE;
2716#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002717#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2719 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2720 if (gui.in_use)
2721 {
2722 jump_flags = 0;
2723 if (STRCMP(p_mousem, "popup_setpos") == 0)
2724 {
2725 /* First set the cursor position before showing the popup
2726 * menu. */
2727#ifdef FEAT_VISUAL
2728 if (VIsual_active)
2729 {
2730 pos_T m_pos;
2731
2732 /*
2733 * set MOUSE_MAY_STOP_VIS if we are outside the
2734 * selection or the current window (might have false
2735 * negative here)
2736 */
2737 if (mouse_row < W_WINROW(curwin)
2738 || mouse_row
2739 > (W_WINROW(curwin) + curwin->w_height))
2740 jump_flags = MOUSE_MAY_STOP_VIS;
2741 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2742 jump_flags = MOUSE_MAY_STOP_VIS;
2743 else
2744 {
2745 if ((lt(curwin->w_cursor, VIsual)
2746 && (lt(m_pos, curwin->w_cursor)
2747 || lt(VIsual, m_pos)))
2748 || (lt(VIsual, curwin->w_cursor)
2749 && (lt(m_pos, VIsual)
2750 || lt(curwin->w_cursor, m_pos))))
2751 {
2752 jump_flags = MOUSE_MAY_STOP_VIS;
2753 }
2754 else if (VIsual_mode == Ctrl_V)
2755 {
2756 getvcols(curwin, &curwin->w_cursor, &VIsual,
2757 &leftcol, &rightcol);
2758 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2759 if (m_pos.col < leftcol || m_pos.col > rightcol)
2760 jump_flags = MOUSE_MAY_STOP_VIS;
2761 }
2762 }
2763 }
2764 else
2765 jump_flags = MOUSE_MAY_STOP_VIS;
2766#endif
2767 }
2768 if (jump_flags)
2769 {
2770 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2771 update_curbuf(
2772#ifdef FEAT_VISUAL
2773 VIsual_active ? INVERTED :
2774#endif
2775 VALID);
2776 setcursor();
2777 out_flush(); /* Update before showing popup menu */
2778 }
2779# ifdef FEAT_MENU
2780 gui_show_popupmenu();
2781# endif
2782 return (jump_flags & CURSOR_MOVED) != 0;
2783 }
2784 else
2785 return FALSE;
2786#else
2787 return FALSE;
2788#endif
2789 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002790 if (which_button == MOUSE_LEFT
2791 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
2793 which_button = MOUSE_RIGHT;
2794 mod_mask &= ~MOD_MASK_SHIFT;
2795 }
2796 }
2797
2798#ifdef FEAT_VISUAL
2799 if ((State & (NORMAL | INSERT))
2800 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2801 {
2802 if (which_button == MOUSE_LEFT)
2803 {
2804 if (is_click)
2805 {
2806 /* stop Visual mode for a left click in a window, but not when
2807 * on a status line */
2808 if (VIsual_active)
2809 jump_flags |= MOUSE_MAY_STOP_VIS;
2810 }
2811 else if (mouse_has(MOUSE_VISUAL))
2812 jump_flags |= MOUSE_MAY_VIS;
2813 }
2814 else if (which_button == MOUSE_RIGHT)
2815 {
2816 if (is_click && VIsual_active)
2817 {
2818 /*
2819 * Remember the start and end of visual before moving the
2820 * cursor.
2821 */
2822 if (lt(curwin->w_cursor, VIsual))
2823 {
2824 start_visual = curwin->w_cursor;
2825 end_visual = VIsual;
2826 }
2827 else
2828 {
2829 start_visual = VIsual;
2830 end_visual = curwin->w_cursor;
2831 }
2832 }
2833 jump_flags |= MOUSE_FOCUS;
2834 if (mouse_has(MOUSE_VISUAL))
2835 jump_flags |= MOUSE_MAY_VIS;
2836 }
2837 }
2838#endif
2839
2840 /*
2841 * If an operator is pending, ignore all drags and releases until the
2842 * next mouse click.
2843 */
2844 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2845 {
2846 got_click = FALSE;
2847 oap->motion_type = MCHAR;
2848 }
2849
2850 /* When releasing the button let jump_to_mouse() know. */
2851 if (!is_click && !is_drag)
2852 jump_flags |= MOUSE_RELEASED;
2853
2854 /*
2855 * JUMP!
2856 */
2857 jump_flags = jump_to_mouse(jump_flags,
2858 oap == NULL ? NULL : &(oap->inclusive), which_button);
2859 moved = (jump_flags & CURSOR_MOVED);
2860 in_status_line = (jump_flags & IN_STATUS_LINE);
2861#ifdef FEAT_VERTSPLIT
2862 in_sep_line = (jump_flags & IN_SEP_LINE);
2863#endif
2864
2865#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002866 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2868 {
2869 int key = KEY2TERMCAP1(c);
2870
2871 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2872 || key == (int)KE_RIGHTRELEASE)
2873 netbeans_button_release(which_button);
2874 }
2875#endif
2876
2877 /* When jumping to another window, clear a pending operator. That's a bit
2878 * friendlier than beeping and not jumping to that window. */
2879 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2880 clearop(oap);
2881
2882#ifdef FEAT_FOLDING
2883 if (mod_mask == 0
2884 && !is_drag
2885 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2886 && which_button == MOUSE_LEFT)
2887 {
2888 /* open or close a fold at this line */
2889 if (jump_flags & MOUSE_FOLD_OPEN)
2890 openFold(curwin->w_cursor.lnum, 1L);
2891 else
2892 closeFold(curwin->w_cursor.lnum, 1L);
2893 /* don't move the cursor if still in the same window */
2894 if (curwin == old_curwin)
2895 curwin->w_cursor = save_cursor;
2896 }
2897#endif
2898
2899#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2900 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2901 {
2902 clip_modeless(which_button, is_click, is_drag);
2903 return FALSE;
2904 }
2905#endif
2906
2907#ifdef FEAT_VISUAL
2908 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002909 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (VIsual_active && is_drag && p_so)
2911 {
2912 /* In the very first line, allow scrolling one line */
2913 if (mouse_row == 0)
2914 mouse_dragging = 2;
2915 else
2916 mouse_dragging = 1;
2917 }
2918
2919 /* When dragging the mouse above the window, scroll down. */
2920 if (is_drag && mouse_row < 0 && !in_status_line)
2921 {
2922 scroll_redraw(FALSE, 1L);
2923 mouse_row = 0;
2924 }
2925
2926 if (start_visual.lnum) /* right click in visual mode */
2927 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002928 /* When ALT is pressed make Visual mode blockwise. */
2929 if (mod_mask & MOD_MASK_ALT)
2930 VIsual_mode = Ctrl_V;
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 /*
2933 * In Visual-block mode, divide the area in four, pick up the corner
2934 * that is in the quarter that the cursor is in.
2935 */
2936 if (VIsual_mode == Ctrl_V)
2937 {
2938 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2939 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2940 end_visual.col = leftcol;
2941 else
2942 end_visual.col = rightcol;
2943 if (curwin->w_cursor.lnum <
2944 (start_visual.lnum + end_visual.lnum) / 2)
2945 end_visual.lnum = end_visual.lnum;
2946 else
2947 end_visual.lnum = start_visual.lnum;
2948
2949 /* move VIsual to the right column */
2950 start_visual = curwin->w_cursor; /* save the cursor pos */
2951 curwin->w_cursor = end_visual;
2952 coladvance(end_visual.col);
2953 VIsual = curwin->w_cursor;
2954 curwin->w_cursor = start_visual; /* restore the cursor */
2955 }
2956 else
2957 {
2958 /*
2959 * If the click is before the start of visual, change the start.
2960 * If the click is after the end of visual, change the end. If
2961 * the click is inside the visual, change the closest side.
2962 */
2963 if (lt(curwin->w_cursor, start_visual))
2964 VIsual = end_visual;
2965 else if (lt(end_visual, curwin->w_cursor))
2966 VIsual = start_visual;
2967 else
2968 {
2969 /* In the same line, compare column number */
2970 if (end_visual.lnum == start_visual.lnum)
2971 {
2972 if (curwin->w_cursor.col - start_visual.col >
2973 end_visual.col - curwin->w_cursor.col)
2974 VIsual = start_visual;
2975 else
2976 VIsual = end_visual;
2977 }
2978
2979 /* In different lines, compare line number */
2980 else
2981 {
2982 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2983 (end_visual.lnum - curwin->w_cursor.lnum);
2984
2985 if (diff > 0) /* closest to end */
2986 VIsual = start_visual;
2987 else if (diff < 0) /* closest to start */
2988 VIsual = end_visual;
2989 else /* in the middle line */
2990 {
2991 if (curwin->w_cursor.col <
2992 (start_visual.col + end_visual.col) / 2)
2993 VIsual = end_visual;
2994 else
2995 VIsual = start_visual;
2996 }
2997 }
2998 }
2999 }
3000 }
3001 /*
3002 * If Visual mode started in insert mode, execute "CTRL-O"
3003 */
3004 else if ((State & INSERT) && VIsual_active)
3005 stuffcharReadbuff(Ctrl_O);
3006#endif
3007
3008 /*
3009 * Middle mouse click: Put text before cursor.
3010 */
3011 if (which_button == MOUSE_MIDDLE)
3012 {
3013#ifdef FEAT_CLIPBOARD
3014 if (clip_star.available && regname == 0)
3015 regname = '*';
3016#endif
3017 if (yank_register_mline(regname))
3018 {
3019 if (mouse_past_bottom)
3020 dir = FORWARD;
3021 }
3022 else if (mouse_past_eol)
3023 dir = FORWARD;
3024
3025 if (fixindent)
3026 {
3027 c1 = (dir == BACKWARD) ? '[' : ']';
3028 c2 = 'p';
3029 }
3030 else
3031 {
3032 c1 = (dir == FORWARD) ? 'p' : 'P';
3033 c2 = NUL;
3034 }
3035 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3036
3037 /*
3038 * Remember where the paste started, so in edit() Insstart can be set
3039 * to this position
3040 */
3041 if (restart_edit != 0)
3042 where_paste_started = curwin->w_cursor;
3043 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3044 }
3045
3046#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3047 /*
3048 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3049 * error under the mouse pointer.
3050 */
3051 else if (((mod_mask & MOD_MASK_CTRL)
3052 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3053 && bt_quickfix(curbuf))
3054 {
3055 if (State & INSERT)
3056 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003057 if (curwin->w_llist_ref == NULL) /* quickfix window */
3058 stuffReadbuff((char_u *)":.cc\n");
3059 else /* location list window */
3060 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 got_click = FALSE; /* ignore drag&release now */
3062 }
3063#endif
3064
3065 /*
3066 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3067 * under the mouse pointer.
3068 */
3069 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3070 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3071 {
3072 if (State & INSERT)
3073 stuffcharReadbuff(Ctrl_O);
3074 stuffcharReadbuff(Ctrl_RSB);
3075 got_click = FALSE; /* ignore drag&release now */
3076 }
3077
3078 /*
3079 * Shift-Mouse click searches for the next occurrence of the word under
3080 * the mouse pointer
3081 */
3082 else if ((mod_mask & MOD_MASK_SHIFT))
3083 {
3084 if (State & INSERT
3085#ifdef FEAT_VISUAL
3086 || (VIsual_active && VIsual_select)
3087#endif
3088 )
3089 stuffcharReadbuff(Ctrl_O);
3090 if (which_button == MOUSE_LEFT)
3091 stuffcharReadbuff('*');
3092 else /* MOUSE_RIGHT */
3093 stuffcharReadbuff('#');
3094 }
3095
3096 /* Handle double clicks, unless on status line */
3097 else if (in_status_line)
3098 {
3099#ifdef FEAT_MOUSESHAPE
3100 if ((is_drag || is_click) && !drag_status_line)
3101 {
3102 drag_status_line = TRUE;
3103 update_mouseshape(-1);
3104 }
3105#endif
3106 }
3107#ifdef FEAT_VERTSPLIT
3108 else if (in_sep_line)
3109 {
3110# ifdef FEAT_MOUSESHAPE
3111 if ((is_drag || is_click) && !drag_sep_line)
3112 {
3113 drag_sep_line = TRUE;
3114 update_mouseshape(-1);
3115 }
3116# endif
3117 }
3118#endif
3119#ifdef FEAT_VISUAL
3120 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3121 && mouse_has(MOUSE_VISUAL))
3122 {
3123 if (is_click || !VIsual_active)
3124 {
3125 if (VIsual_active)
3126 orig_cursor = VIsual;
3127 else
3128 {
3129 check_visual_highlight();
3130 VIsual = curwin->w_cursor;
3131 orig_cursor = VIsual;
3132 VIsual_active = TRUE;
3133 VIsual_reselect = TRUE;
3134 /* start Select mode if 'selectmode' contains "mouse" */
3135 may_start_select('o');
3136 setmouse();
3137 }
3138 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003139 {
3140 /* Double click with ALT pressed makes it blockwise. */
3141 if (mod_mask & MOD_MASK_ALT)
3142 VIsual_mode = Ctrl_V;
3143 else
3144 VIsual_mode = 'v';
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3147 VIsual_mode = 'V';
3148 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3149 VIsual_mode = Ctrl_V;
3150#ifdef FEAT_CLIPBOARD
3151 /* Make sure the clipboard gets updated. Needed because start and
3152 * end may still be the same, and the selection needs to be owned */
3153 clip_star.vmode = NUL;
3154#endif
3155 }
3156 /*
3157 * A double click selects a word or a block.
3158 */
3159 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3160 {
3161 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003162 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 if (is_click)
3165 {
3166 /* If the character under the cursor (skipping white space) is
3167 * not a word character, try finding a match and select a (),
3168 * {}, [], #if/#endif, etc. block. */
3169 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003170 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 inc(&end_visual);
3172 if (oap != NULL)
3173 oap->motion_type = MCHAR;
3174 if (oap != NULL
3175 && VIsual_mode == 'v'
3176 && !vim_iswordc(gchar_pos(&end_visual))
3177 && equalpos(curwin->w_cursor, VIsual)
3178 && (pos = findmatch(oap, NUL)) != NULL)
3179 {
3180 curwin->w_cursor = *pos;
3181 if (oap->motion_type == MLINE)
3182 VIsual_mode = 'V';
3183 else if (*p_sel == 'e')
3184 {
3185 if (lt(curwin->w_cursor, VIsual))
3186 ++VIsual.col;
3187 else
3188 ++curwin->w_cursor.col;
3189 }
3190 }
3191 }
3192
3193 if (pos == NULL && (is_click || is_drag))
3194 {
3195 /* When not found a match or when dragging: extend to include
3196 * a word. */
3197 if (lt(curwin->w_cursor, orig_cursor))
3198 {
3199 find_start_of_word(&curwin->w_cursor);
3200 find_end_of_word(&VIsual);
3201 }
3202 else
3203 {
3204 find_start_of_word(&VIsual);
3205 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3206#ifdef FEAT_MBYTE
3207 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003208 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209#else
3210 ++curwin->w_cursor.col;
3211#endif
3212 find_end_of_word(&curwin->w_cursor);
3213 }
3214 }
3215 curwin->w_set_curswant = TRUE;
3216 }
3217 if (is_click)
3218 redraw_curbuf_later(INVERTED); /* update the inversion */
3219 }
3220 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003221 {
3222 if (mod_mask & MOD_MASK_ALT)
3223 VIsual_mode = Ctrl_V;
3224 else
3225 VIsual_mode = 'v';
3226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
3228 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003229 if ((!VIsual_active && old_active && mode_displayed)
3230 || (VIsual_active && p_smd && msg_silent == 0
3231 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 redraw_cmdline = TRUE;
3233#endif
3234
3235 return moved;
3236}
3237
3238#ifdef FEAT_VISUAL
3239/*
3240 * Move "pos" back to the start of the word it's in.
3241 */
3242 static void
3243find_start_of_word(pos)
3244 pos_T *pos;
3245{
3246 char_u *line;
3247 int cclass;
3248 int col;
3249
3250 line = ml_get(pos->lnum);
3251 cclass = get_mouse_class(line + pos->col);
3252
3253 while (pos->col > 0)
3254 {
3255 col = pos->col - 1;
3256#ifdef FEAT_MBYTE
3257 col -= (*mb_head_off)(line, line + col);
3258#endif
3259 if (get_mouse_class(line + col) != cclass)
3260 break;
3261 pos->col = col;
3262 }
3263}
3264
3265/*
3266 * Move "pos" forward to the end of the word it's in.
3267 * When 'selection' is "exclusive", the position is just after the word.
3268 */
3269 static void
3270find_end_of_word(pos)
3271 pos_T *pos;
3272{
3273 char_u *line;
3274 int cclass;
3275 int col;
3276
3277 line = ml_get(pos->lnum);
3278 if (*p_sel == 'e' && pos->col > 0)
3279 {
3280 --pos->col;
3281#ifdef FEAT_MBYTE
3282 pos->col -= (*mb_head_off)(line, line + pos->col);
3283#endif
3284 }
3285 cclass = get_mouse_class(line + pos->col);
3286 while (line[pos->col] != NUL)
3287 {
3288#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003289 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#else
3291 col = pos->col + 1;
3292#endif
3293 if (get_mouse_class(line + col) != cclass)
3294 {
3295 if (*p_sel == 'e')
3296 pos->col = col;
3297 break;
3298 }
3299 pos->col = col;
3300 }
3301}
3302
3303/*
3304 * Get class of a character for selection: same class means same word.
3305 * 0: blank
3306 * 1: punctuation groups
3307 * 2: normal word character
3308 * >2: multi-byte word character.
3309 */
3310 static int
3311get_mouse_class(p)
3312 char_u *p;
3313{
3314 int c;
3315
3316#ifdef FEAT_MBYTE
3317 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3318 return mb_get_class(p);
3319#endif
3320
3321 c = *p;
3322 if (c == ' ' || c == '\t')
3323 return 0;
3324
3325 if (vim_iswordc(c))
3326 return 2;
3327
3328 /*
3329 * There are a few special cases where we want certain combinations of
3330 * characters to be considered as a single word. These are things like
3331 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003332 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 */
3334 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3335 return 1;
3336 return c;
3337}
3338#endif /* FEAT_VISUAL */
3339#endif /* FEAT_MOUSE */
3340
3341#if defined(FEAT_VISUAL) || defined(PROTO)
3342/*
3343 * Check if highlighting for visual mode is possible, give a warning message
3344 * if not.
3345 */
3346 void
3347check_visual_highlight()
3348{
3349 static int did_check = FALSE;
3350
3351 if (full_screen)
3352 {
3353 if (!did_check && hl_attr(HLF_V) == 0)
3354 MSG(_("Warning: terminal cannot highlight"));
3355 did_check = TRUE;
3356 }
3357}
3358
3359/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003360 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 * This function should ALWAYS be called to end Visual mode, except from
3362 * do_pending_operator().
3363 */
3364 void
3365end_visual_mode()
3366{
3367#ifdef FEAT_CLIPBOARD
3368 /*
3369 * If we are using the clipboard, then remember what was selected in case
3370 * we need to paste it somewhere while we still own the selection.
3371 * Only do this when the clipboard is already owned. Don't want to grab
3372 * the selection when hitting ESC.
3373 */
3374 if (clip_star.available && clip_star.owned)
3375 clip_auto_select();
3376#endif
3377
3378 VIsual_active = FALSE;
3379#ifdef FEAT_MOUSE
3380 setmouse();
3381 mouse_dragging = 0;
3382#endif
3383
3384 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003385 curbuf->b_visual.vi_mode = VIsual_mode;
3386 curbuf->b_visual.vi_start = VIsual;
3387 curbuf->b_visual.vi_end = curwin->w_cursor;
3388 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#ifdef FEAT_EVAL
3390 curbuf->b_visual_mode_eval = VIsual_mode;
3391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#ifdef FEAT_VIRTUALEDIT
3393 if (!virtual_active())
3394 curwin->w_cursor.coladd = 0;
3395#endif
3396
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003397 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 clear_cmdline = TRUE; /* unshow visual mode later */
3399#ifdef FEAT_CMDL_INFO
3400 else
3401 clear_showcmd();
3402#endif
3403
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003404 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405}
3406
3407/*
3408 * Reset VIsual_active and VIsual_reselect.
3409 */
3410 void
3411reset_VIsual_and_resel()
3412{
3413 if (VIsual_active)
3414 {
3415 end_visual_mode();
3416 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3417 }
3418 VIsual_reselect = FALSE;
3419}
3420
3421/*
3422 * Reset VIsual_active and VIsual_reselect if it's set.
3423 */
3424 void
3425reset_VIsual()
3426{
3427 if (VIsual_active)
3428 {
3429 end_visual_mode();
3430 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3431 VIsual_reselect = FALSE;
3432 }
3433}
3434#endif /* FEAT_VISUAL */
3435
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3438
3439/*
3440 * Check for a balloon-eval special item to include when searching for an
3441 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3442 * Returns TRUE if the character at "*ptr" should be included.
3443 * "dir" is FORWARD or BACKWARD, the direction of searching.
3444 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3445 * "bnp" points to a counter for square brackets.
3446 */
3447 static int
3448find_is_eval_item(ptr, colp, bnp, dir)
3449 char_u *ptr;
3450 int *colp;
3451 int *bnp;
3452 int dir;
3453{
3454 /* Accept everything inside []. */
3455 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3456 ++*bnp;
3457 if (*bnp > 0)
3458 {
3459 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3460 --*bnp;
3461 return TRUE;
3462 }
3463
3464 /* skip over "s.var" */
3465 if (*ptr == '.')
3466 return TRUE;
3467
3468 /* two-character item: s->var */
3469 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3470 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3471 {
3472 *colp += dir;
3473 return TRUE;
3474 }
3475 return FALSE;
3476}
3477#endif
3478
3479/*
3480 * Find the identifier under or to the right of the cursor.
3481 * "find_type" can have one of three values:
3482 * FIND_IDENT: find an identifier (keyword)
3483 * FIND_STRING: find any non-white string
3484 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 *
3487 * There are three steps:
3488 * 1. Search forward for the start of an identifier/string. Doesn't move if
3489 * already on one.
3490 * 2. Search backward for the start of this identifier/string.
3491 * This doesn't match the real Vi but I like it a little better and it
3492 * shouldn't bother anyone.
3493 * 3. Search forward to the end of this identifier/string.
3494 * When FIND_IDENT isn't defined, we backup until a blank.
3495 *
3496 * Returns the length of the string, or zero if no string is found.
3497 * If a string is found, a pointer to the string is put in "*string". This
3498 * string is not always NUL terminated.
3499 */
3500 int
3501find_ident_under_cursor(string, find_type)
3502 char_u **string;
3503 int find_type;
3504{
3505 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3506 curwin->w_cursor.col, string, find_type);
3507}
3508
3509/*
3510 * Like find_ident_under_cursor(), but for any window and any position.
3511 * However: Uses 'iskeyword' from the current window!.
3512 */
3513 int
3514find_ident_at_pos(wp, lnum, startcol, string, find_type)
3515 win_T *wp;
3516 linenr_T lnum;
3517 colnr_T startcol;
3518 char_u **string;
3519 int find_type;
3520{
3521 char_u *ptr;
3522 int col = 0; /* init to shut up GCC */
3523 int i;
3524#ifdef FEAT_MBYTE
3525 int this_class = 0;
3526 int prev_class;
3527 int prevcol;
3528#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 int bn = 0; /* bracket nesting */
3531#endif
3532
3533 /*
3534 * if i == 0: try to find an identifier
3535 * if i == 1: try to find any non-white string
3536 */
3537 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3538 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3539 {
3540 /*
3541 * 1. skip to start of identifier/string
3542 */
3543 col = startcol;
3544#ifdef FEAT_MBYTE
3545 if (has_mbyte)
3546 {
3547 while (ptr[col] != NUL)
3548 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* Stop at a ']' to evaluate "a[x]". */
3551 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3552 break;
3553# endif
3554 this_class = mb_get_class(ptr + col);
3555 if (this_class != 0 && (i == 1 || this_class != 1))
3556 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003557 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 }
3560 else
3561#endif
3562 while (ptr[col] != NUL
3563 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003564# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3566# endif
3567 )
3568 ++col;
3569
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 /* When starting on a ']' count it, so that we include the '['. */
3572 bn = ptr[col] == ']';
3573#endif
3574
3575 /*
3576 * 2. Back up to start of identifier/string.
3577 */
3578#ifdef FEAT_MBYTE
3579 if (has_mbyte)
3580 {
3581 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3584 this_class = mb_get_class((char_u *)"a");
3585 else
3586# endif
3587 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003588 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3591 prev_class = mb_get_class(ptr + prevcol);
3592 if (this_class != prev_class
3593 && (i == 0
3594 || prev_class == 0
3595 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 && (!(find_type & FIND_EVAL)
3598 || prevcol == 0
3599 || !find_is_eval_item(ptr + prevcol, &prevcol,
3600 &bn, BACKWARD))
3601# endif
3602 )
3603 break;
3604 col = prevcol;
3605 }
3606
3607 /* If we don't want just any old string, or we've found an
3608 * identifier, stop searching. */
3609 if (this_class > 2)
3610 this_class = 2;
3611 if (!(find_type & FIND_STRING) || this_class == 2)
3612 break;
3613 }
3614 else
3615#endif
3616 {
3617 while (col > 0
3618 && ((i == 0
3619 ? vim_iswordc(ptr[col - 1])
3620 : (!vim_iswhite(ptr[col - 1])
3621 && (!(find_type & FIND_IDENT)
3622 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003623#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 || ((find_type & FIND_EVAL)
3625 && col > 1
3626 && find_is_eval_item(ptr + col - 1, &col,
3627 &bn, BACKWARD))
3628#endif
3629 ))
3630 --col;
3631
3632 /* If we don't want just any old string, or we've found an
3633 * identifier, stop searching. */
3634 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3635 break;
3636 }
3637 }
3638
3639 if (ptr[col] == NUL || (i == 0 && (
3640#ifdef FEAT_MBYTE
3641 has_mbyte ? this_class != 2 :
3642#endif
3643 !vim_iswordc(ptr[col]))))
3644 {
3645 /*
3646 * didn't find an identifier or string
3647 */
3648 if (find_type & FIND_STRING)
3649 EMSG(_("E348: No string under cursor"));
3650 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003651 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return 0;
3653 }
3654 ptr += col;
3655 *string = ptr;
3656
3657 /*
3658 * 3. Find the end if the identifier/string.
3659 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 bn = 0;
3662 startcol -= col;
3663#endif
3664 col = 0;
3665#ifdef FEAT_MBYTE
3666 if (has_mbyte)
3667 {
3668 /* Search for point of changing multibyte character class. */
3669 this_class = mb_get_class(ptr);
3670 while (ptr[col] != NUL
3671 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3672 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003673# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 || ((find_type & FIND_EVAL)
3675 && col <= (int)startcol
3676 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3677# endif
3678 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003679 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 else
3682#endif
3683 while ((i == 0 ? vim_iswordc(ptr[col])
3684 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003685# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 || ((find_type & FIND_EVAL)
3687 && col <= (int)startcol
3688 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3689# endif
3690 )
3691 {
3692 ++col;
3693 }
3694
3695 return col;
3696}
3697
3698/*
3699 * Prepare for redo of a normal command.
3700 */
3701 static void
3702prep_redo_cmd(cap)
3703 cmdarg_T *cap;
3704{
3705 prep_redo(cap->oap->regname, cap->count0,
3706 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3707}
3708
3709/*
3710 * Prepare for redo of any command.
3711 * Note that only the last argument can be a multi-byte char.
3712 */
3713 static void
3714prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3715 int regname;
3716 long num;
3717 int cmd1;
3718 int cmd2;
3719 int cmd3;
3720 int cmd4;
3721 int cmd5;
3722{
3723 ResetRedobuff();
3724 if (regname != 0) /* yank from specified buffer */
3725 {
3726 AppendCharToRedobuff('"');
3727 AppendCharToRedobuff(regname);
3728 }
3729 if (num)
3730 AppendNumberToRedobuff(num);
3731
3732 if (cmd1 != NUL)
3733 AppendCharToRedobuff(cmd1);
3734 if (cmd2 != NUL)
3735 AppendCharToRedobuff(cmd2);
3736 if (cmd3 != NUL)
3737 AppendCharToRedobuff(cmd3);
3738 if (cmd4 != NUL)
3739 AppendCharToRedobuff(cmd4);
3740 if (cmd5 != NUL)
3741 AppendCharToRedobuff(cmd5);
3742}
3743
3744/*
3745 * check for operator active and clear it
3746 *
3747 * return TRUE if operator was active
3748 */
3749 static int
3750checkclearop(oap)
3751 oparg_T *oap;
3752{
3753 if (oap->op_type == OP_NOP)
3754 return FALSE;
3755 clearopbeep(oap);
3756 return TRUE;
3757}
3758
3759/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003760 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003762 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 */
3764 static int
3765checkclearopq(oap)
3766 oparg_T *oap;
3767{
3768 if (oap->op_type == OP_NOP
3769#ifdef FEAT_VISUAL
3770 && !VIsual_active
3771#endif
3772 )
3773 return FALSE;
3774 clearopbeep(oap);
3775 return TRUE;
3776}
3777
3778 static void
3779clearop(oap)
3780 oparg_T *oap;
3781{
3782 oap->op_type = OP_NOP;
3783 oap->regname = 0;
3784 oap->motion_force = NUL;
3785 oap->use_reg_one = FALSE;
3786}
3787
3788 static void
3789clearopbeep(oap)
3790 oparg_T *oap;
3791{
3792 clearop(oap);
3793 beep_flush();
3794}
3795
3796#ifdef FEAT_VISUAL
3797/*
3798 * Remove the shift modifier from a special key.
3799 */
3800 static void
3801unshift_special(cap)
3802 cmdarg_T *cap;
3803{
3804 switch (cap->cmdchar)
3805 {
3806 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3807 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3808 case K_S_UP: cap->cmdchar = K_UP; break;
3809 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3810 case K_S_HOME: cap->cmdchar = K_HOME; break;
3811 case K_S_END: cap->cmdchar = K_END; break;
3812 }
3813 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3814}
3815#endif
3816
3817#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3818/*
3819 * Routines for displaying a partly typed command
3820 */
3821
3822#ifdef FEAT_VISUAL /* need room for size of Visual area */
3823# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3824#else
3825# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3826#endif
3827static char_u showcmd_buf[SHOWCMD_BUFLEN];
3828static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3829static int showcmd_is_clear = TRUE;
3830static int showcmd_visual = FALSE;
3831
3832static void display_showcmd __ARGS((void));
3833
3834 void
3835clear_showcmd()
3836{
3837 if (!p_sc)
3838 return;
3839
3840#ifdef FEAT_VISUAL
3841 if (VIsual_active && !char_avail())
3842 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003843 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 long lines;
3845 colnr_T leftcol, rightcol;
3846 linenr_T top, bot;
3847
3848 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003849 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
3851 top = VIsual.lnum;
3852 bot = curwin->w_cursor.lnum;
3853 }
3854 else
3855 {
3856 top = curwin->w_cursor.lnum;
3857 bot = VIsual.lnum;
3858 }
3859# ifdef FEAT_FOLDING
3860 /* Include closed folds as a whole. */
3861 hasFolding(top, &top, NULL);
3862 hasFolding(bot, NULL, &bot);
3863# endif
3864 lines = bot - top + 1;
3865
3866 if (VIsual_mode == Ctrl_V)
3867 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003868# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003869 char_u *saved_sbr = p_sbr;
3870
3871 /* Make 'sbr' empty for a moment to get the correct size. */
3872 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003875# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003876 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3879 (long)(rightcol - leftcol + 1));
3880 }
3881 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3882 sprintf((char *)showcmd_buf, "%ld", lines);
3883 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003884 {
3885 char_u *s, *e;
3886 int l;
3887 int bytes = 0;
3888 int chars = 0;
3889
3890 if (cursor_bot)
3891 {
3892 s = ml_get_pos(&VIsual);
3893 e = ml_get_cursor();
3894 }
3895 else
3896 {
3897 s = ml_get_cursor();
3898 e = ml_get_pos(&VIsual);
3899 }
3900 while ((*p_sel != 'e') ? s <= e : s < e)
3901 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003902# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003903 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003904# else
3905 l = (*s == NUL) ? 0 : 1;
3906# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003907 if (l == 0)
3908 {
3909 ++bytes;
3910 ++chars;
3911 break; /* end of line */
3912 }
3913 bytes += l;
3914 ++chars;
3915 s += l;
3916 }
3917 if (bytes == chars)
3918 sprintf((char *)showcmd_buf, "%d", chars);
3919 else
3920 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3923 showcmd_visual = TRUE;
3924 }
3925 else
3926#endif
3927 {
3928 showcmd_buf[0] = NUL;
3929 showcmd_visual = FALSE;
3930
3931 /* Don't actually display something if there is nothing to clear. */
3932 if (showcmd_is_clear)
3933 return;
3934 }
3935
3936 display_showcmd();
3937}
3938
3939/*
3940 * Add 'c' to string of shown command chars.
3941 * Return TRUE if output has been written (and setcursor() has been called).
3942 */
3943 int
3944add_to_showcmd(c)
3945 int c;
3946{
3947 char_u *p;
3948 int old_len;
3949 int extra_len;
3950 int overflow;
3951#if defined(FEAT_MOUSE)
3952 int i;
3953 static int ignore[] =
3954 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003955# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3957 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 K_IGNORE,
3960 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3961 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3962 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003963 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003965 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 0
3967 };
3968#endif
3969
Bram Moolenaar09df3122006-01-23 22:23:09 +00003970 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 return FALSE;
3972
3973 if (showcmd_visual)
3974 {
3975 showcmd_buf[0] = NUL;
3976 showcmd_visual = FALSE;
3977 }
3978
3979#if defined(FEAT_MOUSE)
3980 /* Ignore keys that are scrollbar updates and mouse clicks */
3981 if (IS_SPECIAL(c))
3982 for (i = 0; ignore[i] != 0; ++i)
3983 if (ignore[i] == c)
3984 return FALSE;
3985#endif
3986
3987 p = transchar(c);
3988 old_len = (int)STRLEN(showcmd_buf);
3989 extra_len = (int)STRLEN(p);
3990 overflow = old_len + extra_len - SHOWCMD_COLS;
3991 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003992 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3993 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 STRCAT(showcmd_buf, p);
3995
3996 if (char_avail())
3997 return FALSE;
3998
3999 display_showcmd();
4000
4001 return TRUE;
4002}
4003
4004 void
4005add_to_showcmd_c(c)
4006 int c;
4007{
4008 if (!add_to_showcmd(c))
4009 setcursor();
4010}
4011
4012/*
4013 * Delete 'len' characters from the end of the shown command.
4014 */
4015 static void
4016del_from_showcmd(len)
4017 int len;
4018{
4019 int old_len;
4020
4021 if (!p_sc)
4022 return;
4023
4024 old_len = (int)STRLEN(showcmd_buf);
4025 if (len > old_len)
4026 len = old_len;
4027 showcmd_buf[old_len - len] = NUL;
4028
4029 if (!char_avail())
4030 display_showcmd();
4031}
4032
4033/*
4034 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4035 * something and there is a partial mapping.
4036 */
4037 void
4038push_showcmd()
4039{
4040 if (p_sc)
4041 STRCPY(old_showcmd_buf, showcmd_buf);
4042}
4043
4044 void
4045pop_showcmd()
4046{
4047 if (!p_sc)
4048 return;
4049
4050 STRCPY(showcmd_buf, old_showcmd_buf);
4051
4052 display_showcmd();
4053}
4054
4055 static void
4056display_showcmd()
4057{
4058 int len;
4059
4060 cursor_off();
4061
4062 len = (int)STRLEN(showcmd_buf);
4063 if (len == 0)
4064 showcmd_is_clear = TRUE;
4065 else
4066 {
4067 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4068 showcmd_is_clear = FALSE;
4069 }
4070
4071 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004072 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4073 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 */
4075 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4076
4077 setcursor(); /* put cursor back where it belongs */
4078}
4079#endif
4080
4081#ifdef FEAT_SCROLLBIND
4082/*
4083 * When "check" is FALSE, prepare for commands that scroll the window.
4084 * When "check" is TRUE, take care of scroll-binding after the window has
4085 * scrolled. Called from normal_cmd() and edit().
4086 */
4087 void
4088do_check_scrollbind(check)
4089 int check;
4090{
4091 static win_T *old_curwin = NULL;
4092 static linenr_T old_topline = 0;
4093#ifdef FEAT_DIFF
4094 static int old_topfill = 0;
4095#endif
4096 static buf_T *old_buf = NULL;
4097 static colnr_T old_leftcol = 0;
4098
4099 if (check && curwin->w_p_scb)
4100 {
4101 /* If a ":syncbind" command was just used, don't scroll, only reset
4102 * the values. */
4103 if (did_syncbind)
4104 did_syncbind = FALSE;
4105 else if (curwin == old_curwin)
4106 {
4107 /*
4108 * Synchronize other windows, as necessary according to
4109 * 'scrollbind'. Don't do this after an ":edit" command, except
4110 * when 'diff' is set.
4111 */
4112 if ((curwin->w_buffer == old_buf
4113#ifdef FEAT_DIFF
4114 || curwin->w_p_diff
4115#endif
4116 )
4117 && (curwin->w_topline != old_topline
4118#ifdef FEAT_DIFF
4119 || curwin->w_topfill != old_topfill
4120#endif
4121 || curwin->w_leftcol != old_leftcol))
4122 {
4123 check_scrollbind(curwin->w_topline - old_topline,
4124 (long)(curwin->w_leftcol - old_leftcol));
4125 }
4126 }
4127 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4128 {
4129 /*
4130 * When switching between windows, make sure that the relative
4131 * vertical offset is valid for the new window. The relative
4132 * offset is invalid whenever another 'scrollbind' window has
4133 * scrolled to a point that would force the current window to
4134 * scroll past the beginning or end of its buffer. When the
4135 * resync is performed, some of the other 'scrollbind' windows may
4136 * need to jump so that the current window's relative position is
4137 * visible on-screen.
4138 */
4139 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4140 }
4141 curwin->w_scbind_pos = curwin->w_topline;
4142 }
4143
4144 old_curwin = curwin;
4145 old_topline = curwin->w_topline;
4146#ifdef FEAT_DIFF
4147 old_topfill = curwin->w_topfill;
4148#endif
4149 old_buf = curwin->w_buffer;
4150 old_leftcol = curwin->w_leftcol;
4151}
4152
4153/*
4154 * Synchronize any windows that have "scrollbind" set, based on the
4155 * number of rows by which the current window has changed
4156 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4157 */
4158 void
4159check_scrollbind(topline_diff, leftcol_diff)
4160 linenr_T topline_diff;
4161 long leftcol_diff;
4162{
4163 int want_ver;
4164 int want_hor;
4165 win_T *old_curwin = curwin;
4166 buf_T *old_curbuf = curbuf;
4167#ifdef FEAT_VISUAL
4168 int old_VIsual_select = VIsual_select;
4169 int old_VIsual_active = VIsual_active;
4170#endif
4171 colnr_T tgt_leftcol = curwin->w_leftcol;
4172 long topline;
4173 long y;
4174
4175 /*
4176 * check 'scrollopt' string for vertical and horizontal scroll options
4177 */
4178 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4179#ifdef FEAT_DIFF
4180 want_ver |= old_curwin->w_p_diff;
4181#endif
4182 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4183
4184 /*
4185 * loop through the scrollbound windows and scroll accordingly
4186 */
4187#ifdef FEAT_VISUAL
4188 VIsual_select = VIsual_active = 0;
4189#endif
4190 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4191 {
4192 curbuf = curwin->w_buffer;
4193 /* skip original window and windows with 'noscrollbind' */
4194 if (curwin != old_curwin && curwin->w_p_scb)
4195 {
4196 /*
4197 * do the vertical scroll
4198 */
4199 if (want_ver)
4200 {
4201#ifdef FEAT_DIFF
4202 if (old_curwin->w_p_diff && curwin->w_p_diff)
4203 {
4204 diff_set_topline(old_curwin, curwin);
4205 }
4206 else
4207#endif
4208 {
4209 curwin->w_scbind_pos += topline_diff;
4210 topline = curwin->w_scbind_pos;
4211 if (topline > curbuf->b_ml.ml_line_count)
4212 topline = curbuf->b_ml.ml_line_count;
4213 if (topline < 1)
4214 topline = 1;
4215
4216 y = topline - curwin->w_topline;
4217 if (y > 0)
4218 scrollup(y, FALSE);
4219 else
4220 scrolldown(-y, FALSE);
4221 }
4222
4223 redraw_later(VALID);
4224 cursor_correct();
4225#ifdef FEAT_WINDOWS
4226 curwin->w_redr_status = TRUE;
4227#endif
4228 }
4229
4230 /*
4231 * do the horizontal scroll
4232 */
4233 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4234 {
4235 curwin->w_leftcol = tgt_leftcol;
4236 leftcol_changed();
4237 }
4238 }
4239 }
4240
4241 /*
4242 * reset current-window
4243 */
4244#ifdef FEAT_VISUAL
4245 VIsual_select = old_VIsual_select;
4246 VIsual_active = old_VIsual_active;
4247#endif
4248 curwin = old_curwin;
4249 curbuf = old_curbuf;
4250}
4251#endif /* #ifdef FEAT_SCROLLBIND */
4252
4253/*
4254 * Command character that's ignored.
4255 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004256 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 static void
4259nv_ignore(cap)
4260 cmdarg_T *cap;
4261{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004262 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263}
4264
4265/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004266 * Command character that doesn't do anything, but unlike nv_ignore() does
4267 * start edit(). Used for "startinsert" executed while starting up.
4268 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004269 static void
4270nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004271 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004272{
4273}
4274
4275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 * Command character doesn't exist.
4277 */
4278 static void
4279nv_error(cap)
4280 cmdarg_T *cap;
4281{
4282 clearopbeep(cap->oap);
4283}
4284
4285/*
4286 * <Help> and <F1> commands.
4287 */
4288 static void
4289nv_help(cap)
4290 cmdarg_T *cap;
4291{
4292 if (!checkclearopq(cap->oap))
4293 ex_help(NULL);
4294}
4295
4296/*
4297 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4298 */
4299 static void
4300nv_addsub(cap)
4301 cmdarg_T *cap;
4302{
4303 if (!checkclearopq(cap->oap)
4304 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4305 prep_redo_cmd(cap);
4306}
4307
4308/*
4309 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4310 */
4311 static void
4312nv_page(cap)
4313 cmdarg_T *cap;
4314{
4315 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004316 {
4317#ifdef FEAT_WINDOWS
4318 if (mod_mask & MOD_MASK_CTRL)
4319 {
4320 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4321 if (cap->arg == BACKWARD)
4322 goto_tabpage(-(int)cap->count1);
4323 else
4324 goto_tabpage((int)cap->count0);
4325 }
4326 else
4327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331
4332/*
4333 * Implementation of "gd" and "gD" command.
4334 */
4335 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004336nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004337 oparg_T *oap;
4338 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004339 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 char_u *ptr;
4343
Bram Moolenaard9d30582005-05-18 22:10:28 +00004344 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004345 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347#ifdef FEAT_FOLDING
4348 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4349 foldOpenCursor();
4350#endif
4351}
4352
4353/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004354 * Search for variable declaration of "ptr[len]".
4355 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4356 * current file ("gD").
4357 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004358 * Return FAIL when not found.
4359 */
4360 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004361find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004362 char_u *ptr;
4363 int len;
4364 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004365 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004366 int searchflags; /* flags passed to searchit() */
4367{
4368 char_u *pat;
4369 pos_T old_pos;
4370 pos_T par_pos;
4371 pos_T found_pos;
4372 int t;
4373 int save_p_ws;
4374 int save_p_scs;
4375 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004376 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377
4378 if ((pat = alloc(len + 7)) == NULL)
4379 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004380
4381 /* Put "\V" before the pattern to avoid that the special meaning of "."
4382 * and "~" causes trouble. */
4383 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4384 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 old_pos = curwin->w_cursor;
4386 save_p_ws = p_ws;
4387 save_p_scs = p_scs;
4388 p_ws = FALSE; /* don't wrap around end of file now */
4389 p_scs = FALSE; /* don't switch ignorecase off now */
4390
4391 /*
4392 * With "gD" go to line 1.
4393 * With "gd" Search back for the start of the current function, then go
4394 * back until a blank line. If this fails go to line 1.
4395 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004396 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
4398 setpcmark(); /* Set in findpar() otherwise */
4399 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004400 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else
4403 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004404 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4406 --curwin->w_cursor.lnum;
4407 }
4408 curwin->w_cursor.col = 0;
4409
4410 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004411 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004412 for (;;)
4413 {
4414 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004415 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416 if (curwin->w_cursor.lnum >= old_pos.lnum)
4417 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004418
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004419 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004420 {
4421 pos_T *pos;
4422
4423 /* Check that the block the match is in doesn't end before the
4424 * position where we started the search from. */
4425 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4426 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4427 && pos->lnum < old_pos.lnum)
4428 continue;
4429 }
4430
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 if (t == FAIL)
4432 {
4433 /* If we previously found a valid position, use it. */
4434 if (found_pos.lnum != 0)
4435 {
4436 curwin->w_cursor = found_pos;
4437 t = OK;
4438 }
4439 break;
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004442 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004443 {
4444 /* Ignore this line, continue at start of next line. */
4445 ++curwin->w_cursor.lnum;
4446 curwin->w_cursor.col = 0;
4447 continue;
4448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004450 if (!locally) /* global search: use first match found */
4451 break;
4452 if (curwin->w_cursor.lnum >= par_pos.lnum)
4453 {
4454 /* If we previously found a valid position, use it. */
4455 if (found_pos.lnum != 0)
4456 curwin->w_cursor = found_pos;
4457 break;
4458 }
4459
4460 /* For finding a local variable and the match is before the "{" search
4461 * to find a later match. For K&R style function declarations this
4462 * skips the function header without types. */
4463 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004465
4466 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004468 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 curwin->w_cursor = old_pos;
4470 }
4471 else
4472 {
4473 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 /* "n" searches forward now */
4475 reset_search_dir();
4476 }
4477
4478 vim_free(pat);
4479 p_ws = save_p_ws;
4480 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004481
4482 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
4484
4485/*
4486 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4487 * lines rather than lines in the file.
4488 * 'dist' must be positive.
4489 *
4490 * Return OK if able to move cursor, FAIL otherwise.
4491 */
4492 static int
4493nv_screengo(oap, dir, dist)
4494 oparg_T *oap;
4495 int dir;
4496 long dist;
4497{
4498 int linelen = linetabsize(ml_get_curline());
4499 int retval = OK;
4500 int atend = FALSE;
4501 int n;
4502 int col_off1; /* margin offset for first screen line */
4503 int col_off2; /* margin offset for wrapped screen line */
4504 int width1; /* text width for first screen line */
4505 int width2; /* test width for wrapped screen line */
4506
4507 oap->motion_type = MCHAR;
4508 oap->inclusive = FALSE;
4509
4510 col_off1 = curwin_col_off();
4511 col_off2 = col_off1 - curwin_col_off2();
4512 width1 = W_WIDTH(curwin) - col_off1;
4513 width2 = W_WIDTH(curwin) - col_off2;
4514
4515#ifdef FEAT_VERTSPLIT
4516 if (curwin->w_width != 0)
4517 {
4518#endif
4519 /*
4520 * Instead of sticking at the last character of the buffer line we
4521 * try to stick in the last column of the screen.
4522 */
4523 if (curwin->w_curswant == MAXCOL)
4524 {
4525 atend = TRUE;
4526 validate_virtcol();
4527 if (width1 <= 0)
4528 curwin->w_curswant = 0;
4529 else
4530 {
4531 curwin->w_curswant = width1 - 1;
4532 if (curwin->w_virtcol > curwin->w_curswant)
4533 curwin->w_curswant += ((curwin->w_virtcol
4534 - curwin->w_curswant - 1) / width2 + 1) * width2;
4535 }
4536 }
4537 else
4538 {
4539 if (linelen > width1)
4540 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4541 else
4542 n = width1;
4543 if (curwin->w_curswant > (colnr_T)n + 1)
4544 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4545 * width2;
4546 }
4547
4548 while (dist--)
4549 {
4550 if (dir == BACKWARD)
4551 {
4552 if ((long)curwin->w_curswant >= width2)
4553 /* move back within line */
4554 curwin->w_curswant -= width2;
4555 else
4556 {
4557 /* to previous line */
4558 if (curwin->w_cursor.lnum == 1)
4559 {
4560 retval = FAIL;
4561 break;
4562 }
4563 --curwin->w_cursor.lnum;
4564#ifdef FEAT_FOLDING
4565 /* Move to the start of a closed fold. Don't do that when
4566 * 'foldopen' contains "all": it will open in a moment. */
4567 if (!(fdo_flags & FDO_ALL))
4568 (void)hasFolding(curwin->w_cursor.lnum,
4569 &curwin->w_cursor.lnum, NULL);
4570#endif
4571 linelen = linetabsize(ml_get_curline());
4572 if (linelen > width1)
4573 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4574 + 1) * width2;
4575 }
4576 }
4577 else /* dir == FORWARD */
4578 {
4579 if (linelen > width1)
4580 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4581 else
4582 n = width1;
4583 if (curwin->w_curswant + width2 < (colnr_T)n)
4584 /* move forward within line */
4585 curwin->w_curswant += width2;
4586 else
4587 {
4588 /* to next line */
4589#ifdef FEAT_FOLDING
4590 /* Move to the end of a closed fold. */
4591 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4592 &curwin->w_cursor.lnum);
4593#endif
4594 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4595 {
4596 retval = FAIL;
4597 break;
4598 }
4599 curwin->w_cursor.lnum++;
4600 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004601 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604 }
4605#ifdef FEAT_VERTSPLIT
4606 }
4607#endif
4608
4609 coladvance(curwin->w_curswant);
4610
4611#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4612 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4613 {
4614 /*
4615 * Check for landing on a character that got split at the end of the
4616 * last line. We want to advance a screenline, not end up in the same
4617 * screenline or move two screenlines.
4618 */
4619 validate_virtcol();
4620 if (curwin->w_virtcol > curwin->w_curswant
4621 && (curwin->w_curswant < (colnr_T)width1
4622 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4623 : ((curwin->w_curswant - width1) % width2
4624 > (colnr_T)width2 / 2)))
4625 --curwin->w_cursor.col;
4626 }
4627#endif
4628
4629 if (atend)
4630 curwin->w_curswant = MAXCOL; /* stick in the last column */
4631
4632 return retval;
4633}
4634
4635#ifdef FEAT_MOUSE
4636/*
4637 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4638 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004639 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4640 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 */
4642 static void
4643nv_mousescroll(cap)
4644 cmdarg_T *cap;
4645{
4646# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004647 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 /* Currently we only get the mouse coordinates in the GUI. */
4650 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4651 {
4652 int row, col;
4653
4654 row = mouse_row;
4655 col = mouse_col;
4656
4657 /* find the window at the pointer coordinates */
4658 curwin = mouse_find_win(&row, &col);
4659 curbuf = curwin->w_buffer;
4660 }
4661# endif
4662
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004665 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4666 {
4667 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4668 }
4669 else
4670 {
4671 cap->count1 = 3;
4672 cap->count0 = 3;
4673 nv_scroll_line(cap);
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004676# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
4678 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004679 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4680 if (!curwin->w_p_wrap)
4681 {
4682 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004683
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004684 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4685 step = W_WIDTH(curwin);
4686 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4687 if (val < 0)
4688 val = 0;
4689
4690 gui_do_horiz_scroll(val, TRUE);
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4696 curwin->w_redr_status = TRUE;
4697
4698 curwin = old_curwin;
4699 curbuf = curwin->w_buffer;
4700# endif
4701}
4702
4703/*
4704 * Mouse clicks and drags.
4705 */
4706 static void
4707nv_mouse(cap)
4708 cmdarg_T *cap;
4709{
4710 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4711}
4712#endif
4713
4714/*
4715 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4716 * cap->arg must be TRUE for CTRL-E.
4717 */
4718 static void
4719nv_scroll_line(cap)
4720 cmdarg_T *cap;
4721{
4722 if (!checkclearop(cap->oap))
4723 scroll_redraw(cap->arg, cap->count1);
4724}
4725
4726/*
4727 * Scroll "count" lines up or down, and redraw.
4728 */
4729 void
4730scroll_redraw(up, count)
4731 int up;
4732 long count;
4733{
4734 linenr_T prev_topline = curwin->w_topline;
4735#ifdef FEAT_DIFF
4736 int prev_topfill = curwin->w_topfill;
4737#endif
4738 linenr_T prev_lnum = curwin->w_cursor.lnum;
4739
4740 if (up)
4741 scrollup(count, TRUE);
4742 else
4743 scrolldown(count, TRUE);
4744 if (p_so)
4745 {
4746 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4747 * valid, otherwise the screen jumps back at the end of the file. */
4748 cursor_correct();
4749 check_cursor_moved(curwin);
4750 curwin->w_valid |= VALID_TOPLINE;
4751
4752 /* If moved back to where we were, at least move the cursor, otherwise
4753 * we get stuck at one position. Don't move the cursor up if the
4754 * first line of the buffer is already on the screen */
4755 while (curwin->w_topline == prev_topline
4756#ifdef FEAT_DIFF
4757 && curwin->w_topfill == prev_topfill
4758#endif
4759 )
4760 {
4761 if (up)
4762 {
4763 if (curwin->w_cursor.lnum > prev_lnum
4764 || cursor_down(1L, FALSE) == FAIL)
4765 break;
4766 }
4767 else
4768 {
4769 if (curwin->w_cursor.lnum < prev_lnum
4770 || prev_topline == 1L
4771 || cursor_up(1L, FALSE) == FAIL)
4772 break;
4773 }
4774 /* Mark w_topline as valid, otherwise the screen jumps back at the
4775 * end of the file. */
4776 check_cursor_moved(curwin);
4777 curwin->w_valid |= VALID_TOPLINE;
4778 }
4779 }
4780 if (curwin->w_cursor.lnum != prev_lnum)
4781 coladvance(curwin->w_curswant);
4782 redraw_later(VALID);
4783}
4784
4785/*
4786 * Commands that start with "z".
4787 */
4788 static void
4789nv_zet(cap)
4790 cmdarg_T *cap;
4791{
4792 long n;
4793 colnr_T col;
4794 int nchar = cap->nchar;
4795#ifdef FEAT_FOLDING
4796 long old_fdl = curwin->w_p_fdl;
4797 int old_fen = curwin->w_p_fen;
4798#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004799#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004800 int undo = FALSE;
4801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 if (VIM_ISDIGIT(nchar))
4804 {
4805 /*
4806 * "z123{nchar}": edit the count before obtaining {nchar}
4807 */
4808 if (checkclearop(cap->oap))
4809 return;
4810 n = nchar - '0';
4811 for (;;)
4812 {
4813#ifdef USE_ON_FLY_SCROLL
4814 dont_scroll = TRUE; /* disallow scrolling here */
4815#endif
4816 ++no_mapping;
4817 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004818 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 --no_mapping;
4821 --allow_keys;
4822#ifdef FEAT_CMDL_INFO
4823 (void)add_to_showcmd(nchar);
4824#endif
4825 if (nchar == K_DEL || nchar == K_KDEL)
4826 n /= 10;
4827 else if (VIM_ISDIGIT(nchar))
4828 n = n * 10 + (nchar - '0');
4829 else if (nchar == CAR)
4830 {
4831#ifdef FEAT_GUI
4832 need_mouse_correct = TRUE;
4833#endif
4834 win_setheight((int)n);
4835 break;
4836 }
4837 else if (nchar == 'l'
4838 || nchar == 'h'
4839 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004840 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 cap->count1 = n ? n * cap->count1 : cap->count1;
4843 goto dozet;
4844 }
4845 else
4846 {
4847 clearopbeep(cap->oap);
4848 break;
4849 }
4850 }
4851 cap->oap->op_type = OP_NOP;
4852 return;
4853 }
4854
4855dozet:
4856 if (
4857#ifdef FEAT_FOLDING
4858 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4859 * and "zC" only in Visual mode. "zj" and "zk" are motion
4860 * commands. */
4861 cap->nchar != 'f' && cap->nchar != 'F'
4862 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4863 && cap->nchar != 'j' && cap->nchar != 'k'
4864 &&
4865#endif
4866 checkclearop(cap->oap))
4867 return;
4868
4869 /*
4870 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4871 * If line number given, set cursor.
4872 */
4873 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4874 && cap->count0
4875 && cap->count0 != curwin->w_cursor.lnum)
4876 {
4877 setpcmark();
4878 if (cap->count0 > curbuf->b_ml.ml_line_count)
4879 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4880 else
4881 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004882 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884
4885 switch (nchar)
4886 {
4887 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4888 case '+':
4889 if (cap->count0 == 0)
4890 {
4891 /* No count given: put cursor at the line below screen */
4892 validate_botline(); /* make sure w_botline is valid */
4893 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4894 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4895 else
4896 curwin->w_cursor.lnum = curwin->w_botline;
4897 }
4898 /* FALLTHROUGH */
4899 case NL:
4900 case CAR:
4901 case K_KENTER:
4902 beginline(BL_WHITE | BL_FIX);
4903 /* FALLTHROUGH */
4904
4905 case 't': scroll_cursor_top(0, TRUE);
4906 redraw_later(VALID);
4907 break;
4908
4909 /* "z." and "zz": put cursor in middle of screen */
4910 case '.': beginline(BL_WHITE | BL_FIX);
4911 /* FALLTHROUGH */
4912
4913 case 'z': scroll_cursor_halfway(TRUE);
4914 redraw_later(VALID);
4915 break;
4916
4917 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4918 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4919 * when <count> is at bottom of window, and puts that one at
4920 * bottom of window. */
4921 if (cap->count0 != 0)
4922 {
4923 scroll_cursor_bot(0, TRUE);
4924 curwin->w_cursor.lnum = curwin->w_topline;
4925 }
4926 else if (curwin->w_topline == 1)
4927 curwin->w_cursor.lnum = 1;
4928 else
4929 curwin->w_cursor.lnum = curwin->w_topline - 1;
4930 /* FALLTHROUGH */
4931 case '-':
4932 beginline(BL_WHITE | BL_FIX);
4933 /* FALLTHROUGH */
4934
4935 case 'b': scroll_cursor_bot(0, TRUE);
4936 redraw_later(VALID);
4937 break;
4938
4939 /* "zH" - scroll screen right half-page */
4940 case 'H':
4941 cap->count1 *= W_WIDTH(curwin) / 2;
4942 /* FALLTHROUGH */
4943
4944 /* "zh" - scroll screen to the right */
4945 case 'h':
4946 case K_LEFT:
4947 if (!curwin->w_p_wrap)
4948 {
4949 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4950 curwin->w_leftcol = 0;
4951 else
4952 curwin->w_leftcol -= (colnr_T)cap->count1;
4953 leftcol_changed();
4954 }
4955 break;
4956
4957 /* "zL" - scroll screen left half-page */
4958 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4959 /* FALLTHROUGH */
4960
4961 /* "zl" - scroll screen to the left */
4962 case 'l':
4963 case K_RIGHT:
4964 if (!curwin->w_p_wrap)
4965 {
4966 /* scroll the window left */
4967 curwin->w_leftcol += (colnr_T)cap->count1;
4968 leftcol_changed();
4969 }
4970 break;
4971
4972 /* "zs" - scroll screen, cursor at the start */
4973 case 's': if (!curwin->w_p_wrap)
4974 {
4975#ifdef FEAT_FOLDING
4976 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4977 col = 0; /* like the cursor is in col 0 */
4978 else
4979#endif
4980 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4981 if ((long)col > p_siso)
4982 col -= p_siso;
4983 else
4984 col = 0;
4985 if (curwin->w_leftcol != col)
4986 {
4987 curwin->w_leftcol = col;
4988 redraw_later(NOT_VALID);
4989 }
4990 }
4991 break;
4992
4993 /* "ze" - scroll screen, cursor at the end */
4994 case 'e': if (!curwin->w_p_wrap)
4995 {
4996#ifdef FEAT_FOLDING
4997 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4998 col = 0; /* like the cursor is in col 0 */
4999 else
5000#endif
5001 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5002 n = W_WIDTH(curwin) - curwin_col_off();
5003 if ((long)col + p_siso < n)
5004 col = 0;
5005 else
5006 col = col + p_siso - n + 1;
5007 if (curwin->w_leftcol != col)
5008 {
5009 curwin->w_leftcol = col;
5010 redraw_later(NOT_VALID);
5011 }
5012 }
5013 break;
5014
5015#ifdef FEAT_FOLDING
5016 /* "zF": create fold command */
5017 /* "zf": create fold operator */
5018 case 'F':
5019 case 'f': if (foldManualAllowed(TRUE))
5020 {
5021 cap->nchar = 'f';
5022 nv_operator(cap);
5023 curwin->w_p_fen = TRUE;
5024
5025 /* "zF" is like "zfzf" */
5026 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5027 {
5028 nv_operator(cap);
5029 finish_op = TRUE;
5030 }
5031 }
5032 else
5033 clearopbeep(cap->oap);
5034 break;
5035
5036 /* "zd": delete fold at cursor */
5037 /* "zD": delete fold at cursor recursively */
5038 case 'd':
5039 case 'D': if (foldManualAllowed(FALSE))
5040 {
5041 if (VIsual_active)
5042 nv_operator(cap);
5043 else
5044 deleteFold(curwin->w_cursor.lnum,
5045 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5046 }
5047 break;
5048
5049 /* "zE": erease all folds */
5050 case 'E': if (foldmethodIsManual(curwin))
5051 {
5052 clearFolding(curwin);
5053 changed_window_setting();
5054 }
5055 else if (foldmethodIsMarker(curwin))
5056 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5057 TRUE, FALSE);
5058 else
5059 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5060 break;
5061
5062 /* "zn": fold none: reset 'foldenable' */
5063 case 'n': curwin->w_p_fen = FALSE;
5064 break;
5065
5066 /* "zN": fold Normal: set 'foldenable' */
5067 case 'N': curwin->w_p_fen = TRUE;
5068 break;
5069
5070 /* "zi": invert folding: toggle 'foldenable' */
5071 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5072 break;
5073
5074 /* "za": open closed fold or close open fold at cursor */
5075 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5076 openFold(curwin->w_cursor.lnum, cap->count1);
5077 else
5078 {
5079 closeFold(curwin->w_cursor.lnum, cap->count1);
5080 curwin->w_p_fen = TRUE;
5081 }
5082 break;
5083
5084 /* "zA": open fold at cursor recursively */
5085 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5086 openFoldRecurse(curwin->w_cursor.lnum);
5087 else
5088 {
5089 closeFoldRecurse(curwin->w_cursor.lnum);
5090 curwin->w_p_fen = TRUE;
5091 }
5092 break;
5093
5094 /* "zo": open fold at cursor or Visual area */
5095 case 'o': if (VIsual_active)
5096 nv_operator(cap);
5097 else
5098 openFold(curwin->w_cursor.lnum, cap->count1);
5099 break;
5100
5101 /* "zO": open fold recursively */
5102 case 'O': if (VIsual_active)
5103 nv_operator(cap);
5104 else
5105 openFoldRecurse(curwin->w_cursor.lnum);
5106 break;
5107
5108 /* "zc": close fold at cursor or Visual area */
5109 case 'c': if (VIsual_active)
5110 nv_operator(cap);
5111 else
5112 closeFold(curwin->w_cursor.lnum, cap->count1);
5113 curwin->w_p_fen = TRUE;
5114 break;
5115
5116 /* "zC": close fold recursively */
5117 case 'C': if (VIsual_active)
5118 nv_operator(cap);
5119 else
5120 closeFoldRecurse(curwin->w_cursor.lnum);
5121 curwin->w_p_fen = TRUE;
5122 break;
5123
5124 /* "zv": open folds at the cursor */
5125 case 'v': foldOpenCursor();
5126 break;
5127
5128 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5129 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005130 curwin->w_foldinvalid = TRUE; /* recompute folds */
5131 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 foldOpenCursor();
5133 break;
5134
5135 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5136 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005137 curwin->w_foldinvalid = TRUE; /* recompute folds */
5138 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140
5141 /* "zm": fold more */
5142 case 'm': if (curwin->w_p_fdl > 0)
5143 --curwin->w_p_fdl;
5144 old_fdl = -1; /* force an update */
5145 curwin->w_p_fen = TRUE;
5146 break;
5147
5148 /* "zM": close all folds */
5149 case 'M': curwin->w_p_fdl = 0;
5150 old_fdl = -1; /* force an update */
5151 curwin->w_p_fen = TRUE;
5152 break;
5153
5154 /* "zr": reduce folding */
5155 case 'r': ++curwin->w_p_fdl;
5156 break;
5157
5158 /* "zR": open all folds */
5159 case 'R': curwin->w_p_fdl = getDeepestNesting();
5160 old_fdl = -1; /* force an update */
5161 break;
5162
5163 case 'j': /* "zj" move to next fold downwards */
5164 case 'k': /* "zk" move to next fold upwards */
5165 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5166 cap->count1) == FAIL)
5167 clearopbeep(cap->oap);
5168 break;
5169
5170#endif /* FEAT_FOLDING */
5171
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005172#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005173 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5174 ++no_mapping;
5175 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005176 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005177 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005178 --no_mapping;
5179 --allow_keys;
5180#ifdef FEAT_CMDL_INFO
5181 (void)add_to_showcmd(nchar);
5182#endif
5183 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5184 {
5185 clearopbeep(cap->oap);
5186 break;
5187 }
5188 undo = TRUE;
5189 /*FALLTHROUGH*/
5190
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 case 'g': /* "zg": add good word to word list */
5192 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005193 case 'G': /* "zG": add good word to temp word list */
5194 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005195 {
5196 char_u *ptr = NULL;
5197 int len;
5198
5199 if (checkclearop(cap->oap))
5200 break;
5201# ifdef FEAT_VISUAL
5202 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5203 == FAIL)
5204 return;
5205# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005206 if (ptr == NULL)
5207 {
5208 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005209
5210 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005211 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005212 if (len != 0 && curwin->w_cursor.col <= pos.col)
5213 ptr = ml_get_pos(&curwin->w_cursor);
5214 curwin->w_cursor = pos;
5215 }
5216
Bram Moolenaarb765d632005-06-07 21:00:02 +00005217 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5218 FIND_IDENT)) == 0)
5219 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005220 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005221 (nchar == 'G' || nchar == 'W')
5222 ? 0 : (int)cap->count1,
5223 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005224 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005225 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226
Bram Moolenaar43abc522005-12-10 20:15:02 +00005227 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005228 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005229 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005231#endif
5232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 default: clearopbeep(cap->oap);
5234 }
5235
5236#ifdef FEAT_FOLDING
5237 /* Redraw when 'foldenable' changed */
5238 if (old_fen != curwin->w_p_fen)
5239 {
5240# ifdef FEAT_DIFF
5241 win_T *wp;
5242
5243 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5244 {
5245 /* Adjust 'foldenable' in diff-synced windows. */
5246 FOR_ALL_WINDOWS(wp)
5247 {
5248 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5249 {
5250 wp->w_p_fen = curwin->w_p_fen;
5251 changed_window_setting_win(wp);
5252 }
5253 }
5254 }
5255# endif
5256 changed_window_setting();
5257 }
5258
5259 /* Redraw when 'foldlevel' changed. */
5260 if (old_fdl != curwin->w_p_fdl)
5261 newFoldLevel();
5262#endif
5263}
5264
5265#ifdef FEAT_GUI
5266/*
5267 * Vertical scrollbar movement.
5268 */
5269 static void
5270nv_ver_scrollbar(cap)
5271 cmdarg_T *cap;
5272{
5273 if (cap->oap->op_type != OP_NOP)
5274 clearopbeep(cap->oap);
5275
5276 /* Even if an operator was pending, we still want to scroll */
5277 gui_do_scroll();
5278}
5279
5280/*
5281 * Horizontal scrollbar movement.
5282 */
5283 static void
5284nv_hor_scrollbar(cap)
5285 cmdarg_T *cap;
5286{
5287 if (cap->oap->op_type != OP_NOP)
5288 clearopbeep(cap->oap);
5289
5290 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005291 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292}
5293#endif
5294
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005295#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005296/*
5297 * Click in GUI tab.
5298 */
5299 static void
5300nv_tabline(cap)
5301 cmdarg_T *cap;
5302{
5303 if (cap->oap->op_type != OP_NOP)
5304 clearopbeep(cap->oap);
5305
5306 /* Even if an operator was pending, we still want to jump tabs. */
5307 goto_tabpage(current_tab);
5308}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005309
5310/*
5311 * Selected item in tab line menu.
5312 */
5313 static void
5314nv_tabmenu(cap)
5315 cmdarg_T *cap;
5316{
5317 if (cap->oap->op_type != OP_NOP)
5318 clearopbeep(cap->oap);
5319
5320 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005321 handle_tabmenu();
5322}
5323
5324/*
5325 * Handle selecting an item of the GUI tab line menu.
5326 * Used in Normal and Insert mode.
5327 */
5328 void
5329handle_tabmenu()
5330{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005331 switch (current_tabmenu)
5332 {
5333 case TABLINE_MENU_CLOSE:
5334 if (current_tab == 0)
5335 do_cmdline_cmd((char_u *)"tabclose");
5336 else
5337 {
5338 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5339 current_tab);
5340 do_cmdline_cmd(IObuff);
5341 }
5342 break;
5343
5344 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005345 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5346 current_tab > 0 ? current_tab - 1 : 999);
5347 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005348 break;
5349
5350 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005351 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5352 current_tab > 0 ? current_tab - 1 : 999);
5353 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005354 break;
5355 }
5356}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005357#endif
5358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359/*
5360 * "Q" command.
5361 */
5362 static void
5363nv_exmode(cap)
5364 cmdarg_T *cap;
5365{
5366 /*
5367 * Ignore 'Q' in Visual mode, just give a beep.
5368 */
5369#ifdef FEAT_VISUAL
5370 if (VIsual_active)
5371 vim_beep();
5372 else
5373#endif
5374 if (!checkclearop(cap->oap))
5375 do_exmode(FALSE);
5376}
5377
5378/*
5379 * Handle a ":" command.
5380 */
5381 static void
5382nv_colon(cap)
5383 cmdarg_T *cap;
5384{
5385 int old_p_im;
5386
5387#ifdef FEAT_VISUAL
5388 if (VIsual_active)
5389 nv_operator(cap);
5390 else
5391#endif
5392 {
5393 if (cap->oap->op_type != OP_NOP)
5394 {
5395 /* Using ":" as a movement is characterwise exclusive. */
5396 cap->oap->motion_type = MCHAR;
5397 cap->oap->inclusive = FALSE;
5398 }
5399 else if (cap->count0)
5400 {
5401 /* translate "count:" into ":.,.+(count - 1)" */
5402 stuffcharReadbuff('.');
5403 if (cap->count0 > 1)
5404 {
5405 stuffReadbuff((char_u *)",.+");
5406 stuffnumReadbuff((long)cap->count0 - 1L);
5407 }
5408 }
5409
5410 /* When typing, don't type below an old message */
5411 if (KeyTyped)
5412 compute_cmdrow();
5413
5414 old_p_im = p_im;
5415
5416 /* get a command line and execute it */
5417 do_cmdline(NULL, getexline, NULL,
5418 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5419
5420 /* If 'insertmode' changed, enter or exit Insert mode */
5421 if (p_im != old_p_im)
5422 {
5423 if (p_im)
5424 restart_edit = 'i';
5425 else
5426 restart_edit = 0;
5427 }
5428
5429 /* The start of the operator may have become invalid by the Ex
5430 * command. */
5431 if (cap->oap->op_type != OP_NOP
5432 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5433 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005434 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 clearopbeep(cap->oap);
5436 }
5437}
5438
5439/*
5440 * Handle CTRL-G command.
5441 */
5442 static void
5443nv_ctrlg(cap)
5444 cmdarg_T *cap;
5445{
5446#ifdef FEAT_VISUAL
5447 if (VIsual_active) /* toggle Selection/Visual mode */
5448 {
5449 VIsual_select = !VIsual_select;
5450 showmode();
5451 }
5452 else
5453#endif
5454 if (!checkclearop(cap->oap))
5455 /* print full name if count given or :cd used */
5456 fileinfo((int)cap->count0, FALSE, TRUE);
5457}
5458
5459/*
5460 * Handle CTRL-H <Backspace> command.
5461 */
5462 static void
5463nv_ctrlh(cap)
5464 cmdarg_T *cap;
5465{
5466#ifdef FEAT_VISUAL
5467 if (VIsual_active && VIsual_select)
5468 {
5469 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5470 v_visop(cap);
5471 }
5472 else
5473#endif
5474 nv_left(cap);
5475}
5476
5477/*
5478 * CTRL-L: clear screen and redraw.
5479 */
5480 static void
5481nv_clear(cap)
5482 cmdarg_T *cap;
5483{
5484 if (!checkclearop(cap->oap))
5485 {
5486#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5487 /*
5488 * Right now, the BeBox doesn't seem to have an easy way to detect
5489 * window resizing, so we cheat and make the user detect it
5490 * manually with CTRL-L instead
5491 */
5492 ui_get_shellsize();
5493#endif
5494#ifdef FEAT_SYN_HL
5495 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005496 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#endif
5498 redraw_later(CLEAR);
5499 }
5500}
5501
5502/*
5503 * CTRL-O: In Select mode: switch to Visual mode for one command.
5504 * Otherwise: Go to older pcmark.
5505 */
5506 static void
5507nv_ctrlo(cap)
5508 cmdarg_T *cap;
5509{
5510#ifdef FEAT_VISUAL
5511 if (VIsual_active && VIsual_select)
5512 {
5513 VIsual_select = FALSE;
5514 showmode();
5515 restart_VIsual_select = 2; /* restart Select mode later */
5516 }
5517 else
5518#endif
5519 {
5520 cap->count1 = -cap->count1;
5521 nv_pcmark(cap);
5522 }
5523}
5524
5525/*
5526 * CTRL-^ command, short for ":e #"
5527 */
5528 static void
5529nv_hat(cap)
5530 cmdarg_T *cap;
5531{
5532 if (!checkclearopq(cap->oap))
5533 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5534 GETF_SETMARK|GETF_ALT, FALSE);
5535}
5536
5537/*
5538 * "Z" commands.
5539 */
5540 static void
5541nv_Zet(cap)
5542 cmdarg_T *cap;
5543{
5544 if (!checkclearopq(cap->oap))
5545 {
5546 switch (cap->nchar)
5547 {
5548 /* "ZZ": equivalent to ":x". */
5549 case 'Z': do_cmdline_cmd((char_u *)"x");
5550 break;
5551
5552 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5553 case 'Q': do_cmdline_cmd((char_u *)"q!");
5554 break;
5555
5556 default: clearopbeep(cap->oap);
5557 }
5558 }
5559}
5560
5561#if defined(FEAT_WINDOWS) || defined(PROTO)
5562/*
5563 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5564 */
5565 void
5566do_nv_ident(c1, c2)
5567 int c1;
5568 int c2;
5569{
5570 oparg_T oa;
5571 cmdarg_T ca;
5572
5573 clear_oparg(&oa);
5574 vim_memset(&ca, 0, sizeof(ca));
5575 ca.oap = &oa;
5576 ca.cmdchar = c1;
5577 ca.nchar = c2;
5578 nv_ident(&ca);
5579}
5580#endif
5581
5582/*
5583 * Handle the commands that use the word under the cursor.
5584 * [g] CTRL-] :ta to current identifier
5585 * [g] 'K' run program for current identifier
5586 * [g] '*' / to current identifier or string
5587 * [g] '#' ? to current identifier or string
5588 * g ']' :tselect for current identifier
5589 */
5590 static void
5591nv_ident(cap)
5592 cmdarg_T *cap;
5593{
5594 char_u *ptr = NULL;
5595 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005596 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 char_u *p;
5598 char_u *kp; /* value of 'keywordprg' */
5599 int kp_help; /* 'keywordprg' is ":help" */
5600 int n = 0; /* init for GCC */
5601 int cmdchar;
5602 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005603 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 char_u *aux_ptr;
5605 int isman;
5606 int isman_s;
5607
5608 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5609 {
5610 cmdchar = cap->nchar;
5611 g_cmd = TRUE;
5612 }
5613 else
5614 {
5615 cmdchar = cap->cmdchar;
5616 g_cmd = FALSE;
5617 }
5618
5619 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5620 cmdchar = '#';
5621
5622 /*
5623 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5624 */
5625 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5626 {
5627#ifdef FEAT_VISUAL
5628 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5629 return;
5630#endif
5631 if (checkclearopq(cap->oap))
5632 return;
5633 }
5634
5635 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5636 (cmdchar == '*' || cmdchar == '#')
5637 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5638 {
5639 clearop(cap->oap);
5640 return;
5641 }
5642
5643 /* Allocate buffer to put the command in. Inserting backslashes can
5644 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5645 * and some numbers. */
5646 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5647 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5648 || STRCMP(kp, ":help") == 0);
5649 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5650 if (buf == NULL)
5651 return;
5652 buf[0] = NUL;
5653
5654 switch (cmdchar)
5655 {
5656 case '*':
5657 case '#':
5658 /*
5659 * Put cursor at start of word, makes search skip the word
5660 * under the cursor.
5661 * Call setpcmark() first, so "*``" puts the cursor back where
5662 * it was.
5663 */
5664 setpcmark();
5665 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5666
5667 if (!g_cmd && vim_iswordp(ptr))
5668 STRCPY(buf, "\\<");
5669 no_smartcase = TRUE; /* don't use 'smartcase' now */
5670 break;
5671
5672 case 'K':
5673 if (kp_help)
5674 STRCPY(buf, "he! ");
5675 else
5676 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005677 /* An external command will probably use an argument starting
5678 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005679 while (*ptr == '-' && n > 0)
5680 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005681 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005682 --n;
5683 }
5684 if (n == 0)
5685 {
5686 EMSG(_(e_noident)); /* found dashes only */
5687 vim_free(buf);
5688 return;
5689 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 /* When a count is given, turn it into a range. Is this
5692 * really what we want? */
5693 isman = (STRCMP(kp, "man") == 0);
5694 isman_s = (STRCMP(kp, "man -s") == 0);
5695 if (cap->count0 != 0 && !(isman || isman_s))
5696 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5697
5698 STRCAT(buf, "! ");
5699 if (cap->count0 == 0 && isman_s)
5700 STRCAT(buf, "man");
5701 else
5702 STRCAT(buf, kp);
5703 STRCAT(buf, " ");
5704 if (cap->count0 != 0 && (isman || isman_s))
5705 {
5706 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5707 STRCAT(buf, " ");
5708 }
5709 }
5710 break;
5711
5712 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005713 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714#ifdef FEAT_CSCOPE
5715 if (p_cst)
5716 STRCPY(buf, "cstag ");
5717 else
5718#endif
5719 STRCPY(buf, "ts ");
5720 break;
5721
5722 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005723 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 if (curbuf->b_help)
5725 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005727 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005728 if (g_cmd)
5729 STRCPY(buf, "tj ");
5730 else
5731 sprintf((char *)buf, "%ldta ", cap->count0);
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734
5735 /*
5736 * Now grab the chars in the identifier
5737 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005738 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005740 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005741 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005743 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005746 vim_free(buf);
5747 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005749 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5750 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 {
5752 vim_free(buf);
5753 vim_free(p);
5754 return;
5755 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005756 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 STRCAT(buf, p);
5758 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005760 else
5761 {
5762 if (cmdchar == '*')
5763 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5764 else if (cmdchar == '#')
5765 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005766 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005767 {
5768 if (curbuf->b_help)
5769 /* ":help" handles unescaped argument */
5770 aux_ptr = (char_u *)"";
5771 else
5772 aux_ptr = (char_u *)"\\|\"\n[";
5773 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005774 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005775 aux_ptr = (char_u *)"\\|\"\n*?[";
5776
5777 p = buf + STRLEN(buf);
5778 while (n-- > 0)
5779 {
5780 /* put a backslash before \ and some others */
5781 if (vim_strchr(aux_ptr, *ptr) != NULL)
5782 *p++ = '\\';
5783#ifdef FEAT_MBYTE
5784 /* When current byte is a part of multibyte character, copy all
5785 * bytes of that character. */
5786 if (has_mbyte)
5787 {
5788 int i;
5789 int len = (*mb_ptr2len)(ptr) - 1;
5790
5791 for (i = 0; i < len && n >= 1; ++i, --n)
5792 *p++ = *ptr++;
5793 }
5794#endif
5795 *p++ = *ptr++;
5796 }
5797 *p = NUL;
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 /*
5801 * Execute the command.
5802 */
5803 if (cmdchar == '*' || cmdchar == '#')
5804 {
5805 if (!g_cmd && (
5806#ifdef FEAT_MBYTE
5807 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5808#endif
5809 vim_iswordc(ptr[-1])))
5810 STRCAT(buf, "\\>");
5811#ifdef FEAT_CMDHIST
5812 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005813 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5815#endif
5816 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5817 }
5818 else
5819 do_cmdline_cmd(buf);
5820
5821 vim_free(buf);
5822}
5823
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005824#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825/*
5826 * Get visually selected text, within one line only.
5827 * Returns FAIL if more than one line selected.
5828 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005829 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830get_visual_text(cap, pp, lenp)
5831 cmdarg_T *cap;
5832 char_u **pp; /* return: start of selected text */
5833 int *lenp; /* return: length of selected text */
5834{
5835 if (VIsual_mode != 'V')
5836 unadjust_for_sel();
5837 if (VIsual.lnum != curwin->w_cursor.lnum)
5838 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005839 if (cap != NULL)
5840 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return FAIL;
5842 }
5843 if (VIsual_mode == 'V')
5844 {
5845 *pp = ml_get_curline();
5846 *lenp = (int)STRLEN(*pp);
5847 }
5848 else
5849 {
5850 if (lt(curwin->w_cursor, VIsual))
5851 {
5852 *pp = ml_get_pos(&curwin->w_cursor);
5853 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5854 }
5855 else
5856 {
5857 *pp = ml_get_pos(&VIsual);
5858 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5859 }
5860#ifdef FEAT_MBYTE
5861 if (has_mbyte)
5862 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005863 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#endif
5865 }
5866 reset_VIsual_and_resel();
5867 return OK;
5868}
5869#endif
5870
5871/*
5872 * CTRL-T: backwards in tag stack
5873 */
5874 static void
5875nv_tagpop(cap)
5876 cmdarg_T *cap;
5877{
5878 if (!checkclearopq(cap->oap))
5879 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5880}
5881
5882/*
5883 * Handle scrolling command 'H', 'L' and 'M'.
5884 */
5885 static void
5886nv_scroll(cap)
5887 cmdarg_T *cap;
5888{
5889 int used = 0;
5890 long n;
5891#ifdef FEAT_FOLDING
5892 linenr_T lnum;
5893#endif
5894 int half;
5895
5896 cap->oap->motion_type = MLINE;
5897 setpcmark();
5898
5899 if (cap->cmdchar == 'L')
5900 {
5901 validate_botline(); /* make sure curwin->w_botline is valid */
5902 curwin->w_cursor.lnum = curwin->w_botline - 1;
5903 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5904 curwin->w_cursor.lnum = 1;
5905 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005906 {
5907#ifdef FEAT_FOLDING
5908 if (hasAnyFolding(curwin))
5909 {
5910 /* Count a fold for one screen line. */
5911 for (n = cap->count1 - 1; n > 0
5912 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5913 {
5914 (void)hasFolding(curwin->w_cursor.lnum,
5915 &curwin->w_cursor.lnum, NULL);
5916 --curwin->w_cursor.lnum;
5917 }
5918 }
5919 else
5920#endif
5921 curwin->w_cursor.lnum -= cap->count1 - 1;
5922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
5924 else
5925 {
5926 if (cap->cmdchar == 'M')
5927 {
5928#ifdef FEAT_DIFF
5929 /* Don't count filler lines above the window. */
5930 used -= diff_check_fill(curwin, curwin->w_topline)
5931 - curwin->w_topfill;
5932#endif
5933 validate_botline(); /* make sure w_empty_rows is valid */
5934 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5935 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5936 {
5937#ifdef FEAT_DIFF
5938 /* Count half he number of filler lines to be "below this
5939 * line" and half to be "above the next line". */
5940 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5941 + n) / 2 >= half)
5942 {
5943 --n;
5944 break;
5945 }
5946#endif
5947 used += plines(curwin->w_topline + n);
5948 if (used >= half)
5949 break;
5950#ifdef FEAT_FOLDING
5951 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5952 n = lnum - curwin->w_topline;
5953#endif
5954 }
5955 if (n > 0 && used > curwin->w_height)
5956 --n;
5957 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005958 else /* (cap->cmdchar == 'H') */
5959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005961#ifdef FEAT_FOLDING
5962 if (hasAnyFolding(curwin))
5963 {
5964 /* Count a fold for one screen line. */
5965 lnum = curwin->w_topline;
5966 while (n-- > 0 && lnum < curwin->w_botline - 1)
5967 {
5968 hasFolding(lnum, NULL, &lnum);
5969 ++lnum;
5970 }
5971 n = lnum - curwin->w_topline;
5972 }
5973#endif
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 curwin->w_cursor.lnum = curwin->w_topline + n;
5976 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5977 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5978 }
5979
5980 cursor_correct(); /* correct for 'so' */
5981 beginline(BL_SOL | BL_FIX);
5982}
5983
5984/*
5985 * Cursor right commands.
5986 */
5987 static void
5988nv_right(cap)
5989 cmdarg_T *cap;
5990{
5991 long n;
5992#ifdef FEAT_VISUAL
5993 int PAST_LINE;
5994#else
5995# define PAST_LINE 0
5996#endif
5997
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005998 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5999 {
6000 /* <C-Right> and <S-Right> move a word or WORD right */
6001 if (mod_mask & MOD_MASK_CTRL)
6002 cap->arg = TRUE;
6003 nv_wordcmd(cap);
6004 return;
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 cap->oap->motion_type = MCHAR;
6008 cap->oap->inclusive = FALSE;
6009#ifdef FEAT_VISUAL
6010 PAST_LINE = (VIsual_active && *p_sel != 'o');
6011
6012# ifdef FEAT_VIRTUALEDIT
6013 /*
6014 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006015 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 */
6017 if (virtual_active())
6018 PAST_LINE = 0;
6019# endif
6020#endif
6021
6022 for (n = cap->count1; n > 0; --n)
6023 {
6024 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006025#ifdef FEAT_VISUAL
6026 || (PAST_LINE && *ml_get_cursor() == NUL)
6027#endif
6028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {
6030 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 * <Space> wraps to next line if 'whichwrap' has 's'.
6032 * 'l' wraps to next line if 'whichwrap' has 'l'.
6033 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 */
6035 if ( ((cap->cmdchar == ' '
6036 && vim_strchr(p_ww, 's') != NULL)
6037 || (cap->cmdchar == 'l'
6038 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006039 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 && vim_strchr(p_ww, '>') != NULL))
6041 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6042 {
6043 /* When deleting we also count the NL as a character.
6044 * Set cap->oap->inclusive when last char in the line is
6045 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006046 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 && !cap->oap->inclusive
6048 && !lineempty(curwin->w_cursor.lnum))
6049 cap->oap->inclusive = TRUE;
6050 else
6051 {
6052 ++curwin->w_cursor.lnum;
6053 curwin->w_cursor.col = 0;
6054#ifdef FEAT_VIRTUALEDIT
6055 curwin->w_cursor.coladd = 0;
6056#endif
6057 curwin->w_set_curswant = TRUE;
6058 cap->oap->inclusive = FALSE;
6059 }
6060 continue;
6061 }
6062 if (cap->oap->op_type == OP_NOP)
6063 {
6064 /* Only beep and flush if not moved at all */
6065 if (n == cap->count1)
6066 beep_flush();
6067 }
6068 else
6069 {
6070 if (!lineempty(curwin->w_cursor.lnum))
6071 cap->oap->inclusive = TRUE;
6072 }
6073 break;
6074 }
6075#ifdef FEAT_VISUAL
6076 else if (PAST_LINE)
6077 {
6078 curwin->w_set_curswant = TRUE;
6079# ifdef FEAT_VIRTUALEDIT
6080 if (virtual_active())
6081 oneright();
6082 else
6083# endif
6084 {
6085# ifdef FEAT_MBYTE
6086 if (has_mbyte)
6087 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006088 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 else
6090# endif
6091 ++curwin->w_cursor.col;
6092 }
6093 }
6094#endif
6095 }
6096#ifdef FEAT_FOLDING
6097 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6098 && cap->oap->op_type == OP_NOP)
6099 foldOpenCursor();
6100#endif
6101}
6102
6103/*
6104 * Cursor left commands.
6105 *
6106 * Returns TRUE when operator end should not be adjusted.
6107 */
6108 static void
6109nv_left(cap)
6110 cmdarg_T *cap;
6111{
6112 long n;
6113
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006114 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6115 {
6116 /* <C-Left> and <S-Left> move a word or WORD left */
6117 if (mod_mask & MOD_MASK_CTRL)
6118 cap->arg = 1;
6119 nv_bck_word(cap);
6120 return;
6121 }
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 cap->oap->motion_type = MCHAR;
6124 cap->oap->inclusive = FALSE;
6125 for (n = cap->count1; n > 0; --n)
6126 {
6127 if (oneleft() == FAIL)
6128 {
6129 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6130 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6131 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6132 */
6133 if ( (((cap->cmdchar == K_BS
6134 || cap->cmdchar == Ctrl_H)
6135 && vim_strchr(p_ww, 'b') != NULL)
6136 || (cap->cmdchar == 'h'
6137 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006138 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 && vim_strchr(p_ww, '<') != NULL))
6140 && curwin->w_cursor.lnum > 1)
6141 {
6142 --(curwin->w_cursor.lnum);
6143 coladvance((colnr_T)MAXCOL);
6144 curwin->w_set_curswant = TRUE;
6145
6146 /* When the NL before the first char has to be deleted we
6147 * put the cursor on the NUL after the previous line.
6148 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006149 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if ( (cap->oap->op_type == OP_DELETE
6151 || cap->oap->op_type == OP_CHANGE)
6152 && !lineempty(curwin->w_cursor.lnum))
6153 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006154 if (*ml_get_cursor() != NUL)
6155 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 cap->retval |= CA_NO_ADJ_OP_END;
6157 }
6158 continue;
6159 }
6160 /* Only beep and flush if not moved at all */
6161 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6162 beep_flush();
6163 break;
6164 }
6165 }
6166#ifdef FEAT_FOLDING
6167 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6168 && cap->oap->op_type == OP_NOP)
6169 foldOpenCursor();
6170#endif
6171}
6172
6173/*
6174 * Cursor up commands.
6175 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6176 */
6177 static void
6178nv_up(cap)
6179 cmdarg_T *cap;
6180{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006181 if (mod_mask & MOD_MASK_SHIFT)
6182 {
6183 /* <S-Up> is page up */
6184 cap->arg = BACKWARD;
6185 nv_page(cap);
6186 }
6187 else
6188 {
6189 cap->oap->motion_type = MLINE;
6190 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6191 clearopbeep(cap->oap);
6192 else if (cap->arg)
6193 beginline(BL_WHITE | BL_FIX);
6194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195}
6196
6197/*
6198 * Cursor down commands.
6199 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6200 */
6201 static void
6202nv_down(cap)
6203 cmdarg_T *cap;
6204{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006205 if (mod_mask & MOD_MASK_SHIFT)
6206 {
6207 /* <S-Down> is page down */
6208 cap->arg = FORWARD;
6209 nv_page(cap);
6210 }
6211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6213 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006214 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006215 if (curwin->w_llist_ref == NULL)
6216 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6217 else
6218 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 else
6220#endif
6221 {
6222#ifdef FEAT_CMDWIN
6223 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006224 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 cmdwin_result = CAR;
6226 else
6227#endif
6228 {
6229 cap->oap->motion_type = MLINE;
6230 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6231 clearopbeep(cap->oap);
6232 else if (cap->arg)
6233 beginline(BL_WHITE | BL_FIX);
6234 }
6235 }
6236}
6237
6238#ifdef FEAT_SEARCHPATH
6239/*
6240 * Grab the file name under the cursor and edit it.
6241 */
6242 static void
6243nv_gotofile(cap)
6244 cmdarg_T *cap;
6245{
6246 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006247 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006249 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006252 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 return;
6254 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006255#ifdef FEAT_AUTOCMD
6256 if (curbuf_locked())
6257 {
6258 clearop(cap->oap);
6259 return;
6260 }
6261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006263 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265 if (ptr != NULL)
6266 {
6267 /* do autowrite if necessary */
6268 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6269 autowrite(curbuf, FALSE);
6270 setpcmark();
6271 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006272 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006273 if (cap->nchar == 'F' && lnum >= 0)
6274 {
6275 curwin->w_cursor.lnum = lnum;
6276 check_cursor_lnum();
6277 beginline(BL_SOL | BL_FIX);
6278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 vim_free(ptr);
6280 }
6281 else
6282 clearop(cap->oap);
6283}
6284#endif
6285
6286/*
6287 * <End> command: to end of current line or last line.
6288 */
6289 static void
6290nv_end(cap)
6291 cmdarg_T *cap;
6292{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006293 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006295 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 nv_goto(cap);
6297 cap->count1 = 1; /* to end of current line */
6298 }
6299 nv_dollar(cap);
6300}
6301
6302/*
6303 * Handle the "$" command.
6304 */
6305 static void
6306nv_dollar(cap)
6307 cmdarg_T *cap;
6308{
6309 cap->oap->motion_type = MCHAR;
6310 cap->oap->inclusive = TRUE;
6311#ifdef FEAT_VIRTUALEDIT
6312 /* In virtual mode when off the edge of a line and an operator
6313 * is pending (whew!) keep the cursor where it is.
6314 * Otherwise, send it to the end of the line. */
6315 if (!virtual_active() || gchar_cursor() != NUL
6316 || cap->oap->op_type == OP_NOP)
6317#endif
6318 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6319 if (cursor_down((long)(cap->count1 - 1),
6320 cap->oap->op_type == OP_NOP) == FAIL)
6321 clearopbeep(cap->oap);
6322#ifdef FEAT_FOLDING
6323 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6324 foldOpenCursor();
6325#endif
6326}
6327
6328/*
6329 * Implementation of '?' and '/' commands.
6330 * If cap->arg is TRUE don't set PC mark.
6331 */
6332 static void
6333nv_search(cap)
6334 cmdarg_T *cap;
6335{
6336 oparg_T *oap = cap->oap;
6337
6338 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6339 {
6340 /* Translate "g??" to "g?g?" */
6341 cap->cmdchar = 'g';
6342 cap->nchar = '?';
6343 nv_operator(cap);
6344 return;
6345 }
6346
6347 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6348
6349 if (cap->searchbuf == NULL)
6350 {
6351 clearop(oap);
6352 return;
6353 }
6354
6355 normal_search(cap, cap->cmdchar, cap->searchbuf,
6356 (cap->arg ? 0 : SEARCH_MARK));
6357}
6358
6359/*
6360 * Handle "N" and "n" commands.
6361 * cap->arg is SEARCH_REV for "N", 0 for "n".
6362 */
6363 static void
6364nv_next(cap)
6365 cmdarg_T *cap;
6366{
6367 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6368}
6369
6370/*
6371 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6372 * Uses only cap->count1 and cap->oap from "cap".
6373 */
6374 static void
6375normal_search(cap, dir, pat, opt)
6376 cmdarg_T *cap;
6377 int dir;
6378 char_u *pat;
6379 int opt; /* extra flags for do_search() */
6380{
6381 int i;
6382
6383 cap->oap->motion_type = MCHAR;
6384 cap->oap->inclusive = FALSE;
6385 cap->oap->use_reg_one = TRUE;
6386 curwin->w_set_curswant = TRUE;
6387
6388 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006389 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (i == 0)
6391 clearop(cap->oap);
6392 else
6393 {
6394 if (i == 2)
6395 cap->oap->motion_type = MLINE;
6396#ifdef FEAT_VIRTUALEDIT
6397 curwin->w_cursor.coladd = 0;
6398#endif
6399#ifdef FEAT_FOLDING
6400 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6401 foldOpenCursor();
6402#endif
6403 }
6404
6405 /* "/$" will put the cursor after the end of the line, may need to
6406 * correct that here */
6407 check_cursor();
6408}
6409
6410/*
6411 * Character search commands.
6412 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6413 * ',' and FALSE for ';'.
6414 * cap->nchar is NUL for ',' and ';' (repeat the search)
6415 */
6416 static void
6417nv_csearch(cap)
6418 cmdarg_T *cap;
6419{
6420 int t_cmd;
6421
6422 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6423 t_cmd = TRUE;
6424 else
6425 t_cmd = FALSE;
6426
6427 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6429 clearopbeep(cap->oap);
6430 else
6431 {
6432 curwin->w_set_curswant = TRUE;
6433#ifdef FEAT_VIRTUALEDIT
6434 /* Include a Tab for "tx" and for "dfx". */
6435 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6436 && (t_cmd || cap->oap->op_type != OP_NOP))
6437 {
6438 colnr_T scol, ecol;
6439
6440 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6441 curwin->w_cursor.coladd = ecol - scol;
6442 }
6443 else
6444 curwin->w_cursor.coladd = 0;
6445#endif
6446#ifdef FEAT_VISUAL
6447 adjust_for_sel(cap);
6448#endif
6449#ifdef FEAT_FOLDING
6450 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6451 foldOpenCursor();
6452#endif
6453 }
6454}
6455
6456/*
6457 * "[" and "]" commands.
6458 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6459 */
6460 static void
6461nv_brackets(cap)
6462 cmdarg_T *cap;
6463{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006464 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 pos_T prev_pos;
6466 pos_T *pos = NULL; /* init for GCC */
6467 pos_T old_pos; /* cursor position before command */
6468 int flag;
6469 long n;
6470 int findc;
6471 int c;
6472
6473 cap->oap->motion_type = MCHAR;
6474 cap->oap->inclusive = FALSE;
6475 old_pos = curwin->w_cursor;
6476#ifdef FEAT_VIRTUALEDIT
6477 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6478#endif
6479
6480#ifdef FEAT_SEARCHPATH
6481 /*
6482 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6483 */
6484 if (cap->nchar == 'f')
6485 nv_gotofile(cap);
6486 else
6487#endif
6488
6489#ifdef FEAT_FIND_ID
6490 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006491 * Find the occurrence(s) of the identifier or define under cursor
6492 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 *
6494 * search list jump
6495 * fwd bwd fwd bwd fwd bwd
6496 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6497 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6498 */
6499 if (vim_strchr((char_u *)
6500#ifdef EBCDIC
6501 "iI\005dD\067",
6502#else
6503 "iI\011dD\004",
6504#endif
6505 cap->nchar) != NULL)
6506 {
6507 char_u *ptr;
6508 int len;
6509
6510 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6511 clearop(cap->oap);
6512 else
6513 {
6514 find_pattern_in_path(ptr, 0, len, TRUE,
6515 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6516 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6517 cap->count1,
6518 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6519 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6520 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6521 (linenr_T)MAXLNUM);
6522 curwin->w_set_curswant = TRUE;
6523 }
6524 }
6525 else
6526#endif
6527
6528 /*
6529 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6530 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6531 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6532 * "[m" or "]m" search for prev/next start of (Java) method.
6533 * "[M" or "]M" search for prev/next end of (Java) method.
6534 */
6535 if ( (cap->cmdchar == '['
6536 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6537 || (cap->cmdchar == ']'
6538 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6539 {
6540 if (cap->nchar == '*')
6541 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 prev_pos.lnum = 0;
6543 if (cap->nchar == 'm' || cap->nchar == 'M')
6544 {
6545 if (cap->cmdchar == '[')
6546 findc = '{';
6547 else
6548 findc = '}';
6549 n = 9999;
6550 }
6551 else
6552 {
6553 findc = cap->nchar;
6554 n = cap->count1;
6555 }
6556 for ( ; n > 0; --n)
6557 {
6558 if ((pos = findmatchlimit(cap->oap, findc,
6559 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6560 {
6561 if (new_pos.lnum == 0) /* nothing found */
6562 {
6563 if (cap->nchar != 'm' && cap->nchar != 'M')
6564 clearopbeep(cap->oap);
6565 }
6566 else
6567 pos = &new_pos; /* use last one found */
6568 break;
6569 }
6570 prev_pos = new_pos;
6571 curwin->w_cursor = *pos;
6572 new_pos = *pos;
6573 }
6574 curwin->w_cursor = old_pos;
6575
6576 /*
6577 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6578 * brought us to the match for "[m" and "]M" when inside a method.
6579 * Try finding the '{' or '}' we want to be at.
6580 * Also repeat for the given count.
6581 */
6582 if (cap->nchar == 'm' || cap->nchar == 'M')
6583 {
6584 /* norm is TRUE for "]M" and "[m" */
6585 int norm = ((findc == '{') == (cap->nchar == 'm'));
6586
6587 n = cap->count1;
6588 /* found a match: we were inside a method */
6589 if (prev_pos.lnum != 0)
6590 {
6591 pos = &prev_pos;
6592 curwin->w_cursor = prev_pos;
6593 if (norm)
6594 --n;
6595 }
6596 else
6597 pos = NULL;
6598 while (n > 0)
6599 {
6600 for (;;)
6601 {
6602 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6603 {
6604 /* if not found anything, that's an error */
6605 if (pos == NULL)
6606 clearopbeep(cap->oap);
6607 n = 0;
6608 break;
6609 }
6610 c = gchar_cursor();
6611 if (c == '{' || c == '}')
6612 {
6613 /* Must have found end/start of class: use it.
6614 * Or found the place to be at. */
6615 if ((c == findc && norm) || (n == 1 && !norm))
6616 {
6617 new_pos = curwin->w_cursor;
6618 pos = &new_pos;
6619 n = 0;
6620 }
6621 /* if no match found at all, we started outside of the
6622 * class and we're inside now. Just go on. */
6623 else if (new_pos.lnum == 0)
6624 {
6625 new_pos = curwin->w_cursor;
6626 pos = &new_pos;
6627 }
6628 /* found start/end of other method: go to match */
6629 else if ((pos = findmatchlimit(cap->oap, findc,
6630 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6631 0)) == NULL)
6632 n = 0;
6633 else
6634 curwin->w_cursor = *pos;
6635 break;
6636 }
6637 }
6638 --n;
6639 }
6640 curwin->w_cursor = old_pos;
6641 if (pos == NULL && new_pos.lnum != 0)
6642 clearopbeep(cap->oap);
6643 }
6644 if (pos != NULL)
6645 {
6646 setpcmark();
6647 curwin->w_cursor = *pos;
6648 curwin->w_set_curswant = TRUE;
6649#ifdef FEAT_FOLDING
6650 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6651 && cap->oap->op_type == OP_NOP)
6652 foldOpenCursor();
6653#endif
6654 }
6655 }
6656
6657 /*
6658 * "[[", "[]", "]]" and "][": move to start or end of function
6659 */
6660 else if (cap->nchar == '[' || cap->nchar == ']')
6661 {
6662 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6663 flag = '{';
6664 else
6665 flag = '}'; /* "][" or "[]" */
6666
6667 curwin->w_set_curswant = TRUE;
6668 /*
6669 * Imitate strange Vi behaviour: When using "]]" with an operator
6670 * we also stop at '}'.
6671 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006672 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 (cap->oap->op_type != OP_NOP
6674 && cap->arg == FORWARD && flag == '{')))
6675 clearopbeep(cap->oap);
6676 else
6677 {
6678 if (cap->oap->op_type == OP_NOP)
6679 beginline(BL_WHITE | BL_FIX);
6680#ifdef FEAT_FOLDING
6681 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6682 foldOpenCursor();
6683#endif
6684 }
6685 }
6686
6687 /*
6688 * "[p", "[P", "]P" and "]p": put with indent adjustment
6689 */
6690 else if (cap->nchar == 'p' || cap->nchar == 'P')
6691 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006692 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
6694 prep_redo_cmd(cap);
6695 do_put(cap->oap->regname,
6696 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6697 cap->count1, PUT_FIXINDENT);
6698 }
6699 }
6700
6701 /*
6702 * "['", "[`", "]'" and "]`": jump to next mark
6703 */
6704 else if (cap->nchar == '\'' || cap->nchar == '`')
6705 {
6706 pos = &curwin->w_cursor;
6707 for (n = cap->count1; n > 0; --n)
6708 {
6709 prev_pos = *pos;
6710 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6711 cap->nchar == '\'');
6712 if (pos == NULL)
6713 break;
6714 }
6715 if (pos == NULL)
6716 pos = &prev_pos;
6717 nv_cursormark(cap, cap->nchar == '\'', pos);
6718 }
6719
6720#ifdef FEAT_MOUSE
6721 /*
6722 * [ or ] followed by a middle mouse click: put selected text with
6723 * indent adjustment. Any other button just does as usual.
6724 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006725 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {
6727 (void)do_mouse(cap->oap, cap->nchar,
6728 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6729 cap->count1, PUT_FIXINDENT);
6730 }
6731#endif /* FEAT_MOUSE */
6732
6733#ifdef FEAT_FOLDING
6734 /*
6735 * "[z" and "]z": move to start or end of open fold.
6736 */
6737 else if (cap->nchar == 'z')
6738 {
6739 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6740 cap->count1) == FAIL)
6741 clearopbeep(cap->oap);
6742 }
6743#endif
6744
6745#ifdef FEAT_DIFF
6746 /*
6747 * "[c" and "]c": move to next or previous diff-change.
6748 */
6749 else if (cap->nchar == 'c')
6750 {
6751 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6752 cap->count1) == FAIL)
6753 clearopbeep(cap->oap);
6754 }
6755#endif
6756
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006757#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006758 /*
6759 * "[s", "[S", "]s" and "]S": move to next spell error.
6760 */
6761 else if (cap->nchar == 's' || cap->nchar == 'S')
6762 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006763 setpcmark();
6764 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006765 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6766 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006767 {
6768 clearopbeep(cap->oap);
6769 break;
6770 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006771# ifdef FEAT_FOLDING
6772 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6773 foldOpenCursor();
6774# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006775 }
6776#endif
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 /* Not a valid cap->nchar. */
6779 else
6780 clearopbeep(cap->oap);
6781}
6782
6783/*
6784 * Handle Normal mode "%" command.
6785 */
6786 static void
6787nv_percent(cap)
6788 cmdarg_T *cap;
6789{
6790 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006791#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 linenr_T lnum = curwin->w_cursor.lnum;
6793#endif
6794
6795 cap->oap->inclusive = TRUE;
6796 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6797 {
6798 if (cap->count0 > 100)
6799 clearopbeep(cap->oap);
6800 else
6801 {
6802 cap->oap->motion_type = MLINE;
6803 setpcmark();
6804 /* Round up, so CTRL-G will give same value. Watch out for a
6805 * large line count, the line number must not go negative! */
6806 if (curbuf->b_ml.ml_line_count > 1000000)
6807 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6808 / 100L * cap->count0;
6809 else
6810 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6811 cap->count0 + 99L) / 100L;
6812 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6813 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6814 beginline(BL_SOL | BL_FIX);
6815 }
6816 }
6817 else /* "%" : go to matching paren */
6818 {
6819 cap->oap->motion_type = MCHAR;
6820 cap->oap->use_reg_one = TRUE;
6821 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6822 clearopbeep(cap->oap);
6823 else
6824 {
6825 setpcmark();
6826 curwin->w_cursor = *pos;
6827 curwin->w_set_curswant = TRUE;
6828#ifdef FEAT_VIRTUALEDIT
6829 curwin->w_cursor.coladd = 0;
6830#endif
6831#ifdef FEAT_VISUAL
6832 adjust_for_sel(cap);
6833#endif
6834 }
6835 }
6836#ifdef FEAT_FOLDING
6837 if (cap->oap->op_type == OP_NOP
6838 && lnum != curwin->w_cursor.lnum
6839 && (fdo_flags & FDO_PERCENT)
6840 && KeyTyped)
6841 foldOpenCursor();
6842#endif
6843}
6844
6845/*
6846 * Handle "(" and ")" commands.
6847 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6848 */
6849 static void
6850nv_brace(cap)
6851 cmdarg_T *cap;
6852{
6853 cap->oap->motion_type = MCHAR;
6854 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006855 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6856 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 curwin->w_set_curswant = TRUE;
6858
6859 if (findsent(cap->arg, cap->count1) == FAIL)
6860 clearopbeep(cap->oap);
6861 else
6862 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006863 /* Don't leave the cursor on the NUL past end of line. */
6864 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865#ifdef FEAT_VIRTUALEDIT
6866 curwin->w_cursor.coladd = 0;
6867#endif
6868#ifdef FEAT_FOLDING
6869 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6870 foldOpenCursor();
6871#endif
6872 }
6873}
6874
6875/*
6876 * "m" command: Mark a position.
6877 */
6878 static void
6879nv_mark(cap)
6880 cmdarg_T *cap;
6881{
6882 if (!checkclearop(cap->oap))
6883 {
6884 if (setmark(cap->nchar) == FAIL)
6885 clearopbeep(cap->oap);
6886 }
6887}
6888
6889/*
6890 * "{" and "}" commands.
6891 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6892 */
6893 static void
6894nv_findpar(cap)
6895 cmdarg_T *cap;
6896{
6897 cap->oap->motion_type = MCHAR;
6898 cap->oap->inclusive = FALSE;
6899 cap->oap->use_reg_one = TRUE;
6900 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006901 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 clearopbeep(cap->oap);
6903 else
6904 {
6905#ifdef FEAT_VIRTUALEDIT
6906 curwin->w_cursor.coladd = 0;
6907#endif
6908#ifdef FEAT_FOLDING
6909 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6910 foldOpenCursor();
6911#endif
6912 }
6913}
6914
6915/*
6916 * "u" command: Undo or make lower case.
6917 */
6918 static void
6919nv_undo(cap)
6920 cmdarg_T *cap;
6921{
6922 if (cap->oap->op_type == OP_LOWER
6923#ifdef FEAT_VISUAL
6924 || VIsual_active
6925#endif
6926 )
6927 {
6928 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6929 cap->cmdchar = 'g';
6930 cap->nchar = 'u';
6931 nv_operator(cap);
6932 }
6933 else
6934 nv_kundo(cap);
6935}
6936
6937/*
6938 * <Undo> command.
6939 */
6940 static void
6941nv_kundo(cap)
6942 cmdarg_T *cap;
6943{
6944 if (!checkclearopq(cap->oap))
6945 {
6946 u_undo((int)cap->count1);
6947 curwin->w_set_curswant = TRUE;
6948 }
6949}
6950
6951/*
6952 * Handle the "r" command.
6953 */
6954 static void
6955nv_replace(cap)
6956 cmdarg_T *cap;
6957{
6958 char_u *ptr;
6959 int had_ctrl_v;
6960 long n;
6961
6962 if (checkclearop(cap->oap))
6963 return;
6964
6965 /* get another character */
6966 if (cap->nchar == Ctrl_V)
6967 {
6968 had_ctrl_v = Ctrl_V;
6969 cap->nchar = get_literal();
6970 /* Don't redo a multibyte character with CTRL-V. */
6971 if (cap->nchar > DEL)
6972 had_ctrl_v = NUL;
6973 }
6974 else
6975 had_ctrl_v = NUL;
6976
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006977 /* Abort if the character is a special key. */
6978 if (IS_SPECIAL(cap->nchar))
6979 {
6980 clearopbeep(cap->oap);
6981 return;
6982 }
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#ifdef FEAT_VISUAL
6985 /* Visual mode "r" */
6986 if (VIsual_active)
6987 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006988 if (got_int)
6989 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 nv_operator(cap);
6991 return;
6992 }
6993#endif
6994
6995#ifdef FEAT_VIRTUALEDIT
6996 /* Break tabs, etc. */
6997 if (virtual_active())
6998 {
6999 if (u_save_cursor() == FAIL)
7000 return;
7001 if (gchar_cursor() == NUL)
7002 {
7003 /* Add extra space and put the cursor on the first one. */
7004 coladvance_force((colnr_T)(getviscol() + cap->count1));
7005 curwin->w_cursor.col -= cap->count1;
7006 }
7007 else if (gchar_cursor() == TAB)
7008 coladvance_force(getviscol());
7009 }
7010#endif
7011
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007012 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007014 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015#ifdef FEAT_MBYTE
7016 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7017#endif
7018 )
7019 {
7020 clearopbeep(cap->oap);
7021 return;
7022 }
7023
7024 /*
7025 * Replacing with a TAB is done by edit() when it is complicated because
7026 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7027 * Other characters are done below to avoid problems with things like
7028 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7029 */
7030 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7031 {
7032 stuffnumReadbuff(cap->count1);
7033 stuffcharReadbuff('R');
7034 stuffcharReadbuff('\t');
7035 stuffcharReadbuff(ESC);
7036 return;
7037 }
7038
7039 /* save line for undo */
7040 if (u_save_cursor() == FAIL)
7041 return;
7042
7043 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7044 {
7045 /*
7046 * Replace character(s) by a single newline.
7047 * Strange vi behaviour: Only one newline is inserted.
7048 * Delete the characters here.
7049 * Insert the newline with an insert command, takes care of
7050 * autoindent. The insert command depends on being on the last
7051 * character of a line or not.
7052 */
7053#ifdef FEAT_MBYTE
7054 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7055#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007056 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#endif
7058 stuffcharReadbuff('\r');
7059 stuffcharReadbuff(ESC);
7060
7061 /* Give 'r' to edit(), to get the redo command right. */
7062 invoke_edit(cap, TRUE, 'r', FALSE);
7063 }
7064 else
7065 {
7066 prep_redo(cap->oap->regname, cap->count1,
7067 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7068
7069 curbuf->b_op_start = curwin->w_cursor;
7070#ifdef FEAT_MBYTE
7071 if (has_mbyte)
7072 {
7073 int old_State = State;
7074
7075 if (cap->ncharC1 != 0)
7076 AppendCharToRedobuff(cap->ncharC1);
7077 if (cap->ncharC2 != 0)
7078 AppendCharToRedobuff(cap->ncharC2);
7079
7080 /* This is slow, but it handles replacing a single-byte with a
7081 * multi-byte and the other way around. Also handles adding
7082 * composing characters for utf-8. */
7083 for (n = cap->count1; n > 0; --n)
7084 {
7085 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007086 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7087 {
7088 int c = ins_copychar(curwin->w_cursor.lnum
7089 + (cap->nchar == Ctrl_Y ? -1 : 1));
7090 if (c != NUL)
7091 ins_char(c);
7092 else
7093 /* will be decremented further down */
7094 ++curwin->w_cursor.col;
7095 }
7096 else
7097 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 State = old_State;
7099 if (cap->ncharC1 != 0)
7100 ins_char(cap->ncharC1);
7101 if (cap->ncharC2 != 0)
7102 ins_char(cap->ncharC2);
7103 }
7104 }
7105 else
7106#endif
7107 {
7108 /*
7109 * Replace the characters within one line.
7110 */
7111 for (n = cap->count1; n > 0; --n)
7112 {
7113 /*
7114 * Get ptr again, because u_save and/or showmatch() will have
7115 * released the line. At the same time we let know that the
7116 * line will be changed.
7117 */
7118 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007119 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7120 {
7121 int c = ins_copychar(curwin->w_cursor.lnum
7122 + (cap->nchar == Ctrl_Y ? -1 : 1));
7123 if (c != NUL)
7124 ptr[curwin->w_cursor.col] = c;
7125 }
7126 else
7127 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 if (p_sm && msg_silent == 0)
7129 showmatch(cap->nchar);
7130 ++curwin->w_cursor.col;
7131 }
7132#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007133 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007135 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
Bram Moolenaar009b2592004-10-24 19:18:58 +00007137 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007138 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007140 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142#endif
7143
7144 /* mark the buffer as changed and prepare for displaying */
7145 changed_bytes(curwin->w_cursor.lnum,
7146 (colnr_T)(curwin->w_cursor.col - cap->count1));
7147 }
7148 --curwin->w_cursor.col; /* cursor on the last replaced char */
7149#ifdef FEAT_MBYTE
7150 /* if the character on the left of the current cursor is a multi-byte
7151 * character, move two characters left */
7152 if (has_mbyte)
7153 mb_adjust_cursor();
7154#endif
7155 curbuf->b_op_end = curwin->w_cursor;
7156 curwin->w_set_curswant = TRUE;
7157 set_last_insert(cap->nchar);
7158 }
7159}
7160
7161#ifdef FEAT_VISUAL
7162/*
7163 * 'o': Exchange start and end of Visual area.
7164 * 'O': same, but in block mode exchange left and right corners.
7165 */
7166 static void
7167v_swap_corners(cmdchar)
7168 int cmdchar;
7169{
7170 pos_T old_cursor;
7171 colnr_T left, right;
7172
7173 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7174 {
7175 old_cursor = curwin->w_cursor;
7176 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7177 curwin->w_cursor.lnum = VIsual.lnum;
7178 coladvance(left);
7179 VIsual = curwin->w_cursor;
7180
7181 curwin->w_cursor.lnum = old_cursor.lnum;
7182 curwin->w_curswant = right;
7183 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7184 * right one column */
7185 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7186 ++curwin->w_curswant;
7187 coladvance(curwin->w_curswant);
7188 if (curwin->w_cursor.col == old_cursor.col
7189#ifdef FEAT_VIRTUALEDIT
7190 && (!virtual_active()
7191 || curwin->w_cursor.coladd == old_cursor.coladd)
7192#endif
7193 )
7194 {
7195 curwin->w_cursor.lnum = VIsual.lnum;
7196 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7197 ++right;
7198 coladvance(right);
7199 VIsual = curwin->w_cursor;
7200
7201 curwin->w_cursor.lnum = old_cursor.lnum;
7202 coladvance(left);
7203 curwin->w_curswant = left;
7204 }
7205 }
7206 else
7207 {
7208 old_cursor = curwin->w_cursor;
7209 curwin->w_cursor = VIsual;
7210 VIsual = old_cursor;
7211 curwin->w_set_curswant = TRUE;
7212 }
7213}
7214#endif /* FEAT_VISUAL */
7215
7216/*
7217 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7218 */
7219 static void
7220nv_Replace(cap)
7221 cmdarg_T *cap;
7222{
7223#ifdef FEAT_VISUAL
7224 if (VIsual_active) /* "R" is replace lines */
7225 {
7226 cap->cmdchar = 'c';
7227 cap->nchar = NUL;
7228 VIsual_mode = 'V';
7229 nv_operator(cap);
7230 }
7231 else
7232#endif
7233 if (!checkclearopq(cap->oap))
7234 {
7235 if (!curbuf->b_p_ma)
7236 EMSG(_(e_modifiable));
7237 else
7238 {
7239#ifdef FEAT_VIRTUALEDIT
7240 if (virtual_active())
7241 coladvance(getviscol());
7242#endif
7243 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7244 }
7245 }
7246}
7247
7248#ifdef FEAT_VREPLACE
7249/*
7250 * "gr".
7251 */
7252 static void
7253nv_vreplace(cap)
7254 cmdarg_T *cap;
7255{
7256# ifdef FEAT_VISUAL
7257 if (VIsual_active)
7258 {
7259 cap->cmdchar = 'r';
7260 cap->nchar = cap->extra_char;
7261 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7262 }
7263 else
7264# endif
7265 if (!checkclearopq(cap->oap))
7266 {
7267 if (!curbuf->b_p_ma)
7268 EMSG(_(e_modifiable));
7269 else
7270 {
7271 if (cap->extra_char == Ctrl_V) /* get another character */
7272 cap->extra_char = get_literal();
7273 stuffcharReadbuff(cap->extra_char);
7274 stuffcharReadbuff(ESC);
7275# ifdef FEAT_VIRTUALEDIT
7276 if (virtual_active())
7277 coladvance(getviscol());
7278# endif
7279 invoke_edit(cap, TRUE, 'v', FALSE);
7280 }
7281 }
7282}
7283#endif
7284
7285/*
7286 * Swap case for "~" command, when it does not work like an operator.
7287 */
7288 static void
7289n_swapchar(cap)
7290 cmdarg_T *cap;
7291{
7292 long n;
7293 pos_T startpos;
7294 int did_change = 0;
7295#ifdef FEAT_NETBEANS_INTG
7296 pos_T pos;
7297 char_u *ptr;
7298 int count;
7299#endif
7300
7301 if (checkclearopq(cap->oap))
7302 return;
7303
7304 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7305 {
7306 clearopbeep(cap->oap);
7307 return;
7308 }
7309
7310 prep_redo_cmd(cap);
7311
7312 if (u_save_cursor() == FAIL)
7313 return;
7314
7315 startpos = curwin->w_cursor;
7316#ifdef FEAT_NETBEANS_INTG
7317 pos = startpos;
7318#endif
7319 for (n = cap->count1; n > 0; --n)
7320 {
7321 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7322 inc_cursor();
7323 if (gchar_cursor() == NUL)
7324 {
7325 if (vim_strchr(p_ww, '~') != NULL
7326 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7327 {
7328#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007329 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
7331 if (did_change)
7332 {
7333 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007334 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007335 netbeans_removed(curbuf, pos.lnum, pos.col,
7336 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007338 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
7340 pos.col = 0;
7341 pos.lnum++;
7342 }
7343#endif
7344 ++curwin->w_cursor.lnum;
7345 curwin->w_cursor.col = 0;
7346 if (n > 1)
7347 {
7348 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7349 break;
7350 u_clearline();
7351 }
7352 }
7353 else
7354 break;
7355 }
7356 }
7357#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007358 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 ptr = ml_get(pos.lnum);
7361 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007362 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7363 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365#endif
7366
7367
7368 check_cursor();
7369 curwin->w_set_curswant = TRUE;
7370 if (did_change)
7371 {
7372 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7373 0L);
7374 curbuf->b_op_start = startpos;
7375 curbuf->b_op_end = curwin->w_cursor;
7376 if (curbuf->b_op_end.col > 0)
7377 --curbuf->b_op_end.col;
7378 }
7379}
7380
7381/*
7382 * Move cursor to mark.
7383 */
7384 static void
7385nv_cursormark(cap, flag, pos)
7386 cmdarg_T *cap;
7387 int flag;
7388 pos_T *pos;
7389{
7390 if (check_mark(pos) == FAIL)
7391 clearop(cap->oap);
7392 else
7393 {
7394 if (cap->cmdchar == '\''
7395 || cap->cmdchar == '`'
7396 || cap->cmdchar == '['
7397 || cap->cmdchar == ']')
7398 setpcmark();
7399 curwin->w_cursor = *pos;
7400 if (flag)
7401 beginline(BL_WHITE | BL_FIX);
7402 else
7403 check_cursor();
7404 }
7405 cap->oap->motion_type = flag ? MLINE : MCHAR;
7406 if (cap->cmdchar == '`')
7407 cap->oap->use_reg_one = TRUE;
7408 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7409 curwin->w_set_curswant = TRUE;
7410}
7411
7412#ifdef FEAT_VISUAL
7413/*
7414 * Handle commands that are operators in Visual mode.
7415 */
7416 static void
7417v_visop(cap)
7418 cmdarg_T *cap;
7419{
7420 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7421
7422 /* Uppercase means linewise, except in block mode, then "D" deletes till
7423 * the end of the line, and "C" replaces til EOL */
7424 if (isupper(cap->cmdchar))
7425 {
7426 if (VIsual_mode != Ctrl_V)
7427 VIsual_mode = 'V';
7428 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7429 curwin->w_curswant = MAXCOL;
7430 }
7431 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7432 nv_operator(cap);
7433}
7434#endif
7435
7436/*
7437 * "s" and "S" commands.
7438 */
7439 static void
7440nv_subst(cap)
7441 cmdarg_T *cap;
7442{
7443#ifdef FEAT_VISUAL
7444 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7445 {
7446 if (cap->cmdchar == 'S')
7447 VIsual_mode = 'V';
7448 cap->cmdchar = 'c';
7449 nv_operator(cap);
7450 }
7451 else
7452#endif
7453 nv_optrans(cap);
7454}
7455
7456/*
7457 * Abbreviated commands.
7458 */
7459 static void
7460nv_abbrev(cap)
7461 cmdarg_T *cap;
7462{
7463 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7464 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7465
7466#ifdef FEAT_VISUAL
7467 /* in Visual mode these commands are operators */
7468 if (VIsual_active)
7469 v_visop(cap);
7470 else
7471#endif
7472 nv_optrans(cap);
7473}
7474
7475/*
7476 * Translate a command into another command.
7477 */
7478 static void
7479nv_optrans(cap)
7480 cmdarg_T *cap;
7481{
7482 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7483 (char_u *)"d$", (char_u *)"c$",
7484 (char_u *)"cl", (char_u *)"cc",
7485 (char_u *)"yy", (char_u *)":s\r"};
7486 static char_u *str = (char_u *)"xXDCsSY&";
7487
7488 if (!checkclearopq(cap->oap))
7489 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007490 /* In Vi "2D" doesn't delete the next line. Can't translate it
7491 * either, because "2." should also not use the count. */
7492 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7493 {
7494 cap->oap->start = curwin->w_cursor;
7495 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007496#ifdef FEAT_EVAL
7497 set_op_var(OP_DELETE);
7498#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007499 cap->count1 = 1;
7500 nv_dollar(cap);
7501 finish_op = TRUE;
7502 ResetRedobuff();
7503 AppendCharToRedobuff('D');
7504 }
7505 else
7506 {
7507 if (cap->count0)
7508 stuffnumReadbuff(cap->count0);
7509 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 cap->opcount = 0;
7513}
7514
7515/*
7516 * "'" and "`" commands. Also for "g'" and "g`".
7517 * cap->arg is TRUE for "'" and "g'".
7518 */
7519 static void
7520nv_gomark(cap)
7521 cmdarg_T *cap;
7522{
7523 pos_T *pos;
7524 int c;
7525#ifdef FEAT_FOLDING
7526 linenr_T lnum = curwin->w_cursor.lnum;
7527 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7528#endif
7529
7530 if (cap->cmdchar == 'g')
7531 c = cap->extra_char;
7532 else
7533 c = cap->nchar;
7534 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7535 if (pos == (pos_T *)-1) /* jumped to other file */
7536 {
7537 if (cap->arg)
7538 {
7539 check_cursor_lnum();
7540 beginline(BL_WHITE | BL_FIX);
7541 }
7542 else
7543 check_cursor();
7544 }
7545 else
7546 nv_cursormark(cap, cap->arg, pos);
7547
7548#ifdef FEAT_VIRTUALEDIT
7549 /* May need to clear the coladd that a mark includes. */
7550 if (!virtual_active())
7551 curwin->w_cursor.coladd = 0;
7552#endif
7553#ifdef FEAT_FOLDING
7554 if (cap->oap->op_type == OP_NOP
7555 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7556 && (fdo_flags & FDO_MARK)
7557 && old_KeyTyped)
7558 foldOpenCursor();
7559#endif
7560}
7561
7562/*
7563 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7564 */
7565 static void
7566nv_pcmark(cap)
7567 cmdarg_T *cap;
7568{
7569#ifdef FEAT_JUMPLIST
7570 pos_T *pos;
7571# ifdef FEAT_FOLDING
7572 linenr_T lnum = curwin->w_cursor.lnum;
7573 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7574# endif
7575
7576 if (!checkclearopq(cap->oap))
7577 {
7578 if (cap->cmdchar == 'g')
7579 pos = movechangelist((int)cap->count1);
7580 else
7581 pos = movemark((int)cap->count1);
7582 if (pos == (pos_T *)-1) /* jump to other file */
7583 {
7584 curwin->w_set_curswant = TRUE;
7585 check_cursor();
7586 }
7587 else if (pos != NULL) /* can jump */
7588 nv_cursormark(cap, FALSE, pos);
7589 else if (cap->cmdchar == 'g')
7590 {
7591 if (curbuf->b_changelistlen == 0)
7592 EMSG(_("E664: changelist is empty"));
7593 else if (cap->count1 < 0)
7594 EMSG(_("E662: At start of changelist"));
7595 else
7596 EMSG(_("E663: At end of changelist"));
7597 }
7598 else
7599 clearopbeep(cap->oap);
7600# ifdef FEAT_FOLDING
7601 if (cap->oap->op_type == OP_NOP
7602 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7603 && (fdo_flags & FDO_MARK)
7604 && old_KeyTyped)
7605 foldOpenCursor();
7606# endif
7607 }
7608#else
7609 clearopbeep(cap->oap);
7610#endif
7611}
7612
7613/*
7614 * Handle '"' command.
7615 */
7616 static void
7617nv_regname(cap)
7618 cmdarg_T *cap;
7619{
7620 if (checkclearop(cap->oap))
7621 return;
7622#ifdef FEAT_EVAL
7623 if (cap->nchar == '=')
7624 cap->nchar = get_expr_register();
7625#endif
7626 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7627 {
7628 cap->oap->regname = cap->nchar;
7629 cap->opcount = cap->count0; /* remember count before '"' */
7630#ifdef FEAT_EVAL
7631 set_reg_var(cap->oap->regname);
7632#endif
7633 }
7634 else
7635 clearopbeep(cap->oap);
7636}
7637
7638#ifdef FEAT_VISUAL
7639/*
7640 * Handle "v", "V" and "CTRL-V" commands.
7641 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7642 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 */
7645 static void
7646nv_visual(cap)
7647 cmdarg_T *cap;
7648{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 if (cap->cmdchar == Ctrl_Q)
7650 cap->cmdchar = Ctrl_V;
7651
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7653 * characterwise, linewise, or blockwise. */
7654 if (cap->oap->op_type != OP_NOP)
7655 {
7656 cap->oap->motion_force = cap->cmdchar;
7657 finish_op = FALSE; /* operator doesn't finish now but later */
7658 return;
7659 }
7660
7661 VIsual_select = cap->arg;
7662 if (VIsual_active) /* change Visual mode */
7663 {
7664 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7665 end_visual_mode();
7666 else /* toggle char/block mode */
7667 { /* or char/line mode */
7668 VIsual_mode = cap->cmdchar;
7669 showmode();
7670 }
7671 redraw_curbuf_later(INVERTED); /* update the inversion */
7672 }
7673 else /* start Visual mode */
7674 {
7675 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007676 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007678 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 VIsual = curwin->w_cursor;
7680
7681 VIsual_active = TRUE;
7682 VIsual_reselect = TRUE;
7683 if (!cap->arg)
7684 /* start Select mode when 'selectmode' contains "cmd" */
7685 may_start_select('c');
7686#ifdef FEAT_MOUSE
7687 setmouse();
7688#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007689 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 redraw_cmdline = TRUE; /* show visual mode later */
7691 /*
7692 * For V and ^V, we multiply the number of lines even if there
7693 * was only one -- webb
7694 */
7695 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7696 {
7697 curwin->w_cursor.lnum +=
7698 resel_VIsual_line_count * cap->count0 - 1;
7699 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7700 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7701 }
7702 VIsual_mode = resel_VIsual_mode;
7703 if (VIsual_mode == 'v')
7704 {
7705 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007706 {
7707 validate_virtcol();
7708 curwin->w_curswant = curwin->w_virtcol
7709 + resel_VIsual_vcol * cap->count0 - 1;
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007712 curwin->w_curswant = resel_VIsual_vcol;
7713 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007715 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 curwin->w_curswant = MAXCOL;
7718 coladvance((colnr_T)MAXCOL);
7719 }
7720 else if (VIsual_mode == Ctrl_V)
7721 {
7722 validate_virtcol();
7723 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007724 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 coladvance(curwin->w_curswant);
7726 }
7727 else
7728 curwin->w_set_curswant = TRUE;
7729 redraw_curbuf_later(INVERTED); /* show the inversion */
7730 }
7731 else
7732 {
7733 if (!cap->arg)
7734 /* start Select mode when 'selectmode' contains "cmd" */
7735 may_start_select('c');
7736 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007737 if (VIsual_mode != 'V' && *p_sel == 'e')
7738 ++cap->count1; /* include one more char */
7739 if (cap->count0 > 0 && --cap->count1 > 0)
7740 {
7741 /* With a count select that many characters or lines. */
7742 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7743 nv_right(cap);
7744 else if (VIsual_mode == 'V')
7745 nv_down(cap);
7746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 }
7749}
7750
7751/*
7752 * Start selection for Shift-movement keys.
7753 */
7754 void
7755start_selection()
7756{
7757 /* if 'selectmode' contains "key", start Select mode */
7758 may_start_select('k');
7759 n_start_visual_mode('v');
7760}
7761
7762/*
7763 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7764 */
7765 void
7766may_start_select(c)
7767 int c;
7768{
7769 VIsual_select = (stuff_empty() && typebuf_typed()
7770 && (vim_strchr(p_slm, c) != NULL));
7771}
7772
7773/*
7774 * Start Visual mode "c".
7775 * Should set VIsual_select before calling this.
7776 */
7777 static void
7778n_start_visual_mode(c)
7779 int c;
7780{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007781#ifdef FEAT_CONCEAL
7782 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007783 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007784#endif
7785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 VIsual_mode = c;
7787 VIsual_active = TRUE;
7788 VIsual_reselect = TRUE;
7789#ifdef FEAT_VIRTUALEDIT
7790 /* Corner case: the 0 position in a tab may change when going into
7791 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7792 */
7793 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007794 {
7795 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798#endif
7799 VIsual = curwin->w_cursor;
7800
7801#ifdef FEAT_FOLDING
7802 foldAdjustVisual();
7803#endif
7804
7805#ifdef FEAT_MOUSE
7806 setmouse();
7807#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007808#ifdef FEAT_CONCEAL
7809 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007810 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007811#endif
7812
Bram Moolenaar09df3122006-01-23 22:23:09 +00007813 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 redraw_cmdline = TRUE; /* show visual mode later */
7815#ifdef FEAT_CLIPBOARD
7816 /* Make sure the clipboard gets updated. Needed because start and
7817 * end may still be the same, and the selection needs to be owned */
7818 clip_star.vmode = NUL;
7819#endif
7820
7821 /* Only need to redraw this line, unless still need to redraw an old
7822 * Visual area (when 'lazyredraw' is set). */
7823 if (curwin->w_redr_type < INVERTED)
7824 {
7825 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7826 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7827 }
7828}
7829
7830#endif /* FEAT_VISUAL */
7831
7832/*
7833 * CTRL-W: Window commands
7834 */
7835 static void
7836nv_window(cap)
7837 cmdarg_T *cap;
7838{
7839#ifdef FEAT_WINDOWS
7840 if (!checkclearop(cap->oap))
7841 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7842#else
7843 (void)checkclearop(cap->oap);
7844#endif
7845}
7846
7847/*
7848 * CTRL-Z: Suspend
7849 */
7850 static void
7851nv_suspend(cap)
7852 cmdarg_T *cap;
7853{
7854 clearop(cap->oap);
7855#ifdef FEAT_VISUAL
7856 if (VIsual_active)
7857 end_visual_mode(); /* stop Visual mode */
7858#endif
7859 do_cmdline_cmd((char_u *)"st");
7860}
7861
7862/*
7863 * Commands starting with "g".
7864 */
7865 static void
7866nv_g_cmd(cap)
7867 cmdarg_T *cap;
7868{
7869 oparg_T *oap = cap->oap;
7870#ifdef FEAT_VISUAL
7871 pos_T tpos;
7872#endif
7873 int i;
7874 int flag = FALSE;
7875
7876 switch (cap->nchar)
7877 {
7878#ifdef MEM_PROFILE
7879 /*
7880 * "g^A": dump log of used memory.
7881 */
7882 case Ctrl_A:
7883 vim_mem_profile_dump();
7884 break;
7885#endif
7886
7887#ifdef FEAT_VREPLACE
7888 /*
7889 * "gR": Enter virtual replace mode.
7890 */
7891 case 'R':
7892 cap->arg = TRUE;
7893 nv_Replace(cap);
7894 break;
7895
7896 case 'r':
7897 nv_vreplace(cap);
7898 break;
7899#endif
7900
7901 case '&':
7902 do_cmdline_cmd((char_u *)"%s//~/&");
7903 break;
7904
7905#ifdef FEAT_VISUAL
7906 /*
7907 * "gv": Reselect the previous Visual area. If Visual already active,
7908 * exchange previous and current Visual area.
7909 */
7910 case 'v':
7911 if (checkclearop(oap))
7912 break;
7913
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007914 if ( curbuf->b_visual.vi_start.lnum == 0
7915 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7916 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 beep_flush();
7918 else
7919 {
7920 /* set w_cursor to the start of the Visual area, tpos to the end */
7921 if (VIsual_active)
7922 {
7923 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007924 VIsual_mode = curbuf->b_visual.vi_mode;
7925 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926# ifdef FEAT_EVAL
7927 curbuf->b_visual_mode_eval = i;
7928# endif
7929 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007930 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7931 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007933 tpos = curbuf->b_visual.vi_end;
7934 curbuf->b_visual.vi_end = curwin->w_cursor;
7935 curwin->w_cursor = curbuf->b_visual.vi_start;
7936 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938 else
7939 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007940 VIsual_mode = curbuf->b_visual.vi_mode;
7941 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7942 tpos = curbuf->b_visual.vi_end;
7943 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 }
7945
7946 VIsual_active = TRUE;
7947 VIsual_reselect = TRUE;
7948
7949 /* Set Visual to the start and w_cursor to the end of the Visual
7950 * area. Make sure they are on an existing character. */
7951 check_cursor();
7952 VIsual = curwin->w_cursor;
7953 curwin->w_cursor = tpos;
7954 check_cursor();
7955 update_topline();
7956 /*
7957 * When called from normal "g" command: start Select mode when
7958 * 'selectmode' contains "cmd". When called for K_SELECT, always
7959 * start Select mode.
7960 */
7961 if (cap->arg)
7962 VIsual_select = TRUE;
7963 else
7964 may_start_select('c');
7965#ifdef FEAT_MOUSE
7966 setmouse();
7967#endif
7968#ifdef FEAT_CLIPBOARD
7969 /* Make sure the clipboard gets updated. Needed because start and
7970 * end are still the same, and the selection needs to be owned */
7971 clip_star.vmode = NUL;
7972#endif
7973 redraw_curbuf_later(INVERTED);
7974 showmode();
7975 }
7976 break;
7977 /*
7978 * "gV": Don't reselect the previous Visual area after a Select mode
7979 * mapping of menu.
7980 */
7981 case 'V':
7982 VIsual_reselect = FALSE;
7983 break;
7984
7985 /*
7986 * "gh": start Select mode.
7987 * "gH": start Select line mode.
7988 * "g^H": start Select block mode.
7989 */
7990 case K_BS:
7991 cap->nchar = Ctrl_H;
7992 /* FALLTHROUGH */
7993 case 'h':
7994 case 'H':
7995 case Ctrl_H:
7996# ifdef EBCDIC
7997 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7998 if (cap->nchar == Ctrl_H)
7999 cap->cmdchar = Ctrl_V;
8000 else
8001# endif
8002 cap->cmdchar = cap->nchar + ('v' - 'h');
8003 cap->arg = TRUE;
8004 nv_visual(cap);
8005 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008006#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008007
8008 /* "gn", "gN" visually select next/previous search match
8009 * "gn" selects next match
8010 * "gN" selects previous match
8011 */
8012 case 'N':
8013 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008014#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008015 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008016#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008017 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 /*
8021 * "gj" and "gk" two new funny movement keys -- up and down
8022 * movement based on *screen* line rather than *file* line.
8023 */
8024 case 'j':
8025 case K_DOWN:
8026 /* with 'nowrap' it works just like the normal "j" command; also when
8027 * in a closed fold */
8028 if (!curwin->w_p_wrap
8029#ifdef FEAT_FOLDING
8030 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8031#endif
8032 )
8033 {
8034 oap->motion_type = MLINE;
8035 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8036 }
8037 else
8038 i = nv_screengo(oap, FORWARD, cap->count1);
8039 if (i == FAIL)
8040 clearopbeep(oap);
8041 break;
8042
8043 case 'k':
8044 case K_UP:
8045 /* with 'nowrap' it works just like the normal "k" command; also when
8046 * in a closed fold */
8047 if (!curwin->w_p_wrap
8048#ifdef FEAT_FOLDING
8049 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8050#endif
8051 )
8052 {
8053 oap->motion_type = MLINE;
8054 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8055 }
8056 else
8057 i = nv_screengo(oap, BACKWARD, cap->count1);
8058 if (i == FAIL)
8059 clearopbeep(oap);
8060 break;
8061
8062 /*
8063 * "gJ": join two lines without inserting a space.
8064 */
8065 case 'J':
8066 nv_join(cap);
8067 break;
8068
8069 /*
8070 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8071 * "gm": middle of "g0" and "g$".
8072 */
8073 case '^':
8074 flag = TRUE;
8075 /* FALLTHROUGH */
8076
8077 case '0':
8078 case 'm':
8079 case K_HOME:
8080 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 oap->motion_type = MCHAR;
8082 oap->inclusive = FALSE;
8083 if (curwin->w_p_wrap
8084#ifdef FEAT_VERTSPLIT
8085 && curwin->w_width != 0
8086#endif
8087 )
8088 {
8089 int width1 = W_WIDTH(curwin) - curwin_col_off();
8090 int width2 = width1 + curwin_col_off2();
8091
8092 validate_virtcol();
8093 i = 0;
8094 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8095 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8096 }
8097 else
8098 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008099 /* Go to the middle of the screen line. When 'number' or
8100 * 'relativenumber' is on and lines are wrapping the middle can be more
8101 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (cap->nchar == 'm')
8103 i += (W_WIDTH(curwin) - curwin_col_off()
8104 + ((curwin->w_p_wrap && i > 0)
8105 ? curwin_col_off2() : 0)) / 2;
8106 coladvance((colnr_T)i);
8107 if (flag)
8108 {
8109 do
8110 i = gchar_cursor();
8111 while (vim_iswhite(i) && oneright() == OK);
8112 }
8113 curwin->w_set_curswant = TRUE;
8114 break;
8115
8116 case '_':
8117 /* "g_": to the last non-blank character in the line or <count> lines
8118 * downward. */
8119 cap->oap->motion_type = MCHAR;
8120 cap->oap->inclusive = TRUE;
8121 curwin->w_curswant = MAXCOL;
8122 if (cursor_down((long)(cap->count1 - 1),
8123 cap->oap->op_type == OP_NOP) == FAIL)
8124 clearopbeep(cap->oap);
8125 else
8126 {
8127 char_u *ptr = ml_get_curline();
8128
8129 /* In Visual mode we may end up after the line. */
8130 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8131 --curwin->w_cursor.col;
8132
8133 /* Decrease the cursor column until it's on a non-blank. */
8134 while (curwin->w_cursor.col > 0
8135 && vim_iswhite(ptr[curwin->w_cursor.col]))
8136 --curwin->w_cursor.col;
8137 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008138#ifdef FEAT_VISUAL
8139 adjust_for_sel(cap);
8140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142 break;
8143
8144 case '$':
8145 case K_END:
8146 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {
8148 int col_off = curwin_col_off();
8149
8150 oap->motion_type = MCHAR;
8151 oap->inclusive = TRUE;
8152 if (curwin->w_p_wrap
8153#ifdef FEAT_VERTSPLIT
8154 && curwin->w_width != 0
8155#endif
8156 )
8157 {
8158 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8159 if (cap->count1 == 1)
8160 {
8161 int width1 = W_WIDTH(curwin) - col_off;
8162 int width2 = width1 + curwin_col_off2();
8163
8164 validate_virtcol();
8165 i = width1 - 1;
8166 if (curwin->w_virtcol >= (colnr_T)width1)
8167 i += ((curwin->w_virtcol - width1) / width2 + 1)
8168 * width2;
8169 coladvance((colnr_T)i);
8170#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8171 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8172 {
8173 /*
8174 * Check for landing on a character that got split at
8175 * the end of the line. We do not want to advance to
8176 * the next screen line.
8177 */
8178 validate_virtcol();
8179 if (curwin->w_virtcol > (colnr_T)i)
8180 --curwin->w_cursor.col;
8181 }
8182#endif
8183 }
8184 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8185 clearopbeep(oap);
8186 }
8187 else
8188 {
8189 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8190 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008191
8192 /* Make sure we stick in this column. */
8193 validate_virtcol();
8194 curwin->w_curswant = curwin->w_virtcol;
8195 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 }
8197 }
8198 break;
8199
8200 /*
8201 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8202 */
8203 case '*':
8204 case '#':
8205#if POUND != '#'
8206 case POUND: /* pound sign (sometimes equal to '#') */
8207#endif
8208 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8209 case ']': /* :tselect for current identifier */
8210 nv_ident(cap);
8211 break;
8212
8213 /*
8214 * ge and gE: go back to end of word
8215 */
8216 case 'e':
8217 case 'E':
8218 oap->motion_type = MCHAR;
8219 curwin->w_set_curswant = TRUE;
8220 oap->inclusive = TRUE;
8221 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8222 clearopbeep(oap);
8223 break;
8224
8225 /*
8226 * "g CTRL-G": display info about cursor position
8227 */
8228 case Ctrl_G:
8229 cursor_pos_info();
8230 break;
8231
8232 /*
8233 * "gi": start Insert at the last position.
8234 */
8235 case 'i':
8236 if (curbuf->b_last_insert.lnum != 0)
8237 {
8238 curwin->w_cursor = curbuf->b_last_insert;
8239 check_cursor_lnum();
8240 i = (int)STRLEN(ml_get_curline());
8241 if (curwin->w_cursor.col > (colnr_T)i)
8242 {
8243#ifdef FEAT_VIRTUALEDIT
8244 if (virtual_active())
8245 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8246#endif
8247 curwin->w_cursor.col = i;
8248 }
8249 }
8250 cap->cmdchar = 'i';
8251 nv_edit(cap);
8252 break;
8253
8254 /*
8255 * "gI": Start insert in column 1.
8256 */
8257 case 'I':
8258 beginline(0);
8259 if (!checkclearopq(oap))
8260 invoke_edit(cap, FALSE, 'g', FALSE);
8261 break;
8262
8263#ifdef FEAT_SEARCHPATH
8264 /*
8265 * "gf": goto file, edit file under cursor
8266 * "]f" and "[f": can also be used.
8267 */
8268 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008269 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 nv_gotofile(cap);
8271 break;
8272#endif
8273
8274 /* "g'm" and "g`m": jump to mark without setting pcmark */
8275 case '\'':
8276 cap->arg = TRUE;
8277 /*FALLTHROUGH*/
8278 case '`':
8279 nv_gomark(cap);
8280 break;
8281
8282 /*
8283 * "gs": Goto sleep.
8284 */
8285 case 's':
8286 do_sleep(cap->count1 * 1000L);
8287 break;
8288
8289 /*
8290 * "ga": Display the ascii value of the character under the
8291 * cursor. It is displayed in decimal, hex, and octal. -- webb
8292 */
8293 case 'a':
8294 do_ascii(NULL);
8295 break;
8296
8297#ifdef FEAT_MBYTE
8298 /*
8299 * "g8": Display the bytes used for the UTF-8 character under the
8300 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008301 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 */
8303 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008304 if (cap->count0 == 8)
8305 utf_find_illegal();
8306 else
8307 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 break;
8309#endif
8310
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008311 case '<':
8312 show_sb_text();
8313 break;
8314
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 /*
8316 * "gg": Goto the first line in file. With a count it goes to
8317 * that line number like for "G". -- webb
8318 */
8319 case 'g':
8320 cap->arg = FALSE;
8321 nv_goto(cap);
8322 break;
8323
8324 /*
8325 * Two-character operators:
8326 * "gq" Format text
8327 * "gw" Format text and keep cursor position
8328 * "g~" Toggle the case of the text.
8329 * "gu" Change text to lower case.
8330 * "gU" Change text to upper case.
8331 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008332 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 */
8334 case 'q':
8335 case 'w':
8336 oap->cursor_start = curwin->w_cursor;
8337 /*FALLTHROUGH*/
8338 case '~':
8339 case 'u':
8340 case 'U':
8341 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008342 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 nv_operator(cap);
8344 break;
8345
8346 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008347 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 * current function
8349 * "gD": idem, but in the current file.
8350 */
8351 case 'd':
8352 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008353 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 break;
8355
8356#ifdef FEAT_MOUSE
8357 /*
8358 * g<*Mouse> : <C-*mouse>
8359 */
8360 case K_MIDDLEMOUSE:
8361 case K_MIDDLEDRAG:
8362 case K_MIDDLERELEASE:
8363 case K_LEFTMOUSE:
8364 case K_LEFTDRAG:
8365 case K_LEFTRELEASE:
8366 case K_RIGHTMOUSE:
8367 case K_RIGHTDRAG:
8368 case K_RIGHTRELEASE:
8369 case K_X1MOUSE:
8370 case K_X1DRAG:
8371 case K_X1RELEASE:
8372 case K_X2MOUSE:
8373 case K_X2DRAG:
8374 case K_X2RELEASE:
8375 mod_mask = MOD_MASK_CTRL;
8376 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8377 break;
8378#endif
8379
8380 case K_IGNORE:
8381 break;
8382
8383 /*
8384 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8385 */
8386 case 'p':
8387 case 'P':
8388 nv_put(cap);
8389 break;
8390
8391#ifdef FEAT_BYTEOFF
8392 /* "go": goto byte count from start of buffer */
8393 case 'o':
8394 goto_byte(cap->count0);
8395 break;
8396#endif
8397
8398 /* "gQ": improved Ex mode */
8399 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008400 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {
8402 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008403 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 break;
8405 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (!checkclearopq(oap))
8408 do_exmode(TRUE);
8409 break;
8410
8411#ifdef FEAT_JUMPLIST
8412 case ',':
8413 nv_pcmark(cap);
8414 break;
8415
8416 case ';':
8417 cap->count1 = -cap->count1;
8418 nv_pcmark(cap);
8419 break;
8420#endif
8421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008422#ifdef FEAT_WINDOWS
8423 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008424 if (!checkclearop(oap))
8425 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008427 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008428 if (!checkclearop(oap))
8429 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008430 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008431#endif
8432
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008433 case '+':
8434 case '-': /* "g+" and "g-": undo or redo along the timeline */
8435 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008436 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008437 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008438 break;
8439
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 default:
8441 clearopbeep(oap);
8442 break;
8443 }
8444}
8445
8446/*
8447 * Handle "o" and "O" commands.
8448 */
8449 static void
8450n_opencmd(cap)
8451 cmdarg_T *cap;
8452{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008453#ifdef FEAT_CONCEAL
8454 linenr_T oldline = curwin->w_cursor.lnum;
8455#endif
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 if (!checkclearopq(cap->oap))
8458 {
8459#ifdef FEAT_FOLDING
8460 if (cap->cmdchar == 'O')
8461 /* Open above the first line of a folded sequence of lines */
8462 (void)hasFolding(curwin->w_cursor.lnum,
8463 &curwin->w_cursor.lnum, NULL);
8464 else
8465 /* Open below the last line of a folded sequence of lines */
8466 (void)hasFolding(curwin->w_cursor.lnum,
8467 NULL, &curwin->w_cursor.lnum);
8468#endif
8469 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8470 (cap->cmdchar == 'O' ? 1 : 0)),
8471 (linenr_T)(curwin->w_cursor.lnum +
8472 (cap->cmdchar == 'o' ? 1 : 0))
8473 ) == OK
8474 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8475#ifdef FEAT_COMMENTS
8476 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8477#endif
8478 0, 0))
8479 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008480#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008481 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008482 update_single_line(curwin, oldline);
8483#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008484 /* When '#' is in 'cpoptions' ignore the count. */
8485 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8486 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8488 }
8489 }
8490}
8491
8492/*
8493 * "." command: redo last change.
8494 */
8495 static void
8496nv_dot(cap)
8497 cmdarg_T *cap;
8498{
8499 if (!checkclearopq(cap->oap))
8500 {
8501 /*
8502 * If "restart_edit" is TRUE, the last but one command is repeated
8503 * instead of the last command (inserting text). This is used for
8504 * CTRL-O <.> in insert mode.
8505 */
8506 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8507 clearopbeep(cap->oap);
8508 }
8509}
8510
8511/*
8512 * CTRL-R: undo undo
8513 */
8514 static void
8515nv_redo(cap)
8516 cmdarg_T *cap;
8517{
8518 if (!checkclearopq(cap->oap))
8519 {
8520 u_redo((int)cap->count1);
8521 curwin->w_set_curswant = TRUE;
8522 }
8523}
8524
8525/*
8526 * Handle "U" command.
8527 */
8528 static void
8529nv_Undo(cap)
8530 cmdarg_T *cap;
8531{
8532 /* In Visual mode and typing "gUU" triggers an operator */
8533 if (cap->oap->op_type == OP_UPPER
8534#ifdef FEAT_VISUAL
8535 || VIsual_active
8536#endif
8537 )
8538 {
8539 /* translate "gUU" to "gUgU" */
8540 cap->cmdchar = 'g';
8541 cap->nchar = 'U';
8542 nv_operator(cap);
8543 }
8544 else if (!checkclearopq(cap->oap))
8545 {
8546 u_undoline();
8547 curwin->w_set_curswant = TRUE;
8548 }
8549}
8550
8551/*
8552 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8553 * single character.
8554 */
8555 static void
8556nv_tilde(cap)
8557 cmdarg_T *cap;
8558{
8559 if (!p_to
8560#ifdef FEAT_VISUAL
8561 && !VIsual_active
8562#endif
8563 && cap->oap->op_type != OP_TILDE)
8564 n_swapchar(cap);
8565 else
8566 nv_operator(cap);
8567}
8568
8569/*
8570 * Handle an operator command.
8571 * The actual work is done by do_pending_operator().
8572 */
8573 static void
8574nv_operator(cap)
8575 cmdarg_T *cap;
8576{
8577 int op_type;
8578
8579 op_type = get_op_type(cap->cmdchar, cap->nchar);
8580
8581 if (op_type == cap->oap->op_type) /* double operator works on lines */
8582 nv_lineop(cap);
8583 else if (!checkclearop(cap->oap))
8584 {
8585 cap->oap->start = curwin->w_cursor;
8586 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008587#ifdef FEAT_EVAL
8588 set_op_var(op_type);
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591}
8592
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008593#ifdef FEAT_EVAL
8594/*
8595 * Set v:operator to the characters for "optype".
8596 */
8597 static void
8598set_op_var(optype)
8599 int optype;
8600{
8601 char_u opchars[3];
8602
8603 if (optype == OP_NOP)
8604 set_vim_var_string(VV_OP, NULL, 0);
8605 else
8606 {
8607 opchars[0] = get_op_char(optype);
8608 opchars[1] = get_extra_op_char(optype);
8609 opchars[2] = NUL;
8610 set_vim_var_string(VV_OP, opchars, -1);
8611 }
8612}
8613#endif
8614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615/*
8616 * Handle linewise operator "dd", "yy", etc.
8617 *
8618 * "_" is is a strange motion command that helps make operators more logical.
8619 * It is actually implemented, but not documented in the real Vi. This motion
8620 * command actually refers to "the current line". Commands like "dd" and "yy"
8621 * are really an alternate form of "d_" and "y_". It does accept a count, so
8622 * "d3_" works to delete 3 lines.
8623 */
8624 static void
8625nv_lineop(cap)
8626 cmdarg_T *cap;
8627{
8628 cap->oap->motion_type = MLINE;
8629 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8630 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008631 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8632 && cap->oap->motion_force != 'v'
8633 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 || cap->oap->op_type == OP_LSHIFT
8635 || cap->oap->op_type == OP_RSHIFT)
8636 beginline(BL_SOL | BL_FIX);
8637 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8638 beginline(BL_WHITE | BL_FIX);
8639}
8640
8641/*
8642 * <Home> command.
8643 */
8644 static void
8645nv_home(cap)
8646 cmdarg_T *cap;
8647{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008648 /* CTRL-HOME is like "gg" */
8649 if (mod_mask & MOD_MASK_CTRL)
8650 nv_goto(cap);
8651 else
8652 {
8653 cap->count0 = 1;
8654 nv_pipe(cap);
8655 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008656 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8657 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658}
8659
8660/*
8661 * "|" command.
8662 */
8663 static void
8664nv_pipe(cap)
8665 cmdarg_T *cap;
8666{
8667 cap->oap->motion_type = MCHAR;
8668 cap->oap->inclusive = FALSE;
8669 beginline(0);
8670 if (cap->count0 > 0)
8671 {
8672 coladvance((colnr_T)(cap->count0 - 1));
8673 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8674 }
8675 else
8676 curwin->w_curswant = 0;
8677 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008678 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 curwin->w_set_curswant = FALSE;
8680}
8681
8682/*
8683 * Handle back-word command "b" and "B".
8684 * cap->arg is 1 for "B"
8685 */
8686 static void
8687nv_bck_word(cap)
8688 cmdarg_T *cap;
8689{
8690 cap->oap->motion_type = MCHAR;
8691 cap->oap->inclusive = FALSE;
8692 curwin->w_set_curswant = TRUE;
8693 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8694 clearopbeep(cap->oap);
8695#ifdef FEAT_FOLDING
8696 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8697 foldOpenCursor();
8698#endif
8699}
8700
8701/*
8702 * Handle word motion commands "e", "E", "w" and "W".
8703 * cap->arg is TRUE for "E" and "W".
8704 */
8705 static void
8706nv_wordcmd(cap)
8707 cmdarg_T *cap;
8708{
8709 int n;
8710 int word_end;
8711 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008712 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713
8714 /*
8715 * Set inclusive for the "E" and "e" command.
8716 */
8717 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8718 word_end = TRUE;
8719 else
8720 word_end = FALSE;
8721 cap->oap->inclusive = word_end;
8722
8723 /*
8724 * "cw" and "cW" are a special case.
8725 */
8726 if (!word_end && cap->oap->op_type == OP_CHANGE)
8727 {
8728 n = gchar_cursor();
8729 if (n != NUL) /* not an empty line */
8730 {
8731 if (vim_iswhite(n))
8732 {
8733 /*
8734 * Reproduce a funny Vi behaviour: "cw" on a blank only
8735 * changes one character, not all blanks until the start of
8736 * the next word. Only do this when the 'w' flag is included
8737 * in 'cpoptions'.
8738 */
8739 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8740 {
8741 cap->oap->inclusive = TRUE;
8742 cap->oap->motion_type = MCHAR;
8743 return;
8744 }
8745 }
8746 else
8747 {
8748 /*
8749 * This is a little strange. To match what the real Vi does,
8750 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8751 * that we are not on a space or a TAB. This seems impolite
8752 * at first, but it's really more what we mean when we say
8753 * 'cw'.
8754 * Another strangeness: When standing on the end of a word
8755 * "ce" will change until the end of the next wordt, but "cw"
8756 * will change only one character! This is done by setting
8757 * flag.
8758 */
8759 cap->oap->inclusive = TRUE;
8760 word_end = TRUE;
8761 flag = TRUE;
8762 }
8763 }
8764 }
8765
8766 cap->oap->motion_type = MCHAR;
8767 curwin->w_set_curswant = TRUE;
8768 if (word_end)
8769 n = end_word(cap->count1, cap->arg, flag, FALSE);
8770 else
8771 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8772
Bram Moolenaardfefb982008-04-01 10:06:39 +00008773 /* Don't leave the cursor on the NUL past the end of line. Unless we
8774 * didn't move it forward. */
8775 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008776 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
8778 if (n == FAIL && cap->oap->op_type == OP_NOP)
8779 clearopbeep(cap->oap);
8780 else
8781 {
8782#ifdef FEAT_VISUAL
8783 adjust_for_sel(cap);
8784#endif
8785#ifdef FEAT_FOLDING
8786 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8787 foldOpenCursor();
8788#endif
8789 }
8790}
8791
8792/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008793 * Used after a movement command: If the cursor ends up on the NUL after the
8794 * end of the line, may move it back to the last character and make the motion
8795 * inclusive.
8796 */
8797 static void
8798adjust_cursor(oap)
8799 oparg_T *oap;
8800{
8801 /* The cursor cannot remain on the NUL when:
8802 * - the column is > 0
8803 * - not in Visual mode or 'selection' is "o"
8804 * - 'virtualedit' is not "all" and not "onemore".
8805 */
8806 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8807#ifdef FEAT_VISUAL
8808 && (!VIsual_active || *p_sel == 'o')
8809#endif
8810#ifdef FEAT_VIRTUALEDIT
8811 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8812#endif
8813 )
8814 {
8815 --curwin->w_cursor.col;
8816#ifdef FEAT_MBYTE
8817 /* prevent cursor from moving on the trail byte */
8818 if (has_mbyte)
8819 mb_adjust_cursor();
8820#endif
8821 oap->inclusive = TRUE;
8822 }
8823}
8824
8825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 * "0" and "^" commands.
8827 * cap->arg is the argument for beginline().
8828 */
8829 static void
8830nv_beginline(cap)
8831 cmdarg_T *cap;
8832{
8833 cap->oap->motion_type = MCHAR;
8834 cap->oap->inclusive = FALSE;
8835 beginline(cap->arg);
8836#ifdef FEAT_FOLDING
8837 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8838 foldOpenCursor();
8839#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008840 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8841 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
8844#ifdef FEAT_VISUAL
8845/*
8846 * In exclusive Visual mode, may include the last character.
8847 */
8848 static void
8849adjust_for_sel(cap)
8850 cmdarg_T *cap;
8851{
8852 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8853 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8854 {
8855# ifdef FEAT_MBYTE
8856 if (has_mbyte)
8857 inc_cursor();
8858 else
8859# endif
8860 ++curwin->w_cursor.col;
8861 cap->oap->inclusive = FALSE;
8862 }
8863}
8864
8865/*
8866 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8867 * Should check VIsual_mode before calling this.
8868 * Returns TRUE when backed up to the previous line.
8869 */
8870 static int
8871unadjust_for_sel()
8872{
8873 pos_T *pp;
8874
8875 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8876 {
8877 if (lt(VIsual, curwin->w_cursor))
8878 pp = &curwin->w_cursor;
8879 else
8880 pp = &VIsual;
8881#ifdef FEAT_VIRTUALEDIT
8882 if (pp->coladd > 0)
8883 --pp->coladd;
8884 else
8885#endif
8886 if (pp->col > 0)
8887 {
8888 --pp->col;
8889#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008890 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#endif
8892 }
8893 else if (pp->lnum > 1)
8894 {
8895 --pp->lnum;
8896 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8897 return TRUE;
8898 }
8899 }
8900 return FALSE;
8901}
8902
8903/*
8904 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8905 */
8906 static void
8907nv_select(cap)
8908 cmdarg_T *cap;
8909{
8910 if (VIsual_active)
8911 VIsual_select = TRUE;
8912 else if (VIsual_reselect)
8913 {
8914 cap->nchar = 'v'; /* fake "gv" command */
8915 cap->arg = TRUE;
8916 nv_g_cmd(cap);
8917 }
8918}
8919
8920#endif
8921
8922/*
8923 * "G", "gg", CTRL-END, CTRL-HOME.
8924 * cap->arg is TRUE for "G".
8925 */
8926 static void
8927nv_goto(cap)
8928 cmdarg_T *cap;
8929{
8930 linenr_T lnum;
8931
8932 if (cap->arg)
8933 lnum = curbuf->b_ml.ml_line_count;
8934 else
8935 lnum = 1L;
8936 cap->oap->motion_type = MLINE;
8937 setpcmark();
8938
8939 /* When a count is given, use it instead of the default lnum */
8940 if (cap->count0 != 0)
8941 lnum = cap->count0;
8942 if (lnum < 1L)
8943 lnum = 1L;
8944 else if (lnum > curbuf->b_ml.ml_line_count)
8945 lnum = curbuf->b_ml.ml_line_count;
8946 curwin->w_cursor.lnum = lnum;
8947 beginline(BL_SOL | BL_FIX);
8948#ifdef FEAT_FOLDING
8949 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8950 foldOpenCursor();
8951#endif
8952}
8953
8954/*
8955 * CTRL-\ in Normal mode.
8956 */
8957 static void
8958nv_normal(cap)
8959 cmdarg_T *cap;
8960{
8961 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8962 {
8963 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008964 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 clear_cmdline = TRUE; /* unshow mode later */
8966 restart_edit = 0;
8967#ifdef FEAT_CMDWIN
8968 if (cmdwin_type != 0)
8969 cmdwin_result = Ctrl_C;
8970#endif
8971#ifdef FEAT_VISUAL
8972 if (VIsual_active)
8973 {
8974 end_visual_mode(); /* stop Visual */
8975 redraw_curbuf_later(INVERTED);
8976 }
8977#endif
8978 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8979 if (cap->nchar == Ctrl_G && p_im)
8980 restart_edit = 'a';
8981 }
8982 else
8983 clearopbeep(cap->oap);
8984}
8985
8986/*
8987 * ESC in Normal mode: beep, but don't flush buffers.
8988 * Don't even beep if we are canceling a command.
8989 */
8990 static void
8991nv_esc(cap)
8992 cmdarg_T *cap;
8993{
8994 int no_reason;
8995
8996 no_reason = (cap->oap->op_type == OP_NOP
8997 && cap->opcount == 0
8998 && cap->count0 == 0
8999 && cap->oap->regname == 0
9000 && !p_im);
9001
9002 if (cap->arg) /* TRUE for CTRL-C */
9003 {
9004 if (restart_edit == 0
9005#ifdef FEAT_CMDWIN
9006 && cmdwin_type == 0
9007#endif
9008#ifdef FEAT_VISUAL
9009 && !VIsual_active
9010#endif
9011 && no_reason)
9012 MSG(_("Type :quit<Enter> to exit Vim"));
9013
9014 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9015 * set again below when halfway a mapping. */
9016 if (!p_im)
9017 restart_edit = 0;
9018#ifdef FEAT_CMDWIN
9019 if (cmdwin_type != 0)
9020 {
9021 cmdwin_result = K_IGNORE;
9022 got_int = FALSE; /* don't stop executing autocommands et al. */
9023 return;
9024 }
9025#endif
9026 }
9027
9028#ifdef FEAT_VISUAL
9029 if (VIsual_active)
9030 {
9031 end_visual_mode(); /* stop Visual */
9032 check_cursor_col(); /* make sure cursor is not beyond EOL */
9033 curwin->w_set_curswant = TRUE;
9034 redraw_curbuf_later(INVERTED);
9035 }
9036 else
9037#endif
9038 if (no_reason)
9039 vim_beep();
9040 clearop(cap->oap);
9041
9042 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9043 * set return to Insert mode afterwards. */
9044 if (restart_edit == 0 && goto_im()
9045#ifdef FEAT_EX_EXTRA
9046 && ex_normal_busy == 0
9047#endif
9048 )
9049 restart_edit = 'a';
9050}
9051
9052/*
9053 * Handle "A", "a", "I", "i" and <Insert> commands.
9054 */
9055 static void
9056nv_edit(cap)
9057 cmdarg_T *cap;
9058{
9059 /* <Insert> is equal to "i" */
9060 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9061 cap->cmdchar = 'i';
9062
9063#ifdef FEAT_VISUAL
9064 /* in Visual mode "A" and "I" are an operator */
9065 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9066 v_visop(cap);
9067
9068 /* in Visual mode and after an operator "a" and "i" are for text objects */
9069 else
9070#endif
9071 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9072 && (cap->oap->op_type != OP_NOP
9073#ifdef FEAT_VISUAL
9074 || VIsual_active
9075#endif
9076 ))
9077 {
9078#ifdef FEAT_TEXTOBJ
9079 nv_object(cap);
9080#else
9081 clearopbeep(cap->oap);
9082#endif
9083 }
9084 else if (!curbuf->b_p_ma && !p_im)
9085 {
9086 /* Only give this error when 'insertmode' is off. */
9087 EMSG(_(e_modifiable));
9088 clearop(cap->oap);
9089 }
9090 else if (!checkclearopq(cap->oap))
9091 {
9092 switch (cap->cmdchar)
9093 {
9094 case 'A': /* "A"ppend after the line */
9095 curwin->w_set_curswant = TRUE;
9096#ifdef FEAT_VIRTUALEDIT
9097 if (ve_flags == VE_ALL)
9098 {
9099 int save_State = State;
9100
9101 /* Pretent Insert mode here to allow the cursor on the
9102 * character past the end of the line */
9103 State = INSERT;
9104 coladvance((colnr_T)MAXCOL);
9105 State = save_State;
9106 }
9107 else
9108#endif
9109 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9110 break;
9111
9112 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009113 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9114 beginline(BL_WHITE);
9115 else
9116 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 break;
9118
9119 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9120#ifdef FEAT_VIRTUALEDIT
9121 /* increment coladd when in virtual space, increment the
9122 * column otherwise, also to append after an unprintable char */
9123 if (virtual_active()
9124 && (curwin->w_cursor.coladd > 0
9125 || *ml_get_cursor() == NUL
9126 || *ml_get_cursor() == TAB))
9127 curwin->w_cursor.coladd++;
9128 else
9129#endif
9130 if (*ml_get_cursor() != NUL)
9131 inc_cursor();
9132 break;
9133 }
9134
9135#ifdef FEAT_VIRTUALEDIT
9136 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9137 {
9138 int save_State = State;
9139
9140 /* Pretent Insert mode here to allow the cursor on the
9141 * character past the end of the line */
9142 State = INSERT;
9143 coladvance(getviscol());
9144 State = save_State;
9145 }
9146#endif
9147
9148 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9149 }
9150}
9151
9152/*
9153 * Invoke edit() and take care of "restart_edit" and the return value.
9154 */
9155 static void
9156invoke_edit(cap, repl, cmd, startln)
9157 cmdarg_T *cap;
9158 int repl; /* "r" or "gr" command */
9159 int cmd;
9160 int startln;
9161{
9162 int restart_edit_save = 0;
9163
9164 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9165 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9166 * it. */
9167 if (repl || !stuff_empty())
9168 restart_edit_save = restart_edit;
9169 else
9170 restart_edit_save = 0;
9171
9172 /* Always reset "restart_edit", this is not a restarted edit. */
9173 restart_edit = 0;
9174
9175 if (edit(cmd, startln, cap->count1))
9176 cap->retval |= CA_COMMAND_BUSY;
9177
9178 if (restart_edit == 0)
9179 restart_edit = restart_edit_save;
9180}
9181
9182#ifdef FEAT_TEXTOBJ
9183/*
9184 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9185 */
9186 static void
9187nv_object(cap)
9188 cmdarg_T *cap;
9189{
9190 int flag;
9191 int include;
9192 char_u *mps_save;
9193
9194 if (cap->cmdchar == 'i')
9195 include = FALSE; /* "ix" = inner object: exclude white space */
9196 else
9197 include = TRUE; /* "ax" = an object: include white space */
9198
9199 /* Make sure (), [], {} and <> are in 'matchpairs' */
9200 mps_save = curbuf->b_p_mps;
9201 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9202
9203 switch (cap->nchar)
9204 {
9205 case 'w': /* "aw" = a word */
9206 flag = current_word(cap->oap, cap->count1, include, FALSE);
9207 break;
9208 case 'W': /* "aW" = a WORD */
9209 flag = current_word(cap->oap, cap->count1, include, TRUE);
9210 break;
9211 case 'b': /* "ab" = a braces block */
9212 case '(':
9213 case ')':
9214 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9215 break;
9216 case 'B': /* "aB" = a Brackets block */
9217 case '{':
9218 case '}':
9219 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9220 break;
9221 case '[': /* "a[" = a [] block */
9222 case ']':
9223 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9224 break;
9225 case '<': /* "a<" = a <> block */
9226 case '>':
9227 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9228 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009229 case 't': /* "at" = a tag block (xml and html) */
9230 flag = current_tagblock(cap->oap, cap->count1, include);
9231 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 case 'p': /* "ap" = a paragraph */
9233 flag = current_par(cap->oap, cap->count1, include, 'p');
9234 break;
9235 case 's': /* "as" = a sentence */
9236 flag = current_sent(cap->oap, cap->count1, include);
9237 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009238 case '"': /* "a"" = a double quoted string */
9239 case '\'': /* "a'" = a single quoted string */
9240 case '`': /* "a`" = a backtick quoted string */
9241 flag = current_quote(cap->oap, cap->count1, include,
9242 cap->nchar);
9243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#if 0 /* TODO */
9245 case 'S': /* "aS" = a section */
9246 case 'f': /* "af" = a filename */
9247 case 'u': /* "au" = a URL */
9248#endif
9249 default:
9250 flag = FAIL;
9251 break;
9252 }
9253
9254 curbuf->b_p_mps = mps_save;
9255 if (flag == FAIL)
9256 clearopbeep(cap->oap);
9257 adjust_cursor_col();
9258 curwin->w_set_curswant = TRUE;
9259}
9260#endif
9261
9262/*
9263 * "q" command: Start/stop recording.
9264 * "q:", "q/", "q?": edit command-line in command-line window.
9265 */
9266 static void
9267nv_record(cap)
9268 cmdarg_T *cap;
9269{
9270 if (cap->oap->op_type == OP_FORMAT)
9271 {
9272 /* "gqq" is the same as "gqgq": format line */
9273 cap->cmdchar = 'g';
9274 cap->nchar = 'q';
9275 nv_operator(cap);
9276 }
9277 else if (!checkclearop(cap->oap))
9278 {
9279#ifdef FEAT_CMDWIN
9280 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9281 {
9282 stuffcharReadbuff(cap->nchar);
9283 stuffcharReadbuff(K_CMDWIN);
9284 }
9285 else
9286#endif
9287 /* (stop) recording into a named register, unless executing a
9288 * register */
9289 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9290 clearopbeep(cap->oap);
9291 }
9292}
9293
9294/*
9295 * Handle the "@r" command.
9296 */
9297 static void
9298nv_at(cap)
9299 cmdarg_T *cap;
9300{
9301 if (checkclearop(cap->oap))
9302 return;
9303#ifdef FEAT_EVAL
9304 if (cap->nchar == '=')
9305 {
9306 if (get_expr_register() == NUL)
9307 return;
9308 }
9309#endif
9310 while (cap->count1-- && !got_int)
9311 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009312 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 {
9314 clearopbeep(cap->oap);
9315 break;
9316 }
9317 line_breakcheck();
9318 }
9319}
9320
9321/*
9322 * Handle the CTRL-U and CTRL-D commands.
9323 */
9324 static void
9325nv_halfpage(cap)
9326 cmdarg_T *cap;
9327{
9328 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9329 || (cap->cmdchar == Ctrl_D
9330 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9331 clearopbeep(cap->oap);
9332 else if (!checkclearop(cap->oap))
9333 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9334}
9335
9336/*
9337 * Handle "J" or "gJ" command.
9338 */
9339 static void
9340nv_join(cap)
9341 cmdarg_T *cap;
9342{
9343#ifdef FEAT_VISUAL
9344 if (VIsual_active) /* join the visual lines */
9345 nv_operator(cap);
9346 else
9347#endif
9348 if (!checkclearop(cap->oap))
9349 {
9350 if (cap->count0 <= 1)
9351 cap->count0 = 2; /* default for join is two lines! */
9352 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9353 curbuf->b_ml.ml_line_count)
9354 clearopbeep(cap->oap); /* beyond last line */
9355 else
9356 {
9357 prep_redo(cap->oap->regname, cap->count0,
9358 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009359 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 }
9361 }
9362}
9363
9364/*
9365 * "P", "gP", "p" and "gp" commands.
9366 */
9367 static void
9368nv_put(cap)
9369 cmdarg_T *cap;
9370{
9371#ifdef FEAT_VISUAL
9372 int regname = 0;
9373 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009374 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009375 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#endif
9377 int dir;
9378 int flags = 0;
9379
9380 if (cap->oap->op_type != OP_NOP)
9381 {
9382#ifdef FEAT_DIFF
9383 /* "dp" is ":diffput" */
9384 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9385 {
9386 clearop(cap->oap);
9387 nv_diffgetput(TRUE);
9388 }
9389 else
9390#endif
9391 clearopbeep(cap->oap);
9392 }
9393 else
9394 {
9395 dir = (cap->cmdchar == 'P'
9396 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9397 ? BACKWARD : FORWARD;
9398 prep_redo_cmd(cap);
9399 if (cap->cmdchar == 'g')
9400 flags |= PUT_CURSEND;
9401
9402#ifdef FEAT_VISUAL
9403 if (VIsual_active)
9404 {
9405 /* Putting in Visual mode: The put text replaces the selected
9406 * text. First delete the selected text, then put the new text.
9407 * Need to save and restore the registers that the delete
9408 * overwrites if the old contents is being put.
9409 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009410 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 regname = cap->oap->regname;
9412# ifdef FEAT_CLIPBOARD
9413 adjust_clip_reg(&regname);
9414# endif
Bram Moolenaar53748fc2012-01-26 11:43:09 +01009415 if (regname == 0 || regname == '"' || VIM_ISDIGIT(regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416# ifdef FEAT_CLIPBOARD
9417 || (clip_unnamed && (regname == '*' || regname == '+'))
9418# endif
9419
9420 )
9421 {
9422 /* the delete is going to overwrite the register we want to
9423 * put, save it first. */
9424 reg1 = get_register(regname, TRUE);
9425 }
9426
9427 /* Now delete the selected text. */
9428 cap->cmdchar = 'd';
9429 cap->nchar = NUL;
9430 cap->oap->regname = NUL;
9431 nv_operator(cap);
9432 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009433 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434
9435 /* delete PUT_LINE_BACKWARD; */
9436 cap->oap->regname = regname;
9437
9438 if (reg1 != NULL)
9439 {
9440 /* Delete probably changed the register we want to put, save
9441 * it first. Then put back what was there before the delete. */
9442 reg2 = get_register(regname, FALSE);
9443 put_register(regname, reg1);
9444 }
9445
9446 /* When deleted a linewise Visual area, put the register as
9447 * lines to avoid it joined with the next line. When deletion was
9448 * characterwise, split a line when putting lines. */
9449 if (VIsual_mode == 'V')
9450 flags |= PUT_LINE;
9451 else if (VIsual_mode == 'v')
9452 flags |= PUT_LINE_SPLIT;
9453 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9454 flags |= PUT_LINE_FORWARD;
9455 dir = BACKWARD;
9456 if ((VIsual_mode != 'V'
9457 && curwin->w_cursor.col < curbuf->b_op_start.col)
9458 || (VIsual_mode == 'V'
9459 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9460 /* cursor is at the end of the line or end of file, put
9461 * forward. */
9462 dir = FORWARD;
9463 }
9464#endif
9465 do_put(cap->oap->regname, dir, cap->count1, flags);
9466
9467#ifdef FEAT_VISUAL
9468 /* If a register was saved, put it back now. */
9469 if (reg2 != NULL)
9470 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009471
9472 /* What to reselect with "gv"? Selecting the just put text seems to
9473 * be the most useful, since the original text was removed. */
9474 if (was_visual)
9475 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009476 curbuf->b_visual.vi_start = curbuf->b_op_start;
9477 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009478 }
9479
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009480 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009481 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009482 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009483 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009484 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009485
9486 /* If the cursor was in that line, move it to the end of the last
9487 * line. */
9488 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9489 {
9490 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9491 coladvance((colnr_T)MAXCOL);
9492 }
9493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494#endif
9495 auto_format(FALSE, TRUE);
9496 }
9497}
9498
9499/*
9500 * "o" and "O" commands.
9501 */
9502 static void
9503nv_open(cap)
9504 cmdarg_T *cap;
9505{
9506#ifdef FEAT_DIFF
9507 /* "do" is ":diffget" */
9508 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9509 {
9510 clearop(cap->oap);
9511 nv_diffgetput(FALSE);
9512 }
9513 else
9514#endif
9515#ifdef FEAT_VISUAL
9516 if (VIsual_active) /* switch start and end of visual */
9517 v_swap_corners(cap->cmdchar);
9518 else
9519#endif
9520 n_opencmd(cap);
9521}
9522
9523#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 static void
9525nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009526 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527{
9528 ProcessSniffRequests();
9529}
9530#endif
9531
9532#ifdef FEAT_NETBEANS_INTG
9533 static void
9534nv_nbcmd(cap)
9535 cmdarg_T *cap;
9536{
9537 netbeans_keycommand(cap->nchar);
9538}
9539#endif
9540
9541#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 static void
9543nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009544 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9547}
9548#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009549
9550#ifdef FEAT_AUTOCMD
9551/*
9552 * Trigger CursorHold event.
9553 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9554 * input buffer. "did_cursorhold" is set to avoid retriggering.
9555 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009556 static void
9557nv_cursorhold(cap)
9558 cmdarg_T *cap;
9559{
9560 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9561 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009562 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009563}
9564#endif