blob: 6a4fa781c9ed1bc457783d298b8489266091ba62 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020023static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
26#endif
27
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010028#ifdef FEAT_EVAL
29static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
30#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020032#ifdef __BORLANDC__
33 _RTLENTRYF
34#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 nv_compare __ARGS((const void *s1, const void *s2));
36static int find_command __ARGS((int cmdchar));
37static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000038static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
40static void find_start_of_word __ARGS((pos_T *));
41static void find_end_of_word __ARGS((pos_T *));
42static int get_mouse_class __ARGS((char_u *p));
43#endif
44static void prep_redo_cmd __ARGS((cmdarg_T *cap));
45static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
46static int checkclearop __ARGS((oparg_T *oap));
47static int checkclearopq __ARGS((oparg_T *oap));
48static void clearop __ARGS((oparg_T *oap));
49static void clearopbeep __ARGS((oparg_T *oap));
50#ifdef FEAT_VISUAL
51static void unshift_special __ARGS((cmdarg_T *cap));
52#endif
53#ifdef FEAT_CMDL_INFO
54static void del_from_showcmd __ARGS((int));
55#endif
56
57/*
58 * nv_*(): functions called to handle Normal and Visual mode commands.
59 * n_*(): functions called to handle Normal mode commands.
60 * v_*(): functions called to handle Visual mode commands.
61 */
62static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000063static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void nv_error __ARGS((cmdarg_T *cap));
65static void nv_help __ARGS((cmdarg_T *cap));
66static void nv_addsub __ARGS((cmdarg_T *cap));
67static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000068static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
70#ifdef FEAT_MOUSE
71static void nv_mousescroll __ARGS((cmdarg_T *cap));
72static void nv_mouse __ARGS((cmdarg_T *cap));
73#endif
74static void nv_scroll_line __ARGS((cmdarg_T *cap));
75static void nv_zet __ARGS((cmdarg_T *cap));
76#ifdef FEAT_GUI
77static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
78static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
79#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#ifdef FEAT_GUI_TABLINE
81static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000082static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000084static void nv_exmode __ARGS((cmdarg_T *cap));
85static void nv_colon __ARGS((cmdarg_T *cap));
86static void nv_ctrlg __ARGS((cmdarg_T *cap));
87static void nv_ctrlh __ARGS((cmdarg_T *cap));
88static void nv_clear __ARGS((cmdarg_T *cap));
89static void nv_ctrlo __ARGS((cmdarg_T *cap));
90static void nv_hat __ARGS((cmdarg_T *cap));
91static void nv_Zet __ARGS((cmdarg_T *cap));
92static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000093static void nv_tagpop __ARGS((cmdarg_T *cap));
94static void nv_scroll __ARGS((cmdarg_T *cap));
95static void nv_right __ARGS((cmdarg_T *cap));
96static void nv_left __ARGS((cmdarg_T *cap));
97static void nv_up __ARGS((cmdarg_T *cap));
98static void nv_down __ARGS((cmdarg_T *cap));
99#ifdef FEAT_SEARCHPATH
100static void nv_gotofile __ARGS((cmdarg_T *cap));
101#endif
102static void nv_end __ARGS((cmdarg_T *cap));
103static void nv_dollar __ARGS((cmdarg_T *cap));
104static void nv_search __ARGS((cmdarg_T *cap));
105static void nv_next __ARGS((cmdarg_T *cap));
106static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
107static void nv_csearch __ARGS((cmdarg_T *cap));
108static void nv_brackets __ARGS((cmdarg_T *cap));
109static void nv_percent __ARGS((cmdarg_T *cap));
110static void nv_brace __ARGS((cmdarg_T *cap));
111static void nv_mark __ARGS((cmdarg_T *cap));
112static void nv_findpar __ARGS((cmdarg_T *cap));
113static void nv_undo __ARGS((cmdarg_T *cap));
114static void nv_kundo __ARGS((cmdarg_T *cap));
115static void nv_Replace __ARGS((cmdarg_T *cap));
116#ifdef FEAT_VREPLACE
117static void nv_vreplace __ARGS((cmdarg_T *cap));
118#endif
119#ifdef FEAT_VISUAL
120static void v_swap_corners __ARGS((int cmdchar));
121#endif
122static void nv_replace __ARGS((cmdarg_T *cap));
123static void n_swapchar __ARGS((cmdarg_T *cap));
124static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
125#ifdef FEAT_VISUAL
126static void v_visop __ARGS((cmdarg_T *cap));
127#endif
128static void nv_subst __ARGS((cmdarg_T *cap));
129static void nv_abbrev __ARGS((cmdarg_T *cap));
130static void nv_optrans __ARGS((cmdarg_T *cap));
131static void nv_gomark __ARGS((cmdarg_T *cap));
132static void nv_pcmark __ARGS((cmdarg_T *cap));
133static void nv_regname __ARGS((cmdarg_T *cap));
134#ifdef FEAT_VISUAL
135static void nv_visual __ARGS((cmdarg_T *cap));
136static void n_start_visual_mode __ARGS((int c));
137#endif
138static void nv_window __ARGS((cmdarg_T *cap));
139static void nv_suspend __ARGS((cmdarg_T *cap));
140static void nv_g_cmd __ARGS((cmdarg_T *cap));
141static void n_opencmd __ARGS((cmdarg_T *cap));
142static void nv_dot __ARGS((cmdarg_T *cap));
143static void nv_redo __ARGS((cmdarg_T *cap));
144static void nv_Undo __ARGS((cmdarg_T *cap));
145static void nv_tilde __ARGS((cmdarg_T *cap));
146static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000147#ifdef FEAT_EVAL
148static void set_op_var __ARGS((int optype));
149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150static void nv_lineop __ARGS((cmdarg_T *cap));
151static void nv_home __ARGS((cmdarg_T *cap));
152static void nv_pipe __ARGS((cmdarg_T *cap));
153static void nv_bck_word __ARGS((cmdarg_T *cap));
154static void nv_wordcmd __ARGS((cmdarg_T *cap));
155static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000156static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_VISUAL
158static void adjust_for_sel __ARGS((cmdarg_T *cap));
159static int unadjust_for_sel __ARGS((void));
160static void nv_select __ARGS((cmdarg_T *cap));
161#endif
162static void nv_goto __ARGS((cmdarg_T *cap));
163static void nv_normal __ARGS((cmdarg_T *cap));
164static void nv_esc __ARGS((cmdarg_T *oap));
165static void nv_edit __ARGS((cmdarg_T *cap));
166static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
167#ifdef FEAT_TEXTOBJ
168static void nv_object __ARGS((cmdarg_T *cap));
169#endif
170static void nv_record __ARGS((cmdarg_T *cap));
171static void nv_at __ARGS((cmdarg_T *cap));
172static void nv_halfpage __ARGS((cmdarg_T *cap));
173static void nv_join __ARGS((cmdarg_T *cap));
174static void nv_put __ARGS((cmdarg_T *cap));
175static void nv_open __ARGS((cmdarg_T *cap));
176#ifdef FEAT_SNIFF
177static void nv_sniff __ARGS((cmdarg_T *cap));
178#endif
179#ifdef FEAT_NETBEANS_INTG
180static void nv_nbcmd __ARGS((cmdarg_T *cap));
181#endif
182#ifdef FEAT_DND
183static void nv_drop __ARGS((cmdarg_T *cap));
184#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000185#ifdef FEAT_AUTOCMD
186static void nv_cursorhold __ARGS((cmdarg_T *cap));
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000189static char *e_noident = N_("E349: No identifier under cursor");
190
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/*
192 * Function to be called for a Normal or Visual mode command.
193 * The argument is a cmdarg_T.
194 */
195typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
196
197/* Values for cmd_flags. */
198#define NV_NCH 0x01 /* may need to get a second char */
199#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
200#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
201#define NV_LANG 0x08 /* second char needs language adjustment */
202
203#define NV_SS 0x10 /* may start selection */
204#define NV_SSS 0x20 /* may start selection with shift modifier */
205#define NV_STS 0x40 /* may stop selection without shift modif. */
206#define NV_RL 0x80 /* 'rightleft' modifies command */
207#define NV_KEEPREG 0x100 /* don't clear regname */
208#define NV_NCW 0x200 /* not allowed in command-line window */
209
210/*
211 * Generally speaking, every Normal mode command should either clear any
212 * pending operator (with *clearop*()), or set the motion type variable
213 * oap->motion_type.
214 *
215 * When a cursor motion command is made, it is marked as being a character or
216 * line oriented motion. Then, if an operator is in effect, the operation
217 * becomes character or line oriented accordingly.
218 */
219
220/*
221 * This table contains one entry for every Normal or Visual mode command.
222 * The order doesn't matter, init_normal_cmds() will create a sorted index.
223 * It is faster when all keys from zero to '~' are present.
224 */
225static const struct nv_cmd
226{
227 int cmd_char; /* (first) command character */
228 nv_func_T cmd_func; /* function for this command */
229 short_u cmd_flags; /* NV_ flags */
230 short cmd_arg; /* value for ca.arg */
231} nv_cmds[] =
232{
233 {NUL, nv_error, 0, 0},
234 {Ctrl_A, nv_addsub, 0, 0},
235 {Ctrl_B, nv_page, NV_STS, BACKWARD},
236 {Ctrl_C, nv_esc, 0, TRUE},
237 {Ctrl_D, nv_halfpage, 0, 0},
238 {Ctrl_E, nv_scroll_line, 0, TRUE},
239 {Ctrl_F, nv_page, NV_STS, FORWARD},
240 {Ctrl_G, nv_ctrlg, 0, 0},
241 {Ctrl_H, nv_ctrlh, 0, 0},
242 {Ctrl_I, nv_pcmark, 0, 0},
243 {NL, nv_down, 0, FALSE},
244 {Ctrl_K, nv_error, 0, 0},
245 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000246 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_N, nv_down, NV_STS, FALSE},
248 {Ctrl_O, nv_ctrlo, 0, 0},
249 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000250#ifdef FEAT_VISUAL
251 {Ctrl_Q, nv_visual, 0, FALSE},
252#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 {Ctrl_R, nv_redo, 0, 0},
256 {Ctrl_S, nv_ignore, 0, 0},
257 {Ctrl_T, nv_tagpop, NV_NCW, 0},
258 {Ctrl_U, nv_halfpage, 0, 0},
259#ifdef FEAT_VISUAL
260 {Ctrl_V, nv_visual, 0, FALSE},
261 {'V', nv_visual, 0, FALSE},
262 {'v', nv_visual, 0, FALSE},
263#else
264 {Ctrl_V, nv_error, 0, 0},
265 {'V', nv_error, 0, 0},
266 {'v', nv_error, 0, 0},
267#endif
268 {Ctrl_W, nv_window, 0, 0},
269 {Ctrl_X, nv_addsub, 0, 0},
270 {Ctrl_Y, nv_scroll_line, 0, FALSE},
271 {Ctrl_Z, nv_suspend, 0, 0},
272 {ESC, nv_esc, 0, FALSE},
273 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
274 {Ctrl_RSB, nv_ident, NV_NCW, 0},
275 {Ctrl_HAT, nv_hat, NV_NCW, 0},
276 {Ctrl__, nv_error, 0, 0},
277 {' ', nv_right, 0, 0},
278 {'!', nv_operator, 0, 0},
279 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
280 {'#', nv_ident, 0, 0},
281 {'$', nv_dollar, 0, 0},
282 {'%', nv_percent, 0, 0},
283 {'&', nv_optrans, 0, 0},
284 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
285 {'(', nv_brace, 0, BACKWARD},
286 {')', nv_brace, 0, FORWARD},
287 {'*', nv_ident, 0, 0},
288 {'+', nv_down, 0, TRUE},
289 {',', nv_csearch, 0, TRUE},
290 {'-', nv_up, 0, TRUE},
291 {'.', nv_dot, NV_KEEPREG, 0},
292 {'/', nv_search, 0, FALSE},
293 {'0', nv_beginline, 0, 0},
294 {'1', nv_ignore, 0, 0},
295 {'2', nv_ignore, 0, 0},
296 {'3', nv_ignore, 0, 0},
297 {'4', nv_ignore, 0, 0},
298 {'5', nv_ignore, 0, 0},
299 {'6', nv_ignore, 0, 0},
300 {'7', nv_ignore, 0, 0},
301 {'8', nv_ignore, 0, 0},
302 {'9', nv_ignore, 0, 0},
303 {':', nv_colon, 0, 0},
304 {';', nv_csearch, 0, FALSE},
305 {'<', nv_operator, NV_RL, 0},
306 {'=', nv_operator, 0, 0},
307 {'>', nv_operator, NV_RL, 0},
308 {'?', nv_search, 0, FALSE},
309 {'@', nv_at, NV_NCH_NOP, FALSE},
310 {'A', nv_edit, 0, 0},
311 {'B', nv_bck_word, 0, 1},
312 {'C', nv_abbrev, NV_KEEPREG, 0},
313 {'D', nv_abbrev, NV_KEEPREG, 0},
314 {'E', nv_wordcmd, 0, TRUE},
315 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
316 {'G', nv_goto, 0, TRUE},
317 {'H', nv_scroll, 0, 0},
318 {'I', nv_edit, 0, 0},
319 {'J', nv_join, 0, 0},
320 {'K', nv_ident, 0, 0},
321 {'L', nv_scroll, 0, 0},
322 {'M', nv_scroll, 0, 0},
323 {'N', nv_next, 0, SEARCH_REV},
324 {'O', nv_open, 0, 0},
325 {'P', nv_put, 0, 0},
326 {'Q', nv_exmode, NV_NCW, 0},
327 {'R', nv_Replace, 0, FALSE},
328 {'S', nv_subst, NV_KEEPREG, 0},
329 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
330 {'U', nv_Undo, 0, 0},
331 {'W', nv_wordcmd, 0, TRUE},
332 {'X', nv_abbrev, NV_KEEPREG, 0},
333 {'Y', nv_abbrev, NV_KEEPREG, 0},
334 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
335 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
336 {'\\', nv_error, 0, 0},
337 {']', nv_brackets, NV_NCH_ALW, FORWARD},
338 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
339 {'_', nv_lineop, 0, 0},
340 {'`', nv_gomark, NV_NCH_ALW, FALSE},
341 {'a', nv_edit, NV_NCH, 0},
342 {'b', nv_bck_word, 0, 0},
343 {'c', nv_operator, 0, 0},
344 {'d', nv_operator, 0, 0},
345 {'e', nv_wordcmd, 0, FALSE},
346 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
347 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
348 {'h', nv_left, NV_RL, 0},
349 {'i', nv_edit, NV_NCH, 0},
350 {'j', nv_down, 0, FALSE},
351 {'k', nv_up, 0, FALSE},
352 {'l', nv_right, NV_RL, 0},
353 {'m', nv_mark, NV_NCH_NOP, 0},
354 {'n', nv_next, 0, 0},
355 {'o', nv_open, 0, 0},
356 {'p', nv_put, 0, 0},
357 {'q', nv_record, NV_NCH, 0},
358 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
359 {'s', nv_subst, NV_KEEPREG, 0},
360 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
361 {'u', nv_undo, 0, 0},
362 {'w', nv_wordcmd, 0, FALSE},
363 {'x', nv_abbrev, NV_KEEPREG, 0},
364 {'y', nv_operator, 0, 0},
365 {'z', nv_zet, NV_NCH_ALW, 0},
366 {'{', nv_findpar, 0, BACKWARD},
367 {'|', nv_pipe, 0, 0},
368 {'}', nv_findpar, 0, FORWARD},
369 {'~', nv_tilde, 0, 0},
370
371 /* pound sign */
372 {POUND, nv_ident, 0, 0},
373#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200374 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
375 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
376 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
377 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 {K_LEFTMOUSE, nv_mouse, 0, 0},
379 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
380 {K_LEFTDRAG, nv_mouse, 0, 0},
381 {K_LEFTRELEASE, nv_mouse, 0, 0},
382 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
383 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
384 {K_MIDDLEDRAG, nv_mouse, 0, 0},
385 {K_MIDDLERELEASE, nv_mouse, 0, 0},
386 {K_RIGHTMOUSE, nv_mouse, 0, 0},
387 {K_RIGHTDRAG, nv_mouse, 0, 0},
388 {K_RIGHTRELEASE, nv_mouse, 0, 0},
389 {K_X1MOUSE, nv_mouse, 0, 0},
390 {K_X1DRAG, nv_mouse, 0, 0},
391 {K_X1RELEASE, nv_mouse, 0, 0},
392 {K_X2MOUSE, nv_mouse, 0, 0},
393 {K_X2DRAG, nv_mouse, 0, 0},
394 {K_X2RELEASE, nv_mouse, 0, 0},
395#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000397 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_INS, nv_edit, 0, 0},
399 {K_KINS, nv_edit, 0, 0},
400 {K_BS, nv_ctrlh, 0, 0},
401 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
402 {K_S_UP, nv_page, NV_SS, BACKWARD},
403 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
404 {K_S_DOWN, nv_page, NV_SS, FORWARD},
405 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
406 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
407 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
408 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
409 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
410 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
411 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
412 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
413 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
414 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
415 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
416 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {K_S_END, nv_end, NV_SS, FALSE},
418 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
419 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
420 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {K_S_HOME, nv_home, NV_SS, 0},
422 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
423 {K_DEL, nv_abbrev, 0, 0},
424 {K_KDEL, nv_abbrev, 0, 0},
425 {K_UNDO, nv_kundo, 0, 0},
426 {K_HELP, nv_help, NV_NCW, 0},
427 {K_F1, nv_help, NV_NCW, 0},
428 {K_XF1, nv_help, NV_NCW, 0},
429#ifdef FEAT_VISUAL
430 {K_SELECT, nv_select, 0, 0},
431#endif
432#ifdef FEAT_GUI
433 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
434 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
435#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000436#ifdef FEAT_GUI_TABLINE
437 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000438 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440#ifdef FEAT_FKMAP
441 {K_F8, farsi_fkey, 0, 0},
442 {K_F9, farsi_fkey, 0, 0},
443#endif
444#ifdef FEAT_SNIFF
445 {K_SNIFF, nv_sniff, 0, 0},
446#endif
447#ifdef FEAT_NETBEANS_INTG
448 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
449#endif
450#ifdef FEAT_DND
451 {K_DROP, nv_drop, NV_STS, 0},
452#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000453#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000454 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456};
457
458/* Number of commands in nv_cmds[]. */
459#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
460
461/* Sorted index of commands in nv_cmds[]. */
462static short nv_cmd_idx[NV_CMDS_SIZE];
463
464/* The highest index for which
465 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
466static int nv_max_linear;
467
468/*
469 * Compare functions for qsort() below, that checks the command character
470 * through the index in nv_cmd_idx[].
471 */
472 static int
473#ifdef __BORLANDC__
474_RTLENTRYF
475#endif
476nv_compare(s1, s2)
477 const void *s1;
478 const void *s2;
479{
480 int c1, c2;
481
482 /* The commands are sorted on absolute value. */
483 c1 = nv_cmds[*(const short *)s1].cmd_char;
484 c2 = nv_cmds[*(const short *)s2].cmd_char;
485 if (c1 < 0)
486 c1 = -c1;
487 if (c2 < 0)
488 c2 = -c2;
489 return c1 - c2;
490}
491
492/*
493 * Initialize the nv_cmd_idx[] table.
494 */
495 void
496init_normal_cmds()
497{
498 int i;
499
500 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000501 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 nv_cmd_idx[i] = i;
503
504 /* Sort the commands by the command character. */
505 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
506
507 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000508 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
510 break;
511 nv_max_linear = i - 1;
512}
513
514/*
515 * Search for a command in the commands table.
516 * Returns -1 for invalid command.
517 */
518 static int
519find_command(cmdchar)
520 int cmdchar;
521{
522 int i;
523 int idx;
524 int top, bot;
525 int c;
526
527#ifdef FEAT_MBYTE
528 /* A multi-byte character is never a command. */
529 if (cmdchar >= 0x100)
530 return -1;
531#endif
532
533 /* We use the absolute value of the character. Special keys have a
534 * negative value, but are sorted on their absolute value. */
535 if (cmdchar < 0)
536 cmdchar = -cmdchar;
537
538 /* If the character is in the first part: The character is the index into
539 * nv_cmd_idx[]. */
540 if (cmdchar <= nv_max_linear)
541 return nv_cmd_idx[cmdchar];
542
543 /* Perform a binary search. */
544 bot = nv_max_linear + 1;
545 top = NV_CMDS_SIZE - 1;
546 idx = -1;
547 while (bot <= top)
548 {
549 i = (top + bot) / 2;
550 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
551 if (c < 0)
552 c = -c;
553 if (cmdchar == c)
554 {
555 idx = nv_cmd_idx[i];
556 break;
557 }
558 if (cmdchar > c)
559 bot = i + 1;
560 else
561 top = i - 1;
562 }
563 return idx;
564}
565
566/*
567 * Execute a command in Normal mode.
568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 void
570normal_cmd(oap, toplevel)
571 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000572 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 cmdarg_T ca; /* command arguments */
575 int c;
576 int ctrl_w = FALSE; /* got CTRL-W command */
577 int old_col = curwin->w_curswant;
578#ifdef FEAT_CMDL_INFO
579 int need_flushbuf; /* need to call out_flush() */
580#endif
581#ifdef FEAT_VISUAL
582 pos_T old_pos; /* cursor position before command */
583 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000587#ifdef FEAT_EVAL
588 int set_prevcount = FALSE;
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
592 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000593
594 /* Use a count remembered from before entering an operator. After typing
595 * "3d" we return from normal_cmd() and come back here, the "3" is
596 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ca.opcount = opcount;
598
599#ifdef FEAT_SNIFF
600 want_sniff_request = sniff_connected;
601#endif
602
603 /*
604 * If there is an operator pending, then the command we take this time
605 * will terminate it. Finish_op tells us to finish the operation before
606 * returning this time (unless the operation was cancelled).
607 */
608#ifdef CURSOR_SHAPE
609 c = finish_op;
610#endif
611 finish_op = (oap->op_type != OP_NOP);
612#ifdef CURSOR_SHAPE
613 if (finish_op != c)
614 {
615 ui_cursor_shape(); /* may show different cursor shape */
616# ifdef FEAT_MOUSESHAPE
617 update_mouseshape(-1);
618# endif
619 }
620#endif
621
Bram Moolenaara983fe92008-07-31 20:04:27 +0000622 /* When not finishing an operator and no register name typed, reset the
623 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000627#ifdef FEAT_EVAL
628 set_prevcount = TRUE;
629#endif
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaara983fe92008-07-31 20:04:27 +0000632#ifdef FEAT_AUTOCMD
633 /* Restore counts from before receiving K_CURSORHOLD. This means after
634 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
635 * "3 * 2". */
636 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
637 {
638 ca.opcount = oap->prev_opcount;
639 ca.count0 = oap->prev_count0;
640 oap->prev_opcount = 0;
641 oap->prev_count0 = 0;
642 }
643#endif
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_VISUAL
646 mapped_len = typebuf_maplen();
647#endif
648
649 State = NORMAL_BUSY;
650#ifdef USE_ON_FLY_SCROLL
651 dont_scroll = FALSE; /* allow scrolling here */
652#endif
653
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100654#ifdef FEAT_EVAL
655 /* Set v:count here, when called from main() and not a stuffed
656 * command, so that v:count can be used in an expression mapping
657 * when there is no count. */
658 if (toplevel && stuff_empty())
659 set_vcount_ca(&ca, &set_prevcount);
660#endif
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 /*
663 * Get the command character from the user.
664 */
665 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000668#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 /*
670 * If a mapping was started in Visual or Select mode, remember the length
671 * of the mapping. This is used below to not return to Insert mode for as
672 * long as the mapping is being executed.
673 */
674 if (restart_edit == 0)
675 old_mapped_len = 0;
676 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000677 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681 if (c == NUL)
682 c = K_ZERO;
683
684#ifdef FEAT_VISUAL
685 /*
686 * In Select mode, typed text replaces the selection.
687 */
688 if (VIsual_active
689 && VIsual_select
690 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
691 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000692 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
693 * 'insertmode' is set) fake a "d"elete command, Insert mode will
694 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000695 * Insert the typed character in the typeahead buffer, so that it can
696 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000697 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000698 if (restart_edit != 0)
699 c = 'd';
700 else
701 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000702 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 }
704#endif
705
706#ifdef FEAT_CMDL_INFO
707 need_flushbuf = add_to_showcmd(c);
708#endif
709
710getcount:
711#ifdef FEAT_VISUAL
712 if (!(VIsual_active && VIsual_select))
713#endif
714 {
715 /*
716 * Handle a count before a command and compute ca.count0.
717 * Note that '0' is a command and not the start of a count, but it's
718 * part of a count after other digits.
719 */
720 while ( (c >= '1' && c <= '9')
721 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
722 {
723 if (c == K_DEL || c == K_KDEL)
724 {
725 ca.count0 /= 10;
726#ifdef FEAT_CMDL_INFO
727 del_from_showcmd(4); /* delete the digit and ~@% */
728#endif
729 }
730 else
731 ca.count0 = ca.count0 * 10 + (c - '0');
732 if (ca.count0 < 0) /* got too large! */
733 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000734#ifdef FEAT_EVAL
735 /* Set v:count here, when called from main() and not a stuffed
736 * command, so that v:count can be used in an expression mapping
737 * right after the count. */
738 if (toplevel && stuff_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100739 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (ctrl_w)
742 {
743 ++no_mapping;
744 ++allow_keys; /* no mapping for nchar, but keys */
745 }
746 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000747 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 --no_zero_mapping;
750 if (ctrl_w)
751 {
752 --no_mapping;
753 --allow_keys;
754 }
755#ifdef FEAT_CMDL_INFO
756 need_flushbuf |= add_to_showcmd(c);
757#endif
758 }
759
760 /*
761 * If we got CTRL-W there may be a/another count
762 */
763 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
764 {
765 ctrl_w = TRUE;
766 ca.opcount = ca.count0; /* remember first count */
767 ca.count0 = 0;
768 ++no_mapping;
769 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000770 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 --no_mapping;
773 --allow_keys;
774#ifdef FEAT_CMDL_INFO
775 need_flushbuf |= add_to_showcmd(c);
776#endif
777 goto getcount; /* jump back */
778 }
779 }
780
Bram Moolenaara983fe92008-07-31 20:04:27 +0000781#ifdef FEAT_AUTOCMD
782 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000784 /* Save the count values so that ca.opcount and ca.count0 are exactly
785 * the same when coming back here after handling K_CURSORHOLD. */
786 oap->prev_opcount = ca.opcount;
787 oap->prev_count0 = ca.count0;
788 }
789 else
790#endif
791 if (ca.opcount != 0)
792 {
793 /*
794 * If we're in the middle of an operator (including after entering a
795 * yank buffer with '"') AND we had a count before the operator, then
796 * that count overrides the current value of ca.count0.
797 * What this means effectively, is that commands like "3dw" get turned
798 * into "d3w" which makes things fall into place pretty neatly.
799 * If you give a count before AND after the operator, they are
800 * multiplied.
801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (ca.count0)
803 ca.count0 *= ca.opcount;
804 else
805 ca.count0 = ca.opcount;
806 }
807
808 /*
809 * Always remember the count. It will be set to zero (on the next call,
810 * above) when there is no pending operator.
811 * When called from main(), save the count for use by the "count" built-in
812 * variable.
813 */
814 ca.opcount = ca.count0;
815 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
816
817#ifdef FEAT_EVAL
818 /*
819 * Only set v:count when called from main() and not a stuffed command.
820 */
821 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000822 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824
825 /*
826 * Find the command character in the table of commands.
827 * For CTRL-W we already got nchar when looking for a count.
828 */
829 if (ctrl_w)
830 {
831 ca.nchar = c;
832 ca.cmdchar = Ctrl_W;
833 }
834 else
835 ca.cmdchar = c;
836 idx = find_command(ca.cmdchar);
837 if (idx < 0)
838 {
839 /* Not a known command: beep. */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000843
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000844 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000846 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000848 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 goto normal_end;
850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000851#ifdef FEAT_AUTOCMD
852 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
853 goto normal_end;
854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856#ifdef FEAT_VISUAL
857 /*
858 * In Visual/Select mode, a few keys are handled in a special way.
859 */
860 if (VIsual_active)
861 {
862 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
863 if (km_stopsel
864 && (nv_cmds[idx].cmd_flags & NV_STS)
865 && !(mod_mask & MOD_MASK_SHIFT))
866 {
867 end_visual_mode();
868 redraw_curbuf_later(INVERTED);
869 }
870
871 /* Keys that work different when 'keymodel' contains "startsel" */
872 if (km_startsel)
873 {
874 if (nv_cmds[idx].cmd_flags & NV_SS)
875 {
876 unshift_special(&ca);
877 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000878 if (idx < 0)
879 {
880 /* Just in case */
881 clearopbeep(oap);
882 goto normal_end;
883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
886 && (mod_mask & MOD_MASK_SHIFT))
887 {
888 mod_mask &= ~MOD_MASK_SHIFT;
889 }
890 }
891 }
892#endif
893
894#ifdef FEAT_RIGHTLEFT
895 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
896 && (nv_cmds[idx].cmd_flags & NV_RL))
897 {
898 /* Invert horizontal movements and operations. Only when typed by the
899 * user directly, not when the result of a mapping or "x" translated
900 * to "dl". */
901 switch (ca.cmdchar)
902 {
903 case 'l': ca.cmdchar = 'h'; break;
904 case K_RIGHT: ca.cmdchar = K_LEFT; break;
905 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
906 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
907 case 'h': ca.cmdchar = 'l'; break;
908 case K_LEFT: ca.cmdchar = K_RIGHT; break;
909 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
910 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
911 case '>': ca.cmdchar = '<'; break;
912 case '<': ca.cmdchar = '>'; break;
913 }
914 idx = find_command(ca.cmdchar);
915 }
916#endif
917
918 /*
919 * Get an additional character if we need one.
920 */
921 if ((nv_cmds[idx].cmd_flags & NV_NCH)
922 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
923 && oap->op_type == OP_NOP)
924 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
925 || (ca.cmdchar == 'q'
926 && oap->op_type == OP_NOP
927 && !Recording
928 && !Exec_reg)
929 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
930 && (oap->op_type != OP_NOP
931#ifdef FEAT_VISUAL
932 || VIsual_active
933#endif
934 ))))
935 {
936 int *cp;
937 int repl = FALSE; /* get character for replace mode */
938 int lit = FALSE; /* get extra character literally */
939 int langmap_active = FALSE; /* using :lmap mappings */
940 int lang; /* getting a text character */
941#ifdef USE_IM_CONTROL
942 int save_smd; /* saved value of p_smd */
943#endif
944
945 ++no_mapping;
946 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000947#ifdef FEAT_AUTOCMD
948 /* Don't generate a CursorHold event here, most commands can't handle
949 * it, e.g., nv_replace(), nv_csearch(). */
950 did_cursorhold = TRUE;
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (ca.cmdchar == 'g')
953 {
954 /*
955 * For 'g' get the next character now, so that we can check for
956 * "gr", "g'" and "g`".
957 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000958 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_CMDL_INFO
961 need_flushbuf |= add_to_showcmd(ca.nchar);
962#endif
963 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
964 || ca.nchar == Ctrl_BSL)
965 {
966 cp = &ca.extra_char; /* need to get a third character */
967 if (ca.nchar != 'r')
968 lit = TRUE; /* get it literally */
969 else
970 repl = TRUE; /* get it in replace mode */
971 }
972 else
973 cp = NULL; /* no third character needed */
974 }
975 else
976 {
977 if (ca.cmdchar == 'r') /* get it in replace mode */
978 repl = TRUE;
979 cp = &ca.nchar;
980 }
981 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
982
983 /*
984 * Get a second or third character.
985 */
986 if (cp != NULL)
987 {
988#ifdef CURSOR_SHAPE
989 if (repl)
990 {
991 State = REPLACE; /* pretend Replace mode */
992 ui_cursor_shape(); /* show different cursor shape */
993 }
994#endif
995 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
996 {
997 /* Allow mappings defined with ":lmap". */
998 --no_mapping;
999 --allow_keys;
1000 if (repl)
1001 State = LREPLACE;
1002 else
1003 State = LANGMAP;
1004 langmap_active = TRUE;
1005 }
1006#ifdef USE_IM_CONTROL
1007 save_smd = p_smd;
1008 p_smd = FALSE; /* Don't let the IM code show the mode here */
1009 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1010 im_set_active(TRUE);
1011#endif
1012
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001013 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015 if (langmap_active)
1016 {
1017 /* Undo the decrement done above */
1018 ++no_mapping;
1019 ++allow_keys;
1020 State = NORMAL_BUSY;
1021 }
1022#ifdef USE_IM_CONTROL
1023 if (lang)
1024 {
1025 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1026 im_save_status(&curbuf->b_p_iminsert);
1027 im_set_active(FALSE);
1028 }
1029 p_smd = save_smd;
1030#endif
1031#ifdef CURSOR_SHAPE
1032 State = NORMAL_BUSY;
1033#endif
1034#ifdef FEAT_CMDL_INFO
1035 need_flushbuf |= add_to_showcmd(*cp);
1036#endif
1037
1038 if (!lit)
1039 {
1040#ifdef FEAT_DIGRAPHS
1041 /* Typing CTRL-K gets a digraph. */
1042 if (*cp == Ctrl_K
1043 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1044 || cp == &ca.extra_char)
1045 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1046 {
1047 c = get_digraph(FALSE);
1048 if (c > 0)
1049 {
1050 *cp = c;
1051# ifdef FEAT_CMDL_INFO
1052 /* Guessing how to update showcmd here... */
1053 del_from_showcmd(3);
1054 need_flushbuf |= add_to_showcmd(*cp);
1055# endif
1056 }
1057 }
1058#endif
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /* adjust chars > 127, except after "tTfFr" commands */
1061 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_RIGHTLEFT
1063 /* adjust Hebrew mapped char */
1064 if (p_hkmap && lang && KeyTyped)
1065 *cp = hkmap(*cp);
1066# ifdef FEAT_FKMAP
1067 /* adjust Farsi mapped char */
1068 if (p_fkmap && lang && KeyTyped)
1069 *cp = fkmap(*cp);
1070# endif
1071#endif
1072 }
1073
1074 /*
1075 * When the next character is CTRL-\ a following CTRL-N means the
1076 * command is aborted and we go to Normal mode.
1077 */
1078 if (cp == &ca.extra_char
1079 && ca.nchar == Ctrl_BSL
1080 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1081 {
1082 ca.cmdchar = Ctrl_BSL;
1083 ca.nchar = ca.extra_char;
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if (*cp == Ctrl_BSL)
1087 {
1088 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1089
1090 /* There is a busy wait here when typing "f<C-\>" and then
1091 * something different from CTRL-N. Can't be avoided. */
1092 while ((c = vpeekc()) <= 0 && towait > 0L)
1093 {
1094 do_sleep(towait > 50L ? 50L : towait);
1095 towait -= 50L;
1096 }
1097 if (c > 0)
1098 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001099 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (c != Ctrl_N && c != Ctrl_G)
1101 vungetc(c);
1102 else
1103 {
1104 ca.cmdchar = Ctrl_BSL;
1105 ca.nchar = c;
1106 idx = find_command(ca.cmdchar);
1107 }
1108 }
1109 }
1110
1111#ifdef FEAT_MBYTE
1112 /* When getting a text character and the next character is a
1113 * multi-byte character, it could be a composing character.
1114 * However, don't wait for it to arrive. */
1115 while (enc_utf8 && lang && (c = vpeekc()) > 0
1116 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1117 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001118 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (!utf_iscomposing(c))
1120 {
1121 vungetc(c); /* it wasn't, put it back */
1122 break;
1123 }
1124 else if (ca.ncharC1 == 0)
1125 ca.ncharC1 = c;
1126 else
1127 ca.ncharC2 = c;
1128 }
1129#endif
1130 }
1131 --no_mapping;
1132 --allow_keys;
1133 }
1134
1135#ifdef FEAT_CMDL_INFO
1136 /*
1137 * Flush the showcmd characters onto the screen so we can see them while
1138 * the command is being executed. Only do this when the shown command was
1139 * actually displayed, otherwise this will slow down a lot when executing
1140 * mappings.
1141 */
1142 if (need_flushbuf)
1143 out_flush();
1144#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001145#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001146 if (ca.cmdchar != K_IGNORE)
1147 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
1150 State = NORMAL;
1151
1152 if (ca.nchar == ESC)
1153 {
1154 clearop(oap);
1155 if (restart_edit == 0 && goto_im())
1156 restart_edit = 'a';
1157 goto normal_end;
1158 }
1159
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160 if (ca.cmdchar != K_IGNORE)
1161 {
1162 msg_didout = FALSE; /* don't scroll screen up for normal command */
1163 msg_col = 0;
1164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166#ifdef FEAT_VISUAL
1167 old_pos = curwin->w_cursor; /* remember where cursor was */
1168
1169 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1170 * mode. */
1171 if (!VIsual_active && km_startsel)
1172 {
1173 if (nv_cmds[idx].cmd_flags & NV_SS)
1174 {
1175 start_selection();
1176 unshift_special(&ca);
1177 idx = find_command(ca.cmdchar);
1178 }
1179 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1180 && (mod_mask & MOD_MASK_SHIFT))
1181 {
1182 start_selection();
1183 mod_mask &= ~MOD_MASK_SHIFT;
1184 }
1185 }
1186#endif
1187
1188 /*
1189 * Execute the command!
1190 * Call the command function found in the commands table.
1191 */
1192 ca.arg = nv_cmds[idx].cmd_arg;
1193 (nv_cmds[idx].cmd_func)(&ca);
1194
1195 /*
1196 * If we didn't start or finish an operator, reset oap->regname, unless we
1197 * need it later.
1198 */
1199 if (!finish_op
1200 && !oap->op_type
1201 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1202 {
1203 clearop(oap);
1204#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001205 {
1206 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001207
Bram Moolenaar536681b2011-05-10 16:12:45 +02001208 /* Adjust the register according to 'clipboard', so that when
1209 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001210# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001211 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 set_reg_var(regname);
1214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216 }
1217
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001218#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001219 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001220 * character or "z333<cr>". */
1221 if (old_mapped_len > 0)
1222 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 /*
1226 * If an operation is pending, handle it...
1227 */
1228 do_pending_operator(&ca, old_col, FALSE);
1229
1230 /*
1231 * Wait for a moment when a message is displayed that will be overwritten
1232 * by the mode message.
1233 * In Visual mode and with "^O" in Insert mode, a short message will be
1234 * overwritten by the mode message. Wait a bit, until a key is hit.
1235 * In Visual mode, it's more important to keep the Visual area updated
1236 * than keeping a message (e.g. from a /pat search).
1237 * Only do this if the command was typed, not from a mapping.
1238 * Don't wait when emsg_silent is non-zero.
1239 * Also wait a bit after an error message, e.g. for "^O:".
1240 * Don't redraw the screen, it would remove the message.
1241 */
1242 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001243 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 && (restart_edit != 0
1245#ifdef FEAT_VISUAL
1246 || (VIsual_active
1247 && old_pos.lnum == curwin->w_cursor.lnum
1248 && old_pos.col == curwin->w_cursor.col)
1249#endif
1250 )
1251 && (clear_cmdline
1252 || redraw_cmdline)
1253 && (msg_didout || (msg_didany && msg_scroll))
1254 && !msg_nowait
1255 && KeyTyped)
1256 || (restart_edit != 0
1257#ifdef FEAT_VISUAL
1258 && !VIsual_active
1259#endif
1260 && (msg_scroll
1261 || emsg_on_display)))
1262 && oap->regname == 0
1263 && !(ca.retval & CA_COMMAND_BUSY)
1264 && stuff_empty()
1265 && typebuf_typed()
1266 && emsg_silent == 0
1267 && !did_wait_return
1268 && oap->op_type == OP_NOP)
1269 {
1270 int save_State = State;
1271
1272 /* Draw the cursor with the right shape here */
1273 if (restart_edit != 0)
1274 State = INSERT;
1275
1276 /* If need to redraw, and there is a "keep_msg", redraw before the
1277 * delay */
1278 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1279 {
1280 char_u *kmsg;
1281
1282 kmsg = keep_msg;
1283 keep_msg = NULL;
1284 /* showmode() will clear keep_msg, but we want to use it anyway */
1285 update_screen(0);
1286 /* now reset it, otherwise it's put in the history again */
1287 keep_msg = kmsg;
1288 msg_attr(kmsg, keep_msg_attr);
1289 vim_free(kmsg);
1290 }
1291 setcursor();
1292 cursor_on();
1293 out_flush();
1294 if (msg_scroll || emsg_on_display)
1295 ui_delay(1000L, TRUE); /* wait at least one second */
1296 ui_delay(3000L, FALSE); /* wait up to three seconds */
1297 State = save_State;
1298
1299 msg_scroll = FALSE;
1300 emsg_on_display = FALSE;
1301 }
1302
1303 /*
1304 * Finish up after executing a Normal mode command.
1305 */
1306normal_end:
1307
1308 msg_nowait = FALSE;
1309
1310 /* Reset finish_op, in case it was set */
1311#ifdef CURSOR_SHAPE
1312 c = finish_op;
1313#endif
1314 finish_op = FALSE;
1315#ifdef CURSOR_SHAPE
1316 /* Redraw the cursor with another shape, if we were in Operator-pending
1317 * mode or did a replace command. */
1318 if (c || ca.cmdchar == 'r')
1319 {
1320 ui_cursor_shape(); /* may show different cursor shape */
1321# ifdef FEAT_MOUSESHAPE
1322 update_mouseshape(-1);
1323# endif
1324 }
1325#endif
1326
1327#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001328 if (oap->op_type == OP_NOP && oap->regname == 0
1329# ifdef FEAT_AUTOCMD
1330 && ca.cmdchar != K_CURSORHOLD
1331# endif
1332 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 clear_showcmd();
1334#endif
1335
1336 checkpcmark(); /* check if we moved since setting pcmark */
1337 vim_free(ca.searchbuf);
1338
1339#ifdef FEAT_MBYTE
1340 if (has_mbyte)
1341 mb_adjust_cursor();
1342#endif
1343
1344#ifdef FEAT_SCROLLBIND
1345 if (curwin->w_p_scb && toplevel)
1346 {
1347 validate_cursor(); /* may need to update w_leftcol */
1348 do_check_scrollbind(TRUE);
1349 }
1350#endif
1351
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352#ifdef FEAT_CURSORBIND
1353 if (curwin->w_p_crb && toplevel)
1354 {
1355 validate_cursor(); /* may need to update w_leftcol */
1356 do_check_cursorbind();
1357 }
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1362 * if still inside a mapping that started in Visual mode).
1363 * May switch from Visual to Select mode after CTRL-O command.
1364 */
1365 if ( oap->op_type == OP_NOP
1366#ifdef FEAT_VISUAL
1367 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1368 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001369#else
1370 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371#endif
1372 && !(ca.retval & CA_COMMAND_BUSY)
1373 && stuff_empty()
1374 && oap->regname == 0)
1375 {
1376#ifdef FEAT_VISUAL
1377 if (restart_VIsual_select == 1)
1378 {
1379 VIsual_select = TRUE;
1380 showmode();
1381 restart_VIsual_select = 0;
1382 }
1383#endif
1384 if (restart_edit != 0
1385#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001386 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001388 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 (void)edit(restart_edit, FALSE, 1L);
1390 }
1391
1392#ifdef FEAT_VISUAL
1393 if (restart_VIsual_select == 2)
1394 restart_VIsual_select = 1;
1395#endif
1396
1397 /* Save count before an operator for next time. */
1398 opcount = ca.opcount;
1399}
1400
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001401#ifdef FEAT_EVAL
1402/*
1403 * Set v:count and v:count1 according to "cap".
1404 * Set v:prevcount only when "set_prevcount" is TRUE.
1405 */
1406 static void
1407set_vcount_ca(cap, set_prevcount)
1408 cmdarg_T *cap;
1409 int *set_prevcount;
1410{
1411 long count = cap->count0;
1412
1413 /* multiply with cap->opcount the same way as above */
1414 if (cap->opcount != 0)
1415 count = cap->opcount * (count == 0 ? 1 : count);
1416 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1417 *set_prevcount = FALSE; /* only set v:prevcount once */
1418}
1419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421/*
1422 * Handle an operator after visual mode or when the movement is finished
1423 */
1424 void
1425do_pending_operator(cap, old_col, gui_yank)
1426 cmdarg_T *cap;
1427 int old_col;
1428 int gui_yank;
1429{
1430 oparg_T *oap = cap->oap;
1431 pos_T old_cursor;
1432 int empty_region_error;
1433 int restart_edit_save;
1434
1435#ifdef FEAT_VISUAL
1436 /* The visual area is remembered for redo */
1437 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1438 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001439 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 static long redo_VIsual_count; /* count for Visual operator */
1441# ifdef FEAT_VIRTUALEDIT
1442 int include_line_break = FALSE;
1443# endif
1444#endif
1445
1446#if defined(FEAT_CLIPBOARD)
1447 /*
1448 * Yank the visual area into the GUI selection register before we operate
1449 * on it and lose it forever.
1450 * Don't do it if a specific register was specified, so that ""x"*P works.
1451 * This could call do_pending_operator() recursively, but that's OK
1452 * because gui_yank will be TRUE for the nested call.
1453 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001454 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 && oap->op_type != OP_NOP
1456 && !gui_yank
1457# ifdef FEAT_VISUAL
1458 && VIsual_active
1459 && !redo_VIsual_busy
1460# endif
1461 && oap->regname == 0)
1462 clip_auto_select();
1463#endif
1464 old_cursor = curwin->w_cursor;
1465
1466 /*
1467 * If an operation is pending, handle it...
1468 */
1469 if ((finish_op
1470#ifdef FEAT_VISUAL
1471 || VIsual_active
1472#endif
1473 ) && oap->op_type != OP_NOP)
1474 {
1475#ifdef FEAT_VISUAL
1476 oap->is_VIsual = VIsual_active;
1477 if (oap->motion_force == 'V')
1478 oap->motion_type = MLINE;
1479 else if (oap->motion_force == 'v')
1480 {
1481 /* If the motion was linewise, "inclusive" will not have been set.
1482 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1483 if (oap->motion_type == MLINE)
1484 oap->inclusive = FALSE;
1485 /* If the motion already was characterwise, toggle "inclusive" */
1486 else if (oap->motion_type == MCHAR)
1487 oap->inclusive = !oap->inclusive;
1488 oap->motion_type = MCHAR;
1489 }
1490 else if (oap->motion_force == Ctrl_V)
1491 {
1492 /* Change line- or characterwise motion into Visual block mode. */
1493 VIsual_active = TRUE;
1494 VIsual = oap->start;
1495 VIsual_mode = Ctrl_V;
1496 VIsual_select = FALSE;
1497 VIsual_reselect = FALSE;
1498 }
1499#endif
1500
1501 /* only redo yank when 'y' flag is in 'cpoptions' */
1502 /* never redo "zf" (define fold) */
1503 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1504#ifdef FEAT_VISUAL
1505 && (!VIsual_active || oap->motion_force)
1506#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001507 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_FOLDING
1509 && oap->op_type != OP_FOLD
1510 && oap->op_type != OP_FOLDOPEN
1511 && oap->op_type != OP_FOLDOPENREC
1512 && oap->op_type != OP_FOLDCLOSE
1513 && oap->op_type != OP_FOLDCLOSEREC
1514 && oap->op_type != OP_FOLDDEL
1515 && oap->op_type != OP_FOLDDELREC
1516#endif
1517 )
1518 {
1519 prep_redo(oap->regname, cap->count0,
1520 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1521 oap->motion_force, cap->cmdchar, cap->nchar);
1522 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1523 {
1524 /*
1525 * If 'cpoptions' does not contain 'r', insert the search
1526 * pattern to really repeat the same command.
1527 */
1528 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001529 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 AppendToRedobuff(NL_STR);
1531 }
1532 else if (cap->cmdchar == ':')
1533 {
1534 /* do_cmdline() has stored the first typed line in
1535 * "repeat_cmdline". When several lines are typed repeating
1536 * won't be possible. */
1537 if (repeat_cmdline == NULL)
1538 ResetRedobuff();
1539 else
1540 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001541 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 AppendToRedobuff(NL_STR);
1543 vim_free(repeat_cmdline);
1544 repeat_cmdline = NULL;
1545 }
1546 }
1547 }
1548
1549#ifdef FEAT_VISUAL
1550 if (redo_VIsual_busy)
1551 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001552 /* Redo of an operation on a Visual area. Use the same size from
1553 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 oap->start = curwin->w_cursor;
1555 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1556 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1557 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1558 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001559 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001561 if (VIsual_mode == 'v')
1562 {
1563 if (redo_VIsual_line_count <= 1)
1564 {
1565 validate_virtcol();
1566 curwin->w_curswant =
1567 curwin->w_virtcol + redo_VIsual_vcol - 1;
1568 }
1569 else
1570 curwin->w_curswant = redo_VIsual_vcol;
1571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001573 {
1574 curwin->w_curswant = MAXCOL;
1575 }
1576 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 cap->count0 = redo_VIsual_count;
1579 if (redo_VIsual_count != 0)
1580 cap->count1 = redo_VIsual_count;
1581 else
1582 cap->count1 = 1;
1583 }
1584 else if (VIsual_active)
1585 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001586 if (!gui_yank)
1587 {
1588 /* Save the current VIsual area for '< and '> marks, and "gv" */
1589 curbuf->b_visual.vi_start = VIsual;
1590 curbuf->b_visual.vi_end = curwin->w_cursor;
1591 curbuf->b_visual.vi_mode = VIsual_mode;
1592 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001594 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597
1598 /* In Select mode, a linewise selection is operated upon like a
1599 * characterwise selection. */
1600 if (VIsual_select && VIsual_mode == 'V')
1601 {
1602 if (lt(VIsual, curwin->w_cursor))
1603 {
1604 VIsual.col = 0;
1605 curwin->w_cursor.col =
1606 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1607 }
1608 else
1609 {
1610 curwin->w_cursor.col = 0;
1611 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1612 }
1613 VIsual_mode = 'v';
1614 }
1615 /* If 'selection' is "exclusive", backup one character for
1616 * charwise selections. */
1617 else if (VIsual_mode == 'v')
1618 {
1619# ifdef FEAT_VIRTUALEDIT
1620 include_line_break =
1621# endif
1622 unadjust_for_sel();
1623 }
1624
1625 oap->start = VIsual;
1626 if (VIsual_mode == 'V')
1627 oap->start.col = 0;
1628 }
1629#endif /* FEAT_VISUAL */
1630
1631 /*
1632 * Set oap->start to the first position of the operated text, oap->end
1633 * to the end of the operated text. w_cursor is equal to oap->start.
1634 */
1635 if (lt(oap->start, curwin->w_cursor))
1636 {
1637#ifdef FEAT_FOLDING
1638 /* Include folded lines completely. */
1639 if (!VIsual_active)
1640 {
1641 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1642 oap->start.col = 0;
1643 if (hasFolding(curwin->w_cursor.lnum, NULL,
1644 &curwin->w_cursor.lnum))
1645 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1646 }
1647#endif
1648 oap->end = curwin->w_cursor;
1649 curwin->w_cursor = oap->start;
1650
1651 /* w_virtcol may have been updated; if the cursor goes back to its
1652 * previous position w_virtcol becomes invalid and isn't updated
1653 * automatically. */
1654 curwin->w_valid &= ~VALID_VIRTCOL;
1655 }
1656 else
1657 {
1658#ifdef FEAT_FOLDING
1659 /* Include folded lines completely. */
1660 if (!VIsual_active && oap->motion_type == MLINE)
1661 {
1662 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1663 NULL))
1664 curwin->w_cursor.col = 0;
1665 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1666 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1667 }
1668#endif
1669 oap->end = oap->start;
1670 oap->start = curwin->w_cursor;
1671 }
1672
1673 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1674
1675#ifdef FEAT_VIRTUALEDIT
1676 /* Set "virtual_op" before resetting VIsual_active. */
1677 virtual_op = virtual_active();
1678#endif
1679
1680#ifdef FEAT_VISUAL
1681 if (VIsual_active || redo_VIsual_busy)
1682 {
1683 if (VIsual_mode == Ctrl_V) /* block mode */
1684 {
1685 colnr_T start, end;
1686
1687 oap->block_mode = TRUE;
1688
1689 getvvcol(curwin, &(oap->start),
1690 &oap->start_vcol, NULL, &oap->end_vcol);
1691 if (!redo_VIsual_busy)
1692 {
1693 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1694
1695 if (start < oap->start_vcol)
1696 oap->start_vcol = start;
1697 if (end > oap->end_vcol)
1698 {
1699 if (*p_sel == 'e' && start >= 1
1700 && start - 1 >= oap->end_vcol)
1701 oap->end_vcol = start - 1;
1702 else
1703 oap->end_vcol = end;
1704 }
1705 }
1706
1707 /* if '$' was used, get oap->end_vcol from longest line */
1708 if (curwin->w_curswant == MAXCOL)
1709 {
1710 curwin->w_cursor.col = MAXCOL;
1711 oap->end_vcol = 0;
1712 for (curwin->w_cursor.lnum = oap->start.lnum;
1713 curwin->w_cursor.lnum <= oap->end.lnum;
1714 ++curwin->w_cursor.lnum)
1715 {
1716 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1717 if (end > oap->end_vcol)
1718 oap->end_vcol = end;
1719 }
1720 }
1721 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001722 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /*
1724 * Correct oap->end.col and oap->start.col to be the
1725 * upper-left and lower-right corner of the block area.
1726 *
1727 * (Actually, this does convert column positions into character
1728 * positions)
1729 */
1730 curwin->w_cursor.lnum = oap->end.lnum;
1731 coladvance(oap->end_vcol);
1732 oap->end = curwin->w_cursor;
1733
1734 curwin->w_cursor = oap->start;
1735 coladvance(oap->start_vcol);
1736 oap->start = curwin->w_cursor;
1737 }
1738
1739 if (!redo_VIsual_busy && !gui_yank)
1740 {
1741 /*
1742 * Prepare to reselect and redo Visual: this is based on the
1743 * size of the Visual text
1744 */
1745 resel_VIsual_mode = VIsual_mode;
1746 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001747 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001749 {
1750 if (VIsual_mode != Ctrl_V)
1751 getvvcol(curwin, &(oap->end),
1752 NULL, NULL, &oap->end_vcol);
1753 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1754 {
1755 if (VIsual_mode != Ctrl_V)
1756 getvvcol(curwin, &(oap->start),
1757 &oap->start_vcol, NULL, NULL);
1758 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1759 }
1760 else
1761 resel_VIsual_vcol = oap->end_vcol;
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 resel_VIsual_line_count = oap->line_count;
1764 }
1765
1766 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1767 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1768 && oap->op_type != OP_COLON
1769#ifdef FEAT_FOLDING
1770 && oap->op_type != OP_FOLD
1771 && oap->op_type != OP_FOLDOPEN
1772 && oap->op_type != OP_FOLDOPENREC
1773 && oap->op_type != OP_FOLDCLOSE
1774 && oap->op_type != OP_FOLDCLOSEREC
1775 && oap->op_type != OP_FOLDDEL
1776 && oap->op_type != OP_FOLDDELREC
1777#endif
1778 && oap->motion_force == NUL
1779 )
1780 {
1781 /* Prepare for redoing. Only use the nchar field for "r",
1782 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001783 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1784 || cap->nchar == 'N'))
1785 /* "gn" and "gN" are a bit different */
1786 prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
1787 get_op_char(oap->op_type),
1788 get_extra_op_char(oap->op_type));
1789 else
1790 prep_redo(oap->regname, 0L, NUL, 'v',
1791 get_op_char(oap->op_type),
1792 get_extra_op_char(oap->op_type),
1793 oap->op_type == OP_REPLACE
1794 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (!redo_VIsual_busy)
1796 {
1797 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001798 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 redo_VIsual_line_count = resel_VIsual_line_count;
1800 redo_VIsual_count = cap->count0;
1801 }
1802 }
1803
1804 /*
1805 * oap->inclusive defaults to TRUE.
1806 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1807 * FALSE. This makes "d}P" and "v}dP" work the same.
1808 */
1809 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1810 oap->inclusive = TRUE;
1811 if (VIsual_mode == 'V')
1812 oap->motion_type = MLINE;
1813 else
1814 {
1815 oap->motion_type = MCHAR;
1816 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1817# ifdef FEAT_VIRTUALEDIT
1818 && (include_line_break || !virtual_op)
1819# endif
1820 )
1821 {
1822 oap->inclusive = FALSE;
1823 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001824 * that works on lines only. */
1825 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001827 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1828 {
1829 ++oap->end.lnum;
1830 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001832 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001834 ++oap->line_count;
1835 }
1836 else
1837 {
1838 /* Cannot move below the last line, make the op
1839 * inclusive to tell the operation to include the
1840 * line break. */
1841 oap->inclusive = TRUE;
1842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
1845 }
1846
1847 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 /*
1850 * Switch Visual off now, so screen updating does
1851 * not show inverted text when the screen is redrawn.
1852 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1853 * no screen redraw, so it is done here to remove the inverted
1854 * part.
1855 */
1856 if (!gui_yank)
1857 {
1858 VIsual_active = FALSE;
1859# ifdef FEAT_MOUSE
1860 setmouse();
1861 mouse_dragging = 0;
1862# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001863 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 clear_cmdline = TRUE; /* unshow visual mode later */
1865#ifdef FEAT_CMDL_INFO
1866 else
1867 clear_showcmd();
1868#endif
1869 if ((oap->op_type == OP_YANK
1870 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001871 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 || oap->op_type == OP_FILTER)
1873 && oap->motion_force == NUL)
1874 redraw_curbuf_later(INVERTED);
1875 }
1876 }
1877#endif
1878
1879#ifdef FEAT_MBYTE
1880 /* Include the trailing byte of a multi-byte char. */
1881 if (has_mbyte && oap->inclusive)
1882 {
1883 int l;
1884
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001885 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (l > 1)
1887 oap->end.col += l - 1;
1888 }
1889#endif
1890 curwin->w_set_curswant = TRUE;
1891
1892 /*
1893 * oap->empty is set when start and end are the same. The inclusive
1894 * flag affects this too, unless yanking and the end is on a NUL.
1895 */
1896 oap->empty = (oap->motion_type == MCHAR
1897 && (!oap->inclusive
1898 || (oap->op_type == OP_YANK
1899 && gchar_pos(&oap->end) == NUL))
1900 && equalpos(oap->start, oap->end)
1901#ifdef FEAT_VIRTUALEDIT
1902 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1903#endif
1904 );
1905 /*
1906 * For delete, change and yank, it's an error to operate on an
1907 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1908 */
1909 empty_region_error = (oap->empty
1910 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1911
1912#ifdef FEAT_VISUAL
1913 /* Force a redraw when operating on an empty Visual region, when
1914 * 'modifiable is off or creating a fold. */
1915 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1916# ifdef FEAT_FOLDING
1917 || oap->op_type == OP_FOLD
1918# endif
1919 ))
1920 redraw_curbuf_later(INVERTED);
1921#endif
1922
1923 /*
1924 * If the end of an operator is in column one while oap->motion_type
1925 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1926 * character in the previous line. If op_start is on or before the
1927 * first non-blank in the line, the operator becomes linewise
1928 * (strange, but that's the way vi does it).
1929 */
1930 if ( oap->motion_type == MCHAR
1931 && oap->inclusive == FALSE
1932 && !(cap->retval & CA_NO_ADJ_OP_END)
1933 && oap->end.col == 0
1934#ifdef FEAT_VISUAL
1935 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001936 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#endif
1938 && oap->line_count > 1)
1939 {
1940 oap->end_adjusted = TRUE; /* remember that we did this */
1941 --oap->line_count;
1942 --oap->end.lnum;
1943 if (inindent(0))
1944 oap->motion_type = MLINE;
1945 else
1946 {
1947 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1948 if (oap->end.col)
1949 {
1950 --oap->end.col;
1951 oap->inclusive = TRUE;
1952 }
1953 }
1954 }
1955 else
1956 oap->end_adjusted = FALSE;
1957
1958 switch (oap->op_type)
1959 {
1960 case OP_LSHIFT:
1961 case OP_RSHIFT:
1962 op_shift(oap, TRUE,
1963#ifdef FEAT_VISUAL
1964 oap->is_VIsual ? (int)cap->count1 :
1965#endif
1966 1);
1967 auto_format(FALSE, TRUE);
1968 break;
1969
1970 case OP_JOIN_NS:
1971 case OP_JOIN:
1972 if (oap->line_count < 2)
1973 oap->line_count = 2;
1974 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1975 curbuf->b_ml.ml_line_count)
1976 beep_flush();
1977 else
1978 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001979 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 auto_format(FALSE, TRUE);
1981 }
1982 break;
1983
1984 case OP_DELETE:
1985#ifdef FEAT_VISUAL
1986 VIsual_reselect = FALSE; /* don't reselect now */
1987#endif
1988 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001991 CancelRedo();
1992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 else
1994 {
1995 (void)op_delete(oap);
1996 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1997 u_save_cursor(); /* cursor line wasn't saved yet */
1998 auto_format(FALSE, TRUE);
1999 }
2000 break;
2001
2002 case OP_YANK:
2003 if (empty_region_error)
2004 {
2005 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002008 CancelRedo();
2009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 else
2012 (void)op_yank(oap, FALSE, !gui_yank);
2013 check_cursor_col();
2014 break;
2015
2016 case OP_CHANGE:
2017#ifdef FEAT_VISUAL
2018 VIsual_reselect = FALSE; /* don't reselect now */
2019#endif
2020 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002023 CancelRedo();
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026 {
2027 /* This is a new edit command, not a restart. Need to
2028 * remember it to make 'insertmode' work with mappings for
2029 * Visual mode. But do this only once and not when typed and
2030 * 'insertmode' isn't set. */
2031 if (p_im || !KeyTyped)
2032 restart_edit_save = restart_edit;
2033 else
2034 restart_edit_save = 0;
2035 restart_edit = 0;
2036 /* Reset finish_op now, don't want it set inside edit(). */
2037 finish_op = FALSE;
2038 if (op_change(oap)) /* will call edit() */
2039 cap->retval |= CA_COMMAND_BUSY;
2040 if (restart_edit == 0)
2041 restart_edit = restart_edit_save;
2042 }
2043 break;
2044
2045 case OP_FILTER:
2046 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2047 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2048 else
2049 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2050
2051 case OP_INDENT:
2052 case OP_COLON:
2053
2054#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2055 /*
2056 * If 'equalprg' is empty, do the indenting internally.
2057 */
2058 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2059 {
2060# ifdef FEAT_LISP
2061 if (curbuf->b_p_lisp)
2062 {
2063 op_reindent(oap, get_lisp_indent);
2064 break;
2065 }
2066# endif
2067# ifdef FEAT_CINDENT
2068 op_reindent(oap,
2069# ifdef FEAT_EVAL
2070 *curbuf->b_p_inde != NUL ? get_expr_indent :
2071# endif
2072 get_c_indent);
2073 break;
2074# endif
2075 }
2076#endif
2077
2078 op_colon(oap);
2079 break;
2080
2081 case OP_TILDE:
2082 case OP_UPPER:
2083 case OP_LOWER:
2084 case OP_ROT13:
2085 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002088 CancelRedo();
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 op_tilde(oap);
2092 check_cursor_col();
2093 break;
2094
2095 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002096#if defined(FEAT_EVAL)
2097 if (*curbuf->b_p_fex != NUL)
2098 op_formatexpr(oap); /* use expression */
2099 else
2100#endif
2101 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 op_colon(oap); /* use external command */
2103 else
2104 op_format(oap, FALSE); /* use internal function */
2105 break;
2106
2107 case OP_FORMAT2:
2108 op_format(oap, TRUE); /* use internal function */
2109 break;
2110
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002111 case OP_FUNCTION:
2112 op_function(oap); /* call 'operatorfunc' */
2113 break;
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 case OP_INSERT:
2116 case OP_APPEND:
2117#ifdef FEAT_VISUAL
2118 VIsual_reselect = FALSE; /* don't reselect now */
2119#endif
2120#ifdef FEAT_VISUALEXTRA
2121 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002124 CancelRedo();
2125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 else
2127 {
2128 /* This is a new edit command, not a restart. Need to
2129 * remember it to make 'insertmode' work with mappings for
2130 * Visual mode. But do this only once. */
2131 restart_edit_save = restart_edit;
2132 restart_edit = 0;
2133
2134 op_insert(oap, cap->count1);
2135
2136 /* TODO: when inserting in several lines, should format all
2137 * the lines. */
2138 auto_format(FALSE, TRUE);
2139
2140 if (restart_edit == 0)
2141 restart_edit = restart_edit_save;
2142 }
2143#else
2144 vim_beep();
2145#endif
2146 break;
2147
2148 case OP_REPLACE:
2149#ifdef FEAT_VISUAL
2150 VIsual_reselect = FALSE; /* don't reselect now */
2151#endif
2152#ifdef FEAT_VISUALEXTRA
2153 if (empty_region_error)
2154#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002157 CancelRedo();
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159#ifdef FEAT_VISUALEXTRA
2160 else
2161 op_replace(oap, cap->nchar);
2162#endif
2163 break;
2164
2165#ifdef FEAT_FOLDING
2166 case OP_FOLD:
2167 VIsual_reselect = FALSE; /* don't reselect now */
2168 foldCreate(oap->start.lnum, oap->end.lnum);
2169 break;
2170
2171 case OP_FOLDOPEN:
2172 case OP_FOLDOPENREC:
2173 case OP_FOLDCLOSE:
2174 case OP_FOLDCLOSEREC:
2175 VIsual_reselect = FALSE; /* don't reselect now */
2176 opFoldRange(oap->start.lnum, oap->end.lnum,
2177 oap->op_type == OP_FOLDOPEN
2178 || oap->op_type == OP_FOLDOPENREC,
2179 oap->op_type == OP_FOLDOPENREC
2180 || oap->op_type == OP_FOLDCLOSEREC,
2181 oap->is_VIsual);
2182 break;
2183
2184 case OP_FOLDDEL:
2185 case OP_FOLDDELREC:
2186 VIsual_reselect = FALSE; /* don't reselect now */
2187 deleteFold(oap->start.lnum, oap->end.lnum,
2188 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2189 break;
2190#endif
2191 default:
2192 clearopbeep(oap);
2193 }
2194#ifdef FEAT_VIRTUALEDIT
2195 virtual_op = MAYBE;
2196#endif
2197 if (!gui_yank)
2198 {
2199 /*
2200 * if 'sol' not set, go back to old column for some commands
2201 */
2202 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2203 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2204 || oap->op_type == OP_DELETE))
2205 coladvance(curwin->w_curswant = old_col);
2206 }
2207 else
2208 {
2209 curwin->w_cursor = old_cursor;
2210 }
2211#ifdef FEAT_VISUAL
2212 oap->block_mode = FALSE;
2213#endif
2214 clearop(oap);
2215 }
2216}
2217
2218/*
2219 * Handle indent and format operators and visual mode ":".
2220 */
2221 static void
2222op_colon(oap)
2223 oparg_T *oap;
2224{
2225 stuffcharReadbuff(':');
2226#ifdef FEAT_VISUAL
2227 if (oap->is_VIsual)
2228 stuffReadbuff((char_u *)"'<,'>");
2229 else
2230#endif
2231 {
2232 /*
2233 * Make the range look nice, so it can be repeated.
2234 */
2235 if (oap->start.lnum == curwin->w_cursor.lnum)
2236 stuffcharReadbuff('.');
2237 else
2238 stuffnumReadbuff((long)oap->start.lnum);
2239 if (oap->end.lnum != oap->start.lnum)
2240 {
2241 stuffcharReadbuff(',');
2242 if (oap->end.lnum == curwin->w_cursor.lnum)
2243 stuffcharReadbuff('.');
2244 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2245 stuffcharReadbuff('$');
2246 else if (oap->start.lnum == curwin->w_cursor.lnum)
2247 {
2248 stuffReadbuff((char_u *)".+");
2249 stuffnumReadbuff((long)oap->line_count - 1);
2250 }
2251 else
2252 stuffnumReadbuff((long)oap->end.lnum);
2253 }
2254 }
2255 if (oap->op_type != OP_COLON)
2256 stuffReadbuff((char_u *)"!");
2257 if (oap->op_type == OP_INDENT)
2258 {
2259#ifndef FEAT_CINDENT
2260 if (*get_equalprg() == NUL)
2261 stuffReadbuff((char_u *)"indent");
2262 else
2263#endif
2264 stuffReadbuff(get_equalprg());
2265 stuffReadbuff((char_u *)"\n");
2266 }
2267 else if (oap->op_type == OP_FORMAT)
2268 {
2269 if (*p_fp == NUL)
2270 stuffReadbuff((char_u *)"fmt");
2271 else
2272 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002273 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
2276 /*
2277 * do_cmdline() does the rest
2278 */
2279}
2280
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002281/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002282 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002283 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002284 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002285op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002286 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002287{
2288#ifdef FEAT_EVAL
2289 char_u *(argv[1]);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002290 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002291
2292 if (*p_opfunc == NUL)
2293 EMSG(_("E774: 'operatorfunc' is empty"));
2294 else
2295 {
2296 /* Set '[ and '] marks to text to be operated on. */
2297 curbuf->b_op_start = oap->start;
2298 curbuf->b_op_end = oap->end;
2299 if (oap->motion_type != MLINE && !oap->inclusive)
2300 /* Exclude the end position. */
2301 decl(&curbuf->b_op_end);
2302
2303 if (oap->block_mode)
2304 argv[0] = (char_u *)"block";
2305 else if (oap->motion_type == MLINE)
2306 argv[0] = (char_u *)"line";
2307 else
2308 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002309
2310 /* Reset virtual_op so that 'virtualedit' can be changed in the
2311 * function. */
2312 virtual_op = MAYBE;
2313
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002314 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002315
2316 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002317 }
2318#else
2319 EMSG(_("E775: Eval feature not available"));
2320#endif
2321}
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#if defined(FEAT_MOUSE) || defined(PROTO)
2324/*
2325 * Do the appropriate action for the current mouse click in the current mode.
2326 * Not used for Command-line mode.
2327 *
2328 * Normal Mode:
2329 * event modi- position visual change action
2330 * fier cursor window
2331 * left press - yes end yes
2332 * left press C yes end yes "^]" (2)
2333 * left press S yes end yes "*" (2)
2334 * left drag - yes start if moved no
2335 * left relse - yes start if moved no
2336 * middle press - yes if not active no put register
2337 * middle press - yes if active no yank and put
2338 * right press - yes start or extend yes
2339 * right press S yes no change yes "#" (2)
2340 * right drag - yes extend no
2341 * right relse - yes extend no
2342 *
2343 * Insert or Replace Mode:
2344 * event modi- position visual change action
2345 * fier cursor window
2346 * left press - yes (cannot be active) yes
2347 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2348 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2349 * left drag - yes start or extend (1) no CTRL-O (1)
2350 * left relse - yes start or extend (1) no CTRL-O (1)
2351 * middle press - no (cannot be active) no put register
2352 * right press - yes start or extend yes CTRL-O
2353 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2354 *
2355 * (1) only if mouse pointer moved since press
2356 * (2) only if click is in same buffer
2357 *
2358 * Return TRUE if start_arrow() should be called for edit mode.
2359 */
2360 int
2361do_mouse(oap, c, dir, count, fixindent)
2362 oparg_T *oap; /* operator argument, can be NULL */
2363 int c; /* K_LEFTMOUSE, etc */
2364 int dir; /* Direction to 'put' if necessary */
2365 long count;
2366 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2367{
2368 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2369 static int got_click = FALSE; /* got a click some time back */
2370
2371 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2372 int is_click; /* If FALSE it's a drag or release event */
2373 int is_drag; /* If TRUE it's a drag event */
2374 int jump_flags = 0; /* flags for jump_to_mouse() */
2375 pos_T start_visual;
2376 int moved; /* Has cursor moved? */
2377 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002378#ifdef FEAT_WINDOWS
2379 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381#ifdef FEAT_VERTSPLIT
2382 int in_sep_line; /* mouse in vertical separator line */
2383#endif
2384 int c1, c2;
2385#if defined(FEAT_FOLDING)
2386 pos_T save_cursor;
2387#endif
2388 win_T *old_curwin = curwin;
2389#ifdef FEAT_VISUAL
2390 static pos_T orig_cursor;
2391 colnr_T leftcol, rightcol;
2392 pos_T end_visual;
2393 int diff;
2394 int old_active = VIsual_active;
2395 int old_mode = VIsual_mode;
2396#endif
2397 int regname;
2398
2399#if defined(FEAT_FOLDING)
2400 save_cursor = curwin->w_cursor;
2401#endif
2402
2403 /*
2404 * When GUI is active, always recognize mouse events, otherwise:
2405 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2406 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2407 * - For command line and insert mode 'mouse' is checked before calling
2408 * do_mouse().
2409 */
2410 if (do_always)
2411 do_always = FALSE;
2412 else
2413#ifdef FEAT_GUI
2414 if (!gui.in_use)
2415#endif
2416 {
2417#ifdef FEAT_VISUAL
2418 if (VIsual_active)
2419 {
2420 if (!mouse_has(MOUSE_VISUAL))
2421 return FALSE;
2422 }
2423 else
2424#endif
2425 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2426 return FALSE;
2427 }
2428
2429 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2430
2431#ifdef FEAT_MOUSESHAPE
2432 /* May have stopped dragging the status or separator line. The pointer is
2433 * most likely still on the status or separator line. */
2434 if (!is_drag && drag_status_line)
2435 {
2436 drag_status_line = FALSE;
2437 update_mouseshape(SHAPE_IDX_STATUS);
2438 }
2439# ifdef FEAT_VERTSPLIT
2440 if (!is_drag && drag_sep_line)
2441 {
2442 drag_sep_line = FALSE;
2443 update_mouseshape(SHAPE_IDX_VSEP);
2444 }
2445# endif
2446#endif
2447
2448 /*
2449 * Ignore drag and release events if we didn't get a click.
2450 */
2451 if (is_click)
2452 got_click = TRUE;
2453 else
2454 {
2455 if (!got_click) /* didn't get click, ignore */
2456 return FALSE;
2457 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002460#ifdef FEAT_WINDOWS
2461 if (in_tab_line)
2462 {
2463 in_tab_line = FALSE;
2464 return FALSE;
2465 }
2466#endif
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469
Bram Moolenaar64969662005-12-14 21:59:55 +00002470#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002472 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
2474 if ((mod_mask & MOD_MASK_ALT))
2475 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 /*
2479 * CTRL right mouse button does CTRL-T
2480 */
2481 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2482 {
2483 if (State & INSERT)
2484 stuffcharReadbuff(Ctrl_O);
2485 if (count > 1)
2486 stuffnumReadbuff(count);
2487 stuffcharReadbuff(Ctrl_T);
2488 got_click = FALSE; /* ignore drag&release now */
2489 return FALSE;
2490 }
2491
2492 /*
2493 * CTRL only works with left mouse button
2494 */
2495 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2496 return FALSE;
2497
2498 /*
2499 * When a modifier is down, ignore drag and release events, as well as
2500 * multiple clicks and the middle mouse button.
2501 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2502 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002503 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2504 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 && (!is_click
2506 || (mod_mask & MOD_MASK_MULTI_CLICK)
2507 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002508 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 && mouse_model_popup()
2510 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002511 && !((mod_mask & MOD_MASK_ALT)
2512 && !mouse_model_popup()
2513 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 )
2515 return FALSE;
2516
2517 /*
2518 * If the button press was used as the movement command for an operator
2519 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2520 * drag/release events.
2521 */
2522 if (!is_click && which_button == MOUSE_MIDDLE)
2523 return FALSE;
2524
2525 if (oap != NULL)
2526 regname = oap->regname;
2527 else
2528 regname = 0;
2529
2530 /*
2531 * Middle mouse button does a 'put' of the selected text
2532 */
2533 if (which_button == MOUSE_MIDDLE)
2534 {
2535 if (State == NORMAL)
2536 {
2537 /*
2538 * If an operator was pending, we don't know what the user wanted
2539 * to do. Go back to normal mode: Clear the operator and beep().
2540 */
2541 if (oap != NULL && oap->op_type != OP_NOP)
2542 {
2543 clearopbeep(oap);
2544 return FALSE;
2545 }
2546
2547#ifdef FEAT_VISUAL
2548 /*
2549 * If visual was active, yank the highlighted text and put it
2550 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002551 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 */
2553 if (VIsual_active)
2554 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002555 if (VIsual_select)
2556 {
2557 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002558 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002559 }
2560 else
2561 {
2562 stuffcharReadbuff('y');
2563 stuffcharReadbuff(K_MIDDLEMOUSE);
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 do_always = TRUE; /* ignore 'mouse' setting next time */
2566 return FALSE;
2567 }
2568#endif
2569 /*
2570 * The rest is below jump_to_mouse()
2571 */
2572 }
2573
2574 else if ((State & INSERT) == 0)
2575 return FALSE;
2576
2577 /*
2578 * Middle click in insert mode doesn't move the mouse, just insert the
2579 * contents of a register. '.' register is special, can't insert that
2580 * with do_put().
2581 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2582 * happens for the GUI).
2583 */
2584 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2585 {
2586 if (regname == '.')
2587 insert_reg(regname, TRUE);
2588 else
2589 {
2590#ifdef FEAT_CLIPBOARD
2591 if (clip_star.available && regname == 0)
2592 regname = '*';
2593#endif
2594 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2595 insert_reg(regname, TRUE);
2596 else
2597 {
2598 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2599
2600 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2601 AppendCharToRedobuff(Ctrl_R);
2602 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2603 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2604 }
2605 }
2606 return FALSE;
2607 }
2608 }
2609
2610 /* When dragging or button-up stay in the same window. */
2611 if (!is_click)
2612 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2613
2614 start_visual.lnum = 0;
2615
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616#ifdef FEAT_WINDOWS
2617 /* Check for clicking in the tab page line. */
2618 if (mouse_row == 0 && firstwin->w_winrow > 0)
2619 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002620 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002621 {
2622 if (in_tab_line)
2623 {
2624 c1 = TabPageIdxs[mouse_col];
2625 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2626 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002627 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002628 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002629
Bram Moolenaarf740b292006-02-16 22:11:02 +00002630 /* click in a tab selects that tab page */
2631 if (is_click
2632# ifdef FEAT_CMDWIN
2633 && cmdwin_type == 0
2634# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002636 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002637 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002638 c1 = TabPageIdxs[mouse_col];
2639 if (c1 >= 0)
2640 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002641 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2642 {
2643 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002644 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002645 tabpage_new();
2646 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2647 }
2648 else
2649 {
2650 /* Go to specified tab page, or next one if not clicking
2651 * on a label. */
2652 goto_tabpage(c1);
2653
2654 /* It's like clicking on the status line of a window. */
2655 if (curwin != old_curwin)
2656 end_visual_mode();
2657 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002658 }
2659 else if (c1 < 0)
2660 {
2661 tabpage_T *tp;
2662
2663 /* Close the current or specified tab page. */
2664 if (c1 == -999)
2665 tp = curtab;
2666 else
2667 tp = find_tabpage(-c1);
2668 if (tp == curtab)
2669 {
2670 if (first_tabpage->tp_next != NULL)
2671 tabpage_close(FALSE);
2672 }
2673 else if (tp != NULL)
2674 tabpage_close_other(tp, FALSE);
2675 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002676 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002677 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002679 else if (is_drag && in_tab_line)
2680 {
2681 c1 = TabPageIdxs[mouse_col];
2682 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2683 return FALSE;
2684 }
2685
Bram Moolenaarf740b292006-02-16 22:11:02 +00002686#endif
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /*
2689 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2690 * right button up -> pop-up menu
2691 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002692 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
2694 if (mouse_model_popup())
2695 {
2696 if (which_button == MOUSE_RIGHT
2697 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2698 {
2699 /*
2700 * NOTE: Ignore right button down and drag mouse events.
2701 * Windows only shows the popup menu on the button up event.
2702 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002703#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2704 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 if (!is_click)
2706 return FALSE;
2707#endif
2708#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2709 if (is_click || is_drag)
2710 return FALSE;
2711#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002712#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2714 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2715 if (gui.in_use)
2716 {
2717 jump_flags = 0;
2718 if (STRCMP(p_mousem, "popup_setpos") == 0)
2719 {
2720 /* First set the cursor position before showing the popup
2721 * menu. */
2722#ifdef FEAT_VISUAL
2723 if (VIsual_active)
2724 {
2725 pos_T m_pos;
2726
2727 /*
2728 * set MOUSE_MAY_STOP_VIS if we are outside the
2729 * selection or the current window (might have false
2730 * negative here)
2731 */
2732 if (mouse_row < W_WINROW(curwin)
2733 || mouse_row
2734 > (W_WINROW(curwin) + curwin->w_height))
2735 jump_flags = MOUSE_MAY_STOP_VIS;
2736 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2737 jump_flags = MOUSE_MAY_STOP_VIS;
2738 else
2739 {
2740 if ((lt(curwin->w_cursor, VIsual)
2741 && (lt(m_pos, curwin->w_cursor)
2742 || lt(VIsual, m_pos)))
2743 || (lt(VIsual, curwin->w_cursor)
2744 && (lt(m_pos, VIsual)
2745 || lt(curwin->w_cursor, m_pos))))
2746 {
2747 jump_flags = MOUSE_MAY_STOP_VIS;
2748 }
2749 else if (VIsual_mode == Ctrl_V)
2750 {
2751 getvcols(curwin, &curwin->w_cursor, &VIsual,
2752 &leftcol, &rightcol);
2753 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2754 if (m_pos.col < leftcol || m_pos.col > rightcol)
2755 jump_flags = MOUSE_MAY_STOP_VIS;
2756 }
2757 }
2758 }
2759 else
2760 jump_flags = MOUSE_MAY_STOP_VIS;
2761#endif
2762 }
2763 if (jump_flags)
2764 {
2765 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2766 update_curbuf(
2767#ifdef FEAT_VISUAL
2768 VIsual_active ? INVERTED :
2769#endif
2770 VALID);
2771 setcursor();
2772 out_flush(); /* Update before showing popup menu */
2773 }
2774# ifdef FEAT_MENU
2775 gui_show_popupmenu();
2776# endif
2777 return (jump_flags & CURSOR_MOVED) != 0;
2778 }
2779 else
2780 return FALSE;
2781#else
2782 return FALSE;
2783#endif
2784 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002785 if (which_button == MOUSE_LEFT
2786 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 which_button = MOUSE_RIGHT;
2789 mod_mask &= ~MOD_MASK_SHIFT;
2790 }
2791 }
2792
2793#ifdef FEAT_VISUAL
2794 if ((State & (NORMAL | INSERT))
2795 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2796 {
2797 if (which_button == MOUSE_LEFT)
2798 {
2799 if (is_click)
2800 {
2801 /* stop Visual mode for a left click in a window, but not when
2802 * on a status line */
2803 if (VIsual_active)
2804 jump_flags |= MOUSE_MAY_STOP_VIS;
2805 }
2806 else if (mouse_has(MOUSE_VISUAL))
2807 jump_flags |= MOUSE_MAY_VIS;
2808 }
2809 else if (which_button == MOUSE_RIGHT)
2810 {
2811 if (is_click && VIsual_active)
2812 {
2813 /*
2814 * Remember the start and end of visual before moving the
2815 * cursor.
2816 */
2817 if (lt(curwin->w_cursor, VIsual))
2818 {
2819 start_visual = curwin->w_cursor;
2820 end_visual = VIsual;
2821 }
2822 else
2823 {
2824 start_visual = VIsual;
2825 end_visual = curwin->w_cursor;
2826 }
2827 }
2828 jump_flags |= MOUSE_FOCUS;
2829 if (mouse_has(MOUSE_VISUAL))
2830 jump_flags |= MOUSE_MAY_VIS;
2831 }
2832 }
2833#endif
2834
2835 /*
2836 * If an operator is pending, ignore all drags and releases until the
2837 * next mouse click.
2838 */
2839 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2840 {
2841 got_click = FALSE;
2842 oap->motion_type = MCHAR;
2843 }
2844
2845 /* When releasing the button let jump_to_mouse() know. */
2846 if (!is_click && !is_drag)
2847 jump_flags |= MOUSE_RELEASED;
2848
2849 /*
2850 * JUMP!
2851 */
2852 jump_flags = jump_to_mouse(jump_flags,
2853 oap == NULL ? NULL : &(oap->inclusive), which_button);
2854 moved = (jump_flags & CURSOR_MOVED);
2855 in_status_line = (jump_flags & IN_STATUS_LINE);
2856#ifdef FEAT_VERTSPLIT
2857 in_sep_line = (jump_flags & IN_SEP_LINE);
2858#endif
2859
2860#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002861 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2863 {
2864 int key = KEY2TERMCAP1(c);
2865
2866 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2867 || key == (int)KE_RIGHTRELEASE)
2868 netbeans_button_release(which_button);
2869 }
2870#endif
2871
2872 /* When jumping to another window, clear a pending operator. That's a bit
2873 * friendlier than beeping and not jumping to that window. */
2874 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2875 clearop(oap);
2876
2877#ifdef FEAT_FOLDING
2878 if (mod_mask == 0
2879 && !is_drag
2880 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2881 && which_button == MOUSE_LEFT)
2882 {
2883 /* open or close a fold at this line */
2884 if (jump_flags & MOUSE_FOLD_OPEN)
2885 openFold(curwin->w_cursor.lnum, 1L);
2886 else
2887 closeFold(curwin->w_cursor.lnum, 1L);
2888 /* don't move the cursor if still in the same window */
2889 if (curwin == old_curwin)
2890 curwin->w_cursor = save_cursor;
2891 }
2892#endif
2893
2894#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2895 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2896 {
2897 clip_modeless(which_button, is_click, is_drag);
2898 return FALSE;
2899 }
2900#endif
2901
2902#ifdef FEAT_VISUAL
2903 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002904 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (VIsual_active && is_drag && p_so)
2906 {
2907 /* In the very first line, allow scrolling one line */
2908 if (mouse_row == 0)
2909 mouse_dragging = 2;
2910 else
2911 mouse_dragging = 1;
2912 }
2913
2914 /* When dragging the mouse above the window, scroll down. */
2915 if (is_drag && mouse_row < 0 && !in_status_line)
2916 {
2917 scroll_redraw(FALSE, 1L);
2918 mouse_row = 0;
2919 }
2920
2921 if (start_visual.lnum) /* right click in visual mode */
2922 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002923 /* When ALT is pressed make Visual mode blockwise. */
2924 if (mod_mask & MOD_MASK_ALT)
2925 VIsual_mode = Ctrl_V;
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 /*
2928 * In Visual-block mode, divide the area in four, pick up the corner
2929 * that is in the quarter that the cursor is in.
2930 */
2931 if (VIsual_mode == Ctrl_V)
2932 {
2933 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2934 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2935 end_visual.col = leftcol;
2936 else
2937 end_visual.col = rightcol;
2938 if (curwin->w_cursor.lnum <
2939 (start_visual.lnum + end_visual.lnum) / 2)
2940 end_visual.lnum = end_visual.lnum;
2941 else
2942 end_visual.lnum = start_visual.lnum;
2943
2944 /* move VIsual to the right column */
2945 start_visual = curwin->w_cursor; /* save the cursor pos */
2946 curwin->w_cursor = end_visual;
2947 coladvance(end_visual.col);
2948 VIsual = curwin->w_cursor;
2949 curwin->w_cursor = start_visual; /* restore the cursor */
2950 }
2951 else
2952 {
2953 /*
2954 * If the click is before the start of visual, change the start.
2955 * If the click is after the end of visual, change the end. If
2956 * the click is inside the visual, change the closest side.
2957 */
2958 if (lt(curwin->w_cursor, start_visual))
2959 VIsual = end_visual;
2960 else if (lt(end_visual, curwin->w_cursor))
2961 VIsual = start_visual;
2962 else
2963 {
2964 /* In the same line, compare column number */
2965 if (end_visual.lnum == start_visual.lnum)
2966 {
2967 if (curwin->w_cursor.col - start_visual.col >
2968 end_visual.col - curwin->w_cursor.col)
2969 VIsual = start_visual;
2970 else
2971 VIsual = end_visual;
2972 }
2973
2974 /* In different lines, compare line number */
2975 else
2976 {
2977 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2978 (end_visual.lnum - curwin->w_cursor.lnum);
2979
2980 if (diff > 0) /* closest to end */
2981 VIsual = start_visual;
2982 else if (diff < 0) /* closest to start */
2983 VIsual = end_visual;
2984 else /* in the middle line */
2985 {
2986 if (curwin->w_cursor.col <
2987 (start_visual.col + end_visual.col) / 2)
2988 VIsual = end_visual;
2989 else
2990 VIsual = start_visual;
2991 }
2992 }
2993 }
2994 }
2995 }
2996 /*
2997 * If Visual mode started in insert mode, execute "CTRL-O"
2998 */
2999 else if ((State & INSERT) && VIsual_active)
3000 stuffcharReadbuff(Ctrl_O);
3001#endif
3002
3003 /*
3004 * Middle mouse click: Put text before cursor.
3005 */
3006 if (which_button == MOUSE_MIDDLE)
3007 {
3008#ifdef FEAT_CLIPBOARD
3009 if (clip_star.available && regname == 0)
3010 regname = '*';
3011#endif
3012 if (yank_register_mline(regname))
3013 {
3014 if (mouse_past_bottom)
3015 dir = FORWARD;
3016 }
3017 else if (mouse_past_eol)
3018 dir = FORWARD;
3019
3020 if (fixindent)
3021 {
3022 c1 = (dir == BACKWARD) ? '[' : ']';
3023 c2 = 'p';
3024 }
3025 else
3026 {
3027 c1 = (dir == FORWARD) ? 'p' : 'P';
3028 c2 = NUL;
3029 }
3030 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3031
3032 /*
3033 * Remember where the paste started, so in edit() Insstart can be set
3034 * to this position
3035 */
3036 if (restart_edit != 0)
3037 where_paste_started = curwin->w_cursor;
3038 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3039 }
3040
3041#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3042 /*
3043 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3044 * error under the mouse pointer.
3045 */
3046 else if (((mod_mask & MOD_MASK_CTRL)
3047 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3048 && bt_quickfix(curbuf))
3049 {
3050 if (State & INSERT)
3051 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003052 if (curwin->w_llist_ref == NULL) /* quickfix window */
3053 stuffReadbuff((char_u *)":.cc\n");
3054 else /* location list window */
3055 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 got_click = FALSE; /* ignore drag&release now */
3057 }
3058#endif
3059
3060 /*
3061 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3062 * under the mouse pointer.
3063 */
3064 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3065 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3066 {
3067 if (State & INSERT)
3068 stuffcharReadbuff(Ctrl_O);
3069 stuffcharReadbuff(Ctrl_RSB);
3070 got_click = FALSE; /* ignore drag&release now */
3071 }
3072
3073 /*
3074 * Shift-Mouse click searches for the next occurrence of the word under
3075 * the mouse pointer
3076 */
3077 else if ((mod_mask & MOD_MASK_SHIFT))
3078 {
3079 if (State & INSERT
3080#ifdef FEAT_VISUAL
3081 || (VIsual_active && VIsual_select)
3082#endif
3083 )
3084 stuffcharReadbuff(Ctrl_O);
3085 if (which_button == MOUSE_LEFT)
3086 stuffcharReadbuff('*');
3087 else /* MOUSE_RIGHT */
3088 stuffcharReadbuff('#');
3089 }
3090
3091 /* Handle double clicks, unless on status line */
3092 else if (in_status_line)
3093 {
3094#ifdef FEAT_MOUSESHAPE
3095 if ((is_drag || is_click) && !drag_status_line)
3096 {
3097 drag_status_line = TRUE;
3098 update_mouseshape(-1);
3099 }
3100#endif
3101 }
3102#ifdef FEAT_VERTSPLIT
3103 else if (in_sep_line)
3104 {
3105# ifdef FEAT_MOUSESHAPE
3106 if ((is_drag || is_click) && !drag_sep_line)
3107 {
3108 drag_sep_line = TRUE;
3109 update_mouseshape(-1);
3110 }
3111# endif
3112 }
3113#endif
3114#ifdef FEAT_VISUAL
3115 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3116 && mouse_has(MOUSE_VISUAL))
3117 {
3118 if (is_click || !VIsual_active)
3119 {
3120 if (VIsual_active)
3121 orig_cursor = VIsual;
3122 else
3123 {
3124 check_visual_highlight();
3125 VIsual = curwin->w_cursor;
3126 orig_cursor = VIsual;
3127 VIsual_active = TRUE;
3128 VIsual_reselect = TRUE;
3129 /* start Select mode if 'selectmode' contains "mouse" */
3130 may_start_select('o');
3131 setmouse();
3132 }
3133 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003134 {
3135 /* Double click with ALT pressed makes it blockwise. */
3136 if (mod_mask & MOD_MASK_ALT)
3137 VIsual_mode = Ctrl_V;
3138 else
3139 VIsual_mode = 'v';
3140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3142 VIsual_mode = 'V';
3143 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3144 VIsual_mode = Ctrl_V;
3145#ifdef FEAT_CLIPBOARD
3146 /* Make sure the clipboard gets updated. Needed because start and
3147 * end may still be the same, and the selection needs to be owned */
3148 clip_star.vmode = NUL;
3149#endif
3150 }
3151 /*
3152 * A double click selects a word or a block.
3153 */
3154 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3155 {
3156 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003157 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 if (is_click)
3160 {
3161 /* If the character under the cursor (skipping white space) is
3162 * not a word character, try finding a match and select a (),
3163 * {}, [], #if/#endif, etc. block. */
3164 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003165 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 inc(&end_visual);
3167 if (oap != NULL)
3168 oap->motion_type = MCHAR;
3169 if (oap != NULL
3170 && VIsual_mode == 'v'
3171 && !vim_iswordc(gchar_pos(&end_visual))
3172 && equalpos(curwin->w_cursor, VIsual)
3173 && (pos = findmatch(oap, NUL)) != NULL)
3174 {
3175 curwin->w_cursor = *pos;
3176 if (oap->motion_type == MLINE)
3177 VIsual_mode = 'V';
3178 else if (*p_sel == 'e')
3179 {
3180 if (lt(curwin->w_cursor, VIsual))
3181 ++VIsual.col;
3182 else
3183 ++curwin->w_cursor.col;
3184 }
3185 }
3186 }
3187
3188 if (pos == NULL && (is_click || is_drag))
3189 {
3190 /* When not found a match or when dragging: extend to include
3191 * a word. */
3192 if (lt(curwin->w_cursor, orig_cursor))
3193 {
3194 find_start_of_word(&curwin->w_cursor);
3195 find_end_of_word(&VIsual);
3196 }
3197 else
3198 {
3199 find_start_of_word(&VIsual);
3200 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3201#ifdef FEAT_MBYTE
3202 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003203 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#else
3205 ++curwin->w_cursor.col;
3206#endif
3207 find_end_of_word(&curwin->w_cursor);
3208 }
3209 }
3210 curwin->w_set_curswant = TRUE;
3211 }
3212 if (is_click)
3213 redraw_curbuf_later(INVERTED); /* update the inversion */
3214 }
3215 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003216 {
3217 if (mod_mask & MOD_MASK_ALT)
3218 VIsual_mode = Ctrl_V;
3219 else
3220 VIsual_mode = 'v';
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003224 if ((!VIsual_active && old_active && mode_displayed)
3225 || (VIsual_active && p_smd && msg_silent == 0
3226 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 redraw_cmdline = TRUE;
3228#endif
3229
3230 return moved;
3231}
3232
3233#ifdef FEAT_VISUAL
3234/*
3235 * Move "pos" back to the start of the word it's in.
3236 */
3237 static void
3238find_start_of_word(pos)
3239 pos_T *pos;
3240{
3241 char_u *line;
3242 int cclass;
3243 int col;
3244
3245 line = ml_get(pos->lnum);
3246 cclass = get_mouse_class(line + pos->col);
3247
3248 while (pos->col > 0)
3249 {
3250 col = pos->col - 1;
3251#ifdef FEAT_MBYTE
3252 col -= (*mb_head_off)(line, line + col);
3253#endif
3254 if (get_mouse_class(line + col) != cclass)
3255 break;
3256 pos->col = col;
3257 }
3258}
3259
3260/*
3261 * Move "pos" forward to the end of the word it's in.
3262 * When 'selection' is "exclusive", the position is just after the word.
3263 */
3264 static void
3265find_end_of_word(pos)
3266 pos_T *pos;
3267{
3268 char_u *line;
3269 int cclass;
3270 int col;
3271
3272 line = ml_get(pos->lnum);
3273 if (*p_sel == 'e' && pos->col > 0)
3274 {
3275 --pos->col;
3276#ifdef FEAT_MBYTE
3277 pos->col -= (*mb_head_off)(line, line + pos->col);
3278#endif
3279 }
3280 cclass = get_mouse_class(line + pos->col);
3281 while (line[pos->col] != NUL)
3282 {
3283#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003284 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#else
3286 col = pos->col + 1;
3287#endif
3288 if (get_mouse_class(line + col) != cclass)
3289 {
3290 if (*p_sel == 'e')
3291 pos->col = col;
3292 break;
3293 }
3294 pos->col = col;
3295 }
3296}
3297
3298/*
3299 * Get class of a character for selection: same class means same word.
3300 * 0: blank
3301 * 1: punctuation groups
3302 * 2: normal word character
3303 * >2: multi-byte word character.
3304 */
3305 static int
3306get_mouse_class(p)
3307 char_u *p;
3308{
3309 int c;
3310
3311#ifdef FEAT_MBYTE
3312 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3313 return mb_get_class(p);
3314#endif
3315
3316 c = *p;
3317 if (c == ' ' || c == '\t')
3318 return 0;
3319
3320 if (vim_iswordc(c))
3321 return 2;
3322
3323 /*
3324 * There are a few special cases where we want certain combinations of
3325 * characters to be considered as a single word. These are things like
3326 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003327 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 */
3329 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3330 return 1;
3331 return c;
3332}
3333#endif /* FEAT_VISUAL */
3334#endif /* FEAT_MOUSE */
3335
3336#if defined(FEAT_VISUAL) || defined(PROTO)
3337/*
3338 * Check if highlighting for visual mode is possible, give a warning message
3339 * if not.
3340 */
3341 void
3342check_visual_highlight()
3343{
3344 static int did_check = FALSE;
3345
3346 if (full_screen)
3347 {
3348 if (!did_check && hl_attr(HLF_V) == 0)
3349 MSG(_("Warning: terminal cannot highlight"));
3350 did_check = TRUE;
3351 }
3352}
3353
3354/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003355 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 * This function should ALWAYS be called to end Visual mode, except from
3357 * do_pending_operator().
3358 */
3359 void
3360end_visual_mode()
3361{
3362#ifdef FEAT_CLIPBOARD
3363 /*
3364 * If we are using the clipboard, then remember what was selected in case
3365 * we need to paste it somewhere while we still own the selection.
3366 * Only do this when the clipboard is already owned. Don't want to grab
3367 * the selection when hitting ESC.
3368 */
3369 if (clip_star.available && clip_star.owned)
3370 clip_auto_select();
3371#endif
3372
3373 VIsual_active = FALSE;
3374#ifdef FEAT_MOUSE
3375 setmouse();
3376 mouse_dragging = 0;
3377#endif
3378
3379 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003380 curbuf->b_visual.vi_mode = VIsual_mode;
3381 curbuf->b_visual.vi_start = VIsual;
3382 curbuf->b_visual.vi_end = curwin->w_cursor;
3383 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#ifdef FEAT_EVAL
3385 curbuf->b_visual_mode_eval = VIsual_mode;
3386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#ifdef FEAT_VIRTUALEDIT
3388 if (!virtual_active())
3389 curwin->w_cursor.coladd = 0;
3390#endif
3391
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003392 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 clear_cmdline = TRUE; /* unshow visual mode later */
3394#ifdef FEAT_CMDL_INFO
3395 else
3396 clear_showcmd();
3397#endif
3398
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003399 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400}
3401
3402/*
3403 * Reset VIsual_active and VIsual_reselect.
3404 */
3405 void
3406reset_VIsual_and_resel()
3407{
3408 if (VIsual_active)
3409 {
3410 end_visual_mode();
3411 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3412 }
3413 VIsual_reselect = FALSE;
3414}
3415
3416/*
3417 * Reset VIsual_active and VIsual_reselect if it's set.
3418 */
3419 void
3420reset_VIsual()
3421{
3422 if (VIsual_active)
3423 {
3424 end_visual_mode();
3425 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3426 VIsual_reselect = FALSE;
3427 }
3428}
3429#endif /* FEAT_VISUAL */
3430
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3433
3434/*
3435 * Check for a balloon-eval special item to include when searching for an
3436 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3437 * Returns TRUE if the character at "*ptr" should be included.
3438 * "dir" is FORWARD or BACKWARD, the direction of searching.
3439 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3440 * "bnp" points to a counter for square brackets.
3441 */
3442 static int
3443find_is_eval_item(ptr, colp, bnp, dir)
3444 char_u *ptr;
3445 int *colp;
3446 int *bnp;
3447 int dir;
3448{
3449 /* Accept everything inside []. */
3450 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3451 ++*bnp;
3452 if (*bnp > 0)
3453 {
3454 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3455 --*bnp;
3456 return TRUE;
3457 }
3458
3459 /* skip over "s.var" */
3460 if (*ptr == '.')
3461 return TRUE;
3462
3463 /* two-character item: s->var */
3464 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3465 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3466 {
3467 *colp += dir;
3468 return TRUE;
3469 }
3470 return FALSE;
3471}
3472#endif
3473
3474/*
3475 * Find the identifier under or to the right of the cursor.
3476 * "find_type" can have one of three values:
3477 * FIND_IDENT: find an identifier (keyword)
3478 * FIND_STRING: find any non-white string
3479 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003480 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 *
3482 * There are three steps:
3483 * 1. Search forward for the start of an identifier/string. Doesn't move if
3484 * already on one.
3485 * 2. Search backward for the start of this identifier/string.
3486 * This doesn't match the real Vi but I like it a little better and it
3487 * shouldn't bother anyone.
3488 * 3. Search forward to the end of this identifier/string.
3489 * When FIND_IDENT isn't defined, we backup until a blank.
3490 *
3491 * Returns the length of the string, or zero if no string is found.
3492 * If a string is found, a pointer to the string is put in "*string". This
3493 * string is not always NUL terminated.
3494 */
3495 int
3496find_ident_under_cursor(string, find_type)
3497 char_u **string;
3498 int find_type;
3499{
3500 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3501 curwin->w_cursor.col, string, find_type);
3502}
3503
3504/*
3505 * Like find_ident_under_cursor(), but for any window and any position.
3506 * However: Uses 'iskeyword' from the current window!.
3507 */
3508 int
3509find_ident_at_pos(wp, lnum, startcol, string, find_type)
3510 win_T *wp;
3511 linenr_T lnum;
3512 colnr_T startcol;
3513 char_u **string;
3514 int find_type;
3515{
3516 char_u *ptr;
3517 int col = 0; /* init to shut up GCC */
3518 int i;
3519#ifdef FEAT_MBYTE
3520 int this_class = 0;
3521 int prev_class;
3522 int prevcol;
3523#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 int bn = 0; /* bracket nesting */
3526#endif
3527
3528 /*
3529 * if i == 0: try to find an identifier
3530 * if i == 1: try to find any non-white string
3531 */
3532 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3533 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3534 {
3535 /*
3536 * 1. skip to start of identifier/string
3537 */
3538 col = startcol;
3539#ifdef FEAT_MBYTE
3540 if (has_mbyte)
3541 {
3542 while (ptr[col] != NUL)
3543 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* Stop at a ']' to evaluate "a[x]". */
3546 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3547 break;
3548# endif
3549 this_class = mb_get_class(ptr + col);
3550 if (this_class != 0 && (i == 1 || this_class != 1))
3551 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003552 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554 }
3555 else
3556#endif
3557 while (ptr[col] != NUL
3558 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3561# endif
3562 )
3563 ++col;
3564
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 /* When starting on a ']' count it, so that we include the '['. */
3567 bn = ptr[col] == ']';
3568#endif
3569
3570 /*
3571 * 2. Back up to start of identifier/string.
3572 */
3573#ifdef FEAT_MBYTE
3574 if (has_mbyte)
3575 {
3576 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3579 this_class = mb_get_class((char_u *)"a");
3580 else
3581# endif
3582 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003583 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3586 prev_class = mb_get_class(ptr + prevcol);
3587 if (this_class != prev_class
3588 && (i == 0
3589 || prev_class == 0
3590 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003591# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 && (!(find_type & FIND_EVAL)
3593 || prevcol == 0
3594 || !find_is_eval_item(ptr + prevcol, &prevcol,
3595 &bn, BACKWARD))
3596# endif
3597 )
3598 break;
3599 col = prevcol;
3600 }
3601
3602 /* If we don't want just any old string, or we've found an
3603 * identifier, stop searching. */
3604 if (this_class > 2)
3605 this_class = 2;
3606 if (!(find_type & FIND_STRING) || this_class == 2)
3607 break;
3608 }
3609 else
3610#endif
3611 {
3612 while (col > 0
3613 && ((i == 0
3614 ? vim_iswordc(ptr[col - 1])
3615 : (!vim_iswhite(ptr[col - 1])
3616 && (!(find_type & FIND_IDENT)
3617 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 || ((find_type & FIND_EVAL)
3620 && col > 1
3621 && find_is_eval_item(ptr + col - 1, &col,
3622 &bn, BACKWARD))
3623#endif
3624 ))
3625 --col;
3626
3627 /* If we don't want just any old string, or we've found an
3628 * identifier, stop searching. */
3629 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3630 break;
3631 }
3632 }
3633
3634 if (ptr[col] == NUL || (i == 0 && (
3635#ifdef FEAT_MBYTE
3636 has_mbyte ? this_class != 2 :
3637#endif
3638 !vim_iswordc(ptr[col]))))
3639 {
3640 /*
3641 * didn't find an identifier or string
3642 */
3643 if (find_type & FIND_STRING)
3644 EMSG(_("E348: No string under cursor"));
3645 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003646 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 return 0;
3648 }
3649 ptr += col;
3650 *string = ptr;
3651
3652 /*
3653 * 3. Find the end if the identifier/string.
3654 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003655#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 bn = 0;
3657 startcol -= col;
3658#endif
3659 col = 0;
3660#ifdef FEAT_MBYTE
3661 if (has_mbyte)
3662 {
3663 /* Search for point of changing multibyte character class. */
3664 this_class = mb_get_class(ptr);
3665 while (ptr[col] != NUL
3666 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3667 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003668# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 || ((find_type & FIND_EVAL)
3670 && col <= (int)startcol
3671 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3672# endif
3673 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003674 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 else
3677#endif
3678 while ((i == 0 ? vim_iswordc(ptr[col])
3679 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003680# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 || ((find_type & FIND_EVAL)
3682 && col <= (int)startcol
3683 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3684# endif
3685 )
3686 {
3687 ++col;
3688 }
3689
3690 return col;
3691}
3692
3693/*
3694 * Prepare for redo of a normal command.
3695 */
3696 static void
3697prep_redo_cmd(cap)
3698 cmdarg_T *cap;
3699{
3700 prep_redo(cap->oap->regname, cap->count0,
3701 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3702}
3703
3704/*
3705 * Prepare for redo of any command.
3706 * Note that only the last argument can be a multi-byte char.
3707 */
3708 static void
3709prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3710 int regname;
3711 long num;
3712 int cmd1;
3713 int cmd2;
3714 int cmd3;
3715 int cmd4;
3716 int cmd5;
3717{
3718 ResetRedobuff();
3719 if (regname != 0) /* yank from specified buffer */
3720 {
3721 AppendCharToRedobuff('"');
3722 AppendCharToRedobuff(regname);
3723 }
3724 if (num)
3725 AppendNumberToRedobuff(num);
3726
3727 if (cmd1 != NUL)
3728 AppendCharToRedobuff(cmd1);
3729 if (cmd2 != NUL)
3730 AppendCharToRedobuff(cmd2);
3731 if (cmd3 != NUL)
3732 AppendCharToRedobuff(cmd3);
3733 if (cmd4 != NUL)
3734 AppendCharToRedobuff(cmd4);
3735 if (cmd5 != NUL)
3736 AppendCharToRedobuff(cmd5);
3737}
3738
3739/*
3740 * check for operator active and clear it
3741 *
3742 * return TRUE if operator was active
3743 */
3744 static int
3745checkclearop(oap)
3746 oparg_T *oap;
3747{
3748 if (oap->op_type == OP_NOP)
3749 return FALSE;
3750 clearopbeep(oap);
3751 return TRUE;
3752}
3753
3754/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003755 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003757 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 */
3759 static int
3760checkclearopq(oap)
3761 oparg_T *oap;
3762{
3763 if (oap->op_type == OP_NOP
3764#ifdef FEAT_VISUAL
3765 && !VIsual_active
3766#endif
3767 )
3768 return FALSE;
3769 clearopbeep(oap);
3770 return TRUE;
3771}
3772
3773 static void
3774clearop(oap)
3775 oparg_T *oap;
3776{
3777 oap->op_type = OP_NOP;
3778 oap->regname = 0;
3779 oap->motion_force = NUL;
3780 oap->use_reg_one = FALSE;
3781}
3782
3783 static void
3784clearopbeep(oap)
3785 oparg_T *oap;
3786{
3787 clearop(oap);
3788 beep_flush();
3789}
3790
3791#ifdef FEAT_VISUAL
3792/*
3793 * Remove the shift modifier from a special key.
3794 */
3795 static void
3796unshift_special(cap)
3797 cmdarg_T *cap;
3798{
3799 switch (cap->cmdchar)
3800 {
3801 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3802 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3803 case K_S_UP: cap->cmdchar = K_UP; break;
3804 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3805 case K_S_HOME: cap->cmdchar = K_HOME; break;
3806 case K_S_END: cap->cmdchar = K_END; break;
3807 }
3808 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3809}
3810#endif
3811
3812#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3813/*
3814 * Routines for displaying a partly typed command
3815 */
3816
3817#ifdef FEAT_VISUAL /* need room for size of Visual area */
3818# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3819#else
3820# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3821#endif
3822static char_u showcmd_buf[SHOWCMD_BUFLEN];
3823static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3824static int showcmd_is_clear = TRUE;
3825static int showcmd_visual = FALSE;
3826
3827static void display_showcmd __ARGS((void));
3828
3829 void
3830clear_showcmd()
3831{
3832 if (!p_sc)
3833 return;
3834
3835#ifdef FEAT_VISUAL
3836 if (VIsual_active && !char_avail())
3837 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003838 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 long lines;
3840 colnr_T leftcol, rightcol;
3841 linenr_T top, bot;
3842
3843 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003844 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 top = VIsual.lnum;
3847 bot = curwin->w_cursor.lnum;
3848 }
3849 else
3850 {
3851 top = curwin->w_cursor.lnum;
3852 bot = VIsual.lnum;
3853 }
3854# ifdef FEAT_FOLDING
3855 /* Include closed folds as a whole. */
3856 hasFolding(top, &top, NULL);
3857 hasFolding(bot, NULL, &bot);
3858# endif
3859 lines = bot - top + 1;
3860
3861 if (VIsual_mode == Ctrl_V)
3862 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003863# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003864 char_u *saved_sbr = p_sbr;
3865
3866 /* Make 'sbr' empty for a moment to get the correct size. */
3867 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003870# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003871 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3874 (long)(rightcol - leftcol + 1));
3875 }
3876 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3877 sprintf((char *)showcmd_buf, "%ld", lines);
3878 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003879 {
3880 char_u *s, *e;
3881 int l;
3882 int bytes = 0;
3883 int chars = 0;
3884
3885 if (cursor_bot)
3886 {
3887 s = ml_get_pos(&VIsual);
3888 e = ml_get_cursor();
3889 }
3890 else
3891 {
3892 s = ml_get_cursor();
3893 e = ml_get_pos(&VIsual);
3894 }
3895 while ((*p_sel != 'e') ? s <= e : s < e)
3896 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003897# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003898 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003899# else
3900 l = (*s == NUL) ? 0 : 1;
3901# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003902 if (l == 0)
3903 {
3904 ++bytes;
3905 ++chars;
3906 break; /* end of line */
3907 }
3908 bytes += l;
3909 ++chars;
3910 s += l;
3911 }
3912 if (bytes == chars)
3913 sprintf((char *)showcmd_buf, "%d", chars);
3914 else
3915 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3918 showcmd_visual = TRUE;
3919 }
3920 else
3921#endif
3922 {
3923 showcmd_buf[0] = NUL;
3924 showcmd_visual = FALSE;
3925
3926 /* Don't actually display something if there is nothing to clear. */
3927 if (showcmd_is_clear)
3928 return;
3929 }
3930
3931 display_showcmd();
3932}
3933
3934/*
3935 * Add 'c' to string of shown command chars.
3936 * Return TRUE if output has been written (and setcursor() has been called).
3937 */
3938 int
3939add_to_showcmd(c)
3940 int c;
3941{
3942 char_u *p;
3943 int old_len;
3944 int extra_len;
3945 int overflow;
3946#if defined(FEAT_MOUSE)
3947 int i;
3948 static int ignore[] =
3949 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003950# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3952 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 K_IGNORE,
3955 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3956 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3957 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003958 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003960 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 0
3962 };
3963#endif
3964
Bram Moolenaar09df3122006-01-23 22:23:09 +00003965 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return FALSE;
3967
3968 if (showcmd_visual)
3969 {
3970 showcmd_buf[0] = NUL;
3971 showcmd_visual = FALSE;
3972 }
3973
3974#if defined(FEAT_MOUSE)
3975 /* Ignore keys that are scrollbar updates and mouse clicks */
3976 if (IS_SPECIAL(c))
3977 for (i = 0; ignore[i] != 0; ++i)
3978 if (ignore[i] == c)
3979 return FALSE;
3980#endif
3981
3982 p = transchar(c);
3983 old_len = (int)STRLEN(showcmd_buf);
3984 extra_len = (int)STRLEN(p);
3985 overflow = old_len + extra_len - SHOWCMD_COLS;
3986 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003987 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3988 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 STRCAT(showcmd_buf, p);
3990
3991 if (char_avail())
3992 return FALSE;
3993
3994 display_showcmd();
3995
3996 return TRUE;
3997}
3998
3999 void
4000add_to_showcmd_c(c)
4001 int c;
4002{
4003 if (!add_to_showcmd(c))
4004 setcursor();
4005}
4006
4007/*
4008 * Delete 'len' characters from the end of the shown command.
4009 */
4010 static void
4011del_from_showcmd(len)
4012 int len;
4013{
4014 int old_len;
4015
4016 if (!p_sc)
4017 return;
4018
4019 old_len = (int)STRLEN(showcmd_buf);
4020 if (len > old_len)
4021 len = old_len;
4022 showcmd_buf[old_len - len] = NUL;
4023
4024 if (!char_avail())
4025 display_showcmd();
4026}
4027
4028/*
4029 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4030 * something and there is a partial mapping.
4031 */
4032 void
4033push_showcmd()
4034{
4035 if (p_sc)
4036 STRCPY(old_showcmd_buf, showcmd_buf);
4037}
4038
4039 void
4040pop_showcmd()
4041{
4042 if (!p_sc)
4043 return;
4044
4045 STRCPY(showcmd_buf, old_showcmd_buf);
4046
4047 display_showcmd();
4048}
4049
4050 static void
4051display_showcmd()
4052{
4053 int len;
4054
4055 cursor_off();
4056
4057 len = (int)STRLEN(showcmd_buf);
4058 if (len == 0)
4059 showcmd_is_clear = TRUE;
4060 else
4061 {
4062 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4063 showcmd_is_clear = FALSE;
4064 }
4065
4066 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004067 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4068 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 */
4070 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4071
4072 setcursor(); /* put cursor back where it belongs */
4073}
4074#endif
4075
4076#ifdef FEAT_SCROLLBIND
4077/*
4078 * When "check" is FALSE, prepare for commands that scroll the window.
4079 * When "check" is TRUE, take care of scroll-binding after the window has
4080 * scrolled. Called from normal_cmd() and edit().
4081 */
4082 void
4083do_check_scrollbind(check)
4084 int check;
4085{
4086 static win_T *old_curwin = NULL;
4087 static linenr_T old_topline = 0;
4088#ifdef FEAT_DIFF
4089 static int old_topfill = 0;
4090#endif
4091 static buf_T *old_buf = NULL;
4092 static colnr_T old_leftcol = 0;
4093
4094 if (check && curwin->w_p_scb)
4095 {
4096 /* If a ":syncbind" command was just used, don't scroll, only reset
4097 * the values. */
4098 if (did_syncbind)
4099 did_syncbind = FALSE;
4100 else if (curwin == old_curwin)
4101 {
4102 /*
4103 * Synchronize other windows, as necessary according to
4104 * 'scrollbind'. Don't do this after an ":edit" command, except
4105 * when 'diff' is set.
4106 */
4107 if ((curwin->w_buffer == old_buf
4108#ifdef FEAT_DIFF
4109 || curwin->w_p_diff
4110#endif
4111 )
4112 && (curwin->w_topline != old_topline
4113#ifdef FEAT_DIFF
4114 || curwin->w_topfill != old_topfill
4115#endif
4116 || curwin->w_leftcol != old_leftcol))
4117 {
4118 check_scrollbind(curwin->w_topline - old_topline,
4119 (long)(curwin->w_leftcol - old_leftcol));
4120 }
4121 }
4122 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4123 {
4124 /*
4125 * When switching between windows, make sure that the relative
4126 * vertical offset is valid for the new window. The relative
4127 * offset is invalid whenever another 'scrollbind' window has
4128 * scrolled to a point that would force the current window to
4129 * scroll past the beginning or end of its buffer. When the
4130 * resync is performed, some of the other 'scrollbind' windows may
4131 * need to jump so that the current window's relative position is
4132 * visible on-screen.
4133 */
4134 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4135 }
4136 curwin->w_scbind_pos = curwin->w_topline;
4137 }
4138
4139 old_curwin = curwin;
4140 old_topline = curwin->w_topline;
4141#ifdef FEAT_DIFF
4142 old_topfill = curwin->w_topfill;
4143#endif
4144 old_buf = curwin->w_buffer;
4145 old_leftcol = curwin->w_leftcol;
4146}
4147
4148/*
4149 * Synchronize any windows that have "scrollbind" set, based on the
4150 * number of rows by which the current window has changed
4151 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4152 */
4153 void
4154check_scrollbind(topline_diff, leftcol_diff)
4155 linenr_T topline_diff;
4156 long leftcol_diff;
4157{
4158 int want_ver;
4159 int want_hor;
4160 win_T *old_curwin = curwin;
4161 buf_T *old_curbuf = curbuf;
4162#ifdef FEAT_VISUAL
4163 int old_VIsual_select = VIsual_select;
4164 int old_VIsual_active = VIsual_active;
4165#endif
4166 colnr_T tgt_leftcol = curwin->w_leftcol;
4167 long topline;
4168 long y;
4169
4170 /*
4171 * check 'scrollopt' string for vertical and horizontal scroll options
4172 */
4173 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4174#ifdef FEAT_DIFF
4175 want_ver |= old_curwin->w_p_diff;
4176#endif
4177 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4178
4179 /*
4180 * loop through the scrollbound windows and scroll accordingly
4181 */
4182#ifdef FEAT_VISUAL
4183 VIsual_select = VIsual_active = 0;
4184#endif
4185 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4186 {
4187 curbuf = curwin->w_buffer;
4188 /* skip original window and windows with 'noscrollbind' */
4189 if (curwin != old_curwin && curwin->w_p_scb)
4190 {
4191 /*
4192 * do the vertical scroll
4193 */
4194 if (want_ver)
4195 {
4196#ifdef FEAT_DIFF
4197 if (old_curwin->w_p_diff && curwin->w_p_diff)
4198 {
4199 diff_set_topline(old_curwin, curwin);
4200 }
4201 else
4202#endif
4203 {
4204 curwin->w_scbind_pos += topline_diff;
4205 topline = curwin->w_scbind_pos;
4206 if (topline > curbuf->b_ml.ml_line_count)
4207 topline = curbuf->b_ml.ml_line_count;
4208 if (topline < 1)
4209 topline = 1;
4210
4211 y = topline - curwin->w_topline;
4212 if (y > 0)
4213 scrollup(y, FALSE);
4214 else
4215 scrolldown(-y, FALSE);
4216 }
4217
4218 redraw_later(VALID);
4219 cursor_correct();
4220#ifdef FEAT_WINDOWS
4221 curwin->w_redr_status = TRUE;
4222#endif
4223 }
4224
4225 /*
4226 * do the horizontal scroll
4227 */
4228 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4229 {
4230 curwin->w_leftcol = tgt_leftcol;
4231 leftcol_changed();
4232 }
4233 }
4234 }
4235
4236 /*
4237 * reset current-window
4238 */
4239#ifdef FEAT_VISUAL
4240 VIsual_select = old_VIsual_select;
4241 VIsual_active = old_VIsual_active;
4242#endif
4243 curwin = old_curwin;
4244 curbuf = old_curbuf;
4245}
4246#endif /* #ifdef FEAT_SCROLLBIND */
4247
4248/*
4249 * Command character that's ignored.
4250 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004251 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 static void
4254nv_ignore(cap)
4255 cmdarg_T *cap;
4256{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004257 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258}
4259
4260/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004261 * Command character that doesn't do anything, but unlike nv_ignore() does
4262 * start edit(). Used for "startinsert" executed while starting up.
4263 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004264 static void
4265nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004266 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004267{
4268}
4269
4270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 * Command character doesn't exist.
4272 */
4273 static void
4274nv_error(cap)
4275 cmdarg_T *cap;
4276{
4277 clearopbeep(cap->oap);
4278}
4279
4280/*
4281 * <Help> and <F1> commands.
4282 */
4283 static void
4284nv_help(cap)
4285 cmdarg_T *cap;
4286{
4287 if (!checkclearopq(cap->oap))
4288 ex_help(NULL);
4289}
4290
4291/*
4292 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4293 */
4294 static void
4295nv_addsub(cap)
4296 cmdarg_T *cap;
4297{
4298 if (!checkclearopq(cap->oap)
4299 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4300 prep_redo_cmd(cap);
4301}
4302
4303/*
4304 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4305 */
4306 static void
4307nv_page(cap)
4308 cmdarg_T *cap;
4309{
4310 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 {
4312#ifdef FEAT_WINDOWS
4313 if (mod_mask & MOD_MASK_CTRL)
4314 {
4315 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4316 if (cap->arg == BACKWARD)
4317 goto_tabpage(-(int)cap->count1);
4318 else
4319 goto_tabpage((int)cap->count0);
4320 }
4321 else
4322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325}
4326
4327/*
4328 * Implementation of "gd" and "gD" command.
4329 */
4330 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004331nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 oparg_T *oap;
4333 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004334 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 char_u *ptr;
4338
Bram Moolenaard9d30582005-05-18 22:10:28 +00004339 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004340 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342#ifdef FEAT_FOLDING
4343 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4344 foldOpenCursor();
4345#endif
4346}
4347
4348/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004349 * Search for variable declaration of "ptr[len]".
4350 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4351 * current file ("gD").
4352 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004353 * Return FAIL when not found.
4354 */
4355 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004356find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004357 char_u *ptr;
4358 int len;
4359 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004361 int searchflags; /* flags passed to searchit() */
4362{
4363 char_u *pat;
4364 pos_T old_pos;
4365 pos_T par_pos;
4366 pos_T found_pos;
4367 int t;
4368 int save_p_ws;
4369 int save_p_scs;
4370 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004371 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372
4373 if ((pat = alloc(len + 7)) == NULL)
4374 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004375
4376 /* Put "\V" before the pattern to avoid that the special meaning of "."
4377 * and "~" causes trouble. */
4378 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4379 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 old_pos = curwin->w_cursor;
4381 save_p_ws = p_ws;
4382 save_p_scs = p_scs;
4383 p_ws = FALSE; /* don't wrap around end of file now */
4384 p_scs = FALSE; /* don't switch ignorecase off now */
4385
4386 /*
4387 * With "gD" go to line 1.
4388 * With "gd" Search back for the start of the current function, then go
4389 * back until a blank line. If this fails go to line 1.
4390 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004391 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
4393 setpcmark(); /* Set in findpar() otherwise */
4394 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004395 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else
4398 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004399 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4401 --curwin->w_cursor.lnum;
4402 }
4403 curwin->w_cursor.col = 0;
4404
4405 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004406 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 for (;;)
4408 {
4409 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004410 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004411 if (curwin->w_cursor.lnum >= old_pos.lnum)
4412 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004413
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004414 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004415 {
4416 pos_T *pos;
4417
4418 /* Check that the block the match is in doesn't end before the
4419 * position where we started the search from. */
4420 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4421 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4422 && pos->lnum < old_pos.lnum)
4423 continue;
4424 }
4425
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004426 if (t == FAIL)
4427 {
4428 /* If we previously found a valid position, use it. */
4429 if (found_pos.lnum != 0)
4430 {
4431 curwin->w_cursor = found_pos;
4432 t = OK;
4433 }
4434 break;
4435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004437 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004438 {
4439 /* Ignore this line, continue at start of next line. */
4440 ++curwin->w_cursor.lnum;
4441 curwin->w_cursor.col = 0;
4442 continue;
4443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004445 if (!locally) /* global search: use first match found */
4446 break;
4447 if (curwin->w_cursor.lnum >= par_pos.lnum)
4448 {
4449 /* If we previously found a valid position, use it. */
4450 if (found_pos.lnum != 0)
4451 curwin->w_cursor = found_pos;
4452 break;
4453 }
4454
4455 /* For finding a local variable and the match is before the "{" search
4456 * to find a later match. For K&R style function declarations this
4457 * skips the function header without types. */
4458 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004460
4461 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004463 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 curwin->w_cursor = old_pos;
4465 }
4466 else
4467 {
4468 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 /* "n" searches forward now */
4470 reset_search_dir();
4471 }
4472
4473 vim_free(pat);
4474 p_ws = save_p_ws;
4475 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004476
4477 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478}
4479
4480/*
4481 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4482 * lines rather than lines in the file.
4483 * 'dist' must be positive.
4484 *
4485 * Return OK if able to move cursor, FAIL otherwise.
4486 */
4487 static int
4488nv_screengo(oap, dir, dist)
4489 oparg_T *oap;
4490 int dir;
4491 long dist;
4492{
4493 int linelen = linetabsize(ml_get_curline());
4494 int retval = OK;
4495 int atend = FALSE;
4496 int n;
4497 int col_off1; /* margin offset for first screen line */
4498 int col_off2; /* margin offset for wrapped screen line */
4499 int width1; /* text width for first screen line */
4500 int width2; /* test width for wrapped screen line */
4501
4502 oap->motion_type = MCHAR;
4503 oap->inclusive = FALSE;
4504
4505 col_off1 = curwin_col_off();
4506 col_off2 = col_off1 - curwin_col_off2();
4507 width1 = W_WIDTH(curwin) - col_off1;
4508 width2 = W_WIDTH(curwin) - col_off2;
4509
4510#ifdef FEAT_VERTSPLIT
4511 if (curwin->w_width != 0)
4512 {
4513#endif
4514 /*
4515 * Instead of sticking at the last character of the buffer line we
4516 * try to stick in the last column of the screen.
4517 */
4518 if (curwin->w_curswant == MAXCOL)
4519 {
4520 atend = TRUE;
4521 validate_virtcol();
4522 if (width1 <= 0)
4523 curwin->w_curswant = 0;
4524 else
4525 {
4526 curwin->w_curswant = width1 - 1;
4527 if (curwin->w_virtcol > curwin->w_curswant)
4528 curwin->w_curswant += ((curwin->w_virtcol
4529 - curwin->w_curswant - 1) / width2 + 1) * width2;
4530 }
4531 }
4532 else
4533 {
4534 if (linelen > width1)
4535 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4536 else
4537 n = width1;
4538 if (curwin->w_curswant > (colnr_T)n + 1)
4539 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4540 * width2;
4541 }
4542
4543 while (dist--)
4544 {
4545 if (dir == BACKWARD)
4546 {
4547 if ((long)curwin->w_curswant >= width2)
4548 /* move back within line */
4549 curwin->w_curswant -= width2;
4550 else
4551 {
4552 /* to previous line */
4553 if (curwin->w_cursor.lnum == 1)
4554 {
4555 retval = FAIL;
4556 break;
4557 }
4558 --curwin->w_cursor.lnum;
4559#ifdef FEAT_FOLDING
4560 /* Move to the start of a closed fold. Don't do that when
4561 * 'foldopen' contains "all": it will open in a moment. */
4562 if (!(fdo_flags & FDO_ALL))
4563 (void)hasFolding(curwin->w_cursor.lnum,
4564 &curwin->w_cursor.lnum, NULL);
4565#endif
4566 linelen = linetabsize(ml_get_curline());
4567 if (linelen > width1)
4568 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4569 + 1) * width2;
4570 }
4571 }
4572 else /* dir == FORWARD */
4573 {
4574 if (linelen > width1)
4575 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4576 else
4577 n = width1;
4578 if (curwin->w_curswant + width2 < (colnr_T)n)
4579 /* move forward within line */
4580 curwin->w_curswant += width2;
4581 else
4582 {
4583 /* to next line */
4584#ifdef FEAT_FOLDING
4585 /* Move to the end of a closed fold. */
4586 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4587 &curwin->w_cursor.lnum);
4588#endif
4589 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4590 {
4591 retval = FAIL;
4592 break;
4593 }
4594 curwin->w_cursor.lnum++;
4595 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004596 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598 }
4599 }
4600#ifdef FEAT_VERTSPLIT
4601 }
4602#endif
4603
4604 coladvance(curwin->w_curswant);
4605
4606#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4607 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4608 {
4609 /*
4610 * Check for landing on a character that got split at the end of the
4611 * last line. We want to advance a screenline, not end up in the same
4612 * screenline or move two screenlines.
4613 */
4614 validate_virtcol();
4615 if (curwin->w_virtcol > curwin->w_curswant
4616 && (curwin->w_curswant < (colnr_T)width1
4617 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4618 : ((curwin->w_curswant - width1) % width2
4619 > (colnr_T)width2 / 2)))
4620 --curwin->w_cursor.col;
4621 }
4622#endif
4623
4624 if (atend)
4625 curwin->w_curswant = MAXCOL; /* stick in the last column */
4626
4627 return retval;
4628}
4629
4630#ifdef FEAT_MOUSE
4631/*
4632 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4633 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004634 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4635 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 */
4637 static void
4638nv_mousescroll(cap)
4639 cmdarg_T *cap;
4640{
4641# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004642 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 /* Currently we only get the mouse coordinates in the GUI. */
4645 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4646 {
4647 int row, col;
4648
4649 row = mouse_row;
4650 col = mouse_col;
4651
4652 /* find the window at the pointer coordinates */
4653 curwin = mouse_find_win(&row, &col);
4654 curbuf = curwin->w_buffer;
4655 }
4656# endif
4657
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004658 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4661 {
4662 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4663 }
4664 else
4665 {
4666 cap->count1 = 3;
4667 cap->count0 = 3;
4668 nv_scroll_line(cap);
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
4673 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004674 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4675 if (!curwin->w_p_wrap)
4676 {
4677 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004678
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004679 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4680 step = W_WIDTH(curwin);
4681 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4682 if (val < 0)
4683 val = 0;
4684
4685 gui_do_horiz_scroll(val, TRUE);
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4691 curwin->w_redr_status = TRUE;
4692
4693 curwin = old_curwin;
4694 curbuf = curwin->w_buffer;
4695# endif
4696}
4697
4698/*
4699 * Mouse clicks and drags.
4700 */
4701 static void
4702nv_mouse(cap)
4703 cmdarg_T *cap;
4704{
4705 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4706}
4707#endif
4708
4709/*
4710 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4711 * cap->arg must be TRUE for CTRL-E.
4712 */
4713 static void
4714nv_scroll_line(cap)
4715 cmdarg_T *cap;
4716{
4717 if (!checkclearop(cap->oap))
4718 scroll_redraw(cap->arg, cap->count1);
4719}
4720
4721/*
4722 * Scroll "count" lines up or down, and redraw.
4723 */
4724 void
4725scroll_redraw(up, count)
4726 int up;
4727 long count;
4728{
4729 linenr_T prev_topline = curwin->w_topline;
4730#ifdef FEAT_DIFF
4731 int prev_topfill = curwin->w_topfill;
4732#endif
4733 linenr_T prev_lnum = curwin->w_cursor.lnum;
4734
4735 if (up)
4736 scrollup(count, TRUE);
4737 else
4738 scrolldown(count, TRUE);
4739 if (p_so)
4740 {
4741 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4742 * valid, otherwise the screen jumps back at the end of the file. */
4743 cursor_correct();
4744 check_cursor_moved(curwin);
4745 curwin->w_valid |= VALID_TOPLINE;
4746
4747 /* If moved back to where we were, at least move the cursor, otherwise
4748 * we get stuck at one position. Don't move the cursor up if the
4749 * first line of the buffer is already on the screen */
4750 while (curwin->w_topline == prev_topline
4751#ifdef FEAT_DIFF
4752 && curwin->w_topfill == prev_topfill
4753#endif
4754 )
4755 {
4756 if (up)
4757 {
4758 if (curwin->w_cursor.lnum > prev_lnum
4759 || cursor_down(1L, FALSE) == FAIL)
4760 break;
4761 }
4762 else
4763 {
4764 if (curwin->w_cursor.lnum < prev_lnum
4765 || prev_topline == 1L
4766 || cursor_up(1L, FALSE) == FAIL)
4767 break;
4768 }
4769 /* Mark w_topline as valid, otherwise the screen jumps back at the
4770 * end of the file. */
4771 check_cursor_moved(curwin);
4772 curwin->w_valid |= VALID_TOPLINE;
4773 }
4774 }
4775 if (curwin->w_cursor.lnum != prev_lnum)
4776 coladvance(curwin->w_curswant);
4777 redraw_later(VALID);
4778}
4779
4780/*
4781 * Commands that start with "z".
4782 */
4783 static void
4784nv_zet(cap)
4785 cmdarg_T *cap;
4786{
4787 long n;
4788 colnr_T col;
4789 int nchar = cap->nchar;
4790#ifdef FEAT_FOLDING
4791 long old_fdl = curwin->w_p_fdl;
4792 int old_fen = curwin->w_p_fen;
4793#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004794#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004795 int undo = FALSE;
4796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 if (VIM_ISDIGIT(nchar))
4799 {
4800 /*
4801 * "z123{nchar}": edit the count before obtaining {nchar}
4802 */
4803 if (checkclearop(cap->oap))
4804 return;
4805 n = nchar - '0';
4806 for (;;)
4807 {
4808#ifdef USE_ON_FLY_SCROLL
4809 dont_scroll = TRUE; /* disallow scrolling here */
4810#endif
4811 ++no_mapping;
4812 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004813 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 --no_mapping;
4816 --allow_keys;
4817#ifdef FEAT_CMDL_INFO
4818 (void)add_to_showcmd(nchar);
4819#endif
4820 if (nchar == K_DEL || nchar == K_KDEL)
4821 n /= 10;
4822 else if (VIM_ISDIGIT(nchar))
4823 n = n * 10 + (nchar - '0');
4824 else if (nchar == CAR)
4825 {
4826#ifdef FEAT_GUI
4827 need_mouse_correct = TRUE;
4828#endif
4829 win_setheight((int)n);
4830 break;
4831 }
4832 else if (nchar == 'l'
4833 || nchar == 'h'
4834 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004835 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 cap->count1 = n ? n * cap->count1 : cap->count1;
4838 goto dozet;
4839 }
4840 else
4841 {
4842 clearopbeep(cap->oap);
4843 break;
4844 }
4845 }
4846 cap->oap->op_type = OP_NOP;
4847 return;
4848 }
4849
4850dozet:
4851 if (
4852#ifdef FEAT_FOLDING
4853 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4854 * and "zC" only in Visual mode. "zj" and "zk" are motion
4855 * commands. */
4856 cap->nchar != 'f' && cap->nchar != 'F'
4857 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4858 && cap->nchar != 'j' && cap->nchar != 'k'
4859 &&
4860#endif
4861 checkclearop(cap->oap))
4862 return;
4863
4864 /*
4865 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4866 * If line number given, set cursor.
4867 */
4868 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4869 && cap->count0
4870 && cap->count0 != curwin->w_cursor.lnum)
4871 {
4872 setpcmark();
4873 if (cap->count0 > curbuf->b_ml.ml_line_count)
4874 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4875 else
4876 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004877 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
4880 switch (nchar)
4881 {
4882 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4883 case '+':
4884 if (cap->count0 == 0)
4885 {
4886 /* No count given: put cursor at the line below screen */
4887 validate_botline(); /* make sure w_botline is valid */
4888 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4889 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4890 else
4891 curwin->w_cursor.lnum = curwin->w_botline;
4892 }
4893 /* FALLTHROUGH */
4894 case NL:
4895 case CAR:
4896 case K_KENTER:
4897 beginline(BL_WHITE | BL_FIX);
4898 /* FALLTHROUGH */
4899
4900 case 't': scroll_cursor_top(0, TRUE);
4901 redraw_later(VALID);
4902 break;
4903
4904 /* "z." and "zz": put cursor in middle of screen */
4905 case '.': beginline(BL_WHITE | BL_FIX);
4906 /* FALLTHROUGH */
4907
4908 case 'z': scroll_cursor_halfway(TRUE);
4909 redraw_later(VALID);
4910 break;
4911
4912 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4913 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4914 * when <count> is at bottom of window, and puts that one at
4915 * bottom of window. */
4916 if (cap->count0 != 0)
4917 {
4918 scroll_cursor_bot(0, TRUE);
4919 curwin->w_cursor.lnum = curwin->w_topline;
4920 }
4921 else if (curwin->w_topline == 1)
4922 curwin->w_cursor.lnum = 1;
4923 else
4924 curwin->w_cursor.lnum = curwin->w_topline - 1;
4925 /* FALLTHROUGH */
4926 case '-':
4927 beginline(BL_WHITE | BL_FIX);
4928 /* FALLTHROUGH */
4929
4930 case 'b': scroll_cursor_bot(0, TRUE);
4931 redraw_later(VALID);
4932 break;
4933
4934 /* "zH" - scroll screen right half-page */
4935 case 'H':
4936 cap->count1 *= W_WIDTH(curwin) / 2;
4937 /* FALLTHROUGH */
4938
4939 /* "zh" - scroll screen to the right */
4940 case 'h':
4941 case K_LEFT:
4942 if (!curwin->w_p_wrap)
4943 {
4944 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4945 curwin->w_leftcol = 0;
4946 else
4947 curwin->w_leftcol -= (colnr_T)cap->count1;
4948 leftcol_changed();
4949 }
4950 break;
4951
4952 /* "zL" - scroll screen left half-page */
4953 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4954 /* FALLTHROUGH */
4955
4956 /* "zl" - scroll screen to the left */
4957 case 'l':
4958 case K_RIGHT:
4959 if (!curwin->w_p_wrap)
4960 {
4961 /* scroll the window left */
4962 curwin->w_leftcol += (colnr_T)cap->count1;
4963 leftcol_changed();
4964 }
4965 break;
4966
4967 /* "zs" - scroll screen, cursor at the start */
4968 case 's': if (!curwin->w_p_wrap)
4969 {
4970#ifdef FEAT_FOLDING
4971 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4972 col = 0; /* like the cursor is in col 0 */
4973 else
4974#endif
4975 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4976 if ((long)col > p_siso)
4977 col -= p_siso;
4978 else
4979 col = 0;
4980 if (curwin->w_leftcol != col)
4981 {
4982 curwin->w_leftcol = col;
4983 redraw_later(NOT_VALID);
4984 }
4985 }
4986 break;
4987
4988 /* "ze" - scroll screen, cursor at the end */
4989 case 'e': if (!curwin->w_p_wrap)
4990 {
4991#ifdef FEAT_FOLDING
4992 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4993 col = 0; /* like the cursor is in col 0 */
4994 else
4995#endif
4996 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4997 n = W_WIDTH(curwin) - curwin_col_off();
4998 if ((long)col + p_siso < n)
4999 col = 0;
5000 else
5001 col = col + p_siso - n + 1;
5002 if (curwin->w_leftcol != col)
5003 {
5004 curwin->w_leftcol = col;
5005 redraw_later(NOT_VALID);
5006 }
5007 }
5008 break;
5009
5010#ifdef FEAT_FOLDING
5011 /* "zF": create fold command */
5012 /* "zf": create fold operator */
5013 case 'F':
5014 case 'f': if (foldManualAllowed(TRUE))
5015 {
5016 cap->nchar = 'f';
5017 nv_operator(cap);
5018 curwin->w_p_fen = TRUE;
5019
5020 /* "zF" is like "zfzf" */
5021 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5022 {
5023 nv_operator(cap);
5024 finish_op = TRUE;
5025 }
5026 }
5027 else
5028 clearopbeep(cap->oap);
5029 break;
5030
5031 /* "zd": delete fold at cursor */
5032 /* "zD": delete fold at cursor recursively */
5033 case 'd':
5034 case 'D': if (foldManualAllowed(FALSE))
5035 {
5036 if (VIsual_active)
5037 nv_operator(cap);
5038 else
5039 deleteFold(curwin->w_cursor.lnum,
5040 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5041 }
5042 break;
5043
5044 /* "zE": erease all folds */
5045 case 'E': if (foldmethodIsManual(curwin))
5046 {
5047 clearFolding(curwin);
5048 changed_window_setting();
5049 }
5050 else if (foldmethodIsMarker(curwin))
5051 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5052 TRUE, FALSE);
5053 else
5054 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5055 break;
5056
5057 /* "zn": fold none: reset 'foldenable' */
5058 case 'n': curwin->w_p_fen = FALSE;
5059 break;
5060
5061 /* "zN": fold Normal: set 'foldenable' */
5062 case 'N': curwin->w_p_fen = TRUE;
5063 break;
5064
5065 /* "zi": invert folding: toggle 'foldenable' */
5066 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5067 break;
5068
5069 /* "za": open closed fold or close open fold at cursor */
5070 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5071 openFold(curwin->w_cursor.lnum, cap->count1);
5072 else
5073 {
5074 closeFold(curwin->w_cursor.lnum, cap->count1);
5075 curwin->w_p_fen = TRUE;
5076 }
5077 break;
5078
5079 /* "zA": open fold at cursor recursively */
5080 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5081 openFoldRecurse(curwin->w_cursor.lnum);
5082 else
5083 {
5084 closeFoldRecurse(curwin->w_cursor.lnum);
5085 curwin->w_p_fen = TRUE;
5086 }
5087 break;
5088
5089 /* "zo": open fold at cursor or Visual area */
5090 case 'o': if (VIsual_active)
5091 nv_operator(cap);
5092 else
5093 openFold(curwin->w_cursor.lnum, cap->count1);
5094 break;
5095
5096 /* "zO": open fold recursively */
5097 case 'O': if (VIsual_active)
5098 nv_operator(cap);
5099 else
5100 openFoldRecurse(curwin->w_cursor.lnum);
5101 break;
5102
5103 /* "zc": close fold at cursor or Visual area */
5104 case 'c': if (VIsual_active)
5105 nv_operator(cap);
5106 else
5107 closeFold(curwin->w_cursor.lnum, cap->count1);
5108 curwin->w_p_fen = TRUE;
5109 break;
5110
5111 /* "zC": close fold recursively */
5112 case 'C': if (VIsual_active)
5113 nv_operator(cap);
5114 else
5115 closeFoldRecurse(curwin->w_cursor.lnum);
5116 curwin->w_p_fen = TRUE;
5117 break;
5118
5119 /* "zv": open folds at the cursor */
5120 case 'v': foldOpenCursor();
5121 break;
5122
5123 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5124 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005125 curwin->w_foldinvalid = TRUE; /* recompute folds */
5126 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 foldOpenCursor();
5128 break;
5129
5130 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5131 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005132 curwin->w_foldinvalid = TRUE; /* recompute folds */
5133 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 break;
5135
5136 /* "zm": fold more */
5137 case 'm': if (curwin->w_p_fdl > 0)
5138 --curwin->w_p_fdl;
5139 old_fdl = -1; /* force an update */
5140 curwin->w_p_fen = TRUE;
5141 break;
5142
5143 /* "zM": close all folds */
5144 case 'M': curwin->w_p_fdl = 0;
5145 old_fdl = -1; /* force an update */
5146 curwin->w_p_fen = TRUE;
5147 break;
5148
5149 /* "zr": reduce folding */
5150 case 'r': ++curwin->w_p_fdl;
5151 break;
5152
5153 /* "zR": open all folds */
5154 case 'R': curwin->w_p_fdl = getDeepestNesting();
5155 old_fdl = -1; /* force an update */
5156 break;
5157
5158 case 'j': /* "zj" move to next fold downwards */
5159 case 'k': /* "zk" move to next fold upwards */
5160 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5161 cap->count1) == FAIL)
5162 clearopbeep(cap->oap);
5163 break;
5164
5165#endif /* FEAT_FOLDING */
5166
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005167#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005168 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5169 ++no_mapping;
5170 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005171 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005172 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005173 --no_mapping;
5174 --allow_keys;
5175#ifdef FEAT_CMDL_INFO
5176 (void)add_to_showcmd(nchar);
5177#endif
5178 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5179 {
5180 clearopbeep(cap->oap);
5181 break;
5182 }
5183 undo = TRUE;
5184 /*FALLTHROUGH*/
5185
Bram Moolenaarb765d632005-06-07 21:00:02 +00005186 case 'g': /* "zg": add good word to word list */
5187 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005188 case 'G': /* "zG": add good word to temp word list */
5189 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005190 {
5191 char_u *ptr = NULL;
5192 int len;
5193
5194 if (checkclearop(cap->oap))
5195 break;
5196# ifdef FEAT_VISUAL
5197 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5198 == FAIL)
5199 return;
5200# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005201 if (ptr == NULL)
5202 {
5203 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005204
5205 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005206 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005207 if (len != 0 && curwin->w_cursor.col <= pos.col)
5208 ptr = ml_get_pos(&curwin->w_cursor);
5209 curwin->w_cursor = pos;
5210 }
5211
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5213 FIND_IDENT)) == 0)
5214 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005215 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005216 (nchar == 'G' || nchar == 'W')
5217 ? 0 : (int)cap->count1,
5218 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005219 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005220 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221
Bram Moolenaar43abc522005-12-10 20:15:02 +00005222 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005223 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005224 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005226#endif
5227
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 default: clearopbeep(cap->oap);
5229 }
5230
5231#ifdef FEAT_FOLDING
5232 /* Redraw when 'foldenable' changed */
5233 if (old_fen != curwin->w_p_fen)
5234 {
5235# ifdef FEAT_DIFF
5236 win_T *wp;
5237
5238 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5239 {
5240 /* Adjust 'foldenable' in diff-synced windows. */
5241 FOR_ALL_WINDOWS(wp)
5242 {
5243 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5244 {
5245 wp->w_p_fen = curwin->w_p_fen;
5246 changed_window_setting_win(wp);
5247 }
5248 }
5249 }
5250# endif
5251 changed_window_setting();
5252 }
5253
5254 /* Redraw when 'foldlevel' changed. */
5255 if (old_fdl != curwin->w_p_fdl)
5256 newFoldLevel();
5257#endif
5258}
5259
5260#ifdef FEAT_GUI
5261/*
5262 * Vertical scrollbar movement.
5263 */
5264 static void
5265nv_ver_scrollbar(cap)
5266 cmdarg_T *cap;
5267{
5268 if (cap->oap->op_type != OP_NOP)
5269 clearopbeep(cap->oap);
5270
5271 /* Even if an operator was pending, we still want to scroll */
5272 gui_do_scroll();
5273}
5274
5275/*
5276 * Horizontal scrollbar movement.
5277 */
5278 static void
5279nv_hor_scrollbar(cap)
5280 cmdarg_T *cap;
5281{
5282 if (cap->oap->op_type != OP_NOP)
5283 clearopbeep(cap->oap);
5284
5285 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005286 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
5288#endif
5289
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005290#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291/*
5292 * Click in GUI tab.
5293 */
5294 static void
5295nv_tabline(cap)
5296 cmdarg_T *cap;
5297{
5298 if (cap->oap->op_type != OP_NOP)
5299 clearopbeep(cap->oap);
5300
5301 /* Even if an operator was pending, we still want to jump tabs. */
5302 goto_tabpage(current_tab);
5303}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005304
5305/*
5306 * Selected item in tab line menu.
5307 */
5308 static void
5309nv_tabmenu(cap)
5310 cmdarg_T *cap;
5311{
5312 if (cap->oap->op_type != OP_NOP)
5313 clearopbeep(cap->oap);
5314
5315 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005316 handle_tabmenu();
5317}
5318
5319/*
5320 * Handle selecting an item of the GUI tab line menu.
5321 * Used in Normal and Insert mode.
5322 */
5323 void
5324handle_tabmenu()
5325{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005326 switch (current_tabmenu)
5327 {
5328 case TABLINE_MENU_CLOSE:
5329 if (current_tab == 0)
5330 do_cmdline_cmd((char_u *)"tabclose");
5331 else
5332 {
5333 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5334 current_tab);
5335 do_cmdline_cmd(IObuff);
5336 }
5337 break;
5338
5339 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005340 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5341 current_tab > 0 ? current_tab - 1 : 999);
5342 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005343 break;
5344
5345 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005346 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5347 current_tab > 0 ? current_tab - 1 : 999);
5348 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005349 break;
5350 }
5351}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005352#endif
5353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354/*
5355 * "Q" command.
5356 */
5357 static void
5358nv_exmode(cap)
5359 cmdarg_T *cap;
5360{
5361 /*
5362 * Ignore 'Q' in Visual mode, just give a beep.
5363 */
5364#ifdef FEAT_VISUAL
5365 if (VIsual_active)
5366 vim_beep();
5367 else
5368#endif
5369 if (!checkclearop(cap->oap))
5370 do_exmode(FALSE);
5371}
5372
5373/*
5374 * Handle a ":" command.
5375 */
5376 static void
5377nv_colon(cap)
5378 cmdarg_T *cap;
5379{
5380 int old_p_im;
5381
5382#ifdef FEAT_VISUAL
5383 if (VIsual_active)
5384 nv_operator(cap);
5385 else
5386#endif
5387 {
5388 if (cap->oap->op_type != OP_NOP)
5389 {
5390 /* Using ":" as a movement is characterwise exclusive. */
5391 cap->oap->motion_type = MCHAR;
5392 cap->oap->inclusive = FALSE;
5393 }
5394 else if (cap->count0)
5395 {
5396 /* translate "count:" into ":.,.+(count - 1)" */
5397 stuffcharReadbuff('.');
5398 if (cap->count0 > 1)
5399 {
5400 stuffReadbuff((char_u *)",.+");
5401 stuffnumReadbuff((long)cap->count0 - 1L);
5402 }
5403 }
5404
5405 /* When typing, don't type below an old message */
5406 if (KeyTyped)
5407 compute_cmdrow();
5408
5409 old_p_im = p_im;
5410
5411 /* get a command line and execute it */
5412 do_cmdline(NULL, getexline, NULL,
5413 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5414
5415 /* If 'insertmode' changed, enter or exit Insert mode */
5416 if (p_im != old_p_im)
5417 {
5418 if (p_im)
5419 restart_edit = 'i';
5420 else
5421 restart_edit = 0;
5422 }
5423
5424 /* The start of the operator may have become invalid by the Ex
5425 * command. */
5426 if (cap->oap->op_type != OP_NOP
5427 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5428 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005429 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 clearopbeep(cap->oap);
5431 }
5432}
5433
5434/*
5435 * Handle CTRL-G command.
5436 */
5437 static void
5438nv_ctrlg(cap)
5439 cmdarg_T *cap;
5440{
5441#ifdef FEAT_VISUAL
5442 if (VIsual_active) /* toggle Selection/Visual mode */
5443 {
5444 VIsual_select = !VIsual_select;
5445 showmode();
5446 }
5447 else
5448#endif
5449 if (!checkclearop(cap->oap))
5450 /* print full name if count given or :cd used */
5451 fileinfo((int)cap->count0, FALSE, TRUE);
5452}
5453
5454/*
5455 * Handle CTRL-H <Backspace> command.
5456 */
5457 static void
5458nv_ctrlh(cap)
5459 cmdarg_T *cap;
5460{
5461#ifdef FEAT_VISUAL
5462 if (VIsual_active && VIsual_select)
5463 {
5464 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5465 v_visop(cap);
5466 }
5467 else
5468#endif
5469 nv_left(cap);
5470}
5471
5472/*
5473 * CTRL-L: clear screen and redraw.
5474 */
5475 static void
5476nv_clear(cap)
5477 cmdarg_T *cap;
5478{
5479 if (!checkclearop(cap->oap))
5480 {
5481#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5482 /*
5483 * Right now, the BeBox doesn't seem to have an easy way to detect
5484 * window resizing, so we cheat and make the user detect it
5485 * manually with CTRL-L instead
5486 */
5487 ui_get_shellsize();
5488#endif
5489#ifdef FEAT_SYN_HL
5490 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005491 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#endif
5493 redraw_later(CLEAR);
5494 }
5495}
5496
5497/*
5498 * CTRL-O: In Select mode: switch to Visual mode for one command.
5499 * Otherwise: Go to older pcmark.
5500 */
5501 static void
5502nv_ctrlo(cap)
5503 cmdarg_T *cap;
5504{
5505#ifdef FEAT_VISUAL
5506 if (VIsual_active && VIsual_select)
5507 {
5508 VIsual_select = FALSE;
5509 showmode();
5510 restart_VIsual_select = 2; /* restart Select mode later */
5511 }
5512 else
5513#endif
5514 {
5515 cap->count1 = -cap->count1;
5516 nv_pcmark(cap);
5517 }
5518}
5519
5520/*
5521 * CTRL-^ command, short for ":e #"
5522 */
5523 static void
5524nv_hat(cap)
5525 cmdarg_T *cap;
5526{
5527 if (!checkclearopq(cap->oap))
5528 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5529 GETF_SETMARK|GETF_ALT, FALSE);
5530}
5531
5532/*
5533 * "Z" commands.
5534 */
5535 static void
5536nv_Zet(cap)
5537 cmdarg_T *cap;
5538{
5539 if (!checkclearopq(cap->oap))
5540 {
5541 switch (cap->nchar)
5542 {
5543 /* "ZZ": equivalent to ":x". */
5544 case 'Z': do_cmdline_cmd((char_u *)"x");
5545 break;
5546
5547 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5548 case 'Q': do_cmdline_cmd((char_u *)"q!");
5549 break;
5550
5551 default: clearopbeep(cap->oap);
5552 }
5553 }
5554}
5555
5556#if defined(FEAT_WINDOWS) || defined(PROTO)
5557/*
5558 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5559 */
5560 void
5561do_nv_ident(c1, c2)
5562 int c1;
5563 int c2;
5564{
5565 oparg_T oa;
5566 cmdarg_T ca;
5567
5568 clear_oparg(&oa);
5569 vim_memset(&ca, 0, sizeof(ca));
5570 ca.oap = &oa;
5571 ca.cmdchar = c1;
5572 ca.nchar = c2;
5573 nv_ident(&ca);
5574}
5575#endif
5576
5577/*
5578 * Handle the commands that use the word under the cursor.
5579 * [g] CTRL-] :ta to current identifier
5580 * [g] 'K' run program for current identifier
5581 * [g] '*' / to current identifier or string
5582 * [g] '#' ? to current identifier or string
5583 * g ']' :tselect for current identifier
5584 */
5585 static void
5586nv_ident(cap)
5587 cmdarg_T *cap;
5588{
5589 char_u *ptr = NULL;
5590 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005591 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u *p;
5593 char_u *kp; /* value of 'keywordprg' */
5594 int kp_help; /* 'keywordprg' is ":help" */
5595 int n = 0; /* init for GCC */
5596 int cmdchar;
5597 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005598 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 char_u *aux_ptr;
5600 int isman;
5601 int isman_s;
5602
5603 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5604 {
5605 cmdchar = cap->nchar;
5606 g_cmd = TRUE;
5607 }
5608 else
5609 {
5610 cmdchar = cap->cmdchar;
5611 g_cmd = FALSE;
5612 }
5613
5614 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5615 cmdchar = '#';
5616
5617 /*
5618 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5619 */
5620 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5621 {
5622#ifdef FEAT_VISUAL
5623 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5624 return;
5625#endif
5626 if (checkclearopq(cap->oap))
5627 return;
5628 }
5629
5630 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5631 (cmdchar == '*' || cmdchar == '#')
5632 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5633 {
5634 clearop(cap->oap);
5635 return;
5636 }
5637
5638 /* Allocate buffer to put the command in. Inserting backslashes can
5639 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5640 * and some numbers. */
5641 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5642 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5643 || STRCMP(kp, ":help") == 0);
5644 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5645 if (buf == NULL)
5646 return;
5647 buf[0] = NUL;
5648
5649 switch (cmdchar)
5650 {
5651 case '*':
5652 case '#':
5653 /*
5654 * Put cursor at start of word, makes search skip the word
5655 * under the cursor.
5656 * Call setpcmark() first, so "*``" puts the cursor back where
5657 * it was.
5658 */
5659 setpcmark();
5660 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5661
5662 if (!g_cmd && vim_iswordp(ptr))
5663 STRCPY(buf, "\\<");
5664 no_smartcase = TRUE; /* don't use 'smartcase' now */
5665 break;
5666
5667 case 'K':
5668 if (kp_help)
5669 STRCPY(buf, "he! ");
5670 else
5671 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005672 /* An external command will probably use an argument starting
5673 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005674 while (*ptr == '-' && n > 0)
5675 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005676 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005677 --n;
5678 }
5679 if (n == 0)
5680 {
5681 EMSG(_(e_noident)); /* found dashes only */
5682 vim_free(buf);
5683 return;
5684 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 /* When a count is given, turn it into a range. Is this
5687 * really what we want? */
5688 isman = (STRCMP(kp, "man") == 0);
5689 isman_s = (STRCMP(kp, "man -s") == 0);
5690 if (cap->count0 != 0 && !(isman || isman_s))
5691 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5692
5693 STRCAT(buf, "! ");
5694 if (cap->count0 == 0 && isman_s)
5695 STRCAT(buf, "man");
5696 else
5697 STRCAT(buf, kp);
5698 STRCAT(buf, " ");
5699 if (cap->count0 != 0 && (isman || isman_s))
5700 {
5701 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5702 STRCAT(buf, " ");
5703 }
5704 }
5705 break;
5706
5707 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005708 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#ifdef FEAT_CSCOPE
5710 if (p_cst)
5711 STRCPY(buf, "cstag ");
5712 else
5713#endif
5714 STRCPY(buf, "ts ");
5715 break;
5716
5717 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005718 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (curbuf->b_help)
5720 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005722 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005723 if (g_cmd)
5724 STRCPY(buf, "tj ");
5725 else
5726 sprintf((char *)buf, "%ldta ", cap->count0);
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729
5730 /*
5731 * Now grab the chars in the identifier
5732 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005733 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005735 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005736 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005737 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005738 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005739 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741 vim_free(buf);
5742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005744 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5745 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005746 {
5747 vim_free(buf);
5748 vim_free(p);
5749 return;
5750 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005751 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005752 STRCAT(buf, p);
5753 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005755 else
5756 {
5757 if (cmdchar == '*')
5758 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5759 else if (cmdchar == '#')
5760 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005761 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005762 {
5763 if (curbuf->b_help)
5764 /* ":help" handles unescaped argument */
5765 aux_ptr = (char_u *)"";
5766 else
5767 aux_ptr = (char_u *)"\\|\"\n[";
5768 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005769 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005770 aux_ptr = (char_u *)"\\|\"\n*?[";
5771
5772 p = buf + STRLEN(buf);
5773 while (n-- > 0)
5774 {
5775 /* put a backslash before \ and some others */
5776 if (vim_strchr(aux_ptr, *ptr) != NULL)
5777 *p++ = '\\';
5778#ifdef FEAT_MBYTE
5779 /* When current byte is a part of multibyte character, copy all
5780 * bytes of that character. */
5781 if (has_mbyte)
5782 {
5783 int i;
5784 int len = (*mb_ptr2len)(ptr) - 1;
5785
5786 for (i = 0; i < len && n >= 1; ++i, --n)
5787 *p++ = *ptr++;
5788 }
5789#endif
5790 *p++ = *ptr++;
5791 }
5792 *p = NUL;
5793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 /*
5796 * Execute the command.
5797 */
5798 if (cmdchar == '*' || cmdchar == '#')
5799 {
5800 if (!g_cmd && (
5801#ifdef FEAT_MBYTE
5802 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5803#endif
5804 vim_iswordc(ptr[-1])))
5805 STRCAT(buf, "\\>");
5806#ifdef FEAT_CMDHIST
5807 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005808 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5810#endif
5811 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5812 }
5813 else
5814 do_cmdline_cmd(buf);
5815
5816 vim_free(buf);
5817}
5818
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005819#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820/*
5821 * Get visually selected text, within one line only.
5822 * Returns FAIL if more than one line selected.
5823 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005824 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825get_visual_text(cap, pp, lenp)
5826 cmdarg_T *cap;
5827 char_u **pp; /* return: start of selected text */
5828 int *lenp; /* return: length of selected text */
5829{
5830 if (VIsual_mode != 'V')
5831 unadjust_for_sel();
5832 if (VIsual.lnum != curwin->w_cursor.lnum)
5833 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005834 if (cap != NULL)
5835 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return FAIL;
5837 }
5838 if (VIsual_mode == 'V')
5839 {
5840 *pp = ml_get_curline();
5841 *lenp = (int)STRLEN(*pp);
5842 }
5843 else
5844 {
5845 if (lt(curwin->w_cursor, VIsual))
5846 {
5847 *pp = ml_get_pos(&curwin->w_cursor);
5848 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5849 }
5850 else
5851 {
5852 *pp = ml_get_pos(&VIsual);
5853 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5854 }
5855#ifdef FEAT_MBYTE
5856 if (has_mbyte)
5857 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005858 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#endif
5860 }
5861 reset_VIsual_and_resel();
5862 return OK;
5863}
5864#endif
5865
5866/*
5867 * CTRL-T: backwards in tag stack
5868 */
5869 static void
5870nv_tagpop(cap)
5871 cmdarg_T *cap;
5872{
5873 if (!checkclearopq(cap->oap))
5874 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5875}
5876
5877/*
5878 * Handle scrolling command 'H', 'L' and 'M'.
5879 */
5880 static void
5881nv_scroll(cap)
5882 cmdarg_T *cap;
5883{
5884 int used = 0;
5885 long n;
5886#ifdef FEAT_FOLDING
5887 linenr_T lnum;
5888#endif
5889 int half;
5890
5891 cap->oap->motion_type = MLINE;
5892 setpcmark();
5893
5894 if (cap->cmdchar == 'L')
5895 {
5896 validate_botline(); /* make sure curwin->w_botline is valid */
5897 curwin->w_cursor.lnum = curwin->w_botline - 1;
5898 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5899 curwin->w_cursor.lnum = 1;
5900 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005901 {
5902#ifdef FEAT_FOLDING
5903 if (hasAnyFolding(curwin))
5904 {
5905 /* Count a fold for one screen line. */
5906 for (n = cap->count1 - 1; n > 0
5907 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5908 {
5909 (void)hasFolding(curwin->w_cursor.lnum,
5910 &curwin->w_cursor.lnum, NULL);
5911 --curwin->w_cursor.lnum;
5912 }
5913 }
5914 else
5915#endif
5916 curwin->w_cursor.lnum -= cap->count1 - 1;
5917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 else
5920 {
5921 if (cap->cmdchar == 'M')
5922 {
5923#ifdef FEAT_DIFF
5924 /* Don't count filler lines above the window. */
5925 used -= diff_check_fill(curwin, curwin->w_topline)
5926 - curwin->w_topfill;
5927#endif
5928 validate_botline(); /* make sure w_empty_rows is valid */
5929 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5930 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5931 {
5932#ifdef FEAT_DIFF
5933 /* Count half he number of filler lines to be "below this
5934 * line" and half to be "above the next line". */
5935 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5936 + n) / 2 >= half)
5937 {
5938 --n;
5939 break;
5940 }
5941#endif
5942 used += plines(curwin->w_topline + n);
5943 if (used >= half)
5944 break;
5945#ifdef FEAT_FOLDING
5946 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5947 n = lnum - curwin->w_topline;
5948#endif
5949 }
5950 if (n > 0 && used > curwin->w_height)
5951 --n;
5952 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005953 else /* (cap->cmdchar == 'H') */
5954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005956#ifdef FEAT_FOLDING
5957 if (hasAnyFolding(curwin))
5958 {
5959 /* Count a fold for one screen line. */
5960 lnum = curwin->w_topline;
5961 while (n-- > 0 && lnum < curwin->w_botline - 1)
5962 {
5963 hasFolding(lnum, NULL, &lnum);
5964 ++lnum;
5965 }
5966 n = lnum - curwin->w_topline;
5967 }
5968#endif
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 curwin->w_cursor.lnum = curwin->w_topline + n;
5971 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5972 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5973 }
5974
5975 cursor_correct(); /* correct for 'so' */
5976 beginline(BL_SOL | BL_FIX);
5977}
5978
5979/*
5980 * Cursor right commands.
5981 */
5982 static void
5983nv_right(cap)
5984 cmdarg_T *cap;
5985{
5986 long n;
5987#ifdef FEAT_VISUAL
5988 int PAST_LINE;
5989#else
5990# define PAST_LINE 0
5991#endif
5992
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005993 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5994 {
5995 /* <C-Right> and <S-Right> move a word or WORD right */
5996 if (mod_mask & MOD_MASK_CTRL)
5997 cap->arg = TRUE;
5998 nv_wordcmd(cap);
5999 return;
6000 }
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 cap->oap->motion_type = MCHAR;
6003 cap->oap->inclusive = FALSE;
6004#ifdef FEAT_VISUAL
6005 PAST_LINE = (VIsual_active && *p_sel != 'o');
6006
6007# ifdef FEAT_VIRTUALEDIT
6008 /*
6009 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006010 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
6012 if (virtual_active())
6013 PAST_LINE = 0;
6014# endif
6015#endif
6016
6017 for (n = cap->count1; n > 0; --n)
6018 {
6019 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006020#ifdef FEAT_VISUAL
6021 || (PAST_LINE && *ml_get_cursor() == NUL)
6022#endif
6023 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 {
6025 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006026 * <Space> wraps to next line if 'whichwrap' has 's'.
6027 * 'l' wraps to next line if 'whichwrap' has 'l'.
6028 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 */
6030 if ( ((cap->cmdchar == ' '
6031 && vim_strchr(p_ww, 's') != NULL)
6032 || (cap->cmdchar == 'l'
6033 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006034 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 && vim_strchr(p_ww, '>') != NULL))
6036 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6037 {
6038 /* When deleting we also count the NL as a character.
6039 * Set cap->oap->inclusive when last char in the line is
6040 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006041 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 && !cap->oap->inclusive
6043 && !lineempty(curwin->w_cursor.lnum))
6044 cap->oap->inclusive = TRUE;
6045 else
6046 {
6047 ++curwin->w_cursor.lnum;
6048 curwin->w_cursor.col = 0;
6049#ifdef FEAT_VIRTUALEDIT
6050 curwin->w_cursor.coladd = 0;
6051#endif
6052 curwin->w_set_curswant = TRUE;
6053 cap->oap->inclusive = FALSE;
6054 }
6055 continue;
6056 }
6057 if (cap->oap->op_type == OP_NOP)
6058 {
6059 /* Only beep and flush if not moved at all */
6060 if (n == cap->count1)
6061 beep_flush();
6062 }
6063 else
6064 {
6065 if (!lineempty(curwin->w_cursor.lnum))
6066 cap->oap->inclusive = TRUE;
6067 }
6068 break;
6069 }
6070#ifdef FEAT_VISUAL
6071 else if (PAST_LINE)
6072 {
6073 curwin->w_set_curswant = TRUE;
6074# ifdef FEAT_VIRTUALEDIT
6075 if (virtual_active())
6076 oneright();
6077 else
6078# endif
6079 {
6080# ifdef FEAT_MBYTE
6081 if (has_mbyte)
6082 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
6085# endif
6086 ++curwin->w_cursor.col;
6087 }
6088 }
6089#endif
6090 }
6091#ifdef FEAT_FOLDING
6092 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6093 && cap->oap->op_type == OP_NOP)
6094 foldOpenCursor();
6095#endif
6096}
6097
6098/*
6099 * Cursor left commands.
6100 *
6101 * Returns TRUE when operator end should not be adjusted.
6102 */
6103 static void
6104nv_left(cap)
6105 cmdarg_T *cap;
6106{
6107 long n;
6108
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006109 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6110 {
6111 /* <C-Left> and <S-Left> move a word or WORD left */
6112 if (mod_mask & MOD_MASK_CTRL)
6113 cap->arg = 1;
6114 nv_bck_word(cap);
6115 return;
6116 }
6117
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 cap->oap->motion_type = MCHAR;
6119 cap->oap->inclusive = FALSE;
6120 for (n = cap->count1; n > 0; --n)
6121 {
6122 if (oneleft() == FAIL)
6123 {
6124 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6125 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6126 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6127 */
6128 if ( (((cap->cmdchar == K_BS
6129 || cap->cmdchar == Ctrl_H)
6130 && vim_strchr(p_ww, 'b') != NULL)
6131 || (cap->cmdchar == 'h'
6132 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006133 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 && vim_strchr(p_ww, '<') != NULL))
6135 && curwin->w_cursor.lnum > 1)
6136 {
6137 --(curwin->w_cursor.lnum);
6138 coladvance((colnr_T)MAXCOL);
6139 curwin->w_set_curswant = TRUE;
6140
6141 /* When the NL before the first char has to be deleted we
6142 * put the cursor on the NUL after the previous line.
6143 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006144 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if ( (cap->oap->op_type == OP_DELETE
6146 || cap->oap->op_type == OP_CHANGE)
6147 && !lineempty(curwin->w_cursor.lnum))
6148 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006149 if (*ml_get_cursor() != NUL)
6150 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 cap->retval |= CA_NO_ADJ_OP_END;
6152 }
6153 continue;
6154 }
6155 /* Only beep and flush if not moved at all */
6156 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6157 beep_flush();
6158 break;
6159 }
6160 }
6161#ifdef FEAT_FOLDING
6162 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6163 && cap->oap->op_type == OP_NOP)
6164 foldOpenCursor();
6165#endif
6166}
6167
6168/*
6169 * Cursor up commands.
6170 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6171 */
6172 static void
6173nv_up(cap)
6174 cmdarg_T *cap;
6175{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006176 if (mod_mask & MOD_MASK_SHIFT)
6177 {
6178 /* <S-Up> is page up */
6179 cap->arg = BACKWARD;
6180 nv_page(cap);
6181 }
6182 else
6183 {
6184 cap->oap->motion_type = MLINE;
6185 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6186 clearopbeep(cap->oap);
6187 else if (cap->arg)
6188 beginline(BL_WHITE | BL_FIX);
6189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190}
6191
6192/*
6193 * Cursor down commands.
6194 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6195 */
6196 static void
6197nv_down(cap)
6198 cmdarg_T *cap;
6199{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006200 if (mod_mask & MOD_MASK_SHIFT)
6201 {
6202 /* <S-Down> is page down */
6203 cap->arg = FORWARD;
6204 nv_page(cap);
6205 }
6206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6208 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006210 if (curwin->w_llist_ref == NULL)
6211 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6212 else
6213 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else
6215#endif
6216 {
6217#ifdef FEAT_CMDWIN
6218 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006219 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 cmdwin_result = CAR;
6221 else
6222#endif
6223 {
6224 cap->oap->motion_type = MLINE;
6225 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6226 clearopbeep(cap->oap);
6227 else if (cap->arg)
6228 beginline(BL_WHITE | BL_FIX);
6229 }
6230 }
6231}
6232
6233#ifdef FEAT_SEARCHPATH
6234/*
6235 * Grab the file name under the cursor and edit it.
6236 */
6237 static void
6238nv_gotofile(cap)
6239 cmdarg_T *cap;
6240{
6241 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006242 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006244 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006247 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return;
6249 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006250#ifdef FEAT_AUTOCMD
6251 if (curbuf_locked())
6252 {
6253 clearop(cap->oap);
6254 return;
6255 }
6256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 if (ptr != NULL)
6261 {
6262 /* do autowrite if necessary */
6263 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6264 autowrite(curbuf, FALSE);
6265 setpcmark();
6266 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006267 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006268 if (cap->nchar == 'F' && lnum >= 0)
6269 {
6270 curwin->w_cursor.lnum = lnum;
6271 check_cursor_lnum();
6272 beginline(BL_SOL | BL_FIX);
6273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 vim_free(ptr);
6275 }
6276 else
6277 clearop(cap->oap);
6278}
6279#endif
6280
6281/*
6282 * <End> command: to end of current line or last line.
6283 */
6284 static void
6285nv_end(cap)
6286 cmdarg_T *cap;
6287{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006288 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006290 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 nv_goto(cap);
6292 cap->count1 = 1; /* to end of current line */
6293 }
6294 nv_dollar(cap);
6295}
6296
6297/*
6298 * Handle the "$" command.
6299 */
6300 static void
6301nv_dollar(cap)
6302 cmdarg_T *cap;
6303{
6304 cap->oap->motion_type = MCHAR;
6305 cap->oap->inclusive = TRUE;
6306#ifdef FEAT_VIRTUALEDIT
6307 /* In virtual mode when off the edge of a line and an operator
6308 * is pending (whew!) keep the cursor where it is.
6309 * Otherwise, send it to the end of the line. */
6310 if (!virtual_active() || gchar_cursor() != NUL
6311 || cap->oap->op_type == OP_NOP)
6312#endif
6313 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6314 if (cursor_down((long)(cap->count1 - 1),
6315 cap->oap->op_type == OP_NOP) == FAIL)
6316 clearopbeep(cap->oap);
6317#ifdef FEAT_FOLDING
6318 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6319 foldOpenCursor();
6320#endif
6321}
6322
6323/*
6324 * Implementation of '?' and '/' commands.
6325 * If cap->arg is TRUE don't set PC mark.
6326 */
6327 static void
6328nv_search(cap)
6329 cmdarg_T *cap;
6330{
6331 oparg_T *oap = cap->oap;
6332
6333 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6334 {
6335 /* Translate "g??" to "g?g?" */
6336 cap->cmdchar = 'g';
6337 cap->nchar = '?';
6338 nv_operator(cap);
6339 return;
6340 }
6341
6342 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6343
6344 if (cap->searchbuf == NULL)
6345 {
6346 clearop(oap);
6347 return;
6348 }
6349
6350 normal_search(cap, cap->cmdchar, cap->searchbuf,
6351 (cap->arg ? 0 : SEARCH_MARK));
6352}
6353
6354/*
6355 * Handle "N" and "n" commands.
6356 * cap->arg is SEARCH_REV for "N", 0 for "n".
6357 */
6358 static void
6359nv_next(cap)
6360 cmdarg_T *cap;
6361{
6362 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6363}
6364
6365/*
6366 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6367 * Uses only cap->count1 and cap->oap from "cap".
6368 */
6369 static void
6370normal_search(cap, dir, pat, opt)
6371 cmdarg_T *cap;
6372 int dir;
6373 char_u *pat;
6374 int opt; /* extra flags for do_search() */
6375{
6376 int i;
6377
6378 cap->oap->motion_type = MCHAR;
6379 cap->oap->inclusive = FALSE;
6380 cap->oap->use_reg_one = TRUE;
6381 curwin->w_set_curswant = TRUE;
6382
6383 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006384 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if (i == 0)
6386 clearop(cap->oap);
6387 else
6388 {
6389 if (i == 2)
6390 cap->oap->motion_type = MLINE;
6391#ifdef FEAT_VIRTUALEDIT
6392 curwin->w_cursor.coladd = 0;
6393#endif
6394#ifdef FEAT_FOLDING
6395 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6396 foldOpenCursor();
6397#endif
6398 }
6399
6400 /* "/$" will put the cursor after the end of the line, may need to
6401 * correct that here */
6402 check_cursor();
6403}
6404
6405/*
6406 * Character search commands.
6407 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6408 * ',' and FALSE for ';'.
6409 * cap->nchar is NUL for ',' and ';' (repeat the search)
6410 */
6411 static void
6412nv_csearch(cap)
6413 cmdarg_T *cap;
6414{
6415 int t_cmd;
6416
6417 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6418 t_cmd = TRUE;
6419 else
6420 t_cmd = FALSE;
6421
6422 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6424 clearopbeep(cap->oap);
6425 else
6426 {
6427 curwin->w_set_curswant = TRUE;
6428#ifdef FEAT_VIRTUALEDIT
6429 /* Include a Tab for "tx" and for "dfx". */
6430 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6431 && (t_cmd || cap->oap->op_type != OP_NOP))
6432 {
6433 colnr_T scol, ecol;
6434
6435 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6436 curwin->w_cursor.coladd = ecol - scol;
6437 }
6438 else
6439 curwin->w_cursor.coladd = 0;
6440#endif
6441#ifdef FEAT_VISUAL
6442 adjust_for_sel(cap);
6443#endif
6444#ifdef FEAT_FOLDING
6445 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6446 foldOpenCursor();
6447#endif
6448 }
6449}
6450
6451/*
6452 * "[" and "]" commands.
6453 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6454 */
6455 static void
6456nv_brackets(cap)
6457 cmdarg_T *cap;
6458{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006459 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 pos_T prev_pos;
6461 pos_T *pos = NULL; /* init for GCC */
6462 pos_T old_pos; /* cursor position before command */
6463 int flag;
6464 long n;
6465 int findc;
6466 int c;
6467
6468 cap->oap->motion_type = MCHAR;
6469 cap->oap->inclusive = FALSE;
6470 old_pos = curwin->w_cursor;
6471#ifdef FEAT_VIRTUALEDIT
6472 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6473#endif
6474
6475#ifdef FEAT_SEARCHPATH
6476 /*
6477 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6478 */
6479 if (cap->nchar == 'f')
6480 nv_gotofile(cap);
6481 else
6482#endif
6483
6484#ifdef FEAT_FIND_ID
6485 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006486 * Find the occurrence(s) of the identifier or define under cursor
6487 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 *
6489 * search list jump
6490 * fwd bwd fwd bwd fwd bwd
6491 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6492 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6493 */
6494 if (vim_strchr((char_u *)
6495#ifdef EBCDIC
6496 "iI\005dD\067",
6497#else
6498 "iI\011dD\004",
6499#endif
6500 cap->nchar) != NULL)
6501 {
6502 char_u *ptr;
6503 int len;
6504
6505 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6506 clearop(cap->oap);
6507 else
6508 {
6509 find_pattern_in_path(ptr, 0, len, TRUE,
6510 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6511 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6512 cap->count1,
6513 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6514 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6515 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6516 (linenr_T)MAXLNUM);
6517 curwin->w_set_curswant = TRUE;
6518 }
6519 }
6520 else
6521#endif
6522
6523 /*
6524 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6525 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6526 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6527 * "[m" or "]m" search for prev/next start of (Java) method.
6528 * "[M" or "]M" search for prev/next end of (Java) method.
6529 */
6530 if ( (cap->cmdchar == '['
6531 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6532 || (cap->cmdchar == ']'
6533 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6534 {
6535 if (cap->nchar == '*')
6536 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 prev_pos.lnum = 0;
6538 if (cap->nchar == 'm' || cap->nchar == 'M')
6539 {
6540 if (cap->cmdchar == '[')
6541 findc = '{';
6542 else
6543 findc = '}';
6544 n = 9999;
6545 }
6546 else
6547 {
6548 findc = cap->nchar;
6549 n = cap->count1;
6550 }
6551 for ( ; n > 0; --n)
6552 {
6553 if ((pos = findmatchlimit(cap->oap, findc,
6554 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6555 {
6556 if (new_pos.lnum == 0) /* nothing found */
6557 {
6558 if (cap->nchar != 'm' && cap->nchar != 'M')
6559 clearopbeep(cap->oap);
6560 }
6561 else
6562 pos = &new_pos; /* use last one found */
6563 break;
6564 }
6565 prev_pos = new_pos;
6566 curwin->w_cursor = *pos;
6567 new_pos = *pos;
6568 }
6569 curwin->w_cursor = old_pos;
6570
6571 /*
6572 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6573 * brought us to the match for "[m" and "]M" when inside a method.
6574 * Try finding the '{' or '}' we want to be at.
6575 * Also repeat for the given count.
6576 */
6577 if (cap->nchar == 'm' || cap->nchar == 'M')
6578 {
6579 /* norm is TRUE for "]M" and "[m" */
6580 int norm = ((findc == '{') == (cap->nchar == 'm'));
6581
6582 n = cap->count1;
6583 /* found a match: we were inside a method */
6584 if (prev_pos.lnum != 0)
6585 {
6586 pos = &prev_pos;
6587 curwin->w_cursor = prev_pos;
6588 if (norm)
6589 --n;
6590 }
6591 else
6592 pos = NULL;
6593 while (n > 0)
6594 {
6595 for (;;)
6596 {
6597 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6598 {
6599 /* if not found anything, that's an error */
6600 if (pos == NULL)
6601 clearopbeep(cap->oap);
6602 n = 0;
6603 break;
6604 }
6605 c = gchar_cursor();
6606 if (c == '{' || c == '}')
6607 {
6608 /* Must have found end/start of class: use it.
6609 * Or found the place to be at. */
6610 if ((c == findc && norm) || (n == 1 && !norm))
6611 {
6612 new_pos = curwin->w_cursor;
6613 pos = &new_pos;
6614 n = 0;
6615 }
6616 /* if no match found at all, we started outside of the
6617 * class and we're inside now. Just go on. */
6618 else if (new_pos.lnum == 0)
6619 {
6620 new_pos = curwin->w_cursor;
6621 pos = &new_pos;
6622 }
6623 /* found start/end of other method: go to match */
6624 else if ((pos = findmatchlimit(cap->oap, findc,
6625 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6626 0)) == NULL)
6627 n = 0;
6628 else
6629 curwin->w_cursor = *pos;
6630 break;
6631 }
6632 }
6633 --n;
6634 }
6635 curwin->w_cursor = old_pos;
6636 if (pos == NULL && new_pos.lnum != 0)
6637 clearopbeep(cap->oap);
6638 }
6639 if (pos != NULL)
6640 {
6641 setpcmark();
6642 curwin->w_cursor = *pos;
6643 curwin->w_set_curswant = TRUE;
6644#ifdef FEAT_FOLDING
6645 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6646 && cap->oap->op_type == OP_NOP)
6647 foldOpenCursor();
6648#endif
6649 }
6650 }
6651
6652 /*
6653 * "[[", "[]", "]]" and "][": move to start or end of function
6654 */
6655 else if (cap->nchar == '[' || cap->nchar == ']')
6656 {
6657 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6658 flag = '{';
6659 else
6660 flag = '}'; /* "][" or "[]" */
6661
6662 curwin->w_set_curswant = TRUE;
6663 /*
6664 * Imitate strange Vi behaviour: When using "]]" with an operator
6665 * we also stop at '}'.
6666 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006667 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 (cap->oap->op_type != OP_NOP
6669 && cap->arg == FORWARD && flag == '{')))
6670 clearopbeep(cap->oap);
6671 else
6672 {
6673 if (cap->oap->op_type == OP_NOP)
6674 beginline(BL_WHITE | BL_FIX);
6675#ifdef FEAT_FOLDING
6676 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6677 foldOpenCursor();
6678#endif
6679 }
6680 }
6681
6682 /*
6683 * "[p", "[P", "]P" and "]p": put with indent adjustment
6684 */
6685 else if (cap->nchar == 'p' || cap->nchar == 'P')
6686 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006687 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 prep_redo_cmd(cap);
6690 do_put(cap->oap->regname,
6691 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6692 cap->count1, PUT_FIXINDENT);
6693 }
6694 }
6695
6696 /*
6697 * "['", "[`", "]'" and "]`": jump to next mark
6698 */
6699 else if (cap->nchar == '\'' || cap->nchar == '`')
6700 {
6701 pos = &curwin->w_cursor;
6702 for (n = cap->count1; n > 0; --n)
6703 {
6704 prev_pos = *pos;
6705 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6706 cap->nchar == '\'');
6707 if (pos == NULL)
6708 break;
6709 }
6710 if (pos == NULL)
6711 pos = &prev_pos;
6712 nv_cursormark(cap, cap->nchar == '\'', pos);
6713 }
6714
6715#ifdef FEAT_MOUSE
6716 /*
6717 * [ or ] followed by a middle mouse click: put selected text with
6718 * indent adjustment. Any other button just does as usual.
6719 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006720 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
6722 (void)do_mouse(cap->oap, cap->nchar,
6723 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6724 cap->count1, PUT_FIXINDENT);
6725 }
6726#endif /* FEAT_MOUSE */
6727
6728#ifdef FEAT_FOLDING
6729 /*
6730 * "[z" and "]z": move to start or end of open fold.
6731 */
6732 else if (cap->nchar == 'z')
6733 {
6734 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6735 cap->count1) == FAIL)
6736 clearopbeep(cap->oap);
6737 }
6738#endif
6739
6740#ifdef FEAT_DIFF
6741 /*
6742 * "[c" and "]c": move to next or previous diff-change.
6743 */
6744 else if (cap->nchar == 'c')
6745 {
6746 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6747 cap->count1) == FAIL)
6748 clearopbeep(cap->oap);
6749 }
6750#endif
6751
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006752#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006753 /*
6754 * "[s", "[S", "]s" and "]S": move to next spell error.
6755 */
6756 else if (cap->nchar == 's' || cap->nchar == 'S')
6757 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006758 setpcmark();
6759 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006760 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6761 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006762 {
6763 clearopbeep(cap->oap);
6764 break;
6765 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006766# ifdef FEAT_FOLDING
6767 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6768 foldOpenCursor();
6769# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006770 }
6771#endif
6772
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 /* Not a valid cap->nchar. */
6774 else
6775 clearopbeep(cap->oap);
6776}
6777
6778/*
6779 * Handle Normal mode "%" command.
6780 */
6781 static void
6782nv_percent(cap)
6783 cmdarg_T *cap;
6784{
6785 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006786#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 linenr_T lnum = curwin->w_cursor.lnum;
6788#endif
6789
6790 cap->oap->inclusive = TRUE;
6791 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6792 {
6793 if (cap->count0 > 100)
6794 clearopbeep(cap->oap);
6795 else
6796 {
6797 cap->oap->motion_type = MLINE;
6798 setpcmark();
6799 /* Round up, so CTRL-G will give same value. Watch out for a
6800 * large line count, the line number must not go negative! */
6801 if (curbuf->b_ml.ml_line_count > 1000000)
6802 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6803 / 100L * cap->count0;
6804 else
6805 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6806 cap->count0 + 99L) / 100L;
6807 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6808 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6809 beginline(BL_SOL | BL_FIX);
6810 }
6811 }
6812 else /* "%" : go to matching paren */
6813 {
6814 cap->oap->motion_type = MCHAR;
6815 cap->oap->use_reg_one = TRUE;
6816 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6817 clearopbeep(cap->oap);
6818 else
6819 {
6820 setpcmark();
6821 curwin->w_cursor = *pos;
6822 curwin->w_set_curswant = TRUE;
6823#ifdef FEAT_VIRTUALEDIT
6824 curwin->w_cursor.coladd = 0;
6825#endif
6826#ifdef FEAT_VISUAL
6827 adjust_for_sel(cap);
6828#endif
6829 }
6830 }
6831#ifdef FEAT_FOLDING
6832 if (cap->oap->op_type == OP_NOP
6833 && lnum != curwin->w_cursor.lnum
6834 && (fdo_flags & FDO_PERCENT)
6835 && KeyTyped)
6836 foldOpenCursor();
6837#endif
6838}
6839
6840/*
6841 * Handle "(" and ")" commands.
6842 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6843 */
6844 static void
6845nv_brace(cap)
6846 cmdarg_T *cap;
6847{
6848 cap->oap->motion_type = MCHAR;
6849 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006850 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6851 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 curwin->w_set_curswant = TRUE;
6853
6854 if (findsent(cap->arg, cap->count1) == FAIL)
6855 clearopbeep(cap->oap);
6856 else
6857 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006858 /* Don't leave the cursor on the NUL past end of line. */
6859 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#ifdef FEAT_VIRTUALEDIT
6861 curwin->w_cursor.coladd = 0;
6862#endif
6863#ifdef FEAT_FOLDING
6864 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6865 foldOpenCursor();
6866#endif
6867 }
6868}
6869
6870/*
6871 * "m" command: Mark a position.
6872 */
6873 static void
6874nv_mark(cap)
6875 cmdarg_T *cap;
6876{
6877 if (!checkclearop(cap->oap))
6878 {
6879 if (setmark(cap->nchar) == FAIL)
6880 clearopbeep(cap->oap);
6881 }
6882}
6883
6884/*
6885 * "{" and "}" commands.
6886 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6887 */
6888 static void
6889nv_findpar(cap)
6890 cmdarg_T *cap;
6891{
6892 cap->oap->motion_type = MCHAR;
6893 cap->oap->inclusive = FALSE;
6894 cap->oap->use_reg_one = TRUE;
6895 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006896 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 clearopbeep(cap->oap);
6898 else
6899 {
6900#ifdef FEAT_VIRTUALEDIT
6901 curwin->w_cursor.coladd = 0;
6902#endif
6903#ifdef FEAT_FOLDING
6904 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6905 foldOpenCursor();
6906#endif
6907 }
6908}
6909
6910/*
6911 * "u" command: Undo or make lower case.
6912 */
6913 static void
6914nv_undo(cap)
6915 cmdarg_T *cap;
6916{
6917 if (cap->oap->op_type == OP_LOWER
6918#ifdef FEAT_VISUAL
6919 || VIsual_active
6920#endif
6921 )
6922 {
6923 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6924 cap->cmdchar = 'g';
6925 cap->nchar = 'u';
6926 nv_operator(cap);
6927 }
6928 else
6929 nv_kundo(cap);
6930}
6931
6932/*
6933 * <Undo> command.
6934 */
6935 static void
6936nv_kundo(cap)
6937 cmdarg_T *cap;
6938{
6939 if (!checkclearopq(cap->oap))
6940 {
6941 u_undo((int)cap->count1);
6942 curwin->w_set_curswant = TRUE;
6943 }
6944}
6945
6946/*
6947 * Handle the "r" command.
6948 */
6949 static void
6950nv_replace(cap)
6951 cmdarg_T *cap;
6952{
6953 char_u *ptr;
6954 int had_ctrl_v;
6955 long n;
6956
6957 if (checkclearop(cap->oap))
6958 return;
6959
6960 /* get another character */
6961 if (cap->nchar == Ctrl_V)
6962 {
6963 had_ctrl_v = Ctrl_V;
6964 cap->nchar = get_literal();
6965 /* Don't redo a multibyte character with CTRL-V. */
6966 if (cap->nchar > DEL)
6967 had_ctrl_v = NUL;
6968 }
6969 else
6970 had_ctrl_v = NUL;
6971
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006972 /* Abort if the character is a special key. */
6973 if (IS_SPECIAL(cap->nchar))
6974 {
6975 clearopbeep(cap->oap);
6976 return;
6977 }
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979#ifdef FEAT_VISUAL
6980 /* Visual mode "r" */
6981 if (VIsual_active)
6982 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006983 if (got_int)
6984 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 nv_operator(cap);
6986 return;
6987 }
6988#endif
6989
6990#ifdef FEAT_VIRTUALEDIT
6991 /* Break tabs, etc. */
6992 if (virtual_active())
6993 {
6994 if (u_save_cursor() == FAIL)
6995 return;
6996 if (gchar_cursor() == NUL)
6997 {
6998 /* Add extra space and put the cursor on the first one. */
6999 coladvance_force((colnr_T)(getviscol() + cap->count1));
7000 curwin->w_cursor.col -= cap->count1;
7001 }
7002 else if (gchar_cursor() == TAB)
7003 coladvance_force(getviscol());
7004 }
7005#endif
7006
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007007 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007009 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010#ifdef FEAT_MBYTE
7011 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7012#endif
7013 )
7014 {
7015 clearopbeep(cap->oap);
7016 return;
7017 }
7018
7019 /*
7020 * Replacing with a TAB is done by edit() when it is complicated because
7021 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7022 * Other characters are done below to avoid problems with things like
7023 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7024 */
7025 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7026 {
7027 stuffnumReadbuff(cap->count1);
7028 stuffcharReadbuff('R');
7029 stuffcharReadbuff('\t');
7030 stuffcharReadbuff(ESC);
7031 return;
7032 }
7033
7034 /* save line for undo */
7035 if (u_save_cursor() == FAIL)
7036 return;
7037
7038 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7039 {
7040 /*
7041 * Replace character(s) by a single newline.
7042 * Strange vi behaviour: Only one newline is inserted.
7043 * Delete the characters here.
7044 * Insert the newline with an insert command, takes care of
7045 * autoindent. The insert command depends on being on the last
7046 * character of a line or not.
7047 */
7048#ifdef FEAT_MBYTE
7049 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7050#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007051 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052#endif
7053 stuffcharReadbuff('\r');
7054 stuffcharReadbuff(ESC);
7055
7056 /* Give 'r' to edit(), to get the redo command right. */
7057 invoke_edit(cap, TRUE, 'r', FALSE);
7058 }
7059 else
7060 {
7061 prep_redo(cap->oap->regname, cap->count1,
7062 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7063
7064 curbuf->b_op_start = curwin->w_cursor;
7065#ifdef FEAT_MBYTE
7066 if (has_mbyte)
7067 {
7068 int old_State = State;
7069
7070 if (cap->ncharC1 != 0)
7071 AppendCharToRedobuff(cap->ncharC1);
7072 if (cap->ncharC2 != 0)
7073 AppendCharToRedobuff(cap->ncharC2);
7074
7075 /* This is slow, but it handles replacing a single-byte with a
7076 * multi-byte and the other way around. Also handles adding
7077 * composing characters for utf-8. */
7078 for (n = cap->count1; n > 0; --n)
7079 {
7080 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007081 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7082 {
7083 int c = ins_copychar(curwin->w_cursor.lnum
7084 + (cap->nchar == Ctrl_Y ? -1 : 1));
7085 if (c != NUL)
7086 ins_char(c);
7087 else
7088 /* will be decremented further down */
7089 ++curwin->w_cursor.col;
7090 }
7091 else
7092 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 State = old_State;
7094 if (cap->ncharC1 != 0)
7095 ins_char(cap->ncharC1);
7096 if (cap->ncharC2 != 0)
7097 ins_char(cap->ncharC2);
7098 }
7099 }
7100 else
7101#endif
7102 {
7103 /*
7104 * Replace the characters within one line.
7105 */
7106 for (n = cap->count1; n > 0; --n)
7107 {
7108 /*
7109 * Get ptr again, because u_save and/or showmatch() will have
7110 * released the line. At the same time we let know that the
7111 * line will be changed.
7112 */
7113 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007114 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7115 {
7116 int c = ins_copychar(curwin->w_cursor.lnum
7117 + (cap->nchar == Ctrl_Y ? -1 : 1));
7118 if (c != NUL)
7119 ptr[curwin->w_cursor.col] = c;
7120 }
7121 else
7122 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (p_sm && msg_silent == 0)
7124 showmatch(cap->nchar);
7125 ++curwin->w_cursor.col;
7126 }
7127#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007128 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007130 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaar009b2592004-10-24 19:18:58 +00007132 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007133 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007135 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
7137#endif
7138
7139 /* mark the buffer as changed and prepare for displaying */
7140 changed_bytes(curwin->w_cursor.lnum,
7141 (colnr_T)(curwin->w_cursor.col - cap->count1));
7142 }
7143 --curwin->w_cursor.col; /* cursor on the last replaced char */
7144#ifdef FEAT_MBYTE
7145 /* if the character on the left of the current cursor is a multi-byte
7146 * character, move two characters left */
7147 if (has_mbyte)
7148 mb_adjust_cursor();
7149#endif
7150 curbuf->b_op_end = curwin->w_cursor;
7151 curwin->w_set_curswant = TRUE;
7152 set_last_insert(cap->nchar);
7153 }
7154}
7155
7156#ifdef FEAT_VISUAL
7157/*
7158 * 'o': Exchange start and end of Visual area.
7159 * 'O': same, but in block mode exchange left and right corners.
7160 */
7161 static void
7162v_swap_corners(cmdchar)
7163 int cmdchar;
7164{
7165 pos_T old_cursor;
7166 colnr_T left, right;
7167
7168 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7169 {
7170 old_cursor = curwin->w_cursor;
7171 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7172 curwin->w_cursor.lnum = VIsual.lnum;
7173 coladvance(left);
7174 VIsual = curwin->w_cursor;
7175
7176 curwin->w_cursor.lnum = old_cursor.lnum;
7177 curwin->w_curswant = right;
7178 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7179 * right one column */
7180 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7181 ++curwin->w_curswant;
7182 coladvance(curwin->w_curswant);
7183 if (curwin->w_cursor.col == old_cursor.col
7184#ifdef FEAT_VIRTUALEDIT
7185 && (!virtual_active()
7186 || curwin->w_cursor.coladd == old_cursor.coladd)
7187#endif
7188 )
7189 {
7190 curwin->w_cursor.lnum = VIsual.lnum;
7191 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7192 ++right;
7193 coladvance(right);
7194 VIsual = curwin->w_cursor;
7195
7196 curwin->w_cursor.lnum = old_cursor.lnum;
7197 coladvance(left);
7198 curwin->w_curswant = left;
7199 }
7200 }
7201 else
7202 {
7203 old_cursor = curwin->w_cursor;
7204 curwin->w_cursor = VIsual;
7205 VIsual = old_cursor;
7206 curwin->w_set_curswant = TRUE;
7207 }
7208}
7209#endif /* FEAT_VISUAL */
7210
7211/*
7212 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7213 */
7214 static void
7215nv_Replace(cap)
7216 cmdarg_T *cap;
7217{
7218#ifdef FEAT_VISUAL
7219 if (VIsual_active) /* "R" is replace lines */
7220 {
7221 cap->cmdchar = 'c';
7222 cap->nchar = NUL;
7223 VIsual_mode = 'V';
7224 nv_operator(cap);
7225 }
7226 else
7227#endif
7228 if (!checkclearopq(cap->oap))
7229 {
7230 if (!curbuf->b_p_ma)
7231 EMSG(_(e_modifiable));
7232 else
7233 {
7234#ifdef FEAT_VIRTUALEDIT
7235 if (virtual_active())
7236 coladvance(getviscol());
7237#endif
7238 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7239 }
7240 }
7241}
7242
7243#ifdef FEAT_VREPLACE
7244/*
7245 * "gr".
7246 */
7247 static void
7248nv_vreplace(cap)
7249 cmdarg_T *cap;
7250{
7251# ifdef FEAT_VISUAL
7252 if (VIsual_active)
7253 {
7254 cap->cmdchar = 'r';
7255 cap->nchar = cap->extra_char;
7256 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7257 }
7258 else
7259# endif
7260 if (!checkclearopq(cap->oap))
7261 {
7262 if (!curbuf->b_p_ma)
7263 EMSG(_(e_modifiable));
7264 else
7265 {
7266 if (cap->extra_char == Ctrl_V) /* get another character */
7267 cap->extra_char = get_literal();
7268 stuffcharReadbuff(cap->extra_char);
7269 stuffcharReadbuff(ESC);
7270# ifdef FEAT_VIRTUALEDIT
7271 if (virtual_active())
7272 coladvance(getviscol());
7273# endif
7274 invoke_edit(cap, TRUE, 'v', FALSE);
7275 }
7276 }
7277}
7278#endif
7279
7280/*
7281 * Swap case for "~" command, when it does not work like an operator.
7282 */
7283 static void
7284n_swapchar(cap)
7285 cmdarg_T *cap;
7286{
7287 long n;
7288 pos_T startpos;
7289 int did_change = 0;
7290#ifdef FEAT_NETBEANS_INTG
7291 pos_T pos;
7292 char_u *ptr;
7293 int count;
7294#endif
7295
7296 if (checkclearopq(cap->oap))
7297 return;
7298
7299 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7300 {
7301 clearopbeep(cap->oap);
7302 return;
7303 }
7304
7305 prep_redo_cmd(cap);
7306
7307 if (u_save_cursor() == FAIL)
7308 return;
7309
7310 startpos = curwin->w_cursor;
7311#ifdef FEAT_NETBEANS_INTG
7312 pos = startpos;
7313#endif
7314 for (n = cap->count1; n > 0; --n)
7315 {
7316 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7317 inc_cursor();
7318 if (gchar_cursor() == NUL)
7319 {
7320 if (vim_strchr(p_ww, '~') != NULL
7321 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7322 {
7323#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007324 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
7326 if (did_change)
7327 {
7328 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007329 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007330 netbeans_removed(curbuf, pos.lnum, pos.col,
7331 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007333 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
7335 pos.col = 0;
7336 pos.lnum++;
7337 }
7338#endif
7339 ++curwin->w_cursor.lnum;
7340 curwin->w_cursor.col = 0;
7341 if (n > 1)
7342 {
7343 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7344 break;
7345 u_clearline();
7346 }
7347 }
7348 else
7349 break;
7350 }
7351 }
7352#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007353 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 ptr = ml_get(pos.lnum);
7356 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007357 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7358 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360#endif
7361
7362
7363 check_cursor();
7364 curwin->w_set_curswant = TRUE;
7365 if (did_change)
7366 {
7367 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7368 0L);
7369 curbuf->b_op_start = startpos;
7370 curbuf->b_op_end = curwin->w_cursor;
7371 if (curbuf->b_op_end.col > 0)
7372 --curbuf->b_op_end.col;
7373 }
7374}
7375
7376/*
7377 * Move cursor to mark.
7378 */
7379 static void
7380nv_cursormark(cap, flag, pos)
7381 cmdarg_T *cap;
7382 int flag;
7383 pos_T *pos;
7384{
7385 if (check_mark(pos) == FAIL)
7386 clearop(cap->oap);
7387 else
7388 {
7389 if (cap->cmdchar == '\''
7390 || cap->cmdchar == '`'
7391 || cap->cmdchar == '['
7392 || cap->cmdchar == ']')
7393 setpcmark();
7394 curwin->w_cursor = *pos;
7395 if (flag)
7396 beginline(BL_WHITE | BL_FIX);
7397 else
7398 check_cursor();
7399 }
7400 cap->oap->motion_type = flag ? MLINE : MCHAR;
7401 if (cap->cmdchar == '`')
7402 cap->oap->use_reg_one = TRUE;
7403 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7404 curwin->w_set_curswant = TRUE;
7405}
7406
7407#ifdef FEAT_VISUAL
7408/*
7409 * Handle commands that are operators in Visual mode.
7410 */
7411 static void
7412v_visop(cap)
7413 cmdarg_T *cap;
7414{
7415 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7416
7417 /* Uppercase means linewise, except in block mode, then "D" deletes till
7418 * the end of the line, and "C" replaces til EOL */
7419 if (isupper(cap->cmdchar))
7420 {
7421 if (VIsual_mode != Ctrl_V)
7422 VIsual_mode = 'V';
7423 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7424 curwin->w_curswant = MAXCOL;
7425 }
7426 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7427 nv_operator(cap);
7428}
7429#endif
7430
7431/*
7432 * "s" and "S" commands.
7433 */
7434 static void
7435nv_subst(cap)
7436 cmdarg_T *cap;
7437{
7438#ifdef FEAT_VISUAL
7439 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7440 {
7441 if (cap->cmdchar == 'S')
7442 VIsual_mode = 'V';
7443 cap->cmdchar = 'c';
7444 nv_operator(cap);
7445 }
7446 else
7447#endif
7448 nv_optrans(cap);
7449}
7450
7451/*
7452 * Abbreviated commands.
7453 */
7454 static void
7455nv_abbrev(cap)
7456 cmdarg_T *cap;
7457{
7458 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7459 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7460
7461#ifdef FEAT_VISUAL
7462 /* in Visual mode these commands are operators */
7463 if (VIsual_active)
7464 v_visop(cap);
7465 else
7466#endif
7467 nv_optrans(cap);
7468}
7469
7470/*
7471 * Translate a command into another command.
7472 */
7473 static void
7474nv_optrans(cap)
7475 cmdarg_T *cap;
7476{
7477 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7478 (char_u *)"d$", (char_u *)"c$",
7479 (char_u *)"cl", (char_u *)"cc",
7480 (char_u *)"yy", (char_u *)":s\r"};
7481 static char_u *str = (char_u *)"xXDCsSY&";
7482
7483 if (!checkclearopq(cap->oap))
7484 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007485 /* In Vi "2D" doesn't delete the next line. Can't translate it
7486 * either, because "2." should also not use the count. */
7487 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7488 {
7489 cap->oap->start = curwin->w_cursor;
7490 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007491#ifdef FEAT_EVAL
7492 set_op_var(OP_DELETE);
7493#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007494 cap->count1 = 1;
7495 nv_dollar(cap);
7496 finish_op = TRUE;
7497 ResetRedobuff();
7498 AppendCharToRedobuff('D');
7499 }
7500 else
7501 {
7502 if (cap->count0)
7503 stuffnumReadbuff(cap->count0);
7504 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507 cap->opcount = 0;
7508}
7509
7510/*
7511 * "'" and "`" commands. Also for "g'" and "g`".
7512 * cap->arg is TRUE for "'" and "g'".
7513 */
7514 static void
7515nv_gomark(cap)
7516 cmdarg_T *cap;
7517{
7518 pos_T *pos;
7519 int c;
7520#ifdef FEAT_FOLDING
7521 linenr_T lnum = curwin->w_cursor.lnum;
7522 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7523#endif
7524
7525 if (cap->cmdchar == 'g')
7526 c = cap->extra_char;
7527 else
7528 c = cap->nchar;
7529 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7530 if (pos == (pos_T *)-1) /* jumped to other file */
7531 {
7532 if (cap->arg)
7533 {
7534 check_cursor_lnum();
7535 beginline(BL_WHITE | BL_FIX);
7536 }
7537 else
7538 check_cursor();
7539 }
7540 else
7541 nv_cursormark(cap, cap->arg, pos);
7542
7543#ifdef FEAT_VIRTUALEDIT
7544 /* May need to clear the coladd that a mark includes. */
7545 if (!virtual_active())
7546 curwin->w_cursor.coladd = 0;
7547#endif
7548#ifdef FEAT_FOLDING
7549 if (cap->oap->op_type == OP_NOP
7550 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7551 && (fdo_flags & FDO_MARK)
7552 && old_KeyTyped)
7553 foldOpenCursor();
7554#endif
7555}
7556
7557/*
7558 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7559 */
7560 static void
7561nv_pcmark(cap)
7562 cmdarg_T *cap;
7563{
7564#ifdef FEAT_JUMPLIST
7565 pos_T *pos;
7566# ifdef FEAT_FOLDING
7567 linenr_T lnum = curwin->w_cursor.lnum;
7568 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7569# endif
7570
7571 if (!checkclearopq(cap->oap))
7572 {
7573 if (cap->cmdchar == 'g')
7574 pos = movechangelist((int)cap->count1);
7575 else
7576 pos = movemark((int)cap->count1);
7577 if (pos == (pos_T *)-1) /* jump to other file */
7578 {
7579 curwin->w_set_curswant = TRUE;
7580 check_cursor();
7581 }
7582 else if (pos != NULL) /* can jump */
7583 nv_cursormark(cap, FALSE, pos);
7584 else if (cap->cmdchar == 'g')
7585 {
7586 if (curbuf->b_changelistlen == 0)
7587 EMSG(_("E664: changelist is empty"));
7588 else if (cap->count1 < 0)
7589 EMSG(_("E662: At start of changelist"));
7590 else
7591 EMSG(_("E663: At end of changelist"));
7592 }
7593 else
7594 clearopbeep(cap->oap);
7595# ifdef FEAT_FOLDING
7596 if (cap->oap->op_type == OP_NOP
7597 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7598 && (fdo_flags & FDO_MARK)
7599 && old_KeyTyped)
7600 foldOpenCursor();
7601# endif
7602 }
7603#else
7604 clearopbeep(cap->oap);
7605#endif
7606}
7607
7608/*
7609 * Handle '"' command.
7610 */
7611 static void
7612nv_regname(cap)
7613 cmdarg_T *cap;
7614{
7615 if (checkclearop(cap->oap))
7616 return;
7617#ifdef FEAT_EVAL
7618 if (cap->nchar == '=')
7619 cap->nchar = get_expr_register();
7620#endif
7621 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7622 {
7623 cap->oap->regname = cap->nchar;
7624 cap->opcount = cap->count0; /* remember count before '"' */
7625#ifdef FEAT_EVAL
7626 set_reg_var(cap->oap->regname);
7627#endif
7628 }
7629 else
7630 clearopbeep(cap->oap);
7631}
7632
7633#ifdef FEAT_VISUAL
7634/*
7635 * Handle "v", "V" and "CTRL-V" commands.
7636 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7637 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 */
7640 static void
7641nv_visual(cap)
7642 cmdarg_T *cap;
7643{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 if (cap->cmdchar == Ctrl_Q)
7645 cap->cmdchar = Ctrl_V;
7646
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7648 * characterwise, linewise, or blockwise. */
7649 if (cap->oap->op_type != OP_NOP)
7650 {
7651 cap->oap->motion_force = cap->cmdchar;
7652 finish_op = FALSE; /* operator doesn't finish now but later */
7653 return;
7654 }
7655
7656 VIsual_select = cap->arg;
7657 if (VIsual_active) /* change Visual mode */
7658 {
7659 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7660 end_visual_mode();
7661 else /* toggle char/block mode */
7662 { /* or char/line mode */
7663 VIsual_mode = cap->cmdchar;
7664 showmode();
7665 }
7666 redraw_curbuf_later(INVERTED); /* update the inversion */
7667 }
7668 else /* start Visual mode */
7669 {
7670 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007671 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007673 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 VIsual = curwin->w_cursor;
7675
7676 VIsual_active = TRUE;
7677 VIsual_reselect = TRUE;
7678 if (!cap->arg)
7679 /* start Select mode when 'selectmode' contains "cmd" */
7680 may_start_select('c');
7681#ifdef FEAT_MOUSE
7682 setmouse();
7683#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007684 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 redraw_cmdline = TRUE; /* show visual mode later */
7686 /*
7687 * For V and ^V, we multiply the number of lines even if there
7688 * was only one -- webb
7689 */
7690 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7691 {
7692 curwin->w_cursor.lnum +=
7693 resel_VIsual_line_count * cap->count0 - 1;
7694 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7696 }
7697 VIsual_mode = resel_VIsual_mode;
7698 if (VIsual_mode == 'v')
7699 {
7700 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007701 {
7702 validate_virtcol();
7703 curwin->w_curswant = curwin->w_virtcol
7704 + resel_VIsual_vcol * cap->count0 - 1;
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007707 curwin->w_curswant = resel_VIsual_vcol;
7708 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007710 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
7712 curwin->w_curswant = MAXCOL;
7713 coladvance((colnr_T)MAXCOL);
7714 }
7715 else if (VIsual_mode == Ctrl_V)
7716 {
7717 validate_virtcol();
7718 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007719 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 coladvance(curwin->w_curswant);
7721 }
7722 else
7723 curwin->w_set_curswant = TRUE;
7724 redraw_curbuf_later(INVERTED); /* show the inversion */
7725 }
7726 else
7727 {
7728 if (!cap->arg)
7729 /* start Select mode when 'selectmode' contains "cmd" */
7730 may_start_select('c');
7731 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007732 if (VIsual_mode != 'V' && *p_sel == 'e')
7733 ++cap->count1; /* include one more char */
7734 if (cap->count0 > 0 && --cap->count1 > 0)
7735 {
7736 /* With a count select that many characters or lines. */
7737 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7738 nv_right(cap);
7739 else if (VIsual_mode == 'V')
7740 nv_down(cap);
7741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743 }
7744}
7745
7746/*
7747 * Start selection for Shift-movement keys.
7748 */
7749 void
7750start_selection()
7751{
7752 /* if 'selectmode' contains "key", start Select mode */
7753 may_start_select('k');
7754 n_start_visual_mode('v');
7755}
7756
7757/*
7758 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7759 */
7760 void
7761may_start_select(c)
7762 int c;
7763{
7764 VIsual_select = (stuff_empty() && typebuf_typed()
7765 && (vim_strchr(p_slm, c) != NULL));
7766}
7767
7768/*
7769 * Start Visual mode "c".
7770 * Should set VIsual_select before calling this.
7771 */
7772 static void
7773n_start_visual_mode(c)
7774 int c;
7775{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007776#ifdef FEAT_CONCEAL
7777 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007778 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007779#endif
7780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 VIsual_mode = c;
7782 VIsual_active = TRUE;
7783 VIsual_reselect = TRUE;
7784#ifdef FEAT_VIRTUALEDIT
7785 /* Corner case: the 0 position in a tab may change when going into
7786 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7787 */
7788 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007789 {
7790 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793#endif
7794 VIsual = curwin->w_cursor;
7795
7796#ifdef FEAT_FOLDING
7797 foldAdjustVisual();
7798#endif
7799
7800#ifdef FEAT_MOUSE
7801 setmouse();
7802#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007803#ifdef FEAT_CONCEAL
7804 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007805 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007806#endif
7807
Bram Moolenaar09df3122006-01-23 22:23:09 +00007808 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 redraw_cmdline = TRUE; /* show visual mode later */
7810#ifdef FEAT_CLIPBOARD
7811 /* Make sure the clipboard gets updated. Needed because start and
7812 * end may still be the same, and the selection needs to be owned */
7813 clip_star.vmode = NUL;
7814#endif
7815
7816 /* Only need to redraw this line, unless still need to redraw an old
7817 * Visual area (when 'lazyredraw' is set). */
7818 if (curwin->w_redr_type < INVERTED)
7819 {
7820 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7821 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7822 }
7823}
7824
7825#endif /* FEAT_VISUAL */
7826
7827/*
7828 * CTRL-W: Window commands
7829 */
7830 static void
7831nv_window(cap)
7832 cmdarg_T *cap;
7833{
7834#ifdef FEAT_WINDOWS
7835 if (!checkclearop(cap->oap))
7836 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7837#else
7838 (void)checkclearop(cap->oap);
7839#endif
7840}
7841
7842/*
7843 * CTRL-Z: Suspend
7844 */
7845 static void
7846nv_suspend(cap)
7847 cmdarg_T *cap;
7848{
7849 clearop(cap->oap);
7850#ifdef FEAT_VISUAL
7851 if (VIsual_active)
7852 end_visual_mode(); /* stop Visual mode */
7853#endif
7854 do_cmdline_cmd((char_u *)"st");
7855}
7856
7857/*
7858 * Commands starting with "g".
7859 */
7860 static void
7861nv_g_cmd(cap)
7862 cmdarg_T *cap;
7863{
7864 oparg_T *oap = cap->oap;
7865#ifdef FEAT_VISUAL
7866 pos_T tpos;
7867#endif
7868 int i;
7869 int flag = FALSE;
7870
7871 switch (cap->nchar)
7872 {
7873#ifdef MEM_PROFILE
7874 /*
7875 * "g^A": dump log of used memory.
7876 */
7877 case Ctrl_A:
7878 vim_mem_profile_dump();
7879 break;
7880#endif
7881
7882#ifdef FEAT_VREPLACE
7883 /*
7884 * "gR": Enter virtual replace mode.
7885 */
7886 case 'R':
7887 cap->arg = TRUE;
7888 nv_Replace(cap);
7889 break;
7890
7891 case 'r':
7892 nv_vreplace(cap);
7893 break;
7894#endif
7895
7896 case '&':
7897 do_cmdline_cmd((char_u *)"%s//~/&");
7898 break;
7899
7900#ifdef FEAT_VISUAL
7901 /*
7902 * "gv": Reselect the previous Visual area. If Visual already active,
7903 * exchange previous and current Visual area.
7904 */
7905 case 'v':
7906 if (checkclearop(oap))
7907 break;
7908
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007909 if ( curbuf->b_visual.vi_start.lnum == 0
7910 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7911 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 beep_flush();
7913 else
7914 {
7915 /* set w_cursor to the start of the Visual area, tpos to the end */
7916 if (VIsual_active)
7917 {
7918 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007919 VIsual_mode = curbuf->b_visual.vi_mode;
7920 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921# ifdef FEAT_EVAL
7922 curbuf->b_visual_mode_eval = i;
7923# endif
7924 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007925 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7926 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007928 tpos = curbuf->b_visual.vi_end;
7929 curbuf->b_visual.vi_end = curwin->w_cursor;
7930 curwin->w_cursor = curbuf->b_visual.vi_start;
7931 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933 else
7934 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007935 VIsual_mode = curbuf->b_visual.vi_mode;
7936 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7937 tpos = curbuf->b_visual.vi_end;
7938 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940
7941 VIsual_active = TRUE;
7942 VIsual_reselect = TRUE;
7943
7944 /* Set Visual to the start and w_cursor to the end of the Visual
7945 * area. Make sure they are on an existing character. */
7946 check_cursor();
7947 VIsual = curwin->w_cursor;
7948 curwin->w_cursor = tpos;
7949 check_cursor();
7950 update_topline();
7951 /*
7952 * When called from normal "g" command: start Select mode when
7953 * 'selectmode' contains "cmd". When called for K_SELECT, always
7954 * start Select mode.
7955 */
7956 if (cap->arg)
7957 VIsual_select = TRUE;
7958 else
7959 may_start_select('c');
7960#ifdef FEAT_MOUSE
7961 setmouse();
7962#endif
7963#ifdef FEAT_CLIPBOARD
7964 /* Make sure the clipboard gets updated. Needed because start and
7965 * end are still the same, and the selection needs to be owned */
7966 clip_star.vmode = NUL;
7967#endif
7968 redraw_curbuf_later(INVERTED);
7969 showmode();
7970 }
7971 break;
7972 /*
7973 * "gV": Don't reselect the previous Visual area after a Select mode
7974 * mapping of menu.
7975 */
7976 case 'V':
7977 VIsual_reselect = FALSE;
7978 break;
7979
7980 /*
7981 * "gh": start Select mode.
7982 * "gH": start Select line mode.
7983 * "g^H": start Select block mode.
7984 */
7985 case K_BS:
7986 cap->nchar = Ctrl_H;
7987 /* FALLTHROUGH */
7988 case 'h':
7989 case 'H':
7990 case Ctrl_H:
7991# ifdef EBCDIC
7992 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7993 if (cap->nchar == Ctrl_H)
7994 cap->cmdchar = Ctrl_V;
7995 else
7996# endif
7997 cap->cmdchar = cap->nchar + ('v' - 'h');
7998 cap->arg = TRUE;
7999 nv_visual(cap);
8000 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008001#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008002
8003 /* "gn", "gN" visually select next/previous search match
8004 * "gn" selects next match
8005 * "gN" selects previous match
8006 */
8007 case 'N':
8008 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008009#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008010 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008011#endif
Bram Moolenaar641e2862012-07-25 15:06:34 +02008012 beep_flush();
Bram Moolenaar641e2862012-07-25 15:06:34 +02008013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 /*
8016 * "gj" and "gk" two new funny movement keys -- up and down
8017 * movement based on *screen* line rather than *file* line.
8018 */
8019 case 'j':
8020 case K_DOWN:
8021 /* with 'nowrap' it works just like the normal "j" command; also when
8022 * in a closed fold */
8023 if (!curwin->w_p_wrap
8024#ifdef FEAT_FOLDING
8025 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8026#endif
8027 )
8028 {
8029 oap->motion_type = MLINE;
8030 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8031 }
8032 else
8033 i = nv_screengo(oap, FORWARD, cap->count1);
8034 if (i == FAIL)
8035 clearopbeep(oap);
8036 break;
8037
8038 case 'k':
8039 case K_UP:
8040 /* with 'nowrap' it works just like the normal "k" command; also when
8041 * in a closed fold */
8042 if (!curwin->w_p_wrap
8043#ifdef FEAT_FOLDING
8044 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8045#endif
8046 )
8047 {
8048 oap->motion_type = MLINE;
8049 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8050 }
8051 else
8052 i = nv_screengo(oap, BACKWARD, cap->count1);
8053 if (i == FAIL)
8054 clearopbeep(oap);
8055 break;
8056
8057 /*
8058 * "gJ": join two lines without inserting a space.
8059 */
8060 case 'J':
8061 nv_join(cap);
8062 break;
8063
8064 /*
8065 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8066 * "gm": middle of "g0" and "g$".
8067 */
8068 case '^':
8069 flag = TRUE;
8070 /* FALLTHROUGH */
8071
8072 case '0':
8073 case 'm':
8074 case K_HOME:
8075 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 oap->motion_type = MCHAR;
8077 oap->inclusive = FALSE;
8078 if (curwin->w_p_wrap
8079#ifdef FEAT_VERTSPLIT
8080 && curwin->w_width != 0
8081#endif
8082 )
8083 {
8084 int width1 = W_WIDTH(curwin) - curwin_col_off();
8085 int width2 = width1 + curwin_col_off2();
8086
8087 validate_virtcol();
8088 i = 0;
8089 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8090 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8091 }
8092 else
8093 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008094 /* Go to the middle of the screen line. When 'number' or
8095 * 'relativenumber' is on and lines are wrapping the middle can be more
8096 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (cap->nchar == 'm')
8098 i += (W_WIDTH(curwin) - curwin_col_off()
8099 + ((curwin->w_p_wrap && i > 0)
8100 ? curwin_col_off2() : 0)) / 2;
8101 coladvance((colnr_T)i);
8102 if (flag)
8103 {
8104 do
8105 i = gchar_cursor();
8106 while (vim_iswhite(i) && oneright() == OK);
8107 }
8108 curwin->w_set_curswant = TRUE;
8109 break;
8110
8111 case '_':
8112 /* "g_": to the last non-blank character in the line or <count> lines
8113 * downward. */
8114 cap->oap->motion_type = MCHAR;
8115 cap->oap->inclusive = TRUE;
8116 curwin->w_curswant = MAXCOL;
8117 if (cursor_down((long)(cap->count1 - 1),
8118 cap->oap->op_type == OP_NOP) == FAIL)
8119 clearopbeep(cap->oap);
8120 else
8121 {
8122 char_u *ptr = ml_get_curline();
8123
8124 /* In Visual mode we may end up after the line. */
8125 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8126 --curwin->w_cursor.col;
8127
8128 /* Decrease the cursor column until it's on a non-blank. */
8129 while (curwin->w_cursor.col > 0
8130 && vim_iswhite(ptr[curwin->w_cursor.col]))
8131 --curwin->w_cursor.col;
8132 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008133#ifdef FEAT_VISUAL
8134 adjust_for_sel(cap);
8135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137 break;
8138
8139 case '$':
8140 case K_END:
8141 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {
8143 int col_off = curwin_col_off();
8144
8145 oap->motion_type = MCHAR;
8146 oap->inclusive = TRUE;
8147 if (curwin->w_p_wrap
8148#ifdef FEAT_VERTSPLIT
8149 && curwin->w_width != 0
8150#endif
8151 )
8152 {
8153 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8154 if (cap->count1 == 1)
8155 {
8156 int width1 = W_WIDTH(curwin) - col_off;
8157 int width2 = width1 + curwin_col_off2();
8158
8159 validate_virtcol();
8160 i = width1 - 1;
8161 if (curwin->w_virtcol >= (colnr_T)width1)
8162 i += ((curwin->w_virtcol - width1) / width2 + 1)
8163 * width2;
8164 coladvance((colnr_T)i);
8165#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8166 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8167 {
8168 /*
8169 * Check for landing on a character that got split at
8170 * the end of the line. We do not want to advance to
8171 * the next screen line.
8172 */
8173 validate_virtcol();
8174 if (curwin->w_virtcol > (colnr_T)i)
8175 --curwin->w_cursor.col;
8176 }
8177#endif
8178 }
8179 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8180 clearopbeep(oap);
8181 }
8182 else
8183 {
8184 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8185 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008186
8187 /* Make sure we stick in this column. */
8188 validate_virtcol();
8189 curwin->w_curswant = curwin->w_virtcol;
8190 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
8192 }
8193 break;
8194
8195 /*
8196 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8197 */
8198 case '*':
8199 case '#':
8200#if POUND != '#'
8201 case POUND: /* pound sign (sometimes equal to '#') */
8202#endif
8203 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8204 case ']': /* :tselect for current identifier */
8205 nv_ident(cap);
8206 break;
8207
8208 /*
8209 * ge and gE: go back to end of word
8210 */
8211 case 'e':
8212 case 'E':
8213 oap->motion_type = MCHAR;
8214 curwin->w_set_curswant = TRUE;
8215 oap->inclusive = TRUE;
8216 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8217 clearopbeep(oap);
8218 break;
8219
8220 /*
8221 * "g CTRL-G": display info about cursor position
8222 */
8223 case Ctrl_G:
8224 cursor_pos_info();
8225 break;
8226
8227 /*
8228 * "gi": start Insert at the last position.
8229 */
8230 case 'i':
8231 if (curbuf->b_last_insert.lnum != 0)
8232 {
8233 curwin->w_cursor = curbuf->b_last_insert;
8234 check_cursor_lnum();
8235 i = (int)STRLEN(ml_get_curline());
8236 if (curwin->w_cursor.col > (colnr_T)i)
8237 {
8238#ifdef FEAT_VIRTUALEDIT
8239 if (virtual_active())
8240 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8241#endif
8242 curwin->w_cursor.col = i;
8243 }
8244 }
8245 cap->cmdchar = 'i';
8246 nv_edit(cap);
8247 break;
8248
8249 /*
8250 * "gI": Start insert in column 1.
8251 */
8252 case 'I':
8253 beginline(0);
8254 if (!checkclearopq(oap))
8255 invoke_edit(cap, FALSE, 'g', FALSE);
8256 break;
8257
8258#ifdef FEAT_SEARCHPATH
8259 /*
8260 * "gf": goto file, edit file under cursor
8261 * "]f" and "[f": can also be used.
8262 */
8263 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008264 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 nv_gotofile(cap);
8266 break;
8267#endif
8268
8269 /* "g'm" and "g`m": jump to mark without setting pcmark */
8270 case '\'':
8271 cap->arg = TRUE;
8272 /*FALLTHROUGH*/
8273 case '`':
8274 nv_gomark(cap);
8275 break;
8276
8277 /*
8278 * "gs": Goto sleep.
8279 */
8280 case 's':
8281 do_sleep(cap->count1 * 1000L);
8282 break;
8283
8284 /*
8285 * "ga": Display the ascii value of the character under the
8286 * cursor. It is displayed in decimal, hex, and octal. -- webb
8287 */
8288 case 'a':
8289 do_ascii(NULL);
8290 break;
8291
8292#ifdef FEAT_MBYTE
8293 /*
8294 * "g8": Display the bytes used for the UTF-8 character under the
8295 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008296 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 */
8298 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008299 if (cap->count0 == 8)
8300 utf_find_illegal();
8301 else
8302 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 break;
8304#endif
8305
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008306 case '<':
8307 show_sb_text();
8308 break;
8309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 /*
8311 * "gg": Goto the first line in file. With a count it goes to
8312 * that line number like for "G". -- webb
8313 */
8314 case 'g':
8315 cap->arg = FALSE;
8316 nv_goto(cap);
8317 break;
8318
8319 /*
8320 * Two-character operators:
8321 * "gq" Format text
8322 * "gw" Format text and keep cursor position
8323 * "g~" Toggle the case of the text.
8324 * "gu" Change text to lower case.
8325 * "gU" Change text to upper case.
8326 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008327 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 */
8329 case 'q':
8330 case 'w':
8331 oap->cursor_start = curwin->w_cursor;
8332 /*FALLTHROUGH*/
8333 case '~':
8334 case 'u':
8335 case 'U':
8336 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008337 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 nv_operator(cap);
8339 break;
8340
8341 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008342 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 * current function
8344 * "gD": idem, but in the current file.
8345 */
8346 case 'd':
8347 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008348 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 break;
8350
8351#ifdef FEAT_MOUSE
8352 /*
8353 * g<*Mouse> : <C-*mouse>
8354 */
8355 case K_MIDDLEMOUSE:
8356 case K_MIDDLEDRAG:
8357 case K_MIDDLERELEASE:
8358 case K_LEFTMOUSE:
8359 case K_LEFTDRAG:
8360 case K_LEFTRELEASE:
8361 case K_RIGHTMOUSE:
8362 case K_RIGHTDRAG:
8363 case K_RIGHTRELEASE:
8364 case K_X1MOUSE:
8365 case K_X1DRAG:
8366 case K_X1RELEASE:
8367 case K_X2MOUSE:
8368 case K_X2DRAG:
8369 case K_X2RELEASE:
8370 mod_mask = MOD_MASK_CTRL;
8371 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8372 break;
8373#endif
8374
8375 case K_IGNORE:
8376 break;
8377
8378 /*
8379 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8380 */
8381 case 'p':
8382 case 'P':
8383 nv_put(cap);
8384 break;
8385
8386#ifdef FEAT_BYTEOFF
8387 /* "go": goto byte count from start of buffer */
8388 case 'o':
8389 goto_byte(cap->count0);
8390 break;
8391#endif
8392
8393 /* "gQ": improved Ex mode */
8394 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008395 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008398 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 break;
8400 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008401
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 if (!checkclearopq(oap))
8403 do_exmode(TRUE);
8404 break;
8405
8406#ifdef FEAT_JUMPLIST
8407 case ',':
8408 nv_pcmark(cap);
8409 break;
8410
8411 case ';':
8412 cap->count1 = -cap->count1;
8413 nv_pcmark(cap);
8414 break;
8415#endif
8416
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008417#ifdef FEAT_WINDOWS
8418 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008419 if (!checkclearop(oap))
8420 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008421 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008422 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008423 if (!checkclearop(oap))
8424 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008425 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008426#endif
8427
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008428 case '+':
8429 case '-': /* "g+" and "g-": undo or redo along the timeline */
8430 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008431 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008432 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008433 break;
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 default:
8436 clearopbeep(oap);
8437 break;
8438 }
8439}
8440
8441/*
8442 * Handle "o" and "O" commands.
8443 */
8444 static void
8445n_opencmd(cap)
8446 cmdarg_T *cap;
8447{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008448#ifdef FEAT_CONCEAL
8449 linenr_T oldline = curwin->w_cursor.lnum;
8450#endif
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 if (!checkclearopq(cap->oap))
8453 {
8454#ifdef FEAT_FOLDING
8455 if (cap->cmdchar == 'O')
8456 /* Open above the first line of a folded sequence of lines */
8457 (void)hasFolding(curwin->w_cursor.lnum,
8458 &curwin->w_cursor.lnum, NULL);
8459 else
8460 /* Open below the last line of a folded sequence of lines */
8461 (void)hasFolding(curwin->w_cursor.lnum,
8462 NULL, &curwin->w_cursor.lnum);
8463#endif
8464 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8465 (cap->cmdchar == 'O' ? 1 : 0)),
8466 (linenr_T)(curwin->w_cursor.lnum +
8467 (cap->cmdchar == 'o' ? 1 : 0))
8468 ) == OK
8469 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8470#ifdef FEAT_COMMENTS
8471 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8472#endif
8473 0, 0))
8474 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008475#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008476 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008477 update_single_line(curwin, oldline);
8478#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008479 /* When '#' is in 'cpoptions' ignore the count. */
8480 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8481 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8483 }
8484 }
8485}
8486
8487/*
8488 * "." command: redo last change.
8489 */
8490 static void
8491nv_dot(cap)
8492 cmdarg_T *cap;
8493{
8494 if (!checkclearopq(cap->oap))
8495 {
8496 /*
8497 * If "restart_edit" is TRUE, the last but one command is repeated
8498 * instead of the last command (inserting text). This is used for
8499 * CTRL-O <.> in insert mode.
8500 */
8501 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8502 clearopbeep(cap->oap);
8503 }
8504}
8505
8506/*
8507 * CTRL-R: undo undo
8508 */
8509 static void
8510nv_redo(cap)
8511 cmdarg_T *cap;
8512{
8513 if (!checkclearopq(cap->oap))
8514 {
8515 u_redo((int)cap->count1);
8516 curwin->w_set_curswant = TRUE;
8517 }
8518}
8519
8520/*
8521 * Handle "U" command.
8522 */
8523 static void
8524nv_Undo(cap)
8525 cmdarg_T *cap;
8526{
8527 /* In Visual mode and typing "gUU" triggers an operator */
8528 if (cap->oap->op_type == OP_UPPER
8529#ifdef FEAT_VISUAL
8530 || VIsual_active
8531#endif
8532 )
8533 {
8534 /* translate "gUU" to "gUgU" */
8535 cap->cmdchar = 'g';
8536 cap->nchar = 'U';
8537 nv_operator(cap);
8538 }
8539 else if (!checkclearopq(cap->oap))
8540 {
8541 u_undoline();
8542 curwin->w_set_curswant = TRUE;
8543 }
8544}
8545
8546/*
8547 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8548 * single character.
8549 */
8550 static void
8551nv_tilde(cap)
8552 cmdarg_T *cap;
8553{
8554 if (!p_to
8555#ifdef FEAT_VISUAL
8556 && !VIsual_active
8557#endif
8558 && cap->oap->op_type != OP_TILDE)
8559 n_swapchar(cap);
8560 else
8561 nv_operator(cap);
8562}
8563
8564/*
8565 * Handle an operator command.
8566 * The actual work is done by do_pending_operator().
8567 */
8568 static void
8569nv_operator(cap)
8570 cmdarg_T *cap;
8571{
8572 int op_type;
8573
8574 op_type = get_op_type(cap->cmdchar, cap->nchar);
8575
8576 if (op_type == cap->oap->op_type) /* double operator works on lines */
8577 nv_lineop(cap);
8578 else if (!checkclearop(cap->oap))
8579 {
8580 cap->oap->start = curwin->w_cursor;
8581 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008582#ifdef FEAT_EVAL
8583 set_op_var(op_type);
8584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 }
8586}
8587
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008588#ifdef FEAT_EVAL
8589/*
8590 * Set v:operator to the characters for "optype".
8591 */
8592 static void
8593set_op_var(optype)
8594 int optype;
8595{
8596 char_u opchars[3];
8597
8598 if (optype == OP_NOP)
8599 set_vim_var_string(VV_OP, NULL, 0);
8600 else
8601 {
8602 opchars[0] = get_op_char(optype);
8603 opchars[1] = get_extra_op_char(optype);
8604 opchars[2] = NUL;
8605 set_vim_var_string(VV_OP, opchars, -1);
8606 }
8607}
8608#endif
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610/*
8611 * Handle linewise operator "dd", "yy", etc.
8612 *
8613 * "_" is is a strange motion command that helps make operators more logical.
8614 * It is actually implemented, but not documented in the real Vi. This motion
8615 * command actually refers to "the current line". Commands like "dd" and "yy"
8616 * are really an alternate form of "d_" and "y_". It does accept a count, so
8617 * "d3_" works to delete 3 lines.
8618 */
8619 static void
8620nv_lineop(cap)
8621 cmdarg_T *cap;
8622{
8623 cap->oap->motion_type = MLINE;
8624 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8625 clearopbeep(cap->oap);
8626 else if ( cap->oap->op_type == OP_DELETE
8627 || cap->oap->op_type == OP_LSHIFT
8628 || cap->oap->op_type == OP_RSHIFT)
8629 beginline(BL_SOL | BL_FIX);
8630 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8631 beginline(BL_WHITE | BL_FIX);
8632}
8633
8634/*
8635 * <Home> command.
8636 */
8637 static void
8638nv_home(cap)
8639 cmdarg_T *cap;
8640{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008641 /* CTRL-HOME is like "gg" */
8642 if (mod_mask & MOD_MASK_CTRL)
8643 nv_goto(cap);
8644 else
8645 {
8646 cap->count0 = 1;
8647 nv_pipe(cap);
8648 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008649 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8650 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
8654 * "|" command.
8655 */
8656 static void
8657nv_pipe(cap)
8658 cmdarg_T *cap;
8659{
8660 cap->oap->motion_type = MCHAR;
8661 cap->oap->inclusive = FALSE;
8662 beginline(0);
8663 if (cap->count0 > 0)
8664 {
8665 coladvance((colnr_T)(cap->count0 - 1));
8666 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8667 }
8668 else
8669 curwin->w_curswant = 0;
8670 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008671 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 curwin->w_set_curswant = FALSE;
8673}
8674
8675/*
8676 * Handle back-word command "b" and "B".
8677 * cap->arg is 1 for "B"
8678 */
8679 static void
8680nv_bck_word(cap)
8681 cmdarg_T *cap;
8682{
8683 cap->oap->motion_type = MCHAR;
8684 cap->oap->inclusive = FALSE;
8685 curwin->w_set_curswant = TRUE;
8686 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8687 clearopbeep(cap->oap);
8688#ifdef FEAT_FOLDING
8689 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8690 foldOpenCursor();
8691#endif
8692}
8693
8694/*
8695 * Handle word motion commands "e", "E", "w" and "W".
8696 * cap->arg is TRUE for "E" and "W".
8697 */
8698 static void
8699nv_wordcmd(cap)
8700 cmdarg_T *cap;
8701{
8702 int n;
8703 int word_end;
8704 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008705 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 /*
8708 * Set inclusive for the "E" and "e" command.
8709 */
8710 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8711 word_end = TRUE;
8712 else
8713 word_end = FALSE;
8714 cap->oap->inclusive = word_end;
8715
8716 /*
8717 * "cw" and "cW" are a special case.
8718 */
8719 if (!word_end && cap->oap->op_type == OP_CHANGE)
8720 {
8721 n = gchar_cursor();
8722 if (n != NUL) /* not an empty line */
8723 {
8724 if (vim_iswhite(n))
8725 {
8726 /*
8727 * Reproduce a funny Vi behaviour: "cw" on a blank only
8728 * changes one character, not all blanks until the start of
8729 * the next word. Only do this when the 'w' flag is included
8730 * in 'cpoptions'.
8731 */
8732 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8733 {
8734 cap->oap->inclusive = TRUE;
8735 cap->oap->motion_type = MCHAR;
8736 return;
8737 }
8738 }
8739 else
8740 {
8741 /*
8742 * This is a little strange. To match what the real Vi does,
8743 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8744 * that we are not on a space or a TAB. This seems impolite
8745 * at first, but it's really more what we mean when we say
8746 * 'cw'.
8747 * Another strangeness: When standing on the end of a word
8748 * "ce" will change until the end of the next wordt, but "cw"
8749 * will change only one character! This is done by setting
8750 * flag.
8751 */
8752 cap->oap->inclusive = TRUE;
8753 word_end = TRUE;
8754 flag = TRUE;
8755 }
8756 }
8757 }
8758
8759 cap->oap->motion_type = MCHAR;
8760 curwin->w_set_curswant = TRUE;
8761 if (word_end)
8762 n = end_word(cap->count1, cap->arg, flag, FALSE);
8763 else
8764 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8765
Bram Moolenaardfefb982008-04-01 10:06:39 +00008766 /* Don't leave the cursor on the NUL past the end of line. Unless we
8767 * didn't move it forward. */
8768 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008769 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770
8771 if (n == FAIL && cap->oap->op_type == OP_NOP)
8772 clearopbeep(cap->oap);
8773 else
8774 {
8775#ifdef FEAT_VISUAL
8776 adjust_for_sel(cap);
8777#endif
8778#ifdef FEAT_FOLDING
8779 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8780 foldOpenCursor();
8781#endif
8782 }
8783}
8784
8785/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008786 * Used after a movement command: If the cursor ends up on the NUL after the
8787 * end of the line, may move it back to the last character and make the motion
8788 * inclusive.
8789 */
8790 static void
8791adjust_cursor(oap)
8792 oparg_T *oap;
8793{
8794 /* The cursor cannot remain on the NUL when:
8795 * - the column is > 0
8796 * - not in Visual mode or 'selection' is "o"
8797 * - 'virtualedit' is not "all" and not "onemore".
8798 */
8799 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8800#ifdef FEAT_VISUAL
8801 && (!VIsual_active || *p_sel == 'o')
8802#endif
8803#ifdef FEAT_VIRTUALEDIT
8804 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8805#endif
8806 )
8807 {
8808 --curwin->w_cursor.col;
8809#ifdef FEAT_MBYTE
8810 /* prevent cursor from moving on the trail byte */
8811 if (has_mbyte)
8812 mb_adjust_cursor();
8813#endif
8814 oap->inclusive = TRUE;
8815 }
8816}
8817
8818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 * "0" and "^" commands.
8820 * cap->arg is the argument for beginline().
8821 */
8822 static void
8823nv_beginline(cap)
8824 cmdarg_T *cap;
8825{
8826 cap->oap->motion_type = MCHAR;
8827 cap->oap->inclusive = FALSE;
8828 beginline(cap->arg);
8829#ifdef FEAT_FOLDING
8830 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8831 foldOpenCursor();
8832#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008833 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8834 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
8837#ifdef FEAT_VISUAL
8838/*
8839 * In exclusive Visual mode, may include the last character.
8840 */
8841 static void
8842adjust_for_sel(cap)
8843 cmdarg_T *cap;
8844{
8845 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8846 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8847 {
8848# ifdef FEAT_MBYTE
8849 if (has_mbyte)
8850 inc_cursor();
8851 else
8852# endif
8853 ++curwin->w_cursor.col;
8854 cap->oap->inclusive = FALSE;
8855 }
8856}
8857
8858/*
8859 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8860 * Should check VIsual_mode before calling this.
8861 * Returns TRUE when backed up to the previous line.
8862 */
8863 static int
8864unadjust_for_sel()
8865{
8866 pos_T *pp;
8867
8868 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8869 {
8870 if (lt(VIsual, curwin->w_cursor))
8871 pp = &curwin->w_cursor;
8872 else
8873 pp = &VIsual;
8874#ifdef FEAT_VIRTUALEDIT
8875 if (pp->coladd > 0)
8876 --pp->coladd;
8877 else
8878#endif
8879 if (pp->col > 0)
8880 {
8881 --pp->col;
8882#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008883 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884#endif
8885 }
8886 else if (pp->lnum > 1)
8887 {
8888 --pp->lnum;
8889 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8890 return TRUE;
8891 }
8892 }
8893 return FALSE;
8894}
8895
8896/*
8897 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8898 */
8899 static void
8900nv_select(cap)
8901 cmdarg_T *cap;
8902{
8903 if (VIsual_active)
8904 VIsual_select = TRUE;
8905 else if (VIsual_reselect)
8906 {
8907 cap->nchar = 'v'; /* fake "gv" command */
8908 cap->arg = TRUE;
8909 nv_g_cmd(cap);
8910 }
8911}
8912
8913#endif
8914
8915/*
8916 * "G", "gg", CTRL-END, CTRL-HOME.
8917 * cap->arg is TRUE for "G".
8918 */
8919 static void
8920nv_goto(cap)
8921 cmdarg_T *cap;
8922{
8923 linenr_T lnum;
8924
8925 if (cap->arg)
8926 lnum = curbuf->b_ml.ml_line_count;
8927 else
8928 lnum = 1L;
8929 cap->oap->motion_type = MLINE;
8930 setpcmark();
8931
8932 /* When a count is given, use it instead of the default lnum */
8933 if (cap->count0 != 0)
8934 lnum = cap->count0;
8935 if (lnum < 1L)
8936 lnum = 1L;
8937 else if (lnum > curbuf->b_ml.ml_line_count)
8938 lnum = curbuf->b_ml.ml_line_count;
8939 curwin->w_cursor.lnum = lnum;
8940 beginline(BL_SOL | BL_FIX);
8941#ifdef FEAT_FOLDING
8942 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8943 foldOpenCursor();
8944#endif
8945}
8946
8947/*
8948 * CTRL-\ in Normal mode.
8949 */
8950 static void
8951nv_normal(cap)
8952 cmdarg_T *cap;
8953{
8954 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8955 {
8956 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008957 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 clear_cmdline = TRUE; /* unshow mode later */
8959 restart_edit = 0;
8960#ifdef FEAT_CMDWIN
8961 if (cmdwin_type != 0)
8962 cmdwin_result = Ctrl_C;
8963#endif
8964#ifdef FEAT_VISUAL
8965 if (VIsual_active)
8966 {
8967 end_visual_mode(); /* stop Visual */
8968 redraw_curbuf_later(INVERTED);
8969 }
8970#endif
8971 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8972 if (cap->nchar == Ctrl_G && p_im)
8973 restart_edit = 'a';
8974 }
8975 else
8976 clearopbeep(cap->oap);
8977}
8978
8979/*
8980 * ESC in Normal mode: beep, but don't flush buffers.
8981 * Don't even beep if we are canceling a command.
8982 */
8983 static void
8984nv_esc(cap)
8985 cmdarg_T *cap;
8986{
8987 int no_reason;
8988
8989 no_reason = (cap->oap->op_type == OP_NOP
8990 && cap->opcount == 0
8991 && cap->count0 == 0
8992 && cap->oap->regname == 0
8993 && !p_im);
8994
8995 if (cap->arg) /* TRUE for CTRL-C */
8996 {
8997 if (restart_edit == 0
8998#ifdef FEAT_CMDWIN
8999 && cmdwin_type == 0
9000#endif
9001#ifdef FEAT_VISUAL
9002 && !VIsual_active
9003#endif
9004 && no_reason)
9005 MSG(_("Type :quit<Enter> to exit Vim"));
9006
9007 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9008 * set again below when halfway a mapping. */
9009 if (!p_im)
9010 restart_edit = 0;
9011#ifdef FEAT_CMDWIN
9012 if (cmdwin_type != 0)
9013 {
9014 cmdwin_result = K_IGNORE;
9015 got_int = FALSE; /* don't stop executing autocommands et al. */
9016 return;
9017 }
9018#endif
9019 }
9020
9021#ifdef FEAT_VISUAL
9022 if (VIsual_active)
9023 {
9024 end_visual_mode(); /* stop Visual */
9025 check_cursor_col(); /* make sure cursor is not beyond EOL */
9026 curwin->w_set_curswant = TRUE;
9027 redraw_curbuf_later(INVERTED);
9028 }
9029 else
9030#endif
9031 if (no_reason)
9032 vim_beep();
9033 clearop(cap->oap);
9034
9035 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9036 * set return to Insert mode afterwards. */
9037 if (restart_edit == 0 && goto_im()
9038#ifdef FEAT_EX_EXTRA
9039 && ex_normal_busy == 0
9040#endif
9041 )
9042 restart_edit = 'a';
9043}
9044
9045/*
9046 * Handle "A", "a", "I", "i" and <Insert> commands.
9047 */
9048 static void
9049nv_edit(cap)
9050 cmdarg_T *cap;
9051{
9052 /* <Insert> is equal to "i" */
9053 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9054 cap->cmdchar = 'i';
9055
9056#ifdef FEAT_VISUAL
9057 /* in Visual mode "A" and "I" are an operator */
9058 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9059 v_visop(cap);
9060
9061 /* in Visual mode and after an operator "a" and "i" are for text objects */
9062 else
9063#endif
9064 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9065 && (cap->oap->op_type != OP_NOP
9066#ifdef FEAT_VISUAL
9067 || VIsual_active
9068#endif
9069 ))
9070 {
9071#ifdef FEAT_TEXTOBJ
9072 nv_object(cap);
9073#else
9074 clearopbeep(cap->oap);
9075#endif
9076 }
9077 else if (!curbuf->b_p_ma && !p_im)
9078 {
9079 /* Only give this error when 'insertmode' is off. */
9080 EMSG(_(e_modifiable));
9081 clearop(cap->oap);
9082 }
9083 else if (!checkclearopq(cap->oap))
9084 {
9085 switch (cap->cmdchar)
9086 {
9087 case 'A': /* "A"ppend after the line */
9088 curwin->w_set_curswant = TRUE;
9089#ifdef FEAT_VIRTUALEDIT
9090 if (ve_flags == VE_ALL)
9091 {
9092 int save_State = State;
9093
9094 /* Pretent Insert mode here to allow the cursor on the
9095 * character past the end of the line */
9096 State = INSERT;
9097 coladvance((colnr_T)MAXCOL);
9098 State = save_State;
9099 }
9100 else
9101#endif
9102 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9103 break;
9104
9105 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009106 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9107 beginline(BL_WHITE);
9108 else
9109 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 break;
9111
9112 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9113#ifdef FEAT_VIRTUALEDIT
9114 /* increment coladd when in virtual space, increment the
9115 * column otherwise, also to append after an unprintable char */
9116 if (virtual_active()
9117 && (curwin->w_cursor.coladd > 0
9118 || *ml_get_cursor() == NUL
9119 || *ml_get_cursor() == TAB))
9120 curwin->w_cursor.coladd++;
9121 else
9122#endif
9123 if (*ml_get_cursor() != NUL)
9124 inc_cursor();
9125 break;
9126 }
9127
9128#ifdef FEAT_VIRTUALEDIT
9129 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9130 {
9131 int save_State = State;
9132
9133 /* Pretent Insert mode here to allow the cursor on the
9134 * character past the end of the line */
9135 State = INSERT;
9136 coladvance(getviscol());
9137 State = save_State;
9138 }
9139#endif
9140
9141 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9142 }
9143}
9144
9145/*
9146 * Invoke edit() and take care of "restart_edit" and the return value.
9147 */
9148 static void
9149invoke_edit(cap, repl, cmd, startln)
9150 cmdarg_T *cap;
9151 int repl; /* "r" or "gr" command */
9152 int cmd;
9153 int startln;
9154{
9155 int restart_edit_save = 0;
9156
9157 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9158 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9159 * it. */
9160 if (repl || !stuff_empty())
9161 restart_edit_save = restart_edit;
9162 else
9163 restart_edit_save = 0;
9164
9165 /* Always reset "restart_edit", this is not a restarted edit. */
9166 restart_edit = 0;
9167
9168 if (edit(cmd, startln, cap->count1))
9169 cap->retval |= CA_COMMAND_BUSY;
9170
9171 if (restart_edit == 0)
9172 restart_edit = restart_edit_save;
9173}
9174
9175#ifdef FEAT_TEXTOBJ
9176/*
9177 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9178 */
9179 static void
9180nv_object(cap)
9181 cmdarg_T *cap;
9182{
9183 int flag;
9184 int include;
9185 char_u *mps_save;
9186
9187 if (cap->cmdchar == 'i')
9188 include = FALSE; /* "ix" = inner object: exclude white space */
9189 else
9190 include = TRUE; /* "ax" = an object: include white space */
9191
9192 /* Make sure (), [], {} and <> are in 'matchpairs' */
9193 mps_save = curbuf->b_p_mps;
9194 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9195
9196 switch (cap->nchar)
9197 {
9198 case 'w': /* "aw" = a word */
9199 flag = current_word(cap->oap, cap->count1, include, FALSE);
9200 break;
9201 case 'W': /* "aW" = a WORD */
9202 flag = current_word(cap->oap, cap->count1, include, TRUE);
9203 break;
9204 case 'b': /* "ab" = a braces block */
9205 case '(':
9206 case ')':
9207 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9208 break;
9209 case 'B': /* "aB" = a Brackets block */
9210 case '{':
9211 case '}':
9212 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9213 break;
9214 case '[': /* "a[" = a [] block */
9215 case ']':
9216 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9217 break;
9218 case '<': /* "a<" = a <> block */
9219 case '>':
9220 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9221 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009222 case 't': /* "at" = a tag block (xml and html) */
9223 flag = current_tagblock(cap->oap, cap->count1, include);
9224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 case 'p': /* "ap" = a paragraph */
9226 flag = current_par(cap->oap, cap->count1, include, 'p');
9227 break;
9228 case 's': /* "as" = a sentence */
9229 flag = current_sent(cap->oap, cap->count1, include);
9230 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009231 case '"': /* "a"" = a double quoted string */
9232 case '\'': /* "a'" = a single quoted string */
9233 case '`': /* "a`" = a backtick quoted string */
9234 flag = current_quote(cap->oap, cap->count1, include,
9235 cap->nchar);
9236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237#if 0 /* TODO */
9238 case 'S': /* "aS" = a section */
9239 case 'f': /* "af" = a filename */
9240 case 'u': /* "au" = a URL */
9241#endif
9242 default:
9243 flag = FAIL;
9244 break;
9245 }
9246
9247 curbuf->b_p_mps = mps_save;
9248 if (flag == FAIL)
9249 clearopbeep(cap->oap);
9250 adjust_cursor_col();
9251 curwin->w_set_curswant = TRUE;
9252}
9253#endif
9254
9255/*
9256 * "q" command: Start/stop recording.
9257 * "q:", "q/", "q?": edit command-line in command-line window.
9258 */
9259 static void
9260nv_record(cap)
9261 cmdarg_T *cap;
9262{
9263 if (cap->oap->op_type == OP_FORMAT)
9264 {
9265 /* "gqq" is the same as "gqgq": format line */
9266 cap->cmdchar = 'g';
9267 cap->nchar = 'q';
9268 nv_operator(cap);
9269 }
9270 else if (!checkclearop(cap->oap))
9271 {
9272#ifdef FEAT_CMDWIN
9273 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9274 {
9275 stuffcharReadbuff(cap->nchar);
9276 stuffcharReadbuff(K_CMDWIN);
9277 }
9278 else
9279#endif
9280 /* (stop) recording into a named register, unless executing a
9281 * register */
9282 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9283 clearopbeep(cap->oap);
9284 }
9285}
9286
9287/*
9288 * Handle the "@r" command.
9289 */
9290 static void
9291nv_at(cap)
9292 cmdarg_T *cap;
9293{
9294 if (checkclearop(cap->oap))
9295 return;
9296#ifdef FEAT_EVAL
9297 if (cap->nchar == '=')
9298 {
9299 if (get_expr_register() == NUL)
9300 return;
9301 }
9302#endif
9303 while (cap->count1-- && !got_int)
9304 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009305 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 {
9307 clearopbeep(cap->oap);
9308 break;
9309 }
9310 line_breakcheck();
9311 }
9312}
9313
9314/*
9315 * Handle the CTRL-U and CTRL-D commands.
9316 */
9317 static void
9318nv_halfpage(cap)
9319 cmdarg_T *cap;
9320{
9321 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9322 || (cap->cmdchar == Ctrl_D
9323 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9324 clearopbeep(cap->oap);
9325 else if (!checkclearop(cap->oap))
9326 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9327}
9328
9329/*
9330 * Handle "J" or "gJ" command.
9331 */
9332 static void
9333nv_join(cap)
9334 cmdarg_T *cap;
9335{
9336#ifdef FEAT_VISUAL
9337 if (VIsual_active) /* join the visual lines */
9338 nv_operator(cap);
9339 else
9340#endif
9341 if (!checkclearop(cap->oap))
9342 {
9343 if (cap->count0 <= 1)
9344 cap->count0 = 2; /* default for join is two lines! */
9345 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9346 curbuf->b_ml.ml_line_count)
9347 clearopbeep(cap->oap); /* beyond last line */
9348 else
9349 {
9350 prep_redo(cap->oap->regname, cap->count0,
9351 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009352 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 }
9354 }
9355}
9356
9357/*
9358 * "P", "gP", "p" and "gp" commands.
9359 */
9360 static void
9361nv_put(cap)
9362 cmdarg_T *cap;
9363{
9364#ifdef FEAT_VISUAL
9365 int regname = 0;
9366 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009367 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009368 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369#endif
9370 int dir;
9371 int flags = 0;
9372
9373 if (cap->oap->op_type != OP_NOP)
9374 {
9375#ifdef FEAT_DIFF
9376 /* "dp" is ":diffput" */
9377 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9378 {
9379 clearop(cap->oap);
9380 nv_diffgetput(TRUE);
9381 }
9382 else
9383#endif
9384 clearopbeep(cap->oap);
9385 }
9386 else
9387 {
9388 dir = (cap->cmdchar == 'P'
9389 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9390 ? BACKWARD : FORWARD;
9391 prep_redo_cmd(cap);
9392 if (cap->cmdchar == 'g')
9393 flags |= PUT_CURSEND;
9394
9395#ifdef FEAT_VISUAL
9396 if (VIsual_active)
9397 {
9398 /* Putting in Visual mode: The put text replaces the selected
9399 * text. First delete the selected text, then put the new text.
9400 * Need to save and restore the registers that the delete
9401 * overwrites if the old contents is being put.
9402 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009403 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 regname = cap->oap->regname;
9405# ifdef FEAT_CLIPBOARD
9406 adjust_clip_reg(&regname);
9407# endif
Bram Moolenaar53748fc2012-01-26 11:43:09 +01009408 if (regname == 0 || regname == '"' || VIM_ISDIGIT(regname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409# ifdef FEAT_CLIPBOARD
9410 || (clip_unnamed && (regname == '*' || regname == '+'))
9411# endif
9412
9413 )
9414 {
9415 /* the delete is going to overwrite the register we want to
9416 * put, save it first. */
9417 reg1 = get_register(regname, TRUE);
9418 }
9419
9420 /* Now delete the selected text. */
9421 cap->cmdchar = 'd';
9422 cap->nchar = NUL;
9423 cap->oap->regname = NUL;
9424 nv_operator(cap);
9425 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009426 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
9428 /* delete PUT_LINE_BACKWARD; */
9429 cap->oap->regname = regname;
9430
9431 if (reg1 != NULL)
9432 {
9433 /* Delete probably changed the register we want to put, save
9434 * it first. Then put back what was there before the delete. */
9435 reg2 = get_register(regname, FALSE);
9436 put_register(regname, reg1);
9437 }
9438
9439 /* When deleted a linewise Visual area, put the register as
9440 * lines to avoid it joined with the next line. When deletion was
9441 * characterwise, split a line when putting lines. */
9442 if (VIsual_mode == 'V')
9443 flags |= PUT_LINE;
9444 else if (VIsual_mode == 'v')
9445 flags |= PUT_LINE_SPLIT;
9446 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9447 flags |= PUT_LINE_FORWARD;
9448 dir = BACKWARD;
9449 if ((VIsual_mode != 'V'
9450 && curwin->w_cursor.col < curbuf->b_op_start.col)
9451 || (VIsual_mode == 'V'
9452 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9453 /* cursor is at the end of the line or end of file, put
9454 * forward. */
9455 dir = FORWARD;
9456 }
9457#endif
9458 do_put(cap->oap->regname, dir, cap->count1, flags);
9459
9460#ifdef FEAT_VISUAL
9461 /* If a register was saved, put it back now. */
9462 if (reg2 != NULL)
9463 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009464
9465 /* What to reselect with "gv"? Selecting the just put text seems to
9466 * be the most useful, since the original text was removed. */
9467 if (was_visual)
9468 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009469 curbuf->b_visual.vi_start = curbuf->b_op_start;
9470 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009471 }
9472
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009473 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009474 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009475 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009476 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009477 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009478
9479 /* If the cursor was in that line, move it to the end of the last
9480 * line. */
9481 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9482 {
9483 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9484 coladvance((colnr_T)MAXCOL);
9485 }
9486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487#endif
9488 auto_format(FALSE, TRUE);
9489 }
9490}
9491
9492/*
9493 * "o" and "O" commands.
9494 */
9495 static void
9496nv_open(cap)
9497 cmdarg_T *cap;
9498{
9499#ifdef FEAT_DIFF
9500 /* "do" is ":diffget" */
9501 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9502 {
9503 clearop(cap->oap);
9504 nv_diffgetput(FALSE);
9505 }
9506 else
9507#endif
9508#ifdef FEAT_VISUAL
9509 if (VIsual_active) /* switch start and end of visual */
9510 v_swap_corners(cap->cmdchar);
9511 else
9512#endif
9513 n_opencmd(cap);
9514}
9515
9516#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 static void
9518nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009519 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 ProcessSniffRequests();
9522}
9523#endif
9524
9525#ifdef FEAT_NETBEANS_INTG
9526 static void
9527nv_nbcmd(cap)
9528 cmdarg_T *cap;
9529{
9530 netbeans_keycommand(cap->nchar);
9531}
9532#endif
9533
9534#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 static void
9536nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009537 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9540}
9541#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009542
9543#ifdef FEAT_AUTOCMD
9544/*
9545 * Trigger CursorHold event.
9546 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9547 * input buffer. "did_cursorhold" is set to avoid retriggering.
9548 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009549 static void
9550nv_cursorhold(cap)
9551 cmdarg_T *cap;
9552{
9553 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9554 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009555 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009556}
9557#endif