blob: a7301162bd2accf6b88c3b0c0f21bcf3c1430005 [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]);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002282 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002283
2284 if (*p_opfunc == NUL)
2285 EMSG(_("E774: 'operatorfunc' is empty"));
2286 else
2287 {
2288 /* Set '[ and '] marks to text to be operated on. */
2289 curbuf->b_op_start = oap->start;
2290 curbuf->b_op_end = oap->end;
2291 if (oap->motion_type != MLINE && !oap->inclusive)
2292 /* Exclude the end position. */
2293 decl(&curbuf->b_op_end);
2294
2295 if (oap->block_mode)
2296 argv[0] = (char_u *)"block";
2297 else if (oap->motion_type == MLINE)
2298 argv[0] = (char_u *)"line";
2299 else
2300 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002301
2302 /* Reset virtual_op so that 'virtualedit' can be changed in the
2303 * function. */
2304 virtual_op = MAYBE;
2305
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002306 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002307
2308 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002309 }
2310#else
2311 EMSG(_("E775: Eval feature not available"));
2312#endif
2313}
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#if defined(FEAT_MOUSE) || defined(PROTO)
2316/*
2317 * Do the appropriate action for the current mouse click in the current mode.
2318 * Not used for Command-line mode.
2319 *
2320 * Normal Mode:
2321 * event modi- position visual change action
2322 * fier cursor window
2323 * left press - yes end yes
2324 * left press C yes end yes "^]" (2)
2325 * left press S yes end yes "*" (2)
2326 * left drag - yes start if moved no
2327 * left relse - yes start if moved no
2328 * middle press - yes if not active no put register
2329 * middle press - yes if active no yank and put
2330 * right press - yes start or extend yes
2331 * right press S yes no change yes "#" (2)
2332 * right drag - yes extend no
2333 * right relse - yes extend no
2334 *
2335 * Insert or Replace Mode:
2336 * event modi- position visual change action
2337 * fier cursor window
2338 * left press - yes (cannot be active) yes
2339 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2340 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2341 * left drag - yes start or extend (1) no CTRL-O (1)
2342 * left relse - yes start or extend (1) no CTRL-O (1)
2343 * middle press - no (cannot be active) no put register
2344 * right press - yes start or extend yes CTRL-O
2345 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2346 *
2347 * (1) only if mouse pointer moved since press
2348 * (2) only if click is in same buffer
2349 *
2350 * Return TRUE if start_arrow() should be called for edit mode.
2351 */
2352 int
2353do_mouse(oap, c, dir, count, fixindent)
2354 oparg_T *oap; /* operator argument, can be NULL */
2355 int c; /* K_LEFTMOUSE, etc */
2356 int dir; /* Direction to 'put' if necessary */
2357 long count;
2358 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2359{
2360 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2361 static int got_click = FALSE; /* got a click some time back */
2362
2363 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2364 int is_click; /* If FALSE it's a drag or release event */
2365 int is_drag; /* If TRUE it's a drag event */
2366 int jump_flags = 0; /* flags for jump_to_mouse() */
2367 pos_T start_visual;
2368 int moved; /* Has cursor moved? */
2369 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002370#ifdef FEAT_WINDOWS
2371 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#ifdef FEAT_VERTSPLIT
2374 int in_sep_line; /* mouse in vertical separator line */
2375#endif
2376 int c1, c2;
2377#if defined(FEAT_FOLDING)
2378 pos_T save_cursor;
2379#endif
2380 win_T *old_curwin = curwin;
2381#ifdef FEAT_VISUAL
2382 static pos_T orig_cursor;
2383 colnr_T leftcol, rightcol;
2384 pos_T end_visual;
2385 int diff;
2386 int old_active = VIsual_active;
2387 int old_mode = VIsual_mode;
2388#endif
2389 int regname;
2390
2391#if defined(FEAT_FOLDING)
2392 save_cursor = curwin->w_cursor;
2393#endif
2394
2395 /*
2396 * When GUI is active, always recognize mouse events, otherwise:
2397 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2398 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2399 * - For command line and insert mode 'mouse' is checked before calling
2400 * do_mouse().
2401 */
2402 if (do_always)
2403 do_always = FALSE;
2404 else
2405#ifdef FEAT_GUI
2406 if (!gui.in_use)
2407#endif
2408 {
2409#ifdef FEAT_VISUAL
2410 if (VIsual_active)
2411 {
2412 if (!mouse_has(MOUSE_VISUAL))
2413 return FALSE;
2414 }
2415 else
2416#endif
2417 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2418 return FALSE;
2419 }
2420
2421 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2422
2423#ifdef FEAT_MOUSESHAPE
2424 /* May have stopped dragging the status or separator line. The pointer is
2425 * most likely still on the status or separator line. */
2426 if (!is_drag && drag_status_line)
2427 {
2428 drag_status_line = FALSE;
2429 update_mouseshape(SHAPE_IDX_STATUS);
2430 }
2431# ifdef FEAT_VERTSPLIT
2432 if (!is_drag && drag_sep_line)
2433 {
2434 drag_sep_line = FALSE;
2435 update_mouseshape(SHAPE_IDX_VSEP);
2436 }
2437# endif
2438#endif
2439
2440 /*
2441 * Ignore drag and release events if we didn't get a click.
2442 */
2443 if (is_click)
2444 got_click = TRUE;
2445 else
2446 {
2447 if (!got_click) /* didn't get click, ignore */
2448 return FALSE;
2449 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002452#ifdef FEAT_WINDOWS
2453 if (in_tab_line)
2454 {
2455 in_tab_line = FALSE;
2456 return FALSE;
2457 }
2458#endif
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461
Bram Moolenaar64969662005-12-14 21:59:55 +00002462#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002464 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
2466 if ((mod_mask & MOD_MASK_ALT))
2467 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 /*
2471 * CTRL right mouse button does CTRL-T
2472 */
2473 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2474 {
2475 if (State & INSERT)
2476 stuffcharReadbuff(Ctrl_O);
2477 if (count > 1)
2478 stuffnumReadbuff(count);
2479 stuffcharReadbuff(Ctrl_T);
2480 got_click = FALSE; /* ignore drag&release now */
2481 return FALSE;
2482 }
2483
2484 /*
2485 * CTRL only works with left mouse button
2486 */
2487 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2488 return FALSE;
2489
2490 /*
2491 * When a modifier is down, ignore drag and release events, as well as
2492 * multiple clicks and the middle mouse button.
2493 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2494 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002495 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2496 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 && (!is_click
2498 || (mod_mask & MOD_MASK_MULTI_CLICK)
2499 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002500 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 && mouse_model_popup()
2502 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002503 && !((mod_mask & MOD_MASK_ALT)
2504 && !mouse_model_popup()
2505 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 )
2507 return FALSE;
2508
2509 /*
2510 * If the button press was used as the movement command for an operator
2511 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2512 * drag/release events.
2513 */
2514 if (!is_click && which_button == MOUSE_MIDDLE)
2515 return FALSE;
2516
2517 if (oap != NULL)
2518 regname = oap->regname;
2519 else
2520 regname = 0;
2521
2522 /*
2523 * Middle mouse button does a 'put' of the selected text
2524 */
2525 if (which_button == MOUSE_MIDDLE)
2526 {
2527 if (State == NORMAL)
2528 {
2529 /*
2530 * If an operator was pending, we don't know what the user wanted
2531 * to do. Go back to normal mode: Clear the operator and beep().
2532 */
2533 if (oap != NULL && oap->op_type != OP_NOP)
2534 {
2535 clearopbeep(oap);
2536 return FALSE;
2537 }
2538
2539#ifdef FEAT_VISUAL
2540 /*
2541 * If visual was active, yank the highlighted text and put it
2542 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002543 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 */
2545 if (VIsual_active)
2546 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002547 if (VIsual_select)
2548 {
2549 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002550 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002551 }
2552 else
2553 {
2554 stuffcharReadbuff('y');
2555 stuffcharReadbuff(K_MIDDLEMOUSE);
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 do_always = TRUE; /* ignore 'mouse' setting next time */
2558 return FALSE;
2559 }
2560#endif
2561 /*
2562 * The rest is below jump_to_mouse()
2563 */
2564 }
2565
2566 else if ((State & INSERT) == 0)
2567 return FALSE;
2568
2569 /*
2570 * Middle click in insert mode doesn't move the mouse, just insert the
2571 * contents of a register. '.' register is special, can't insert that
2572 * with do_put().
2573 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2574 * happens for the GUI).
2575 */
2576 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2577 {
2578 if (regname == '.')
2579 insert_reg(regname, TRUE);
2580 else
2581 {
2582#ifdef FEAT_CLIPBOARD
2583 if (clip_star.available && regname == 0)
2584 regname = '*';
2585#endif
2586 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2587 insert_reg(regname, TRUE);
2588 else
2589 {
2590 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2591
2592 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2593 AppendCharToRedobuff(Ctrl_R);
2594 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2595 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2596 }
2597 }
2598 return FALSE;
2599 }
2600 }
2601
2602 /* When dragging or button-up stay in the same window. */
2603 if (!is_click)
2604 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2605
2606 start_visual.lnum = 0;
2607
Bram Moolenaarf740b292006-02-16 22:11:02 +00002608#ifdef FEAT_WINDOWS
2609 /* Check for clicking in the tab page line. */
2610 if (mouse_row == 0 && firstwin->w_winrow > 0)
2611 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002612 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002613 {
2614 if (in_tab_line)
2615 {
2616 c1 = TabPageIdxs[mouse_col];
2617 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2618 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002619 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002620 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002621
Bram Moolenaarf740b292006-02-16 22:11:02 +00002622 /* click in a tab selects that tab page */
2623 if (is_click
2624# ifdef FEAT_CMDWIN
2625 && cmdwin_type == 0
2626# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002627 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002628 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002629 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002630 c1 = TabPageIdxs[mouse_col];
2631 if (c1 >= 0)
2632 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002633 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2634 {
2635 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002636 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002637 tabpage_new();
2638 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2639 }
2640 else
2641 {
2642 /* Go to specified tab page, or next one if not clicking
2643 * on a label. */
2644 goto_tabpage(c1);
2645
2646 /* It's like clicking on the status line of a window. */
2647 if (curwin != old_curwin)
2648 end_visual_mode();
2649 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002650 }
2651 else if (c1 < 0)
2652 {
2653 tabpage_T *tp;
2654
2655 /* Close the current or specified tab page. */
2656 if (c1 == -999)
2657 tp = curtab;
2658 else
2659 tp = find_tabpage(-c1);
2660 if (tp == curtab)
2661 {
2662 if (first_tabpage->tp_next != NULL)
2663 tabpage_close(FALSE);
2664 }
2665 else if (tp != NULL)
2666 tabpage_close_other(tp, FALSE);
2667 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002668 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002670 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002671 else if (is_drag && in_tab_line)
2672 {
2673 c1 = TabPageIdxs[mouse_col];
2674 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2675 return FALSE;
2676 }
2677
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678#endif
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /*
2681 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2682 * right button up -> pop-up menu
2683 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002684 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
2686 if (mouse_model_popup())
2687 {
2688 if (which_button == MOUSE_RIGHT
2689 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2690 {
2691 /*
2692 * NOTE: Ignore right button down and drag mouse events.
2693 * Windows only shows the popup menu on the button up event.
2694 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002695#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2696 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (!is_click)
2698 return FALSE;
2699#endif
2700#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2701 if (is_click || is_drag)
2702 return FALSE;
2703#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002704#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2706 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2707 if (gui.in_use)
2708 {
2709 jump_flags = 0;
2710 if (STRCMP(p_mousem, "popup_setpos") == 0)
2711 {
2712 /* First set the cursor position before showing the popup
2713 * menu. */
2714#ifdef FEAT_VISUAL
2715 if (VIsual_active)
2716 {
2717 pos_T m_pos;
2718
2719 /*
2720 * set MOUSE_MAY_STOP_VIS if we are outside the
2721 * selection or the current window (might have false
2722 * negative here)
2723 */
2724 if (mouse_row < W_WINROW(curwin)
2725 || mouse_row
2726 > (W_WINROW(curwin) + curwin->w_height))
2727 jump_flags = MOUSE_MAY_STOP_VIS;
2728 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2729 jump_flags = MOUSE_MAY_STOP_VIS;
2730 else
2731 {
2732 if ((lt(curwin->w_cursor, VIsual)
2733 && (lt(m_pos, curwin->w_cursor)
2734 || lt(VIsual, m_pos)))
2735 || (lt(VIsual, curwin->w_cursor)
2736 && (lt(m_pos, VIsual)
2737 || lt(curwin->w_cursor, m_pos))))
2738 {
2739 jump_flags = MOUSE_MAY_STOP_VIS;
2740 }
2741 else if (VIsual_mode == Ctrl_V)
2742 {
2743 getvcols(curwin, &curwin->w_cursor, &VIsual,
2744 &leftcol, &rightcol);
2745 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2746 if (m_pos.col < leftcol || m_pos.col > rightcol)
2747 jump_flags = MOUSE_MAY_STOP_VIS;
2748 }
2749 }
2750 }
2751 else
2752 jump_flags = MOUSE_MAY_STOP_VIS;
2753#endif
2754 }
2755 if (jump_flags)
2756 {
2757 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2758 update_curbuf(
2759#ifdef FEAT_VISUAL
2760 VIsual_active ? INVERTED :
2761#endif
2762 VALID);
2763 setcursor();
2764 out_flush(); /* Update before showing popup menu */
2765 }
2766# ifdef FEAT_MENU
2767 gui_show_popupmenu();
2768# endif
2769 return (jump_flags & CURSOR_MOVED) != 0;
2770 }
2771 else
2772 return FALSE;
2773#else
2774 return FALSE;
2775#endif
2776 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002777 if (which_button == MOUSE_LEFT
2778 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
2780 which_button = MOUSE_RIGHT;
2781 mod_mask &= ~MOD_MASK_SHIFT;
2782 }
2783 }
2784
2785#ifdef FEAT_VISUAL
2786 if ((State & (NORMAL | INSERT))
2787 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2788 {
2789 if (which_button == MOUSE_LEFT)
2790 {
2791 if (is_click)
2792 {
2793 /* stop Visual mode for a left click in a window, but not when
2794 * on a status line */
2795 if (VIsual_active)
2796 jump_flags |= MOUSE_MAY_STOP_VIS;
2797 }
2798 else if (mouse_has(MOUSE_VISUAL))
2799 jump_flags |= MOUSE_MAY_VIS;
2800 }
2801 else if (which_button == MOUSE_RIGHT)
2802 {
2803 if (is_click && VIsual_active)
2804 {
2805 /*
2806 * Remember the start and end of visual before moving the
2807 * cursor.
2808 */
2809 if (lt(curwin->w_cursor, VIsual))
2810 {
2811 start_visual = curwin->w_cursor;
2812 end_visual = VIsual;
2813 }
2814 else
2815 {
2816 start_visual = VIsual;
2817 end_visual = curwin->w_cursor;
2818 }
2819 }
2820 jump_flags |= MOUSE_FOCUS;
2821 if (mouse_has(MOUSE_VISUAL))
2822 jump_flags |= MOUSE_MAY_VIS;
2823 }
2824 }
2825#endif
2826
2827 /*
2828 * If an operator is pending, ignore all drags and releases until the
2829 * next mouse click.
2830 */
2831 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2832 {
2833 got_click = FALSE;
2834 oap->motion_type = MCHAR;
2835 }
2836
2837 /* When releasing the button let jump_to_mouse() know. */
2838 if (!is_click && !is_drag)
2839 jump_flags |= MOUSE_RELEASED;
2840
2841 /*
2842 * JUMP!
2843 */
2844 jump_flags = jump_to_mouse(jump_flags,
2845 oap == NULL ? NULL : &(oap->inclusive), which_button);
2846 moved = (jump_flags & CURSOR_MOVED);
2847 in_status_line = (jump_flags & IN_STATUS_LINE);
2848#ifdef FEAT_VERTSPLIT
2849 in_sep_line = (jump_flags & IN_SEP_LINE);
2850#endif
2851
2852#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002853 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2855 {
2856 int key = KEY2TERMCAP1(c);
2857
2858 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2859 || key == (int)KE_RIGHTRELEASE)
2860 netbeans_button_release(which_button);
2861 }
2862#endif
2863
2864 /* When jumping to another window, clear a pending operator. That's a bit
2865 * friendlier than beeping and not jumping to that window. */
2866 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2867 clearop(oap);
2868
2869#ifdef FEAT_FOLDING
2870 if (mod_mask == 0
2871 && !is_drag
2872 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2873 && which_button == MOUSE_LEFT)
2874 {
2875 /* open or close a fold at this line */
2876 if (jump_flags & MOUSE_FOLD_OPEN)
2877 openFold(curwin->w_cursor.lnum, 1L);
2878 else
2879 closeFold(curwin->w_cursor.lnum, 1L);
2880 /* don't move the cursor if still in the same window */
2881 if (curwin == old_curwin)
2882 curwin->w_cursor = save_cursor;
2883 }
2884#endif
2885
2886#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2887 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2888 {
2889 clip_modeless(which_button, is_click, is_drag);
2890 return FALSE;
2891 }
2892#endif
2893
2894#ifdef FEAT_VISUAL
2895 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002896 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (VIsual_active && is_drag && p_so)
2898 {
2899 /* In the very first line, allow scrolling one line */
2900 if (mouse_row == 0)
2901 mouse_dragging = 2;
2902 else
2903 mouse_dragging = 1;
2904 }
2905
2906 /* When dragging the mouse above the window, scroll down. */
2907 if (is_drag && mouse_row < 0 && !in_status_line)
2908 {
2909 scroll_redraw(FALSE, 1L);
2910 mouse_row = 0;
2911 }
2912
2913 if (start_visual.lnum) /* right click in visual mode */
2914 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002915 /* When ALT is pressed make Visual mode blockwise. */
2916 if (mod_mask & MOD_MASK_ALT)
2917 VIsual_mode = Ctrl_V;
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 /*
2920 * In Visual-block mode, divide the area in four, pick up the corner
2921 * that is in the quarter that the cursor is in.
2922 */
2923 if (VIsual_mode == Ctrl_V)
2924 {
2925 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2926 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2927 end_visual.col = leftcol;
2928 else
2929 end_visual.col = rightcol;
2930 if (curwin->w_cursor.lnum <
2931 (start_visual.lnum + end_visual.lnum) / 2)
2932 end_visual.lnum = end_visual.lnum;
2933 else
2934 end_visual.lnum = start_visual.lnum;
2935
2936 /* move VIsual to the right column */
2937 start_visual = curwin->w_cursor; /* save the cursor pos */
2938 curwin->w_cursor = end_visual;
2939 coladvance(end_visual.col);
2940 VIsual = curwin->w_cursor;
2941 curwin->w_cursor = start_visual; /* restore the cursor */
2942 }
2943 else
2944 {
2945 /*
2946 * If the click is before the start of visual, change the start.
2947 * If the click is after the end of visual, change the end. If
2948 * the click is inside the visual, change the closest side.
2949 */
2950 if (lt(curwin->w_cursor, start_visual))
2951 VIsual = end_visual;
2952 else if (lt(end_visual, curwin->w_cursor))
2953 VIsual = start_visual;
2954 else
2955 {
2956 /* In the same line, compare column number */
2957 if (end_visual.lnum == start_visual.lnum)
2958 {
2959 if (curwin->w_cursor.col - start_visual.col >
2960 end_visual.col - curwin->w_cursor.col)
2961 VIsual = start_visual;
2962 else
2963 VIsual = end_visual;
2964 }
2965
2966 /* In different lines, compare line number */
2967 else
2968 {
2969 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2970 (end_visual.lnum - curwin->w_cursor.lnum);
2971
2972 if (diff > 0) /* closest to end */
2973 VIsual = start_visual;
2974 else if (diff < 0) /* closest to start */
2975 VIsual = end_visual;
2976 else /* in the middle line */
2977 {
2978 if (curwin->w_cursor.col <
2979 (start_visual.col + end_visual.col) / 2)
2980 VIsual = end_visual;
2981 else
2982 VIsual = start_visual;
2983 }
2984 }
2985 }
2986 }
2987 }
2988 /*
2989 * If Visual mode started in insert mode, execute "CTRL-O"
2990 */
2991 else if ((State & INSERT) && VIsual_active)
2992 stuffcharReadbuff(Ctrl_O);
2993#endif
2994
2995 /*
2996 * Middle mouse click: Put text before cursor.
2997 */
2998 if (which_button == MOUSE_MIDDLE)
2999 {
3000#ifdef FEAT_CLIPBOARD
3001 if (clip_star.available && regname == 0)
3002 regname = '*';
3003#endif
3004 if (yank_register_mline(regname))
3005 {
3006 if (mouse_past_bottom)
3007 dir = FORWARD;
3008 }
3009 else if (mouse_past_eol)
3010 dir = FORWARD;
3011
3012 if (fixindent)
3013 {
3014 c1 = (dir == BACKWARD) ? '[' : ']';
3015 c2 = 'p';
3016 }
3017 else
3018 {
3019 c1 = (dir == FORWARD) ? 'p' : 'P';
3020 c2 = NUL;
3021 }
3022 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3023
3024 /*
3025 * Remember where the paste started, so in edit() Insstart can be set
3026 * to this position
3027 */
3028 if (restart_edit != 0)
3029 where_paste_started = curwin->w_cursor;
3030 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3031 }
3032
3033#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3034 /*
3035 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3036 * error under the mouse pointer.
3037 */
3038 else if (((mod_mask & MOD_MASK_CTRL)
3039 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3040 && bt_quickfix(curbuf))
3041 {
3042 if (State & INSERT)
3043 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003044 if (curwin->w_llist_ref == NULL) /* quickfix window */
3045 stuffReadbuff((char_u *)":.cc\n");
3046 else /* location list window */
3047 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 got_click = FALSE; /* ignore drag&release now */
3049 }
3050#endif
3051
3052 /*
3053 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3054 * under the mouse pointer.
3055 */
3056 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3057 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3058 {
3059 if (State & INSERT)
3060 stuffcharReadbuff(Ctrl_O);
3061 stuffcharReadbuff(Ctrl_RSB);
3062 got_click = FALSE; /* ignore drag&release now */
3063 }
3064
3065 /*
3066 * Shift-Mouse click searches for the next occurrence of the word under
3067 * the mouse pointer
3068 */
3069 else if ((mod_mask & MOD_MASK_SHIFT))
3070 {
3071 if (State & INSERT
3072#ifdef FEAT_VISUAL
3073 || (VIsual_active && VIsual_select)
3074#endif
3075 )
3076 stuffcharReadbuff(Ctrl_O);
3077 if (which_button == MOUSE_LEFT)
3078 stuffcharReadbuff('*');
3079 else /* MOUSE_RIGHT */
3080 stuffcharReadbuff('#');
3081 }
3082
3083 /* Handle double clicks, unless on status line */
3084 else if (in_status_line)
3085 {
3086#ifdef FEAT_MOUSESHAPE
3087 if ((is_drag || is_click) && !drag_status_line)
3088 {
3089 drag_status_line = TRUE;
3090 update_mouseshape(-1);
3091 }
3092#endif
3093 }
3094#ifdef FEAT_VERTSPLIT
3095 else if (in_sep_line)
3096 {
3097# ifdef FEAT_MOUSESHAPE
3098 if ((is_drag || is_click) && !drag_sep_line)
3099 {
3100 drag_sep_line = TRUE;
3101 update_mouseshape(-1);
3102 }
3103# endif
3104 }
3105#endif
3106#ifdef FEAT_VISUAL
3107 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3108 && mouse_has(MOUSE_VISUAL))
3109 {
3110 if (is_click || !VIsual_active)
3111 {
3112 if (VIsual_active)
3113 orig_cursor = VIsual;
3114 else
3115 {
3116 check_visual_highlight();
3117 VIsual = curwin->w_cursor;
3118 orig_cursor = VIsual;
3119 VIsual_active = TRUE;
3120 VIsual_reselect = TRUE;
3121 /* start Select mode if 'selectmode' contains "mouse" */
3122 may_start_select('o');
3123 setmouse();
3124 }
3125 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003126 {
3127 /* Double click with ALT pressed makes it blockwise. */
3128 if (mod_mask & MOD_MASK_ALT)
3129 VIsual_mode = Ctrl_V;
3130 else
3131 VIsual_mode = 'v';
3132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3134 VIsual_mode = 'V';
3135 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3136 VIsual_mode = Ctrl_V;
3137#ifdef FEAT_CLIPBOARD
3138 /* Make sure the clipboard gets updated. Needed because start and
3139 * end may still be the same, and the selection needs to be owned */
3140 clip_star.vmode = NUL;
3141#endif
3142 }
3143 /*
3144 * A double click selects a word or a block.
3145 */
3146 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3147 {
3148 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003149 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 if (is_click)
3152 {
3153 /* If the character under the cursor (skipping white space) is
3154 * not a word character, try finding a match and select a (),
3155 * {}, [], #if/#endif, etc. block. */
3156 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 inc(&end_visual);
3159 if (oap != NULL)
3160 oap->motion_type = MCHAR;
3161 if (oap != NULL
3162 && VIsual_mode == 'v'
3163 && !vim_iswordc(gchar_pos(&end_visual))
3164 && equalpos(curwin->w_cursor, VIsual)
3165 && (pos = findmatch(oap, NUL)) != NULL)
3166 {
3167 curwin->w_cursor = *pos;
3168 if (oap->motion_type == MLINE)
3169 VIsual_mode = 'V';
3170 else if (*p_sel == 'e')
3171 {
3172 if (lt(curwin->w_cursor, VIsual))
3173 ++VIsual.col;
3174 else
3175 ++curwin->w_cursor.col;
3176 }
3177 }
3178 }
3179
3180 if (pos == NULL && (is_click || is_drag))
3181 {
3182 /* When not found a match or when dragging: extend to include
3183 * a word. */
3184 if (lt(curwin->w_cursor, orig_cursor))
3185 {
3186 find_start_of_word(&curwin->w_cursor);
3187 find_end_of_word(&VIsual);
3188 }
3189 else
3190 {
3191 find_start_of_word(&VIsual);
3192 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3193#ifdef FEAT_MBYTE
3194 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003195 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#else
3197 ++curwin->w_cursor.col;
3198#endif
3199 find_end_of_word(&curwin->w_cursor);
3200 }
3201 }
3202 curwin->w_set_curswant = TRUE;
3203 }
3204 if (is_click)
3205 redraw_curbuf_later(INVERTED); /* update the inversion */
3206 }
3207 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003208 {
3209 if (mod_mask & MOD_MASK_ALT)
3210 VIsual_mode = Ctrl_V;
3211 else
3212 VIsual_mode = 'v';
3213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
3215 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003216 if ((!VIsual_active && old_active && mode_displayed)
3217 || (VIsual_active && p_smd && msg_silent == 0
3218 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 redraw_cmdline = TRUE;
3220#endif
3221
3222 return moved;
3223}
3224
3225#ifdef FEAT_VISUAL
3226/*
3227 * Move "pos" back to the start of the word it's in.
3228 */
3229 static void
3230find_start_of_word(pos)
3231 pos_T *pos;
3232{
3233 char_u *line;
3234 int cclass;
3235 int col;
3236
3237 line = ml_get(pos->lnum);
3238 cclass = get_mouse_class(line + pos->col);
3239
3240 while (pos->col > 0)
3241 {
3242 col = pos->col - 1;
3243#ifdef FEAT_MBYTE
3244 col -= (*mb_head_off)(line, line + col);
3245#endif
3246 if (get_mouse_class(line + col) != cclass)
3247 break;
3248 pos->col = col;
3249 }
3250}
3251
3252/*
3253 * Move "pos" forward to the end of the word it's in.
3254 * When 'selection' is "exclusive", the position is just after the word.
3255 */
3256 static void
3257find_end_of_word(pos)
3258 pos_T *pos;
3259{
3260 char_u *line;
3261 int cclass;
3262 int col;
3263
3264 line = ml_get(pos->lnum);
3265 if (*p_sel == 'e' && pos->col > 0)
3266 {
3267 --pos->col;
3268#ifdef FEAT_MBYTE
3269 pos->col -= (*mb_head_off)(line, line + pos->col);
3270#endif
3271 }
3272 cclass = get_mouse_class(line + pos->col);
3273 while (line[pos->col] != NUL)
3274 {
3275#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003276 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#else
3278 col = pos->col + 1;
3279#endif
3280 if (get_mouse_class(line + col) != cclass)
3281 {
3282 if (*p_sel == 'e')
3283 pos->col = col;
3284 break;
3285 }
3286 pos->col = col;
3287 }
3288}
3289
3290/*
3291 * Get class of a character for selection: same class means same word.
3292 * 0: blank
3293 * 1: punctuation groups
3294 * 2: normal word character
3295 * >2: multi-byte word character.
3296 */
3297 static int
3298get_mouse_class(p)
3299 char_u *p;
3300{
3301 int c;
3302
3303#ifdef FEAT_MBYTE
3304 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3305 return mb_get_class(p);
3306#endif
3307
3308 c = *p;
3309 if (c == ' ' || c == '\t')
3310 return 0;
3311
3312 if (vim_iswordc(c))
3313 return 2;
3314
3315 /*
3316 * There are a few special cases where we want certain combinations of
3317 * characters to be considered as a single word. These are things like
3318 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003319 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3322 return 1;
3323 return c;
3324}
3325#endif /* FEAT_VISUAL */
3326#endif /* FEAT_MOUSE */
3327
3328#if defined(FEAT_VISUAL) || defined(PROTO)
3329/*
3330 * Check if highlighting for visual mode is possible, give a warning message
3331 * if not.
3332 */
3333 void
3334check_visual_highlight()
3335{
3336 static int did_check = FALSE;
3337
3338 if (full_screen)
3339 {
3340 if (!did_check && hl_attr(HLF_V) == 0)
3341 MSG(_("Warning: terminal cannot highlight"));
3342 did_check = TRUE;
3343 }
3344}
3345
3346/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003347 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * This function should ALWAYS be called to end Visual mode, except from
3349 * do_pending_operator().
3350 */
3351 void
3352end_visual_mode()
3353{
3354#ifdef FEAT_CLIPBOARD
3355 /*
3356 * If we are using the clipboard, then remember what was selected in case
3357 * we need to paste it somewhere while we still own the selection.
3358 * Only do this when the clipboard is already owned. Don't want to grab
3359 * the selection when hitting ESC.
3360 */
3361 if (clip_star.available && clip_star.owned)
3362 clip_auto_select();
3363#endif
3364
3365 VIsual_active = FALSE;
3366#ifdef FEAT_MOUSE
3367 setmouse();
3368 mouse_dragging = 0;
3369#endif
3370
3371 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003372 curbuf->b_visual.vi_mode = VIsual_mode;
3373 curbuf->b_visual.vi_start = VIsual;
3374 curbuf->b_visual.vi_end = curwin->w_cursor;
3375 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#ifdef FEAT_EVAL
3377 curbuf->b_visual_mode_eval = VIsual_mode;
3378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#ifdef FEAT_VIRTUALEDIT
3380 if (!virtual_active())
3381 curwin->w_cursor.coladd = 0;
3382#endif
3383
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003384 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 clear_cmdline = TRUE; /* unshow visual mode later */
3386#ifdef FEAT_CMDL_INFO
3387 else
3388 clear_showcmd();
3389#endif
3390
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003391 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392}
3393
3394/*
3395 * Reset VIsual_active and VIsual_reselect.
3396 */
3397 void
3398reset_VIsual_and_resel()
3399{
3400 if (VIsual_active)
3401 {
3402 end_visual_mode();
3403 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3404 }
3405 VIsual_reselect = FALSE;
3406}
3407
3408/*
3409 * Reset VIsual_active and VIsual_reselect if it's set.
3410 */
3411 void
3412reset_VIsual()
3413{
3414 if (VIsual_active)
3415 {
3416 end_visual_mode();
3417 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3418 VIsual_reselect = FALSE;
3419 }
3420}
3421#endif /* FEAT_VISUAL */
3422
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3425
3426/*
3427 * Check for a balloon-eval special item to include when searching for an
3428 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3429 * Returns TRUE if the character at "*ptr" should be included.
3430 * "dir" is FORWARD or BACKWARD, the direction of searching.
3431 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3432 * "bnp" points to a counter for square brackets.
3433 */
3434 static int
3435find_is_eval_item(ptr, colp, bnp, dir)
3436 char_u *ptr;
3437 int *colp;
3438 int *bnp;
3439 int dir;
3440{
3441 /* Accept everything inside []. */
3442 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3443 ++*bnp;
3444 if (*bnp > 0)
3445 {
3446 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3447 --*bnp;
3448 return TRUE;
3449 }
3450
3451 /* skip over "s.var" */
3452 if (*ptr == '.')
3453 return TRUE;
3454
3455 /* two-character item: s->var */
3456 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3457 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3458 {
3459 *colp += dir;
3460 return TRUE;
3461 }
3462 return FALSE;
3463}
3464#endif
3465
3466/*
3467 * Find the identifier under or to the right of the cursor.
3468 * "find_type" can have one of three values:
3469 * FIND_IDENT: find an identifier (keyword)
3470 * FIND_STRING: find any non-white string
3471 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003472 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 *
3474 * There are three steps:
3475 * 1. Search forward for the start of an identifier/string. Doesn't move if
3476 * already on one.
3477 * 2. Search backward for the start of this identifier/string.
3478 * This doesn't match the real Vi but I like it a little better and it
3479 * shouldn't bother anyone.
3480 * 3. Search forward to the end of this identifier/string.
3481 * When FIND_IDENT isn't defined, we backup until a blank.
3482 *
3483 * Returns the length of the string, or zero if no string is found.
3484 * If a string is found, a pointer to the string is put in "*string". This
3485 * string is not always NUL terminated.
3486 */
3487 int
3488find_ident_under_cursor(string, find_type)
3489 char_u **string;
3490 int find_type;
3491{
3492 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3493 curwin->w_cursor.col, string, find_type);
3494}
3495
3496/*
3497 * Like find_ident_under_cursor(), but for any window and any position.
3498 * However: Uses 'iskeyword' from the current window!.
3499 */
3500 int
3501find_ident_at_pos(wp, lnum, startcol, string, find_type)
3502 win_T *wp;
3503 linenr_T lnum;
3504 colnr_T startcol;
3505 char_u **string;
3506 int find_type;
3507{
3508 char_u *ptr;
3509 int col = 0; /* init to shut up GCC */
3510 int i;
3511#ifdef FEAT_MBYTE
3512 int this_class = 0;
3513 int prev_class;
3514 int prevcol;
3515#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 int bn = 0; /* bracket nesting */
3518#endif
3519
3520 /*
3521 * if i == 0: try to find an identifier
3522 * if i == 1: try to find any non-white string
3523 */
3524 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3525 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3526 {
3527 /*
3528 * 1. skip to start of identifier/string
3529 */
3530 col = startcol;
3531#ifdef FEAT_MBYTE
3532 if (has_mbyte)
3533 {
3534 while (ptr[col] != NUL)
3535 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003536# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 /* Stop at a ']' to evaluate "a[x]". */
3538 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3539 break;
3540# endif
3541 this_class = mb_get_class(ptr + col);
3542 if (this_class != 0 && (i == 1 || this_class != 1))
3543 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003544 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546 }
3547 else
3548#endif
3549 while (ptr[col] != NUL
3550 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003551# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3553# endif
3554 )
3555 ++col;
3556
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003557#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 /* When starting on a ']' count it, so that we include the '['. */
3559 bn = ptr[col] == ']';
3560#endif
3561
3562 /*
3563 * 2. Back up to start of identifier/string.
3564 */
3565#ifdef FEAT_MBYTE
3566 if (has_mbyte)
3567 {
3568 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003569# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3571 this_class = mb_get_class((char_u *)"a");
3572 else
3573# endif
3574 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003575 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3578 prev_class = mb_get_class(ptr + prevcol);
3579 if (this_class != prev_class
3580 && (i == 0
3581 || prev_class == 0
3582 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003583# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 && (!(find_type & FIND_EVAL)
3585 || prevcol == 0
3586 || !find_is_eval_item(ptr + prevcol, &prevcol,
3587 &bn, BACKWARD))
3588# endif
3589 )
3590 break;
3591 col = prevcol;
3592 }
3593
3594 /* If we don't want just any old string, or we've found an
3595 * identifier, stop searching. */
3596 if (this_class > 2)
3597 this_class = 2;
3598 if (!(find_type & FIND_STRING) || this_class == 2)
3599 break;
3600 }
3601 else
3602#endif
3603 {
3604 while (col > 0
3605 && ((i == 0
3606 ? vim_iswordc(ptr[col - 1])
3607 : (!vim_iswhite(ptr[col - 1])
3608 && (!(find_type & FIND_IDENT)
3609 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003610#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 || ((find_type & FIND_EVAL)
3612 && col > 1
3613 && find_is_eval_item(ptr + col - 1, &col,
3614 &bn, BACKWARD))
3615#endif
3616 ))
3617 --col;
3618
3619 /* If we don't want just any old string, or we've found an
3620 * identifier, stop searching. */
3621 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3622 break;
3623 }
3624 }
3625
3626 if (ptr[col] == NUL || (i == 0 && (
3627#ifdef FEAT_MBYTE
3628 has_mbyte ? this_class != 2 :
3629#endif
3630 !vim_iswordc(ptr[col]))))
3631 {
3632 /*
3633 * didn't find an identifier or string
3634 */
3635 if (find_type & FIND_STRING)
3636 EMSG(_("E348: No string under cursor"));
3637 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003638 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 return 0;
3640 }
3641 ptr += col;
3642 *string = ptr;
3643
3644 /*
3645 * 3. Find the end if the identifier/string.
3646 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 bn = 0;
3649 startcol -= col;
3650#endif
3651 col = 0;
3652#ifdef FEAT_MBYTE
3653 if (has_mbyte)
3654 {
3655 /* Search for point of changing multibyte character class. */
3656 this_class = mb_get_class(ptr);
3657 while (ptr[col] != NUL
3658 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3659 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || ((find_type & FIND_EVAL)
3662 && col <= (int)startcol
3663 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3664# endif
3665 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003666 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 else
3669#endif
3670 while ((i == 0 ? vim_iswordc(ptr[col])
3671 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 || ((find_type & FIND_EVAL)
3674 && col <= (int)startcol
3675 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3676# endif
3677 )
3678 {
3679 ++col;
3680 }
3681
3682 return col;
3683}
3684
3685/*
3686 * Prepare for redo of a normal command.
3687 */
3688 static void
3689prep_redo_cmd(cap)
3690 cmdarg_T *cap;
3691{
3692 prep_redo(cap->oap->regname, cap->count0,
3693 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3694}
3695
3696/*
3697 * Prepare for redo of any command.
3698 * Note that only the last argument can be a multi-byte char.
3699 */
3700 static void
3701prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3702 int regname;
3703 long num;
3704 int cmd1;
3705 int cmd2;
3706 int cmd3;
3707 int cmd4;
3708 int cmd5;
3709{
3710 ResetRedobuff();
3711 if (regname != 0) /* yank from specified buffer */
3712 {
3713 AppendCharToRedobuff('"');
3714 AppendCharToRedobuff(regname);
3715 }
3716 if (num)
3717 AppendNumberToRedobuff(num);
3718
3719 if (cmd1 != NUL)
3720 AppendCharToRedobuff(cmd1);
3721 if (cmd2 != NUL)
3722 AppendCharToRedobuff(cmd2);
3723 if (cmd3 != NUL)
3724 AppendCharToRedobuff(cmd3);
3725 if (cmd4 != NUL)
3726 AppendCharToRedobuff(cmd4);
3727 if (cmd5 != NUL)
3728 AppendCharToRedobuff(cmd5);
3729}
3730
3731/*
3732 * check for operator active and clear it
3733 *
3734 * return TRUE if operator was active
3735 */
3736 static int
3737checkclearop(oap)
3738 oparg_T *oap;
3739{
3740 if (oap->op_type == OP_NOP)
3741 return FALSE;
3742 clearopbeep(oap);
3743 return TRUE;
3744}
3745
3746/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003747 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003749 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
3751 static int
3752checkclearopq(oap)
3753 oparg_T *oap;
3754{
3755 if (oap->op_type == OP_NOP
3756#ifdef FEAT_VISUAL
3757 && !VIsual_active
3758#endif
3759 )
3760 return FALSE;
3761 clearopbeep(oap);
3762 return TRUE;
3763}
3764
3765 static void
3766clearop(oap)
3767 oparg_T *oap;
3768{
3769 oap->op_type = OP_NOP;
3770 oap->regname = 0;
3771 oap->motion_force = NUL;
3772 oap->use_reg_one = FALSE;
3773}
3774
3775 static void
3776clearopbeep(oap)
3777 oparg_T *oap;
3778{
3779 clearop(oap);
3780 beep_flush();
3781}
3782
3783#ifdef FEAT_VISUAL
3784/*
3785 * Remove the shift modifier from a special key.
3786 */
3787 static void
3788unshift_special(cap)
3789 cmdarg_T *cap;
3790{
3791 switch (cap->cmdchar)
3792 {
3793 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3794 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3795 case K_S_UP: cap->cmdchar = K_UP; break;
3796 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3797 case K_S_HOME: cap->cmdchar = K_HOME; break;
3798 case K_S_END: cap->cmdchar = K_END; break;
3799 }
3800 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3801}
3802#endif
3803
3804#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3805/*
3806 * Routines for displaying a partly typed command
3807 */
3808
3809#ifdef FEAT_VISUAL /* need room for size of Visual area */
3810# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3811#else
3812# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3813#endif
3814static char_u showcmd_buf[SHOWCMD_BUFLEN];
3815static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3816static int showcmd_is_clear = TRUE;
3817static int showcmd_visual = FALSE;
3818
3819static void display_showcmd __ARGS((void));
3820
3821 void
3822clear_showcmd()
3823{
3824 if (!p_sc)
3825 return;
3826
3827#ifdef FEAT_VISUAL
3828 if (VIsual_active && !char_avail())
3829 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003830 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 long lines;
3832 colnr_T leftcol, rightcol;
3833 linenr_T top, bot;
3834
3835 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003836 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 top = VIsual.lnum;
3839 bot = curwin->w_cursor.lnum;
3840 }
3841 else
3842 {
3843 top = curwin->w_cursor.lnum;
3844 bot = VIsual.lnum;
3845 }
3846# ifdef FEAT_FOLDING
3847 /* Include closed folds as a whole. */
3848 hasFolding(top, &top, NULL);
3849 hasFolding(bot, NULL, &bot);
3850# endif
3851 lines = bot - top + 1;
3852
3853 if (VIsual_mode == Ctrl_V)
3854 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003855# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003856 char_u *saved_sbr = p_sbr;
3857
3858 /* Make 'sbr' empty for a moment to get the correct size. */
3859 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003862# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003863 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3866 (long)(rightcol - leftcol + 1));
3867 }
3868 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3869 sprintf((char *)showcmd_buf, "%ld", lines);
3870 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003871 {
3872 char_u *s, *e;
3873 int l;
3874 int bytes = 0;
3875 int chars = 0;
3876
3877 if (cursor_bot)
3878 {
3879 s = ml_get_pos(&VIsual);
3880 e = ml_get_cursor();
3881 }
3882 else
3883 {
3884 s = ml_get_cursor();
3885 e = ml_get_pos(&VIsual);
3886 }
3887 while ((*p_sel != 'e') ? s <= e : s < e)
3888 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003889# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003890 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003891# else
3892 l = (*s == NUL) ? 0 : 1;
3893# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003894 if (l == 0)
3895 {
3896 ++bytes;
3897 ++chars;
3898 break; /* end of line */
3899 }
3900 bytes += l;
3901 ++chars;
3902 s += l;
3903 }
3904 if (bytes == chars)
3905 sprintf((char *)showcmd_buf, "%d", chars);
3906 else
3907 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3910 showcmd_visual = TRUE;
3911 }
3912 else
3913#endif
3914 {
3915 showcmd_buf[0] = NUL;
3916 showcmd_visual = FALSE;
3917
3918 /* Don't actually display something if there is nothing to clear. */
3919 if (showcmd_is_clear)
3920 return;
3921 }
3922
3923 display_showcmd();
3924}
3925
3926/*
3927 * Add 'c' to string of shown command chars.
3928 * Return TRUE if output has been written (and setcursor() has been called).
3929 */
3930 int
3931add_to_showcmd(c)
3932 int c;
3933{
3934 char_u *p;
3935 int old_len;
3936 int extra_len;
3937 int overflow;
3938#if defined(FEAT_MOUSE)
3939 int i;
3940 static int ignore[] =
3941 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003942# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3944 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 K_IGNORE,
3947 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3948 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3949 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003950 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003952 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 0
3954 };
3955#endif
3956
Bram Moolenaar09df3122006-01-23 22:23:09 +00003957 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 return FALSE;
3959
3960 if (showcmd_visual)
3961 {
3962 showcmd_buf[0] = NUL;
3963 showcmd_visual = FALSE;
3964 }
3965
3966#if defined(FEAT_MOUSE)
3967 /* Ignore keys that are scrollbar updates and mouse clicks */
3968 if (IS_SPECIAL(c))
3969 for (i = 0; ignore[i] != 0; ++i)
3970 if (ignore[i] == c)
3971 return FALSE;
3972#endif
3973
3974 p = transchar(c);
3975 old_len = (int)STRLEN(showcmd_buf);
3976 extra_len = (int)STRLEN(p);
3977 overflow = old_len + extra_len - SHOWCMD_COLS;
3978 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003979 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3980 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 STRCAT(showcmd_buf, p);
3982
3983 if (char_avail())
3984 return FALSE;
3985
3986 display_showcmd();
3987
3988 return TRUE;
3989}
3990
3991 void
3992add_to_showcmd_c(c)
3993 int c;
3994{
3995 if (!add_to_showcmd(c))
3996 setcursor();
3997}
3998
3999/*
4000 * Delete 'len' characters from the end of the shown command.
4001 */
4002 static void
4003del_from_showcmd(len)
4004 int len;
4005{
4006 int old_len;
4007
4008 if (!p_sc)
4009 return;
4010
4011 old_len = (int)STRLEN(showcmd_buf);
4012 if (len > old_len)
4013 len = old_len;
4014 showcmd_buf[old_len - len] = NUL;
4015
4016 if (!char_avail())
4017 display_showcmd();
4018}
4019
4020/*
4021 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4022 * something and there is a partial mapping.
4023 */
4024 void
4025push_showcmd()
4026{
4027 if (p_sc)
4028 STRCPY(old_showcmd_buf, showcmd_buf);
4029}
4030
4031 void
4032pop_showcmd()
4033{
4034 if (!p_sc)
4035 return;
4036
4037 STRCPY(showcmd_buf, old_showcmd_buf);
4038
4039 display_showcmd();
4040}
4041
4042 static void
4043display_showcmd()
4044{
4045 int len;
4046
4047 cursor_off();
4048
4049 len = (int)STRLEN(showcmd_buf);
4050 if (len == 0)
4051 showcmd_is_clear = TRUE;
4052 else
4053 {
4054 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4055 showcmd_is_clear = FALSE;
4056 }
4057
4058 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004059 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4060 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 */
4062 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4063
4064 setcursor(); /* put cursor back where it belongs */
4065}
4066#endif
4067
4068#ifdef FEAT_SCROLLBIND
4069/*
4070 * When "check" is FALSE, prepare for commands that scroll the window.
4071 * When "check" is TRUE, take care of scroll-binding after the window has
4072 * scrolled. Called from normal_cmd() and edit().
4073 */
4074 void
4075do_check_scrollbind(check)
4076 int check;
4077{
4078 static win_T *old_curwin = NULL;
4079 static linenr_T old_topline = 0;
4080#ifdef FEAT_DIFF
4081 static int old_topfill = 0;
4082#endif
4083 static buf_T *old_buf = NULL;
4084 static colnr_T old_leftcol = 0;
4085
4086 if (check && curwin->w_p_scb)
4087 {
4088 /* If a ":syncbind" command was just used, don't scroll, only reset
4089 * the values. */
4090 if (did_syncbind)
4091 did_syncbind = FALSE;
4092 else if (curwin == old_curwin)
4093 {
4094 /*
4095 * Synchronize other windows, as necessary according to
4096 * 'scrollbind'. Don't do this after an ":edit" command, except
4097 * when 'diff' is set.
4098 */
4099 if ((curwin->w_buffer == old_buf
4100#ifdef FEAT_DIFF
4101 || curwin->w_p_diff
4102#endif
4103 )
4104 && (curwin->w_topline != old_topline
4105#ifdef FEAT_DIFF
4106 || curwin->w_topfill != old_topfill
4107#endif
4108 || curwin->w_leftcol != old_leftcol))
4109 {
4110 check_scrollbind(curwin->w_topline - old_topline,
4111 (long)(curwin->w_leftcol - old_leftcol));
4112 }
4113 }
4114 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4115 {
4116 /*
4117 * When switching between windows, make sure that the relative
4118 * vertical offset is valid for the new window. The relative
4119 * offset is invalid whenever another 'scrollbind' window has
4120 * scrolled to a point that would force the current window to
4121 * scroll past the beginning or end of its buffer. When the
4122 * resync is performed, some of the other 'scrollbind' windows may
4123 * need to jump so that the current window's relative position is
4124 * visible on-screen.
4125 */
4126 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4127 }
4128 curwin->w_scbind_pos = curwin->w_topline;
4129 }
4130
4131 old_curwin = curwin;
4132 old_topline = curwin->w_topline;
4133#ifdef FEAT_DIFF
4134 old_topfill = curwin->w_topfill;
4135#endif
4136 old_buf = curwin->w_buffer;
4137 old_leftcol = curwin->w_leftcol;
4138}
4139
4140/*
4141 * Synchronize any windows that have "scrollbind" set, based on the
4142 * number of rows by which the current window has changed
4143 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4144 */
4145 void
4146check_scrollbind(topline_diff, leftcol_diff)
4147 linenr_T topline_diff;
4148 long leftcol_diff;
4149{
4150 int want_ver;
4151 int want_hor;
4152 win_T *old_curwin = curwin;
4153 buf_T *old_curbuf = curbuf;
4154#ifdef FEAT_VISUAL
4155 int old_VIsual_select = VIsual_select;
4156 int old_VIsual_active = VIsual_active;
4157#endif
4158 colnr_T tgt_leftcol = curwin->w_leftcol;
4159 long topline;
4160 long y;
4161
4162 /*
4163 * check 'scrollopt' string for vertical and horizontal scroll options
4164 */
4165 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4166#ifdef FEAT_DIFF
4167 want_ver |= old_curwin->w_p_diff;
4168#endif
4169 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4170
4171 /*
4172 * loop through the scrollbound windows and scroll accordingly
4173 */
4174#ifdef FEAT_VISUAL
4175 VIsual_select = VIsual_active = 0;
4176#endif
4177 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4178 {
4179 curbuf = curwin->w_buffer;
4180 /* skip original window and windows with 'noscrollbind' */
4181 if (curwin != old_curwin && curwin->w_p_scb)
4182 {
4183 /*
4184 * do the vertical scroll
4185 */
4186 if (want_ver)
4187 {
4188#ifdef FEAT_DIFF
4189 if (old_curwin->w_p_diff && curwin->w_p_diff)
4190 {
4191 diff_set_topline(old_curwin, curwin);
4192 }
4193 else
4194#endif
4195 {
4196 curwin->w_scbind_pos += topline_diff;
4197 topline = curwin->w_scbind_pos;
4198 if (topline > curbuf->b_ml.ml_line_count)
4199 topline = curbuf->b_ml.ml_line_count;
4200 if (topline < 1)
4201 topline = 1;
4202
4203 y = topline - curwin->w_topline;
4204 if (y > 0)
4205 scrollup(y, FALSE);
4206 else
4207 scrolldown(-y, FALSE);
4208 }
4209
4210 redraw_later(VALID);
4211 cursor_correct();
4212#ifdef FEAT_WINDOWS
4213 curwin->w_redr_status = TRUE;
4214#endif
4215 }
4216
4217 /*
4218 * do the horizontal scroll
4219 */
4220 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4221 {
4222 curwin->w_leftcol = tgt_leftcol;
4223 leftcol_changed();
4224 }
4225 }
4226 }
4227
4228 /*
4229 * reset current-window
4230 */
4231#ifdef FEAT_VISUAL
4232 VIsual_select = old_VIsual_select;
4233 VIsual_active = old_VIsual_active;
4234#endif
4235 curwin = old_curwin;
4236 curbuf = old_curbuf;
4237}
4238#endif /* #ifdef FEAT_SCROLLBIND */
4239
4240/*
4241 * Command character that's ignored.
4242 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004243 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 static void
4246nv_ignore(cap)
4247 cmdarg_T *cap;
4248{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004249 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250}
4251
4252/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004253 * Command character that doesn't do anything, but unlike nv_ignore() does
4254 * start edit(). Used for "startinsert" executed while starting up.
4255 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004256 static void
4257nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004258 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004259{
4260}
4261
4262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 * Command character doesn't exist.
4264 */
4265 static void
4266nv_error(cap)
4267 cmdarg_T *cap;
4268{
4269 clearopbeep(cap->oap);
4270}
4271
4272/*
4273 * <Help> and <F1> commands.
4274 */
4275 static void
4276nv_help(cap)
4277 cmdarg_T *cap;
4278{
4279 if (!checkclearopq(cap->oap))
4280 ex_help(NULL);
4281}
4282
4283/*
4284 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4285 */
4286 static void
4287nv_addsub(cap)
4288 cmdarg_T *cap;
4289{
4290 if (!checkclearopq(cap->oap)
4291 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4292 prep_redo_cmd(cap);
4293}
4294
4295/*
4296 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4297 */
4298 static void
4299nv_page(cap)
4300 cmdarg_T *cap;
4301{
4302 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004303 {
4304#ifdef FEAT_WINDOWS
4305 if (mod_mask & MOD_MASK_CTRL)
4306 {
4307 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4308 if (cap->arg == BACKWARD)
4309 goto_tabpage(-(int)cap->count1);
4310 else
4311 goto_tabpage((int)cap->count0);
4312 }
4313 else
4314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317}
4318
4319/*
4320 * Implementation of "gd" and "gD" command.
4321 */
4322 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004323nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 oparg_T *oap;
4325 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004326 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 char_u *ptr;
4330
Bram Moolenaard9d30582005-05-18 22:10:28 +00004331 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004332 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004334#ifdef FEAT_FOLDING
4335 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4336 foldOpenCursor();
4337#endif
4338}
4339
4340/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004341 * Search for variable declaration of "ptr[len]".
4342 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4343 * current file ("gD").
4344 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 * Return FAIL when not found.
4346 */
4347 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004348find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004349 char_u *ptr;
4350 int len;
4351 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004352 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004353 int searchflags; /* flags passed to searchit() */
4354{
4355 char_u *pat;
4356 pos_T old_pos;
4357 pos_T par_pos;
4358 pos_T found_pos;
4359 int t;
4360 int save_p_ws;
4361 int save_p_scs;
4362 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004363 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004364
4365 if ((pat = alloc(len + 7)) == NULL)
4366 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004367
4368 /* Put "\V" before the pattern to avoid that the special meaning of "."
4369 * and "~" causes trouble. */
4370 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4371 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 old_pos = curwin->w_cursor;
4373 save_p_ws = p_ws;
4374 save_p_scs = p_scs;
4375 p_ws = FALSE; /* don't wrap around end of file now */
4376 p_scs = FALSE; /* don't switch ignorecase off now */
4377
4378 /*
4379 * With "gD" go to line 1.
4380 * With "gd" Search back for the start of the current function, then go
4381 * back until a blank line. If this fails go to line 1.
4382 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004383 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 setpcmark(); /* Set in findpar() otherwise */
4386 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004387 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389 else
4390 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004391 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4393 --curwin->w_cursor.lnum;
4394 }
4395 curwin->w_cursor.col = 0;
4396
4397 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004398 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004399 for (;;)
4400 {
4401 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004402 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004403 if (curwin->w_cursor.lnum >= old_pos.lnum)
4404 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004405
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004406 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004407 {
4408 pos_T *pos;
4409
4410 /* Check that the block the match is in doesn't end before the
4411 * position where we started the search from. */
4412 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4413 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4414 && pos->lnum < old_pos.lnum)
4415 continue;
4416 }
4417
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004418 if (t == FAIL)
4419 {
4420 /* If we previously found a valid position, use it. */
4421 if (found_pos.lnum != 0)
4422 {
4423 curwin->w_cursor = found_pos;
4424 t = OK;
4425 }
4426 break;
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004429 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4430 {
4431 /* Ignore this line, continue at start of next line. */
4432 ++curwin->w_cursor.lnum;
4433 curwin->w_cursor.col = 0;
4434 continue;
4435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437 if (!locally) /* global search: use first match found */
4438 break;
4439 if (curwin->w_cursor.lnum >= par_pos.lnum)
4440 {
4441 /* If we previously found a valid position, use it. */
4442 if (found_pos.lnum != 0)
4443 curwin->w_cursor = found_pos;
4444 break;
4445 }
4446
4447 /* For finding a local variable and the match is before the "{" search
4448 * to find a later match. For K&R style function declarations this
4449 * skips the function header without types. */
4450 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004452
4453 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004455 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 curwin->w_cursor = old_pos;
4457 }
4458 else
4459 {
4460 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 /* "n" searches forward now */
4462 reset_search_dir();
4463 }
4464
4465 vim_free(pat);
4466 p_ws = save_p_ws;
4467 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004468
4469 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470}
4471
4472/*
4473 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4474 * lines rather than lines in the file.
4475 * 'dist' must be positive.
4476 *
4477 * Return OK if able to move cursor, FAIL otherwise.
4478 */
4479 static int
4480nv_screengo(oap, dir, dist)
4481 oparg_T *oap;
4482 int dir;
4483 long dist;
4484{
4485 int linelen = linetabsize(ml_get_curline());
4486 int retval = OK;
4487 int atend = FALSE;
4488 int n;
4489 int col_off1; /* margin offset for first screen line */
4490 int col_off2; /* margin offset for wrapped screen line */
4491 int width1; /* text width for first screen line */
4492 int width2; /* test width for wrapped screen line */
4493
4494 oap->motion_type = MCHAR;
4495 oap->inclusive = FALSE;
4496
4497 col_off1 = curwin_col_off();
4498 col_off2 = col_off1 - curwin_col_off2();
4499 width1 = W_WIDTH(curwin) - col_off1;
4500 width2 = W_WIDTH(curwin) - col_off2;
4501
4502#ifdef FEAT_VERTSPLIT
4503 if (curwin->w_width != 0)
4504 {
4505#endif
4506 /*
4507 * Instead of sticking at the last character of the buffer line we
4508 * try to stick in the last column of the screen.
4509 */
4510 if (curwin->w_curswant == MAXCOL)
4511 {
4512 atend = TRUE;
4513 validate_virtcol();
4514 if (width1 <= 0)
4515 curwin->w_curswant = 0;
4516 else
4517 {
4518 curwin->w_curswant = width1 - 1;
4519 if (curwin->w_virtcol > curwin->w_curswant)
4520 curwin->w_curswant += ((curwin->w_virtcol
4521 - curwin->w_curswant - 1) / width2 + 1) * width2;
4522 }
4523 }
4524 else
4525 {
4526 if (linelen > width1)
4527 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4528 else
4529 n = width1;
4530 if (curwin->w_curswant > (colnr_T)n + 1)
4531 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4532 * width2;
4533 }
4534
4535 while (dist--)
4536 {
4537 if (dir == BACKWARD)
4538 {
4539 if ((long)curwin->w_curswant >= width2)
4540 /* move back within line */
4541 curwin->w_curswant -= width2;
4542 else
4543 {
4544 /* to previous line */
4545 if (curwin->w_cursor.lnum == 1)
4546 {
4547 retval = FAIL;
4548 break;
4549 }
4550 --curwin->w_cursor.lnum;
4551#ifdef FEAT_FOLDING
4552 /* Move to the start of a closed fold. Don't do that when
4553 * 'foldopen' contains "all": it will open in a moment. */
4554 if (!(fdo_flags & FDO_ALL))
4555 (void)hasFolding(curwin->w_cursor.lnum,
4556 &curwin->w_cursor.lnum, NULL);
4557#endif
4558 linelen = linetabsize(ml_get_curline());
4559 if (linelen > width1)
4560 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4561 + 1) * width2;
4562 }
4563 }
4564 else /* dir == FORWARD */
4565 {
4566 if (linelen > width1)
4567 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4568 else
4569 n = width1;
4570 if (curwin->w_curswant + width2 < (colnr_T)n)
4571 /* move forward within line */
4572 curwin->w_curswant += width2;
4573 else
4574 {
4575 /* to next line */
4576#ifdef FEAT_FOLDING
4577 /* Move to the end of a closed fold. */
4578 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4579 &curwin->w_cursor.lnum);
4580#endif
4581 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4582 {
4583 retval = FAIL;
4584 break;
4585 }
4586 curwin->w_cursor.lnum++;
4587 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004588 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 }
4591 }
4592#ifdef FEAT_VERTSPLIT
4593 }
4594#endif
4595
4596 coladvance(curwin->w_curswant);
4597
4598#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4599 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4600 {
4601 /*
4602 * Check for landing on a character that got split at the end of the
4603 * last line. We want to advance a screenline, not end up in the same
4604 * screenline or move two screenlines.
4605 */
4606 validate_virtcol();
4607 if (curwin->w_virtcol > curwin->w_curswant
4608 && (curwin->w_curswant < (colnr_T)width1
4609 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4610 : ((curwin->w_curswant - width1) % width2
4611 > (colnr_T)width2 / 2)))
4612 --curwin->w_cursor.col;
4613 }
4614#endif
4615
4616 if (atend)
4617 curwin->w_curswant = MAXCOL; /* stick in the last column */
4618
4619 return retval;
4620}
4621
4622#ifdef FEAT_MOUSE
4623/*
4624 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4625 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004626 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4627 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 */
4629 static void
4630nv_mousescroll(cap)
4631 cmdarg_T *cap;
4632{
4633# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004634 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 /* Currently we only get the mouse coordinates in the GUI. */
4637 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4638 {
4639 int row, col;
4640
4641 row = mouse_row;
4642 col = mouse_col;
4643
4644 /* find the window at the pointer coordinates */
4645 curwin = mouse_find_win(&row, &col);
4646 curbuf = curwin->w_buffer;
4647 }
4648# endif
4649
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4653 {
4654 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4655 }
4656 else
4657 {
4658 cap->count1 = 3;
4659 cap->count0 = 3;
4660 nv_scroll_line(cap);
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else
4665 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004666 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4667 if (!curwin->w_p_wrap)
4668 {
4669 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004670
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4672 step = W_WIDTH(curwin);
4673 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4674 if (val < 0)
4675 val = 0;
4676
4677 gui_do_horiz_scroll(val, TRUE);
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
4682# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4683 curwin->w_redr_status = TRUE;
4684
4685 curwin = old_curwin;
4686 curbuf = curwin->w_buffer;
4687# endif
4688}
4689
4690/*
4691 * Mouse clicks and drags.
4692 */
4693 static void
4694nv_mouse(cap)
4695 cmdarg_T *cap;
4696{
4697 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4698}
4699#endif
4700
4701/*
4702 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4703 * cap->arg must be TRUE for CTRL-E.
4704 */
4705 static void
4706nv_scroll_line(cap)
4707 cmdarg_T *cap;
4708{
4709 if (!checkclearop(cap->oap))
4710 scroll_redraw(cap->arg, cap->count1);
4711}
4712
4713/*
4714 * Scroll "count" lines up or down, and redraw.
4715 */
4716 void
4717scroll_redraw(up, count)
4718 int up;
4719 long count;
4720{
4721 linenr_T prev_topline = curwin->w_topline;
4722#ifdef FEAT_DIFF
4723 int prev_topfill = curwin->w_topfill;
4724#endif
4725 linenr_T prev_lnum = curwin->w_cursor.lnum;
4726
4727 if (up)
4728 scrollup(count, TRUE);
4729 else
4730 scrolldown(count, TRUE);
4731 if (p_so)
4732 {
4733 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4734 * valid, otherwise the screen jumps back at the end of the file. */
4735 cursor_correct();
4736 check_cursor_moved(curwin);
4737 curwin->w_valid |= VALID_TOPLINE;
4738
4739 /* If moved back to where we were, at least move the cursor, otherwise
4740 * we get stuck at one position. Don't move the cursor up if the
4741 * first line of the buffer is already on the screen */
4742 while (curwin->w_topline == prev_topline
4743#ifdef FEAT_DIFF
4744 && curwin->w_topfill == prev_topfill
4745#endif
4746 )
4747 {
4748 if (up)
4749 {
4750 if (curwin->w_cursor.lnum > prev_lnum
4751 || cursor_down(1L, FALSE) == FAIL)
4752 break;
4753 }
4754 else
4755 {
4756 if (curwin->w_cursor.lnum < prev_lnum
4757 || prev_topline == 1L
4758 || cursor_up(1L, FALSE) == FAIL)
4759 break;
4760 }
4761 /* Mark w_topline as valid, otherwise the screen jumps back at the
4762 * end of the file. */
4763 check_cursor_moved(curwin);
4764 curwin->w_valid |= VALID_TOPLINE;
4765 }
4766 }
4767 if (curwin->w_cursor.lnum != prev_lnum)
4768 coladvance(curwin->w_curswant);
4769 redraw_later(VALID);
4770}
4771
4772/*
4773 * Commands that start with "z".
4774 */
4775 static void
4776nv_zet(cap)
4777 cmdarg_T *cap;
4778{
4779 long n;
4780 colnr_T col;
4781 int nchar = cap->nchar;
4782#ifdef FEAT_FOLDING
4783 long old_fdl = curwin->w_p_fdl;
4784 int old_fen = curwin->w_p_fen;
4785#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004786#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004787 int undo = FALSE;
4788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 if (VIM_ISDIGIT(nchar))
4791 {
4792 /*
4793 * "z123{nchar}": edit the count before obtaining {nchar}
4794 */
4795 if (checkclearop(cap->oap))
4796 return;
4797 n = nchar - '0';
4798 for (;;)
4799 {
4800#ifdef USE_ON_FLY_SCROLL
4801 dont_scroll = TRUE; /* disallow scrolling here */
4802#endif
4803 ++no_mapping;
4804 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004805 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 --no_mapping;
4808 --allow_keys;
4809#ifdef FEAT_CMDL_INFO
4810 (void)add_to_showcmd(nchar);
4811#endif
4812 if (nchar == K_DEL || nchar == K_KDEL)
4813 n /= 10;
4814 else if (VIM_ISDIGIT(nchar))
4815 n = n * 10 + (nchar - '0');
4816 else if (nchar == CAR)
4817 {
4818#ifdef FEAT_GUI
4819 need_mouse_correct = TRUE;
4820#endif
4821 win_setheight((int)n);
4822 break;
4823 }
4824 else if (nchar == 'l'
4825 || nchar == 'h'
4826 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004827 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 cap->count1 = n ? n * cap->count1 : cap->count1;
4830 goto dozet;
4831 }
4832 else
4833 {
4834 clearopbeep(cap->oap);
4835 break;
4836 }
4837 }
4838 cap->oap->op_type = OP_NOP;
4839 return;
4840 }
4841
4842dozet:
4843 if (
4844#ifdef FEAT_FOLDING
4845 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4846 * and "zC" only in Visual mode. "zj" and "zk" are motion
4847 * commands. */
4848 cap->nchar != 'f' && cap->nchar != 'F'
4849 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4850 && cap->nchar != 'j' && cap->nchar != 'k'
4851 &&
4852#endif
4853 checkclearop(cap->oap))
4854 return;
4855
4856 /*
4857 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4858 * If line number given, set cursor.
4859 */
4860 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4861 && cap->count0
4862 && cap->count0 != curwin->w_cursor.lnum)
4863 {
4864 setpcmark();
4865 if (cap->count0 > curbuf->b_ml.ml_line_count)
4866 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4867 else
4868 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004869 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871
4872 switch (nchar)
4873 {
4874 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4875 case '+':
4876 if (cap->count0 == 0)
4877 {
4878 /* No count given: put cursor at the line below screen */
4879 validate_botline(); /* make sure w_botline is valid */
4880 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4881 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4882 else
4883 curwin->w_cursor.lnum = curwin->w_botline;
4884 }
4885 /* FALLTHROUGH */
4886 case NL:
4887 case CAR:
4888 case K_KENTER:
4889 beginline(BL_WHITE | BL_FIX);
4890 /* FALLTHROUGH */
4891
4892 case 't': scroll_cursor_top(0, TRUE);
4893 redraw_later(VALID);
4894 break;
4895
4896 /* "z." and "zz": put cursor in middle of screen */
4897 case '.': beginline(BL_WHITE | BL_FIX);
4898 /* FALLTHROUGH */
4899
4900 case 'z': scroll_cursor_halfway(TRUE);
4901 redraw_later(VALID);
4902 break;
4903
4904 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4905 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4906 * when <count> is at bottom of window, and puts that one at
4907 * bottom of window. */
4908 if (cap->count0 != 0)
4909 {
4910 scroll_cursor_bot(0, TRUE);
4911 curwin->w_cursor.lnum = curwin->w_topline;
4912 }
4913 else if (curwin->w_topline == 1)
4914 curwin->w_cursor.lnum = 1;
4915 else
4916 curwin->w_cursor.lnum = curwin->w_topline - 1;
4917 /* FALLTHROUGH */
4918 case '-':
4919 beginline(BL_WHITE | BL_FIX);
4920 /* FALLTHROUGH */
4921
4922 case 'b': scroll_cursor_bot(0, TRUE);
4923 redraw_later(VALID);
4924 break;
4925
4926 /* "zH" - scroll screen right half-page */
4927 case 'H':
4928 cap->count1 *= W_WIDTH(curwin) / 2;
4929 /* FALLTHROUGH */
4930
4931 /* "zh" - scroll screen to the right */
4932 case 'h':
4933 case K_LEFT:
4934 if (!curwin->w_p_wrap)
4935 {
4936 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4937 curwin->w_leftcol = 0;
4938 else
4939 curwin->w_leftcol -= (colnr_T)cap->count1;
4940 leftcol_changed();
4941 }
4942 break;
4943
4944 /* "zL" - scroll screen left half-page */
4945 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4946 /* FALLTHROUGH */
4947
4948 /* "zl" - scroll screen to the left */
4949 case 'l':
4950 case K_RIGHT:
4951 if (!curwin->w_p_wrap)
4952 {
4953 /* scroll the window left */
4954 curwin->w_leftcol += (colnr_T)cap->count1;
4955 leftcol_changed();
4956 }
4957 break;
4958
4959 /* "zs" - scroll screen, cursor at the start */
4960 case 's': if (!curwin->w_p_wrap)
4961 {
4962#ifdef FEAT_FOLDING
4963 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4964 col = 0; /* like the cursor is in col 0 */
4965 else
4966#endif
4967 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4968 if ((long)col > p_siso)
4969 col -= p_siso;
4970 else
4971 col = 0;
4972 if (curwin->w_leftcol != col)
4973 {
4974 curwin->w_leftcol = col;
4975 redraw_later(NOT_VALID);
4976 }
4977 }
4978 break;
4979
4980 /* "ze" - scroll screen, cursor at the end */
4981 case 'e': if (!curwin->w_p_wrap)
4982 {
4983#ifdef FEAT_FOLDING
4984 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4985 col = 0; /* like the cursor is in col 0 */
4986 else
4987#endif
4988 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4989 n = W_WIDTH(curwin) - curwin_col_off();
4990 if ((long)col + p_siso < n)
4991 col = 0;
4992 else
4993 col = col + p_siso - n + 1;
4994 if (curwin->w_leftcol != col)
4995 {
4996 curwin->w_leftcol = col;
4997 redraw_later(NOT_VALID);
4998 }
4999 }
5000 break;
5001
5002#ifdef FEAT_FOLDING
5003 /* "zF": create fold command */
5004 /* "zf": create fold operator */
5005 case 'F':
5006 case 'f': if (foldManualAllowed(TRUE))
5007 {
5008 cap->nchar = 'f';
5009 nv_operator(cap);
5010 curwin->w_p_fen = TRUE;
5011
5012 /* "zF" is like "zfzf" */
5013 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5014 {
5015 nv_operator(cap);
5016 finish_op = TRUE;
5017 }
5018 }
5019 else
5020 clearopbeep(cap->oap);
5021 break;
5022
5023 /* "zd": delete fold at cursor */
5024 /* "zD": delete fold at cursor recursively */
5025 case 'd':
5026 case 'D': if (foldManualAllowed(FALSE))
5027 {
5028 if (VIsual_active)
5029 nv_operator(cap);
5030 else
5031 deleteFold(curwin->w_cursor.lnum,
5032 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5033 }
5034 break;
5035
5036 /* "zE": erease all folds */
5037 case 'E': if (foldmethodIsManual(curwin))
5038 {
5039 clearFolding(curwin);
5040 changed_window_setting();
5041 }
5042 else if (foldmethodIsMarker(curwin))
5043 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5044 TRUE, FALSE);
5045 else
5046 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5047 break;
5048
5049 /* "zn": fold none: reset 'foldenable' */
5050 case 'n': curwin->w_p_fen = FALSE;
5051 break;
5052
5053 /* "zN": fold Normal: set 'foldenable' */
5054 case 'N': curwin->w_p_fen = TRUE;
5055 break;
5056
5057 /* "zi": invert folding: toggle 'foldenable' */
5058 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5059 break;
5060
5061 /* "za": open closed fold or close open fold at cursor */
5062 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5063 openFold(curwin->w_cursor.lnum, cap->count1);
5064 else
5065 {
5066 closeFold(curwin->w_cursor.lnum, cap->count1);
5067 curwin->w_p_fen = TRUE;
5068 }
5069 break;
5070
5071 /* "zA": open fold at cursor recursively */
5072 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5073 openFoldRecurse(curwin->w_cursor.lnum);
5074 else
5075 {
5076 closeFoldRecurse(curwin->w_cursor.lnum);
5077 curwin->w_p_fen = TRUE;
5078 }
5079 break;
5080
5081 /* "zo": open fold at cursor or Visual area */
5082 case 'o': if (VIsual_active)
5083 nv_operator(cap);
5084 else
5085 openFold(curwin->w_cursor.lnum, cap->count1);
5086 break;
5087
5088 /* "zO": open fold recursively */
5089 case 'O': if (VIsual_active)
5090 nv_operator(cap);
5091 else
5092 openFoldRecurse(curwin->w_cursor.lnum);
5093 break;
5094
5095 /* "zc": close fold at cursor or Visual area */
5096 case 'c': if (VIsual_active)
5097 nv_operator(cap);
5098 else
5099 closeFold(curwin->w_cursor.lnum, cap->count1);
5100 curwin->w_p_fen = TRUE;
5101 break;
5102
5103 /* "zC": close fold recursively */
5104 case 'C': if (VIsual_active)
5105 nv_operator(cap);
5106 else
5107 closeFoldRecurse(curwin->w_cursor.lnum);
5108 curwin->w_p_fen = TRUE;
5109 break;
5110
5111 /* "zv": open folds at the cursor */
5112 case 'v': foldOpenCursor();
5113 break;
5114
5115 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5116 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005117 curwin->w_foldinvalid = TRUE; /* recompute folds */
5118 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 foldOpenCursor();
5120 break;
5121
5122 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5123 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005124 curwin->w_foldinvalid = TRUE; /* recompute folds */
5125 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 break;
5127
5128 /* "zm": fold more */
5129 case 'm': if (curwin->w_p_fdl > 0)
5130 --curwin->w_p_fdl;
5131 old_fdl = -1; /* force an update */
5132 curwin->w_p_fen = TRUE;
5133 break;
5134
5135 /* "zM": close all folds */
5136 case 'M': curwin->w_p_fdl = 0;
5137 old_fdl = -1; /* force an update */
5138 curwin->w_p_fen = TRUE;
5139 break;
5140
5141 /* "zr": reduce folding */
5142 case 'r': ++curwin->w_p_fdl;
5143 break;
5144
5145 /* "zR": open all folds */
5146 case 'R': curwin->w_p_fdl = getDeepestNesting();
5147 old_fdl = -1; /* force an update */
5148 break;
5149
5150 case 'j': /* "zj" move to next fold downwards */
5151 case 'k': /* "zk" move to next fold upwards */
5152 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5153 cap->count1) == FAIL)
5154 clearopbeep(cap->oap);
5155 break;
5156
5157#endif /* FEAT_FOLDING */
5158
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005159#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005160 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5161 ++no_mapping;
5162 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005163 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005164 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005165 --no_mapping;
5166 --allow_keys;
5167#ifdef FEAT_CMDL_INFO
5168 (void)add_to_showcmd(nchar);
5169#endif
5170 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5171 {
5172 clearopbeep(cap->oap);
5173 break;
5174 }
5175 undo = TRUE;
5176 /*FALLTHROUGH*/
5177
Bram Moolenaarb765d632005-06-07 21:00:02 +00005178 case 'g': /* "zg": add good word to word list */
5179 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005180 case 'G': /* "zG": add good word to temp word list */
5181 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005182 {
5183 char_u *ptr = NULL;
5184 int len;
5185
5186 if (checkclearop(cap->oap))
5187 break;
5188# ifdef FEAT_VISUAL
5189 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5190 == FAIL)
5191 return;
5192# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005193 if (ptr == NULL)
5194 {
5195 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005196
5197 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005198 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005199 if (len != 0 && curwin->w_cursor.col <= pos.col)
5200 ptr = ml_get_pos(&curwin->w_cursor);
5201 curwin->w_cursor = pos;
5202 }
5203
Bram Moolenaarb765d632005-06-07 21:00:02 +00005204 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5205 FIND_IDENT)) == 0)
5206 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005207 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005208 (nchar == 'G' || nchar == 'W')
5209 ? 0 : (int)cap->count1,
5210 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005211 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005212 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005213
Bram Moolenaar43abc522005-12-10 20:15:02 +00005214 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005215 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005216 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005218#endif
5219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 default: clearopbeep(cap->oap);
5221 }
5222
5223#ifdef FEAT_FOLDING
5224 /* Redraw when 'foldenable' changed */
5225 if (old_fen != curwin->w_p_fen)
5226 {
5227# ifdef FEAT_DIFF
5228 win_T *wp;
5229
5230 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5231 {
5232 /* Adjust 'foldenable' in diff-synced windows. */
5233 FOR_ALL_WINDOWS(wp)
5234 {
5235 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5236 {
5237 wp->w_p_fen = curwin->w_p_fen;
5238 changed_window_setting_win(wp);
5239 }
5240 }
5241 }
5242# endif
5243 changed_window_setting();
5244 }
5245
5246 /* Redraw when 'foldlevel' changed. */
5247 if (old_fdl != curwin->w_p_fdl)
5248 newFoldLevel();
5249#endif
5250}
5251
5252#ifdef FEAT_GUI
5253/*
5254 * Vertical scrollbar movement.
5255 */
5256 static void
5257nv_ver_scrollbar(cap)
5258 cmdarg_T *cap;
5259{
5260 if (cap->oap->op_type != OP_NOP)
5261 clearopbeep(cap->oap);
5262
5263 /* Even if an operator was pending, we still want to scroll */
5264 gui_do_scroll();
5265}
5266
5267/*
5268 * Horizontal scrollbar movement.
5269 */
5270 static void
5271nv_hor_scrollbar(cap)
5272 cmdarg_T *cap;
5273{
5274 if (cap->oap->op_type != OP_NOP)
5275 clearopbeep(cap->oap);
5276
5277 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005278 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279}
5280#endif
5281
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005282#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005283/*
5284 * Click in GUI tab.
5285 */
5286 static void
5287nv_tabline(cap)
5288 cmdarg_T *cap;
5289{
5290 if (cap->oap->op_type != OP_NOP)
5291 clearopbeep(cap->oap);
5292
5293 /* Even if an operator was pending, we still want to jump tabs. */
5294 goto_tabpage(current_tab);
5295}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005296
5297/*
5298 * Selected item in tab line menu.
5299 */
5300 static void
5301nv_tabmenu(cap)
5302 cmdarg_T *cap;
5303{
5304 if (cap->oap->op_type != OP_NOP)
5305 clearopbeep(cap->oap);
5306
5307 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005308 handle_tabmenu();
5309}
5310
5311/*
5312 * Handle selecting an item of the GUI tab line menu.
5313 * Used in Normal and Insert mode.
5314 */
5315 void
5316handle_tabmenu()
5317{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005318 switch (current_tabmenu)
5319 {
5320 case TABLINE_MENU_CLOSE:
5321 if (current_tab == 0)
5322 do_cmdline_cmd((char_u *)"tabclose");
5323 else
5324 {
5325 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5326 current_tab);
5327 do_cmdline_cmd(IObuff);
5328 }
5329 break;
5330
5331 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005332 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5333 current_tab > 0 ? current_tab - 1 : 999);
5334 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005335 break;
5336
5337 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005338 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5339 current_tab > 0 ? current_tab - 1 : 999);
5340 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005341 break;
5342 }
5343}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005344#endif
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346/*
5347 * "Q" command.
5348 */
5349 static void
5350nv_exmode(cap)
5351 cmdarg_T *cap;
5352{
5353 /*
5354 * Ignore 'Q' in Visual mode, just give a beep.
5355 */
5356#ifdef FEAT_VISUAL
5357 if (VIsual_active)
5358 vim_beep();
5359 else
5360#endif
5361 if (!checkclearop(cap->oap))
5362 do_exmode(FALSE);
5363}
5364
5365/*
5366 * Handle a ":" command.
5367 */
5368 static void
5369nv_colon(cap)
5370 cmdarg_T *cap;
5371{
5372 int old_p_im;
5373
5374#ifdef FEAT_VISUAL
5375 if (VIsual_active)
5376 nv_operator(cap);
5377 else
5378#endif
5379 {
5380 if (cap->oap->op_type != OP_NOP)
5381 {
5382 /* Using ":" as a movement is characterwise exclusive. */
5383 cap->oap->motion_type = MCHAR;
5384 cap->oap->inclusive = FALSE;
5385 }
5386 else if (cap->count0)
5387 {
5388 /* translate "count:" into ":.,.+(count - 1)" */
5389 stuffcharReadbuff('.');
5390 if (cap->count0 > 1)
5391 {
5392 stuffReadbuff((char_u *)",.+");
5393 stuffnumReadbuff((long)cap->count0 - 1L);
5394 }
5395 }
5396
5397 /* When typing, don't type below an old message */
5398 if (KeyTyped)
5399 compute_cmdrow();
5400
5401 old_p_im = p_im;
5402
5403 /* get a command line and execute it */
5404 do_cmdline(NULL, getexline, NULL,
5405 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5406
5407 /* If 'insertmode' changed, enter or exit Insert mode */
5408 if (p_im != old_p_im)
5409 {
5410 if (p_im)
5411 restart_edit = 'i';
5412 else
5413 restart_edit = 0;
5414 }
5415
5416 /* The start of the operator may have become invalid by the Ex
5417 * command. */
5418 if (cap->oap->op_type != OP_NOP
5419 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5420 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005421 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 clearopbeep(cap->oap);
5423 }
5424}
5425
5426/*
5427 * Handle CTRL-G command.
5428 */
5429 static void
5430nv_ctrlg(cap)
5431 cmdarg_T *cap;
5432{
5433#ifdef FEAT_VISUAL
5434 if (VIsual_active) /* toggle Selection/Visual mode */
5435 {
5436 VIsual_select = !VIsual_select;
5437 showmode();
5438 }
5439 else
5440#endif
5441 if (!checkclearop(cap->oap))
5442 /* print full name if count given or :cd used */
5443 fileinfo((int)cap->count0, FALSE, TRUE);
5444}
5445
5446/*
5447 * Handle CTRL-H <Backspace> command.
5448 */
5449 static void
5450nv_ctrlh(cap)
5451 cmdarg_T *cap;
5452{
5453#ifdef FEAT_VISUAL
5454 if (VIsual_active && VIsual_select)
5455 {
5456 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5457 v_visop(cap);
5458 }
5459 else
5460#endif
5461 nv_left(cap);
5462}
5463
5464/*
5465 * CTRL-L: clear screen and redraw.
5466 */
5467 static void
5468nv_clear(cap)
5469 cmdarg_T *cap;
5470{
5471 if (!checkclearop(cap->oap))
5472 {
5473#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5474 /*
5475 * Right now, the BeBox doesn't seem to have an easy way to detect
5476 * window resizing, so we cheat and make the user detect it
5477 * manually with CTRL-L instead
5478 */
5479 ui_get_shellsize();
5480#endif
5481#ifdef FEAT_SYN_HL
5482 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005483 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#endif
5485 redraw_later(CLEAR);
5486 }
5487}
5488
5489/*
5490 * CTRL-O: In Select mode: switch to Visual mode for one command.
5491 * Otherwise: Go to older pcmark.
5492 */
5493 static void
5494nv_ctrlo(cap)
5495 cmdarg_T *cap;
5496{
5497#ifdef FEAT_VISUAL
5498 if (VIsual_active && VIsual_select)
5499 {
5500 VIsual_select = FALSE;
5501 showmode();
5502 restart_VIsual_select = 2; /* restart Select mode later */
5503 }
5504 else
5505#endif
5506 {
5507 cap->count1 = -cap->count1;
5508 nv_pcmark(cap);
5509 }
5510}
5511
5512/*
5513 * CTRL-^ command, short for ":e #"
5514 */
5515 static void
5516nv_hat(cap)
5517 cmdarg_T *cap;
5518{
5519 if (!checkclearopq(cap->oap))
5520 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5521 GETF_SETMARK|GETF_ALT, FALSE);
5522}
5523
5524/*
5525 * "Z" commands.
5526 */
5527 static void
5528nv_Zet(cap)
5529 cmdarg_T *cap;
5530{
5531 if (!checkclearopq(cap->oap))
5532 {
5533 switch (cap->nchar)
5534 {
5535 /* "ZZ": equivalent to ":x". */
5536 case 'Z': do_cmdline_cmd((char_u *)"x");
5537 break;
5538
5539 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5540 case 'Q': do_cmdline_cmd((char_u *)"q!");
5541 break;
5542
5543 default: clearopbeep(cap->oap);
5544 }
5545 }
5546}
5547
5548#if defined(FEAT_WINDOWS) || defined(PROTO)
5549/*
5550 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5551 */
5552 void
5553do_nv_ident(c1, c2)
5554 int c1;
5555 int c2;
5556{
5557 oparg_T oa;
5558 cmdarg_T ca;
5559
5560 clear_oparg(&oa);
5561 vim_memset(&ca, 0, sizeof(ca));
5562 ca.oap = &oa;
5563 ca.cmdchar = c1;
5564 ca.nchar = c2;
5565 nv_ident(&ca);
5566}
5567#endif
5568
5569/*
5570 * Handle the commands that use the word under the cursor.
5571 * [g] CTRL-] :ta to current identifier
5572 * [g] 'K' run program for current identifier
5573 * [g] '*' / to current identifier or string
5574 * [g] '#' ? to current identifier or string
5575 * g ']' :tselect for current identifier
5576 */
5577 static void
5578nv_ident(cap)
5579 cmdarg_T *cap;
5580{
5581 char_u *ptr = NULL;
5582 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005583 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 char_u *p;
5585 char_u *kp; /* value of 'keywordprg' */
5586 int kp_help; /* 'keywordprg' is ":help" */
5587 int n = 0; /* init for GCC */
5588 int cmdchar;
5589 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005590 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 char_u *aux_ptr;
5592 int isman;
5593 int isman_s;
5594
5595 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5596 {
5597 cmdchar = cap->nchar;
5598 g_cmd = TRUE;
5599 }
5600 else
5601 {
5602 cmdchar = cap->cmdchar;
5603 g_cmd = FALSE;
5604 }
5605
5606 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5607 cmdchar = '#';
5608
5609 /*
5610 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5611 */
5612 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5613 {
5614#ifdef FEAT_VISUAL
5615 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5616 return;
5617#endif
5618 if (checkclearopq(cap->oap))
5619 return;
5620 }
5621
5622 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5623 (cmdchar == '*' || cmdchar == '#')
5624 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5625 {
5626 clearop(cap->oap);
5627 return;
5628 }
5629
5630 /* Allocate buffer to put the command in. Inserting backslashes can
5631 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5632 * and some numbers. */
5633 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5634 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5635 || STRCMP(kp, ":help") == 0);
5636 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5637 if (buf == NULL)
5638 return;
5639 buf[0] = NUL;
5640
5641 switch (cmdchar)
5642 {
5643 case '*':
5644 case '#':
5645 /*
5646 * Put cursor at start of word, makes search skip the word
5647 * under the cursor.
5648 * Call setpcmark() first, so "*``" puts the cursor back where
5649 * it was.
5650 */
5651 setpcmark();
5652 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5653
5654 if (!g_cmd && vim_iswordp(ptr))
5655 STRCPY(buf, "\\<");
5656 no_smartcase = TRUE; /* don't use 'smartcase' now */
5657 break;
5658
5659 case 'K':
5660 if (kp_help)
5661 STRCPY(buf, "he! ");
5662 else
5663 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005664 /* An external command will probably use an argument starting
5665 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005666 while (*ptr == '-' && n > 0)
5667 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005669 --n;
5670 }
5671 if (n == 0)
5672 {
5673 EMSG(_(e_noident)); /* found dashes only */
5674 vim_free(buf);
5675 return;
5676 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 /* When a count is given, turn it into a range. Is this
5679 * really what we want? */
5680 isman = (STRCMP(kp, "man") == 0);
5681 isman_s = (STRCMP(kp, "man -s") == 0);
5682 if (cap->count0 != 0 && !(isman || isman_s))
5683 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5684
5685 STRCAT(buf, "! ");
5686 if (cap->count0 == 0 && isman_s)
5687 STRCAT(buf, "man");
5688 else
5689 STRCAT(buf, kp);
5690 STRCAT(buf, " ");
5691 if (cap->count0 != 0 && (isman || isman_s))
5692 {
5693 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5694 STRCAT(buf, " ");
5695 }
5696 }
5697 break;
5698
5699 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005700 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#ifdef FEAT_CSCOPE
5702 if (p_cst)
5703 STRCPY(buf, "cstag ");
5704 else
5705#endif
5706 STRCPY(buf, "ts ");
5707 break;
5708
5709 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005710 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (curbuf->b_help)
5712 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005714 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005715 if (g_cmd)
5716 STRCPY(buf, "tj ");
5717 else
5718 sprintf((char *)buf, "%ldta ", cap->count0);
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721
5722 /*
5723 * Now grab the chars in the identifier
5724 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005725 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005727 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005728 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005729 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005730 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005731 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005733 vim_free(buf);
5734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005736 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5737 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005738 {
5739 vim_free(buf);
5740 vim_free(p);
5741 return;
5742 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005743 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 STRCAT(buf, p);
5745 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 else
5748 {
5749 if (cmdchar == '*')
5750 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5751 else if (cmdchar == '#')
5752 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005753 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005754 {
5755 if (curbuf->b_help)
5756 /* ":help" handles unescaped argument */
5757 aux_ptr = (char_u *)"";
5758 else
5759 aux_ptr = (char_u *)"\\|\"\n[";
5760 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005761 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005762 aux_ptr = (char_u *)"\\|\"\n*?[";
5763
5764 p = buf + STRLEN(buf);
5765 while (n-- > 0)
5766 {
5767 /* put a backslash before \ and some others */
5768 if (vim_strchr(aux_ptr, *ptr) != NULL)
5769 *p++ = '\\';
5770#ifdef FEAT_MBYTE
5771 /* When current byte is a part of multibyte character, copy all
5772 * bytes of that character. */
5773 if (has_mbyte)
5774 {
5775 int i;
5776 int len = (*mb_ptr2len)(ptr) - 1;
5777
5778 for (i = 0; i < len && n >= 1; ++i, --n)
5779 *p++ = *ptr++;
5780 }
5781#endif
5782 *p++ = *ptr++;
5783 }
5784 *p = NUL;
5785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 /*
5788 * Execute the command.
5789 */
5790 if (cmdchar == '*' || cmdchar == '#')
5791 {
5792 if (!g_cmd && (
5793#ifdef FEAT_MBYTE
5794 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5795#endif
5796 vim_iswordc(ptr[-1])))
5797 STRCAT(buf, "\\>");
5798#ifdef FEAT_CMDHIST
5799 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005800 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5802#endif
5803 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5804 }
5805 else
5806 do_cmdline_cmd(buf);
5807
5808 vim_free(buf);
5809}
5810
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005811#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812/*
5813 * Get visually selected text, within one line only.
5814 * Returns FAIL if more than one line selected.
5815 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005816 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817get_visual_text(cap, pp, lenp)
5818 cmdarg_T *cap;
5819 char_u **pp; /* return: start of selected text */
5820 int *lenp; /* return: length of selected text */
5821{
5822 if (VIsual_mode != 'V')
5823 unadjust_for_sel();
5824 if (VIsual.lnum != curwin->w_cursor.lnum)
5825 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005826 if (cap != NULL)
5827 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 return FAIL;
5829 }
5830 if (VIsual_mode == 'V')
5831 {
5832 *pp = ml_get_curline();
5833 *lenp = (int)STRLEN(*pp);
5834 }
5835 else
5836 {
5837 if (lt(curwin->w_cursor, VIsual))
5838 {
5839 *pp = ml_get_pos(&curwin->w_cursor);
5840 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5841 }
5842 else
5843 {
5844 *pp = ml_get_pos(&VIsual);
5845 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5846 }
5847#ifdef FEAT_MBYTE
5848 if (has_mbyte)
5849 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005850 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
5852 }
5853 reset_VIsual_and_resel();
5854 return OK;
5855}
5856#endif
5857
5858/*
5859 * CTRL-T: backwards in tag stack
5860 */
5861 static void
5862nv_tagpop(cap)
5863 cmdarg_T *cap;
5864{
5865 if (!checkclearopq(cap->oap))
5866 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5867}
5868
5869/*
5870 * Handle scrolling command 'H', 'L' and 'M'.
5871 */
5872 static void
5873nv_scroll(cap)
5874 cmdarg_T *cap;
5875{
5876 int used = 0;
5877 long n;
5878#ifdef FEAT_FOLDING
5879 linenr_T lnum;
5880#endif
5881 int half;
5882
5883 cap->oap->motion_type = MLINE;
5884 setpcmark();
5885
5886 if (cap->cmdchar == 'L')
5887 {
5888 validate_botline(); /* make sure curwin->w_botline is valid */
5889 curwin->w_cursor.lnum = curwin->w_botline - 1;
5890 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5891 curwin->w_cursor.lnum = 1;
5892 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005893 {
5894#ifdef FEAT_FOLDING
5895 if (hasAnyFolding(curwin))
5896 {
5897 /* Count a fold for one screen line. */
5898 for (n = cap->count1 - 1; n > 0
5899 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5900 {
5901 (void)hasFolding(curwin->w_cursor.lnum,
5902 &curwin->w_cursor.lnum, NULL);
5903 --curwin->w_cursor.lnum;
5904 }
5905 }
5906 else
5907#endif
5908 curwin->w_cursor.lnum -= cap->count1 - 1;
5909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
5911 else
5912 {
5913 if (cap->cmdchar == 'M')
5914 {
5915#ifdef FEAT_DIFF
5916 /* Don't count filler lines above the window. */
5917 used -= diff_check_fill(curwin, curwin->w_topline)
5918 - curwin->w_topfill;
5919#endif
5920 validate_botline(); /* make sure w_empty_rows is valid */
5921 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5922 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5923 {
5924#ifdef FEAT_DIFF
5925 /* Count half he number of filler lines to be "below this
5926 * line" and half to be "above the next line". */
5927 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5928 + n) / 2 >= half)
5929 {
5930 --n;
5931 break;
5932 }
5933#endif
5934 used += plines(curwin->w_topline + n);
5935 if (used >= half)
5936 break;
5937#ifdef FEAT_FOLDING
5938 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5939 n = lnum - curwin->w_topline;
5940#endif
5941 }
5942 if (n > 0 && used > curwin->w_height)
5943 --n;
5944 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005945 else /* (cap->cmdchar == 'H') */
5946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005948#ifdef FEAT_FOLDING
5949 if (hasAnyFolding(curwin))
5950 {
5951 /* Count a fold for one screen line. */
5952 lnum = curwin->w_topline;
5953 while (n-- > 0 && lnum < curwin->w_botline - 1)
5954 {
5955 hasFolding(lnum, NULL, &lnum);
5956 ++lnum;
5957 }
5958 n = lnum - curwin->w_topline;
5959 }
5960#endif
5961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 curwin->w_cursor.lnum = curwin->w_topline + n;
5963 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5964 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5965 }
5966
5967 cursor_correct(); /* correct for 'so' */
5968 beginline(BL_SOL | BL_FIX);
5969}
5970
5971/*
5972 * Cursor right commands.
5973 */
5974 static void
5975nv_right(cap)
5976 cmdarg_T *cap;
5977{
5978 long n;
5979#ifdef FEAT_VISUAL
5980 int PAST_LINE;
5981#else
5982# define PAST_LINE 0
5983#endif
5984
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005985 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5986 {
5987 /* <C-Right> and <S-Right> move a word or WORD right */
5988 if (mod_mask & MOD_MASK_CTRL)
5989 cap->arg = TRUE;
5990 nv_wordcmd(cap);
5991 return;
5992 }
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 cap->oap->motion_type = MCHAR;
5995 cap->oap->inclusive = FALSE;
5996#ifdef FEAT_VISUAL
5997 PAST_LINE = (VIsual_active && *p_sel != 'o');
5998
5999# ifdef FEAT_VIRTUALEDIT
6000 /*
6001 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006002 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 */
6004 if (virtual_active())
6005 PAST_LINE = 0;
6006# endif
6007#endif
6008
6009 for (n = cap->count1; n > 0; --n)
6010 {
6011 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006012#ifdef FEAT_VISUAL
6013 || (PAST_LINE && *ml_get_cursor() == NUL)
6014#endif
6015 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
6017 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006018 * <Space> wraps to next line if 'whichwrap' has 's'.
6019 * 'l' wraps to next line if 'whichwrap' has 'l'.
6020 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 */
6022 if ( ((cap->cmdchar == ' '
6023 && vim_strchr(p_ww, 's') != NULL)
6024 || (cap->cmdchar == 'l'
6025 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006026 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 && vim_strchr(p_ww, '>') != NULL))
6028 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6029 {
6030 /* When deleting we also count the NL as a character.
6031 * Set cap->oap->inclusive when last char in the line is
6032 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006033 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 && !cap->oap->inclusive
6035 && !lineempty(curwin->w_cursor.lnum))
6036 cap->oap->inclusive = TRUE;
6037 else
6038 {
6039 ++curwin->w_cursor.lnum;
6040 curwin->w_cursor.col = 0;
6041#ifdef FEAT_VIRTUALEDIT
6042 curwin->w_cursor.coladd = 0;
6043#endif
6044 curwin->w_set_curswant = TRUE;
6045 cap->oap->inclusive = FALSE;
6046 }
6047 continue;
6048 }
6049 if (cap->oap->op_type == OP_NOP)
6050 {
6051 /* Only beep and flush if not moved at all */
6052 if (n == cap->count1)
6053 beep_flush();
6054 }
6055 else
6056 {
6057 if (!lineempty(curwin->w_cursor.lnum))
6058 cap->oap->inclusive = TRUE;
6059 }
6060 break;
6061 }
6062#ifdef FEAT_VISUAL
6063 else if (PAST_LINE)
6064 {
6065 curwin->w_set_curswant = TRUE;
6066# ifdef FEAT_VIRTUALEDIT
6067 if (virtual_active())
6068 oneright();
6069 else
6070# endif
6071 {
6072# ifdef FEAT_MBYTE
6073 if (has_mbyte)
6074 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 else
6077# endif
6078 ++curwin->w_cursor.col;
6079 }
6080 }
6081#endif
6082 }
6083#ifdef FEAT_FOLDING
6084 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6085 && cap->oap->op_type == OP_NOP)
6086 foldOpenCursor();
6087#endif
6088}
6089
6090/*
6091 * Cursor left commands.
6092 *
6093 * Returns TRUE when operator end should not be adjusted.
6094 */
6095 static void
6096nv_left(cap)
6097 cmdarg_T *cap;
6098{
6099 long n;
6100
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6102 {
6103 /* <C-Left> and <S-Left> move a word or WORD left */
6104 if (mod_mask & MOD_MASK_CTRL)
6105 cap->arg = 1;
6106 nv_bck_word(cap);
6107 return;
6108 }
6109
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 cap->oap->motion_type = MCHAR;
6111 cap->oap->inclusive = FALSE;
6112 for (n = cap->count1; n > 0; --n)
6113 {
6114 if (oneleft() == FAIL)
6115 {
6116 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6117 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6118 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6119 */
6120 if ( (((cap->cmdchar == K_BS
6121 || cap->cmdchar == Ctrl_H)
6122 && vim_strchr(p_ww, 'b') != NULL)
6123 || (cap->cmdchar == 'h'
6124 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006125 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 && vim_strchr(p_ww, '<') != NULL))
6127 && curwin->w_cursor.lnum > 1)
6128 {
6129 --(curwin->w_cursor.lnum);
6130 coladvance((colnr_T)MAXCOL);
6131 curwin->w_set_curswant = TRUE;
6132
6133 /* When the NL before the first char has to be deleted we
6134 * put the cursor on the NUL after the previous line.
6135 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006136 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 if ( (cap->oap->op_type == OP_DELETE
6138 || cap->oap->op_type == OP_CHANGE)
6139 && !lineempty(curwin->w_cursor.lnum))
6140 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006141 if (*ml_get_cursor() != NUL)
6142 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 cap->retval |= CA_NO_ADJ_OP_END;
6144 }
6145 continue;
6146 }
6147 /* Only beep and flush if not moved at all */
6148 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6149 beep_flush();
6150 break;
6151 }
6152 }
6153#ifdef FEAT_FOLDING
6154 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6155 && cap->oap->op_type == OP_NOP)
6156 foldOpenCursor();
6157#endif
6158}
6159
6160/*
6161 * Cursor up commands.
6162 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6163 */
6164 static void
6165nv_up(cap)
6166 cmdarg_T *cap;
6167{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006168 if (mod_mask & MOD_MASK_SHIFT)
6169 {
6170 /* <S-Up> is page up */
6171 cap->arg = BACKWARD;
6172 nv_page(cap);
6173 }
6174 else
6175 {
6176 cap->oap->motion_type = MLINE;
6177 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6178 clearopbeep(cap->oap);
6179 else if (cap->arg)
6180 beginline(BL_WHITE | BL_FIX);
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182}
6183
6184/*
6185 * Cursor down commands.
6186 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6187 */
6188 static void
6189nv_down(cap)
6190 cmdarg_T *cap;
6191{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006192 if (mod_mask & MOD_MASK_SHIFT)
6193 {
6194 /* <S-Down> is page down */
6195 cap->arg = FORWARD;
6196 nv_page(cap);
6197 }
6198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6200 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006201 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006202 if (curwin->w_llist_ref == NULL)
6203 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6204 else
6205 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 else
6207#endif
6208 {
6209#ifdef FEAT_CMDWIN
6210 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006211 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 cmdwin_result = CAR;
6213 else
6214#endif
6215 {
6216 cap->oap->motion_type = MLINE;
6217 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6218 clearopbeep(cap->oap);
6219 else if (cap->arg)
6220 beginline(BL_WHITE | BL_FIX);
6221 }
6222 }
6223}
6224
6225#ifdef FEAT_SEARCHPATH
6226/*
6227 * Grab the file name under the cursor and edit it.
6228 */
6229 static void
6230nv_gotofile(cap)
6231 cmdarg_T *cap;
6232{
6233 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006234 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006236 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 {
6238 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006239 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 return;
6241 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006242#ifdef FEAT_AUTOCMD
6243 if (curbuf_locked())
6244 {
6245 clearop(cap->oap);
6246 return;
6247 }
6248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006250 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
6252 if (ptr != NULL)
6253 {
6254 /* do autowrite if necessary */
6255 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6256 autowrite(curbuf, FALSE);
6257 setpcmark();
6258 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006259 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006260 if (cap->nchar == 'F' && lnum >= 0)
6261 {
6262 curwin->w_cursor.lnum = lnum;
6263 check_cursor_lnum();
6264 beginline(BL_SOL | BL_FIX);
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 vim_free(ptr);
6267 }
6268 else
6269 clearop(cap->oap);
6270}
6271#endif
6272
6273/*
6274 * <End> command: to end of current line or last line.
6275 */
6276 static void
6277nv_end(cap)
6278 cmdarg_T *cap;
6279{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006280 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006282 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 nv_goto(cap);
6284 cap->count1 = 1; /* to end of current line */
6285 }
6286 nv_dollar(cap);
6287}
6288
6289/*
6290 * Handle the "$" command.
6291 */
6292 static void
6293nv_dollar(cap)
6294 cmdarg_T *cap;
6295{
6296 cap->oap->motion_type = MCHAR;
6297 cap->oap->inclusive = TRUE;
6298#ifdef FEAT_VIRTUALEDIT
6299 /* In virtual mode when off the edge of a line and an operator
6300 * is pending (whew!) keep the cursor where it is.
6301 * Otherwise, send it to the end of the line. */
6302 if (!virtual_active() || gchar_cursor() != NUL
6303 || cap->oap->op_type == OP_NOP)
6304#endif
6305 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6306 if (cursor_down((long)(cap->count1 - 1),
6307 cap->oap->op_type == OP_NOP) == FAIL)
6308 clearopbeep(cap->oap);
6309#ifdef FEAT_FOLDING
6310 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6311 foldOpenCursor();
6312#endif
6313}
6314
6315/*
6316 * Implementation of '?' and '/' commands.
6317 * If cap->arg is TRUE don't set PC mark.
6318 */
6319 static void
6320nv_search(cap)
6321 cmdarg_T *cap;
6322{
6323 oparg_T *oap = cap->oap;
6324
6325 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6326 {
6327 /* Translate "g??" to "g?g?" */
6328 cap->cmdchar = 'g';
6329 cap->nchar = '?';
6330 nv_operator(cap);
6331 return;
6332 }
6333
6334 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6335
6336 if (cap->searchbuf == NULL)
6337 {
6338 clearop(oap);
6339 return;
6340 }
6341
6342 normal_search(cap, cap->cmdchar, cap->searchbuf,
6343 (cap->arg ? 0 : SEARCH_MARK));
6344}
6345
6346/*
6347 * Handle "N" and "n" commands.
6348 * cap->arg is SEARCH_REV for "N", 0 for "n".
6349 */
6350 static void
6351nv_next(cap)
6352 cmdarg_T *cap;
6353{
6354 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6355}
6356
6357/*
6358 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6359 * Uses only cap->count1 and cap->oap from "cap".
6360 */
6361 static void
6362normal_search(cap, dir, pat, opt)
6363 cmdarg_T *cap;
6364 int dir;
6365 char_u *pat;
6366 int opt; /* extra flags for do_search() */
6367{
6368 int i;
6369
6370 cap->oap->motion_type = MCHAR;
6371 cap->oap->inclusive = FALSE;
6372 cap->oap->use_reg_one = TRUE;
6373 curwin->w_set_curswant = TRUE;
6374
6375 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006376 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 if (i == 0)
6378 clearop(cap->oap);
6379 else
6380 {
6381 if (i == 2)
6382 cap->oap->motion_type = MLINE;
6383#ifdef FEAT_VIRTUALEDIT
6384 curwin->w_cursor.coladd = 0;
6385#endif
6386#ifdef FEAT_FOLDING
6387 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6388 foldOpenCursor();
6389#endif
6390 }
6391
6392 /* "/$" will put the cursor after the end of the line, may need to
6393 * correct that here */
6394 check_cursor();
6395}
6396
6397/*
6398 * Character search commands.
6399 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6400 * ',' and FALSE for ';'.
6401 * cap->nchar is NUL for ',' and ';' (repeat the search)
6402 */
6403 static void
6404nv_csearch(cap)
6405 cmdarg_T *cap;
6406{
6407 int t_cmd;
6408
6409 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6410 t_cmd = TRUE;
6411 else
6412 t_cmd = FALSE;
6413
6414 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6416 clearopbeep(cap->oap);
6417 else
6418 {
6419 curwin->w_set_curswant = TRUE;
6420#ifdef FEAT_VIRTUALEDIT
6421 /* Include a Tab for "tx" and for "dfx". */
6422 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6423 && (t_cmd || cap->oap->op_type != OP_NOP))
6424 {
6425 colnr_T scol, ecol;
6426
6427 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6428 curwin->w_cursor.coladd = ecol - scol;
6429 }
6430 else
6431 curwin->w_cursor.coladd = 0;
6432#endif
6433#ifdef FEAT_VISUAL
6434 adjust_for_sel(cap);
6435#endif
6436#ifdef FEAT_FOLDING
6437 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6438 foldOpenCursor();
6439#endif
6440 }
6441}
6442
6443/*
6444 * "[" and "]" commands.
6445 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6446 */
6447 static void
6448nv_brackets(cap)
6449 cmdarg_T *cap;
6450{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006451 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 pos_T prev_pos;
6453 pos_T *pos = NULL; /* init for GCC */
6454 pos_T old_pos; /* cursor position before command */
6455 int flag;
6456 long n;
6457 int findc;
6458 int c;
6459
6460 cap->oap->motion_type = MCHAR;
6461 cap->oap->inclusive = FALSE;
6462 old_pos = curwin->w_cursor;
6463#ifdef FEAT_VIRTUALEDIT
6464 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6465#endif
6466
6467#ifdef FEAT_SEARCHPATH
6468 /*
6469 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6470 */
6471 if (cap->nchar == 'f')
6472 nv_gotofile(cap);
6473 else
6474#endif
6475
6476#ifdef FEAT_FIND_ID
6477 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006478 * Find the occurrence(s) of the identifier or define under cursor
6479 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 *
6481 * search list jump
6482 * fwd bwd fwd bwd fwd bwd
6483 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6484 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6485 */
6486 if (vim_strchr((char_u *)
6487#ifdef EBCDIC
6488 "iI\005dD\067",
6489#else
6490 "iI\011dD\004",
6491#endif
6492 cap->nchar) != NULL)
6493 {
6494 char_u *ptr;
6495 int len;
6496
6497 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6498 clearop(cap->oap);
6499 else
6500 {
6501 find_pattern_in_path(ptr, 0, len, TRUE,
6502 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6503 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6504 cap->count1,
6505 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6506 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6507 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6508 (linenr_T)MAXLNUM);
6509 curwin->w_set_curswant = TRUE;
6510 }
6511 }
6512 else
6513#endif
6514
6515 /*
6516 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6517 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6518 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6519 * "[m" or "]m" search for prev/next start of (Java) method.
6520 * "[M" or "]M" search for prev/next end of (Java) method.
6521 */
6522 if ( (cap->cmdchar == '['
6523 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6524 || (cap->cmdchar == ']'
6525 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6526 {
6527 if (cap->nchar == '*')
6528 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 prev_pos.lnum = 0;
6530 if (cap->nchar == 'm' || cap->nchar == 'M')
6531 {
6532 if (cap->cmdchar == '[')
6533 findc = '{';
6534 else
6535 findc = '}';
6536 n = 9999;
6537 }
6538 else
6539 {
6540 findc = cap->nchar;
6541 n = cap->count1;
6542 }
6543 for ( ; n > 0; --n)
6544 {
6545 if ((pos = findmatchlimit(cap->oap, findc,
6546 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6547 {
6548 if (new_pos.lnum == 0) /* nothing found */
6549 {
6550 if (cap->nchar != 'm' && cap->nchar != 'M')
6551 clearopbeep(cap->oap);
6552 }
6553 else
6554 pos = &new_pos; /* use last one found */
6555 break;
6556 }
6557 prev_pos = new_pos;
6558 curwin->w_cursor = *pos;
6559 new_pos = *pos;
6560 }
6561 curwin->w_cursor = old_pos;
6562
6563 /*
6564 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6565 * brought us to the match for "[m" and "]M" when inside a method.
6566 * Try finding the '{' or '}' we want to be at.
6567 * Also repeat for the given count.
6568 */
6569 if (cap->nchar == 'm' || cap->nchar == 'M')
6570 {
6571 /* norm is TRUE for "]M" and "[m" */
6572 int norm = ((findc == '{') == (cap->nchar == 'm'));
6573
6574 n = cap->count1;
6575 /* found a match: we were inside a method */
6576 if (prev_pos.lnum != 0)
6577 {
6578 pos = &prev_pos;
6579 curwin->w_cursor = prev_pos;
6580 if (norm)
6581 --n;
6582 }
6583 else
6584 pos = NULL;
6585 while (n > 0)
6586 {
6587 for (;;)
6588 {
6589 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6590 {
6591 /* if not found anything, that's an error */
6592 if (pos == NULL)
6593 clearopbeep(cap->oap);
6594 n = 0;
6595 break;
6596 }
6597 c = gchar_cursor();
6598 if (c == '{' || c == '}')
6599 {
6600 /* Must have found end/start of class: use it.
6601 * Or found the place to be at. */
6602 if ((c == findc && norm) || (n == 1 && !norm))
6603 {
6604 new_pos = curwin->w_cursor;
6605 pos = &new_pos;
6606 n = 0;
6607 }
6608 /* if no match found at all, we started outside of the
6609 * class and we're inside now. Just go on. */
6610 else if (new_pos.lnum == 0)
6611 {
6612 new_pos = curwin->w_cursor;
6613 pos = &new_pos;
6614 }
6615 /* found start/end of other method: go to match */
6616 else if ((pos = findmatchlimit(cap->oap, findc,
6617 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6618 0)) == NULL)
6619 n = 0;
6620 else
6621 curwin->w_cursor = *pos;
6622 break;
6623 }
6624 }
6625 --n;
6626 }
6627 curwin->w_cursor = old_pos;
6628 if (pos == NULL && new_pos.lnum != 0)
6629 clearopbeep(cap->oap);
6630 }
6631 if (pos != NULL)
6632 {
6633 setpcmark();
6634 curwin->w_cursor = *pos;
6635 curwin->w_set_curswant = TRUE;
6636#ifdef FEAT_FOLDING
6637 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6638 && cap->oap->op_type == OP_NOP)
6639 foldOpenCursor();
6640#endif
6641 }
6642 }
6643
6644 /*
6645 * "[[", "[]", "]]" and "][": move to start or end of function
6646 */
6647 else if (cap->nchar == '[' || cap->nchar == ']')
6648 {
6649 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6650 flag = '{';
6651 else
6652 flag = '}'; /* "][" or "[]" */
6653
6654 curwin->w_set_curswant = TRUE;
6655 /*
6656 * Imitate strange Vi behaviour: When using "]]" with an operator
6657 * we also stop at '}'.
6658 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006659 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 (cap->oap->op_type != OP_NOP
6661 && cap->arg == FORWARD && flag == '{')))
6662 clearopbeep(cap->oap);
6663 else
6664 {
6665 if (cap->oap->op_type == OP_NOP)
6666 beginline(BL_WHITE | BL_FIX);
6667#ifdef FEAT_FOLDING
6668 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6669 foldOpenCursor();
6670#endif
6671 }
6672 }
6673
6674 /*
6675 * "[p", "[P", "]P" and "]p": put with indent adjustment
6676 */
6677 else if (cap->nchar == 'p' || cap->nchar == 'P')
6678 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006679 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
6681 prep_redo_cmd(cap);
6682 do_put(cap->oap->regname,
6683 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6684 cap->count1, PUT_FIXINDENT);
6685 }
6686 }
6687
6688 /*
6689 * "['", "[`", "]'" and "]`": jump to next mark
6690 */
6691 else if (cap->nchar == '\'' || cap->nchar == '`')
6692 {
6693 pos = &curwin->w_cursor;
6694 for (n = cap->count1; n > 0; --n)
6695 {
6696 prev_pos = *pos;
6697 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6698 cap->nchar == '\'');
6699 if (pos == NULL)
6700 break;
6701 }
6702 if (pos == NULL)
6703 pos = &prev_pos;
6704 nv_cursormark(cap, cap->nchar == '\'', pos);
6705 }
6706
6707#ifdef FEAT_MOUSE
6708 /*
6709 * [ or ] followed by a middle mouse click: put selected text with
6710 * indent adjustment. Any other button just does as usual.
6711 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006712 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
6714 (void)do_mouse(cap->oap, cap->nchar,
6715 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6716 cap->count1, PUT_FIXINDENT);
6717 }
6718#endif /* FEAT_MOUSE */
6719
6720#ifdef FEAT_FOLDING
6721 /*
6722 * "[z" and "]z": move to start or end of open fold.
6723 */
6724 else if (cap->nchar == 'z')
6725 {
6726 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6727 cap->count1) == FAIL)
6728 clearopbeep(cap->oap);
6729 }
6730#endif
6731
6732#ifdef FEAT_DIFF
6733 /*
6734 * "[c" and "]c": move to next or previous diff-change.
6735 */
6736 else if (cap->nchar == 'c')
6737 {
6738 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6739 cap->count1) == FAIL)
6740 clearopbeep(cap->oap);
6741 }
6742#endif
6743
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006744#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006745 /*
6746 * "[s", "[S", "]s" and "]S": move to next spell error.
6747 */
6748 else if (cap->nchar == 's' || cap->nchar == 'S')
6749 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006750 setpcmark();
6751 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006752 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6753 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006754 {
6755 clearopbeep(cap->oap);
6756 break;
6757 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006758# ifdef FEAT_FOLDING
6759 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6760 foldOpenCursor();
6761# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006762 }
6763#endif
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 /* Not a valid cap->nchar. */
6766 else
6767 clearopbeep(cap->oap);
6768}
6769
6770/*
6771 * Handle Normal mode "%" command.
6772 */
6773 static void
6774nv_percent(cap)
6775 cmdarg_T *cap;
6776{
6777 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006778#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 linenr_T lnum = curwin->w_cursor.lnum;
6780#endif
6781
6782 cap->oap->inclusive = TRUE;
6783 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6784 {
6785 if (cap->count0 > 100)
6786 clearopbeep(cap->oap);
6787 else
6788 {
6789 cap->oap->motion_type = MLINE;
6790 setpcmark();
6791 /* Round up, so CTRL-G will give same value. Watch out for a
6792 * large line count, the line number must not go negative! */
6793 if (curbuf->b_ml.ml_line_count > 1000000)
6794 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6795 / 100L * cap->count0;
6796 else
6797 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6798 cap->count0 + 99L) / 100L;
6799 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6801 beginline(BL_SOL | BL_FIX);
6802 }
6803 }
6804 else /* "%" : go to matching paren */
6805 {
6806 cap->oap->motion_type = MCHAR;
6807 cap->oap->use_reg_one = TRUE;
6808 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6809 clearopbeep(cap->oap);
6810 else
6811 {
6812 setpcmark();
6813 curwin->w_cursor = *pos;
6814 curwin->w_set_curswant = TRUE;
6815#ifdef FEAT_VIRTUALEDIT
6816 curwin->w_cursor.coladd = 0;
6817#endif
6818#ifdef FEAT_VISUAL
6819 adjust_for_sel(cap);
6820#endif
6821 }
6822 }
6823#ifdef FEAT_FOLDING
6824 if (cap->oap->op_type == OP_NOP
6825 && lnum != curwin->w_cursor.lnum
6826 && (fdo_flags & FDO_PERCENT)
6827 && KeyTyped)
6828 foldOpenCursor();
6829#endif
6830}
6831
6832/*
6833 * Handle "(" and ")" commands.
6834 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6835 */
6836 static void
6837nv_brace(cap)
6838 cmdarg_T *cap;
6839{
6840 cap->oap->motion_type = MCHAR;
6841 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006842 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6843 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 curwin->w_set_curswant = TRUE;
6845
6846 if (findsent(cap->arg, cap->count1) == FAIL)
6847 clearopbeep(cap->oap);
6848 else
6849 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006850 /* Don't leave the cursor on the NUL past end of line. */
6851 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852#ifdef FEAT_VIRTUALEDIT
6853 curwin->w_cursor.coladd = 0;
6854#endif
6855#ifdef FEAT_FOLDING
6856 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6857 foldOpenCursor();
6858#endif
6859 }
6860}
6861
6862/*
6863 * "m" command: Mark a position.
6864 */
6865 static void
6866nv_mark(cap)
6867 cmdarg_T *cap;
6868{
6869 if (!checkclearop(cap->oap))
6870 {
6871 if (setmark(cap->nchar) == FAIL)
6872 clearopbeep(cap->oap);
6873 }
6874}
6875
6876/*
6877 * "{" and "}" commands.
6878 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6879 */
6880 static void
6881nv_findpar(cap)
6882 cmdarg_T *cap;
6883{
6884 cap->oap->motion_type = MCHAR;
6885 cap->oap->inclusive = FALSE;
6886 cap->oap->use_reg_one = TRUE;
6887 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006888 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 clearopbeep(cap->oap);
6890 else
6891 {
6892#ifdef FEAT_VIRTUALEDIT
6893 curwin->w_cursor.coladd = 0;
6894#endif
6895#ifdef FEAT_FOLDING
6896 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6897 foldOpenCursor();
6898#endif
6899 }
6900}
6901
6902/*
6903 * "u" command: Undo or make lower case.
6904 */
6905 static void
6906nv_undo(cap)
6907 cmdarg_T *cap;
6908{
6909 if (cap->oap->op_type == OP_LOWER
6910#ifdef FEAT_VISUAL
6911 || VIsual_active
6912#endif
6913 )
6914 {
6915 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6916 cap->cmdchar = 'g';
6917 cap->nchar = 'u';
6918 nv_operator(cap);
6919 }
6920 else
6921 nv_kundo(cap);
6922}
6923
6924/*
6925 * <Undo> command.
6926 */
6927 static void
6928nv_kundo(cap)
6929 cmdarg_T *cap;
6930{
6931 if (!checkclearopq(cap->oap))
6932 {
6933 u_undo((int)cap->count1);
6934 curwin->w_set_curswant = TRUE;
6935 }
6936}
6937
6938/*
6939 * Handle the "r" command.
6940 */
6941 static void
6942nv_replace(cap)
6943 cmdarg_T *cap;
6944{
6945 char_u *ptr;
6946 int had_ctrl_v;
6947 long n;
6948
6949 if (checkclearop(cap->oap))
6950 return;
6951
6952 /* get another character */
6953 if (cap->nchar == Ctrl_V)
6954 {
6955 had_ctrl_v = Ctrl_V;
6956 cap->nchar = get_literal();
6957 /* Don't redo a multibyte character with CTRL-V. */
6958 if (cap->nchar > DEL)
6959 had_ctrl_v = NUL;
6960 }
6961 else
6962 had_ctrl_v = NUL;
6963
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006964 /* Abort if the character is a special key. */
6965 if (IS_SPECIAL(cap->nchar))
6966 {
6967 clearopbeep(cap->oap);
6968 return;
6969 }
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971#ifdef FEAT_VISUAL
6972 /* Visual mode "r" */
6973 if (VIsual_active)
6974 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006975 if (got_int)
6976 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 nv_operator(cap);
6978 return;
6979 }
6980#endif
6981
6982#ifdef FEAT_VIRTUALEDIT
6983 /* Break tabs, etc. */
6984 if (virtual_active())
6985 {
6986 if (u_save_cursor() == FAIL)
6987 return;
6988 if (gchar_cursor() == NUL)
6989 {
6990 /* Add extra space and put the cursor on the first one. */
6991 coladvance_force((colnr_T)(getviscol() + cap->count1));
6992 curwin->w_cursor.col -= cap->count1;
6993 }
6994 else if (gchar_cursor() == TAB)
6995 coladvance_force(getviscol());
6996 }
6997#endif
6998
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006999 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007001 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002#ifdef FEAT_MBYTE
7003 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7004#endif
7005 )
7006 {
7007 clearopbeep(cap->oap);
7008 return;
7009 }
7010
7011 /*
7012 * Replacing with a TAB is done by edit() when it is complicated because
7013 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7014 * Other characters are done below to avoid problems with things like
7015 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7016 */
7017 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7018 {
7019 stuffnumReadbuff(cap->count1);
7020 stuffcharReadbuff('R');
7021 stuffcharReadbuff('\t');
7022 stuffcharReadbuff(ESC);
7023 return;
7024 }
7025
7026 /* save line for undo */
7027 if (u_save_cursor() == FAIL)
7028 return;
7029
7030 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7031 {
7032 /*
7033 * Replace character(s) by a single newline.
7034 * Strange vi behaviour: Only one newline is inserted.
7035 * Delete the characters here.
7036 * Insert the newline with an insert command, takes care of
7037 * autoindent. The insert command depends on being on the last
7038 * character of a line or not.
7039 */
7040#ifdef FEAT_MBYTE
7041 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7042#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007043 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044#endif
7045 stuffcharReadbuff('\r');
7046 stuffcharReadbuff(ESC);
7047
7048 /* Give 'r' to edit(), to get the redo command right. */
7049 invoke_edit(cap, TRUE, 'r', FALSE);
7050 }
7051 else
7052 {
7053 prep_redo(cap->oap->regname, cap->count1,
7054 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7055
7056 curbuf->b_op_start = curwin->w_cursor;
7057#ifdef FEAT_MBYTE
7058 if (has_mbyte)
7059 {
7060 int old_State = State;
7061
7062 if (cap->ncharC1 != 0)
7063 AppendCharToRedobuff(cap->ncharC1);
7064 if (cap->ncharC2 != 0)
7065 AppendCharToRedobuff(cap->ncharC2);
7066
7067 /* This is slow, but it handles replacing a single-byte with a
7068 * multi-byte and the other way around. Also handles adding
7069 * composing characters for utf-8. */
7070 for (n = cap->count1; n > 0; --n)
7071 {
7072 State = REPLACE;
7073 ins_char(cap->nchar);
7074 State = old_State;
7075 if (cap->ncharC1 != 0)
7076 ins_char(cap->ncharC1);
7077 if (cap->ncharC2 != 0)
7078 ins_char(cap->ncharC2);
7079 }
7080 }
7081 else
7082#endif
7083 {
7084 /*
7085 * Replace the characters within one line.
7086 */
7087 for (n = cap->count1; n > 0; --n)
7088 {
7089 /*
7090 * Get ptr again, because u_save and/or showmatch() will have
7091 * released the line. At the same time we let know that the
7092 * line will be changed.
7093 */
7094 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7095 ptr[curwin->w_cursor.col] = cap->nchar;
7096 if (p_sm && msg_silent == 0)
7097 showmatch(cap->nchar);
7098 ++curwin->w_cursor.col;
7099 }
7100#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007101 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007103 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
Bram Moolenaar009b2592004-10-24 19:18:58 +00007105 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007106 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007108 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110#endif
7111
7112 /* mark the buffer as changed and prepare for displaying */
7113 changed_bytes(curwin->w_cursor.lnum,
7114 (colnr_T)(curwin->w_cursor.col - cap->count1));
7115 }
7116 --curwin->w_cursor.col; /* cursor on the last replaced char */
7117#ifdef FEAT_MBYTE
7118 /* if the character on the left of the current cursor is a multi-byte
7119 * character, move two characters left */
7120 if (has_mbyte)
7121 mb_adjust_cursor();
7122#endif
7123 curbuf->b_op_end = curwin->w_cursor;
7124 curwin->w_set_curswant = TRUE;
7125 set_last_insert(cap->nchar);
7126 }
7127}
7128
7129#ifdef FEAT_VISUAL
7130/*
7131 * 'o': Exchange start and end of Visual area.
7132 * 'O': same, but in block mode exchange left and right corners.
7133 */
7134 static void
7135v_swap_corners(cmdchar)
7136 int cmdchar;
7137{
7138 pos_T old_cursor;
7139 colnr_T left, right;
7140
7141 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7142 {
7143 old_cursor = curwin->w_cursor;
7144 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7145 curwin->w_cursor.lnum = VIsual.lnum;
7146 coladvance(left);
7147 VIsual = curwin->w_cursor;
7148
7149 curwin->w_cursor.lnum = old_cursor.lnum;
7150 curwin->w_curswant = right;
7151 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7152 * right one column */
7153 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7154 ++curwin->w_curswant;
7155 coladvance(curwin->w_curswant);
7156 if (curwin->w_cursor.col == old_cursor.col
7157#ifdef FEAT_VIRTUALEDIT
7158 && (!virtual_active()
7159 || curwin->w_cursor.coladd == old_cursor.coladd)
7160#endif
7161 )
7162 {
7163 curwin->w_cursor.lnum = VIsual.lnum;
7164 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7165 ++right;
7166 coladvance(right);
7167 VIsual = curwin->w_cursor;
7168
7169 curwin->w_cursor.lnum = old_cursor.lnum;
7170 coladvance(left);
7171 curwin->w_curswant = left;
7172 }
7173 }
7174 else
7175 {
7176 old_cursor = curwin->w_cursor;
7177 curwin->w_cursor = VIsual;
7178 VIsual = old_cursor;
7179 curwin->w_set_curswant = TRUE;
7180 }
7181}
7182#endif /* FEAT_VISUAL */
7183
7184/*
7185 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7186 */
7187 static void
7188nv_Replace(cap)
7189 cmdarg_T *cap;
7190{
7191#ifdef FEAT_VISUAL
7192 if (VIsual_active) /* "R" is replace lines */
7193 {
7194 cap->cmdchar = 'c';
7195 cap->nchar = NUL;
7196 VIsual_mode = 'V';
7197 nv_operator(cap);
7198 }
7199 else
7200#endif
7201 if (!checkclearopq(cap->oap))
7202 {
7203 if (!curbuf->b_p_ma)
7204 EMSG(_(e_modifiable));
7205 else
7206 {
7207#ifdef FEAT_VIRTUALEDIT
7208 if (virtual_active())
7209 coladvance(getviscol());
7210#endif
7211 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7212 }
7213 }
7214}
7215
7216#ifdef FEAT_VREPLACE
7217/*
7218 * "gr".
7219 */
7220 static void
7221nv_vreplace(cap)
7222 cmdarg_T *cap;
7223{
7224# ifdef FEAT_VISUAL
7225 if (VIsual_active)
7226 {
7227 cap->cmdchar = 'r';
7228 cap->nchar = cap->extra_char;
7229 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7230 }
7231 else
7232# endif
7233 if (!checkclearopq(cap->oap))
7234 {
7235 if (!curbuf->b_p_ma)
7236 EMSG(_(e_modifiable));
7237 else
7238 {
7239 if (cap->extra_char == Ctrl_V) /* get another character */
7240 cap->extra_char = get_literal();
7241 stuffcharReadbuff(cap->extra_char);
7242 stuffcharReadbuff(ESC);
7243# ifdef FEAT_VIRTUALEDIT
7244 if (virtual_active())
7245 coladvance(getviscol());
7246# endif
7247 invoke_edit(cap, TRUE, 'v', FALSE);
7248 }
7249 }
7250}
7251#endif
7252
7253/*
7254 * Swap case for "~" command, when it does not work like an operator.
7255 */
7256 static void
7257n_swapchar(cap)
7258 cmdarg_T *cap;
7259{
7260 long n;
7261 pos_T startpos;
7262 int did_change = 0;
7263#ifdef FEAT_NETBEANS_INTG
7264 pos_T pos;
7265 char_u *ptr;
7266 int count;
7267#endif
7268
7269 if (checkclearopq(cap->oap))
7270 return;
7271
7272 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7273 {
7274 clearopbeep(cap->oap);
7275 return;
7276 }
7277
7278 prep_redo_cmd(cap);
7279
7280 if (u_save_cursor() == FAIL)
7281 return;
7282
7283 startpos = curwin->w_cursor;
7284#ifdef FEAT_NETBEANS_INTG
7285 pos = startpos;
7286#endif
7287 for (n = cap->count1; n > 0; --n)
7288 {
7289 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7290 inc_cursor();
7291 if (gchar_cursor() == NUL)
7292 {
7293 if (vim_strchr(p_ww, '~') != NULL
7294 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7295 {
7296#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007297 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 if (did_change)
7300 {
7301 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007302 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007303 netbeans_removed(curbuf, pos.lnum, pos.col,
7304 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007306 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
7308 pos.col = 0;
7309 pos.lnum++;
7310 }
7311#endif
7312 ++curwin->w_cursor.lnum;
7313 curwin->w_cursor.col = 0;
7314 if (n > 1)
7315 {
7316 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7317 break;
7318 u_clearline();
7319 }
7320 }
7321 else
7322 break;
7323 }
7324 }
7325#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007326 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 ptr = ml_get(pos.lnum);
7329 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007330 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7331 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 }
7333#endif
7334
7335
7336 check_cursor();
7337 curwin->w_set_curswant = TRUE;
7338 if (did_change)
7339 {
7340 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7341 0L);
7342 curbuf->b_op_start = startpos;
7343 curbuf->b_op_end = curwin->w_cursor;
7344 if (curbuf->b_op_end.col > 0)
7345 --curbuf->b_op_end.col;
7346 }
7347}
7348
7349/*
7350 * Move cursor to mark.
7351 */
7352 static void
7353nv_cursormark(cap, flag, pos)
7354 cmdarg_T *cap;
7355 int flag;
7356 pos_T *pos;
7357{
7358 if (check_mark(pos) == FAIL)
7359 clearop(cap->oap);
7360 else
7361 {
7362 if (cap->cmdchar == '\''
7363 || cap->cmdchar == '`'
7364 || cap->cmdchar == '['
7365 || cap->cmdchar == ']')
7366 setpcmark();
7367 curwin->w_cursor = *pos;
7368 if (flag)
7369 beginline(BL_WHITE | BL_FIX);
7370 else
7371 check_cursor();
7372 }
7373 cap->oap->motion_type = flag ? MLINE : MCHAR;
7374 if (cap->cmdchar == '`')
7375 cap->oap->use_reg_one = TRUE;
7376 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7377 curwin->w_set_curswant = TRUE;
7378}
7379
7380#ifdef FEAT_VISUAL
7381/*
7382 * Handle commands that are operators in Visual mode.
7383 */
7384 static void
7385v_visop(cap)
7386 cmdarg_T *cap;
7387{
7388 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7389
7390 /* Uppercase means linewise, except in block mode, then "D" deletes till
7391 * the end of the line, and "C" replaces til EOL */
7392 if (isupper(cap->cmdchar))
7393 {
7394 if (VIsual_mode != Ctrl_V)
7395 VIsual_mode = 'V';
7396 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7397 curwin->w_curswant = MAXCOL;
7398 }
7399 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7400 nv_operator(cap);
7401}
7402#endif
7403
7404/*
7405 * "s" and "S" commands.
7406 */
7407 static void
7408nv_subst(cap)
7409 cmdarg_T *cap;
7410{
7411#ifdef FEAT_VISUAL
7412 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7413 {
7414 if (cap->cmdchar == 'S')
7415 VIsual_mode = 'V';
7416 cap->cmdchar = 'c';
7417 nv_operator(cap);
7418 }
7419 else
7420#endif
7421 nv_optrans(cap);
7422}
7423
7424/*
7425 * Abbreviated commands.
7426 */
7427 static void
7428nv_abbrev(cap)
7429 cmdarg_T *cap;
7430{
7431 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7432 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7433
7434#ifdef FEAT_VISUAL
7435 /* in Visual mode these commands are operators */
7436 if (VIsual_active)
7437 v_visop(cap);
7438 else
7439#endif
7440 nv_optrans(cap);
7441}
7442
7443/*
7444 * Translate a command into another command.
7445 */
7446 static void
7447nv_optrans(cap)
7448 cmdarg_T *cap;
7449{
7450 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7451 (char_u *)"d$", (char_u *)"c$",
7452 (char_u *)"cl", (char_u *)"cc",
7453 (char_u *)"yy", (char_u *)":s\r"};
7454 static char_u *str = (char_u *)"xXDCsSY&";
7455
7456 if (!checkclearopq(cap->oap))
7457 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007458 /* In Vi "2D" doesn't delete the next line. Can't translate it
7459 * either, because "2." should also not use the count. */
7460 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7461 {
7462 cap->oap->start = curwin->w_cursor;
7463 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007464#ifdef FEAT_EVAL
7465 set_op_var(OP_DELETE);
7466#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007467 cap->count1 = 1;
7468 nv_dollar(cap);
7469 finish_op = TRUE;
7470 ResetRedobuff();
7471 AppendCharToRedobuff('D');
7472 }
7473 else
7474 {
7475 if (cap->count0)
7476 stuffnumReadbuff(cap->count0);
7477 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
7480 cap->opcount = 0;
7481}
7482
7483/*
7484 * "'" and "`" commands. Also for "g'" and "g`".
7485 * cap->arg is TRUE for "'" and "g'".
7486 */
7487 static void
7488nv_gomark(cap)
7489 cmdarg_T *cap;
7490{
7491 pos_T *pos;
7492 int c;
7493#ifdef FEAT_FOLDING
7494 linenr_T lnum = curwin->w_cursor.lnum;
7495 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7496#endif
7497
7498 if (cap->cmdchar == 'g')
7499 c = cap->extra_char;
7500 else
7501 c = cap->nchar;
7502 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7503 if (pos == (pos_T *)-1) /* jumped to other file */
7504 {
7505 if (cap->arg)
7506 {
7507 check_cursor_lnum();
7508 beginline(BL_WHITE | BL_FIX);
7509 }
7510 else
7511 check_cursor();
7512 }
7513 else
7514 nv_cursormark(cap, cap->arg, pos);
7515
7516#ifdef FEAT_VIRTUALEDIT
7517 /* May need to clear the coladd that a mark includes. */
7518 if (!virtual_active())
7519 curwin->w_cursor.coladd = 0;
7520#endif
7521#ifdef FEAT_FOLDING
7522 if (cap->oap->op_type == OP_NOP
7523 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7524 && (fdo_flags & FDO_MARK)
7525 && old_KeyTyped)
7526 foldOpenCursor();
7527#endif
7528}
7529
7530/*
7531 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7532 */
7533 static void
7534nv_pcmark(cap)
7535 cmdarg_T *cap;
7536{
7537#ifdef FEAT_JUMPLIST
7538 pos_T *pos;
7539# ifdef FEAT_FOLDING
7540 linenr_T lnum = curwin->w_cursor.lnum;
7541 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7542# endif
7543
7544 if (!checkclearopq(cap->oap))
7545 {
7546 if (cap->cmdchar == 'g')
7547 pos = movechangelist((int)cap->count1);
7548 else
7549 pos = movemark((int)cap->count1);
7550 if (pos == (pos_T *)-1) /* jump to other file */
7551 {
7552 curwin->w_set_curswant = TRUE;
7553 check_cursor();
7554 }
7555 else if (pos != NULL) /* can jump */
7556 nv_cursormark(cap, FALSE, pos);
7557 else if (cap->cmdchar == 'g')
7558 {
7559 if (curbuf->b_changelistlen == 0)
7560 EMSG(_("E664: changelist is empty"));
7561 else if (cap->count1 < 0)
7562 EMSG(_("E662: At start of changelist"));
7563 else
7564 EMSG(_("E663: At end of changelist"));
7565 }
7566 else
7567 clearopbeep(cap->oap);
7568# ifdef FEAT_FOLDING
7569 if (cap->oap->op_type == OP_NOP
7570 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7571 && (fdo_flags & FDO_MARK)
7572 && old_KeyTyped)
7573 foldOpenCursor();
7574# endif
7575 }
7576#else
7577 clearopbeep(cap->oap);
7578#endif
7579}
7580
7581/*
7582 * Handle '"' command.
7583 */
7584 static void
7585nv_regname(cap)
7586 cmdarg_T *cap;
7587{
7588 if (checkclearop(cap->oap))
7589 return;
7590#ifdef FEAT_EVAL
7591 if (cap->nchar == '=')
7592 cap->nchar = get_expr_register();
7593#endif
7594 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7595 {
7596 cap->oap->regname = cap->nchar;
7597 cap->opcount = cap->count0; /* remember count before '"' */
7598#ifdef FEAT_EVAL
7599 set_reg_var(cap->oap->regname);
7600#endif
7601 }
7602 else
7603 clearopbeep(cap->oap);
7604}
7605
7606#ifdef FEAT_VISUAL
7607/*
7608 * Handle "v", "V" and "CTRL-V" commands.
7609 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7610 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 */
7613 static void
7614nv_visual(cap)
7615 cmdarg_T *cap;
7616{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007617 if (cap->cmdchar == Ctrl_Q)
7618 cap->cmdchar = Ctrl_V;
7619
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7621 * characterwise, linewise, or blockwise. */
7622 if (cap->oap->op_type != OP_NOP)
7623 {
7624 cap->oap->motion_force = cap->cmdchar;
7625 finish_op = FALSE; /* operator doesn't finish now but later */
7626 return;
7627 }
7628
7629 VIsual_select = cap->arg;
7630 if (VIsual_active) /* change Visual mode */
7631 {
7632 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7633 end_visual_mode();
7634 else /* toggle char/block mode */
7635 { /* or char/line mode */
7636 VIsual_mode = cap->cmdchar;
7637 showmode();
7638 }
7639 redraw_curbuf_later(INVERTED); /* update the inversion */
7640 }
7641 else /* start Visual mode */
7642 {
7643 check_visual_highlight();
7644 if (cap->count0) /* use previously selected part */
7645 {
7646 if (resel_VIsual_mode == NUL) /* there is none */
7647 {
7648 beep_flush();
7649 return;
7650 }
7651 VIsual = curwin->w_cursor;
7652
7653 VIsual_active = TRUE;
7654 VIsual_reselect = TRUE;
7655 if (!cap->arg)
7656 /* start Select mode when 'selectmode' contains "cmd" */
7657 may_start_select('c');
7658#ifdef FEAT_MOUSE
7659 setmouse();
7660#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007661 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 redraw_cmdline = TRUE; /* show visual mode later */
7663 /*
7664 * For V and ^V, we multiply the number of lines even if there
7665 * was only one -- webb
7666 */
7667 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7668 {
7669 curwin->w_cursor.lnum +=
7670 resel_VIsual_line_count * cap->count0 - 1;
7671 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7672 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7673 }
7674 VIsual_mode = resel_VIsual_mode;
7675 if (VIsual_mode == 'v')
7676 {
7677 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007678 {
7679 validate_virtcol();
7680 curwin->w_curswant = curwin->w_virtcol
7681 + resel_VIsual_vcol * cap->count0 - 1;
7682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007684 curwin->w_curswant = resel_VIsual_vcol;
7685 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007687 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {
7689 curwin->w_curswant = MAXCOL;
7690 coladvance((colnr_T)MAXCOL);
7691 }
7692 else if (VIsual_mode == Ctrl_V)
7693 {
7694 validate_virtcol();
7695 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007696 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 coladvance(curwin->w_curswant);
7698 }
7699 else
7700 curwin->w_set_curswant = TRUE;
7701 redraw_curbuf_later(INVERTED); /* show the inversion */
7702 }
7703 else
7704 {
7705 if (!cap->arg)
7706 /* start Select mode when 'selectmode' contains "cmd" */
7707 may_start_select('c');
7708 n_start_visual_mode(cap->cmdchar);
7709 }
7710 }
7711}
7712
7713/*
7714 * Start selection for Shift-movement keys.
7715 */
7716 void
7717start_selection()
7718{
7719 /* if 'selectmode' contains "key", start Select mode */
7720 may_start_select('k');
7721 n_start_visual_mode('v');
7722}
7723
7724/*
7725 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7726 */
7727 void
7728may_start_select(c)
7729 int c;
7730{
7731 VIsual_select = (stuff_empty() && typebuf_typed()
7732 && (vim_strchr(p_slm, c) != NULL));
7733}
7734
7735/*
7736 * Start Visual mode "c".
7737 * Should set VIsual_select before calling this.
7738 */
7739 static void
7740n_start_visual_mode(c)
7741 int c;
7742{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007743#ifdef FEAT_CONCEAL
7744 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007745 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007746#endif
7747
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 VIsual_mode = c;
7749 VIsual_active = TRUE;
7750 VIsual_reselect = TRUE;
7751#ifdef FEAT_VIRTUALEDIT
7752 /* Corner case: the 0 position in a tab may change when going into
7753 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7754 */
7755 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7756 coladvance(curwin->w_virtcol);
7757#endif
7758 VIsual = curwin->w_cursor;
7759
7760#ifdef FEAT_FOLDING
7761 foldAdjustVisual();
7762#endif
7763
7764#ifdef FEAT_MOUSE
7765 setmouse();
7766#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007767#ifdef FEAT_CONCEAL
7768 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007769 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007770#endif
7771
Bram Moolenaar09df3122006-01-23 22:23:09 +00007772 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 redraw_cmdline = TRUE; /* show visual mode later */
7774#ifdef FEAT_CLIPBOARD
7775 /* Make sure the clipboard gets updated. Needed because start and
7776 * end may still be the same, and the selection needs to be owned */
7777 clip_star.vmode = NUL;
7778#endif
7779
7780 /* Only need to redraw this line, unless still need to redraw an old
7781 * Visual area (when 'lazyredraw' is set). */
7782 if (curwin->w_redr_type < INVERTED)
7783 {
7784 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7785 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7786 }
7787}
7788
7789#endif /* FEAT_VISUAL */
7790
7791/*
7792 * CTRL-W: Window commands
7793 */
7794 static void
7795nv_window(cap)
7796 cmdarg_T *cap;
7797{
7798#ifdef FEAT_WINDOWS
7799 if (!checkclearop(cap->oap))
7800 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7801#else
7802 (void)checkclearop(cap->oap);
7803#endif
7804}
7805
7806/*
7807 * CTRL-Z: Suspend
7808 */
7809 static void
7810nv_suspend(cap)
7811 cmdarg_T *cap;
7812{
7813 clearop(cap->oap);
7814#ifdef FEAT_VISUAL
7815 if (VIsual_active)
7816 end_visual_mode(); /* stop Visual mode */
7817#endif
7818 do_cmdline_cmd((char_u *)"st");
7819}
7820
7821/*
7822 * Commands starting with "g".
7823 */
7824 static void
7825nv_g_cmd(cap)
7826 cmdarg_T *cap;
7827{
7828 oparg_T *oap = cap->oap;
7829#ifdef FEAT_VISUAL
7830 pos_T tpos;
7831#endif
7832 int i;
7833 int flag = FALSE;
7834
7835 switch (cap->nchar)
7836 {
7837#ifdef MEM_PROFILE
7838 /*
7839 * "g^A": dump log of used memory.
7840 */
7841 case Ctrl_A:
7842 vim_mem_profile_dump();
7843 break;
7844#endif
7845
7846#ifdef FEAT_VREPLACE
7847 /*
7848 * "gR": Enter virtual replace mode.
7849 */
7850 case 'R':
7851 cap->arg = TRUE;
7852 nv_Replace(cap);
7853 break;
7854
7855 case 'r':
7856 nv_vreplace(cap);
7857 break;
7858#endif
7859
7860 case '&':
7861 do_cmdline_cmd((char_u *)"%s//~/&");
7862 break;
7863
7864#ifdef FEAT_VISUAL
7865 /*
7866 * "gv": Reselect the previous Visual area. If Visual already active,
7867 * exchange previous and current Visual area.
7868 */
7869 case 'v':
7870 if (checkclearop(oap))
7871 break;
7872
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007873 if ( curbuf->b_visual.vi_start.lnum == 0
7874 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7875 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 beep_flush();
7877 else
7878 {
7879 /* set w_cursor to the start of the Visual area, tpos to the end */
7880 if (VIsual_active)
7881 {
7882 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007883 VIsual_mode = curbuf->b_visual.vi_mode;
7884 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885# ifdef FEAT_EVAL
7886 curbuf->b_visual_mode_eval = i;
7887# endif
7888 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007889 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7890 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007892 tpos = curbuf->b_visual.vi_end;
7893 curbuf->b_visual.vi_end = curwin->w_cursor;
7894 curwin->w_cursor = curbuf->b_visual.vi_start;
7895 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 else
7898 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007899 VIsual_mode = curbuf->b_visual.vi_mode;
7900 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7901 tpos = curbuf->b_visual.vi_end;
7902 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904
7905 VIsual_active = TRUE;
7906 VIsual_reselect = TRUE;
7907
7908 /* Set Visual to the start and w_cursor to the end of the Visual
7909 * area. Make sure they are on an existing character. */
7910 check_cursor();
7911 VIsual = curwin->w_cursor;
7912 curwin->w_cursor = tpos;
7913 check_cursor();
7914 update_topline();
7915 /*
7916 * When called from normal "g" command: start Select mode when
7917 * 'selectmode' contains "cmd". When called for K_SELECT, always
7918 * start Select mode.
7919 */
7920 if (cap->arg)
7921 VIsual_select = TRUE;
7922 else
7923 may_start_select('c');
7924#ifdef FEAT_MOUSE
7925 setmouse();
7926#endif
7927#ifdef FEAT_CLIPBOARD
7928 /* Make sure the clipboard gets updated. Needed because start and
7929 * end are still the same, and the selection needs to be owned */
7930 clip_star.vmode = NUL;
7931#endif
7932 redraw_curbuf_later(INVERTED);
7933 showmode();
7934 }
7935 break;
7936 /*
7937 * "gV": Don't reselect the previous Visual area after a Select mode
7938 * mapping of menu.
7939 */
7940 case 'V':
7941 VIsual_reselect = FALSE;
7942 break;
7943
7944 /*
7945 * "gh": start Select mode.
7946 * "gH": start Select line mode.
7947 * "g^H": start Select block mode.
7948 */
7949 case K_BS:
7950 cap->nchar = Ctrl_H;
7951 /* FALLTHROUGH */
7952 case 'h':
7953 case 'H':
7954 case Ctrl_H:
7955# ifdef EBCDIC
7956 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7957 if (cap->nchar == Ctrl_H)
7958 cap->cmdchar = Ctrl_V;
7959 else
7960# endif
7961 cap->cmdchar = cap->nchar + ('v' - 'h');
7962 cap->arg = TRUE;
7963 nv_visual(cap);
7964 break;
7965#endif /* FEAT_VISUAL */
7966
7967 /*
7968 * "gj" and "gk" two new funny movement keys -- up and down
7969 * movement based on *screen* line rather than *file* line.
7970 */
7971 case 'j':
7972 case K_DOWN:
7973 /* with 'nowrap' it works just like the normal "j" command; also when
7974 * in a closed fold */
7975 if (!curwin->w_p_wrap
7976#ifdef FEAT_FOLDING
7977 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7978#endif
7979 )
7980 {
7981 oap->motion_type = MLINE;
7982 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7983 }
7984 else
7985 i = nv_screengo(oap, FORWARD, cap->count1);
7986 if (i == FAIL)
7987 clearopbeep(oap);
7988 break;
7989
7990 case 'k':
7991 case K_UP:
7992 /* with 'nowrap' it works just like the normal "k" command; also when
7993 * in a closed fold */
7994 if (!curwin->w_p_wrap
7995#ifdef FEAT_FOLDING
7996 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7997#endif
7998 )
7999 {
8000 oap->motion_type = MLINE;
8001 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8002 }
8003 else
8004 i = nv_screengo(oap, BACKWARD, cap->count1);
8005 if (i == FAIL)
8006 clearopbeep(oap);
8007 break;
8008
8009 /*
8010 * "gJ": join two lines without inserting a space.
8011 */
8012 case 'J':
8013 nv_join(cap);
8014 break;
8015
8016 /*
8017 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8018 * "gm": middle of "g0" and "g$".
8019 */
8020 case '^':
8021 flag = TRUE;
8022 /* FALLTHROUGH */
8023
8024 case '0':
8025 case 'm':
8026 case K_HOME:
8027 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 oap->motion_type = MCHAR;
8029 oap->inclusive = FALSE;
8030 if (curwin->w_p_wrap
8031#ifdef FEAT_VERTSPLIT
8032 && curwin->w_width != 0
8033#endif
8034 )
8035 {
8036 int width1 = W_WIDTH(curwin) - curwin_col_off();
8037 int width2 = width1 + curwin_col_off2();
8038
8039 validate_virtcol();
8040 i = 0;
8041 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8042 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8043 }
8044 else
8045 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008046 /* Go to the middle of the screen line. When 'number' or
8047 * 'relativenumber' is on and lines are wrapping the middle can be more
8048 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 if (cap->nchar == 'm')
8050 i += (W_WIDTH(curwin) - curwin_col_off()
8051 + ((curwin->w_p_wrap && i > 0)
8052 ? curwin_col_off2() : 0)) / 2;
8053 coladvance((colnr_T)i);
8054 if (flag)
8055 {
8056 do
8057 i = gchar_cursor();
8058 while (vim_iswhite(i) && oneright() == OK);
8059 }
8060 curwin->w_set_curswant = TRUE;
8061 break;
8062
8063 case '_':
8064 /* "g_": to the last non-blank character in the line or <count> lines
8065 * downward. */
8066 cap->oap->motion_type = MCHAR;
8067 cap->oap->inclusive = TRUE;
8068 curwin->w_curswant = MAXCOL;
8069 if (cursor_down((long)(cap->count1 - 1),
8070 cap->oap->op_type == OP_NOP) == FAIL)
8071 clearopbeep(cap->oap);
8072 else
8073 {
8074 char_u *ptr = ml_get_curline();
8075
8076 /* In Visual mode we may end up after the line. */
8077 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8078 --curwin->w_cursor.col;
8079
8080 /* Decrease the cursor column until it's on a non-blank. */
8081 while (curwin->w_cursor.col > 0
8082 && vim_iswhite(ptr[curwin->w_cursor.col]))
8083 --curwin->w_cursor.col;
8084 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008085#ifdef FEAT_VISUAL
8086 adjust_for_sel(cap);
8087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 break;
8090
8091 case '$':
8092 case K_END:
8093 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
8095 int col_off = curwin_col_off();
8096
8097 oap->motion_type = MCHAR;
8098 oap->inclusive = TRUE;
8099 if (curwin->w_p_wrap
8100#ifdef FEAT_VERTSPLIT
8101 && curwin->w_width != 0
8102#endif
8103 )
8104 {
8105 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8106 if (cap->count1 == 1)
8107 {
8108 int width1 = W_WIDTH(curwin) - col_off;
8109 int width2 = width1 + curwin_col_off2();
8110
8111 validate_virtcol();
8112 i = width1 - 1;
8113 if (curwin->w_virtcol >= (colnr_T)width1)
8114 i += ((curwin->w_virtcol - width1) / width2 + 1)
8115 * width2;
8116 coladvance((colnr_T)i);
8117#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8118 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8119 {
8120 /*
8121 * Check for landing on a character that got split at
8122 * the end of the line. We do not want to advance to
8123 * the next screen line.
8124 */
8125 validate_virtcol();
8126 if (curwin->w_virtcol > (colnr_T)i)
8127 --curwin->w_cursor.col;
8128 }
8129#endif
8130 }
8131 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8132 clearopbeep(oap);
8133 }
8134 else
8135 {
8136 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8137 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008138
8139 /* Make sure we stick in this column. */
8140 validate_virtcol();
8141 curwin->w_curswant = curwin->w_virtcol;
8142 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144 }
8145 break;
8146
8147 /*
8148 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8149 */
8150 case '*':
8151 case '#':
8152#if POUND != '#'
8153 case POUND: /* pound sign (sometimes equal to '#') */
8154#endif
8155 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8156 case ']': /* :tselect for current identifier */
8157 nv_ident(cap);
8158 break;
8159
8160 /*
8161 * ge and gE: go back to end of word
8162 */
8163 case 'e':
8164 case 'E':
8165 oap->motion_type = MCHAR;
8166 curwin->w_set_curswant = TRUE;
8167 oap->inclusive = TRUE;
8168 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8169 clearopbeep(oap);
8170 break;
8171
8172 /*
8173 * "g CTRL-G": display info about cursor position
8174 */
8175 case Ctrl_G:
8176 cursor_pos_info();
8177 break;
8178
8179 /*
8180 * "gi": start Insert at the last position.
8181 */
8182 case 'i':
8183 if (curbuf->b_last_insert.lnum != 0)
8184 {
8185 curwin->w_cursor = curbuf->b_last_insert;
8186 check_cursor_lnum();
8187 i = (int)STRLEN(ml_get_curline());
8188 if (curwin->w_cursor.col > (colnr_T)i)
8189 {
8190#ifdef FEAT_VIRTUALEDIT
8191 if (virtual_active())
8192 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8193#endif
8194 curwin->w_cursor.col = i;
8195 }
8196 }
8197 cap->cmdchar = 'i';
8198 nv_edit(cap);
8199 break;
8200
8201 /*
8202 * "gI": Start insert in column 1.
8203 */
8204 case 'I':
8205 beginline(0);
8206 if (!checkclearopq(oap))
8207 invoke_edit(cap, FALSE, 'g', FALSE);
8208 break;
8209
8210#ifdef FEAT_SEARCHPATH
8211 /*
8212 * "gf": goto file, edit file under cursor
8213 * "]f" and "[f": can also be used.
8214 */
8215 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008216 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 nv_gotofile(cap);
8218 break;
8219#endif
8220
8221 /* "g'm" and "g`m": jump to mark without setting pcmark */
8222 case '\'':
8223 cap->arg = TRUE;
8224 /*FALLTHROUGH*/
8225 case '`':
8226 nv_gomark(cap);
8227 break;
8228
8229 /*
8230 * "gs": Goto sleep.
8231 */
8232 case 's':
8233 do_sleep(cap->count1 * 1000L);
8234 break;
8235
8236 /*
8237 * "ga": Display the ascii value of the character under the
8238 * cursor. It is displayed in decimal, hex, and octal. -- webb
8239 */
8240 case 'a':
8241 do_ascii(NULL);
8242 break;
8243
8244#ifdef FEAT_MBYTE
8245 /*
8246 * "g8": Display the bytes used for the UTF-8 character under the
8247 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008248 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 */
8250 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008251 if (cap->count0 == 8)
8252 utf_find_illegal();
8253 else
8254 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 break;
8256#endif
8257
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008258 case '<':
8259 show_sb_text();
8260 break;
8261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 /*
8263 * "gg": Goto the first line in file. With a count it goes to
8264 * that line number like for "G". -- webb
8265 */
8266 case 'g':
8267 cap->arg = FALSE;
8268 nv_goto(cap);
8269 break;
8270
8271 /*
8272 * Two-character operators:
8273 * "gq" Format text
8274 * "gw" Format text and keep cursor position
8275 * "g~" Toggle the case of the text.
8276 * "gu" Change text to lower case.
8277 * "gU" Change text to upper case.
8278 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008279 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 */
8281 case 'q':
8282 case 'w':
8283 oap->cursor_start = curwin->w_cursor;
8284 /*FALLTHROUGH*/
8285 case '~':
8286 case 'u':
8287 case 'U':
8288 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008289 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 nv_operator(cap);
8291 break;
8292
8293 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008294 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 * current function
8296 * "gD": idem, but in the current file.
8297 */
8298 case 'd':
8299 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008300 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 break;
8302
8303#ifdef FEAT_MOUSE
8304 /*
8305 * g<*Mouse> : <C-*mouse>
8306 */
8307 case K_MIDDLEMOUSE:
8308 case K_MIDDLEDRAG:
8309 case K_MIDDLERELEASE:
8310 case K_LEFTMOUSE:
8311 case K_LEFTDRAG:
8312 case K_LEFTRELEASE:
8313 case K_RIGHTMOUSE:
8314 case K_RIGHTDRAG:
8315 case K_RIGHTRELEASE:
8316 case K_X1MOUSE:
8317 case K_X1DRAG:
8318 case K_X1RELEASE:
8319 case K_X2MOUSE:
8320 case K_X2DRAG:
8321 case K_X2RELEASE:
8322 mod_mask = MOD_MASK_CTRL;
8323 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8324 break;
8325#endif
8326
8327 case K_IGNORE:
8328 break;
8329
8330 /*
8331 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8332 */
8333 case 'p':
8334 case 'P':
8335 nv_put(cap);
8336 break;
8337
8338#ifdef FEAT_BYTEOFF
8339 /* "go": goto byte count from start of buffer */
8340 case 'o':
8341 goto_byte(cap->count0);
8342 break;
8343#endif
8344
8345 /* "gQ": improved Ex mode */
8346 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008347 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008350 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 break;
8352 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (!checkclearopq(oap))
8355 do_exmode(TRUE);
8356 break;
8357
8358#ifdef FEAT_JUMPLIST
8359 case ',':
8360 nv_pcmark(cap);
8361 break;
8362
8363 case ';':
8364 cap->count1 = -cap->count1;
8365 nv_pcmark(cap);
8366 break;
8367#endif
8368
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008369#ifdef FEAT_WINDOWS
8370 case 't':
8371 goto_tabpage((int)cap->count0);
8372 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008373 case 'T':
8374 goto_tabpage(-(int)cap->count1);
8375 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008376#endif
8377
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008378 case '+':
8379 case '-': /* "g+" and "g-": undo or redo along the timeline */
8380 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008381 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008382 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008383 break;
8384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 default:
8386 clearopbeep(oap);
8387 break;
8388 }
8389}
8390
8391/*
8392 * Handle "o" and "O" commands.
8393 */
8394 static void
8395n_opencmd(cap)
8396 cmdarg_T *cap;
8397{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008398#ifdef FEAT_CONCEAL
8399 linenr_T oldline = curwin->w_cursor.lnum;
8400#endif
8401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (!checkclearopq(cap->oap))
8403 {
8404#ifdef FEAT_FOLDING
8405 if (cap->cmdchar == 'O')
8406 /* Open above the first line of a folded sequence of lines */
8407 (void)hasFolding(curwin->w_cursor.lnum,
8408 &curwin->w_cursor.lnum, NULL);
8409 else
8410 /* Open below the last line of a folded sequence of lines */
8411 (void)hasFolding(curwin->w_cursor.lnum,
8412 NULL, &curwin->w_cursor.lnum);
8413#endif
8414 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8415 (cap->cmdchar == 'O' ? 1 : 0)),
8416 (linenr_T)(curwin->w_cursor.lnum +
8417 (cap->cmdchar == 'o' ? 1 : 0))
8418 ) == OK
8419 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8420#ifdef FEAT_COMMENTS
8421 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8422#endif
8423 0, 0))
8424 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008425#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008426 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008427 update_single_line(curwin, oldline);
8428#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008429 /* When '#' is in 'cpoptions' ignore the count. */
8430 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8431 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8433 }
8434 }
8435}
8436
8437/*
8438 * "." command: redo last change.
8439 */
8440 static void
8441nv_dot(cap)
8442 cmdarg_T *cap;
8443{
8444 if (!checkclearopq(cap->oap))
8445 {
8446 /*
8447 * If "restart_edit" is TRUE, the last but one command is repeated
8448 * instead of the last command (inserting text). This is used for
8449 * CTRL-O <.> in insert mode.
8450 */
8451 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8452 clearopbeep(cap->oap);
8453 }
8454}
8455
8456/*
8457 * CTRL-R: undo undo
8458 */
8459 static void
8460nv_redo(cap)
8461 cmdarg_T *cap;
8462{
8463 if (!checkclearopq(cap->oap))
8464 {
8465 u_redo((int)cap->count1);
8466 curwin->w_set_curswant = TRUE;
8467 }
8468}
8469
8470/*
8471 * Handle "U" command.
8472 */
8473 static void
8474nv_Undo(cap)
8475 cmdarg_T *cap;
8476{
8477 /* In Visual mode and typing "gUU" triggers an operator */
8478 if (cap->oap->op_type == OP_UPPER
8479#ifdef FEAT_VISUAL
8480 || VIsual_active
8481#endif
8482 )
8483 {
8484 /* translate "gUU" to "gUgU" */
8485 cap->cmdchar = 'g';
8486 cap->nchar = 'U';
8487 nv_operator(cap);
8488 }
8489 else if (!checkclearopq(cap->oap))
8490 {
8491 u_undoline();
8492 curwin->w_set_curswant = TRUE;
8493 }
8494}
8495
8496/*
8497 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8498 * single character.
8499 */
8500 static void
8501nv_tilde(cap)
8502 cmdarg_T *cap;
8503{
8504 if (!p_to
8505#ifdef FEAT_VISUAL
8506 && !VIsual_active
8507#endif
8508 && cap->oap->op_type != OP_TILDE)
8509 n_swapchar(cap);
8510 else
8511 nv_operator(cap);
8512}
8513
8514/*
8515 * Handle an operator command.
8516 * The actual work is done by do_pending_operator().
8517 */
8518 static void
8519nv_operator(cap)
8520 cmdarg_T *cap;
8521{
8522 int op_type;
8523
8524 op_type = get_op_type(cap->cmdchar, cap->nchar);
8525
8526 if (op_type == cap->oap->op_type) /* double operator works on lines */
8527 nv_lineop(cap);
8528 else if (!checkclearop(cap->oap))
8529 {
8530 cap->oap->start = curwin->w_cursor;
8531 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008532#ifdef FEAT_EVAL
8533 set_op_var(op_type);
8534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536}
8537
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008538#ifdef FEAT_EVAL
8539/*
8540 * Set v:operator to the characters for "optype".
8541 */
8542 static void
8543set_op_var(optype)
8544 int optype;
8545{
8546 char_u opchars[3];
8547
8548 if (optype == OP_NOP)
8549 set_vim_var_string(VV_OP, NULL, 0);
8550 else
8551 {
8552 opchars[0] = get_op_char(optype);
8553 opchars[1] = get_extra_op_char(optype);
8554 opchars[2] = NUL;
8555 set_vim_var_string(VV_OP, opchars, -1);
8556 }
8557}
8558#endif
8559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560/*
8561 * Handle linewise operator "dd", "yy", etc.
8562 *
8563 * "_" is is a strange motion command that helps make operators more logical.
8564 * It is actually implemented, but not documented in the real Vi. This motion
8565 * command actually refers to "the current line". Commands like "dd" and "yy"
8566 * are really an alternate form of "d_" and "y_". It does accept a count, so
8567 * "d3_" works to delete 3 lines.
8568 */
8569 static void
8570nv_lineop(cap)
8571 cmdarg_T *cap;
8572{
8573 cap->oap->motion_type = MLINE;
8574 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8575 clearopbeep(cap->oap);
8576 else if ( cap->oap->op_type == OP_DELETE
8577 || cap->oap->op_type == OP_LSHIFT
8578 || cap->oap->op_type == OP_RSHIFT)
8579 beginline(BL_SOL | BL_FIX);
8580 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8581 beginline(BL_WHITE | BL_FIX);
8582}
8583
8584/*
8585 * <Home> command.
8586 */
8587 static void
8588nv_home(cap)
8589 cmdarg_T *cap;
8590{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008591 /* CTRL-HOME is like "gg" */
8592 if (mod_mask & MOD_MASK_CTRL)
8593 nv_goto(cap);
8594 else
8595 {
8596 cap->count0 = 1;
8597 nv_pipe(cap);
8598 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008599 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8600 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601}
8602
8603/*
8604 * "|" command.
8605 */
8606 static void
8607nv_pipe(cap)
8608 cmdarg_T *cap;
8609{
8610 cap->oap->motion_type = MCHAR;
8611 cap->oap->inclusive = FALSE;
8612 beginline(0);
8613 if (cap->count0 > 0)
8614 {
8615 coladvance((colnr_T)(cap->count0 - 1));
8616 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8617 }
8618 else
8619 curwin->w_curswant = 0;
8620 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008621 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 curwin->w_set_curswant = FALSE;
8623}
8624
8625/*
8626 * Handle back-word command "b" and "B".
8627 * cap->arg is 1 for "B"
8628 */
8629 static void
8630nv_bck_word(cap)
8631 cmdarg_T *cap;
8632{
8633 cap->oap->motion_type = MCHAR;
8634 cap->oap->inclusive = FALSE;
8635 curwin->w_set_curswant = TRUE;
8636 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8637 clearopbeep(cap->oap);
8638#ifdef FEAT_FOLDING
8639 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8640 foldOpenCursor();
8641#endif
8642}
8643
8644/*
8645 * Handle word motion commands "e", "E", "w" and "W".
8646 * cap->arg is TRUE for "E" and "W".
8647 */
8648 static void
8649nv_wordcmd(cap)
8650 cmdarg_T *cap;
8651{
8652 int n;
8653 int word_end;
8654 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008655 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657 /*
8658 * Set inclusive for the "E" and "e" command.
8659 */
8660 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8661 word_end = TRUE;
8662 else
8663 word_end = FALSE;
8664 cap->oap->inclusive = word_end;
8665
8666 /*
8667 * "cw" and "cW" are a special case.
8668 */
8669 if (!word_end && cap->oap->op_type == OP_CHANGE)
8670 {
8671 n = gchar_cursor();
8672 if (n != NUL) /* not an empty line */
8673 {
8674 if (vim_iswhite(n))
8675 {
8676 /*
8677 * Reproduce a funny Vi behaviour: "cw" on a blank only
8678 * changes one character, not all blanks until the start of
8679 * the next word. Only do this when the 'w' flag is included
8680 * in 'cpoptions'.
8681 */
8682 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8683 {
8684 cap->oap->inclusive = TRUE;
8685 cap->oap->motion_type = MCHAR;
8686 return;
8687 }
8688 }
8689 else
8690 {
8691 /*
8692 * This is a little strange. To match what the real Vi does,
8693 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8694 * that we are not on a space or a TAB. This seems impolite
8695 * at first, but it's really more what we mean when we say
8696 * 'cw'.
8697 * Another strangeness: When standing on the end of a word
8698 * "ce" will change until the end of the next wordt, but "cw"
8699 * will change only one character! This is done by setting
8700 * flag.
8701 */
8702 cap->oap->inclusive = TRUE;
8703 word_end = TRUE;
8704 flag = TRUE;
8705 }
8706 }
8707 }
8708
8709 cap->oap->motion_type = MCHAR;
8710 curwin->w_set_curswant = TRUE;
8711 if (word_end)
8712 n = end_word(cap->count1, cap->arg, flag, FALSE);
8713 else
8714 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8715
Bram Moolenaardfefb982008-04-01 10:06:39 +00008716 /* Don't leave the cursor on the NUL past the end of line. Unless we
8717 * didn't move it forward. */
8718 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008719 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720
8721 if (n == FAIL && cap->oap->op_type == OP_NOP)
8722 clearopbeep(cap->oap);
8723 else
8724 {
8725#ifdef FEAT_VISUAL
8726 adjust_for_sel(cap);
8727#endif
8728#ifdef FEAT_FOLDING
8729 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8730 foldOpenCursor();
8731#endif
8732 }
8733}
8734
8735/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008736 * Used after a movement command: If the cursor ends up on the NUL after the
8737 * end of the line, may move it back to the last character and make the motion
8738 * inclusive.
8739 */
8740 static void
8741adjust_cursor(oap)
8742 oparg_T *oap;
8743{
8744 /* The cursor cannot remain on the NUL when:
8745 * - the column is > 0
8746 * - not in Visual mode or 'selection' is "o"
8747 * - 'virtualedit' is not "all" and not "onemore".
8748 */
8749 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8750#ifdef FEAT_VISUAL
8751 && (!VIsual_active || *p_sel == 'o')
8752#endif
8753#ifdef FEAT_VIRTUALEDIT
8754 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8755#endif
8756 )
8757 {
8758 --curwin->w_cursor.col;
8759#ifdef FEAT_MBYTE
8760 /* prevent cursor from moving on the trail byte */
8761 if (has_mbyte)
8762 mb_adjust_cursor();
8763#endif
8764 oap->inclusive = TRUE;
8765 }
8766}
8767
8768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 * "0" and "^" commands.
8770 * cap->arg is the argument for beginline().
8771 */
8772 static void
8773nv_beginline(cap)
8774 cmdarg_T *cap;
8775{
8776 cap->oap->motion_type = MCHAR;
8777 cap->oap->inclusive = FALSE;
8778 beginline(cap->arg);
8779#ifdef FEAT_FOLDING
8780 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8781 foldOpenCursor();
8782#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008783 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8784 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787#ifdef FEAT_VISUAL
8788/*
8789 * In exclusive Visual mode, may include the last character.
8790 */
8791 static void
8792adjust_for_sel(cap)
8793 cmdarg_T *cap;
8794{
8795 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8796 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8797 {
8798# ifdef FEAT_MBYTE
8799 if (has_mbyte)
8800 inc_cursor();
8801 else
8802# endif
8803 ++curwin->w_cursor.col;
8804 cap->oap->inclusive = FALSE;
8805 }
8806}
8807
8808/*
8809 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8810 * Should check VIsual_mode before calling this.
8811 * Returns TRUE when backed up to the previous line.
8812 */
8813 static int
8814unadjust_for_sel()
8815{
8816 pos_T *pp;
8817
8818 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8819 {
8820 if (lt(VIsual, curwin->w_cursor))
8821 pp = &curwin->w_cursor;
8822 else
8823 pp = &VIsual;
8824#ifdef FEAT_VIRTUALEDIT
8825 if (pp->coladd > 0)
8826 --pp->coladd;
8827 else
8828#endif
8829 if (pp->col > 0)
8830 {
8831 --pp->col;
8832#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008833 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834#endif
8835 }
8836 else if (pp->lnum > 1)
8837 {
8838 --pp->lnum;
8839 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8840 return TRUE;
8841 }
8842 }
8843 return FALSE;
8844}
8845
8846/*
8847 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8848 */
8849 static void
8850nv_select(cap)
8851 cmdarg_T *cap;
8852{
8853 if (VIsual_active)
8854 VIsual_select = TRUE;
8855 else if (VIsual_reselect)
8856 {
8857 cap->nchar = 'v'; /* fake "gv" command */
8858 cap->arg = TRUE;
8859 nv_g_cmd(cap);
8860 }
8861}
8862
8863#endif
8864
8865/*
8866 * "G", "gg", CTRL-END, CTRL-HOME.
8867 * cap->arg is TRUE for "G".
8868 */
8869 static void
8870nv_goto(cap)
8871 cmdarg_T *cap;
8872{
8873 linenr_T lnum;
8874
8875 if (cap->arg)
8876 lnum = curbuf->b_ml.ml_line_count;
8877 else
8878 lnum = 1L;
8879 cap->oap->motion_type = MLINE;
8880 setpcmark();
8881
8882 /* When a count is given, use it instead of the default lnum */
8883 if (cap->count0 != 0)
8884 lnum = cap->count0;
8885 if (lnum < 1L)
8886 lnum = 1L;
8887 else if (lnum > curbuf->b_ml.ml_line_count)
8888 lnum = curbuf->b_ml.ml_line_count;
8889 curwin->w_cursor.lnum = lnum;
8890 beginline(BL_SOL | BL_FIX);
8891#ifdef FEAT_FOLDING
8892 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8893 foldOpenCursor();
8894#endif
8895}
8896
8897/*
8898 * CTRL-\ in Normal mode.
8899 */
8900 static void
8901nv_normal(cap)
8902 cmdarg_T *cap;
8903{
8904 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8905 {
8906 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008907 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 clear_cmdline = TRUE; /* unshow mode later */
8909 restart_edit = 0;
8910#ifdef FEAT_CMDWIN
8911 if (cmdwin_type != 0)
8912 cmdwin_result = Ctrl_C;
8913#endif
8914#ifdef FEAT_VISUAL
8915 if (VIsual_active)
8916 {
8917 end_visual_mode(); /* stop Visual */
8918 redraw_curbuf_later(INVERTED);
8919 }
8920#endif
8921 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8922 if (cap->nchar == Ctrl_G && p_im)
8923 restart_edit = 'a';
8924 }
8925 else
8926 clearopbeep(cap->oap);
8927}
8928
8929/*
8930 * ESC in Normal mode: beep, but don't flush buffers.
8931 * Don't even beep if we are canceling a command.
8932 */
8933 static void
8934nv_esc(cap)
8935 cmdarg_T *cap;
8936{
8937 int no_reason;
8938
8939 no_reason = (cap->oap->op_type == OP_NOP
8940 && cap->opcount == 0
8941 && cap->count0 == 0
8942 && cap->oap->regname == 0
8943 && !p_im);
8944
8945 if (cap->arg) /* TRUE for CTRL-C */
8946 {
8947 if (restart_edit == 0
8948#ifdef FEAT_CMDWIN
8949 && cmdwin_type == 0
8950#endif
8951#ifdef FEAT_VISUAL
8952 && !VIsual_active
8953#endif
8954 && no_reason)
8955 MSG(_("Type :quit<Enter> to exit Vim"));
8956
8957 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8958 * set again below when halfway a mapping. */
8959 if (!p_im)
8960 restart_edit = 0;
8961#ifdef FEAT_CMDWIN
8962 if (cmdwin_type != 0)
8963 {
8964 cmdwin_result = K_IGNORE;
8965 got_int = FALSE; /* don't stop executing autocommands et al. */
8966 return;
8967 }
8968#endif
8969 }
8970
8971#ifdef FEAT_VISUAL
8972 if (VIsual_active)
8973 {
8974 end_visual_mode(); /* stop Visual */
8975 check_cursor_col(); /* make sure cursor is not beyond EOL */
8976 curwin->w_set_curswant = TRUE;
8977 redraw_curbuf_later(INVERTED);
8978 }
8979 else
8980#endif
8981 if (no_reason)
8982 vim_beep();
8983 clearop(cap->oap);
8984
8985 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8986 * set return to Insert mode afterwards. */
8987 if (restart_edit == 0 && goto_im()
8988#ifdef FEAT_EX_EXTRA
8989 && ex_normal_busy == 0
8990#endif
8991 )
8992 restart_edit = 'a';
8993}
8994
8995/*
8996 * Handle "A", "a", "I", "i" and <Insert> commands.
8997 */
8998 static void
8999nv_edit(cap)
9000 cmdarg_T *cap;
9001{
9002 /* <Insert> is equal to "i" */
9003 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9004 cap->cmdchar = 'i';
9005
9006#ifdef FEAT_VISUAL
9007 /* in Visual mode "A" and "I" are an operator */
9008 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9009 v_visop(cap);
9010
9011 /* in Visual mode and after an operator "a" and "i" are for text objects */
9012 else
9013#endif
9014 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9015 && (cap->oap->op_type != OP_NOP
9016#ifdef FEAT_VISUAL
9017 || VIsual_active
9018#endif
9019 ))
9020 {
9021#ifdef FEAT_TEXTOBJ
9022 nv_object(cap);
9023#else
9024 clearopbeep(cap->oap);
9025#endif
9026 }
9027 else if (!curbuf->b_p_ma && !p_im)
9028 {
9029 /* Only give this error when 'insertmode' is off. */
9030 EMSG(_(e_modifiable));
9031 clearop(cap->oap);
9032 }
9033 else if (!checkclearopq(cap->oap))
9034 {
9035 switch (cap->cmdchar)
9036 {
9037 case 'A': /* "A"ppend after the line */
9038 curwin->w_set_curswant = TRUE;
9039#ifdef FEAT_VIRTUALEDIT
9040 if (ve_flags == VE_ALL)
9041 {
9042 int save_State = State;
9043
9044 /* Pretent Insert mode here to allow the cursor on the
9045 * character past the end of the line */
9046 State = INSERT;
9047 coladvance((colnr_T)MAXCOL);
9048 State = save_State;
9049 }
9050 else
9051#endif
9052 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9053 break;
9054
9055 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009056 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9057 beginline(BL_WHITE);
9058 else
9059 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 break;
9061
9062 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9063#ifdef FEAT_VIRTUALEDIT
9064 /* increment coladd when in virtual space, increment the
9065 * column otherwise, also to append after an unprintable char */
9066 if (virtual_active()
9067 && (curwin->w_cursor.coladd > 0
9068 || *ml_get_cursor() == NUL
9069 || *ml_get_cursor() == TAB))
9070 curwin->w_cursor.coladd++;
9071 else
9072#endif
9073 if (*ml_get_cursor() != NUL)
9074 inc_cursor();
9075 break;
9076 }
9077
9078#ifdef FEAT_VIRTUALEDIT
9079 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9080 {
9081 int save_State = State;
9082
9083 /* Pretent Insert mode here to allow the cursor on the
9084 * character past the end of the line */
9085 State = INSERT;
9086 coladvance(getviscol());
9087 State = save_State;
9088 }
9089#endif
9090
9091 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9092 }
9093}
9094
9095/*
9096 * Invoke edit() and take care of "restart_edit" and the return value.
9097 */
9098 static void
9099invoke_edit(cap, repl, cmd, startln)
9100 cmdarg_T *cap;
9101 int repl; /* "r" or "gr" command */
9102 int cmd;
9103 int startln;
9104{
9105 int restart_edit_save = 0;
9106
9107 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9108 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9109 * it. */
9110 if (repl || !stuff_empty())
9111 restart_edit_save = restart_edit;
9112 else
9113 restart_edit_save = 0;
9114
9115 /* Always reset "restart_edit", this is not a restarted edit. */
9116 restart_edit = 0;
9117
9118 if (edit(cmd, startln, cap->count1))
9119 cap->retval |= CA_COMMAND_BUSY;
9120
9121 if (restart_edit == 0)
9122 restart_edit = restart_edit_save;
9123}
9124
9125#ifdef FEAT_TEXTOBJ
9126/*
9127 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9128 */
9129 static void
9130nv_object(cap)
9131 cmdarg_T *cap;
9132{
9133 int flag;
9134 int include;
9135 char_u *mps_save;
9136
9137 if (cap->cmdchar == 'i')
9138 include = FALSE; /* "ix" = inner object: exclude white space */
9139 else
9140 include = TRUE; /* "ax" = an object: include white space */
9141
9142 /* Make sure (), [], {} and <> are in 'matchpairs' */
9143 mps_save = curbuf->b_p_mps;
9144 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9145
9146 switch (cap->nchar)
9147 {
9148 case 'w': /* "aw" = a word */
9149 flag = current_word(cap->oap, cap->count1, include, FALSE);
9150 break;
9151 case 'W': /* "aW" = a WORD */
9152 flag = current_word(cap->oap, cap->count1, include, TRUE);
9153 break;
9154 case 'b': /* "ab" = a braces block */
9155 case '(':
9156 case ')':
9157 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9158 break;
9159 case 'B': /* "aB" = a Brackets block */
9160 case '{':
9161 case '}':
9162 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9163 break;
9164 case '[': /* "a[" = a [] block */
9165 case ']':
9166 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9167 break;
9168 case '<': /* "a<" = a <> block */
9169 case '>':
9170 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9171 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009172 case 't': /* "at" = a tag block (xml and html) */
9173 flag = current_tagblock(cap->oap, cap->count1, include);
9174 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 case 'p': /* "ap" = a paragraph */
9176 flag = current_par(cap->oap, cap->count1, include, 'p');
9177 break;
9178 case 's': /* "as" = a sentence */
9179 flag = current_sent(cap->oap, cap->count1, include);
9180 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009181 case '"': /* "a"" = a double quoted string */
9182 case '\'': /* "a'" = a single quoted string */
9183 case '`': /* "a`" = a backtick quoted string */
9184 flag = current_quote(cap->oap, cap->count1, include,
9185 cap->nchar);
9186 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187#if 0 /* TODO */
9188 case 'S': /* "aS" = a section */
9189 case 'f': /* "af" = a filename */
9190 case 'u': /* "au" = a URL */
9191#endif
9192 default:
9193 flag = FAIL;
9194 break;
9195 }
9196
9197 curbuf->b_p_mps = mps_save;
9198 if (flag == FAIL)
9199 clearopbeep(cap->oap);
9200 adjust_cursor_col();
9201 curwin->w_set_curswant = TRUE;
9202}
9203#endif
9204
9205/*
9206 * "q" command: Start/stop recording.
9207 * "q:", "q/", "q?": edit command-line in command-line window.
9208 */
9209 static void
9210nv_record(cap)
9211 cmdarg_T *cap;
9212{
9213 if (cap->oap->op_type == OP_FORMAT)
9214 {
9215 /* "gqq" is the same as "gqgq": format line */
9216 cap->cmdchar = 'g';
9217 cap->nchar = 'q';
9218 nv_operator(cap);
9219 }
9220 else if (!checkclearop(cap->oap))
9221 {
9222#ifdef FEAT_CMDWIN
9223 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9224 {
9225 stuffcharReadbuff(cap->nchar);
9226 stuffcharReadbuff(K_CMDWIN);
9227 }
9228 else
9229#endif
9230 /* (stop) recording into a named register, unless executing a
9231 * register */
9232 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9233 clearopbeep(cap->oap);
9234 }
9235}
9236
9237/*
9238 * Handle the "@r" command.
9239 */
9240 static void
9241nv_at(cap)
9242 cmdarg_T *cap;
9243{
9244 if (checkclearop(cap->oap))
9245 return;
9246#ifdef FEAT_EVAL
9247 if (cap->nchar == '=')
9248 {
9249 if (get_expr_register() == NUL)
9250 return;
9251 }
9252#endif
9253 while (cap->count1-- && !got_int)
9254 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009255 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 {
9257 clearopbeep(cap->oap);
9258 break;
9259 }
9260 line_breakcheck();
9261 }
9262}
9263
9264/*
9265 * Handle the CTRL-U and CTRL-D commands.
9266 */
9267 static void
9268nv_halfpage(cap)
9269 cmdarg_T *cap;
9270{
9271 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9272 || (cap->cmdchar == Ctrl_D
9273 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9274 clearopbeep(cap->oap);
9275 else if (!checkclearop(cap->oap))
9276 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9277}
9278
9279/*
9280 * Handle "J" or "gJ" command.
9281 */
9282 static void
9283nv_join(cap)
9284 cmdarg_T *cap;
9285{
9286#ifdef FEAT_VISUAL
9287 if (VIsual_active) /* join the visual lines */
9288 nv_operator(cap);
9289 else
9290#endif
9291 if (!checkclearop(cap->oap))
9292 {
9293 if (cap->count0 <= 1)
9294 cap->count0 = 2; /* default for join is two lines! */
9295 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9296 curbuf->b_ml.ml_line_count)
9297 clearopbeep(cap->oap); /* beyond last line */
9298 else
9299 {
9300 prep_redo(cap->oap->regname, cap->count0,
9301 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02009302 (void)do_join(cap->count0, cap->nchar == NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 }
9304 }
9305}
9306
9307/*
9308 * "P", "gP", "p" and "gp" commands.
9309 */
9310 static void
9311nv_put(cap)
9312 cmdarg_T *cap;
9313{
9314#ifdef FEAT_VISUAL
9315 int regname = 0;
9316 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009317 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009318 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319#endif
9320 int dir;
9321 int flags = 0;
9322
9323 if (cap->oap->op_type != OP_NOP)
9324 {
9325#ifdef FEAT_DIFF
9326 /* "dp" is ":diffput" */
9327 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9328 {
9329 clearop(cap->oap);
9330 nv_diffgetput(TRUE);
9331 }
9332 else
9333#endif
9334 clearopbeep(cap->oap);
9335 }
9336 else
9337 {
9338 dir = (cap->cmdchar == 'P'
9339 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9340 ? BACKWARD : FORWARD;
9341 prep_redo_cmd(cap);
9342 if (cap->cmdchar == 'g')
9343 flags |= PUT_CURSEND;
9344
9345#ifdef FEAT_VISUAL
9346 if (VIsual_active)
9347 {
9348 /* Putting in Visual mode: The put text replaces the selected
9349 * text. First delete the selected text, then put the new text.
9350 * Need to save and restore the registers that the delete
9351 * overwrites if the old contents is being put.
9352 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009353 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 regname = cap->oap->regname;
9355# ifdef FEAT_CLIPBOARD
9356 adjust_clip_reg(&regname);
9357# endif
Bram Moolenaar53748fc2012-01-26 11:43:09 +01009358 if (regname == 0 || regname == '"' || VIM_ISDIGIT(regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359# ifdef FEAT_CLIPBOARD
9360 || (clip_unnamed && (regname == '*' || regname == '+'))
9361# endif
9362
9363 )
9364 {
9365 /* the delete is going to overwrite the register we want to
9366 * put, save it first. */
9367 reg1 = get_register(regname, TRUE);
9368 }
9369
9370 /* Now delete the selected text. */
9371 cap->cmdchar = 'd';
9372 cap->nchar = NUL;
9373 cap->oap->regname = NUL;
9374 nv_operator(cap);
9375 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009376 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377
9378 /* delete PUT_LINE_BACKWARD; */
9379 cap->oap->regname = regname;
9380
9381 if (reg1 != NULL)
9382 {
9383 /* Delete probably changed the register we want to put, save
9384 * it first. Then put back what was there before the delete. */
9385 reg2 = get_register(regname, FALSE);
9386 put_register(regname, reg1);
9387 }
9388
9389 /* When deleted a linewise Visual area, put the register as
9390 * lines to avoid it joined with the next line. When deletion was
9391 * characterwise, split a line when putting lines. */
9392 if (VIsual_mode == 'V')
9393 flags |= PUT_LINE;
9394 else if (VIsual_mode == 'v')
9395 flags |= PUT_LINE_SPLIT;
9396 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9397 flags |= PUT_LINE_FORWARD;
9398 dir = BACKWARD;
9399 if ((VIsual_mode != 'V'
9400 && curwin->w_cursor.col < curbuf->b_op_start.col)
9401 || (VIsual_mode == 'V'
9402 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9403 /* cursor is at the end of the line or end of file, put
9404 * forward. */
9405 dir = FORWARD;
9406 }
9407#endif
9408 do_put(cap->oap->regname, dir, cap->count1, flags);
9409
9410#ifdef FEAT_VISUAL
9411 /* If a register was saved, put it back now. */
9412 if (reg2 != NULL)
9413 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009414
9415 /* What to reselect with "gv"? Selecting the just put text seems to
9416 * be the most useful, since the original text was removed. */
9417 if (was_visual)
9418 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009419 curbuf->b_visual.vi_start = curbuf->b_op_start;
9420 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009421 }
9422
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009423 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009424 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009425 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009426 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009427 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009428
9429 /* If the cursor was in that line, move it to the end of the last
9430 * line. */
9431 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9432 {
9433 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9434 coladvance((colnr_T)MAXCOL);
9435 }
9436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437#endif
9438 auto_format(FALSE, TRUE);
9439 }
9440}
9441
9442/*
9443 * "o" and "O" commands.
9444 */
9445 static void
9446nv_open(cap)
9447 cmdarg_T *cap;
9448{
9449#ifdef FEAT_DIFF
9450 /* "do" is ":diffget" */
9451 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9452 {
9453 clearop(cap->oap);
9454 nv_diffgetput(FALSE);
9455 }
9456 else
9457#endif
9458#ifdef FEAT_VISUAL
9459 if (VIsual_active) /* switch start and end of visual */
9460 v_swap_corners(cap->cmdchar);
9461 else
9462#endif
9463 n_opencmd(cap);
9464}
9465
9466#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 static void
9468nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009469 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 ProcessSniffRequests();
9472}
9473#endif
9474
9475#ifdef FEAT_NETBEANS_INTG
9476 static void
9477nv_nbcmd(cap)
9478 cmdarg_T *cap;
9479{
9480 netbeans_keycommand(cap->nchar);
9481}
9482#endif
9483
9484#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 static void
9486nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009487 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9490}
9491#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009492
9493#ifdef FEAT_AUTOCMD
9494/*
9495 * Trigger CursorHold event.
9496 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9497 * input buffer. "did_cursorhold" is set to avoid retriggering.
9498 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009499 static void
9500nv_cursorhold(cap)
9501 cmdarg_T *cap;
9502{
9503 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9504 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009505 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009506}
9507#endif