blob: 344bd7db70988101d42de78519a33620a23fa2e4 [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 */
23static colnr_T resel_VIsual_col; /* nr of cols or end col */
24
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 */
1439 static colnr_T redo_VIsual_col; /* number of cols or end column */
1440 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 {
1552 oap->start = curwin->w_cursor;
1553 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1554 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1555 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1556 VIsual_mode = redo_VIsual_mode;
1557 if (VIsual_mode == 'v')
1558 {
1559 if (redo_VIsual_line_count <= 1)
1560 curwin->w_cursor.col += redo_VIsual_col - 1;
1561 else
1562 curwin->w_cursor.col = redo_VIsual_col;
1563 }
1564 if (redo_VIsual_col == MAXCOL)
1565 {
1566 curwin->w_curswant = MAXCOL;
1567 coladvance((colnr_T)MAXCOL);
1568 }
1569 cap->count0 = redo_VIsual_count;
1570 if (redo_VIsual_count != 0)
1571 cap->count1 = redo_VIsual_count;
1572 else
1573 cap->count1 = 1;
1574 }
1575 else if (VIsual_active)
1576 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001577 if (!gui_yank)
1578 {
1579 /* Save the current VIsual area for '< and '> marks, and "gv" */
1580 curbuf->b_visual.vi_start = VIsual;
1581 curbuf->b_visual.vi_end = curwin->w_cursor;
1582 curbuf->b_visual.vi_mode = VIsual_mode;
1583 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001585 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 /* In Select mode, a linewise selection is operated upon like a
1590 * characterwise selection. */
1591 if (VIsual_select && VIsual_mode == 'V')
1592 {
1593 if (lt(VIsual, curwin->w_cursor))
1594 {
1595 VIsual.col = 0;
1596 curwin->w_cursor.col =
1597 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1598 }
1599 else
1600 {
1601 curwin->w_cursor.col = 0;
1602 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1603 }
1604 VIsual_mode = 'v';
1605 }
1606 /* If 'selection' is "exclusive", backup one character for
1607 * charwise selections. */
1608 else if (VIsual_mode == 'v')
1609 {
1610# ifdef FEAT_VIRTUALEDIT
1611 include_line_break =
1612# endif
1613 unadjust_for_sel();
1614 }
1615
1616 oap->start = VIsual;
1617 if (VIsual_mode == 'V')
1618 oap->start.col = 0;
1619 }
1620#endif /* FEAT_VISUAL */
1621
1622 /*
1623 * Set oap->start to the first position of the operated text, oap->end
1624 * to the end of the operated text. w_cursor is equal to oap->start.
1625 */
1626 if (lt(oap->start, curwin->w_cursor))
1627 {
1628#ifdef FEAT_FOLDING
1629 /* Include folded lines completely. */
1630 if (!VIsual_active)
1631 {
1632 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1633 oap->start.col = 0;
1634 if (hasFolding(curwin->w_cursor.lnum, NULL,
1635 &curwin->w_cursor.lnum))
1636 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1637 }
1638#endif
1639 oap->end = curwin->w_cursor;
1640 curwin->w_cursor = oap->start;
1641
1642 /* w_virtcol may have been updated; if the cursor goes back to its
1643 * previous position w_virtcol becomes invalid and isn't updated
1644 * automatically. */
1645 curwin->w_valid &= ~VALID_VIRTCOL;
1646 }
1647 else
1648 {
1649#ifdef FEAT_FOLDING
1650 /* Include folded lines completely. */
1651 if (!VIsual_active && oap->motion_type == MLINE)
1652 {
1653 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1654 NULL))
1655 curwin->w_cursor.col = 0;
1656 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1657 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1658 }
1659#endif
1660 oap->end = oap->start;
1661 oap->start = curwin->w_cursor;
1662 }
1663
1664 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1665
1666#ifdef FEAT_VIRTUALEDIT
1667 /* Set "virtual_op" before resetting VIsual_active. */
1668 virtual_op = virtual_active();
1669#endif
1670
1671#ifdef FEAT_VISUAL
1672 if (VIsual_active || redo_VIsual_busy)
1673 {
1674 if (VIsual_mode == Ctrl_V) /* block mode */
1675 {
1676 colnr_T start, end;
1677
1678 oap->block_mode = TRUE;
1679
1680 getvvcol(curwin, &(oap->start),
1681 &oap->start_vcol, NULL, &oap->end_vcol);
1682 if (!redo_VIsual_busy)
1683 {
1684 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1685
1686 if (start < oap->start_vcol)
1687 oap->start_vcol = start;
1688 if (end > oap->end_vcol)
1689 {
1690 if (*p_sel == 'e' && start >= 1
1691 && start - 1 >= oap->end_vcol)
1692 oap->end_vcol = start - 1;
1693 else
1694 oap->end_vcol = end;
1695 }
1696 }
1697
1698 /* if '$' was used, get oap->end_vcol from longest line */
1699 if (curwin->w_curswant == MAXCOL)
1700 {
1701 curwin->w_cursor.col = MAXCOL;
1702 oap->end_vcol = 0;
1703 for (curwin->w_cursor.lnum = oap->start.lnum;
1704 curwin->w_cursor.lnum <= oap->end.lnum;
1705 ++curwin->w_cursor.lnum)
1706 {
1707 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1708 if (end > oap->end_vcol)
1709 oap->end_vcol = end;
1710 }
1711 }
1712 else if (redo_VIsual_busy)
1713 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1714 /*
1715 * Correct oap->end.col and oap->start.col to be the
1716 * upper-left and lower-right corner of the block area.
1717 *
1718 * (Actually, this does convert column positions into character
1719 * positions)
1720 */
1721 curwin->w_cursor.lnum = oap->end.lnum;
1722 coladvance(oap->end_vcol);
1723 oap->end = curwin->w_cursor;
1724
1725 curwin->w_cursor = oap->start;
1726 coladvance(oap->start_vcol);
1727 oap->start = curwin->w_cursor;
1728 }
1729
1730 if (!redo_VIsual_busy && !gui_yank)
1731 {
1732 /*
1733 * Prepare to reselect and redo Visual: this is based on the
1734 * size of the Visual text
1735 */
1736 resel_VIsual_mode = VIsual_mode;
1737 if (curwin->w_curswant == MAXCOL)
1738 resel_VIsual_col = MAXCOL;
1739 else if (VIsual_mode == Ctrl_V)
1740 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1741 else if (oap->line_count > 1)
1742 resel_VIsual_col = oap->end.col;
1743 else
1744 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1745 resel_VIsual_line_count = oap->line_count;
1746 }
1747
1748 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1749 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1750 && oap->op_type != OP_COLON
1751#ifdef FEAT_FOLDING
1752 && oap->op_type != OP_FOLD
1753 && oap->op_type != OP_FOLDOPEN
1754 && oap->op_type != OP_FOLDOPENREC
1755 && oap->op_type != OP_FOLDCLOSE
1756 && oap->op_type != OP_FOLDCLOSEREC
1757 && oap->op_type != OP_FOLDDEL
1758 && oap->op_type != OP_FOLDDELREC
1759#endif
1760 && oap->motion_force == NUL
1761 )
1762 {
1763 /* Prepare for redoing. Only use the nchar field for "r",
1764 * otherwise it might be the second char of the operator. */
1765 prep_redo(oap->regname, 0L, NUL, 'v',
1766 get_op_char(oap->op_type),
1767 get_extra_op_char(oap->op_type),
1768 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1769 if (!redo_VIsual_busy)
1770 {
1771 redo_VIsual_mode = resel_VIsual_mode;
1772 redo_VIsual_col = resel_VIsual_col;
1773 redo_VIsual_line_count = resel_VIsual_line_count;
1774 redo_VIsual_count = cap->count0;
1775 }
1776 }
1777
1778 /*
1779 * oap->inclusive defaults to TRUE.
1780 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1781 * FALSE. This makes "d}P" and "v}dP" work the same.
1782 */
1783 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1784 oap->inclusive = TRUE;
1785 if (VIsual_mode == 'V')
1786 oap->motion_type = MLINE;
1787 else
1788 {
1789 oap->motion_type = MCHAR;
1790 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1791# ifdef FEAT_VIRTUALEDIT
1792 && (include_line_break || !virtual_op)
1793# endif
1794 )
1795 {
1796 oap->inclusive = FALSE;
1797 /* Try to include the newline, unless it's an operator
1798 * that works on lines only */
1799 if (*p_sel != 'o'
1800 && !op_on_lines(oap->op_type)
1801 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1802 {
1803 ++oap->end.lnum;
1804 oap->end.col = 0;
1805# ifdef FEAT_VIRTUALEDIT
1806 oap->end.coladd = 0;
1807# endif
1808 ++oap->line_count;
1809 }
1810 }
1811 }
1812
1813 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Switch Visual off now, so screen updating does
1817 * not show inverted text when the screen is redrawn.
1818 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1819 * no screen redraw, so it is done here to remove the inverted
1820 * part.
1821 */
1822 if (!gui_yank)
1823 {
1824 VIsual_active = FALSE;
1825# ifdef FEAT_MOUSE
1826 setmouse();
1827 mouse_dragging = 0;
1828# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001829 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 clear_cmdline = TRUE; /* unshow visual mode later */
1831#ifdef FEAT_CMDL_INFO
1832 else
1833 clear_showcmd();
1834#endif
1835 if ((oap->op_type == OP_YANK
1836 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001837 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 || oap->op_type == OP_FILTER)
1839 && oap->motion_force == NUL)
1840 redraw_curbuf_later(INVERTED);
1841 }
1842 }
1843#endif
1844
1845#ifdef FEAT_MBYTE
1846 /* Include the trailing byte of a multi-byte char. */
1847 if (has_mbyte && oap->inclusive)
1848 {
1849 int l;
1850
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001851 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (l > 1)
1853 oap->end.col += l - 1;
1854 }
1855#endif
1856 curwin->w_set_curswant = TRUE;
1857
1858 /*
1859 * oap->empty is set when start and end are the same. The inclusive
1860 * flag affects this too, unless yanking and the end is on a NUL.
1861 */
1862 oap->empty = (oap->motion_type == MCHAR
1863 && (!oap->inclusive
1864 || (oap->op_type == OP_YANK
1865 && gchar_pos(&oap->end) == NUL))
1866 && equalpos(oap->start, oap->end)
1867#ifdef FEAT_VIRTUALEDIT
1868 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1869#endif
1870 );
1871 /*
1872 * For delete, change and yank, it's an error to operate on an
1873 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1874 */
1875 empty_region_error = (oap->empty
1876 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1877
1878#ifdef FEAT_VISUAL
1879 /* Force a redraw when operating on an empty Visual region, when
1880 * 'modifiable is off or creating a fold. */
1881 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1882# ifdef FEAT_FOLDING
1883 || oap->op_type == OP_FOLD
1884# endif
1885 ))
1886 redraw_curbuf_later(INVERTED);
1887#endif
1888
1889 /*
1890 * If the end of an operator is in column one while oap->motion_type
1891 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1892 * character in the previous line. If op_start is on or before the
1893 * first non-blank in the line, the operator becomes linewise
1894 * (strange, but that's the way vi does it).
1895 */
1896 if ( oap->motion_type == MCHAR
1897 && oap->inclusive == FALSE
1898 && !(cap->retval & CA_NO_ADJ_OP_END)
1899 && oap->end.col == 0
1900#ifdef FEAT_VISUAL
1901 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001902 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#endif
1904 && oap->line_count > 1)
1905 {
1906 oap->end_adjusted = TRUE; /* remember that we did this */
1907 --oap->line_count;
1908 --oap->end.lnum;
1909 if (inindent(0))
1910 oap->motion_type = MLINE;
1911 else
1912 {
1913 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1914 if (oap->end.col)
1915 {
1916 --oap->end.col;
1917 oap->inclusive = TRUE;
1918 }
1919 }
1920 }
1921 else
1922 oap->end_adjusted = FALSE;
1923
1924 switch (oap->op_type)
1925 {
1926 case OP_LSHIFT:
1927 case OP_RSHIFT:
1928 op_shift(oap, TRUE,
1929#ifdef FEAT_VISUAL
1930 oap->is_VIsual ? (int)cap->count1 :
1931#endif
1932 1);
1933 auto_format(FALSE, TRUE);
1934 break;
1935
1936 case OP_JOIN_NS:
1937 case OP_JOIN:
1938 if (oap->line_count < 2)
1939 oap->line_count = 2;
1940 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1941 curbuf->b_ml.ml_line_count)
1942 beep_flush();
1943 else
1944 {
Bram Moolenaar893eaab2010-07-10 17:51:46 +02001945 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 auto_format(FALSE, TRUE);
1947 }
1948 break;
1949
1950 case OP_DELETE:
1951#ifdef FEAT_VISUAL
1952 VIsual_reselect = FALSE; /* don't reselect now */
1953#endif
1954 if (empty_region_error)
1955 vim_beep();
1956 else
1957 {
1958 (void)op_delete(oap);
1959 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1960 u_save_cursor(); /* cursor line wasn't saved yet */
1961 auto_format(FALSE, TRUE);
1962 }
1963 break;
1964
1965 case OP_YANK:
1966 if (empty_region_error)
1967 {
1968 if (!gui_yank)
1969 vim_beep();
1970 }
1971 else
1972 (void)op_yank(oap, FALSE, !gui_yank);
1973 check_cursor_col();
1974 break;
1975
1976 case OP_CHANGE:
1977#ifdef FEAT_VISUAL
1978 VIsual_reselect = FALSE; /* don't reselect now */
1979#endif
1980 if (empty_region_error)
1981 vim_beep();
1982 else
1983 {
1984 /* This is a new edit command, not a restart. Need to
1985 * remember it to make 'insertmode' work with mappings for
1986 * Visual mode. But do this only once and not when typed and
1987 * 'insertmode' isn't set. */
1988 if (p_im || !KeyTyped)
1989 restart_edit_save = restart_edit;
1990 else
1991 restart_edit_save = 0;
1992 restart_edit = 0;
1993 /* Reset finish_op now, don't want it set inside edit(). */
1994 finish_op = FALSE;
1995 if (op_change(oap)) /* will call edit() */
1996 cap->retval |= CA_COMMAND_BUSY;
1997 if (restart_edit == 0)
1998 restart_edit = restart_edit_save;
1999 }
2000 break;
2001
2002 case OP_FILTER:
2003 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2004 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2005 else
2006 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2007
2008 case OP_INDENT:
2009 case OP_COLON:
2010
2011#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2012 /*
2013 * If 'equalprg' is empty, do the indenting internally.
2014 */
2015 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2016 {
2017# ifdef FEAT_LISP
2018 if (curbuf->b_p_lisp)
2019 {
2020 op_reindent(oap, get_lisp_indent);
2021 break;
2022 }
2023# endif
2024# ifdef FEAT_CINDENT
2025 op_reindent(oap,
2026# ifdef FEAT_EVAL
2027 *curbuf->b_p_inde != NUL ? get_expr_indent :
2028# endif
2029 get_c_indent);
2030 break;
2031# endif
2032 }
2033#endif
2034
2035 op_colon(oap);
2036 break;
2037
2038 case OP_TILDE:
2039 case OP_UPPER:
2040 case OP_LOWER:
2041 case OP_ROT13:
2042 if (empty_region_error)
2043 vim_beep();
2044 else
2045 op_tilde(oap);
2046 check_cursor_col();
2047 break;
2048
2049 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002050#if defined(FEAT_EVAL)
2051 if (*curbuf->b_p_fex != NUL)
2052 op_formatexpr(oap); /* use expression */
2053 else
2054#endif
2055 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 op_colon(oap); /* use external command */
2057 else
2058 op_format(oap, FALSE); /* use internal function */
2059 break;
2060
2061 case OP_FORMAT2:
2062 op_format(oap, TRUE); /* use internal function */
2063 break;
2064
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002065 case OP_FUNCTION:
2066 op_function(oap); /* call 'operatorfunc' */
2067 break;
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 case OP_INSERT:
2070 case OP_APPEND:
2071#ifdef FEAT_VISUAL
2072 VIsual_reselect = FALSE; /* don't reselect now */
2073#endif
2074#ifdef FEAT_VISUALEXTRA
2075 if (empty_region_error)
2076 vim_beep();
2077 else
2078 {
2079 /* This is a new edit command, not a restart. Need to
2080 * remember it to make 'insertmode' work with mappings for
2081 * Visual mode. But do this only once. */
2082 restart_edit_save = restart_edit;
2083 restart_edit = 0;
2084
2085 op_insert(oap, cap->count1);
2086
2087 /* TODO: when inserting in several lines, should format all
2088 * the lines. */
2089 auto_format(FALSE, TRUE);
2090
2091 if (restart_edit == 0)
2092 restart_edit = restart_edit_save;
2093 }
2094#else
2095 vim_beep();
2096#endif
2097 break;
2098
2099 case OP_REPLACE:
2100#ifdef FEAT_VISUAL
2101 VIsual_reselect = FALSE; /* don't reselect now */
2102#endif
2103#ifdef FEAT_VISUALEXTRA
2104 if (empty_region_error)
2105#endif
2106 vim_beep();
2107#ifdef FEAT_VISUALEXTRA
2108 else
2109 op_replace(oap, cap->nchar);
2110#endif
2111 break;
2112
2113#ifdef FEAT_FOLDING
2114 case OP_FOLD:
2115 VIsual_reselect = FALSE; /* don't reselect now */
2116 foldCreate(oap->start.lnum, oap->end.lnum);
2117 break;
2118
2119 case OP_FOLDOPEN:
2120 case OP_FOLDOPENREC:
2121 case OP_FOLDCLOSE:
2122 case OP_FOLDCLOSEREC:
2123 VIsual_reselect = FALSE; /* don't reselect now */
2124 opFoldRange(oap->start.lnum, oap->end.lnum,
2125 oap->op_type == OP_FOLDOPEN
2126 || oap->op_type == OP_FOLDOPENREC,
2127 oap->op_type == OP_FOLDOPENREC
2128 || oap->op_type == OP_FOLDCLOSEREC,
2129 oap->is_VIsual);
2130 break;
2131
2132 case OP_FOLDDEL:
2133 case OP_FOLDDELREC:
2134 VIsual_reselect = FALSE; /* don't reselect now */
2135 deleteFold(oap->start.lnum, oap->end.lnum,
2136 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2137 break;
2138#endif
2139 default:
2140 clearopbeep(oap);
2141 }
2142#ifdef FEAT_VIRTUALEDIT
2143 virtual_op = MAYBE;
2144#endif
2145 if (!gui_yank)
2146 {
2147 /*
2148 * if 'sol' not set, go back to old column for some commands
2149 */
2150 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2151 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2152 || oap->op_type == OP_DELETE))
2153 coladvance(curwin->w_curswant = old_col);
2154 }
2155 else
2156 {
2157 curwin->w_cursor = old_cursor;
2158 }
2159#ifdef FEAT_VISUAL
2160 oap->block_mode = FALSE;
2161#endif
2162 clearop(oap);
2163 }
2164}
2165
2166/*
2167 * Handle indent and format operators and visual mode ":".
2168 */
2169 static void
2170op_colon(oap)
2171 oparg_T *oap;
2172{
2173 stuffcharReadbuff(':');
2174#ifdef FEAT_VISUAL
2175 if (oap->is_VIsual)
2176 stuffReadbuff((char_u *)"'<,'>");
2177 else
2178#endif
2179 {
2180 /*
2181 * Make the range look nice, so it can be repeated.
2182 */
2183 if (oap->start.lnum == curwin->w_cursor.lnum)
2184 stuffcharReadbuff('.');
2185 else
2186 stuffnumReadbuff((long)oap->start.lnum);
2187 if (oap->end.lnum != oap->start.lnum)
2188 {
2189 stuffcharReadbuff(',');
2190 if (oap->end.lnum == curwin->w_cursor.lnum)
2191 stuffcharReadbuff('.');
2192 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2193 stuffcharReadbuff('$');
2194 else if (oap->start.lnum == curwin->w_cursor.lnum)
2195 {
2196 stuffReadbuff((char_u *)".+");
2197 stuffnumReadbuff((long)oap->line_count - 1);
2198 }
2199 else
2200 stuffnumReadbuff((long)oap->end.lnum);
2201 }
2202 }
2203 if (oap->op_type != OP_COLON)
2204 stuffReadbuff((char_u *)"!");
2205 if (oap->op_type == OP_INDENT)
2206 {
2207#ifndef FEAT_CINDENT
2208 if (*get_equalprg() == NUL)
2209 stuffReadbuff((char_u *)"indent");
2210 else
2211#endif
2212 stuffReadbuff(get_equalprg());
2213 stuffReadbuff((char_u *)"\n");
2214 }
2215 else if (oap->op_type == OP_FORMAT)
2216 {
2217 if (*p_fp == NUL)
2218 stuffReadbuff((char_u *)"fmt");
2219 else
2220 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002221 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 }
2223
2224 /*
2225 * do_cmdline() does the rest
2226 */
2227}
2228
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002229/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002230 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002231 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002232 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002233op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002234 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002235{
2236#ifdef FEAT_EVAL
2237 char_u *(argv[1]);
2238
2239 if (*p_opfunc == NUL)
2240 EMSG(_("E774: 'operatorfunc' is empty"));
2241 else
2242 {
2243 /* Set '[ and '] marks to text to be operated on. */
2244 curbuf->b_op_start = oap->start;
2245 curbuf->b_op_end = oap->end;
2246 if (oap->motion_type != MLINE && !oap->inclusive)
2247 /* Exclude the end position. */
2248 decl(&curbuf->b_op_end);
2249
2250 if (oap->block_mode)
2251 argv[0] = (char_u *)"block";
2252 else if (oap->motion_type == MLINE)
2253 argv[0] = (char_u *)"line";
2254 else
2255 argv[0] = (char_u *)"char";
2256 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2257 }
2258#else
2259 EMSG(_("E775: Eval feature not available"));
2260#endif
2261}
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#if defined(FEAT_MOUSE) || defined(PROTO)
2264/*
2265 * Do the appropriate action for the current mouse click in the current mode.
2266 * Not used for Command-line mode.
2267 *
2268 * Normal Mode:
2269 * event modi- position visual change action
2270 * fier cursor window
2271 * left press - yes end yes
2272 * left press C yes end yes "^]" (2)
2273 * left press S yes end yes "*" (2)
2274 * left drag - yes start if moved no
2275 * left relse - yes start if moved no
2276 * middle press - yes if not active no put register
2277 * middle press - yes if active no yank and put
2278 * right press - yes start or extend yes
2279 * right press S yes no change yes "#" (2)
2280 * right drag - yes extend no
2281 * right relse - yes extend no
2282 *
2283 * Insert or Replace Mode:
2284 * event modi- position visual change action
2285 * fier cursor window
2286 * left press - yes (cannot be active) yes
2287 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2288 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2289 * left drag - yes start or extend (1) no CTRL-O (1)
2290 * left relse - yes start or extend (1) no CTRL-O (1)
2291 * middle press - no (cannot be active) no put register
2292 * right press - yes start or extend yes CTRL-O
2293 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2294 *
2295 * (1) only if mouse pointer moved since press
2296 * (2) only if click is in same buffer
2297 *
2298 * Return TRUE if start_arrow() should be called for edit mode.
2299 */
2300 int
2301do_mouse(oap, c, dir, count, fixindent)
2302 oparg_T *oap; /* operator argument, can be NULL */
2303 int c; /* K_LEFTMOUSE, etc */
2304 int dir; /* Direction to 'put' if necessary */
2305 long count;
2306 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2307{
2308 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2309 static int got_click = FALSE; /* got a click some time back */
2310
2311 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2312 int is_click; /* If FALSE it's a drag or release event */
2313 int is_drag; /* If TRUE it's a drag event */
2314 int jump_flags = 0; /* flags for jump_to_mouse() */
2315 pos_T start_visual;
2316 int moved; /* Has cursor moved? */
2317 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002318#ifdef FEAT_WINDOWS
2319 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#ifdef FEAT_VERTSPLIT
2322 int in_sep_line; /* mouse in vertical separator line */
2323#endif
2324 int c1, c2;
2325#if defined(FEAT_FOLDING)
2326 pos_T save_cursor;
2327#endif
2328 win_T *old_curwin = curwin;
2329#ifdef FEAT_VISUAL
2330 static pos_T orig_cursor;
2331 colnr_T leftcol, rightcol;
2332 pos_T end_visual;
2333 int diff;
2334 int old_active = VIsual_active;
2335 int old_mode = VIsual_mode;
2336#endif
2337 int regname;
2338
2339#if defined(FEAT_FOLDING)
2340 save_cursor = curwin->w_cursor;
2341#endif
2342
2343 /*
2344 * When GUI is active, always recognize mouse events, otherwise:
2345 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2346 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2347 * - For command line and insert mode 'mouse' is checked before calling
2348 * do_mouse().
2349 */
2350 if (do_always)
2351 do_always = FALSE;
2352 else
2353#ifdef FEAT_GUI
2354 if (!gui.in_use)
2355#endif
2356 {
2357#ifdef FEAT_VISUAL
2358 if (VIsual_active)
2359 {
2360 if (!mouse_has(MOUSE_VISUAL))
2361 return FALSE;
2362 }
2363 else
2364#endif
2365 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2366 return FALSE;
2367 }
2368
2369 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2370
2371#ifdef FEAT_MOUSESHAPE
2372 /* May have stopped dragging the status or separator line. The pointer is
2373 * most likely still on the status or separator line. */
2374 if (!is_drag && drag_status_line)
2375 {
2376 drag_status_line = FALSE;
2377 update_mouseshape(SHAPE_IDX_STATUS);
2378 }
2379# ifdef FEAT_VERTSPLIT
2380 if (!is_drag && drag_sep_line)
2381 {
2382 drag_sep_line = FALSE;
2383 update_mouseshape(SHAPE_IDX_VSEP);
2384 }
2385# endif
2386#endif
2387
2388 /*
2389 * Ignore drag and release events if we didn't get a click.
2390 */
2391 if (is_click)
2392 got_click = TRUE;
2393 else
2394 {
2395 if (!got_click) /* didn't get click, ignore */
2396 return FALSE;
2397 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002400#ifdef FEAT_WINDOWS
2401 if (in_tab_line)
2402 {
2403 in_tab_line = FALSE;
2404 return FALSE;
2405 }
2406#endif
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409
Bram Moolenaar64969662005-12-14 21:59:55 +00002410#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002412 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
2414 if ((mod_mask & MOD_MASK_ALT))
2415 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
2418 /*
2419 * CTRL right mouse button does CTRL-T
2420 */
2421 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2422 {
2423 if (State & INSERT)
2424 stuffcharReadbuff(Ctrl_O);
2425 if (count > 1)
2426 stuffnumReadbuff(count);
2427 stuffcharReadbuff(Ctrl_T);
2428 got_click = FALSE; /* ignore drag&release now */
2429 return FALSE;
2430 }
2431
2432 /*
2433 * CTRL only works with left mouse button
2434 */
2435 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2436 return FALSE;
2437
2438 /*
2439 * When a modifier is down, ignore drag and release events, as well as
2440 * multiple clicks and the middle mouse button.
2441 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2442 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002443 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2444 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 && (!is_click
2446 || (mod_mask & MOD_MASK_MULTI_CLICK)
2447 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002448 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 && mouse_model_popup()
2450 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002451 && !((mod_mask & MOD_MASK_ALT)
2452 && !mouse_model_popup()
2453 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 )
2455 return FALSE;
2456
2457 /*
2458 * If the button press was used as the movement command for an operator
2459 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2460 * drag/release events.
2461 */
2462 if (!is_click && which_button == MOUSE_MIDDLE)
2463 return FALSE;
2464
2465 if (oap != NULL)
2466 regname = oap->regname;
2467 else
2468 regname = 0;
2469
2470 /*
2471 * Middle mouse button does a 'put' of the selected text
2472 */
2473 if (which_button == MOUSE_MIDDLE)
2474 {
2475 if (State == NORMAL)
2476 {
2477 /*
2478 * If an operator was pending, we don't know what the user wanted
2479 * to do. Go back to normal mode: Clear the operator and beep().
2480 */
2481 if (oap != NULL && oap->op_type != OP_NOP)
2482 {
2483 clearopbeep(oap);
2484 return FALSE;
2485 }
2486
2487#ifdef FEAT_VISUAL
2488 /*
2489 * If visual was active, yank the highlighted text and put it
2490 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002491 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 if (VIsual_active)
2494 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002495 if (VIsual_select)
2496 {
2497 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002498 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002499 }
2500 else
2501 {
2502 stuffcharReadbuff('y');
2503 stuffcharReadbuff(K_MIDDLEMOUSE);
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 do_always = TRUE; /* ignore 'mouse' setting next time */
2506 return FALSE;
2507 }
2508#endif
2509 /*
2510 * The rest is below jump_to_mouse()
2511 */
2512 }
2513
2514 else if ((State & INSERT) == 0)
2515 return FALSE;
2516
2517 /*
2518 * Middle click in insert mode doesn't move the mouse, just insert the
2519 * contents of a register. '.' register is special, can't insert that
2520 * with do_put().
2521 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2522 * happens for the GUI).
2523 */
2524 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2525 {
2526 if (regname == '.')
2527 insert_reg(regname, TRUE);
2528 else
2529 {
2530#ifdef FEAT_CLIPBOARD
2531 if (clip_star.available && regname == 0)
2532 regname = '*';
2533#endif
2534 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2535 insert_reg(regname, TRUE);
2536 else
2537 {
2538 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2539
2540 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2541 AppendCharToRedobuff(Ctrl_R);
2542 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2543 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2544 }
2545 }
2546 return FALSE;
2547 }
2548 }
2549
2550 /* When dragging or button-up stay in the same window. */
2551 if (!is_click)
2552 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2553
2554 start_visual.lnum = 0;
2555
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556#ifdef FEAT_WINDOWS
2557 /* Check for clicking in the tab page line. */
2558 if (mouse_row == 0 && firstwin->w_winrow > 0)
2559 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002560 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002561 {
2562 if (in_tab_line)
2563 {
2564 c1 = TabPageIdxs[mouse_col];
2565 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2566 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002567 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002568 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* click in a tab selects that tab page */
2571 if (is_click
2572# ifdef FEAT_CMDWIN
2573 && cmdwin_type == 0
2574# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002576 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002577 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002578 c1 = TabPageIdxs[mouse_col];
2579 if (c1 >= 0)
2580 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002581 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2582 {
2583 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002585 tabpage_new();
2586 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2587 }
2588 else
2589 {
2590 /* Go to specified tab page, or next one if not clicking
2591 * on a label. */
2592 goto_tabpage(c1);
2593
2594 /* It's like clicking on the status line of a window. */
2595 if (curwin != old_curwin)
2596 end_visual_mode();
2597 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 }
2599 else if (c1 < 0)
2600 {
2601 tabpage_T *tp;
2602
2603 /* Close the current or specified tab page. */
2604 if (c1 == -999)
2605 tp = curtab;
2606 else
2607 tp = find_tabpage(-c1);
2608 if (tp == curtab)
2609 {
2610 if (first_tabpage->tp_next != NULL)
2611 tabpage_close(FALSE);
2612 }
2613 else if (tp != NULL)
2614 tabpage_close_other(tp, FALSE);
2615 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002619 else if (is_drag && in_tab_line)
2620 {
2621 c1 = TabPageIdxs[mouse_col];
2622 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2623 return FALSE;
2624 }
2625
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 /*
2629 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2630 * right button up -> pop-up menu
2631 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002632 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 */
2634 if (mouse_model_popup())
2635 {
2636 if (which_button == MOUSE_RIGHT
2637 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2638 {
2639 /*
2640 * NOTE: Ignore right button down and drag mouse events.
2641 * Windows only shows the popup menu on the button up event.
2642 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002643#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2644 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (!is_click)
2646 return FALSE;
2647#endif
2648#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2649 if (is_click || is_drag)
2650 return FALSE;
2651#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002652#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2654 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2655 if (gui.in_use)
2656 {
2657 jump_flags = 0;
2658 if (STRCMP(p_mousem, "popup_setpos") == 0)
2659 {
2660 /* First set the cursor position before showing the popup
2661 * menu. */
2662#ifdef FEAT_VISUAL
2663 if (VIsual_active)
2664 {
2665 pos_T m_pos;
2666
2667 /*
2668 * set MOUSE_MAY_STOP_VIS if we are outside the
2669 * selection or the current window (might have false
2670 * negative here)
2671 */
2672 if (mouse_row < W_WINROW(curwin)
2673 || mouse_row
2674 > (W_WINROW(curwin) + curwin->w_height))
2675 jump_flags = MOUSE_MAY_STOP_VIS;
2676 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2677 jump_flags = MOUSE_MAY_STOP_VIS;
2678 else
2679 {
2680 if ((lt(curwin->w_cursor, VIsual)
2681 && (lt(m_pos, curwin->w_cursor)
2682 || lt(VIsual, m_pos)))
2683 || (lt(VIsual, curwin->w_cursor)
2684 && (lt(m_pos, VIsual)
2685 || lt(curwin->w_cursor, m_pos))))
2686 {
2687 jump_flags = MOUSE_MAY_STOP_VIS;
2688 }
2689 else if (VIsual_mode == Ctrl_V)
2690 {
2691 getvcols(curwin, &curwin->w_cursor, &VIsual,
2692 &leftcol, &rightcol);
2693 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2694 if (m_pos.col < leftcol || m_pos.col > rightcol)
2695 jump_flags = MOUSE_MAY_STOP_VIS;
2696 }
2697 }
2698 }
2699 else
2700 jump_flags = MOUSE_MAY_STOP_VIS;
2701#endif
2702 }
2703 if (jump_flags)
2704 {
2705 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2706 update_curbuf(
2707#ifdef FEAT_VISUAL
2708 VIsual_active ? INVERTED :
2709#endif
2710 VALID);
2711 setcursor();
2712 out_flush(); /* Update before showing popup menu */
2713 }
2714# ifdef FEAT_MENU
2715 gui_show_popupmenu();
2716# endif
2717 return (jump_flags & CURSOR_MOVED) != 0;
2718 }
2719 else
2720 return FALSE;
2721#else
2722 return FALSE;
2723#endif
2724 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002725 if (which_button == MOUSE_LEFT
2726 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 which_button = MOUSE_RIGHT;
2729 mod_mask &= ~MOD_MASK_SHIFT;
2730 }
2731 }
2732
2733#ifdef FEAT_VISUAL
2734 if ((State & (NORMAL | INSERT))
2735 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2736 {
2737 if (which_button == MOUSE_LEFT)
2738 {
2739 if (is_click)
2740 {
2741 /* stop Visual mode for a left click in a window, but not when
2742 * on a status line */
2743 if (VIsual_active)
2744 jump_flags |= MOUSE_MAY_STOP_VIS;
2745 }
2746 else if (mouse_has(MOUSE_VISUAL))
2747 jump_flags |= MOUSE_MAY_VIS;
2748 }
2749 else if (which_button == MOUSE_RIGHT)
2750 {
2751 if (is_click && VIsual_active)
2752 {
2753 /*
2754 * Remember the start and end of visual before moving the
2755 * cursor.
2756 */
2757 if (lt(curwin->w_cursor, VIsual))
2758 {
2759 start_visual = curwin->w_cursor;
2760 end_visual = VIsual;
2761 }
2762 else
2763 {
2764 start_visual = VIsual;
2765 end_visual = curwin->w_cursor;
2766 }
2767 }
2768 jump_flags |= MOUSE_FOCUS;
2769 if (mouse_has(MOUSE_VISUAL))
2770 jump_flags |= MOUSE_MAY_VIS;
2771 }
2772 }
2773#endif
2774
2775 /*
2776 * If an operator is pending, ignore all drags and releases until the
2777 * next mouse click.
2778 */
2779 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2780 {
2781 got_click = FALSE;
2782 oap->motion_type = MCHAR;
2783 }
2784
2785 /* When releasing the button let jump_to_mouse() know. */
2786 if (!is_click && !is_drag)
2787 jump_flags |= MOUSE_RELEASED;
2788
2789 /*
2790 * JUMP!
2791 */
2792 jump_flags = jump_to_mouse(jump_flags,
2793 oap == NULL ? NULL : &(oap->inclusive), which_button);
2794 moved = (jump_flags & CURSOR_MOVED);
2795 in_status_line = (jump_flags & IN_STATUS_LINE);
2796#ifdef FEAT_VERTSPLIT
2797 in_sep_line = (jump_flags & IN_SEP_LINE);
2798#endif
2799
2800#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002801 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2803 {
2804 int key = KEY2TERMCAP1(c);
2805
2806 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2807 || key == (int)KE_RIGHTRELEASE)
2808 netbeans_button_release(which_button);
2809 }
2810#endif
2811
2812 /* When jumping to another window, clear a pending operator. That's a bit
2813 * friendlier than beeping and not jumping to that window. */
2814 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2815 clearop(oap);
2816
2817#ifdef FEAT_FOLDING
2818 if (mod_mask == 0
2819 && !is_drag
2820 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2821 && which_button == MOUSE_LEFT)
2822 {
2823 /* open or close a fold at this line */
2824 if (jump_flags & MOUSE_FOLD_OPEN)
2825 openFold(curwin->w_cursor.lnum, 1L);
2826 else
2827 closeFold(curwin->w_cursor.lnum, 1L);
2828 /* don't move the cursor if still in the same window */
2829 if (curwin == old_curwin)
2830 curwin->w_cursor = save_cursor;
2831 }
2832#endif
2833
2834#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2835 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2836 {
2837 clip_modeless(which_button, is_click, is_drag);
2838 return FALSE;
2839 }
2840#endif
2841
2842#ifdef FEAT_VISUAL
2843 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002844 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (VIsual_active && is_drag && p_so)
2846 {
2847 /* In the very first line, allow scrolling one line */
2848 if (mouse_row == 0)
2849 mouse_dragging = 2;
2850 else
2851 mouse_dragging = 1;
2852 }
2853
2854 /* When dragging the mouse above the window, scroll down. */
2855 if (is_drag && mouse_row < 0 && !in_status_line)
2856 {
2857 scroll_redraw(FALSE, 1L);
2858 mouse_row = 0;
2859 }
2860
2861 if (start_visual.lnum) /* right click in visual mode */
2862 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002863 /* When ALT is pressed make Visual mode blockwise. */
2864 if (mod_mask & MOD_MASK_ALT)
2865 VIsual_mode = Ctrl_V;
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 /*
2868 * In Visual-block mode, divide the area in four, pick up the corner
2869 * that is in the quarter that the cursor is in.
2870 */
2871 if (VIsual_mode == Ctrl_V)
2872 {
2873 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2874 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2875 end_visual.col = leftcol;
2876 else
2877 end_visual.col = rightcol;
2878 if (curwin->w_cursor.lnum <
2879 (start_visual.lnum + end_visual.lnum) / 2)
2880 end_visual.lnum = end_visual.lnum;
2881 else
2882 end_visual.lnum = start_visual.lnum;
2883
2884 /* move VIsual to the right column */
2885 start_visual = curwin->w_cursor; /* save the cursor pos */
2886 curwin->w_cursor = end_visual;
2887 coladvance(end_visual.col);
2888 VIsual = curwin->w_cursor;
2889 curwin->w_cursor = start_visual; /* restore the cursor */
2890 }
2891 else
2892 {
2893 /*
2894 * If the click is before the start of visual, change the start.
2895 * If the click is after the end of visual, change the end. If
2896 * the click is inside the visual, change the closest side.
2897 */
2898 if (lt(curwin->w_cursor, start_visual))
2899 VIsual = end_visual;
2900 else if (lt(end_visual, curwin->w_cursor))
2901 VIsual = start_visual;
2902 else
2903 {
2904 /* In the same line, compare column number */
2905 if (end_visual.lnum == start_visual.lnum)
2906 {
2907 if (curwin->w_cursor.col - start_visual.col >
2908 end_visual.col - curwin->w_cursor.col)
2909 VIsual = start_visual;
2910 else
2911 VIsual = end_visual;
2912 }
2913
2914 /* In different lines, compare line number */
2915 else
2916 {
2917 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2918 (end_visual.lnum - curwin->w_cursor.lnum);
2919
2920 if (diff > 0) /* closest to end */
2921 VIsual = start_visual;
2922 else if (diff < 0) /* closest to start */
2923 VIsual = end_visual;
2924 else /* in the middle line */
2925 {
2926 if (curwin->w_cursor.col <
2927 (start_visual.col + end_visual.col) / 2)
2928 VIsual = end_visual;
2929 else
2930 VIsual = start_visual;
2931 }
2932 }
2933 }
2934 }
2935 }
2936 /*
2937 * If Visual mode started in insert mode, execute "CTRL-O"
2938 */
2939 else if ((State & INSERT) && VIsual_active)
2940 stuffcharReadbuff(Ctrl_O);
2941#endif
2942
2943 /*
2944 * Middle mouse click: Put text before cursor.
2945 */
2946 if (which_button == MOUSE_MIDDLE)
2947 {
2948#ifdef FEAT_CLIPBOARD
2949 if (clip_star.available && regname == 0)
2950 regname = '*';
2951#endif
2952 if (yank_register_mline(regname))
2953 {
2954 if (mouse_past_bottom)
2955 dir = FORWARD;
2956 }
2957 else if (mouse_past_eol)
2958 dir = FORWARD;
2959
2960 if (fixindent)
2961 {
2962 c1 = (dir == BACKWARD) ? '[' : ']';
2963 c2 = 'p';
2964 }
2965 else
2966 {
2967 c1 = (dir == FORWARD) ? 'p' : 'P';
2968 c2 = NUL;
2969 }
2970 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2971
2972 /*
2973 * Remember where the paste started, so in edit() Insstart can be set
2974 * to this position
2975 */
2976 if (restart_edit != 0)
2977 where_paste_started = curwin->w_cursor;
2978 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2979 }
2980
2981#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2982 /*
2983 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2984 * error under the mouse pointer.
2985 */
2986 else if (((mod_mask & MOD_MASK_CTRL)
2987 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2988 && bt_quickfix(curbuf))
2989 {
2990 if (State & INSERT)
2991 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002992 if (curwin->w_llist_ref == NULL) /* quickfix window */
2993 stuffReadbuff((char_u *)":.cc\n");
2994 else /* location list window */
2995 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 got_click = FALSE; /* ignore drag&release now */
2997 }
2998#endif
2999
3000 /*
3001 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3002 * under the mouse pointer.
3003 */
3004 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3005 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3006 {
3007 if (State & INSERT)
3008 stuffcharReadbuff(Ctrl_O);
3009 stuffcharReadbuff(Ctrl_RSB);
3010 got_click = FALSE; /* ignore drag&release now */
3011 }
3012
3013 /*
3014 * Shift-Mouse click searches for the next occurrence of the word under
3015 * the mouse pointer
3016 */
3017 else if ((mod_mask & MOD_MASK_SHIFT))
3018 {
3019 if (State & INSERT
3020#ifdef FEAT_VISUAL
3021 || (VIsual_active && VIsual_select)
3022#endif
3023 )
3024 stuffcharReadbuff(Ctrl_O);
3025 if (which_button == MOUSE_LEFT)
3026 stuffcharReadbuff('*');
3027 else /* MOUSE_RIGHT */
3028 stuffcharReadbuff('#');
3029 }
3030
3031 /* Handle double clicks, unless on status line */
3032 else if (in_status_line)
3033 {
3034#ifdef FEAT_MOUSESHAPE
3035 if ((is_drag || is_click) && !drag_status_line)
3036 {
3037 drag_status_line = TRUE;
3038 update_mouseshape(-1);
3039 }
3040#endif
3041 }
3042#ifdef FEAT_VERTSPLIT
3043 else if (in_sep_line)
3044 {
3045# ifdef FEAT_MOUSESHAPE
3046 if ((is_drag || is_click) && !drag_sep_line)
3047 {
3048 drag_sep_line = TRUE;
3049 update_mouseshape(-1);
3050 }
3051# endif
3052 }
3053#endif
3054#ifdef FEAT_VISUAL
3055 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3056 && mouse_has(MOUSE_VISUAL))
3057 {
3058 if (is_click || !VIsual_active)
3059 {
3060 if (VIsual_active)
3061 orig_cursor = VIsual;
3062 else
3063 {
3064 check_visual_highlight();
3065 VIsual = curwin->w_cursor;
3066 orig_cursor = VIsual;
3067 VIsual_active = TRUE;
3068 VIsual_reselect = TRUE;
3069 /* start Select mode if 'selectmode' contains "mouse" */
3070 may_start_select('o');
3071 setmouse();
3072 }
3073 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003074 {
3075 /* Double click with ALT pressed makes it blockwise. */
3076 if (mod_mask & MOD_MASK_ALT)
3077 VIsual_mode = Ctrl_V;
3078 else
3079 VIsual_mode = 'v';
3080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3082 VIsual_mode = 'V';
3083 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3084 VIsual_mode = Ctrl_V;
3085#ifdef FEAT_CLIPBOARD
3086 /* Make sure the clipboard gets updated. Needed because start and
3087 * end may still be the same, and the selection needs to be owned */
3088 clip_star.vmode = NUL;
3089#endif
3090 }
3091 /*
3092 * A double click selects a word or a block.
3093 */
3094 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3095 {
3096 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003097 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 if (is_click)
3100 {
3101 /* If the character under the cursor (skipping white space) is
3102 * not a word character, try finding a match and select a (),
3103 * {}, [], #if/#endif, etc. block. */
3104 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003105 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 inc(&end_visual);
3107 if (oap != NULL)
3108 oap->motion_type = MCHAR;
3109 if (oap != NULL
3110 && VIsual_mode == 'v'
3111 && !vim_iswordc(gchar_pos(&end_visual))
3112 && equalpos(curwin->w_cursor, VIsual)
3113 && (pos = findmatch(oap, NUL)) != NULL)
3114 {
3115 curwin->w_cursor = *pos;
3116 if (oap->motion_type == MLINE)
3117 VIsual_mode = 'V';
3118 else if (*p_sel == 'e')
3119 {
3120 if (lt(curwin->w_cursor, VIsual))
3121 ++VIsual.col;
3122 else
3123 ++curwin->w_cursor.col;
3124 }
3125 }
3126 }
3127
3128 if (pos == NULL && (is_click || is_drag))
3129 {
3130 /* When not found a match or when dragging: extend to include
3131 * a word. */
3132 if (lt(curwin->w_cursor, orig_cursor))
3133 {
3134 find_start_of_word(&curwin->w_cursor);
3135 find_end_of_word(&VIsual);
3136 }
3137 else
3138 {
3139 find_start_of_word(&VIsual);
3140 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3141#ifdef FEAT_MBYTE
3142 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003143 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#else
3145 ++curwin->w_cursor.col;
3146#endif
3147 find_end_of_word(&curwin->w_cursor);
3148 }
3149 }
3150 curwin->w_set_curswant = TRUE;
3151 }
3152 if (is_click)
3153 redraw_curbuf_later(INVERTED); /* update the inversion */
3154 }
3155 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003156 {
3157 if (mod_mask & MOD_MASK_ALT)
3158 VIsual_mode = Ctrl_V;
3159 else
3160 VIsual_mode = 'v';
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003164 if ((!VIsual_active && old_active && mode_displayed)
3165 || (VIsual_active && p_smd && msg_silent == 0
3166 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 redraw_cmdline = TRUE;
3168#endif
3169
3170 return moved;
3171}
3172
3173#ifdef FEAT_VISUAL
3174/*
3175 * Move "pos" back to the start of the word it's in.
3176 */
3177 static void
3178find_start_of_word(pos)
3179 pos_T *pos;
3180{
3181 char_u *line;
3182 int cclass;
3183 int col;
3184
3185 line = ml_get(pos->lnum);
3186 cclass = get_mouse_class(line + pos->col);
3187
3188 while (pos->col > 0)
3189 {
3190 col = pos->col - 1;
3191#ifdef FEAT_MBYTE
3192 col -= (*mb_head_off)(line, line + col);
3193#endif
3194 if (get_mouse_class(line + col) != cclass)
3195 break;
3196 pos->col = col;
3197 }
3198}
3199
3200/*
3201 * Move "pos" forward to the end of the word it's in.
3202 * When 'selection' is "exclusive", the position is just after the word.
3203 */
3204 static void
3205find_end_of_word(pos)
3206 pos_T *pos;
3207{
3208 char_u *line;
3209 int cclass;
3210 int col;
3211
3212 line = ml_get(pos->lnum);
3213 if (*p_sel == 'e' && pos->col > 0)
3214 {
3215 --pos->col;
3216#ifdef FEAT_MBYTE
3217 pos->col -= (*mb_head_off)(line, line + pos->col);
3218#endif
3219 }
3220 cclass = get_mouse_class(line + pos->col);
3221 while (line[pos->col] != NUL)
3222 {
3223#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003224 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225#else
3226 col = pos->col + 1;
3227#endif
3228 if (get_mouse_class(line + col) != cclass)
3229 {
3230 if (*p_sel == 'e')
3231 pos->col = col;
3232 break;
3233 }
3234 pos->col = col;
3235 }
3236}
3237
3238/*
3239 * Get class of a character for selection: same class means same word.
3240 * 0: blank
3241 * 1: punctuation groups
3242 * 2: normal word character
3243 * >2: multi-byte word character.
3244 */
3245 static int
3246get_mouse_class(p)
3247 char_u *p;
3248{
3249 int c;
3250
3251#ifdef FEAT_MBYTE
3252 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3253 return mb_get_class(p);
3254#endif
3255
3256 c = *p;
3257 if (c == ' ' || c == '\t')
3258 return 0;
3259
3260 if (vim_iswordc(c))
3261 return 2;
3262
3263 /*
3264 * There are a few special cases where we want certain combinations of
3265 * characters to be considered as a single word. These are things like
3266 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003267 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 */
3269 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3270 return 1;
3271 return c;
3272}
3273#endif /* FEAT_VISUAL */
3274#endif /* FEAT_MOUSE */
3275
3276#if defined(FEAT_VISUAL) || defined(PROTO)
3277/*
3278 * Check if highlighting for visual mode is possible, give a warning message
3279 * if not.
3280 */
3281 void
3282check_visual_highlight()
3283{
3284 static int did_check = FALSE;
3285
3286 if (full_screen)
3287 {
3288 if (!did_check && hl_attr(HLF_V) == 0)
3289 MSG(_("Warning: terminal cannot highlight"));
3290 did_check = TRUE;
3291 }
3292}
3293
3294/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003295 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 * This function should ALWAYS be called to end Visual mode, except from
3297 * do_pending_operator().
3298 */
3299 void
3300end_visual_mode()
3301{
3302#ifdef FEAT_CLIPBOARD
3303 /*
3304 * If we are using the clipboard, then remember what was selected in case
3305 * we need to paste it somewhere while we still own the selection.
3306 * Only do this when the clipboard is already owned. Don't want to grab
3307 * the selection when hitting ESC.
3308 */
3309 if (clip_star.available && clip_star.owned)
3310 clip_auto_select();
3311#endif
3312
3313 VIsual_active = FALSE;
3314#ifdef FEAT_MOUSE
3315 setmouse();
3316 mouse_dragging = 0;
3317#endif
3318
3319 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003320 curbuf->b_visual.vi_mode = VIsual_mode;
3321 curbuf->b_visual.vi_start = VIsual;
3322 curbuf->b_visual.vi_end = curwin->w_cursor;
3323 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#ifdef FEAT_EVAL
3325 curbuf->b_visual_mode_eval = VIsual_mode;
3326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_VIRTUALEDIT
3328 if (!virtual_active())
3329 curwin->w_cursor.coladd = 0;
3330#endif
3331
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003332 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 clear_cmdline = TRUE; /* unshow visual mode later */
3334#ifdef FEAT_CMDL_INFO
3335 else
3336 clear_showcmd();
3337#endif
3338
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003339 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341
3342/*
3343 * Reset VIsual_active and VIsual_reselect.
3344 */
3345 void
3346reset_VIsual_and_resel()
3347{
3348 if (VIsual_active)
3349 {
3350 end_visual_mode();
3351 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3352 }
3353 VIsual_reselect = FALSE;
3354}
3355
3356/*
3357 * Reset VIsual_active and VIsual_reselect if it's set.
3358 */
3359 void
3360reset_VIsual()
3361{
3362 if (VIsual_active)
3363 {
3364 end_visual_mode();
3365 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3366 VIsual_reselect = FALSE;
3367 }
3368}
3369#endif /* FEAT_VISUAL */
3370
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3373
3374/*
3375 * Check for a balloon-eval special item to include when searching for an
3376 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3377 * Returns TRUE if the character at "*ptr" should be included.
3378 * "dir" is FORWARD or BACKWARD, the direction of searching.
3379 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3380 * "bnp" points to a counter for square brackets.
3381 */
3382 static int
3383find_is_eval_item(ptr, colp, bnp, dir)
3384 char_u *ptr;
3385 int *colp;
3386 int *bnp;
3387 int dir;
3388{
3389 /* Accept everything inside []. */
3390 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3391 ++*bnp;
3392 if (*bnp > 0)
3393 {
3394 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3395 --*bnp;
3396 return TRUE;
3397 }
3398
3399 /* skip over "s.var" */
3400 if (*ptr == '.')
3401 return TRUE;
3402
3403 /* two-character item: s->var */
3404 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3405 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3406 {
3407 *colp += dir;
3408 return TRUE;
3409 }
3410 return FALSE;
3411}
3412#endif
3413
3414/*
3415 * Find the identifier under or to the right of the cursor.
3416 * "find_type" can have one of three values:
3417 * FIND_IDENT: find an identifier (keyword)
3418 * FIND_STRING: find any non-white string
3419 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * There are three steps:
3423 * 1. Search forward for the start of an identifier/string. Doesn't move if
3424 * already on one.
3425 * 2. Search backward for the start of this identifier/string.
3426 * This doesn't match the real Vi but I like it a little better and it
3427 * shouldn't bother anyone.
3428 * 3. Search forward to the end of this identifier/string.
3429 * When FIND_IDENT isn't defined, we backup until a blank.
3430 *
3431 * Returns the length of the string, or zero if no string is found.
3432 * If a string is found, a pointer to the string is put in "*string". This
3433 * string is not always NUL terminated.
3434 */
3435 int
3436find_ident_under_cursor(string, find_type)
3437 char_u **string;
3438 int find_type;
3439{
3440 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3441 curwin->w_cursor.col, string, find_type);
3442}
3443
3444/*
3445 * Like find_ident_under_cursor(), but for any window and any position.
3446 * However: Uses 'iskeyword' from the current window!.
3447 */
3448 int
3449find_ident_at_pos(wp, lnum, startcol, string, find_type)
3450 win_T *wp;
3451 linenr_T lnum;
3452 colnr_T startcol;
3453 char_u **string;
3454 int find_type;
3455{
3456 char_u *ptr;
3457 int col = 0; /* init to shut up GCC */
3458 int i;
3459#ifdef FEAT_MBYTE
3460 int this_class = 0;
3461 int prev_class;
3462 int prevcol;
3463#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 int bn = 0; /* bracket nesting */
3466#endif
3467
3468 /*
3469 * if i == 0: try to find an identifier
3470 * if i == 1: try to find any non-white string
3471 */
3472 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3473 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3474 {
3475 /*
3476 * 1. skip to start of identifier/string
3477 */
3478 col = startcol;
3479#ifdef FEAT_MBYTE
3480 if (has_mbyte)
3481 {
3482 while (ptr[col] != NUL)
3483 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 /* Stop at a ']' to evaluate "a[x]". */
3486 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3487 break;
3488# endif
3489 this_class = mb_get_class(ptr + col);
3490 if (this_class != 0 && (i == 1 || this_class != 1))
3491 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003492 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 }
3495 else
3496#endif
3497 while (ptr[col] != NUL
3498 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3501# endif
3502 )
3503 ++col;
3504
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* When starting on a ']' count it, so that we include the '['. */
3507 bn = ptr[col] == ']';
3508#endif
3509
3510 /*
3511 * 2. Back up to start of identifier/string.
3512 */
3513#ifdef FEAT_MBYTE
3514 if (has_mbyte)
3515 {
3516 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003517# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3519 this_class = mb_get_class((char_u *)"a");
3520 else
3521# endif
3522 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3526 prev_class = mb_get_class(ptr + prevcol);
3527 if (this_class != prev_class
3528 && (i == 0
3529 || prev_class == 0
3530 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 && (!(find_type & FIND_EVAL)
3533 || prevcol == 0
3534 || !find_is_eval_item(ptr + prevcol, &prevcol,
3535 &bn, BACKWARD))
3536# endif
3537 )
3538 break;
3539 col = prevcol;
3540 }
3541
3542 /* If we don't want just any old string, or we've found an
3543 * identifier, stop searching. */
3544 if (this_class > 2)
3545 this_class = 2;
3546 if (!(find_type & FIND_STRING) || this_class == 2)
3547 break;
3548 }
3549 else
3550#endif
3551 {
3552 while (col > 0
3553 && ((i == 0
3554 ? vim_iswordc(ptr[col - 1])
3555 : (!vim_iswhite(ptr[col - 1])
3556 && (!(find_type & FIND_IDENT)
3557 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 || ((find_type & FIND_EVAL)
3560 && col > 1
3561 && find_is_eval_item(ptr + col - 1, &col,
3562 &bn, BACKWARD))
3563#endif
3564 ))
3565 --col;
3566
3567 /* If we don't want just any old string, or we've found an
3568 * identifier, stop searching. */
3569 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3570 break;
3571 }
3572 }
3573
3574 if (ptr[col] == NUL || (i == 0 && (
3575#ifdef FEAT_MBYTE
3576 has_mbyte ? this_class != 2 :
3577#endif
3578 !vim_iswordc(ptr[col]))))
3579 {
3580 /*
3581 * didn't find an identifier or string
3582 */
3583 if (find_type & FIND_STRING)
3584 EMSG(_("E348: No string under cursor"));
3585 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003586 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return 0;
3588 }
3589 ptr += col;
3590 *string = ptr;
3591
3592 /*
3593 * 3. Find the end if the identifier/string.
3594 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003595#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 bn = 0;
3597 startcol -= col;
3598#endif
3599 col = 0;
3600#ifdef FEAT_MBYTE
3601 if (has_mbyte)
3602 {
3603 /* Search for point of changing multibyte character class. */
3604 this_class = mb_get_class(ptr);
3605 while (ptr[col] != NUL
3606 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3607 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 || ((find_type & FIND_EVAL)
3610 && col <= (int)startcol
3611 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3612# endif
3613 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003614 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 else
3617#endif
3618 while ((i == 0 ? vim_iswordc(ptr[col])
3619 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003620# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 || ((find_type & FIND_EVAL)
3622 && col <= (int)startcol
3623 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3624# endif
3625 )
3626 {
3627 ++col;
3628 }
3629
3630 return col;
3631}
3632
3633/*
3634 * Prepare for redo of a normal command.
3635 */
3636 static void
3637prep_redo_cmd(cap)
3638 cmdarg_T *cap;
3639{
3640 prep_redo(cap->oap->regname, cap->count0,
3641 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3642}
3643
3644/*
3645 * Prepare for redo of any command.
3646 * Note that only the last argument can be a multi-byte char.
3647 */
3648 static void
3649prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3650 int regname;
3651 long num;
3652 int cmd1;
3653 int cmd2;
3654 int cmd3;
3655 int cmd4;
3656 int cmd5;
3657{
3658 ResetRedobuff();
3659 if (regname != 0) /* yank from specified buffer */
3660 {
3661 AppendCharToRedobuff('"');
3662 AppendCharToRedobuff(regname);
3663 }
3664 if (num)
3665 AppendNumberToRedobuff(num);
3666
3667 if (cmd1 != NUL)
3668 AppendCharToRedobuff(cmd1);
3669 if (cmd2 != NUL)
3670 AppendCharToRedobuff(cmd2);
3671 if (cmd3 != NUL)
3672 AppendCharToRedobuff(cmd3);
3673 if (cmd4 != NUL)
3674 AppendCharToRedobuff(cmd4);
3675 if (cmd5 != NUL)
3676 AppendCharToRedobuff(cmd5);
3677}
3678
3679/*
3680 * check for operator active and clear it
3681 *
3682 * return TRUE if operator was active
3683 */
3684 static int
3685checkclearop(oap)
3686 oparg_T *oap;
3687{
3688 if (oap->op_type == OP_NOP)
3689 return FALSE;
3690 clearopbeep(oap);
3691 return TRUE;
3692}
3693
3694/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003695 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003697 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
3699 static int
3700checkclearopq(oap)
3701 oparg_T *oap;
3702{
3703 if (oap->op_type == OP_NOP
3704#ifdef FEAT_VISUAL
3705 && !VIsual_active
3706#endif
3707 )
3708 return FALSE;
3709 clearopbeep(oap);
3710 return TRUE;
3711}
3712
3713 static void
3714clearop(oap)
3715 oparg_T *oap;
3716{
3717 oap->op_type = OP_NOP;
3718 oap->regname = 0;
3719 oap->motion_force = NUL;
3720 oap->use_reg_one = FALSE;
3721}
3722
3723 static void
3724clearopbeep(oap)
3725 oparg_T *oap;
3726{
3727 clearop(oap);
3728 beep_flush();
3729}
3730
3731#ifdef FEAT_VISUAL
3732/*
3733 * Remove the shift modifier from a special key.
3734 */
3735 static void
3736unshift_special(cap)
3737 cmdarg_T *cap;
3738{
3739 switch (cap->cmdchar)
3740 {
3741 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3742 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3743 case K_S_UP: cap->cmdchar = K_UP; break;
3744 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3745 case K_S_HOME: cap->cmdchar = K_HOME; break;
3746 case K_S_END: cap->cmdchar = K_END; break;
3747 }
3748 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3749}
3750#endif
3751
3752#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3753/*
3754 * Routines for displaying a partly typed command
3755 */
3756
3757#ifdef FEAT_VISUAL /* need room for size of Visual area */
3758# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3759#else
3760# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3761#endif
3762static char_u showcmd_buf[SHOWCMD_BUFLEN];
3763static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3764static int showcmd_is_clear = TRUE;
3765static int showcmd_visual = FALSE;
3766
3767static void display_showcmd __ARGS((void));
3768
3769 void
3770clear_showcmd()
3771{
3772 if (!p_sc)
3773 return;
3774
3775#ifdef FEAT_VISUAL
3776 if (VIsual_active && !char_avail())
3777 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003778 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 long lines;
3780 colnr_T leftcol, rightcol;
3781 linenr_T top, bot;
3782
3783 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003784 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
3786 top = VIsual.lnum;
3787 bot = curwin->w_cursor.lnum;
3788 }
3789 else
3790 {
3791 top = curwin->w_cursor.lnum;
3792 bot = VIsual.lnum;
3793 }
3794# ifdef FEAT_FOLDING
3795 /* Include closed folds as a whole. */
3796 hasFolding(top, &top, NULL);
3797 hasFolding(bot, NULL, &bot);
3798# endif
3799 lines = bot - top + 1;
3800
3801 if (VIsual_mode == Ctrl_V)
3802 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003803# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003804 char_u *saved_sbr = p_sbr;
3805
3806 /* Make 'sbr' empty for a moment to get the correct size. */
3807 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003810# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003811 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3814 (long)(rightcol - leftcol + 1));
3815 }
3816 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3817 sprintf((char *)showcmd_buf, "%ld", lines);
3818 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003819 {
3820 char_u *s, *e;
3821 int l;
3822 int bytes = 0;
3823 int chars = 0;
3824
3825 if (cursor_bot)
3826 {
3827 s = ml_get_pos(&VIsual);
3828 e = ml_get_cursor();
3829 }
3830 else
3831 {
3832 s = ml_get_cursor();
3833 e = ml_get_pos(&VIsual);
3834 }
3835 while ((*p_sel != 'e') ? s <= e : s < e)
3836 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003837# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003838 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003839# else
3840 l = (*s == NUL) ? 0 : 1;
3841# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003842 if (l == 0)
3843 {
3844 ++bytes;
3845 ++chars;
3846 break; /* end of line */
3847 }
3848 bytes += l;
3849 ++chars;
3850 s += l;
3851 }
3852 if (bytes == chars)
3853 sprintf((char *)showcmd_buf, "%d", chars);
3854 else
3855 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3858 showcmd_visual = TRUE;
3859 }
3860 else
3861#endif
3862 {
3863 showcmd_buf[0] = NUL;
3864 showcmd_visual = FALSE;
3865
3866 /* Don't actually display something if there is nothing to clear. */
3867 if (showcmd_is_clear)
3868 return;
3869 }
3870
3871 display_showcmd();
3872}
3873
3874/*
3875 * Add 'c' to string of shown command chars.
3876 * Return TRUE if output has been written (and setcursor() has been called).
3877 */
3878 int
3879add_to_showcmd(c)
3880 int c;
3881{
3882 char_u *p;
3883 int old_len;
3884 int extra_len;
3885 int overflow;
3886#if defined(FEAT_MOUSE)
3887 int i;
3888 static int ignore[] =
3889 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003890# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3892 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 K_IGNORE,
3895 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3896 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3897 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003898 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003900 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 0
3902 };
3903#endif
3904
Bram Moolenaar09df3122006-01-23 22:23:09 +00003905 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return FALSE;
3907
3908 if (showcmd_visual)
3909 {
3910 showcmd_buf[0] = NUL;
3911 showcmd_visual = FALSE;
3912 }
3913
3914#if defined(FEAT_MOUSE)
3915 /* Ignore keys that are scrollbar updates and mouse clicks */
3916 if (IS_SPECIAL(c))
3917 for (i = 0; ignore[i] != 0; ++i)
3918 if (ignore[i] == c)
3919 return FALSE;
3920#endif
3921
3922 p = transchar(c);
3923 old_len = (int)STRLEN(showcmd_buf);
3924 extra_len = (int)STRLEN(p);
3925 overflow = old_len + extra_len - SHOWCMD_COLS;
3926 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003927 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3928 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 STRCAT(showcmd_buf, p);
3930
3931 if (char_avail())
3932 return FALSE;
3933
3934 display_showcmd();
3935
3936 return TRUE;
3937}
3938
3939 void
3940add_to_showcmd_c(c)
3941 int c;
3942{
3943 if (!add_to_showcmd(c))
3944 setcursor();
3945}
3946
3947/*
3948 * Delete 'len' characters from the end of the shown command.
3949 */
3950 static void
3951del_from_showcmd(len)
3952 int len;
3953{
3954 int old_len;
3955
3956 if (!p_sc)
3957 return;
3958
3959 old_len = (int)STRLEN(showcmd_buf);
3960 if (len > old_len)
3961 len = old_len;
3962 showcmd_buf[old_len - len] = NUL;
3963
3964 if (!char_avail())
3965 display_showcmd();
3966}
3967
3968/*
3969 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3970 * something and there is a partial mapping.
3971 */
3972 void
3973push_showcmd()
3974{
3975 if (p_sc)
3976 STRCPY(old_showcmd_buf, showcmd_buf);
3977}
3978
3979 void
3980pop_showcmd()
3981{
3982 if (!p_sc)
3983 return;
3984
3985 STRCPY(showcmd_buf, old_showcmd_buf);
3986
3987 display_showcmd();
3988}
3989
3990 static void
3991display_showcmd()
3992{
3993 int len;
3994
3995 cursor_off();
3996
3997 len = (int)STRLEN(showcmd_buf);
3998 if (len == 0)
3999 showcmd_is_clear = TRUE;
4000 else
4001 {
4002 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4003 showcmd_is_clear = FALSE;
4004 }
4005
4006 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004007 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4008 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
4010 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4011
4012 setcursor(); /* put cursor back where it belongs */
4013}
4014#endif
4015
4016#ifdef FEAT_SCROLLBIND
4017/*
4018 * When "check" is FALSE, prepare for commands that scroll the window.
4019 * When "check" is TRUE, take care of scroll-binding after the window has
4020 * scrolled. Called from normal_cmd() and edit().
4021 */
4022 void
4023do_check_scrollbind(check)
4024 int check;
4025{
4026 static win_T *old_curwin = NULL;
4027 static linenr_T old_topline = 0;
4028#ifdef FEAT_DIFF
4029 static int old_topfill = 0;
4030#endif
4031 static buf_T *old_buf = NULL;
4032 static colnr_T old_leftcol = 0;
4033
4034 if (check && curwin->w_p_scb)
4035 {
4036 /* If a ":syncbind" command was just used, don't scroll, only reset
4037 * the values. */
4038 if (did_syncbind)
4039 did_syncbind = FALSE;
4040 else if (curwin == old_curwin)
4041 {
4042 /*
4043 * Synchronize other windows, as necessary according to
4044 * 'scrollbind'. Don't do this after an ":edit" command, except
4045 * when 'diff' is set.
4046 */
4047 if ((curwin->w_buffer == old_buf
4048#ifdef FEAT_DIFF
4049 || curwin->w_p_diff
4050#endif
4051 )
4052 && (curwin->w_topline != old_topline
4053#ifdef FEAT_DIFF
4054 || curwin->w_topfill != old_topfill
4055#endif
4056 || curwin->w_leftcol != old_leftcol))
4057 {
4058 check_scrollbind(curwin->w_topline - old_topline,
4059 (long)(curwin->w_leftcol - old_leftcol));
4060 }
4061 }
4062 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4063 {
4064 /*
4065 * When switching between windows, make sure that the relative
4066 * vertical offset is valid for the new window. The relative
4067 * offset is invalid whenever another 'scrollbind' window has
4068 * scrolled to a point that would force the current window to
4069 * scroll past the beginning or end of its buffer. When the
4070 * resync is performed, some of the other 'scrollbind' windows may
4071 * need to jump so that the current window's relative position is
4072 * visible on-screen.
4073 */
4074 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4075 }
4076 curwin->w_scbind_pos = curwin->w_topline;
4077 }
4078
4079 old_curwin = curwin;
4080 old_topline = curwin->w_topline;
4081#ifdef FEAT_DIFF
4082 old_topfill = curwin->w_topfill;
4083#endif
4084 old_buf = curwin->w_buffer;
4085 old_leftcol = curwin->w_leftcol;
4086}
4087
4088/*
4089 * Synchronize any windows that have "scrollbind" set, based on the
4090 * number of rows by which the current window has changed
4091 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4092 */
4093 void
4094check_scrollbind(topline_diff, leftcol_diff)
4095 linenr_T topline_diff;
4096 long leftcol_diff;
4097{
4098 int want_ver;
4099 int want_hor;
4100 win_T *old_curwin = curwin;
4101 buf_T *old_curbuf = curbuf;
4102#ifdef FEAT_VISUAL
4103 int old_VIsual_select = VIsual_select;
4104 int old_VIsual_active = VIsual_active;
4105#endif
4106 colnr_T tgt_leftcol = curwin->w_leftcol;
4107 long topline;
4108 long y;
4109
4110 /*
4111 * check 'scrollopt' string for vertical and horizontal scroll options
4112 */
4113 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4114#ifdef FEAT_DIFF
4115 want_ver |= old_curwin->w_p_diff;
4116#endif
4117 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4118
4119 /*
4120 * loop through the scrollbound windows and scroll accordingly
4121 */
4122#ifdef FEAT_VISUAL
4123 VIsual_select = VIsual_active = 0;
4124#endif
4125 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4126 {
4127 curbuf = curwin->w_buffer;
4128 /* skip original window and windows with 'noscrollbind' */
4129 if (curwin != old_curwin && curwin->w_p_scb)
4130 {
4131 /*
4132 * do the vertical scroll
4133 */
4134 if (want_ver)
4135 {
4136#ifdef FEAT_DIFF
4137 if (old_curwin->w_p_diff && curwin->w_p_diff)
4138 {
4139 diff_set_topline(old_curwin, curwin);
4140 }
4141 else
4142#endif
4143 {
4144 curwin->w_scbind_pos += topline_diff;
4145 topline = curwin->w_scbind_pos;
4146 if (topline > curbuf->b_ml.ml_line_count)
4147 topline = curbuf->b_ml.ml_line_count;
4148 if (topline < 1)
4149 topline = 1;
4150
4151 y = topline - curwin->w_topline;
4152 if (y > 0)
4153 scrollup(y, FALSE);
4154 else
4155 scrolldown(-y, FALSE);
4156 }
4157
4158 redraw_later(VALID);
4159 cursor_correct();
4160#ifdef FEAT_WINDOWS
4161 curwin->w_redr_status = TRUE;
4162#endif
4163 }
4164
4165 /*
4166 * do the horizontal scroll
4167 */
4168 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4169 {
4170 curwin->w_leftcol = tgt_leftcol;
4171 leftcol_changed();
4172 }
4173 }
4174 }
4175
4176 /*
4177 * reset current-window
4178 */
4179#ifdef FEAT_VISUAL
4180 VIsual_select = old_VIsual_select;
4181 VIsual_active = old_VIsual_active;
4182#endif
4183 curwin = old_curwin;
4184 curbuf = old_curbuf;
4185}
4186#endif /* #ifdef FEAT_SCROLLBIND */
4187
4188/*
4189 * Command character that's ignored.
4190 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004191 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 static void
4194nv_ignore(cap)
4195 cmdarg_T *cap;
4196{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004197 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198}
4199
4200/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004201 * Command character that doesn't do anything, but unlike nv_ignore() does
4202 * start edit(). Used for "startinsert" executed while starting up.
4203 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004204 static void
4205nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004206 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004207{
4208}
4209
4210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 * Command character doesn't exist.
4212 */
4213 static void
4214nv_error(cap)
4215 cmdarg_T *cap;
4216{
4217 clearopbeep(cap->oap);
4218}
4219
4220/*
4221 * <Help> and <F1> commands.
4222 */
4223 static void
4224nv_help(cap)
4225 cmdarg_T *cap;
4226{
4227 if (!checkclearopq(cap->oap))
4228 ex_help(NULL);
4229}
4230
4231/*
4232 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4233 */
4234 static void
4235nv_addsub(cap)
4236 cmdarg_T *cap;
4237{
4238 if (!checkclearopq(cap->oap)
4239 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4240 prep_redo_cmd(cap);
4241}
4242
4243/*
4244 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4245 */
4246 static void
4247nv_page(cap)
4248 cmdarg_T *cap;
4249{
4250 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004251 {
4252#ifdef FEAT_WINDOWS
4253 if (mod_mask & MOD_MASK_CTRL)
4254 {
4255 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4256 if (cap->arg == BACKWARD)
4257 goto_tabpage(-(int)cap->count1);
4258 else
4259 goto_tabpage((int)cap->count0);
4260 }
4261 else
4262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265}
4266
4267/*
4268 * Implementation of "gd" and "gD" command.
4269 */
4270 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004272 oparg_T *oap;
4273 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004274 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275{
4276 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 char_u *ptr;
4278
Bram Moolenaard9d30582005-05-18 22:10:28 +00004279 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004282#ifdef FEAT_FOLDING
4283 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4284 foldOpenCursor();
4285#endif
4286}
4287
4288/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004289 * Search for variable declaration of "ptr[len]".
4290 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4291 * current file ("gD").
4292 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 * Return FAIL when not found.
4294 */
4295 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004296find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004297 char_u *ptr;
4298 int len;
4299 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004300 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 int searchflags; /* flags passed to searchit() */
4302{
4303 char_u *pat;
4304 pos_T old_pos;
4305 pos_T par_pos;
4306 pos_T found_pos;
4307 int t;
4308 int save_p_ws;
4309 int save_p_scs;
4310 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004311 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312
4313 if ((pat = alloc(len + 7)) == NULL)
4314 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004315
4316 /* Put "\V" before the pattern to avoid that the special meaning of "."
4317 * and "~" causes trouble. */
4318 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4319 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 old_pos = curwin->w_cursor;
4321 save_p_ws = p_ws;
4322 save_p_scs = p_scs;
4323 p_ws = FALSE; /* don't wrap around end of file now */
4324 p_scs = FALSE; /* don't switch ignorecase off now */
4325
4326 /*
4327 * With "gD" go to line 1.
4328 * With "gd" Search back for the start of the current function, then go
4329 * back until a blank line. If this fails go to line 1.
4330 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004331 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 setpcmark(); /* Set in findpar() otherwise */
4334 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004335 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 else
4338 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004339 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4341 --curwin->w_cursor.lnum;
4342 }
4343 curwin->w_cursor.col = 0;
4344
4345 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004346 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 for (;;)
4348 {
4349 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004350 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351 if (curwin->w_cursor.lnum >= old_pos.lnum)
4352 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004353
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004354 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004355 {
4356 pos_T *pos;
4357
4358 /* Check that the block the match is in doesn't end before the
4359 * position where we started the search from. */
4360 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4361 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4362 && pos->lnum < old_pos.lnum)
4363 continue;
4364 }
4365
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004366 if (t == FAIL)
4367 {
4368 /* If we previously found a valid position, use it. */
4369 if (found_pos.lnum != 0)
4370 {
4371 curwin->w_cursor = found_pos;
4372 t = OK;
4373 }
4374 break;
4375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4378 {
4379 /* Ignore this line, continue at start of next line. */
4380 ++curwin->w_cursor.lnum;
4381 curwin->w_cursor.col = 0;
4382 continue;
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004385 if (!locally) /* global search: use first match found */
4386 break;
4387 if (curwin->w_cursor.lnum >= par_pos.lnum)
4388 {
4389 /* If we previously found a valid position, use it. */
4390 if (found_pos.lnum != 0)
4391 curwin->w_cursor = found_pos;
4392 break;
4393 }
4394
4395 /* For finding a local variable and the match is before the "{" search
4396 * to find a later match. For K&R style function declarations this
4397 * skips the function header without types. */
4398 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400
4401 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004403 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 curwin->w_cursor = old_pos;
4405 }
4406 else
4407 {
4408 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 /* "n" searches forward now */
4410 reset_search_dir();
4411 }
4412
4413 vim_free(pat);
4414 p_ws = save_p_ws;
4415 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416
4417 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418}
4419
4420/*
4421 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4422 * lines rather than lines in the file.
4423 * 'dist' must be positive.
4424 *
4425 * Return OK if able to move cursor, FAIL otherwise.
4426 */
4427 static int
4428nv_screengo(oap, dir, dist)
4429 oparg_T *oap;
4430 int dir;
4431 long dist;
4432{
4433 int linelen = linetabsize(ml_get_curline());
4434 int retval = OK;
4435 int atend = FALSE;
4436 int n;
4437 int col_off1; /* margin offset for first screen line */
4438 int col_off2; /* margin offset for wrapped screen line */
4439 int width1; /* text width for first screen line */
4440 int width2; /* test width for wrapped screen line */
4441
4442 oap->motion_type = MCHAR;
4443 oap->inclusive = FALSE;
4444
4445 col_off1 = curwin_col_off();
4446 col_off2 = col_off1 - curwin_col_off2();
4447 width1 = W_WIDTH(curwin) - col_off1;
4448 width2 = W_WIDTH(curwin) - col_off2;
4449
4450#ifdef FEAT_VERTSPLIT
4451 if (curwin->w_width != 0)
4452 {
4453#endif
4454 /*
4455 * Instead of sticking at the last character of the buffer line we
4456 * try to stick in the last column of the screen.
4457 */
4458 if (curwin->w_curswant == MAXCOL)
4459 {
4460 atend = TRUE;
4461 validate_virtcol();
4462 if (width1 <= 0)
4463 curwin->w_curswant = 0;
4464 else
4465 {
4466 curwin->w_curswant = width1 - 1;
4467 if (curwin->w_virtcol > curwin->w_curswant)
4468 curwin->w_curswant += ((curwin->w_virtcol
4469 - curwin->w_curswant - 1) / width2 + 1) * width2;
4470 }
4471 }
4472 else
4473 {
4474 if (linelen > width1)
4475 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4476 else
4477 n = width1;
4478 if (curwin->w_curswant > (colnr_T)n + 1)
4479 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4480 * width2;
4481 }
4482
4483 while (dist--)
4484 {
4485 if (dir == BACKWARD)
4486 {
4487 if ((long)curwin->w_curswant >= width2)
4488 /* move back within line */
4489 curwin->w_curswant -= width2;
4490 else
4491 {
4492 /* to previous line */
4493 if (curwin->w_cursor.lnum == 1)
4494 {
4495 retval = FAIL;
4496 break;
4497 }
4498 --curwin->w_cursor.lnum;
4499#ifdef FEAT_FOLDING
4500 /* Move to the start of a closed fold. Don't do that when
4501 * 'foldopen' contains "all": it will open in a moment. */
4502 if (!(fdo_flags & FDO_ALL))
4503 (void)hasFolding(curwin->w_cursor.lnum,
4504 &curwin->w_cursor.lnum, NULL);
4505#endif
4506 linelen = linetabsize(ml_get_curline());
4507 if (linelen > width1)
4508 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4509 + 1) * width2;
4510 }
4511 }
4512 else /* dir == FORWARD */
4513 {
4514 if (linelen > width1)
4515 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4516 else
4517 n = width1;
4518 if (curwin->w_curswant + width2 < (colnr_T)n)
4519 /* move forward within line */
4520 curwin->w_curswant += width2;
4521 else
4522 {
4523 /* to next line */
4524#ifdef FEAT_FOLDING
4525 /* Move to the end of a closed fold. */
4526 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4527 &curwin->w_cursor.lnum);
4528#endif
4529 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4530 {
4531 retval = FAIL;
4532 break;
4533 }
4534 curwin->w_cursor.lnum++;
4535 curwin->w_curswant %= width2;
4536 }
4537 }
4538 }
4539#ifdef FEAT_VERTSPLIT
4540 }
4541#endif
4542
4543 coladvance(curwin->w_curswant);
4544
4545#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4546 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4547 {
4548 /*
4549 * Check for landing on a character that got split at the end of the
4550 * last line. We want to advance a screenline, not end up in the same
4551 * screenline or move two screenlines.
4552 */
4553 validate_virtcol();
4554 if (curwin->w_virtcol > curwin->w_curswant
4555 && (curwin->w_curswant < (colnr_T)width1
4556 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4557 : ((curwin->w_curswant - width1) % width2
4558 > (colnr_T)width2 / 2)))
4559 --curwin->w_cursor.col;
4560 }
4561#endif
4562
4563 if (atend)
4564 curwin->w_curswant = MAXCOL; /* stick in the last column */
4565
4566 return retval;
4567}
4568
4569#ifdef FEAT_MOUSE
4570/*
4571 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4572 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004573 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4574 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 */
4576 static void
4577nv_mousescroll(cap)
4578 cmdarg_T *cap;
4579{
4580# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004581 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 /* Currently we only get the mouse coordinates in the GUI. */
4584 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4585 {
4586 int row, col;
4587
4588 row = mouse_row;
4589 col = mouse_col;
4590
4591 /* find the window at the pointer coordinates */
4592 curwin = mouse_find_win(&row, &col);
4593 curbuf = curwin->w_buffer;
4594 }
4595# endif
4596
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004597 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004599 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4600 {
4601 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4602 }
4603 else
4604 {
4605 cap->count1 = 3;
4606 cap->count0 = 3;
4607 nv_scroll_line(cap);
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004610# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 else
4612 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004613 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4614 if (!curwin->w_p_wrap)
4615 {
4616 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004617
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004618 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4619 step = W_WIDTH(curwin);
4620 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4621 if (val < 0)
4622 val = 0;
4623
4624 gui_do_horiz_scroll(val, TRUE);
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4630 curwin->w_redr_status = TRUE;
4631
4632 curwin = old_curwin;
4633 curbuf = curwin->w_buffer;
4634# endif
4635}
4636
4637/*
4638 * Mouse clicks and drags.
4639 */
4640 static void
4641nv_mouse(cap)
4642 cmdarg_T *cap;
4643{
4644 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4645}
4646#endif
4647
4648/*
4649 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4650 * cap->arg must be TRUE for CTRL-E.
4651 */
4652 static void
4653nv_scroll_line(cap)
4654 cmdarg_T *cap;
4655{
4656 if (!checkclearop(cap->oap))
4657 scroll_redraw(cap->arg, cap->count1);
4658}
4659
4660/*
4661 * Scroll "count" lines up or down, and redraw.
4662 */
4663 void
4664scroll_redraw(up, count)
4665 int up;
4666 long count;
4667{
4668 linenr_T prev_topline = curwin->w_topline;
4669#ifdef FEAT_DIFF
4670 int prev_topfill = curwin->w_topfill;
4671#endif
4672 linenr_T prev_lnum = curwin->w_cursor.lnum;
4673
4674 if (up)
4675 scrollup(count, TRUE);
4676 else
4677 scrolldown(count, TRUE);
4678 if (p_so)
4679 {
4680 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4681 * valid, otherwise the screen jumps back at the end of the file. */
4682 cursor_correct();
4683 check_cursor_moved(curwin);
4684 curwin->w_valid |= VALID_TOPLINE;
4685
4686 /* If moved back to where we were, at least move the cursor, otherwise
4687 * we get stuck at one position. Don't move the cursor up if the
4688 * first line of the buffer is already on the screen */
4689 while (curwin->w_topline == prev_topline
4690#ifdef FEAT_DIFF
4691 && curwin->w_topfill == prev_topfill
4692#endif
4693 )
4694 {
4695 if (up)
4696 {
4697 if (curwin->w_cursor.lnum > prev_lnum
4698 || cursor_down(1L, FALSE) == FAIL)
4699 break;
4700 }
4701 else
4702 {
4703 if (curwin->w_cursor.lnum < prev_lnum
4704 || prev_topline == 1L
4705 || cursor_up(1L, FALSE) == FAIL)
4706 break;
4707 }
4708 /* Mark w_topline as valid, otherwise the screen jumps back at the
4709 * end of the file. */
4710 check_cursor_moved(curwin);
4711 curwin->w_valid |= VALID_TOPLINE;
4712 }
4713 }
4714 if (curwin->w_cursor.lnum != prev_lnum)
4715 coladvance(curwin->w_curswant);
4716 redraw_later(VALID);
4717}
4718
4719/*
4720 * Commands that start with "z".
4721 */
4722 static void
4723nv_zet(cap)
4724 cmdarg_T *cap;
4725{
4726 long n;
4727 colnr_T col;
4728 int nchar = cap->nchar;
4729#ifdef FEAT_FOLDING
4730 long old_fdl = curwin->w_p_fdl;
4731 int old_fen = curwin->w_p_fen;
4732#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004733#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004734 int undo = FALSE;
4735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 if (VIM_ISDIGIT(nchar))
4738 {
4739 /*
4740 * "z123{nchar}": edit the count before obtaining {nchar}
4741 */
4742 if (checkclearop(cap->oap))
4743 return;
4744 n = nchar - '0';
4745 for (;;)
4746 {
4747#ifdef USE_ON_FLY_SCROLL
4748 dont_scroll = TRUE; /* disallow scrolling here */
4749#endif
4750 ++no_mapping;
4751 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004752 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 --no_mapping;
4755 --allow_keys;
4756#ifdef FEAT_CMDL_INFO
4757 (void)add_to_showcmd(nchar);
4758#endif
4759 if (nchar == K_DEL || nchar == K_KDEL)
4760 n /= 10;
4761 else if (VIM_ISDIGIT(nchar))
4762 n = n * 10 + (nchar - '0');
4763 else if (nchar == CAR)
4764 {
4765#ifdef FEAT_GUI
4766 need_mouse_correct = TRUE;
4767#endif
4768 win_setheight((int)n);
4769 break;
4770 }
4771 else if (nchar == 'l'
4772 || nchar == 'h'
4773 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004774 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 {
4776 cap->count1 = n ? n * cap->count1 : cap->count1;
4777 goto dozet;
4778 }
4779 else
4780 {
4781 clearopbeep(cap->oap);
4782 break;
4783 }
4784 }
4785 cap->oap->op_type = OP_NOP;
4786 return;
4787 }
4788
4789dozet:
4790 if (
4791#ifdef FEAT_FOLDING
4792 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4793 * and "zC" only in Visual mode. "zj" and "zk" are motion
4794 * commands. */
4795 cap->nchar != 'f' && cap->nchar != 'F'
4796 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4797 && cap->nchar != 'j' && cap->nchar != 'k'
4798 &&
4799#endif
4800 checkclearop(cap->oap))
4801 return;
4802
4803 /*
4804 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4805 * If line number given, set cursor.
4806 */
4807 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4808 && cap->count0
4809 && cap->count0 != curwin->w_cursor.lnum)
4810 {
4811 setpcmark();
4812 if (cap->count0 > curbuf->b_ml.ml_line_count)
4813 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4814 else
4815 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004816 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818
4819 switch (nchar)
4820 {
4821 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4822 case '+':
4823 if (cap->count0 == 0)
4824 {
4825 /* No count given: put cursor at the line below screen */
4826 validate_botline(); /* make sure w_botline is valid */
4827 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4828 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4829 else
4830 curwin->w_cursor.lnum = curwin->w_botline;
4831 }
4832 /* FALLTHROUGH */
4833 case NL:
4834 case CAR:
4835 case K_KENTER:
4836 beginline(BL_WHITE | BL_FIX);
4837 /* FALLTHROUGH */
4838
4839 case 't': scroll_cursor_top(0, TRUE);
4840 redraw_later(VALID);
4841 break;
4842
4843 /* "z." and "zz": put cursor in middle of screen */
4844 case '.': beginline(BL_WHITE | BL_FIX);
4845 /* FALLTHROUGH */
4846
4847 case 'z': scroll_cursor_halfway(TRUE);
4848 redraw_later(VALID);
4849 break;
4850
4851 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4852 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4853 * when <count> is at bottom of window, and puts that one at
4854 * bottom of window. */
4855 if (cap->count0 != 0)
4856 {
4857 scroll_cursor_bot(0, TRUE);
4858 curwin->w_cursor.lnum = curwin->w_topline;
4859 }
4860 else if (curwin->w_topline == 1)
4861 curwin->w_cursor.lnum = 1;
4862 else
4863 curwin->w_cursor.lnum = curwin->w_topline - 1;
4864 /* FALLTHROUGH */
4865 case '-':
4866 beginline(BL_WHITE | BL_FIX);
4867 /* FALLTHROUGH */
4868
4869 case 'b': scroll_cursor_bot(0, TRUE);
4870 redraw_later(VALID);
4871 break;
4872
4873 /* "zH" - scroll screen right half-page */
4874 case 'H':
4875 cap->count1 *= W_WIDTH(curwin) / 2;
4876 /* FALLTHROUGH */
4877
4878 /* "zh" - scroll screen to the right */
4879 case 'h':
4880 case K_LEFT:
4881 if (!curwin->w_p_wrap)
4882 {
4883 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4884 curwin->w_leftcol = 0;
4885 else
4886 curwin->w_leftcol -= (colnr_T)cap->count1;
4887 leftcol_changed();
4888 }
4889 break;
4890
4891 /* "zL" - scroll screen left half-page */
4892 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4893 /* FALLTHROUGH */
4894
4895 /* "zl" - scroll screen to the left */
4896 case 'l':
4897 case K_RIGHT:
4898 if (!curwin->w_p_wrap)
4899 {
4900 /* scroll the window left */
4901 curwin->w_leftcol += (colnr_T)cap->count1;
4902 leftcol_changed();
4903 }
4904 break;
4905
4906 /* "zs" - scroll screen, cursor at the start */
4907 case 's': if (!curwin->w_p_wrap)
4908 {
4909#ifdef FEAT_FOLDING
4910 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4911 col = 0; /* like the cursor is in col 0 */
4912 else
4913#endif
4914 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4915 if ((long)col > p_siso)
4916 col -= p_siso;
4917 else
4918 col = 0;
4919 if (curwin->w_leftcol != col)
4920 {
4921 curwin->w_leftcol = col;
4922 redraw_later(NOT_VALID);
4923 }
4924 }
4925 break;
4926
4927 /* "ze" - scroll screen, cursor at the end */
4928 case 'e': if (!curwin->w_p_wrap)
4929 {
4930#ifdef FEAT_FOLDING
4931 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4932 col = 0; /* like the cursor is in col 0 */
4933 else
4934#endif
4935 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4936 n = W_WIDTH(curwin) - curwin_col_off();
4937 if ((long)col + p_siso < n)
4938 col = 0;
4939 else
4940 col = col + p_siso - n + 1;
4941 if (curwin->w_leftcol != col)
4942 {
4943 curwin->w_leftcol = col;
4944 redraw_later(NOT_VALID);
4945 }
4946 }
4947 break;
4948
4949#ifdef FEAT_FOLDING
4950 /* "zF": create fold command */
4951 /* "zf": create fold operator */
4952 case 'F':
4953 case 'f': if (foldManualAllowed(TRUE))
4954 {
4955 cap->nchar = 'f';
4956 nv_operator(cap);
4957 curwin->w_p_fen = TRUE;
4958
4959 /* "zF" is like "zfzf" */
4960 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4961 {
4962 nv_operator(cap);
4963 finish_op = TRUE;
4964 }
4965 }
4966 else
4967 clearopbeep(cap->oap);
4968 break;
4969
4970 /* "zd": delete fold at cursor */
4971 /* "zD": delete fold at cursor recursively */
4972 case 'd':
4973 case 'D': if (foldManualAllowed(FALSE))
4974 {
4975 if (VIsual_active)
4976 nv_operator(cap);
4977 else
4978 deleteFold(curwin->w_cursor.lnum,
4979 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4980 }
4981 break;
4982
4983 /* "zE": erease all folds */
4984 case 'E': if (foldmethodIsManual(curwin))
4985 {
4986 clearFolding(curwin);
4987 changed_window_setting();
4988 }
4989 else if (foldmethodIsMarker(curwin))
4990 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4991 TRUE, FALSE);
4992 else
4993 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4994 break;
4995
4996 /* "zn": fold none: reset 'foldenable' */
4997 case 'n': curwin->w_p_fen = FALSE;
4998 break;
4999
5000 /* "zN": fold Normal: set 'foldenable' */
5001 case 'N': curwin->w_p_fen = TRUE;
5002 break;
5003
5004 /* "zi": invert folding: toggle 'foldenable' */
5005 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5006 break;
5007
5008 /* "za": open closed fold or close open fold at cursor */
5009 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5010 openFold(curwin->w_cursor.lnum, cap->count1);
5011 else
5012 {
5013 closeFold(curwin->w_cursor.lnum, cap->count1);
5014 curwin->w_p_fen = TRUE;
5015 }
5016 break;
5017
5018 /* "zA": open fold at cursor recursively */
5019 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5020 openFoldRecurse(curwin->w_cursor.lnum);
5021 else
5022 {
5023 closeFoldRecurse(curwin->w_cursor.lnum);
5024 curwin->w_p_fen = TRUE;
5025 }
5026 break;
5027
5028 /* "zo": open fold at cursor or Visual area */
5029 case 'o': if (VIsual_active)
5030 nv_operator(cap);
5031 else
5032 openFold(curwin->w_cursor.lnum, cap->count1);
5033 break;
5034
5035 /* "zO": open fold recursively */
5036 case 'O': if (VIsual_active)
5037 nv_operator(cap);
5038 else
5039 openFoldRecurse(curwin->w_cursor.lnum);
5040 break;
5041
5042 /* "zc": close fold at cursor or Visual area */
5043 case 'c': if (VIsual_active)
5044 nv_operator(cap);
5045 else
5046 closeFold(curwin->w_cursor.lnum, cap->count1);
5047 curwin->w_p_fen = TRUE;
5048 break;
5049
5050 /* "zC": close fold recursively */
5051 case 'C': if (VIsual_active)
5052 nv_operator(cap);
5053 else
5054 closeFoldRecurse(curwin->w_cursor.lnum);
5055 curwin->w_p_fen = TRUE;
5056 break;
5057
5058 /* "zv": open folds at the cursor */
5059 case 'v': foldOpenCursor();
5060 break;
5061
5062 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5063 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005064 curwin->w_foldinvalid = TRUE; /* recompute folds */
5065 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 foldOpenCursor();
5067 break;
5068
5069 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5070 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005071 curwin->w_foldinvalid = TRUE; /* recompute folds */
5072 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 break;
5074
5075 /* "zm": fold more */
5076 case 'm': if (curwin->w_p_fdl > 0)
5077 --curwin->w_p_fdl;
5078 old_fdl = -1; /* force an update */
5079 curwin->w_p_fen = TRUE;
5080 break;
5081
5082 /* "zM": close all folds */
5083 case 'M': curwin->w_p_fdl = 0;
5084 old_fdl = -1; /* force an update */
5085 curwin->w_p_fen = TRUE;
5086 break;
5087
5088 /* "zr": reduce folding */
5089 case 'r': ++curwin->w_p_fdl;
5090 break;
5091
5092 /* "zR": open all folds */
5093 case 'R': curwin->w_p_fdl = getDeepestNesting();
5094 old_fdl = -1; /* force an update */
5095 break;
5096
5097 case 'j': /* "zj" move to next fold downwards */
5098 case 'k': /* "zk" move to next fold upwards */
5099 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5100 cap->count1) == FAIL)
5101 clearopbeep(cap->oap);
5102 break;
5103
5104#endif /* FEAT_FOLDING */
5105
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005106#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005107 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5108 ++no_mapping;
5109 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005110 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005111 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005112 --no_mapping;
5113 --allow_keys;
5114#ifdef FEAT_CMDL_INFO
5115 (void)add_to_showcmd(nchar);
5116#endif
5117 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5118 {
5119 clearopbeep(cap->oap);
5120 break;
5121 }
5122 undo = TRUE;
5123 /*FALLTHROUGH*/
5124
Bram Moolenaarb765d632005-06-07 21:00:02 +00005125 case 'g': /* "zg": add good word to word list */
5126 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005127 case 'G': /* "zG": add good word to temp word list */
5128 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005129 {
5130 char_u *ptr = NULL;
5131 int len;
5132
5133 if (checkclearop(cap->oap))
5134 break;
5135# ifdef FEAT_VISUAL
5136 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5137 == FAIL)
5138 return;
5139# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005140 if (ptr == NULL)
5141 {
5142 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005143
5144 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005145 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005146 if (len != 0 && curwin->w_cursor.col <= pos.col)
5147 ptr = ml_get_pos(&curwin->w_cursor);
5148 curwin->w_cursor = pos;
5149 }
5150
Bram Moolenaarb765d632005-06-07 21:00:02 +00005151 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5152 FIND_IDENT)) == 0)
5153 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005154 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005155 (nchar == 'G' || nchar == 'W')
5156 ? 0 : (int)cap->count1,
5157 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005158 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005159 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005160
Bram Moolenaar43abc522005-12-10 20:15:02 +00005161 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005162 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005163 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005164 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005165#endif
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 default: clearopbeep(cap->oap);
5168 }
5169
5170#ifdef FEAT_FOLDING
5171 /* Redraw when 'foldenable' changed */
5172 if (old_fen != curwin->w_p_fen)
5173 {
5174# ifdef FEAT_DIFF
5175 win_T *wp;
5176
5177 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5178 {
5179 /* Adjust 'foldenable' in diff-synced windows. */
5180 FOR_ALL_WINDOWS(wp)
5181 {
5182 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5183 {
5184 wp->w_p_fen = curwin->w_p_fen;
5185 changed_window_setting_win(wp);
5186 }
5187 }
5188 }
5189# endif
5190 changed_window_setting();
5191 }
5192
5193 /* Redraw when 'foldlevel' changed. */
5194 if (old_fdl != curwin->w_p_fdl)
5195 newFoldLevel();
5196#endif
5197}
5198
5199#ifdef FEAT_GUI
5200/*
5201 * Vertical scrollbar movement.
5202 */
5203 static void
5204nv_ver_scrollbar(cap)
5205 cmdarg_T *cap;
5206{
5207 if (cap->oap->op_type != OP_NOP)
5208 clearopbeep(cap->oap);
5209
5210 /* Even if an operator was pending, we still want to scroll */
5211 gui_do_scroll();
5212}
5213
5214/*
5215 * Horizontal scrollbar movement.
5216 */
5217 static void
5218nv_hor_scrollbar(cap)
5219 cmdarg_T *cap;
5220{
5221 if (cap->oap->op_type != OP_NOP)
5222 clearopbeep(cap->oap);
5223
5224 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005225 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226}
5227#endif
5228
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005229#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005230/*
5231 * Click in GUI tab.
5232 */
5233 static void
5234nv_tabline(cap)
5235 cmdarg_T *cap;
5236{
5237 if (cap->oap->op_type != OP_NOP)
5238 clearopbeep(cap->oap);
5239
5240 /* Even if an operator was pending, we still want to jump tabs. */
5241 goto_tabpage(current_tab);
5242}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005243
5244/*
5245 * Selected item in tab line menu.
5246 */
5247 static void
5248nv_tabmenu(cap)
5249 cmdarg_T *cap;
5250{
5251 if (cap->oap->op_type != OP_NOP)
5252 clearopbeep(cap->oap);
5253
5254 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005255 handle_tabmenu();
5256}
5257
5258/*
5259 * Handle selecting an item of the GUI tab line menu.
5260 * Used in Normal and Insert mode.
5261 */
5262 void
5263handle_tabmenu()
5264{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005265 switch (current_tabmenu)
5266 {
5267 case TABLINE_MENU_CLOSE:
5268 if (current_tab == 0)
5269 do_cmdline_cmd((char_u *)"tabclose");
5270 else
5271 {
5272 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5273 current_tab);
5274 do_cmdline_cmd(IObuff);
5275 }
5276 break;
5277
5278 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005279 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5280 current_tab > 0 ? current_tab - 1 : 999);
5281 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005282 break;
5283
5284 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005285 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5286 current_tab > 0 ? current_tab - 1 : 999);
5287 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005288 break;
5289 }
5290}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291#endif
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293/*
5294 * "Q" command.
5295 */
5296 static void
5297nv_exmode(cap)
5298 cmdarg_T *cap;
5299{
5300 /*
5301 * Ignore 'Q' in Visual mode, just give a beep.
5302 */
5303#ifdef FEAT_VISUAL
5304 if (VIsual_active)
5305 vim_beep();
5306 else
5307#endif
5308 if (!checkclearop(cap->oap))
5309 do_exmode(FALSE);
5310}
5311
5312/*
5313 * Handle a ":" command.
5314 */
5315 static void
5316nv_colon(cap)
5317 cmdarg_T *cap;
5318{
5319 int old_p_im;
5320
5321#ifdef FEAT_VISUAL
5322 if (VIsual_active)
5323 nv_operator(cap);
5324 else
5325#endif
5326 {
5327 if (cap->oap->op_type != OP_NOP)
5328 {
5329 /* Using ":" as a movement is characterwise exclusive. */
5330 cap->oap->motion_type = MCHAR;
5331 cap->oap->inclusive = FALSE;
5332 }
5333 else if (cap->count0)
5334 {
5335 /* translate "count:" into ":.,.+(count - 1)" */
5336 stuffcharReadbuff('.');
5337 if (cap->count0 > 1)
5338 {
5339 stuffReadbuff((char_u *)",.+");
5340 stuffnumReadbuff((long)cap->count0 - 1L);
5341 }
5342 }
5343
5344 /* When typing, don't type below an old message */
5345 if (KeyTyped)
5346 compute_cmdrow();
5347
5348 old_p_im = p_im;
5349
5350 /* get a command line and execute it */
5351 do_cmdline(NULL, getexline, NULL,
5352 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5353
5354 /* If 'insertmode' changed, enter or exit Insert mode */
5355 if (p_im != old_p_im)
5356 {
5357 if (p_im)
5358 restart_edit = 'i';
5359 else
5360 restart_edit = 0;
5361 }
5362
5363 /* The start of the operator may have become invalid by the Ex
5364 * command. */
5365 if (cap->oap->op_type != OP_NOP
5366 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5367 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005368 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 clearopbeep(cap->oap);
5370 }
5371}
5372
5373/*
5374 * Handle CTRL-G command.
5375 */
5376 static void
5377nv_ctrlg(cap)
5378 cmdarg_T *cap;
5379{
5380#ifdef FEAT_VISUAL
5381 if (VIsual_active) /* toggle Selection/Visual mode */
5382 {
5383 VIsual_select = !VIsual_select;
5384 showmode();
5385 }
5386 else
5387#endif
5388 if (!checkclearop(cap->oap))
5389 /* print full name if count given or :cd used */
5390 fileinfo((int)cap->count0, FALSE, TRUE);
5391}
5392
5393/*
5394 * Handle CTRL-H <Backspace> command.
5395 */
5396 static void
5397nv_ctrlh(cap)
5398 cmdarg_T *cap;
5399{
5400#ifdef FEAT_VISUAL
5401 if (VIsual_active && VIsual_select)
5402 {
5403 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5404 v_visop(cap);
5405 }
5406 else
5407#endif
5408 nv_left(cap);
5409}
5410
5411/*
5412 * CTRL-L: clear screen and redraw.
5413 */
5414 static void
5415nv_clear(cap)
5416 cmdarg_T *cap;
5417{
5418 if (!checkclearop(cap->oap))
5419 {
5420#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5421 /*
5422 * Right now, the BeBox doesn't seem to have an easy way to detect
5423 * window resizing, so we cheat and make the user detect it
5424 * manually with CTRL-L instead
5425 */
5426 ui_get_shellsize();
5427#endif
5428#ifdef FEAT_SYN_HL
5429 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005430 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#endif
5432 redraw_later(CLEAR);
5433 }
5434}
5435
5436/*
5437 * CTRL-O: In Select mode: switch to Visual mode for one command.
5438 * Otherwise: Go to older pcmark.
5439 */
5440 static void
5441nv_ctrlo(cap)
5442 cmdarg_T *cap;
5443{
5444#ifdef FEAT_VISUAL
5445 if (VIsual_active && VIsual_select)
5446 {
5447 VIsual_select = FALSE;
5448 showmode();
5449 restart_VIsual_select = 2; /* restart Select mode later */
5450 }
5451 else
5452#endif
5453 {
5454 cap->count1 = -cap->count1;
5455 nv_pcmark(cap);
5456 }
5457}
5458
5459/*
5460 * CTRL-^ command, short for ":e #"
5461 */
5462 static void
5463nv_hat(cap)
5464 cmdarg_T *cap;
5465{
5466 if (!checkclearopq(cap->oap))
5467 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5468 GETF_SETMARK|GETF_ALT, FALSE);
5469}
5470
5471/*
5472 * "Z" commands.
5473 */
5474 static void
5475nv_Zet(cap)
5476 cmdarg_T *cap;
5477{
5478 if (!checkclearopq(cap->oap))
5479 {
5480 switch (cap->nchar)
5481 {
5482 /* "ZZ": equivalent to ":x". */
5483 case 'Z': do_cmdline_cmd((char_u *)"x");
5484 break;
5485
5486 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5487 case 'Q': do_cmdline_cmd((char_u *)"q!");
5488 break;
5489
5490 default: clearopbeep(cap->oap);
5491 }
5492 }
5493}
5494
5495#if defined(FEAT_WINDOWS) || defined(PROTO)
5496/*
5497 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5498 */
5499 void
5500do_nv_ident(c1, c2)
5501 int c1;
5502 int c2;
5503{
5504 oparg_T oa;
5505 cmdarg_T ca;
5506
5507 clear_oparg(&oa);
5508 vim_memset(&ca, 0, sizeof(ca));
5509 ca.oap = &oa;
5510 ca.cmdchar = c1;
5511 ca.nchar = c2;
5512 nv_ident(&ca);
5513}
5514#endif
5515
5516/*
5517 * Handle the commands that use the word under the cursor.
5518 * [g] CTRL-] :ta to current identifier
5519 * [g] 'K' run program for current identifier
5520 * [g] '*' / to current identifier or string
5521 * [g] '#' ? to current identifier or string
5522 * g ']' :tselect for current identifier
5523 */
5524 static void
5525nv_ident(cap)
5526 cmdarg_T *cap;
5527{
5528 char_u *ptr = NULL;
5529 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005530 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 char_u *p;
5532 char_u *kp; /* value of 'keywordprg' */
5533 int kp_help; /* 'keywordprg' is ":help" */
5534 int n = 0; /* init for GCC */
5535 int cmdchar;
5536 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005537 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 char_u *aux_ptr;
5539 int isman;
5540 int isman_s;
5541
5542 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5543 {
5544 cmdchar = cap->nchar;
5545 g_cmd = TRUE;
5546 }
5547 else
5548 {
5549 cmdchar = cap->cmdchar;
5550 g_cmd = FALSE;
5551 }
5552
5553 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5554 cmdchar = '#';
5555
5556 /*
5557 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5558 */
5559 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5560 {
5561#ifdef FEAT_VISUAL
5562 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5563 return;
5564#endif
5565 if (checkclearopq(cap->oap))
5566 return;
5567 }
5568
5569 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5570 (cmdchar == '*' || cmdchar == '#')
5571 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5572 {
5573 clearop(cap->oap);
5574 return;
5575 }
5576
5577 /* Allocate buffer to put the command in. Inserting backslashes can
5578 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5579 * and some numbers. */
5580 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5581 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5582 || STRCMP(kp, ":help") == 0);
5583 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5584 if (buf == NULL)
5585 return;
5586 buf[0] = NUL;
5587
5588 switch (cmdchar)
5589 {
5590 case '*':
5591 case '#':
5592 /*
5593 * Put cursor at start of word, makes search skip the word
5594 * under the cursor.
5595 * Call setpcmark() first, so "*``" puts the cursor back where
5596 * it was.
5597 */
5598 setpcmark();
5599 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5600
5601 if (!g_cmd && vim_iswordp(ptr))
5602 STRCPY(buf, "\\<");
5603 no_smartcase = TRUE; /* don't use 'smartcase' now */
5604 break;
5605
5606 case 'K':
5607 if (kp_help)
5608 STRCPY(buf, "he! ");
5609 else
5610 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005611 /* An external command will probably use an argument starting
5612 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005613 while (*ptr == '-' && n > 0)
5614 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005615 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005616 --n;
5617 }
5618 if (n == 0)
5619 {
5620 EMSG(_(e_noident)); /* found dashes only */
5621 vim_free(buf);
5622 return;
5623 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 /* When a count is given, turn it into a range. Is this
5626 * really what we want? */
5627 isman = (STRCMP(kp, "man") == 0);
5628 isman_s = (STRCMP(kp, "man -s") == 0);
5629 if (cap->count0 != 0 && !(isman || isman_s))
5630 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5631
5632 STRCAT(buf, "! ");
5633 if (cap->count0 == 0 && isman_s)
5634 STRCAT(buf, "man");
5635 else
5636 STRCAT(buf, kp);
5637 STRCAT(buf, " ");
5638 if (cap->count0 != 0 && (isman || isman_s))
5639 {
5640 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5641 STRCAT(buf, " ");
5642 }
5643 }
5644 break;
5645
5646 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005647 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#ifdef FEAT_CSCOPE
5649 if (p_cst)
5650 STRCPY(buf, "cstag ");
5651 else
5652#endif
5653 STRCPY(buf, "ts ");
5654 break;
5655
5656 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005657 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (curbuf->b_help)
5659 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005661 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005662 if (g_cmd)
5663 STRCPY(buf, "tj ");
5664 else
5665 sprintf((char *)buf, "%ldta ", cap->count0);
5666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
5668
5669 /*
5670 * Now grab the chars in the identifier
5671 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005672 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005674 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005675 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005676 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005677 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005678 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005680 vim_free(buf);
5681 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005683 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5684 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005685 {
5686 vim_free(buf);
5687 vim_free(p);
5688 return;
5689 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005690 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005691 STRCAT(buf, p);
5692 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005694 else
5695 {
5696 if (cmdchar == '*')
5697 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5698 else if (cmdchar == '#')
5699 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005700 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005701 {
5702 if (curbuf->b_help)
5703 /* ":help" handles unescaped argument */
5704 aux_ptr = (char_u *)"";
5705 else
5706 aux_ptr = (char_u *)"\\|\"\n[";
5707 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005708 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005709 aux_ptr = (char_u *)"\\|\"\n*?[";
5710
5711 p = buf + STRLEN(buf);
5712 while (n-- > 0)
5713 {
5714 /* put a backslash before \ and some others */
5715 if (vim_strchr(aux_ptr, *ptr) != NULL)
5716 *p++ = '\\';
5717#ifdef FEAT_MBYTE
5718 /* When current byte is a part of multibyte character, copy all
5719 * bytes of that character. */
5720 if (has_mbyte)
5721 {
5722 int i;
5723 int len = (*mb_ptr2len)(ptr) - 1;
5724
5725 for (i = 0; i < len && n >= 1; ++i, --n)
5726 *p++ = *ptr++;
5727 }
5728#endif
5729 *p++ = *ptr++;
5730 }
5731 *p = NUL;
5732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 /*
5735 * Execute the command.
5736 */
5737 if (cmdchar == '*' || cmdchar == '#')
5738 {
5739 if (!g_cmd && (
5740#ifdef FEAT_MBYTE
5741 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5742#endif
5743 vim_iswordc(ptr[-1])))
5744 STRCAT(buf, "\\>");
5745#ifdef FEAT_CMDHIST
5746 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005747 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5749#endif
5750 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5751 }
5752 else
5753 do_cmdline_cmd(buf);
5754
5755 vim_free(buf);
5756}
5757
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005758#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759/*
5760 * Get visually selected text, within one line only.
5761 * Returns FAIL if more than one line selected.
5762 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005763 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764get_visual_text(cap, pp, lenp)
5765 cmdarg_T *cap;
5766 char_u **pp; /* return: start of selected text */
5767 int *lenp; /* return: length of selected text */
5768{
5769 if (VIsual_mode != 'V')
5770 unadjust_for_sel();
5771 if (VIsual.lnum != curwin->w_cursor.lnum)
5772 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005773 if (cap != NULL)
5774 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 return FAIL;
5776 }
5777 if (VIsual_mode == 'V')
5778 {
5779 *pp = ml_get_curline();
5780 *lenp = (int)STRLEN(*pp);
5781 }
5782 else
5783 {
5784 if (lt(curwin->w_cursor, VIsual))
5785 {
5786 *pp = ml_get_pos(&curwin->w_cursor);
5787 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5788 }
5789 else
5790 {
5791 *pp = ml_get_pos(&VIsual);
5792 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5793 }
5794#ifdef FEAT_MBYTE
5795 if (has_mbyte)
5796 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#endif
5799 }
5800 reset_VIsual_and_resel();
5801 return OK;
5802}
5803#endif
5804
5805/*
5806 * CTRL-T: backwards in tag stack
5807 */
5808 static void
5809nv_tagpop(cap)
5810 cmdarg_T *cap;
5811{
5812 if (!checkclearopq(cap->oap))
5813 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5814}
5815
5816/*
5817 * Handle scrolling command 'H', 'L' and 'M'.
5818 */
5819 static void
5820nv_scroll(cap)
5821 cmdarg_T *cap;
5822{
5823 int used = 0;
5824 long n;
5825#ifdef FEAT_FOLDING
5826 linenr_T lnum;
5827#endif
5828 int half;
5829
5830 cap->oap->motion_type = MLINE;
5831 setpcmark();
5832
5833 if (cap->cmdchar == 'L')
5834 {
5835 validate_botline(); /* make sure curwin->w_botline is valid */
5836 curwin->w_cursor.lnum = curwin->w_botline - 1;
5837 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5838 curwin->w_cursor.lnum = 1;
5839 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005840 {
5841#ifdef FEAT_FOLDING
5842 if (hasAnyFolding(curwin))
5843 {
5844 /* Count a fold for one screen line. */
5845 for (n = cap->count1 - 1; n > 0
5846 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5847 {
5848 (void)hasFolding(curwin->w_cursor.lnum,
5849 &curwin->w_cursor.lnum, NULL);
5850 --curwin->w_cursor.lnum;
5851 }
5852 }
5853 else
5854#endif
5855 curwin->w_cursor.lnum -= cap->count1 - 1;
5856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858 else
5859 {
5860 if (cap->cmdchar == 'M')
5861 {
5862#ifdef FEAT_DIFF
5863 /* Don't count filler lines above the window. */
5864 used -= diff_check_fill(curwin, curwin->w_topline)
5865 - curwin->w_topfill;
5866#endif
5867 validate_botline(); /* make sure w_empty_rows is valid */
5868 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5869 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5870 {
5871#ifdef FEAT_DIFF
5872 /* Count half he number of filler lines to be "below this
5873 * line" and half to be "above the next line". */
5874 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5875 + n) / 2 >= half)
5876 {
5877 --n;
5878 break;
5879 }
5880#endif
5881 used += plines(curwin->w_topline + n);
5882 if (used >= half)
5883 break;
5884#ifdef FEAT_FOLDING
5885 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5886 n = lnum - curwin->w_topline;
5887#endif
5888 }
5889 if (n > 0 && used > curwin->w_height)
5890 --n;
5891 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005892 else /* (cap->cmdchar == 'H') */
5893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005895#ifdef FEAT_FOLDING
5896 if (hasAnyFolding(curwin))
5897 {
5898 /* Count a fold for one screen line. */
5899 lnum = curwin->w_topline;
5900 while (n-- > 0 && lnum < curwin->w_botline - 1)
5901 {
5902 hasFolding(lnum, NULL, &lnum);
5903 ++lnum;
5904 }
5905 n = lnum - curwin->w_topline;
5906 }
5907#endif
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 curwin->w_cursor.lnum = curwin->w_topline + n;
5910 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5911 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5912 }
5913
5914 cursor_correct(); /* correct for 'so' */
5915 beginline(BL_SOL | BL_FIX);
5916}
5917
5918/*
5919 * Cursor right commands.
5920 */
5921 static void
5922nv_right(cap)
5923 cmdarg_T *cap;
5924{
5925 long n;
5926#ifdef FEAT_VISUAL
5927 int PAST_LINE;
5928#else
5929# define PAST_LINE 0
5930#endif
5931
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005932 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5933 {
5934 /* <C-Right> and <S-Right> move a word or WORD right */
5935 if (mod_mask & MOD_MASK_CTRL)
5936 cap->arg = TRUE;
5937 nv_wordcmd(cap);
5938 return;
5939 }
5940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 cap->oap->motion_type = MCHAR;
5942 cap->oap->inclusive = FALSE;
5943#ifdef FEAT_VISUAL
5944 PAST_LINE = (VIsual_active && *p_sel != 'o');
5945
5946# ifdef FEAT_VIRTUALEDIT
5947 /*
5948 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005949 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 */
5951 if (virtual_active())
5952 PAST_LINE = 0;
5953# endif
5954#endif
5955
5956 for (n = cap->count1; n > 0; --n)
5957 {
5958 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005959#ifdef FEAT_VISUAL
5960 || (PAST_LINE && *ml_get_cursor() == NUL)
5961#endif
5962 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
5964 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005965 * <Space> wraps to next line if 'whichwrap' has 's'.
5966 * 'l' wraps to next line if 'whichwrap' has 'l'.
5967 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 */
5969 if ( ((cap->cmdchar == ' '
5970 && vim_strchr(p_ww, 's') != NULL)
5971 || (cap->cmdchar == 'l'
5972 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005973 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 && vim_strchr(p_ww, '>') != NULL))
5975 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5976 {
5977 /* When deleting we also count the NL as a character.
5978 * Set cap->oap->inclusive when last char in the line is
5979 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005980 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 && !cap->oap->inclusive
5982 && !lineempty(curwin->w_cursor.lnum))
5983 cap->oap->inclusive = TRUE;
5984 else
5985 {
5986 ++curwin->w_cursor.lnum;
5987 curwin->w_cursor.col = 0;
5988#ifdef FEAT_VIRTUALEDIT
5989 curwin->w_cursor.coladd = 0;
5990#endif
5991 curwin->w_set_curswant = TRUE;
5992 cap->oap->inclusive = FALSE;
5993 }
5994 continue;
5995 }
5996 if (cap->oap->op_type == OP_NOP)
5997 {
5998 /* Only beep and flush if not moved at all */
5999 if (n == cap->count1)
6000 beep_flush();
6001 }
6002 else
6003 {
6004 if (!lineempty(curwin->w_cursor.lnum))
6005 cap->oap->inclusive = TRUE;
6006 }
6007 break;
6008 }
6009#ifdef FEAT_VISUAL
6010 else if (PAST_LINE)
6011 {
6012 curwin->w_set_curswant = TRUE;
6013# ifdef FEAT_VIRTUALEDIT
6014 if (virtual_active())
6015 oneright();
6016 else
6017# endif
6018 {
6019# ifdef FEAT_MBYTE
6020 if (has_mbyte)
6021 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006022 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 else
6024# endif
6025 ++curwin->w_cursor.col;
6026 }
6027 }
6028#endif
6029 }
6030#ifdef FEAT_FOLDING
6031 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6032 && cap->oap->op_type == OP_NOP)
6033 foldOpenCursor();
6034#endif
6035}
6036
6037/*
6038 * Cursor left commands.
6039 *
6040 * Returns TRUE when operator end should not be adjusted.
6041 */
6042 static void
6043nv_left(cap)
6044 cmdarg_T *cap;
6045{
6046 long n;
6047
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006048 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6049 {
6050 /* <C-Left> and <S-Left> move a word or WORD left */
6051 if (mod_mask & MOD_MASK_CTRL)
6052 cap->arg = 1;
6053 nv_bck_word(cap);
6054 return;
6055 }
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 cap->oap->motion_type = MCHAR;
6058 cap->oap->inclusive = FALSE;
6059 for (n = cap->count1; n > 0; --n)
6060 {
6061 if (oneleft() == FAIL)
6062 {
6063 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6064 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6065 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6066 */
6067 if ( (((cap->cmdchar == K_BS
6068 || cap->cmdchar == Ctrl_H)
6069 && vim_strchr(p_ww, 'b') != NULL)
6070 || (cap->cmdchar == 'h'
6071 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006072 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 && vim_strchr(p_ww, '<') != NULL))
6074 && curwin->w_cursor.lnum > 1)
6075 {
6076 --(curwin->w_cursor.lnum);
6077 coladvance((colnr_T)MAXCOL);
6078 curwin->w_set_curswant = TRUE;
6079
6080 /* When the NL before the first char has to be deleted we
6081 * put the cursor on the NUL after the previous line.
6082 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006083 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if ( (cap->oap->op_type == OP_DELETE
6085 || cap->oap->op_type == OP_CHANGE)
6086 && !lineempty(curwin->w_cursor.lnum))
6087 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006088 if (*ml_get_cursor() != NUL)
6089 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 cap->retval |= CA_NO_ADJ_OP_END;
6091 }
6092 continue;
6093 }
6094 /* Only beep and flush if not moved at all */
6095 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6096 beep_flush();
6097 break;
6098 }
6099 }
6100#ifdef FEAT_FOLDING
6101 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6102 && cap->oap->op_type == OP_NOP)
6103 foldOpenCursor();
6104#endif
6105}
6106
6107/*
6108 * Cursor up commands.
6109 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6110 */
6111 static void
6112nv_up(cap)
6113 cmdarg_T *cap;
6114{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006115 if (mod_mask & MOD_MASK_SHIFT)
6116 {
6117 /* <S-Up> is page up */
6118 cap->arg = BACKWARD;
6119 nv_page(cap);
6120 }
6121 else
6122 {
6123 cap->oap->motion_type = MLINE;
6124 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6125 clearopbeep(cap->oap);
6126 else if (cap->arg)
6127 beginline(BL_WHITE | BL_FIX);
6128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129}
6130
6131/*
6132 * Cursor down commands.
6133 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6134 */
6135 static void
6136nv_down(cap)
6137 cmdarg_T *cap;
6138{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006139 if (mod_mask & MOD_MASK_SHIFT)
6140 {
6141 /* <S-Down> is page down */
6142 cap->arg = FORWARD;
6143 nv_page(cap);
6144 }
6145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6147 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006148 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006149 if (curwin->w_llist_ref == NULL)
6150 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6151 else
6152 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 else
6154#endif
6155 {
6156#ifdef FEAT_CMDWIN
6157 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006158 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 cmdwin_result = CAR;
6160 else
6161#endif
6162 {
6163 cap->oap->motion_type = MLINE;
6164 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6165 clearopbeep(cap->oap);
6166 else if (cap->arg)
6167 beginline(BL_WHITE | BL_FIX);
6168 }
6169 }
6170}
6171
6172#ifdef FEAT_SEARCHPATH
6173/*
6174 * Grab the file name under the cursor and edit it.
6175 */
6176 static void
6177nv_gotofile(cap)
6178 cmdarg_T *cap;
6179{
6180 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006181 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006183 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
6185 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006186 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 return;
6188 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006189#ifdef FEAT_AUTOCMD
6190 if (curbuf_locked())
6191 {
6192 clearop(cap->oap);
6193 return;
6194 }
6195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006197 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198
6199 if (ptr != NULL)
6200 {
6201 /* do autowrite if necessary */
6202 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6203 autowrite(curbuf, FALSE);
6204 setpcmark();
6205 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006206 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006207 if (cap->nchar == 'F' && lnum >= 0)
6208 {
6209 curwin->w_cursor.lnum = lnum;
6210 check_cursor_lnum();
6211 beginline(BL_SOL | BL_FIX);
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 vim_free(ptr);
6214 }
6215 else
6216 clearop(cap->oap);
6217}
6218#endif
6219
6220/*
6221 * <End> command: to end of current line or last line.
6222 */
6223 static void
6224nv_end(cap)
6225 cmdarg_T *cap;
6226{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006227 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006229 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 nv_goto(cap);
6231 cap->count1 = 1; /* to end of current line */
6232 }
6233 nv_dollar(cap);
6234}
6235
6236/*
6237 * Handle the "$" command.
6238 */
6239 static void
6240nv_dollar(cap)
6241 cmdarg_T *cap;
6242{
6243 cap->oap->motion_type = MCHAR;
6244 cap->oap->inclusive = TRUE;
6245#ifdef FEAT_VIRTUALEDIT
6246 /* In virtual mode when off the edge of a line and an operator
6247 * is pending (whew!) keep the cursor where it is.
6248 * Otherwise, send it to the end of the line. */
6249 if (!virtual_active() || gchar_cursor() != NUL
6250 || cap->oap->op_type == OP_NOP)
6251#endif
6252 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6253 if (cursor_down((long)(cap->count1 - 1),
6254 cap->oap->op_type == OP_NOP) == FAIL)
6255 clearopbeep(cap->oap);
6256#ifdef FEAT_FOLDING
6257 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6258 foldOpenCursor();
6259#endif
6260}
6261
6262/*
6263 * Implementation of '?' and '/' commands.
6264 * If cap->arg is TRUE don't set PC mark.
6265 */
6266 static void
6267nv_search(cap)
6268 cmdarg_T *cap;
6269{
6270 oparg_T *oap = cap->oap;
6271
6272 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6273 {
6274 /* Translate "g??" to "g?g?" */
6275 cap->cmdchar = 'g';
6276 cap->nchar = '?';
6277 nv_operator(cap);
6278 return;
6279 }
6280
6281 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6282
6283 if (cap->searchbuf == NULL)
6284 {
6285 clearop(oap);
6286 return;
6287 }
6288
6289 normal_search(cap, cap->cmdchar, cap->searchbuf,
6290 (cap->arg ? 0 : SEARCH_MARK));
6291}
6292
6293/*
6294 * Handle "N" and "n" commands.
6295 * cap->arg is SEARCH_REV for "N", 0 for "n".
6296 */
6297 static void
6298nv_next(cap)
6299 cmdarg_T *cap;
6300{
6301 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6302}
6303
6304/*
6305 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6306 * Uses only cap->count1 and cap->oap from "cap".
6307 */
6308 static void
6309normal_search(cap, dir, pat, opt)
6310 cmdarg_T *cap;
6311 int dir;
6312 char_u *pat;
6313 int opt; /* extra flags for do_search() */
6314{
6315 int i;
6316
6317 cap->oap->motion_type = MCHAR;
6318 cap->oap->inclusive = FALSE;
6319 cap->oap->use_reg_one = TRUE;
6320 curwin->w_set_curswant = TRUE;
6321
6322 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006323 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (i == 0)
6325 clearop(cap->oap);
6326 else
6327 {
6328 if (i == 2)
6329 cap->oap->motion_type = MLINE;
6330#ifdef FEAT_VIRTUALEDIT
6331 curwin->w_cursor.coladd = 0;
6332#endif
6333#ifdef FEAT_FOLDING
6334 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6335 foldOpenCursor();
6336#endif
6337 }
6338
6339 /* "/$" will put the cursor after the end of the line, may need to
6340 * correct that here */
6341 check_cursor();
6342}
6343
6344/*
6345 * Character search commands.
6346 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6347 * ',' and FALSE for ';'.
6348 * cap->nchar is NUL for ',' and ';' (repeat the search)
6349 */
6350 static void
6351nv_csearch(cap)
6352 cmdarg_T *cap;
6353{
6354 int t_cmd;
6355
6356 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6357 t_cmd = TRUE;
6358 else
6359 t_cmd = FALSE;
6360
6361 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6363 clearopbeep(cap->oap);
6364 else
6365 {
6366 curwin->w_set_curswant = TRUE;
6367#ifdef FEAT_VIRTUALEDIT
6368 /* Include a Tab for "tx" and for "dfx". */
6369 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6370 && (t_cmd || cap->oap->op_type != OP_NOP))
6371 {
6372 colnr_T scol, ecol;
6373
6374 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6375 curwin->w_cursor.coladd = ecol - scol;
6376 }
6377 else
6378 curwin->w_cursor.coladd = 0;
6379#endif
6380#ifdef FEAT_VISUAL
6381 adjust_for_sel(cap);
6382#endif
6383#ifdef FEAT_FOLDING
6384 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6385 foldOpenCursor();
6386#endif
6387 }
6388}
6389
6390/*
6391 * "[" and "]" commands.
6392 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6393 */
6394 static void
6395nv_brackets(cap)
6396 cmdarg_T *cap;
6397{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006398 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 pos_T prev_pos;
6400 pos_T *pos = NULL; /* init for GCC */
6401 pos_T old_pos; /* cursor position before command */
6402 int flag;
6403 long n;
6404 int findc;
6405 int c;
6406
6407 cap->oap->motion_type = MCHAR;
6408 cap->oap->inclusive = FALSE;
6409 old_pos = curwin->w_cursor;
6410#ifdef FEAT_VIRTUALEDIT
6411 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6412#endif
6413
6414#ifdef FEAT_SEARCHPATH
6415 /*
6416 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6417 */
6418 if (cap->nchar == 'f')
6419 nv_gotofile(cap);
6420 else
6421#endif
6422
6423#ifdef FEAT_FIND_ID
6424 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006425 * Find the occurrence(s) of the identifier or define under cursor
6426 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 *
6428 * search list jump
6429 * fwd bwd fwd bwd fwd bwd
6430 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6431 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6432 */
6433 if (vim_strchr((char_u *)
6434#ifdef EBCDIC
6435 "iI\005dD\067",
6436#else
6437 "iI\011dD\004",
6438#endif
6439 cap->nchar) != NULL)
6440 {
6441 char_u *ptr;
6442 int len;
6443
6444 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6445 clearop(cap->oap);
6446 else
6447 {
6448 find_pattern_in_path(ptr, 0, len, TRUE,
6449 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6450 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6451 cap->count1,
6452 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6453 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6454 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6455 (linenr_T)MAXLNUM);
6456 curwin->w_set_curswant = TRUE;
6457 }
6458 }
6459 else
6460#endif
6461
6462 /*
6463 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6464 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6465 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6466 * "[m" or "]m" search for prev/next start of (Java) method.
6467 * "[M" or "]M" search for prev/next end of (Java) method.
6468 */
6469 if ( (cap->cmdchar == '['
6470 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6471 || (cap->cmdchar == ']'
6472 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6473 {
6474 if (cap->nchar == '*')
6475 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 prev_pos.lnum = 0;
6477 if (cap->nchar == 'm' || cap->nchar == 'M')
6478 {
6479 if (cap->cmdchar == '[')
6480 findc = '{';
6481 else
6482 findc = '}';
6483 n = 9999;
6484 }
6485 else
6486 {
6487 findc = cap->nchar;
6488 n = cap->count1;
6489 }
6490 for ( ; n > 0; --n)
6491 {
6492 if ((pos = findmatchlimit(cap->oap, findc,
6493 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6494 {
6495 if (new_pos.lnum == 0) /* nothing found */
6496 {
6497 if (cap->nchar != 'm' && cap->nchar != 'M')
6498 clearopbeep(cap->oap);
6499 }
6500 else
6501 pos = &new_pos; /* use last one found */
6502 break;
6503 }
6504 prev_pos = new_pos;
6505 curwin->w_cursor = *pos;
6506 new_pos = *pos;
6507 }
6508 curwin->w_cursor = old_pos;
6509
6510 /*
6511 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6512 * brought us to the match for "[m" and "]M" when inside a method.
6513 * Try finding the '{' or '}' we want to be at.
6514 * Also repeat for the given count.
6515 */
6516 if (cap->nchar == 'm' || cap->nchar == 'M')
6517 {
6518 /* norm is TRUE for "]M" and "[m" */
6519 int norm = ((findc == '{') == (cap->nchar == 'm'));
6520
6521 n = cap->count1;
6522 /* found a match: we were inside a method */
6523 if (prev_pos.lnum != 0)
6524 {
6525 pos = &prev_pos;
6526 curwin->w_cursor = prev_pos;
6527 if (norm)
6528 --n;
6529 }
6530 else
6531 pos = NULL;
6532 while (n > 0)
6533 {
6534 for (;;)
6535 {
6536 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6537 {
6538 /* if not found anything, that's an error */
6539 if (pos == NULL)
6540 clearopbeep(cap->oap);
6541 n = 0;
6542 break;
6543 }
6544 c = gchar_cursor();
6545 if (c == '{' || c == '}')
6546 {
6547 /* Must have found end/start of class: use it.
6548 * Or found the place to be at. */
6549 if ((c == findc && norm) || (n == 1 && !norm))
6550 {
6551 new_pos = curwin->w_cursor;
6552 pos = &new_pos;
6553 n = 0;
6554 }
6555 /* if no match found at all, we started outside of the
6556 * class and we're inside now. Just go on. */
6557 else if (new_pos.lnum == 0)
6558 {
6559 new_pos = curwin->w_cursor;
6560 pos = &new_pos;
6561 }
6562 /* found start/end of other method: go to match */
6563 else if ((pos = findmatchlimit(cap->oap, findc,
6564 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6565 0)) == NULL)
6566 n = 0;
6567 else
6568 curwin->w_cursor = *pos;
6569 break;
6570 }
6571 }
6572 --n;
6573 }
6574 curwin->w_cursor = old_pos;
6575 if (pos == NULL && new_pos.lnum != 0)
6576 clearopbeep(cap->oap);
6577 }
6578 if (pos != NULL)
6579 {
6580 setpcmark();
6581 curwin->w_cursor = *pos;
6582 curwin->w_set_curswant = TRUE;
6583#ifdef FEAT_FOLDING
6584 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6585 && cap->oap->op_type == OP_NOP)
6586 foldOpenCursor();
6587#endif
6588 }
6589 }
6590
6591 /*
6592 * "[[", "[]", "]]" and "][": move to start or end of function
6593 */
6594 else if (cap->nchar == '[' || cap->nchar == ']')
6595 {
6596 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6597 flag = '{';
6598 else
6599 flag = '}'; /* "][" or "[]" */
6600
6601 curwin->w_set_curswant = TRUE;
6602 /*
6603 * Imitate strange Vi behaviour: When using "]]" with an operator
6604 * we also stop at '}'.
6605 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006606 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 (cap->oap->op_type != OP_NOP
6608 && cap->arg == FORWARD && flag == '{')))
6609 clearopbeep(cap->oap);
6610 else
6611 {
6612 if (cap->oap->op_type == OP_NOP)
6613 beginline(BL_WHITE | BL_FIX);
6614#ifdef FEAT_FOLDING
6615 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6616 foldOpenCursor();
6617#endif
6618 }
6619 }
6620
6621 /*
6622 * "[p", "[P", "]P" and "]p": put with indent adjustment
6623 */
6624 else if (cap->nchar == 'p' || cap->nchar == 'P')
6625 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006626 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 {
6628 prep_redo_cmd(cap);
6629 do_put(cap->oap->regname,
6630 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6631 cap->count1, PUT_FIXINDENT);
6632 }
6633 }
6634
6635 /*
6636 * "['", "[`", "]'" and "]`": jump to next mark
6637 */
6638 else if (cap->nchar == '\'' || cap->nchar == '`')
6639 {
6640 pos = &curwin->w_cursor;
6641 for (n = cap->count1; n > 0; --n)
6642 {
6643 prev_pos = *pos;
6644 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6645 cap->nchar == '\'');
6646 if (pos == NULL)
6647 break;
6648 }
6649 if (pos == NULL)
6650 pos = &prev_pos;
6651 nv_cursormark(cap, cap->nchar == '\'', pos);
6652 }
6653
6654#ifdef FEAT_MOUSE
6655 /*
6656 * [ or ] followed by a middle mouse click: put selected text with
6657 * indent adjustment. Any other button just does as usual.
6658 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006659 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 {
6661 (void)do_mouse(cap->oap, cap->nchar,
6662 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6663 cap->count1, PUT_FIXINDENT);
6664 }
6665#endif /* FEAT_MOUSE */
6666
6667#ifdef FEAT_FOLDING
6668 /*
6669 * "[z" and "]z": move to start or end of open fold.
6670 */
6671 else if (cap->nchar == 'z')
6672 {
6673 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6674 cap->count1) == FAIL)
6675 clearopbeep(cap->oap);
6676 }
6677#endif
6678
6679#ifdef FEAT_DIFF
6680 /*
6681 * "[c" and "]c": move to next or previous diff-change.
6682 */
6683 else if (cap->nchar == 'c')
6684 {
6685 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6686 cap->count1) == FAIL)
6687 clearopbeep(cap->oap);
6688 }
6689#endif
6690
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006691#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006692 /*
6693 * "[s", "[S", "]s" and "]S": move to next spell error.
6694 */
6695 else if (cap->nchar == 's' || cap->nchar == 'S')
6696 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006697 setpcmark();
6698 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006699 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6700 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006701 {
6702 clearopbeep(cap->oap);
6703 break;
6704 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006705# ifdef FEAT_FOLDING
6706 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6707 foldOpenCursor();
6708# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006709 }
6710#endif
6711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 /* Not a valid cap->nchar. */
6713 else
6714 clearopbeep(cap->oap);
6715}
6716
6717/*
6718 * Handle Normal mode "%" command.
6719 */
6720 static void
6721nv_percent(cap)
6722 cmdarg_T *cap;
6723{
6724 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006725#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 linenr_T lnum = curwin->w_cursor.lnum;
6727#endif
6728
6729 cap->oap->inclusive = TRUE;
6730 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6731 {
6732 if (cap->count0 > 100)
6733 clearopbeep(cap->oap);
6734 else
6735 {
6736 cap->oap->motion_type = MLINE;
6737 setpcmark();
6738 /* Round up, so CTRL-G will give same value. Watch out for a
6739 * large line count, the line number must not go negative! */
6740 if (curbuf->b_ml.ml_line_count > 1000000)
6741 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6742 / 100L * cap->count0;
6743 else
6744 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6745 cap->count0 + 99L) / 100L;
6746 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6747 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6748 beginline(BL_SOL | BL_FIX);
6749 }
6750 }
6751 else /* "%" : go to matching paren */
6752 {
6753 cap->oap->motion_type = MCHAR;
6754 cap->oap->use_reg_one = TRUE;
6755 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6756 clearopbeep(cap->oap);
6757 else
6758 {
6759 setpcmark();
6760 curwin->w_cursor = *pos;
6761 curwin->w_set_curswant = TRUE;
6762#ifdef FEAT_VIRTUALEDIT
6763 curwin->w_cursor.coladd = 0;
6764#endif
6765#ifdef FEAT_VISUAL
6766 adjust_for_sel(cap);
6767#endif
6768 }
6769 }
6770#ifdef FEAT_FOLDING
6771 if (cap->oap->op_type == OP_NOP
6772 && lnum != curwin->w_cursor.lnum
6773 && (fdo_flags & FDO_PERCENT)
6774 && KeyTyped)
6775 foldOpenCursor();
6776#endif
6777}
6778
6779/*
6780 * Handle "(" and ")" commands.
6781 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6782 */
6783 static void
6784nv_brace(cap)
6785 cmdarg_T *cap;
6786{
6787 cap->oap->motion_type = MCHAR;
6788 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006789 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6790 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 curwin->w_set_curswant = TRUE;
6792
6793 if (findsent(cap->arg, cap->count1) == FAIL)
6794 clearopbeep(cap->oap);
6795 else
6796 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006797 /* Don't leave the cursor on the NUL past end of line. */
6798 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#ifdef FEAT_VIRTUALEDIT
6800 curwin->w_cursor.coladd = 0;
6801#endif
6802#ifdef FEAT_FOLDING
6803 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6804 foldOpenCursor();
6805#endif
6806 }
6807}
6808
6809/*
6810 * "m" command: Mark a position.
6811 */
6812 static void
6813nv_mark(cap)
6814 cmdarg_T *cap;
6815{
6816 if (!checkclearop(cap->oap))
6817 {
6818 if (setmark(cap->nchar) == FAIL)
6819 clearopbeep(cap->oap);
6820 }
6821}
6822
6823/*
6824 * "{" and "}" commands.
6825 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6826 */
6827 static void
6828nv_findpar(cap)
6829 cmdarg_T *cap;
6830{
6831 cap->oap->motion_type = MCHAR;
6832 cap->oap->inclusive = FALSE;
6833 cap->oap->use_reg_one = TRUE;
6834 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006835 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 clearopbeep(cap->oap);
6837 else
6838 {
6839#ifdef FEAT_VIRTUALEDIT
6840 curwin->w_cursor.coladd = 0;
6841#endif
6842#ifdef FEAT_FOLDING
6843 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6844 foldOpenCursor();
6845#endif
6846 }
6847}
6848
6849/*
6850 * "u" command: Undo or make lower case.
6851 */
6852 static void
6853nv_undo(cap)
6854 cmdarg_T *cap;
6855{
6856 if (cap->oap->op_type == OP_LOWER
6857#ifdef FEAT_VISUAL
6858 || VIsual_active
6859#endif
6860 )
6861 {
6862 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6863 cap->cmdchar = 'g';
6864 cap->nchar = 'u';
6865 nv_operator(cap);
6866 }
6867 else
6868 nv_kundo(cap);
6869}
6870
6871/*
6872 * <Undo> command.
6873 */
6874 static void
6875nv_kundo(cap)
6876 cmdarg_T *cap;
6877{
6878 if (!checkclearopq(cap->oap))
6879 {
6880 u_undo((int)cap->count1);
6881 curwin->w_set_curswant = TRUE;
6882 }
6883}
6884
6885/*
6886 * Handle the "r" command.
6887 */
6888 static void
6889nv_replace(cap)
6890 cmdarg_T *cap;
6891{
6892 char_u *ptr;
6893 int had_ctrl_v;
6894 long n;
6895
6896 if (checkclearop(cap->oap))
6897 return;
6898
6899 /* get another character */
6900 if (cap->nchar == Ctrl_V)
6901 {
6902 had_ctrl_v = Ctrl_V;
6903 cap->nchar = get_literal();
6904 /* Don't redo a multibyte character with CTRL-V. */
6905 if (cap->nchar > DEL)
6906 had_ctrl_v = NUL;
6907 }
6908 else
6909 had_ctrl_v = NUL;
6910
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006911 /* Abort if the character is a special key. */
6912 if (IS_SPECIAL(cap->nchar))
6913 {
6914 clearopbeep(cap->oap);
6915 return;
6916 }
6917
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#ifdef FEAT_VISUAL
6919 /* Visual mode "r" */
6920 if (VIsual_active)
6921 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006922 if (got_int)
6923 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 nv_operator(cap);
6925 return;
6926 }
6927#endif
6928
6929#ifdef FEAT_VIRTUALEDIT
6930 /* Break tabs, etc. */
6931 if (virtual_active())
6932 {
6933 if (u_save_cursor() == FAIL)
6934 return;
6935 if (gchar_cursor() == NUL)
6936 {
6937 /* Add extra space and put the cursor on the first one. */
6938 coladvance_force((colnr_T)(getviscol() + cap->count1));
6939 curwin->w_cursor.col -= cap->count1;
6940 }
6941 else if (gchar_cursor() == TAB)
6942 coladvance_force(getviscol());
6943 }
6944#endif
6945
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006946 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006948 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949#ifdef FEAT_MBYTE
6950 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6951#endif
6952 )
6953 {
6954 clearopbeep(cap->oap);
6955 return;
6956 }
6957
6958 /*
6959 * Replacing with a TAB is done by edit() when it is complicated because
6960 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6961 * Other characters are done below to avoid problems with things like
6962 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6963 */
6964 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6965 {
6966 stuffnumReadbuff(cap->count1);
6967 stuffcharReadbuff('R');
6968 stuffcharReadbuff('\t');
6969 stuffcharReadbuff(ESC);
6970 return;
6971 }
6972
6973 /* save line for undo */
6974 if (u_save_cursor() == FAIL)
6975 return;
6976
6977 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6978 {
6979 /*
6980 * Replace character(s) by a single newline.
6981 * Strange vi behaviour: Only one newline is inserted.
6982 * Delete the characters here.
6983 * Insert the newline with an insert command, takes care of
6984 * autoindent. The insert command depends on being on the last
6985 * character of a line or not.
6986 */
6987#ifdef FEAT_MBYTE
6988 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6989#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006990 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991#endif
6992 stuffcharReadbuff('\r');
6993 stuffcharReadbuff(ESC);
6994
6995 /* Give 'r' to edit(), to get the redo command right. */
6996 invoke_edit(cap, TRUE, 'r', FALSE);
6997 }
6998 else
6999 {
7000 prep_redo(cap->oap->regname, cap->count1,
7001 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7002
7003 curbuf->b_op_start = curwin->w_cursor;
7004#ifdef FEAT_MBYTE
7005 if (has_mbyte)
7006 {
7007 int old_State = State;
7008
7009 if (cap->ncharC1 != 0)
7010 AppendCharToRedobuff(cap->ncharC1);
7011 if (cap->ncharC2 != 0)
7012 AppendCharToRedobuff(cap->ncharC2);
7013
7014 /* This is slow, but it handles replacing a single-byte with a
7015 * multi-byte and the other way around. Also handles adding
7016 * composing characters for utf-8. */
7017 for (n = cap->count1; n > 0; --n)
7018 {
7019 State = REPLACE;
7020 ins_char(cap->nchar);
7021 State = old_State;
7022 if (cap->ncharC1 != 0)
7023 ins_char(cap->ncharC1);
7024 if (cap->ncharC2 != 0)
7025 ins_char(cap->ncharC2);
7026 }
7027 }
7028 else
7029#endif
7030 {
7031 /*
7032 * Replace the characters within one line.
7033 */
7034 for (n = cap->count1; n > 0; --n)
7035 {
7036 /*
7037 * Get ptr again, because u_save and/or showmatch() will have
7038 * released the line. At the same time we let know that the
7039 * line will be changed.
7040 */
7041 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7042 ptr[curwin->w_cursor.col] = cap->nchar;
7043 if (p_sm && msg_silent == 0)
7044 showmatch(cap->nchar);
7045 ++curwin->w_cursor.col;
7046 }
7047#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007048 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007050 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar009b2592004-10-24 19:18:58 +00007052 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007053 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007055 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 }
7057#endif
7058
7059 /* mark the buffer as changed and prepare for displaying */
7060 changed_bytes(curwin->w_cursor.lnum,
7061 (colnr_T)(curwin->w_cursor.col - cap->count1));
7062 }
7063 --curwin->w_cursor.col; /* cursor on the last replaced char */
7064#ifdef FEAT_MBYTE
7065 /* if the character on the left of the current cursor is a multi-byte
7066 * character, move two characters left */
7067 if (has_mbyte)
7068 mb_adjust_cursor();
7069#endif
7070 curbuf->b_op_end = curwin->w_cursor;
7071 curwin->w_set_curswant = TRUE;
7072 set_last_insert(cap->nchar);
7073 }
7074}
7075
7076#ifdef FEAT_VISUAL
7077/*
7078 * 'o': Exchange start and end of Visual area.
7079 * 'O': same, but in block mode exchange left and right corners.
7080 */
7081 static void
7082v_swap_corners(cmdchar)
7083 int cmdchar;
7084{
7085 pos_T old_cursor;
7086 colnr_T left, right;
7087
7088 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7089 {
7090 old_cursor = curwin->w_cursor;
7091 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7092 curwin->w_cursor.lnum = VIsual.lnum;
7093 coladvance(left);
7094 VIsual = curwin->w_cursor;
7095
7096 curwin->w_cursor.lnum = old_cursor.lnum;
7097 curwin->w_curswant = right;
7098 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7099 * right one column */
7100 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7101 ++curwin->w_curswant;
7102 coladvance(curwin->w_curswant);
7103 if (curwin->w_cursor.col == old_cursor.col
7104#ifdef FEAT_VIRTUALEDIT
7105 && (!virtual_active()
7106 || curwin->w_cursor.coladd == old_cursor.coladd)
7107#endif
7108 )
7109 {
7110 curwin->w_cursor.lnum = VIsual.lnum;
7111 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7112 ++right;
7113 coladvance(right);
7114 VIsual = curwin->w_cursor;
7115
7116 curwin->w_cursor.lnum = old_cursor.lnum;
7117 coladvance(left);
7118 curwin->w_curswant = left;
7119 }
7120 }
7121 else
7122 {
7123 old_cursor = curwin->w_cursor;
7124 curwin->w_cursor = VIsual;
7125 VIsual = old_cursor;
7126 curwin->w_set_curswant = TRUE;
7127 }
7128}
7129#endif /* FEAT_VISUAL */
7130
7131/*
7132 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7133 */
7134 static void
7135nv_Replace(cap)
7136 cmdarg_T *cap;
7137{
7138#ifdef FEAT_VISUAL
7139 if (VIsual_active) /* "R" is replace lines */
7140 {
7141 cap->cmdchar = 'c';
7142 cap->nchar = NUL;
7143 VIsual_mode = 'V';
7144 nv_operator(cap);
7145 }
7146 else
7147#endif
7148 if (!checkclearopq(cap->oap))
7149 {
7150 if (!curbuf->b_p_ma)
7151 EMSG(_(e_modifiable));
7152 else
7153 {
7154#ifdef FEAT_VIRTUALEDIT
7155 if (virtual_active())
7156 coladvance(getviscol());
7157#endif
7158 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7159 }
7160 }
7161}
7162
7163#ifdef FEAT_VREPLACE
7164/*
7165 * "gr".
7166 */
7167 static void
7168nv_vreplace(cap)
7169 cmdarg_T *cap;
7170{
7171# ifdef FEAT_VISUAL
7172 if (VIsual_active)
7173 {
7174 cap->cmdchar = 'r';
7175 cap->nchar = cap->extra_char;
7176 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7177 }
7178 else
7179# endif
7180 if (!checkclearopq(cap->oap))
7181 {
7182 if (!curbuf->b_p_ma)
7183 EMSG(_(e_modifiable));
7184 else
7185 {
7186 if (cap->extra_char == Ctrl_V) /* get another character */
7187 cap->extra_char = get_literal();
7188 stuffcharReadbuff(cap->extra_char);
7189 stuffcharReadbuff(ESC);
7190# ifdef FEAT_VIRTUALEDIT
7191 if (virtual_active())
7192 coladvance(getviscol());
7193# endif
7194 invoke_edit(cap, TRUE, 'v', FALSE);
7195 }
7196 }
7197}
7198#endif
7199
7200/*
7201 * Swap case for "~" command, when it does not work like an operator.
7202 */
7203 static void
7204n_swapchar(cap)
7205 cmdarg_T *cap;
7206{
7207 long n;
7208 pos_T startpos;
7209 int did_change = 0;
7210#ifdef FEAT_NETBEANS_INTG
7211 pos_T pos;
7212 char_u *ptr;
7213 int count;
7214#endif
7215
7216 if (checkclearopq(cap->oap))
7217 return;
7218
7219 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7220 {
7221 clearopbeep(cap->oap);
7222 return;
7223 }
7224
7225 prep_redo_cmd(cap);
7226
7227 if (u_save_cursor() == FAIL)
7228 return;
7229
7230 startpos = curwin->w_cursor;
7231#ifdef FEAT_NETBEANS_INTG
7232 pos = startpos;
7233#endif
7234 for (n = cap->count1; n > 0; --n)
7235 {
7236 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7237 inc_cursor();
7238 if (gchar_cursor() == NUL)
7239 {
7240 if (vim_strchr(p_ww, '~') != NULL
7241 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7242 {
7243#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007244 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
7246 if (did_change)
7247 {
7248 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007249 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007250 netbeans_removed(curbuf, pos.lnum, pos.col,
7251 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007253 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 pos.col = 0;
7256 pos.lnum++;
7257 }
7258#endif
7259 ++curwin->w_cursor.lnum;
7260 curwin->w_cursor.col = 0;
7261 if (n > 1)
7262 {
7263 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7264 break;
7265 u_clearline();
7266 }
7267 }
7268 else
7269 break;
7270 }
7271 }
7272#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007273 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
7275 ptr = ml_get(pos.lnum);
7276 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007277 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7278 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
7280#endif
7281
7282
7283 check_cursor();
7284 curwin->w_set_curswant = TRUE;
7285 if (did_change)
7286 {
7287 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7288 0L);
7289 curbuf->b_op_start = startpos;
7290 curbuf->b_op_end = curwin->w_cursor;
7291 if (curbuf->b_op_end.col > 0)
7292 --curbuf->b_op_end.col;
7293 }
7294}
7295
7296/*
7297 * Move cursor to mark.
7298 */
7299 static void
7300nv_cursormark(cap, flag, pos)
7301 cmdarg_T *cap;
7302 int flag;
7303 pos_T *pos;
7304{
7305 if (check_mark(pos) == FAIL)
7306 clearop(cap->oap);
7307 else
7308 {
7309 if (cap->cmdchar == '\''
7310 || cap->cmdchar == '`'
7311 || cap->cmdchar == '['
7312 || cap->cmdchar == ']')
7313 setpcmark();
7314 curwin->w_cursor = *pos;
7315 if (flag)
7316 beginline(BL_WHITE | BL_FIX);
7317 else
7318 check_cursor();
7319 }
7320 cap->oap->motion_type = flag ? MLINE : MCHAR;
7321 if (cap->cmdchar == '`')
7322 cap->oap->use_reg_one = TRUE;
7323 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7324 curwin->w_set_curswant = TRUE;
7325}
7326
7327#ifdef FEAT_VISUAL
7328/*
7329 * Handle commands that are operators in Visual mode.
7330 */
7331 static void
7332v_visop(cap)
7333 cmdarg_T *cap;
7334{
7335 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7336
7337 /* Uppercase means linewise, except in block mode, then "D" deletes till
7338 * the end of the line, and "C" replaces til EOL */
7339 if (isupper(cap->cmdchar))
7340 {
7341 if (VIsual_mode != Ctrl_V)
7342 VIsual_mode = 'V';
7343 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7344 curwin->w_curswant = MAXCOL;
7345 }
7346 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7347 nv_operator(cap);
7348}
7349#endif
7350
7351/*
7352 * "s" and "S" commands.
7353 */
7354 static void
7355nv_subst(cap)
7356 cmdarg_T *cap;
7357{
7358#ifdef FEAT_VISUAL
7359 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7360 {
7361 if (cap->cmdchar == 'S')
7362 VIsual_mode = 'V';
7363 cap->cmdchar = 'c';
7364 nv_operator(cap);
7365 }
7366 else
7367#endif
7368 nv_optrans(cap);
7369}
7370
7371/*
7372 * Abbreviated commands.
7373 */
7374 static void
7375nv_abbrev(cap)
7376 cmdarg_T *cap;
7377{
7378 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7379 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7380
7381#ifdef FEAT_VISUAL
7382 /* in Visual mode these commands are operators */
7383 if (VIsual_active)
7384 v_visop(cap);
7385 else
7386#endif
7387 nv_optrans(cap);
7388}
7389
7390/*
7391 * Translate a command into another command.
7392 */
7393 static void
7394nv_optrans(cap)
7395 cmdarg_T *cap;
7396{
7397 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7398 (char_u *)"d$", (char_u *)"c$",
7399 (char_u *)"cl", (char_u *)"cc",
7400 (char_u *)"yy", (char_u *)":s\r"};
7401 static char_u *str = (char_u *)"xXDCsSY&";
7402
7403 if (!checkclearopq(cap->oap))
7404 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007405 /* In Vi "2D" doesn't delete the next line. Can't translate it
7406 * either, because "2." should also not use the count. */
7407 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7408 {
7409 cap->oap->start = curwin->w_cursor;
7410 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007411#ifdef FEAT_EVAL
7412 set_op_var(OP_DELETE);
7413#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007414 cap->count1 = 1;
7415 nv_dollar(cap);
7416 finish_op = TRUE;
7417 ResetRedobuff();
7418 AppendCharToRedobuff('D');
7419 }
7420 else
7421 {
7422 if (cap->count0)
7423 stuffnumReadbuff(cap->count0);
7424 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 cap->opcount = 0;
7428}
7429
7430/*
7431 * "'" and "`" commands. Also for "g'" and "g`".
7432 * cap->arg is TRUE for "'" and "g'".
7433 */
7434 static void
7435nv_gomark(cap)
7436 cmdarg_T *cap;
7437{
7438 pos_T *pos;
7439 int c;
7440#ifdef FEAT_FOLDING
7441 linenr_T lnum = curwin->w_cursor.lnum;
7442 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7443#endif
7444
7445 if (cap->cmdchar == 'g')
7446 c = cap->extra_char;
7447 else
7448 c = cap->nchar;
7449 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7450 if (pos == (pos_T *)-1) /* jumped to other file */
7451 {
7452 if (cap->arg)
7453 {
7454 check_cursor_lnum();
7455 beginline(BL_WHITE | BL_FIX);
7456 }
7457 else
7458 check_cursor();
7459 }
7460 else
7461 nv_cursormark(cap, cap->arg, pos);
7462
7463#ifdef FEAT_VIRTUALEDIT
7464 /* May need to clear the coladd that a mark includes. */
7465 if (!virtual_active())
7466 curwin->w_cursor.coladd = 0;
7467#endif
7468#ifdef FEAT_FOLDING
7469 if (cap->oap->op_type == OP_NOP
7470 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7471 && (fdo_flags & FDO_MARK)
7472 && old_KeyTyped)
7473 foldOpenCursor();
7474#endif
7475}
7476
7477/*
7478 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7479 */
7480 static void
7481nv_pcmark(cap)
7482 cmdarg_T *cap;
7483{
7484#ifdef FEAT_JUMPLIST
7485 pos_T *pos;
7486# ifdef FEAT_FOLDING
7487 linenr_T lnum = curwin->w_cursor.lnum;
7488 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7489# endif
7490
7491 if (!checkclearopq(cap->oap))
7492 {
7493 if (cap->cmdchar == 'g')
7494 pos = movechangelist((int)cap->count1);
7495 else
7496 pos = movemark((int)cap->count1);
7497 if (pos == (pos_T *)-1) /* jump to other file */
7498 {
7499 curwin->w_set_curswant = TRUE;
7500 check_cursor();
7501 }
7502 else if (pos != NULL) /* can jump */
7503 nv_cursormark(cap, FALSE, pos);
7504 else if (cap->cmdchar == 'g')
7505 {
7506 if (curbuf->b_changelistlen == 0)
7507 EMSG(_("E664: changelist is empty"));
7508 else if (cap->count1 < 0)
7509 EMSG(_("E662: At start of changelist"));
7510 else
7511 EMSG(_("E663: At end of changelist"));
7512 }
7513 else
7514 clearopbeep(cap->oap);
7515# ifdef FEAT_FOLDING
7516 if (cap->oap->op_type == OP_NOP
7517 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7518 && (fdo_flags & FDO_MARK)
7519 && old_KeyTyped)
7520 foldOpenCursor();
7521# endif
7522 }
7523#else
7524 clearopbeep(cap->oap);
7525#endif
7526}
7527
7528/*
7529 * Handle '"' command.
7530 */
7531 static void
7532nv_regname(cap)
7533 cmdarg_T *cap;
7534{
7535 if (checkclearop(cap->oap))
7536 return;
7537#ifdef FEAT_EVAL
7538 if (cap->nchar == '=')
7539 cap->nchar = get_expr_register();
7540#endif
7541 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7542 {
7543 cap->oap->regname = cap->nchar;
7544 cap->opcount = cap->count0; /* remember count before '"' */
7545#ifdef FEAT_EVAL
7546 set_reg_var(cap->oap->regname);
7547#endif
7548 }
7549 else
7550 clearopbeep(cap->oap);
7551}
7552
7553#ifdef FEAT_VISUAL
7554/*
7555 * Handle "v", "V" and "CTRL-V" commands.
7556 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7557 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 */
7560 static void
7561nv_visual(cap)
7562 cmdarg_T *cap;
7563{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007564 if (cap->cmdchar == Ctrl_Q)
7565 cap->cmdchar = Ctrl_V;
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7568 * characterwise, linewise, or blockwise. */
7569 if (cap->oap->op_type != OP_NOP)
7570 {
7571 cap->oap->motion_force = cap->cmdchar;
7572 finish_op = FALSE; /* operator doesn't finish now but later */
7573 return;
7574 }
7575
7576 VIsual_select = cap->arg;
7577 if (VIsual_active) /* change Visual mode */
7578 {
7579 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7580 end_visual_mode();
7581 else /* toggle char/block mode */
7582 { /* or char/line mode */
7583 VIsual_mode = cap->cmdchar;
7584 showmode();
7585 }
7586 redraw_curbuf_later(INVERTED); /* update the inversion */
7587 }
7588 else /* start Visual mode */
7589 {
7590 check_visual_highlight();
7591 if (cap->count0) /* use previously selected part */
7592 {
7593 if (resel_VIsual_mode == NUL) /* there is none */
7594 {
7595 beep_flush();
7596 return;
7597 }
7598 VIsual = curwin->w_cursor;
7599
7600 VIsual_active = TRUE;
7601 VIsual_reselect = TRUE;
7602 if (!cap->arg)
7603 /* start Select mode when 'selectmode' contains "cmd" */
7604 may_start_select('c');
7605#ifdef FEAT_MOUSE
7606 setmouse();
7607#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007608 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 redraw_cmdline = TRUE; /* show visual mode later */
7610 /*
7611 * For V and ^V, we multiply the number of lines even if there
7612 * was only one -- webb
7613 */
7614 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7615 {
7616 curwin->w_cursor.lnum +=
7617 resel_VIsual_line_count * cap->count0 - 1;
7618 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7619 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7620 }
7621 VIsual_mode = resel_VIsual_mode;
7622 if (VIsual_mode == 'v')
7623 {
7624 if (resel_VIsual_line_count <= 1)
7625 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7626 else
7627 curwin->w_cursor.col = resel_VIsual_col;
7628 check_cursor_col();
7629 }
7630 if (resel_VIsual_col == MAXCOL)
7631 {
7632 curwin->w_curswant = MAXCOL;
7633 coladvance((colnr_T)MAXCOL);
7634 }
7635 else if (VIsual_mode == Ctrl_V)
7636 {
7637 validate_virtcol();
7638 curwin->w_curswant = curwin->w_virtcol
7639 + resel_VIsual_col * cap->count0 - 1;
7640 coladvance(curwin->w_curswant);
7641 }
7642 else
7643 curwin->w_set_curswant = TRUE;
7644 redraw_curbuf_later(INVERTED); /* show the inversion */
7645 }
7646 else
7647 {
7648 if (!cap->arg)
7649 /* start Select mode when 'selectmode' contains "cmd" */
7650 may_start_select('c');
7651 n_start_visual_mode(cap->cmdchar);
7652 }
7653 }
7654}
7655
7656/*
7657 * Start selection for Shift-movement keys.
7658 */
7659 void
7660start_selection()
7661{
7662 /* if 'selectmode' contains "key", start Select mode */
7663 may_start_select('k');
7664 n_start_visual_mode('v');
7665}
7666
7667/*
7668 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7669 */
7670 void
7671may_start_select(c)
7672 int c;
7673{
7674 VIsual_select = (stuff_empty() && typebuf_typed()
7675 && (vim_strchr(p_slm, c) != NULL));
7676}
7677
7678/*
7679 * Start Visual mode "c".
7680 * Should set VIsual_select before calling this.
7681 */
7682 static void
7683n_start_visual_mode(c)
7684 int c;
7685{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007686#ifdef FEAT_CONCEAL
7687 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007688 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007689#endif
7690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 VIsual_mode = c;
7692 VIsual_active = TRUE;
7693 VIsual_reselect = TRUE;
7694#ifdef FEAT_VIRTUALEDIT
7695 /* Corner case: the 0 position in a tab may change when going into
7696 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7697 */
7698 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7699 coladvance(curwin->w_virtcol);
7700#endif
7701 VIsual = curwin->w_cursor;
7702
7703#ifdef FEAT_FOLDING
7704 foldAdjustVisual();
7705#endif
7706
7707#ifdef FEAT_MOUSE
7708 setmouse();
7709#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007710#ifdef FEAT_CONCEAL
7711 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007712 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007713#endif
7714
Bram Moolenaar09df3122006-01-23 22:23:09 +00007715 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 redraw_cmdline = TRUE; /* show visual mode later */
7717#ifdef FEAT_CLIPBOARD
7718 /* Make sure the clipboard gets updated. Needed because start and
7719 * end may still be the same, and the selection needs to be owned */
7720 clip_star.vmode = NUL;
7721#endif
7722
7723 /* Only need to redraw this line, unless still need to redraw an old
7724 * Visual area (when 'lazyredraw' is set). */
7725 if (curwin->w_redr_type < INVERTED)
7726 {
7727 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7728 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7729 }
7730}
7731
7732#endif /* FEAT_VISUAL */
7733
7734/*
7735 * CTRL-W: Window commands
7736 */
7737 static void
7738nv_window(cap)
7739 cmdarg_T *cap;
7740{
7741#ifdef FEAT_WINDOWS
7742 if (!checkclearop(cap->oap))
7743 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7744#else
7745 (void)checkclearop(cap->oap);
7746#endif
7747}
7748
7749/*
7750 * CTRL-Z: Suspend
7751 */
7752 static void
7753nv_suspend(cap)
7754 cmdarg_T *cap;
7755{
7756 clearop(cap->oap);
7757#ifdef FEAT_VISUAL
7758 if (VIsual_active)
7759 end_visual_mode(); /* stop Visual mode */
7760#endif
7761 do_cmdline_cmd((char_u *)"st");
7762}
7763
7764/*
7765 * Commands starting with "g".
7766 */
7767 static void
7768nv_g_cmd(cap)
7769 cmdarg_T *cap;
7770{
7771 oparg_T *oap = cap->oap;
7772#ifdef FEAT_VISUAL
7773 pos_T tpos;
7774#endif
7775 int i;
7776 int flag = FALSE;
7777
7778 switch (cap->nchar)
7779 {
7780#ifdef MEM_PROFILE
7781 /*
7782 * "g^A": dump log of used memory.
7783 */
7784 case Ctrl_A:
7785 vim_mem_profile_dump();
7786 break;
7787#endif
7788
7789#ifdef FEAT_VREPLACE
7790 /*
7791 * "gR": Enter virtual replace mode.
7792 */
7793 case 'R':
7794 cap->arg = TRUE;
7795 nv_Replace(cap);
7796 break;
7797
7798 case 'r':
7799 nv_vreplace(cap);
7800 break;
7801#endif
7802
7803 case '&':
7804 do_cmdline_cmd((char_u *)"%s//~/&");
7805 break;
7806
7807#ifdef FEAT_VISUAL
7808 /*
7809 * "gv": Reselect the previous Visual area. If Visual already active,
7810 * exchange previous and current Visual area.
7811 */
7812 case 'v':
7813 if (checkclearop(oap))
7814 break;
7815
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007816 if ( curbuf->b_visual.vi_start.lnum == 0
7817 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7818 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 beep_flush();
7820 else
7821 {
7822 /* set w_cursor to the start of the Visual area, tpos to the end */
7823 if (VIsual_active)
7824 {
7825 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007826 VIsual_mode = curbuf->b_visual.vi_mode;
7827 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828# ifdef FEAT_EVAL
7829 curbuf->b_visual_mode_eval = i;
7830# endif
7831 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007832 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7833 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007835 tpos = curbuf->b_visual.vi_end;
7836 curbuf->b_visual.vi_end = curwin->w_cursor;
7837 curwin->w_cursor = curbuf->b_visual.vi_start;
7838 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840 else
7841 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007842 VIsual_mode = curbuf->b_visual.vi_mode;
7843 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7844 tpos = curbuf->b_visual.vi_end;
7845 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847
7848 VIsual_active = TRUE;
7849 VIsual_reselect = TRUE;
7850
7851 /* Set Visual to the start and w_cursor to the end of the Visual
7852 * area. Make sure they are on an existing character. */
7853 check_cursor();
7854 VIsual = curwin->w_cursor;
7855 curwin->w_cursor = tpos;
7856 check_cursor();
7857 update_topline();
7858 /*
7859 * When called from normal "g" command: start Select mode when
7860 * 'selectmode' contains "cmd". When called for K_SELECT, always
7861 * start Select mode.
7862 */
7863 if (cap->arg)
7864 VIsual_select = TRUE;
7865 else
7866 may_start_select('c');
7867#ifdef FEAT_MOUSE
7868 setmouse();
7869#endif
7870#ifdef FEAT_CLIPBOARD
7871 /* Make sure the clipboard gets updated. Needed because start and
7872 * end are still the same, and the selection needs to be owned */
7873 clip_star.vmode = NUL;
7874#endif
7875 redraw_curbuf_later(INVERTED);
7876 showmode();
7877 }
7878 break;
7879 /*
7880 * "gV": Don't reselect the previous Visual area after a Select mode
7881 * mapping of menu.
7882 */
7883 case 'V':
7884 VIsual_reselect = FALSE;
7885 break;
7886
7887 /*
7888 * "gh": start Select mode.
7889 * "gH": start Select line mode.
7890 * "g^H": start Select block mode.
7891 */
7892 case K_BS:
7893 cap->nchar = Ctrl_H;
7894 /* FALLTHROUGH */
7895 case 'h':
7896 case 'H':
7897 case Ctrl_H:
7898# ifdef EBCDIC
7899 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7900 if (cap->nchar == Ctrl_H)
7901 cap->cmdchar = Ctrl_V;
7902 else
7903# endif
7904 cap->cmdchar = cap->nchar + ('v' - 'h');
7905 cap->arg = TRUE;
7906 nv_visual(cap);
7907 break;
7908#endif /* FEAT_VISUAL */
7909
7910 /*
7911 * "gj" and "gk" two new funny movement keys -- up and down
7912 * movement based on *screen* line rather than *file* line.
7913 */
7914 case 'j':
7915 case K_DOWN:
7916 /* with 'nowrap' it works just like the normal "j" command; also when
7917 * in a closed fold */
7918 if (!curwin->w_p_wrap
7919#ifdef FEAT_FOLDING
7920 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7921#endif
7922 )
7923 {
7924 oap->motion_type = MLINE;
7925 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7926 }
7927 else
7928 i = nv_screengo(oap, FORWARD, cap->count1);
7929 if (i == FAIL)
7930 clearopbeep(oap);
7931 break;
7932
7933 case 'k':
7934 case K_UP:
7935 /* with 'nowrap' it works just like the normal "k" command; also when
7936 * in a closed fold */
7937 if (!curwin->w_p_wrap
7938#ifdef FEAT_FOLDING
7939 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7940#endif
7941 )
7942 {
7943 oap->motion_type = MLINE;
7944 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7945 }
7946 else
7947 i = nv_screengo(oap, BACKWARD, cap->count1);
7948 if (i == FAIL)
7949 clearopbeep(oap);
7950 break;
7951
7952 /*
7953 * "gJ": join two lines without inserting a space.
7954 */
7955 case 'J':
7956 nv_join(cap);
7957 break;
7958
7959 /*
7960 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7961 * "gm": middle of "g0" and "g$".
7962 */
7963 case '^':
7964 flag = TRUE;
7965 /* FALLTHROUGH */
7966
7967 case '0':
7968 case 'm':
7969 case K_HOME:
7970 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 oap->motion_type = MCHAR;
7972 oap->inclusive = FALSE;
7973 if (curwin->w_p_wrap
7974#ifdef FEAT_VERTSPLIT
7975 && curwin->w_width != 0
7976#endif
7977 )
7978 {
7979 int width1 = W_WIDTH(curwin) - curwin_col_off();
7980 int width2 = width1 + curwin_col_off2();
7981
7982 validate_virtcol();
7983 i = 0;
7984 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7985 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7986 }
7987 else
7988 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007989 /* Go to the middle of the screen line. When 'number' or
7990 * 'relativenumber' is on and lines are wrapping the middle can be more
7991 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (cap->nchar == 'm')
7993 i += (W_WIDTH(curwin) - curwin_col_off()
7994 + ((curwin->w_p_wrap && i > 0)
7995 ? curwin_col_off2() : 0)) / 2;
7996 coladvance((colnr_T)i);
7997 if (flag)
7998 {
7999 do
8000 i = gchar_cursor();
8001 while (vim_iswhite(i) && oneright() == OK);
8002 }
8003 curwin->w_set_curswant = TRUE;
8004 break;
8005
8006 case '_':
8007 /* "g_": to the last non-blank character in the line or <count> lines
8008 * downward. */
8009 cap->oap->motion_type = MCHAR;
8010 cap->oap->inclusive = TRUE;
8011 curwin->w_curswant = MAXCOL;
8012 if (cursor_down((long)(cap->count1 - 1),
8013 cap->oap->op_type == OP_NOP) == FAIL)
8014 clearopbeep(cap->oap);
8015 else
8016 {
8017 char_u *ptr = ml_get_curline();
8018
8019 /* In Visual mode we may end up after the line. */
8020 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8021 --curwin->w_cursor.col;
8022
8023 /* Decrease the cursor column until it's on a non-blank. */
8024 while (curwin->w_cursor.col > 0
8025 && vim_iswhite(ptr[curwin->w_cursor.col]))
8026 --curwin->w_cursor.col;
8027 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008028#ifdef FEAT_VISUAL
8029 adjust_for_sel(cap);
8030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 }
8032 break;
8033
8034 case '$':
8035 case K_END:
8036 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {
8038 int col_off = curwin_col_off();
8039
8040 oap->motion_type = MCHAR;
8041 oap->inclusive = TRUE;
8042 if (curwin->w_p_wrap
8043#ifdef FEAT_VERTSPLIT
8044 && curwin->w_width != 0
8045#endif
8046 )
8047 {
8048 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8049 if (cap->count1 == 1)
8050 {
8051 int width1 = W_WIDTH(curwin) - col_off;
8052 int width2 = width1 + curwin_col_off2();
8053
8054 validate_virtcol();
8055 i = width1 - 1;
8056 if (curwin->w_virtcol >= (colnr_T)width1)
8057 i += ((curwin->w_virtcol - width1) / width2 + 1)
8058 * width2;
8059 coladvance((colnr_T)i);
8060#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8061 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8062 {
8063 /*
8064 * Check for landing on a character that got split at
8065 * the end of the line. We do not want to advance to
8066 * the next screen line.
8067 */
8068 validate_virtcol();
8069 if (curwin->w_virtcol > (colnr_T)i)
8070 --curwin->w_cursor.col;
8071 }
8072#endif
8073 }
8074 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8075 clearopbeep(oap);
8076 }
8077 else
8078 {
8079 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8080 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008081
8082 /* Make sure we stick in this column. */
8083 validate_virtcol();
8084 curwin->w_curswant = curwin->w_virtcol;
8085 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 }
8087 }
8088 break;
8089
8090 /*
8091 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8092 */
8093 case '*':
8094 case '#':
8095#if POUND != '#'
8096 case POUND: /* pound sign (sometimes equal to '#') */
8097#endif
8098 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8099 case ']': /* :tselect for current identifier */
8100 nv_ident(cap);
8101 break;
8102
8103 /*
8104 * ge and gE: go back to end of word
8105 */
8106 case 'e':
8107 case 'E':
8108 oap->motion_type = MCHAR;
8109 curwin->w_set_curswant = TRUE;
8110 oap->inclusive = TRUE;
8111 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8112 clearopbeep(oap);
8113 break;
8114
8115 /*
8116 * "g CTRL-G": display info about cursor position
8117 */
8118 case Ctrl_G:
8119 cursor_pos_info();
8120 break;
8121
8122 /*
8123 * "gi": start Insert at the last position.
8124 */
8125 case 'i':
8126 if (curbuf->b_last_insert.lnum != 0)
8127 {
8128 curwin->w_cursor = curbuf->b_last_insert;
8129 check_cursor_lnum();
8130 i = (int)STRLEN(ml_get_curline());
8131 if (curwin->w_cursor.col > (colnr_T)i)
8132 {
8133#ifdef FEAT_VIRTUALEDIT
8134 if (virtual_active())
8135 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8136#endif
8137 curwin->w_cursor.col = i;
8138 }
8139 }
8140 cap->cmdchar = 'i';
8141 nv_edit(cap);
8142 break;
8143
8144 /*
8145 * "gI": Start insert in column 1.
8146 */
8147 case 'I':
8148 beginline(0);
8149 if (!checkclearopq(oap))
8150 invoke_edit(cap, FALSE, 'g', FALSE);
8151 break;
8152
8153#ifdef FEAT_SEARCHPATH
8154 /*
8155 * "gf": goto file, edit file under cursor
8156 * "]f" and "[f": can also be used.
8157 */
8158 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008159 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 nv_gotofile(cap);
8161 break;
8162#endif
8163
8164 /* "g'm" and "g`m": jump to mark without setting pcmark */
8165 case '\'':
8166 cap->arg = TRUE;
8167 /*FALLTHROUGH*/
8168 case '`':
8169 nv_gomark(cap);
8170 break;
8171
8172 /*
8173 * "gs": Goto sleep.
8174 */
8175 case 's':
8176 do_sleep(cap->count1 * 1000L);
8177 break;
8178
8179 /*
8180 * "ga": Display the ascii value of the character under the
8181 * cursor. It is displayed in decimal, hex, and octal. -- webb
8182 */
8183 case 'a':
8184 do_ascii(NULL);
8185 break;
8186
8187#ifdef FEAT_MBYTE
8188 /*
8189 * "g8": Display the bytes used for the UTF-8 character under the
8190 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008191 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 */
8193 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008194 if (cap->count0 == 8)
8195 utf_find_illegal();
8196 else
8197 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 break;
8199#endif
8200
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008201 case '<':
8202 show_sb_text();
8203 break;
8204
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 /*
8206 * "gg": Goto the first line in file. With a count it goes to
8207 * that line number like for "G". -- webb
8208 */
8209 case 'g':
8210 cap->arg = FALSE;
8211 nv_goto(cap);
8212 break;
8213
8214 /*
8215 * Two-character operators:
8216 * "gq" Format text
8217 * "gw" Format text and keep cursor position
8218 * "g~" Toggle the case of the text.
8219 * "gu" Change text to lower case.
8220 * "gU" Change text to upper case.
8221 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008222 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 */
8224 case 'q':
8225 case 'w':
8226 oap->cursor_start = curwin->w_cursor;
8227 /*FALLTHROUGH*/
8228 case '~':
8229 case 'u':
8230 case 'U':
8231 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008232 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 nv_operator(cap);
8234 break;
8235
8236 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008237 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 * current function
8239 * "gD": idem, but in the current file.
8240 */
8241 case 'd':
8242 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008243 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 break;
8245
8246#ifdef FEAT_MOUSE
8247 /*
8248 * g<*Mouse> : <C-*mouse>
8249 */
8250 case K_MIDDLEMOUSE:
8251 case K_MIDDLEDRAG:
8252 case K_MIDDLERELEASE:
8253 case K_LEFTMOUSE:
8254 case K_LEFTDRAG:
8255 case K_LEFTRELEASE:
8256 case K_RIGHTMOUSE:
8257 case K_RIGHTDRAG:
8258 case K_RIGHTRELEASE:
8259 case K_X1MOUSE:
8260 case K_X1DRAG:
8261 case K_X1RELEASE:
8262 case K_X2MOUSE:
8263 case K_X2DRAG:
8264 case K_X2RELEASE:
8265 mod_mask = MOD_MASK_CTRL;
8266 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8267 break;
8268#endif
8269
8270 case K_IGNORE:
8271 break;
8272
8273 /*
8274 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8275 */
8276 case 'p':
8277 case 'P':
8278 nv_put(cap);
8279 break;
8280
8281#ifdef FEAT_BYTEOFF
8282 /* "go": goto byte count from start of buffer */
8283 case 'o':
8284 goto_byte(cap->count0);
8285 break;
8286#endif
8287
8288 /* "gQ": improved Ex mode */
8289 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008290 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
8292 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008293 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 break;
8295 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008296
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (!checkclearopq(oap))
8298 do_exmode(TRUE);
8299 break;
8300
8301#ifdef FEAT_JUMPLIST
8302 case ',':
8303 nv_pcmark(cap);
8304 break;
8305
8306 case ';':
8307 cap->count1 = -cap->count1;
8308 nv_pcmark(cap);
8309 break;
8310#endif
8311
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008312#ifdef FEAT_WINDOWS
8313 case 't':
8314 goto_tabpage((int)cap->count0);
8315 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008316 case 'T':
8317 goto_tabpage(-(int)cap->count1);
8318 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008319#endif
8320
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008321 case '+':
8322 case '-': /* "g+" and "g-": undo or redo along the timeline */
8323 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008324 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008325 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008326 break;
8327
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 default:
8329 clearopbeep(oap);
8330 break;
8331 }
8332}
8333
8334/*
8335 * Handle "o" and "O" commands.
8336 */
8337 static void
8338n_opencmd(cap)
8339 cmdarg_T *cap;
8340{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008341#ifdef FEAT_CONCEAL
8342 linenr_T oldline = curwin->w_cursor.lnum;
8343#endif
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 if (!checkclearopq(cap->oap))
8346 {
8347#ifdef FEAT_FOLDING
8348 if (cap->cmdchar == 'O')
8349 /* Open above the first line of a folded sequence of lines */
8350 (void)hasFolding(curwin->w_cursor.lnum,
8351 &curwin->w_cursor.lnum, NULL);
8352 else
8353 /* Open below the last line of a folded sequence of lines */
8354 (void)hasFolding(curwin->w_cursor.lnum,
8355 NULL, &curwin->w_cursor.lnum);
8356#endif
8357 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8358 (cap->cmdchar == 'O' ? 1 : 0)),
8359 (linenr_T)(curwin->w_cursor.lnum +
8360 (cap->cmdchar == 'o' ? 1 : 0))
8361 ) == OK
8362 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8363#ifdef FEAT_COMMENTS
8364 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8365#endif
8366 0, 0))
8367 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008368#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008369 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008370 update_single_line(curwin, oldline);
8371#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008372 /* When '#' is in 'cpoptions' ignore the count. */
8373 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8374 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8376 }
8377 }
8378}
8379
8380/*
8381 * "." command: redo last change.
8382 */
8383 static void
8384nv_dot(cap)
8385 cmdarg_T *cap;
8386{
8387 if (!checkclearopq(cap->oap))
8388 {
8389 /*
8390 * If "restart_edit" is TRUE, the last but one command is repeated
8391 * instead of the last command (inserting text). This is used for
8392 * CTRL-O <.> in insert mode.
8393 */
8394 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8395 clearopbeep(cap->oap);
8396 }
8397}
8398
8399/*
8400 * CTRL-R: undo undo
8401 */
8402 static void
8403nv_redo(cap)
8404 cmdarg_T *cap;
8405{
8406 if (!checkclearopq(cap->oap))
8407 {
8408 u_redo((int)cap->count1);
8409 curwin->w_set_curswant = TRUE;
8410 }
8411}
8412
8413/*
8414 * Handle "U" command.
8415 */
8416 static void
8417nv_Undo(cap)
8418 cmdarg_T *cap;
8419{
8420 /* In Visual mode and typing "gUU" triggers an operator */
8421 if (cap->oap->op_type == OP_UPPER
8422#ifdef FEAT_VISUAL
8423 || VIsual_active
8424#endif
8425 )
8426 {
8427 /* translate "gUU" to "gUgU" */
8428 cap->cmdchar = 'g';
8429 cap->nchar = 'U';
8430 nv_operator(cap);
8431 }
8432 else if (!checkclearopq(cap->oap))
8433 {
8434 u_undoline();
8435 curwin->w_set_curswant = TRUE;
8436 }
8437}
8438
8439/*
8440 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8441 * single character.
8442 */
8443 static void
8444nv_tilde(cap)
8445 cmdarg_T *cap;
8446{
8447 if (!p_to
8448#ifdef FEAT_VISUAL
8449 && !VIsual_active
8450#endif
8451 && cap->oap->op_type != OP_TILDE)
8452 n_swapchar(cap);
8453 else
8454 nv_operator(cap);
8455}
8456
8457/*
8458 * Handle an operator command.
8459 * The actual work is done by do_pending_operator().
8460 */
8461 static void
8462nv_operator(cap)
8463 cmdarg_T *cap;
8464{
8465 int op_type;
8466
8467 op_type = get_op_type(cap->cmdchar, cap->nchar);
8468
8469 if (op_type == cap->oap->op_type) /* double operator works on lines */
8470 nv_lineop(cap);
8471 else if (!checkclearop(cap->oap))
8472 {
8473 cap->oap->start = curwin->w_cursor;
8474 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008475#ifdef FEAT_EVAL
8476 set_op_var(op_type);
8477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 }
8479}
8480
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008481#ifdef FEAT_EVAL
8482/*
8483 * Set v:operator to the characters for "optype".
8484 */
8485 static void
8486set_op_var(optype)
8487 int optype;
8488{
8489 char_u opchars[3];
8490
8491 if (optype == OP_NOP)
8492 set_vim_var_string(VV_OP, NULL, 0);
8493 else
8494 {
8495 opchars[0] = get_op_char(optype);
8496 opchars[1] = get_extra_op_char(optype);
8497 opchars[2] = NUL;
8498 set_vim_var_string(VV_OP, opchars, -1);
8499 }
8500}
8501#endif
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503/*
8504 * Handle linewise operator "dd", "yy", etc.
8505 *
8506 * "_" is is a strange motion command that helps make operators more logical.
8507 * It is actually implemented, but not documented in the real Vi. This motion
8508 * command actually refers to "the current line". Commands like "dd" and "yy"
8509 * are really an alternate form of "d_" and "y_". It does accept a count, so
8510 * "d3_" works to delete 3 lines.
8511 */
8512 static void
8513nv_lineop(cap)
8514 cmdarg_T *cap;
8515{
8516 cap->oap->motion_type = MLINE;
8517 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8518 clearopbeep(cap->oap);
8519 else if ( cap->oap->op_type == OP_DELETE
8520 || cap->oap->op_type == OP_LSHIFT
8521 || cap->oap->op_type == OP_RSHIFT)
8522 beginline(BL_SOL | BL_FIX);
8523 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8524 beginline(BL_WHITE | BL_FIX);
8525}
8526
8527/*
8528 * <Home> command.
8529 */
8530 static void
8531nv_home(cap)
8532 cmdarg_T *cap;
8533{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008534 /* CTRL-HOME is like "gg" */
8535 if (mod_mask & MOD_MASK_CTRL)
8536 nv_goto(cap);
8537 else
8538 {
8539 cap->count0 = 1;
8540 nv_pipe(cap);
8541 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008542 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8543 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544}
8545
8546/*
8547 * "|" command.
8548 */
8549 static void
8550nv_pipe(cap)
8551 cmdarg_T *cap;
8552{
8553 cap->oap->motion_type = MCHAR;
8554 cap->oap->inclusive = FALSE;
8555 beginline(0);
8556 if (cap->count0 > 0)
8557 {
8558 coladvance((colnr_T)(cap->count0 - 1));
8559 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8560 }
8561 else
8562 curwin->w_curswant = 0;
8563 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008564 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 curwin->w_set_curswant = FALSE;
8566}
8567
8568/*
8569 * Handle back-word command "b" and "B".
8570 * cap->arg is 1 for "B"
8571 */
8572 static void
8573nv_bck_word(cap)
8574 cmdarg_T *cap;
8575{
8576 cap->oap->motion_type = MCHAR;
8577 cap->oap->inclusive = FALSE;
8578 curwin->w_set_curswant = TRUE;
8579 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8580 clearopbeep(cap->oap);
8581#ifdef FEAT_FOLDING
8582 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8583 foldOpenCursor();
8584#endif
8585}
8586
8587/*
8588 * Handle word motion commands "e", "E", "w" and "W".
8589 * cap->arg is TRUE for "E" and "W".
8590 */
8591 static void
8592nv_wordcmd(cap)
8593 cmdarg_T *cap;
8594{
8595 int n;
8596 int word_end;
8597 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008598 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 /*
8601 * Set inclusive for the "E" and "e" command.
8602 */
8603 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8604 word_end = TRUE;
8605 else
8606 word_end = FALSE;
8607 cap->oap->inclusive = word_end;
8608
8609 /*
8610 * "cw" and "cW" are a special case.
8611 */
8612 if (!word_end && cap->oap->op_type == OP_CHANGE)
8613 {
8614 n = gchar_cursor();
8615 if (n != NUL) /* not an empty line */
8616 {
8617 if (vim_iswhite(n))
8618 {
8619 /*
8620 * Reproduce a funny Vi behaviour: "cw" on a blank only
8621 * changes one character, not all blanks until the start of
8622 * the next word. Only do this when the 'w' flag is included
8623 * in 'cpoptions'.
8624 */
8625 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8626 {
8627 cap->oap->inclusive = TRUE;
8628 cap->oap->motion_type = MCHAR;
8629 return;
8630 }
8631 }
8632 else
8633 {
8634 /*
8635 * This is a little strange. To match what the real Vi does,
8636 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8637 * that we are not on a space or a TAB. This seems impolite
8638 * at first, but it's really more what we mean when we say
8639 * 'cw'.
8640 * Another strangeness: When standing on the end of a word
8641 * "ce" will change until the end of the next wordt, but "cw"
8642 * will change only one character! This is done by setting
8643 * flag.
8644 */
8645 cap->oap->inclusive = TRUE;
8646 word_end = TRUE;
8647 flag = TRUE;
8648 }
8649 }
8650 }
8651
8652 cap->oap->motion_type = MCHAR;
8653 curwin->w_set_curswant = TRUE;
8654 if (word_end)
8655 n = end_word(cap->count1, cap->arg, flag, FALSE);
8656 else
8657 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8658
Bram Moolenaardfefb982008-04-01 10:06:39 +00008659 /* Don't leave the cursor on the NUL past the end of line. Unless we
8660 * didn't move it forward. */
8661 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008662 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
8664 if (n == FAIL && cap->oap->op_type == OP_NOP)
8665 clearopbeep(cap->oap);
8666 else
8667 {
8668#ifdef FEAT_VISUAL
8669 adjust_for_sel(cap);
8670#endif
8671#ifdef FEAT_FOLDING
8672 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8673 foldOpenCursor();
8674#endif
8675 }
8676}
8677
8678/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008679 * Used after a movement command: If the cursor ends up on the NUL after the
8680 * end of the line, may move it back to the last character and make the motion
8681 * inclusive.
8682 */
8683 static void
8684adjust_cursor(oap)
8685 oparg_T *oap;
8686{
8687 /* The cursor cannot remain on the NUL when:
8688 * - the column is > 0
8689 * - not in Visual mode or 'selection' is "o"
8690 * - 'virtualedit' is not "all" and not "onemore".
8691 */
8692 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8693#ifdef FEAT_VISUAL
8694 && (!VIsual_active || *p_sel == 'o')
8695#endif
8696#ifdef FEAT_VIRTUALEDIT
8697 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8698#endif
8699 )
8700 {
8701 --curwin->w_cursor.col;
8702#ifdef FEAT_MBYTE
8703 /* prevent cursor from moving on the trail byte */
8704 if (has_mbyte)
8705 mb_adjust_cursor();
8706#endif
8707 oap->inclusive = TRUE;
8708 }
8709}
8710
8711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 * "0" and "^" commands.
8713 * cap->arg is the argument for beginline().
8714 */
8715 static void
8716nv_beginline(cap)
8717 cmdarg_T *cap;
8718{
8719 cap->oap->motion_type = MCHAR;
8720 cap->oap->inclusive = FALSE;
8721 beginline(cap->arg);
8722#ifdef FEAT_FOLDING
8723 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8724 foldOpenCursor();
8725#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008726 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8727 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728}
8729
8730#ifdef FEAT_VISUAL
8731/*
8732 * In exclusive Visual mode, may include the last character.
8733 */
8734 static void
8735adjust_for_sel(cap)
8736 cmdarg_T *cap;
8737{
8738 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8739 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8740 {
8741# ifdef FEAT_MBYTE
8742 if (has_mbyte)
8743 inc_cursor();
8744 else
8745# endif
8746 ++curwin->w_cursor.col;
8747 cap->oap->inclusive = FALSE;
8748 }
8749}
8750
8751/*
8752 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8753 * Should check VIsual_mode before calling this.
8754 * Returns TRUE when backed up to the previous line.
8755 */
8756 static int
8757unadjust_for_sel()
8758{
8759 pos_T *pp;
8760
8761 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8762 {
8763 if (lt(VIsual, curwin->w_cursor))
8764 pp = &curwin->w_cursor;
8765 else
8766 pp = &VIsual;
8767#ifdef FEAT_VIRTUALEDIT
8768 if (pp->coladd > 0)
8769 --pp->coladd;
8770 else
8771#endif
8772 if (pp->col > 0)
8773 {
8774 --pp->col;
8775#ifdef FEAT_MBYTE
8776 mb_adjustpos(pp);
8777#endif
8778 }
8779 else if (pp->lnum > 1)
8780 {
8781 --pp->lnum;
8782 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8783 return TRUE;
8784 }
8785 }
8786 return FALSE;
8787}
8788
8789/*
8790 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8791 */
8792 static void
8793nv_select(cap)
8794 cmdarg_T *cap;
8795{
8796 if (VIsual_active)
8797 VIsual_select = TRUE;
8798 else if (VIsual_reselect)
8799 {
8800 cap->nchar = 'v'; /* fake "gv" command */
8801 cap->arg = TRUE;
8802 nv_g_cmd(cap);
8803 }
8804}
8805
8806#endif
8807
8808/*
8809 * "G", "gg", CTRL-END, CTRL-HOME.
8810 * cap->arg is TRUE for "G".
8811 */
8812 static void
8813nv_goto(cap)
8814 cmdarg_T *cap;
8815{
8816 linenr_T lnum;
8817
8818 if (cap->arg)
8819 lnum = curbuf->b_ml.ml_line_count;
8820 else
8821 lnum = 1L;
8822 cap->oap->motion_type = MLINE;
8823 setpcmark();
8824
8825 /* When a count is given, use it instead of the default lnum */
8826 if (cap->count0 != 0)
8827 lnum = cap->count0;
8828 if (lnum < 1L)
8829 lnum = 1L;
8830 else if (lnum > curbuf->b_ml.ml_line_count)
8831 lnum = curbuf->b_ml.ml_line_count;
8832 curwin->w_cursor.lnum = lnum;
8833 beginline(BL_SOL | BL_FIX);
8834#ifdef FEAT_FOLDING
8835 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8836 foldOpenCursor();
8837#endif
8838}
8839
8840/*
8841 * CTRL-\ in Normal mode.
8842 */
8843 static void
8844nv_normal(cap)
8845 cmdarg_T *cap;
8846{
8847 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8848 {
8849 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008850 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 clear_cmdline = TRUE; /* unshow mode later */
8852 restart_edit = 0;
8853#ifdef FEAT_CMDWIN
8854 if (cmdwin_type != 0)
8855 cmdwin_result = Ctrl_C;
8856#endif
8857#ifdef FEAT_VISUAL
8858 if (VIsual_active)
8859 {
8860 end_visual_mode(); /* stop Visual */
8861 redraw_curbuf_later(INVERTED);
8862 }
8863#endif
8864 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8865 if (cap->nchar == Ctrl_G && p_im)
8866 restart_edit = 'a';
8867 }
8868 else
8869 clearopbeep(cap->oap);
8870}
8871
8872/*
8873 * ESC in Normal mode: beep, but don't flush buffers.
8874 * Don't even beep if we are canceling a command.
8875 */
8876 static void
8877nv_esc(cap)
8878 cmdarg_T *cap;
8879{
8880 int no_reason;
8881
8882 no_reason = (cap->oap->op_type == OP_NOP
8883 && cap->opcount == 0
8884 && cap->count0 == 0
8885 && cap->oap->regname == 0
8886 && !p_im);
8887
8888 if (cap->arg) /* TRUE for CTRL-C */
8889 {
8890 if (restart_edit == 0
8891#ifdef FEAT_CMDWIN
8892 && cmdwin_type == 0
8893#endif
8894#ifdef FEAT_VISUAL
8895 && !VIsual_active
8896#endif
8897 && no_reason)
8898 MSG(_("Type :quit<Enter> to exit Vim"));
8899
8900 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8901 * set again below when halfway a mapping. */
8902 if (!p_im)
8903 restart_edit = 0;
8904#ifdef FEAT_CMDWIN
8905 if (cmdwin_type != 0)
8906 {
8907 cmdwin_result = K_IGNORE;
8908 got_int = FALSE; /* don't stop executing autocommands et al. */
8909 return;
8910 }
8911#endif
8912 }
8913
8914#ifdef FEAT_VISUAL
8915 if (VIsual_active)
8916 {
8917 end_visual_mode(); /* stop Visual */
8918 check_cursor_col(); /* make sure cursor is not beyond EOL */
8919 curwin->w_set_curswant = TRUE;
8920 redraw_curbuf_later(INVERTED);
8921 }
8922 else
8923#endif
8924 if (no_reason)
8925 vim_beep();
8926 clearop(cap->oap);
8927
8928 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8929 * set return to Insert mode afterwards. */
8930 if (restart_edit == 0 && goto_im()
8931#ifdef FEAT_EX_EXTRA
8932 && ex_normal_busy == 0
8933#endif
8934 )
8935 restart_edit = 'a';
8936}
8937
8938/*
8939 * Handle "A", "a", "I", "i" and <Insert> commands.
8940 */
8941 static void
8942nv_edit(cap)
8943 cmdarg_T *cap;
8944{
8945 /* <Insert> is equal to "i" */
8946 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8947 cap->cmdchar = 'i';
8948
8949#ifdef FEAT_VISUAL
8950 /* in Visual mode "A" and "I" are an operator */
8951 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8952 v_visop(cap);
8953
8954 /* in Visual mode and after an operator "a" and "i" are for text objects */
8955 else
8956#endif
8957 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8958 && (cap->oap->op_type != OP_NOP
8959#ifdef FEAT_VISUAL
8960 || VIsual_active
8961#endif
8962 ))
8963 {
8964#ifdef FEAT_TEXTOBJ
8965 nv_object(cap);
8966#else
8967 clearopbeep(cap->oap);
8968#endif
8969 }
8970 else if (!curbuf->b_p_ma && !p_im)
8971 {
8972 /* Only give this error when 'insertmode' is off. */
8973 EMSG(_(e_modifiable));
8974 clearop(cap->oap);
8975 }
8976 else if (!checkclearopq(cap->oap))
8977 {
8978 switch (cap->cmdchar)
8979 {
8980 case 'A': /* "A"ppend after the line */
8981 curwin->w_set_curswant = TRUE;
8982#ifdef FEAT_VIRTUALEDIT
8983 if (ve_flags == VE_ALL)
8984 {
8985 int save_State = State;
8986
8987 /* Pretent Insert mode here to allow the cursor on the
8988 * character past the end of the line */
8989 State = INSERT;
8990 coladvance((colnr_T)MAXCOL);
8991 State = save_State;
8992 }
8993 else
8994#endif
8995 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8996 break;
8997
8998 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008999 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9000 beginline(BL_WHITE);
9001 else
9002 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 break;
9004
9005 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9006#ifdef FEAT_VIRTUALEDIT
9007 /* increment coladd when in virtual space, increment the
9008 * column otherwise, also to append after an unprintable char */
9009 if (virtual_active()
9010 && (curwin->w_cursor.coladd > 0
9011 || *ml_get_cursor() == NUL
9012 || *ml_get_cursor() == TAB))
9013 curwin->w_cursor.coladd++;
9014 else
9015#endif
9016 if (*ml_get_cursor() != NUL)
9017 inc_cursor();
9018 break;
9019 }
9020
9021#ifdef FEAT_VIRTUALEDIT
9022 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9023 {
9024 int save_State = State;
9025
9026 /* Pretent Insert mode here to allow the cursor on the
9027 * character past the end of the line */
9028 State = INSERT;
9029 coladvance(getviscol());
9030 State = save_State;
9031 }
9032#endif
9033
9034 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9035 }
9036}
9037
9038/*
9039 * Invoke edit() and take care of "restart_edit" and the return value.
9040 */
9041 static void
9042invoke_edit(cap, repl, cmd, startln)
9043 cmdarg_T *cap;
9044 int repl; /* "r" or "gr" command */
9045 int cmd;
9046 int startln;
9047{
9048 int restart_edit_save = 0;
9049
9050 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9051 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9052 * it. */
9053 if (repl || !stuff_empty())
9054 restart_edit_save = restart_edit;
9055 else
9056 restart_edit_save = 0;
9057
9058 /* Always reset "restart_edit", this is not a restarted edit. */
9059 restart_edit = 0;
9060
9061 if (edit(cmd, startln, cap->count1))
9062 cap->retval |= CA_COMMAND_BUSY;
9063
9064 if (restart_edit == 0)
9065 restart_edit = restart_edit_save;
9066}
9067
9068#ifdef FEAT_TEXTOBJ
9069/*
9070 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9071 */
9072 static void
9073nv_object(cap)
9074 cmdarg_T *cap;
9075{
9076 int flag;
9077 int include;
9078 char_u *mps_save;
9079
9080 if (cap->cmdchar == 'i')
9081 include = FALSE; /* "ix" = inner object: exclude white space */
9082 else
9083 include = TRUE; /* "ax" = an object: include white space */
9084
9085 /* Make sure (), [], {} and <> are in 'matchpairs' */
9086 mps_save = curbuf->b_p_mps;
9087 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9088
9089 switch (cap->nchar)
9090 {
9091 case 'w': /* "aw" = a word */
9092 flag = current_word(cap->oap, cap->count1, include, FALSE);
9093 break;
9094 case 'W': /* "aW" = a WORD */
9095 flag = current_word(cap->oap, cap->count1, include, TRUE);
9096 break;
9097 case 'b': /* "ab" = a braces block */
9098 case '(':
9099 case ')':
9100 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9101 break;
9102 case 'B': /* "aB" = a Brackets block */
9103 case '{':
9104 case '}':
9105 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9106 break;
9107 case '[': /* "a[" = a [] block */
9108 case ']':
9109 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9110 break;
9111 case '<': /* "a<" = a <> block */
9112 case '>':
9113 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9114 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009115 case 't': /* "at" = a tag block (xml and html) */
9116 flag = current_tagblock(cap->oap, cap->count1, include);
9117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 case 'p': /* "ap" = a paragraph */
9119 flag = current_par(cap->oap, cap->count1, include, 'p');
9120 break;
9121 case 's': /* "as" = a sentence */
9122 flag = current_sent(cap->oap, cap->count1, include);
9123 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009124 case '"': /* "a"" = a double quoted string */
9125 case '\'': /* "a'" = a single quoted string */
9126 case '`': /* "a`" = a backtick quoted string */
9127 flag = current_quote(cap->oap, cap->count1, include,
9128 cap->nchar);
9129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130#if 0 /* TODO */
9131 case 'S': /* "aS" = a section */
9132 case 'f': /* "af" = a filename */
9133 case 'u': /* "au" = a URL */
9134#endif
9135 default:
9136 flag = FAIL;
9137 break;
9138 }
9139
9140 curbuf->b_p_mps = mps_save;
9141 if (flag == FAIL)
9142 clearopbeep(cap->oap);
9143 adjust_cursor_col();
9144 curwin->w_set_curswant = TRUE;
9145}
9146#endif
9147
9148/*
9149 * "q" command: Start/stop recording.
9150 * "q:", "q/", "q?": edit command-line in command-line window.
9151 */
9152 static void
9153nv_record(cap)
9154 cmdarg_T *cap;
9155{
9156 if (cap->oap->op_type == OP_FORMAT)
9157 {
9158 /* "gqq" is the same as "gqgq": format line */
9159 cap->cmdchar = 'g';
9160 cap->nchar = 'q';
9161 nv_operator(cap);
9162 }
9163 else if (!checkclearop(cap->oap))
9164 {
9165#ifdef FEAT_CMDWIN
9166 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9167 {
9168 stuffcharReadbuff(cap->nchar);
9169 stuffcharReadbuff(K_CMDWIN);
9170 }
9171 else
9172#endif
9173 /* (stop) recording into a named register, unless executing a
9174 * register */
9175 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9176 clearopbeep(cap->oap);
9177 }
9178}
9179
9180/*
9181 * Handle the "@r" command.
9182 */
9183 static void
9184nv_at(cap)
9185 cmdarg_T *cap;
9186{
9187 if (checkclearop(cap->oap))
9188 return;
9189#ifdef FEAT_EVAL
9190 if (cap->nchar == '=')
9191 {
9192 if (get_expr_register() == NUL)
9193 return;
9194 }
9195#endif
9196 while (cap->count1-- && !got_int)
9197 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009198 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
9200 clearopbeep(cap->oap);
9201 break;
9202 }
9203 line_breakcheck();
9204 }
9205}
9206
9207/*
9208 * Handle the CTRL-U and CTRL-D commands.
9209 */
9210 static void
9211nv_halfpage(cap)
9212 cmdarg_T *cap;
9213{
9214 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9215 || (cap->cmdchar == Ctrl_D
9216 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9217 clearopbeep(cap->oap);
9218 else if (!checkclearop(cap->oap))
9219 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9220}
9221
9222/*
9223 * Handle "J" or "gJ" command.
9224 */
9225 static void
9226nv_join(cap)
9227 cmdarg_T *cap;
9228{
9229#ifdef FEAT_VISUAL
9230 if (VIsual_active) /* join the visual lines */
9231 nv_operator(cap);
9232 else
9233#endif
9234 if (!checkclearop(cap->oap))
9235 {
9236 if (cap->count0 <= 1)
9237 cap->count0 = 2; /* default for join is two lines! */
9238 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9239 curbuf->b_ml.ml_line_count)
9240 clearopbeep(cap->oap); /* beyond last line */
9241 else
9242 {
9243 prep_redo(cap->oap->regname, cap->count0,
9244 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar893eaab2010-07-10 17:51:46 +02009245 (void)do_join(cap->count0, cap->nchar == NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247 }
9248}
9249
9250/*
9251 * "P", "gP", "p" and "gp" commands.
9252 */
9253 static void
9254nv_put(cap)
9255 cmdarg_T *cap;
9256{
9257#ifdef FEAT_VISUAL
9258 int regname = 0;
9259 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009260 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009261 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262#endif
9263 int dir;
9264 int flags = 0;
9265
9266 if (cap->oap->op_type != OP_NOP)
9267 {
9268#ifdef FEAT_DIFF
9269 /* "dp" is ":diffput" */
9270 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9271 {
9272 clearop(cap->oap);
9273 nv_diffgetput(TRUE);
9274 }
9275 else
9276#endif
9277 clearopbeep(cap->oap);
9278 }
9279 else
9280 {
9281 dir = (cap->cmdchar == 'P'
9282 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9283 ? BACKWARD : FORWARD;
9284 prep_redo_cmd(cap);
9285 if (cap->cmdchar == 'g')
9286 flags |= PUT_CURSEND;
9287
9288#ifdef FEAT_VISUAL
9289 if (VIsual_active)
9290 {
9291 /* Putting in Visual mode: The put text replaces the selected
9292 * text. First delete the selected text, then put the new text.
9293 * Need to save and restore the registers that the delete
9294 * overwrites if the old contents is being put.
9295 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009296 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 regname = cap->oap->regname;
9298# ifdef FEAT_CLIPBOARD
9299 adjust_clip_reg(&regname);
9300# endif
9301 if (regname == 0 || VIM_ISDIGIT(regname)
9302# ifdef FEAT_CLIPBOARD
9303 || (clip_unnamed && (regname == '*' || regname == '+'))
9304# endif
9305
9306 )
9307 {
9308 /* the delete is going to overwrite the register we want to
9309 * put, save it first. */
9310 reg1 = get_register(regname, TRUE);
9311 }
9312
9313 /* Now delete the selected text. */
9314 cap->cmdchar = 'd';
9315 cap->nchar = NUL;
9316 cap->oap->regname = NUL;
9317 nv_operator(cap);
9318 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009319 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320
9321 /* delete PUT_LINE_BACKWARD; */
9322 cap->oap->regname = regname;
9323
9324 if (reg1 != NULL)
9325 {
9326 /* Delete probably changed the register we want to put, save
9327 * it first. Then put back what was there before the delete. */
9328 reg2 = get_register(regname, FALSE);
9329 put_register(regname, reg1);
9330 }
9331
9332 /* When deleted a linewise Visual area, put the register as
9333 * lines to avoid it joined with the next line. When deletion was
9334 * characterwise, split a line when putting lines. */
9335 if (VIsual_mode == 'V')
9336 flags |= PUT_LINE;
9337 else if (VIsual_mode == 'v')
9338 flags |= PUT_LINE_SPLIT;
9339 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9340 flags |= PUT_LINE_FORWARD;
9341 dir = BACKWARD;
9342 if ((VIsual_mode != 'V'
9343 && curwin->w_cursor.col < curbuf->b_op_start.col)
9344 || (VIsual_mode == 'V'
9345 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9346 /* cursor is at the end of the line or end of file, put
9347 * forward. */
9348 dir = FORWARD;
9349 }
9350#endif
9351 do_put(cap->oap->regname, dir, cap->count1, flags);
9352
9353#ifdef FEAT_VISUAL
9354 /* If a register was saved, put it back now. */
9355 if (reg2 != NULL)
9356 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009357
9358 /* What to reselect with "gv"? Selecting the just put text seems to
9359 * be the most useful, since the original text was removed. */
9360 if (was_visual)
9361 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009362 curbuf->b_visual.vi_start = curbuf->b_op_start;
9363 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009364 }
9365
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009366 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009367 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009368 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009369 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009370 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009371
9372 /* If the cursor was in that line, move it to the end of the last
9373 * line. */
9374 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9375 {
9376 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9377 coladvance((colnr_T)MAXCOL);
9378 }
9379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380#endif
9381 auto_format(FALSE, TRUE);
9382 }
9383}
9384
9385/*
9386 * "o" and "O" commands.
9387 */
9388 static void
9389nv_open(cap)
9390 cmdarg_T *cap;
9391{
9392#ifdef FEAT_DIFF
9393 /* "do" is ":diffget" */
9394 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9395 {
9396 clearop(cap->oap);
9397 nv_diffgetput(FALSE);
9398 }
9399 else
9400#endif
9401#ifdef FEAT_VISUAL
9402 if (VIsual_active) /* switch start and end of visual */
9403 v_swap_corners(cap->cmdchar);
9404 else
9405#endif
9406 n_opencmd(cap);
9407}
9408
9409#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 static void
9411nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009412 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 ProcessSniffRequests();
9415}
9416#endif
9417
9418#ifdef FEAT_NETBEANS_INTG
9419 static void
9420nv_nbcmd(cap)
9421 cmdarg_T *cap;
9422{
9423 netbeans_keycommand(cap->nchar);
9424}
9425#endif
9426
9427#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 static void
9429nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009430 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9433}
9434#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009435
9436#ifdef FEAT_AUTOCMD
9437/*
9438 * Trigger CursorHold event.
9439 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9440 * input buffer. "did_cursorhold" is set to avoid retriggering.
9441 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009442 static void
9443nv_cursorhold(cap)
9444 cmdarg_T *cap;
9445{
9446 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9447 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009448 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009449}
9450#endif