blob: aa470e4f7577bd318a4d7f33ff4e77b68b0e3c24 [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
32# ifdef __BORLANDC__
33_RTLENTRYF
34# endif
35 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
963 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
964 || ca.nchar == Ctrl_BSL)
965 {
966 cp = &ca.extra_char; /* need to get a third character */
967 if (ca.nchar != 'r')
968 lit = TRUE; /* get it literally */
969 else
970 repl = TRUE; /* get it in replace mode */
971 }
972 else
973 cp = NULL; /* no third character needed */
974 }
975 else
976 {
977 if (ca.cmdchar == 'r') /* get it in replace mode */
978 repl = TRUE;
979 cp = &ca.nchar;
980 }
981 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
982
983 /*
984 * Get a second or third character.
985 */
986 if (cp != NULL)
987 {
988#ifdef CURSOR_SHAPE
989 if (repl)
990 {
991 State = REPLACE; /* pretend Replace mode */
992 ui_cursor_shape(); /* show different cursor shape */
993 }
994#endif
995 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
996 {
997 /* Allow mappings defined with ":lmap". */
998 --no_mapping;
999 --allow_keys;
1000 if (repl)
1001 State = LREPLACE;
1002 else
1003 State = LANGMAP;
1004 langmap_active = TRUE;
1005 }
1006#ifdef USE_IM_CONTROL
1007 save_smd = p_smd;
1008 p_smd = FALSE; /* Don't let the IM code show the mode here */
1009 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1010 im_set_active(TRUE);
1011#endif
1012
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001013 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if (langmap_active)
1016 {
1017 /* Undo the decrement done above */
1018 ++no_mapping;
1019 ++allow_keys;
1020 State = NORMAL_BUSY;
1021 }
1022#ifdef USE_IM_CONTROL
1023 if (lang)
1024 {
1025 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1026 im_save_status(&curbuf->b_p_iminsert);
1027 im_set_active(FALSE);
1028 }
1029 p_smd = save_smd;
1030#endif
1031#ifdef CURSOR_SHAPE
1032 State = NORMAL_BUSY;
1033#endif
1034#ifdef FEAT_CMDL_INFO
1035 need_flushbuf |= add_to_showcmd(*cp);
1036#endif
1037
1038 if (!lit)
1039 {
1040#ifdef FEAT_DIGRAPHS
1041 /* Typing CTRL-K gets a digraph. */
1042 if (*cp == Ctrl_K
1043 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1044 || cp == &ca.extra_char)
1045 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1046 {
1047 c = get_digraph(FALSE);
1048 if (c > 0)
1049 {
1050 *cp = c;
1051# ifdef FEAT_CMDL_INFO
1052 /* Guessing how to update showcmd here... */
1053 del_from_showcmd(3);
1054 need_flushbuf |= add_to_showcmd(*cp);
1055# endif
1056 }
1057 }
1058#endif
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /* adjust chars > 127, except after "tTfFr" commands */
1061 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_RIGHTLEFT
1063 /* adjust Hebrew mapped char */
1064 if (p_hkmap && lang && KeyTyped)
1065 *cp = hkmap(*cp);
1066# ifdef FEAT_FKMAP
1067 /* adjust Farsi mapped char */
1068 if (p_fkmap && lang && KeyTyped)
1069 *cp = fkmap(*cp);
1070# endif
1071#endif
1072 }
1073
1074 /*
1075 * When the next character is CTRL-\ a following CTRL-N means the
1076 * command is aborted and we go to Normal mode.
1077 */
1078 if (cp == &ca.extra_char
1079 && ca.nchar == Ctrl_BSL
1080 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1081 {
1082 ca.cmdchar = Ctrl_BSL;
1083 ca.nchar = ca.extra_char;
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if (*cp == Ctrl_BSL)
1087 {
1088 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1089
1090 /* There is a busy wait here when typing "f<C-\>" and then
1091 * something different from CTRL-N. Can't be avoided. */
1092 while ((c = vpeekc()) <= 0 && towait > 0L)
1093 {
1094 do_sleep(towait > 50L ? 50L : towait);
1095 towait -= 50L;
1096 }
1097 if (c > 0)
1098 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001099 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (c != Ctrl_N && c != Ctrl_G)
1101 vungetc(c);
1102 else
1103 {
1104 ca.cmdchar = Ctrl_BSL;
1105 ca.nchar = c;
1106 idx = find_command(ca.cmdchar);
1107 }
1108 }
1109 }
1110
1111#ifdef FEAT_MBYTE
1112 /* When getting a text character and the next character is a
1113 * multi-byte character, it could be a composing character.
1114 * However, don't wait for it to arrive. */
1115 while (enc_utf8 && lang && (c = vpeekc()) > 0
1116 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1117 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001118 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!utf_iscomposing(c))
1120 {
1121 vungetc(c); /* it wasn't, put it back */
1122 break;
1123 }
1124 else if (ca.ncharC1 == 0)
1125 ca.ncharC1 = c;
1126 else
1127 ca.ncharC2 = c;
1128 }
1129#endif
1130 }
1131 --no_mapping;
1132 --allow_keys;
1133 }
1134
1135#ifdef FEAT_CMDL_INFO
1136 /*
1137 * Flush the showcmd characters onto the screen so we can see them while
1138 * the command is being executed. Only do this when the shown command was
1139 * actually displayed, otherwise this will slow down a lot when executing
1140 * mappings.
1141 */
1142 if (need_flushbuf)
1143 out_flush();
1144#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001145#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001146 if (ca.cmdchar != K_IGNORE)
1147 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 State = NORMAL;
1151
1152 if (ca.nchar == ESC)
1153 {
1154 clearop(oap);
1155 if (restart_edit == 0 && goto_im())
1156 restart_edit = 'a';
1157 goto normal_end;
1158 }
1159
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160 if (ca.cmdchar != K_IGNORE)
1161 {
1162 msg_didout = FALSE; /* don't scroll screen up for normal command */
1163 msg_col = 0;
1164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166#ifdef FEAT_VISUAL
1167 old_pos = curwin->w_cursor; /* remember where cursor was */
1168
1169 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1170 * mode. */
1171 if (!VIsual_active && km_startsel)
1172 {
1173 if (nv_cmds[idx].cmd_flags & NV_SS)
1174 {
1175 start_selection();
1176 unshift_special(&ca);
1177 idx = find_command(ca.cmdchar);
1178 }
1179 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1180 && (mod_mask & MOD_MASK_SHIFT))
1181 {
1182 start_selection();
1183 mod_mask &= ~MOD_MASK_SHIFT;
1184 }
1185 }
1186#endif
1187
1188 /*
1189 * Execute the command!
1190 * Call the command function found in the commands table.
1191 */
1192 ca.arg = nv_cmds[idx].cmd_arg;
1193 (nv_cmds[idx].cmd_func)(&ca);
1194
1195 /*
1196 * If we didn't start or finish an operator, reset oap->regname, unless we
1197 * need it later.
1198 */
1199 if (!finish_op
1200 && !oap->op_type
1201 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1202 {
1203 clearop(oap);
1204#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001205 {
1206 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001207
Bram Moolenaar536681b2011-05-10 16:12:45 +02001208 /* Adjust the register according to 'clipboard', so that when
1209 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001210# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001211 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 set_reg_var(regname);
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216 }
1217
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001218#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001219 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001220 * character or "z333<cr>". */
1221 if (old_mapped_len > 0)
1222 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 /*
1226 * If an operation is pending, handle it...
1227 */
1228 do_pending_operator(&ca, old_col, FALSE);
1229
1230 /*
1231 * Wait for a moment when a message is displayed that will be overwritten
1232 * by the mode message.
1233 * In Visual mode and with "^O" in Insert mode, a short message will be
1234 * overwritten by the mode message. Wait a bit, until a key is hit.
1235 * In Visual mode, it's more important to keep the Visual area updated
1236 * than keeping a message (e.g. from a /pat search).
1237 * Only do this if the command was typed, not from a mapping.
1238 * Don't wait when emsg_silent is non-zero.
1239 * Also wait a bit after an error message, e.g. for "^O:".
1240 * Don't redraw the screen, it would remove the message.
1241 */
1242 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001243 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 && (restart_edit != 0
1245#ifdef FEAT_VISUAL
1246 || (VIsual_active
1247 && old_pos.lnum == curwin->w_cursor.lnum
1248 && old_pos.col == curwin->w_cursor.col)
1249#endif
1250 )
1251 && (clear_cmdline
1252 || redraw_cmdline)
1253 && (msg_didout || (msg_didany && msg_scroll))
1254 && !msg_nowait
1255 && KeyTyped)
1256 || (restart_edit != 0
1257#ifdef FEAT_VISUAL
1258 && !VIsual_active
1259#endif
1260 && (msg_scroll
1261 || emsg_on_display)))
1262 && oap->regname == 0
1263 && !(ca.retval & CA_COMMAND_BUSY)
1264 && stuff_empty()
1265 && typebuf_typed()
1266 && emsg_silent == 0
1267 && !did_wait_return
1268 && oap->op_type == OP_NOP)
1269 {
1270 int save_State = State;
1271
1272 /* Draw the cursor with the right shape here */
1273 if (restart_edit != 0)
1274 State = INSERT;
1275
1276 /* If need to redraw, and there is a "keep_msg", redraw before the
1277 * delay */
1278 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1279 {
1280 char_u *kmsg;
1281
1282 kmsg = keep_msg;
1283 keep_msg = NULL;
1284 /* showmode() will clear keep_msg, but we want to use it anyway */
1285 update_screen(0);
1286 /* now reset it, otherwise it's put in the history again */
1287 keep_msg = kmsg;
1288 msg_attr(kmsg, keep_msg_attr);
1289 vim_free(kmsg);
1290 }
1291 setcursor();
1292 cursor_on();
1293 out_flush();
1294 if (msg_scroll || emsg_on_display)
1295 ui_delay(1000L, TRUE); /* wait at least one second */
1296 ui_delay(3000L, FALSE); /* wait up to three seconds */
1297 State = save_State;
1298
1299 msg_scroll = FALSE;
1300 emsg_on_display = FALSE;
1301 }
1302
1303 /*
1304 * Finish up after executing a Normal mode command.
1305 */
1306normal_end:
1307
1308 msg_nowait = FALSE;
1309
1310 /* Reset finish_op, in case it was set */
1311#ifdef CURSOR_SHAPE
1312 c = finish_op;
1313#endif
1314 finish_op = FALSE;
1315#ifdef CURSOR_SHAPE
1316 /* Redraw the cursor with another shape, if we were in Operator-pending
1317 * mode or did a replace command. */
1318 if (c || ca.cmdchar == 'r')
1319 {
1320 ui_cursor_shape(); /* may show different cursor shape */
1321# ifdef FEAT_MOUSESHAPE
1322 update_mouseshape(-1);
1323# endif
1324 }
1325#endif
1326
1327#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001328 if (oap->op_type == OP_NOP && oap->regname == 0
1329# ifdef FEAT_AUTOCMD
1330 && ca.cmdchar != K_CURSORHOLD
1331# endif
1332 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 clear_showcmd();
1334#endif
1335
1336 checkpcmark(); /* check if we moved since setting pcmark */
1337 vim_free(ca.searchbuf);
1338
1339#ifdef FEAT_MBYTE
1340 if (has_mbyte)
1341 mb_adjust_cursor();
1342#endif
1343
1344#ifdef FEAT_SCROLLBIND
1345 if (curwin->w_p_scb && toplevel)
1346 {
1347 validate_cursor(); /* may need to update w_leftcol */
1348 do_check_scrollbind(TRUE);
1349 }
1350#endif
1351
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352#ifdef FEAT_CURSORBIND
1353 if (curwin->w_p_crb && toplevel)
1354 {
1355 validate_cursor(); /* may need to update w_leftcol */
1356 do_check_cursorbind();
1357 }
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1362 * if still inside a mapping that started in Visual mode).
1363 * May switch from Visual to Select mode after CTRL-O command.
1364 */
1365 if ( oap->op_type == OP_NOP
1366#ifdef FEAT_VISUAL
1367 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1368 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001369#else
1370 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#endif
1372 && !(ca.retval & CA_COMMAND_BUSY)
1373 && stuff_empty()
1374 && oap->regname == 0)
1375 {
1376#ifdef FEAT_VISUAL
1377 if (restart_VIsual_select == 1)
1378 {
1379 VIsual_select = TRUE;
1380 showmode();
1381 restart_VIsual_select = 0;
1382 }
1383#endif
1384 if (restart_edit != 0
1385#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001386 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001388 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 (void)edit(restart_edit, FALSE, 1L);
1390 }
1391
1392#ifdef FEAT_VISUAL
1393 if (restart_VIsual_select == 2)
1394 restart_VIsual_select = 1;
1395#endif
1396
1397 /* Save count before an operator for next time. */
1398 opcount = ca.opcount;
1399}
1400
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001401#ifdef FEAT_EVAL
1402/*
1403 * Set v:count and v:count1 according to "cap".
1404 * Set v:prevcount only when "set_prevcount" is TRUE.
1405 */
1406 static void
1407set_vcount_ca(cap, set_prevcount)
1408 cmdarg_T *cap;
1409 int *set_prevcount;
1410{
1411 long count = cap->count0;
1412
1413 /* multiply with cap->opcount the same way as above */
1414 if (cap->opcount != 0)
1415 count = cap->opcount * (count == 0 ? 1 : count);
1416 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1417 *set_prevcount = FALSE; /* only set v:prevcount once */
1418}
1419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
1422 * Handle an operator after visual mode or when the movement is finished
1423 */
1424 void
1425do_pending_operator(cap, old_col, gui_yank)
1426 cmdarg_T *cap;
1427 int old_col;
1428 int gui_yank;
1429{
1430 oparg_T *oap = cap->oap;
1431 pos_T old_cursor;
1432 int empty_region_error;
1433 int restart_edit_save;
1434
1435#ifdef FEAT_VISUAL
1436 /* The visual area is remembered for redo */
1437 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1438 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001439 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 static long redo_VIsual_count; /* count for Visual operator */
1441# ifdef FEAT_VIRTUALEDIT
1442 int include_line_break = FALSE;
1443# endif
1444#endif
1445
1446#if defined(FEAT_CLIPBOARD)
1447 /*
1448 * Yank the visual area into the GUI selection register before we operate
1449 * on it and lose it forever.
1450 * Don't do it if a specific register was specified, so that ""x"*P works.
1451 * This could call do_pending_operator() recursively, but that's OK
1452 * because gui_yank will be TRUE for the nested call.
1453 */
1454 if (clip_star.available
1455 && oap->op_type != OP_NOP
1456 && !gui_yank
1457# ifdef FEAT_VISUAL
1458 && VIsual_active
1459 && !redo_VIsual_busy
1460# endif
1461 && oap->regname == 0)
1462 clip_auto_select();
1463#endif
1464 old_cursor = curwin->w_cursor;
1465
1466 /*
1467 * If an operation is pending, handle it...
1468 */
1469 if ((finish_op
1470#ifdef FEAT_VISUAL
1471 || VIsual_active
1472#endif
1473 ) && oap->op_type != OP_NOP)
1474 {
1475#ifdef FEAT_VISUAL
1476 oap->is_VIsual = VIsual_active;
1477 if (oap->motion_force == 'V')
1478 oap->motion_type = MLINE;
1479 else if (oap->motion_force == 'v')
1480 {
1481 /* If the motion was linewise, "inclusive" will not have been set.
1482 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1483 if (oap->motion_type == MLINE)
1484 oap->inclusive = FALSE;
1485 /* If the motion already was characterwise, toggle "inclusive" */
1486 else if (oap->motion_type == MCHAR)
1487 oap->inclusive = !oap->inclusive;
1488 oap->motion_type = MCHAR;
1489 }
1490 else if (oap->motion_force == Ctrl_V)
1491 {
1492 /* Change line- or characterwise motion into Visual block mode. */
1493 VIsual_active = TRUE;
1494 VIsual = oap->start;
1495 VIsual_mode = Ctrl_V;
1496 VIsual_select = FALSE;
1497 VIsual_reselect = FALSE;
1498 }
1499#endif
1500
1501 /* only redo yank when 'y' flag is in 'cpoptions' */
1502 /* never redo "zf" (define fold) */
1503 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1504#ifdef FEAT_VISUAL
1505 && (!VIsual_active || oap->motion_force)
1506#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001507 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_FOLDING
1509 && oap->op_type != OP_FOLD
1510 && oap->op_type != OP_FOLDOPEN
1511 && oap->op_type != OP_FOLDOPENREC
1512 && oap->op_type != OP_FOLDCLOSE
1513 && oap->op_type != OP_FOLDCLOSEREC
1514 && oap->op_type != OP_FOLDDEL
1515 && oap->op_type != OP_FOLDDELREC
1516#endif
1517 )
1518 {
1519 prep_redo(oap->regname, cap->count0,
1520 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1521 oap->motion_force, cap->cmdchar, cap->nchar);
1522 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1523 {
1524 /*
1525 * If 'cpoptions' does not contain 'r', insert the search
1526 * pattern to really repeat the same command.
1527 */
1528 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001529 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 AppendToRedobuff(NL_STR);
1531 }
1532 else if (cap->cmdchar == ':')
1533 {
1534 /* do_cmdline() has stored the first typed line in
1535 * "repeat_cmdline". When several lines are typed repeating
1536 * won't be possible. */
1537 if (repeat_cmdline == NULL)
1538 ResetRedobuff();
1539 else
1540 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001541 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 AppendToRedobuff(NL_STR);
1543 vim_free(repeat_cmdline);
1544 repeat_cmdline = NULL;
1545 }
1546 }
1547 }
1548
1549#ifdef FEAT_VISUAL
1550 if (redo_VIsual_busy)
1551 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001552 /* Redo of an operation on a Visual area. Use the same size from
1553 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 oap->start = curwin->w_cursor;
1555 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1556 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1557 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1558 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001559 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001561 if (VIsual_mode == 'v')
1562 {
1563 if (redo_VIsual_line_count <= 1)
1564 {
1565 validate_virtcol();
1566 curwin->w_curswant =
1567 curwin->w_virtcol + redo_VIsual_vcol - 1;
1568 }
1569 else
1570 curwin->w_curswant = redo_VIsual_vcol;
1571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001573 {
1574 curwin->w_curswant = MAXCOL;
1575 }
1576 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 cap->count0 = redo_VIsual_count;
1579 if (redo_VIsual_count != 0)
1580 cap->count1 = redo_VIsual_count;
1581 else
1582 cap->count1 = 1;
1583 }
1584 else if (VIsual_active)
1585 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001586 if (!gui_yank)
1587 {
1588 /* Save the current VIsual area for '< and '> marks, and "gv" */
1589 curbuf->b_visual.vi_start = VIsual;
1590 curbuf->b_visual.vi_end = curwin->w_cursor;
1591 curbuf->b_visual.vi_mode = VIsual_mode;
1592 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001594 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598 /* In Select mode, a linewise selection is operated upon like a
1599 * characterwise selection. */
1600 if (VIsual_select && VIsual_mode == 'V')
1601 {
1602 if (lt(VIsual, curwin->w_cursor))
1603 {
1604 VIsual.col = 0;
1605 curwin->w_cursor.col =
1606 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1607 }
1608 else
1609 {
1610 curwin->w_cursor.col = 0;
1611 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1612 }
1613 VIsual_mode = 'v';
1614 }
1615 /* If 'selection' is "exclusive", backup one character for
1616 * charwise selections. */
1617 else if (VIsual_mode == 'v')
1618 {
1619# ifdef FEAT_VIRTUALEDIT
1620 include_line_break =
1621# endif
1622 unadjust_for_sel();
1623 }
1624
1625 oap->start = VIsual;
1626 if (VIsual_mode == 'V')
1627 oap->start.col = 0;
1628 }
1629#endif /* FEAT_VISUAL */
1630
1631 /*
1632 * Set oap->start to the first position of the operated text, oap->end
1633 * to the end of the operated text. w_cursor is equal to oap->start.
1634 */
1635 if (lt(oap->start, curwin->w_cursor))
1636 {
1637#ifdef FEAT_FOLDING
1638 /* Include folded lines completely. */
1639 if (!VIsual_active)
1640 {
1641 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1642 oap->start.col = 0;
1643 if (hasFolding(curwin->w_cursor.lnum, NULL,
1644 &curwin->w_cursor.lnum))
1645 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1646 }
1647#endif
1648 oap->end = curwin->w_cursor;
1649 curwin->w_cursor = oap->start;
1650
1651 /* w_virtcol may have been updated; if the cursor goes back to its
1652 * previous position w_virtcol becomes invalid and isn't updated
1653 * automatically. */
1654 curwin->w_valid &= ~VALID_VIRTCOL;
1655 }
1656 else
1657 {
1658#ifdef FEAT_FOLDING
1659 /* Include folded lines completely. */
1660 if (!VIsual_active && oap->motion_type == MLINE)
1661 {
1662 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1663 NULL))
1664 curwin->w_cursor.col = 0;
1665 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1666 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1667 }
1668#endif
1669 oap->end = oap->start;
1670 oap->start = curwin->w_cursor;
1671 }
1672
1673 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1674
1675#ifdef FEAT_VIRTUALEDIT
1676 /* Set "virtual_op" before resetting VIsual_active. */
1677 virtual_op = virtual_active();
1678#endif
1679
1680#ifdef FEAT_VISUAL
1681 if (VIsual_active || redo_VIsual_busy)
1682 {
1683 if (VIsual_mode == Ctrl_V) /* block mode */
1684 {
1685 colnr_T start, end;
1686
1687 oap->block_mode = TRUE;
1688
1689 getvvcol(curwin, &(oap->start),
1690 &oap->start_vcol, NULL, &oap->end_vcol);
1691 if (!redo_VIsual_busy)
1692 {
1693 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1694
1695 if (start < oap->start_vcol)
1696 oap->start_vcol = start;
1697 if (end > oap->end_vcol)
1698 {
1699 if (*p_sel == 'e' && start >= 1
1700 && start - 1 >= oap->end_vcol)
1701 oap->end_vcol = start - 1;
1702 else
1703 oap->end_vcol = end;
1704 }
1705 }
1706
1707 /* if '$' was used, get oap->end_vcol from longest line */
1708 if (curwin->w_curswant == MAXCOL)
1709 {
1710 curwin->w_cursor.col = MAXCOL;
1711 oap->end_vcol = 0;
1712 for (curwin->w_cursor.lnum = oap->start.lnum;
1713 curwin->w_cursor.lnum <= oap->end.lnum;
1714 ++curwin->w_cursor.lnum)
1715 {
1716 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1717 if (end > oap->end_vcol)
1718 oap->end_vcol = end;
1719 }
1720 }
1721 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001722 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /*
1724 * Correct oap->end.col and oap->start.col to be the
1725 * upper-left and lower-right corner of the block area.
1726 *
1727 * (Actually, this does convert column positions into character
1728 * positions)
1729 */
1730 curwin->w_cursor.lnum = oap->end.lnum;
1731 coladvance(oap->end_vcol);
1732 oap->end = curwin->w_cursor;
1733
1734 curwin->w_cursor = oap->start;
1735 coladvance(oap->start_vcol);
1736 oap->start = curwin->w_cursor;
1737 }
1738
1739 if (!redo_VIsual_busy && !gui_yank)
1740 {
1741 /*
1742 * Prepare to reselect and redo Visual: this is based on the
1743 * size of the Visual text
1744 */
1745 resel_VIsual_mode = VIsual_mode;
1746 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001747 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001749 {
1750 if (VIsual_mode != Ctrl_V)
1751 getvvcol(curwin, &(oap->end),
1752 NULL, NULL, &oap->end_vcol);
1753 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1754 {
1755 if (VIsual_mode != Ctrl_V)
1756 getvvcol(curwin, &(oap->start),
1757 &oap->start_vcol, NULL, NULL);
1758 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1759 }
1760 else
1761 resel_VIsual_vcol = oap->end_vcol;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 resel_VIsual_line_count = oap->line_count;
1764 }
1765
1766 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1767 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1768 && oap->op_type != OP_COLON
1769#ifdef FEAT_FOLDING
1770 && oap->op_type != OP_FOLD
1771 && oap->op_type != OP_FOLDOPEN
1772 && oap->op_type != OP_FOLDOPENREC
1773 && oap->op_type != OP_FOLDCLOSE
1774 && oap->op_type != OP_FOLDCLOSEREC
1775 && oap->op_type != OP_FOLDDEL
1776 && oap->op_type != OP_FOLDDELREC
1777#endif
1778 && oap->motion_force == NUL
1779 )
1780 {
1781 /* Prepare for redoing. Only use the nchar field for "r",
1782 * otherwise it might be the second char of the operator. */
1783 prep_redo(oap->regname, 0L, NUL, 'v',
1784 get_op_char(oap->op_type),
1785 get_extra_op_char(oap->op_type),
1786 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1787 if (!redo_VIsual_busy)
1788 {
1789 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001790 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 redo_VIsual_line_count = resel_VIsual_line_count;
1792 redo_VIsual_count = cap->count0;
1793 }
1794 }
1795
1796 /*
1797 * oap->inclusive defaults to TRUE.
1798 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1799 * FALSE. This makes "d}P" and "v}dP" work the same.
1800 */
1801 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1802 oap->inclusive = TRUE;
1803 if (VIsual_mode == 'V')
1804 oap->motion_type = MLINE;
1805 else
1806 {
1807 oap->motion_type = MCHAR;
1808 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1809# ifdef FEAT_VIRTUALEDIT
1810 && (include_line_break || !virtual_op)
1811# endif
1812 )
1813 {
1814 oap->inclusive = FALSE;
1815 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001816 * that works on lines only. */
1817 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001819 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1820 {
1821 ++oap->end.lnum;
1822 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001824 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001826 ++oap->line_count;
1827 }
1828 else
1829 {
1830 /* Cannot move below the last line, make the op
1831 * inclusive to tell the operation to include the
1832 * line break. */
1833 oap->inclusive = TRUE;
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 }
1836 }
1837 }
1838
1839 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /*
1842 * Switch Visual off now, so screen updating does
1843 * not show inverted text when the screen is redrawn.
1844 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1845 * no screen redraw, so it is done here to remove the inverted
1846 * part.
1847 */
1848 if (!gui_yank)
1849 {
1850 VIsual_active = FALSE;
1851# ifdef FEAT_MOUSE
1852 setmouse();
1853 mouse_dragging = 0;
1854# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001855 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 clear_cmdline = TRUE; /* unshow visual mode later */
1857#ifdef FEAT_CMDL_INFO
1858 else
1859 clear_showcmd();
1860#endif
1861 if ((oap->op_type == OP_YANK
1862 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001863 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 || oap->op_type == OP_FILTER)
1865 && oap->motion_force == NUL)
1866 redraw_curbuf_later(INVERTED);
1867 }
1868 }
1869#endif
1870
1871#ifdef FEAT_MBYTE
1872 /* Include the trailing byte of a multi-byte char. */
1873 if (has_mbyte && oap->inclusive)
1874 {
1875 int l;
1876
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001877 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (l > 1)
1879 oap->end.col += l - 1;
1880 }
1881#endif
1882 curwin->w_set_curswant = TRUE;
1883
1884 /*
1885 * oap->empty is set when start and end are the same. The inclusive
1886 * flag affects this too, unless yanking and the end is on a NUL.
1887 */
1888 oap->empty = (oap->motion_type == MCHAR
1889 && (!oap->inclusive
1890 || (oap->op_type == OP_YANK
1891 && gchar_pos(&oap->end) == NUL))
1892 && equalpos(oap->start, oap->end)
1893#ifdef FEAT_VIRTUALEDIT
1894 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1895#endif
1896 );
1897 /*
1898 * For delete, change and yank, it's an error to operate on an
1899 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1900 */
1901 empty_region_error = (oap->empty
1902 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1903
1904#ifdef FEAT_VISUAL
1905 /* Force a redraw when operating on an empty Visual region, when
1906 * 'modifiable is off or creating a fold. */
1907 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1908# ifdef FEAT_FOLDING
1909 || oap->op_type == OP_FOLD
1910# endif
1911 ))
1912 redraw_curbuf_later(INVERTED);
1913#endif
1914
1915 /*
1916 * If the end of an operator is in column one while oap->motion_type
1917 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1918 * character in the previous line. If op_start is on or before the
1919 * first non-blank in the line, the operator becomes linewise
1920 * (strange, but that's the way vi does it).
1921 */
1922 if ( oap->motion_type == MCHAR
1923 && oap->inclusive == FALSE
1924 && !(cap->retval & CA_NO_ADJ_OP_END)
1925 && oap->end.col == 0
1926#ifdef FEAT_VISUAL
1927 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001928 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929#endif
1930 && oap->line_count > 1)
1931 {
1932 oap->end_adjusted = TRUE; /* remember that we did this */
1933 --oap->line_count;
1934 --oap->end.lnum;
1935 if (inindent(0))
1936 oap->motion_type = MLINE;
1937 else
1938 {
1939 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1940 if (oap->end.col)
1941 {
1942 --oap->end.col;
1943 oap->inclusive = TRUE;
1944 }
1945 }
1946 }
1947 else
1948 oap->end_adjusted = FALSE;
1949
1950 switch (oap->op_type)
1951 {
1952 case OP_LSHIFT:
1953 case OP_RSHIFT:
1954 op_shift(oap, TRUE,
1955#ifdef FEAT_VISUAL
1956 oap->is_VIsual ? (int)cap->count1 :
1957#endif
1958 1);
1959 auto_format(FALSE, TRUE);
1960 break;
1961
1962 case OP_JOIN_NS:
1963 case OP_JOIN:
1964 if (oap->line_count < 2)
1965 oap->line_count = 2;
1966 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1967 curbuf->b_ml.ml_line_count)
1968 beep_flush();
1969 else
1970 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001971 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 auto_format(FALSE, TRUE);
1973 }
1974 break;
1975
1976 case OP_DELETE:
1977#ifdef FEAT_VISUAL
1978 VIsual_reselect = FALSE; /* don't reselect now */
1979#endif
1980 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001983 CancelRedo();
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 else
1986 {
1987 (void)op_delete(oap);
1988 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1989 u_save_cursor(); /* cursor line wasn't saved yet */
1990 auto_format(FALSE, TRUE);
1991 }
1992 break;
1993
1994 case OP_YANK:
1995 if (empty_region_error)
1996 {
1997 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002000 CancelRedo();
2001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 else
2004 (void)op_yank(oap, FALSE, !gui_yank);
2005 check_cursor_col();
2006 break;
2007
2008 case OP_CHANGE:
2009#ifdef FEAT_VISUAL
2010 VIsual_reselect = FALSE; /* don't reselect now */
2011#endif
2012 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002015 CancelRedo();
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 else
2018 {
2019 /* This is a new edit command, not a restart. Need to
2020 * remember it to make 'insertmode' work with mappings for
2021 * Visual mode. But do this only once and not when typed and
2022 * 'insertmode' isn't set. */
2023 if (p_im || !KeyTyped)
2024 restart_edit_save = restart_edit;
2025 else
2026 restart_edit_save = 0;
2027 restart_edit = 0;
2028 /* Reset finish_op now, don't want it set inside edit(). */
2029 finish_op = FALSE;
2030 if (op_change(oap)) /* will call edit() */
2031 cap->retval |= CA_COMMAND_BUSY;
2032 if (restart_edit == 0)
2033 restart_edit = restart_edit_save;
2034 }
2035 break;
2036
2037 case OP_FILTER:
2038 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2039 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2040 else
2041 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2042
2043 case OP_INDENT:
2044 case OP_COLON:
2045
2046#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2047 /*
2048 * If 'equalprg' is empty, do the indenting internally.
2049 */
2050 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2051 {
2052# ifdef FEAT_LISP
2053 if (curbuf->b_p_lisp)
2054 {
2055 op_reindent(oap, get_lisp_indent);
2056 break;
2057 }
2058# endif
2059# ifdef FEAT_CINDENT
2060 op_reindent(oap,
2061# ifdef FEAT_EVAL
2062 *curbuf->b_p_inde != NUL ? get_expr_indent :
2063# endif
2064 get_c_indent);
2065 break;
2066# endif
2067 }
2068#endif
2069
2070 op_colon(oap);
2071 break;
2072
2073 case OP_TILDE:
2074 case OP_UPPER:
2075 case OP_LOWER:
2076 case OP_ROT13:
2077 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002080 CancelRedo();
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 else
2083 op_tilde(oap);
2084 check_cursor_col();
2085 break;
2086
2087 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002088#if defined(FEAT_EVAL)
2089 if (*curbuf->b_p_fex != NUL)
2090 op_formatexpr(oap); /* use expression */
2091 else
2092#endif
2093 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 op_colon(oap); /* use external command */
2095 else
2096 op_format(oap, FALSE); /* use internal function */
2097 break;
2098
2099 case OP_FORMAT2:
2100 op_format(oap, TRUE); /* use internal function */
2101 break;
2102
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002103 case OP_FUNCTION:
2104 op_function(oap); /* call 'operatorfunc' */
2105 break;
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 case OP_INSERT:
2108 case OP_APPEND:
2109#ifdef FEAT_VISUAL
2110 VIsual_reselect = FALSE; /* don't reselect now */
2111#endif
2112#ifdef FEAT_VISUALEXTRA
2113 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002116 CancelRedo();
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119 {
2120 /* This is a new edit command, not a restart. Need to
2121 * remember it to make 'insertmode' work with mappings for
2122 * Visual mode. But do this only once. */
2123 restart_edit_save = restart_edit;
2124 restart_edit = 0;
2125
2126 op_insert(oap, cap->count1);
2127
2128 /* TODO: when inserting in several lines, should format all
2129 * the lines. */
2130 auto_format(FALSE, TRUE);
2131
2132 if (restart_edit == 0)
2133 restart_edit = restart_edit_save;
2134 }
2135#else
2136 vim_beep();
2137#endif
2138 break;
2139
2140 case OP_REPLACE:
2141#ifdef FEAT_VISUAL
2142 VIsual_reselect = FALSE; /* don't reselect now */
2143#endif
2144#ifdef FEAT_VISUALEXTRA
2145 if (empty_region_error)
2146#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002149 CancelRedo();
2150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#ifdef FEAT_VISUALEXTRA
2152 else
2153 op_replace(oap, cap->nchar);
2154#endif
2155 break;
2156
2157#ifdef FEAT_FOLDING
2158 case OP_FOLD:
2159 VIsual_reselect = FALSE; /* don't reselect now */
2160 foldCreate(oap->start.lnum, oap->end.lnum);
2161 break;
2162
2163 case OP_FOLDOPEN:
2164 case OP_FOLDOPENREC:
2165 case OP_FOLDCLOSE:
2166 case OP_FOLDCLOSEREC:
2167 VIsual_reselect = FALSE; /* don't reselect now */
2168 opFoldRange(oap->start.lnum, oap->end.lnum,
2169 oap->op_type == OP_FOLDOPEN
2170 || oap->op_type == OP_FOLDOPENREC,
2171 oap->op_type == OP_FOLDOPENREC
2172 || oap->op_type == OP_FOLDCLOSEREC,
2173 oap->is_VIsual);
2174 break;
2175
2176 case OP_FOLDDEL:
2177 case OP_FOLDDELREC:
2178 VIsual_reselect = FALSE; /* don't reselect now */
2179 deleteFold(oap->start.lnum, oap->end.lnum,
2180 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2181 break;
2182#endif
2183 default:
2184 clearopbeep(oap);
2185 }
2186#ifdef FEAT_VIRTUALEDIT
2187 virtual_op = MAYBE;
2188#endif
2189 if (!gui_yank)
2190 {
2191 /*
2192 * if 'sol' not set, go back to old column for some commands
2193 */
2194 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2195 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2196 || oap->op_type == OP_DELETE))
2197 coladvance(curwin->w_curswant = old_col);
2198 }
2199 else
2200 {
2201 curwin->w_cursor = old_cursor;
2202 }
2203#ifdef FEAT_VISUAL
2204 oap->block_mode = FALSE;
2205#endif
2206 clearop(oap);
2207 }
2208}
2209
2210/*
2211 * Handle indent and format operators and visual mode ":".
2212 */
2213 static void
2214op_colon(oap)
2215 oparg_T *oap;
2216{
2217 stuffcharReadbuff(':');
2218#ifdef FEAT_VISUAL
2219 if (oap->is_VIsual)
2220 stuffReadbuff((char_u *)"'<,'>");
2221 else
2222#endif
2223 {
2224 /*
2225 * Make the range look nice, so it can be repeated.
2226 */
2227 if (oap->start.lnum == curwin->w_cursor.lnum)
2228 stuffcharReadbuff('.');
2229 else
2230 stuffnumReadbuff((long)oap->start.lnum);
2231 if (oap->end.lnum != oap->start.lnum)
2232 {
2233 stuffcharReadbuff(',');
2234 if (oap->end.lnum == curwin->w_cursor.lnum)
2235 stuffcharReadbuff('.');
2236 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2237 stuffcharReadbuff('$');
2238 else if (oap->start.lnum == curwin->w_cursor.lnum)
2239 {
2240 stuffReadbuff((char_u *)".+");
2241 stuffnumReadbuff((long)oap->line_count - 1);
2242 }
2243 else
2244 stuffnumReadbuff((long)oap->end.lnum);
2245 }
2246 }
2247 if (oap->op_type != OP_COLON)
2248 stuffReadbuff((char_u *)"!");
2249 if (oap->op_type == OP_INDENT)
2250 {
2251#ifndef FEAT_CINDENT
2252 if (*get_equalprg() == NUL)
2253 stuffReadbuff((char_u *)"indent");
2254 else
2255#endif
2256 stuffReadbuff(get_equalprg());
2257 stuffReadbuff((char_u *)"\n");
2258 }
2259 else if (oap->op_type == OP_FORMAT)
2260 {
2261 if (*p_fp == NUL)
2262 stuffReadbuff((char_u *)"fmt");
2263 else
2264 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002265 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
2267
2268 /*
2269 * do_cmdline() does the rest
2270 */
2271}
2272
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002273/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002274 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002275 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002276 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002277op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002278 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002279{
2280#ifdef FEAT_EVAL
2281 char_u *(argv[1]);
2282
2283 if (*p_opfunc == NUL)
2284 EMSG(_("E774: 'operatorfunc' is empty"));
2285 else
2286 {
2287 /* Set '[ and '] marks to text to be operated on. */
2288 curbuf->b_op_start = oap->start;
2289 curbuf->b_op_end = oap->end;
2290 if (oap->motion_type != MLINE && !oap->inclusive)
2291 /* Exclude the end position. */
2292 decl(&curbuf->b_op_end);
2293
2294 if (oap->block_mode)
2295 argv[0] = (char_u *)"block";
2296 else if (oap->motion_type == MLINE)
2297 argv[0] = (char_u *)"line";
2298 else
2299 argv[0] = (char_u *)"char";
2300 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2301 }
2302#else
2303 EMSG(_("E775: Eval feature not available"));
2304#endif
2305}
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307#if defined(FEAT_MOUSE) || defined(PROTO)
2308/*
2309 * Do the appropriate action for the current mouse click in the current mode.
2310 * Not used for Command-line mode.
2311 *
2312 * Normal Mode:
2313 * event modi- position visual change action
2314 * fier cursor window
2315 * left press - yes end yes
2316 * left press C yes end yes "^]" (2)
2317 * left press S yes end yes "*" (2)
2318 * left drag - yes start if moved no
2319 * left relse - yes start if moved no
2320 * middle press - yes if not active no put register
2321 * middle press - yes if active no yank and put
2322 * right press - yes start or extend yes
2323 * right press S yes no change yes "#" (2)
2324 * right drag - yes extend no
2325 * right relse - yes extend no
2326 *
2327 * Insert or Replace Mode:
2328 * event modi- position visual change action
2329 * fier cursor window
2330 * left press - yes (cannot be active) yes
2331 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2332 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2333 * left drag - yes start or extend (1) no CTRL-O (1)
2334 * left relse - yes start or extend (1) no CTRL-O (1)
2335 * middle press - no (cannot be active) no put register
2336 * right press - yes start or extend yes CTRL-O
2337 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2338 *
2339 * (1) only if mouse pointer moved since press
2340 * (2) only if click is in same buffer
2341 *
2342 * Return TRUE if start_arrow() should be called for edit mode.
2343 */
2344 int
2345do_mouse(oap, c, dir, count, fixindent)
2346 oparg_T *oap; /* operator argument, can be NULL */
2347 int c; /* K_LEFTMOUSE, etc */
2348 int dir; /* Direction to 'put' if necessary */
2349 long count;
2350 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2351{
2352 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2353 static int got_click = FALSE; /* got a click some time back */
2354
2355 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2356 int is_click; /* If FALSE it's a drag or release event */
2357 int is_drag; /* If TRUE it's a drag event */
2358 int jump_flags = 0; /* flags for jump_to_mouse() */
2359 pos_T start_visual;
2360 int moved; /* Has cursor moved? */
2361 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002362#ifdef FEAT_WINDOWS
2363 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_VERTSPLIT
2366 int in_sep_line; /* mouse in vertical separator line */
2367#endif
2368 int c1, c2;
2369#if defined(FEAT_FOLDING)
2370 pos_T save_cursor;
2371#endif
2372 win_T *old_curwin = curwin;
2373#ifdef FEAT_VISUAL
2374 static pos_T orig_cursor;
2375 colnr_T leftcol, rightcol;
2376 pos_T end_visual;
2377 int diff;
2378 int old_active = VIsual_active;
2379 int old_mode = VIsual_mode;
2380#endif
2381 int regname;
2382
2383#if defined(FEAT_FOLDING)
2384 save_cursor = curwin->w_cursor;
2385#endif
2386
2387 /*
2388 * When GUI is active, always recognize mouse events, otherwise:
2389 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2390 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2391 * - For command line and insert mode 'mouse' is checked before calling
2392 * do_mouse().
2393 */
2394 if (do_always)
2395 do_always = FALSE;
2396 else
2397#ifdef FEAT_GUI
2398 if (!gui.in_use)
2399#endif
2400 {
2401#ifdef FEAT_VISUAL
2402 if (VIsual_active)
2403 {
2404 if (!mouse_has(MOUSE_VISUAL))
2405 return FALSE;
2406 }
2407 else
2408#endif
2409 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2410 return FALSE;
2411 }
2412
2413 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2414
2415#ifdef FEAT_MOUSESHAPE
2416 /* May have stopped dragging the status or separator line. The pointer is
2417 * most likely still on the status or separator line. */
2418 if (!is_drag && drag_status_line)
2419 {
2420 drag_status_line = FALSE;
2421 update_mouseshape(SHAPE_IDX_STATUS);
2422 }
2423# ifdef FEAT_VERTSPLIT
2424 if (!is_drag && drag_sep_line)
2425 {
2426 drag_sep_line = FALSE;
2427 update_mouseshape(SHAPE_IDX_VSEP);
2428 }
2429# endif
2430#endif
2431
2432 /*
2433 * Ignore drag and release events if we didn't get a click.
2434 */
2435 if (is_click)
2436 got_click = TRUE;
2437 else
2438 {
2439 if (!got_click) /* didn't get click, ignore */
2440 return FALSE;
2441 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002444#ifdef FEAT_WINDOWS
2445 if (in_tab_line)
2446 {
2447 in_tab_line = FALSE;
2448 return FALSE;
2449 }
2450#endif
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
Bram Moolenaar64969662005-12-14 21:59:55 +00002454#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002456 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
2458 if ((mod_mask & MOD_MASK_ALT))
2459 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 /*
2463 * CTRL right mouse button does CTRL-T
2464 */
2465 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2466 {
2467 if (State & INSERT)
2468 stuffcharReadbuff(Ctrl_O);
2469 if (count > 1)
2470 stuffnumReadbuff(count);
2471 stuffcharReadbuff(Ctrl_T);
2472 got_click = FALSE; /* ignore drag&release now */
2473 return FALSE;
2474 }
2475
2476 /*
2477 * CTRL only works with left mouse button
2478 */
2479 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2480 return FALSE;
2481
2482 /*
2483 * When a modifier is down, ignore drag and release events, as well as
2484 * multiple clicks and the middle mouse button.
2485 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2486 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002487 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2488 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 && (!is_click
2490 || (mod_mask & MOD_MASK_MULTI_CLICK)
2491 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002492 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 && mouse_model_popup()
2494 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002495 && !((mod_mask & MOD_MASK_ALT)
2496 && !mouse_model_popup()
2497 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 )
2499 return FALSE;
2500
2501 /*
2502 * If the button press was used as the movement command for an operator
2503 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2504 * drag/release events.
2505 */
2506 if (!is_click && which_button == MOUSE_MIDDLE)
2507 return FALSE;
2508
2509 if (oap != NULL)
2510 regname = oap->regname;
2511 else
2512 regname = 0;
2513
2514 /*
2515 * Middle mouse button does a 'put' of the selected text
2516 */
2517 if (which_button == MOUSE_MIDDLE)
2518 {
2519 if (State == NORMAL)
2520 {
2521 /*
2522 * If an operator was pending, we don't know what the user wanted
2523 * to do. Go back to normal mode: Clear the operator and beep().
2524 */
2525 if (oap != NULL && oap->op_type != OP_NOP)
2526 {
2527 clearopbeep(oap);
2528 return FALSE;
2529 }
2530
2531#ifdef FEAT_VISUAL
2532 /*
2533 * If visual was active, yank the highlighted text and put it
2534 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002535 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 */
2537 if (VIsual_active)
2538 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002539 if (VIsual_select)
2540 {
2541 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002542 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002543 }
2544 else
2545 {
2546 stuffcharReadbuff('y');
2547 stuffcharReadbuff(K_MIDDLEMOUSE);
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 do_always = TRUE; /* ignore 'mouse' setting next time */
2550 return FALSE;
2551 }
2552#endif
2553 /*
2554 * The rest is below jump_to_mouse()
2555 */
2556 }
2557
2558 else if ((State & INSERT) == 0)
2559 return FALSE;
2560
2561 /*
2562 * Middle click in insert mode doesn't move the mouse, just insert the
2563 * contents of a register. '.' register is special, can't insert that
2564 * with do_put().
2565 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2566 * happens for the GUI).
2567 */
2568 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2569 {
2570 if (regname == '.')
2571 insert_reg(regname, TRUE);
2572 else
2573 {
2574#ifdef FEAT_CLIPBOARD
2575 if (clip_star.available && regname == 0)
2576 regname = '*';
2577#endif
2578 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2579 insert_reg(regname, TRUE);
2580 else
2581 {
2582 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2583
2584 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2585 AppendCharToRedobuff(Ctrl_R);
2586 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2587 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2588 }
2589 }
2590 return FALSE;
2591 }
2592 }
2593
2594 /* When dragging or button-up stay in the same window. */
2595 if (!is_click)
2596 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2597
2598 start_visual.lnum = 0;
2599
Bram Moolenaarf740b292006-02-16 22:11:02 +00002600#ifdef FEAT_WINDOWS
2601 /* Check for clicking in the tab page line. */
2602 if (mouse_row == 0 && firstwin->w_winrow > 0)
2603 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002604 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002605 {
2606 if (in_tab_line)
2607 {
2608 c1 = TabPageIdxs[mouse_col];
2609 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2610 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002611 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002612 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002613
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614 /* click in a tab selects that tab page */
2615 if (is_click
2616# ifdef FEAT_CMDWIN
2617 && cmdwin_type == 0
2618# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002620 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002621 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622 c1 = TabPageIdxs[mouse_col];
2623 if (c1 >= 0)
2624 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002625 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2626 {
2627 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002628 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002629 tabpage_new();
2630 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2631 }
2632 else
2633 {
2634 /* Go to specified tab page, or next one if not clicking
2635 * on a label. */
2636 goto_tabpage(c1);
2637
2638 /* It's like clicking on the status line of a window. */
2639 if (curwin != old_curwin)
2640 end_visual_mode();
2641 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002642 }
2643 else if (c1 < 0)
2644 {
2645 tabpage_T *tp;
2646
2647 /* Close the current or specified tab page. */
2648 if (c1 == -999)
2649 tp = curtab;
2650 else
2651 tp = find_tabpage(-c1);
2652 if (tp == curtab)
2653 {
2654 if (first_tabpage->tp_next != NULL)
2655 tabpage_close(FALSE);
2656 }
2657 else if (tp != NULL)
2658 tabpage_close_other(tp, FALSE);
2659 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002660 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002661 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002662 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002663 else if (is_drag && in_tab_line)
2664 {
2665 c1 = TabPageIdxs[mouse_col];
2666 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2667 return FALSE;
2668 }
2669
Bram Moolenaarf740b292006-02-16 22:11:02 +00002670#endif
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /*
2673 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2674 * right button up -> pop-up menu
2675 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002676 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
2678 if (mouse_model_popup())
2679 {
2680 if (which_button == MOUSE_RIGHT
2681 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2682 {
2683 /*
2684 * NOTE: Ignore right button down and drag mouse events.
2685 * Windows only shows the popup menu on the button up event.
2686 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002687#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2688 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (!is_click)
2690 return FALSE;
2691#endif
2692#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2693 if (is_click || is_drag)
2694 return FALSE;
2695#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002696#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2698 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2699 if (gui.in_use)
2700 {
2701 jump_flags = 0;
2702 if (STRCMP(p_mousem, "popup_setpos") == 0)
2703 {
2704 /* First set the cursor position before showing the popup
2705 * menu. */
2706#ifdef FEAT_VISUAL
2707 if (VIsual_active)
2708 {
2709 pos_T m_pos;
2710
2711 /*
2712 * set MOUSE_MAY_STOP_VIS if we are outside the
2713 * selection or the current window (might have false
2714 * negative here)
2715 */
2716 if (mouse_row < W_WINROW(curwin)
2717 || mouse_row
2718 > (W_WINROW(curwin) + curwin->w_height))
2719 jump_flags = MOUSE_MAY_STOP_VIS;
2720 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2721 jump_flags = MOUSE_MAY_STOP_VIS;
2722 else
2723 {
2724 if ((lt(curwin->w_cursor, VIsual)
2725 && (lt(m_pos, curwin->w_cursor)
2726 || lt(VIsual, m_pos)))
2727 || (lt(VIsual, curwin->w_cursor)
2728 && (lt(m_pos, VIsual)
2729 || lt(curwin->w_cursor, m_pos))))
2730 {
2731 jump_flags = MOUSE_MAY_STOP_VIS;
2732 }
2733 else if (VIsual_mode == Ctrl_V)
2734 {
2735 getvcols(curwin, &curwin->w_cursor, &VIsual,
2736 &leftcol, &rightcol);
2737 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2738 if (m_pos.col < leftcol || m_pos.col > rightcol)
2739 jump_flags = MOUSE_MAY_STOP_VIS;
2740 }
2741 }
2742 }
2743 else
2744 jump_flags = MOUSE_MAY_STOP_VIS;
2745#endif
2746 }
2747 if (jump_flags)
2748 {
2749 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2750 update_curbuf(
2751#ifdef FEAT_VISUAL
2752 VIsual_active ? INVERTED :
2753#endif
2754 VALID);
2755 setcursor();
2756 out_flush(); /* Update before showing popup menu */
2757 }
2758# ifdef FEAT_MENU
2759 gui_show_popupmenu();
2760# endif
2761 return (jump_flags & CURSOR_MOVED) != 0;
2762 }
2763 else
2764 return FALSE;
2765#else
2766 return FALSE;
2767#endif
2768 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002769 if (which_button == MOUSE_LEFT
2770 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 which_button = MOUSE_RIGHT;
2773 mod_mask &= ~MOD_MASK_SHIFT;
2774 }
2775 }
2776
2777#ifdef FEAT_VISUAL
2778 if ((State & (NORMAL | INSERT))
2779 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2780 {
2781 if (which_button == MOUSE_LEFT)
2782 {
2783 if (is_click)
2784 {
2785 /* stop Visual mode for a left click in a window, but not when
2786 * on a status line */
2787 if (VIsual_active)
2788 jump_flags |= MOUSE_MAY_STOP_VIS;
2789 }
2790 else if (mouse_has(MOUSE_VISUAL))
2791 jump_flags |= MOUSE_MAY_VIS;
2792 }
2793 else if (which_button == MOUSE_RIGHT)
2794 {
2795 if (is_click && VIsual_active)
2796 {
2797 /*
2798 * Remember the start and end of visual before moving the
2799 * cursor.
2800 */
2801 if (lt(curwin->w_cursor, VIsual))
2802 {
2803 start_visual = curwin->w_cursor;
2804 end_visual = VIsual;
2805 }
2806 else
2807 {
2808 start_visual = VIsual;
2809 end_visual = curwin->w_cursor;
2810 }
2811 }
2812 jump_flags |= MOUSE_FOCUS;
2813 if (mouse_has(MOUSE_VISUAL))
2814 jump_flags |= MOUSE_MAY_VIS;
2815 }
2816 }
2817#endif
2818
2819 /*
2820 * If an operator is pending, ignore all drags and releases until the
2821 * next mouse click.
2822 */
2823 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2824 {
2825 got_click = FALSE;
2826 oap->motion_type = MCHAR;
2827 }
2828
2829 /* When releasing the button let jump_to_mouse() know. */
2830 if (!is_click && !is_drag)
2831 jump_flags |= MOUSE_RELEASED;
2832
2833 /*
2834 * JUMP!
2835 */
2836 jump_flags = jump_to_mouse(jump_flags,
2837 oap == NULL ? NULL : &(oap->inclusive), which_button);
2838 moved = (jump_flags & CURSOR_MOVED);
2839 in_status_line = (jump_flags & IN_STATUS_LINE);
2840#ifdef FEAT_VERTSPLIT
2841 in_sep_line = (jump_flags & IN_SEP_LINE);
2842#endif
2843
2844#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002845 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2847 {
2848 int key = KEY2TERMCAP1(c);
2849
2850 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2851 || key == (int)KE_RIGHTRELEASE)
2852 netbeans_button_release(which_button);
2853 }
2854#endif
2855
2856 /* When jumping to another window, clear a pending operator. That's a bit
2857 * friendlier than beeping and not jumping to that window. */
2858 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2859 clearop(oap);
2860
2861#ifdef FEAT_FOLDING
2862 if (mod_mask == 0
2863 && !is_drag
2864 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2865 && which_button == MOUSE_LEFT)
2866 {
2867 /* open or close a fold at this line */
2868 if (jump_flags & MOUSE_FOLD_OPEN)
2869 openFold(curwin->w_cursor.lnum, 1L);
2870 else
2871 closeFold(curwin->w_cursor.lnum, 1L);
2872 /* don't move the cursor if still in the same window */
2873 if (curwin == old_curwin)
2874 curwin->w_cursor = save_cursor;
2875 }
2876#endif
2877
2878#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2879 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2880 {
2881 clip_modeless(which_button, is_click, is_drag);
2882 return FALSE;
2883 }
2884#endif
2885
2886#ifdef FEAT_VISUAL
2887 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002888 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (VIsual_active && is_drag && p_so)
2890 {
2891 /* In the very first line, allow scrolling one line */
2892 if (mouse_row == 0)
2893 mouse_dragging = 2;
2894 else
2895 mouse_dragging = 1;
2896 }
2897
2898 /* When dragging the mouse above the window, scroll down. */
2899 if (is_drag && mouse_row < 0 && !in_status_line)
2900 {
2901 scroll_redraw(FALSE, 1L);
2902 mouse_row = 0;
2903 }
2904
2905 if (start_visual.lnum) /* right click in visual mode */
2906 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002907 /* When ALT is pressed make Visual mode blockwise. */
2908 if (mod_mask & MOD_MASK_ALT)
2909 VIsual_mode = Ctrl_V;
2910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 /*
2912 * In Visual-block mode, divide the area in four, pick up the corner
2913 * that is in the quarter that the cursor is in.
2914 */
2915 if (VIsual_mode == Ctrl_V)
2916 {
2917 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2918 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2919 end_visual.col = leftcol;
2920 else
2921 end_visual.col = rightcol;
2922 if (curwin->w_cursor.lnum <
2923 (start_visual.lnum + end_visual.lnum) / 2)
2924 end_visual.lnum = end_visual.lnum;
2925 else
2926 end_visual.lnum = start_visual.lnum;
2927
2928 /* move VIsual to the right column */
2929 start_visual = curwin->w_cursor; /* save the cursor pos */
2930 curwin->w_cursor = end_visual;
2931 coladvance(end_visual.col);
2932 VIsual = curwin->w_cursor;
2933 curwin->w_cursor = start_visual; /* restore the cursor */
2934 }
2935 else
2936 {
2937 /*
2938 * If the click is before the start of visual, change the start.
2939 * If the click is after the end of visual, change the end. If
2940 * the click is inside the visual, change the closest side.
2941 */
2942 if (lt(curwin->w_cursor, start_visual))
2943 VIsual = end_visual;
2944 else if (lt(end_visual, curwin->w_cursor))
2945 VIsual = start_visual;
2946 else
2947 {
2948 /* In the same line, compare column number */
2949 if (end_visual.lnum == start_visual.lnum)
2950 {
2951 if (curwin->w_cursor.col - start_visual.col >
2952 end_visual.col - curwin->w_cursor.col)
2953 VIsual = start_visual;
2954 else
2955 VIsual = end_visual;
2956 }
2957
2958 /* In different lines, compare line number */
2959 else
2960 {
2961 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2962 (end_visual.lnum - curwin->w_cursor.lnum);
2963
2964 if (diff > 0) /* closest to end */
2965 VIsual = start_visual;
2966 else if (diff < 0) /* closest to start */
2967 VIsual = end_visual;
2968 else /* in the middle line */
2969 {
2970 if (curwin->w_cursor.col <
2971 (start_visual.col + end_visual.col) / 2)
2972 VIsual = end_visual;
2973 else
2974 VIsual = start_visual;
2975 }
2976 }
2977 }
2978 }
2979 }
2980 /*
2981 * If Visual mode started in insert mode, execute "CTRL-O"
2982 */
2983 else if ((State & INSERT) && VIsual_active)
2984 stuffcharReadbuff(Ctrl_O);
2985#endif
2986
2987 /*
2988 * Middle mouse click: Put text before cursor.
2989 */
2990 if (which_button == MOUSE_MIDDLE)
2991 {
2992#ifdef FEAT_CLIPBOARD
2993 if (clip_star.available && regname == 0)
2994 regname = '*';
2995#endif
2996 if (yank_register_mline(regname))
2997 {
2998 if (mouse_past_bottom)
2999 dir = FORWARD;
3000 }
3001 else if (mouse_past_eol)
3002 dir = FORWARD;
3003
3004 if (fixindent)
3005 {
3006 c1 = (dir == BACKWARD) ? '[' : ']';
3007 c2 = 'p';
3008 }
3009 else
3010 {
3011 c1 = (dir == FORWARD) ? 'p' : 'P';
3012 c2 = NUL;
3013 }
3014 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3015
3016 /*
3017 * Remember where the paste started, so in edit() Insstart can be set
3018 * to this position
3019 */
3020 if (restart_edit != 0)
3021 where_paste_started = curwin->w_cursor;
3022 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3023 }
3024
3025#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3026 /*
3027 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3028 * error under the mouse pointer.
3029 */
3030 else if (((mod_mask & MOD_MASK_CTRL)
3031 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3032 && bt_quickfix(curbuf))
3033 {
3034 if (State & INSERT)
3035 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003036 if (curwin->w_llist_ref == NULL) /* quickfix window */
3037 stuffReadbuff((char_u *)":.cc\n");
3038 else /* location list window */
3039 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 got_click = FALSE; /* ignore drag&release now */
3041 }
3042#endif
3043
3044 /*
3045 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3046 * under the mouse pointer.
3047 */
3048 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3049 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3050 {
3051 if (State & INSERT)
3052 stuffcharReadbuff(Ctrl_O);
3053 stuffcharReadbuff(Ctrl_RSB);
3054 got_click = FALSE; /* ignore drag&release now */
3055 }
3056
3057 /*
3058 * Shift-Mouse click searches for the next occurrence of the word under
3059 * the mouse pointer
3060 */
3061 else if ((mod_mask & MOD_MASK_SHIFT))
3062 {
3063 if (State & INSERT
3064#ifdef FEAT_VISUAL
3065 || (VIsual_active && VIsual_select)
3066#endif
3067 )
3068 stuffcharReadbuff(Ctrl_O);
3069 if (which_button == MOUSE_LEFT)
3070 stuffcharReadbuff('*');
3071 else /* MOUSE_RIGHT */
3072 stuffcharReadbuff('#');
3073 }
3074
3075 /* Handle double clicks, unless on status line */
3076 else if (in_status_line)
3077 {
3078#ifdef FEAT_MOUSESHAPE
3079 if ((is_drag || is_click) && !drag_status_line)
3080 {
3081 drag_status_line = TRUE;
3082 update_mouseshape(-1);
3083 }
3084#endif
3085 }
3086#ifdef FEAT_VERTSPLIT
3087 else if (in_sep_line)
3088 {
3089# ifdef FEAT_MOUSESHAPE
3090 if ((is_drag || is_click) && !drag_sep_line)
3091 {
3092 drag_sep_line = TRUE;
3093 update_mouseshape(-1);
3094 }
3095# endif
3096 }
3097#endif
3098#ifdef FEAT_VISUAL
3099 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3100 && mouse_has(MOUSE_VISUAL))
3101 {
3102 if (is_click || !VIsual_active)
3103 {
3104 if (VIsual_active)
3105 orig_cursor = VIsual;
3106 else
3107 {
3108 check_visual_highlight();
3109 VIsual = curwin->w_cursor;
3110 orig_cursor = VIsual;
3111 VIsual_active = TRUE;
3112 VIsual_reselect = TRUE;
3113 /* start Select mode if 'selectmode' contains "mouse" */
3114 may_start_select('o');
3115 setmouse();
3116 }
3117 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003118 {
3119 /* Double click with ALT pressed makes it blockwise. */
3120 if (mod_mask & MOD_MASK_ALT)
3121 VIsual_mode = Ctrl_V;
3122 else
3123 VIsual_mode = 'v';
3124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3126 VIsual_mode = 'V';
3127 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3128 VIsual_mode = Ctrl_V;
3129#ifdef FEAT_CLIPBOARD
3130 /* Make sure the clipboard gets updated. Needed because start and
3131 * end may still be the same, and the selection needs to be owned */
3132 clip_star.vmode = NUL;
3133#endif
3134 }
3135 /*
3136 * A double click selects a word or a block.
3137 */
3138 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3139 {
3140 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003141 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142
3143 if (is_click)
3144 {
3145 /* If the character under the cursor (skipping white space) is
3146 * not a word character, try finding a match and select a (),
3147 * {}, [], #if/#endif, etc. block. */
3148 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003149 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 inc(&end_visual);
3151 if (oap != NULL)
3152 oap->motion_type = MCHAR;
3153 if (oap != NULL
3154 && VIsual_mode == 'v'
3155 && !vim_iswordc(gchar_pos(&end_visual))
3156 && equalpos(curwin->w_cursor, VIsual)
3157 && (pos = findmatch(oap, NUL)) != NULL)
3158 {
3159 curwin->w_cursor = *pos;
3160 if (oap->motion_type == MLINE)
3161 VIsual_mode = 'V';
3162 else if (*p_sel == 'e')
3163 {
3164 if (lt(curwin->w_cursor, VIsual))
3165 ++VIsual.col;
3166 else
3167 ++curwin->w_cursor.col;
3168 }
3169 }
3170 }
3171
3172 if (pos == NULL && (is_click || is_drag))
3173 {
3174 /* When not found a match or when dragging: extend to include
3175 * a word. */
3176 if (lt(curwin->w_cursor, orig_cursor))
3177 {
3178 find_start_of_word(&curwin->w_cursor);
3179 find_end_of_word(&VIsual);
3180 }
3181 else
3182 {
3183 find_start_of_word(&VIsual);
3184 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3185#ifdef FEAT_MBYTE
3186 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003187 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#else
3189 ++curwin->w_cursor.col;
3190#endif
3191 find_end_of_word(&curwin->w_cursor);
3192 }
3193 }
3194 curwin->w_set_curswant = TRUE;
3195 }
3196 if (is_click)
3197 redraw_curbuf_later(INVERTED); /* update the inversion */
3198 }
3199 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003200 {
3201 if (mod_mask & MOD_MASK_ALT)
3202 VIsual_mode = Ctrl_V;
3203 else
3204 VIsual_mode = 'v';
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003208 if ((!VIsual_active && old_active && mode_displayed)
3209 || (VIsual_active && p_smd && msg_silent == 0
3210 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 redraw_cmdline = TRUE;
3212#endif
3213
3214 return moved;
3215}
3216
3217#ifdef FEAT_VISUAL
3218/*
3219 * Move "pos" back to the start of the word it's in.
3220 */
3221 static void
3222find_start_of_word(pos)
3223 pos_T *pos;
3224{
3225 char_u *line;
3226 int cclass;
3227 int col;
3228
3229 line = ml_get(pos->lnum);
3230 cclass = get_mouse_class(line + pos->col);
3231
3232 while (pos->col > 0)
3233 {
3234 col = pos->col - 1;
3235#ifdef FEAT_MBYTE
3236 col -= (*mb_head_off)(line, line + col);
3237#endif
3238 if (get_mouse_class(line + col) != cclass)
3239 break;
3240 pos->col = col;
3241 }
3242}
3243
3244/*
3245 * Move "pos" forward to the end of the word it's in.
3246 * When 'selection' is "exclusive", the position is just after the word.
3247 */
3248 static void
3249find_end_of_word(pos)
3250 pos_T *pos;
3251{
3252 char_u *line;
3253 int cclass;
3254 int col;
3255
3256 line = ml_get(pos->lnum);
3257 if (*p_sel == 'e' && pos->col > 0)
3258 {
3259 --pos->col;
3260#ifdef FEAT_MBYTE
3261 pos->col -= (*mb_head_off)(line, line + pos->col);
3262#endif
3263 }
3264 cclass = get_mouse_class(line + pos->col);
3265 while (line[pos->col] != NUL)
3266 {
3267#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003268 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#else
3270 col = pos->col + 1;
3271#endif
3272 if (get_mouse_class(line + col) != cclass)
3273 {
3274 if (*p_sel == 'e')
3275 pos->col = col;
3276 break;
3277 }
3278 pos->col = col;
3279 }
3280}
3281
3282/*
3283 * Get class of a character for selection: same class means same word.
3284 * 0: blank
3285 * 1: punctuation groups
3286 * 2: normal word character
3287 * >2: multi-byte word character.
3288 */
3289 static int
3290get_mouse_class(p)
3291 char_u *p;
3292{
3293 int c;
3294
3295#ifdef FEAT_MBYTE
3296 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3297 return mb_get_class(p);
3298#endif
3299
3300 c = *p;
3301 if (c == ' ' || c == '\t')
3302 return 0;
3303
3304 if (vim_iswordc(c))
3305 return 2;
3306
3307 /*
3308 * There are a few special cases where we want certain combinations of
3309 * characters to be considered as a single word. These are things like
3310 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003311 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 */
3313 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3314 return 1;
3315 return c;
3316}
3317#endif /* FEAT_VISUAL */
3318#endif /* FEAT_MOUSE */
3319
3320#if defined(FEAT_VISUAL) || defined(PROTO)
3321/*
3322 * Check if highlighting for visual mode is possible, give a warning message
3323 * if not.
3324 */
3325 void
3326check_visual_highlight()
3327{
3328 static int did_check = FALSE;
3329
3330 if (full_screen)
3331 {
3332 if (!did_check && hl_attr(HLF_V) == 0)
3333 MSG(_("Warning: terminal cannot highlight"));
3334 did_check = TRUE;
3335 }
3336}
3337
3338/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003339 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * This function should ALWAYS be called to end Visual mode, except from
3341 * do_pending_operator().
3342 */
3343 void
3344end_visual_mode()
3345{
3346#ifdef FEAT_CLIPBOARD
3347 /*
3348 * If we are using the clipboard, then remember what was selected in case
3349 * we need to paste it somewhere while we still own the selection.
3350 * Only do this when the clipboard is already owned. Don't want to grab
3351 * the selection when hitting ESC.
3352 */
3353 if (clip_star.available && clip_star.owned)
3354 clip_auto_select();
3355#endif
3356
3357 VIsual_active = FALSE;
3358#ifdef FEAT_MOUSE
3359 setmouse();
3360 mouse_dragging = 0;
3361#endif
3362
3363 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003364 curbuf->b_visual.vi_mode = VIsual_mode;
3365 curbuf->b_visual.vi_start = VIsual;
3366 curbuf->b_visual.vi_end = curwin->w_cursor;
3367 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#ifdef FEAT_EVAL
3369 curbuf->b_visual_mode_eval = VIsual_mode;
3370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_VIRTUALEDIT
3372 if (!virtual_active())
3373 curwin->w_cursor.coladd = 0;
3374#endif
3375
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003376 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 clear_cmdline = TRUE; /* unshow visual mode later */
3378#ifdef FEAT_CMDL_INFO
3379 else
3380 clear_showcmd();
3381#endif
3382
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003383 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
3385
3386/*
3387 * Reset VIsual_active and VIsual_reselect.
3388 */
3389 void
3390reset_VIsual_and_resel()
3391{
3392 if (VIsual_active)
3393 {
3394 end_visual_mode();
3395 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3396 }
3397 VIsual_reselect = FALSE;
3398}
3399
3400/*
3401 * Reset VIsual_active and VIsual_reselect if it's set.
3402 */
3403 void
3404reset_VIsual()
3405{
3406 if (VIsual_active)
3407 {
3408 end_visual_mode();
3409 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3410 VIsual_reselect = FALSE;
3411 }
3412}
3413#endif /* FEAT_VISUAL */
3414
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3417
3418/*
3419 * Check for a balloon-eval special item to include when searching for an
3420 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3421 * Returns TRUE if the character at "*ptr" should be included.
3422 * "dir" is FORWARD or BACKWARD, the direction of searching.
3423 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3424 * "bnp" points to a counter for square brackets.
3425 */
3426 static int
3427find_is_eval_item(ptr, colp, bnp, dir)
3428 char_u *ptr;
3429 int *colp;
3430 int *bnp;
3431 int dir;
3432{
3433 /* Accept everything inside []. */
3434 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3435 ++*bnp;
3436 if (*bnp > 0)
3437 {
3438 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3439 --*bnp;
3440 return TRUE;
3441 }
3442
3443 /* skip over "s.var" */
3444 if (*ptr == '.')
3445 return TRUE;
3446
3447 /* two-character item: s->var */
3448 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3449 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3450 {
3451 *colp += dir;
3452 return TRUE;
3453 }
3454 return FALSE;
3455}
3456#endif
3457
3458/*
3459 * Find the identifier under or to the right of the cursor.
3460 * "find_type" can have one of three values:
3461 * FIND_IDENT: find an identifier (keyword)
3462 * FIND_STRING: find any non-white string
3463 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 *
3466 * There are three steps:
3467 * 1. Search forward for the start of an identifier/string. Doesn't move if
3468 * already on one.
3469 * 2. Search backward for the start of this identifier/string.
3470 * This doesn't match the real Vi but I like it a little better and it
3471 * shouldn't bother anyone.
3472 * 3. Search forward to the end of this identifier/string.
3473 * When FIND_IDENT isn't defined, we backup until a blank.
3474 *
3475 * Returns the length of the string, or zero if no string is found.
3476 * If a string is found, a pointer to the string is put in "*string". This
3477 * string is not always NUL terminated.
3478 */
3479 int
3480find_ident_under_cursor(string, find_type)
3481 char_u **string;
3482 int find_type;
3483{
3484 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3485 curwin->w_cursor.col, string, find_type);
3486}
3487
3488/*
3489 * Like find_ident_under_cursor(), but for any window and any position.
3490 * However: Uses 'iskeyword' from the current window!.
3491 */
3492 int
3493find_ident_at_pos(wp, lnum, startcol, string, find_type)
3494 win_T *wp;
3495 linenr_T lnum;
3496 colnr_T startcol;
3497 char_u **string;
3498 int find_type;
3499{
3500 char_u *ptr;
3501 int col = 0; /* init to shut up GCC */
3502 int i;
3503#ifdef FEAT_MBYTE
3504 int this_class = 0;
3505 int prev_class;
3506 int prevcol;
3507#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003508#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 int bn = 0; /* bracket nesting */
3510#endif
3511
3512 /*
3513 * if i == 0: try to find an identifier
3514 * if i == 1: try to find any non-white string
3515 */
3516 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3517 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3518 {
3519 /*
3520 * 1. skip to start of identifier/string
3521 */
3522 col = startcol;
3523#ifdef FEAT_MBYTE
3524 if (has_mbyte)
3525 {
3526 while (ptr[col] != NUL)
3527 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003528# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 /* Stop at a ']' to evaluate "a[x]". */
3530 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3531 break;
3532# endif
3533 this_class = mb_get_class(ptr + col);
3534 if (this_class != 0 && (i == 1 || this_class != 1))
3535 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003536 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 }
3539 else
3540#endif
3541 while (ptr[col] != NUL
3542 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3545# endif
3546 )
3547 ++col;
3548
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* When starting on a ']' count it, so that we include the '['. */
3551 bn = ptr[col] == ']';
3552#endif
3553
3554 /*
3555 * 2. Back up to start of identifier/string.
3556 */
3557#ifdef FEAT_MBYTE
3558 if (has_mbyte)
3559 {
3560 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3563 this_class = mb_get_class((char_u *)"a");
3564 else
3565# endif
3566 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3570 prev_class = mb_get_class(ptr + prevcol);
3571 if (this_class != prev_class
3572 && (i == 0
3573 || prev_class == 0
3574 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003575# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 && (!(find_type & FIND_EVAL)
3577 || prevcol == 0
3578 || !find_is_eval_item(ptr + prevcol, &prevcol,
3579 &bn, BACKWARD))
3580# endif
3581 )
3582 break;
3583 col = prevcol;
3584 }
3585
3586 /* If we don't want just any old string, or we've found an
3587 * identifier, stop searching. */
3588 if (this_class > 2)
3589 this_class = 2;
3590 if (!(find_type & FIND_STRING) || this_class == 2)
3591 break;
3592 }
3593 else
3594#endif
3595 {
3596 while (col > 0
3597 && ((i == 0
3598 ? vim_iswordc(ptr[col - 1])
3599 : (!vim_iswhite(ptr[col - 1])
3600 && (!(find_type & FIND_IDENT)
3601 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003602#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 || ((find_type & FIND_EVAL)
3604 && col > 1
3605 && find_is_eval_item(ptr + col - 1, &col,
3606 &bn, BACKWARD))
3607#endif
3608 ))
3609 --col;
3610
3611 /* If we don't want just any old string, or we've found an
3612 * identifier, stop searching. */
3613 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3614 break;
3615 }
3616 }
3617
3618 if (ptr[col] == NUL || (i == 0 && (
3619#ifdef FEAT_MBYTE
3620 has_mbyte ? this_class != 2 :
3621#endif
3622 !vim_iswordc(ptr[col]))))
3623 {
3624 /*
3625 * didn't find an identifier or string
3626 */
3627 if (find_type & FIND_STRING)
3628 EMSG(_("E348: No string under cursor"));
3629 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003630 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return 0;
3632 }
3633 ptr += col;
3634 *string = ptr;
3635
3636 /*
3637 * 3. Find the end if the identifier/string.
3638 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003639#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 bn = 0;
3641 startcol -= col;
3642#endif
3643 col = 0;
3644#ifdef FEAT_MBYTE
3645 if (has_mbyte)
3646 {
3647 /* Search for point of changing multibyte character class. */
3648 this_class = mb_get_class(ptr);
3649 while (ptr[col] != NUL
3650 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3651 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003652# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 || ((find_type & FIND_EVAL)
3654 && col <= (int)startcol
3655 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3656# endif
3657 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003658 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660 else
3661#endif
3662 while ((i == 0 ? vim_iswordc(ptr[col])
3663 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003664# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 || ((find_type & FIND_EVAL)
3666 && col <= (int)startcol
3667 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3668# endif
3669 )
3670 {
3671 ++col;
3672 }
3673
3674 return col;
3675}
3676
3677/*
3678 * Prepare for redo of a normal command.
3679 */
3680 static void
3681prep_redo_cmd(cap)
3682 cmdarg_T *cap;
3683{
3684 prep_redo(cap->oap->regname, cap->count0,
3685 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3686}
3687
3688/*
3689 * Prepare for redo of any command.
3690 * Note that only the last argument can be a multi-byte char.
3691 */
3692 static void
3693prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3694 int regname;
3695 long num;
3696 int cmd1;
3697 int cmd2;
3698 int cmd3;
3699 int cmd4;
3700 int cmd5;
3701{
3702 ResetRedobuff();
3703 if (regname != 0) /* yank from specified buffer */
3704 {
3705 AppendCharToRedobuff('"');
3706 AppendCharToRedobuff(regname);
3707 }
3708 if (num)
3709 AppendNumberToRedobuff(num);
3710
3711 if (cmd1 != NUL)
3712 AppendCharToRedobuff(cmd1);
3713 if (cmd2 != NUL)
3714 AppendCharToRedobuff(cmd2);
3715 if (cmd3 != NUL)
3716 AppendCharToRedobuff(cmd3);
3717 if (cmd4 != NUL)
3718 AppendCharToRedobuff(cmd4);
3719 if (cmd5 != NUL)
3720 AppendCharToRedobuff(cmd5);
3721}
3722
3723/*
3724 * check for operator active and clear it
3725 *
3726 * return TRUE if operator was active
3727 */
3728 static int
3729checkclearop(oap)
3730 oparg_T *oap;
3731{
3732 if (oap->op_type == OP_NOP)
3733 return FALSE;
3734 clearopbeep(oap);
3735 return TRUE;
3736}
3737
3738/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003739 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003741 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 */
3743 static int
3744checkclearopq(oap)
3745 oparg_T *oap;
3746{
3747 if (oap->op_type == OP_NOP
3748#ifdef FEAT_VISUAL
3749 && !VIsual_active
3750#endif
3751 )
3752 return FALSE;
3753 clearopbeep(oap);
3754 return TRUE;
3755}
3756
3757 static void
3758clearop(oap)
3759 oparg_T *oap;
3760{
3761 oap->op_type = OP_NOP;
3762 oap->regname = 0;
3763 oap->motion_force = NUL;
3764 oap->use_reg_one = FALSE;
3765}
3766
3767 static void
3768clearopbeep(oap)
3769 oparg_T *oap;
3770{
3771 clearop(oap);
3772 beep_flush();
3773}
3774
3775#ifdef FEAT_VISUAL
3776/*
3777 * Remove the shift modifier from a special key.
3778 */
3779 static void
3780unshift_special(cap)
3781 cmdarg_T *cap;
3782{
3783 switch (cap->cmdchar)
3784 {
3785 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3786 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3787 case K_S_UP: cap->cmdchar = K_UP; break;
3788 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3789 case K_S_HOME: cap->cmdchar = K_HOME; break;
3790 case K_S_END: cap->cmdchar = K_END; break;
3791 }
3792 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3793}
3794#endif
3795
3796#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3797/*
3798 * Routines for displaying a partly typed command
3799 */
3800
3801#ifdef FEAT_VISUAL /* need room for size of Visual area */
3802# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3803#else
3804# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3805#endif
3806static char_u showcmd_buf[SHOWCMD_BUFLEN];
3807static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3808static int showcmd_is_clear = TRUE;
3809static int showcmd_visual = FALSE;
3810
3811static void display_showcmd __ARGS((void));
3812
3813 void
3814clear_showcmd()
3815{
3816 if (!p_sc)
3817 return;
3818
3819#ifdef FEAT_VISUAL
3820 if (VIsual_active && !char_avail())
3821 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003822 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 long lines;
3824 colnr_T leftcol, rightcol;
3825 linenr_T top, bot;
3826
3827 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003828 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
3830 top = VIsual.lnum;
3831 bot = curwin->w_cursor.lnum;
3832 }
3833 else
3834 {
3835 top = curwin->w_cursor.lnum;
3836 bot = VIsual.lnum;
3837 }
3838# ifdef FEAT_FOLDING
3839 /* Include closed folds as a whole. */
3840 hasFolding(top, &top, NULL);
3841 hasFolding(bot, NULL, &bot);
3842# endif
3843 lines = bot - top + 1;
3844
3845 if (VIsual_mode == Ctrl_V)
3846 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003847# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003848 char_u *saved_sbr = p_sbr;
3849
3850 /* Make 'sbr' empty for a moment to get the correct size. */
3851 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003854# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003855 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3858 (long)(rightcol - leftcol + 1));
3859 }
3860 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3861 sprintf((char *)showcmd_buf, "%ld", lines);
3862 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003863 {
3864 char_u *s, *e;
3865 int l;
3866 int bytes = 0;
3867 int chars = 0;
3868
3869 if (cursor_bot)
3870 {
3871 s = ml_get_pos(&VIsual);
3872 e = ml_get_cursor();
3873 }
3874 else
3875 {
3876 s = ml_get_cursor();
3877 e = ml_get_pos(&VIsual);
3878 }
3879 while ((*p_sel != 'e') ? s <= e : s < e)
3880 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003881# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003882 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003883# else
3884 l = (*s == NUL) ? 0 : 1;
3885# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003886 if (l == 0)
3887 {
3888 ++bytes;
3889 ++chars;
3890 break; /* end of line */
3891 }
3892 bytes += l;
3893 ++chars;
3894 s += l;
3895 }
3896 if (bytes == chars)
3897 sprintf((char *)showcmd_buf, "%d", chars);
3898 else
3899 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3902 showcmd_visual = TRUE;
3903 }
3904 else
3905#endif
3906 {
3907 showcmd_buf[0] = NUL;
3908 showcmd_visual = FALSE;
3909
3910 /* Don't actually display something if there is nothing to clear. */
3911 if (showcmd_is_clear)
3912 return;
3913 }
3914
3915 display_showcmd();
3916}
3917
3918/*
3919 * Add 'c' to string of shown command chars.
3920 * Return TRUE if output has been written (and setcursor() has been called).
3921 */
3922 int
3923add_to_showcmd(c)
3924 int c;
3925{
3926 char_u *p;
3927 int old_len;
3928 int extra_len;
3929 int overflow;
3930#if defined(FEAT_MOUSE)
3931 int i;
3932 static int ignore[] =
3933 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003934# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3936 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 K_IGNORE,
3939 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3940 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3941 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003942 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003944 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 0
3946 };
3947#endif
3948
Bram Moolenaar09df3122006-01-23 22:23:09 +00003949 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 return FALSE;
3951
3952 if (showcmd_visual)
3953 {
3954 showcmd_buf[0] = NUL;
3955 showcmd_visual = FALSE;
3956 }
3957
3958#if defined(FEAT_MOUSE)
3959 /* Ignore keys that are scrollbar updates and mouse clicks */
3960 if (IS_SPECIAL(c))
3961 for (i = 0; ignore[i] != 0; ++i)
3962 if (ignore[i] == c)
3963 return FALSE;
3964#endif
3965
3966 p = transchar(c);
3967 old_len = (int)STRLEN(showcmd_buf);
3968 extra_len = (int)STRLEN(p);
3969 overflow = old_len + extra_len - SHOWCMD_COLS;
3970 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003971 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3972 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 STRCAT(showcmd_buf, p);
3974
3975 if (char_avail())
3976 return FALSE;
3977
3978 display_showcmd();
3979
3980 return TRUE;
3981}
3982
3983 void
3984add_to_showcmd_c(c)
3985 int c;
3986{
3987 if (!add_to_showcmd(c))
3988 setcursor();
3989}
3990
3991/*
3992 * Delete 'len' characters from the end of the shown command.
3993 */
3994 static void
3995del_from_showcmd(len)
3996 int len;
3997{
3998 int old_len;
3999
4000 if (!p_sc)
4001 return;
4002
4003 old_len = (int)STRLEN(showcmd_buf);
4004 if (len > old_len)
4005 len = old_len;
4006 showcmd_buf[old_len - len] = NUL;
4007
4008 if (!char_avail())
4009 display_showcmd();
4010}
4011
4012/*
4013 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4014 * something and there is a partial mapping.
4015 */
4016 void
4017push_showcmd()
4018{
4019 if (p_sc)
4020 STRCPY(old_showcmd_buf, showcmd_buf);
4021}
4022
4023 void
4024pop_showcmd()
4025{
4026 if (!p_sc)
4027 return;
4028
4029 STRCPY(showcmd_buf, old_showcmd_buf);
4030
4031 display_showcmd();
4032}
4033
4034 static void
4035display_showcmd()
4036{
4037 int len;
4038
4039 cursor_off();
4040
4041 len = (int)STRLEN(showcmd_buf);
4042 if (len == 0)
4043 showcmd_is_clear = TRUE;
4044 else
4045 {
4046 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4047 showcmd_is_clear = FALSE;
4048 }
4049
4050 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004051 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4052 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 */
4054 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4055
4056 setcursor(); /* put cursor back where it belongs */
4057}
4058#endif
4059
4060#ifdef FEAT_SCROLLBIND
4061/*
4062 * When "check" is FALSE, prepare for commands that scroll the window.
4063 * When "check" is TRUE, take care of scroll-binding after the window has
4064 * scrolled. Called from normal_cmd() and edit().
4065 */
4066 void
4067do_check_scrollbind(check)
4068 int check;
4069{
4070 static win_T *old_curwin = NULL;
4071 static linenr_T old_topline = 0;
4072#ifdef FEAT_DIFF
4073 static int old_topfill = 0;
4074#endif
4075 static buf_T *old_buf = NULL;
4076 static colnr_T old_leftcol = 0;
4077
4078 if (check && curwin->w_p_scb)
4079 {
4080 /* If a ":syncbind" command was just used, don't scroll, only reset
4081 * the values. */
4082 if (did_syncbind)
4083 did_syncbind = FALSE;
4084 else if (curwin == old_curwin)
4085 {
4086 /*
4087 * Synchronize other windows, as necessary according to
4088 * 'scrollbind'. Don't do this after an ":edit" command, except
4089 * when 'diff' is set.
4090 */
4091 if ((curwin->w_buffer == old_buf
4092#ifdef FEAT_DIFF
4093 || curwin->w_p_diff
4094#endif
4095 )
4096 && (curwin->w_topline != old_topline
4097#ifdef FEAT_DIFF
4098 || curwin->w_topfill != old_topfill
4099#endif
4100 || curwin->w_leftcol != old_leftcol))
4101 {
4102 check_scrollbind(curwin->w_topline - old_topline,
4103 (long)(curwin->w_leftcol - old_leftcol));
4104 }
4105 }
4106 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4107 {
4108 /*
4109 * When switching between windows, make sure that the relative
4110 * vertical offset is valid for the new window. The relative
4111 * offset is invalid whenever another 'scrollbind' window has
4112 * scrolled to a point that would force the current window to
4113 * scroll past the beginning or end of its buffer. When the
4114 * resync is performed, some of the other 'scrollbind' windows may
4115 * need to jump so that the current window's relative position is
4116 * visible on-screen.
4117 */
4118 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4119 }
4120 curwin->w_scbind_pos = curwin->w_topline;
4121 }
4122
4123 old_curwin = curwin;
4124 old_topline = curwin->w_topline;
4125#ifdef FEAT_DIFF
4126 old_topfill = curwin->w_topfill;
4127#endif
4128 old_buf = curwin->w_buffer;
4129 old_leftcol = curwin->w_leftcol;
4130}
4131
4132/*
4133 * Synchronize any windows that have "scrollbind" set, based on the
4134 * number of rows by which the current window has changed
4135 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4136 */
4137 void
4138check_scrollbind(topline_diff, leftcol_diff)
4139 linenr_T topline_diff;
4140 long leftcol_diff;
4141{
4142 int want_ver;
4143 int want_hor;
4144 win_T *old_curwin = curwin;
4145 buf_T *old_curbuf = curbuf;
4146#ifdef FEAT_VISUAL
4147 int old_VIsual_select = VIsual_select;
4148 int old_VIsual_active = VIsual_active;
4149#endif
4150 colnr_T tgt_leftcol = curwin->w_leftcol;
4151 long topline;
4152 long y;
4153
4154 /*
4155 * check 'scrollopt' string for vertical and horizontal scroll options
4156 */
4157 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4158#ifdef FEAT_DIFF
4159 want_ver |= old_curwin->w_p_diff;
4160#endif
4161 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4162
4163 /*
4164 * loop through the scrollbound windows and scroll accordingly
4165 */
4166#ifdef FEAT_VISUAL
4167 VIsual_select = VIsual_active = 0;
4168#endif
4169 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4170 {
4171 curbuf = curwin->w_buffer;
4172 /* skip original window and windows with 'noscrollbind' */
4173 if (curwin != old_curwin && curwin->w_p_scb)
4174 {
4175 /*
4176 * do the vertical scroll
4177 */
4178 if (want_ver)
4179 {
4180#ifdef FEAT_DIFF
4181 if (old_curwin->w_p_diff && curwin->w_p_diff)
4182 {
4183 diff_set_topline(old_curwin, curwin);
4184 }
4185 else
4186#endif
4187 {
4188 curwin->w_scbind_pos += topline_diff;
4189 topline = curwin->w_scbind_pos;
4190 if (topline > curbuf->b_ml.ml_line_count)
4191 topline = curbuf->b_ml.ml_line_count;
4192 if (topline < 1)
4193 topline = 1;
4194
4195 y = topline - curwin->w_topline;
4196 if (y > 0)
4197 scrollup(y, FALSE);
4198 else
4199 scrolldown(-y, FALSE);
4200 }
4201
4202 redraw_later(VALID);
4203 cursor_correct();
4204#ifdef FEAT_WINDOWS
4205 curwin->w_redr_status = TRUE;
4206#endif
4207 }
4208
4209 /*
4210 * do the horizontal scroll
4211 */
4212 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4213 {
4214 curwin->w_leftcol = tgt_leftcol;
4215 leftcol_changed();
4216 }
4217 }
4218 }
4219
4220 /*
4221 * reset current-window
4222 */
4223#ifdef FEAT_VISUAL
4224 VIsual_select = old_VIsual_select;
4225 VIsual_active = old_VIsual_active;
4226#endif
4227 curwin = old_curwin;
4228 curbuf = old_curbuf;
4229}
4230#endif /* #ifdef FEAT_SCROLLBIND */
4231
4232/*
4233 * Command character that's ignored.
4234 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004235 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 static void
4238nv_ignore(cap)
4239 cmdarg_T *cap;
4240{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004241 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242}
4243
4244/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004245 * Command character that doesn't do anything, but unlike nv_ignore() does
4246 * start edit(). Used for "startinsert" executed while starting up.
4247 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004248 static void
4249nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004250 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004251{
4252}
4253
4254/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 * Command character doesn't exist.
4256 */
4257 static void
4258nv_error(cap)
4259 cmdarg_T *cap;
4260{
4261 clearopbeep(cap->oap);
4262}
4263
4264/*
4265 * <Help> and <F1> commands.
4266 */
4267 static void
4268nv_help(cap)
4269 cmdarg_T *cap;
4270{
4271 if (!checkclearopq(cap->oap))
4272 ex_help(NULL);
4273}
4274
4275/*
4276 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4277 */
4278 static void
4279nv_addsub(cap)
4280 cmdarg_T *cap;
4281{
4282 if (!checkclearopq(cap->oap)
4283 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4284 prep_redo_cmd(cap);
4285}
4286
4287/*
4288 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4289 */
4290 static void
4291nv_page(cap)
4292 cmdarg_T *cap;
4293{
4294 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004295 {
4296#ifdef FEAT_WINDOWS
4297 if (mod_mask & MOD_MASK_CTRL)
4298 {
4299 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4300 if (cap->arg == BACKWARD)
4301 goto_tabpage(-(int)cap->count1);
4302 else
4303 goto_tabpage((int)cap->count0);
4304 }
4305 else
4306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
4311/*
4312 * Implementation of "gd" and "gD" command.
4313 */
4314 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004315nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316 oparg_T *oap;
4317 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004318 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
4320 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 char_u *ptr;
4322
Bram Moolenaard9d30582005-05-18 22:10:28 +00004323 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004324 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326#ifdef FEAT_FOLDING
4327 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4328 foldOpenCursor();
4329#endif
4330}
4331
4332/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004333 * Search for variable declaration of "ptr[len]".
4334 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4335 * current file ("gD").
4336 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004337 * Return FAIL when not found.
4338 */
4339 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004340find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004341 char_u *ptr;
4342 int len;
4343 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004344 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 int searchflags; /* flags passed to searchit() */
4346{
4347 char_u *pat;
4348 pos_T old_pos;
4349 pos_T par_pos;
4350 pos_T found_pos;
4351 int t;
4352 int save_p_ws;
4353 int save_p_scs;
4354 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004355 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356
4357 if ((pat = alloc(len + 7)) == NULL)
4358 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004359
4360 /* Put "\V" before the pattern to avoid that the special meaning of "."
4361 * and "~" causes trouble. */
4362 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4363 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 old_pos = curwin->w_cursor;
4365 save_p_ws = p_ws;
4366 save_p_scs = p_scs;
4367 p_ws = FALSE; /* don't wrap around end of file now */
4368 p_scs = FALSE; /* don't switch ignorecase off now */
4369
4370 /*
4371 * With "gD" go to line 1.
4372 * With "gd" Search back for the start of the current function, then go
4373 * back until a blank line. If this fails go to line 1.
4374 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004375 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 setpcmark(); /* Set in findpar() otherwise */
4378 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004379 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381 else
4382 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004383 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4385 --curwin->w_cursor.lnum;
4386 }
4387 curwin->w_cursor.col = 0;
4388
4389 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004390 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004391 for (;;)
4392 {
4393 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004394 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004395 if (curwin->w_cursor.lnum >= old_pos.lnum)
4396 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004397
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004398 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004399 {
4400 pos_T *pos;
4401
4402 /* Check that the block the match is in doesn't end before the
4403 * position where we started the search from. */
4404 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4405 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4406 && pos->lnum < old_pos.lnum)
4407 continue;
4408 }
4409
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 if (t == FAIL)
4411 {
4412 /* If we previously found a valid position, use it. */
4413 if (found_pos.lnum != 0)
4414 {
4415 curwin->w_cursor = found_pos;
4416 t = OK;
4417 }
4418 break;
4419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004421 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4422 {
4423 /* Ignore this line, continue at start of next line. */
4424 ++curwin->w_cursor.lnum;
4425 curwin->w_cursor.col = 0;
4426 continue;
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004429 if (!locally) /* global search: use first match found */
4430 break;
4431 if (curwin->w_cursor.lnum >= par_pos.lnum)
4432 {
4433 /* If we previously found a valid position, use it. */
4434 if (found_pos.lnum != 0)
4435 curwin->w_cursor = found_pos;
4436 break;
4437 }
4438
4439 /* For finding a local variable and the match is before the "{" search
4440 * to find a later match. For K&R style function declarations this
4441 * skips the function header without types. */
4442 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004444
4445 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004447 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 curwin->w_cursor = old_pos;
4449 }
4450 else
4451 {
4452 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 /* "n" searches forward now */
4454 reset_search_dir();
4455 }
4456
4457 vim_free(pat);
4458 p_ws = save_p_ws;
4459 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004460
4461 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462}
4463
4464/*
4465 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4466 * lines rather than lines in the file.
4467 * 'dist' must be positive.
4468 *
4469 * Return OK if able to move cursor, FAIL otherwise.
4470 */
4471 static int
4472nv_screengo(oap, dir, dist)
4473 oparg_T *oap;
4474 int dir;
4475 long dist;
4476{
4477 int linelen = linetabsize(ml_get_curline());
4478 int retval = OK;
4479 int atend = FALSE;
4480 int n;
4481 int col_off1; /* margin offset for first screen line */
4482 int col_off2; /* margin offset for wrapped screen line */
4483 int width1; /* text width for first screen line */
4484 int width2; /* test width for wrapped screen line */
4485
4486 oap->motion_type = MCHAR;
4487 oap->inclusive = FALSE;
4488
4489 col_off1 = curwin_col_off();
4490 col_off2 = col_off1 - curwin_col_off2();
4491 width1 = W_WIDTH(curwin) - col_off1;
4492 width2 = W_WIDTH(curwin) - col_off2;
4493
4494#ifdef FEAT_VERTSPLIT
4495 if (curwin->w_width != 0)
4496 {
4497#endif
4498 /*
4499 * Instead of sticking at the last character of the buffer line we
4500 * try to stick in the last column of the screen.
4501 */
4502 if (curwin->w_curswant == MAXCOL)
4503 {
4504 atend = TRUE;
4505 validate_virtcol();
4506 if (width1 <= 0)
4507 curwin->w_curswant = 0;
4508 else
4509 {
4510 curwin->w_curswant = width1 - 1;
4511 if (curwin->w_virtcol > curwin->w_curswant)
4512 curwin->w_curswant += ((curwin->w_virtcol
4513 - curwin->w_curswant - 1) / width2 + 1) * width2;
4514 }
4515 }
4516 else
4517 {
4518 if (linelen > width1)
4519 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4520 else
4521 n = width1;
4522 if (curwin->w_curswant > (colnr_T)n + 1)
4523 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4524 * width2;
4525 }
4526
4527 while (dist--)
4528 {
4529 if (dir == BACKWARD)
4530 {
4531 if ((long)curwin->w_curswant >= width2)
4532 /* move back within line */
4533 curwin->w_curswant -= width2;
4534 else
4535 {
4536 /* to previous line */
4537 if (curwin->w_cursor.lnum == 1)
4538 {
4539 retval = FAIL;
4540 break;
4541 }
4542 --curwin->w_cursor.lnum;
4543#ifdef FEAT_FOLDING
4544 /* Move to the start of a closed fold. Don't do that when
4545 * 'foldopen' contains "all": it will open in a moment. */
4546 if (!(fdo_flags & FDO_ALL))
4547 (void)hasFolding(curwin->w_cursor.lnum,
4548 &curwin->w_cursor.lnum, NULL);
4549#endif
4550 linelen = linetabsize(ml_get_curline());
4551 if (linelen > width1)
4552 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4553 + 1) * width2;
4554 }
4555 }
4556 else /* dir == FORWARD */
4557 {
4558 if (linelen > width1)
4559 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4560 else
4561 n = width1;
4562 if (curwin->w_curswant + width2 < (colnr_T)n)
4563 /* move forward within line */
4564 curwin->w_curswant += width2;
4565 else
4566 {
4567 /* to next line */
4568#ifdef FEAT_FOLDING
4569 /* Move to the end of a closed fold. */
4570 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4571 &curwin->w_cursor.lnum);
4572#endif
4573 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4574 {
4575 retval = FAIL;
4576 break;
4577 }
4578 curwin->w_cursor.lnum++;
4579 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004580 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583 }
4584#ifdef FEAT_VERTSPLIT
4585 }
4586#endif
4587
4588 coladvance(curwin->w_curswant);
4589
4590#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4591 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4592 {
4593 /*
4594 * Check for landing on a character that got split at the end of the
4595 * last line. We want to advance a screenline, not end up in the same
4596 * screenline or move two screenlines.
4597 */
4598 validate_virtcol();
4599 if (curwin->w_virtcol > curwin->w_curswant
4600 && (curwin->w_curswant < (colnr_T)width1
4601 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4602 : ((curwin->w_curswant - width1) % width2
4603 > (colnr_T)width2 / 2)))
4604 --curwin->w_cursor.col;
4605 }
4606#endif
4607
4608 if (atend)
4609 curwin->w_curswant = MAXCOL; /* stick in the last column */
4610
4611 return retval;
4612}
4613
4614#ifdef FEAT_MOUSE
4615/*
4616 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4617 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004618 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4619 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 */
4621 static void
4622nv_mousescroll(cap)
4623 cmdarg_T *cap;
4624{
4625# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004626 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 /* Currently we only get the mouse coordinates in the GUI. */
4629 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4630 {
4631 int row, col;
4632
4633 row = mouse_row;
4634 col = mouse_col;
4635
4636 /* find the window at the pointer coordinates */
4637 curwin = mouse_find_win(&row, &col);
4638 curbuf = curwin->w_buffer;
4639 }
4640# endif
4641
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004642 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004644 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4645 {
4646 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4647 }
4648 else
4649 {
4650 cap->count1 = 3;
4651 cap->count0 = 3;
4652 nv_scroll_line(cap);
4653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004655# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 else
4657 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004658 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4659 if (!curwin->w_p_wrap)
4660 {
4661 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004662
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4664 step = W_WIDTH(curwin);
4665 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4666 if (val < 0)
4667 val = 0;
4668
4669 gui_do_horiz_scroll(val, TRUE);
4670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4675 curwin->w_redr_status = TRUE;
4676
4677 curwin = old_curwin;
4678 curbuf = curwin->w_buffer;
4679# endif
4680}
4681
4682/*
4683 * Mouse clicks and drags.
4684 */
4685 static void
4686nv_mouse(cap)
4687 cmdarg_T *cap;
4688{
4689 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4690}
4691#endif
4692
4693/*
4694 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4695 * cap->arg must be TRUE for CTRL-E.
4696 */
4697 static void
4698nv_scroll_line(cap)
4699 cmdarg_T *cap;
4700{
4701 if (!checkclearop(cap->oap))
4702 scroll_redraw(cap->arg, cap->count1);
4703}
4704
4705/*
4706 * Scroll "count" lines up or down, and redraw.
4707 */
4708 void
4709scroll_redraw(up, count)
4710 int up;
4711 long count;
4712{
4713 linenr_T prev_topline = curwin->w_topline;
4714#ifdef FEAT_DIFF
4715 int prev_topfill = curwin->w_topfill;
4716#endif
4717 linenr_T prev_lnum = curwin->w_cursor.lnum;
4718
4719 if (up)
4720 scrollup(count, TRUE);
4721 else
4722 scrolldown(count, TRUE);
4723 if (p_so)
4724 {
4725 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4726 * valid, otherwise the screen jumps back at the end of the file. */
4727 cursor_correct();
4728 check_cursor_moved(curwin);
4729 curwin->w_valid |= VALID_TOPLINE;
4730
4731 /* If moved back to where we were, at least move the cursor, otherwise
4732 * we get stuck at one position. Don't move the cursor up if the
4733 * first line of the buffer is already on the screen */
4734 while (curwin->w_topline == prev_topline
4735#ifdef FEAT_DIFF
4736 && curwin->w_topfill == prev_topfill
4737#endif
4738 )
4739 {
4740 if (up)
4741 {
4742 if (curwin->w_cursor.lnum > prev_lnum
4743 || cursor_down(1L, FALSE) == FAIL)
4744 break;
4745 }
4746 else
4747 {
4748 if (curwin->w_cursor.lnum < prev_lnum
4749 || prev_topline == 1L
4750 || cursor_up(1L, FALSE) == FAIL)
4751 break;
4752 }
4753 /* Mark w_topline as valid, otherwise the screen jumps back at the
4754 * end of the file. */
4755 check_cursor_moved(curwin);
4756 curwin->w_valid |= VALID_TOPLINE;
4757 }
4758 }
4759 if (curwin->w_cursor.lnum != prev_lnum)
4760 coladvance(curwin->w_curswant);
4761 redraw_later(VALID);
4762}
4763
4764/*
4765 * Commands that start with "z".
4766 */
4767 static void
4768nv_zet(cap)
4769 cmdarg_T *cap;
4770{
4771 long n;
4772 colnr_T col;
4773 int nchar = cap->nchar;
4774#ifdef FEAT_FOLDING
4775 long old_fdl = curwin->w_p_fdl;
4776 int old_fen = curwin->w_p_fen;
4777#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004778#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004779 int undo = FALSE;
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 if (VIM_ISDIGIT(nchar))
4783 {
4784 /*
4785 * "z123{nchar}": edit the count before obtaining {nchar}
4786 */
4787 if (checkclearop(cap->oap))
4788 return;
4789 n = nchar - '0';
4790 for (;;)
4791 {
4792#ifdef USE_ON_FLY_SCROLL
4793 dont_scroll = TRUE; /* disallow scrolling here */
4794#endif
4795 ++no_mapping;
4796 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004797 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 --no_mapping;
4800 --allow_keys;
4801#ifdef FEAT_CMDL_INFO
4802 (void)add_to_showcmd(nchar);
4803#endif
4804 if (nchar == K_DEL || nchar == K_KDEL)
4805 n /= 10;
4806 else if (VIM_ISDIGIT(nchar))
4807 n = n * 10 + (nchar - '0');
4808 else if (nchar == CAR)
4809 {
4810#ifdef FEAT_GUI
4811 need_mouse_correct = TRUE;
4812#endif
4813 win_setheight((int)n);
4814 break;
4815 }
4816 else if (nchar == 'l'
4817 || nchar == 'h'
4818 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004819 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 cap->count1 = n ? n * cap->count1 : cap->count1;
4822 goto dozet;
4823 }
4824 else
4825 {
4826 clearopbeep(cap->oap);
4827 break;
4828 }
4829 }
4830 cap->oap->op_type = OP_NOP;
4831 return;
4832 }
4833
4834dozet:
4835 if (
4836#ifdef FEAT_FOLDING
4837 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4838 * and "zC" only in Visual mode. "zj" and "zk" are motion
4839 * commands. */
4840 cap->nchar != 'f' && cap->nchar != 'F'
4841 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4842 && cap->nchar != 'j' && cap->nchar != 'k'
4843 &&
4844#endif
4845 checkclearop(cap->oap))
4846 return;
4847
4848 /*
4849 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4850 * If line number given, set cursor.
4851 */
4852 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4853 && cap->count0
4854 && cap->count0 != curwin->w_cursor.lnum)
4855 {
4856 setpcmark();
4857 if (cap->count0 > curbuf->b_ml.ml_line_count)
4858 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4859 else
4860 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004861 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863
4864 switch (nchar)
4865 {
4866 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4867 case '+':
4868 if (cap->count0 == 0)
4869 {
4870 /* No count given: put cursor at the line below screen */
4871 validate_botline(); /* make sure w_botline is valid */
4872 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4873 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4874 else
4875 curwin->w_cursor.lnum = curwin->w_botline;
4876 }
4877 /* FALLTHROUGH */
4878 case NL:
4879 case CAR:
4880 case K_KENTER:
4881 beginline(BL_WHITE | BL_FIX);
4882 /* FALLTHROUGH */
4883
4884 case 't': scroll_cursor_top(0, TRUE);
4885 redraw_later(VALID);
4886 break;
4887
4888 /* "z." and "zz": put cursor in middle of screen */
4889 case '.': beginline(BL_WHITE | BL_FIX);
4890 /* FALLTHROUGH */
4891
4892 case 'z': scroll_cursor_halfway(TRUE);
4893 redraw_later(VALID);
4894 break;
4895
4896 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4897 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4898 * when <count> is at bottom of window, and puts that one at
4899 * bottom of window. */
4900 if (cap->count0 != 0)
4901 {
4902 scroll_cursor_bot(0, TRUE);
4903 curwin->w_cursor.lnum = curwin->w_topline;
4904 }
4905 else if (curwin->w_topline == 1)
4906 curwin->w_cursor.lnum = 1;
4907 else
4908 curwin->w_cursor.lnum = curwin->w_topline - 1;
4909 /* FALLTHROUGH */
4910 case '-':
4911 beginline(BL_WHITE | BL_FIX);
4912 /* FALLTHROUGH */
4913
4914 case 'b': scroll_cursor_bot(0, TRUE);
4915 redraw_later(VALID);
4916 break;
4917
4918 /* "zH" - scroll screen right half-page */
4919 case 'H':
4920 cap->count1 *= W_WIDTH(curwin) / 2;
4921 /* FALLTHROUGH */
4922
4923 /* "zh" - scroll screen to the right */
4924 case 'h':
4925 case K_LEFT:
4926 if (!curwin->w_p_wrap)
4927 {
4928 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4929 curwin->w_leftcol = 0;
4930 else
4931 curwin->w_leftcol -= (colnr_T)cap->count1;
4932 leftcol_changed();
4933 }
4934 break;
4935
4936 /* "zL" - scroll screen left half-page */
4937 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4938 /* FALLTHROUGH */
4939
4940 /* "zl" - scroll screen to the left */
4941 case 'l':
4942 case K_RIGHT:
4943 if (!curwin->w_p_wrap)
4944 {
4945 /* scroll the window left */
4946 curwin->w_leftcol += (colnr_T)cap->count1;
4947 leftcol_changed();
4948 }
4949 break;
4950
4951 /* "zs" - scroll screen, cursor at the start */
4952 case 's': if (!curwin->w_p_wrap)
4953 {
4954#ifdef FEAT_FOLDING
4955 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4956 col = 0; /* like the cursor is in col 0 */
4957 else
4958#endif
4959 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4960 if ((long)col > p_siso)
4961 col -= p_siso;
4962 else
4963 col = 0;
4964 if (curwin->w_leftcol != col)
4965 {
4966 curwin->w_leftcol = col;
4967 redraw_later(NOT_VALID);
4968 }
4969 }
4970 break;
4971
4972 /* "ze" - scroll screen, cursor at the end */
4973 case 'e': 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, NULL, NULL, &col);
4981 n = W_WIDTH(curwin) - curwin_col_off();
4982 if ((long)col + p_siso < n)
4983 col = 0;
4984 else
4985 col = col + p_siso - n + 1;
4986 if (curwin->w_leftcol != col)
4987 {
4988 curwin->w_leftcol = col;
4989 redraw_later(NOT_VALID);
4990 }
4991 }
4992 break;
4993
4994#ifdef FEAT_FOLDING
4995 /* "zF": create fold command */
4996 /* "zf": create fold operator */
4997 case 'F':
4998 case 'f': if (foldManualAllowed(TRUE))
4999 {
5000 cap->nchar = 'f';
5001 nv_operator(cap);
5002 curwin->w_p_fen = TRUE;
5003
5004 /* "zF" is like "zfzf" */
5005 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5006 {
5007 nv_operator(cap);
5008 finish_op = TRUE;
5009 }
5010 }
5011 else
5012 clearopbeep(cap->oap);
5013 break;
5014
5015 /* "zd": delete fold at cursor */
5016 /* "zD": delete fold at cursor recursively */
5017 case 'd':
5018 case 'D': if (foldManualAllowed(FALSE))
5019 {
5020 if (VIsual_active)
5021 nv_operator(cap);
5022 else
5023 deleteFold(curwin->w_cursor.lnum,
5024 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5025 }
5026 break;
5027
5028 /* "zE": erease all folds */
5029 case 'E': if (foldmethodIsManual(curwin))
5030 {
5031 clearFolding(curwin);
5032 changed_window_setting();
5033 }
5034 else if (foldmethodIsMarker(curwin))
5035 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5036 TRUE, FALSE);
5037 else
5038 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5039 break;
5040
5041 /* "zn": fold none: reset 'foldenable' */
5042 case 'n': curwin->w_p_fen = FALSE;
5043 break;
5044
5045 /* "zN": fold Normal: set 'foldenable' */
5046 case 'N': curwin->w_p_fen = TRUE;
5047 break;
5048
5049 /* "zi": invert folding: toggle 'foldenable' */
5050 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5051 break;
5052
5053 /* "za": open closed fold or close open fold at cursor */
5054 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5055 openFold(curwin->w_cursor.lnum, cap->count1);
5056 else
5057 {
5058 closeFold(curwin->w_cursor.lnum, cap->count1);
5059 curwin->w_p_fen = TRUE;
5060 }
5061 break;
5062
5063 /* "zA": open fold at cursor recursively */
5064 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5065 openFoldRecurse(curwin->w_cursor.lnum);
5066 else
5067 {
5068 closeFoldRecurse(curwin->w_cursor.lnum);
5069 curwin->w_p_fen = TRUE;
5070 }
5071 break;
5072
5073 /* "zo": open fold at cursor or Visual area */
5074 case 'o': if (VIsual_active)
5075 nv_operator(cap);
5076 else
5077 openFold(curwin->w_cursor.lnum, cap->count1);
5078 break;
5079
5080 /* "zO": open fold recursively */
5081 case 'O': if (VIsual_active)
5082 nv_operator(cap);
5083 else
5084 openFoldRecurse(curwin->w_cursor.lnum);
5085 break;
5086
5087 /* "zc": close fold at cursor or Visual area */
5088 case 'c': if (VIsual_active)
5089 nv_operator(cap);
5090 else
5091 closeFold(curwin->w_cursor.lnum, cap->count1);
5092 curwin->w_p_fen = TRUE;
5093 break;
5094
5095 /* "zC": close fold recursively */
5096 case 'C': if (VIsual_active)
5097 nv_operator(cap);
5098 else
5099 closeFoldRecurse(curwin->w_cursor.lnum);
5100 curwin->w_p_fen = TRUE;
5101 break;
5102
5103 /* "zv": open folds at the cursor */
5104 case 'v': foldOpenCursor();
5105 break;
5106
5107 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5108 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005109 curwin->w_foldinvalid = TRUE; /* recompute folds */
5110 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 foldOpenCursor();
5112 break;
5113
5114 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5115 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005116 curwin->w_foldinvalid = TRUE; /* recompute folds */
5117 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
5119
5120 /* "zm": fold more */
5121 case 'm': if (curwin->w_p_fdl > 0)
5122 --curwin->w_p_fdl;
5123 old_fdl = -1; /* force an update */
5124 curwin->w_p_fen = TRUE;
5125 break;
5126
5127 /* "zM": close all folds */
5128 case 'M': curwin->w_p_fdl = 0;
5129 old_fdl = -1; /* force an update */
5130 curwin->w_p_fen = TRUE;
5131 break;
5132
5133 /* "zr": reduce folding */
5134 case 'r': ++curwin->w_p_fdl;
5135 break;
5136
5137 /* "zR": open all folds */
5138 case 'R': curwin->w_p_fdl = getDeepestNesting();
5139 old_fdl = -1; /* force an update */
5140 break;
5141
5142 case 'j': /* "zj" move to next fold downwards */
5143 case 'k': /* "zk" move to next fold upwards */
5144 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5145 cap->count1) == FAIL)
5146 clearopbeep(cap->oap);
5147 break;
5148
5149#endif /* FEAT_FOLDING */
5150
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005151#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005152 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5153 ++no_mapping;
5154 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005155 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005157 --no_mapping;
5158 --allow_keys;
5159#ifdef FEAT_CMDL_INFO
5160 (void)add_to_showcmd(nchar);
5161#endif
5162 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5163 {
5164 clearopbeep(cap->oap);
5165 break;
5166 }
5167 undo = TRUE;
5168 /*FALLTHROUGH*/
5169
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170 case 'g': /* "zg": add good word to word list */
5171 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005172 case 'G': /* "zG": add good word to temp word list */
5173 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005174 {
5175 char_u *ptr = NULL;
5176 int len;
5177
5178 if (checkclearop(cap->oap))
5179 break;
5180# ifdef FEAT_VISUAL
5181 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5182 == FAIL)
5183 return;
5184# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005185 if (ptr == NULL)
5186 {
5187 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005188
5189 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005190 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005191 if (len != 0 && curwin->w_cursor.col <= pos.col)
5192 ptr = ml_get_pos(&curwin->w_cursor);
5193 curwin->w_cursor = pos;
5194 }
5195
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5197 FIND_IDENT)) == 0)
5198 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005199 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005200 (nchar == 'G' || nchar == 'W')
5201 ? 0 : (int)cap->count1,
5202 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005203 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005204 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005205
Bram Moolenaar43abc522005-12-10 20:15:02 +00005206 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005207 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005208 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005210#endif
5211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 default: clearopbeep(cap->oap);
5213 }
5214
5215#ifdef FEAT_FOLDING
5216 /* Redraw when 'foldenable' changed */
5217 if (old_fen != curwin->w_p_fen)
5218 {
5219# ifdef FEAT_DIFF
5220 win_T *wp;
5221
5222 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5223 {
5224 /* Adjust 'foldenable' in diff-synced windows. */
5225 FOR_ALL_WINDOWS(wp)
5226 {
5227 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5228 {
5229 wp->w_p_fen = curwin->w_p_fen;
5230 changed_window_setting_win(wp);
5231 }
5232 }
5233 }
5234# endif
5235 changed_window_setting();
5236 }
5237
5238 /* Redraw when 'foldlevel' changed. */
5239 if (old_fdl != curwin->w_p_fdl)
5240 newFoldLevel();
5241#endif
5242}
5243
5244#ifdef FEAT_GUI
5245/*
5246 * Vertical scrollbar movement.
5247 */
5248 static void
5249nv_ver_scrollbar(cap)
5250 cmdarg_T *cap;
5251{
5252 if (cap->oap->op_type != OP_NOP)
5253 clearopbeep(cap->oap);
5254
5255 /* Even if an operator was pending, we still want to scroll */
5256 gui_do_scroll();
5257}
5258
5259/*
5260 * Horizontal scrollbar movement.
5261 */
5262 static void
5263nv_hor_scrollbar(cap)
5264 cmdarg_T *cap;
5265{
5266 if (cap->oap->op_type != OP_NOP)
5267 clearopbeep(cap->oap);
5268
5269 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005270 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
5272#endif
5273
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005274#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005275/*
5276 * Click in GUI tab.
5277 */
5278 static void
5279nv_tabline(cap)
5280 cmdarg_T *cap;
5281{
5282 if (cap->oap->op_type != OP_NOP)
5283 clearopbeep(cap->oap);
5284
5285 /* Even if an operator was pending, we still want to jump tabs. */
5286 goto_tabpage(current_tab);
5287}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005288
5289/*
5290 * Selected item in tab line menu.
5291 */
5292 static void
5293nv_tabmenu(cap)
5294 cmdarg_T *cap;
5295{
5296 if (cap->oap->op_type != OP_NOP)
5297 clearopbeep(cap->oap);
5298
5299 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005300 handle_tabmenu();
5301}
5302
5303/*
5304 * Handle selecting an item of the GUI tab line menu.
5305 * Used in Normal and Insert mode.
5306 */
5307 void
5308handle_tabmenu()
5309{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005310 switch (current_tabmenu)
5311 {
5312 case TABLINE_MENU_CLOSE:
5313 if (current_tab == 0)
5314 do_cmdline_cmd((char_u *)"tabclose");
5315 else
5316 {
5317 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5318 current_tab);
5319 do_cmdline_cmd(IObuff);
5320 }
5321 break;
5322
5323 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005324 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5325 current_tab > 0 ? current_tab - 1 : 999);
5326 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005327 break;
5328
5329 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005330 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5331 current_tab > 0 ? current_tab - 1 : 999);
5332 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005333 break;
5334 }
5335}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005336#endif
5337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338/*
5339 * "Q" command.
5340 */
5341 static void
5342nv_exmode(cap)
5343 cmdarg_T *cap;
5344{
5345 /*
5346 * Ignore 'Q' in Visual mode, just give a beep.
5347 */
5348#ifdef FEAT_VISUAL
5349 if (VIsual_active)
5350 vim_beep();
5351 else
5352#endif
5353 if (!checkclearop(cap->oap))
5354 do_exmode(FALSE);
5355}
5356
5357/*
5358 * Handle a ":" command.
5359 */
5360 static void
5361nv_colon(cap)
5362 cmdarg_T *cap;
5363{
5364 int old_p_im;
5365
5366#ifdef FEAT_VISUAL
5367 if (VIsual_active)
5368 nv_operator(cap);
5369 else
5370#endif
5371 {
5372 if (cap->oap->op_type != OP_NOP)
5373 {
5374 /* Using ":" as a movement is characterwise exclusive. */
5375 cap->oap->motion_type = MCHAR;
5376 cap->oap->inclusive = FALSE;
5377 }
5378 else if (cap->count0)
5379 {
5380 /* translate "count:" into ":.,.+(count - 1)" */
5381 stuffcharReadbuff('.');
5382 if (cap->count0 > 1)
5383 {
5384 stuffReadbuff((char_u *)",.+");
5385 stuffnumReadbuff((long)cap->count0 - 1L);
5386 }
5387 }
5388
5389 /* When typing, don't type below an old message */
5390 if (KeyTyped)
5391 compute_cmdrow();
5392
5393 old_p_im = p_im;
5394
5395 /* get a command line and execute it */
5396 do_cmdline(NULL, getexline, NULL,
5397 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5398
5399 /* If 'insertmode' changed, enter or exit Insert mode */
5400 if (p_im != old_p_im)
5401 {
5402 if (p_im)
5403 restart_edit = 'i';
5404 else
5405 restart_edit = 0;
5406 }
5407
5408 /* The start of the operator may have become invalid by the Ex
5409 * command. */
5410 if (cap->oap->op_type != OP_NOP
5411 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5412 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005413 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 clearopbeep(cap->oap);
5415 }
5416}
5417
5418/*
5419 * Handle CTRL-G command.
5420 */
5421 static void
5422nv_ctrlg(cap)
5423 cmdarg_T *cap;
5424{
5425#ifdef FEAT_VISUAL
5426 if (VIsual_active) /* toggle Selection/Visual mode */
5427 {
5428 VIsual_select = !VIsual_select;
5429 showmode();
5430 }
5431 else
5432#endif
5433 if (!checkclearop(cap->oap))
5434 /* print full name if count given or :cd used */
5435 fileinfo((int)cap->count0, FALSE, TRUE);
5436}
5437
5438/*
5439 * Handle CTRL-H <Backspace> command.
5440 */
5441 static void
5442nv_ctrlh(cap)
5443 cmdarg_T *cap;
5444{
5445#ifdef FEAT_VISUAL
5446 if (VIsual_active && VIsual_select)
5447 {
5448 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5449 v_visop(cap);
5450 }
5451 else
5452#endif
5453 nv_left(cap);
5454}
5455
5456/*
5457 * CTRL-L: clear screen and redraw.
5458 */
5459 static void
5460nv_clear(cap)
5461 cmdarg_T *cap;
5462{
5463 if (!checkclearop(cap->oap))
5464 {
5465#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5466 /*
5467 * Right now, the BeBox doesn't seem to have an easy way to detect
5468 * window resizing, so we cheat and make the user detect it
5469 * manually with CTRL-L instead
5470 */
5471 ui_get_shellsize();
5472#endif
5473#ifdef FEAT_SYN_HL
5474 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005475 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476#endif
5477 redraw_later(CLEAR);
5478 }
5479}
5480
5481/*
5482 * CTRL-O: In Select mode: switch to Visual mode for one command.
5483 * Otherwise: Go to older pcmark.
5484 */
5485 static void
5486nv_ctrlo(cap)
5487 cmdarg_T *cap;
5488{
5489#ifdef FEAT_VISUAL
5490 if (VIsual_active && VIsual_select)
5491 {
5492 VIsual_select = FALSE;
5493 showmode();
5494 restart_VIsual_select = 2; /* restart Select mode later */
5495 }
5496 else
5497#endif
5498 {
5499 cap->count1 = -cap->count1;
5500 nv_pcmark(cap);
5501 }
5502}
5503
5504/*
5505 * CTRL-^ command, short for ":e #"
5506 */
5507 static void
5508nv_hat(cap)
5509 cmdarg_T *cap;
5510{
5511 if (!checkclearopq(cap->oap))
5512 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5513 GETF_SETMARK|GETF_ALT, FALSE);
5514}
5515
5516/*
5517 * "Z" commands.
5518 */
5519 static void
5520nv_Zet(cap)
5521 cmdarg_T *cap;
5522{
5523 if (!checkclearopq(cap->oap))
5524 {
5525 switch (cap->nchar)
5526 {
5527 /* "ZZ": equivalent to ":x". */
5528 case 'Z': do_cmdline_cmd((char_u *)"x");
5529 break;
5530
5531 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5532 case 'Q': do_cmdline_cmd((char_u *)"q!");
5533 break;
5534
5535 default: clearopbeep(cap->oap);
5536 }
5537 }
5538}
5539
5540#if defined(FEAT_WINDOWS) || defined(PROTO)
5541/*
5542 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5543 */
5544 void
5545do_nv_ident(c1, c2)
5546 int c1;
5547 int c2;
5548{
5549 oparg_T oa;
5550 cmdarg_T ca;
5551
5552 clear_oparg(&oa);
5553 vim_memset(&ca, 0, sizeof(ca));
5554 ca.oap = &oa;
5555 ca.cmdchar = c1;
5556 ca.nchar = c2;
5557 nv_ident(&ca);
5558}
5559#endif
5560
5561/*
5562 * Handle the commands that use the word under the cursor.
5563 * [g] CTRL-] :ta to current identifier
5564 * [g] 'K' run program for current identifier
5565 * [g] '*' / to current identifier or string
5566 * [g] '#' ? to current identifier or string
5567 * g ']' :tselect for current identifier
5568 */
5569 static void
5570nv_ident(cap)
5571 cmdarg_T *cap;
5572{
5573 char_u *ptr = NULL;
5574 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005575 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *p;
5577 char_u *kp; /* value of 'keywordprg' */
5578 int kp_help; /* 'keywordprg' is ":help" */
5579 int n = 0; /* init for GCC */
5580 int cmdchar;
5581 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005582 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 char_u *aux_ptr;
5584 int isman;
5585 int isman_s;
5586
5587 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5588 {
5589 cmdchar = cap->nchar;
5590 g_cmd = TRUE;
5591 }
5592 else
5593 {
5594 cmdchar = cap->cmdchar;
5595 g_cmd = FALSE;
5596 }
5597
5598 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5599 cmdchar = '#';
5600
5601 /*
5602 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5603 */
5604 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5605 {
5606#ifdef FEAT_VISUAL
5607 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5608 return;
5609#endif
5610 if (checkclearopq(cap->oap))
5611 return;
5612 }
5613
5614 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5615 (cmdchar == '*' || cmdchar == '#')
5616 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5617 {
5618 clearop(cap->oap);
5619 return;
5620 }
5621
5622 /* Allocate buffer to put the command in. Inserting backslashes can
5623 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5624 * and some numbers. */
5625 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5626 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5627 || STRCMP(kp, ":help") == 0);
5628 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5629 if (buf == NULL)
5630 return;
5631 buf[0] = NUL;
5632
5633 switch (cmdchar)
5634 {
5635 case '*':
5636 case '#':
5637 /*
5638 * Put cursor at start of word, makes search skip the word
5639 * under the cursor.
5640 * Call setpcmark() first, so "*``" puts the cursor back where
5641 * it was.
5642 */
5643 setpcmark();
5644 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5645
5646 if (!g_cmd && vim_iswordp(ptr))
5647 STRCPY(buf, "\\<");
5648 no_smartcase = TRUE; /* don't use 'smartcase' now */
5649 break;
5650
5651 case 'K':
5652 if (kp_help)
5653 STRCPY(buf, "he! ");
5654 else
5655 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005656 /* An external command will probably use an argument starting
5657 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005658 while (*ptr == '-' && n > 0)
5659 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005660 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005661 --n;
5662 }
5663 if (n == 0)
5664 {
5665 EMSG(_(e_noident)); /* found dashes only */
5666 vim_free(buf);
5667 return;
5668 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 /* When a count is given, turn it into a range. Is this
5671 * really what we want? */
5672 isman = (STRCMP(kp, "man") == 0);
5673 isman_s = (STRCMP(kp, "man -s") == 0);
5674 if (cap->count0 != 0 && !(isman || isman_s))
5675 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5676
5677 STRCAT(buf, "! ");
5678 if (cap->count0 == 0 && isman_s)
5679 STRCAT(buf, "man");
5680 else
5681 STRCAT(buf, kp);
5682 STRCAT(buf, " ");
5683 if (cap->count0 != 0 && (isman || isman_s))
5684 {
5685 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5686 STRCAT(buf, " ");
5687 }
5688 }
5689 break;
5690
5691 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005692 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#ifdef FEAT_CSCOPE
5694 if (p_cst)
5695 STRCPY(buf, "cstag ");
5696 else
5697#endif
5698 STRCPY(buf, "ts ");
5699 break;
5700
5701 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005702 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 if (curbuf->b_help)
5704 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005706 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005707 if (g_cmd)
5708 STRCPY(buf, "tj ");
5709 else
5710 sprintf((char *)buf, "%ldta ", cap->count0);
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713
5714 /*
5715 * Now grab the chars in the identifier
5716 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005717 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005719 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005720 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005721 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005722 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005723 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005725 vim_free(buf);
5726 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005728 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5729 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005730 {
5731 vim_free(buf);
5732 vim_free(p);
5733 return;
5734 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005735 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005736 STRCAT(buf, p);
5737 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005739 else
5740 {
5741 if (cmdchar == '*')
5742 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5743 else if (cmdchar == '#')
5744 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005745 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005746 {
5747 if (curbuf->b_help)
5748 /* ":help" handles unescaped argument */
5749 aux_ptr = (char_u *)"";
5750 else
5751 aux_ptr = (char_u *)"\\|\"\n[";
5752 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005753 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005754 aux_ptr = (char_u *)"\\|\"\n*?[";
5755
5756 p = buf + STRLEN(buf);
5757 while (n-- > 0)
5758 {
5759 /* put a backslash before \ and some others */
5760 if (vim_strchr(aux_ptr, *ptr) != NULL)
5761 *p++ = '\\';
5762#ifdef FEAT_MBYTE
5763 /* When current byte is a part of multibyte character, copy all
5764 * bytes of that character. */
5765 if (has_mbyte)
5766 {
5767 int i;
5768 int len = (*mb_ptr2len)(ptr) - 1;
5769
5770 for (i = 0; i < len && n >= 1; ++i, --n)
5771 *p++ = *ptr++;
5772 }
5773#endif
5774 *p++ = *ptr++;
5775 }
5776 *p = NUL;
5777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 /*
5780 * Execute the command.
5781 */
5782 if (cmdchar == '*' || cmdchar == '#')
5783 {
5784 if (!g_cmd && (
5785#ifdef FEAT_MBYTE
5786 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5787#endif
5788 vim_iswordc(ptr[-1])))
5789 STRCAT(buf, "\\>");
5790#ifdef FEAT_CMDHIST
5791 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005792 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5794#endif
5795 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5796 }
5797 else
5798 do_cmdline_cmd(buf);
5799
5800 vim_free(buf);
5801}
5802
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005803#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804/*
5805 * Get visually selected text, within one line only.
5806 * Returns FAIL if more than one line selected.
5807 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005808 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809get_visual_text(cap, pp, lenp)
5810 cmdarg_T *cap;
5811 char_u **pp; /* return: start of selected text */
5812 int *lenp; /* return: length of selected text */
5813{
5814 if (VIsual_mode != 'V')
5815 unadjust_for_sel();
5816 if (VIsual.lnum != curwin->w_cursor.lnum)
5817 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005818 if (cap != NULL)
5819 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return FAIL;
5821 }
5822 if (VIsual_mode == 'V')
5823 {
5824 *pp = ml_get_curline();
5825 *lenp = (int)STRLEN(*pp);
5826 }
5827 else
5828 {
5829 if (lt(curwin->w_cursor, VIsual))
5830 {
5831 *pp = ml_get_pos(&curwin->w_cursor);
5832 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5833 }
5834 else
5835 {
5836 *pp = ml_get_pos(&VIsual);
5837 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5838 }
5839#ifdef FEAT_MBYTE
5840 if (has_mbyte)
5841 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005842 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#endif
5844 }
5845 reset_VIsual_and_resel();
5846 return OK;
5847}
5848#endif
5849
5850/*
5851 * CTRL-T: backwards in tag stack
5852 */
5853 static void
5854nv_tagpop(cap)
5855 cmdarg_T *cap;
5856{
5857 if (!checkclearopq(cap->oap))
5858 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5859}
5860
5861/*
5862 * Handle scrolling command 'H', 'L' and 'M'.
5863 */
5864 static void
5865nv_scroll(cap)
5866 cmdarg_T *cap;
5867{
5868 int used = 0;
5869 long n;
5870#ifdef FEAT_FOLDING
5871 linenr_T lnum;
5872#endif
5873 int half;
5874
5875 cap->oap->motion_type = MLINE;
5876 setpcmark();
5877
5878 if (cap->cmdchar == 'L')
5879 {
5880 validate_botline(); /* make sure curwin->w_botline is valid */
5881 curwin->w_cursor.lnum = curwin->w_botline - 1;
5882 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5883 curwin->w_cursor.lnum = 1;
5884 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005885 {
5886#ifdef FEAT_FOLDING
5887 if (hasAnyFolding(curwin))
5888 {
5889 /* Count a fold for one screen line. */
5890 for (n = cap->count1 - 1; n > 0
5891 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5892 {
5893 (void)hasFolding(curwin->w_cursor.lnum,
5894 &curwin->w_cursor.lnum, NULL);
5895 --curwin->w_cursor.lnum;
5896 }
5897 }
5898 else
5899#endif
5900 curwin->w_cursor.lnum -= cap->count1 - 1;
5901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903 else
5904 {
5905 if (cap->cmdchar == 'M')
5906 {
5907#ifdef FEAT_DIFF
5908 /* Don't count filler lines above the window. */
5909 used -= diff_check_fill(curwin, curwin->w_topline)
5910 - curwin->w_topfill;
5911#endif
5912 validate_botline(); /* make sure w_empty_rows is valid */
5913 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5914 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5915 {
5916#ifdef FEAT_DIFF
5917 /* Count half he number of filler lines to be "below this
5918 * line" and half to be "above the next line". */
5919 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5920 + n) / 2 >= half)
5921 {
5922 --n;
5923 break;
5924 }
5925#endif
5926 used += plines(curwin->w_topline + n);
5927 if (used >= half)
5928 break;
5929#ifdef FEAT_FOLDING
5930 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5931 n = lnum - curwin->w_topline;
5932#endif
5933 }
5934 if (n > 0 && used > curwin->w_height)
5935 --n;
5936 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005937 else /* (cap->cmdchar == 'H') */
5938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005940#ifdef FEAT_FOLDING
5941 if (hasAnyFolding(curwin))
5942 {
5943 /* Count a fold for one screen line. */
5944 lnum = curwin->w_topline;
5945 while (n-- > 0 && lnum < curwin->w_botline - 1)
5946 {
5947 hasFolding(lnum, NULL, &lnum);
5948 ++lnum;
5949 }
5950 n = lnum - curwin->w_topline;
5951 }
5952#endif
5953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 curwin->w_cursor.lnum = curwin->w_topline + n;
5955 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5956 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5957 }
5958
5959 cursor_correct(); /* correct for 'so' */
5960 beginline(BL_SOL | BL_FIX);
5961}
5962
5963/*
5964 * Cursor right commands.
5965 */
5966 static void
5967nv_right(cap)
5968 cmdarg_T *cap;
5969{
5970 long n;
5971#ifdef FEAT_VISUAL
5972 int PAST_LINE;
5973#else
5974# define PAST_LINE 0
5975#endif
5976
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005977 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5978 {
5979 /* <C-Right> and <S-Right> move a word or WORD right */
5980 if (mod_mask & MOD_MASK_CTRL)
5981 cap->arg = TRUE;
5982 nv_wordcmd(cap);
5983 return;
5984 }
5985
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 cap->oap->motion_type = MCHAR;
5987 cap->oap->inclusive = FALSE;
5988#ifdef FEAT_VISUAL
5989 PAST_LINE = (VIsual_active && *p_sel != 'o');
5990
5991# ifdef FEAT_VIRTUALEDIT
5992 /*
5993 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005994 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 */
5996 if (virtual_active())
5997 PAST_LINE = 0;
5998# endif
5999#endif
6000
6001 for (n = cap->count1; n > 0; --n)
6002 {
6003 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006004#ifdef FEAT_VISUAL
6005 || (PAST_LINE && *ml_get_cursor() == NUL)
6006#endif
6007 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
6009 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006010 * <Space> wraps to next line if 'whichwrap' has 's'.
6011 * 'l' wraps to next line if 'whichwrap' has 'l'.
6012 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 */
6014 if ( ((cap->cmdchar == ' '
6015 && vim_strchr(p_ww, 's') != NULL)
6016 || (cap->cmdchar == 'l'
6017 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006018 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 && vim_strchr(p_ww, '>') != NULL))
6020 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6021 {
6022 /* When deleting we also count the NL as a character.
6023 * Set cap->oap->inclusive when last char in the line is
6024 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006025 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 && !cap->oap->inclusive
6027 && !lineempty(curwin->w_cursor.lnum))
6028 cap->oap->inclusive = TRUE;
6029 else
6030 {
6031 ++curwin->w_cursor.lnum;
6032 curwin->w_cursor.col = 0;
6033#ifdef FEAT_VIRTUALEDIT
6034 curwin->w_cursor.coladd = 0;
6035#endif
6036 curwin->w_set_curswant = TRUE;
6037 cap->oap->inclusive = FALSE;
6038 }
6039 continue;
6040 }
6041 if (cap->oap->op_type == OP_NOP)
6042 {
6043 /* Only beep and flush if not moved at all */
6044 if (n == cap->count1)
6045 beep_flush();
6046 }
6047 else
6048 {
6049 if (!lineempty(curwin->w_cursor.lnum))
6050 cap->oap->inclusive = TRUE;
6051 }
6052 break;
6053 }
6054#ifdef FEAT_VISUAL
6055 else if (PAST_LINE)
6056 {
6057 curwin->w_set_curswant = TRUE;
6058# ifdef FEAT_VIRTUALEDIT
6059 if (virtual_active())
6060 oneright();
6061 else
6062# endif
6063 {
6064# ifdef FEAT_MBYTE
6065 if (has_mbyte)
6066 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006067 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 else
6069# endif
6070 ++curwin->w_cursor.col;
6071 }
6072 }
6073#endif
6074 }
6075#ifdef FEAT_FOLDING
6076 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6077 && cap->oap->op_type == OP_NOP)
6078 foldOpenCursor();
6079#endif
6080}
6081
6082/*
6083 * Cursor left commands.
6084 *
6085 * Returns TRUE when operator end should not be adjusted.
6086 */
6087 static void
6088nv_left(cap)
6089 cmdarg_T *cap;
6090{
6091 long n;
6092
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006093 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6094 {
6095 /* <C-Left> and <S-Left> move a word or WORD left */
6096 if (mod_mask & MOD_MASK_CTRL)
6097 cap->arg = 1;
6098 nv_bck_word(cap);
6099 return;
6100 }
6101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 cap->oap->motion_type = MCHAR;
6103 cap->oap->inclusive = FALSE;
6104 for (n = cap->count1; n > 0; --n)
6105 {
6106 if (oneleft() == FAIL)
6107 {
6108 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6109 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6110 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6111 */
6112 if ( (((cap->cmdchar == K_BS
6113 || cap->cmdchar == Ctrl_H)
6114 && vim_strchr(p_ww, 'b') != NULL)
6115 || (cap->cmdchar == 'h'
6116 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006117 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 && vim_strchr(p_ww, '<') != NULL))
6119 && curwin->w_cursor.lnum > 1)
6120 {
6121 --(curwin->w_cursor.lnum);
6122 coladvance((colnr_T)MAXCOL);
6123 curwin->w_set_curswant = TRUE;
6124
6125 /* When the NL before the first char has to be deleted we
6126 * put the cursor on the NUL after the previous line.
6127 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006128 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if ( (cap->oap->op_type == OP_DELETE
6130 || cap->oap->op_type == OP_CHANGE)
6131 && !lineempty(curwin->w_cursor.lnum))
6132 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006133 if (*ml_get_cursor() != NUL)
6134 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 cap->retval |= CA_NO_ADJ_OP_END;
6136 }
6137 continue;
6138 }
6139 /* Only beep and flush if not moved at all */
6140 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6141 beep_flush();
6142 break;
6143 }
6144 }
6145#ifdef FEAT_FOLDING
6146 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6147 && cap->oap->op_type == OP_NOP)
6148 foldOpenCursor();
6149#endif
6150}
6151
6152/*
6153 * Cursor up commands.
6154 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6155 */
6156 static void
6157nv_up(cap)
6158 cmdarg_T *cap;
6159{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006160 if (mod_mask & MOD_MASK_SHIFT)
6161 {
6162 /* <S-Up> is page up */
6163 cap->arg = BACKWARD;
6164 nv_page(cap);
6165 }
6166 else
6167 {
6168 cap->oap->motion_type = MLINE;
6169 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6170 clearopbeep(cap->oap);
6171 else if (cap->arg)
6172 beginline(BL_WHITE | BL_FIX);
6173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174}
6175
6176/*
6177 * Cursor down commands.
6178 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6179 */
6180 static void
6181nv_down(cap)
6182 cmdarg_T *cap;
6183{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006184 if (mod_mask & MOD_MASK_SHIFT)
6185 {
6186 /* <S-Down> is page down */
6187 cap->arg = FORWARD;
6188 nv_page(cap);
6189 }
6190 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6192 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006193 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006194 if (curwin->w_llist_ref == NULL)
6195 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6196 else
6197 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 else
6199#endif
6200 {
6201#ifdef FEAT_CMDWIN
6202 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006203 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 cmdwin_result = CAR;
6205 else
6206#endif
6207 {
6208 cap->oap->motion_type = MLINE;
6209 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6210 clearopbeep(cap->oap);
6211 else if (cap->arg)
6212 beginline(BL_WHITE | BL_FIX);
6213 }
6214 }
6215}
6216
6217#ifdef FEAT_SEARCHPATH
6218/*
6219 * Grab the file name under the cursor and edit it.
6220 */
6221 static void
6222nv_gotofile(cap)
6223 cmdarg_T *cap;
6224{
6225 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006226 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006228 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
6230 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006231 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 return;
6233 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006234#ifdef FEAT_AUTOCMD
6235 if (curbuf_locked())
6236 {
6237 clearop(cap->oap);
6238 return;
6239 }
6240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006242 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
6244 if (ptr != NULL)
6245 {
6246 /* do autowrite if necessary */
6247 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6248 autowrite(curbuf, FALSE);
6249 setpcmark();
6250 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006251 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006252 if (cap->nchar == 'F' && lnum >= 0)
6253 {
6254 curwin->w_cursor.lnum = lnum;
6255 check_cursor_lnum();
6256 beginline(BL_SOL | BL_FIX);
6257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 vim_free(ptr);
6259 }
6260 else
6261 clearop(cap->oap);
6262}
6263#endif
6264
6265/*
6266 * <End> command: to end of current line or last line.
6267 */
6268 static void
6269nv_end(cap)
6270 cmdarg_T *cap;
6271{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006272 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006274 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 nv_goto(cap);
6276 cap->count1 = 1; /* to end of current line */
6277 }
6278 nv_dollar(cap);
6279}
6280
6281/*
6282 * Handle the "$" command.
6283 */
6284 static void
6285nv_dollar(cap)
6286 cmdarg_T *cap;
6287{
6288 cap->oap->motion_type = MCHAR;
6289 cap->oap->inclusive = TRUE;
6290#ifdef FEAT_VIRTUALEDIT
6291 /* In virtual mode when off the edge of a line and an operator
6292 * is pending (whew!) keep the cursor where it is.
6293 * Otherwise, send it to the end of the line. */
6294 if (!virtual_active() || gchar_cursor() != NUL
6295 || cap->oap->op_type == OP_NOP)
6296#endif
6297 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6298 if (cursor_down((long)(cap->count1 - 1),
6299 cap->oap->op_type == OP_NOP) == FAIL)
6300 clearopbeep(cap->oap);
6301#ifdef FEAT_FOLDING
6302 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6303 foldOpenCursor();
6304#endif
6305}
6306
6307/*
6308 * Implementation of '?' and '/' commands.
6309 * If cap->arg is TRUE don't set PC mark.
6310 */
6311 static void
6312nv_search(cap)
6313 cmdarg_T *cap;
6314{
6315 oparg_T *oap = cap->oap;
6316
6317 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6318 {
6319 /* Translate "g??" to "g?g?" */
6320 cap->cmdchar = 'g';
6321 cap->nchar = '?';
6322 nv_operator(cap);
6323 return;
6324 }
6325
6326 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6327
6328 if (cap->searchbuf == NULL)
6329 {
6330 clearop(oap);
6331 return;
6332 }
6333
6334 normal_search(cap, cap->cmdchar, cap->searchbuf,
6335 (cap->arg ? 0 : SEARCH_MARK));
6336}
6337
6338/*
6339 * Handle "N" and "n" commands.
6340 * cap->arg is SEARCH_REV for "N", 0 for "n".
6341 */
6342 static void
6343nv_next(cap)
6344 cmdarg_T *cap;
6345{
6346 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6347}
6348
6349/*
6350 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6351 * Uses only cap->count1 and cap->oap from "cap".
6352 */
6353 static void
6354normal_search(cap, dir, pat, opt)
6355 cmdarg_T *cap;
6356 int dir;
6357 char_u *pat;
6358 int opt; /* extra flags for do_search() */
6359{
6360 int i;
6361
6362 cap->oap->motion_type = MCHAR;
6363 cap->oap->inclusive = FALSE;
6364 cap->oap->use_reg_one = TRUE;
6365 curwin->w_set_curswant = TRUE;
6366
6367 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006368 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (i == 0)
6370 clearop(cap->oap);
6371 else
6372 {
6373 if (i == 2)
6374 cap->oap->motion_type = MLINE;
6375#ifdef FEAT_VIRTUALEDIT
6376 curwin->w_cursor.coladd = 0;
6377#endif
6378#ifdef FEAT_FOLDING
6379 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6380 foldOpenCursor();
6381#endif
6382 }
6383
6384 /* "/$" will put the cursor after the end of the line, may need to
6385 * correct that here */
6386 check_cursor();
6387}
6388
6389/*
6390 * Character search commands.
6391 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6392 * ',' and FALSE for ';'.
6393 * cap->nchar is NUL for ',' and ';' (repeat the search)
6394 */
6395 static void
6396nv_csearch(cap)
6397 cmdarg_T *cap;
6398{
6399 int t_cmd;
6400
6401 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6402 t_cmd = TRUE;
6403 else
6404 t_cmd = FALSE;
6405
6406 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6408 clearopbeep(cap->oap);
6409 else
6410 {
6411 curwin->w_set_curswant = TRUE;
6412#ifdef FEAT_VIRTUALEDIT
6413 /* Include a Tab for "tx" and for "dfx". */
6414 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6415 && (t_cmd || cap->oap->op_type != OP_NOP))
6416 {
6417 colnr_T scol, ecol;
6418
6419 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6420 curwin->w_cursor.coladd = ecol - scol;
6421 }
6422 else
6423 curwin->w_cursor.coladd = 0;
6424#endif
6425#ifdef FEAT_VISUAL
6426 adjust_for_sel(cap);
6427#endif
6428#ifdef FEAT_FOLDING
6429 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6430 foldOpenCursor();
6431#endif
6432 }
6433}
6434
6435/*
6436 * "[" and "]" commands.
6437 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6438 */
6439 static void
6440nv_brackets(cap)
6441 cmdarg_T *cap;
6442{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006443 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 pos_T prev_pos;
6445 pos_T *pos = NULL; /* init for GCC */
6446 pos_T old_pos; /* cursor position before command */
6447 int flag;
6448 long n;
6449 int findc;
6450 int c;
6451
6452 cap->oap->motion_type = MCHAR;
6453 cap->oap->inclusive = FALSE;
6454 old_pos = curwin->w_cursor;
6455#ifdef FEAT_VIRTUALEDIT
6456 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6457#endif
6458
6459#ifdef FEAT_SEARCHPATH
6460 /*
6461 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6462 */
6463 if (cap->nchar == 'f')
6464 nv_gotofile(cap);
6465 else
6466#endif
6467
6468#ifdef FEAT_FIND_ID
6469 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006470 * Find the occurrence(s) of the identifier or define under cursor
6471 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 *
6473 * search list jump
6474 * fwd bwd fwd bwd fwd bwd
6475 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6476 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6477 */
6478 if (vim_strchr((char_u *)
6479#ifdef EBCDIC
6480 "iI\005dD\067",
6481#else
6482 "iI\011dD\004",
6483#endif
6484 cap->nchar) != NULL)
6485 {
6486 char_u *ptr;
6487 int len;
6488
6489 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6490 clearop(cap->oap);
6491 else
6492 {
6493 find_pattern_in_path(ptr, 0, len, TRUE,
6494 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6495 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6496 cap->count1,
6497 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6498 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6499 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6500 (linenr_T)MAXLNUM);
6501 curwin->w_set_curswant = TRUE;
6502 }
6503 }
6504 else
6505#endif
6506
6507 /*
6508 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6509 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6510 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6511 * "[m" or "]m" search for prev/next start of (Java) method.
6512 * "[M" or "]M" search for prev/next end of (Java) method.
6513 */
6514 if ( (cap->cmdchar == '['
6515 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6516 || (cap->cmdchar == ']'
6517 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6518 {
6519 if (cap->nchar == '*')
6520 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 prev_pos.lnum = 0;
6522 if (cap->nchar == 'm' || cap->nchar == 'M')
6523 {
6524 if (cap->cmdchar == '[')
6525 findc = '{';
6526 else
6527 findc = '}';
6528 n = 9999;
6529 }
6530 else
6531 {
6532 findc = cap->nchar;
6533 n = cap->count1;
6534 }
6535 for ( ; n > 0; --n)
6536 {
6537 if ((pos = findmatchlimit(cap->oap, findc,
6538 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6539 {
6540 if (new_pos.lnum == 0) /* nothing found */
6541 {
6542 if (cap->nchar != 'm' && cap->nchar != 'M')
6543 clearopbeep(cap->oap);
6544 }
6545 else
6546 pos = &new_pos; /* use last one found */
6547 break;
6548 }
6549 prev_pos = new_pos;
6550 curwin->w_cursor = *pos;
6551 new_pos = *pos;
6552 }
6553 curwin->w_cursor = old_pos;
6554
6555 /*
6556 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6557 * brought us to the match for "[m" and "]M" when inside a method.
6558 * Try finding the '{' or '}' we want to be at.
6559 * Also repeat for the given count.
6560 */
6561 if (cap->nchar == 'm' || cap->nchar == 'M')
6562 {
6563 /* norm is TRUE for "]M" and "[m" */
6564 int norm = ((findc == '{') == (cap->nchar == 'm'));
6565
6566 n = cap->count1;
6567 /* found a match: we were inside a method */
6568 if (prev_pos.lnum != 0)
6569 {
6570 pos = &prev_pos;
6571 curwin->w_cursor = prev_pos;
6572 if (norm)
6573 --n;
6574 }
6575 else
6576 pos = NULL;
6577 while (n > 0)
6578 {
6579 for (;;)
6580 {
6581 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6582 {
6583 /* if not found anything, that's an error */
6584 if (pos == NULL)
6585 clearopbeep(cap->oap);
6586 n = 0;
6587 break;
6588 }
6589 c = gchar_cursor();
6590 if (c == '{' || c == '}')
6591 {
6592 /* Must have found end/start of class: use it.
6593 * Or found the place to be at. */
6594 if ((c == findc && norm) || (n == 1 && !norm))
6595 {
6596 new_pos = curwin->w_cursor;
6597 pos = &new_pos;
6598 n = 0;
6599 }
6600 /* if no match found at all, we started outside of the
6601 * class and we're inside now. Just go on. */
6602 else if (new_pos.lnum == 0)
6603 {
6604 new_pos = curwin->w_cursor;
6605 pos = &new_pos;
6606 }
6607 /* found start/end of other method: go to match */
6608 else if ((pos = findmatchlimit(cap->oap, findc,
6609 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6610 0)) == NULL)
6611 n = 0;
6612 else
6613 curwin->w_cursor = *pos;
6614 break;
6615 }
6616 }
6617 --n;
6618 }
6619 curwin->w_cursor = old_pos;
6620 if (pos == NULL && new_pos.lnum != 0)
6621 clearopbeep(cap->oap);
6622 }
6623 if (pos != NULL)
6624 {
6625 setpcmark();
6626 curwin->w_cursor = *pos;
6627 curwin->w_set_curswant = TRUE;
6628#ifdef FEAT_FOLDING
6629 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6630 && cap->oap->op_type == OP_NOP)
6631 foldOpenCursor();
6632#endif
6633 }
6634 }
6635
6636 /*
6637 * "[[", "[]", "]]" and "][": move to start or end of function
6638 */
6639 else if (cap->nchar == '[' || cap->nchar == ']')
6640 {
6641 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6642 flag = '{';
6643 else
6644 flag = '}'; /* "][" or "[]" */
6645
6646 curwin->w_set_curswant = TRUE;
6647 /*
6648 * Imitate strange Vi behaviour: When using "]]" with an operator
6649 * we also stop at '}'.
6650 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006651 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 (cap->oap->op_type != OP_NOP
6653 && cap->arg == FORWARD && flag == '{')))
6654 clearopbeep(cap->oap);
6655 else
6656 {
6657 if (cap->oap->op_type == OP_NOP)
6658 beginline(BL_WHITE | BL_FIX);
6659#ifdef FEAT_FOLDING
6660 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6661 foldOpenCursor();
6662#endif
6663 }
6664 }
6665
6666 /*
6667 * "[p", "[P", "]P" and "]p": put with indent adjustment
6668 */
6669 else if (cap->nchar == 'p' || cap->nchar == 'P')
6670 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006671 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 prep_redo_cmd(cap);
6674 do_put(cap->oap->regname,
6675 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6676 cap->count1, PUT_FIXINDENT);
6677 }
6678 }
6679
6680 /*
6681 * "['", "[`", "]'" and "]`": jump to next mark
6682 */
6683 else if (cap->nchar == '\'' || cap->nchar == '`')
6684 {
6685 pos = &curwin->w_cursor;
6686 for (n = cap->count1; n > 0; --n)
6687 {
6688 prev_pos = *pos;
6689 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6690 cap->nchar == '\'');
6691 if (pos == NULL)
6692 break;
6693 }
6694 if (pos == NULL)
6695 pos = &prev_pos;
6696 nv_cursormark(cap, cap->nchar == '\'', pos);
6697 }
6698
6699#ifdef FEAT_MOUSE
6700 /*
6701 * [ or ] followed by a middle mouse click: put selected text with
6702 * indent adjustment. Any other button just does as usual.
6703 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006704 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 (void)do_mouse(cap->oap, cap->nchar,
6707 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6708 cap->count1, PUT_FIXINDENT);
6709 }
6710#endif /* FEAT_MOUSE */
6711
6712#ifdef FEAT_FOLDING
6713 /*
6714 * "[z" and "]z": move to start or end of open fold.
6715 */
6716 else if (cap->nchar == 'z')
6717 {
6718 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6719 cap->count1) == FAIL)
6720 clearopbeep(cap->oap);
6721 }
6722#endif
6723
6724#ifdef FEAT_DIFF
6725 /*
6726 * "[c" and "]c": move to next or previous diff-change.
6727 */
6728 else if (cap->nchar == 'c')
6729 {
6730 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6731 cap->count1) == FAIL)
6732 clearopbeep(cap->oap);
6733 }
6734#endif
6735
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006736#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006737 /*
6738 * "[s", "[S", "]s" and "]S": move to next spell error.
6739 */
6740 else if (cap->nchar == 's' || cap->nchar == 'S')
6741 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006742 setpcmark();
6743 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006744 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6745 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006746 {
6747 clearopbeep(cap->oap);
6748 break;
6749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750# ifdef FEAT_FOLDING
6751 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6752 foldOpenCursor();
6753# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006754 }
6755#endif
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 /* Not a valid cap->nchar. */
6758 else
6759 clearopbeep(cap->oap);
6760}
6761
6762/*
6763 * Handle Normal mode "%" command.
6764 */
6765 static void
6766nv_percent(cap)
6767 cmdarg_T *cap;
6768{
6769 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006770#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 linenr_T lnum = curwin->w_cursor.lnum;
6772#endif
6773
6774 cap->oap->inclusive = TRUE;
6775 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6776 {
6777 if (cap->count0 > 100)
6778 clearopbeep(cap->oap);
6779 else
6780 {
6781 cap->oap->motion_type = MLINE;
6782 setpcmark();
6783 /* Round up, so CTRL-G will give same value. Watch out for a
6784 * large line count, the line number must not go negative! */
6785 if (curbuf->b_ml.ml_line_count > 1000000)
6786 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6787 / 100L * cap->count0;
6788 else
6789 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6790 cap->count0 + 99L) / 100L;
6791 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6793 beginline(BL_SOL | BL_FIX);
6794 }
6795 }
6796 else /* "%" : go to matching paren */
6797 {
6798 cap->oap->motion_type = MCHAR;
6799 cap->oap->use_reg_one = TRUE;
6800 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6801 clearopbeep(cap->oap);
6802 else
6803 {
6804 setpcmark();
6805 curwin->w_cursor = *pos;
6806 curwin->w_set_curswant = TRUE;
6807#ifdef FEAT_VIRTUALEDIT
6808 curwin->w_cursor.coladd = 0;
6809#endif
6810#ifdef FEAT_VISUAL
6811 adjust_for_sel(cap);
6812#endif
6813 }
6814 }
6815#ifdef FEAT_FOLDING
6816 if (cap->oap->op_type == OP_NOP
6817 && lnum != curwin->w_cursor.lnum
6818 && (fdo_flags & FDO_PERCENT)
6819 && KeyTyped)
6820 foldOpenCursor();
6821#endif
6822}
6823
6824/*
6825 * Handle "(" and ")" commands.
6826 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6827 */
6828 static void
6829nv_brace(cap)
6830 cmdarg_T *cap;
6831{
6832 cap->oap->motion_type = MCHAR;
6833 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006834 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6835 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 curwin->w_set_curswant = TRUE;
6837
6838 if (findsent(cap->arg, cap->count1) == FAIL)
6839 clearopbeep(cap->oap);
6840 else
6841 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006842 /* Don't leave the cursor on the NUL past end of line. */
6843 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#ifdef FEAT_VIRTUALEDIT
6845 curwin->w_cursor.coladd = 0;
6846#endif
6847#ifdef FEAT_FOLDING
6848 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6849 foldOpenCursor();
6850#endif
6851 }
6852}
6853
6854/*
6855 * "m" command: Mark a position.
6856 */
6857 static void
6858nv_mark(cap)
6859 cmdarg_T *cap;
6860{
6861 if (!checkclearop(cap->oap))
6862 {
6863 if (setmark(cap->nchar) == FAIL)
6864 clearopbeep(cap->oap);
6865 }
6866}
6867
6868/*
6869 * "{" and "}" commands.
6870 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6871 */
6872 static void
6873nv_findpar(cap)
6874 cmdarg_T *cap;
6875{
6876 cap->oap->motion_type = MCHAR;
6877 cap->oap->inclusive = FALSE;
6878 cap->oap->use_reg_one = TRUE;
6879 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006880 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 clearopbeep(cap->oap);
6882 else
6883 {
6884#ifdef FEAT_VIRTUALEDIT
6885 curwin->w_cursor.coladd = 0;
6886#endif
6887#ifdef FEAT_FOLDING
6888 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6889 foldOpenCursor();
6890#endif
6891 }
6892}
6893
6894/*
6895 * "u" command: Undo or make lower case.
6896 */
6897 static void
6898nv_undo(cap)
6899 cmdarg_T *cap;
6900{
6901 if (cap->oap->op_type == OP_LOWER
6902#ifdef FEAT_VISUAL
6903 || VIsual_active
6904#endif
6905 )
6906 {
6907 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6908 cap->cmdchar = 'g';
6909 cap->nchar = 'u';
6910 nv_operator(cap);
6911 }
6912 else
6913 nv_kundo(cap);
6914}
6915
6916/*
6917 * <Undo> command.
6918 */
6919 static void
6920nv_kundo(cap)
6921 cmdarg_T *cap;
6922{
6923 if (!checkclearopq(cap->oap))
6924 {
6925 u_undo((int)cap->count1);
6926 curwin->w_set_curswant = TRUE;
6927 }
6928}
6929
6930/*
6931 * Handle the "r" command.
6932 */
6933 static void
6934nv_replace(cap)
6935 cmdarg_T *cap;
6936{
6937 char_u *ptr;
6938 int had_ctrl_v;
6939 long n;
6940
6941 if (checkclearop(cap->oap))
6942 return;
6943
6944 /* get another character */
6945 if (cap->nchar == Ctrl_V)
6946 {
6947 had_ctrl_v = Ctrl_V;
6948 cap->nchar = get_literal();
6949 /* Don't redo a multibyte character with CTRL-V. */
6950 if (cap->nchar > DEL)
6951 had_ctrl_v = NUL;
6952 }
6953 else
6954 had_ctrl_v = NUL;
6955
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006956 /* Abort if the character is a special key. */
6957 if (IS_SPECIAL(cap->nchar))
6958 {
6959 clearopbeep(cap->oap);
6960 return;
6961 }
6962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#ifdef FEAT_VISUAL
6964 /* Visual mode "r" */
6965 if (VIsual_active)
6966 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006967 if (got_int)
6968 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 nv_operator(cap);
6970 return;
6971 }
6972#endif
6973
6974#ifdef FEAT_VIRTUALEDIT
6975 /* Break tabs, etc. */
6976 if (virtual_active())
6977 {
6978 if (u_save_cursor() == FAIL)
6979 return;
6980 if (gchar_cursor() == NUL)
6981 {
6982 /* Add extra space and put the cursor on the first one. */
6983 coladvance_force((colnr_T)(getviscol() + cap->count1));
6984 curwin->w_cursor.col -= cap->count1;
6985 }
6986 else if (gchar_cursor() == TAB)
6987 coladvance_force(getviscol());
6988 }
6989#endif
6990
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006991 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006993 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#ifdef FEAT_MBYTE
6995 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6996#endif
6997 )
6998 {
6999 clearopbeep(cap->oap);
7000 return;
7001 }
7002
7003 /*
7004 * Replacing with a TAB is done by edit() when it is complicated because
7005 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7006 * Other characters are done below to avoid problems with things like
7007 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7008 */
7009 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7010 {
7011 stuffnumReadbuff(cap->count1);
7012 stuffcharReadbuff('R');
7013 stuffcharReadbuff('\t');
7014 stuffcharReadbuff(ESC);
7015 return;
7016 }
7017
7018 /* save line for undo */
7019 if (u_save_cursor() == FAIL)
7020 return;
7021
7022 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7023 {
7024 /*
7025 * Replace character(s) by a single newline.
7026 * Strange vi behaviour: Only one newline is inserted.
7027 * Delete the characters here.
7028 * Insert the newline with an insert command, takes care of
7029 * autoindent. The insert command depends on being on the last
7030 * character of a line or not.
7031 */
7032#ifdef FEAT_MBYTE
7033 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7034#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007035 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#endif
7037 stuffcharReadbuff('\r');
7038 stuffcharReadbuff(ESC);
7039
7040 /* Give 'r' to edit(), to get the redo command right. */
7041 invoke_edit(cap, TRUE, 'r', FALSE);
7042 }
7043 else
7044 {
7045 prep_redo(cap->oap->regname, cap->count1,
7046 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7047
7048 curbuf->b_op_start = curwin->w_cursor;
7049#ifdef FEAT_MBYTE
7050 if (has_mbyte)
7051 {
7052 int old_State = State;
7053
7054 if (cap->ncharC1 != 0)
7055 AppendCharToRedobuff(cap->ncharC1);
7056 if (cap->ncharC2 != 0)
7057 AppendCharToRedobuff(cap->ncharC2);
7058
7059 /* This is slow, but it handles replacing a single-byte with a
7060 * multi-byte and the other way around. Also handles adding
7061 * composing characters for utf-8. */
7062 for (n = cap->count1; n > 0; --n)
7063 {
7064 State = REPLACE;
7065 ins_char(cap->nchar);
7066 State = old_State;
7067 if (cap->ncharC1 != 0)
7068 ins_char(cap->ncharC1);
7069 if (cap->ncharC2 != 0)
7070 ins_char(cap->ncharC2);
7071 }
7072 }
7073 else
7074#endif
7075 {
7076 /*
7077 * Replace the characters within one line.
7078 */
7079 for (n = cap->count1; n > 0; --n)
7080 {
7081 /*
7082 * Get ptr again, because u_save and/or showmatch() will have
7083 * released the line. At the same time we let know that the
7084 * line will be changed.
7085 */
7086 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7087 ptr[curwin->w_cursor.col] = cap->nchar;
7088 if (p_sm && msg_silent == 0)
7089 showmatch(cap->nchar);
7090 ++curwin->w_cursor.col;
7091 }
7092#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007093 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007095 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
Bram Moolenaar009b2592004-10-24 19:18:58 +00007097 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007098 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007100 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102#endif
7103
7104 /* mark the buffer as changed and prepare for displaying */
7105 changed_bytes(curwin->w_cursor.lnum,
7106 (colnr_T)(curwin->w_cursor.col - cap->count1));
7107 }
7108 --curwin->w_cursor.col; /* cursor on the last replaced char */
7109#ifdef FEAT_MBYTE
7110 /* if the character on the left of the current cursor is a multi-byte
7111 * character, move two characters left */
7112 if (has_mbyte)
7113 mb_adjust_cursor();
7114#endif
7115 curbuf->b_op_end = curwin->w_cursor;
7116 curwin->w_set_curswant = TRUE;
7117 set_last_insert(cap->nchar);
7118 }
7119}
7120
7121#ifdef FEAT_VISUAL
7122/*
7123 * 'o': Exchange start and end of Visual area.
7124 * 'O': same, but in block mode exchange left and right corners.
7125 */
7126 static void
7127v_swap_corners(cmdchar)
7128 int cmdchar;
7129{
7130 pos_T old_cursor;
7131 colnr_T left, right;
7132
7133 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7134 {
7135 old_cursor = curwin->w_cursor;
7136 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7137 curwin->w_cursor.lnum = VIsual.lnum;
7138 coladvance(left);
7139 VIsual = curwin->w_cursor;
7140
7141 curwin->w_cursor.lnum = old_cursor.lnum;
7142 curwin->w_curswant = right;
7143 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7144 * right one column */
7145 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7146 ++curwin->w_curswant;
7147 coladvance(curwin->w_curswant);
7148 if (curwin->w_cursor.col == old_cursor.col
7149#ifdef FEAT_VIRTUALEDIT
7150 && (!virtual_active()
7151 || curwin->w_cursor.coladd == old_cursor.coladd)
7152#endif
7153 )
7154 {
7155 curwin->w_cursor.lnum = VIsual.lnum;
7156 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7157 ++right;
7158 coladvance(right);
7159 VIsual = curwin->w_cursor;
7160
7161 curwin->w_cursor.lnum = old_cursor.lnum;
7162 coladvance(left);
7163 curwin->w_curswant = left;
7164 }
7165 }
7166 else
7167 {
7168 old_cursor = curwin->w_cursor;
7169 curwin->w_cursor = VIsual;
7170 VIsual = old_cursor;
7171 curwin->w_set_curswant = TRUE;
7172 }
7173}
7174#endif /* FEAT_VISUAL */
7175
7176/*
7177 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7178 */
7179 static void
7180nv_Replace(cap)
7181 cmdarg_T *cap;
7182{
7183#ifdef FEAT_VISUAL
7184 if (VIsual_active) /* "R" is replace lines */
7185 {
7186 cap->cmdchar = 'c';
7187 cap->nchar = NUL;
7188 VIsual_mode = 'V';
7189 nv_operator(cap);
7190 }
7191 else
7192#endif
7193 if (!checkclearopq(cap->oap))
7194 {
7195 if (!curbuf->b_p_ma)
7196 EMSG(_(e_modifiable));
7197 else
7198 {
7199#ifdef FEAT_VIRTUALEDIT
7200 if (virtual_active())
7201 coladvance(getviscol());
7202#endif
7203 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7204 }
7205 }
7206}
7207
7208#ifdef FEAT_VREPLACE
7209/*
7210 * "gr".
7211 */
7212 static void
7213nv_vreplace(cap)
7214 cmdarg_T *cap;
7215{
7216# ifdef FEAT_VISUAL
7217 if (VIsual_active)
7218 {
7219 cap->cmdchar = 'r';
7220 cap->nchar = cap->extra_char;
7221 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7222 }
7223 else
7224# endif
7225 if (!checkclearopq(cap->oap))
7226 {
7227 if (!curbuf->b_p_ma)
7228 EMSG(_(e_modifiable));
7229 else
7230 {
7231 if (cap->extra_char == Ctrl_V) /* get another character */
7232 cap->extra_char = get_literal();
7233 stuffcharReadbuff(cap->extra_char);
7234 stuffcharReadbuff(ESC);
7235# ifdef FEAT_VIRTUALEDIT
7236 if (virtual_active())
7237 coladvance(getviscol());
7238# endif
7239 invoke_edit(cap, TRUE, 'v', FALSE);
7240 }
7241 }
7242}
7243#endif
7244
7245/*
7246 * Swap case for "~" command, when it does not work like an operator.
7247 */
7248 static void
7249n_swapchar(cap)
7250 cmdarg_T *cap;
7251{
7252 long n;
7253 pos_T startpos;
7254 int did_change = 0;
7255#ifdef FEAT_NETBEANS_INTG
7256 pos_T pos;
7257 char_u *ptr;
7258 int count;
7259#endif
7260
7261 if (checkclearopq(cap->oap))
7262 return;
7263
7264 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7265 {
7266 clearopbeep(cap->oap);
7267 return;
7268 }
7269
7270 prep_redo_cmd(cap);
7271
7272 if (u_save_cursor() == FAIL)
7273 return;
7274
7275 startpos = curwin->w_cursor;
7276#ifdef FEAT_NETBEANS_INTG
7277 pos = startpos;
7278#endif
7279 for (n = cap->count1; n > 0; --n)
7280 {
7281 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7282 inc_cursor();
7283 if (gchar_cursor() == NUL)
7284 {
7285 if (vim_strchr(p_ww, '~') != NULL
7286 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7287 {
7288#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007289 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 {
7291 if (did_change)
7292 {
7293 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007294 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007295 netbeans_removed(curbuf, pos.lnum, pos.col,
7296 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007298 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 }
7300 pos.col = 0;
7301 pos.lnum++;
7302 }
7303#endif
7304 ++curwin->w_cursor.lnum;
7305 curwin->w_cursor.col = 0;
7306 if (n > 1)
7307 {
7308 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7309 break;
7310 u_clearline();
7311 }
7312 }
7313 else
7314 break;
7315 }
7316 }
7317#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007318 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 ptr = ml_get(pos.lnum);
7321 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007322 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7323 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325#endif
7326
7327
7328 check_cursor();
7329 curwin->w_set_curswant = TRUE;
7330 if (did_change)
7331 {
7332 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7333 0L);
7334 curbuf->b_op_start = startpos;
7335 curbuf->b_op_end = curwin->w_cursor;
7336 if (curbuf->b_op_end.col > 0)
7337 --curbuf->b_op_end.col;
7338 }
7339}
7340
7341/*
7342 * Move cursor to mark.
7343 */
7344 static void
7345nv_cursormark(cap, flag, pos)
7346 cmdarg_T *cap;
7347 int flag;
7348 pos_T *pos;
7349{
7350 if (check_mark(pos) == FAIL)
7351 clearop(cap->oap);
7352 else
7353 {
7354 if (cap->cmdchar == '\''
7355 || cap->cmdchar == '`'
7356 || cap->cmdchar == '['
7357 || cap->cmdchar == ']')
7358 setpcmark();
7359 curwin->w_cursor = *pos;
7360 if (flag)
7361 beginline(BL_WHITE | BL_FIX);
7362 else
7363 check_cursor();
7364 }
7365 cap->oap->motion_type = flag ? MLINE : MCHAR;
7366 if (cap->cmdchar == '`')
7367 cap->oap->use_reg_one = TRUE;
7368 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7369 curwin->w_set_curswant = TRUE;
7370}
7371
7372#ifdef FEAT_VISUAL
7373/*
7374 * Handle commands that are operators in Visual mode.
7375 */
7376 static void
7377v_visop(cap)
7378 cmdarg_T *cap;
7379{
7380 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7381
7382 /* Uppercase means linewise, except in block mode, then "D" deletes till
7383 * the end of the line, and "C" replaces til EOL */
7384 if (isupper(cap->cmdchar))
7385 {
7386 if (VIsual_mode != Ctrl_V)
7387 VIsual_mode = 'V';
7388 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7389 curwin->w_curswant = MAXCOL;
7390 }
7391 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7392 nv_operator(cap);
7393}
7394#endif
7395
7396/*
7397 * "s" and "S" commands.
7398 */
7399 static void
7400nv_subst(cap)
7401 cmdarg_T *cap;
7402{
7403#ifdef FEAT_VISUAL
7404 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7405 {
7406 if (cap->cmdchar == 'S')
7407 VIsual_mode = 'V';
7408 cap->cmdchar = 'c';
7409 nv_operator(cap);
7410 }
7411 else
7412#endif
7413 nv_optrans(cap);
7414}
7415
7416/*
7417 * Abbreviated commands.
7418 */
7419 static void
7420nv_abbrev(cap)
7421 cmdarg_T *cap;
7422{
7423 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7424 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7425
7426#ifdef FEAT_VISUAL
7427 /* in Visual mode these commands are operators */
7428 if (VIsual_active)
7429 v_visop(cap);
7430 else
7431#endif
7432 nv_optrans(cap);
7433}
7434
7435/*
7436 * Translate a command into another command.
7437 */
7438 static void
7439nv_optrans(cap)
7440 cmdarg_T *cap;
7441{
7442 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7443 (char_u *)"d$", (char_u *)"c$",
7444 (char_u *)"cl", (char_u *)"cc",
7445 (char_u *)"yy", (char_u *)":s\r"};
7446 static char_u *str = (char_u *)"xXDCsSY&";
7447
7448 if (!checkclearopq(cap->oap))
7449 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007450 /* In Vi "2D" doesn't delete the next line. Can't translate it
7451 * either, because "2." should also not use the count. */
7452 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7453 {
7454 cap->oap->start = curwin->w_cursor;
7455 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007456#ifdef FEAT_EVAL
7457 set_op_var(OP_DELETE);
7458#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007459 cap->count1 = 1;
7460 nv_dollar(cap);
7461 finish_op = TRUE;
7462 ResetRedobuff();
7463 AppendCharToRedobuff('D');
7464 }
7465 else
7466 {
7467 if (cap->count0)
7468 stuffnumReadbuff(cap->count0);
7469 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 }
7472 cap->opcount = 0;
7473}
7474
7475/*
7476 * "'" and "`" commands. Also for "g'" and "g`".
7477 * cap->arg is TRUE for "'" and "g'".
7478 */
7479 static void
7480nv_gomark(cap)
7481 cmdarg_T *cap;
7482{
7483 pos_T *pos;
7484 int c;
7485#ifdef FEAT_FOLDING
7486 linenr_T lnum = curwin->w_cursor.lnum;
7487 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7488#endif
7489
7490 if (cap->cmdchar == 'g')
7491 c = cap->extra_char;
7492 else
7493 c = cap->nchar;
7494 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7495 if (pos == (pos_T *)-1) /* jumped to other file */
7496 {
7497 if (cap->arg)
7498 {
7499 check_cursor_lnum();
7500 beginline(BL_WHITE | BL_FIX);
7501 }
7502 else
7503 check_cursor();
7504 }
7505 else
7506 nv_cursormark(cap, cap->arg, pos);
7507
7508#ifdef FEAT_VIRTUALEDIT
7509 /* May need to clear the coladd that a mark includes. */
7510 if (!virtual_active())
7511 curwin->w_cursor.coladd = 0;
7512#endif
7513#ifdef FEAT_FOLDING
7514 if (cap->oap->op_type == OP_NOP
7515 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7516 && (fdo_flags & FDO_MARK)
7517 && old_KeyTyped)
7518 foldOpenCursor();
7519#endif
7520}
7521
7522/*
7523 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7524 */
7525 static void
7526nv_pcmark(cap)
7527 cmdarg_T *cap;
7528{
7529#ifdef FEAT_JUMPLIST
7530 pos_T *pos;
7531# ifdef FEAT_FOLDING
7532 linenr_T lnum = curwin->w_cursor.lnum;
7533 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7534# endif
7535
7536 if (!checkclearopq(cap->oap))
7537 {
7538 if (cap->cmdchar == 'g')
7539 pos = movechangelist((int)cap->count1);
7540 else
7541 pos = movemark((int)cap->count1);
7542 if (pos == (pos_T *)-1) /* jump to other file */
7543 {
7544 curwin->w_set_curswant = TRUE;
7545 check_cursor();
7546 }
7547 else if (pos != NULL) /* can jump */
7548 nv_cursormark(cap, FALSE, pos);
7549 else if (cap->cmdchar == 'g')
7550 {
7551 if (curbuf->b_changelistlen == 0)
7552 EMSG(_("E664: changelist is empty"));
7553 else if (cap->count1 < 0)
7554 EMSG(_("E662: At start of changelist"));
7555 else
7556 EMSG(_("E663: At end of changelist"));
7557 }
7558 else
7559 clearopbeep(cap->oap);
7560# ifdef FEAT_FOLDING
7561 if (cap->oap->op_type == OP_NOP
7562 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7563 && (fdo_flags & FDO_MARK)
7564 && old_KeyTyped)
7565 foldOpenCursor();
7566# endif
7567 }
7568#else
7569 clearopbeep(cap->oap);
7570#endif
7571}
7572
7573/*
7574 * Handle '"' command.
7575 */
7576 static void
7577nv_regname(cap)
7578 cmdarg_T *cap;
7579{
7580 if (checkclearop(cap->oap))
7581 return;
7582#ifdef FEAT_EVAL
7583 if (cap->nchar == '=')
7584 cap->nchar = get_expr_register();
7585#endif
7586 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7587 {
7588 cap->oap->regname = cap->nchar;
7589 cap->opcount = cap->count0; /* remember count before '"' */
7590#ifdef FEAT_EVAL
7591 set_reg_var(cap->oap->regname);
7592#endif
7593 }
7594 else
7595 clearopbeep(cap->oap);
7596}
7597
7598#ifdef FEAT_VISUAL
7599/*
7600 * Handle "v", "V" and "CTRL-V" commands.
7601 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7602 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007603 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 */
7605 static void
7606nv_visual(cap)
7607 cmdarg_T *cap;
7608{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007609 if (cap->cmdchar == Ctrl_Q)
7610 cap->cmdchar = Ctrl_V;
7611
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7613 * characterwise, linewise, or blockwise. */
7614 if (cap->oap->op_type != OP_NOP)
7615 {
7616 cap->oap->motion_force = cap->cmdchar;
7617 finish_op = FALSE; /* operator doesn't finish now but later */
7618 return;
7619 }
7620
7621 VIsual_select = cap->arg;
7622 if (VIsual_active) /* change Visual mode */
7623 {
7624 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7625 end_visual_mode();
7626 else /* toggle char/block mode */
7627 { /* or char/line mode */
7628 VIsual_mode = cap->cmdchar;
7629 showmode();
7630 }
7631 redraw_curbuf_later(INVERTED); /* update the inversion */
7632 }
7633 else /* start Visual mode */
7634 {
7635 check_visual_highlight();
7636 if (cap->count0) /* use previously selected part */
7637 {
7638 if (resel_VIsual_mode == NUL) /* there is none */
7639 {
7640 beep_flush();
7641 return;
7642 }
7643 VIsual = curwin->w_cursor;
7644
7645 VIsual_active = TRUE;
7646 VIsual_reselect = TRUE;
7647 if (!cap->arg)
7648 /* start Select mode when 'selectmode' contains "cmd" */
7649 may_start_select('c');
7650#ifdef FEAT_MOUSE
7651 setmouse();
7652#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007653 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 redraw_cmdline = TRUE; /* show visual mode later */
7655 /*
7656 * For V and ^V, we multiply the number of lines even if there
7657 * was only one -- webb
7658 */
7659 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7660 {
7661 curwin->w_cursor.lnum +=
7662 resel_VIsual_line_count * cap->count0 - 1;
7663 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7664 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7665 }
7666 VIsual_mode = resel_VIsual_mode;
7667 if (VIsual_mode == 'v')
7668 {
7669 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007670 {
7671 validate_virtcol();
7672 curwin->w_curswant = curwin->w_virtcol
7673 + resel_VIsual_vcol * cap->count0 - 1;
7674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007676 curwin->w_curswant = resel_VIsual_vcol;
7677 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007679 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
7681 curwin->w_curswant = MAXCOL;
7682 coladvance((colnr_T)MAXCOL);
7683 }
7684 else if (VIsual_mode == Ctrl_V)
7685 {
7686 validate_virtcol();
7687 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007688 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 coladvance(curwin->w_curswant);
7690 }
7691 else
7692 curwin->w_set_curswant = TRUE;
7693 redraw_curbuf_later(INVERTED); /* show the inversion */
7694 }
7695 else
7696 {
7697 if (!cap->arg)
7698 /* start Select mode when 'selectmode' contains "cmd" */
7699 may_start_select('c');
7700 n_start_visual_mode(cap->cmdchar);
7701 }
7702 }
7703}
7704
7705/*
7706 * Start selection for Shift-movement keys.
7707 */
7708 void
7709start_selection()
7710{
7711 /* if 'selectmode' contains "key", start Select mode */
7712 may_start_select('k');
7713 n_start_visual_mode('v');
7714}
7715
7716/*
7717 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7718 */
7719 void
7720may_start_select(c)
7721 int c;
7722{
7723 VIsual_select = (stuff_empty() && typebuf_typed()
7724 && (vim_strchr(p_slm, c) != NULL));
7725}
7726
7727/*
7728 * Start Visual mode "c".
7729 * Should set VIsual_select before calling this.
7730 */
7731 static void
7732n_start_visual_mode(c)
7733 int c;
7734{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007735#ifdef FEAT_CONCEAL
7736 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007737 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007738#endif
7739
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 VIsual_mode = c;
7741 VIsual_active = TRUE;
7742 VIsual_reselect = TRUE;
7743#ifdef FEAT_VIRTUALEDIT
7744 /* Corner case: the 0 position in a tab may change when going into
7745 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7746 */
7747 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7748 coladvance(curwin->w_virtcol);
7749#endif
7750 VIsual = curwin->w_cursor;
7751
7752#ifdef FEAT_FOLDING
7753 foldAdjustVisual();
7754#endif
7755
7756#ifdef FEAT_MOUSE
7757 setmouse();
7758#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007759#ifdef FEAT_CONCEAL
7760 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007761 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007762#endif
7763
Bram Moolenaar09df3122006-01-23 22:23:09 +00007764 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 redraw_cmdline = TRUE; /* show visual mode later */
7766#ifdef FEAT_CLIPBOARD
7767 /* Make sure the clipboard gets updated. Needed because start and
7768 * end may still be the same, and the selection needs to be owned */
7769 clip_star.vmode = NUL;
7770#endif
7771
7772 /* Only need to redraw this line, unless still need to redraw an old
7773 * Visual area (when 'lazyredraw' is set). */
7774 if (curwin->w_redr_type < INVERTED)
7775 {
7776 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7777 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7778 }
7779}
7780
7781#endif /* FEAT_VISUAL */
7782
7783/*
7784 * CTRL-W: Window commands
7785 */
7786 static void
7787nv_window(cap)
7788 cmdarg_T *cap;
7789{
7790#ifdef FEAT_WINDOWS
7791 if (!checkclearop(cap->oap))
7792 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7793#else
7794 (void)checkclearop(cap->oap);
7795#endif
7796}
7797
7798/*
7799 * CTRL-Z: Suspend
7800 */
7801 static void
7802nv_suspend(cap)
7803 cmdarg_T *cap;
7804{
7805 clearop(cap->oap);
7806#ifdef FEAT_VISUAL
7807 if (VIsual_active)
7808 end_visual_mode(); /* stop Visual mode */
7809#endif
7810 do_cmdline_cmd((char_u *)"st");
7811}
7812
7813/*
7814 * Commands starting with "g".
7815 */
7816 static void
7817nv_g_cmd(cap)
7818 cmdarg_T *cap;
7819{
7820 oparg_T *oap = cap->oap;
7821#ifdef FEAT_VISUAL
7822 pos_T tpos;
7823#endif
7824 int i;
7825 int flag = FALSE;
7826
7827 switch (cap->nchar)
7828 {
7829#ifdef MEM_PROFILE
7830 /*
7831 * "g^A": dump log of used memory.
7832 */
7833 case Ctrl_A:
7834 vim_mem_profile_dump();
7835 break;
7836#endif
7837
7838#ifdef FEAT_VREPLACE
7839 /*
7840 * "gR": Enter virtual replace mode.
7841 */
7842 case 'R':
7843 cap->arg = TRUE;
7844 nv_Replace(cap);
7845 break;
7846
7847 case 'r':
7848 nv_vreplace(cap);
7849 break;
7850#endif
7851
7852 case '&':
7853 do_cmdline_cmd((char_u *)"%s//~/&");
7854 break;
7855
7856#ifdef FEAT_VISUAL
7857 /*
7858 * "gv": Reselect the previous Visual area. If Visual already active,
7859 * exchange previous and current Visual area.
7860 */
7861 case 'v':
7862 if (checkclearop(oap))
7863 break;
7864
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007865 if ( curbuf->b_visual.vi_start.lnum == 0
7866 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7867 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 beep_flush();
7869 else
7870 {
7871 /* set w_cursor to the start of the Visual area, tpos to the end */
7872 if (VIsual_active)
7873 {
7874 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007875 VIsual_mode = curbuf->b_visual.vi_mode;
7876 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877# ifdef FEAT_EVAL
7878 curbuf->b_visual_mode_eval = i;
7879# endif
7880 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007881 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7882 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007884 tpos = curbuf->b_visual.vi_end;
7885 curbuf->b_visual.vi_end = curwin->w_cursor;
7886 curwin->w_cursor = curbuf->b_visual.vi_start;
7887 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889 else
7890 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007891 VIsual_mode = curbuf->b_visual.vi_mode;
7892 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7893 tpos = curbuf->b_visual.vi_end;
7894 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896
7897 VIsual_active = TRUE;
7898 VIsual_reselect = TRUE;
7899
7900 /* Set Visual to the start and w_cursor to the end of the Visual
7901 * area. Make sure they are on an existing character. */
7902 check_cursor();
7903 VIsual = curwin->w_cursor;
7904 curwin->w_cursor = tpos;
7905 check_cursor();
7906 update_topline();
7907 /*
7908 * When called from normal "g" command: start Select mode when
7909 * 'selectmode' contains "cmd". When called for K_SELECT, always
7910 * start Select mode.
7911 */
7912 if (cap->arg)
7913 VIsual_select = TRUE;
7914 else
7915 may_start_select('c');
7916#ifdef FEAT_MOUSE
7917 setmouse();
7918#endif
7919#ifdef FEAT_CLIPBOARD
7920 /* Make sure the clipboard gets updated. Needed because start and
7921 * end are still the same, and the selection needs to be owned */
7922 clip_star.vmode = NUL;
7923#endif
7924 redraw_curbuf_later(INVERTED);
7925 showmode();
7926 }
7927 break;
7928 /*
7929 * "gV": Don't reselect the previous Visual area after a Select mode
7930 * mapping of menu.
7931 */
7932 case 'V':
7933 VIsual_reselect = FALSE;
7934 break;
7935
7936 /*
7937 * "gh": start Select mode.
7938 * "gH": start Select line mode.
7939 * "g^H": start Select block mode.
7940 */
7941 case K_BS:
7942 cap->nchar = Ctrl_H;
7943 /* FALLTHROUGH */
7944 case 'h':
7945 case 'H':
7946 case Ctrl_H:
7947# ifdef EBCDIC
7948 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7949 if (cap->nchar == Ctrl_H)
7950 cap->cmdchar = Ctrl_V;
7951 else
7952# endif
7953 cap->cmdchar = cap->nchar + ('v' - 'h');
7954 cap->arg = TRUE;
7955 nv_visual(cap);
7956 break;
7957#endif /* FEAT_VISUAL */
7958
7959 /*
7960 * "gj" and "gk" two new funny movement keys -- up and down
7961 * movement based on *screen* line rather than *file* line.
7962 */
7963 case 'j':
7964 case K_DOWN:
7965 /* with 'nowrap' it works just like the normal "j" command; also when
7966 * in a closed fold */
7967 if (!curwin->w_p_wrap
7968#ifdef FEAT_FOLDING
7969 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7970#endif
7971 )
7972 {
7973 oap->motion_type = MLINE;
7974 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7975 }
7976 else
7977 i = nv_screengo(oap, FORWARD, cap->count1);
7978 if (i == FAIL)
7979 clearopbeep(oap);
7980 break;
7981
7982 case 'k':
7983 case K_UP:
7984 /* with 'nowrap' it works just like the normal "k" command; also when
7985 * in a closed fold */
7986 if (!curwin->w_p_wrap
7987#ifdef FEAT_FOLDING
7988 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7989#endif
7990 )
7991 {
7992 oap->motion_type = MLINE;
7993 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7994 }
7995 else
7996 i = nv_screengo(oap, BACKWARD, cap->count1);
7997 if (i == FAIL)
7998 clearopbeep(oap);
7999 break;
8000
8001 /*
8002 * "gJ": join two lines without inserting a space.
8003 */
8004 case 'J':
8005 nv_join(cap);
8006 break;
8007
8008 /*
8009 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8010 * "gm": middle of "g0" and "g$".
8011 */
8012 case '^':
8013 flag = TRUE;
8014 /* FALLTHROUGH */
8015
8016 case '0':
8017 case 'm':
8018 case K_HOME:
8019 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 oap->motion_type = MCHAR;
8021 oap->inclusive = FALSE;
8022 if (curwin->w_p_wrap
8023#ifdef FEAT_VERTSPLIT
8024 && curwin->w_width != 0
8025#endif
8026 )
8027 {
8028 int width1 = W_WIDTH(curwin) - curwin_col_off();
8029 int width2 = width1 + curwin_col_off2();
8030
8031 validate_virtcol();
8032 i = 0;
8033 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8034 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8035 }
8036 else
8037 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008038 /* Go to the middle of the screen line. When 'number' or
8039 * 'relativenumber' is on and lines are wrapping the middle can be more
8040 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (cap->nchar == 'm')
8042 i += (W_WIDTH(curwin) - curwin_col_off()
8043 + ((curwin->w_p_wrap && i > 0)
8044 ? curwin_col_off2() : 0)) / 2;
8045 coladvance((colnr_T)i);
8046 if (flag)
8047 {
8048 do
8049 i = gchar_cursor();
8050 while (vim_iswhite(i) && oneright() == OK);
8051 }
8052 curwin->w_set_curswant = TRUE;
8053 break;
8054
8055 case '_':
8056 /* "g_": to the last non-blank character in the line or <count> lines
8057 * downward. */
8058 cap->oap->motion_type = MCHAR;
8059 cap->oap->inclusive = TRUE;
8060 curwin->w_curswant = MAXCOL;
8061 if (cursor_down((long)(cap->count1 - 1),
8062 cap->oap->op_type == OP_NOP) == FAIL)
8063 clearopbeep(cap->oap);
8064 else
8065 {
8066 char_u *ptr = ml_get_curline();
8067
8068 /* In Visual mode we may end up after the line. */
8069 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8070 --curwin->w_cursor.col;
8071
8072 /* Decrease the cursor column until it's on a non-blank. */
8073 while (curwin->w_cursor.col > 0
8074 && vim_iswhite(ptr[curwin->w_cursor.col]))
8075 --curwin->w_cursor.col;
8076 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008077#ifdef FEAT_VISUAL
8078 adjust_for_sel(cap);
8079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
8081 break;
8082
8083 case '$':
8084 case K_END:
8085 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 {
8087 int col_off = curwin_col_off();
8088
8089 oap->motion_type = MCHAR;
8090 oap->inclusive = TRUE;
8091 if (curwin->w_p_wrap
8092#ifdef FEAT_VERTSPLIT
8093 && curwin->w_width != 0
8094#endif
8095 )
8096 {
8097 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8098 if (cap->count1 == 1)
8099 {
8100 int width1 = W_WIDTH(curwin) - col_off;
8101 int width2 = width1 + curwin_col_off2();
8102
8103 validate_virtcol();
8104 i = width1 - 1;
8105 if (curwin->w_virtcol >= (colnr_T)width1)
8106 i += ((curwin->w_virtcol - width1) / width2 + 1)
8107 * width2;
8108 coladvance((colnr_T)i);
8109#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8110 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8111 {
8112 /*
8113 * Check for landing on a character that got split at
8114 * the end of the line. We do not want to advance to
8115 * the next screen line.
8116 */
8117 validate_virtcol();
8118 if (curwin->w_virtcol > (colnr_T)i)
8119 --curwin->w_cursor.col;
8120 }
8121#endif
8122 }
8123 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8124 clearopbeep(oap);
8125 }
8126 else
8127 {
8128 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8129 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008130
8131 /* Make sure we stick in this column. */
8132 validate_virtcol();
8133 curwin->w_curswant = curwin->w_virtcol;
8134 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 }
8136 }
8137 break;
8138
8139 /*
8140 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8141 */
8142 case '*':
8143 case '#':
8144#if POUND != '#'
8145 case POUND: /* pound sign (sometimes equal to '#') */
8146#endif
8147 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8148 case ']': /* :tselect for current identifier */
8149 nv_ident(cap);
8150 break;
8151
8152 /*
8153 * ge and gE: go back to end of word
8154 */
8155 case 'e':
8156 case 'E':
8157 oap->motion_type = MCHAR;
8158 curwin->w_set_curswant = TRUE;
8159 oap->inclusive = TRUE;
8160 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8161 clearopbeep(oap);
8162 break;
8163
8164 /*
8165 * "g CTRL-G": display info about cursor position
8166 */
8167 case Ctrl_G:
8168 cursor_pos_info();
8169 break;
8170
8171 /*
8172 * "gi": start Insert at the last position.
8173 */
8174 case 'i':
8175 if (curbuf->b_last_insert.lnum != 0)
8176 {
8177 curwin->w_cursor = curbuf->b_last_insert;
8178 check_cursor_lnum();
8179 i = (int)STRLEN(ml_get_curline());
8180 if (curwin->w_cursor.col > (colnr_T)i)
8181 {
8182#ifdef FEAT_VIRTUALEDIT
8183 if (virtual_active())
8184 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8185#endif
8186 curwin->w_cursor.col = i;
8187 }
8188 }
8189 cap->cmdchar = 'i';
8190 nv_edit(cap);
8191 break;
8192
8193 /*
8194 * "gI": Start insert in column 1.
8195 */
8196 case 'I':
8197 beginline(0);
8198 if (!checkclearopq(oap))
8199 invoke_edit(cap, FALSE, 'g', FALSE);
8200 break;
8201
8202#ifdef FEAT_SEARCHPATH
8203 /*
8204 * "gf": goto file, edit file under cursor
8205 * "]f" and "[f": can also be used.
8206 */
8207 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008208 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 nv_gotofile(cap);
8210 break;
8211#endif
8212
8213 /* "g'm" and "g`m": jump to mark without setting pcmark */
8214 case '\'':
8215 cap->arg = TRUE;
8216 /*FALLTHROUGH*/
8217 case '`':
8218 nv_gomark(cap);
8219 break;
8220
8221 /*
8222 * "gs": Goto sleep.
8223 */
8224 case 's':
8225 do_sleep(cap->count1 * 1000L);
8226 break;
8227
8228 /*
8229 * "ga": Display the ascii value of the character under the
8230 * cursor. It is displayed in decimal, hex, and octal. -- webb
8231 */
8232 case 'a':
8233 do_ascii(NULL);
8234 break;
8235
8236#ifdef FEAT_MBYTE
8237 /*
8238 * "g8": Display the bytes used for the UTF-8 character under the
8239 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008240 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 */
8242 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008243 if (cap->count0 == 8)
8244 utf_find_illegal();
8245 else
8246 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 break;
8248#endif
8249
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008250 case '<':
8251 show_sb_text();
8252 break;
8253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 /*
8255 * "gg": Goto the first line in file. With a count it goes to
8256 * that line number like for "G". -- webb
8257 */
8258 case 'g':
8259 cap->arg = FALSE;
8260 nv_goto(cap);
8261 break;
8262
8263 /*
8264 * Two-character operators:
8265 * "gq" Format text
8266 * "gw" Format text and keep cursor position
8267 * "g~" Toggle the case of the text.
8268 * "gu" Change text to lower case.
8269 * "gU" Change text to upper case.
8270 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008271 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 */
8273 case 'q':
8274 case 'w':
8275 oap->cursor_start = curwin->w_cursor;
8276 /*FALLTHROUGH*/
8277 case '~':
8278 case 'u':
8279 case 'U':
8280 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008281 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 nv_operator(cap);
8283 break;
8284
8285 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008286 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 * current function
8288 * "gD": idem, but in the current file.
8289 */
8290 case 'd':
8291 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008292 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 break;
8294
8295#ifdef FEAT_MOUSE
8296 /*
8297 * g<*Mouse> : <C-*mouse>
8298 */
8299 case K_MIDDLEMOUSE:
8300 case K_MIDDLEDRAG:
8301 case K_MIDDLERELEASE:
8302 case K_LEFTMOUSE:
8303 case K_LEFTDRAG:
8304 case K_LEFTRELEASE:
8305 case K_RIGHTMOUSE:
8306 case K_RIGHTDRAG:
8307 case K_RIGHTRELEASE:
8308 case K_X1MOUSE:
8309 case K_X1DRAG:
8310 case K_X1RELEASE:
8311 case K_X2MOUSE:
8312 case K_X2DRAG:
8313 case K_X2RELEASE:
8314 mod_mask = MOD_MASK_CTRL;
8315 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8316 break;
8317#endif
8318
8319 case K_IGNORE:
8320 break;
8321
8322 /*
8323 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8324 */
8325 case 'p':
8326 case 'P':
8327 nv_put(cap);
8328 break;
8329
8330#ifdef FEAT_BYTEOFF
8331 /* "go": goto byte count from start of buffer */
8332 case 'o':
8333 goto_byte(cap->count0);
8334 break;
8335#endif
8336
8337 /* "gQ": improved Ex mode */
8338 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008339 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
8341 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008342 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 break;
8344 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 if (!checkclearopq(oap))
8347 do_exmode(TRUE);
8348 break;
8349
8350#ifdef FEAT_JUMPLIST
8351 case ',':
8352 nv_pcmark(cap);
8353 break;
8354
8355 case ';':
8356 cap->count1 = -cap->count1;
8357 nv_pcmark(cap);
8358 break;
8359#endif
8360
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008361#ifdef FEAT_WINDOWS
8362 case 't':
8363 goto_tabpage((int)cap->count0);
8364 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008365 case 'T':
8366 goto_tabpage(-(int)cap->count1);
8367 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008368#endif
8369
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008370 case '+':
8371 case '-': /* "g+" and "g-": undo or redo along the timeline */
8372 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008373 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008374 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008375 break;
8376
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 default:
8378 clearopbeep(oap);
8379 break;
8380 }
8381}
8382
8383/*
8384 * Handle "o" and "O" commands.
8385 */
8386 static void
8387n_opencmd(cap)
8388 cmdarg_T *cap;
8389{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008390#ifdef FEAT_CONCEAL
8391 linenr_T oldline = curwin->w_cursor.lnum;
8392#endif
8393
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 if (!checkclearopq(cap->oap))
8395 {
8396#ifdef FEAT_FOLDING
8397 if (cap->cmdchar == 'O')
8398 /* Open above the first line of a folded sequence of lines */
8399 (void)hasFolding(curwin->w_cursor.lnum,
8400 &curwin->w_cursor.lnum, NULL);
8401 else
8402 /* Open below the last line of a folded sequence of lines */
8403 (void)hasFolding(curwin->w_cursor.lnum,
8404 NULL, &curwin->w_cursor.lnum);
8405#endif
8406 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8407 (cap->cmdchar == 'O' ? 1 : 0)),
8408 (linenr_T)(curwin->w_cursor.lnum +
8409 (cap->cmdchar == 'o' ? 1 : 0))
8410 ) == OK
8411 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8412#ifdef FEAT_COMMENTS
8413 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8414#endif
8415 0, 0))
8416 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008417#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008418 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008419 update_single_line(curwin, oldline);
8420#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008421 /* When '#' is in 'cpoptions' ignore the count. */
8422 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8423 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8425 }
8426 }
8427}
8428
8429/*
8430 * "." command: redo last change.
8431 */
8432 static void
8433nv_dot(cap)
8434 cmdarg_T *cap;
8435{
8436 if (!checkclearopq(cap->oap))
8437 {
8438 /*
8439 * If "restart_edit" is TRUE, the last but one command is repeated
8440 * instead of the last command (inserting text). This is used for
8441 * CTRL-O <.> in insert mode.
8442 */
8443 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8444 clearopbeep(cap->oap);
8445 }
8446}
8447
8448/*
8449 * CTRL-R: undo undo
8450 */
8451 static void
8452nv_redo(cap)
8453 cmdarg_T *cap;
8454{
8455 if (!checkclearopq(cap->oap))
8456 {
8457 u_redo((int)cap->count1);
8458 curwin->w_set_curswant = TRUE;
8459 }
8460}
8461
8462/*
8463 * Handle "U" command.
8464 */
8465 static void
8466nv_Undo(cap)
8467 cmdarg_T *cap;
8468{
8469 /* In Visual mode and typing "gUU" triggers an operator */
8470 if (cap->oap->op_type == OP_UPPER
8471#ifdef FEAT_VISUAL
8472 || VIsual_active
8473#endif
8474 )
8475 {
8476 /* translate "gUU" to "gUgU" */
8477 cap->cmdchar = 'g';
8478 cap->nchar = 'U';
8479 nv_operator(cap);
8480 }
8481 else if (!checkclearopq(cap->oap))
8482 {
8483 u_undoline();
8484 curwin->w_set_curswant = TRUE;
8485 }
8486}
8487
8488/*
8489 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8490 * single character.
8491 */
8492 static void
8493nv_tilde(cap)
8494 cmdarg_T *cap;
8495{
8496 if (!p_to
8497#ifdef FEAT_VISUAL
8498 && !VIsual_active
8499#endif
8500 && cap->oap->op_type != OP_TILDE)
8501 n_swapchar(cap);
8502 else
8503 nv_operator(cap);
8504}
8505
8506/*
8507 * Handle an operator command.
8508 * The actual work is done by do_pending_operator().
8509 */
8510 static void
8511nv_operator(cap)
8512 cmdarg_T *cap;
8513{
8514 int op_type;
8515
8516 op_type = get_op_type(cap->cmdchar, cap->nchar);
8517
8518 if (op_type == cap->oap->op_type) /* double operator works on lines */
8519 nv_lineop(cap);
8520 else if (!checkclearop(cap->oap))
8521 {
8522 cap->oap->start = curwin->w_cursor;
8523 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008524#ifdef FEAT_EVAL
8525 set_op_var(op_type);
8526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 }
8528}
8529
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008530#ifdef FEAT_EVAL
8531/*
8532 * Set v:operator to the characters for "optype".
8533 */
8534 static void
8535set_op_var(optype)
8536 int optype;
8537{
8538 char_u opchars[3];
8539
8540 if (optype == OP_NOP)
8541 set_vim_var_string(VV_OP, NULL, 0);
8542 else
8543 {
8544 opchars[0] = get_op_char(optype);
8545 opchars[1] = get_extra_op_char(optype);
8546 opchars[2] = NUL;
8547 set_vim_var_string(VV_OP, opchars, -1);
8548 }
8549}
8550#endif
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552/*
8553 * Handle linewise operator "dd", "yy", etc.
8554 *
8555 * "_" is is a strange motion command that helps make operators more logical.
8556 * It is actually implemented, but not documented in the real Vi. This motion
8557 * command actually refers to "the current line". Commands like "dd" and "yy"
8558 * are really an alternate form of "d_" and "y_". It does accept a count, so
8559 * "d3_" works to delete 3 lines.
8560 */
8561 static void
8562nv_lineop(cap)
8563 cmdarg_T *cap;
8564{
8565 cap->oap->motion_type = MLINE;
8566 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8567 clearopbeep(cap->oap);
8568 else if ( cap->oap->op_type == OP_DELETE
8569 || cap->oap->op_type == OP_LSHIFT
8570 || cap->oap->op_type == OP_RSHIFT)
8571 beginline(BL_SOL | BL_FIX);
8572 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8573 beginline(BL_WHITE | BL_FIX);
8574}
8575
8576/*
8577 * <Home> command.
8578 */
8579 static void
8580nv_home(cap)
8581 cmdarg_T *cap;
8582{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008583 /* CTRL-HOME is like "gg" */
8584 if (mod_mask & MOD_MASK_CTRL)
8585 nv_goto(cap);
8586 else
8587 {
8588 cap->count0 = 1;
8589 nv_pipe(cap);
8590 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008591 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8592 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593}
8594
8595/*
8596 * "|" command.
8597 */
8598 static void
8599nv_pipe(cap)
8600 cmdarg_T *cap;
8601{
8602 cap->oap->motion_type = MCHAR;
8603 cap->oap->inclusive = FALSE;
8604 beginline(0);
8605 if (cap->count0 > 0)
8606 {
8607 coladvance((colnr_T)(cap->count0 - 1));
8608 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8609 }
8610 else
8611 curwin->w_curswant = 0;
8612 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008613 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 curwin->w_set_curswant = FALSE;
8615}
8616
8617/*
8618 * Handle back-word command "b" and "B".
8619 * cap->arg is 1 for "B"
8620 */
8621 static void
8622nv_bck_word(cap)
8623 cmdarg_T *cap;
8624{
8625 cap->oap->motion_type = MCHAR;
8626 cap->oap->inclusive = FALSE;
8627 curwin->w_set_curswant = TRUE;
8628 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8629 clearopbeep(cap->oap);
8630#ifdef FEAT_FOLDING
8631 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8632 foldOpenCursor();
8633#endif
8634}
8635
8636/*
8637 * Handle word motion commands "e", "E", "w" and "W".
8638 * cap->arg is TRUE for "E" and "W".
8639 */
8640 static void
8641nv_wordcmd(cap)
8642 cmdarg_T *cap;
8643{
8644 int n;
8645 int word_end;
8646 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008647 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 /*
8650 * Set inclusive for the "E" and "e" command.
8651 */
8652 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8653 word_end = TRUE;
8654 else
8655 word_end = FALSE;
8656 cap->oap->inclusive = word_end;
8657
8658 /*
8659 * "cw" and "cW" are a special case.
8660 */
8661 if (!word_end && cap->oap->op_type == OP_CHANGE)
8662 {
8663 n = gchar_cursor();
8664 if (n != NUL) /* not an empty line */
8665 {
8666 if (vim_iswhite(n))
8667 {
8668 /*
8669 * Reproduce a funny Vi behaviour: "cw" on a blank only
8670 * changes one character, not all blanks until the start of
8671 * the next word. Only do this when the 'w' flag is included
8672 * in 'cpoptions'.
8673 */
8674 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8675 {
8676 cap->oap->inclusive = TRUE;
8677 cap->oap->motion_type = MCHAR;
8678 return;
8679 }
8680 }
8681 else
8682 {
8683 /*
8684 * This is a little strange. To match what the real Vi does,
8685 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8686 * that we are not on a space or a TAB. This seems impolite
8687 * at first, but it's really more what we mean when we say
8688 * 'cw'.
8689 * Another strangeness: When standing on the end of a word
8690 * "ce" will change until the end of the next wordt, but "cw"
8691 * will change only one character! This is done by setting
8692 * flag.
8693 */
8694 cap->oap->inclusive = TRUE;
8695 word_end = TRUE;
8696 flag = TRUE;
8697 }
8698 }
8699 }
8700
8701 cap->oap->motion_type = MCHAR;
8702 curwin->w_set_curswant = TRUE;
8703 if (word_end)
8704 n = end_word(cap->count1, cap->arg, flag, FALSE);
8705 else
8706 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8707
Bram Moolenaardfefb982008-04-01 10:06:39 +00008708 /* Don't leave the cursor on the NUL past the end of line. Unless we
8709 * didn't move it forward. */
8710 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008711 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 if (n == FAIL && cap->oap->op_type == OP_NOP)
8714 clearopbeep(cap->oap);
8715 else
8716 {
8717#ifdef FEAT_VISUAL
8718 adjust_for_sel(cap);
8719#endif
8720#ifdef FEAT_FOLDING
8721 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8722 foldOpenCursor();
8723#endif
8724 }
8725}
8726
8727/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008728 * Used after a movement command: If the cursor ends up on the NUL after the
8729 * end of the line, may move it back to the last character and make the motion
8730 * inclusive.
8731 */
8732 static void
8733adjust_cursor(oap)
8734 oparg_T *oap;
8735{
8736 /* The cursor cannot remain on the NUL when:
8737 * - the column is > 0
8738 * - not in Visual mode or 'selection' is "o"
8739 * - 'virtualedit' is not "all" and not "onemore".
8740 */
8741 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8742#ifdef FEAT_VISUAL
8743 && (!VIsual_active || *p_sel == 'o')
8744#endif
8745#ifdef FEAT_VIRTUALEDIT
8746 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8747#endif
8748 )
8749 {
8750 --curwin->w_cursor.col;
8751#ifdef FEAT_MBYTE
8752 /* prevent cursor from moving on the trail byte */
8753 if (has_mbyte)
8754 mb_adjust_cursor();
8755#endif
8756 oap->inclusive = TRUE;
8757 }
8758}
8759
8760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 * "0" and "^" commands.
8762 * cap->arg is the argument for beginline().
8763 */
8764 static void
8765nv_beginline(cap)
8766 cmdarg_T *cap;
8767{
8768 cap->oap->motion_type = MCHAR;
8769 cap->oap->inclusive = FALSE;
8770 beginline(cap->arg);
8771#ifdef FEAT_FOLDING
8772 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8773 foldOpenCursor();
8774#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008775 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8776 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777}
8778
8779#ifdef FEAT_VISUAL
8780/*
8781 * In exclusive Visual mode, may include the last character.
8782 */
8783 static void
8784adjust_for_sel(cap)
8785 cmdarg_T *cap;
8786{
8787 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8788 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8789 {
8790# ifdef FEAT_MBYTE
8791 if (has_mbyte)
8792 inc_cursor();
8793 else
8794# endif
8795 ++curwin->w_cursor.col;
8796 cap->oap->inclusive = FALSE;
8797 }
8798}
8799
8800/*
8801 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8802 * Should check VIsual_mode before calling this.
8803 * Returns TRUE when backed up to the previous line.
8804 */
8805 static int
8806unadjust_for_sel()
8807{
8808 pos_T *pp;
8809
8810 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8811 {
8812 if (lt(VIsual, curwin->w_cursor))
8813 pp = &curwin->w_cursor;
8814 else
8815 pp = &VIsual;
8816#ifdef FEAT_VIRTUALEDIT
8817 if (pp->coladd > 0)
8818 --pp->coladd;
8819 else
8820#endif
8821 if (pp->col > 0)
8822 {
8823 --pp->col;
8824#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008825 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826#endif
8827 }
8828 else if (pp->lnum > 1)
8829 {
8830 --pp->lnum;
8831 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8832 return TRUE;
8833 }
8834 }
8835 return FALSE;
8836}
8837
8838/*
8839 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8840 */
8841 static void
8842nv_select(cap)
8843 cmdarg_T *cap;
8844{
8845 if (VIsual_active)
8846 VIsual_select = TRUE;
8847 else if (VIsual_reselect)
8848 {
8849 cap->nchar = 'v'; /* fake "gv" command */
8850 cap->arg = TRUE;
8851 nv_g_cmd(cap);
8852 }
8853}
8854
8855#endif
8856
8857/*
8858 * "G", "gg", CTRL-END, CTRL-HOME.
8859 * cap->arg is TRUE for "G".
8860 */
8861 static void
8862nv_goto(cap)
8863 cmdarg_T *cap;
8864{
8865 linenr_T lnum;
8866
8867 if (cap->arg)
8868 lnum = curbuf->b_ml.ml_line_count;
8869 else
8870 lnum = 1L;
8871 cap->oap->motion_type = MLINE;
8872 setpcmark();
8873
8874 /* When a count is given, use it instead of the default lnum */
8875 if (cap->count0 != 0)
8876 lnum = cap->count0;
8877 if (lnum < 1L)
8878 lnum = 1L;
8879 else if (lnum > curbuf->b_ml.ml_line_count)
8880 lnum = curbuf->b_ml.ml_line_count;
8881 curwin->w_cursor.lnum = lnum;
8882 beginline(BL_SOL | BL_FIX);
8883#ifdef FEAT_FOLDING
8884 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8885 foldOpenCursor();
8886#endif
8887}
8888
8889/*
8890 * CTRL-\ in Normal mode.
8891 */
8892 static void
8893nv_normal(cap)
8894 cmdarg_T *cap;
8895{
8896 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8897 {
8898 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008899 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 clear_cmdline = TRUE; /* unshow mode later */
8901 restart_edit = 0;
8902#ifdef FEAT_CMDWIN
8903 if (cmdwin_type != 0)
8904 cmdwin_result = Ctrl_C;
8905#endif
8906#ifdef FEAT_VISUAL
8907 if (VIsual_active)
8908 {
8909 end_visual_mode(); /* stop Visual */
8910 redraw_curbuf_later(INVERTED);
8911 }
8912#endif
8913 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8914 if (cap->nchar == Ctrl_G && p_im)
8915 restart_edit = 'a';
8916 }
8917 else
8918 clearopbeep(cap->oap);
8919}
8920
8921/*
8922 * ESC in Normal mode: beep, but don't flush buffers.
8923 * Don't even beep if we are canceling a command.
8924 */
8925 static void
8926nv_esc(cap)
8927 cmdarg_T *cap;
8928{
8929 int no_reason;
8930
8931 no_reason = (cap->oap->op_type == OP_NOP
8932 && cap->opcount == 0
8933 && cap->count0 == 0
8934 && cap->oap->regname == 0
8935 && !p_im);
8936
8937 if (cap->arg) /* TRUE for CTRL-C */
8938 {
8939 if (restart_edit == 0
8940#ifdef FEAT_CMDWIN
8941 && cmdwin_type == 0
8942#endif
8943#ifdef FEAT_VISUAL
8944 && !VIsual_active
8945#endif
8946 && no_reason)
8947 MSG(_("Type :quit<Enter> to exit Vim"));
8948
8949 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8950 * set again below when halfway a mapping. */
8951 if (!p_im)
8952 restart_edit = 0;
8953#ifdef FEAT_CMDWIN
8954 if (cmdwin_type != 0)
8955 {
8956 cmdwin_result = K_IGNORE;
8957 got_int = FALSE; /* don't stop executing autocommands et al. */
8958 return;
8959 }
8960#endif
8961 }
8962
8963#ifdef FEAT_VISUAL
8964 if (VIsual_active)
8965 {
8966 end_visual_mode(); /* stop Visual */
8967 check_cursor_col(); /* make sure cursor is not beyond EOL */
8968 curwin->w_set_curswant = TRUE;
8969 redraw_curbuf_later(INVERTED);
8970 }
8971 else
8972#endif
8973 if (no_reason)
8974 vim_beep();
8975 clearop(cap->oap);
8976
8977 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8978 * set return to Insert mode afterwards. */
8979 if (restart_edit == 0 && goto_im()
8980#ifdef FEAT_EX_EXTRA
8981 && ex_normal_busy == 0
8982#endif
8983 )
8984 restart_edit = 'a';
8985}
8986
8987/*
8988 * Handle "A", "a", "I", "i" and <Insert> commands.
8989 */
8990 static void
8991nv_edit(cap)
8992 cmdarg_T *cap;
8993{
8994 /* <Insert> is equal to "i" */
8995 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8996 cap->cmdchar = 'i';
8997
8998#ifdef FEAT_VISUAL
8999 /* in Visual mode "A" and "I" are an operator */
9000 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9001 v_visop(cap);
9002
9003 /* in Visual mode and after an operator "a" and "i" are for text objects */
9004 else
9005#endif
9006 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9007 && (cap->oap->op_type != OP_NOP
9008#ifdef FEAT_VISUAL
9009 || VIsual_active
9010#endif
9011 ))
9012 {
9013#ifdef FEAT_TEXTOBJ
9014 nv_object(cap);
9015#else
9016 clearopbeep(cap->oap);
9017#endif
9018 }
9019 else if (!curbuf->b_p_ma && !p_im)
9020 {
9021 /* Only give this error when 'insertmode' is off. */
9022 EMSG(_(e_modifiable));
9023 clearop(cap->oap);
9024 }
9025 else if (!checkclearopq(cap->oap))
9026 {
9027 switch (cap->cmdchar)
9028 {
9029 case 'A': /* "A"ppend after the line */
9030 curwin->w_set_curswant = TRUE;
9031#ifdef FEAT_VIRTUALEDIT
9032 if (ve_flags == VE_ALL)
9033 {
9034 int save_State = State;
9035
9036 /* Pretent Insert mode here to allow the cursor on the
9037 * character past the end of the line */
9038 State = INSERT;
9039 coladvance((colnr_T)MAXCOL);
9040 State = save_State;
9041 }
9042 else
9043#endif
9044 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9045 break;
9046
9047 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009048 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9049 beginline(BL_WHITE);
9050 else
9051 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 break;
9053
9054 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9055#ifdef FEAT_VIRTUALEDIT
9056 /* increment coladd when in virtual space, increment the
9057 * column otherwise, also to append after an unprintable char */
9058 if (virtual_active()
9059 && (curwin->w_cursor.coladd > 0
9060 || *ml_get_cursor() == NUL
9061 || *ml_get_cursor() == TAB))
9062 curwin->w_cursor.coladd++;
9063 else
9064#endif
9065 if (*ml_get_cursor() != NUL)
9066 inc_cursor();
9067 break;
9068 }
9069
9070#ifdef FEAT_VIRTUALEDIT
9071 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9072 {
9073 int save_State = State;
9074
9075 /* Pretent Insert mode here to allow the cursor on the
9076 * character past the end of the line */
9077 State = INSERT;
9078 coladvance(getviscol());
9079 State = save_State;
9080 }
9081#endif
9082
9083 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9084 }
9085}
9086
9087/*
9088 * Invoke edit() and take care of "restart_edit" and the return value.
9089 */
9090 static void
9091invoke_edit(cap, repl, cmd, startln)
9092 cmdarg_T *cap;
9093 int repl; /* "r" or "gr" command */
9094 int cmd;
9095 int startln;
9096{
9097 int restart_edit_save = 0;
9098
9099 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9100 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9101 * it. */
9102 if (repl || !stuff_empty())
9103 restart_edit_save = restart_edit;
9104 else
9105 restart_edit_save = 0;
9106
9107 /* Always reset "restart_edit", this is not a restarted edit. */
9108 restart_edit = 0;
9109
9110 if (edit(cmd, startln, cap->count1))
9111 cap->retval |= CA_COMMAND_BUSY;
9112
9113 if (restart_edit == 0)
9114 restart_edit = restart_edit_save;
9115}
9116
9117#ifdef FEAT_TEXTOBJ
9118/*
9119 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9120 */
9121 static void
9122nv_object(cap)
9123 cmdarg_T *cap;
9124{
9125 int flag;
9126 int include;
9127 char_u *mps_save;
9128
9129 if (cap->cmdchar == 'i')
9130 include = FALSE; /* "ix" = inner object: exclude white space */
9131 else
9132 include = TRUE; /* "ax" = an object: include white space */
9133
9134 /* Make sure (), [], {} and <> are in 'matchpairs' */
9135 mps_save = curbuf->b_p_mps;
9136 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9137
9138 switch (cap->nchar)
9139 {
9140 case 'w': /* "aw" = a word */
9141 flag = current_word(cap->oap, cap->count1, include, FALSE);
9142 break;
9143 case 'W': /* "aW" = a WORD */
9144 flag = current_word(cap->oap, cap->count1, include, TRUE);
9145 break;
9146 case 'b': /* "ab" = a braces block */
9147 case '(':
9148 case ')':
9149 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9150 break;
9151 case 'B': /* "aB" = a Brackets block */
9152 case '{':
9153 case '}':
9154 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9155 break;
9156 case '[': /* "a[" = a [] block */
9157 case ']':
9158 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9159 break;
9160 case '<': /* "a<" = a <> block */
9161 case '>':
9162 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9163 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009164 case 't': /* "at" = a tag block (xml and html) */
9165 flag = current_tagblock(cap->oap, cap->count1, include);
9166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 case 'p': /* "ap" = a paragraph */
9168 flag = current_par(cap->oap, cap->count1, include, 'p');
9169 break;
9170 case 's': /* "as" = a sentence */
9171 flag = current_sent(cap->oap, cap->count1, include);
9172 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009173 case '"': /* "a"" = a double quoted string */
9174 case '\'': /* "a'" = a single quoted string */
9175 case '`': /* "a`" = a backtick quoted string */
9176 flag = current_quote(cap->oap, cap->count1, include,
9177 cap->nchar);
9178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#if 0 /* TODO */
9180 case 'S': /* "aS" = a section */
9181 case 'f': /* "af" = a filename */
9182 case 'u': /* "au" = a URL */
9183#endif
9184 default:
9185 flag = FAIL;
9186 break;
9187 }
9188
9189 curbuf->b_p_mps = mps_save;
9190 if (flag == FAIL)
9191 clearopbeep(cap->oap);
9192 adjust_cursor_col();
9193 curwin->w_set_curswant = TRUE;
9194}
9195#endif
9196
9197/*
9198 * "q" command: Start/stop recording.
9199 * "q:", "q/", "q?": edit command-line in command-line window.
9200 */
9201 static void
9202nv_record(cap)
9203 cmdarg_T *cap;
9204{
9205 if (cap->oap->op_type == OP_FORMAT)
9206 {
9207 /* "gqq" is the same as "gqgq": format line */
9208 cap->cmdchar = 'g';
9209 cap->nchar = 'q';
9210 nv_operator(cap);
9211 }
9212 else if (!checkclearop(cap->oap))
9213 {
9214#ifdef FEAT_CMDWIN
9215 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9216 {
9217 stuffcharReadbuff(cap->nchar);
9218 stuffcharReadbuff(K_CMDWIN);
9219 }
9220 else
9221#endif
9222 /* (stop) recording into a named register, unless executing a
9223 * register */
9224 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9225 clearopbeep(cap->oap);
9226 }
9227}
9228
9229/*
9230 * Handle the "@r" command.
9231 */
9232 static void
9233nv_at(cap)
9234 cmdarg_T *cap;
9235{
9236 if (checkclearop(cap->oap))
9237 return;
9238#ifdef FEAT_EVAL
9239 if (cap->nchar == '=')
9240 {
9241 if (get_expr_register() == NUL)
9242 return;
9243 }
9244#endif
9245 while (cap->count1-- && !got_int)
9246 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009247 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 {
9249 clearopbeep(cap->oap);
9250 break;
9251 }
9252 line_breakcheck();
9253 }
9254}
9255
9256/*
9257 * Handle the CTRL-U and CTRL-D commands.
9258 */
9259 static void
9260nv_halfpage(cap)
9261 cmdarg_T *cap;
9262{
9263 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9264 || (cap->cmdchar == Ctrl_D
9265 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9266 clearopbeep(cap->oap);
9267 else if (!checkclearop(cap->oap))
9268 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9269}
9270
9271/*
9272 * Handle "J" or "gJ" command.
9273 */
9274 static void
9275nv_join(cap)
9276 cmdarg_T *cap;
9277{
9278#ifdef FEAT_VISUAL
9279 if (VIsual_active) /* join the visual lines */
9280 nv_operator(cap);
9281 else
9282#endif
9283 if (!checkclearop(cap->oap))
9284 {
9285 if (cap->count0 <= 1)
9286 cap->count0 = 2; /* default for join is two lines! */
9287 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9288 curbuf->b_ml.ml_line_count)
9289 clearopbeep(cap->oap); /* beyond last line */
9290 else
9291 {
9292 prep_redo(cap->oap->regname, cap->count0,
9293 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02009294 (void)do_join(cap->count0, cap->nchar == NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 }
9296 }
9297}
9298
9299/*
9300 * "P", "gP", "p" and "gp" commands.
9301 */
9302 static void
9303nv_put(cap)
9304 cmdarg_T *cap;
9305{
9306#ifdef FEAT_VISUAL
9307 int regname = 0;
9308 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009309 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009310 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311#endif
9312 int dir;
9313 int flags = 0;
9314
9315 if (cap->oap->op_type != OP_NOP)
9316 {
9317#ifdef FEAT_DIFF
9318 /* "dp" is ":diffput" */
9319 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9320 {
9321 clearop(cap->oap);
9322 nv_diffgetput(TRUE);
9323 }
9324 else
9325#endif
9326 clearopbeep(cap->oap);
9327 }
9328 else
9329 {
9330 dir = (cap->cmdchar == 'P'
9331 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9332 ? BACKWARD : FORWARD;
9333 prep_redo_cmd(cap);
9334 if (cap->cmdchar == 'g')
9335 flags |= PUT_CURSEND;
9336
9337#ifdef FEAT_VISUAL
9338 if (VIsual_active)
9339 {
9340 /* Putting in Visual mode: The put text replaces the selected
9341 * text. First delete the selected text, then put the new text.
9342 * Need to save and restore the registers that the delete
9343 * overwrites if the old contents is being put.
9344 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009345 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 regname = cap->oap->regname;
9347# ifdef FEAT_CLIPBOARD
9348 adjust_clip_reg(&regname);
9349# endif
Bram Moolenaar53748fc2012-01-26 11:43:09 +01009350 if (regname == 0 || regname == '"' || VIM_ISDIGIT(regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351# ifdef FEAT_CLIPBOARD
9352 || (clip_unnamed && (regname == '*' || regname == '+'))
9353# endif
9354
9355 )
9356 {
9357 /* the delete is going to overwrite the register we want to
9358 * put, save it first. */
9359 reg1 = get_register(regname, TRUE);
9360 }
9361
9362 /* Now delete the selected text. */
9363 cap->cmdchar = 'd';
9364 cap->nchar = NUL;
9365 cap->oap->regname = NUL;
9366 nv_operator(cap);
9367 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009368 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370 /* delete PUT_LINE_BACKWARD; */
9371 cap->oap->regname = regname;
9372
9373 if (reg1 != NULL)
9374 {
9375 /* Delete probably changed the register we want to put, save
9376 * it first. Then put back what was there before the delete. */
9377 reg2 = get_register(regname, FALSE);
9378 put_register(regname, reg1);
9379 }
9380
9381 /* When deleted a linewise Visual area, put the register as
9382 * lines to avoid it joined with the next line. When deletion was
9383 * characterwise, split a line when putting lines. */
9384 if (VIsual_mode == 'V')
9385 flags |= PUT_LINE;
9386 else if (VIsual_mode == 'v')
9387 flags |= PUT_LINE_SPLIT;
9388 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9389 flags |= PUT_LINE_FORWARD;
9390 dir = BACKWARD;
9391 if ((VIsual_mode != 'V'
9392 && curwin->w_cursor.col < curbuf->b_op_start.col)
9393 || (VIsual_mode == 'V'
9394 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9395 /* cursor is at the end of the line or end of file, put
9396 * forward. */
9397 dir = FORWARD;
9398 }
9399#endif
9400 do_put(cap->oap->regname, dir, cap->count1, flags);
9401
9402#ifdef FEAT_VISUAL
9403 /* If a register was saved, put it back now. */
9404 if (reg2 != NULL)
9405 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009406
9407 /* What to reselect with "gv"? Selecting the just put text seems to
9408 * be the most useful, since the original text was removed. */
9409 if (was_visual)
9410 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009411 curbuf->b_visual.vi_start = curbuf->b_op_start;
9412 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009413 }
9414
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009415 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009416 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009417 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009418 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009419 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009420
9421 /* If the cursor was in that line, move it to the end of the last
9422 * line. */
9423 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9424 {
9425 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9426 coladvance((colnr_T)MAXCOL);
9427 }
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429#endif
9430 auto_format(FALSE, TRUE);
9431 }
9432}
9433
9434/*
9435 * "o" and "O" commands.
9436 */
9437 static void
9438nv_open(cap)
9439 cmdarg_T *cap;
9440{
9441#ifdef FEAT_DIFF
9442 /* "do" is ":diffget" */
9443 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9444 {
9445 clearop(cap->oap);
9446 nv_diffgetput(FALSE);
9447 }
9448 else
9449#endif
9450#ifdef FEAT_VISUAL
9451 if (VIsual_active) /* switch start and end of visual */
9452 v_swap_corners(cap->cmdchar);
9453 else
9454#endif
9455 n_opencmd(cap);
9456}
9457
9458#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 static void
9460nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009461 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462{
9463 ProcessSniffRequests();
9464}
9465#endif
9466
9467#ifdef FEAT_NETBEANS_INTG
9468 static void
9469nv_nbcmd(cap)
9470 cmdarg_T *cap;
9471{
9472 netbeans_keycommand(cap->nchar);
9473}
9474#endif
9475
9476#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 static void
9478nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009479 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
9481 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9482}
9483#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009484
9485#ifdef FEAT_AUTOCMD
9486/*
9487 * Trigger CursorHold event.
9488 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9489 * input buffer. "did_cursorhold" is set to avoid retriggering.
9490 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009491 static void
9492nv_cursorhold(cap)
9493 cmdarg_T *cap;
9494{
9495 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9496 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009497 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009498}
9499#endif