blob: f76aeee37efc7f57e244483048e7fa7621360e6c [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 Moolenaara390bb62013-03-13 19:02:41 +010024static int VIsual_mode_orig = NUL; /* type of Visual mode, that user entered */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26static int restart_VIsual_select = 0;
27#endif
28
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#ifdef FEAT_EVAL
30static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020033#ifdef __BORLANDC__
34 _RTLENTRYF
35#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 nv_compare __ARGS((const void *s1, const void *s2));
37static int find_command __ARGS((int cmdchar));
38static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000039static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
41static void find_start_of_word __ARGS((pos_T *));
42static void find_end_of_word __ARGS((pos_T *));
43static int get_mouse_class __ARGS((char_u *p));
44#endif
45static void prep_redo_cmd __ARGS((cmdarg_T *cap));
46static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
47static int checkclearop __ARGS((oparg_T *oap));
48static int checkclearopq __ARGS((oparg_T *oap));
49static void clearop __ARGS((oparg_T *oap));
50static void clearopbeep __ARGS((oparg_T *oap));
51#ifdef FEAT_VISUAL
52static void unshift_special __ARGS((cmdarg_T *cap));
53#endif
54#ifdef FEAT_CMDL_INFO
55static void del_from_showcmd __ARGS((int));
56#endif
57
58/*
59 * nv_*(): functions called to handle Normal and Visual mode commands.
60 * n_*(): functions called to handle Normal mode commands.
61 * v_*(): functions called to handle Visual mode commands.
62 */
63static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000064static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static void nv_error __ARGS((cmdarg_T *cap));
66static void nv_help __ARGS((cmdarg_T *cap));
67static void nv_addsub __ARGS((cmdarg_T *cap));
68static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000069static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
71#ifdef FEAT_MOUSE
72static void nv_mousescroll __ARGS((cmdarg_T *cap));
73static void nv_mouse __ARGS((cmdarg_T *cap));
74#endif
75static void nv_scroll_line __ARGS((cmdarg_T *cap));
76static void nv_zet __ARGS((cmdarg_T *cap));
77#ifdef FEAT_GUI
78static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
79static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
80#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#ifdef FEAT_GUI_TABLINE
82static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000083static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static void nv_exmode __ARGS((cmdarg_T *cap));
86static void nv_colon __ARGS((cmdarg_T *cap));
87static void nv_ctrlg __ARGS((cmdarg_T *cap));
88static void nv_ctrlh __ARGS((cmdarg_T *cap));
89static void nv_clear __ARGS((cmdarg_T *cap));
90static void nv_ctrlo __ARGS((cmdarg_T *cap));
91static void nv_hat __ARGS((cmdarg_T *cap));
92static void nv_Zet __ARGS((cmdarg_T *cap));
93static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void nv_tagpop __ARGS((cmdarg_T *cap));
95static void nv_scroll __ARGS((cmdarg_T *cap));
96static void nv_right __ARGS((cmdarg_T *cap));
97static void nv_left __ARGS((cmdarg_T *cap));
98static void nv_up __ARGS((cmdarg_T *cap));
99static void nv_down __ARGS((cmdarg_T *cap));
100#ifdef FEAT_SEARCHPATH
101static void nv_gotofile __ARGS((cmdarg_T *cap));
102#endif
103static void nv_end __ARGS((cmdarg_T *cap));
104static void nv_dollar __ARGS((cmdarg_T *cap));
105static void nv_search __ARGS((cmdarg_T *cap));
106static void nv_next __ARGS((cmdarg_T *cap));
107static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
108static void nv_csearch __ARGS((cmdarg_T *cap));
109static void nv_brackets __ARGS((cmdarg_T *cap));
110static void nv_percent __ARGS((cmdarg_T *cap));
111static void nv_brace __ARGS((cmdarg_T *cap));
112static void nv_mark __ARGS((cmdarg_T *cap));
113static void nv_findpar __ARGS((cmdarg_T *cap));
114static void nv_undo __ARGS((cmdarg_T *cap));
115static void nv_kundo __ARGS((cmdarg_T *cap));
116static void nv_Replace __ARGS((cmdarg_T *cap));
117#ifdef FEAT_VREPLACE
118static void nv_vreplace __ARGS((cmdarg_T *cap));
119#endif
120#ifdef FEAT_VISUAL
121static void v_swap_corners __ARGS((int cmdchar));
122#endif
123static void nv_replace __ARGS((cmdarg_T *cap));
124static void n_swapchar __ARGS((cmdarg_T *cap));
125static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
126#ifdef FEAT_VISUAL
127static void v_visop __ARGS((cmdarg_T *cap));
128#endif
129static void nv_subst __ARGS((cmdarg_T *cap));
130static void nv_abbrev __ARGS((cmdarg_T *cap));
131static void nv_optrans __ARGS((cmdarg_T *cap));
132static void nv_gomark __ARGS((cmdarg_T *cap));
133static void nv_pcmark __ARGS((cmdarg_T *cap));
134static void nv_regname __ARGS((cmdarg_T *cap));
135#ifdef FEAT_VISUAL
136static void nv_visual __ARGS((cmdarg_T *cap));
137static void n_start_visual_mode __ARGS((int c));
138#endif
139static void nv_window __ARGS((cmdarg_T *cap));
140static void nv_suspend __ARGS((cmdarg_T *cap));
141static void nv_g_cmd __ARGS((cmdarg_T *cap));
142static void n_opencmd __ARGS((cmdarg_T *cap));
143static void nv_dot __ARGS((cmdarg_T *cap));
144static void nv_redo __ARGS((cmdarg_T *cap));
145static void nv_Undo __ARGS((cmdarg_T *cap));
146static void nv_tilde __ARGS((cmdarg_T *cap));
147static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000148#ifdef FEAT_EVAL
149static void set_op_var __ARGS((int optype));
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void nv_lineop __ARGS((cmdarg_T *cap));
152static void nv_home __ARGS((cmdarg_T *cap));
153static void nv_pipe __ARGS((cmdarg_T *cap));
154static void nv_bck_word __ARGS((cmdarg_T *cap));
155static void nv_wordcmd __ARGS((cmdarg_T *cap));
156static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000157static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#ifdef FEAT_VISUAL
159static void adjust_for_sel __ARGS((cmdarg_T *cap));
160static int unadjust_for_sel __ARGS((void));
161static void nv_select __ARGS((cmdarg_T *cap));
162#endif
163static void nv_goto __ARGS((cmdarg_T *cap));
164static void nv_normal __ARGS((cmdarg_T *cap));
165static void nv_esc __ARGS((cmdarg_T *oap));
166static void nv_edit __ARGS((cmdarg_T *cap));
167static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
168#ifdef FEAT_TEXTOBJ
169static void nv_object __ARGS((cmdarg_T *cap));
170#endif
171static void nv_record __ARGS((cmdarg_T *cap));
172static void nv_at __ARGS((cmdarg_T *cap));
173static void nv_halfpage __ARGS((cmdarg_T *cap));
174static void nv_join __ARGS((cmdarg_T *cap));
175static void nv_put __ARGS((cmdarg_T *cap));
176static void nv_open __ARGS((cmdarg_T *cap));
177#ifdef FEAT_SNIFF
178static void nv_sniff __ARGS((cmdarg_T *cap));
179#endif
180#ifdef FEAT_NETBEANS_INTG
181static void nv_nbcmd __ARGS((cmdarg_T *cap));
182#endif
183#ifdef FEAT_DND
184static void nv_drop __ARGS((cmdarg_T *cap));
185#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000186#ifdef FEAT_AUTOCMD
187static void nv_cursorhold __ARGS((cmdarg_T *cap));
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000190static char *e_noident = N_("E349: No identifier under cursor");
191
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192/*
193 * Function to be called for a Normal or Visual mode command.
194 * The argument is a cmdarg_T.
195 */
196typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
197
198/* Values for cmd_flags. */
199#define NV_NCH 0x01 /* may need to get a second char */
200#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
201#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
202#define NV_LANG 0x08 /* second char needs language adjustment */
203
204#define NV_SS 0x10 /* may start selection */
205#define NV_SSS 0x20 /* may start selection with shift modifier */
206#define NV_STS 0x40 /* may stop selection without shift modif. */
207#define NV_RL 0x80 /* 'rightleft' modifies command */
208#define NV_KEEPREG 0x100 /* don't clear regname */
209#define NV_NCW 0x200 /* not allowed in command-line window */
210
211/*
212 * Generally speaking, every Normal mode command should either clear any
213 * pending operator (with *clearop*()), or set the motion type variable
214 * oap->motion_type.
215 *
216 * When a cursor motion command is made, it is marked as being a character or
217 * line oriented motion. Then, if an operator is in effect, the operation
218 * becomes character or line oriented accordingly.
219 */
220
221/*
222 * This table contains one entry for every Normal or Visual mode command.
223 * The order doesn't matter, init_normal_cmds() will create a sorted index.
224 * It is faster when all keys from zero to '~' are present.
225 */
226static const struct nv_cmd
227{
228 int cmd_char; /* (first) command character */
229 nv_func_T cmd_func; /* function for this command */
230 short_u cmd_flags; /* NV_ flags */
231 short cmd_arg; /* value for ca.arg */
232} nv_cmds[] =
233{
234 {NUL, nv_error, 0, 0},
235 {Ctrl_A, nv_addsub, 0, 0},
236 {Ctrl_B, nv_page, NV_STS, BACKWARD},
237 {Ctrl_C, nv_esc, 0, TRUE},
238 {Ctrl_D, nv_halfpage, 0, 0},
239 {Ctrl_E, nv_scroll_line, 0, TRUE},
240 {Ctrl_F, nv_page, NV_STS, FORWARD},
241 {Ctrl_G, nv_ctrlg, 0, 0},
242 {Ctrl_H, nv_ctrlh, 0, 0},
243 {Ctrl_I, nv_pcmark, 0, 0},
244 {NL, nv_down, 0, FALSE},
245 {Ctrl_K, nv_error, 0, 0},
246 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000247 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {Ctrl_N, nv_down, NV_STS, FALSE},
249 {Ctrl_O, nv_ctrlo, 0, 0},
250 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000251#ifdef FEAT_VISUAL
252 {Ctrl_Q, nv_visual, 0, FALSE},
253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {Ctrl_R, nv_redo, 0, 0},
257 {Ctrl_S, nv_ignore, 0, 0},
258 {Ctrl_T, nv_tagpop, NV_NCW, 0},
259 {Ctrl_U, nv_halfpage, 0, 0},
260#ifdef FEAT_VISUAL
261 {Ctrl_V, nv_visual, 0, FALSE},
262 {'V', nv_visual, 0, FALSE},
263 {'v', nv_visual, 0, FALSE},
264#else
265 {Ctrl_V, nv_error, 0, 0},
266 {'V', nv_error, 0, 0},
267 {'v', nv_error, 0, 0},
268#endif
269 {Ctrl_W, nv_window, 0, 0},
270 {Ctrl_X, nv_addsub, 0, 0},
271 {Ctrl_Y, nv_scroll_line, 0, FALSE},
272 {Ctrl_Z, nv_suspend, 0, 0},
273 {ESC, nv_esc, 0, FALSE},
274 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
275 {Ctrl_RSB, nv_ident, NV_NCW, 0},
276 {Ctrl_HAT, nv_hat, NV_NCW, 0},
277 {Ctrl__, nv_error, 0, 0},
278 {' ', nv_right, 0, 0},
279 {'!', nv_operator, 0, 0},
280 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
281 {'#', nv_ident, 0, 0},
282 {'$', nv_dollar, 0, 0},
283 {'%', nv_percent, 0, 0},
284 {'&', nv_optrans, 0, 0},
285 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
286 {'(', nv_brace, 0, BACKWARD},
287 {')', nv_brace, 0, FORWARD},
288 {'*', nv_ident, 0, 0},
289 {'+', nv_down, 0, TRUE},
290 {',', nv_csearch, 0, TRUE},
291 {'-', nv_up, 0, TRUE},
292 {'.', nv_dot, NV_KEEPREG, 0},
293 {'/', nv_search, 0, FALSE},
294 {'0', nv_beginline, 0, 0},
295 {'1', nv_ignore, 0, 0},
296 {'2', nv_ignore, 0, 0},
297 {'3', nv_ignore, 0, 0},
298 {'4', nv_ignore, 0, 0},
299 {'5', nv_ignore, 0, 0},
300 {'6', nv_ignore, 0, 0},
301 {'7', nv_ignore, 0, 0},
302 {'8', nv_ignore, 0, 0},
303 {'9', nv_ignore, 0, 0},
304 {':', nv_colon, 0, 0},
305 {';', nv_csearch, 0, FALSE},
306 {'<', nv_operator, NV_RL, 0},
307 {'=', nv_operator, 0, 0},
308 {'>', nv_operator, NV_RL, 0},
309 {'?', nv_search, 0, FALSE},
310 {'@', nv_at, NV_NCH_NOP, FALSE},
311 {'A', nv_edit, 0, 0},
312 {'B', nv_bck_word, 0, 1},
313 {'C', nv_abbrev, NV_KEEPREG, 0},
314 {'D', nv_abbrev, NV_KEEPREG, 0},
315 {'E', nv_wordcmd, 0, TRUE},
316 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
317 {'G', nv_goto, 0, TRUE},
318 {'H', nv_scroll, 0, 0},
319 {'I', nv_edit, 0, 0},
320 {'J', nv_join, 0, 0},
321 {'K', nv_ident, 0, 0},
322 {'L', nv_scroll, 0, 0},
323 {'M', nv_scroll, 0, 0},
324 {'N', nv_next, 0, SEARCH_REV},
325 {'O', nv_open, 0, 0},
326 {'P', nv_put, 0, 0},
327 {'Q', nv_exmode, NV_NCW, 0},
328 {'R', nv_Replace, 0, FALSE},
329 {'S', nv_subst, NV_KEEPREG, 0},
330 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
331 {'U', nv_Undo, 0, 0},
332 {'W', nv_wordcmd, 0, TRUE},
333 {'X', nv_abbrev, NV_KEEPREG, 0},
334 {'Y', nv_abbrev, NV_KEEPREG, 0},
335 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
336 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
337 {'\\', nv_error, 0, 0},
338 {']', nv_brackets, NV_NCH_ALW, FORWARD},
339 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
340 {'_', nv_lineop, 0, 0},
341 {'`', nv_gomark, NV_NCH_ALW, FALSE},
342 {'a', nv_edit, NV_NCH, 0},
343 {'b', nv_bck_word, 0, 0},
344 {'c', nv_operator, 0, 0},
345 {'d', nv_operator, 0, 0},
346 {'e', nv_wordcmd, 0, FALSE},
347 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
348 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
349 {'h', nv_left, NV_RL, 0},
350 {'i', nv_edit, NV_NCH, 0},
351 {'j', nv_down, 0, FALSE},
352 {'k', nv_up, 0, FALSE},
353 {'l', nv_right, NV_RL, 0},
354 {'m', nv_mark, NV_NCH_NOP, 0},
355 {'n', nv_next, 0, 0},
356 {'o', nv_open, 0, 0},
357 {'p', nv_put, 0, 0},
358 {'q', nv_record, NV_NCH, 0},
359 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
360 {'s', nv_subst, NV_KEEPREG, 0},
361 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
362 {'u', nv_undo, 0, 0},
363 {'w', nv_wordcmd, 0, FALSE},
364 {'x', nv_abbrev, NV_KEEPREG, 0},
365 {'y', nv_operator, 0, 0},
366 {'z', nv_zet, NV_NCH_ALW, 0},
367 {'{', nv_findpar, 0, BACKWARD},
368 {'|', nv_pipe, 0, 0},
369 {'}', nv_findpar, 0, FORWARD},
370 {'~', nv_tilde, 0, 0},
371
372 /* pound sign */
373 {POUND, nv_ident, 0, 0},
374#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200375 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
376 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
377 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
378 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_LEFTMOUSE, nv_mouse, 0, 0},
380 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
381 {K_LEFTDRAG, nv_mouse, 0, 0},
382 {K_LEFTRELEASE, nv_mouse, 0, 0},
383 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
384 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
385 {K_MIDDLEDRAG, nv_mouse, 0, 0},
386 {K_MIDDLERELEASE, nv_mouse, 0, 0},
387 {K_RIGHTMOUSE, nv_mouse, 0, 0},
388 {K_RIGHTDRAG, nv_mouse, 0, 0},
389 {K_RIGHTRELEASE, nv_mouse, 0, 0},
390 {K_X1MOUSE, nv_mouse, 0, 0},
391 {K_X1DRAG, nv_mouse, 0, 0},
392 {K_X1RELEASE, nv_mouse, 0, 0},
393 {K_X2MOUSE, nv_mouse, 0, 0},
394 {K_X2DRAG, nv_mouse, 0, 0},
395 {K_X2RELEASE, nv_mouse, 0, 0},
396#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000398 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {K_INS, nv_edit, 0, 0},
400 {K_KINS, nv_edit, 0, 0},
401 {K_BS, nv_ctrlh, 0, 0},
402 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
403 {K_S_UP, nv_page, NV_SS, BACKWARD},
404 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
405 {K_S_DOWN, nv_page, NV_SS, FORWARD},
406 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
407 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
408 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
409 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
410 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
411 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
412 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
413 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
414 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
415 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
416 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
417 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {K_S_END, nv_end, NV_SS, FALSE},
419 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
420 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
421 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {K_S_HOME, nv_home, NV_SS, 0},
423 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
424 {K_DEL, nv_abbrev, 0, 0},
425 {K_KDEL, nv_abbrev, 0, 0},
426 {K_UNDO, nv_kundo, 0, 0},
427 {K_HELP, nv_help, NV_NCW, 0},
428 {K_F1, nv_help, NV_NCW, 0},
429 {K_XF1, nv_help, NV_NCW, 0},
430#ifdef FEAT_VISUAL
431 {K_SELECT, nv_select, 0, 0},
432#endif
433#ifdef FEAT_GUI
434 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
435 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
436#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000437#ifdef FEAT_GUI_TABLINE
438 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000439 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef FEAT_FKMAP
442 {K_F8, farsi_fkey, 0, 0},
443 {K_F9, farsi_fkey, 0, 0},
444#endif
445#ifdef FEAT_SNIFF
446 {K_SNIFF, nv_sniff, 0, 0},
447#endif
448#ifdef FEAT_NETBEANS_INTG
449 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
450#endif
451#ifdef FEAT_DND
452 {K_DROP, nv_drop, NV_STS, 0},
453#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000454#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000455 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457};
458
459/* Number of commands in nv_cmds[]. */
460#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
461
462/* Sorted index of commands in nv_cmds[]. */
463static short nv_cmd_idx[NV_CMDS_SIZE];
464
465/* The highest index for which
466 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
467static int nv_max_linear;
468
469/*
470 * Compare functions for qsort() below, that checks the command character
471 * through the index in nv_cmd_idx[].
472 */
473 static int
474#ifdef __BORLANDC__
475_RTLENTRYF
476#endif
477nv_compare(s1, s2)
478 const void *s1;
479 const void *s2;
480{
481 int c1, c2;
482
483 /* The commands are sorted on absolute value. */
484 c1 = nv_cmds[*(const short *)s1].cmd_char;
485 c2 = nv_cmds[*(const short *)s2].cmd_char;
486 if (c1 < 0)
487 c1 = -c1;
488 if (c2 < 0)
489 c2 = -c2;
490 return c1 - c2;
491}
492
493/*
494 * Initialize the nv_cmd_idx[] table.
495 */
496 void
497init_normal_cmds()
498{
499 int i;
500
501 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000502 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 nv_cmd_idx[i] = i;
504
505 /* Sort the commands by the command character. */
506 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
507
508 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000509 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
511 break;
512 nv_max_linear = i - 1;
513}
514
515/*
516 * Search for a command in the commands table.
517 * Returns -1 for invalid command.
518 */
519 static int
520find_command(cmdchar)
521 int cmdchar;
522{
523 int i;
524 int idx;
525 int top, bot;
526 int c;
527
528#ifdef FEAT_MBYTE
529 /* A multi-byte character is never a command. */
530 if (cmdchar >= 0x100)
531 return -1;
532#endif
533
534 /* We use the absolute value of the character. Special keys have a
535 * negative value, but are sorted on their absolute value. */
536 if (cmdchar < 0)
537 cmdchar = -cmdchar;
538
539 /* If the character is in the first part: The character is the index into
540 * nv_cmd_idx[]. */
541 if (cmdchar <= nv_max_linear)
542 return nv_cmd_idx[cmdchar];
543
544 /* Perform a binary search. */
545 bot = nv_max_linear + 1;
546 top = NV_CMDS_SIZE - 1;
547 idx = -1;
548 while (bot <= top)
549 {
550 i = (top + bot) / 2;
551 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
552 if (c < 0)
553 c = -c;
554 if (cmdchar == c)
555 {
556 idx = nv_cmd_idx[i];
557 break;
558 }
559 if (cmdchar > c)
560 bot = i + 1;
561 else
562 top = i - 1;
563 }
564 return idx;
565}
566
567/*
568 * Execute a command in Normal mode.
569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 void
571normal_cmd(oap, toplevel)
572 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000573 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 cmdarg_T ca; /* command arguments */
576 int c;
577 int ctrl_w = FALSE; /* got CTRL-W command */
578 int old_col = curwin->w_curswant;
579#ifdef FEAT_CMDL_INFO
580 int need_flushbuf; /* need to call out_flush() */
581#endif
582#ifdef FEAT_VISUAL
583 pos_T old_pos; /* cursor position before command */
584 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000588#ifdef FEAT_EVAL
589 int set_prevcount = FALSE;
590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
593 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000594
595 /* Use a count remembered from before entering an operator. After typing
596 * "3d" we return from normal_cmd() and come back here, the "3" is
597 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 ca.opcount = opcount;
599
600#ifdef FEAT_SNIFF
601 want_sniff_request = sniff_connected;
602#endif
603
604 /*
605 * If there is an operator pending, then the command we take this time
606 * will terminate it. Finish_op tells us to finish the operation before
607 * returning this time (unless the operation was cancelled).
608 */
609#ifdef CURSOR_SHAPE
610 c = finish_op;
611#endif
612 finish_op = (oap->op_type != OP_NOP);
613#ifdef CURSOR_SHAPE
614 if (finish_op != c)
615 {
616 ui_cursor_shape(); /* may show different cursor shape */
617# ifdef FEAT_MOUSESHAPE
618 update_mouseshape(-1);
619# endif
620 }
621#endif
622
Bram Moolenaara983fe92008-07-31 20:04:27 +0000623 /* When not finishing an operator and no register name typed, reset the
624 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000628#ifdef FEAT_EVAL
629 set_prevcount = TRUE;
630#endif
631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaara983fe92008-07-31 20:04:27 +0000633#ifdef FEAT_AUTOCMD
634 /* Restore counts from before receiving K_CURSORHOLD. This means after
635 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
636 * "3 * 2". */
637 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
638 {
639 ca.opcount = oap->prev_opcount;
640 ca.count0 = oap->prev_count0;
641 oap->prev_opcount = 0;
642 oap->prev_count0 = 0;
643 }
644#endif
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#ifdef FEAT_VISUAL
647 mapped_len = typebuf_maplen();
648#endif
649
650 State = NORMAL_BUSY;
651#ifdef USE_ON_FLY_SCROLL
652 dont_scroll = FALSE; /* allow scrolling here */
653#endif
654
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100655#ifdef FEAT_EVAL
656 /* Set v:count here, when called from main() and not a stuffed
657 * command, so that v:count can be used in an expression mapping
658 * when there is no count. */
659 if (toplevel && stuff_empty())
660 set_vcount_ca(&ca, &set_prevcount);
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Get the command character from the user.
665 */
666 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000669#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 /*
671 * If a mapping was started in Visual or Select mode, remember the length
672 * of the mapping. This is used below to not return to Insert mode for as
673 * long as the mapping is being executed.
674 */
675 if (restart_edit == 0)
676 old_mapped_len = 0;
677 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000678 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 if (c == NUL)
683 c = K_ZERO;
684
685#ifdef FEAT_VISUAL
686 /*
687 * In Select mode, typed text replaces the selection.
688 */
689 if (VIsual_active
690 && VIsual_select
691 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
692 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000693 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
694 * 'insertmode' is set) fake a "d"elete command, Insert mode will
695 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000696 * Insert the typed character in the typeahead buffer, so that it can
697 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000698 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000699 if (restart_edit != 0)
700 c = 'd';
701 else
702 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000703 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200704 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706#endif
707
708#ifdef FEAT_CMDL_INFO
709 need_flushbuf = add_to_showcmd(c);
710#endif
711
712getcount:
713#ifdef FEAT_VISUAL
714 if (!(VIsual_active && VIsual_select))
715#endif
716 {
717 /*
718 * Handle a count before a command and compute ca.count0.
719 * Note that '0' is a command and not the start of a count, but it's
720 * part of a count after other digits.
721 */
722 while ( (c >= '1' && c <= '9')
723 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
724 {
725 if (c == K_DEL || c == K_KDEL)
726 {
727 ca.count0 /= 10;
728#ifdef FEAT_CMDL_INFO
729 del_from_showcmd(4); /* delete the digit and ~@% */
730#endif
731 }
732 else
733 ca.count0 = ca.count0 * 10 + (c - '0');
734 if (ca.count0 < 0) /* got too large! */
735 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000736#ifdef FEAT_EVAL
737 /* Set v:count here, when called from main() and not a stuffed
738 * command, so that v:count can be used in an expression mapping
739 * right after the count. */
740 if (toplevel && stuff_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100741 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 if (ctrl_w)
744 {
745 ++no_mapping;
746 ++allow_keys; /* no mapping for nchar, but keys */
747 }
748 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000749 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 --no_zero_mapping;
752 if (ctrl_w)
753 {
754 --no_mapping;
755 --allow_keys;
756 }
757#ifdef FEAT_CMDL_INFO
758 need_flushbuf |= add_to_showcmd(c);
759#endif
760 }
761
762 /*
763 * If we got CTRL-W there may be a/another count
764 */
765 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
766 {
767 ctrl_w = TRUE;
768 ca.opcount = ca.count0; /* remember first count */
769 ca.count0 = 0;
770 ++no_mapping;
771 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000772 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 --no_mapping;
775 --allow_keys;
776#ifdef FEAT_CMDL_INFO
777 need_flushbuf |= add_to_showcmd(c);
778#endif
779 goto getcount; /* jump back */
780 }
781 }
782
Bram Moolenaara983fe92008-07-31 20:04:27 +0000783#ifdef FEAT_AUTOCMD
784 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000786 /* Save the count values so that ca.opcount and ca.count0 are exactly
787 * the same when coming back here after handling K_CURSORHOLD. */
788 oap->prev_opcount = ca.opcount;
789 oap->prev_count0 = ca.count0;
790 }
791 else
792#endif
793 if (ca.opcount != 0)
794 {
795 /*
796 * If we're in the middle of an operator (including after entering a
797 * yank buffer with '"') AND we had a count before the operator, then
798 * that count overrides the current value of ca.count0.
799 * What this means effectively, is that commands like "3dw" get turned
800 * into "d3w" which makes things fall into place pretty neatly.
801 * If you give a count before AND after the operator, they are
802 * multiplied.
803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (ca.count0)
805 ca.count0 *= ca.opcount;
806 else
807 ca.count0 = ca.opcount;
808 }
809
810 /*
811 * Always remember the count. It will be set to zero (on the next call,
812 * above) when there is no pending operator.
813 * When called from main(), save the count for use by the "count" built-in
814 * variable.
815 */
816 ca.opcount = ca.count0;
817 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
818
819#ifdef FEAT_EVAL
820 /*
821 * Only set v:count when called from main() and not a stuffed command.
822 */
823 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000824 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif
826
827 /*
828 * Find the command character in the table of commands.
829 * For CTRL-W we already got nchar when looking for a count.
830 */
831 if (ctrl_w)
832 {
833 ca.nchar = c;
834 ca.cmdchar = Ctrl_W;
835 }
836 else
837 ca.cmdchar = c;
838 idx = find_command(ca.cmdchar);
839 if (idx < 0)
840 {
841 /* Not a known command: beep. */
842 clearopbeep(oap);
843 goto normal_end;
844 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000845
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000846 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000848 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000850 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 goto normal_end;
852 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000853#ifdef FEAT_AUTOCMD
854 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
855 goto normal_end;
856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858#ifdef FEAT_VISUAL
859 /*
860 * In Visual/Select mode, a few keys are handled in a special way.
861 */
862 if (VIsual_active)
863 {
864 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
865 if (km_stopsel
866 && (nv_cmds[idx].cmd_flags & NV_STS)
867 && !(mod_mask & MOD_MASK_SHIFT))
868 {
869 end_visual_mode();
870 redraw_curbuf_later(INVERTED);
871 }
872
873 /* Keys that work different when 'keymodel' contains "startsel" */
874 if (km_startsel)
875 {
876 if (nv_cmds[idx].cmd_flags & NV_SS)
877 {
878 unshift_special(&ca);
879 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000880 if (idx < 0)
881 {
882 /* Just in case */
883 clearopbeep(oap);
884 goto normal_end;
885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
888 && (mod_mask & MOD_MASK_SHIFT))
889 {
890 mod_mask &= ~MOD_MASK_SHIFT;
891 }
892 }
893 }
894#endif
895
896#ifdef FEAT_RIGHTLEFT
897 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
898 && (nv_cmds[idx].cmd_flags & NV_RL))
899 {
900 /* Invert horizontal movements and operations. Only when typed by the
901 * user directly, not when the result of a mapping or "x" translated
902 * to "dl". */
903 switch (ca.cmdchar)
904 {
905 case 'l': ca.cmdchar = 'h'; break;
906 case K_RIGHT: ca.cmdchar = K_LEFT; break;
907 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
908 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
909 case 'h': ca.cmdchar = 'l'; break;
910 case K_LEFT: ca.cmdchar = K_RIGHT; break;
911 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
912 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
913 case '>': ca.cmdchar = '<'; break;
914 case '<': ca.cmdchar = '>'; break;
915 }
916 idx = find_command(ca.cmdchar);
917 }
918#endif
919
920 /*
921 * Get an additional character if we need one.
922 */
923 if ((nv_cmds[idx].cmd_flags & NV_NCH)
924 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
925 && oap->op_type == OP_NOP)
926 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
927 || (ca.cmdchar == 'q'
928 && oap->op_type == OP_NOP
929 && !Recording
930 && !Exec_reg)
931 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
932 && (oap->op_type != OP_NOP
933#ifdef FEAT_VISUAL
934 || VIsual_active
935#endif
936 ))))
937 {
938 int *cp;
939 int repl = FALSE; /* get character for replace mode */
940 int lit = FALSE; /* get extra character literally */
941 int langmap_active = FALSE; /* using :lmap mappings */
942 int lang; /* getting a text character */
943#ifdef USE_IM_CONTROL
944 int save_smd; /* saved value of p_smd */
945#endif
946
947 ++no_mapping;
948 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000949#ifdef FEAT_AUTOCMD
950 /* Don't generate a CursorHold event here, most commands can't handle
951 * it, e.g., nv_replace(), nv_csearch(). */
952 did_cursorhold = TRUE;
953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (ca.cmdchar == 'g')
955 {
956 /*
957 * For 'g' get the next character now, so that we can check for
958 * "gr", "g'" and "g`".
959 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000960 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962#ifdef FEAT_CMDL_INFO
963 need_flushbuf |= add_to_showcmd(ca.nchar);
964#endif
965 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100966 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 cp = &ca.extra_char; /* need to get a third character */
969 if (ca.nchar != 'r')
970 lit = TRUE; /* get it literally */
971 else
972 repl = TRUE; /* get it in replace mode */
973 }
974 else
975 cp = NULL; /* no third character needed */
976 }
977 else
978 {
979 if (ca.cmdchar == 'r') /* get it in replace mode */
980 repl = TRUE;
981 cp = &ca.nchar;
982 }
983 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
984
985 /*
986 * Get a second or third character.
987 */
988 if (cp != NULL)
989 {
990#ifdef CURSOR_SHAPE
991 if (repl)
992 {
993 State = REPLACE; /* pretend Replace mode */
994 ui_cursor_shape(); /* show different cursor shape */
995 }
996#endif
997 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
998 {
999 /* Allow mappings defined with ":lmap". */
1000 --no_mapping;
1001 --allow_keys;
1002 if (repl)
1003 State = LREPLACE;
1004 else
1005 State = LANGMAP;
1006 langmap_active = TRUE;
1007 }
1008#ifdef USE_IM_CONTROL
1009 save_smd = p_smd;
1010 p_smd = FALSE; /* Don't let the IM code show the mode here */
1011 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1012 im_set_active(TRUE);
1013#endif
1014
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001015 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 if (langmap_active)
1018 {
1019 /* Undo the decrement done above */
1020 ++no_mapping;
1021 ++allow_keys;
1022 State = NORMAL_BUSY;
1023 }
1024#ifdef USE_IM_CONTROL
1025 if (lang)
1026 {
1027 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1028 im_save_status(&curbuf->b_p_iminsert);
1029 im_set_active(FALSE);
1030 }
1031 p_smd = save_smd;
1032#endif
1033#ifdef CURSOR_SHAPE
1034 State = NORMAL_BUSY;
1035#endif
1036#ifdef FEAT_CMDL_INFO
1037 need_flushbuf |= add_to_showcmd(*cp);
1038#endif
1039
1040 if (!lit)
1041 {
1042#ifdef FEAT_DIGRAPHS
1043 /* Typing CTRL-K gets a digraph. */
1044 if (*cp == Ctrl_K
1045 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1046 || cp == &ca.extra_char)
1047 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1048 {
1049 c = get_digraph(FALSE);
1050 if (c > 0)
1051 {
1052 *cp = c;
1053# ifdef FEAT_CMDL_INFO
1054 /* Guessing how to update showcmd here... */
1055 del_from_showcmd(3);
1056 need_flushbuf |= add_to_showcmd(*cp);
1057# endif
1058 }
1059 }
1060#endif
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 /* adjust chars > 127, except after "tTfFr" commands */
1063 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#ifdef FEAT_RIGHTLEFT
1065 /* adjust Hebrew mapped char */
1066 if (p_hkmap && lang && KeyTyped)
1067 *cp = hkmap(*cp);
1068# ifdef FEAT_FKMAP
1069 /* adjust Farsi mapped char */
1070 if (p_fkmap && lang && KeyTyped)
1071 *cp = fkmap(*cp);
1072# endif
1073#endif
1074 }
1075
1076 /*
1077 * When the next character is CTRL-\ a following CTRL-N means the
1078 * command is aborted and we go to Normal mode.
1079 */
1080 if (cp == &ca.extra_char
1081 && ca.nchar == Ctrl_BSL
1082 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1083 {
1084 ca.cmdchar = Ctrl_BSL;
1085 ca.nchar = ca.extra_char;
1086 idx = find_command(ca.cmdchar);
1087 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001088 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001089 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 else if (*cp == Ctrl_BSL)
1091 {
1092 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1093
1094 /* There is a busy wait here when typing "f<C-\>" and then
1095 * something different from CTRL-N. Can't be avoided. */
1096 while ((c = vpeekc()) <= 0 && towait > 0L)
1097 {
1098 do_sleep(towait > 50L ? 50L : towait);
1099 towait -= 50L;
1100 }
1101 if (c > 0)
1102 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001103 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (c != Ctrl_N && c != Ctrl_G)
1105 vungetc(c);
1106 else
1107 {
1108 ca.cmdchar = Ctrl_BSL;
1109 ca.nchar = c;
1110 idx = find_command(ca.cmdchar);
1111 }
1112 }
1113 }
1114
1115#ifdef FEAT_MBYTE
1116 /* When getting a text character and the next character is a
1117 * multi-byte character, it could be a composing character.
1118 * However, don't wait for it to arrive. */
1119 while (enc_utf8 && lang && (c = vpeekc()) > 0
1120 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1121 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001122 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (!utf_iscomposing(c))
1124 {
1125 vungetc(c); /* it wasn't, put it back */
1126 break;
1127 }
1128 else if (ca.ncharC1 == 0)
1129 ca.ncharC1 = c;
1130 else
1131 ca.ncharC2 = c;
1132 }
1133#endif
1134 }
1135 --no_mapping;
1136 --allow_keys;
1137 }
1138
1139#ifdef FEAT_CMDL_INFO
1140 /*
1141 * Flush the showcmd characters onto the screen so we can see them while
1142 * the command is being executed. Only do this when the shown command was
1143 * actually displayed, otherwise this will slow down a lot when executing
1144 * mappings.
1145 */
1146 if (need_flushbuf)
1147 out_flush();
1148#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001149#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001150 if (ca.cmdchar != K_IGNORE)
1151 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154 State = NORMAL;
1155
1156 if (ca.nchar == ESC)
1157 {
1158 clearop(oap);
1159 if (restart_edit == 0 && goto_im())
1160 restart_edit = 'a';
1161 goto normal_end;
1162 }
1163
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001164 if (ca.cmdchar != K_IGNORE)
1165 {
1166 msg_didout = FALSE; /* don't scroll screen up for normal command */
1167 msg_col = 0;
1168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170#ifdef FEAT_VISUAL
1171 old_pos = curwin->w_cursor; /* remember where cursor was */
1172
1173 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1174 * mode. */
1175 if (!VIsual_active && km_startsel)
1176 {
1177 if (nv_cmds[idx].cmd_flags & NV_SS)
1178 {
1179 start_selection();
1180 unshift_special(&ca);
1181 idx = find_command(ca.cmdchar);
1182 }
1183 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1184 && (mod_mask & MOD_MASK_SHIFT))
1185 {
1186 start_selection();
1187 mod_mask &= ~MOD_MASK_SHIFT;
1188 }
1189 }
1190#endif
1191
1192 /*
1193 * Execute the command!
1194 * Call the command function found in the commands table.
1195 */
1196 ca.arg = nv_cmds[idx].cmd_arg;
1197 (nv_cmds[idx].cmd_func)(&ca);
1198
1199 /*
1200 * If we didn't start or finish an operator, reset oap->regname, unless we
1201 * need it later.
1202 */
1203 if (!finish_op
1204 && !oap->op_type
1205 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1206 {
1207 clearop(oap);
1208#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001209 {
1210 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001211
Bram Moolenaar536681b2011-05-10 16:12:45 +02001212 /* Adjust the register according to 'clipboard', so that when
1213 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001214# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001215 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001216# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001217 set_reg_var(regname);
1218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
1220 }
1221
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001222#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001223 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001224 * character or "z333<cr>". */
1225 if (old_mapped_len > 0)
1226 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001227#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 /*
1230 * If an operation is pending, handle it...
1231 */
1232 do_pending_operator(&ca, old_col, FALSE);
1233
1234 /*
1235 * Wait for a moment when a message is displayed that will be overwritten
1236 * by the mode message.
1237 * In Visual mode and with "^O" in Insert mode, a short message will be
1238 * overwritten by the mode message. Wait a bit, until a key is hit.
1239 * In Visual mode, it's more important to keep the Visual area updated
1240 * than keeping a message (e.g. from a /pat search).
1241 * Only do this if the command was typed, not from a mapping.
1242 * Don't wait when emsg_silent is non-zero.
1243 * Also wait a bit after an error message, e.g. for "^O:".
1244 * Don't redraw the screen, it would remove the message.
1245 */
1246 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001247 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && (restart_edit != 0
1249#ifdef FEAT_VISUAL
1250 || (VIsual_active
1251 && old_pos.lnum == curwin->w_cursor.lnum
1252 && old_pos.col == curwin->w_cursor.col)
1253#endif
1254 )
1255 && (clear_cmdline
1256 || redraw_cmdline)
1257 && (msg_didout || (msg_didany && msg_scroll))
1258 && !msg_nowait
1259 && KeyTyped)
1260 || (restart_edit != 0
1261#ifdef FEAT_VISUAL
1262 && !VIsual_active
1263#endif
1264 && (msg_scroll
1265 || emsg_on_display)))
1266 && oap->regname == 0
1267 && !(ca.retval & CA_COMMAND_BUSY)
1268 && stuff_empty()
1269 && typebuf_typed()
1270 && emsg_silent == 0
1271 && !did_wait_return
1272 && oap->op_type == OP_NOP)
1273 {
1274 int save_State = State;
1275
1276 /* Draw the cursor with the right shape here */
1277 if (restart_edit != 0)
1278 State = INSERT;
1279
1280 /* If need to redraw, and there is a "keep_msg", redraw before the
1281 * delay */
1282 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1283 {
1284 char_u *kmsg;
1285
1286 kmsg = keep_msg;
1287 keep_msg = NULL;
1288 /* showmode() will clear keep_msg, but we want to use it anyway */
1289 update_screen(0);
1290 /* now reset it, otherwise it's put in the history again */
1291 keep_msg = kmsg;
1292 msg_attr(kmsg, keep_msg_attr);
1293 vim_free(kmsg);
1294 }
1295 setcursor();
1296 cursor_on();
1297 out_flush();
1298 if (msg_scroll || emsg_on_display)
1299 ui_delay(1000L, TRUE); /* wait at least one second */
1300 ui_delay(3000L, FALSE); /* wait up to three seconds */
1301 State = save_State;
1302
1303 msg_scroll = FALSE;
1304 emsg_on_display = FALSE;
1305 }
1306
1307 /*
1308 * Finish up after executing a Normal mode command.
1309 */
1310normal_end:
1311
1312 msg_nowait = FALSE;
1313
1314 /* Reset finish_op, in case it was set */
1315#ifdef CURSOR_SHAPE
1316 c = finish_op;
1317#endif
1318 finish_op = FALSE;
1319#ifdef CURSOR_SHAPE
1320 /* Redraw the cursor with another shape, if we were in Operator-pending
1321 * mode or did a replace command. */
1322 if (c || ca.cmdchar == 'r')
1323 {
1324 ui_cursor_shape(); /* may show different cursor shape */
1325# ifdef FEAT_MOUSESHAPE
1326 update_mouseshape(-1);
1327# endif
1328 }
1329#endif
1330
1331#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001332 if (oap->op_type == OP_NOP && oap->regname == 0
1333# ifdef FEAT_AUTOCMD
1334 && ca.cmdchar != K_CURSORHOLD
1335# endif
1336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 clear_showcmd();
1338#endif
1339
1340 checkpcmark(); /* check if we moved since setting pcmark */
1341 vim_free(ca.searchbuf);
1342
1343#ifdef FEAT_MBYTE
1344 if (has_mbyte)
1345 mb_adjust_cursor();
1346#endif
1347
1348#ifdef FEAT_SCROLLBIND
1349 if (curwin->w_p_scb && toplevel)
1350 {
1351 validate_cursor(); /* may need to update w_leftcol */
1352 do_check_scrollbind(TRUE);
1353 }
1354#endif
1355
Bram Moolenaar860cae12010-06-05 23:22:07 +02001356#ifdef FEAT_CURSORBIND
1357 if (curwin->w_p_crb && toplevel)
1358 {
1359 validate_cursor(); /* may need to update w_leftcol */
1360 do_check_cursorbind();
1361 }
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /*
1365 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1366 * if still inside a mapping that started in Visual mode).
1367 * May switch from Visual to Select mode after CTRL-O command.
1368 */
1369 if ( oap->op_type == OP_NOP
1370#ifdef FEAT_VISUAL
1371 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1372 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001373#else
1374 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#endif
1376 && !(ca.retval & CA_COMMAND_BUSY)
1377 && stuff_empty()
1378 && oap->regname == 0)
1379 {
1380#ifdef FEAT_VISUAL
1381 if (restart_VIsual_select == 1)
1382 {
1383 VIsual_select = TRUE;
1384 showmode();
1385 restart_VIsual_select = 0;
1386 }
1387#endif
1388 if (restart_edit != 0
1389#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001390 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001392 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 (void)edit(restart_edit, FALSE, 1L);
1394 }
1395
1396#ifdef FEAT_VISUAL
1397 if (restart_VIsual_select == 2)
1398 restart_VIsual_select = 1;
1399#endif
1400
1401 /* Save count before an operator for next time. */
1402 opcount = ca.opcount;
1403}
1404
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001405#ifdef FEAT_EVAL
1406/*
1407 * Set v:count and v:count1 according to "cap".
1408 * Set v:prevcount only when "set_prevcount" is TRUE.
1409 */
1410 static void
1411set_vcount_ca(cap, set_prevcount)
1412 cmdarg_T *cap;
1413 int *set_prevcount;
1414{
1415 long count = cap->count0;
1416
1417 /* multiply with cap->opcount the same way as above */
1418 if (cap->opcount != 0)
1419 count = cap->opcount * (count == 0 ? 1 : count);
1420 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1421 *set_prevcount = FALSE; /* only set v:prevcount once */
1422}
1423#endif
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425/*
1426 * Handle an operator after visual mode or when the movement is finished
1427 */
1428 void
1429do_pending_operator(cap, old_col, gui_yank)
1430 cmdarg_T *cap;
1431 int old_col;
1432 int gui_yank;
1433{
1434 oparg_T *oap = cap->oap;
1435 pos_T old_cursor;
1436 int empty_region_error;
1437 int restart_edit_save;
1438
1439#ifdef FEAT_VISUAL
1440 /* The visual area is remembered for redo */
1441 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1442 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001443 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 static long redo_VIsual_count; /* count for Visual operator */
1445# ifdef FEAT_VIRTUALEDIT
1446 int include_line_break = FALSE;
1447# endif
1448#endif
1449
1450#if defined(FEAT_CLIPBOARD)
1451 /*
1452 * Yank the visual area into the GUI selection register before we operate
1453 * on it and lose it forever.
1454 * Don't do it if a specific register was specified, so that ""x"*P works.
1455 * This could call do_pending_operator() recursively, but that's OK
1456 * because gui_yank will be TRUE for the nested call.
1457 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001458 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 && oap->op_type != OP_NOP
1460 && !gui_yank
1461# ifdef FEAT_VISUAL
1462 && VIsual_active
1463 && !redo_VIsual_busy
1464# endif
1465 && oap->regname == 0)
1466 clip_auto_select();
1467#endif
1468 old_cursor = curwin->w_cursor;
1469
1470 /*
1471 * If an operation is pending, handle it...
1472 */
1473 if ((finish_op
1474#ifdef FEAT_VISUAL
1475 || VIsual_active
1476#endif
1477 ) && oap->op_type != OP_NOP)
1478 {
1479#ifdef FEAT_VISUAL
1480 oap->is_VIsual = VIsual_active;
1481 if (oap->motion_force == 'V')
1482 oap->motion_type = MLINE;
1483 else if (oap->motion_force == 'v')
1484 {
1485 /* If the motion was linewise, "inclusive" will not have been set.
1486 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1487 if (oap->motion_type == MLINE)
1488 oap->inclusive = FALSE;
1489 /* If the motion already was characterwise, toggle "inclusive" */
1490 else if (oap->motion_type == MCHAR)
1491 oap->inclusive = !oap->inclusive;
1492 oap->motion_type = MCHAR;
1493 }
1494 else if (oap->motion_force == Ctrl_V)
1495 {
1496 /* Change line- or characterwise motion into Visual block mode. */
1497 VIsual_active = TRUE;
1498 VIsual = oap->start;
1499 VIsual_mode = Ctrl_V;
1500 VIsual_select = FALSE;
1501 VIsual_reselect = FALSE;
1502 }
1503#endif
1504
Bram Moolenaar7afea822013-04-24 18:34:45 +02001505 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1506 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1508#ifdef FEAT_VISUAL
Bram Moolenaar7afea822013-04-24 18:34:45 +02001509 && ((!VIsual_active || oap->motion_force)
1510 /* Also redo Operator-pending Visual mode mappings */
1511 || (VIsual_active && cap->cmdchar == ':'
1512 && oap->op_type != OP_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001514 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515#ifdef FEAT_FOLDING
1516 && oap->op_type != OP_FOLD
1517 && oap->op_type != OP_FOLDOPEN
1518 && oap->op_type != OP_FOLDOPENREC
1519 && oap->op_type != OP_FOLDCLOSE
1520 && oap->op_type != OP_FOLDCLOSEREC
1521 && oap->op_type != OP_FOLDDEL
1522 && oap->op_type != OP_FOLDDELREC
1523#endif
1524 )
1525 {
1526 prep_redo(oap->regname, cap->count0,
1527 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1528 oap->motion_force, cap->cmdchar, cap->nchar);
1529 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1530 {
1531 /*
1532 * If 'cpoptions' does not contain 'r', insert the search
1533 * pattern to really repeat the same command.
1534 */
1535 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001536 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 AppendToRedobuff(NL_STR);
1538 }
1539 else if (cap->cmdchar == ':')
1540 {
1541 /* do_cmdline() has stored the first typed line in
1542 * "repeat_cmdline". When several lines are typed repeating
1543 * won't be possible. */
1544 if (repeat_cmdline == NULL)
1545 ResetRedobuff();
1546 else
1547 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001548 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 AppendToRedobuff(NL_STR);
1550 vim_free(repeat_cmdline);
1551 repeat_cmdline = NULL;
1552 }
1553 }
1554 }
1555
1556#ifdef FEAT_VISUAL
1557 if (redo_VIsual_busy)
1558 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001559 /* Redo of an operation on a Visual area. Use the same size from
1560 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 oap->start = curwin->w_cursor;
1562 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1563 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1564 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1565 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001566 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001568 if (VIsual_mode == 'v')
1569 {
1570 if (redo_VIsual_line_count <= 1)
1571 {
1572 validate_virtcol();
1573 curwin->w_curswant =
1574 curwin->w_virtcol + redo_VIsual_vcol - 1;
1575 }
1576 else
1577 curwin->w_curswant = redo_VIsual_vcol;
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001580 {
1581 curwin->w_curswant = MAXCOL;
1582 }
1583 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 }
1585 cap->count0 = redo_VIsual_count;
1586 if (redo_VIsual_count != 0)
1587 cap->count1 = redo_VIsual_count;
1588 else
1589 cap->count1 = 1;
1590 }
1591 else if (VIsual_active)
1592 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001593 if (!gui_yank)
1594 {
1595 /* Save the current VIsual area for '< and '> marks, and "gv" */
1596 curbuf->b_visual.vi_start = VIsual;
1597 curbuf->b_visual.vi_end = curwin->w_cursor;
1598 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001599 if (VIsual_mode_orig != NUL)
1600 {
1601 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1602 VIsual_mode_orig = NUL;
1603 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001604 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001606 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 /* In Select mode, a linewise selection is operated upon like a
1611 * characterwise selection. */
1612 if (VIsual_select && VIsual_mode == 'V')
1613 {
1614 if (lt(VIsual, curwin->w_cursor))
1615 {
1616 VIsual.col = 0;
1617 curwin->w_cursor.col =
1618 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1619 }
1620 else
1621 {
1622 curwin->w_cursor.col = 0;
1623 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1624 }
1625 VIsual_mode = 'v';
1626 }
1627 /* If 'selection' is "exclusive", backup one character for
1628 * charwise selections. */
1629 else if (VIsual_mode == 'v')
1630 {
1631# ifdef FEAT_VIRTUALEDIT
1632 include_line_break =
1633# endif
1634 unadjust_for_sel();
1635 }
1636
1637 oap->start = VIsual;
1638 if (VIsual_mode == 'V')
1639 oap->start.col = 0;
1640 }
1641#endif /* FEAT_VISUAL */
1642
1643 /*
1644 * Set oap->start to the first position of the operated text, oap->end
1645 * to the end of the operated text. w_cursor is equal to oap->start.
1646 */
1647 if (lt(oap->start, curwin->w_cursor))
1648 {
1649#ifdef FEAT_FOLDING
1650 /* Include folded lines completely. */
1651 if (!VIsual_active)
1652 {
1653 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1654 oap->start.col = 0;
1655 if (hasFolding(curwin->w_cursor.lnum, NULL,
1656 &curwin->w_cursor.lnum))
1657 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1658 }
1659#endif
1660 oap->end = curwin->w_cursor;
1661 curwin->w_cursor = oap->start;
1662
1663 /* w_virtcol may have been updated; if the cursor goes back to its
1664 * previous position w_virtcol becomes invalid and isn't updated
1665 * automatically. */
1666 curwin->w_valid &= ~VALID_VIRTCOL;
1667 }
1668 else
1669 {
1670#ifdef FEAT_FOLDING
1671 /* Include folded lines completely. */
1672 if (!VIsual_active && oap->motion_type == MLINE)
1673 {
1674 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1675 NULL))
1676 curwin->w_cursor.col = 0;
1677 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1678 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1679 }
1680#endif
1681 oap->end = oap->start;
1682 oap->start = curwin->w_cursor;
1683 }
1684
1685 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1686
1687#ifdef FEAT_VIRTUALEDIT
1688 /* Set "virtual_op" before resetting VIsual_active. */
1689 virtual_op = virtual_active();
1690#endif
1691
1692#ifdef FEAT_VISUAL
1693 if (VIsual_active || redo_VIsual_busy)
1694 {
1695 if (VIsual_mode == Ctrl_V) /* block mode */
1696 {
1697 colnr_T start, end;
1698
1699 oap->block_mode = TRUE;
1700
1701 getvvcol(curwin, &(oap->start),
1702 &oap->start_vcol, NULL, &oap->end_vcol);
1703 if (!redo_VIsual_busy)
1704 {
1705 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1706
1707 if (start < oap->start_vcol)
1708 oap->start_vcol = start;
1709 if (end > oap->end_vcol)
1710 {
1711 if (*p_sel == 'e' && start >= 1
1712 && start - 1 >= oap->end_vcol)
1713 oap->end_vcol = start - 1;
1714 else
1715 oap->end_vcol = end;
1716 }
1717 }
1718
1719 /* if '$' was used, get oap->end_vcol from longest line */
1720 if (curwin->w_curswant == MAXCOL)
1721 {
1722 curwin->w_cursor.col = MAXCOL;
1723 oap->end_vcol = 0;
1724 for (curwin->w_cursor.lnum = oap->start.lnum;
1725 curwin->w_cursor.lnum <= oap->end.lnum;
1726 ++curwin->w_cursor.lnum)
1727 {
1728 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1729 if (end > oap->end_vcol)
1730 oap->end_vcol = end;
1731 }
1732 }
1733 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001734 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 /*
1736 * Correct oap->end.col and oap->start.col to be the
1737 * upper-left and lower-right corner of the block area.
1738 *
1739 * (Actually, this does convert column positions into character
1740 * positions)
1741 */
1742 curwin->w_cursor.lnum = oap->end.lnum;
1743 coladvance(oap->end_vcol);
1744 oap->end = curwin->w_cursor;
1745
1746 curwin->w_cursor = oap->start;
1747 coladvance(oap->start_vcol);
1748 oap->start = curwin->w_cursor;
1749 }
1750
1751 if (!redo_VIsual_busy && !gui_yank)
1752 {
1753 /*
1754 * Prepare to reselect and redo Visual: this is based on the
1755 * size of the Visual text
1756 */
1757 resel_VIsual_mode = VIsual_mode;
1758 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001759 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001761 {
1762 if (VIsual_mode != Ctrl_V)
1763 getvvcol(curwin, &(oap->end),
1764 NULL, NULL, &oap->end_vcol);
1765 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1766 {
1767 if (VIsual_mode != Ctrl_V)
1768 getvvcol(curwin, &(oap->start),
1769 &oap->start_vcol, NULL, NULL);
1770 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1771 }
1772 else
1773 resel_VIsual_vcol = oap->end_vcol;
1774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 resel_VIsual_line_count = oap->line_count;
1776 }
1777
1778 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1779 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1780 && oap->op_type != OP_COLON
1781#ifdef FEAT_FOLDING
1782 && oap->op_type != OP_FOLD
1783 && oap->op_type != OP_FOLDOPEN
1784 && oap->op_type != OP_FOLDOPENREC
1785 && oap->op_type != OP_FOLDCLOSE
1786 && oap->op_type != OP_FOLDCLOSEREC
1787 && oap->op_type != OP_FOLDDEL
1788 && oap->op_type != OP_FOLDDELREC
1789#endif
1790 && oap->motion_force == NUL
1791 )
1792 {
1793 /* Prepare for redoing. Only use the nchar field for "r",
1794 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001795 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1796 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001797 prep_redo(oap->regname, cap->count0,
1798 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1799 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001800 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001801 prep_redo(oap->regname, 0L, NUL, 'v',
1802 get_op_char(oap->op_type),
1803 get_extra_op_char(oap->op_type),
1804 oap->op_type == OP_REPLACE
1805 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (!redo_VIsual_busy)
1807 {
1808 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001809 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 redo_VIsual_line_count = resel_VIsual_line_count;
1811 redo_VIsual_count = cap->count0;
1812 }
1813 }
1814
1815 /*
1816 * oap->inclusive defaults to TRUE.
1817 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1818 * FALSE. This makes "d}P" and "v}dP" work the same.
1819 */
1820 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1821 oap->inclusive = TRUE;
1822 if (VIsual_mode == 'V')
1823 oap->motion_type = MLINE;
1824 else
1825 {
1826 oap->motion_type = MCHAR;
1827 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1828# ifdef FEAT_VIRTUALEDIT
1829 && (include_line_break || !virtual_op)
1830# endif
1831 )
1832 {
1833 oap->inclusive = FALSE;
1834 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001835 * that works on lines only. */
1836 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001838 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1839 {
1840 ++oap->end.lnum;
1841 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001843 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001845 ++oap->line_count;
1846 }
1847 else
1848 {
1849 /* Cannot move below the last line, make the op
1850 * inclusive to tell the operation to include the
1851 * line break. */
1852 oap->inclusive = TRUE;
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 }
1856 }
1857
1858 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 /*
1861 * Switch Visual off now, so screen updating does
1862 * not show inverted text when the screen is redrawn.
1863 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1864 * no screen redraw, so it is done here to remove the inverted
1865 * part.
1866 */
1867 if (!gui_yank)
1868 {
1869 VIsual_active = FALSE;
1870# ifdef FEAT_MOUSE
1871 setmouse();
1872 mouse_dragging = 0;
1873# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001874 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 clear_cmdline = TRUE; /* unshow visual mode later */
1876#ifdef FEAT_CMDL_INFO
1877 else
1878 clear_showcmd();
1879#endif
1880 if ((oap->op_type == OP_YANK
1881 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001882 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 || oap->op_type == OP_FILTER)
1884 && oap->motion_force == NUL)
1885 redraw_curbuf_later(INVERTED);
1886 }
1887 }
1888#endif
1889
1890#ifdef FEAT_MBYTE
1891 /* Include the trailing byte of a multi-byte char. */
1892 if (has_mbyte && oap->inclusive)
1893 {
1894 int l;
1895
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001896 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (l > 1)
1898 oap->end.col += l - 1;
1899 }
1900#endif
1901 curwin->w_set_curswant = TRUE;
1902
1903 /*
1904 * oap->empty is set when start and end are the same. The inclusive
1905 * flag affects this too, unless yanking and the end is on a NUL.
1906 */
1907 oap->empty = (oap->motion_type == MCHAR
1908 && (!oap->inclusive
1909 || (oap->op_type == OP_YANK
1910 && gchar_pos(&oap->end) == NUL))
1911 && equalpos(oap->start, oap->end)
1912#ifdef FEAT_VIRTUALEDIT
1913 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1914#endif
1915 );
1916 /*
1917 * For delete, change and yank, it's an error to operate on an
1918 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1919 */
1920 empty_region_error = (oap->empty
1921 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1922
1923#ifdef FEAT_VISUAL
1924 /* Force a redraw when operating on an empty Visual region, when
1925 * 'modifiable is off or creating a fold. */
1926 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1927# ifdef FEAT_FOLDING
1928 || oap->op_type == OP_FOLD
1929# endif
1930 ))
1931 redraw_curbuf_later(INVERTED);
1932#endif
1933
1934 /*
1935 * If the end of an operator is in column one while oap->motion_type
1936 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1937 * character in the previous line. If op_start is on or before the
1938 * first non-blank in the line, the operator becomes linewise
1939 * (strange, but that's the way vi does it).
1940 */
1941 if ( oap->motion_type == MCHAR
1942 && oap->inclusive == FALSE
1943 && !(cap->retval & CA_NO_ADJ_OP_END)
1944 && oap->end.col == 0
1945#ifdef FEAT_VISUAL
1946 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001947 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#endif
1949 && oap->line_count > 1)
1950 {
1951 oap->end_adjusted = TRUE; /* remember that we did this */
1952 --oap->line_count;
1953 --oap->end.lnum;
1954 if (inindent(0))
1955 oap->motion_type = MLINE;
1956 else
1957 {
1958 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1959 if (oap->end.col)
1960 {
1961 --oap->end.col;
1962 oap->inclusive = TRUE;
1963 }
1964 }
1965 }
1966 else
1967 oap->end_adjusted = FALSE;
1968
1969 switch (oap->op_type)
1970 {
1971 case OP_LSHIFT:
1972 case OP_RSHIFT:
1973 op_shift(oap, TRUE,
1974#ifdef FEAT_VISUAL
1975 oap->is_VIsual ? (int)cap->count1 :
1976#endif
1977 1);
1978 auto_format(FALSE, TRUE);
1979 break;
1980
1981 case OP_JOIN_NS:
1982 case OP_JOIN:
1983 if (oap->line_count < 2)
1984 oap->line_count = 2;
1985 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1986 curbuf->b_ml.ml_line_count)
1987 beep_flush();
1988 else
1989 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001990 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 auto_format(FALSE, TRUE);
1992 }
1993 break;
1994
1995 case OP_DELETE:
1996#ifdef FEAT_VISUAL
1997 VIsual_reselect = FALSE; /* don't reselect now */
1998#endif
1999 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002002 CancelRedo();
2003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 else
2005 {
2006 (void)op_delete(oap);
2007 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
2008 u_save_cursor(); /* cursor line wasn't saved yet */
2009 auto_format(FALSE, TRUE);
2010 }
2011 break;
2012
2013 case OP_YANK:
2014 if (empty_region_error)
2015 {
2016 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002019 CancelRedo();
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 else
2023 (void)op_yank(oap, FALSE, !gui_yank);
2024 check_cursor_col();
2025 break;
2026
2027 case OP_CHANGE:
2028#ifdef FEAT_VISUAL
2029 VIsual_reselect = FALSE; /* don't reselect now */
2030#endif
2031 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002034 CancelRedo();
2035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 else
2037 {
2038 /* This is a new edit command, not a restart. Need to
2039 * remember it to make 'insertmode' work with mappings for
2040 * Visual mode. But do this only once and not when typed and
2041 * 'insertmode' isn't set. */
2042 if (p_im || !KeyTyped)
2043 restart_edit_save = restart_edit;
2044 else
2045 restart_edit_save = 0;
2046 restart_edit = 0;
2047 /* Reset finish_op now, don't want it set inside edit(). */
2048 finish_op = FALSE;
2049 if (op_change(oap)) /* will call edit() */
2050 cap->retval |= CA_COMMAND_BUSY;
2051 if (restart_edit == 0)
2052 restart_edit = restart_edit_save;
2053 }
2054 break;
2055
2056 case OP_FILTER:
2057 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2058 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2059 else
2060 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2061
2062 case OP_INDENT:
2063 case OP_COLON:
2064
2065#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2066 /*
2067 * If 'equalprg' is empty, do the indenting internally.
2068 */
2069 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2070 {
2071# ifdef FEAT_LISP
2072 if (curbuf->b_p_lisp)
2073 {
2074 op_reindent(oap, get_lisp_indent);
2075 break;
2076 }
2077# endif
2078# ifdef FEAT_CINDENT
2079 op_reindent(oap,
2080# ifdef FEAT_EVAL
2081 *curbuf->b_p_inde != NUL ? get_expr_indent :
2082# endif
2083 get_c_indent);
2084 break;
2085# endif
2086 }
2087#endif
2088
2089 op_colon(oap);
2090 break;
2091
2092 case OP_TILDE:
2093 case OP_UPPER:
2094 case OP_LOWER:
2095 case OP_ROT13:
2096 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002099 CancelRedo();
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 op_tilde(oap);
2103 check_cursor_col();
2104 break;
2105
2106 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002107#if defined(FEAT_EVAL)
2108 if (*curbuf->b_p_fex != NUL)
2109 op_formatexpr(oap); /* use expression */
2110 else
2111#endif
2112 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 op_colon(oap); /* use external command */
2114 else
2115 op_format(oap, FALSE); /* use internal function */
2116 break;
2117
2118 case OP_FORMAT2:
2119 op_format(oap, TRUE); /* use internal function */
2120 break;
2121
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002122 case OP_FUNCTION:
2123 op_function(oap); /* call 'operatorfunc' */
2124 break;
2125
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 case OP_INSERT:
2127 case OP_APPEND:
2128#ifdef FEAT_VISUAL
2129 VIsual_reselect = FALSE; /* don't reselect now */
2130#endif
2131#ifdef FEAT_VISUALEXTRA
2132 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002135 CancelRedo();
2136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
2138 {
2139 /* This is a new edit command, not a restart. Need to
2140 * remember it to make 'insertmode' work with mappings for
2141 * Visual mode. But do this only once. */
2142 restart_edit_save = restart_edit;
2143 restart_edit = 0;
2144
2145 op_insert(oap, cap->count1);
2146
2147 /* TODO: when inserting in several lines, should format all
2148 * the lines. */
2149 auto_format(FALSE, TRUE);
2150
2151 if (restart_edit == 0)
2152 restart_edit = restart_edit_save;
2153 }
2154#else
2155 vim_beep();
2156#endif
2157 break;
2158
2159 case OP_REPLACE:
2160#ifdef FEAT_VISUAL
2161 VIsual_reselect = FALSE; /* don't reselect now */
2162#endif
2163#ifdef FEAT_VISUALEXTRA
2164 if (empty_region_error)
2165#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002168 CancelRedo();
2169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#ifdef FEAT_VISUALEXTRA
2171 else
2172 op_replace(oap, cap->nchar);
2173#endif
2174 break;
2175
2176#ifdef FEAT_FOLDING
2177 case OP_FOLD:
2178 VIsual_reselect = FALSE; /* don't reselect now */
2179 foldCreate(oap->start.lnum, oap->end.lnum);
2180 break;
2181
2182 case OP_FOLDOPEN:
2183 case OP_FOLDOPENREC:
2184 case OP_FOLDCLOSE:
2185 case OP_FOLDCLOSEREC:
2186 VIsual_reselect = FALSE; /* don't reselect now */
2187 opFoldRange(oap->start.lnum, oap->end.lnum,
2188 oap->op_type == OP_FOLDOPEN
2189 || oap->op_type == OP_FOLDOPENREC,
2190 oap->op_type == OP_FOLDOPENREC
2191 || oap->op_type == OP_FOLDCLOSEREC,
2192 oap->is_VIsual);
2193 break;
2194
2195 case OP_FOLDDEL:
2196 case OP_FOLDDELREC:
2197 VIsual_reselect = FALSE; /* don't reselect now */
2198 deleteFold(oap->start.lnum, oap->end.lnum,
2199 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2200 break;
2201#endif
2202 default:
2203 clearopbeep(oap);
2204 }
2205#ifdef FEAT_VIRTUALEDIT
2206 virtual_op = MAYBE;
2207#endif
2208 if (!gui_yank)
2209 {
2210 /*
2211 * if 'sol' not set, go back to old column for some commands
2212 */
2213 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2214 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2215 || oap->op_type == OP_DELETE))
2216 coladvance(curwin->w_curswant = old_col);
2217 }
2218 else
2219 {
2220 curwin->w_cursor = old_cursor;
2221 }
2222#ifdef FEAT_VISUAL
2223 oap->block_mode = FALSE;
2224#endif
2225 clearop(oap);
2226 }
2227}
2228
2229/*
2230 * Handle indent and format operators and visual mode ":".
2231 */
2232 static void
2233op_colon(oap)
2234 oparg_T *oap;
2235{
2236 stuffcharReadbuff(':');
2237#ifdef FEAT_VISUAL
2238 if (oap->is_VIsual)
2239 stuffReadbuff((char_u *)"'<,'>");
2240 else
2241#endif
2242 {
2243 /*
2244 * Make the range look nice, so it can be repeated.
2245 */
2246 if (oap->start.lnum == curwin->w_cursor.lnum)
2247 stuffcharReadbuff('.');
2248 else
2249 stuffnumReadbuff((long)oap->start.lnum);
2250 if (oap->end.lnum != oap->start.lnum)
2251 {
2252 stuffcharReadbuff(',');
2253 if (oap->end.lnum == curwin->w_cursor.lnum)
2254 stuffcharReadbuff('.');
2255 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2256 stuffcharReadbuff('$');
2257 else if (oap->start.lnum == curwin->w_cursor.lnum)
2258 {
2259 stuffReadbuff((char_u *)".+");
2260 stuffnumReadbuff((long)oap->line_count - 1);
2261 }
2262 else
2263 stuffnumReadbuff((long)oap->end.lnum);
2264 }
2265 }
2266 if (oap->op_type != OP_COLON)
2267 stuffReadbuff((char_u *)"!");
2268 if (oap->op_type == OP_INDENT)
2269 {
2270#ifndef FEAT_CINDENT
2271 if (*get_equalprg() == NUL)
2272 stuffReadbuff((char_u *)"indent");
2273 else
2274#endif
2275 stuffReadbuff(get_equalprg());
2276 stuffReadbuff((char_u *)"\n");
2277 }
2278 else if (oap->op_type == OP_FORMAT)
2279 {
2280 if (*p_fp == NUL)
2281 stuffReadbuff((char_u *)"fmt");
2282 else
2283 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002284 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286
2287 /*
2288 * do_cmdline() does the rest
2289 */
2290}
2291
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002292/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002293 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002294 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002295 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002296op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002297 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002298{
2299#ifdef FEAT_EVAL
2300 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002301# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002302 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002303# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002304
2305 if (*p_opfunc == NUL)
2306 EMSG(_("E774: 'operatorfunc' is empty"));
2307 else
2308 {
2309 /* Set '[ and '] marks to text to be operated on. */
2310 curbuf->b_op_start = oap->start;
2311 curbuf->b_op_end = oap->end;
2312 if (oap->motion_type != MLINE && !oap->inclusive)
2313 /* Exclude the end position. */
2314 decl(&curbuf->b_op_end);
2315
2316 if (oap->block_mode)
2317 argv[0] = (char_u *)"block";
2318 else if (oap->motion_type == MLINE)
2319 argv[0] = (char_u *)"line";
2320 else
2321 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002322
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002323# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002324 /* Reset virtual_op so that 'virtualedit' can be changed in the
2325 * function. */
2326 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002327# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002328
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002329 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002330
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002331# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002332 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002333# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002334 }
2335#else
2336 EMSG(_("E775: Eval feature not available"));
2337#endif
2338}
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#if defined(FEAT_MOUSE) || defined(PROTO)
2341/*
2342 * Do the appropriate action for the current mouse click in the current mode.
2343 * Not used for Command-line mode.
2344 *
2345 * Normal Mode:
2346 * event modi- position visual change action
2347 * fier cursor window
2348 * left press - yes end yes
2349 * left press C yes end yes "^]" (2)
2350 * left press S yes end yes "*" (2)
2351 * left drag - yes start if moved no
2352 * left relse - yes start if moved no
2353 * middle press - yes if not active no put register
2354 * middle press - yes if active no yank and put
2355 * right press - yes start or extend yes
2356 * right press S yes no change yes "#" (2)
2357 * right drag - yes extend no
2358 * right relse - yes extend no
2359 *
2360 * Insert or Replace Mode:
2361 * event modi- position visual change action
2362 * fier cursor window
2363 * left press - yes (cannot be active) yes
2364 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2365 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2366 * left drag - yes start or extend (1) no CTRL-O (1)
2367 * left relse - yes start or extend (1) no CTRL-O (1)
2368 * middle press - no (cannot be active) no put register
2369 * right press - yes start or extend yes CTRL-O
2370 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2371 *
2372 * (1) only if mouse pointer moved since press
2373 * (2) only if click is in same buffer
2374 *
2375 * Return TRUE if start_arrow() should be called for edit mode.
2376 */
2377 int
2378do_mouse(oap, c, dir, count, fixindent)
2379 oparg_T *oap; /* operator argument, can be NULL */
2380 int c; /* K_LEFTMOUSE, etc */
2381 int dir; /* Direction to 'put' if necessary */
2382 long count;
2383 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2384{
2385 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2386 static int got_click = FALSE; /* got a click some time back */
2387
2388 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2389 int is_click; /* If FALSE it's a drag or release event */
2390 int is_drag; /* If TRUE it's a drag event */
2391 int jump_flags = 0; /* flags for jump_to_mouse() */
2392 pos_T start_visual;
2393 int moved; /* Has cursor moved? */
2394 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002395#ifdef FEAT_WINDOWS
2396 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifdef FEAT_VERTSPLIT
2399 int in_sep_line; /* mouse in vertical separator line */
2400#endif
2401 int c1, c2;
2402#if defined(FEAT_FOLDING)
2403 pos_T save_cursor;
2404#endif
2405 win_T *old_curwin = curwin;
2406#ifdef FEAT_VISUAL
2407 static pos_T orig_cursor;
2408 colnr_T leftcol, rightcol;
2409 pos_T end_visual;
2410 int diff;
2411 int old_active = VIsual_active;
2412 int old_mode = VIsual_mode;
2413#endif
2414 int regname;
2415
2416#if defined(FEAT_FOLDING)
2417 save_cursor = curwin->w_cursor;
2418#endif
2419
2420 /*
2421 * When GUI is active, always recognize mouse events, otherwise:
2422 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2423 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2424 * - For command line and insert mode 'mouse' is checked before calling
2425 * do_mouse().
2426 */
2427 if (do_always)
2428 do_always = FALSE;
2429 else
2430#ifdef FEAT_GUI
2431 if (!gui.in_use)
2432#endif
2433 {
2434#ifdef FEAT_VISUAL
2435 if (VIsual_active)
2436 {
2437 if (!mouse_has(MOUSE_VISUAL))
2438 return FALSE;
2439 }
2440 else
2441#endif
2442 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2443 return FALSE;
2444 }
2445
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002446 for (;;)
2447 {
2448 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2449 if (is_drag)
2450 {
2451 /* If the next character is the same mouse event then use that
2452 * one. Speeds up dragging the status line. */
2453 if (vpeekc() != NUL)
2454 {
2455 int nc;
2456 int save_mouse_row = mouse_row;
2457 int save_mouse_col = mouse_col;
2458
2459 /* Need to get the character, peeking doesn't get the actual
2460 * one. */
2461 nc = safe_vgetc();
2462 if (c == nc)
2463 continue;
2464 vungetc(nc);
2465 mouse_row = save_mouse_row;
2466 mouse_col = save_mouse_col;
2467 }
2468 }
2469 break;
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472#ifdef FEAT_MOUSESHAPE
2473 /* May have stopped dragging the status or separator line. The pointer is
2474 * most likely still on the status or separator line. */
2475 if (!is_drag && drag_status_line)
2476 {
2477 drag_status_line = FALSE;
2478 update_mouseshape(SHAPE_IDX_STATUS);
2479 }
2480# ifdef FEAT_VERTSPLIT
2481 if (!is_drag && drag_sep_line)
2482 {
2483 drag_sep_line = FALSE;
2484 update_mouseshape(SHAPE_IDX_VSEP);
2485 }
2486# endif
2487#endif
2488
2489 /*
2490 * Ignore drag and release events if we didn't get a click.
2491 */
2492 if (is_click)
2493 got_click = TRUE;
2494 else
2495 {
2496 if (!got_click) /* didn't get click, ignore */
2497 return FALSE;
2498 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002501#ifdef FEAT_WINDOWS
2502 if (in_tab_line)
2503 {
2504 in_tab_line = FALSE;
2505 return FALSE;
2506 }
2507#endif
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510
Bram Moolenaar64969662005-12-14 21:59:55 +00002511#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002513 * ALT is only used for starting/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
2515 if ((mod_mask & MOD_MASK_ALT))
2516 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 /*
2520 * CTRL right mouse button does CTRL-T
2521 */
2522 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2523 {
2524 if (State & INSERT)
2525 stuffcharReadbuff(Ctrl_O);
2526 if (count > 1)
2527 stuffnumReadbuff(count);
2528 stuffcharReadbuff(Ctrl_T);
2529 got_click = FALSE; /* ignore drag&release now */
2530 return FALSE;
2531 }
2532
2533 /*
2534 * CTRL only works with left mouse button
2535 */
2536 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2537 return FALSE;
2538
2539 /*
2540 * When a modifier is down, ignore drag and release events, as well as
2541 * multiple clicks and the middle mouse button.
2542 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2543 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002544 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2545 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && (!is_click
2547 || (mod_mask & MOD_MASK_MULTI_CLICK)
2548 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002549 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 && mouse_model_popup()
2551 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002552 && !((mod_mask & MOD_MASK_ALT)
2553 && !mouse_model_popup()
2554 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 )
2556 return FALSE;
2557
2558 /*
2559 * If the button press was used as the movement command for an operator
2560 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2561 * drag/release events.
2562 */
2563 if (!is_click && which_button == MOUSE_MIDDLE)
2564 return FALSE;
2565
2566 if (oap != NULL)
2567 regname = oap->regname;
2568 else
2569 regname = 0;
2570
2571 /*
2572 * Middle mouse button does a 'put' of the selected text
2573 */
2574 if (which_button == MOUSE_MIDDLE)
2575 {
2576 if (State == NORMAL)
2577 {
2578 /*
2579 * If an operator was pending, we don't know what the user wanted
2580 * to do. Go back to normal mode: Clear the operator and beep().
2581 */
2582 if (oap != NULL && oap->op_type != OP_NOP)
2583 {
2584 clearopbeep(oap);
2585 return FALSE;
2586 }
2587
2588#ifdef FEAT_VISUAL
2589 /*
2590 * If visual was active, yank the highlighted text and put it
2591 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002592 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
2594 if (VIsual_active)
2595 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002596 if (VIsual_select)
2597 {
2598 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002599 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002600 }
2601 else
2602 {
2603 stuffcharReadbuff('y');
2604 stuffcharReadbuff(K_MIDDLEMOUSE);
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 do_always = TRUE; /* ignore 'mouse' setting next time */
2607 return FALSE;
2608 }
2609#endif
2610 /*
2611 * The rest is below jump_to_mouse()
2612 */
2613 }
2614
2615 else if ((State & INSERT) == 0)
2616 return FALSE;
2617
2618 /*
2619 * Middle click in insert mode doesn't move the mouse, just insert the
2620 * contents of a register. '.' register is special, can't insert that
2621 * with do_put().
2622 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2623 * happens for the GUI).
2624 */
2625 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2626 {
2627 if (regname == '.')
2628 insert_reg(regname, TRUE);
2629 else
2630 {
2631#ifdef FEAT_CLIPBOARD
2632 if (clip_star.available && regname == 0)
2633 regname = '*';
2634#endif
2635 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2636 insert_reg(regname, TRUE);
2637 else
2638 {
2639 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2640
2641 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2642 AppendCharToRedobuff(Ctrl_R);
2643 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2644 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2645 }
2646 }
2647 return FALSE;
2648 }
2649 }
2650
2651 /* When dragging or button-up stay in the same window. */
2652 if (!is_click)
2653 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2654
2655 start_visual.lnum = 0;
2656
Bram Moolenaarf740b292006-02-16 22:11:02 +00002657#ifdef FEAT_WINDOWS
2658 /* Check for clicking in the tab page line. */
2659 if (mouse_row == 0 && firstwin->w_winrow > 0)
2660 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002661 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002662 {
2663 if (in_tab_line)
2664 {
2665 c1 = TabPageIdxs[mouse_col];
2666 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2667 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002668 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002669 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002670
Bram Moolenaarf740b292006-02-16 22:11:02 +00002671 /* click in a tab selects that tab page */
2672 if (is_click
2673# ifdef FEAT_CMDWIN
2674 && cmdwin_type == 0
2675# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002676 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002677 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002678 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002679 c1 = TabPageIdxs[mouse_col];
2680 if (c1 >= 0)
2681 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002682 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2683 {
2684 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002685 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002686 tabpage_new();
2687 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2688 }
2689 else
2690 {
2691 /* Go to specified tab page, or next one if not clicking
2692 * on a label. */
2693 goto_tabpage(c1);
2694
2695 /* It's like clicking on the status line of a window. */
2696 if (curwin != old_curwin)
2697 end_visual_mode();
2698 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002699 }
2700 else if (c1 < 0)
2701 {
2702 tabpage_T *tp;
2703
2704 /* Close the current or specified tab page. */
2705 if (c1 == -999)
2706 tp = curtab;
2707 else
2708 tp = find_tabpage(-c1);
2709 if (tp == curtab)
2710 {
2711 if (first_tabpage->tp_next != NULL)
2712 tabpage_close(FALSE);
2713 }
2714 else if (tp != NULL)
2715 tabpage_close_other(tp, FALSE);
2716 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002717 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002718 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002719 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002720 else if (is_drag && in_tab_line)
2721 {
2722 c1 = TabPageIdxs[mouse_col];
2723 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2724 return FALSE;
2725 }
2726
Bram Moolenaarf740b292006-02-16 22:11:02 +00002727#endif
2728
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 /*
2730 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2731 * right button up -> pop-up menu
2732 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002733 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
2735 if (mouse_model_popup())
2736 {
2737 if (which_button == MOUSE_RIGHT
2738 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2739 {
2740 /*
2741 * NOTE: Ignore right button down and drag mouse events.
2742 * Windows only shows the popup menu on the button up event.
2743 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002744#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2745 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (!is_click)
2747 return FALSE;
2748#endif
2749#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2750 if (is_click || is_drag)
2751 return FALSE;
2752#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002753#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2755 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2756 if (gui.in_use)
2757 {
2758 jump_flags = 0;
2759 if (STRCMP(p_mousem, "popup_setpos") == 0)
2760 {
2761 /* First set the cursor position before showing the popup
2762 * menu. */
2763#ifdef FEAT_VISUAL
2764 if (VIsual_active)
2765 {
2766 pos_T m_pos;
2767
2768 /*
2769 * set MOUSE_MAY_STOP_VIS if we are outside the
2770 * selection or the current window (might have false
2771 * negative here)
2772 */
2773 if (mouse_row < W_WINROW(curwin)
2774 || mouse_row
2775 > (W_WINROW(curwin) + curwin->w_height))
2776 jump_flags = MOUSE_MAY_STOP_VIS;
2777 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2778 jump_flags = MOUSE_MAY_STOP_VIS;
2779 else
2780 {
2781 if ((lt(curwin->w_cursor, VIsual)
2782 && (lt(m_pos, curwin->w_cursor)
2783 || lt(VIsual, m_pos)))
2784 || (lt(VIsual, curwin->w_cursor)
2785 && (lt(m_pos, VIsual)
2786 || lt(curwin->w_cursor, m_pos))))
2787 {
2788 jump_flags = MOUSE_MAY_STOP_VIS;
2789 }
2790 else if (VIsual_mode == Ctrl_V)
2791 {
2792 getvcols(curwin, &curwin->w_cursor, &VIsual,
2793 &leftcol, &rightcol);
2794 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2795 if (m_pos.col < leftcol || m_pos.col > rightcol)
2796 jump_flags = MOUSE_MAY_STOP_VIS;
2797 }
2798 }
2799 }
2800 else
2801 jump_flags = MOUSE_MAY_STOP_VIS;
2802#endif
2803 }
2804 if (jump_flags)
2805 {
2806 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2807 update_curbuf(
2808#ifdef FEAT_VISUAL
2809 VIsual_active ? INVERTED :
2810#endif
2811 VALID);
2812 setcursor();
2813 out_flush(); /* Update before showing popup menu */
2814 }
2815# ifdef FEAT_MENU
2816 gui_show_popupmenu();
2817# endif
2818 return (jump_flags & CURSOR_MOVED) != 0;
2819 }
2820 else
2821 return FALSE;
2822#else
2823 return FALSE;
2824#endif
2825 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002826 if (which_button == MOUSE_LEFT
2827 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 which_button = MOUSE_RIGHT;
2830 mod_mask &= ~MOD_MASK_SHIFT;
2831 }
2832 }
2833
2834#ifdef FEAT_VISUAL
2835 if ((State & (NORMAL | INSERT))
2836 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2837 {
2838 if (which_button == MOUSE_LEFT)
2839 {
2840 if (is_click)
2841 {
2842 /* stop Visual mode for a left click in a window, but not when
2843 * on a status line */
2844 if (VIsual_active)
2845 jump_flags |= MOUSE_MAY_STOP_VIS;
2846 }
2847 else if (mouse_has(MOUSE_VISUAL))
2848 jump_flags |= MOUSE_MAY_VIS;
2849 }
2850 else if (which_button == MOUSE_RIGHT)
2851 {
2852 if (is_click && VIsual_active)
2853 {
2854 /*
2855 * Remember the start and end of visual before moving the
2856 * cursor.
2857 */
2858 if (lt(curwin->w_cursor, VIsual))
2859 {
2860 start_visual = curwin->w_cursor;
2861 end_visual = VIsual;
2862 }
2863 else
2864 {
2865 start_visual = VIsual;
2866 end_visual = curwin->w_cursor;
2867 }
2868 }
2869 jump_flags |= MOUSE_FOCUS;
2870 if (mouse_has(MOUSE_VISUAL))
2871 jump_flags |= MOUSE_MAY_VIS;
2872 }
2873 }
2874#endif
2875
2876 /*
2877 * If an operator is pending, ignore all drags and releases until the
2878 * next mouse click.
2879 */
2880 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2881 {
2882 got_click = FALSE;
2883 oap->motion_type = MCHAR;
2884 }
2885
2886 /* When releasing the button let jump_to_mouse() know. */
2887 if (!is_click && !is_drag)
2888 jump_flags |= MOUSE_RELEASED;
2889
2890 /*
2891 * JUMP!
2892 */
2893 jump_flags = jump_to_mouse(jump_flags,
2894 oap == NULL ? NULL : &(oap->inclusive), which_button);
2895 moved = (jump_flags & CURSOR_MOVED);
2896 in_status_line = (jump_flags & IN_STATUS_LINE);
2897#ifdef FEAT_VERTSPLIT
2898 in_sep_line = (jump_flags & IN_SEP_LINE);
2899#endif
2900
2901#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002902 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2904 {
2905 int key = KEY2TERMCAP1(c);
2906
2907 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2908 || key == (int)KE_RIGHTRELEASE)
2909 netbeans_button_release(which_button);
2910 }
2911#endif
2912
2913 /* When jumping to another window, clear a pending operator. That's a bit
2914 * friendlier than beeping and not jumping to that window. */
2915 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2916 clearop(oap);
2917
2918#ifdef FEAT_FOLDING
2919 if (mod_mask == 0
2920 && !is_drag
2921 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2922 && which_button == MOUSE_LEFT)
2923 {
2924 /* open or close a fold at this line */
2925 if (jump_flags & MOUSE_FOLD_OPEN)
2926 openFold(curwin->w_cursor.lnum, 1L);
2927 else
2928 closeFold(curwin->w_cursor.lnum, 1L);
2929 /* don't move the cursor if still in the same window */
2930 if (curwin == old_curwin)
2931 curwin->w_cursor = save_cursor;
2932 }
2933#endif
2934
2935#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2936 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2937 {
2938 clip_modeless(which_button, is_click, is_drag);
2939 return FALSE;
2940 }
2941#endif
2942
2943#ifdef FEAT_VISUAL
2944 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002945 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (VIsual_active && is_drag && p_so)
2947 {
2948 /* In the very first line, allow scrolling one line */
2949 if (mouse_row == 0)
2950 mouse_dragging = 2;
2951 else
2952 mouse_dragging = 1;
2953 }
2954
2955 /* When dragging the mouse above the window, scroll down. */
2956 if (is_drag && mouse_row < 0 && !in_status_line)
2957 {
2958 scroll_redraw(FALSE, 1L);
2959 mouse_row = 0;
2960 }
2961
2962 if (start_visual.lnum) /* right click in visual mode */
2963 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002964 /* When ALT is pressed make Visual mode blockwise. */
2965 if (mod_mask & MOD_MASK_ALT)
2966 VIsual_mode = Ctrl_V;
2967
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 /*
2969 * In Visual-block mode, divide the area in four, pick up the corner
2970 * that is in the quarter that the cursor is in.
2971 */
2972 if (VIsual_mode == Ctrl_V)
2973 {
2974 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2975 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2976 end_visual.col = leftcol;
2977 else
2978 end_visual.col = rightcol;
2979 if (curwin->w_cursor.lnum <
2980 (start_visual.lnum + end_visual.lnum) / 2)
2981 end_visual.lnum = end_visual.lnum;
2982 else
2983 end_visual.lnum = start_visual.lnum;
2984
2985 /* move VIsual to the right column */
2986 start_visual = curwin->w_cursor; /* save the cursor pos */
2987 curwin->w_cursor = end_visual;
2988 coladvance(end_visual.col);
2989 VIsual = curwin->w_cursor;
2990 curwin->w_cursor = start_visual; /* restore the cursor */
2991 }
2992 else
2993 {
2994 /*
2995 * If the click is before the start of visual, change the start.
2996 * If the click is after the end of visual, change the end. If
2997 * the click is inside the visual, change the closest side.
2998 */
2999 if (lt(curwin->w_cursor, start_visual))
3000 VIsual = end_visual;
3001 else if (lt(end_visual, curwin->w_cursor))
3002 VIsual = start_visual;
3003 else
3004 {
3005 /* In the same line, compare column number */
3006 if (end_visual.lnum == start_visual.lnum)
3007 {
3008 if (curwin->w_cursor.col - start_visual.col >
3009 end_visual.col - curwin->w_cursor.col)
3010 VIsual = start_visual;
3011 else
3012 VIsual = end_visual;
3013 }
3014
3015 /* In different lines, compare line number */
3016 else
3017 {
3018 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
3019 (end_visual.lnum - curwin->w_cursor.lnum);
3020
3021 if (diff > 0) /* closest to end */
3022 VIsual = start_visual;
3023 else if (diff < 0) /* closest to start */
3024 VIsual = end_visual;
3025 else /* in the middle line */
3026 {
3027 if (curwin->w_cursor.col <
3028 (start_visual.col + end_visual.col) / 2)
3029 VIsual = end_visual;
3030 else
3031 VIsual = start_visual;
3032 }
3033 }
3034 }
3035 }
3036 }
3037 /*
3038 * If Visual mode started in insert mode, execute "CTRL-O"
3039 */
3040 else if ((State & INSERT) && VIsual_active)
3041 stuffcharReadbuff(Ctrl_O);
3042#endif
3043
3044 /*
3045 * Middle mouse click: Put text before cursor.
3046 */
3047 if (which_button == MOUSE_MIDDLE)
3048 {
3049#ifdef FEAT_CLIPBOARD
3050 if (clip_star.available && regname == 0)
3051 regname = '*';
3052#endif
3053 if (yank_register_mline(regname))
3054 {
3055 if (mouse_past_bottom)
3056 dir = FORWARD;
3057 }
3058 else if (mouse_past_eol)
3059 dir = FORWARD;
3060
3061 if (fixindent)
3062 {
3063 c1 = (dir == BACKWARD) ? '[' : ']';
3064 c2 = 'p';
3065 }
3066 else
3067 {
3068 c1 = (dir == FORWARD) ? 'p' : 'P';
3069 c2 = NUL;
3070 }
3071 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3072
3073 /*
3074 * Remember where the paste started, so in edit() Insstart can be set
3075 * to this position
3076 */
3077 if (restart_edit != 0)
3078 where_paste_started = curwin->w_cursor;
3079 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3080 }
3081
3082#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3083 /*
3084 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3085 * error under the mouse pointer.
3086 */
3087 else if (((mod_mask & MOD_MASK_CTRL)
3088 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3089 && bt_quickfix(curbuf))
3090 {
3091 if (State & INSERT)
3092 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003093 if (curwin->w_llist_ref == NULL) /* quickfix window */
3094 stuffReadbuff((char_u *)":.cc\n");
3095 else /* location list window */
3096 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 got_click = FALSE; /* ignore drag&release now */
3098 }
3099#endif
3100
3101 /*
3102 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3103 * under the mouse pointer.
3104 */
3105 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3106 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3107 {
3108 if (State & INSERT)
3109 stuffcharReadbuff(Ctrl_O);
3110 stuffcharReadbuff(Ctrl_RSB);
3111 got_click = FALSE; /* ignore drag&release now */
3112 }
3113
3114 /*
3115 * Shift-Mouse click searches for the next occurrence of the word under
3116 * the mouse pointer
3117 */
3118 else if ((mod_mask & MOD_MASK_SHIFT))
3119 {
3120 if (State & INSERT
3121#ifdef FEAT_VISUAL
3122 || (VIsual_active && VIsual_select)
3123#endif
3124 )
3125 stuffcharReadbuff(Ctrl_O);
3126 if (which_button == MOUSE_LEFT)
3127 stuffcharReadbuff('*');
3128 else /* MOUSE_RIGHT */
3129 stuffcharReadbuff('#');
3130 }
3131
3132 /* Handle double clicks, unless on status line */
3133 else if (in_status_line)
3134 {
3135#ifdef FEAT_MOUSESHAPE
3136 if ((is_drag || is_click) && !drag_status_line)
3137 {
3138 drag_status_line = TRUE;
3139 update_mouseshape(-1);
3140 }
3141#endif
3142 }
3143#ifdef FEAT_VERTSPLIT
3144 else if (in_sep_line)
3145 {
3146# ifdef FEAT_MOUSESHAPE
3147 if ((is_drag || is_click) && !drag_sep_line)
3148 {
3149 drag_sep_line = TRUE;
3150 update_mouseshape(-1);
3151 }
3152# endif
3153 }
3154#endif
3155#ifdef FEAT_VISUAL
3156 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3157 && mouse_has(MOUSE_VISUAL))
3158 {
3159 if (is_click || !VIsual_active)
3160 {
3161 if (VIsual_active)
3162 orig_cursor = VIsual;
3163 else
3164 {
3165 check_visual_highlight();
3166 VIsual = curwin->w_cursor;
3167 orig_cursor = VIsual;
3168 VIsual_active = TRUE;
3169 VIsual_reselect = TRUE;
3170 /* start Select mode if 'selectmode' contains "mouse" */
3171 may_start_select('o');
3172 setmouse();
3173 }
3174 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003175 {
3176 /* Double click with ALT pressed makes it blockwise. */
3177 if (mod_mask & MOD_MASK_ALT)
3178 VIsual_mode = Ctrl_V;
3179 else
3180 VIsual_mode = 'v';
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3183 VIsual_mode = 'V';
3184 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3185 VIsual_mode = Ctrl_V;
3186#ifdef FEAT_CLIPBOARD
3187 /* Make sure the clipboard gets updated. Needed because start and
3188 * end may still be the same, and the selection needs to be owned */
3189 clip_star.vmode = NUL;
3190#endif
3191 }
3192 /*
3193 * A double click selects a word or a block.
3194 */
3195 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3196 {
3197 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003198 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 if (is_click)
3201 {
3202 /* If the character under the cursor (skipping white space) is
3203 * not a word character, try finding a match and select a (),
3204 * {}, [], #if/#endif, etc. block. */
3205 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003206 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 inc(&end_visual);
3208 if (oap != NULL)
3209 oap->motion_type = MCHAR;
3210 if (oap != NULL
3211 && VIsual_mode == 'v'
3212 && !vim_iswordc(gchar_pos(&end_visual))
3213 && equalpos(curwin->w_cursor, VIsual)
3214 && (pos = findmatch(oap, NUL)) != NULL)
3215 {
3216 curwin->w_cursor = *pos;
3217 if (oap->motion_type == MLINE)
3218 VIsual_mode = 'V';
3219 else if (*p_sel == 'e')
3220 {
3221 if (lt(curwin->w_cursor, VIsual))
3222 ++VIsual.col;
3223 else
3224 ++curwin->w_cursor.col;
3225 }
3226 }
3227 }
3228
3229 if (pos == NULL && (is_click || is_drag))
3230 {
3231 /* When not found a match or when dragging: extend to include
3232 * a word. */
3233 if (lt(curwin->w_cursor, orig_cursor))
3234 {
3235 find_start_of_word(&curwin->w_cursor);
3236 find_end_of_word(&VIsual);
3237 }
3238 else
3239 {
3240 find_start_of_word(&VIsual);
3241 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3242#ifdef FEAT_MBYTE
3243 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003244 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#else
3246 ++curwin->w_cursor.col;
3247#endif
3248 find_end_of_word(&curwin->w_cursor);
3249 }
3250 }
3251 curwin->w_set_curswant = TRUE;
3252 }
3253 if (is_click)
3254 redraw_curbuf_later(INVERTED); /* update the inversion */
3255 }
3256 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003257 {
3258 if (mod_mask & MOD_MASK_ALT)
3259 VIsual_mode = Ctrl_V;
3260 else
3261 VIsual_mode = 'v';
3262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003265 if ((!VIsual_active && old_active && mode_displayed)
3266 || (VIsual_active && p_smd && msg_silent == 0
3267 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 redraw_cmdline = TRUE;
3269#endif
3270
3271 return moved;
3272}
3273
3274#ifdef FEAT_VISUAL
3275/*
3276 * Move "pos" back to the start of the word it's in.
3277 */
3278 static void
3279find_start_of_word(pos)
3280 pos_T *pos;
3281{
3282 char_u *line;
3283 int cclass;
3284 int col;
3285
3286 line = ml_get(pos->lnum);
3287 cclass = get_mouse_class(line + pos->col);
3288
3289 while (pos->col > 0)
3290 {
3291 col = pos->col - 1;
3292#ifdef FEAT_MBYTE
3293 col -= (*mb_head_off)(line, line + col);
3294#endif
3295 if (get_mouse_class(line + col) != cclass)
3296 break;
3297 pos->col = col;
3298 }
3299}
3300
3301/*
3302 * Move "pos" forward to the end of the word it's in.
3303 * When 'selection' is "exclusive", the position is just after the word.
3304 */
3305 static void
3306find_end_of_word(pos)
3307 pos_T *pos;
3308{
3309 char_u *line;
3310 int cclass;
3311 int col;
3312
3313 line = ml_get(pos->lnum);
3314 if (*p_sel == 'e' && pos->col > 0)
3315 {
3316 --pos->col;
3317#ifdef FEAT_MBYTE
3318 pos->col -= (*mb_head_off)(line, line + pos->col);
3319#endif
3320 }
3321 cclass = get_mouse_class(line + pos->col);
3322 while (line[pos->col] != NUL)
3323 {
3324#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003325 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#else
3327 col = pos->col + 1;
3328#endif
3329 if (get_mouse_class(line + col) != cclass)
3330 {
3331 if (*p_sel == 'e')
3332 pos->col = col;
3333 break;
3334 }
3335 pos->col = col;
3336 }
3337}
3338
3339/*
3340 * Get class of a character for selection: same class means same word.
3341 * 0: blank
3342 * 1: punctuation groups
3343 * 2: normal word character
3344 * >2: multi-byte word character.
3345 */
3346 static int
3347get_mouse_class(p)
3348 char_u *p;
3349{
3350 int c;
3351
3352#ifdef FEAT_MBYTE
3353 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3354 return mb_get_class(p);
3355#endif
3356
3357 c = *p;
3358 if (c == ' ' || c == '\t')
3359 return 0;
3360
3361 if (vim_iswordc(c))
3362 return 2;
3363
3364 /*
3365 * There are a few special cases where we want certain combinations of
3366 * characters to be considered as a single word. These are things like
3367 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003368 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
3370 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3371 return 1;
3372 return c;
3373}
3374#endif /* FEAT_VISUAL */
3375#endif /* FEAT_MOUSE */
3376
3377#if defined(FEAT_VISUAL) || defined(PROTO)
3378/*
3379 * Check if highlighting for visual mode is possible, give a warning message
3380 * if not.
3381 */
3382 void
3383check_visual_highlight()
3384{
3385 static int did_check = FALSE;
3386
3387 if (full_screen)
3388 {
3389 if (!did_check && hl_attr(HLF_V) == 0)
3390 MSG(_("Warning: terminal cannot highlight"));
3391 did_check = TRUE;
3392 }
3393}
3394
3395/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003396 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 * This function should ALWAYS be called to end Visual mode, except from
3398 * do_pending_operator().
3399 */
3400 void
3401end_visual_mode()
3402{
3403#ifdef FEAT_CLIPBOARD
3404 /*
3405 * If we are using the clipboard, then remember what was selected in case
3406 * we need to paste it somewhere while we still own the selection.
3407 * Only do this when the clipboard is already owned. Don't want to grab
3408 * the selection when hitting ESC.
3409 */
3410 if (clip_star.available && clip_star.owned)
3411 clip_auto_select();
3412#endif
3413
3414 VIsual_active = FALSE;
3415#ifdef FEAT_MOUSE
3416 setmouse();
3417 mouse_dragging = 0;
3418#endif
3419
3420 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003421 curbuf->b_visual.vi_mode = VIsual_mode;
3422 curbuf->b_visual.vi_start = VIsual;
3423 curbuf->b_visual.vi_end = curwin->w_cursor;
3424 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#ifdef FEAT_EVAL
3426 curbuf->b_visual_mode_eval = VIsual_mode;
3427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#ifdef FEAT_VIRTUALEDIT
3429 if (!virtual_active())
3430 curwin->w_cursor.coladd = 0;
3431#endif
3432
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003433 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 clear_cmdline = TRUE; /* unshow visual mode later */
3435#ifdef FEAT_CMDL_INFO
3436 else
3437 clear_showcmd();
3438#endif
3439
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003440 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
3444 * Reset VIsual_active and VIsual_reselect.
3445 */
3446 void
3447reset_VIsual_and_resel()
3448{
3449 if (VIsual_active)
3450 {
3451 end_visual_mode();
3452 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3453 }
3454 VIsual_reselect = FALSE;
3455}
3456
3457/*
3458 * Reset VIsual_active and VIsual_reselect if it's set.
3459 */
3460 void
3461reset_VIsual()
3462{
3463 if (VIsual_active)
3464 {
3465 end_visual_mode();
3466 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3467 VIsual_reselect = FALSE;
3468 }
3469}
3470#endif /* FEAT_VISUAL */
3471
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003472#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3474
3475/*
3476 * Check for a balloon-eval special item to include when searching for an
3477 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3478 * Returns TRUE if the character at "*ptr" should be included.
3479 * "dir" is FORWARD or BACKWARD, the direction of searching.
3480 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3481 * "bnp" points to a counter for square brackets.
3482 */
3483 static int
3484find_is_eval_item(ptr, colp, bnp, dir)
3485 char_u *ptr;
3486 int *colp;
3487 int *bnp;
3488 int dir;
3489{
3490 /* Accept everything inside []. */
3491 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3492 ++*bnp;
3493 if (*bnp > 0)
3494 {
3495 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3496 --*bnp;
3497 return TRUE;
3498 }
3499
3500 /* skip over "s.var" */
3501 if (*ptr == '.')
3502 return TRUE;
3503
3504 /* two-character item: s->var */
3505 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3506 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3507 {
3508 *colp += dir;
3509 return TRUE;
3510 }
3511 return FALSE;
3512}
3513#endif
3514
3515/*
3516 * Find the identifier under or to the right of the cursor.
3517 * "find_type" can have one of three values:
3518 * FIND_IDENT: find an identifier (keyword)
3519 * FIND_STRING: find any non-white string
3520 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003521 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 *
3523 * There are three steps:
3524 * 1. Search forward for the start of an identifier/string. Doesn't move if
3525 * already on one.
3526 * 2. Search backward for the start of this identifier/string.
3527 * This doesn't match the real Vi but I like it a little better and it
3528 * shouldn't bother anyone.
3529 * 3. Search forward to the end of this identifier/string.
3530 * When FIND_IDENT isn't defined, we backup until a blank.
3531 *
3532 * Returns the length of the string, or zero if no string is found.
3533 * If a string is found, a pointer to the string is put in "*string". This
3534 * string is not always NUL terminated.
3535 */
3536 int
3537find_ident_under_cursor(string, find_type)
3538 char_u **string;
3539 int find_type;
3540{
3541 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3542 curwin->w_cursor.col, string, find_type);
3543}
3544
3545/*
3546 * Like find_ident_under_cursor(), but for any window and any position.
3547 * However: Uses 'iskeyword' from the current window!.
3548 */
3549 int
3550find_ident_at_pos(wp, lnum, startcol, string, find_type)
3551 win_T *wp;
3552 linenr_T lnum;
3553 colnr_T startcol;
3554 char_u **string;
3555 int find_type;
3556{
3557 char_u *ptr;
3558 int col = 0; /* init to shut up GCC */
3559 int i;
3560#ifdef FEAT_MBYTE
3561 int this_class = 0;
3562 int prev_class;
3563 int prevcol;
3564#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003565#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 int bn = 0; /* bracket nesting */
3567#endif
3568
3569 /*
3570 * if i == 0: try to find an identifier
3571 * if i == 1: try to find any non-white string
3572 */
3573 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3574 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3575 {
3576 /*
3577 * 1. skip to start of identifier/string
3578 */
3579 col = startcol;
3580#ifdef FEAT_MBYTE
3581 if (has_mbyte)
3582 {
3583 while (ptr[col] != NUL)
3584 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 /* Stop at a ']' to evaluate "a[x]". */
3587 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3588 break;
3589# endif
3590 this_class = mb_get_class(ptr + col);
3591 if (this_class != 0 && (i == 1 || this_class != 1))
3592 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003593 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
3596 else
3597#endif
3598 while (ptr[col] != NUL
3599 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003600# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3602# endif
3603 )
3604 ++col;
3605
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003606#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /* When starting on a ']' count it, so that we include the '['. */
3608 bn = ptr[col] == ']';
3609#endif
3610
3611 /*
3612 * 2. Back up to start of identifier/string.
3613 */
3614#ifdef FEAT_MBYTE
3615 if (has_mbyte)
3616 {
3617 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003618# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3620 this_class = mb_get_class((char_u *)"a");
3621 else
3622# endif
3623 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003624 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3627 prev_class = mb_get_class(ptr + prevcol);
3628 if (this_class != prev_class
3629 && (i == 0
3630 || prev_class == 0
3631 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003632# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 && (!(find_type & FIND_EVAL)
3634 || prevcol == 0
3635 || !find_is_eval_item(ptr + prevcol, &prevcol,
3636 &bn, BACKWARD))
3637# endif
3638 )
3639 break;
3640 col = prevcol;
3641 }
3642
3643 /* If we don't want just any old string, or we've found an
3644 * identifier, stop searching. */
3645 if (this_class > 2)
3646 this_class = 2;
3647 if (!(find_type & FIND_STRING) || this_class == 2)
3648 break;
3649 }
3650 else
3651#endif
3652 {
3653 while (col > 0
3654 && ((i == 0
3655 ? vim_iswordc(ptr[col - 1])
3656 : (!vim_iswhite(ptr[col - 1])
3657 && (!(find_type & FIND_IDENT)
3658 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003659#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 || ((find_type & FIND_EVAL)
3661 && col > 1
3662 && find_is_eval_item(ptr + col - 1, &col,
3663 &bn, BACKWARD))
3664#endif
3665 ))
3666 --col;
3667
3668 /* If we don't want just any old string, or we've found an
3669 * identifier, stop searching. */
3670 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3671 break;
3672 }
3673 }
3674
3675 if (ptr[col] == NUL || (i == 0 && (
3676#ifdef FEAT_MBYTE
3677 has_mbyte ? this_class != 2 :
3678#endif
3679 !vim_iswordc(ptr[col]))))
3680 {
3681 /*
3682 * didn't find an identifier or string
3683 */
3684 if (find_type & FIND_STRING)
3685 EMSG(_("E348: No string under cursor"));
3686 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003687 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 return 0;
3689 }
3690 ptr += col;
3691 *string = ptr;
3692
3693 /*
3694 * 3. Find the end if the identifier/string.
3695 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003696#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 bn = 0;
3698 startcol -= col;
3699#endif
3700 col = 0;
3701#ifdef FEAT_MBYTE
3702 if (has_mbyte)
3703 {
3704 /* Search for point of changing multibyte character class. */
3705 this_class = mb_get_class(ptr);
3706 while (ptr[col] != NUL
3707 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3708 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003709# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 || ((find_type & FIND_EVAL)
3711 && col <= (int)startcol
3712 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3713# endif
3714 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003715 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 else
3718#endif
3719 while ((i == 0 ? vim_iswordc(ptr[col])
3720 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003721# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 || ((find_type & FIND_EVAL)
3723 && col <= (int)startcol
3724 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3725# endif
3726 )
3727 {
3728 ++col;
3729 }
3730
3731 return col;
3732}
3733
3734/*
3735 * Prepare for redo of a normal command.
3736 */
3737 static void
3738prep_redo_cmd(cap)
3739 cmdarg_T *cap;
3740{
3741 prep_redo(cap->oap->regname, cap->count0,
3742 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3743}
3744
3745/*
3746 * Prepare for redo of any command.
3747 * Note that only the last argument can be a multi-byte char.
3748 */
3749 static void
3750prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3751 int regname;
3752 long num;
3753 int cmd1;
3754 int cmd2;
3755 int cmd3;
3756 int cmd4;
3757 int cmd5;
3758{
3759 ResetRedobuff();
3760 if (regname != 0) /* yank from specified buffer */
3761 {
3762 AppendCharToRedobuff('"');
3763 AppendCharToRedobuff(regname);
3764 }
3765 if (num)
3766 AppendNumberToRedobuff(num);
3767
3768 if (cmd1 != NUL)
3769 AppendCharToRedobuff(cmd1);
3770 if (cmd2 != NUL)
3771 AppendCharToRedobuff(cmd2);
3772 if (cmd3 != NUL)
3773 AppendCharToRedobuff(cmd3);
3774 if (cmd4 != NUL)
3775 AppendCharToRedobuff(cmd4);
3776 if (cmd5 != NUL)
3777 AppendCharToRedobuff(cmd5);
3778}
3779
3780/*
3781 * check for operator active and clear it
3782 *
3783 * return TRUE if operator was active
3784 */
3785 static int
3786checkclearop(oap)
3787 oparg_T *oap;
3788{
3789 if (oap->op_type == OP_NOP)
3790 return FALSE;
3791 clearopbeep(oap);
3792 return TRUE;
3793}
3794
3795/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003796 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003798 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 */
3800 static int
3801checkclearopq(oap)
3802 oparg_T *oap;
3803{
3804 if (oap->op_type == OP_NOP
3805#ifdef FEAT_VISUAL
3806 && !VIsual_active
3807#endif
3808 )
3809 return FALSE;
3810 clearopbeep(oap);
3811 return TRUE;
3812}
3813
3814 static void
3815clearop(oap)
3816 oparg_T *oap;
3817{
3818 oap->op_type = OP_NOP;
3819 oap->regname = 0;
3820 oap->motion_force = NUL;
3821 oap->use_reg_one = FALSE;
3822}
3823
3824 static void
3825clearopbeep(oap)
3826 oparg_T *oap;
3827{
3828 clearop(oap);
3829 beep_flush();
3830}
3831
3832#ifdef FEAT_VISUAL
3833/*
3834 * Remove the shift modifier from a special key.
3835 */
3836 static void
3837unshift_special(cap)
3838 cmdarg_T *cap;
3839{
3840 switch (cap->cmdchar)
3841 {
3842 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3843 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3844 case K_S_UP: cap->cmdchar = K_UP; break;
3845 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3846 case K_S_HOME: cap->cmdchar = K_HOME; break;
3847 case K_S_END: cap->cmdchar = K_END; break;
3848 }
3849 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3850}
3851#endif
3852
3853#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3854/*
3855 * Routines for displaying a partly typed command
3856 */
3857
3858#ifdef FEAT_VISUAL /* need room for size of Visual area */
3859# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3860#else
3861# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3862#endif
3863static char_u showcmd_buf[SHOWCMD_BUFLEN];
3864static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3865static int showcmd_is_clear = TRUE;
3866static int showcmd_visual = FALSE;
3867
3868static void display_showcmd __ARGS((void));
3869
3870 void
3871clear_showcmd()
3872{
3873 if (!p_sc)
3874 return;
3875
3876#ifdef FEAT_VISUAL
3877 if (VIsual_active && !char_avail())
3878 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003879 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 long lines;
3881 colnr_T leftcol, rightcol;
3882 linenr_T top, bot;
3883
3884 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003885 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 top = VIsual.lnum;
3888 bot = curwin->w_cursor.lnum;
3889 }
3890 else
3891 {
3892 top = curwin->w_cursor.lnum;
3893 bot = VIsual.lnum;
3894 }
3895# ifdef FEAT_FOLDING
3896 /* Include closed folds as a whole. */
3897 hasFolding(top, &top, NULL);
3898 hasFolding(bot, NULL, &bot);
3899# endif
3900 lines = bot - top + 1;
3901
3902 if (VIsual_mode == Ctrl_V)
3903 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003904# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003905 char_u *saved_sbr = p_sbr;
3906
3907 /* Make 'sbr' empty for a moment to get the correct size. */
3908 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003911# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003912 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3915 (long)(rightcol - leftcol + 1));
3916 }
3917 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3918 sprintf((char *)showcmd_buf, "%ld", lines);
3919 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003920 {
3921 char_u *s, *e;
3922 int l;
3923 int bytes = 0;
3924 int chars = 0;
3925
3926 if (cursor_bot)
3927 {
3928 s = ml_get_pos(&VIsual);
3929 e = ml_get_cursor();
3930 }
3931 else
3932 {
3933 s = ml_get_cursor();
3934 e = ml_get_pos(&VIsual);
3935 }
3936 while ((*p_sel != 'e') ? s <= e : s < e)
3937 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003938# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003939 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003940# else
3941 l = (*s == NUL) ? 0 : 1;
3942# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003943 if (l == 0)
3944 {
3945 ++bytes;
3946 ++chars;
3947 break; /* end of line */
3948 }
3949 bytes += l;
3950 ++chars;
3951 s += l;
3952 }
3953 if (bytes == chars)
3954 sprintf((char *)showcmd_buf, "%d", chars);
3955 else
3956 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3959 showcmd_visual = TRUE;
3960 }
3961 else
3962#endif
3963 {
3964 showcmd_buf[0] = NUL;
3965 showcmd_visual = FALSE;
3966
3967 /* Don't actually display something if there is nothing to clear. */
3968 if (showcmd_is_clear)
3969 return;
3970 }
3971
3972 display_showcmd();
3973}
3974
3975/*
3976 * Add 'c' to string of shown command chars.
3977 * Return TRUE if output has been written (and setcursor() has been called).
3978 */
3979 int
3980add_to_showcmd(c)
3981 int c;
3982{
3983 char_u *p;
3984 int old_len;
3985 int extra_len;
3986 int overflow;
3987#if defined(FEAT_MOUSE)
3988 int i;
3989 static int ignore[] =
3990 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003991# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3993 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 K_IGNORE,
3996 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3997 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3998 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003999 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004001 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 0
4003 };
4004#endif
4005
Bram Moolenaar09df3122006-01-23 22:23:09 +00004006 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 return FALSE;
4008
4009 if (showcmd_visual)
4010 {
4011 showcmd_buf[0] = NUL;
4012 showcmd_visual = FALSE;
4013 }
4014
4015#if defined(FEAT_MOUSE)
4016 /* Ignore keys that are scrollbar updates and mouse clicks */
4017 if (IS_SPECIAL(c))
4018 for (i = 0; ignore[i] != 0; ++i)
4019 if (ignore[i] == c)
4020 return FALSE;
4021#endif
4022
4023 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01004024 if (*p == ' ')
4025 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 old_len = (int)STRLEN(showcmd_buf);
4027 extra_len = (int)STRLEN(p);
4028 overflow = old_len + extra_len - SHOWCMD_COLS;
4029 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004030 mch_memmove(showcmd_buf, showcmd_buf + overflow,
4031 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 STRCAT(showcmd_buf, p);
4033
4034 if (char_avail())
4035 return FALSE;
4036
4037 display_showcmd();
4038
4039 return TRUE;
4040}
4041
4042 void
4043add_to_showcmd_c(c)
4044 int c;
4045{
4046 if (!add_to_showcmd(c))
4047 setcursor();
4048}
4049
4050/*
4051 * Delete 'len' characters from the end of the shown command.
4052 */
4053 static void
4054del_from_showcmd(len)
4055 int len;
4056{
4057 int old_len;
4058
4059 if (!p_sc)
4060 return;
4061
4062 old_len = (int)STRLEN(showcmd_buf);
4063 if (len > old_len)
4064 len = old_len;
4065 showcmd_buf[old_len - len] = NUL;
4066
4067 if (!char_avail())
4068 display_showcmd();
4069}
4070
4071/*
4072 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4073 * something and there is a partial mapping.
4074 */
4075 void
4076push_showcmd()
4077{
4078 if (p_sc)
4079 STRCPY(old_showcmd_buf, showcmd_buf);
4080}
4081
4082 void
4083pop_showcmd()
4084{
4085 if (!p_sc)
4086 return;
4087
4088 STRCPY(showcmd_buf, old_showcmd_buf);
4089
4090 display_showcmd();
4091}
4092
4093 static void
4094display_showcmd()
4095{
4096 int len;
4097
4098 cursor_off();
4099
4100 len = (int)STRLEN(showcmd_buf);
4101 if (len == 0)
4102 showcmd_is_clear = TRUE;
4103 else
4104 {
4105 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4106 showcmd_is_clear = FALSE;
4107 }
4108
4109 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004110 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4111 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 */
4113 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4114
4115 setcursor(); /* put cursor back where it belongs */
4116}
4117#endif
4118
4119#ifdef FEAT_SCROLLBIND
4120/*
4121 * When "check" is FALSE, prepare for commands that scroll the window.
4122 * When "check" is TRUE, take care of scroll-binding after the window has
4123 * scrolled. Called from normal_cmd() and edit().
4124 */
4125 void
4126do_check_scrollbind(check)
4127 int check;
4128{
4129 static win_T *old_curwin = NULL;
4130 static linenr_T old_topline = 0;
4131#ifdef FEAT_DIFF
4132 static int old_topfill = 0;
4133#endif
4134 static buf_T *old_buf = NULL;
4135 static colnr_T old_leftcol = 0;
4136
4137 if (check && curwin->w_p_scb)
4138 {
4139 /* If a ":syncbind" command was just used, don't scroll, only reset
4140 * the values. */
4141 if (did_syncbind)
4142 did_syncbind = FALSE;
4143 else if (curwin == old_curwin)
4144 {
4145 /*
4146 * Synchronize other windows, as necessary according to
4147 * 'scrollbind'. Don't do this after an ":edit" command, except
4148 * when 'diff' is set.
4149 */
4150 if ((curwin->w_buffer == old_buf
4151#ifdef FEAT_DIFF
4152 || curwin->w_p_diff
4153#endif
4154 )
4155 && (curwin->w_topline != old_topline
4156#ifdef FEAT_DIFF
4157 || curwin->w_topfill != old_topfill
4158#endif
4159 || curwin->w_leftcol != old_leftcol))
4160 {
4161 check_scrollbind(curwin->w_topline - old_topline,
4162 (long)(curwin->w_leftcol - old_leftcol));
4163 }
4164 }
4165 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4166 {
4167 /*
4168 * When switching between windows, make sure that the relative
4169 * vertical offset is valid for the new window. The relative
4170 * offset is invalid whenever another 'scrollbind' window has
4171 * scrolled to a point that would force the current window to
4172 * scroll past the beginning or end of its buffer. When the
4173 * resync is performed, some of the other 'scrollbind' windows may
4174 * need to jump so that the current window's relative position is
4175 * visible on-screen.
4176 */
4177 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4178 }
4179 curwin->w_scbind_pos = curwin->w_topline;
4180 }
4181
4182 old_curwin = curwin;
4183 old_topline = curwin->w_topline;
4184#ifdef FEAT_DIFF
4185 old_topfill = curwin->w_topfill;
4186#endif
4187 old_buf = curwin->w_buffer;
4188 old_leftcol = curwin->w_leftcol;
4189}
4190
4191/*
4192 * Synchronize any windows that have "scrollbind" set, based on the
4193 * number of rows by which the current window has changed
4194 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4195 */
4196 void
4197check_scrollbind(topline_diff, leftcol_diff)
4198 linenr_T topline_diff;
4199 long leftcol_diff;
4200{
4201 int want_ver;
4202 int want_hor;
4203 win_T *old_curwin = curwin;
4204 buf_T *old_curbuf = curbuf;
4205#ifdef FEAT_VISUAL
4206 int old_VIsual_select = VIsual_select;
4207 int old_VIsual_active = VIsual_active;
4208#endif
4209 colnr_T tgt_leftcol = curwin->w_leftcol;
4210 long topline;
4211 long y;
4212
4213 /*
4214 * check 'scrollopt' string for vertical and horizontal scroll options
4215 */
4216 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4217#ifdef FEAT_DIFF
4218 want_ver |= old_curwin->w_p_diff;
4219#endif
4220 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4221
4222 /*
4223 * loop through the scrollbound windows and scroll accordingly
4224 */
4225#ifdef FEAT_VISUAL
4226 VIsual_select = VIsual_active = 0;
4227#endif
4228 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4229 {
4230 curbuf = curwin->w_buffer;
4231 /* skip original window and windows with 'noscrollbind' */
4232 if (curwin != old_curwin && curwin->w_p_scb)
4233 {
4234 /*
4235 * do the vertical scroll
4236 */
4237 if (want_ver)
4238 {
4239#ifdef FEAT_DIFF
4240 if (old_curwin->w_p_diff && curwin->w_p_diff)
4241 {
4242 diff_set_topline(old_curwin, curwin);
4243 }
4244 else
4245#endif
4246 {
4247 curwin->w_scbind_pos += topline_diff;
4248 topline = curwin->w_scbind_pos;
4249 if (topline > curbuf->b_ml.ml_line_count)
4250 topline = curbuf->b_ml.ml_line_count;
4251 if (topline < 1)
4252 topline = 1;
4253
4254 y = topline - curwin->w_topline;
4255 if (y > 0)
4256 scrollup(y, FALSE);
4257 else
4258 scrolldown(-y, FALSE);
4259 }
4260
4261 redraw_later(VALID);
4262 cursor_correct();
4263#ifdef FEAT_WINDOWS
4264 curwin->w_redr_status = TRUE;
4265#endif
4266 }
4267
4268 /*
4269 * do the horizontal scroll
4270 */
4271 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4272 {
4273 curwin->w_leftcol = tgt_leftcol;
4274 leftcol_changed();
4275 }
4276 }
4277 }
4278
4279 /*
4280 * reset current-window
4281 */
4282#ifdef FEAT_VISUAL
4283 VIsual_select = old_VIsual_select;
4284 VIsual_active = old_VIsual_active;
4285#endif
4286 curwin = old_curwin;
4287 curbuf = old_curbuf;
4288}
4289#endif /* #ifdef FEAT_SCROLLBIND */
4290
4291/*
4292 * Command character that's ignored.
4293 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004294 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 static void
4297nv_ignore(cap)
4298 cmdarg_T *cap;
4299{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004300 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301}
4302
4303/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004304 * Command character that doesn't do anything, but unlike nv_ignore() does
4305 * start edit(). Used for "startinsert" executed while starting up.
4306 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004307 static void
4308nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004309 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004310{
4311}
4312
4313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 * Command character doesn't exist.
4315 */
4316 static void
4317nv_error(cap)
4318 cmdarg_T *cap;
4319{
4320 clearopbeep(cap->oap);
4321}
4322
4323/*
4324 * <Help> and <F1> commands.
4325 */
4326 static void
4327nv_help(cap)
4328 cmdarg_T *cap;
4329{
4330 if (!checkclearopq(cap->oap))
4331 ex_help(NULL);
4332}
4333
4334/*
4335 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4336 */
4337 static void
4338nv_addsub(cap)
4339 cmdarg_T *cap;
4340{
4341 if (!checkclearopq(cap->oap)
4342 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4343 prep_redo_cmd(cap);
4344}
4345
4346/*
4347 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4348 */
4349 static void
4350nv_page(cap)
4351 cmdarg_T *cap;
4352{
4353 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004354 {
4355#ifdef FEAT_WINDOWS
4356 if (mod_mask & MOD_MASK_CTRL)
4357 {
4358 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4359 if (cap->arg == BACKWARD)
4360 goto_tabpage(-(int)cap->count1);
4361 else
4362 goto_tabpage((int)cap->count0);
4363 }
4364 else
4365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368}
4369
4370/*
4371 * Implementation of "gd" and "gD" command.
4372 */
4373 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004374nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 oparg_T *oap;
4376 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004377 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
4379 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 char_u *ptr;
4381
Bram Moolenaard9d30582005-05-18 22:10:28 +00004382 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004383 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004385#ifdef FEAT_FOLDING
4386 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4387 foldOpenCursor();
4388#endif
4389}
4390
4391/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004392 * Search for variable declaration of "ptr[len]".
4393 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4394 * current file ("gD").
4395 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004396 * Return FAIL when not found.
4397 */
4398 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004399find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004400 char_u *ptr;
4401 int len;
4402 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004403 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004404 int searchflags; /* flags passed to searchit() */
4405{
4406 char_u *pat;
4407 pos_T old_pos;
4408 pos_T par_pos;
4409 pos_T found_pos;
4410 int t;
4411 int save_p_ws;
4412 int save_p_scs;
4413 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004414 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004415
4416 if ((pat = alloc(len + 7)) == NULL)
4417 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004418
4419 /* Put "\V" before the pattern to avoid that the special meaning of "."
4420 * and "~" causes trouble. */
4421 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4422 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 old_pos = curwin->w_cursor;
4424 save_p_ws = p_ws;
4425 save_p_scs = p_scs;
4426 p_ws = FALSE; /* don't wrap around end of file now */
4427 p_scs = FALSE; /* don't switch ignorecase off now */
4428
4429 /*
4430 * With "gD" go to line 1.
4431 * With "gd" Search back for the start of the current function, then go
4432 * back until a blank line. If this fails go to line 1.
4433 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004434 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
4436 setpcmark(); /* Set in findpar() otherwise */
4437 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004438 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 else
4441 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004442 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4444 --curwin->w_cursor.lnum;
4445 }
4446 curwin->w_cursor.col = 0;
4447
4448 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004449 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004450 for (;;)
4451 {
4452 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004453 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004454 if (curwin->w_cursor.lnum >= old_pos.lnum)
4455 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004456
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004457 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004458 {
4459 pos_T *pos;
4460
4461 /* Check that the block the match is in doesn't end before the
4462 * position where we started the search from. */
4463 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4464 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4465 && pos->lnum < old_pos.lnum)
4466 continue;
4467 }
4468
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004469 if (t == FAIL)
4470 {
4471 /* If we previously found a valid position, use it. */
4472 if (found_pos.lnum != 0)
4473 {
4474 curwin->w_cursor = found_pos;
4475 t = OK;
4476 }
4477 break;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004480 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004481 {
4482 /* Ignore this line, continue at start of next line. */
4483 ++curwin->w_cursor.lnum;
4484 curwin->w_cursor.col = 0;
4485 continue;
4486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004488 if (!locally) /* global search: use first match found */
4489 break;
4490 if (curwin->w_cursor.lnum >= par_pos.lnum)
4491 {
4492 /* If we previously found a valid position, use it. */
4493 if (found_pos.lnum != 0)
4494 curwin->w_cursor = found_pos;
4495 break;
4496 }
4497
4498 /* For finding a local variable and the match is before the "{" search
4499 * to find a later match. For K&R style function declarations this
4500 * skips the function header without types. */
4501 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004503
4504 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004506 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 curwin->w_cursor = old_pos;
4508 }
4509 else
4510 {
4511 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /* "n" searches forward now */
4513 reset_search_dir();
4514 }
4515
4516 vim_free(pat);
4517 p_ws = save_p_ws;
4518 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004519
4520 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521}
4522
4523/*
4524 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4525 * lines rather than lines in the file.
4526 * 'dist' must be positive.
4527 *
4528 * Return OK if able to move cursor, FAIL otherwise.
4529 */
4530 static int
4531nv_screengo(oap, dir, dist)
4532 oparg_T *oap;
4533 int dir;
4534 long dist;
4535{
4536 int linelen = linetabsize(ml_get_curline());
4537 int retval = OK;
4538 int atend = FALSE;
4539 int n;
4540 int col_off1; /* margin offset for first screen line */
4541 int col_off2; /* margin offset for wrapped screen line */
4542 int width1; /* text width for first screen line */
4543 int width2; /* test width for wrapped screen line */
4544
4545 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004546 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 col_off1 = curwin_col_off();
4549 col_off2 = col_off1 - curwin_col_off2();
4550 width1 = W_WIDTH(curwin) - col_off1;
4551 width2 = W_WIDTH(curwin) - col_off2;
4552
4553#ifdef FEAT_VERTSPLIT
4554 if (curwin->w_width != 0)
4555 {
4556#endif
4557 /*
4558 * Instead of sticking at the last character of the buffer line we
4559 * try to stick in the last column of the screen.
4560 */
4561 if (curwin->w_curswant == MAXCOL)
4562 {
4563 atend = TRUE;
4564 validate_virtcol();
4565 if (width1 <= 0)
4566 curwin->w_curswant = 0;
4567 else
4568 {
4569 curwin->w_curswant = width1 - 1;
4570 if (curwin->w_virtcol > curwin->w_curswant)
4571 curwin->w_curswant += ((curwin->w_virtcol
4572 - curwin->w_curswant - 1) / width2 + 1) * width2;
4573 }
4574 }
4575 else
4576 {
4577 if (linelen > width1)
4578 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4579 else
4580 n = width1;
4581 if (curwin->w_curswant > (colnr_T)n + 1)
4582 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4583 * width2;
4584 }
4585
4586 while (dist--)
4587 {
4588 if (dir == BACKWARD)
4589 {
4590 if ((long)curwin->w_curswant >= width2)
4591 /* move back within line */
4592 curwin->w_curswant -= width2;
4593 else
4594 {
4595 /* to previous line */
4596 if (curwin->w_cursor.lnum == 1)
4597 {
4598 retval = FAIL;
4599 break;
4600 }
4601 --curwin->w_cursor.lnum;
4602#ifdef FEAT_FOLDING
4603 /* Move to the start of a closed fold. Don't do that when
4604 * 'foldopen' contains "all": it will open in a moment. */
4605 if (!(fdo_flags & FDO_ALL))
4606 (void)hasFolding(curwin->w_cursor.lnum,
4607 &curwin->w_cursor.lnum, NULL);
4608#endif
4609 linelen = linetabsize(ml_get_curline());
4610 if (linelen > width1)
4611 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4612 + 1) * width2;
4613 }
4614 }
4615 else /* dir == FORWARD */
4616 {
4617 if (linelen > width1)
4618 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4619 else
4620 n = width1;
4621 if (curwin->w_curswant + width2 < (colnr_T)n)
4622 /* move forward within line */
4623 curwin->w_curswant += width2;
4624 else
4625 {
4626 /* to next line */
4627#ifdef FEAT_FOLDING
4628 /* Move to the end of a closed fold. */
4629 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4630 &curwin->w_cursor.lnum);
4631#endif
4632 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4633 {
4634 retval = FAIL;
4635 break;
4636 }
4637 curwin->w_cursor.lnum++;
4638 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004639 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 }
4642 }
4643#ifdef FEAT_VERTSPLIT
4644 }
4645#endif
4646
4647 coladvance(curwin->w_curswant);
4648
4649#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4650 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4651 {
4652 /*
4653 * Check for landing on a character that got split at the end of the
4654 * last line. We want to advance a screenline, not end up in the same
4655 * screenline or move two screenlines.
4656 */
4657 validate_virtcol();
4658 if (curwin->w_virtcol > curwin->w_curswant
4659 && (curwin->w_curswant < (colnr_T)width1
4660 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4661 : ((curwin->w_curswant - width1) % width2
4662 > (colnr_T)width2 / 2)))
4663 --curwin->w_cursor.col;
4664 }
4665#endif
4666
4667 if (atend)
4668 curwin->w_curswant = MAXCOL; /* stick in the last column */
4669
4670 return retval;
4671}
4672
4673#ifdef FEAT_MOUSE
4674/*
4675 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4676 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004677 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4678 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 static void
4681nv_mousescroll(cap)
4682 cmdarg_T *cap;
4683{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004684# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004685 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004687 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 int row, col;
4690
4691 row = mouse_row;
4692 col = mouse_col;
4693
4694 /* find the window at the pointer coordinates */
4695 curwin = mouse_find_win(&row, &col);
4696 curbuf = curwin->w_buffer;
4697 }
4698# endif
4699
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004700 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004702 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4703 {
4704 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4705 }
4706 else
4707 {
4708 cap->count1 = 3;
4709 cap->count0 = 3;
4710 nv_scroll_line(cap);
4711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004713# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 else
4715 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004716 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4717 if (!curwin->w_p_wrap)
4718 {
4719 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004720
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004721 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4722 step = W_WIDTH(curwin);
4723 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4724 if (val < 0)
4725 val = 0;
4726
4727 gui_do_horiz_scroll(val, TRUE);
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004732# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 curwin->w_redr_status = TRUE;
4734
4735 curwin = old_curwin;
4736 curbuf = curwin->w_buffer;
4737# endif
4738}
4739
4740/*
4741 * Mouse clicks and drags.
4742 */
4743 static void
4744nv_mouse(cap)
4745 cmdarg_T *cap;
4746{
4747 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4748}
4749#endif
4750
4751/*
4752 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4753 * cap->arg must be TRUE for CTRL-E.
4754 */
4755 static void
4756nv_scroll_line(cap)
4757 cmdarg_T *cap;
4758{
4759 if (!checkclearop(cap->oap))
4760 scroll_redraw(cap->arg, cap->count1);
4761}
4762
4763/*
4764 * Scroll "count" lines up or down, and redraw.
4765 */
4766 void
4767scroll_redraw(up, count)
4768 int up;
4769 long count;
4770{
4771 linenr_T prev_topline = curwin->w_topline;
4772#ifdef FEAT_DIFF
4773 int prev_topfill = curwin->w_topfill;
4774#endif
4775 linenr_T prev_lnum = curwin->w_cursor.lnum;
4776
4777 if (up)
4778 scrollup(count, TRUE);
4779 else
4780 scrolldown(count, TRUE);
4781 if (p_so)
4782 {
4783 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4784 * valid, otherwise the screen jumps back at the end of the file. */
4785 cursor_correct();
4786 check_cursor_moved(curwin);
4787 curwin->w_valid |= VALID_TOPLINE;
4788
4789 /* If moved back to where we were, at least move the cursor, otherwise
4790 * we get stuck at one position. Don't move the cursor up if the
4791 * first line of the buffer is already on the screen */
4792 while (curwin->w_topline == prev_topline
4793#ifdef FEAT_DIFF
4794 && curwin->w_topfill == prev_topfill
4795#endif
4796 )
4797 {
4798 if (up)
4799 {
4800 if (curwin->w_cursor.lnum > prev_lnum
4801 || cursor_down(1L, FALSE) == FAIL)
4802 break;
4803 }
4804 else
4805 {
4806 if (curwin->w_cursor.lnum < prev_lnum
4807 || prev_topline == 1L
4808 || cursor_up(1L, FALSE) == FAIL)
4809 break;
4810 }
4811 /* Mark w_topline as valid, otherwise the screen jumps back at the
4812 * end of the file. */
4813 check_cursor_moved(curwin);
4814 curwin->w_valid |= VALID_TOPLINE;
4815 }
4816 }
4817 if (curwin->w_cursor.lnum != prev_lnum)
4818 coladvance(curwin->w_curswant);
4819 redraw_later(VALID);
4820}
4821
4822/*
4823 * Commands that start with "z".
4824 */
4825 static void
4826nv_zet(cap)
4827 cmdarg_T *cap;
4828{
4829 long n;
4830 colnr_T col;
4831 int nchar = cap->nchar;
4832#ifdef FEAT_FOLDING
4833 long old_fdl = curwin->w_p_fdl;
4834 int old_fen = curwin->w_p_fen;
4835#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004836#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004837 int undo = FALSE;
4838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 if (VIM_ISDIGIT(nchar))
4841 {
4842 /*
4843 * "z123{nchar}": edit the count before obtaining {nchar}
4844 */
4845 if (checkclearop(cap->oap))
4846 return;
4847 n = nchar - '0';
4848 for (;;)
4849 {
4850#ifdef USE_ON_FLY_SCROLL
4851 dont_scroll = TRUE; /* disallow scrolling here */
4852#endif
4853 ++no_mapping;
4854 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004855 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 --no_mapping;
4858 --allow_keys;
4859#ifdef FEAT_CMDL_INFO
4860 (void)add_to_showcmd(nchar);
4861#endif
4862 if (nchar == K_DEL || nchar == K_KDEL)
4863 n /= 10;
4864 else if (VIM_ISDIGIT(nchar))
4865 n = n * 10 + (nchar - '0');
4866 else if (nchar == CAR)
4867 {
4868#ifdef FEAT_GUI
4869 need_mouse_correct = TRUE;
4870#endif
4871 win_setheight((int)n);
4872 break;
4873 }
4874 else if (nchar == 'l'
4875 || nchar == 'h'
4876 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004877 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 cap->count1 = n ? n * cap->count1 : cap->count1;
4880 goto dozet;
4881 }
4882 else
4883 {
4884 clearopbeep(cap->oap);
4885 break;
4886 }
4887 }
4888 cap->oap->op_type = OP_NOP;
4889 return;
4890 }
4891
4892dozet:
4893 if (
4894#ifdef FEAT_FOLDING
4895 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4896 * and "zC" only in Visual mode. "zj" and "zk" are motion
4897 * commands. */
4898 cap->nchar != 'f' && cap->nchar != 'F'
4899 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4900 && cap->nchar != 'j' && cap->nchar != 'k'
4901 &&
4902#endif
4903 checkclearop(cap->oap))
4904 return;
4905
4906 /*
4907 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4908 * If line number given, set cursor.
4909 */
4910 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4911 && cap->count0
4912 && cap->count0 != curwin->w_cursor.lnum)
4913 {
4914 setpcmark();
4915 if (cap->count0 > curbuf->b_ml.ml_line_count)
4916 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4917 else
4918 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004919 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921
4922 switch (nchar)
4923 {
4924 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4925 case '+':
4926 if (cap->count0 == 0)
4927 {
4928 /* No count given: put cursor at the line below screen */
4929 validate_botline(); /* make sure w_botline is valid */
4930 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4931 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4932 else
4933 curwin->w_cursor.lnum = curwin->w_botline;
4934 }
4935 /* FALLTHROUGH */
4936 case NL:
4937 case CAR:
4938 case K_KENTER:
4939 beginline(BL_WHITE | BL_FIX);
4940 /* FALLTHROUGH */
4941
4942 case 't': scroll_cursor_top(0, TRUE);
4943 redraw_later(VALID);
4944 break;
4945
4946 /* "z." and "zz": put cursor in middle of screen */
4947 case '.': beginline(BL_WHITE | BL_FIX);
4948 /* FALLTHROUGH */
4949
4950 case 'z': scroll_cursor_halfway(TRUE);
4951 redraw_later(VALID);
4952 break;
4953
4954 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4955 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4956 * when <count> is at bottom of window, and puts that one at
4957 * bottom of window. */
4958 if (cap->count0 != 0)
4959 {
4960 scroll_cursor_bot(0, TRUE);
4961 curwin->w_cursor.lnum = curwin->w_topline;
4962 }
4963 else if (curwin->w_topline == 1)
4964 curwin->w_cursor.lnum = 1;
4965 else
4966 curwin->w_cursor.lnum = curwin->w_topline - 1;
4967 /* FALLTHROUGH */
4968 case '-':
4969 beginline(BL_WHITE | BL_FIX);
4970 /* FALLTHROUGH */
4971
4972 case 'b': scroll_cursor_bot(0, TRUE);
4973 redraw_later(VALID);
4974 break;
4975
4976 /* "zH" - scroll screen right half-page */
4977 case 'H':
4978 cap->count1 *= W_WIDTH(curwin) / 2;
4979 /* FALLTHROUGH */
4980
4981 /* "zh" - scroll screen to the right */
4982 case 'h':
4983 case K_LEFT:
4984 if (!curwin->w_p_wrap)
4985 {
4986 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4987 curwin->w_leftcol = 0;
4988 else
4989 curwin->w_leftcol -= (colnr_T)cap->count1;
4990 leftcol_changed();
4991 }
4992 break;
4993
4994 /* "zL" - scroll screen left half-page */
4995 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4996 /* FALLTHROUGH */
4997
4998 /* "zl" - scroll screen to the left */
4999 case 'l':
5000 case K_RIGHT:
5001 if (!curwin->w_p_wrap)
5002 {
5003 /* scroll the window left */
5004 curwin->w_leftcol += (colnr_T)cap->count1;
5005 leftcol_changed();
5006 }
5007 break;
5008
5009 /* "zs" - scroll screen, cursor at the start */
5010 case 's': if (!curwin->w_p_wrap)
5011 {
5012#ifdef FEAT_FOLDING
5013 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5014 col = 0; /* like the cursor is in col 0 */
5015 else
5016#endif
5017 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
5018 if ((long)col > p_siso)
5019 col -= p_siso;
5020 else
5021 col = 0;
5022 if (curwin->w_leftcol != col)
5023 {
5024 curwin->w_leftcol = col;
5025 redraw_later(NOT_VALID);
5026 }
5027 }
5028 break;
5029
5030 /* "ze" - scroll screen, cursor at the end */
5031 case 'e': if (!curwin->w_p_wrap)
5032 {
5033#ifdef FEAT_FOLDING
5034 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5035 col = 0; /* like the cursor is in col 0 */
5036 else
5037#endif
5038 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5039 n = W_WIDTH(curwin) - curwin_col_off();
5040 if ((long)col + p_siso < n)
5041 col = 0;
5042 else
5043 col = col + p_siso - n + 1;
5044 if (curwin->w_leftcol != col)
5045 {
5046 curwin->w_leftcol = col;
5047 redraw_later(NOT_VALID);
5048 }
5049 }
5050 break;
5051
5052#ifdef FEAT_FOLDING
5053 /* "zF": create fold command */
5054 /* "zf": create fold operator */
5055 case 'F':
5056 case 'f': if (foldManualAllowed(TRUE))
5057 {
5058 cap->nchar = 'f';
5059 nv_operator(cap);
5060 curwin->w_p_fen = TRUE;
5061
5062 /* "zF" is like "zfzf" */
5063 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5064 {
5065 nv_operator(cap);
5066 finish_op = TRUE;
5067 }
5068 }
5069 else
5070 clearopbeep(cap->oap);
5071 break;
5072
5073 /* "zd": delete fold at cursor */
5074 /* "zD": delete fold at cursor recursively */
5075 case 'd':
5076 case 'D': if (foldManualAllowed(FALSE))
5077 {
5078 if (VIsual_active)
5079 nv_operator(cap);
5080 else
5081 deleteFold(curwin->w_cursor.lnum,
5082 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5083 }
5084 break;
5085
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005086 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 case 'E': if (foldmethodIsManual(curwin))
5088 {
5089 clearFolding(curwin);
5090 changed_window_setting();
5091 }
5092 else if (foldmethodIsMarker(curwin))
5093 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5094 TRUE, FALSE);
5095 else
5096 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5097 break;
5098
5099 /* "zn": fold none: reset 'foldenable' */
5100 case 'n': curwin->w_p_fen = FALSE;
5101 break;
5102
5103 /* "zN": fold Normal: set 'foldenable' */
5104 case 'N': curwin->w_p_fen = TRUE;
5105 break;
5106
5107 /* "zi": invert folding: toggle 'foldenable' */
5108 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5109 break;
5110
5111 /* "za": open closed fold or close open fold at cursor */
5112 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5113 openFold(curwin->w_cursor.lnum, cap->count1);
5114 else
5115 {
5116 closeFold(curwin->w_cursor.lnum, cap->count1);
5117 curwin->w_p_fen = TRUE;
5118 }
5119 break;
5120
5121 /* "zA": open fold at cursor recursively */
5122 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5123 openFoldRecurse(curwin->w_cursor.lnum);
5124 else
5125 {
5126 closeFoldRecurse(curwin->w_cursor.lnum);
5127 curwin->w_p_fen = TRUE;
5128 }
5129 break;
5130
5131 /* "zo": open fold at cursor or Visual area */
5132 case 'o': if (VIsual_active)
5133 nv_operator(cap);
5134 else
5135 openFold(curwin->w_cursor.lnum, cap->count1);
5136 break;
5137
5138 /* "zO": open fold recursively */
5139 case 'O': if (VIsual_active)
5140 nv_operator(cap);
5141 else
5142 openFoldRecurse(curwin->w_cursor.lnum);
5143 break;
5144
5145 /* "zc": close fold at cursor or Visual area */
5146 case 'c': if (VIsual_active)
5147 nv_operator(cap);
5148 else
5149 closeFold(curwin->w_cursor.lnum, cap->count1);
5150 curwin->w_p_fen = TRUE;
5151 break;
5152
5153 /* "zC": close fold recursively */
5154 case 'C': if (VIsual_active)
5155 nv_operator(cap);
5156 else
5157 closeFoldRecurse(curwin->w_cursor.lnum);
5158 curwin->w_p_fen = TRUE;
5159 break;
5160
5161 /* "zv": open folds at the cursor */
5162 case 'v': foldOpenCursor();
5163 break;
5164
5165 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5166 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005167 curwin->w_foldinvalid = TRUE; /* recompute folds */
5168 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 foldOpenCursor();
5170 break;
5171
5172 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5173 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005174 curwin->w_foldinvalid = TRUE; /* recompute folds */
5175 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /* "zm": fold more */
5179 case 'm': if (curwin->w_p_fdl > 0)
5180 --curwin->w_p_fdl;
5181 old_fdl = -1; /* force an update */
5182 curwin->w_p_fen = TRUE;
5183 break;
5184
5185 /* "zM": close all folds */
5186 case 'M': curwin->w_p_fdl = 0;
5187 old_fdl = -1; /* force an update */
5188 curwin->w_p_fen = TRUE;
5189 break;
5190
5191 /* "zr": reduce folding */
5192 case 'r': ++curwin->w_p_fdl;
5193 break;
5194
5195 /* "zR": open all folds */
5196 case 'R': curwin->w_p_fdl = getDeepestNesting();
5197 old_fdl = -1; /* force an update */
5198 break;
5199
5200 case 'j': /* "zj" move to next fold downwards */
5201 case 'k': /* "zk" move to next fold upwards */
5202 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5203 cap->count1) == FAIL)
5204 clearopbeep(cap->oap);
5205 break;
5206
5207#endif /* FEAT_FOLDING */
5208
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005209#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005210 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5211 ++no_mapping;
5212 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005213 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005214 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005215 --no_mapping;
5216 --allow_keys;
5217#ifdef FEAT_CMDL_INFO
5218 (void)add_to_showcmd(nchar);
5219#endif
5220 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5221 {
5222 clearopbeep(cap->oap);
5223 break;
5224 }
5225 undo = TRUE;
5226 /*FALLTHROUGH*/
5227
Bram Moolenaarb765d632005-06-07 21:00:02 +00005228 case 'g': /* "zg": add good word to word list */
5229 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005230 case 'G': /* "zG": add good word to temp word list */
5231 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005232 {
5233 char_u *ptr = NULL;
5234 int len;
5235
5236 if (checkclearop(cap->oap))
5237 break;
5238# ifdef FEAT_VISUAL
5239 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5240 == FAIL)
5241 return;
5242# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005243 if (ptr == NULL)
5244 {
5245 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005246
Bram Moolenaar134bf072013-09-25 18:54:24 +02005247 /* Find bad word under the cursor. When 'spell' is
5248 * off this fails and find_ident_under_cursor() is
5249 * used below. */
5250 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005251 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005252 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005253 if (len != 0 && curwin->w_cursor.col <= pos.col)
5254 ptr = ml_get_pos(&curwin->w_cursor);
5255 curwin->w_cursor = pos;
5256 }
5257
Bram Moolenaarb765d632005-06-07 21:00:02 +00005258 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5259 FIND_IDENT)) == 0)
5260 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005261 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005262 (nchar == 'G' || nchar == 'W')
5263 ? 0 : (int)cap->count1,
5264 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005265 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005266 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005267
Bram Moolenaar43abc522005-12-10 20:15:02 +00005268 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005269 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005270 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005272#endif
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 default: clearopbeep(cap->oap);
5275 }
5276
5277#ifdef FEAT_FOLDING
5278 /* Redraw when 'foldenable' changed */
5279 if (old_fen != curwin->w_p_fen)
5280 {
5281# ifdef FEAT_DIFF
5282 win_T *wp;
5283
5284 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5285 {
5286 /* Adjust 'foldenable' in diff-synced windows. */
5287 FOR_ALL_WINDOWS(wp)
5288 {
5289 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5290 {
5291 wp->w_p_fen = curwin->w_p_fen;
5292 changed_window_setting_win(wp);
5293 }
5294 }
5295 }
5296# endif
5297 changed_window_setting();
5298 }
5299
5300 /* Redraw when 'foldlevel' changed. */
5301 if (old_fdl != curwin->w_p_fdl)
5302 newFoldLevel();
5303#endif
5304}
5305
5306#ifdef FEAT_GUI
5307/*
5308 * Vertical scrollbar movement.
5309 */
5310 static void
5311nv_ver_scrollbar(cap)
5312 cmdarg_T *cap;
5313{
5314 if (cap->oap->op_type != OP_NOP)
5315 clearopbeep(cap->oap);
5316
5317 /* Even if an operator was pending, we still want to scroll */
5318 gui_do_scroll();
5319}
5320
5321/*
5322 * Horizontal scrollbar movement.
5323 */
5324 static void
5325nv_hor_scrollbar(cap)
5326 cmdarg_T *cap;
5327{
5328 if (cap->oap->op_type != OP_NOP)
5329 clearopbeep(cap->oap);
5330
5331 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005332 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333}
5334#endif
5335
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005336#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337/*
5338 * Click in GUI tab.
5339 */
5340 static void
5341nv_tabline(cap)
5342 cmdarg_T *cap;
5343{
5344 if (cap->oap->op_type != OP_NOP)
5345 clearopbeep(cap->oap);
5346
5347 /* Even if an operator was pending, we still want to jump tabs. */
5348 goto_tabpage(current_tab);
5349}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005350
5351/*
5352 * Selected item in tab line menu.
5353 */
5354 static void
5355nv_tabmenu(cap)
5356 cmdarg_T *cap;
5357{
5358 if (cap->oap->op_type != OP_NOP)
5359 clearopbeep(cap->oap);
5360
5361 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005362 handle_tabmenu();
5363}
5364
5365/*
5366 * Handle selecting an item of the GUI tab line menu.
5367 * Used in Normal and Insert mode.
5368 */
5369 void
5370handle_tabmenu()
5371{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005372 switch (current_tabmenu)
5373 {
5374 case TABLINE_MENU_CLOSE:
5375 if (current_tab == 0)
5376 do_cmdline_cmd((char_u *)"tabclose");
5377 else
5378 {
5379 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5380 current_tab);
5381 do_cmdline_cmd(IObuff);
5382 }
5383 break;
5384
5385 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005386 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5387 current_tab > 0 ? current_tab - 1 : 999);
5388 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005389 break;
5390
5391 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005392 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5393 current_tab > 0 ? current_tab - 1 : 999);
5394 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005395 break;
5396 }
5397}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005398#endif
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400/*
5401 * "Q" command.
5402 */
5403 static void
5404nv_exmode(cap)
5405 cmdarg_T *cap;
5406{
5407 /*
5408 * Ignore 'Q' in Visual mode, just give a beep.
5409 */
5410#ifdef FEAT_VISUAL
5411 if (VIsual_active)
5412 vim_beep();
5413 else
5414#endif
5415 if (!checkclearop(cap->oap))
5416 do_exmode(FALSE);
5417}
5418
5419/*
5420 * Handle a ":" command.
5421 */
5422 static void
5423nv_colon(cap)
5424 cmdarg_T *cap;
5425{
5426 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005427 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429#ifdef FEAT_VISUAL
5430 if (VIsual_active)
5431 nv_operator(cap);
5432 else
5433#endif
5434 {
5435 if (cap->oap->op_type != OP_NOP)
5436 {
5437 /* Using ":" as a movement is characterwise exclusive. */
5438 cap->oap->motion_type = MCHAR;
5439 cap->oap->inclusive = FALSE;
5440 }
5441 else if (cap->count0)
5442 {
5443 /* translate "count:" into ":.,.+(count - 1)" */
5444 stuffcharReadbuff('.');
5445 if (cap->count0 > 1)
5446 {
5447 stuffReadbuff((char_u *)",.+");
5448 stuffnumReadbuff((long)cap->count0 - 1L);
5449 }
5450 }
5451
5452 /* When typing, don't type below an old message */
5453 if (KeyTyped)
5454 compute_cmdrow();
5455
5456 old_p_im = p_im;
5457
5458 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005459 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5461
5462 /* If 'insertmode' changed, enter or exit Insert mode */
5463 if (p_im != old_p_im)
5464 {
5465 if (p_im)
5466 restart_edit = 'i';
5467 else
5468 restart_edit = 0;
5469 }
5470
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005471 if (cmd_result == FAIL)
5472 /* The Ex command failed, do not execute the operator. */
5473 clearop(cap->oap);
5474 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5476 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005477 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5478 || did_emsg
5479 ))
5480 /* The start of the operator has become invalid by the Ex command.
5481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 clearopbeep(cap->oap);
5483 }
5484}
5485
5486/*
5487 * Handle CTRL-G command.
5488 */
5489 static void
5490nv_ctrlg(cap)
5491 cmdarg_T *cap;
5492{
5493#ifdef FEAT_VISUAL
5494 if (VIsual_active) /* toggle Selection/Visual mode */
5495 {
5496 VIsual_select = !VIsual_select;
5497 showmode();
5498 }
5499 else
5500#endif
5501 if (!checkclearop(cap->oap))
5502 /* print full name if count given or :cd used */
5503 fileinfo((int)cap->count0, FALSE, TRUE);
5504}
5505
5506/*
5507 * Handle CTRL-H <Backspace> command.
5508 */
5509 static void
5510nv_ctrlh(cap)
5511 cmdarg_T *cap;
5512{
5513#ifdef FEAT_VISUAL
5514 if (VIsual_active && VIsual_select)
5515 {
5516 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5517 v_visop(cap);
5518 }
5519 else
5520#endif
5521 nv_left(cap);
5522}
5523
5524/*
5525 * CTRL-L: clear screen and redraw.
5526 */
5527 static void
5528nv_clear(cap)
5529 cmdarg_T *cap;
5530{
5531 if (!checkclearop(cap->oap))
5532 {
5533#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5534 /*
5535 * Right now, the BeBox doesn't seem to have an easy way to detect
5536 * window resizing, so we cheat and make the user detect it
5537 * manually with CTRL-L instead
5538 */
5539 ui_get_shellsize();
5540#endif
5541#ifdef FEAT_SYN_HL
5542 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005543 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#endif
5545 redraw_later(CLEAR);
5546 }
5547}
5548
5549/*
5550 * CTRL-O: In Select mode: switch to Visual mode for one command.
5551 * Otherwise: Go to older pcmark.
5552 */
5553 static void
5554nv_ctrlo(cap)
5555 cmdarg_T *cap;
5556{
5557#ifdef FEAT_VISUAL
5558 if (VIsual_active && VIsual_select)
5559 {
5560 VIsual_select = FALSE;
5561 showmode();
5562 restart_VIsual_select = 2; /* restart Select mode later */
5563 }
5564 else
5565#endif
5566 {
5567 cap->count1 = -cap->count1;
5568 nv_pcmark(cap);
5569 }
5570}
5571
5572/*
5573 * CTRL-^ command, short for ":e #"
5574 */
5575 static void
5576nv_hat(cap)
5577 cmdarg_T *cap;
5578{
5579 if (!checkclearopq(cap->oap))
5580 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5581 GETF_SETMARK|GETF_ALT, FALSE);
5582}
5583
5584/*
5585 * "Z" commands.
5586 */
5587 static void
5588nv_Zet(cap)
5589 cmdarg_T *cap;
5590{
5591 if (!checkclearopq(cap->oap))
5592 {
5593 switch (cap->nchar)
5594 {
5595 /* "ZZ": equivalent to ":x". */
5596 case 'Z': do_cmdline_cmd((char_u *)"x");
5597 break;
5598
5599 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5600 case 'Q': do_cmdline_cmd((char_u *)"q!");
5601 break;
5602
5603 default: clearopbeep(cap->oap);
5604 }
5605 }
5606}
5607
5608#if defined(FEAT_WINDOWS) || defined(PROTO)
5609/*
5610 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5611 */
5612 void
5613do_nv_ident(c1, c2)
5614 int c1;
5615 int c2;
5616{
5617 oparg_T oa;
5618 cmdarg_T ca;
5619
5620 clear_oparg(&oa);
5621 vim_memset(&ca, 0, sizeof(ca));
5622 ca.oap = &oa;
5623 ca.cmdchar = c1;
5624 ca.nchar = c2;
5625 nv_ident(&ca);
5626}
5627#endif
5628
5629/*
5630 * Handle the commands that use the word under the cursor.
5631 * [g] CTRL-] :ta to current identifier
5632 * [g] 'K' run program for current identifier
5633 * [g] '*' / to current identifier or string
5634 * [g] '#' ? to current identifier or string
5635 * g ']' :tselect for current identifier
5636 */
5637 static void
5638nv_ident(cap)
5639 cmdarg_T *cap;
5640{
5641 char_u *ptr = NULL;
5642 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005643 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 char_u *p;
5645 char_u *kp; /* value of 'keywordprg' */
5646 int kp_help; /* 'keywordprg' is ":help" */
5647 int n = 0; /* init for GCC */
5648 int cmdchar;
5649 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005650 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 char_u *aux_ptr;
5652 int isman;
5653 int isman_s;
5654
5655 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5656 {
5657 cmdchar = cap->nchar;
5658 g_cmd = TRUE;
5659 }
5660 else
5661 {
5662 cmdchar = cap->cmdchar;
5663 g_cmd = FALSE;
5664 }
5665
5666 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5667 cmdchar = '#';
5668
5669 /*
5670 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5671 */
5672 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5673 {
5674#ifdef FEAT_VISUAL
5675 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5676 return;
5677#endif
5678 if (checkclearopq(cap->oap))
5679 return;
5680 }
5681
5682 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5683 (cmdchar == '*' || cmdchar == '#')
5684 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5685 {
5686 clearop(cap->oap);
5687 return;
5688 }
5689
5690 /* Allocate buffer to put the command in. Inserting backslashes can
5691 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5692 * and some numbers. */
5693 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5694 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5695 || STRCMP(kp, ":help") == 0);
5696 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5697 if (buf == NULL)
5698 return;
5699 buf[0] = NUL;
5700
5701 switch (cmdchar)
5702 {
5703 case '*':
5704 case '#':
5705 /*
5706 * Put cursor at start of word, makes search skip the word
5707 * under the cursor.
5708 * Call setpcmark() first, so "*``" puts the cursor back where
5709 * it was.
5710 */
5711 setpcmark();
5712 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5713
5714 if (!g_cmd && vim_iswordp(ptr))
5715 STRCPY(buf, "\\<");
5716 no_smartcase = TRUE; /* don't use 'smartcase' now */
5717 break;
5718
5719 case 'K':
5720 if (kp_help)
5721 STRCPY(buf, "he! ");
5722 else
5723 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005724 /* An external command will probably use an argument starting
5725 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005726 while (*ptr == '-' && n > 0)
5727 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005728 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005729 --n;
5730 }
5731 if (n == 0)
5732 {
5733 EMSG(_(e_noident)); /* found dashes only */
5734 vim_free(buf);
5735 return;
5736 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 /* When a count is given, turn it into a range. Is this
5739 * really what we want? */
5740 isman = (STRCMP(kp, "man") == 0);
5741 isman_s = (STRCMP(kp, "man -s") == 0);
5742 if (cap->count0 != 0 && !(isman || isman_s))
5743 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5744
5745 STRCAT(buf, "! ");
5746 if (cap->count0 == 0 && isman_s)
5747 STRCAT(buf, "man");
5748 else
5749 STRCAT(buf, kp);
5750 STRCAT(buf, " ");
5751 if (cap->count0 != 0 && (isman || isman_s))
5752 {
5753 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5754 STRCAT(buf, " ");
5755 }
5756 }
5757 break;
5758
5759 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005760 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#ifdef FEAT_CSCOPE
5762 if (p_cst)
5763 STRCPY(buf, "cstag ");
5764 else
5765#endif
5766 STRCPY(buf, "ts ");
5767 break;
5768
5769 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005770 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (curbuf->b_help)
5772 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005774 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005775 if (g_cmd)
5776 STRCPY(buf, "tj ");
5777 else
5778 sprintf((char *)buf, "%ldta ", cap->count0);
5779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781
5782 /*
5783 * Now grab the chars in the identifier
5784 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005785 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005787 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005788 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005789 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005790 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005791 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005793 vim_free(buf);
5794 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005796 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5797 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005798 {
5799 vim_free(buf);
5800 vim_free(p);
5801 return;
5802 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005803 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005804 STRCAT(buf, p);
5805 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005807 else
5808 {
5809 if (cmdchar == '*')
5810 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5811 else if (cmdchar == '#')
5812 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005813 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005814 {
5815 if (curbuf->b_help)
5816 /* ":help" handles unescaped argument */
5817 aux_ptr = (char_u *)"";
5818 else
5819 aux_ptr = (char_u *)"\\|\"\n[";
5820 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005821 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005822 aux_ptr = (char_u *)"\\|\"\n*?[";
5823
5824 p = buf + STRLEN(buf);
5825 while (n-- > 0)
5826 {
5827 /* put a backslash before \ and some others */
5828 if (vim_strchr(aux_ptr, *ptr) != NULL)
5829 *p++ = '\\';
5830#ifdef FEAT_MBYTE
5831 /* When current byte is a part of multibyte character, copy all
5832 * bytes of that character. */
5833 if (has_mbyte)
5834 {
5835 int i;
5836 int len = (*mb_ptr2len)(ptr) - 1;
5837
5838 for (i = 0; i < len && n >= 1; ++i, --n)
5839 *p++ = *ptr++;
5840 }
5841#endif
5842 *p++ = *ptr++;
5843 }
5844 *p = NUL;
5845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 /*
5848 * Execute the command.
5849 */
5850 if (cmdchar == '*' || cmdchar == '#')
5851 {
5852 if (!g_cmd && (
5853#ifdef FEAT_MBYTE
5854 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5855#endif
5856 vim_iswordc(ptr[-1])))
5857 STRCAT(buf, "\\>");
5858#ifdef FEAT_CMDHIST
5859 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005860 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5862#endif
5863 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5864 }
5865 else
5866 do_cmdline_cmd(buf);
5867
5868 vim_free(buf);
5869}
5870
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005871#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872/*
5873 * Get visually selected text, within one line only.
5874 * Returns FAIL if more than one line selected.
5875 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005876 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877get_visual_text(cap, pp, lenp)
5878 cmdarg_T *cap;
5879 char_u **pp; /* return: start of selected text */
5880 int *lenp; /* return: length of selected text */
5881{
5882 if (VIsual_mode != 'V')
5883 unadjust_for_sel();
5884 if (VIsual.lnum != curwin->w_cursor.lnum)
5885 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005886 if (cap != NULL)
5887 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 return FAIL;
5889 }
5890 if (VIsual_mode == 'V')
5891 {
5892 *pp = ml_get_curline();
5893 *lenp = (int)STRLEN(*pp);
5894 }
5895 else
5896 {
5897 if (lt(curwin->w_cursor, VIsual))
5898 {
5899 *pp = ml_get_pos(&curwin->w_cursor);
5900 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5901 }
5902 else
5903 {
5904 *pp = ml_get_pos(&VIsual);
5905 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5906 }
5907#ifdef FEAT_MBYTE
5908 if (has_mbyte)
5909 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005910 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#endif
5912 }
5913 reset_VIsual_and_resel();
5914 return OK;
5915}
5916#endif
5917
5918/*
5919 * CTRL-T: backwards in tag stack
5920 */
5921 static void
5922nv_tagpop(cap)
5923 cmdarg_T *cap;
5924{
5925 if (!checkclearopq(cap->oap))
5926 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5927}
5928
5929/*
5930 * Handle scrolling command 'H', 'L' and 'M'.
5931 */
5932 static void
5933nv_scroll(cap)
5934 cmdarg_T *cap;
5935{
5936 int used = 0;
5937 long n;
5938#ifdef FEAT_FOLDING
5939 linenr_T lnum;
5940#endif
5941 int half;
5942
5943 cap->oap->motion_type = MLINE;
5944 setpcmark();
5945
5946 if (cap->cmdchar == 'L')
5947 {
5948 validate_botline(); /* make sure curwin->w_botline is valid */
5949 curwin->w_cursor.lnum = curwin->w_botline - 1;
5950 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5951 curwin->w_cursor.lnum = 1;
5952 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005953 {
5954#ifdef FEAT_FOLDING
5955 if (hasAnyFolding(curwin))
5956 {
5957 /* Count a fold for one screen line. */
5958 for (n = cap->count1 - 1; n > 0
5959 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5960 {
5961 (void)hasFolding(curwin->w_cursor.lnum,
5962 &curwin->w_cursor.lnum, NULL);
5963 --curwin->w_cursor.lnum;
5964 }
5965 }
5966 else
5967#endif
5968 curwin->w_cursor.lnum -= cap->count1 - 1;
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 }
5971 else
5972 {
5973 if (cap->cmdchar == 'M')
5974 {
5975#ifdef FEAT_DIFF
5976 /* Don't count filler lines above the window. */
5977 used -= diff_check_fill(curwin, curwin->w_topline)
5978 - curwin->w_topfill;
5979#endif
5980 validate_botline(); /* make sure w_empty_rows is valid */
5981 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5982 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5983 {
5984#ifdef FEAT_DIFF
5985 /* Count half he number of filler lines to be "below this
5986 * line" and half to be "above the next line". */
5987 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5988 + n) / 2 >= half)
5989 {
5990 --n;
5991 break;
5992 }
5993#endif
5994 used += plines(curwin->w_topline + n);
5995 if (used >= half)
5996 break;
5997#ifdef FEAT_FOLDING
5998 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5999 n = lnum - curwin->w_topline;
6000#endif
6001 }
6002 if (n > 0 && used > curwin->w_height)
6003 --n;
6004 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006005 else /* (cap->cmdchar == 'H') */
6006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006008#ifdef FEAT_FOLDING
6009 if (hasAnyFolding(curwin))
6010 {
6011 /* Count a fold for one screen line. */
6012 lnum = curwin->w_topline;
6013 while (n-- > 0 && lnum < curwin->w_botline - 1)
6014 {
6015 hasFolding(lnum, NULL, &lnum);
6016 ++lnum;
6017 }
6018 n = lnum - curwin->w_topline;
6019 }
6020#endif
6021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 curwin->w_cursor.lnum = curwin->w_topline + n;
6023 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6024 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6025 }
6026
6027 cursor_correct(); /* correct for 'so' */
6028 beginline(BL_SOL | BL_FIX);
6029}
6030
6031/*
6032 * Cursor right commands.
6033 */
6034 static void
6035nv_right(cap)
6036 cmdarg_T *cap;
6037{
6038 long n;
6039#ifdef FEAT_VISUAL
6040 int PAST_LINE;
6041#else
6042# define PAST_LINE 0
6043#endif
6044
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006045 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6046 {
6047 /* <C-Right> and <S-Right> move a word or WORD right */
6048 if (mod_mask & MOD_MASK_CTRL)
6049 cap->arg = TRUE;
6050 nv_wordcmd(cap);
6051 return;
6052 }
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 cap->oap->motion_type = MCHAR;
6055 cap->oap->inclusive = FALSE;
6056#ifdef FEAT_VISUAL
6057 PAST_LINE = (VIsual_active && *p_sel != 'o');
6058
6059# ifdef FEAT_VIRTUALEDIT
6060 /*
6061 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006062 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 */
6064 if (virtual_active())
6065 PAST_LINE = 0;
6066# endif
6067#endif
6068
6069 for (n = cap->count1; n > 0; --n)
6070 {
6071 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006072#ifdef FEAT_VISUAL
6073 || (PAST_LINE && *ml_get_cursor() == NUL)
6074#endif
6075 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006078 * <Space> wraps to next line if 'whichwrap' has 's'.
6079 * 'l' wraps to next line if 'whichwrap' has 'l'.
6080 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 */
6082 if ( ((cap->cmdchar == ' '
6083 && vim_strchr(p_ww, 's') != NULL)
6084 || (cap->cmdchar == 'l'
6085 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006086 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 && vim_strchr(p_ww, '>') != NULL))
6088 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6089 {
6090 /* When deleting we also count the NL as a character.
6091 * Set cap->oap->inclusive when last char in the line is
6092 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006093 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && !cap->oap->inclusive
6095 && !lineempty(curwin->w_cursor.lnum))
6096 cap->oap->inclusive = TRUE;
6097 else
6098 {
6099 ++curwin->w_cursor.lnum;
6100 curwin->w_cursor.col = 0;
6101#ifdef FEAT_VIRTUALEDIT
6102 curwin->w_cursor.coladd = 0;
6103#endif
6104 curwin->w_set_curswant = TRUE;
6105 cap->oap->inclusive = FALSE;
6106 }
6107 continue;
6108 }
6109 if (cap->oap->op_type == OP_NOP)
6110 {
6111 /* Only beep and flush if not moved at all */
6112 if (n == cap->count1)
6113 beep_flush();
6114 }
6115 else
6116 {
6117 if (!lineempty(curwin->w_cursor.lnum))
6118 cap->oap->inclusive = TRUE;
6119 }
6120 break;
6121 }
6122#ifdef FEAT_VISUAL
6123 else if (PAST_LINE)
6124 {
6125 curwin->w_set_curswant = TRUE;
6126# ifdef FEAT_VIRTUALEDIT
6127 if (virtual_active())
6128 oneright();
6129 else
6130# endif
6131 {
6132# ifdef FEAT_MBYTE
6133 if (has_mbyte)
6134 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006135 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 else
6137# endif
6138 ++curwin->w_cursor.col;
6139 }
6140 }
6141#endif
6142 }
6143#ifdef FEAT_FOLDING
6144 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6145 && cap->oap->op_type == OP_NOP)
6146 foldOpenCursor();
6147#endif
6148}
6149
6150/*
6151 * Cursor left commands.
6152 *
6153 * Returns TRUE when operator end should not be adjusted.
6154 */
6155 static void
6156nv_left(cap)
6157 cmdarg_T *cap;
6158{
6159 long n;
6160
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006161 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6162 {
6163 /* <C-Left> and <S-Left> move a word or WORD left */
6164 if (mod_mask & MOD_MASK_CTRL)
6165 cap->arg = 1;
6166 nv_bck_word(cap);
6167 return;
6168 }
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 cap->oap->motion_type = MCHAR;
6171 cap->oap->inclusive = FALSE;
6172 for (n = cap->count1; n > 0; --n)
6173 {
6174 if (oneleft() == FAIL)
6175 {
6176 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6177 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6178 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6179 */
6180 if ( (((cap->cmdchar == K_BS
6181 || cap->cmdchar == Ctrl_H)
6182 && vim_strchr(p_ww, 'b') != NULL)
6183 || (cap->cmdchar == 'h'
6184 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006185 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 && vim_strchr(p_ww, '<') != NULL))
6187 && curwin->w_cursor.lnum > 1)
6188 {
6189 --(curwin->w_cursor.lnum);
6190 coladvance((colnr_T)MAXCOL);
6191 curwin->w_set_curswant = TRUE;
6192
6193 /* When the NL before the first char has to be deleted we
6194 * put the cursor on the NUL after the previous line.
6195 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006196 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 if ( (cap->oap->op_type == OP_DELETE
6198 || cap->oap->op_type == OP_CHANGE)
6199 && !lineempty(curwin->w_cursor.lnum))
6200 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006201 if (*ml_get_cursor() != NUL)
6202 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 cap->retval |= CA_NO_ADJ_OP_END;
6204 }
6205 continue;
6206 }
6207 /* Only beep and flush if not moved at all */
6208 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6209 beep_flush();
6210 break;
6211 }
6212 }
6213#ifdef FEAT_FOLDING
6214 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6215 && cap->oap->op_type == OP_NOP)
6216 foldOpenCursor();
6217#endif
6218}
6219
6220/*
6221 * Cursor up commands.
6222 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6223 */
6224 static void
6225nv_up(cap)
6226 cmdarg_T *cap;
6227{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006228 if (mod_mask & MOD_MASK_SHIFT)
6229 {
6230 /* <S-Up> is page up */
6231 cap->arg = BACKWARD;
6232 nv_page(cap);
6233 }
6234 else
6235 {
6236 cap->oap->motion_type = MLINE;
6237 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6238 clearopbeep(cap->oap);
6239 else if (cap->arg)
6240 beginline(BL_WHITE | BL_FIX);
6241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242}
6243
6244/*
6245 * Cursor down commands.
6246 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6247 */
6248 static void
6249nv_down(cap)
6250 cmdarg_T *cap;
6251{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006252 if (mod_mask & MOD_MASK_SHIFT)
6253 {
6254 /* <S-Down> is page down */
6255 cap->arg = FORWARD;
6256 nv_page(cap);
6257 }
6258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6260 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006261 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006262 if (curwin->w_llist_ref == NULL)
6263 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6264 else
6265 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 else
6267#endif
6268 {
6269#ifdef FEAT_CMDWIN
6270 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006271 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 cmdwin_result = CAR;
6273 else
6274#endif
6275 {
6276 cap->oap->motion_type = MLINE;
6277 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6278 clearopbeep(cap->oap);
6279 else if (cap->arg)
6280 beginline(BL_WHITE | BL_FIX);
6281 }
6282 }
6283}
6284
6285#ifdef FEAT_SEARCHPATH
6286/*
6287 * Grab the file name under the cursor and edit it.
6288 */
6289 static void
6290nv_gotofile(cap)
6291 cmdarg_T *cap;
6292{
6293 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006294 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006296 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 {
6298 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006299 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 return;
6301 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006302#ifdef FEAT_AUTOCMD
6303 if (curbuf_locked())
6304 {
6305 clearop(cap->oap);
6306 return;
6307 }
6308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006310 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311
6312 if (ptr != NULL)
6313 {
6314 /* do autowrite if necessary */
6315 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6316 autowrite(curbuf, FALSE);
6317 setpcmark();
6318 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006319 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006320 if (cap->nchar == 'F' && lnum >= 0)
6321 {
6322 curwin->w_cursor.lnum = lnum;
6323 check_cursor_lnum();
6324 beginline(BL_SOL | BL_FIX);
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 vim_free(ptr);
6327 }
6328 else
6329 clearop(cap->oap);
6330}
6331#endif
6332
6333/*
6334 * <End> command: to end of current line or last line.
6335 */
6336 static void
6337nv_end(cap)
6338 cmdarg_T *cap;
6339{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006340 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006342 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 nv_goto(cap);
6344 cap->count1 = 1; /* to end of current line */
6345 }
6346 nv_dollar(cap);
6347}
6348
6349/*
6350 * Handle the "$" command.
6351 */
6352 static void
6353nv_dollar(cap)
6354 cmdarg_T *cap;
6355{
6356 cap->oap->motion_type = MCHAR;
6357 cap->oap->inclusive = TRUE;
6358#ifdef FEAT_VIRTUALEDIT
6359 /* In virtual mode when off the edge of a line and an operator
6360 * is pending (whew!) keep the cursor where it is.
6361 * Otherwise, send it to the end of the line. */
6362 if (!virtual_active() || gchar_cursor() != NUL
6363 || cap->oap->op_type == OP_NOP)
6364#endif
6365 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6366 if (cursor_down((long)(cap->count1 - 1),
6367 cap->oap->op_type == OP_NOP) == FAIL)
6368 clearopbeep(cap->oap);
6369#ifdef FEAT_FOLDING
6370 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6371 foldOpenCursor();
6372#endif
6373}
6374
6375/*
6376 * Implementation of '?' and '/' commands.
6377 * If cap->arg is TRUE don't set PC mark.
6378 */
6379 static void
6380nv_search(cap)
6381 cmdarg_T *cap;
6382{
6383 oparg_T *oap = cap->oap;
6384
6385 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6386 {
6387 /* Translate "g??" to "g?g?" */
6388 cap->cmdchar = 'g';
6389 cap->nchar = '?';
6390 nv_operator(cap);
6391 return;
6392 }
6393
6394 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6395
6396 if (cap->searchbuf == NULL)
6397 {
6398 clearop(oap);
6399 return;
6400 }
6401
6402 normal_search(cap, cap->cmdchar, cap->searchbuf,
6403 (cap->arg ? 0 : SEARCH_MARK));
6404}
6405
6406/*
6407 * Handle "N" and "n" commands.
6408 * cap->arg is SEARCH_REV for "N", 0 for "n".
6409 */
6410 static void
6411nv_next(cap)
6412 cmdarg_T *cap;
6413{
6414 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6415}
6416
6417/*
6418 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6419 * Uses only cap->count1 and cap->oap from "cap".
6420 */
6421 static void
6422normal_search(cap, dir, pat, opt)
6423 cmdarg_T *cap;
6424 int dir;
6425 char_u *pat;
6426 int opt; /* extra flags for do_search() */
6427{
6428 int i;
6429
6430 cap->oap->motion_type = MCHAR;
6431 cap->oap->inclusive = FALSE;
6432 cap->oap->use_reg_one = TRUE;
6433 curwin->w_set_curswant = TRUE;
6434
6435 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006436 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (i == 0)
6438 clearop(cap->oap);
6439 else
6440 {
6441 if (i == 2)
6442 cap->oap->motion_type = MLINE;
6443#ifdef FEAT_VIRTUALEDIT
6444 curwin->w_cursor.coladd = 0;
6445#endif
6446#ifdef FEAT_FOLDING
6447 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6448 foldOpenCursor();
6449#endif
6450 }
6451
6452 /* "/$" will put the cursor after the end of the line, may need to
6453 * correct that here */
6454 check_cursor();
6455}
6456
6457/*
6458 * Character search commands.
6459 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6460 * ',' and FALSE for ';'.
6461 * cap->nchar is NUL for ',' and ';' (repeat the search)
6462 */
6463 static void
6464nv_csearch(cap)
6465 cmdarg_T *cap;
6466{
6467 int t_cmd;
6468
6469 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6470 t_cmd = TRUE;
6471 else
6472 t_cmd = FALSE;
6473
6474 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6476 clearopbeep(cap->oap);
6477 else
6478 {
6479 curwin->w_set_curswant = TRUE;
6480#ifdef FEAT_VIRTUALEDIT
6481 /* Include a Tab for "tx" and for "dfx". */
6482 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6483 && (t_cmd || cap->oap->op_type != OP_NOP))
6484 {
6485 colnr_T scol, ecol;
6486
6487 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6488 curwin->w_cursor.coladd = ecol - scol;
6489 }
6490 else
6491 curwin->w_cursor.coladd = 0;
6492#endif
6493#ifdef FEAT_VISUAL
6494 adjust_for_sel(cap);
6495#endif
6496#ifdef FEAT_FOLDING
6497 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6498 foldOpenCursor();
6499#endif
6500 }
6501}
6502
6503/*
6504 * "[" and "]" commands.
6505 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6506 */
6507 static void
6508nv_brackets(cap)
6509 cmdarg_T *cap;
6510{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006511 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 pos_T prev_pos;
6513 pos_T *pos = NULL; /* init for GCC */
6514 pos_T old_pos; /* cursor position before command */
6515 int flag;
6516 long n;
6517 int findc;
6518 int c;
6519
6520 cap->oap->motion_type = MCHAR;
6521 cap->oap->inclusive = FALSE;
6522 old_pos = curwin->w_cursor;
6523#ifdef FEAT_VIRTUALEDIT
6524 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6525#endif
6526
6527#ifdef FEAT_SEARCHPATH
6528 /*
6529 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6530 */
6531 if (cap->nchar == 'f')
6532 nv_gotofile(cap);
6533 else
6534#endif
6535
6536#ifdef FEAT_FIND_ID
6537 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006538 * Find the occurrence(s) of the identifier or define under cursor
6539 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 *
6541 * search list jump
6542 * fwd bwd fwd bwd fwd bwd
6543 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6544 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6545 */
6546 if (vim_strchr((char_u *)
6547#ifdef EBCDIC
6548 "iI\005dD\067",
6549#else
6550 "iI\011dD\004",
6551#endif
6552 cap->nchar) != NULL)
6553 {
6554 char_u *ptr;
6555 int len;
6556
6557 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6558 clearop(cap->oap);
6559 else
6560 {
6561 find_pattern_in_path(ptr, 0, len, TRUE,
6562 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6563 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6564 cap->count1,
6565 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6566 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6567 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6568 (linenr_T)MAXLNUM);
6569 curwin->w_set_curswant = TRUE;
6570 }
6571 }
6572 else
6573#endif
6574
6575 /*
6576 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6577 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6578 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6579 * "[m" or "]m" search for prev/next start of (Java) method.
6580 * "[M" or "]M" search for prev/next end of (Java) method.
6581 */
6582 if ( (cap->cmdchar == '['
6583 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6584 || (cap->cmdchar == ']'
6585 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6586 {
6587 if (cap->nchar == '*')
6588 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 prev_pos.lnum = 0;
6590 if (cap->nchar == 'm' || cap->nchar == 'M')
6591 {
6592 if (cap->cmdchar == '[')
6593 findc = '{';
6594 else
6595 findc = '}';
6596 n = 9999;
6597 }
6598 else
6599 {
6600 findc = cap->nchar;
6601 n = cap->count1;
6602 }
6603 for ( ; n > 0; --n)
6604 {
6605 if ((pos = findmatchlimit(cap->oap, findc,
6606 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6607 {
6608 if (new_pos.lnum == 0) /* nothing found */
6609 {
6610 if (cap->nchar != 'm' && cap->nchar != 'M')
6611 clearopbeep(cap->oap);
6612 }
6613 else
6614 pos = &new_pos; /* use last one found */
6615 break;
6616 }
6617 prev_pos = new_pos;
6618 curwin->w_cursor = *pos;
6619 new_pos = *pos;
6620 }
6621 curwin->w_cursor = old_pos;
6622
6623 /*
6624 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6625 * brought us to the match for "[m" and "]M" when inside a method.
6626 * Try finding the '{' or '}' we want to be at.
6627 * Also repeat for the given count.
6628 */
6629 if (cap->nchar == 'm' || cap->nchar == 'M')
6630 {
6631 /* norm is TRUE for "]M" and "[m" */
6632 int norm = ((findc == '{') == (cap->nchar == 'm'));
6633
6634 n = cap->count1;
6635 /* found a match: we were inside a method */
6636 if (prev_pos.lnum != 0)
6637 {
6638 pos = &prev_pos;
6639 curwin->w_cursor = prev_pos;
6640 if (norm)
6641 --n;
6642 }
6643 else
6644 pos = NULL;
6645 while (n > 0)
6646 {
6647 for (;;)
6648 {
6649 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6650 {
6651 /* if not found anything, that's an error */
6652 if (pos == NULL)
6653 clearopbeep(cap->oap);
6654 n = 0;
6655 break;
6656 }
6657 c = gchar_cursor();
6658 if (c == '{' || c == '}')
6659 {
6660 /* Must have found end/start of class: use it.
6661 * Or found the place to be at. */
6662 if ((c == findc && norm) || (n == 1 && !norm))
6663 {
6664 new_pos = curwin->w_cursor;
6665 pos = &new_pos;
6666 n = 0;
6667 }
6668 /* if no match found at all, we started outside of the
6669 * class and we're inside now. Just go on. */
6670 else if (new_pos.lnum == 0)
6671 {
6672 new_pos = curwin->w_cursor;
6673 pos = &new_pos;
6674 }
6675 /* found start/end of other method: go to match */
6676 else if ((pos = findmatchlimit(cap->oap, findc,
6677 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6678 0)) == NULL)
6679 n = 0;
6680 else
6681 curwin->w_cursor = *pos;
6682 break;
6683 }
6684 }
6685 --n;
6686 }
6687 curwin->w_cursor = old_pos;
6688 if (pos == NULL && new_pos.lnum != 0)
6689 clearopbeep(cap->oap);
6690 }
6691 if (pos != NULL)
6692 {
6693 setpcmark();
6694 curwin->w_cursor = *pos;
6695 curwin->w_set_curswant = TRUE;
6696#ifdef FEAT_FOLDING
6697 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6698 && cap->oap->op_type == OP_NOP)
6699 foldOpenCursor();
6700#endif
6701 }
6702 }
6703
6704 /*
6705 * "[[", "[]", "]]" and "][": move to start or end of function
6706 */
6707 else if (cap->nchar == '[' || cap->nchar == ']')
6708 {
6709 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6710 flag = '{';
6711 else
6712 flag = '}'; /* "][" or "[]" */
6713
6714 curwin->w_set_curswant = TRUE;
6715 /*
6716 * Imitate strange Vi behaviour: When using "]]" with an operator
6717 * we also stop at '}'.
6718 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006719 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 (cap->oap->op_type != OP_NOP
6721 && cap->arg == FORWARD && flag == '{')))
6722 clearopbeep(cap->oap);
6723 else
6724 {
6725 if (cap->oap->op_type == OP_NOP)
6726 beginline(BL_WHITE | BL_FIX);
6727#ifdef FEAT_FOLDING
6728 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6729 foldOpenCursor();
6730#endif
6731 }
6732 }
6733
6734 /*
6735 * "[p", "[P", "]P" and "]p": put with indent adjustment
6736 */
6737 else if (cap->nchar == 'p' || cap->nchar == 'P')
6738 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006739 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 {
6741 prep_redo_cmd(cap);
6742 do_put(cap->oap->regname,
6743 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6744 cap->count1, PUT_FIXINDENT);
6745 }
6746 }
6747
6748 /*
6749 * "['", "[`", "]'" and "]`": jump to next mark
6750 */
6751 else if (cap->nchar == '\'' || cap->nchar == '`')
6752 {
6753 pos = &curwin->w_cursor;
6754 for (n = cap->count1; n > 0; --n)
6755 {
6756 prev_pos = *pos;
6757 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6758 cap->nchar == '\'');
6759 if (pos == NULL)
6760 break;
6761 }
6762 if (pos == NULL)
6763 pos = &prev_pos;
6764 nv_cursormark(cap, cap->nchar == '\'', pos);
6765 }
6766
6767#ifdef FEAT_MOUSE
6768 /*
6769 * [ or ] followed by a middle mouse click: put selected text with
6770 * indent adjustment. Any other button just does as usual.
6771 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006772 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
6774 (void)do_mouse(cap->oap, cap->nchar,
6775 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6776 cap->count1, PUT_FIXINDENT);
6777 }
6778#endif /* FEAT_MOUSE */
6779
6780#ifdef FEAT_FOLDING
6781 /*
6782 * "[z" and "]z": move to start or end of open fold.
6783 */
6784 else if (cap->nchar == 'z')
6785 {
6786 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6787 cap->count1) == FAIL)
6788 clearopbeep(cap->oap);
6789 }
6790#endif
6791
6792#ifdef FEAT_DIFF
6793 /*
6794 * "[c" and "]c": move to next or previous diff-change.
6795 */
6796 else if (cap->nchar == 'c')
6797 {
6798 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6799 cap->count1) == FAIL)
6800 clearopbeep(cap->oap);
6801 }
6802#endif
6803
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006804#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006805 /*
6806 * "[s", "[S", "]s" and "]S": move to next spell error.
6807 */
6808 else if (cap->nchar == 's' || cap->nchar == 'S')
6809 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006810 setpcmark();
6811 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006812 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6813 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006814 {
6815 clearopbeep(cap->oap);
6816 break;
6817 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818# ifdef FEAT_FOLDING
6819 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6820 foldOpenCursor();
6821# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006822 }
6823#endif
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 /* Not a valid cap->nchar. */
6826 else
6827 clearopbeep(cap->oap);
6828}
6829
6830/*
6831 * Handle Normal mode "%" command.
6832 */
6833 static void
6834nv_percent(cap)
6835 cmdarg_T *cap;
6836{
6837 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006838#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 linenr_T lnum = curwin->w_cursor.lnum;
6840#endif
6841
6842 cap->oap->inclusive = TRUE;
6843 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6844 {
6845 if (cap->count0 > 100)
6846 clearopbeep(cap->oap);
6847 else
6848 {
6849 cap->oap->motion_type = MLINE;
6850 setpcmark();
6851 /* Round up, so CTRL-G will give same value. Watch out for a
6852 * large line count, the line number must not go negative! */
6853 if (curbuf->b_ml.ml_line_count > 1000000)
6854 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6855 / 100L * cap->count0;
6856 else
6857 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6858 cap->count0 + 99L) / 100L;
6859 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6861 beginline(BL_SOL | BL_FIX);
6862 }
6863 }
6864 else /* "%" : go to matching paren */
6865 {
6866 cap->oap->motion_type = MCHAR;
6867 cap->oap->use_reg_one = TRUE;
6868 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6869 clearopbeep(cap->oap);
6870 else
6871 {
6872 setpcmark();
6873 curwin->w_cursor = *pos;
6874 curwin->w_set_curswant = TRUE;
6875#ifdef FEAT_VIRTUALEDIT
6876 curwin->w_cursor.coladd = 0;
6877#endif
6878#ifdef FEAT_VISUAL
6879 adjust_for_sel(cap);
6880#endif
6881 }
6882 }
6883#ifdef FEAT_FOLDING
6884 if (cap->oap->op_type == OP_NOP
6885 && lnum != curwin->w_cursor.lnum
6886 && (fdo_flags & FDO_PERCENT)
6887 && KeyTyped)
6888 foldOpenCursor();
6889#endif
6890}
6891
6892/*
6893 * Handle "(" and ")" commands.
6894 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6895 */
6896 static void
6897nv_brace(cap)
6898 cmdarg_T *cap;
6899{
6900 cap->oap->motion_type = MCHAR;
6901 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006902 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6903 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 curwin->w_set_curswant = TRUE;
6905
6906 if (findsent(cap->arg, cap->count1) == FAIL)
6907 clearopbeep(cap->oap);
6908 else
6909 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006910 /* Don't leave the cursor on the NUL past end of line. */
6911 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912#ifdef FEAT_VIRTUALEDIT
6913 curwin->w_cursor.coladd = 0;
6914#endif
6915#ifdef FEAT_FOLDING
6916 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6917 foldOpenCursor();
6918#endif
6919 }
6920}
6921
6922/*
6923 * "m" command: Mark a position.
6924 */
6925 static void
6926nv_mark(cap)
6927 cmdarg_T *cap;
6928{
6929 if (!checkclearop(cap->oap))
6930 {
6931 if (setmark(cap->nchar) == FAIL)
6932 clearopbeep(cap->oap);
6933 }
6934}
6935
6936/*
6937 * "{" and "}" commands.
6938 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6939 */
6940 static void
6941nv_findpar(cap)
6942 cmdarg_T *cap;
6943{
6944 cap->oap->motion_type = MCHAR;
6945 cap->oap->inclusive = FALSE;
6946 cap->oap->use_reg_one = TRUE;
6947 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006948 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 clearopbeep(cap->oap);
6950 else
6951 {
6952#ifdef FEAT_VIRTUALEDIT
6953 curwin->w_cursor.coladd = 0;
6954#endif
6955#ifdef FEAT_FOLDING
6956 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6957 foldOpenCursor();
6958#endif
6959 }
6960}
6961
6962/*
6963 * "u" command: Undo or make lower case.
6964 */
6965 static void
6966nv_undo(cap)
6967 cmdarg_T *cap;
6968{
6969 if (cap->oap->op_type == OP_LOWER
6970#ifdef FEAT_VISUAL
6971 || VIsual_active
6972#endif
6973 )
6974 {
6975 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6976 cap->cmdchar = 'g';
6977 cap->nchar = 'u';
6978 nv_operator(cap);
6979 }
6980 else
6981 nv_kundo(cap);
6982}
6983
6984/*
6985 * <Undo> command.
6986 */
6987 static void
6988nv_kundo(cap)
6989 cmdarg_T *cap;
6990{
6991 if (!checkclearopq(cap->oap))
6992 {
6993 u_undo((int)cap->count1);
6994 curwin->w_set_curswant = TRUE;
6995 }
6996}
6997
6998/*
6999 * Handle the "r" command.
7000 */
7001 static void
7002nv_replace(cap)
7003 cmdarg_T *cap;
7004{
7005 char_u *ptr;
7006 int had_ctrl_v;
7007 long n;
7008
7009 if (checkclearop(cap->oap))
7010 return;
7011
7012 /* get another character */
7013 if (cap->nchar == Ctrl_V)
7014 {
7015 had_ctrl_v = Ctrl_V;
7016 cap->nchar = get_literal();
7017 /* Don't redo a multibyte character with CTRL-V. */
7018 if (cap->nchar > DEL)
7019 had_ctrl_v = NUL;
7020 }
7021 else
7022 had_ctrl_v = NUL;
7023
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007024 /* Abort if the character is a special key. */
7025 if (IS_SPECIAL(cap->nchar))
7026 {
7027 clearopbeep(cap->oap);
7028 return;
7029 }
7030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031#ifdef FEAT_VISUAL
7032 /* Visual mode "r" */
7033 if (VIsual_active)
7034 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007035 if (got_int)
7036 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007037 if (had_ctrl_v)
7038 {
7039 if (cap->nchar == '\r')
7040 cap->nchar = -1;
7041 else if (cap->nchar == '\n')
7042 cap->nchar = -2;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 nv_operator(cap);
7045 return;
7046 }
7047#endif
7048
7049#ifdef FEAT_VIRTUALEDIT
7050 /* Break tabs, etc. */
7051 if (virtual_active())
7052 {
7053 if (u_save_cursor() == FAIL)
7054 return;
7055 if (gchar_cursor() == NUL)
7056 {
7057 /* Add extra space and put the cursor on the first one. */
7058 coladvance_force((colnr_T)(getviscol() + cap->count1));
7059 curwin->w_cursor.col -= cap->count1;
7060 }
7061 else if (gchar_cursor() == TAB)
7062 coladvance_force(getviscol());
7063 }
7064#endif
7065
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007066 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007068 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#ifdef FEAT_MBYTE
7070 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7071#endif
7072 )
7073 {
7074 clearopbeep(cap->oap);
7075 return;
7076 }
7077
7078 /*
7079 * Replacing with a TAB is done by edit() when it is complicated because
7080 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7081 * Other characters are done below to avoid problems with things like
7082 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7083 */
7084 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7085 {
7086 stuffnumReadbuff(cap->count1);
7087 stuffcharReadbuff('R');
7088 stuffcharReadbuff('\t');
7089 stuffcharReadbuff(ESC);
7090 return;
7091 }
7092
7093 /* save line for undo */
7094 if (u_save_cursor() == FAIL)
7095 return;
7096
7097 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7098 {
7099 /*
7100 * Replace character(s) by a single newline.
7101 * Strange vi behaviour: Only one newline is inserted.
7102 * Delete the characters here.
7103 * Insert the newline with an insert command, takes care of
7104 * autoindent. The insert command depends on being on the last
7105 * character of a line or not.
7106 */
7107#ifdef FEAT_MBYTE
7108 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7109#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007110 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#endif
7112 stuffcharReadbuff('\r');
7113 stuffcharReadbuff(ESC);
7114
7115 /* Give 'r' to edit(), to get the redo command right. */
7116 invoke_edit(cap, TRUE, 'r', FALSE);
7117 }
7118 else
7119 {
7120 prep_redo(cap->oap->regname, cap->count1,
7121 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7122
7123 curbuf->b_op_start = curwin->w_cursor;
7124#ifdef FEAT_MBYTE
7125 if (has_mbyte)
7126 {
7127 int old_State = State;
7128
7129 if (cap->ncharC1 != 0)
7130 AppendCharToRedobuff(cap->ncharC1);
7131 if (cap->ncharC2 != 0)
7132 AppendCharToRedobuff(cap->ncharC2);
7133
7134 /* This is slow, but it handles replacing a single-byte with a
7135 * multi-byte and the other way around. Also handles adding
7136 * composing characters for utf-8. */
7137 for (n = cap->count1; n > 0; --n)
7138 {
7139 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007140 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7141 {
7142 int c = ins_copychar(curwin->w_cursor.lnum
7143 + (cap->nchar == Ctrl_Y ? -1 : 1));
7144 if (c != NUL)
7145 ins_char(c);
7146 else
7147 /* will be decremented further down */
7148 ++curwin->w_cursor.col;
7149 }
7150 else
7151 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 State = old_State;
7153 if (cap->ncharC1 != 0)
7154 ins_char(cap->ncharC1);
7155 if (cap->ncharC2 != 0)
7156 ins_char(cap->ncharC2);
7157 }
7158 }
7159 else
7160#endif
7161 {
7162 /*
7163 * Replace the characters within one line.
7164 */
7165 for (n = cap->count1; n > 0; --n)
7166 {
7167 /*
7168 * Get ptr again, because u_save and/or showmatch() will have
7169 * released the line. At the same time we let know that the
7170 * line will be changed.
7171 */
7172 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007173 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7174 {
7175 int c = ins_copychar(curwin->w_cursor.lnum
7176 + (cap->nchar == Ctrl_Y ? -1 : 1));
7177 if (c != NUL)
7178 ptr[curwin->w_cursor.col] = c;
7179 }
7180 else
7181 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (p_sm && msg_silent == 0)
7183 showmatch(cap->nchar);
7184 ++curwin->w_cursor.col;
7185 }
7186#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007187 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007189 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar009b2592004-10-24 19:18:58 +00007191 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007192 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007194 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 }
7196#endif
7197
7198 /* mark the buffer as changed and prepare for displaying */
7199 changed_bytes(curwin->w_cursor.lnum,
7200 (colnr_T)(curwin->w_cursor.col - cap->count1));
7201 }
7202 --curwin->w_cursor.col; /* cursor on the last replaced char */
7203#ifdef FEAT_MBYTE
7204 /* if the character on the left of the current cursor is a multi-byte
7205 * character, move two characters left */
7206 if (has_mbyte)
7207 mb_adjust_cursor();
7208#endif
7209 curbuf->b_op_end = curwin->w_cursor;
7210 curwin->w_set_curswant = TRUE;
7211 set_last_insert(cap->nchar);
7212 }
7213}
7214
7215#ifdef FEAT_VISUAL
7216/*
7217 * 'o': Exchange start and end of Visual area.
7218 * 'O': same, but in block mode exchange left and right corners.
7219 */
7220 static void
7221v_swap_corners(cmdchar)
7222 int cmdchar;
7223{
7224 pos_T old_cursor;
7225 colnr_T left, right;
7226
7227 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7228 {
7229 old_cursor = curwin->w_cursor;
7230 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7231 curwin->w_cursor.lnum = VIsual.lnum;
7232 coladvance(left);
7233 VIsual = curwin->w_cursor;
7234
7235 curwin->w_cursor.lnum = old_cursor.lnum;
7236 curwin->w_curswant = right;
7237 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7238 * right one column */
7239 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7240 ++curwin->w_curswant;
7241 coladvance(curwin->w_curswant);
7242 if (curwin->w_cursor.col == old_cursor.col
7243#ifdef FEAT_VIRTUALEDIT
7244 && (!virtual_active()
7245 || curwin->w_cursor.coladd == old_cursor.coladd)
7246#endif
7247 )
7248 {
7249 curwin->w_cursor.lnum = VIsual.lnum;
7250 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7251 ++right;
7252 coladvance(right);
7253 VIsual = curwin->w_cursor;
7254
7255 curwin->w_cursor.lnum = old_cursor.lnum;
7256 coladvance(left);
7257 curwin->w_curswant = left;
7258 }
7259 }
7260 else
7261 {
7262 old_cursor = curwin->w_cursor;
7263 curwin->w_cursor = VIsual;
7264 VIsual = old_cursor;
7265 curwin->w_set_curswant = TRUE;
7266 }
7267}
7268#endif /* FEAT_VISUAL */
7269
7270/*
7271 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7272 */
7273 static void
7274nv_Replace(cap)
7275 cmdarg_T *cap;
7276{
7277#ifdef FEAT_VISUAL
7278 if (VIsual_active) /* "R" is replace lines */
7279 {
7280 cap->cmdchar = 'c';
7281 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007282 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 VIsual_mode = 'V';
7284 nv_operator(cap);
7285 }
7286 else
7287#endif
7288 if (!checkclearopq(cap->oap))
7289 {
7290 if (!curbuf->b_p_ma)
7291 EMSG(_(e_modifiable));
7292 else
7293 {
7294#ifdef FEAT_VIRTUALEDIT
7295 if (virtual_active())
7296 coladvance(getviscol());
7297#endif
7298 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7299 }
7300 }
7301}
7302
7303#ifdef FEAT_VREPLACE
7304/*
7305 * "gr".
7306 */
7307 static void
7308nv_vreplace(cap)
7309 cmdarg_T *cap;
7310{
7311# ifdef FEAT_VISUAL
7312 if (VIsual_active)
7313 {
7314 cap->cmdchar = 'r';
7315 cap->nchar = cap->extra_char;
7316 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7317 }
7318 else
7319# endif
7320 if (!checkclearopq(cap->oap))
7321 {
7322 if (!curbuf->b_p_ma)
7323 EMSG(_(e_modifiable));
7324 else
7325 {
7326 if (cap->extra_char == Ctrl_V) /* get another character */
7327 cap->extra_char = get_literal();
7328 stuffcharReadbuff(cap->extra_char);
7329 stuffcharReadbuff(ESC);
7330# ifdef FEAT_VIRTUALEDIT
7331 if (virtual_active())
7332 coladvance(getviscol());
7333# endif
7334 invoke_edit(cap, TRUE, 'v', FALSE);
7335 }
7336 }
7337}
7338#endif
7339
7340/*
7341 * Swap case for "~" command, when it does not work like an operator.
7342 */
7343 static void
7344n_swapchar(cap)
7345 cmdarg_T *cap;
7346{
7347 long n;
7348 pos_T startpos;
7349 int did_change = 0;
7350#ifdef FEAT_NETBEANS_INTG
7351 pos_T pos;
7352 char_u *ptr;
7353 int count;
7354#endif
7355
7356 if (checkclearopq(cap->oap))
7357 return;
7358
7359 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7360 {
7361 clearopbeep(cap->oap);
7362 return;
7363 }
7364
7365 prep_redo_cmd(cap);
7366
7367 if (u_save_cursor() == FAIL)
7368 return;
7369
7370 startpos = curwin->w_cursor;
7371#ifdef FEAT_NETBEANS_INTG
7372 pos = startpos;
7373#endif
7374 for (n = cap->count1; n > 0; --n)
7375 {
7376 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7377 inc_cursor();
7378 if (gchar_cursor() == NUL)
7379 {
7380 if (vim_strchr(p_ww, '~') != NULL
7381 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7382 {
7383#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007384 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 if (did_change)
7387 {
7388 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007389 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007390 netbeans_removed(curbuf, pos.lnum, pos.col,
7391 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007393 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395 pos.col = 0;
7396 pos.lnum++;
7397 }
7398#endif
7399 ++curwin->w_cursor.lnum;
7400 curwin->w_cursor.col = 0;
7401 if (n > 1)
7402 {
7403 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7404 break;
7405 u_clearline();
7406 }
7407 }
7408 else
7409 break;
7410 }
7411 }
7412#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007413 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
7415 ptr = ml_get(pos.lnum);
7416 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007417 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7418 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 }
7420#endif
7421
7422
7423 check_cursor();
7424 curwin->w_set_curswant = TRUE;
7425 if (did_change)
7426 {
7427 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7428 0L);
7429 curbuf->b_op_start = startpos;
7430 curbuf->b_op_end = curwin->w_cursor;
7431 if (curbuf->b_op_end.col > 0)
7432 --curbuf->b_op_end.col;
7433 }
7434}
7435
7436/*
7437 * Move cursor to mark.
7438 */
7439 static void
7440nv_cursormark(cap, flag, pos)
7441 cmdarg_T *cap;
7442 int flag;
7443 pos_T *pos;
7444{
7445 if (check_mark(pos) == FAIL)
7446 clearop(cap->oap);
7447 else
7448 {
7449 if (cap->cmdchar == '\''
7450 || cap->cmdchar == '`'
7451 || cap->cmdchar == '['
7452 || cap->cmdchar == ']')
7453 setpcmark();
7454 curwin->w_cursor = *pos;
7455 if (flag)
7456 beginline(BL_WHITE | BL_FIX);
7457 else
7458 check_cursor();
7459 }
7460 cap->oap->motion_type = flag ? MLINE : MCHAR;
7461 if (cap->cmdchar == '`')
7462 cap->oap->use_reg_one = TRUE;
7463 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7464 curwin->w_set_curswant = TRUE;
7465}
7466
7467#ifdef FEAT_VISUAL
7468/*
7469 * Handle commands that are operators in Visual mode.
7470 */
7471 static void
7472v_visop(cap)
7473 cmdarg_T *cap;
7474{
7475 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7476
7477 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007478 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 if (isupper(cap->cmdchar))
7480 {
7481 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007482 {
7483 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7487 curwin->w_curswant = MAXCOL;
7488 }
7489 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7490 nv_operator(cap);
7491}
7492#endif
7493
7494/*
7495 * "s" and "S" commands.
7496 */
7497 static void
7498nv_subst(cap)
7499 cmdarg_T *cap;
7500{
7501#ifdef FEAT_VISUAL
7502 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7503 {
7504 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007505 {
7506 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 cap->cmdchar = 'c';
7510 nv_operator(cap);
7511 }
7512 else
7513#endif
7514 nv_optrans(cap);
7515}
7516
7517/*
7518 * Abbreviated commands.
7519 */
7520 static void
7521nv_abbrev(cap)
7522 cmdarg_T *cap;
7523{
7524 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7525 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7526
7527#ifdef FEAT_VISUAL
7528 /* in Visual mode these commands are operators */
7529 if (VIsual_active)
7530 v_visop(cap);
7531 else
7532#endif
7533 nv_optrans(cap);
7534}
7535
7536/*
7537 * Translate a command into another command.
7538 */
7539 static void
7540nv_optrans(cap)
7541 cmdarg_T *cap;
7542{
7543 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7544 (char_u *)"d$", (char_u *)"c$",
7545 (char_u *)"cl", (char_u *)"cc",
7546 (char_u *)"yy", (char_u *)":s\r"};
7547 static char_u *str = (char_u *)"xXDCsSY&";
7548
7549 if (!checkclearopq(cap->oap))
7550 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007551 /* In Vi "2D" doesn't delete the next line. Can't translate it
7552 * either, because "2." should also not use the count. */
7553 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7554 {
7555 cap->oap->start = curwin->w_cursor;
7556 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007557#ifdef FEAT_EVAL
7558 set_op_var(OP_DELETE);
7559#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007560 cap->count1 = 1;
7561 nv_dollar(cap);
7562 finish_op = TRUE;
7563 ResetRedobuff();
7564 AppendCharToRedobuff('D');
7565 }
7566 else
7567 {
7568 if (cap->count0)
7569 stuffnumReadbuff(cap->count0);
7570 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573 cap->opcount = 0;
7574}
7575
7576/*
7577 * "'" and "`" commands. Also for "g'" and "g`".
7578 * cap->arg is TRUE for "'" and "g'".
7579 */
7580 static void
7581nv_gomark(cap)
7582 cmdarg_T *cap;
7583{
7584 pos_T *pos;
7585 int c;
7586#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007587 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7589#endif
7590
7591 if (cap->cmdchar == 'g')
7592 c = cap->extra_char;
7593 else
7594 c = cap->nchar;
7595 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7596 if (pos == (pos_T *)-1) /* jumped to other file */
7597 {
7598 if (cap->arg)
7599 {
7600 check_cursor_lnum();
7601 beginline(BL_WHITE | BL_FIX);
7602 }
7603 else
7604 check_cursor();
7605 }
7606 else
7607 nv_cursormark(cap, cap->arg, pos);
7608
7609#ifdef FEAT_VIRTUALEDIT
7610 /* May need to clear the coladd that a mark includes. */
7611 if (!virtual_active())
7612 curwin->w_cursor.coladd = 0;
7613#endif
7614#ifdef FEAT_FOLDING
7615 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007616 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007617 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 && (fdo_flags & FDO_MARK)
7619 && old_KeyTyped)
7620 foldOpenCursor();
7621#endif
7622}
7623
7624/*
7625 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7626 */
7627 static void
7628nv_pcmark(cap)
7629 cmdarg_T *cap;
7630{
7631#ifdef FEAT_JUMPLIST
7632 pos_T *pos;
7633# ifdef FEAT_FOLDING
7634 linenr_T lnum = curwin->w_cursor.lnum;
7635 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7636# endif
7637
7638 if (!checkclearopq(cap->oap))
7639 {
7640 if (cap->cmdchar == 'g')
7641 pos = movechangelist((int)cap->count1);
7642 else
7643 pos = movemark((int)cap->count1);
7644 if (pos == (pos_T *)-1) /* jump to other file */
7645 {
7646 curwin->w_set_curswant = TRUE;
7647 check_cursor();
7648 }
7649 else if (pos != NULL) /* can jump */
7650 nv_cursormark(cap, FALSE, pos);
7651 else if (cap->cmdchar == 'g')
7652 {
7653 if (curbuf->b_changelistlen == 0)
7654 EMSG(_("E664: changelist is empty"));
7655 else if (cap->count1 < 0)
7656 EMSG(_("E662: At start of changelist"));
7657 else
7658 EMSG(_("E663: At end of changelist"));
7659 }
7660 else
7661 clearopbeep(cap->oap);
7662# ifdef FEAT_FOLDING
7663 if (cap->oap->op_type == OP_NOP
7664 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7665 && (fdo_flags & FDO_MARK)
7666 && old_KeyTyped)
7667 foldOpenCursor();
7668# endif
7669 }
7670#else
7671 clearopbeep(cap->oap);
7672#endif
7673}
7674
7675/*
7676 * Handle '"' command.
7677 */
7678 static void
7679nv_regname(cap)
7680 cmdarg_T *cap;
7681{
7682 if (checkclearop(cap->oap))
7683 return;
7684#ifdef FEAT_EVAL
7685 if (cap->nchar == '=')
7686 cap->nchar = get_expr_register();
7687#endif
7688 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7689 {
7690 cap->oap->regname = cap->nchar;
7691 cap->opcount = cap->count0; /* remember count before '"' */
7692#ifdef FEAT_EVAL
7693 set_reg_var(cap->oap->regname);
7694#endif
7695 }
7696 else
7697 clearopbeep(cap->oap);
7698}
7699
7700#ifdef FEAT_VISUAL
7701/*
7702 * Handle "v", "V" and "CTRL-V" commands.
7703 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7704 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 */
7707 static void
7708nv_visual(cap)
7709 cmdarg_T *cap;
7710{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007711 if (cap->cmdchar == Ctrl_Q)
7712 cap->cmdchar = Ctrl_V;
7713
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7715 * characterwise, linewise, or blockwise. */
7716 if (cap->oap->op_type != OP_NOP)
7717 {
7718 cap->oap->motion_force = cap->cmdchar;
7719 finish_op = FALSE; /* operator doesn't finish now but later */
7720 return;
7721 }
7722
7723 VIsual_select = cap->arg;
7724 if (VIsual_active) /* change Visual mode */
7725 {
7726 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7727 end_visual_mode();
7728 else /* toggle char/block mode */
7729 { /* or char/line mode */
7730 VIsual_mode = cap->cmdchar;
7731 showmode();
7732 }
7733 redraw_curbuf_later(INVERTED); /* update the inversion */
7734 }
7735 else /* start Visual mode */
7736 {
7737 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007738 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007740 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 VIsual = curwin->w_cursor;
7742
7743 VIsual_active = TRUE;
7744 VIsual_reselect = TRUE;
7745 if (!cap->arg)
7746 /* start Select mode when 'selectmode' contains "cmd" */
7747 may_start_select('c');
7748#ifdef FEAT_MOUSE
7749 setmouse();
7750#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007751 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 redraw_cmdline = TRUE; /* show visual mode later */
7753 /*
7754 * For V and ^V, we multiply the number of lines even if there
7755 * was only one -- webb
7756 */
7757 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7758 {
7759 curwin->w_cursor.lnum +=
7760 resel_VIsual_line_count * cap->count0 - 1;
7761 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7762 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7763 }
7764 VIsual_mode = resel_VIsual_mode;
7765 if (VIsual_mode == 'v')
7766 {
7767 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007768 {
7769 validate_virtcol();
7770 curwin->w_curswant = curwin->w_virtcol
7771 + resel_VIsual_vcol * cap->count0 - 1;
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007774 curwin->w_curswant = resel_VIsual_vcol;
7775 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007777 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 curwin->w_curswant = MAXCOL;
7780 coladvance((colnr_T)MAXCOL);
7781 }
7782 else if (VIsual_mode == Ctrl_V)
7783 {
7784 validate_virtcol();
7785 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007786 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 coladvance(curwin->w_curswant);
7788 }
7789 else
7790 curwin->w_set_curswant = TRUE;
7791 redraw_curbuf_later(INVERTED); /* show the inversion */
7792 }
7793 else
7794 {
7795 if (!cap->arg)
7796 /* start Select mode when 'selectmode' contains "cmd" */
7797 may_start_select('c');
7798 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007799 if (VIsual_mode != 'V' && *p_sel == 'e')
7800 ++cap->count1; /* include one more char */
7801 if (cap->count0 > 0 && --cap->count1 > 0)
7802 {
7803 /* With a count select that many characters or lines. */
7804 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7805 nv_right(cap);
7806 else if (VIsual_mode == 'V')
7807 nv_down(cap);
7808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810 }
7811}
7812
7813/*
7814 * Start selection for Shift-movement keys.
7815 */
7816 void
7817start_selection()
7818{
7819 /* if 'selectmode' contains "key", start Select mode */
7820 may_start_select('k');
7821 n_start_visual_mode('v');
7822}
7823
7824/*
7825 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7826 */
7827 void
7828may_start_select(c)
7829 int c;
7830{
7831 VIsual_select = (stuff_empty() && typebuf_typed()
7832 && (vim_strchr(p_slm, c) != NULL));
7833}
7834
7835/*
7836 * Start Visual mode "c".
7837 * Should set VIsual_select before calling this.
7838 */
7839 static void
7840n_start_visual_mode(c)
7841 int c;
7842{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007843#ifdef FEAT_CONCEAL
7844 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007845 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007846#endif
7847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 VIsual_mode = c;
7849 VIsual_active = TRUE;
7850 VIsual_reselect = TRUE;
7851#ifdef FEAT_VIRTUALEDIT
7852 /* Corner case: the 0 position in a tab may change when going into
7853 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7854 */
7855 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007856 {
7857 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860#endif
7861 VIsual = curwin->w_cursor;
7862
7863#ifdef FEAT_FOLDING
7864 foldAdjustVisual();
7865#endif
7866
7867#ifdef FEAT_MOUSE
7868 setmouse();
7869#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007870#ifdef FEAT_CONCEAL
7871 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007872 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007873#endif
7874
Bram Moolenaar09df3122006-01-23 22:23:09 +00007875 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 redraw_cmdline = TRUE; /* show visual mode later */
7877#ifdef FEAT_CLIPBOARD
7878 /* Make sure the clipboard gets updated. Needed because start and
7879 * end may still be the same, and the selection needs to be owned */
7880 clip_star.vmode = NUL;
7881#endif
7882
7883 /* Only need to redraw this line, unless still need to redraw an old
7884 * Visual area (when 'lazyredraw' is set). */
7885 if (curwin->w_redr_type < INVERTED)
7886 {
7887 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7888 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7889 }
7890}
7891
7892#endif /* FEAT_VISUAL */
7893
7894/*
7895 * CTRL-W: Window commands
7896 */
7897 static void
7898nv_window(cap)
7899 cmdarg_T *cap;
7900{
7901#ifdef FEAT_WINDOWS
7902 if (!checkclearop(cap->oap))
7903 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7904#else
7905 (void)checkclearop(cap->oap);
7906#endif
7907}
7908
7909/*
7910 * CTRL-Z: Suspend
7911 */
7912 static void
7913nv_suspend(cap)
7914 cmdarg_T *cap;
7915{
7916 clearop(cap->oap);
7917#ifdef FEAT_VISUAL
7918 if (VIsual_active)
7919 end_visual_mode(); /* stop Visual mode */
7920#endif
7921 do_cmdline_cmd((char_u *)"st");
7922}
7923
7924/*
7925 * Commands starting with "g".
7926 */
7927 static void
7928nv_g_cmd(cap)
7929 cmdarg_T *cap;
7930{
7931 oparg_T *oap = cap->oap;
7932#ifdef FEAT_VISUAL
7933 pos_T tpos;
7934#endif
7935 int i;
7936 int flag = FALSE;
7937
7938 switch (cap->nchar)
7939 {
7940#ifdef MEM_PROFILE
7941 /*
7942 * "g^A": dump log of used memory.
7943 */
7944 case Ctrl_A:
7945 vim_mem_profile_dump();
7946 break;
7947#endif
7948
7949#ifdef FEAT_VREPLACE
7950 /*
7951 * "gR": Enter virtual replace mode.
7952 */
7953 case 'R':
7954 cap->arg = TRUE;
7955 nv_Replace(cap);
7956 break;
7957
7958 case 'r':
7959 nv_vreplace(cap);
7960 break;
7961#endif
7962
7963 case '&':
7964 do_cmdline_cmd((char_u *)"%s//~/&");
7965 break;
7966
7967#ifdef FEAT_VISUAL
7968 /*
7969 * "gv": Reselect the previous Visual area. If Visual already active,
7970 * exchange previous and current Visual area.
7971 */
7972 case 'v':
7973 if (checkclearop(oap))
7974 break;
7975
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007976 if ( curbuf->b_visual.vi_start.lnum == 0
7977 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7978 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 beep_flush();
7980 else
7981 {
7982 /* set w_cursor to the start of the Visual area, tpos to the end */
7983 if (VIsual_active)
7984 {
7985 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007986 VIsual_mode = curbuf->b_visual.vi_mode;
7987 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988# ifdef FEAT_EVAL
7989 curbuf->b_visual_mode_eval = i;
7990# endif
7991 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007992 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7993 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007995 tpos = curbuf->b_visual.vi_end;
7996 curbuf->b_visual.vi_end = curwin->w_cursor;
7997 curwin->w_cursor = curbuf->b_visual.vi_start;
7998 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 }
8000 else
8001 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008002 VIsual_mode = curbuf->b_visual.vi_mode;
8003 curwin->w_curswant = curbuf->b_visual.vi_curswant;
8004 tpos = curbuf->b_visual.vi_end;
8005 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
8007
8008 VIsual_active = TRUE;
8009 VIsual_reselect = TRUE;
8010
8011 /* Set Visual to the start and w_cursor to the end of the Visual
8012 * area. Make sure they are on an existing character. */
8013 check_cursor();
8014 VIsual = curwin->w_cursor;
8015 curwin->w_cursor = tpos;
8016 check_cursor();
8017 update_topline();
8018 /*
8019 * When called from normal "g" command: start Select mode when
8020 * 'selectmode' contains "cmd". When called for K_SELECT, always
8021 * start Select mode.
8022 */
8023 if (cap->arg)
8024 VIsual_select = TRUE;
8025 else
8026 may_start_select('c');
8027#ifdef FEAT_MOUSE
8028 setmouse();
8029#endif
8030#ifdef FEAT_CLIPBOARD
8031 /* Make sure the clipboard gets updated. Needed because start and
8032 * end are still the same, and the selection needs to be owned */
8033 clip_star.vmode = NUL;
8034#endif
8035 redraw_curbuf_later(INVERTED);
8036 showmode();
8037 }
8038 break;
8039 /*
8040 * "gV": Don't reselect the previous Visual area after a Select mode
8041 * mapping of menu.
8042 */
8043 case 'V':
8044 VIsual_reselect = FALSE;
8045 break;
8046
8047 /*
8048 * "gh": start Select mode.
8049 * "gH": start Select line mode.
8050 * "g^H": start Select block mode.
8051 */
8052 case K_BS:
8053 cap->nchar = Ctrl_H;
8054 /* FALLTHROUGH */
8055 case 'h':
8056 case 'H':
8057 case Ctrl_H:
8058# ifdef EBCDIC
8059 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8060 if (cap->nchar == Ctrl_H)
8061 cap->cmdchar = Ctrl_V;
8062 else
8063# endif
8064 cap->cmdchar = cap->nchar + ('v' - 'h');
8065 cap->arg = TRUE;
8066 nv_visual(cap);
8067 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008068#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008069
8070 /* "gn", "gN" visually select next/previous search match
8071 * "gn" selects next match
8072 * "gN" selects previous match
8073 */
8074 case 'N':
8075 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008076#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008077 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008078#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008079 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 /*
8083 * "gj" and "gk" two new funny movement keys -- up and down
8084 * movement based on *screen* line rather than *file* line.
8085 */
8086 case 'j':
8087 case K_DOWN:
8088 /* with 'nowrap' it works just like the normal "j" command; also when
8089 * in a closed fold */
8090 if (!curwin->w_p_wrap
8091#ifdef FEAT_FOLDING
8092 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8093#endif
8094 )
8095 {
8096 oap->motion_type = MLINE;
8097 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8098 }
8099 else
8100 i = nv_screengo(oap, FORWARD, cap->count1);
8101 if (i == FAIL)
8102 clearopbeep(oap);
8103 break;
8104
8105 case 'k':
8106 case K_UP:
8107 /* with 'nowrap' it works just like the normal "k" command; also when
8108 * in a closed fold */
8109 if (!curwin->w_p_wrap
8110#ifdef FEAT_FOLDING
8111 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8112#endif
8113 )
8114 {
8115 oap->motion_type = MLINE;
8116 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8117 }
8118 else
8119 i = nv_screengo(oap, BACKWARD, cap->count1);
8120 if (i == FAIL)
8121 clearopbeep(oap);
8122 break;
8123
8124 /*
8125 * "gJ": join two lines without inserting a space.
8126 */
8127 case 'J':
8128 nv_join(cap);
8129 break;
8130
8131 /*
8132 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8133 * "gm": middle of "g0" and "g$".
8134 */
8135 case '^':
8136 flag = TRUE;
8137 /* FALLTHROUGH */
8138
8139 case '0':
8140 case 'm':
8141 case K_HOME:
8142 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 oap->motion_type = MCHAR;
8144 oap->inclusive = FALSE;
8145 if (curwin->w_p_wrap
8146#ifdef FEAT_VERTSPLIT
8147 && curwin->w_width != 0
8148#endif
8149 )
8150 {
8151 int width1 = W_WIDTH(curwin) - curwin_col_off();
8152 int width2 = width1 + curwin_col_off2();
8153
8154 validate_virtcol();
8155 i = 0;
8156 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8157 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8158 }
8159 else
8160 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008161 /* Go to the middle of the screen line. When 'number' or
8162 * 'relativenumber' is on and lines are wrapping the middle can be more
8163 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 if (cap->nchar == 'm')
8165 i += (W_WIDTH(curwin) - curwin_col_off()
8166 + ((curwin->w_p_wrap && i > 0)
8167 ? curwin_col_off2() : 0)) / 2;
8168 coladvance((colnr_T)i);
8169 if (flag)
8170 {
8171 do
8172 i = gchar_cursor();
8173 while (vim_iswhite(i) && oneright() == OK);
8174 }
8175 curwin->w_set_curswant = TRUE;
8176 break;
8177
8178 case '_':
8179 /* "g_": to the last non-blank character in the line or <count> lines
8180 * downward. */
8181 cap->oap->motion_type = MCHAR;
8182 cap->oap->inclusive = TRUE;
8183 curwin->w_curswant = MAXCOL;
8184 if (cursor_down((long)(cap->count1 - 1),
8185 cap->oap->op_type == OP_NOP) == FAIL)
8186 clearopbeep(cap->oap);
8187 else
8188 {
8189 char_u *ptr = ml_get_curline();
8190
8191 /* In Visual mode we may end up after the line. */
8192 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8193 --curwin->w_cursor.col;
8194
8195 /* Decrease the cursor column until it's on a non-blank. */
8196 while (curwin->w_cursor.col > 0
8197 && vim_iswhite(ptr[curwin->w_cursor.col]))
8198 --curwin->w_cursor.col;
8199 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008200#ifdef FEAT_VISUAL
8201 adjust_for_sel(cap);
8202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 }
8204 break;
8205
8206 case '$':
8207 case K_END:
8208 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
8210 int col_off = curwin_col_off();
8211
8212 oap->motion_type = MCHAR;
8213 oap->inclusive = TRUE;
8214 if (curwin->w_p_wrap
8215#ifdef FEAT_VERTSPLIT
8216 && curwin->w_width != 0
8217#endif
8218 )
8219 {
8220 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8221 if (cap->count1 == 1)
8222 {
8223 int width1 = W_WIDTH(curwin) - col_off;
8224 int width2 = width1 + curwin_col_off2();
8225
8226 validate_virtcol();
8227 i = width1 - 1;
8228 if (curwin->w_virtcol >= (colnr_T)width1)
8229 i += ((curwin->w_virtcol - width1) / width2 + 1)
8230 * width2;
8231 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008232
8233 /* Make sure we stick in this column. */
8234 validate_virtcol();
8235 curwin->w_curswant = curwin->w_virtcol;
8236 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8238 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8239 {
8240 /*
8241 * Check for landing on a character that got split at
8242 * the end of the line. We do not want to advance to
8243 * the next screen line.
8244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (curwin->w_virtcol > (colnr_T)i)
8246 --curwin->w_cursor.col;
8247 }
8248#endif
8249 }
8250 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8251 clearopbeep(oap);
8252 }
8253 else
8254 {
8255 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8256 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008257
8258 /* Make sure we stick in this column. */
8259 validate_virtcol();
8260 curwin->w_curswant = curwin->w_virtcol;
8261 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 }
8263 }
8264 break;
8265
8266 /*
8267 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8268 */
8269 case '*':
8270 case '#':
8271#if POUND != '#'
8272 case POUND: /* pound sign (sometimes equal to '#') */
8273#endif
8274 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8275 case ']': /* :tselect for current identifier */
8276 nv_ident(cap);
8277 break;
8278
8279 /*
8280 * ge and gE: go back to end of word
8281 */
8282 case 'e':
8283 case 'E':
8284 oap->motion_type = MCHAR;
8285 curwin->w_set_curswant = TRUE;
8286 oap->inclusive = TRUE;
8287 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8288 clearopbeep(oap);
8289 break;
8290
8291 /*
8292 * "g CTRL-G": display info about cursor position
8293 */
8294 case Ctrl_G:
8295 cursor_pos_info();
8296 break;
8297
8298 /*
8299 * "gi": start Insert at the last position.
8300 */
8301 case 'i':
8302 if (curbuf->b_last_insert.lnum != 0)
8303 {
8304 curwin->w_cursor = curbuf->b_last_insert;
8305 check_cursor_lnum();
8306 i = (int)STRLEN(ml_get_curline());
8307 if (curwin->w_cursor.col > (colnr_T)i)
8308 {
8309#ifdef FEAT_VIRTUALEDIT
8310 if (virtual_active())
8311 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8312#endif
8313 curwin->w_cursor.col = i;
8314 }
8315 }
8316 cap->cmdchar = 'i';
8317 nv_edit(cap);
8318 break;
8319
8320 /*
8321 * "gI": Start insert in column 1.
8322 */
8323 case 'I':
8324 beginline(0);
8325 if (!checkclearopq(oap))
8326 invoke_edit(cap, FALSE, 'g', FALSE);
8327 break;
8328
8329#ifdef FEAT_SEARCHPATH
8330 /*
8331 * "gf": goto file, edit file under cursor
8332 * "]f" and "[f": can also be used.
8333 */
8334 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008335 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 nv_gotofile(cap);
8337 break;
8338#endif
8339
8340 /* "g'm" and "g`m": jump to mark without setting pcmark */
8341 case '\'':
8342 cap->arg = TRUE;
8343 /*FALLTHROUGH*/
8344 case '`':
8345 nv_gomark(cap);
8346 break;
8347
8348 /*
8349 * "gs": Goto sleep.
8350 */
8351 case 's':
8352 do_sleep(cap->count1 * 1000L);
8353 break;
8354
8355 /*
8356 * "ga": Display the ascii value of the character under the
8357 * cursor. It is displayed in decimal, hex, and octal. -- webb
8358 */
8359 case 'a':
8360 do_ascii(NULL);
8361 break;
8362
8363#ifdef FEAT_MBYTE
8364 /*
8365 * "g8": Display the bytes used for the UTF-8 character under the
8366 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008367 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 */
8369 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008370 if (cap->count0 == 8)
8371 utf_find_illegal();
8372 else
8373 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 break;
8375#endif
8376
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008377 case '<':
8378 show_sb_text();
8379 break;
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 /*
8382 * "gg": Goto the first line in file. With a count it goes to
8383 * that line number like for "G". -- webb
8384 */
8385 case 'g':
8386 cap->arg = FALSE;
8387 nv_goto(cap);
8388 break;
8389
8390 /*
8391 * Two-character operators:
8392 * "gq" Format text
8393 * "gw" Format text and keep cursor position
8394 * "g~" Toggle the case of the text.
8395 * "gu" Change text to lower case.
8396 * "gU" Change text to upper case.
8397 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008398 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 */
8400 case 'q':
8401 case 'w':
8402 oap->cursor_start = curwin->w_cursor;
8403 /*FALLTHROUGH*/
8404 case '~':
8405 case 'u':
8406 case 'U':
8407 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008408 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 nv_operator(cap);
8410 break;
8411
8412 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008413 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 * current function
8415 * "gD": idem, but in the current file.
8416 */
8417 case 'd':
8418 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008419 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 break;
8421
8422#ifdef FEAT_MOUSE
8423 /*
8424 * g<*Mouse> : <C-*mouse>
8425 */
8426 case K_MIDDLEMOUSE:
8427 case K_MIDDLEDRAG:
8428 case K_MIDDLERELEASE:
8429 case K_LEFTMOUSE:
8430 case K_LEFTDRAG:
8431 case K_LEFTRELEASE:
8432 case K_RIGHTMOUSE:
8433 case K_RIGHTDRAG:
8434 case K_RIGHTRELEASE:
8435 case K_X1MOUSE:
8436 case K_X1DRAG:
8437 case K_X1RELEASE:
8438 case K_X2MOUSE:
8439 case K_X2DRAG:
8440 case K_X2RELEASE:
8441 mod_mask = MOD_MASK_CTRL;
8442 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8443 break;
8444#endif
8445
8446 case K_IGNORE:
8447 break;
8448
8449 /*
8450 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8451 */
8452 case 'p':
8453 case 'P':
8454 nv_put(cap);
8455 break;
8456
8457#ifdef FEAT_BYTEOFF
8458 /* "go": goto byte count from start of buffer */
8459 case 'o':
8460 goto_byte(cap->count0);
8461 break;
8462#endif
8463
8464 /* "gQ": improved Ex mode */
8465 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008466 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
8468 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008469 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 break;
8471 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 if (!checkclearopq(oap))
8474 do_exmode(TRUE);
8475 break;
8476
8477#ifdef FEAT_JUMPLIST
8478 case ',':
8479 nv_pcmark(cap);
8480 break;
8481
8482 case ';':
8483 cap->count1 = -cap->count1;
8484 nv_pcmark(cap);
8485 break;
8486#endif
8487
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008488#ifdef FEAT_WINDOWS
8489 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008490 if (!checkclearop(oap))
8491 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008492 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008493 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008494 if (!checkclearop(oap))
8495 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008496 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008497#endif
8498
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008499 case '+':
8500 case '-': /* "g+" and "g-": undo or redo along the timeline */
8501 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008502 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008503 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008504 break;
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 default:
8507 clearopbeep(oap);
8508 break;
8509 }
8510}
8511
8512/*
8513 * Handle "o" and "O" commands.
8514 */
8515 static void
8516n_opencmd(cap)
8517 cmdarg_T *cap;
8518{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008519#ifdef FEAT_CONCEAL
8520 linenr_T oldline = curwin->w_cursor.lnum;
8521#endif
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (!checkclearopq(cap->oap))
8524 {
8525#ifdef FEAT_FOLDING
8526 if (cap->cmdchar == 'O')
8527 /* Open above the first line of a folded sequence of lines */
8528 (void)hasFolding(curwin->w_cursor.lnum,
8529 &curwin->w_cursor.lnum, NULL);
8530 else
8531 /* Open below the last line of a folded sequence of lines */
8532 (void)hasFolding(curwin->w_cursor.lnum,
8533 NULL, &curwin->w_cursor.lnum);
8534#endif
8535 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8536 (cap->cmdchar == 'O' ? 1 : 0)),
8537 (linenr_T)(curwin->w_cursor.lnum +
8538 (cap->cmdchar == 'o' ? 1 : 0))
8539 ) == OK
8540 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8541#ifdef FEAT_COMMENTS
8542 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8543#endif
8544 0, 0))
8545 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008546#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008547 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008548 update_single_line(curwin, oldline);
8549#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008550 /* When '#' is in 'cpoptions' ignore the count. */
8551 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8552 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8554 }
8555 }
8556}
8557
8558/*
8559 * "." command: redo last change.
8560 */
8561 static void
8562nv_dot(cap)
8563 cmdarg_T *cap;
8564{
8565 if (!checkclearopq(cap->oap))
8566 {
8567 /*
8568 * If "restart_edit" is TRUE, the last but one command is repeated
8569 * instead of the last command (inserting text). This is used for
8570 * CTRL-O <.> in insert mode.
8571 */
8572 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8573 clearopbeep(cap->oap);
8574 }
8575}
8576
8577/*
8578 * CTRL-R: undo undo
8579 */
8580 static void
8581nv_redo(cap)
8582 cmdarg_T *cap;
8583{
8584 if (!checkclearopq(cap->oap))
8585 {
8586 u_redo((int)cap->count1);
8587 curwin->w_set_curswant = TRUE;
8588 }
8589}
8590
8591/*
8592 * Handle "U" command.
8593 */
8594 static void
8595nv_Undo(cap)
8596 cmdarg_T *cap;
8597{
8598 /* In Visual mode and typing "gUU" triggers an operator */
8599 if (cap->oap->op_type == OP_UPPER
8600#ifdef FEAT_VISUAL
8601 || VIsual_active
8602#endif
8603 )
8604 {
8605 /* translate "gUU" to "gUgU" */
8606 cap->cmdchar = 'g';
8607 cap->nchar = 'U';
8608 nv_operator(cap);
8609 }
8610 else if (!checkclearopq(cap->oap))
8611 {
8612 u_undoline();
8613 curwin->w_set_curswant = TRUE;
8614 }
8615}
8616
8617/*
8618 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8619 * single character.
8620 */
8621 static void
8622nv_tilde(cap)
8623 cmdarg_T *cap;
8624{
8625 if (!p_to
8626#ifdef FEAT_VISUAL
8627 && !VIsual_active
8628#endif
8629 && cap->oap->op_type != OP_TILDE)
8630 n_swapchar(cap);
8631 else
8632 nv_operator(cap);
8633}
8634
8635/*
8636 * Handle an operator command.
8637 * The actual work is done by do_pending_operator().
8638 */
8639 static void
8640nv_operator(cap)
8641 cmdarg_T *cap;
8642{
8643 int op_type;
8644
8645 op_type = get_op_type(cap->cmdchar, cap->nchar);
8646
8647 if (op_type == cap->oap->op_type) /* double operator works on lines */
8648 nv_lineop(cap);
8649 else if (!checkclearop(cap->oap))
8650 {
8651 cap->oap->start = curwin->w_cursor;
8652 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008653#ifdef FEAT_EVAL
8654 set_op_var(op_type);
8655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657}
8658
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008659#ifdef FEAT_EVAL
8660/*
8661 * Set v:operator to the characters for "optype".
8662 */
8663 static void
8664set_op_var(optype)
8665 int optype;
8666{
8667 char_u opchars[3];
8668
8669 if (optype == OP_NOP)
8670 set_vim_var_string(VV_OP, NULL, 0);
8671 else
8672 {
8673 opchars[0] = get_op_char(optype);
8674 opchars[1] = get_extra_op_char(optype);
8675 opchars[2] = NUL;
8676 set_vim_var_string(VV_OP, opchars, -1);
8677 }
8678}
8679#endif
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681/*
8682 * Handle linewise operator "dd", "yy", etc.
8683 *
8684 * "_" is is a strange motion command that helps make operators more logical.
8685 * It is actually implemented, but not documented in the real Vi. This motion
8686 * command actually refers to "the current line". Commands like "dd" and "yy"
8687 * are really an alternate form of "d_" and "y_". It does accept a count, so
8688 * "d3_" works to delete 3 lines.
8689 */
8690 static void
8691nv_lineop(cap)
8692 cmdarg_T *cap;
8693{
8694 cap->oap->motion_type = MLINE;
8695 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8696 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008697 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8698 && cap->oap->motion_force != 'v'
8699 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 || cap->oap->op_type == OP_LSHIFT
8701 || cap->oap->op_type == OP_RSHIFT)
8702 beginline(BL_SOL | BL_FIX);
8703 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8704 beginline(BL_WHITE | BL_FIX);
8705}
8706
8707/*
8708 * <Home> command.
8709 */
8710 static void
8711nv_home(cap)
8712 cmdarg_T *cap;
8713{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008714 /* CTRL-HOME is like "gg" */
8715 if (mod_mask & MOD_MASK_CTRL)
8716 nv_goto(cap);
8717 else
8718 {
8719 cap->count0 = 1;
8720 nv_pipe(cap);
8721 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008722 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8723 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726/*
8727 * "|" command.
8728 */
8729 static void
8730nv_pipe(cap)
8731 cmdarg_T *cap;
8732{
8733 cap->oap->motion_type = MCHAR;
8734 cap->oap->inclusive = FALSE;
8735 beginline(0);
8736 if (cap->count0 > 0)
8737 {
8738 coladvance((colnr_T)(cap->count0 - 1));
8739 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8740 }
8741 else
8742 curwin->w_curswant = 0;
8743 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008744 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 curwin->w_set_curswant = FALSE;
8746}
8747
8748/*
8749 * Handle back-word command "b" and "B".
8750 * cap->arg is 1 for "B"
8751 */
8752 static void
8753nv_bck_word(cap)
8754 cmdarg_T *cap;
8755{
8756 cap->oap->motion_type = MCHAR;
8757 cap->oap->inclusive = FALSE;
8758 curwin->w_set_curswant = TRUE;
8759 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8760 clearopbeep(cap->oap);
8761#ifdef FEAT_FOLDING
8762 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8763 foldOpenCursor();
8764#endif
8765}
8766
8767/*
8768 * Handle word motion commands "e", "E", "w" and "W".
8769 * cap->arg is TRUE for "E" and "W".
8770 */
8771 static void
8772nv_wordcmd(cap)
8773 cmdarg_T *cap;
8774{
8775 int n;
8776 int word_end;
8777 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008778 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779
8780 /*
8781 * Set inclusive for the "E" and "e" command.
8782 */
8783 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8784 word_end = TRUE;
8785 else
8786 word_end = FALSE;
8787 cap->oap->inclusive = word_end;
8788
8789 /*
8790 * "cw" and "cW" are a special case.
8791 */
8792 if (!word_end && cap->oap->op_type == OP_CHANGE)
8793 {
8794 n = gchar_cursor();
8795 if (n != NUL) /* not an empty line */
8796 {
8797 if (vim_iswhite(n))
8798 {
8799 /*
8800 * Reproduce a funny Vi behaviour: "cw" on a blank only
8801 * changes one character, not all blanks until the start of
8802 * the next word. Only do this when the 'w' flag is included
8803 * in 'cpoptions'.
8804 */
8805 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8806 {
8807 cap->oap->inclusive = TRUE;
8808 cap->oap->motion_type = MCHAR;
8809 return;
8810 }
8811 }
8812 else
8813 {
8814 /*
8815 * This is a little strange. To match what the real Vi does,
8816 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8817 * that we are not on a space or a TAB. This seems impolite
8818 * at first, but it's really more what we mean when we say
8819 * 'cw'.
8820 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008821 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 * will change only one character! This is done by setting
8823 * flag.
8824 */
8825 cap->oap->inclusive = TRUE;
8826 word_end = TRUE;
8827 flag = TRUE;
8828 }
8829 }
8830 }
8831
8832 cap->oap->motion_type = MCHAR;
8833 curwin->w_set_curswant = TRUE;
8834 if (word_end)
8835 n = end_word(cap->count1, cap->arg, flag, FALSE);
8836 else
8837 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8838
Bram Moolenaardfefb982008-04-01 10:06:39 +00008839 /* Don't leave the cursor on the NUL past the end of line. Unless we
8840 * didn't move it forward. */
8841 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008842 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
8844 if (n == FAIL && cap->oap->op_type == OP_NOP)
8845 clearopbeep(cap->oap);
8846 else
8847 {
8848#ifdef FEAT_VISUAL
8849 adjust_for_sel(cap);
8850#endif
8851#ifdef FEAT_FOLDING
8852 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8853 foldOpenCursor();
8854#endif
8855 }
8856}
8857
8858/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008859 * Used after a movement command: If the cursor ends up on the NUL after the
8860 * end of the line, may move it back to the last character and make the motion
8861 * inclusive.
8862 */
8863 static void
8864adjust_cursor(oap)
8865 oparg_T *oap;
8866{
8867 /* The cursor cannot remain on the NUL when:
8868 * - the column is > 0
8869 * - not in Visual mode or 'selection' is "o"
8870 * - 'virtualedit' is not "all" and not "onemore".
8871 */
8872 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8873#ifdef FEAT_VISUAL
8874 && (!VIsual_active || *p_sel == 'o')
8875#endif
8876#ifdef FEAT_VIRTUALEDIT
8877 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8878#endif
8879 )
8880 {
8881 --curwin->w_cursor.col;
8882#ifdef FEAT_MBYTE
8883 /* prevent cursor from moving on the trail byte */
8884 if (has_mbyte)
8885 mb_adjust_cursor();
8886#endif
8887 oap->inclusive = TRUE;
8888 }
8889}
8890
8891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 * "0" and "^" commands.
8893 * cap->arg is the argument for beginline().
8894 */
8895 static void
8896nv_beginline(cap)
8897 cmdarg_T *cap;
8898{
8899 cap->oap->motion_type = MCHAR;
8900 cap->oap->inclusive = FALSE;
8901 beginline(cap->arg);
8902#ifdef FEAT_FOLDING
8903 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8904 foldOpenCursor();
8905#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008906 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8907 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908}
8909
8910#ifdef FEAT_VISUAL
8911/*
8912 * In exclusive Visual mode, may include the last character.
8913 */
8914 static void
8915adjust_for_sel(cap)
8916 cmdarg_T *cap;
8917{
8918 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8919 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8920 {
8921# ifdef FEAT_MBYTE
8922 if (has_mbyte)
8923 inc_cursor();
8924 else
8925# endif
8926 ++curwin->w_cursor.col;
8927 cap->oap->inclusive = FALSE;
8928 }
8929}
8930
8931/*
8932 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8933 * Should check VIsual_mode before calling this.
8934 * Returns TRUE when backed up to the previous line.
8935 */
8936 static int
8937unadjust_for_sel()
8938{
8939 pos_T *pp;
8940
8941 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8942 {
8943 if (lt(VIsual, curwin->w_cursor))
8944 pp = &curwin->w_cursor;
8945 else
8946 pp = &VIsual;
8947#ifdef FEAT_VIRTUALEDIT
8948 if (pp->coladd > 0)
8949 --pp->coladd;
8950 else
8951#endif
8952 if (pp->col > 0)
8953 {
8954 --pp->col;
8955#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008956 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957#endif
8958 }
8959 else if (pp->lnum > 1)
8960 {
8961 --pp->lnum;
8962 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8963 return TRUE;
8964 }
8965 }
8966 return FALSE;
8967}
8968
8969/*
8970 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8971 */
8972 static void
8973nv_select(cap)
8974 cmdarg_T *cap;
8975{
8976 if (VIsual_active)
8977 VIsual_select = TRUE;
8978 else if (VIsual_reselect)
8979 {
8980 cap->nchar = 'v'; /* fake "gv" command */
8981 cap->arg = TRUE;
8982 nv_g_cmd(cap);
8983 }
8984}
8985
8986#endif
8987
8988/*
8989 * "G", "gg", CTRL-END, CTRL-HOME.
8990 * cap->arg is TRUE for "G".
8991 */
8992 static void
8993nv_goto(cap)
8994 cmdarg_T *cap;
8995{
8996 linenr_T lnum;
8997
8998 if (cap->arg)
8999 lnum = curbuf->b_ml.ml_line_count;
9000 else
9001 lnum = 1L;
9002 cap->oap->motion_type = MLINE;
9003 setpcmark();
9004
9005 /* When a count is given, use it instead of the default lnum */
9006 if (cap->count0 != 0)
9007 lnum = cap->count0;
9008 if (lnum < 1L)
9009 lnum = 1L;
9010 else if (lnum > curbuf->b_ml.ml_line_count)
9011 lnum = curbuf->b_ml.ml_line_count;
9012 curwin->w_cursor.lnum = lnum;
9013 beginline(BL_SOL | BL_FIX);
9014#ifdef FEAT_FOLDING
9015 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
9016 foldOpenCursor();
9017#endif
9018}
9019
9020/*
9021 * CTRL-\ in Normal mode.
9022 */
9023 static void
9024nv_normal(cap)
9025 cmdarg_T *cap;
9026{
9027 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
9028 {
9029 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00009030 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 clear_cmdline = TRUE; /* unshow mode later */
9032 restart_edit = 0;
9033#ifdef FEAT_CMDWIN
9034 if (cmdwin_type != 0)
9035 cmdwin_result = Ctrl_C;
9036#endif
9037#ifdef FEAT_VISUAL
9038 if (VIsual_active)
9039 {
9040 end_visual_mode(); /* stop Visual */
9041 redraw_curbuf_later(INVERTED);
9042 }
9043#endif
9044 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9045 if (cap->nchar == Ctrl_G && p_im)
9046 restart_edit = 'a';
9047 }
9048 else
9049 clearopbeep(cap->oap);
9050}
9051
9052/*
9053 * ESC in Normal mode: beep, but don't flush buffers.
9054 * Don't even beep if we are canceling a command.
9055 */
9056 static void
9057nv_esc(cap)
9058 cmdarg_T *cap;
9059{
9060 int no_reason;
9061
9062 no_reason = (cap->oap->op_type == OP_NOP
9063 && cap->opcount == 0
9064 && cap->count0 == 0
9065 && cap->oap->regname == 0
9066 && !p_im);
9067
9068 if (cap->arg) /* TRUE for CTRL-C */
9069 {
9070 if (restart_edit == 0
9071#ifdef FEAT_CMDWIN
9072 && cmdwin_type == 0
9073#endif
9074#ifdef FEAT_VISUAL
9075 && !VIsual_active
9076#endif
9077 && no_reason)
9078 MSG(_("Type :quit<Enter> to exit Vim"));
9079
9080 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9081 * set again below when halfway a mapping. */
9082 if (!p_im)
9083 restart_edit = 0;
9084#ifdef FEAT_CMDWIN
9085 if (cmdwin_type != 0)
9086 {
9087 cmdwin_result = K_IGNORE;
9088 got_int = FALSE; /* don't stop executing autocommands et al. */
9089 return;
9090 }
9091#endif
9092 }
9093
9094#ifdef FEAT_VISUAL
9095 if (VIsual_active)
9096 {
9097 end_visual_mode(); /* stop Visual */
9098 check_cursor_col(); /* make sure cursor is not beyond EOL */
9099 curwin->w_set_curswant = TRUE;
9100 redraw_curbuf_later(INVERTED);
9101 }
9102 else
9103#endif
9104 if (no_reason)
9105 vim_beep();
9106 clearop(cap->oap);
9107
9108 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9109 * set return to Insert mode afterwards. */
9110 if (restart_edit == 0 && goto_im()
9111#ifdef FEAT_EX_EXTRA
9112 && ex_normal_busy == 0
9113#endif
9114 )
9115 restart_edit = 'a';
9116}
9117
9118/*
9119 * Handle "A", "a", "I", "i" and <Insert> commands.
9120 */
9121 static void
9122nv_edit(cap)
9123 cmdarg_T *cap;
9124{
9125 /* <Insert> is equal to "i" */
9126 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9127 cap->cmdchar = 'i';
9128
9129#ifdef FEAT_VISUAL
9130 /* in Visual mode "A" and "I" are an operator */
9131 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9132 v_visop(cap);
9133
9134 /* in Visual mode and after an operator "a" and "i" are for text objects */
9135 else
9136#endif
9137 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9138 && (cap->oap->op_type != OP_NOP
9139#ifdef FEAT_VISUAL
9140 || VIsual_active
9141#endif
9142 ))
9143 {
9144#ifdef FEAT_TEXTOBJ
9145 nv_object(cap);
9146#else
9147 clearopbeep(cap->oap);
9148#endif
9149 }
9150 else if (!curbuf->b_p_ma && !p_im)
9151 {
9152 /* Only give this error when 'insertmode' is off. */
9153 EMSG(_(e_modifiable));
9154 clearop(cap->oap);
9155 }
9156 else if (!checkclearopq(cap->oap))
9157 {
9158 switch (cap->cmdchar)
9159 {
9160 case 'A': /* "A"ppend after the line */
9161 curwin->w_set_curswant = TRUE;
9162#ifdef FEAT_VIRTUALEDIT
9163 if (ve_flags == VE_ALL)
9164 {
9165 int save_State = State;
9166
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009167 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 * character past the end of the line */
9169 State = INSERT;
9170 coladvance((colnr_T)MAXCOL);
9171 State = save_State;
9172 }
9173 else
9174#endif
9175 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9176 break;
9177
9178 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009179 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9180 beginline(BL_WHITE);
9181 else
9182 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 break;
9184
9185 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9186#ifdef FEAT_VIRTUALEDIT
9187 /* increment coladd when in virtual space, increment the
9188 * column otherwise, also to append after an unprintable char */
9189 if (virtual_active()
9190 && (curwin->w_cursor.coladd > 0
9191 || *ml_get_cursor() == NUL
9192 || *ml_get_cursor() == TAB))
9193 curwin->w_cursor.coladd++;
9194 else
9195#endif
9196 if (*ml_get_cursor() != NUL)
9197 inc_cursor();
9198 break;
9199 }
9200
9201#ifdef FEAT_VIRTUALEDIT
9202 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9203 {
9204 int save_State = State;
9205
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009206 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * character past the end of the line */
9208 State = INSERT;
9209 coladvance(getviscol());
9210 State = save_State;
9211 }
9212#endif
9213
9214 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9215 }
9216}
9217
9218/*
9219 * Invoke edit() and take care of "restart_edit" and the return value.
9220 */
9221 static void
9222invoke_edit(cap, repl, cmd, startln)
9223 cmdarg_T *cap;
9224 int repl; /* "r" or "gr" command */
9225 int cmd;
9226 int startln;
9227{
9228 int restart_edit_save = 0;
9229
9230 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9231 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9232 * it. */
9233 if (repl || !stuff_empty())
9234 restart_edit_save = restart_edit;
9235 else
9236 restart_edit_save = 0;
9237
9238 /* Always reset "restart_edit", this is not a restarted edit. */
9239 restart_edit = 0;
9240
9241 if (edit(cmd, startln, cap->count1))
9242 cap->retval |= CA_COMMAND_BUSY;
9243
9244 if (restart_edit == 0)
9245 restart_edit = restart_edit_save;
9246}
9247
9248#ifdef FEAT_TEXTOBJ
9249/*
9250 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9251 */
9252 static void
9253nv_object(cap)
9254 cmdarg_T *cap;
9255{
9256 int flag;
9257 int include;
9258 char_u *mps_save;
9259
9260 if (cap->cmdchar == 'i')
9261 include = FALSE; /* "ix" = inner object: exclude white space */
9262 else
9263 include = TRUE; /* "ax" = an object: include white space */
9264
9265 /* Make sure (), [], {} and <> are in 'matchpairs' */
9266 mps_save = curbuf->b_p_mps;
9267 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9268
9269 switch (cap->nchar)
9270 {
9271 case 'w': /* "aw" = a word */
9272 flag = current_word(cap->oap, cap->count1, include, FALSE);
9273 break;
9274 case 'W': /* "aW" = a WORD */
9275 flag = current_word(cap->oap, cap->count1, include, TRUE);
9276 break;
9277 case 'b': /* "ab" = a braces block */
9278 case '(':
9279 case ')':
9280 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9281 break;
9282 case 'B': /* "aB" = a Brackets block */
9283 case '{':
9284 case '}':
9285 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9286 break;
9287 case '[': /* "a[" = a [] block */
9288 case ']':
9289 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9290 break;
9291 case '<': /* "a<" = a <> block */
9292 case '>':
9293 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9294 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009295 case 't': /* "at" = a tag block (xml and html) */
9296 flag = current_tagblock(cap->oap, cap->count1, include);
9297 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 case 'p': /* "ap" = a paragraph */
9299 flag = current_par(cap->oap, cap->count1, include, 'p');
9300 break;
9301 case 's': /* "as" = a sentence */
9302 flag = current_sent(cap->oap, cap->count1, include);
9303 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009304 case '"': /* "a"" = a double quoted string */
9305 case '\'': /* "a'" = a single quoted string */
9306 case '`': /* "a`" = a backtick quoted string */
9307 flag = current_quote(cap->oap, cap->count1, include,
9308 cap->nchar);
9309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310#if 0 /* TODO */
9311 case 'S': /* "aS" = a section */
9312 case 'f': /* "af" = a filename */
9313 case 'u': /* "au" = a URL */
9314#endif
9315 default:
9316 flag = FAIL;
9317 break;
9318 }
9319
9320 curbuf->b_p_mps = mps_save;
9321 if (flag == FAIL)
9322 clearopbeep(cap->oap);
9323 adjust_cursor_col();
9324 curwin->w_set_curswant = TRUE;
9325}
9326#endif
9327
9328/*
9329 * "q" command: Start/stop recording.
9330 * "q:", "q/", "q?": edit command-line in command-line window.
9331 */
9332 static void
9333nv_record(cap)
9334 cmdarg_T *cap;
9335{
9336 if (cap->oap->op_type == OP_FORMAT)
9337 {
9338 /* "gqq" is the same as "gqgq": format line */
9339 cap->cmdchar = 'g';
9340 cap->nchar = 'q';
9341 nv_operator(cap);
9342 }
9343 else if (!checkclearop(cap->oap))
9344 {
9345#ifdef FEAT_CMDWIN
9346 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9347 {
9348 stuffcharReadbuff(cap->nchar);
9349 stuffcharReadbuff(K_CMDWIN);
9350 }
9351 else
9352#endif
9353 /* (stop) recording into a named register, unless executing a
9354 * register */
9355 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9356 clearopbeep(cap->oap);
9357 }
9358}
9359
9360/*
9361 * Handle the "@r" command.
9362 */
9363 static void
9364nv_at(cap)
9365 cmdarg_T *cap;
9366{
9367 if (checkclearop(cap->oap))
9368 return;
9369#ifdef FEAT_EVAL
9370 if (cap->nchar == '=')
9371 {
9372 if (get_expr_register() == NUL)
9373 return;
9374 }
9375#endif
9376 while (cap->count1-- && !got_int)
9377 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009378 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 {
9380 clearopbeep(cap->oap);
9381 break;
9382 }
9383 line_breakcheck();
9384 }
9385}
9386
9387/*
9388 * Handle the CTRL-U and CTRL-D commands.
9389 */
9390 static void
9391nv_halfpage(cap)
9392 cmdarg_T *cap;
9393{
9394 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9395 || (cap->cmdchar == Ctrl_D
9396 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9397 clearopbeep(cap->oap);
9398 else if (!checkclearop(cap->oap))
9399 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9400}
9401
9402/*
9403 * Handle "J" or "gJ" command.
9404 */
9405 static void
9406nv_join(cap)
9407 cmdarg_T *cap;
9408{
9409#ifdef FEAT_VISUAL
9410 if (VIsual_active) /* join the visual lines */
9411 nv_operator(cap);
9412 else
9413#endif
9414 if (!checkclearop(cap->oap))
9415 {
9416 if (cap->count0 <= 1)
9417 cap->count0 = 2; /* default for join is two lines! */
9418 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9419 curbuf->b_ml.ml_line_count)
9420 clearopbeep(cap->oap); /* beyond last line */
9421 else
9422 {
9423 prep_redo(cap->oap->regname, cap->count0,
9424 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009425 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427 }
9428}
9429
9430/*
9431 * "P", "gP", "p" and "gp" commands.
9432 */
9433 static void
9434nv_put(cap)
9435 cmdarg_T *cap;
9436{
9437#ifdef FEAT_VISUAL
9438 int regname = 0;
9439 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009440 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009441 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442#endif
9443 int dir;
9444 int flags = 0;
9445
9446 if (cap->oap->op_type != OP_NOP)
9447 {
9448#ifdef FEAT_DIFF
9449 /* "dp" is ":diffput" */
9450 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9451 {
9452 clearop(cap->oap);
9453 nv_diffgetput(TRUE);
9454 }
9455 else
9456#endif
9457 clearopbeep(cap->oap);
9458 }
9459 else
9460 {
9461 dir = (cap->cmdchar == 'P'
9462 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9463 ? BACKWARD : FORWARD;
9464 prep_redo_cmd(cap);
9465 if (cap->cmdchar == 'g')
9466 flags |= PUT_CURSEND;
9467
9468#ifdef FEAT_VISUAL
9469 if (VIsual_active)
9470 {
9471 /* Putting in Visual mode: The put text replaces the selected
9472 * text. First delete the selected text, then put the new text.
9473 * Need to save and restore the registers that the delete
9474 * overwrites if the old contents is being put.
9475 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009476 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 regname = cap->oap->regname;
9478# ifdef FEAT_CLIPBOARD
9479 adjust_clip_reg(&regname);
9480# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009481 if (regname == 0 || regname == '"'
9482 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483# ifdef FEAT_CLIPBOARD
9484 || (clip_unnamed && (regname == '*' || regname == '+'))
9485# endif
9486
9487 )
9488 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009489 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 * put, save it first. */
9491 reg1 = get_register(regname, TRUE);
9492 }
9493
9494 /* Now delete the selected text. */
9495 cap->cmdchar = 'd';
9496 cap->nchar = NUL;
9497 cap->oap->regname = NUL;
9498 nv_operator(cap);
9499 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009500 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501
9502 /* delete PUT_LINE_BACKWARD; */
9503 cap->oap->regname = regname;
9504
9505 if (reg1 != NULL)
9506 {
9507 /* Delete probably changed the register we want to put, save
9508 * it first. Then put back what was there before the delete. */
9509 reg2 = get_register(regname, FALSE);
9510 put_register(regname, reg1);
9511 }
9512
9513 /* When deleted a linewise Visual area, put the register as
9514 * lines to avoid it joined with the next line. When deletion was
9515 * characterwise, split a line when putting lines. */
9516 if (VIsual_mode == 'V')
9517 flags |= PUT_LINE;
9518 else if (VIsual_mode == 'v')
9519 flags |= PUT_LINE_SPLIT;
9520 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9521 flags |= PUT_LINE_FORWARD;
9522 dir = BACKWARD;
9523 if ((VIsual_mode != 'V'
9524 && curwin->w_cursor.col < curbuf->b_op_start.col)
9525 || (VIsual_mode == 'V'
9526 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9527 /* cursor is at the end of the line or end of file, put
9528 * forward. */
9529 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009530 /* May have been reset in do_put(). */
9531 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533#endif
9534 do_put(cap->oap->regname, dir, cap->count1, flags);
9535
9536#ifdef FEAT_VISUAL
9537 /* If a register was saved, put it back now. */
9538 if (reg2 != NULL)
9539 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009540
9541 /* What to reselect with "gv"? Selecting the just put text seems to
9542 * be the most useful, since the original text was removed. */
9543 if (was_visual)
9544 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009545 curbuf->b_visual.vi_start = curbuf->b_op_start;
9546 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009547 }
9548
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009549 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009550 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009551 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009552 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009553 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009554
9555 /* If the cursor was in that line, move it to the end of the last
9556 * line. */
9557 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9558 {
9559 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9560 coladvance((colnr_T)MAXCOL);
9561 }
9562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563#endif
9564 auto_format(FALSE, TRUE);
9565 }
9566}
9567
9568/*
9569 * "o" and "O" commands.
9570 */
9571 static void
9572nv_open(cap)
9573 cmdarg_T *cap;
9574{
9575#ifdef FEAT_DIFF
9576 /* "do" is ":diffget" */
9577 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9578 {
9579 clearop(cap->oap);
9580 nv_diffgetput(FALSE);
9581 }
9582 else
9583#endif
9584#ifdef FEAT_VISUAL
9585 if (VIsual_active) /* switch start and end of visual */
9586 v_swap_corners(cap->cmdchar);
9587 else
9588#endif
9589 n_opencmd(cap);
9590}
9591
9592#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 static void
9594nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009595 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 ProcessSniffRequests();
9598}
9599#endif
9600
9601#ifdef FEAT_NETBEANS_INTG
9602 static void
9603nv_nbcmd(cap)
9604 cmdarg_T *cap;
9605{
9606 netbeans_keycommand(cap->nchar);
9607}
9608#endif
9609
9610#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 static void
9612nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009613 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9616}
9617#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009618
9619#ifdef FEAT_AUTOCMD
9620/*
9621 * Trigger CursorHold event.
9622 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9623 * input buffer. "did_cursorhold" is set to avoid retriggering.
9624 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009625 static void
9626nv_cursorhold(cap)
9627 cmdarg_T *cap;
9628{
9629 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9630 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009631 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009632}
9633#endif