blob: 013fdcee456c483e732b2c82b8363c66c13b40e7 [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);
4024 old_len = (int)STRLEN(showcmd_buf);
4025 extra_len = (int)STRLEN(p);
4026 overflow = old_len + extra_len - SHOWCMD_COLS;
4027 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004028 mch_memmove(showcmd_buf, showcmd_buf + overflow,
4029 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 STRCAT(showcmd_buf, p);
4031
4032 if (char_avail())
4033 return FALSE;
4034
4035 display_showcmd();
4036
4037 return TRUE;
4038}
4039
4040 void
4041add_to_showcmd_c(c)
4042 int c;
4043{
4044 if (!add_to_showcmd(c))
4045 setcursor();
4046}
4047
4048/*
4049 * Delete 'len' characters from the end of the shown command.
4050 */
4051 static void
4052del_from_showcmd(len)
4053 int len;
4054{
4055 int old_len;
4056
4057 if (!p_sc)
4058 return;
4059
4060 old_len = (int)STRLEN(showcmd_buf);
4061 if (len > old_len)
4062 len = old_len;
4063 showcmd_buf[old_len - len] = NUL;
4064
4065 if (!char_avail())
4066 display_showcmd();
4067}
4068
4069/*
4070 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4071 * something and there is a partial mapping.
4072 */
4073 void
4074push_showcmd()
4075{
4076 if (p_sc)
4077 STRCPY(old_showcmd_buf, showcmd_buf);
4078}
4079
4080 void
4081pop_showcmd()
4082{
4083 if (!p_sc)
4084 return;
4085
4086 STRCPY(showcmd_buf, old_showcmd_buf);
4087
4088 display_showcmd();
4089}
4090
4091 static void
4092display_showcmd()
4093{
4094 int len;
4095
4096 cursor_off();
4097
4098 len = (int)STRLEN(showcmd_buf);
4099 if (len == 0)
4100 showcmd_is_clear = TRUE;
4101 else
4102 {
4103 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4104 showcmd_is_clear = FALSE;
4105 }
4106
4107 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004108 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4109 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 */
4111 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4112
4113 setcursor(); /* put cursor back where it belongs */
4114}
4115#endif
4116
4117#ifdef FEAT_SCROLLBIND
4118/*
4119 * When "check" is FALSE, prepare for commands that scroll the window.
4120 * When "check" is TRUE, take care of scroll-binding after the window has
4121 * scrolled. Called from normal_cmd() and edit().
4122 */
4123 void
4124do_check_scrollbind(check)
4125 int check;
4126{
4127 static win_T *old_curwin = NULL;
4128 static linenr_T old_topline = 0;
4129#ifdef FEAT_DIFF
4130 static int old_topfill = 0;
4131#endif
4132 static buf_T *old_buf = NULL;
4133 static colnr_T old_leftcol = 0;
4134
4135 if (check && curwin->w_p_scb)
4136 {
4137 /* If a ":syncbind" command was just used, don't scroll, only reset
4138 * the values. */
4139 if (did_syncbind)
4140 did_syncbind = FALSE;
4141 else if (curwin == old_curwin)
4142 {
4143 /*
4144 * Synchronize other windows, as necessary according to
4145 * 'scrollbind'. Don't do this after an ":edit" command, except
4146 * when 'diff' is set.
4147 */
4148 if ((curwin->w_buffer == old_buf
4149#ifdef FEAT_DIFF
4150 || curwin->w_p_diff
4151#endif
4152 )
4153 && (curwin->w_topline != old_topline
4154#ifdef FEAT_DIFF
4155 || curwin->w_topfill != old_topfill
4156#endif
4157 || curwin->w_leftcol != old_leftcol))
4158 {
4159 check_scrollbind(curwin->w_topline - old_topline,
4160 (long)(curwin->w_leftcol - old_leftcol));
4161 }
4162 }
4163 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4164 {
4165 /*
4166 * When switching between windows, make sure that the relative
4167 * vertical offset is valid for the new window. The relative
4168 * offset is invalid whenever another 'scrollbind' window has
4169 * scrolled to a point that would force the current window to
4170 * scroll past the beginning or end of its buffer. When the
4171 * resync is performed, some of the other 'scrollbind' windows may
4172 * need to jump so that the current window's relative position is
4173 * visible on-screen.
4174 */
4175 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4176 }
4177 curwin->w_scbind_pos = curwin->w_topline;
4178 }
4179
4180 old_curwin = curwin;
4181 old_topline = curwin->w_topline;
4182#ifdef FEAT_DIFF
4183 old_topfill = curwin->w_topfill;
4184#endif
4185 old_buf = curwin->w_buffer;
4186 old_leftcol = curwin->w_leftcol;
4187}
4188
4189/*
4190 * Synchronize any windows that have "scrollbind" set, based on the
4191 * number of rows by which the current window has changed
4192 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4193 */
4194 void
4195check_scrollbind(topline_diff, leftcol_diff)
4196 linenr_T topline_diff;
4197 long leftcol_diff;
4198{
4199 int want_ver;
4200 int want_hor;
4201 win_T *old_curwin = curwin;
4202 buf_T *old_curbuf = curbuf;
4203#ifdef FEAT_VISUAL
4204 int old_VIsual_select = VIsual_select;
4205 int old_VIsual_active = VIsual_active;
4206#endif
4207 colnr_T tgt_leftcol = curwin->w_leftcol;
4208 long topline;
4209 long y;
4210
4211 /*
4212 * check 'scrollopt' string for vertical and horizontal scroll options
4213 */
4214 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4215#ifdef FEAT_DIFF
4216 want_ver |= old_curwin->w_p_diff;
4217#endif
4218 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4219
4220 /*
4221 * loop through the scrollbound windows and scroll accordingly
4222 */
4223#ifdef FEAT_VISUAL
4224 VIsual_select = VIsual_active = 0;
4225#endif
4226 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4227 {
4228 curbuf = curwin->w_buffer;
4229 /* skip original window and windows with 'noscrollbind' */
4230 if (curwin != old_curwin && curwin->w_p_scb)
4231 {
4232 /*
4233 * do the vertical scroll
4234 */
4235 if (want_ver)
4236 {
4237#ifdef FEAT_DIFF
4238 if (old_curwin->w_p_diff && curwin->w_p_diff)
4239 {
4240 diff_set_topline(old_curwin, curwin);
4241 }
4242 else
4243#endif
4244 {
4245 curwin->w_scbind_pos += topline_diff;
4246 topline = curwin->w_scbind_pos;
4247 if (topline > curbuf->b_ml.ml_line_count)
4248 topline = curbuf->b_ml.ml_line_count;
4249 if (topline < 1)
4250 topline = 1;
4251
4252 y = topline - curwin->w_topline;
4253 if (y > 0)
4254 scrollup(y, FALSE);
4255 else
4256 scrolldown(-y, FALSE);
4257 }
4258
4259 redraw_later(VALID);
4260 cursor_correct();
4261#ifdef FEAT_WINDOWS
4262 curwin->w_redr_status = TRUE;
4263#endif
4264 }
4265
4266 /*
4267 * do the horizontal scroll
4268 */
4269 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4270 {
4271 curwin->w_leftcol = tgt_leftcol;
4272 leftcol_changed();
4273 }
4274 }
4275 }
4276
4277 /*
4278 * reset current-window
4279 */
4280#ifdef FEAT_VISUAL
4281 VIsual_select = old_VIsual_select;
4282 VIsual_active = old_VIsual_active;
4283#endif
4284 curwin = old_curwin;
4285 curbuf = old_curbuf;
4286}
4287#endif /* #ifdef FEAT_SCROLLBIND */
4288
4289/*
4290 * Command character that's ignored.
4291 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004292 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 static void
4295nv_ignore(cap)
4296 cmdarg_T *cap;
4297{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004298 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299}
4300
4301/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004302 * Command character that doesn't do anything, but unlike nv_ignore() does
4303 * start edit(). Used for "startinsert" executed while starting up.
4304 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004305 static void
4306nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004307 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004308{
4309}
4310
4311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 * Command character doesn't exist.
4313 */
4314 static void
4315nv_error(cap)
4316 cmdarg_T *cap;
4317{
4318 clearopbeep(cap->oap);
4319}
4320
4321/*
4322 * <Help> and <F1> commands.
4323 */
4324 static void
4325nv_help(cap)
4326 cmdarg_T *cap;
4327{
4328 if (!checkclearopq(cap->oap))
4329 ex_help(NULL);
4330}
4331
4332/*
4333 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4334 */
4335 static void
4336nv_addsub(cap)
4337 cmdarg_T *cap;
4338{
4339 if (!checkclearopq(cap->oap)
4340 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4341 prep_redo_cmd(cap);
4342}
4343
4344/*
4345 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4346 */
4347 static void
4348nv_page(cap)
4349 cmdarg_T *cap;
4350{
4351 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004352 {
4353#ifdef FEAT_WINDOWS
4354 if (mod_mask & MOD_MASK_CTRL)
4355 {
4356 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4357 if (cap->arg == BACKWARD)
4358 goto_tabpage(-(int)cap->count1);
4359 else
4360 goto_tabpage((int)cap->count0);
4361 }
4362 else
4363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366}
4367
4368/*
4369 * Implementation of "gd" and "gD" command.
4370 */
4371 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004372nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004373 oparg_T *oap;
4374 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004375 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u *ptr;
4379
Bram Moolenaard9d30582005-05-18 22:10:28 +00004380 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004381 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004383#ifdef FEAT_FOLDING
4384 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4385 foldOpenCursor();
4386#endif
4387}
4388
4389/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004390 * Search for variable declaration of "ptr[len]".
4391 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4392 * current file ("gD").
4393 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 * Return FAIL when not found.
4395 */
4396 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004397find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 char_u *ptr;
4399 int len;
4400 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004401 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004402 int searchflags; /* flags passed to searchit() */
4403{
4404 char_u *pat;
4405 pos_T old_pos;
4406 pos_T par_pos;
4407 pos_T found_pos;
4408 int t;
4409 int save_p_ws;
4410 int save_p_scs;
4411 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004412 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004413
4414 if ((pat = alloc(len + 7)) == NULL)
4415 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004416
4417 /* Put "\V" before the pattern to avoid that the special meaning of "."
4418 * and "~" causes trouble. */
4419 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4420 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 old_pos = curwin->w_cursor;
4422 save_p_ws = p_ws;
4423 save_p_scs = p_scs;
4424 p_ws = FALSE; /* don't wrap around end of file now */
4425 p_scs = FALSE; /* don't switch ignorecase off now */
4426
4427 /*
4428 * With "gD" go to line 1.
4429 * With "gd" Search back for the start of the current function, then go
4430 * back until a blank line. If this fails go to line 1.
4431 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004432 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
4434 setpcmark(); /* Set in findpar() otherwise */
4435 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004436 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 else
4439 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004440 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4442 --curwin->w_cursor.lnum;
4443 }
4444 curwin->w_cursor.col = 0;
4445
4446 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004448 for (;;)
4449 {
4450 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004451 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004452 if (curwin->w_cursor.lnum >= old_pos.lnum)
4453 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004454
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004455 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004456 {
4457 pos_T *pos;
4458
4459 /* Check that the block the match is in doesn't end before the
4460 * position where we started the search from. */
4461 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4462 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4463 && pos->lnum < old_pos.lnum)
4464 continue;
4465 }
4466
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004467 if (t == FAIL)
4468 {
4469 /* If we previously found a valid position, use it. */
4470 if (found_pos.lnum != 0)
4471 {
4472 curwin->w_cursor = found_pos;
4473 t = OK;
4474 }
4475 break;
4476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004478 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004479 {
4480 /* Ignore this line, continue at start of next line. */
4481 ++curwin->w_cursor.lnum;
4482 curwin->w_cursor.col = 0;
4483 continue;
4484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004486 if (!locally) /* global search: use first match found */
4487 break;
4488 if (curwin->w_cursor.lnum >= par_pos.lnum)
4489 {
4490 /* If we previously found a valid position, use it. */
4491 if (found_pos.lnum != 0)
4492 curwin->w_cursor = found_pos;
4493 break;
4494 }
4495
4496 /* For finding a local variable and the match is before the "{" search
4497 * to find a later match. For K&R style function declarations this
4498 * skips the function header without types. */
4499 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004501
4502 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004504 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 curwin->w_cursor = old_pos;
4506 }
4507 else
4508 {
4509 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 /* "n" searches forward now */
4511 reset_search_dir();
4512 }
4513
4514 vim_free(pat);
4515 p_ws = save_p_ws;
4516 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004517
4518 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519}
4520
4521/*
4522 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4523 * lines rather than lines in the file.
4524 * 'dist' must be positive.
4525 *
4526 * Return OK if able to move cursor, FAIL otherwise.
4527 */
4528 static int
4529nv_screengo(oap, dir, dist)
4530 oparg_T *oap;
4531 int dir;
4532 long dist;
4533{
4534 int linelen = linetabsize(ml_get_curline());
4535 int retval = OK;
4536 int atend = FALSE;
4537 int n;
4538 int col_off1; /* margin offset for first screen line */
4539 int col_off2; /* margin offset for wrapped screen line */
4540 int width1; /* text width for first screen line */
4541 int width2; /* test width for wrapped screen line */
4542
4543 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004544 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546 col_off1 = curwin_col_off();
4547 col_off2 = col_off1 - curwin_col_off2();
4548 width1 = W_WIDTH(curwin) - col_off1;
4549 width2 = W_WIDTH(curwin) - col_off2;
4550
4551#ifdef FEAT_VERTSPLIT
4552 if (curwin->w_width != 0)
4553 {
4554#endif
4555 /*
4556 * Instead of sticking at the last character of the buffer line we
4557 * try to stick in the last column of the screen.
4558 */
4559 if (curwin->w_curswant == MAXCOL)
4560 {
4561 atend = TRUE;
4562 validate_virtcol();
4563 if (width1 <= 0)
4564 curwin->w_curswant = 0;
4565 else
4566 {
4567 curwin->w_curswant = width1 - 1;
4568 if (curwin->w_virtcol > curwin->w_curswant)
4569 curwin->w_curswant += ((curwin->w_virtcol
4570 - curwin->w_curswant - 1) / width2 + 1) * width2;
4571 }
4572 }
4573 else
4574 {
4575 if (linelen > width1)
4576 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4577 else
4578 n = width1;
4579 if (curwin->w_curswant > (colnr_T)n + 1)
4580 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4581 * width2;
4582 }
4583
4584 while (dist--)
4585 {
4586 if (dir == BACKWARD)
4587 {
4588 if ((long)curwin->w_curswant >= width2)
4589 /* move back within line */
4590 curwin->w_curswant -= width2;
4591 else
4592 {
4593 /* to previous line */
4594 if (curwin->w_cursor.lnum == 1)
4595 {
4596 retval = FAIL;
4597 break;
4598 }
4599 --curwin->w_cursor.lnum;
4600#ifdef FEAT_FOLDING
4601 /* Move to the start of a closed fold. Don't do that when
4602 * 'foldopen' contains "all": it will open in a moment. */
4603 if (!(fdo_flags & FDO_ALL))
4604 (void)hasFolding(curwin->w_cursor.lnum,
4605 &curwin->w_cursor.lnum, NULL);
4606#endif
4607 linelen = linetabsize(ml_get_curline());
4608 if (linelen > width1)
4609 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4610 + 1) * width2;
4611 }
4612 }
4613 else /* dir == FORWARD */
4614 {
4615 if (linelen > width1)
4616 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4617 else
4618 n = width1;
4619 if (curwin->w_curswant + width2 < (colnr_T)n)
4620 /* move forward within line */
4621 curwin->w_curswant += width2;
4622 else
4623 {
4624 /* to next line */
4625#ifdef FEAT_FOLDING
4626 /* Move to the end of a closed fold. */
4627 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4628 &curwin->w_cursor.lnum);
4629#endif
4630 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4631 {
4632 retval = FAIL;
4633 break;
4634 }
4635 curwin->w_cursor.lnum++;
4636 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004637 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 }
4640 }
4641#ifdef FEAT_VERTSPLIT
4642 }
4643#endif
4644
4645 coladvance(curwin->w_curswant);
4646
4647#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4648 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4649 {
4650 /*
4651 * Check for landing on a character that got split at the end of the
4652 * last line. We want to advance a screenline, not end up in the same
4653 * screenline or move two screenlines.
4654 */
4655 validate_virtcol();
4656 if (curwin->w_virtcol > curwin->w_curswant
4657 && (curwin->w_curswant < (colnr_T)width1
4658 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4659 : ((curwin->w_curswant - width1) % width2
4660 > (colnr_T)width2 / 2)))
4661 --curwin->w_cursor.col;
4662 }
4663#endif
4664
4665 if (atend)
4666 curwin->w_curswant = MAXCOL; /* stick in the last column */
4667
4668 return retval;
4669}
4670
4671#ifdef FEAT_MOUSE
4672/*
4673 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4674 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004675 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4676 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 */
4678 static void
4679nv_mousescroll(cap)
4680 cmdarg_T *cap;
4681{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004682# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004683 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004685 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
4687 int row, col;
4688
4689 row = mouse_row;
4690 col = mouse_col;
4691
4692 /* find the window at the pointer coordinates */
4693 curwin = mouse_find_win(&row, &col);
4694 curbuf = curwin->w_buffer;
4695 }
4696# endif
4697
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004698 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004700 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4701 {
4702 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4703 }
4704 else
4705 {
4706 cap->count1 = 3;
4707 cap->count0 = 3;
4708 nv_scroll_line(cap);
4709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004711# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 else
4713 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004714 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4715 if (!curwin->w_p_wrap)
4716 {
4717 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004718
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004719 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4720 step = W_WIDTH(curwin);
4721 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4722 if (val < 0)
4723 val = 0;
4724
4725 gui_do_horiz_scroll(val, TRUE);
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004730# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 curwin->w_redr_status = TRUE;
4732
4733 curwin = old_curwin;
4734 curbuf = curwin->w_buffer;
4735# endif
4736}
4737
4738/*
4739 * Mouse clicks and drags.
4740 */
4741 static void
4742nv_mouse(cap)
4743 cmdarg_T *cap;
4744{
4745 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4746}
4747#endif
4748
4749/*
4750 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4751 * cap->arg must be TRUE for CTRL-E.
4752 */
4753 static void
4754nv_scroll_line(cap)
4755 cmdarg_T *cap;
4756{
4757 if (!checkclearop(cap->oap))
4758 scroll_redraw(cap->arg, cap->count1);
4759}
4760
4761/*
4762 * Scroll "count" lines up or down, and redraw.
4763 */
4764 void
4765scroll_redraw(up, count)
4766 int up;
4767 long count;
4768{
4769 linenr_T prev_topline = curwin->w_topline;
4770#ifdef FEAT_DIFF
4771 int prev_topfill = curwin->w_topfill;
4772#endif
4773 linenr_T prev_lnum = curwin->w_cursor.lnum;
4774
4775 if (up)
4776 scrollup(count, TRUE);
4777 else
4778 scrolldown(count, TRUE);
4779 if (p_so)
4780 {
4781 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4782 * valid, otherwise the screen jumps back at the end of the file. */
4783 cursor_correct();
4784 check_cursor_moved(curwin);
4785 curwin->w_valid |= VALID_TOPLINE;
4786
4787 /* If moved back to where we were, at least move the cursor, otherwise
4788 * we get stuck at one position. Don't move the cursor up if the
4789 * first line of the buffer is already on the screen */
4790 while (curwin->w_topline == prev_topline
4791#ifdef FEAT_DIFF
4792 && curwin->w_topfill == prev_topfill
4793#endif
4794 )
4795 {
4796 if (up)
4797 {
4798 if (curwin->w_cursor.lnum > prev_lnum
4799 || cursor_down(1L, FALSE) == FAIL)
4800 break;
4801 }
4802 else
4803 {
4804 if (curwin->w_cursor.lnum < prev_lnum
4805 || prev_topline == 1L
4806 || cursor_up(1L, FALSE) == FAIL)
4807 break;
4808 }
4809 /* Mark w_topline as valid, otherwise the screen jumps back at the
4810 * end of the file. */
4811 check_cursor_moved(curwin);
4812 curwin->w_valid |= VALID_TOPLINE;
4813 }
4814 }
4815 if (curwin->w_cursor.lnum != prev_lnum)
4816 coladvance(curwin->w_curswant);
4817 redraw_later(VALID);
4818}
4819
4820/*
4821 * Commands that start with "z".
4822 */
4823 static void
4824nv_zet(cap)
4825 cmdarg_T *cap;
4826{
4827 long n;
4828 colnr_T col;
4829 int nchar = cap->nchar;
4830#ifdef FEAT_FOLDING
4831 long old_fdl = curwin->w_p_fdl;
4832 int old_fen = curwin->w_p_fen;
4833#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004834#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004835 int undo = FALSE;
4836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
4838 if (VIM_ISDIGIT(nchar))
4839 {
4840 /*
4841 * "z123{nchar}": edit the count before obtaining {nchar}
4842 */
4843 if (checkclearop(cap->oap))
4844 return;
4845 n = nchar - '0';
4846 for (;;)
4847 {
4848#ifdef USE_ON_FLY_SCROLL
4849 dont_scroll = TRUE; /* disallow scrolling here */
4850#endif
4851 ++no_mapping;
4852 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004853 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 --no_mapping;
4856 --allow_keys;
4857#ifdef FEAT_CMDL_INFO
4858 (void)add_to_showcmd(nchar);
4859#endif
4860 if (nchar == K_DEL || nchar == K_KDEL)
4861 n /= 10;
4862 else if (VIM_ISDIGIT(nchar))
4863 n = n * 10 + (nchar - '0');
4864 else if (nchar == CAR)
4865 {
4866#ifdef FEAT_GUI
4867 need_mouse_correct = TRUE;
4868#endif
4869 win_setheight((int)n);
4870 break;
4871 }
4872 else if (nchar == 'l'
4873 || nchar == 'h'
4874 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004875 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 cap->count1 = n ? n * cap->count1 : cap->count1;
4878 goto dozet;
4879 }
4880 else
4881 {
4882 clearopbeep(cap->oap);
4883 break;
4884 }
4885 }
4886 cap->oap->op_type = OP_NOP;
4887 return;
4888 }
4889
4890dozet:
4891 if (
4892#ifdef FEAT_FOLDING
4893 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4894 * and "zC" only in Visual mode. "zj" and "zk" are motion
4895 * commands. */
4896 cap->nchar != 'f' && cap->nchar != 'F'
4897 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4898 && cap->nchar != 'j' && cap->nchar != 'k'
4899 &&
4900#endif
4901 checkclearop(cap->oap))
4902 return;
4903
4904 /*
4905 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4906 * If line number given, set cursor.
4907 */
4908 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4909 && cap->count0
4910 && cap->count0 != curwin->w_cursor.lnum)
4911 {
4912 setpcmark();
4913 if (cap->count0 > curbuf->b_ml.ml_line_count)
4914 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4915 else
4916 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004917 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919
4920 switch (nchar)
4921 {
4922 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4923 case '+':
4924 if (cap->count0 == 0)
4925 {
4926 /* No count given: put cursor at the line below screen */
4927 validate_botline(); /* make sure w_botline is valid */
4928 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4929 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4930 else
4931 curwin->w_cursor.lnum = curwin->w_botline;
4932 }
4933 /* FALLTHROUGH */
4934 case NL:
4935 case CAR:
4936 case K_KENTER:
4937 beginline(BL_WHITE | BL_FIX);
4938 /* FALLTHROUGH */
4939
4940 case 't': scroll_cursor_top(0, TRUE);
4941 redraw_later(VALID);
4942 break;
4943
4944 /* "z." and "zz": put cursor in middle of screen */
4945 case '.': beginline(BL_WHITE | BL_FIX);
4946 /* FALLTHROUGH */
4947
4948 case 'z': scroll_cursor_halfway(TRUE);
4949 redraw_later(VALID);
4950 break;
4951
4952 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4953 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4954 * when <count> is at bottom of window, and puts that one at
4955 * bottom of window. */
4956 if (cap->count0 != 0)
4957 {
4958 scroll_cursor_bot(0, TRUE);
4959 curwin->w_cursor.lnum = curwin->w_topline;
4960 }
4961 else if (curwin->w_topline == 1)
4962 curwin->w_cursor.lnum = 1;
4963 else
4964 curwin->w_cursor.lnum = curwin->w_topline - 1;
4965 /* FALLTHROUGH */
4966 case '-':
4967 beginline(BL_WHITE | BL_FIX);
4968 /* FALLTHROUGH */
4969
4970 case 'b': scroll_cursor_bot(0, TRUE);
4971 redraw_later(VALID);
4972 break;
4973
4974 /* "zH" - scroll screen right half-page */
4975 case 'H':
4976 cap->count1 *= W_WIDTH(curwin) / 2;
4977 /* FALLTHROUGH */
4978
4979 /* "zh" - scroll screen to the right */
4980 case 'h':
4981 case K_LEFT:
4982 if (!curwin->w_p_wrap)
4983 {
4984 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4985 curwin->w_leftcol = 0;
4986 else
4987 curwin->w_leftcol -= (colnr_T)cap->count1;
4988 leftcol_changed();
4989 }
4990 break;
4991
4992 /* "zL" - scroll screen left half-page */
4993 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4994 /* FALLTHROUGH */
4995
4996 /* "zl" - scroll screen to the left */
4997 case 'l':
4998 case K_RIGHT:
4999 if (!curwin->w_p_wrap)
5000 {
5001 /* scroll the window left */
5002 curwin->w_leftcol += (colnr_T)cap->count1;
5003 leftcol_changed();
5004 }
5005 break;
5006
5007 /* "zs" - scroll screen, cursor at the start */
5008 case 's': if (!curwin->w_p_wrap)
5009 {
5010#ifdef FEAT_FOLDING
5011 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5012 col = 0; /* like the cursor is in col 0 */
5013 else
5014#endif
5015 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
5016 if ((long)col > p_siso)
5017 col -= p_siso;
5018 else
5019 col = 0;
5020 if (curwin->w_leftcol != col)
5021 {
5022 curwin->w_leftcol = col;
5023 redraw_later(NOT_VALID);
5024 }
5025 }
5026 break;
5027
5028 /* "ze" - scroll screen, cursor at the end */
5029 case 'e': if (!curwin->w_p_wrap)
5030 {
5031#ifdef FEAT_FOLDING
5032 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5033 col = 0; /* like the cursor is in col 0 */
5034 else
5035#endif
5036 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5037 n = W_WIDTH(curwin) - curwin_col_off();
5038 if ((long)col + p_siso < n)
5039 col = 0;
5040 else
5041 col = col + p_siso - n + 1;
5042 if (curwin->w_leftcol != col)
5043 {
5044 curwin->w_leftcol = col;
5045 redraw_later(NOT_VALID);
5046 }
5047 }
5048 break;
5049
5050#ifdef FEAT_FOLDING
5051 /* "zF": create fold command */
5052 /* "zf": create fold operator */
5053 case 'F':
5054 case 'f': if (foldManualAllowed(TRUE))
5055 {
5056 cap->nchar = 'f';
5057 nv_operator(cap);
5058 curwin->w_p_fen = TRUE;
5059
5060 /* "zF" is like "zfzf" */
5061 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5062 {
5063 nv_operator(cap);
5064 finish_op = TRUE;
5065 }
5066 }
5067 else
5068 clearopbeep(cap->oap);
5069 break;
5070
5071 /* "zd": delete fold at cursor */
5072 /* "zD": delete fold at cursor recursively */
5073 case 'd':
5074 case 'D': if (foldManualAllowed(FALSE))
5075 {
5076 if (VIsual_active)
5077 nv_operator(cap);
5078 else
5079 deleteFold(curwin->w_cursor.lnum,
5080 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5081 }
5082 break;
5083
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005084 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 case 'E': if (foldmethodIsManual(curwin))
5086 {
5087 clearFolding(curwin);
5088 changed_window_setting();
5089 }
5090 else if (foldmethodIsMarker(curwin))
5091 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5092 TRUE, FALSE);
5093 else
5094 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5095 break;
5096
5097 /* "zn": fold none: reset 'foldenable' */
5098 case 'n': curwin->w_p_fen = FALSE;
5099 break;
5100
5101 /* "zN": fold Normal: set 'foldenable' */
5102 case 'N': curwin->w_p_fen = TRUE;
5103 break;
5104
5105 /* "zi": invert folding: toggle 'foldenable' */
5106 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5107 break;
5108
5109 /* "za": open closed fold or close open fold at cursor */
5110 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5111 openFold(curwin->w_cursor.lnum, cap->count1);
5112 else
5113 {
5114 closeFold(curwin->w_cursor.lnum, cap->count1);
5115 curwin->w_p_fen = TRUE;
5116 }
5117 break;
5118
5119 /* "zA": open fold at cursor recursively */
5120 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5121 openFoldRecurse(curwin->w_cursor.lnum);
5122 else
5123 {
5124 closeFoldRecurse(curwin->w_cursor.lnum);
5125 curwin->w_p_fen = TRUE;
5126 }
5127 break;
5128
5129 /* "zo": open fold at cursor or Visual area */
5130 case 'o': if (VIsual_active)
5131 nv_operator(cap);
5132 else
5133 openFold(curwin->w_cursor.lnum, cap->count1);
5134 break;
5135
5136 /* "zO": open fold recursively */
5137 case 'O': if (VIsual_active)
5138 nv_operator(cap);
5139 else
5140 openFoldRecurse(curwin->w_cursor.lnum);
5141 break;
5142
5143 /* "zc": close fold at cursor or Visual area */
5144 case 'c': if (VIsual_active)
5145 nv_operator(cap);
5146 else
5147 closeFold(curwin->w_cursor.lnum, cap->count1);
5148 curwin->w_p_fen = TRUE;
5149 break;
5150
5151 /* "zC": close fold recursively */
5152 case 'C': if (VIsual_active)
5153 nv_operator(cap);
5154 else
5155 closeFoldRecurse(curwin->w_cursor.lnum);
5156 curwin->w_p_fen = TRUE;
5157 break;
5158
5159 /* "zv": open folds at the cursor */
5160 case 'v': foldOpenCursor();
5161 break;
5162
5163 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5164 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005165 curwin->w_foldinvalid = TRUE; /* recompute folds */
5166 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 foldOpenCursor();
5168 break;
5169
5170 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5171 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005172 curwin->w_foldinvalid = TRUE; /* recompute folds */
5173 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /* "zm": fold more */
5177 case 'm': if (curwin->w_p_fdl > 0)
5178 --curwin->w_p_fdl;
5179 old_fdl = -1; /* force an update */
5180 curwin->w_p_fen = TRUE;
5181 break;
5182
5183 /* "zM": close all folds */
5184 case 'M': curwin->w_p_fdl = 0;
5185 old_fdl = -1; /* force an update */
5186 curwin->w_p_fen = TRUE;
5187 break;
5188
5189 /* "zr": reduce folding */
5190 case 'r': ++curwin->w_p_fdl;
5191 break;
5192
5193 /* "zR": open all folds */
5194 case 'R': curwin->w_p_fdl = getDeepestNesting();
5195 old_fdl = -1; /* force an update */
5196 break;
5197
5198 case 'j': /* "zj" move to next fold downwards */
5199 case 'k': /* "zk" move to next fold upwards */
5200 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5201 cap->count1) == FAIL)
5202 clearopbeep(cap->oap);
5203 break;
5204
5205#endif /* FEAT_FOLDING */
5206
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005207#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005208 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5209 ++no_mapping;
5210 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005211 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005212 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005213 --no_mapping;
5214 --allow_keys;
5215#ifdef FEAT_CMDL_INFO
5216 (void)add_to_showcmd(nchar);
5217#endif
5218 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5219 {
5220 clearopbeep(cap->oap);
5221 break;
5222 }
5223 undo = TRUE;
5224 /*FALLTHROUGH*/
5225
Bram Moolenaarb765d632005-06-07 21:00:02 +00005226 case 'g': /* "zg": add good word to word list */
5227 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005228 case 'G': /* "zG": add good word to temp word list */
5229 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005230 {
5231 char_u *ptr = NULL;
5232 int len;
5233
5234 if (checkclearop(cap->oap))
5235 break;
5236# ifdef FEAT_VISUAL
5237 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5238 == FAIL)
5239 return;
5240# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005241 if (ptr == NULL)
5242 {
5243 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005244
Bram Moolenaar134bf072013-09-25 18:54:24 +02005245 /* Find bad word under the cursor. When 'spell' is
5246 * off this fails and find_ident_under_cursor() is
5247 * used below. */
5248 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005249 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005250 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005251 if (len != 0 && curwin->w_cursor.col <= pos.col)
5252 ptr = ml_get_pos(&curwin->w_cursor);
5253 curwin->w_cursor = pos;
5254 }
5255
Bram Moolenaarb765d632005-06-07 21:00:02 +00005256 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5257 FIND_IDENT)) == 0)
5258 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005259 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005260 (nchar == 'G' || nchar == 'W')
5261 ? 0 : (int)cap->count1,
5262 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005263 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005264 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265
Bram Moolenaar43abc522005-12-10 20:15:02 +00005266 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005267 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005268 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005269 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005270#endif
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 default: clearopbeep(cap->oap);
5273 }
5274
5275#ifdef FEAT_FOLDING
5276 /* Redraw when 'foldenable' changed */
5277 if (old_fen != curwin->w_p_fen)
5278 {
5279# ifdef FEAT_DIFF
5280 win_T *wp;
5281
5282 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5283 {
5284 /* Adjust 'foldenable' in diff-synced windows. */
5285 FOR_ALL_WINDOWS(wp)
5286 {
5287 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5288 {
5289 wp->w_p_fen = curwin->w_p_fen;
5290 changed_window_setting_win(wp);
5291 }
5292 }
5293 }
5294# endif
5295 changed_window_setting();
5296 }
5297
5298 /* Redraw when 'foldlevel' changed. */
5299 if (old_fdl != curwin->w_p_fdl)
5300 newFoldLevel();
5301#endif
5302}
5303
5304#ifdef FEAT_GUI
5305/*
5306 * Vertical scrollbar movement.
5307 */
5308 static void
5309nv_ver_scrollbar(cap)
5310 cmdarg_T *cap;
5311{
5312 if (cap->oap->op_type != OP_NOP)
5313 clearopbeep(cap->oap);
5314
5315 /* Even if an operator was pending, we still want to scroll */
5316 gui_do_scroll();
5317}
5318
5319/*
5320 * Horizontal scrollbar movement.
5321 */
5322 static void
5323nv_hor_scrollbar(cap)
5324 cmdarg_T *cap;
5325{
5326 if (cap->oap->op_type != OP_NOP)
5327 clearopbeep(cap->oap);
5328
5329 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005330 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331}
5332#endif
5333
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005334#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005335/*
5336 * Click in GUI tab.
5337 */
5338 static void
5339nv_tabline(cap)
5340 cmdarg_T *cap;
5341{
5342 if (cap->oap->op_type != OP_NOP)
5343 clearopbeep(cap->oap);
5344
5345 /* Even if an operator was pending, we still want to jump tabs. */
5346 goto_tabpage(current_tab);
5347}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005348
5349/*
5350 * Selected item in tab line menu.
5351 */
5352 static void
5353nv_tabmenu(cap)
5354 cmdarg_T *cap;
5355{
5356 if (cap->oap->op_type != OP_NOP)
5357 clearopbeep(cap->oap);
5358
5359 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005360 handle_tabmenu();
5361}
5362
5363/*
5364 * Handle selecting an item of the GUI tab line menu.
5365 * Used in Normal and Insert mode.
5366 */
5367 void
5368handle_tabmenu()
5369{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005370 switch (current_tabmenu)
5371 {
5372 case TABLINE_MENU_CLOSE:
5373 if (current_tab == 0)
5374 do_cmdline_cmd((char_u *)"tabclose");
5375 else
5376 {
5377 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5378 current_tab);
5379 do_cmdline_cmd(IObuff);
5380 }
5381 break;
5382
5383 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005384 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5385 current_tab > 0 ? current_tab - 1 : 999);
5386 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005387 break;
5388
5389 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005390 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5391 current_tab > 0 ? current_tab - 1 : 999);
5392 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005393 break;
5394 }
5395}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005396#endif
5397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398/*
5399 * "Q" command.
5400 */
5401 static void
5402nv_exmode(cap)
5403 cmdarg_T *cap;
5404{
5405 /*
5406 * Ignore 'Q' in Visual mode, just give a beep.
5407 */
5408#ifdef FEAT_VISUAL
5409 if (VIsual_active)
5410 vim_beep();
5411 else
5412#endif
5413 if (!checkclearop(cap->oap))
5414 do_exmode(FALSE);
5415}
5416
5417/*
5418 * Handle a ":" command.
5419 */
5420 static void
5421nv_colon(cap)
5422 cmdarg_T *cap;
5423{
5424 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005425 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427#ifdef FEAT_VISUAL
5428 if (VIsual_active)
5429 nv_operator(cap);
5430 else
5431#endif
5432 {
5433 if (cap->oap->op_type != OP_NOP)
5434 {
5435 /* Using ":" as a movement is characterwise exclusive. */
5436 cap->oap->motion_type = MCHAR;
5437 cap->oap->inclusive = FALSE;
5438 }
5439 else if (cap->count0)
5440 {
5441 /* translate "count:" into ":.,.+(count - 1)" */
5442 stuffcharReadbuff('.');
5443 if (cap->count0 > 1)
5444 {
5445 stuffReadbuff((char_u *)",.+");
5446 stuffnumReadbuff((long)cap->count0 - 1L);
5447 }
5448 }
5449
5450 /* When typing, don't type below an old message */
5451 if (KeyTyped)
5452 compute_cmdrow();
5453
5454 old_p_im = p_im;
5455
5456 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005457 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5459
5460 /* If 'insertmode' changed, enter or exit Insert mode */
5461 if (p_im != old_p_im)
5462 {
5463 if (p_im)
5464 restart_edit = 'i';
5465 else
5466 restart_edit = 0;
5467 }
5468
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005469 if (cmd_result == FAIL)
5470 /* The Ex command failed, do not execute the operator. */
5471 clearop(cap->oap);
5472 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5474 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005475 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5476 || did_emsg
5477 ))
5478 /* The start of the operator has become invalid by the Ex command.
5479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 clearopbeep(cap->oap);
5481 }
5482}
5483
5484/*
5485 * Handle CTRL-G command.
5486 */
5487 static void
5488nv_ctrlg(cap)
5489 cmdarg_T *cap;
5490{
5491#ifdef FEAT_VISUAL
5492 if (VIsual_active) /* toggle Selection/Visual mode */
5493 {
5494 VIsual_select = !VIsual_select;
5495 showmode();
5496 }
5497 else
5498#endif
5499 if (!checkclearop(cap->oap))
5500 /* print full name if count given or :cd used */
5501 fileinfo((int)cap->count0, FALSE, TRUE);
5502}
5503
5504/*
5505 * Handle CTRL-H <Backspace> command.
5506 */
5507 static void
5508nv_ctrlh(cap)
5509 cmdarg_T *cap;
5510{
5511#ifdef FEAT_VISUAL
5512 if (VIsual_active && VIsual_select)
5513 {
5514 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5515 v_visop(cap);
5516 }
5517 else
5518#endif
5519 nv_left(cap);
5520}
5521
5522/*
5523 * CTRL-L: clear screen and redraw.
5524 */
5525 static void
5526nv_clear(cap)
5527 cmdarg_T *cap;
5528{
5529 if (!checkclearop(cap->oap))
5530 {
5531#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5532 /*
5533 * Right now, the BeBox doesn't seem to have an easy way to detect
5534 * window resizing, so we cheat and make the user detect it
5535 * manually with CTRL-L instead
5536 */
5537 ui_get_shellsize();
5538#endif
5539#ifdef FEAT_SYN_HL
5540 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005541 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542#endif
5543 redraw_later(CLEAR);
5544 }
5545}
5546
5547/*
5548 * CTRL-O: In Select mode: switch to Visual mode for one command.
5549 * Otherwise: Go to older pcmark.
5550 */
5551 static void
5552nv_ctrlo(cap)
5553 cmdarg_T *cap;
5554{
5555#ifdef FEAT_VISUAL
5556 if (VIsual_active && VIsual_select)
5557 {
5558 VIsual_select = FALSE;
5559 showmode();
5560 restart_VIsual_select = 2; /* restart Select mode later */
5561 }
5562 else
5563#endif
5564 {
5565 cap->count1 = -cap->count1;
5566 nv_pcmark(cap);
5567 }
5568}
5569
5570/*
5571 * CTRL-^ command, short for ":e #"
5572 */
5573 static void
5574nv_hat(cap)
5575 cmdarg_T *cap;
5576{
5577 if (!checkclearopq(cap->oap))
5578 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5579 GETF_SETMARK|GETF_ALT, FALSE);
5580}
5581
5582/*
5583 * "Z" commands.
5584 */
5585 static void
5586nv_Zet(cap)
5587 cmdarg_T *cap;
5588{
5589 if (!checkclearopq(cap->oap))
5590 {
5591 switch (cap->nchar)
5592 {
5593 /* "ZZ": equivalent to ":x". */
5594 case 'Z': do_cmdline_cmd((char_u *)"x");
5595 break;
5596
5597 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5598 case 'Q': do_cmdline_cmd((char_u *)"q!");
5599 break;
5600
5601 default: clearopbeep(cap->oap);
5602 }
5603 }
5604}
5605
5606#if defined(FEAT_WINDOWS) || defined(PROTO)
5607/*
5608 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5609 */
5610 void
5611do_nv_ident(c1, c2)
5612 int c1;
5613 int c2;
5614{
5615 oparg_T oa;
5616 cmdarg_T ca;
5617
5618 clear_oparg(&oa);
5619 vim_memset(&ca, 0, sizeof(ca));
5620 ca.oap = &oa;
5621 ca.cmdchar = c1;
5622 ca.nchar = c2;
5623 nv_ident(&ca);
5624}
5625#endif
5626
5627/*
5628 * Handle the commands that use the word under the cursor.
5629 * [g] CTRL-] :ta to current identifier
5630 * [g] 'K' run program for current identifier
5631 * [g] '*' / to current identifier or string
5632 * [g] '#' ? to current identifier or string
5633 * g ']' :tselect for current identifier
5634 */
5635 static void
5636nv_ident(cap)
5637 cmdarg_T *cap;
5638{
5639 char_u *ptr = NULL;
5640 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005641 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 char_u *p;
5643 char_u *kp; /* value of 'keywordprg' */
5644 int kp_help; /* 'keywordprg' is ":help" */
5645 int n = 0; /* init for GCC */
5646 int cmdchar;
5647 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005648 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 char_u *aux_ptr;
5650 int isman;
5651 int isman_s;
5652
5653 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5654 {
5655 cmdchar = cap->nchar;
5656 g_cmd = TRUE;
5657 }
5658 else
5659 {
5660 cmdchar = cap->cmdchar;
5661 g_cmd = FALSE;
5662 }
5663
5664 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5665 cmdchar = '#';
5666
5667 /*
5668 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5669 */
5670 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5671 {
5672#ifdef FEAT_VISUAL
5673 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5674 return;
5675#endif
5676 if (checkclearopq(cap->oap))
5677 return;
5678 }
5679
5680 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5681 (cmdchar == '*' || cmdchar == '#')
5682 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5683 {
5684 clearop(cap->oap);
5685 return;
5686 }
5687
5688 /* Allocate buffer to put the command in. Inserting backslashes can
5689 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5690 * and some numbers. */
5691 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5692 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5693 || STRCMP(kp, ":help") == 0);
5694 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5695 if (buf == NULL)
5696 return;
5697 buf[0] = NUL;
5698
5699 switch (cmdchar)
5700 {
5701 case '*':
5702 case '#':
5703 /*
5704 * Put cursor at start of word, makes search skip the word
5705 * under the cursor.
5706 * Call setpcmark() first, so "*``" puts the cursor back where
5707 * it was.
5708 */
5709 setpcmark();
5710 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5711
5712 if (!g_cmd && vim_iswordp(ptr))
5713 STRCPY(buf, "\\<");
5714 no_smartcase = TRUE; /* don't use 'smartcase' now */
5715 break;
5716
5717 case 'K':
5718 if (kp_help)
5719 STRCPY(buf, "he! ");
5720 else
5721 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005722 /* An external command will probably use an argument starting
5723 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005724 while (*ptr == '-' && n > 0)
5725 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005726 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005727 --n;
5728 }
5729 if (n == 0)
5730 {
5731 EMSG(_(e_noident)); /* found dashes only */
5732 vim_free(buf);
5733 return;
5734 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 /* When a count is given, turn it into a range. Is this
5737 * really what we want? */
5738 isman = (STRCMP(kp, "man") == 0);
5739 isman_s = (STRCMP(kp, "man -s") == 0);
5740 if (cap->count0 != 0 && !(isman || isman_s))
5741 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5742
5743 STRCAT(buf, "! ");
5744 if (cap->count0 == 0 && isman_s)
5745 STRCAT(buf, "man");
5746 else
5747 STRCAT(buf, kp);
5748 STRCAT(buf, " ");
5749 if (cap->count0 != 0 && (isman || isman_s))
5750 {
5751 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5752 STRCAT(buf, " ");
5753 }
5754 }
5755 break;
5756
5757 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005758 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#ifdef FEAT_CSCOPE
5760 if (p_cst)
5761 STRCPY(buf, "cstag ");
5762 else
5763#endif
5764 STRCPY(buf, "ts ");
5765 break;
5766
5767 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005768 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (curbuf->b_help)
5770 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005772 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005773 if (g_cmd)
5774 STRCPY(buf, "tj ");
5775 else
5776 sprintf((char *)buf, "%ldta ", cap->count0);
5777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779
5780 /*
5781 * Now grab the chars in the identifier
5782 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005783 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005785 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005786 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005787 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005788 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005789 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005791 vim_free(buf);
5792 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005794 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5795 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005796 {
5797 vim_free(buf);
5798 vim_free(p);
5799 return;
5800 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005801 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005802 STRCAT(buf, p);
5803 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005805 else
5806 {
5807 if (cmdchar == '*')
5808 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5809 else if (cmdchar == '#')
5810 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005811 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005812 {
5813 if (curbuf->b_help)
5814 /* ":help" handles unescaped argument */
5815 aux_ptr = (char_u *)"";
5816 else
5817 aux_ptr = (char_u *)"\\|\"\n[";
5818 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005819 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005820 aux_ptr = (char_u *)"\\|\"\n*?[";
5821
5822 p = buf + STRLEN(buf);
5823 while (n-- > 0)
5824 {
5825 /* put a backslash before \ and some others */
5826 if (vim_strchr(aux_ptr, *ptr) != NULL)
5827 *p++ = '\\';
5828#ifdef FEAT_MBYTE
5829 /* When current byte is a part of multibyte character, copy all
5830 * bytes of that character. */
5831 if (has_mbyte)
5832 {
5833 int i;
5834 int len = (*mb_ptr2len)(ptr) - 1;
5835
5836 for (i = 0; i < len && n >= 1; ++i, --n)
5837 *p++ = *ptr++;
5838 }
5839#endif
5840 *p++ = *ptr++;
5841 }
5842 *p = NUL;
5843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
5845 /*
5846 * Execute the command.
5847 */
5848 if (cmdchar == '*' || cmdchar == '#')
5849 {
5850 if (!g_cmd && (
5851#ifdef FEAT_MBYTE
5852 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5853#endif
5854 vim_iswordc(ptr[-1])))
5855 STRCAT(buf, "\\>");
5856#ifdef FEAT_CMDHIST
5857 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005858 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5860#endif
5861 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5862 }
5863 else
5864 do_cmdline_cmd(buf);
5865
5866 vim_free(buf);
5867}
5868
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005869#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
5871 * Get visually selected text, within one line only.
5872 * Returns FAIL if more than one line selected.
5873 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005874 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875get_visual_text(cap, pp, lenp)
5876 cmdarg_T *cap;
5877 char_u **pp; /* return: start of selected text */
5878 int *lenp; /* return: length of selected text */
5879{
5880 if (VIsual_mode != 'V')
5881 unadjust_for_sel();
5882 if (VIsual.lnum != curwin->w_cursor.lnum)
5883 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005884 if (cap != NULL)
5885 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 return FAIL;
5887 }
5888 if (VIsual_mode == 'V')
5889 {
5890 *pp = ml_get_curline();
5891 *lenp = (int)STRLEN(*pp);
5892 }
5893 else
5894 {
5895 if (lt(curwin->w_cursor, VIsual))
5896 {
5897 *pp = ml_get_pos(&curwin->w_cursor);
5898 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5899 }
5900 else
5901 {
5902 *pp = ml_get_pos(&VIsual);
5903 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5904 }
5905#ifdef FEAT_MBYTE
5906 if (has_mbyte)
5907 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005908 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#endif
5910 }
5911 reset_VIsual_and_resel();
5912 return OK;
5913}
5914#endif
5915
5916/*
5917 * CTRL-T: backwards in tag stack
5918 */
5919 static void
5920nv_tagpop(cap)
5921 cmdarg_T *cap;
5922{
5923 if (!checkclearopq(cap->oap))
5924 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5925}
5926
5927/*
5928 * Handle scrolling command 'H', 'L' and 'M'.
5929 */
5930 static void
5931nv_scroll(cap)
5932 cmdarg_T *cap;
5933{
5934 int used = 0;
5935 long n;
5936#ifdef FEAT_FOLDING
5937 linenr_T lnum;
5938#endif
5939 int half;
5940
5941 cap->oap->motion_type = MLINE;
5942 setpcmark();
5943
5944 if (cap->cmdchar == 'L')
5945 {
5946 validate_botline(); /* make sure curwin->w_botline is valid */
5947 curwin->w_cursor.lnum = curwin->w_botline - 1;
5948 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5949 curwin->w_cursor.lnum = 1;
5950 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005951 {
5952#ifdef FEAT_FOLDING
5953 if (hasAnyFolding(curwin))
5954 {
5955 /* Count a fold for one screen line. */
5956 for (n = cap->count1 - 1; n > 0
5957 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5958 {
5959 (void)hasFolding(curwin->w_cursor.lnum,
5960 &curwin->w_cursor.lnum, NULL);
5961 --curwin->w_cursor.lnum;
5962 }
5963 }
5964 else
5965#endif
5966 curwin->w_cursor.lnum -= cap->count1 - 1;
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 }
5969 else
5970 {
5971 if (cap->cmdchar == 'M')
5972 {
5973#ifdef FEAT_DIFF
5974 /* Don't count filler lines above the window. */
5975 used -= diff_check_fill(curwin, curwin->w_topline)
5976 - curwin->w_topfill;
5977#endif
5978 validate_botline(); /* make sure w_empty_rows is valid */
5979 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5980 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5981 {
5982#ifdef FEAT_DIFF
5983 /* Count half he number of filler lines to be "below this
5984 * line" and half to be "above the next line". */
5985 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5986 + n) / 2 >= half)
5987 {
5988 --n;
5989 break;
5990 }
5991#endif
5992 used += plines(curwin->w_topline + n);
5993 if (used >= half)
5994 break;
5995#ifdef FEAT_FOLDING
5996 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5997 n = lnum - curwin->w_topline;
5998#endif
5999 }
6000 if (n > 0 && used > curwin->w_height)
6001 --n;
6002 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006003 else /* (cap->cmdchar == 'H') */
6004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006006#ifdef FEAT_FOLDING
6007 if (hasAnyFolding(curwin))
6008 {
6009 /* Count a fold for one screen line. */
6010 lnum = curwin->w_topline;
6011 while (n-- > 0 && lnum < curwin->w_botline - 1)
6012 {
6013 hasFolding(lnum, NULL, &lnum);
6014 ++lnum;
6015 }
6016 n = lnum - curwin->w_topline;
6017 }
6018#endif
6019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 curwin->w_cursor.lnum = curwin->w_topline + n;
6021 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6022 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6023 }
6024
6025 cursor_correct(); /* correct for 'so' */
6026 beginline(BL_SOL | BL_FIX);
6027}
6028
6029/*
6030 * Cursor right commands.
6031 */
6032 static void
6033nv_right(cap)
6034 cmdarg_T *cap;
6035{
6036 long n;
6037#ifdef FEAT_VISUAL
6038 int PAST_LINE;
6039#else
6040# define PAST_LINE 0
6041#endif
6042
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006043 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6044 {
6045 /* <C-Right> and <S-Right> move a word or WORD right */
6046 if (mod_mask & MOD_MASK_CTRL)
6047 cap->arg = TRUE;
6048 nv_wordcmd(cap);
6049 return;
6050 }
6051
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 cap->oap->motion_type = MCHAR;
6053 cap->oap->inclusive = FALSE;
6054#ifdef FEAT_VISUAL
6055 PAST_LINE = (VIsual_active && *p_sel != 'o');
6056
6057# ifdef FEAT_VIRTUALEDIT
6058 /*
6059 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006060 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 */
6062 if (virtual_active())
6063 PAST_LINE = 0;
6064# endif
6065#endif
6066
6067 for (n = cap->count1; n > 0; --n)
6068 {
6069 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006070#ifdef FEAT_VISUAL
6071 || (PAST_LINE && *ml_get_cursor() == NUL)
6072#endif
6073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
6075 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006076 * <Space> wraps to next line if 'whichwrap' has 's'.
6077 * 'l' wraps to next line if 'whichwrap' has 'l'.
6078 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 */
6080 if ( ((cap->cmdchar == ' '
6081 && vim_strchr(p_ww, 's') != NULL)
6082 || (cap->cmdchar == 'l'
6083 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006084 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 && vim_strchr(p_ww, '>') != NULL))
6086 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6087 {
6088 /* When deleting we also count the NL as a character.
6089 * Set cap->oap->inclusive when last char in the line is
6090 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006091 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 && !cap->oap->inclusive
6093 && !lineempty(curwin->w_cursor.lnum))
6094 cap->oap->inclusive = TRUE;
6095 else
6096 {
6097 ++curwin->w_cursor.lnum;
6098 curwin->w_cursor.col = 0;
6099#ifdef FEAT_VIRTUALEDIT
6100 curwin->w_cursor.coladd = 0;
6101#endif
6102 curwin->w_set_curswant = TRUE;
6103 cap->oap->inclusive = FALSE;
6104 }
6105 continue;
6106 }
6107 if (cap->oap->op_type == OP_NOP)
6108 {
6109 /* Only beep and flush if not moved at all */
6110 if (n == cap->count1)
6111 beep_flush();
6112 }
6113 else
6114 {
6115 if (!lineempty(curwin->w_cursor.lnum))
6116 cap->oap->inclusive = TRUE;
6117 }
6118 break;
6119 }
6120#ifdef FEAT_VISUAL
6121 else if (PAST_LINE)
6122 {
6123 curwin->w_set_curswant = TRUE;
6124# ifdef FEAT_VIRTUALEDIT
6125 if (virtual_active())
6126 oneright();
6127 else
6128# endif
6129 {
6130# ifdef FEAT_MBYTE
6131 if (has_mbyte)
6132 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006133 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 else
6135# endif
6136 ++curwin->w_cursor.col;
6137 }
6138 }
6139#endif
6140 }
6141#ifdef FEAT_FOLDING
6142 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6143 && cap->oap->op_type == OP_NOP)
6144 foldOpenCursor();
6145#endif
6146}
6147
6148/*
6149 * Cursor left commands.
6150 *
6151 * Returns TRUE when operator end should not be adjusted.
6152 */
6153 static void
6154nv_left(cap)
6155 cmdarg_T *cap;
6156{
6157 long n;
6158
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006159 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6160 {
6161 /* <C-Left> and <S-Left> move a word or WORD left */
6162 if (mod_mask & MOD_MASK_CTRL)
6163 cap->arg = 1;
6164 nv_bck_word(cap);
6165 return;
6166 }
6167
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 cap->oap->motion_type = MCHAR;
6169 cap->oap->inclusive = FALSE;
6170 for (n = cap->count1; n > 0; --n)
6171 {
6172 if (oneleft() == FAIL)
6173 {
6174 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6175 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6176 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6177 */
6178 if ( (((cap->cmdchar == K_BS
6179 || cap->cmdchar == Ctrl_H)
6180 && vim_strchr(p_ww, 'b') != NULL)
6181 || (cap->cmdchar == 'h'
6182 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006183 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 && vim_strchr(p_ww, '<') != NULL))
6185 && curwin->w_cursor.lnum > 1)
6186 {
6187 --(curwin->w_cursor.lnum);
6188 coladvance((colnr_T)MAXCOL);
6189 curwin->w_set_curswant = TRUE;
6190
6191 /* When the NL before the first char has to be deleted we
6192 * put the cursor on the NUL after the previous line.
6193 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006194 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if ( (cap->oap->op_type == OP_DELETE
6196 || cap->oap->op_type == OP_CHANGE)
6197 && !lineempty(curwin->w_cursor.lnum))
6198 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006199 if (*ml_get_cursor() != NUL)
6200 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 cap->retval |= CA_NO_ADJ_OP_END;
6202 }
6203 continue;
6204 }
6205 /* Only beep and flush if not moved at all */
6206 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6207 beep_flush();
6208 break;
6209 }
6210 }
6211#ifdef FEAT_FOLDING
6212 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6213 && cap->oap->op_type == OP_NOP)
6214 foldOpenCursor();
6215#endif
6216}
6217
6218/*
6219 * Cursor up commands.
6220 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6221 */
6222 static void
6223nv_up(cap)
6224 cmdarg_T *cap;
6225{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006226 if (mod_mask & MOD_MASK_SHIFT)
6227 {
6228 /* <S-Up> is page up */
6229 cap->arg = BACKWARD;
6230 nv_page(cap);
6231 }
6232 else
6233 {
6234 cap->oap->motion_type = MLINE;
6235 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6236 clearopbeep(cap->oap);
6237 else if (cap->arg)
6238 beginline(BL_WHITE | BL_FIX);
6239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240}
6241
6242/*
6243 * Cursor down commands.
6244 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6245 */
6246 static void
6247nv_down(cap)
6248 cmdarg_T *cap;
6249{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006250 if (mod_mask & MOD_MASK_SHIFT)
6251 {
6252 /* <S-Down> is page down */
6253 cap->arg = FORWARD;
6254 nv_page(cap);
6255 }
6256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6258 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006259 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006260 if (curwin->w_llist_ref == NULL)
6261 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6262 else
6263 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 else
6265#endif
6266 {
6267#ifdef FEAT_CMDWIN
6268 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006269 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 cmdwin_result = CAR;
6271 else
6272#endif
6273 {
6274 cap->oap->motion_type = MLINE;
6275 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6276 clearopbeep(cap->oap);
6277 else if (cap->arg)
6278 beginline(BL_WHITE | BL_FIX);
6279 }
6280 }
6281}
6282
6283#ifdef FEAT_SEARCHPATH
6284/*
6285 * Grab the file name under the cursor and edit it.
6286 */
6287 static void
6288nv_gotofile(cap)
6289 cmdarg_T *cap;
6290{
6291 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006292 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006294 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 {
6296 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006297 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 return;
6299 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006300#ifdef FEAT_AUTOCMD
6301 if (curbuf_locked())
6302 {
6303 clearop(cap->oap);
6304 return;
6305 }
6306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006308 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 if (ptr != NULL)
6311 {
6312 /* do autowrite if necessary */
6313 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6314 autowrite(curbuf, FALSE);
6315 setpcmark();
6316 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006317 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006318 if (cap->nchar == 'F' && lnum >= 0)
6319 {
6320 curwin->w_cursor.lnum = lnum;
6321 check_cursor_lnum();
6322 beginline(BL_SOL | BL_FIX);
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 vim_free(ptr);
6325 }
6326 else
6327 clearop(cap->oap);
6328}
6329#endif
6330
6331/*
6332 * <End> command: to end of current line or last line.
6333 */
6334 static void
6335nv_end(cap)
6336 cmdarg_T *cap;
6337{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006338 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006340 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 nv_goto(cap);
6342 cap->count1 = 1; /* to end of current line */
6343 }
6344 nv_dollar(cap);
6345}
6346
6347/*
6348 * Handle the "$" command.
6349 */
6350 static void
6351nv_dollar(cap)
6352 cmdarg_T *cap;
6353{
6354 cap->oap->motion_type = MCHAR;
6355 cap->oap->inclusive = TRUE;
6356#ifdef FEAT_VIRTUALEDIT
6357 /* In virtual mode when off the edge of a line and an operator
6358 * is pending (whew!) keep the cursor where it is.
6359 * Otherwise, send it to the end of the line. */
6360 if (!virtual_active() || gchar_cursor() != NUL
6361 || cap->oap->op_type == OP_NOP)
6362#endif
6363 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6364 if (cursor_down((long)(cap->count1 - 1),
6365 cap->oap->op_type == OP_NOP) == FAIL)
6366 clearopbeep(cap->oap);
6367#ifdef FEAT_FOLDING
6368 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6369 foldOpenCursor();
6370#endif
6371}
6372
6373/*
6374 * Implementation of '?' and '/' commands.
6375 * If cap->arg is TRUE don't set PC mark.
6376 */
6377 static void
6378nv_search(cap)
6379 cmdarg_T *cap;
6380{
6381 oparg_T *oap = cap->oap;
6382
6383 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6384 {
6385 /* Translate "g??" to "g?g?" */
6386 cap->cmdchar = 'g';
6387 cap->nchar = '?';
6388 nv_operator(cap);
6389 return;
6390 }
6391
6392 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6393
6394 if (cap->searchbuf == NULL)
6395 {
6396 clearop(oap);
6397 return;
6398 }
6399
6400 normal_search(cap, cap->cmdchar, cap->searchbuf,
6401 (cap->arg ? 0 : SEARCH_MARK));
6402}
6403
6404/*
6405 * Handle "N" and "n" commands.
6406 * cap->arg is SEARCH_REV for "N", 0 for "n".
6407 */
6408 static void
6409nv_next(cap)
6410 cmdarg_T *cap;
6411{
6412 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6413}
6414
6415/*
6416 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6417 * Uses only cap->count1 and cap->oap from "cap".
6418 */
6419 static void
6420normal_search(cap, dir, pat, opt)
6421 cmdarg_T *cap;
6422 int dir;
6423 char_u *pat;
6424 int opt; /* extra flags for do_search() */
6425{
6426 int i;
6427
6428 cap->oap->motion_type = MCHAR;
6429 cap->oap->inclusive = FALSE;
6430 cap->oap->use_reg_one = TRUE;
6431 curwin->w_set_curswant = TRUE;
6432
6433 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006434 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (i == 0)
6436 clearop(cap->oap);
6437 else
6438 {
6439 if (i == 2)
6440 cap->oap->motion_type = MLINE;
6441#ifdef FEAT_VIRTUALEDIT
6442 curwin->w_cursor.coladd = 0;
6443#endif
6444#ifdef FEAT_FOLDING
6445 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6446 foldOpenCursor();
6447#endif
6448 }
6449
6450 /* "/$" will put the cursor after the end of the line, may need to
6451 * correct that here */
6452 check_cursor();
6453}
6454
6455/*
6456 * Character search commands.
6457 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6458 * ',' and FALSE for ';'.
6459 * cap->nchar is NUL for ',' and ';' (repeat the search)
6460 */
6461 static void
6462nv_csearch(cap)
6463 cmdarg_T *cap;
6464{
6465 int t_cmd;
6466
6467 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6468 t_cmd = TRUE;
6469 else
6470 t_cmd = FALSE;
6471
6472 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6474 clearopbeep(cap->oap);
6475 else
6476 {
6477 curwin->w_set_curswant = TRUE;
6478#ifdef FEAT_VIRTUALEDIT
6479 /* Include a Tab for "tx" and for "dfx". */
6480 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6481 && (t_cmd || cap->oap->op_type != OP_NOP))
6482 {
6483 colnr_T scol, ecol;
6484
6485 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6486 curwin->w_cursor.coladd = ecol - scol;
6487 }
6488 else
6489 curwin->w_cursor.coladd = 0;
6490#endif
6491#ifdef FEAT_VISUAL
6492 adjust_for_sel(cap);
6493#endif
6494#ifdef FEAT_FOLDING
6495 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6496 foldOpenCursor();
6497#endif
6498 }
6499}
6500
6501/*
6502 * "[" and "]" commands.
6503 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6504 */
6505 static void
6506nv_brackets(cap)
6507 cmdarg_T *cap;
6508{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006509 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 pos_T prev_pos;
6511 pos_T *pos = NULL; /* init for GCC */
6512 pos_T old_pos; /* cursor position before command */
6513 int flag;
6514 long n;
6515 int findc;
6516 int c;
6517
6518 cap->oap->motion_type = MCHAR;
6519 cap->oap->inclusive = FALSE;
6520 old_pos = curwin->w_cursor;
6521#ifdef FEAT_VIRTUALEDIT
6522 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6523#endif
6524
6525#ifdef FEAT_SEARCHPATH
6526 /*
6527 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6528 */
6529 if (cap->nchar == 'f')
6530 nv_gotofile(cap);
6531 else
6532#endif
6533
6534#ifdef FEAT_FIND_ID
6535 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006536 * Find the occurrence(s) of the identifier or define under cursor
6537 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 *
6539 * search list jump
6540 * fwd bwd fwd bwd fwd bwd
6541 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6542 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6543 */
6544 if (vim_strchr((char_u *)
6545#ifdef EBCDIC
6546 "iI\005dD\067",
6547#else
6548 "iI\011dD\004",
6549#endif
6550 cap->nchar) != NULL)
6551 {
6552 char_u *ptr;
6553 int len;
6554
6555 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6556 clearop(cap->oap);
6557 else
6558 {
6559 find_pattern_in_path(ptr, 0, len, TRUE,
6560 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6561 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6562 cap->count1,
6563 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6564 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6565 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6566 (linenr_T)MAXLNUM);
6567 curwin->w_set_curswant = TRUE;
6568 }
6569 }
6570 else
6571#endif
6572
6573 /*
6574 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6575 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6576 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6577 * "[m" or "]m" search for prev/next start of (Java) method.
6578 * "[M" or "]M" search for prev/next end of (Java) method.
6579 */
6580 if ( (cap->cmdchar == '['
6581 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6582 || (cap->cmdchar == ']'
6583 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6584 {
6585 if (cap->nchar == '*')
6586 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 prev_pos.lnum = 0;
6588 if (cap->nchar == 'm' || cap->nchar == 'M')
6589 {
6590 if (cap->cmdchar == '[')
6591 findc = '{';
6592 else
6593 findc = '}';
6594 n = 9999;
6595 }
6596 else
6597 {
6598 findc = cap->nchar;
6599 n = cap->count1;
6600 }
6601 for ( ; n > 0; --n)
6602 {
6603 if ((pos = findmatchlimit(cap->oap, findc,
6604 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6605 {
6606 if (new_pos.lnum == 0) /* nothing found */
6607 {
6608 if (cap->nchar != 'm' && cap->nchar != 'M')
6609 clearopbeep(cap->oap);
6610 }
6611 else
6612 pos = &new_pos; /* use last one found */
6613 break;
6614 }
6615 prev_pos = new_pos;
6616 curwin->w_cursor = *pos;
6617 new_pos = *pos;
6618 }
6619 curwin->w_cursor = old_pos;
6620
6621 /*
6622 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6623 * brought us to the match for "[m" and "]M" when inside a method.
6624 * Try finding the '{' or '}' we want to be at.
6625 * Also repeat for the given count.
6626 */
6627 if (cap->nchar == 'm' || cap->nchar == 'M')
6628 {
6629 /* norm is TRUE for "]M" and "[m" */
6630 int norm = ((findc == '{') == (cap->nchar == 'm'));
6631
6632 n = cap->count1;
6633 /* found a match: we were inside a method */
6634 if (prev_pos.lnum != 0)
6635 {
6636 pos = &prev_pos;
6637 curwin->w_cursor = prev_pos;
6638 if (norm)
6639 --n;
6640 }
6641 else
6642 pos = NULL;
6643 while (n > 0)
6644 {
6645 for (;;)
6646 {
6647 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6648 {
6649 /* if not found anything, that's an error */
6650 if (pos == NULL)
6651 clearopbeep(cap->oap);
6652 n = 0;
6653 break;
6654 }
6655 c = gchar_cursor();
6656 if (c == '{' || c == '}')
6657 {
6658 /* Must have found end/start of class: use it.
6659 * Or found the place to be at. */
6660 if ((c == findc && norm) || (n == 1 && !norm))
6661 {
6662 new_pos = curwin->w_cursor;
6663 pos = &new_pos;
6664 n = 0;
6665 }
6666 /* if no match found at all, we started outside of the
6667 * class and we're inside now. Just go on. */
6668 else if (new_pos.lnum == 0)
6669 {
6670 new_pos = curwin->w_cursor;
6671 pos = &new_pos;
6672 }
6673 /* found start/end of other method: go to match */
6674 else if ((pos = findmatchlimit(cap->oap, findc,
6675 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6676 0)) == NULL)
6677 n = 0;
6678 else
6679 curwin->w_cursor = *pos;
6680 break;
6681 }
6682 }
6683 --n;
6684 }
6685 curwin->w_cursor = old_pos;
6686 if (pos == NULL && new_pos.lnum != 0)
6687 clearopbeep(cap->oap);
6688 }
6689 if (pos != NULL)
6690 {
6691 setpcmark();
6692 curwin->w_cursor = *pos;
6693 curwin->w_set_curswant = TRUE;
6694#ifdef FEAT_FOLDING
6695 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6696 && cap->oap->op_type == OP_NOP)
6697 foldOpenCursor();
6698#endif
6699 }
6700 }
6701
6702 /*
6703 * "[[", "[]", "]]" and "][": move to start or end of function
6704 */
6705 else if (cap->nchar == '[' || cap->nchar == ']')
6706 {
6707 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6708 flag = '{';
6709 else
6710 flag = '}'; /* "][" or "[]" */
6711
6712 curwin->w_set_curswant = TRUE;
6713 /*
6714 * Imitate strange Vi behaviour: When using "]]" with an operator
6715 * we also stop at '}'.
6716 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006717 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 (cap->oap->op_type != OP_NOP
6719 && cap->arg == FORWARD && flag == '{')))
6720 clearopbeep(cap->oap);
6721 else
6722 {
6723 if (cap->oap->op_type == OP_NOP)
6724 beginline(BL_WHITE | BL_FIX);
6725#ifdef FEAT_FOLDING
6726 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6727 foldOpenCursor();
6728#endif
6729 }
6730 }
6731
6732 /*
6733 * "[p", "[P", "]P" and "]p": put with indent adjustment
6734 */
6735 else if (cap->nchar == 'p' || cap->nchar == 'P')
6736 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006737 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {
6739 prep_redo_cmd(cap);
6740 do_put(cap->oap->regname,
6741 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6742 cap->count1, PUT_FIXINDENT);
6743 }
6744 }
6745
6746 /*
6747 * "['", "[`", "]'" and "]`": jump to next mark
6748 */
6749 else if (cap->nchar == '\'' || cap->nchar == '`')
6750 {
6751 pos = &curwin->w_cursor;
6752 for (n = cap->count1; n > 0; --n)
6753 {
6754 prev_pos = *pos;
6755 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6756 cap->nchar == '\'');
6757 if (pos == NULL)
6758 break;
6759 }
6760 if (pos == NULL)
6761 pos = &prev_pos;
6762 nv_cursormark(cap, cap->nchar == '\'', pos);
6763 }
6764
6765#ifdef FEAT_MOUSE
6766 /*
6767 * [ or ] followed by a middle mouse click: put selected text with
6768 * indent adjustment. Any other button just does as usual.
6769 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006770 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 (void)do_mouse(cap->oap, cap->nchar,
6773 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6774 cap->count1, PUT_FIXINDENT);
6775 }
6776#endif /* FEAT_MOUSE */
6777
6778#ifdef FEAT_FOLDING
6779 /*
6780 * "[z" and "]z": move to start or end of open fold.
6781 */
6782 else if (cap->nchar == 'z')
6783 {
6784 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6785 cap->count1) == FAIL)
6786 clearopbeep(cap->oap);
6787 }
6788#endif
6789
6790#ifdef FEAT_DIFF
6791 /*
6792 * "[c" and "]c": move to next or previous diff-change.
6793 */
6794 else if (cap->nchar == 'c')
6795 {
6796 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6797 cap->count1) == FAIL)
6798 clearopbeep(cap->oap);
6799 }
6800#endif
6801
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006802#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006803 /*
6804 * "[s", "[S", "]s" and "]S": move to next spell error.
6805 */
6806 else if (cap->nchar == 's' || cap->nchar == 'S')
6807 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006808 setpcmark();
6809 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006810 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6811 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006812 {
6813 clearopbeep(cap->oap);
6814 break;
6815 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006816# ifdef FEAT_FOLDING
6817 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6818 foldOpenCursor();
6819# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006820 }
6821#endif
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 /* Not a valid cap->nchar. */
6824 else
6825 clearopbeep(cap->oap);
6826}
6827
6828/*
6829 * Handle Normal mode "%" command.
6830 */
6831 static void
6832nv_percent(cap)
6833 cmdarg_T *cap;
6834{
6835 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006836#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 linenr_T lnum = curwin->w_cursor.lnum;
6838#endif
6839
6840 cap->oap->inclusive = TRUE;
6841 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6842 {
6843 if (cap->count0 > 100)
6844 clearopbeep(cap->oap);
6845 else
6846 {
6847 cap->oap->motion_type = MLINE;
6848 setpcmark();
6849 /* Round up, so CTRL-G will give same value. Watch out for a
6850 * large line count, the line number must not go negative! */
6851 if (curbuf->b_ml.ml_line_count > 1000000)
6852 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6853 / 100L * cap->count0;
6854 else
6855 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6856 cap->count0 + 99L) / 100L;
6857 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6858 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6859 beginline(BL_SOL | BL_FIX);
6860 }
6861 }
6862 else /* "%" : go to matching paren */
6863 {
6864 cap->oap->motion_type = MCHAR;
6865 cap->oap->use_reg_one = TRUE;
6866 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6867 clearopbeep(cap->oap);
6868 else
6869 {
6870 setpcmark();
6871 curwin->w_cursor = *pos;
6872 curwin->w_set_curswant = TRUE;
6873#ifdef FEAT_VIRTUALEDIT
6874 curwin->w_cursor.coladd = 0;
6875#endif
6876#ifdef FEAT_VISUAL
6877 adjust_for_sel(cap);
6878#endif
6879 }
6880 }
6881#ifdef FEAT_FOLDING
6882 if (cap->oap->op_type == OP_NOP
6883 && lnum != curwin->w_cursor.lnum
6884 && (fdo_flags & FDO_PERCENT)
6885 && KeyTyped)
6886 foldOpenCursor();
6887#endif
6888}
6889
6890/*
6891 * Handle "(" and ")" commands.
6892 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6893 */
6894 static void
6895nv_brace(cap)
6896 cmdarg_T *cap;
6897{
6898 cap->oap->motion_type = MCHAR;
6899 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006900 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6901 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 curwin->w_set_curswant = TRUE;
6903
6904 if (findsent(cap->arg, cap->count1) == FAIL)
6905 clearopbeep(cap->oap);
6906 else
6907 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006908 /* Don't leave the cursor on the NUL past end of line. */
6909 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910#ifdef FEAT_VIRTUALEDIT
6911 curwin->w_cursor.coladd = 0;
6912#endif
6913#ifdef FEAT_FOLDING
6914 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6915 foldOpenCursor();
6916#endif
6917 }
6918}
6919
6920/*
6921 * "m" command: Mark a position.
6922 */
6923 static void
6924nv_mark(cap)
6925 cmdarg_T *cap;
6926{
6927 if (!checkclearop(cap->oap))
6928 {
6929 if (setmark(cap->nchar) == FAIL)
6930 clearopbeep(cap->oap);
6931 }
6932}
6933
6934/*
6935 * "{" and "}" commands.
6936 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6937 */
6938 static void
6939nv_findpar(cap)
6940 cmdarg_T *cap;
6941{
6942 cap->oap->motion_type = MCHAR;
6943 cap->oap->inclusive = FALSE;
6944 cap->oap->use_reg_one = TRUE;
6945 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006946 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 clearopbeep(cap->oap);
6948 else
6949 {
6950#ifdef FEAT_VIRTUALEDIT
6951 curwin->w_cursor.coladd = 0;
6952#endif
6953#ifdef FEAT_FOLDING
6954 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6955 foldOpenCursor();
6956#endif
6957 }
6958}
6959
6960/*
6961 * "u" command: Undo or make lower case.
6962 */
6963 static void
6964nv_undo(cap)
6965 cmdarg_T *cap;
6966{
6967 if (cap->oap->op_type == OP_LOWER
6968#ifdef FEAT_VISUAL
6969 || VIsual_active
6970#endif
6971 )
6972 {
6973 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6974 cap->cmdchar = 'g';
6975 cap->nchar = 'u';
6976 nv_operator(cap);
6977 }
6978 else
6979 nv_kundo(cap);
6980}
6981
6982/*
6983 * <Undo> command.
6984 */
6985 static void
6986nv_kundo(cap)
6987 cmdarg_T *cap;
6988{
6989 if (!checkclearopq(cap->oap))
6990 {
6991 u_undo((int)cap->count1);
6992 curwin->w_set_curswant = TRUE;
6993 }
6994}
6995
6996/*
6997 * Handle the "r" command.
6998 */
6999 static void
7000nv_replace(cap)
7001 cmdarg_T *cap;
7002{
7003 char_u *ptr;
7004 int had_ctrl_v;
7005 long n;
7006
7007 if (checkclearop(cap->oap))
7008 return;
7009
7010 /* get another character */
7011 if (cap->nchar == Ctrl_V)
7012 {
7013 had_ctrl_v = Ctrl_V;
7014 cap->nchar = get_literal();
7015 /* Don't redo a multibyte character with CTRL-V. */
7016 if (cap->nchar > DEL)
7017 had_ctrl_v = NUL;
7018 }
7019 else
7020 had_ctrl_v = NUL;
7021
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007022 /* Abort if the character is a special key. */
7023 if (IS_SPECIAL(cap->nchar))
7024 {
7025 clearopbeep(cap->oap);
7026 return;
7027 }
7028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029#ifdef FEAT_VISUAL
7030 /* Visual mode "r" */
7031 if (VIsual_active)
7032 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007033 if (got_int)
7034 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007035 if (had_ctrl_v)
7036 {
7037 if (cap->nchar == '\r')
7038 cap->nchar = -1;
7039 else if (cap->nchar == '\n')
7040 cap->nchar = -2;
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 nv_operator(cap);
7043 return;
7044 }
7045#endif
7046
7047#ifdef FEAT_VIRTUALEDIT
7048 /* Break tabs, etc. */
7049 if (virtual_active())
7050 {
7051 if (u_save_cursor() == FAIL)
7052 return;
7053 if (gchar_cursor() == NUL)
7054 {
7055 /* Add extra space and put the cursor on the first one. */
7056 coladvance_force((colnr_T)(getviscol() + cap->count1));
7057 curwin->w_cursor.col -= cap->count1;
7058 }
7059 else if (gchar_cursor() == TAB)
7060 coladvance_force(getviscol());
7061 }
7062#endif
7063
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007064 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007066 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067#ifdef FEAT_MBYTE
7068 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7069#endif
7070 )
7071 {
7072 clearopbeep(cap->oap);
7073 return;
7074 }
7075
7076 /*
7077 * Replacing with a TAB is done by edit() when it is complicated because
7078 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7079 * Other characters are done below to avoid problems with things like
7080 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7081 */
7082 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7083 {
7084 stuffnumReadbuff(cap->count1);
7085 stuffcharReadbuff('R');
7086 stuffcharReadbuff('\t');
7087 stuffcharReadbuff(ESC);
7088 return;
7089 }
7090
7091 /* save line for undo */
7092 if (u_save_cursor() == FAIL)
7093 return;
7094
7095 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7096 {
7097 /*
7098 * Replace character(s) by a single newline.
7099 * Strange vi behaviour: Only one newline is inserted.
7100 * Delete the characters here.
7101 * Insert the newline with an insert command, takes care of
7102 * autoindent. The insert command depends on being on the last
7103 * character of a line or not.
7104 */
7105#ifdef FEAT_MBYTE
7106 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7107#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007108 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
7110 stuffcharReadbuff('\r');
7111 stuffcharReadbuff(ESC);
7112
7113 /* Give 'r' to edit(), to get the redo command right. */
7114 invoke_edit(cap, TRUE, 'r', FALSE);
7115 }
7116 else
7117 {
7118 prep_redo(cap->oap->regname, cap->count1,
7119 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7120
7121 curbuf->b_op_start = curwin->w_cursor;
7122#ifdef FEAT_MBYTE
7123 if (has_mbyte)
7124 {
7125 int old_State = State;
7126
7127 if (cap->ncharC1 != 0)
7128 AppendCharToRedobuff(cap->ncharC1);
7129 if (cap->ncharC2 != 0)
7130 AppendCharToRedobuff(cap->ncharC2);
7131
7132 /* This is slow, but it handles replacing a single-byte with a
7133 * multi-byte and the other way around. Also handles adding
7134 * composing characters for utf-8. */
7135 for (n = cap->count1; n > 0; --n)
7136 {
7137 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007138 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7139 {
7140 int c = ins_copychar(curwin->w_cursor.lnum
7141 + (cap->nchar == Ctrl_Y ? -1 : 1));
7142 if (c != NUL)
7143 ins_char(c);
7144 else
7145 /* will be decremented further down */
7146 ++curwin->w_cursor.col;
7147 }
7148 else
7149 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 State = old_State;
7151 if (cap->ncharC1 != 0)
7152 ins_char(cap->ncharC1);
7153 if (cap->ncharC2 != 0)
7154 ins_char(cap->ncharC2);
7155 }
7156 }
7157 else
7158#endif
7159 {
7160 /*
7161 * Replace the characters within one line.
7162 */
7163 for (n = cap->count1; n > 0; --n)
7164 {
7165 /*
7166 * Get ptr again, because u_save and/or showmatch() will have
7167 * released the line. At the same time we let know that the
7168 * line will be changed.
7169 */
7170 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007171 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7172 {
7173 int c = ins_copychar(curwin->w_cursor.lnum
7174 + (cap->nchar == Ctrl_Y ? -1 : 1));
7175 if (c != NUL)
7176 ptr[curwin->w_cursor.col] = c;
7177 }
7178 else
7179 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (p_sm && msg_silent == 0)
7181 showmatch(cap->nchar);
7182 ++curwin->w_cursor.col;
7183 }
7184#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007185 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007187 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar009b2592004-10-24 19:18:58 +00007189 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007190 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007192 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194#endif
7195
7196 /* mark the buffer as changed and prepare for displaying */
7197 changed_bytes(curwin->w_cursor.lnum,
7198 (colnr_T)(curwin->w_cursor.col - cap->count1));
7199 }
7200 --curwin->w_cursor.col; /* cursor on the last replaced char */
7201#ifdef FEAT_MBYTE
7202 /* if the character on the left of the current cursor is a multi-byte
7203 * character, move two characters left */
7204 if (has_mbyte)
7205 mb_adjust_cursor();
7206#endif
7207 curbuf->b_op_end = curwin->w_cursor;
7208 curwin->w_set_curswant = TRUE;
7209 set_last_insert(cap->nchar);
7210 }
7211}
7212
7213#ifdef FEAT_VISUAL
7214/*
7215 * 'o': Exchange start and end of Visual area.
7216 * 'O': same, but in block mode exchange left and right corners.
7217 */
7218 static void
7219v_swap_corners(cmdchar)
7220 int cmdchar;
7221{
7222 pos_T old_cursor;
7223 colnr_T left, right;
7224
7225 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7226 {
7227 old_cursor = curwin->w_cursor;
7228 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7229 curwin->w_cursor.lnum = VIsual.lnum;
7230 coladvance(left);
7231 VIsual = curwin->w_cursor;
7232
7233 curwin->w_cursor.lnum = old_cursor.lnum;
7234 curwin->w_curswant = right;
7235 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7236 * right one column */
7237 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7238 ++curwin->w_curswant;
7239 coladvance(curwin->w_curswant);
7240 if (curwin->w_cursor.col == old_cursor.col
7241#ifdef FEAT_VIRTUALEDIT
7242 && (!virtual_active()
7243 || curwin->w_cursor.coladd == old_cursor.coladd)
7244#endif
7245 )
7246 {
7247 curwin->w_cursor.lnum = VIsual.lnum;
7248 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7249 ++right;
7250 coladvance(right);
7251 VIsual = curwin->w_cursor;
7252
7253 curwin->w_cursor.lnum = old_cursor.lnum;
7254 coladvance(left);
7255 curwin->w_curswant = left;
7256 }
7257 }
7258 else
7259 {
7260 old_cursor = curwin->w_cursor;
7261 curwin->w_cursor = VIsual;
7262 VIsual = old_cursor;
7263 curwin->w_set_curswant = TRUE;
7264 }
7265}
7266#endif /* FEAT_VISUAL */
7267
7268/*
7269 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7270 */
7271 static void
7272nv_Replace(cap)
7273 cmdarg_T *cap;
7274{
7275#ifdef FEAT_VISUAL
7276 if (VIsual_active) /* "R" is replace lines */
7277 {
7278 cap->cmdchar = 'c';
7279 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007280 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 VIsual_mode = 'V';
7282 nv_operator(cap);
7283 }
7284 else
7285#endif
7286 if (!checkclearopq(cap->oap))
7287 {
7288 if (!curbuf->b_p_ma)
7289 EMSG(_(e_modifiable));
7290 else
7291 {
7292#ifdef FEAT_VIRTUALEDIT
7293 if (virtual_active())
7294 coladvance(getviscol());
7295#endif
7296 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7297 }
7298 }
7299}
7300
7301#ifdef FEAT_VREPLACE
7302/*
7303 * "gr".
7304 */
7305 static void
7306nv_vreplace(cap)
7307 cmdarg_T *cap;
7308{
7309# ifdef FEAT_VISUAL
7310 if (VIsual_active)
7311 {
7312 cap->cmdchar = 'r';
7313 cap->nchar = cap->extra_char;
7314 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7315 }
7316 else
7317# endif
7318 if (!checkclearopq(cap->oap))
7319 {
7320 if (!curbuf->b_p_ma)
7321 EMSG(_(e_modifiable));
7322 else
7323 {
7324 if (cap->extra_char == Ctrl_V) /* get another character */
7325 cap->extra_char = get_literal();
7326 stuffcharReadbuff(cap->extra_char);
7327 stuffcharReadbuff(ESC);
7328# ifdef FEAT_VIRTUALEDIT
7329 if (virtual_active())
7330 coladvance(getviscol());
7331# endif
7332 invoke_edit(cap, TRUE, 'v', FALSE);
7333 }
7334 }
7335}
7336#endif
7337
7338/*
7339 * Swap case for "~" command, when it does not work like an operator.
7340 */
7341 static void
7342n_swapchar(cap)
7343 cmdarg_T *cap;
7344{
7345 long n;
7346 pos_T startpos;
7347 int did_change = 0;
7348#ifdef FEAT_NETBEANS_INTG
7349 pos_T pos;
7350 char_u *ptr;
7351 int count;
7352#endif
7353
7354 if (checkclearopq(cap->oap))
7355 return;
7356
7357 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7358 {
7359 clearopbeep(cap->oap);
7360 return;
7361 }
7362
7363 prep_redo_cmd(cap);
7364
7365 if (u_save_cursor() == FAIL)
7366 return;
7367
7368 startpos = curwin->w_cursor;
7369#ifdef FEAT_NETBEANS_INTG
7370 pos = startpos;
7371#endif
7372 for (n = cap->count1; n > 0; --n)
7373 {
7374 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7375 inc_cursor();
7376 if (gchar_cursor() == NUL)
7377 {
7378 if (vim_strchr(p_ww, '~') != NULL
7379 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7380 {
7381#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007382 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 if (did_change)
7385 {
7386 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007387 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007388 netbeans_removed(curbuf, pos.lnum, pos.col,
7389 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007391 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 }
7393 pos.col = 0;
7394 pos.lnum++;
7395 }
7396#endif
7397 ++curwin->w_cursor.lnum;
7398 curwin->w_cursor.col = 0;
7399 if (n > 1)
7400 {
7401 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7402 break;
7403 u_clearline();
7404 }
7405 }
7406 else
7407 break;
7408 }
7409 }
7410#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007411 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 {
7413 ptr = ml_get(pos.lnum);
7414 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007415 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7416 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418#endif
7419
7420
7421 check_cursor();
7422 curwin->w_set_curswant = TRUE;
7423 if (did_change)
7424 {
7425 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7426 0L);
7427 curbuf->b_op_start = startpos;
7428 curbuf->b_op_end = curwin->w_cursor;
7429 if (curbuf->b_op_end.col > 0)
7430 --curbuf->b_op_end.col;
7431 }
7432}
7433
7434/*
7435 * Move cursor to mark.
7436 */
7437 static void
7438nv_cursormark(cap, flag, pos)
7439 cmdarg_T *cap;
7440 int flag;
7441 pos_T *pos;
7442{
7443 if (check_mark(pos) == FAIL)
7444 clearop(cap->oap);
7445 else
7446 {
7447 if (cap->cmdchar == '\''
7448 || cap->cmdchar == '`'
7449 || cap->cmdchar == '['
7450 || cap->cmdchar == ']')
7451 setpcmark();
7452 curwin->w_cursor = *pos;
7453 if (flag)
7454 beginline(BL_WHITE | BL_FIX);
7455 else
7456 check_cursor();
7457 }
7458 cap->oap->motion_type = flag ? MLINE : MCHAR;
7459 if (cap->cmdchar == '`')
7460 cap->oap->use_reg_one = TRUE;
7461 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7462 curwin->w_set_curswant = TRUE;
7463}
7464
7465#ifdef FEAT_VISUAL
7466/*
7467 * Handle commands that are operators in Visual mode.
7468 */
7469 static void
7470v_visop(cap)
7471 cmdarg_T *cap;
7472{
7473 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7474
7475 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007476 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (isupper(cap->cmdchar))
7478 {
7479 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007480 {
7481 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7485 curwin->w_curswant = MAXCOL;
7486 }
7487 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7488 nv_operator(cap);
7489}
7490#endif
7491
7492/*
7493 * "s" and "S" commands.
7494 */
7495 static void
7496nv_subst(cap)
7497 cmdarg_T *cap;
7498{
7499#ifdef FEAT_VISUAL
7500 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7501 {
7502 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007503 {
7504 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 cap->cmdchar = 'c';
7508 nv_operator(cap);
7509 }
7510 else
7511#endif
7512 nv_optrans(cap);
7513}
7514
7515/*
7516 * Abbreviated commands.
7517 */
7518 static void
7519nv_abbrev(cap)
7520 cmdarg_T *cap;
7521{
7522 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7523 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7524
7525#ifdef FEAT_VISUAL
7526 /* in Visual mode these commands are operators */
7527 if (VIsual_active)
7528 v_visop(cap);
7529 else
7530#endif
7531 nv_optrans(cap);
7532}
7533
7534/*
7535 * Translate a command into another command.
7536 */
7537 static void
7538nv_optrans(cap)
7539 cmdarg_T *cap;
7540{
7541 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7542 (char_u *)"d$", (char_u *)"c$",
7543 (char_u *)"cl", (char_u *)"cc",
7544 (char_u *)"yy", (char_u *)":s\r"};
7545 static char_u *str = (char_u *)"xXDCsSY&";
7546
7547 if (!checkclearopq(cap->oap))
7548 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007549 /* In Vi "2D" doesn't delete the next line. Can't translate it
7550 * either, because "2." should also not use the count. */
7551 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7552 {
7553 cap->oap->start = curwin->w_cursor;
7554 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007555#ifdef FEAT_EVAL
7556 set_op_var(OP_DELETE);
7557#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007558 cap->count1 = 1;
7559 nv_dollar(cap);
7560 finish_op = TRUE;
7561 ResetRedobuff();
7562 AppendCharToRedobuff('D');
7563 }
7564 else
7565 {
7566 if (cap->count0)
7567 stuffnumReadbuff(cap->count0);
7568 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 cap->opcount = 0;
7572}
7573
7574/*
7575 * "'" and "`" commands. Also for "g'" and "g`".
7576 * cap->arg is TRUE for "'" and "g'".
7577 */
7578 static void
7579nv_gomark(cap)
7580 cmdarg_T *cap;
7581{
7582 pos_T *pos;
7583 int c;
7584#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007585 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7587#endif
7588
7589 if (cap->cmdchar == 'g')
7590 c = cap->extra_char;
7591 else
7592 c = cap->nchar;
7593 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7594 if (pos == (pos_T *)-1) /* jumped to other file */
7595 {
7596 if (cap->arg)
7597 {
7598 check_cursor_lnum();
7599 beginline(BL_WHITE | BL_FIX);
7600 }
7601 else
7602 check_cursor();
7603 }
7604 else
7605 nv_cursormark(cap, cap->arg, pos);
7606
7607#ifdef FEAT_VIRTUALEDIT
7608 /* May need to clear the coladd that a mark includes. */
7609 if (!virtual_active())
7610 curwin->w_cursor.coladd = 0;
7611#endif
7612#ifdef FEAT_FOLDING
7613 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007614 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007615 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 && (fdo_flags & FDO_MARK)
7617 && old_KeyTyped)
7618 foldOpenCursor();
7619#endif
7620}
7621
7622/*
7623 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7624 */
7625 static void
7626nv_pcmark(cap)
7627 cmdarg_T *cap;
7628{
7629#ifdef FEAT_JUMPLIST
7630 pos_T *pos;
7631# ifdef FEAT_FOLDING
7632 linenr_T lnum = curwin->w_cursor.lnum;
7633 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7634# endif
7635
7636 if (!checkclearopq(cap->oap))
7637 {
7638 if (cap->cmdchar == 'g')
7639 pos = movechangelist((int)cap->count1);
7640 else
7641 pos = movemark((int)cap->count1);
7642 if (pos == (pos_T *)-1) /* jump to other file */
7643 {
7644 curwin->w_set_curswant = TRUE;
7645 check_cursor();
7646 }
7647 else if (pos != NULL) /* can jump */
7648 nv_cursormark(cap, FALSE, pos);
7649 else if (cap->cmdchar == 'g')
7650 {
7651 if (curbuf->b_changelistlen == 0)
7652 EMSG(_("E664: changelist is empty"));
7653 else if (cap->count1 < 0)
7654 EMSG(_("E662: At start of changelist"));
7655 else
7656 EMSG(_("E663: At end of changelist"));
7657 }
7658 else
7659 clearopbeep(cap->oap);
7660# ifdef FEAT_FOLDING
7661 if (cap->oap->op_type == OP_NOP
7662 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7663 && (fdo_flags & FDO_MARK)
7664 && old_KeyTyped)
7665 foldOpenCursor();
7666# endif
7667 }
7668#else
7669 clearopbeep(cap->oap);
7670#endif
7671}
7672
7673/*
7674 * Handle '"' command.
7675 */
7676 static void
7677nv_regname(cap)
7678 cmdarg_T *cap;
7679{
7680 if (checkclearop(cap->oap))
7681 return;
7682#ifdef FEAT_EVAL
7683 if (cap->nchar == '=')
7684 cap->nchar = get_expr_register();
7685#endif
7686 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7687 {
7688 cap->oap->regname = cap->nchar;
7689 cap->opcount = cap->count0; /* remember count before '"' */
7690#ifdef FEAT_EVAL
7691 set_reg_var(cap->oap->regname);
7692#endif
7693 }
7694 else
7695 clearopbeep(cap->oap);
7696}
7697
7698#ifdef FEAT_VISUAL
7699/*
7700 * Handle "v", "V" and "CTRL-V" commands.
7701 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7702 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 */
7705 static void
7706nv_visual(cap)
7707 cmdarg_T *cap;
7708{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007709 if (cap->cmdchar == Ctrl_Q)
7710 cap->cmdchar = Ctrl_V;
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7713 * characterwise, linewise, or blockwise. */
7714 if (cap->oap->op_type != OP_NOP)
7715 {
7716 cap->oap->motion_force = cap->cmdchar;
7717 finish_op = FALSE; /* operator doesn't finish now but later */
7718 return;
7719 }
7720
7721 VIsual_select = cap->arg;
7722 if (VIsual_active) /* change Visual mode */
7723 {
7724 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7725 end_visual_mode();
7726 else /* toggle char/block mode */
7727 { /* or char/line mode */
7728 VIsual_mode = cap->cmdchar;
7729 showmode();
7730 }
7731 redraw_curbuf_later(INVERTED); /* update the inversion */
7732 }
7733 else /* start Visual mode */
7734 {
7735 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007736 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007738 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 VIsual = curwin->w_cursor;
7740
7741 VIsual_active = TRUE;
7742 VIsual_reselect = TRUE;
7743 if (!cap->arg)
7744 /* start Select mode when 'selectmode' contains "cmd" */
7745 may_start_select('c');
7746#ifdef FEAT_MOUSE
7747 setmouse();
7748#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007749 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 redraw_cmdline = TRUE; /* show visual mode later */
7751 /*
7752 * For V and ^V, we multiply the number of lines even if there
7753 * was only one -- webb
7754 */
7755 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7756 {
7757 curwin->w_cursor.lnum +=
7758 resel_VIsual_line_count * cap->count0 - 1;
7759 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7760 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7761 }
7762 VIsual_mode = resel_VIsual_mode;
7763 if (VIsual_mode == 'v')
7764 {
7765 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007766 {
7767 validate_virtcol();
7768 curwin->w_curswant = curwin->w_virtcol
7769 + resel_VIsual_vcol * cap->count0 - 1;
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007772 curwin->w_curswant = resel_VIsual_vcol;
7773 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007775 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
7777 curwin->w_curswant = MAXCOL;
7778 coladvance((colnr_T)MAXCOL);
7779 }
7780 else if (VIsual_mode == Ctrl_V)
7781 {
7782 validate_virtcol();
7783 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007784 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 coladvance(curwin->w_curswant);
7786 }
7787 else
7788 curwin->w_set_curswant = TRUE;
7789 redraw_curbuf_later(INVERTED); /* show the inversion */
7790 }
7791 else
7792 {
7793 if (!cap->arg)
7794 /* start Select mode when 'selectmode' contains "cmd" */
7795 may_start_select('c');
7796 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007797 if (VIsual_mode != 'V' && *p_sel == 'e')
7798 ++cap->count1; /* include one more char */
7799 if (cap->count0 > 0 && --cap->count1 > 0)
7800 {
7801 /* With a count select that many characters or lines. */
7802 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7803 nv_right(cap);
7804 else if (VIsual_mode == 'V')
7805 nv_down(cap);
7806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 }
7808 }
7809}
7810
7811/*
7812 * Start selection for Shift-movement keys.
7813 */
7814 void
7815start_selection()
7816{
7817 /* if 'selectmode' contains "key", start Select mode */
7818 may_start_select('k');
7819 n_start_visual_mode('v');
7820}
7821
7822/*
7823 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7824 */
7825 void
7826may_start_select(c)
7827 int c;
7828{
7829 VIsual_select = (stuff_empty() && typebuf_typed()
7830 && (vim_strchr(p_slm, c) != NULL));
7831}
7832
7833/*
7834 * Start Visual mode "c".
7835 * Should set VIsual_select before calling this.
7836 */
7837 static void
7838n_start_visual_mode(c)
7839 int c;
7840{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007841#ifdef FEAT_CONCEAL
7842 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007843 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007844#endif
7845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 VIsual_mode = c;
7847 VIsual_active = TRUE;
7848 VIsual_reselect = TRUE;
7849#ifdef FEAT_VIRTUALEDIT
7850 /* Corner case: the 0 position in a tab may change when going into
7851 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7852 */
7853 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007854 {
7855 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858#endif
7859 VIsual = curwin->w_cursor;
7860
7861#ifdef FEAT_FOLDING
7862 foldAdjustVisual();
7863#endif
7864
7865#ifdef FEAT_MOUSE
7866 setmouse();
7867#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007868#ifdef FEAT_CONCEAL
7869 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007870 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007871#endif
7872
Bram Moolenaar09df3122006-01-23 22:23:09 +00007873 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 redraw_cmdline = TRUE; /* show visual mode later */
7875#ifdef FEAT_CLIPBOARD
7876 /* Make sure the clipboard gets updated. Needed because start and
7877 * end may still be the same, and the selection needs to be owned */
7878 clip_star.vmode = NUL;
7879#endif
7880
7881 /* Only need to redraw this line, unless still need to redraw an old
7882 * Visual area (when 'lazyredraw' is set). */
7883 if (curwin->w_redr_type < INVERTED)
7884 {
7885 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7886 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7887 }
7888}
7889
7890#endif /* FEAT_VISUAL */
7891
7892/*
7893 * CTRL-W: Window commands
7894 */
7895 static void
7896nv_window(cap)
7897 cmdarg_T *cap;
7898{
7899#ifdef FEAT_WINDOWS
7900 if (!checkclearop(cap->oap))
7901 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7902#else
7903 (void)checkclearop(cap->oap);
7904#endif
7905}
7906
7907/*
7908 * CTRL-Z: Suspend
7909 */
7910 static void
7911nv_suspend(cap)
7912 cmdarg_T *cap;
7913{
7914 clearop(cap->oap);
7915#ifdef FEAT_VISUAL
7916 if (VIsual_active)
7917 end_visual_mode(); /* stop Visual mode */
7918#endif
7919 do_cmdline_cmd((char_u *)"st");
7920}
7921
7922/*
7923 * Commands starting with "g".
7924 */
7925 static void
7926nv_g_cmd(cap)
7927 cmdarg_T *cap;
7928{
7929 oparg_T *oap = cap->oap;
7930#ifdef FEAT_VISUAL
7931 pos_T tpos;
7932#endif
7933 int i;
7934 int flag = FALSE;
7935
7936 switch (cap->nchar)
7937 {
7938#ifdef MEM_PROFILE
7939 /*
7940 * "g^A": dump log of used memory.
7941 */
7942 case Ctrl_A:
7943 vim_mem_profile_dump();
7944 break;
7945#endif
7946
7947#ifdef FEAT_VREPLACE
7948 /*
7949 * "gR": Enter virtual replace mode.
7950 */
7951 case 'R':
7952 cap->arg = TRUE;
7953 nv_Replace(cap);
7954 break;
7955
7956 case 'r':
7957 nv_vreplace(cap);
7958 break;
7959#endif
7960
7961 case '&':
7962 do_cmdline_cmd((char_u *)"%s//~/&");
7963 break;
7964
7965#ifdef FEAT_VISUAL
7966 /*
7967 * "gv": Reselect the previous Visual area. If Visual already active,
7968 * exchange previous and current Visual area.
7969 */
7970 case 'v':
7971 if (checkclearop(oap))
7972 break;
7973
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007974 if ( curbuf->b_visual.vi_start.lnum == 0
7975 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7976 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 beep_flush();
7978 else
7979 {
7980 /* set w_cursor to the start of the Visual area, tpos to the end */
7981 if (VIsual_active)
7982 {
7983 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007984 VIsual_mode = curbuf->b_visual.vi_mode;
7985 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986# ifdef FEAT_EVAL
7987 curbuf->b_visual_mode_eval = i;
7988# endif
7989 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007990 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7991 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007993 tpos = curbuf->b_visual.vi_end;
7994 curbuf->b_visual.vi_end = curwin->w_cursor;
7995 curwin->w_cursor = curbuf->b_visual.vi_start;
7996 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 else
7999 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008000 VIsual_mode = curbuf->b_visual.vi_mode;
8001 curwin->w_curswant = curbuf->b_visual.vi_curswant;
8002 tpos = curbuf->b_visual.vi_end;
8003 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 }
8005
8006 VIsual_active = TRUE;
8007 VIsual_reselect = TRUE;
8008
8009 /* Set Visual to the start and w_cursor to the end of the Visual
8010 * area. Make sure they are on an existing character. */
8011 check_cursor();
8012 VIsual = curwin->w_cursor;
8013 curwin->w_cursor = tpos;
8014 check_cursor();
8015 update_topline();
8016 /*
8017 * When called from normal "g" command: start Select mode when
8018 * 'selectmode' contains "cmd". When called for K_SELECT, always
8019 * start Select mode.
8020 */
8021 if (cap->arg)
8022 VIsual_select = TRUE;
8023 else
8024 may_start_select('c');
8025#ifdef FEAT_MOUSE
8026 setmouse();
8027#endif
8028#ifdef FEAT_CLIPBOARD
8029 /* Make sure the clipboard gets updated. Needed because start and
8030 * end are still the same, and the selection needs to be owned */
8031 clip_star.vmode = NUL;
8032#endif
8033 redraw_curbuf_later(INVERTED);
8034 showmode();
8035 }
8036 break;
8037 /*
8038 * "gV": Don't reselect the previous Visual area after a Select mode
8039 * mapping of menu.
8040 */
8041 case 'V':
8042 VIsual_reselect = FALSE;
8043 break;
8044
8045 /*
8046 * "gh": start Select mode.
8047 * "gH": start Select line mode.
8048 * "g^H": start Select block mode.
8049 */
8050 case K_BS:
8051 cap->nchar = Ctrl_H;
8052 /* FALLTHROUGH */
8053 case 'h':
8054 case 'H':
8055 case Ctrl_H:
8056# ifdef EBCDIC
8057 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8058 if (cap->nchar == Ctrl_H)
8059 cap->cmdchar = Ctrl_V;
8060 else
8061# endif
8062 cap->cmdchar = cap->nchar + ('v' - 'h');
8063 cap->arg = TRUE;
8064 nv_visual(cap);
8065 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008066#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008067
8068 /* "gn", "gN" visually select next/previous search match
8069 * "gn" selects next match
8070 * "gN" selects previous match
8071 */
8072 case 'N':
8073 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008074#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008075 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008076#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008077 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 /*
8081 * "gj" and "gk" two new funny movement keys -- up and down
8082 * movement based on *screen* line rather than *file* line.
8083 */
8084 case 'j':
8085 case K_DOWN:
8086 /* with 'nowrap' it works just like the normal "j" command; also when
8087 * in a closed fold */
8088 if (!curwin->w_p_wrap
8089#ifdef FEAT_FOLDING
8090 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8091#endif
8092 )
8093 {
8094 oap->motion_type = MLINE;
8095 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8096 }
8097 else
8098 i = nv_screengo(oap, FORWARD, cap->count1);
8099 if (i == FAIL)
8100 clearopbeep(oap);
8101 break;
8102
8103 case 'k':
8104 case K_UP:
8105 /* with 'nowrap' it works just like the normal "k" command; also when
8106 * in a closed fold */
8107 if (!curwin->w_p_wrap
8108#ifdef FEAT_FOLDING
8109 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8110#endif
8111 )
8112 {
8113 oap->motion_type = MLINE;
8114 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8115 }
8116 else
8117 i = nv_screengo(oap, BACKWARD, cap->count1);
8118 if (i == FAIL)
8119 clearopbeep(oap);
8120 break;
8121
8122 /*
8123 * "gJ": join two lines without inserting a space.
8124 */
8125 case 'J':
8126 nv_join(cap);
8127 break;
8128
8129 /*
8130 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8131 * "gm": middle of "g0" and "g$".
8132 */
8133 case '^':
8134 flag = TRUE;
8135 /* FALLTHROUGH */
8136
8137 case '0':
8138 case 'm':
8139 case K_HOME:
8140 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 oap->motion_type = MCHAR;
8142 oap->inclusive = FALSE;
8143 if (curwin->w_p_wrap
8144#ifdef FEAT_VERTSPLIT
8145 && curwin->w_width != 0
8146#endif
8147 )
8148 {
8149 int width1 = W_WIDTH(curwin) - curwin_col_off();
8150 int width2 = width1 + curwin_col_off2();
8151
8152 validate_virtcol();
8153 i = 0;
8154 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8155 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8156 }
8157 else
8158 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008159 /* Go to the middle of the screen line. When 'number' or
8160 * 'relativenumber' is on and lines are wrapping the middle can be more
8161 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 if (cap->nchar == 'm')
8163 i += (W_WIDTH(curwin) - curwin_col_off()
8164 + ((curwin->w_p_wrap && i > 0)
8165 ? curwin_col_off2() : 0)) / 2;
8166 coladvance((colnr_T)i);
8167 if (flag)
8168 {
8169 do
8170 i = gchar_cursor();
8171 while (vim_iswhite(i) && oneright() == OK);
8172 }
8173 curwin->w_set_curswant = TRUE;
8174 break;
8175
8176 case '_':
8177 /* "g_": to the last non-blank character in the line or <count> lines
8178 * downward. */
8179 cap->oap->motion_type = MCHAR;
8180 cap->oap->inclusive = TRUE;
8181 curwin->w_curswant = MAXCOL;
8182 if (cursor_down((long)(cap->count1 - 1),
8183 cap->oap->op_type == OP_NOP) == FAIL)
8184 clearopbeep(cap->oap);
8185 else
8186 {
8187 char_u *ptr = ml_get_curline();
8188
8189 /* In Visual mode we may end up after the line. */
8190 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8191 --curwin->w_cursor.col;
8192
8193 /* Decrease the cursor column until it's on a non-blank. */
8194 while (curwin->w_cursor.col > 0
8195 && vim_iswhite(ptr[curwin->w_cursor.col]))
8196 --curwin->w_cursor.col;
8197 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008198#ifdef FEAT_VISUAL
8199 adjust_for_sel(cap);
8200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202 break;
8203
8204 case '$':
8205 case K_END:
8206 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
8208 int col_off = curwin_col_off();
8209
8210 oap->motion_type = MCHAR;
8211 oap->inclusive = TRUE;
8212 if (curwin->w_p_wrap
8213#ifdef FEAT_VERTSPLIT
8214 && curwin->w_width != 0
8215#endif
8216 )
8217 {
8218 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8219 if (cap->count1 == 1)
8220 {
8221 int width1 = W_WIDTH(curwin) - col_off;
8222 int width2 = width1 + curwin_col_off2();
8223
8224 validate_virtcol();
8225 i = width1 - 1;
8226 if (curwin->w_virtcol >= (colnr_T)width1)
8227 i += ((curwin->w_virtcol - width1) / width2 + 1)
8228 * width2;
8229 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008230
8231 /* Make sure we stick in this column. */
8232 validate_virtcol();
8233 curwin->w_curswant = curwin->w_virtcol;
8234 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8236 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8237 {
8238 /*
8239 * Check for landing on a character that got split at
8240 * the end of the line. We do not want to advance to
8241 * the next screen line.
8242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 if (curwin->w_virtcol > (colnr_T)i)
8244 --curwin->w_cursor.col;
8245 }
8246#endif
8247 }
8248 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8249 clearopbeep(oap);
8250 }
8251 else
8252 {
8253 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8254 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008255
8256 /* Make sure we stick in this column. */
8257 validate_virtcol();
8258 curwin->w_curswant = curwin->w_virtcol;
8259 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 }
8261 }
8262 break;
8263
8264 /*
8265 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8266 */
8267 case '*':
8268 case '#':
8269#if POUND != '#'
8270 case POUND: /* pound sign (sometimes equal to '#') */
8271#endif
8272 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8273 case ']': /* :tselect for current identifier */
8274 nv_ident(cap);
8275 break;
8276
8277 /*
8278 * ge and gE: go back to end of word
8279 */
8280 case 'e':
8281 case 'E':
8282 oap->motion_type = MCHAR;
8283 curwin->w_set_curswant = TRUE;
8284 oap->inclusive = TRUE;
8285 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8286 clearopbeep(oap);
8287 break;
8288
8289 /*
8290 * "g CTRL-G": display info about cursor position
8291 */
8292 case Ctrl_G:
8293 cursor_pos_info();
8294 break;
8295
8296 /*
8297 * "gi": start Insert at the last position.
8298 */
8299 case 'i':
8300 if (curbuf->b_last_insert.lnum != 0)
8301 {
8302 curwin->w_cursor = curbuf->b_last_insert;
8303 check_cursor_lnum();
8304 i = (int)STRLEN(ml_get_curline());
8305 if (curwin->w_cursor.col > (colnr_T)i)
8306 {
8307#ifdef FEAT_VIRTUALEDIT
8308 if (virtual_active())
8309 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8310#endif
8311 curwin->w_cursor.col = i;
8312 }
8313 }
8314 cap->cmdchar = 'i';
8315 nv_edit(cap);
8316 break;
8317
8318 /*
8319 * "gI": Start insert in column 1.
8320 */
8321 case 'I':
8322 beginline(0);
8323 if (!checkclearopq(oap))
8324 invoke_edit(cap, FALSE, 'g', FALSE);
8325 break;
8326
8327#ifdef FEAT_SEARCHPATH
8328 /*
8329 * "gf": goto file, edit file under cursor
8330 * "]f" and "[f": can also be used.
8331 */
8332 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008333 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 nv_gotofile(cap);
8335 break;
8336#endif
8337
8338 /* "g'm" and "g`m": jump to mark without setting pcmark */
8339 case '\'':
8340 cap->arg = TRUE;
8341 /*FALLTHROUGH*/
8342 case '`':
8343 nv_gomark(cap);
8344 break;
8345
8346 /*
8347 * "gs": Goto sleep.
8348 */
8349 case 's':
8350 do_sleep(cap->count1 * 1000L);
8351 break;
8352
8353 /*
8354 * "ga": Display the ascii value of the character under the
8355 * cursor. It is displayed in decimal, hex, and octal. -- webb
8356 */
8357 case 'a':
8358 do_ascii(NULL);
8359 break;
8360
8361#ifdef FEAT_MBYTE
8362 /*
8363 * "g8": Display the bytes used for the UTF-8 character under the
8364 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008365 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008368 if (cap->count0 == 8)
8369 utf_find_illegal();
8370 else
8371 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 break;
8373#endif
8374
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008375 case '<':
8376 show_sb_text();
8377 break;
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 /*
8380 * "gg": Goto the first line in file. With a count it goes to
8381 * that line number like for "G". -- webb
8382 */
8383 case 'g':
8384 cap->arg = FALSE;
8385 nv_goto(cap);
8386 break;
8387
8388 /*
8389 * Two-character operators:
8390 * "gq" Format text
8391 * "gw" Format text and keep cursor position
8392 * "g~" Toggle the case of the text.
8393 * "gu" Change text to lower case.
8394 * "gU" Change text to upper case.
8395 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008396 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 */
8398 case 'q':
8399 case 'w':
8400 oap->cursor_start = curwin->w_cursor;
8401 /*FALLTHROUGH*/
8402 case '~':
8403 case 'u':
8404 case 'U':
8405 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008406 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 nv_operator(cap);
8408 break;
8409
8410 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008411 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * current function
8413 * "gD": idem, but in the current file.
8414 */
8415 case 'd':
8416 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008417 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 break;
8419
8420#ifdef FEAT_MOUSE
8421 /*
8422 * g<*Mouse> : <C-*mouse>
8423 */
8424 case K_MIDDLEMOUSE:
8425 case K_MIDDLEDRAG:
8426 case K_MIDDLERELEASE:
8427 case K_LEFTMOUSE:
8428 case K_LEFTDRAG:
8429 case K_LEFTRELEASE:
8430 case K_RIGHTMOUSE:
8431 case K_RIGHTDRAG:
8432 case K_RIGHTRELEASE:
8433 case K_X1MOUSE:
8434 case K_X1DRAG:
8435 case K_X1RELEASE:
8436 case K_X2MOUSE:
8437 case K_X2DRAG:
8438 case K_X2RELEASE:
8439 mod_mask = MOD_MASK_CTRL;
8440 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8441 break;
8442#endif
8443
8444 case K_IGNORE:
8445 break;
8446
8447 /*
8448 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8449 */
8450 case 'p':
8451 case 'P':
8452 nv_put(cap);
8453 break;
8454
8455#ifdef FEAT_BYTEOFF
8456 /* "go": goto byte count from start of buffer */
8457 case 'o':
8458 goto_byte(cap->count0);
8459 break;
8460#endif
8461
8462 /* "gQ": improved Ex mode */
8463 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008464 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008467 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 break;
8469 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008470
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 if (!checkclearopq(oap))
8472 do_exmode(TRUE);
8473 break;
8474
8475#ifdef FEAT_JUMPLIST
8476 case ',':
8477 nv_pcmark(cap);
8478 break;
8479
8480 case ';':
8481 cap->count1 = -cap->count1;
8482 nv_pcmark(cap);
8483 break;
8484#endif
8485
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008486#ifdef FEAT_WINDOWS
8487 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008488 if (!checkclearop(oap))
8489 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008490 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008491 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008492 if (!checkclearop(oap))
8493 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008494 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008495#endif
8496
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008497 case '+':
8498 case '-': /* "g+" and "g-": undo or redo along the timeline */
8499 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008500 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008501 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008502 break;
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 default:
8505 clearopbeep(oap);
8506 break;
8507 }
8508}
8509
8510/*
8511 * Handle "o" and "O" commands.
8512 */
8513 static void
8514n_opencmd(cap)
8515 cmdarg_T *cap;
8516{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008517#ifdef FEAT_CONCEAL
8518 linenr_T oldline = curwin->w_cursor.lnum;
8519#endif
8520
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (!checkclearopq(cap->oap))
8522 {
8523#ifdef FEAT_FOLDING
8524 if (cap->cmdchar == 'O')
8525 /* Open above the first line of a folded sequence of lines */
8526 (void)hasFolding(curwin->w_cursor.lnum,
8527 &curwin->w_cursor.lnum, NULL);
8528 else
8529 /* Open below the last line of a folded sequence of lines */
8530 (void)hasFolding(curwin->w_cursor.lnum,
8531 NULL, &curwin->w_cursor.lnum);
8532#endif
8533 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8534 (cap->cmdchar == 'O' ? 1 : 0)),
8535 (linenr_T)(curwin->w_cursor.lnum +
8536 (cap->cmdchar == 'o' ? 1 : 0))
8537 ) == OK
8538 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8539#ifdef FEAT_COMMENTS
8540 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8541#endif
8542 0, 0))
8543 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008544#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008545 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008546 update_single_line(curwin, oldline);
8547#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008548 /* When '#' is in 'cpoptions' ignore the count. */
8549 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8550 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8552 }
8553 }
8554}
8555
8556/*
8557 * "." command: redo last change.
8558 */
8559 static void
8560nv_dot(cap)
8561 cmdarg_T *cap;
8562{
8563 if (!checkclearopq(cap->oap))
8564 {
8565 /*
8566 * If "restart_edit" is TRUE, the last but one command is repeated
8567 * instead of the last command (inserting text). This is used for
8568 * CTRL-O <.> in insert mode.
8569 */
8570 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8571 clearopbeep(cap->oap);
8572 }
8573}
8574
8575/*
8576 * CTRL-R: undo undo
8577 */
8578 static void
8579nv_redo(cap)
8580 cmdarg_T *cap;
8581{
8582 if (!checkclearopq(cap->oap))
8583 {
8584 u_redo((int)cap->count1);
8585 curwin->w_set_curswant = TRUE;
8586 }
8587}
8588
8589/*
8590 * Handle "U" command.
8591 */
8592 static void
8593nv_Undo(cap)
8594 cmdarg_T *cap;
8595{
8596 /* In Visual mode and typing "gUU" triggers an operator */
8597 if (cap->oap->op_type == OP_UPPER
8598#ifdef FEAT_VISUAL
8599 || VIsual_active
8600#endif
8601 )
8602 {
8603 /* translate "gUU" to "gUgU" */
8604 cap->cmdchar = 'g';
8605 cap->nchar = 'U';
8606 nv_operator(cap);
8607 }
8608 else if (!checkclearopq(cap->oap))
8609 {
8610 u_undoline();
8611 curwin->w_set_curswant = TRUE;
8612 }
8613}
8614
8615/*
8616 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8617 * single character.
8618 */
8619 static void
8620nv_tilde(cap)
8621 cmdarg_T *cap;
8622{
8623 if (!p_to
8624#ifdef FEAT_VISUAL
8625 && !VIsual_active
8626#endif
8627 && cap->oap->op_type != OP_TILDE)
8628 n_swapchar(cap);
8629 else
8630 nv_operator(cap);
8631}
8632
8633/*
8634 * Handle an operator command.
8635 * The actual work is done by do_pending_operator().
8636 */
8637 static void
8638nv_operator(cap)
8639 cmdarg_T *cap;
8640{
8641 int op_type;
8642
8643 op_type = get_op_type(cap->cmdchar, cap->nchar);
8644
8645 if (op_type == cap->oap->op_type) /* double operator works on lines */
8646 nv_lineop(cap);
8647 else if (!checkclearop(cap->oap))
8648 {
8649 cap->oap->start = curwin->w_cursor;
8650 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008651#ifdef FEAT_EVAL
8652 set_op_var(op_type);
8653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 }
8655}
8656
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008657#ifdef FEAT_EVAL
8658/*
8659 * Set v:operator to the characters for "optype".
8660 */
8661 static void
8662set_op_var(optype)
8663 int optype;
8664{
8665 char_u opchars[3];
8666
8667 if (optype == OP_NOP)
8668 set_vim_var_string(VV_OP, NULL, 0);
8669 else
8670 {
8671 opchars[0] = get_op_char(optype);
8672 opchars[1] = get_extra_op_char(optype);
8673 opchars[2] = NUL;
8674 set_vim_var_string(VV_OP, opchars, -1);
8675 }
8676}
8677#endif
8678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679/*
8680 * Handle linewise operator "dd", "yy", etc.
8681 *
8682 * "_" is is a strange motion command that helps make operators more logical.
8683 * It is actually implemented, but not documented in the real Vi. This motion
8684 * command actually refers to "the current line". Commands like "dd" and "yy"
8685 * are really an alternate form of "d_" and "y_". It does accept a count, so
8686 * "d3_" works to delete 3 lines.
8687 */
8688 static void
8689nv_lineop(cap)
8690 cmdarg_T *cap;
8691{
8692 cap->oap->motion_type = MLINE;
8693 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8694 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008695 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8696 && cap->oap->motion_force != 'v'
8697 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 || cap->oap->op_type == OP_LSHIFT
8699 || cap->oap->op_type == OP_RSHIFT)
8700 beginline(BL_SOL | BL_FIX);
8701 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8702 beginline(BL_WHITE | BL_FIX);
8703}
8704
8705/*
8706 * <Home> command.
8707 */
8708 static void
8709nv_home(cap)
8710 cmdarg_T *cap;
8711{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008712 /* CTRL-HOME is like "gg" */
8713 if (mod_mask & MOD_MASK_CTRL)
8714 nv_goto(cap);
8715 else
8716 {
8717 cap->count0 = 1;
8718 nv_pipe(cap);
8719 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008720 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8721 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722}
8723
8724/*
8725 * "|" command.
8726 */
8727 static void
8728nv_pipe(cap)
8729 cmdarg_T *cap;
8730{
8731 cap->oap->motion_type = MCHAR;
8732 cap->oap->inclusive = FALSE;
8733 beginline(0);
8734 if (cap->count0 > 0)
8735 {
8736 coladvance((colnr_T)(cap->count0 - 1));
8737 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8738 }
8739 else
8740 curwin->w_curswant = 0;
8741 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008742 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 curwin->w_set_curswant = FALSE;
8744}
8745
8746/*
8747 * Handle back-word command "b" and "B".
8748 * cap->arg is 1 for "B"
8749 */
8750 static void
8751nv_bck_word(cap)
8752 cmdarg_T *cap;
8753{
8754 cap->oap->motion_type = MCHAR;
8755 cap->oap->inclusive = FALSE;
8756 curwin->w_set_curswant = TRUE;
8757 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8758 clearopbeep(cap->oap);
8759#ifdef FEAT_FOLDING
8760 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8761 foldOpenCursor();
8762#endif
8763}
8764
8765/*
8766 * Handle word motion commands "e", "E", "w" and "W".
8767 * cap->arg is TRUE for "E" and "W".
8768 */
8769 static void
8770nv_wordcmd(cap)
8771 cmdarg_T *cap;
8772{
8773 int n;
8774 int word_end;
8775 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008776 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
8778 /*
8779 * Set inclusive for the "E" and "e" command.
8780 */
8781 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8782 word_end = TRUE;
8783 else
8784 word_end = FALSE;
8785 cap->oap->inclusive = word_end;
8786
8787 /*
8788 * "cw" and "cW" are a special case.
8789 */
8790 if (!word_end && cap->oap->op_type == OP_CHANGE)
8791 {
8792 n = gchar_cursor();
8793 if (n != NUL) /* not an empty line */
8794 {
8795 if (vim_iswhite(n))
8796 {
8797 /*
8798 * Reproduce a funny Vi behaviour: "cw" on a blank only
8799 * changes one character, not all blanks until the start of
8800 * the next word. Only do this when the 'w' flag is included
8801 * in 'cpoptions'.
8802 */
8803 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8804 {
8805 cap->oap->inclusive = TRUE;
8806 cap->oap->motion_type = MCHAR;
8807 return;
8808 }
8809 }
8810 else
8811 {
8812 /*
8813 * This is a little strange. To match what the real Vi does,
8814 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8815 * that we are not on a space or a TAB. This seems impolite
8816 * at first, but it's really more what we mean when we say
8817 * 'cw'.
8818 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008819 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 * will change only one character! This is done by setting
8821 * flag.
8822 */
8823 cap->oap->inclusive = TRUE;
8824 word_end = TRUE;
8825 flag = TRUE;
8826 }
8827 }
8828 }
8829
8830 cap->oap->motion_type = MCHAR;
8831 curwin->w_set_curswant = TRUE;
8832 if (word_end)
8833 n = end_word(cap->count1, cap->arg, flag, FALSE);
8834 else
8835 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8836
Bram Moolenaardfefb982008-04-01 10:06:39 +00008837 /* Don't leave the cursor on the NUL past the end of line. Unless we
8838 * didn't move it forward. */
8839 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008840 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
8842 if (n == FAIL && cap->oap->op_type == OP_NOP)
8843 clearopbeep(cap->oap);
8844 else
8845 {
8846#ifdef FEAT_VISUAL
8847 adjust_for_sel(cap);
8848#endif
8849#ifdef FEAT_FOLDING
8850 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8851 foldOpenCursor();
8852#endif
8853 }
8854}
8855
8856/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008857 * Used after a movement command: If the cursor ends up on the NUL after the
8858 * end of the line, may move it back to the last character and make the motion
8859 * inclusive.
8860 */
8861 static void
8862adjust_cursor(oap)
8863 oparg_T *oap;
8864{
8865 /* The cursor cannot remain on the NUL when:
8866 * - the column is > 0
8867 * - not in Visual mode or 'selection' is "o"
8868 * - 'virtualedit' is not "all" and not "onemore".
8869 */
8870 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8871#ifdef FEAT_VISUAL
8872 && (!VIsual_active || *p_sel == 'o')
8873#endif
8874#ifdef FEAT_VIRTUALEDIT
8875 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8876#endif
8877 )
8878 {
8879 --curwin->w_cursor.col;
8880#ifdef FEAT_MBYTE
8881 /* prevent cursor from moving on the trail byte */
8882 if (has_mbyte)
8883 mb_adjust_cursor();
8884#endif
8885 oap->inclusive = TRUE;
8886 }
8887}
8888
8889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 * "0" and "^" commands.
8891 * cap->arg is the argument for beginline().
8892 */
8893 static void
8894nv_beginline(cap)
8895 cmdarg_T *cap;
8896{
8897 cap->oap->motion_type = MCHAR;
8898 cap->oap->inclusive = FALSE;
8899 beginline(cap->arg);
8900#ifdef FEAT_FOLDING
8901 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8902 foldOpenCursor();
8903#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008904 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8905 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906}
8907
8908#ifdef FEAT_VISUAL
8909/*
8910 * In exclusive Visual mode, may include the last character.
8911 */
8912 static void
8913adjust_for_sel(cap)
8914 cmdarg_T *cap;
8915{
8916 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8917 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8918 {
8919# ifdef FEAT_MBYTE
8920 if (has_mbyte)
8921 inc_cursor();
8922 else
8923# endif
8924 ++curwin->w_cursor.col;
8925 cap->oap->inclusive = FALSE;
8926 }
8927}
8928
8929/*
8930 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8931 * Should check VIsual_mode before calling this.
8932 * Returns TRUE when backed up to the previous line.
8933 */
8934 static int
8935unadjust_for_sel()
8936{
8937 pos_T *pp;
8938
8939 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8940 {
8941 if (lt(VIsual, curwin->w_cursor))
8942 pp = &curwin->w_cursor;
8943 else
8944 pp = &VIsual;
8945#ifdef FEAT_VIRTUALEDIT
8946 if (pp->coladd > 0)
8947 --pp->coladd;
8948 else
8949#endif
8950 if (pp->col > 0)
8951 {
8952 --pp->col;
8953#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008954 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955#endif
8956 }
8957 else if (pp->lnum > 1)
8958 {
8959 --pp->lnum;
8960 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8961 return TRUE;
8962 }
8963 }
8964 return FALSE;
8965}
8966
8967/*
8968 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8969 */
8970 static void
8971nv_select(cap)
8972 cmdarg_T *cap;
8973{
8974 if (VIsual_active)
8975 VIsual_select = TRUE;
8976 else if (VIsual_reselect)
8977 {
8978 cap->nchar = 'v'; /* fake "gv" command */
8979 cap->arg = TRUE;
8980 nv_g_cmd(cap);
8981 }
8982}
8983
8984#endif
8985
8986/*
8987 * "G", "gg", CTRL-END, CTRL-HOME.
8988 * cap->arg is TRUE for "G".
8989 */
8990 static void
8991nv_goto(cap)
8992 cmdarg_T *cap;
8993{
8994 linenr_T lnum;
8995
8996 if (cap->arg)
8997 lnum = curbuf->b_ml.ml_line_count;
8998 else
8999 lnum = 1L;
9000 cap->oap->motion_type = MLINE;
9001 setpcmark();
9002
9003 /* When a count is given, use it instead of the default lnum */
9004 if (cap->count0 != 0)
9005 lnum = cap->count0;
9006 if (lnum < 1L)
9007 lnum = 1L;
9008 else if (lnum > curbuf->b_ml.ml_line_count)
9009 lnum = curbuf->b_ml.ml_line_count;
9010 curwin->w_cursor.lnum = lnum;
9011 beginline(BL_SOL | BL_FIX);
9012#ifdef FEAT_FOLDING
9013 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
9014 foldOpenCursor();
9015#endif
9016}
9017
9018/*
9019 * CTRL-\ in Normal mode.
9020 */
9021 static void
9022nv_normal(cap)
9023 cmdarg_T *cap;
9024{
9025 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
9026 {
9027 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00009028 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 clear_cmdline = TRUE; /* unshow mode later */
9030 restart_edit = 0;
9031#ifdef FEAT_CMDWIN
9032 if (cmdwin_type != 0)
9033 cmdwin_result = Ctrl_C;
9034#endif
9035#ifdef FEAT_VISUAL
9036 if (VIsual_active)
9037 {
9038 end_visual_mode(); /* stop Visual */
9039 redraw_curbuf_later(INVERTED);
9040 }
9041#endif
9042 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9043 if (cap->nchar == Ctrl_G && p_im)
9044 restart_edit = 'a';
9045 }
9046 else
9047 clearopbeep(cap->oap);
9048}
9049
9050/*
9051 * ESC in Normal mode: beep, but don't flush buffers.
9052 * Don't even beep if we are canceling a command.
9053 */
9054 static void
9055nv_esc(cap)
9056 cmdarg_T *cap;
9057{
9058 int no_reason;
9059
9060 no_reason = (cap->oap->op_type == OP_NOP
9061 && cap->opcount == 0
9062 && cap->count0 == 0
9063 && cap->oap->regname == 0
9064 && !p_im);
9065
9066 if (cap->arg) /* TRUE for CTRL-C */
9067 {
9068 if (restart_edit == 0
9069#ifdef FEAT_CMDWIN
9070 && cmdwin_type == 0
9071#endif
9072#ifdef FEAT_VISUAL
9073 && !VIsual_active
9074#endif
9075 && no_reason)
9076 MSG(_("Type :quit<Enter> to exit Vim"));
9077
9078 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9079 * set again below when halfway a mapping. */
9080 if (!p_im)
9081 restart_edit = 0;
9082#ifdef FEAT_CMDWIN
9083 if (cmdwin_type != 0)
9084 {
9085 cmdwin_result = K_IGNORE;
9086 got_int = FALSE; /* don't stop executing autocommands et al. */
9087 return;
9088 }
9089#endif
9090 }
9091
9092#ifdef FEAT_VISUAL
9093 if (VIsual_active)
9094 {
9095 end_visual_mode(); /* stop Visual */
9096 check_cursor_col(); /* make sure cursor is not beyond EOL */
9097 curwin->w_set_curswant = TRUE;
9098 redraw_curbuf_later(INVERTED);
9099 }
9100 else
9101#endif
9102 if (no_reason)
9103 vim_beep();
9104 clearop(cap->oap);
9105
9106 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9107 * set return to Insert mode afterwards. */
9108 if (restart_edit == 0 && goto_im()
9109#ifdef FEAT_EX_EXTRA
9110 && ex_normal_busy == 0
9111#endif
9112 )
9113 restart_edit = 'a';
9114}
9115
9116/*
9117 * Handle "A", "a", "I", "i" and <Insert> commands.
9118 */
9119 static void
9120nv_edit(cap)
9121 cmdarg_T *cap;
9122{
9123 /* <Insert> is equal to "i" */
9124 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9125 cap->cmdchar = 'i';
9126
9127#ifdef FEAT_VISUAL
9128 /* in Visual mode "A" and "I" are an operator */
9129 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9130 v_visop(cap);
9131
9132 /* in Visual mode and after an operator "a" and "i" are for text objects */
9133 else
9134#endif
9135 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9136 && (cap->oap->op_type != OP_NOP
9137#ifdef FEAT_VISUAL
9138 || VIsual_active
9139#endif
9140 ))
9141 {
9142#ifdef FEAT_TEXTOBJ
9143 nv_object(cap);
9144#else
9145 clearopbeep(cap->oap);
9146#endif
9147 }
9148 else if (!curbuf->b_p_ma && !p_im)
9149 {
9150 /* Only give this error when 'insertmode' is off. */
9151 EMSG(_(e_modifiable));
9152 clearop(cap->oap);
9153 }
9154 else if (!checkclearopq(cap->oap))
9155 {
9156 switch (cap->cmdchar)
9157 {
9158 case 'A': /* "A"ppend after the line */
9159 curwin->w_set_curswant = TRUE;
9160#ifdef FEAT_VIRTUALEDIT
9161 if (ve_flags == VE_ALL)
9162 {
9163 int save_State = State;
9164
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009165 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 * character past the end of the line */
9167 State = INSERT;
9168 coladvance((colnr_T)MAXCOL);
9169 State = save_State;
9170 }
9171 else
9172#endif
9173 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9174 break;
9175
9176 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009177 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9178 beginline(BL_WHITE);
9179 else
9180 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 break;
9182
9183 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9184#ifdef FEAT_VIRTUALEDIT
9185 /* increment coladd when in virtual space, increment the
9186 * column otherwise, also to append after an unprintable char */
9187 if (virtual_active()
9188 && (curwin->w_cursor.coladd > 0
9189 || *ml_get_cursor() == NUL
9190 || *ml_get_cursor() == TAB))
9191 curwin->w_cursor.coladd++;
9192 else
9193#endif
9194 if (*ml_get_cursor() != NUL)
9195 inc_cursor();
9196 break;
9197 }
9198
9199#ifdef FEAT_VIRTUALEDIT
9200 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9201 {
9202 int save_State = State;
9203
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009204 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 * character past the end of the line */
9206 State = INSERT;
9207 coladvance(getviscol());
9208 State = save_State;
9209 }
9210#endif
9211
9212 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9213 }
9214}
9215
9216/*
9217 * Invoke edit() and take care of "restart_edit" and the return value.
9218 */
9219 static void
9220invoke_edit(cap, repl, cmd, startln)
9221 cmdarg_T *cap;
9222 int repl; /* "r" or "gr" command */
9223 int cmd;
9224 int startln;
9225{
9226 int restart_edit_save = 0;
9227
9228 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9229 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9230 * it. */
9231 if (repl || !stuff_empty())
9232 restart_edit_save = restart_edit;
9233 else
9234 restart_edit_save = 0;
9235
9236 /* Always reset "restart_edit", this is not a restarted edit. */
9237 restart_edit = 0;
9238
9239 if (edit(cmd, startln, cap->count1))
9240 cap->retval |= CA_COMMAND_BUSY;
9241
9242 if (restart_edit == 0)
9243 restart_edit = restart_edit_save;
9244}
9245
9246#ifdef FEAT_TEXTOBJ
9247/*
9248 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9249 */
9250 static void
9251nv_object(cap)
9252 cmdarg_T *cap;
9253{
9254 int flag;
9255 int include;
9256 char_u *mps_save;
9257
9258 if (cap->cmdchar == 'i')
9259 include = FALSE; /* "ix" = inner object: exclude white space */
9260 else
9261 include = TRUE; /* "ax" = an object: include white space */
9262
9263 /* Make sure (), [], {} and <> are in 'matchpairs' */
9264 mps_save = curbuf->b_p_mps;
9265 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9266
9267 switch (cap->nchar)
9268 {
9269 case 'w': /* "aw" = a word */
9270 flag = current_word(cap->oap, cap->count1, include, FALSE);
9271 break;
9272 case 'W': /* "aW" = a WORD */
9273 flag = current_word(cap->oap, cap->count1, include, TRUE);
9274 break;
9275 case 'b': /* "ab" = a braces block */
9276 case '(':
9277 case ')':
9278 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9279 break;
9280 case 'B': /* "aB" = a Brackets block */
9281 case '{':
9282 case '}':
9283 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9284 break;
9285 case '[': /* "a[" = a [] block */
9286 case ']':
9287 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9288 break;
9289 case '<': /* "a<" = a <> block */
9290 case '>':
9291 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9292 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009293 case 't': /* "at" = a tag block (xml and html) */
9294 flag = current_tagblock(cap->oap, cap->count1, include);
9295 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 case 'p': /* "ap" = a paragraph */
9297 flag = current_par(cap->oap, cap->count1, include, 'p');
9298 break;
9299 case 's': /* "as" = a sentence */
9300 flag = current_sent(cap->oap, cap->count1, include);
9301 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009302 case '"': /* "a"" = a double quoted string */
9303 case '\'': /* "a'" = a single quoted string */
9304 case '`': /* "a`" = a backtick quoted string */
9305 flag = current_quote(cap->oap, cap->count1, include,
9306 cap->nchar);
9307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308#if 0 /* TODO */
9309 case 'S': /* "aS" = a section */
9310 case 'f': /* "af" = a filename */
9311 case 'u': /* "au" = a URL */
9312#endif
9313 default:
9314 flag = FAIL;
9315 break;
9316 }
9317
9318 curbuf->b_p_mps = mps_save;
9319 if (flag == FAIL)
9320 clearopbeep(cap->oap);
9321 adjust_cursor_col();
9322 curwin->w_set_curswant = TRUE;
9323}
9324#endif
9325
9326/*
9327 * "q" command: Start/stop recording.
9328 * "q:", "q/", "q?": edit command-line in command-line window.
9329 */
9330 static void
9331nv_record(cap)
9332 cmdarg_T *cap;
9333{
9334 if (cap->oap->op_type == OP_FORMAT)
9335 {
9336 /* "gqq" is the same as "gqgq": format line */
9337 cap->cmdchar = 'g';
9338 cap->nchar = 'q';
9339 nv_operator(cap);
9340 }
9341 else if (!checkclearop(cap->oap))
9342 {
9343#ifdef FEAT_CMDWIN
9344 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9345 {
9346 stuffcharReadbuff(cap->nchar);
9347 stuffcharReadbuff(K_CMDWIN);
9348 }
9349 else
9350#endif
9351 /* (stop) recording into a named register, unless executing a
9352 * register */
9353 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9354 clearopbeep(cap->oap);
9355 }
9356}
9357
9358/*
9359 * Handle the "@r" command.
9360 */
9361 static void
9362nv_at(cap)
9363 cmdarg_T *cap;
9364{
9365 if (checkclearop(cap->oap))
9366 return;
9367#ifdef FEAT_EVAL
9368 if (cap->nchar == '=')
9369 {
9370 if (get_expr_register() == NUL)
9371 return;
9372 }
9373#endif
9374 while (cap->count1-- && !got_int)
9375 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009376 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 {
9378 clearopbeep(cap->oap);
9379 break;
9380 }
9381 line_breakcheck();
9382 }
9383}
9384
9385/*
9386 * Handle the CTRL-U and CTRL-D commands.
9387 */
9388 static void
9389nv_halfpage(cap)
9390 cmdarg_T *cap;
9391{
9392 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9393 || (cap->cmdchar == Ctrl_D
9394 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9395 clearopbeep(cap->oap);
9396 else if (!checkclearop(cap->oap))
9397 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9398}
9399
9400/*
9401 * Handle "J" or "gJ" command.
9402 */
9403 static void
9404nv_join(cap)
9405 cmdarg_T *cap;
9406{
9407#ifdef FEAT_VISUAL
9408 if (VIsual_active) /* join the visual lines */
9409 nv_operator(cap);
9410 else
9411#endif
9412 if (!checkclearop(cap->oap))
9413 {
9414 if (cap->count0 <= 1)
9415 cap->count0 = 2; /* default for join is two lines! */
9416 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9417 curbuf->b_ml.ml_line_count)
9418 clearopbeep(cap->oap); /* beyond last line */
9419 else
9420 {
9421 prep_redo(cap->oap->regname, cap->count0,
9422 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009423 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
9425 }
9426}
9427
9428/*
9429 * "P", "gP", "p" and "gp" commands.
9430 */
9431 static void
9432nv_put(cap)
9433 cmdarg_T *cap;
9434{
9435#ifdef FEAT_VISUAL
9436 int regname = 0;
9437 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009438 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009439 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440#endif
9441 int dir;
9442 int flags = 0;
9443
9444 if (cap->oap->op_type != OP_NOP)
9445 {
9446#ifdef FEAT_DIFF
9447 /* "dp" is ":diffput" */
9448 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9449 {
9450 clearop(cap->oap);
9451 nv_diffgetput(TRUE);
9452 }
9453 else
9454#endif
9455 clearopbeep(cap->oap);
9456 }
9457 else
9458 {
9459 dir = (cap->cmdchar == 'P'
9460 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9461 ? BACKWARD : FORWARD;
9462 prep_redo_cmd(cap);
9463 if (cap->cmdchar == 'g')
9464 flags |= PUT_CURSEND;
9465
9466#ifdef FEAT_VISUAL
9467 if (VIsual_active)
9468 {
9469 /* Putting in Visual mode: The put text replaces the selected
9470 * text. First delete the selected text, then put the new text.
9471 * Need to save and restore the registers that the delete
9472 * overwrites if the old contents is being put.
9473 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009474 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 regname = cap->oap->regname;
9476# ifdef FEAT_CLIPBOARD
9477 adjust_clip_reg(&regname);
9478# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009479 if (regname == 0 || regname == '"'
9480 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481# ifdef FEAT_CLIPBOARD
9482 || (clip_unnamed && (regname == '*' || regname == '+'))
9483# endif
9484
9485 )
9486 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009487 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 * put, save it first. */
9489 reg1 = get_register(regname, TRUE);
9490 }
9491
9492 /* Now delete the selected text. */
9493 cap->cmdchar = 'd';
9494 cap->nchar = NUL;
9495 cap->oap->regname = NUL;
9496 nv_operator(cap);
9497 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009498 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499
9500 /* delete PUT_LINE_BACKWARD; */
9501 cap->oap->regname = regname;
9502
9503 if (reg1 != NULL)
9504 {
9505 /* Delete probably changed the register we want to put, save
9506 * it first. Then put back what was there before the delete. */
9507 reg2 = get_register(regname, FALSE);
9508 put_register(regname, reg1);
9509 }
9510
9511 /* When deleted a linewise Visual area, put the register as
9512 * lines to avoid it joined with the next line. When deletion was
9513 * characterwise, split a line when putting lines. */
9514 if (VIsual_mode == 'V')
9515 flags |= PUT_LINE;
9516 else if (VIsual_mode == 'v')
9517 flags |= PUT_LINE_SPLIT;
9518 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9519 flags |= PUT_LINE_FORWARD;
9520 dir = BACKWARD;
9521 if ((VIsual_mode != 'V'
9522 && curwin->w_cursor.col < curbuf->b_op_start.col)
9523 || (VIsual_mode == 'V'
9524 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9525 /* cursor is at the end of the line or end of file, put
9526 * forward. */
9527 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009528 /* May have been reset in do_put(). */
9529 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 }
9531#endif
9532 do_put(cap->oap->regname, dir, cap->count1, flags);
9533
9534#ifdef FEAT_VISUAL
9535 /* If a register was saved, put it back now. */
9536 if (reg2 != NULL)
9537 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009538
9539 /* What to reselect with "gv"? Selecting the just put text seems to
9540 * be the most useful, since the original text was removed. */
9541 if (was_visual)
9542 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009543 curbuf->b_visual.vi_start = curbuf->b_op_start;
9544 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009545 }
9546
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009547 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009548 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009549 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009550 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009551 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009552
9553 /* If the cursor was in that line, move it to the end of the last
9554 * line. */
9555 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9556 {
9557 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9558 coladvance((colnr_T)MAXCOL);
9559 }
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#endif
9562 auto_format(FALSE, TRUE);
9563 }
9564}
9565
9566/*
9567 * "o" and "O" commands.
9568 */
9569 static void
9570nv_open(cap)
9571 cmdarg_T *cap;
9572{
9573#ifdef FEAT_DIFF
9574 /* "do" is ":diffget" */
9575 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9576 {
9577 clearop(cap->oap);
9578 nv_diffgetput(FALSE);
9579 }
9580 else
9581#endif
9582#ifdef FEAT_VISUAL
9583 if (VIsual_active) /* switch start and end of visual */
9584 v_swap_corners(cap->cmdchar);
9585 else
9586#endif
9587 n_opencmd(cap);
9588}
9589
9590#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 static void
9592nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009593 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 ProcessSniffRequests();
9596}
9597#endif
9598
9599#ifdef FEAT_NETBEANS_INTG
9600 static void
9601nv_nbcmd(cap)
9602 cmdarg_T *cap;
9603{
9604 netbeans_keycommand(cap->nchar);
9605}
9606#endif
9607
9608#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 static void
9610nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009611 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9614}
9615#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009616
9617#ifdef FEAT_AUTOCMD
9618/*
9619 * Trigger CursorHold event.
9620 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9621 * input buffer. "did_cursorhold" is set to avoid retriggering.
9622 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009623 static void
9624nv_cursorhold(cap)
9625 cmdarg_T *cap;
9626{
9627 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9628 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009629 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009630}
9631#endif