blob: 6bef56dcf610909c204050d5e553f393f0166496 [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
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100658 * when there is no count. Do set it for redo. */
659 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100660 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
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100739 * right after the count. Do set it for redo. */
740 if (toplevel && readbuf1_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.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100822 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100824 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000825 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#endif
827
828 /*
829 * Find the command character in the table of commands.
830 * For CTRL-W we already got nchar when looking for a count.
831 */
832 if (ctrl_w)
833 {
834 ca.nchar = c;
835 ca.cmdchar = Ctrl_W;
836 }
837 else
838 ca.cmdchar = c;
839 idx = find_command(ca.cmdchar);
840 if (idx < 0)
841 {
842 /* Not a known command: beep. */
843 clearopbeep(oap);
844 goto normal_end;
845 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000846
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000847 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000849 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000851 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 goto normal_end;
853 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000854#ifdef FEAT_AUTOCMD
855 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
856 goto normal_end;
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859#ifdef FEAT_VISUAL
860 /*
861 * In Visual/Select mode, a few keys are handled in a special way.
862 */
863 if (VIsual_active)
864 {
865 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
866 if (km_stopsel
867 && (nv_cmds[idx].cmd_flags & NV_STS)
868 && !(mod_mask & MOD_MASK_SHIFT))
869 {
870 end_visual_mode();
871 redraw_curbuf_later(INVERTED);
872 }
873
874 /* Keys that work different when 'keymodel' contains "startsel" */
875 if (km_startsel)
876 {
877 if (nv_cmds[idx].cmd_flags & NV_SS)
878 {
879 unshift_special(&ca);
880 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000881 if (idx < 0)
882 {
883 /* Just in case */
884 clearopbeep(oap);
885 goto normal_end;
886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
889 && (mod_mask & MOD_MASK_SHIFT))
890 {
891 mod_mask &= ~MOD_MASK_SHIFT;
892 }
893 }
894 }
895#endif
896
897#ifdef FEAT_RIGHTLEFT
898 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
899 && (nv_cmds[idx].cmd_flags & NV_RL))
900 {
901 /* Invert horizontal movements and operations. Only when typed by the
902 * user directly, not when the result of a mapping or "x" translated
903 * to "dl". */
904 switch (ca.cmdchar)
905 {
906 case 'l': ca.cmdchar = 'h'; break;
907 case K_RIGHT: ca.cmdchar = K_LEFT; break;
908 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
909 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
910 case 'h': ca.cmdchar = 'l'; break;
911 case K_LEFT: ca.cmdchar = K_RIGHT; break;
912 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
913 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
914 case '>': ca.cmdchar = '<'; break;
915 case '<': ca.cmdchar = '>'; break;
916 }
917 idx = find_command(ca.cmdchar);
918 }
919#endif
920
921 /*
922 * Get an additional character if we need one.
923 */
924 if ((nv_cmds[idx].cmd_flags & NV_NCH)
925 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
926 && oap->op_type == OP_NOP)
927 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
928 || (ca.cmdchar == 'q'
929 && oap->op_type == OP_NOP
930 && !Recording
931 && !Exec_reg)
932 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
933 && (oap->op_type != OP_NOP
934#ifdef FEAT_VISUAL
935 || VIsual_active
936#endif
937 ))))
938 {
939 int *cp;
940 int repl = FALSE; /* get character for replace mode */
941 int lit = FALSE; /* get extra character literally */
942 int langmap_active = FALSE; /* using :lmap mappings */
943 int lang; /* getting a text character */
944#ifdef USE_IM_CONTROL
945 int save_smd; /* saved value of p_smd */
946#endif
947
948 ++no_mapping;
949 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000950#ifdef FEAT_AUTOCMD
951 /* Don't generate a CursorHold event here, most commands can't handle
952 * it, e.g., nv_replace(), nv_csearch(). */
953 did_cursorhold = TRUE;
954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (ca.cmdchar == 'g')
956 {
957 /*
958 * For 'g' get the next character now, so that we can check for
959 * "gr", "g'" and "g`".
960 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000961 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#ifdef FEAT_CMDL_INFO
964 need_flushbuf |= add_to_showcmd(ca.nchar);
965#endif
966 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100967 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 cp = &ca.extra_char; /* need to get a third character */
970 if (ca.nchar != 'r')
971 lit = TRUE; /* get it literally */
972 else
973 repl = TRUE; /* get it in replace mode */
974 }
975 else
976 cp = NULL; /* no third character needed */
977 }
978 else
979 {
980 if (ca.cmdchar == 'r') /* get it in replace mode */
981 repl = TRUE;
982 cp = &ca.nchar;
983 }
984 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
985
986 /*
987 * Get a second or third character.
988 */
989 if (cp != NULL)
990 {
991#ifdef CURSOR_SHAPE
992 if (repl)
993 {
994 State = REPLACE; /* pretend Replace mode */
995 ui_cursor_shape(); /* show different cursor shape */
996 }
997#endif
998 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
999 {
1000 /* Allow mappings defined with ":lmap". */
1001 --no_mapping;
1002 --allow_keys;
1003 if (repl)
1004 State = LREPLACE;
1005 else
1006 State = LANGMAP;
1007 langmap_active = TRUE;
1008 }
1009#ifdef USE_IM_CONTROL
1010 save_smd = p_smd;
1011 p_smd = FALSE; /* Don't let the IM code show the mode here */
1012 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1013 im_set_active(TRUE);
1014#endif
1015
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001016 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
1018 if (langmap_active)
1019 {
1020 /* Undo the decrement done above */
1021 ++no_mapping;
1022 ++allow_keys;
1023 State = NORMAL_BUSY;
1024 }
1025#ifdef USE_IM_CONTROL
1026 if (lang)
1027 {
1028 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1029 im_save_status(&curbuf->b_p_iminsert);
1030 im_set_active(FALSE);
1031 }
1032 p_smd = save_smd;
1033#endif
1034#ifdef CURSOR_SHAPE
1035 State = NORMAL_BUSY;
1036#endif
1037#ifdef FEAT_CMDL_INFO
1038 need_flushbuf |= add_to_showcmd(*cp);
1039#endif
1040
1041 if (!lit)
1042 {
1043#ifdef FEAT_DIGRAPHS
1044 /* Typing CTRL-K gets a digraph. */
1045 if (*cp == Ctrl_K
1046 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1047 || cp == &ca.extra_char)
1048 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1049 {
1050 c = get_digraph(FALSE);
1051 if (c > 0)
1052 {
1053 *cp = c;
1054# ifdef FEAT_CMDL_INFO
1055 /* Guessing how to update showcmd here... */
1056 del_from_showcmd(3);
1057 need_flushbuf |= add_to_showcmd(*cp);
1058# endif
1059 }
1060 }
1061#endif
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 /* adjust chars > 127, except after "tTfFr" commands */
1064 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#ifdef FEAT_RIGHTLEFT
1066 /* adjust Hebrew mapped char */
1067 if (p_hkmap && lang && KeyTyped)
1068 *cp = hkmap(*cp);
1069# ifdef FEAT_FKMAP
1070 /* adjust Farsi mapped char */
1071 if (p_fkmap && lang && KeyTyped)
1072 *cp = fkmap(*cp);
1073# endif
1074#endif
1075 }
1076
1077 /*
1078 * When the next character is CTRL-\ a following CTRL-N means the
1079 * command is aborted and we go to Normal mode.
1080 */
1081 if (cp == &ca.extra_char
1082 && ca.nchar == Ctrl_BSL
1083 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1084 {
1085 ca.cmdchar = Ctrl_BSL;
1086 ca.nchar = ca.extra_char;
1087 idx = find_command(ca.cmdchar);
1088 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001089 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001090 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else if (*cp == Ctrl_BSL)
1092 {
1093 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1094
1095 /* There is a busy wait here when typing "f<C-\>" and then
1096 * something different from CTRL-N. Can't be avoided. */
1097 while ((c = vpeekc()) <= 0 && towait > 0L)
1098 {
1099 do_sleep(towait > 50L ? 50L : towait);
1100 towait -= 50L;
1101 }
1102 if (c > 0)
1103 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001104 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (c != Ctrl_N && c != Ctrl_G)
1106 vungetc(c);
1107 else
1108 {
1109 ca.cmdchar = Ctrl_BSL;
1110 ca.nchar = c;
1111 idx = find_command(ca.cmdchar);
1112 }
1113 }
1114 }
1115
1116#ifdef FEAT_MBYTE
1117 /* When getting a text character and the next character is a
1118 * multi-byte character, it could be a composing character.
1119 * However, don't wait for it to arrive. */
1120 while (enc_utf8 && lang && (c = vpeekc()) > 0
1121 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1122 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001123 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (!utf_iscomposing(c))
1125 {
1126 vungetc(c); /* it wasn't, put it back */
1127 break;
1128 }
1129 else if (ca.ncharC1 == 0)
1130 ca.ncharC1 = c;
1131 else
1132 ca.ncharC2 = c;
1133 }
1134#endif
1135 }
1136 --no_mapping;
1137 --allow_keys;
1138 }
1139
1140#ifdef FEAT_CMDL_INFO
1141 /*
1142 * Flush the showcmd characters onto the screen so we can see them while
1143 * the command is being executed. Only do this when the shown command was
1144 * actually displayed, otherwise this will slow down a lot when executing
1145 * mappings.
1146 */
1147 if (need_flushbuf)
1148 out_flush();
1149#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001150#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001151 if (ca.cmdchar != K_IGNORE)
1152 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 State = NORMAL;
1156
1157 if (ca.nchar == ESC)
1158 {
1159 clearop(oap);
1160 if (restart_edit == 0 && goto_im())
1161 restart_edit = 'a';
1162 goto normal_end;
1163 }
1164
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001165 if (ca.cmdchar != K_IGNORE)
1166 {
1167 msg_didout = FALSE; /* don't scroll screen up for normal command */
1168 msg_col = 0;
1169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171#ifdef FEAT_VISUAL
1172 old_pos = curwin->w_cursor; /* remember where cursor was */
1173
1174 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1175 * mode. */
1176 if (!VIsual_active && km_startsel)
1177 {
1178 if (nv_cmds[idx].cmd_flags & NV_SS)
1179 {
1180 start_selection();
1181 unshift_special(&ca);
1182 idx = find_command(ca.cmdchar);
1183 }
1184 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1185 && (mod_mask & MOD_MASK_SHIFT))
1186 {
1187 start_selection();
1188 mod_mask &= ~MOD_MASK_SHIFT;
1189 }
1190 }
1191#endif
1192
1193 /*
1194 * Execute the command!
1195 * Call the command function found in the commands table.
1196 */
1197 ca.arg = nv_cmds[idx].cmd_arg;
1198 (nv_cmds[idx].cmd_func)(&ca);
1199
1200 /*
1201 * If we didn't start or finish an operator, reset oap->regname, unless we
1202 * need it later.
1203 */
1204 if (!finish_op
1205 && !oap->op_type
1206 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1207 {
1208 clearop(oap);
1209#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001210 {
1211 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001212
Bram Moolenaar536681b2011-05-10 16:12:45 +02001213 /* Adjust the register according to 'clipboard', so that when
1214 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001215# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001216 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001217# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001218 set_reg_var(regname);
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 }
1222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001223#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001224 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001225 * character or "z333<cr>". */
1226 if (old_mapped_len > 0)
1227 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001228#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001229
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 /*
1231 * If an operation is pending, handle it...
1232 */
1233 do_pending_operator(&ca, old_col, FALSE);
1234
1235 /*
1236 * Wait for a moment when a message is displayed that will be overwritten
1237 * by the mode message.
1238 * In Visual mode and with "^O" in Insert mode, a short message will be
1239 * overwritten by the mode message. Wait a bit, until a key is hit.
1240 * In Visual mode, it's more important to keep the Visual area updated
1241 * than keeping a message (e.g. from a /pat search).
1242 * Only do this if the command was typed, not from a mapping.
1243 * Don't wait when emsg_silent is non-zero.
1244 * Also wait a bit after an error message, e.g. for "^O:".
1245 * Don't redraw the screen, it would remove the message.
1246 */
1247 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001248 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 && (restart_edit != 0
1250#ifdef FEAT_VISUAL
1251 || (VIsual_active
1252 && old_pos.lnum == curwin->w_cursor.lnum
1253 && old_pos.col == curwin->w_cursor.col)
1254#endif
1255 )
1256 && (clear_cmdline
1257 || redraw_cmdline)
1258 && (msg_didout || (msg_didany && msg_scroll))
1259 && !msg_nowait
1260 && KeyTyped)
1261 || (restart_edit != 0
1262#ifdef FEAT_VISUAL
1263 && !VIsual_active
1264#endif
1265 && (msg_scroll
1266 || emsg_on_display)))
1267 && oap->regname == 0
1268 && !(ca.retval & CA_COMMAND_BUSY)
1269 && stuff_empty()
1270 && typebuf_typed()
1271 && emsg_silent == 0
1272 && !did_wait_return
1273 && oap->op_type == OP_NOP)
1274 {
1275 int save_State = State;
1276
1277 /* Draw the cursor with the right shape here */
1278 if (restart_edit != 0)
1279 State = INSERT;
1280
1281 /* If need to redraw, and there is a "keep_msg", redraw before the
1282 * delay */
1283 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1284 {
1285 char_u *kmsg;
1286
1287 kmsg = keep_msg;
1288 keep_msg = NULL;
1289 /* showmode() will clear keep_msg, but we want to use it anyway */
1290 update_screen(0);
1291 /* now reset it, otherwise it's put in the history again */
1292 keep_msg = kmsg;
1293 msg_attr(kmsg, keep_msg_attr);
1294 vim_free(kmsg);
1295 }
1296 setcursor();
1297 cursor_on();
1298 out_flush();
1299 if (msg_scroll || emsg_on_display)
1300 ui_delay(1000L, TRUE); /* wait at least one second */
1301 ui_delay(3000L, FALSE); /* wait up to three seconds */
1302 State = save_State;
1303
1304 msg_scroll = FALSE;
1305 emsg_on_display = FALSE;
1306 }
1307
1308 /*
1309 * Finish up after executing a Normal mode command.
1310 */
1311normal_end:
1312
1313 msg_nowait = FALSE;
1314
1315 /* Reset finish_op, in case it was set */
1316#ifdef CURSOR_SHAPE
1317 c = finish_op;
1318#endif
1319 finish_op = FALSE;
1320#ifdef CURSOR_SHAPE
1321 /* Redraw the cursor with another shape, if we were in Operator-pending
1322 * mode or did a replace command. */
1323 if (c || ca.cmdchar == 'r')
1324 {
1325 ui_cursor_shape(); /* may show different cursor shape */
1326# ifdef FEAT_MOUSESHAPE
1327 update_mouseshape(-1);
1328# endif
1329 }
1330#endif
1331
1332#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001333 if (oap->op_type == OP_NOP && oap->regname == 0
1334# ifdef FEAT_AUTOCMD
1335 && ca.cmdchar != K_CURSORHOLD
1336# endif
1337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 clear_showcmd();
1339#endif
1340
1341 checkpcmark(); /* check if we moved since setting pcmark */
1342 vim_free(ca.searchbuf);
1343
1344#ifdef FEAT_MBYTE
1345 if (has_mbyte)
1346 mb_adjust_cursor();
1347#endif
1348
1349#ifdef FEAT_SCROLLBIND
1350 if (curwin->w_p_scb && toplevel)
1351 {
1352 validate_cursor(); /* may need to update w_leftcol */
1353 do_check_scrollbind(TRUE);
1354 }
1355#endif
1356
Bram Moolenaar860cae12010-06-05 23:22:07 +02001357#ifdef FEAT_CURSORBIND
1358 if (curwin->w_p_crb && toplevel)
1359 {
1360 validate_cursor(); /* may need to update w_leftcol */
1361 do_check_cursorbind();
1362 }
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 /*
1366 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1367 * if still inside a mapping that started in Visual mode).
1368 * May switch from Visual to Select mode after CTRL-O command.
1369 */
1370 if ( oap->op_type == OP_NOP
1371#ifdef FEAT_VISUAL
1372 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1373 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001374#else
1375 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#endif
1377 && !(ca.retval & CA_COMMAND_BUSY)
1378 && stuff_empty()
1379 && oap->regname == 0)
1380 {
1381#ifdef FEAT_VISUAL
1382 if (restart_VIsual_select == 1)
1383 {
1384 VIsual_select = TRUE;
1385 showmode();
1386 restart_VIsual_select = 0;
1387 }
1388#endif
1389 if (restart_edit != 0
1390#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001391 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001393 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 (void)edit(restart_edit, FALSE, 1L);
1395 }
1396
1397#ifdef FEAT_VISUAL
1398 if (restart_VIsual_select == 2)
1399 restart_VIsual_select = 1;
1400#endif
1401
1402 /* Save count before an operator for next time. */
1403 opcount = ca.opcount;
1404}
1405
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001406#ifdef FEAT_EVAL
1407/*
1408 * Set v:count and v:count1 according to "cap".
1409 * Set v:prevcount only when "set_prevcount" is TRUE.
1410 */
1411 static void
1412set_vcount_ca(cap, set_prevcount)
1413 cmdarg_T *cap;
1414 int *set_prevcount;
1415{
1416 long count = cap->count0;
1417
1418 /* multiply with cap->opcount the same way as above */
1419 if (cap->opcount != 0)
1420 count = cap->opcount * (count == 0 ? 1 : count);
1421 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1422 *set_prevcount = FALSE; /* only set v:prevcount once */
1423}
1424#endif
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/*
1427 * Handle an operator after visual mode or when the movement is finished
1428 */
1429 void
1430do_pending_operator(cap, old_col, gui_yank)
1431 cmdarg_T *cap;
1432 int old_col;
1433 int gui_yank;
1434{
1435 oparg_T *oap = cap->oap;
1436 pos_T old_cursor;
1437 int empty_region_error;
1438 int restart_edit_save;
1439
1440#ifdef FEAT_VISUAL
1441 /* The visual area is remembered for redo */
1442 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1443 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001444 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 static long redo_VIsual_count; /* count for Visual operator */
1446# ifdef FEAT_VIRTUALEDIT
1447 int include_line_break = FALSE;
1448# endif
1449#endif
1450
1451#if defined(FEAT_CLIPBOARD)
1452 /*
1453 * Yank the visual area into the GUI selection register before we operate
1454 * on it and lose it forever.
1455 * Don't do it if a specific register was specified, so that ""x"*P works.
1456 * This could call do_pending_operator() recursively, but that's OK
1457 * because gui_yank will be TRUE for the nested call.
1458 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001459 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 && oap->op_type != OP_NOP
1461 && !gui_yank
1462# ifdef FEAT_VISUAL
1463 && VIsual_active
1464 && !redo_VIsual_busy
1465# endif
1466 && oap->regname == 0)
1467 clip_auto_select();
1468#endif
1469 old_cursor = curwin->w_cursor;
1470
1471 /*
1472 * If an operation is pending, handle it...
1473 */
1474 if ((finish_op
1475#ifdef FEAT_VISUAL
1476 || VIsual_active
1477#endif
1478 ) && oap->op_type != OP_NOP)
1479 {
1480#ifdef FEAT_VISUAL
1481 oap->is_VIsual = VIsual_active;
1482 if (oap->motion_force == 'V')
1483 oap->motion_type = MLINE;
1484 else if (oap->motion_force == 'v')
1485 {
1486 /* If the motion was linewise, "inclusive" will not have been set.
1487 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1488 if (oap->motion_type == MLINE)
1489 oap->inclusive = FALSE;
1490 /* If the motion already was characterwise, toggle "inclusive" */
1491 else if (oap->motion_type == MCHAR)
1492 oap->inclusive = !oap->inclusive;
1493 oap->motion_type = MCHAR;
1494 }
1495 else if (oap->motion_force == Ctrl_V)
1496 {
1497 /* Change line- or characterwise motion into Visual block mode. */
1498 VIsual_active = TRUE;
1499 VIsual = oap->start;
1500 VIsual_mode = Ctrl_V;
1501 VIsual_select = FALSE;
1502 VIsual_reselect = FALSE;
1503 }
1504#endif
1505
Bram Moolenaar7afea822013-04-24 18:34:45 +02001506 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1507 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1509#ifdef FEAT_VISUAL
Bram Moolenaar7afea822013-04-24 18:34:45 +02001510 && ((!VIsual_active || oap->motion_force)
1511 /* Also redo Operator-pending Visual mode mappings */
1512 || (VIsual_active && cap->cmdchar == ':'
1513 && oap->op_type != OP_COLON))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001515 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#ifdef FEAT_FOLDING
1517 && oap->op_type != OP_FOLD
1518 && oap->op_type != OP_FOLDOPEN
1519 && oap->op_type != OP_FOLDOPENREC
1520 && oap->op_type != OP_FOLDCLOSE
1521 && oap->op_type != OP_FOLDCLOSEREC
1522 && oap->op_type != OP_FOLDDEL
1523 && oap->op_type != OP_FOLDDELREC
1524#endif
1525 )
1526 {
1527 prep_redo(oap->regname, cap->count0,
1528 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1529 oap->motion_force, cap->cmdchar, cap->nchar);
1530 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1531 {
1532 /*
1533 * If 'cpoptions' does not contain 'r', insert the search
1534 * pattern to really repeat the same command.
1535 */
1536 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001537 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 AppendToRedobuff(NL_STR);
1539 }
1540 else if (cap->cmdchar == ':')
1541 {
1542 /* do_cmdline() has stored the first typed line in
1543 * "repeat_cmdline". When several lines are typed repeating
1544 * won't be possible. */
1545 if (repeat_cmdline == NULL)
1546 ResetRedobuff();
1547 else
1548 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001549 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 AppendToRedobuff(NL_STR);
1551 vim_free(repeat_cmdline);
1552 repeat_cmdline = NULL;
1553 }
1554 }
1555 }
1556
1557#ifdef FEAT_VISUAL
1558 if (redo_VIsual_busy)
1559 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001560 /* Redo of an operation on a Visual area. Use the same size from
1561 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 oap->start = curwin->w_cursor;
1563 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1564 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1565 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1566 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001567 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001569 if (VIsual_mode == 'v')
1570 {
1571 if (redo_VIsual_line_count <= 1)
1572 {
1573 validate_virtcol();
1574 curwin->w_curswant =
1575 curwin->w_virtcol + redo_VIsual_vcol - 1;
1576 }
1577 else
1578 curwin->w_curswant = redo_VIsual_vcol;
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001581 {
1582 curwin->w_curswant = MAXCOL;
1583 }
1584 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 cap->count0 = redo_VIsual_count;
1587 if (redo_VIsual_count != 0)
1588 cap->count1 = redo_VIsual_count;
1589 else
1590 cap->count1 = 1;
1591 }
1592 else if (VIsual_active)
1593 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001594 if (!gui_yank)
1595 {
1596 /* Save the current VIsual area for '< and '> marks, and "gv" */
1597 curbuf->b_visual.vi_start = VIsual;
1598 curbuf->b_visual.vi_end = curwin->w_cursor;
1599 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001600 if (VIsual_mode_orig != NUL)
1601 {
1602 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1603 VIsual_mode_orig = NUL;
1604 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001605 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001607 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 /* In Select mode, a linewise selection is operated upon like a
1612 * characterwise selection. */
1613 if (VIsual_select && VIsual_mode == 'V')
1614 {
1615 if (lt(VIsual, curwin->w_cursor))
1616 {
1617 VIsual.col = 0;
1618 curwin->w_cursor.col =
1619 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1620 }
1621 else
1622 {
1623 curwin->w_cursor.col = 0;
1624 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1625 }
1626 VIsual_mode = 'v';
1627 }
1628 /* If 'selection' is "exclusive", backup one character for
1629 * charwise selections. */
1630 else if (VIsual_mode == 'v')
1631 {
1632# ifdef FEAT_VIRTUALEDIT
1633 include_line_break =
1634# endif
1635 unadjust_for_sel();
1636 }
1637
1638 oap->start = VIsual;
1639 if (VIsual_mode == 'V')
1640 oap->start.col = 0;
1641 }
1642#endif /* FEAT_VISUAL */
1643
1644 /*
1645 * Set oap->start to the first position of the operated text, oap->end
1646 * to the end of the operated text. w_cursor is equal to oap->start.
1647 */
1648 if (lt(oap->start, curwin->w_cursor))
1649 {
1650#ifdef FEAT_FOLDING
1651 /* Include folded lines completely. */
1652 if (!VIsual_active)
1653 {
1654 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1655 oap->start.col = 0;
1656 if (hasFolding(curwin->w_cursor.lnum, NULL,
1657 &curwin->w_cursor.lnum))
1658 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1659 }
1660#endif
1661 oap->end = curwin->w_cursor;
1662 curwin->w_cursor = oap->start;
1663
1664 /* w_virtcol may have been updated; if the cursor goes back to its
1665 * previous position w_virtcol becomes invalid and isn't updated
1666 * automatically. */
1667 curwin->w_valid &= ~VALID_VIRTCOL;
1668 }
1669 else
1670 {
1671#ifdef FEAT_FOLDING
1672 /* Include folded lines completely. */
1673 if (!VIsual_active && oap->motion_type == MLINE)
1674 {
1675 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1676 NULL))
1677 curwin->w_cursor.col = 0;
1678 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1679 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1680 }
1681#endif
1682 oap->end = oap->start;
1683 oap->start = curwin->w_cursor;
1684 }
1685
1686 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1687
1688#ifdef FEAT_VIRTUALEDIT
1689 /* Set "virtual_op" before resetting VIsual_active. */
1690 virtual_op = virtual_active();
1691#endif
1692
1693#ifdef FEAT_VISUAL
1694 if (VIsual_active || redo_VIsual_busy)
1695 {
1696 if (VIsual_mode == Ctrl_V) /* block mode */
1697 {
1698 colnr_T start, end;
1699
1700 oap->block_mode = TRUE;
1701
1702 getvvcol(curwin, &(oap->start),
1703 &oap->start_vcol, NULL, &oap->end_vcol);
1704 if (!redo_VIsual_busy)
1705 {
1706 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1707
1708 if (start < oap->start_vcol)
1709 oap->start_vcol = start;
1710 if (end > oap->end_vcol)
1711 {
1712 if (*p_sel == 'e' && start >= 1
1713 && start - 1 >= oap->end_vcol)
1714 oap->end_vcol = start - 1;
1715 else
1716 oap->end_vcol = end;
1717 }
1718 }
1719
1720 /* if '$' was used, get oap->end_vcol from longest line */
1721 if (curwin->w_curswant == MAXCOL)
1722 {
1723 curwin->w_cursor.col = MAXCOL;
1724 oap->end_vcol = 0;
1725 for (curwin->w_cursor.lnum = oap->start.lnum;
1726 curwin->w_cursor.lnum <= oap->end.lnum;
1727 ++curwin->w_cursor.lnum)
1728 {
1729 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1730 if (end > oap->end_vcol)
1731 oap->end_vcol = end;
1732 }
1733 }
1734 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001735 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /*
1737 * Correct oap->end.col and oap->start.col to be the
1738 * upper-left and lower-right corner of the block area.
1739 *
1740 * (Actually, this does convert column positions into character
1741 * positions)
1742 */
1743 curwin->w_cursor.lnum = oap->end.lnum;
1744 coladvance(oap->end_vcol);
1745 oap->end = curwin->w_cursor;
1746
1747 curwin->w_cursor = oap->start;
1748 coladvance(oap->start_vcol);
1749 oap->start = curwin->w_cursor;
1750 }
1751
1752 if (!redo_VIsual_busy && !gui_yank)
1753 {
1754 /*
1755 * Prepare to reselect and redo Visual: this is based on the
1756 * size of the Visual text
1757 */
1758 resel_VIsual_mode = VIsual_mode;
1759 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001760 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001762 {
1763 if (VIsual_mode != Ctrl_V)
1764 getvvcol(curwin, &(oap->end),
1765 NULL, NULL, &oap->end_vcol);
1766 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1767 {
1768 if (VIsual_mode != Ctrl_V)
1769 getvvcol(curwin, &(oap->start),
1770 &oap->start_vcol, NULL, NULL);
1771 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1772 }
1773 else
1774 resel_VIsual_vcol = oap->end_vcol;
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 resel_VIsual_line_count = oap->line_count;
1777 }
1778
1779 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1780 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1781 && oap->op_type != OP_COLON
1782#ifdef FEAT_FOLDING
1783 && oap->op_type != OP_FOLD
1784 && oap->op_type != OP_FOLDOPEN
1785 && oap->op_type != OP_FOLDOPENREC
1786 && oap->op_type != OP_FOLDCLOSE
1787 && oap->op_type != OP_FOLDCLOSEREC
1788 && oap->op_type != OP_FOLDDEL
1789 && oap->op_type != OP_FOLDDELREC
1790#endif
1791 && oap->motion_force == NUL
1792 )
1793 {
1794 /* Prepare for redoing. Only use the nchar field for "r",
1795 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001796 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1797 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001798 prep_redo(oap->regname, cap->count0,
1799 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1800 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001801 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001802 prep_redo(oap->regname, 0L, NUL, 'v',
1803 get_op_char(oap->op_type),
1804 get_extra_op_char(oap->op_type),
1805 oap->op_type == OP_REPLACE
1806 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (!redo_VIsual_busy)
1808 {
1809 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001810 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redo_VIsual_line_count = resel_VIsual_line_count;
1812 redo_VIsual_count = cap->count0;
1813 }
1814 }
1815
1816 /*
1817 * oap->inclusive defaults to TRUE.
1818 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1819 * FALSE. This makes "d}P" and "v}dP" work the same.
1820 */
1821 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1822 oap->inclusive = TRUE;
1823 if (VIsual_mode == 'V')
1824 oap->motion_type = MLINE;
1825 else
1826 {
1827 oap->motion_type = MCHAR;
1828 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1829# ifdef FEAT_VIRTUALEDIT
1830 && (include_line_break || !virtual_op)
1831# endif
1832 )
1833 {
1834 oap->inclusive = FALSE;
1835 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001836 * that works on lines only. */
1837 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001839 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1840 {
1841 ++oap->end.lnum;
1842 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001844 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001846 ++oap->line_count;
1847 }
1848 else
1849 {
1850 /* Cannot move below the last line, make the op
1851 * inclusive to tell the operation to include the
1852 * line break. */
1853 oap->inclusive = TRUE;
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856 }
1857 }
1858
1859 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 /*
1862 * Switch Visual off now, so screen updating does
1863 * not show inverted text when the screen is redrawn.
1864 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1865 * no screen redraw, so it is done here to remove the inverted
1866 * part.
1867 */
1868 if (!gui_yank)
1869 {
1870 VIsual_active = FALSE;
1871# ifdef FEAT_MOUSE
1872 setmouse();
1873 mouse_dragging = 0;
1874# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001875 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 clear_cmdline = TRUE; /* unshow visual mode later */
1877#ifdef FEAT_CMDL_INFO
1878 else
1879 clear_showcmd();
1880#endif
1881 if ((oap->op_type == OP_YANK
1882 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001883 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 || oap->op_type == OP_FILTER)
1885 && oap->motion_force == NUL)
1886 redraw_curbuf_later(INVERTED);
1887 }
1888 }
1889#endif
1890
1891#ifdef FEAT_MBYTE
1892 /* Include the trailing byte of a multi-byte char. */
1893 if (has_mbyte && oap->inclusive)
1894 {
1895 int l;
1896
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001897 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (l > 1)
1899 oap->end.col += l - 1;
1900 }
1901#endif
1902 curwin->w_set_curswant = TRUE;
1903
1904 /*
1905 * oap->empty is set when start and end are the same. The inclusive
1906 * flag affects this too, unless yanking and the end is on a NUL.
1907 */
1908 oap->empty = (oap->motion_type == MCHAR
1909 && (!oap->inclusive
1910 || (oap->op_type == OP_YANK
1911 && gchar_pos(&oap->end) == NUL))
1912 && equalpos(oap->start, oap->end)
1913#ifdef FEAT_VIRTUALEDIT
1914 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1915#endif
1916 );
1917 /*
1918 * For delete, change and yank, it's an error to operate on an
1919 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1920 */
1921 empty_region_error = (oap->empty
1922 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1923
1924#ifdef FEAT_VISUAL
1925 /* Force a redraw when operating on an empty Visual region, when
1926 * 'modifiable is off or creating a fold. */
1927 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1928# ifdef FEAT_FOLDING
1929 || oap->op_type == OP_FOLD
1930# endif
1931 ))
1932 redraw_curbuf_later(INVERTED);
1933#endif
1934
1935 /*
1936 * If the end of an operator is in column one while oap->motion_type
1937 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1938 * character in the previous line. If op_start is on or before the
1939 * first non-blank in the line, the operator becomes linewise
1940 * (strange, but that's the way vi does it).
1941 */
1942 if ( oap->motion_type == MCHAR
1943 && oap->inclusive == FALSE
1944 && !(cap->retval & CA_NO_ADJ_OP_END)
1945 && oap->end.col == 0
1946#ifdef FEAT_VISUAL
1947 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001948 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#endif
1950 && oap->line_count > 1)
1951 {
1952 oap->end_adjusted = TRUE; /* remember that we did this */
1953 --oap->line_count;
1954 --oap->end.lnum;
1955 if (inindent(0))
1956 oap->motion_type = MLINE;
1957 else
1958 {
1959 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1960 if (oap->end.col)
1961 {
1962 --oap->end.col;
1963 oap->inclusive = TRUE;
1964 }
1965 }
1966 }
1967 else
1968 oap->end_adjusted = FALSE;
1969
1970 switch (oap->op_type)
1971 {
1972 case OP_LSHIFT:
1973 case OP_RSHIFT:
1974 op_shift(oap, TRUE,
1975#ifdef FEAT_VISUAL
1976 oap->is_VIsual ? (int)cap->count1 :
1977#endif
1978 1);
1979 auto_format(FALSE, TRUE);
1980 break;
1981
1982 case OP_JOIN_NS:
1983 case OP_JOIN:
1984 if (oap->line_count < 2)
1985 oap->line_count = 2;
1986 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1987 curbuf->b_ml.ml_line_count)
1988 beep_flush();
1989 else
1990 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001991 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 auto_format(FALSE, TRUE);
1993 }
1994 break;
1995
1996 case OP_DELETE:
1997#ifdef FEAT_VISUAL
1998 VIsual_reselect = FALSE; /* don't reselect now */
1999#endif
2000 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002003 CancelRedo();
2004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 else
2006 {
2007 (void)op_delete(oap);
2008 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
2009 u_save_cursor(); /* cursor line wasn't saved yet */
2010 auto_format(FALSE, TRUE);
2011 }
2012 break;
2013
2014 case OP_YANK:
2015 if (empty_region_error)
2016 {
2017 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002020 CancelRedo();
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 }
2023 else
2024 (void)op_yank(oap, FALSE, !gui_yank);
2025 check_cursor_col();
2026 break;
2027
2028 case OP_CHANGE:
2029#ifdef FEAT_VISUAL
2030 VIsual_reselect = FALSE; /* don't reselect now */
2031#endif
2032 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002035 CancelRedo();
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 else
2038 {
2039 /* This is a new edit command, not a restart. Need to
2040 * remember it to make 'insertmode' work with mappings for
2041 * Visual mode. But do this only once and not when typed and
2042 * 'insertmode' isn't set. */
2043 if (p_im || !KeyTyped)
2044 restart_edit_save = restart_edit;
2045 else
2046 restart_edit_save = 0;
2047 restart_edit = 0;
2048 /* Reset finish_op now, don't want it set inside edit(). */
2049 finish_op = FALSE;
2050 if (op_change(oap)) /* will call edit() */
2051 cap->retval |= CA_COMMAND_BUSY;
2052 if (restart_edit == 0)
2053 restart_edit = restart_edit_save;
2054 }
2055 break;
2056
2057 case OP_FILTER:
2058 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2059 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2060 else
2061 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2062
2063 case OP_INDENT:
2064 case OP_COLON:
2065
2066#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2067 /*
2068 * If 'equalprg' is empty, do the indenting internally.
2069 */
2070 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2071 {
2072# ifdef FEAT_LISP
2073 if (curbuf->b_p_lisp)
2074 {
2075 op_reindent(oap, get_lisp_indent);
2076 break;
2077 }
2078# endif
2079# ifdef FEAT_CINDENT
2080 op_reindent(oap,
2081# ifdef FEAT_EVAL
2082 *curbuf->b_p_inde != NUL ? get_expr_indent :
2083# endif
2084 get_c_indent);
2085 break;
2086# endif
2087 }
2088#endif
2089
2090 op_colon(oap);
2091 break;
2092
2093 case OP_TILDE:
2094 case OP_UPPER:
2095 case OP_LOWER:
2096 case OP_ROT13:
2097 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002100 CancelRedo();
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 else
2103 op_tilde(oap);
2104 check_cursor_col();
2105 break;
2106
2107 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002108#if defined(FEAT_EVAL)
2109 if (*curbuf->b_p_fex != NUL)
2110 op_formatexpr(oap); /* use expression */
2111 else
2112#endif
2113 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 op_colon(oap); /* use external command */
2115 else
2116 op_format(oap, FALSE); /* use internal function */
2117 break;
2118
2119 case OP_FORMAT2:
2120 op_format(oap, TRUE); /* use internal function */
2121 break;
2122
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002123 case OP_FUNCTION:
2124 op_function(oap); /* call 'operatorfunc' */
2125 break;
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 case OP_INSERT:
2128 case OP_APPEND:
2129#ifdef FEAT_VISUAL
2130 VIsual_reselect = FALSE; /* don't reselect now */
2131#endif
2132#ifdef FEAT_VISUALEXTRA
2133 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002136 CancelRedo();
2137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 else
2139 {
2140 /* This is a new edit command, not a restart. Need to
2141 * remember it to make 'insertmode' work with mappings for
2142 * Visual mode. But do this only once. */
2143 restart_edit_save = restart_edit;
2144 restart_edit = 0;
2145
2146 op_insert(oap, cap->count1);
2147
2148 /* TODO: when inserting in several lines, should format all
2149 * the lines. */
2150 auto_format(FALSE, TRUE);
2151
2152 if (restart_edit == 0)
2153 restart_edit = restart_edit_save;
2154 }
2155#else
2156 vim_beep();
2157#endif
2158 break;
2159
2160 case OP_REPLACE:
2161#ifdef FEAT_VISUAL
2162 VIsual_reselect = FALSE; /* don't reselect now */
2163#endif
2164#ifdef FEAT_VISUALEXTRA
2165 if (empty_region_error)
2166#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002169 CancelRedo();
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#ifdef FEAT_VISUALEXTRA
2172 else
2173 op_replace(oap, cap->nchar);
2174#endif
2175 break;
2176
2177#ifdef FEAT_FOLDING
2178 case OP_FOLD:
2179 VIsual_reselect = FALSE; /* don't reselect now */
2180 foldCreate(oap->start.lnum, oap->end.lnum);
2181 break;
2182
2183 case OP_FOLDOPEN:
2184 case OP_FOLDOPENREC:
2185 case OP_FOLDCLOSE:
2186 case OP_FOLDCLOSEREC:
2187 VIsual_reselect = FALSE; /* don't reselect now */
2188 opFoldRange(oap->start.lnum, oap->end.lnum,
2189 oap->op_type == OP_FOLDOPEN
2190 || oap->op_type == OP_FOLDOPENREC,
2191 oap->op_type == OP_FOLDOPENREC
2192 || oap->op_type == OP_FOLDCLOSEREC,
2193 oap->is_VIsual);
2194 break;
2195
2196 case OP_FOLDDEL:
2197 case OP_FOLDDELREC:
2198 VIsual_reselect = FALSE; /* don't reselect now */
2199 deleteFold(oap->start.lnum, oap->end.lnum,
2200 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2201 break;
2202#endif
2203 default:
2204 clearopbeep(oap);
2205 }
2206#ifdef FEAT_VIRTUALEDIT
2207 virtual_op = MAYBE;
2208#endif
2209 if (!gui_yank)
2210 {
2211 /*
2212 * if 'sol' not set, go back to old column for some commands
2213 */
2214 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2215 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2216 || oap->op_type == OP_DELETE))
2217 coladvance(curwin->w_curswant = old_col);
2218 }
2219 else
2220 {
2221 curwin->w_cursor = old_cursor;
2222 }
2223#ifdef FEAT_VISUAL
2224 oap->block_mode = FALSE;
2225#endif
2226 clearop(oap);
2227 }
2228}
2229
2230/*
2231 * Handle indent and format operators and visual mode ":".
2232 */
2233 static void
2234op_colon(oap)
2235 oparg_T *oap;
2236{
2237 stuffcharReadbuff(':');
2238#ifdef FEAT_VISUAL
2239 if (oap->is_VIsual)
2240 stuffReadbuff((char_u *)"'<,'>");
2241 else
2242#endif
2243 {
2244 /*
2245 * Make the range look nice, so it can be repeated.
2246 */
2247 if (oap->start.lnum == curwin->w_cursor.lnum)
2248 stuffcharReadbuff('.');
2249 else
2250 stuffnumReadbuff((long)oap->start.lnum);
2251 if (oap->end.lnum != oap->start.lnum)
2252 {
2253 stuffcharReadbuff(',');
2254 if (oap->end.lnum == curwin->w_cursor.lnum)
2255 stuffcharReadbuff('.');
2256 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2257 stuffcharReadbuff('$');
2258 else if (oap->start.lnum == curwin->w_cursor.lnum)
2259 {
2260 stuffReadbuff((char_u *)".+");
2261 stuffnumReadbuff((long)oap->line_count - 1);
2262 }
2263 else
2264 stuffnumReadbuff((long)oap->end.lnum);
2265 }
2266 }
2267 if (oap->op_type != OP_COLON)
2268 stuffReadbuff((char_u *)"!");
2269 if (oap->op_type == OP_INDENT)
2270 {
2271#ifndef FEAT_CINDENT
2272 if (*get_equalprg() == NUL)
2273 stuffReadbuff((char_u *)"indent");
2274 else
2275#endif
2276 stuffReadbuff(get_equalprg());
2277 stuffReadbuff((char_u *)"\n");
2278 }
2279 else if (oap->op_type == OP_FORMAT)
2280 {
2281 if (*p_fp == NUL)
2282 stuffReadbuff((char_u *)"fmt");
2283 else
2284 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002285 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 /*
2289 * do_cmdline() does the rest
2290 */
2291}
2292
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002293/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002294 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002295 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002296 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002297op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002298 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002299{
2300#ifdef FEAT_EVAL
2301 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002302# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002303 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002304# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002305
2306 if (*p_opfunc == NUL)
2307 EMSG(_("E774: 'operatorfunc' is empty"));
2308 else
2309 {
2310 /* Set '[ and '] marks to text to be operated on. */
2311 curbuf->b_op_start = oap->start;
2312 curbuf->b_op_end = oap->end;
2313 if (oap->motion_type != MLINE && !oap->inclusive)
2314 /* Exclude the end position. */
2315 decl(&curbuf->b_op_end);
2316
2317 if (oap->block_mode)
2318 argv[0] = (char_u *)"block";
2319 else if (oap->motion_type == MLINE)
2320 argv[0] = (char_u *)"line";
2321 else
2322 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002323
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002324# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002325 /* Reset virtual_op so that 'virtualedit' can be changed in the
2326 * function. */
2327 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002328# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002329
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002330 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002331
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002332# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002333 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002334# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002335 }
2336#else
2337 EMSG(_("E775: Eval feature not available"));
2338#endif
2339}
2340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341#if defined(FEAT_MOUSE) || defined(PROTO)
2342/*
2343 * Do the appropriate action for the current mouse click in the current mode.
2344 * Not used for Command-line mode.
2345 *
2346 * Normal Mode:
2347 * event modi- position visual change action
2348 * fier cursor window
2349 * left press - yes end yes
2350 * left press C yes end yes "^]" (2)
2351 * left press S yes end yes "*" (2)
2352 * left drag - yes start if moved no
2353 * left relse - yes start if moved no
2354 * middle press - yes if not active no put register
2355 * middle press - yes if active no yank and put
2356 * right press - yes start or extend yes
2357 * right press S yes no change yes "#" (2)
2358 * right drag - yes extend no
2359 * right relse - yes extend no
2360 *
2361 * Insert or Replace Mode:
2362 * event modi- position visual change action
2363 * fier cursor window
2364 * left press - yes (cannot be active) yes
2365 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2366 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2367 * left drag - yes start or extend (1) no CTRL-O (1)
2368 * left relse - yes start or extend (1) no CTRL-O (1)
2369 * middle press - no (cannot be active) no put register
2370 * right press - yes start or extend yes CTRL-O
2371 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2372 *
2373 * (1) only if mouse pointer moved since press
2374 * (2) only if click is in same buffer
2375 *
2376 * Return TRUE if start_arrow() should be called for edit mode.
2377 */
2378 int
2379do_mouse(oap, c, dir, count, fixindent)
2380 oparg_T *oap; /* operator argument, can be NULL */
2381 int c; /* K_LEFTMOUSE, etc */
2382 int dir; /* Direction to 'put' if necessary */
2383 long count;
2384 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2385{
2386 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2387 static int got_click = FALSE; /* got a click some time back */
2388
2389 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2390 int is_click; /* If FALSE it's a drag or release event */
2391 int is_drag; /* If TRUE it's a drag event */
2392 int jump_flags = 0; /* flags for jump_to_mouse() */
2393 pos_T start_visual;
2394 int moved; /* Has cursor moved? */
2395 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002396#ifdef FEAT_WINDOWS
2397 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#ifdef FEAT_VERTSPLIT
2400 int in_sep_line; /* mouse in vertical separator line */
2401#endif
2402 int c1, c2;
2403#if defined(FEAT_FOLDING)
2404 pos_T save_cursor;
2405#endif
2406 win_T *old_curwin = curwin;
2407#ifdef FEAT_VISUAL
2408 static pos_T orig_cursor;
2409 colnr_T leftcol, rightcol;
2410 pos_T end_visual;
2411 int diff;
2412 int old_active = VIsual_active;
2413 int old_mode = VIsual_mode;
2414#endif
2415 int regname;
2416
2417#if defined(FEAT_FOLDING)
2418 save_cursor = curwin->w_cursor;
2419#endif
2420
2421 /*
2422 * When GUI is active, always recognize mouse events, otherwise:
2423 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2424 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2425 * - For command line and insert mode 'mouse' is checked before calling
2426 * do_mouse().
2427 */
2428 if (do_always)
2429 do_always = FALSE;
2430 else
2431#ifdef FEAT_GUI
2432 if (!gui.in_use)
2433#endif
2434 {
2435#ifdef FEAT_VISUAL
2436 if (VIsual_active)
2437 {
2438 if (!mouse_has(MOUSE_VISUAL))
2439 return FALSE;
2440 }
2441 else
2442#endif
2443 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2444 return FALSE;
2445 }
2446
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002447 for (;;)
2448 {
2449 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2450 if (is_drag)
2451 {
2452 /* If the next character is the same mouse event then use that
2453 * one. Speeds up dragging the status line. */
2454 if (vpeekc() != NUL)
2455 {
2456 int nc;
2457 int save_mouse_row = mouse_row;
2458 int save_mouse_col = mouse_col;
2459
2460 /* Need to get the character, peeking doesn't get the actual
2461 * one. */
2462 nc = safe_vgetc();
2463 if (c == nc)
2464 continue;
2465 vungetc(nc);
2466 mouse_row = save_mouse_row;
2467 mouse_col = save_mouse_col;
2468 }
2469 }
2470 break;
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473#ifdef FEAT_MOUSESHAPE
2474 /* May have stopped dragging the status or separator line. The pointer is
2475 * most likely still on the status or separator line. */
2476 if (!is_drag && drag_status_line)
2477 {
2478 drag_status_line = FALSE;
2479 update_mouseshape(SHAPE_IDX_STATUS);
2480 }
2481# ifdef FEAT_VERTSPLIT
2482 if (!is_drag && drag_sep_line)
2483 {
2484 drag_sep_line = FALSE;
2485 update_mouseshape(SHAPE_IDX_VSEP);
2486 }
2487# endif
2488#endif
2489
2490 /*
2491 * Ignore drag and release events if we didn't get a click.
2492 */
2493 if (is_click)
2494 got_click = TRUE;
2495 else
2496 {
2497 if (!got_click) /* didn't get click, ignore */
2498 return FALSE;
2499 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002502#ifdef FEAT_WINDOWS
2503 if (in_tab_line)
2504 {
2505 in_tab_line = FALSE;
2506 return FALSE;
2507 }
2508#endif
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 }
2511
Bram Moolenaar64969662005-12-14 21:59:55 +00002512#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002514 * ALT is only used for starting/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
2516 if ((mod_mask & MOD_MASK_ALT))
2517 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
2520 /*
2521 * CTRL right mouse button does CTRL-T
2522 */
2523 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2524 {
2525 if (State & INSERT)
2526 stuffcharReadbuff(Ctrl_O);
2527 if (count > 1)
2528 stuffnumReadbuff(count);
2529 stuffcharReadbuff(Ctrl_T);
2530 got_click = FALSE; /* ignore drag&release now */
2531 return FALSE;
2532 }
2533
2534 /*
2535 * CTRL only works with left mouse button
2536 */
2537 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2538 return FALSE;
2539
2540 /*
2541 * When a modifier is down, ignore drag and release events, as well as
2542 * multiple clicks and the middle mouse button.
2543 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2544 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002545 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2546 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 && (!is_click
2548 || (mod_mask & MOD_MASK_MULTI_CLICK)
2549 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002550 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 && mouse_model_popup()
2552 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002553 && !((mod_mask & MOD_MASK_ALT)
2554 && !mouse_model_popup()
2555 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 )
2557 return FALSE;
2558
2559 /*
2560 * If the button press was used as the movement command for an operator
2561 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2562 * drag/release events.
2563 */
2564 if (!is_click && which_button == MOUSE_MIDDLE)
2565 return FALSE;
2566
2567 if (oap != NULL)
2568 regname = oap->regname;
2569 else
2570 regname = 0;
2571
2572 /*
2573 * Middle mouse button does a 'put' of the selected text
2574 */
2575 if (which_button == MOUSE_MIDDLE)
2576 {
2577 if (State == NORMAL)
2578 {
2579 /*
2580 * If an operator was pending, we don't know what the user wanted
2581 * to do. Go back to normal mode: Clear the operator and beep().
2582 */
2583 if (oap != NULL && oap->op_type != OP_NOP)
2584 {
2585 clearopbeep(oap);
2586 return FALSE;
2587 }
2588
2589#ifdef FEAT_VISUAL
2590 /*
2591 * If visual was active, yank the highlighted text and put it
2592 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002593 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 */
2595 if (VIsual_active)
2596 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002597 if (VIsual_select)
2598 {
2599 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002600 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002601 }
2602 else
2603 {
2604 stuffcharReadbuff('y');
2605 stuffcharReadbuff(K_MIDDLEMOUSE);
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 do_always = TRUE; /* ignore 'mouse' setting next time */
2608 return FALSE;
2609 }
2610#endif
2611 /*
2612 * The rest is below jump_to_mouse()
2613 */
2614 }
2615
2616 else if ((State & INSERT) == 0)
2617 return FALSE;
2618
2619 /*
2620 * Middle click in insert mode doesn't move the mouse, just insert the
2621 * contents of a register. '.' register is special, can't insert that
2622 * with do_put().
2623 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2624 * happens for the GUI).
2625 */
2626 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2627 {
2628 if (regname == '.')
2629 insert_reg(regname, TRUE);
2630 else
2631 {
2632#ifdef FEAT_CLIPBOARD
2633 if (clip_star.available && regname == 0)
2634 regname = '*';
2635#endif
2636 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2637 insert_reg(regname, TRUE);
2638 else
2639 {
2640 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2641
2642 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2643 AppendCharToRedobuff(Ctrl_R);
2644 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2645 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2646 }
2647 }
2648 return FALSE;
2649 }
2650 }
2651
2652 /* When dragging or button-up stay in the same window. */
2653 if (!is_click)
2654 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2655
2656 start_visual.lnum = 0;
2657
Bram Moolenaarf740b292006-02-16 22:11:02 +00002658#ifdef FEAT_WINDOWS
2659 /* Check for clicking in the tab page line. */
2660 if (mouse_row == 0 && firstwin->w_winrow > 0)
2661 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002662 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002663 {
2664 if (in_tab_line)
2665 {
2666 c1 = TabPageIdxs[mouse_col];
2667 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2668 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002669 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002670 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002671
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672 /* click in a tab selects that tab page */
2673 if (is_click
2674# ifdef FEAT_CMDWIN
2675 && cmdwin_type == 0
2676# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002677 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002679 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002680 c1 = TabPageIdxs[mouse_col];
2681 if (c1 >= 0)
2682 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002683 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2684 {
2685 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002686 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002687 tabpage_new();
2688 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2689 }
2690 else
2691 {
2692 /* Go to specified tab page, or next one if not clicking
2693 * on a label. */
2694 goto_tabpage(c1);
2695
2696 /* It's like clicking on the status line of a window. */
2697 if (curwin != old_curwin)
2698 end_visual_mode();
2699 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002700 }
2701 else if (c1 < 0)
2702 {
2703 tabpage_T *tp;
2704
2705 /* Close the current or specified tab page. */
2706 if (c1 == -999)
2707 tp = curtab;
2708 else
2709 tp = find_tabpage(-c1);
2710 if (tp == curtab)
2711 {
2712 if (first_tabpage->tp_next != NULL)
2713 tabpage_close(FALSE);
2714 }
2715 else if (tp != NULL)
2716 tabpage_close_other(tp, FALSE);
2717 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002718 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002719 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002720 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002721 else if (is_drag && in_tab_line)
2722 {
2723 c1 = TabPageIdxs[mouse_col];
2724 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2725 return FALSE;
2726 }
2727
Bram Moolenaarf740b292006-02-16 22:11:02 +00002728#endif
2729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /*
2731 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2732 * right button up -> pop-up menu
2733 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002734 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 */
2736 if (mouse_model_popup())
2737 {
2738 if (which_button == MOUSE_RIGHT
2739 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2740 {
2741 /*
2742 * NOTE: Ignore right button down and drag mouse events.
2743 * Windows only shows the popup menu on the button up event.
2744 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002745#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2746 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (!is_click)
2748 return FALSE;
2749#endif
2750#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2751 if (is_click || is_drag)
2752 return FALSE;
2753#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002754#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2756 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2757 if (gui.in_use)
2758 {
2759 jump_flags = 0;
2760 if (STRCMP(p_mousem, "popup_setpos") == 0)
2761 {
2762 /* First set the cursor position before showing the popup
2763 * menu. */
2764#ifdef FEAT_VISUAL
2765 if (VIsual_active)
2766 {
2767 pos_T m_pos;
2768
2769 /*
2770 * set MOUSE_MAY_STOP_VIS if we are outside the
2771 * selection or the current window (might have false
2772 * negative here)
2773 */
2774 if (mouse_row < W_WINROW(curwin)
2775 || mouse_row
2776 > (W_WINROW(curwin) + curwin->w_height))
2777 jump_flags = MOUSE_MAY_STOP_VIS;
2778 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2779 jump_flags = MOUSE_MAY_STOP_VIS;
2780 else
2781 {
2782 if ((lt(curwin->w_cursor, VIsual)
2783 && (lt(m_pos, curwin->w_cursor)
2784 || lt(VIsual, m_pos)))
2785 || (lt(VIsual, curwin->w_cursor)
2786 && (lt(m_pos, VIsual)
2787 || lt(curwin->w_cursor, m_pos))))
2788 {
2789 jump_flags = MOUSE_MAY_STOP_VIS;
2790 }
2791 else if (VIsual_mode == Ctrl_V)
2792 {
2793 getvcols(curwin, &curwin->w_cursor, &VIsual,
2794 &leftcol, &rightcol);
2795 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2796 if (m_pos.col < leftcol || m_pos.col > rightcol)
2797 jump_flags = MOUSE_MAY_STOP_VIS;
2798 }
2799 }
2800 }
2801 else
2802 jump_flags = MOUSE_MAY_STOP_VIS;
2803#endif
2804 }
2805 if (jump_flags)
2806 {
2807 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2808 update_curbuf(
2809#ifdef FEAT_VISUAL
2810 VIsual_active ? INVERTED :
2811#endif
2812 VALID);
2813 setcursor();
2814 out_flush(); /* Update before showing popup menu */
2815 }
2816# ifdef FEAT_MENU
2817 gui_show_popupmenu();
2818# endif
2819 return (jump_flags & CURSOR_MOVED) != 0;
2820 }
2821 else
2822 return FALSE;
2823#else
2824 return FALSE;
2825#endif
2826 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002827 if (which_button == MOUSE_LEFT
2828 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
2830 which_button = MOUSE_RIGHT;
2831 mod_mask &= ~MOD_MASK_SHIFT;
2832 }
2833 }
2834
2835#ifdef FEAT_VISUAL
2836 if ((State & (NORMAL | INSERT))
2837 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2838 {
2839 if (which_button == MOUSE_LEFT)
2840 {
2841 if (is_click)
2842 {
2843 /* stop Visual mode for a left click in a window, but not when
2844 * on a status line */
2845 if (VIsual_active)
2846 jump_flags |= MOUSE_MAY_STOP_VIS;
2847 }
2848 else if (mouse_has(MOUSE_VISUAL))
2849 jump_flags |= MOUSE_MAY_VIS;
2850 }
2851 else if (which_button == MOUSE_RIGHT)
2852 {
2853 if (is_click && VIsual_active)
2854 {
2855 /*
2856 * Remember the start and end of visual before moving the
2857 * cursor.
2858 */
2859 if (lt(curwin->w_cursor, VIsual))
2860 {
2861 start_visual = curwin->w_cursor;
2862 end_visual = VIsual;
2863 }
2864 else
2865 {
2866 start_visual = VIsual;
2867 end_visual = curwin->w_cursor;
2868 }
2869 }
2870 jump_flags |= MOUSE_FOCUS;
2871 if (mouse_has(MOUSE_VISUAL))
2872 jump_flags |= MOUSE_MAY_VIS;
2873 }
2874 }
2875#endif
2876
2877 /*
2878 * If an operator is pending, ignore all drags and releases until the
2879 * next mouse click.
2880 */
2881 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2882 {
2883 got_click = FALSE;
2884 oap->motion_type = MCHAR;
2885 }
2886
2887 /* When releasing the button let jump_to_mouse() know. */
2888 if (!is_click && !is_drag)
2889 jump_flags |= MOUSE_RELEASED;
2890
2891 /*
2892 * JUMP!
2893 */
2894 jump_flags = jump_to_mouse(jump_flags,
2895 oap == NULL ? NULL : &(oap->inclusive), which_button);
2896 moved = (jump_flags & CURSOR_MOVED);
2897 in_status_line = (jump_flags & IN_STATUS_LINE);
2898#ifdef FEAT_VERTSPLIT
2899 in_sep_line = (jump_flags & IN_SEP_LINE);
2900#endif
2901
2902#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002903 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2905 {
2906 int key = KEY2TERMCAP1(c);
2907
2908 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2909 || key == (int)KE_RIGHTRELEASE)
2910 netbeans_button_release(which_button);
2911 }
2912#endif
2913
2914 /* When jumping to another window, clear a pending operator. That's a bit
2915 * friendlier than beeping and not jumping to that window. */
2916 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2917 clearop(oap);
2918
2919#ifdef FEAT_FOLDING
2920 if (mod_mask == 0
2921 && !is_drag
2922 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2923 && which_button == MOUSE_LEFT)
2924 {
2925 /* open or close a fold at this line */
2926 if (jump_flags & MOUSE_FOLD_OPEN)
2927 openFold(curwin->w_cursor.lnum, 1L);
2928 else
2929 closeFold(curwin->w_cursor.lnum, 1L);
2930 /* don't move the cursor if still in the same window */
2931 if (curwin == old_curwin)
2932 curwin->w_cursor = save_cursor;
2933 }
2934#endif
2935
2936#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2937 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2938 {
2939 clip_modeless(which_button, is_click, is_drag);
2940 return FALSE;
2941 }
2942#endif
2943
2944#ifdef FEAT_VISUAL
2945 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002946 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (VIsual_active && is_drag && p_so)
2948 {
2949 /* In the very first line, allow scrolling one line */
2950 if (mouse_row == 0)
2951 mouse_dragging = 2;
2952 else
2953 mouse_dragging = 1;
2954 }
2955
2956 /* When dragging the mouse above the window, scroll down. */
2957 if (is_drag && mouse_row < 0 && !in_status_line)
2958 {
2959 scroll_redraw(FALSE, 1L);
2960 mouse_row = 0;
2961 }
2962
2963 if (start_visual.lnum) /* right click in visual mode */
2964 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002965 /* When ALT is pressed make Visual mode blockwise. */
2966 if (mod_mask & MOD_MASK_ALT)
2967 VIsual_mode = Ctrl_V;
2968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /*
2970 * In Visual-block mode, divide the area in four, pick up the corner
2971 * that is in the quarter that the cursor is in.
2972 */
2973 if (VIsual_mode == Ctrl_V)
2974 {
2975 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2976 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2977 end_visual.col = leftcol;
2978 else
2979 end_visual.col = rightcol;
2980 if (curwin->w_cursor.lnum <
2981 (start_visual.lnum + end_visual.lnum) / 2)
2982 end_visual.lnum = end_visual.lnum;
2983 else
2984 end_visual.lnum = start_visual.lnum;
2985
2986 /* move VIsual to the right column */
2987 start_visual = curwin->w_cursor; /* save the cursor pos */
2988 curwin->w_cursor = end_visual;
2989 coladvance(end_visual.col);
2990 VIsual = curwin->w_cursor;
2991 curwin->w_cursor = start_visual; /* restore the cursor */
2992 }
2993 else
2994 {
2995 /*
2996 * If the click is before the start of visual, change the start.
2997 * If the click is after the end of visual, change the end. If
2998 * the click is inside the visual, change the closest side.
2999 */
3000 if (lt(curwin->w_cursor, start_visual))
3001 VIsual = end_visual;
3002 else if (lt(end_visual, curwin->w_cursor))
3003 VIsual = start_visual;
3004 else
3005 {
3006 /* In the same line, compare column number */
3007 if (end_visual.lnum == start_visual.lnum)
3008 {
3009 if (curwin->w_cursor.col - start_visual.col >
3010 end_visual.col - curwin->w_cursor.col)
3011 VIsual = start_visual;
3012 else
3013 VIsual = end_visual;
3014 }
3015
3016 /* In different lines, compare line number */
3017 else
3018 {
3019 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
3020 (end_visual.lnum - curwin->w_cursor.lnum);
3021
3022 if (diff > 0) /* closest to end */
3023 VIsual = start_visual;
3024 else if (diff < 0) /* closest to start */
3025 VIsual = end_visual;
3026 else /* in the middle line */
3027 {
3028 if (curwin->w_cursor.col <
3029 (start_visual.col + end_visual.col) / 2)
3030 VIsual = end_visual;
3031 else
3032 VIsual = start_visual;
3033 }
3034 }
3035 }
3036 }
3037 }
3038 /*
3039 * If Visual mode started in insert mode, execute "CTRL-O"
3040 */
3041 else if ((State & INSERT) && VIsual_active)
3042 stuffcharReadbuff(Ctrl_O);
3043#endif
3044
3045 /*
3046 * Middle mouse click: Put text before cursor.
3047 */
3048 if (which_button == MOUSE_MIDDLE)
3049 {
3050#ifdef FEAT_CLIPBOARD
3051 if (clip_star.available && regname == 0)
3052 regname = '*';
3053#endif
3054 if (yank_register_mline(regname))
3055 {
3056 if (mouse_past_bottom)
3057 dir = FORWARD;
3058 }
3059 else if (mouse_past_eol)
3060 dir = FORWARD;
3061
3062 if (fixindent)
3063 {
3064 c1 = (dir == BACKWARD) ? '[' : ']';
3065 c2 = 'p';
3066 }
3067 else
3068 {
3069 c1 = (dir == FORWARD) ? 'p' : 'P';
3070 c2 = NUL;
3071 }
3072 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3073
3074 /*
3075 * Remember where the paste started, so in edit() Insstart can be set
3076 * to this position
3077 */
3078 if (restart_edit != 0)
3079 where_paste_started = curwin->w_cursor;
3080 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3081 }
3082
3083#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3084 /*
3085 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3086 * error under the mouse pointer.
3087 */
3088 else if (((mod_mask & MOD_MASK_CTRL)
3089 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3090 && bt_quickfix(curbuf))
3091 {
3092 if (State & INSERT)
3093 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003094 if (curwin->w_llist_ref == NULL) /* quickfix window */
3095 stuffReadbuff((char_u *)":.cc\n");
3096 else /* location list window */
3097 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 got_click = FALSE; /* ignore drag&release now */
3099 }
3100#endif
3101
3102 /*
3103 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3104 * under the mouse pointer.
3105 */
3106 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3107 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3108 {
3109 if (State & INSERT)
3110 stuffcharReadbuff(Ctrl_O);
3111 stuffcharReadbuff(Ctrl_RSB);
3112 got_click = FALSE; /* ignore drag&release now */
3113 }
3114
3115 /*
3116 * Shift-Mouse click searches for the next occurrence of the word under
3117 * the mouse pointer
3118 */
3119 else if ((mod_mask & MOD_MASK_SHIFT))
3120 {
3121 if (State & INSERT
3122#ifdef FEAT_VISUAL
3123 || (VIsual_active && VIsual_select)
3124#endif
3125 )
3126 stuffcharReadbuff(Ctrl_O);
3127 if (which_button == MOUSE_LEFT)
3128 stuffcharReadbuff('*');
3129 else /* MOUSE_RIGHT */
3130 stuffcharReadbuff('#');
3131 }
3132
3133 /* Handle double clicks, unless on status line */
3134 else if (in_status_line)
3135 {
3136#ifdef FEAT_MOUSESHAPE
3137 if ((is_drag || is_click) && !drag_status_line)
3138 {
3139 drag_status_line = TRUE;
3140 update_mouseshape(-1);
3141 }
3142#endif
3143 }
3144#ifdef FEAT_VERTSPLIT
3145 else if (in_sep_line)
3146 {
3147# ifdef FEAT_MOUSESHAPE
3148 if ((is_drag || is_click) && !drag_sep_line)
3149 {
3150 drag_sep_line = TRUE;
3151 update_mouseshape(-1);
3152 }
3153# endif
3154 }
3155#endif
3156#ifdef FEAT_VISUAL
3157 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3158 && mouse_has(MOUSE_VISUAL))
3159 {
3160 if (is_click || !VIsual_active)
3161 {
3162 if (VIsual_active)
3163 orig_cursor = VIsual;
3164 else
3165 {
3166 check_visual_highlight();
3167 VIsual = curwin->w_cursor;
3168 orig_cursor = VIsual;
3169 VIsual_active = TRUE;
3170 VIsual_reselect = TRUE;
3171 /* start Select mode if 'selectmode' contains "mouse" */
3172 may_start_select('o');
3173 setmouse();
3174 }
3175 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003176 {
3177 /* Double click with ALT pressed makes it blockwise. */
3178 if (mod_mask & MOD_MASK_ALT)
3179 VIsual_mode = Ctrl_V;
3180 else
3181 VIsual_mode = 'v';
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3184 VIsual_mode = 'V';
3185 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3186 VIsual_mode = Ctrl_V;
3187#ifdef FEAT_CLIPBOARD
3188 /* Make sure the clipboard gets updated. Needed because start and
3189 * end may still be the same, and the selection needs to be owned */
3190 clip_star.vmode = NUL;
3191#endif
3192 }
3193 /*
3194 * A double click selects a word or a block.
3195 */
3196 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3197 {
3198 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003199 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 if (is_click)
3202 {
3203 /* If the character under the cursor (skipping white space) is
3204 * not a word character, try finding a match and select a (),
3205 * {}, [], #if/#endif, etc. block. */
3206 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003207 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 inc(&end_visual);
3209 if (oap != NULL)
3210 oap->motion_type = MCHAR;
3211 if (oap != NULL
3212 && VIsual_mode == 'v'
3213 && !vim_iswordc(gchar_pos(&end_visual))
3214 && equalpos(curwin->w_cursor, VIsual)
3215 && (pos = findmatch(oap, NUL)) != NULL)
3216 {
3217 curwin->w_cursor = *pos;
3218 if (oap->motion_type == MLINE)
3219 VIsual_mode = 'V';
3220 else if (*p_sel == 'e')
3221 {
3222 if (lt(curwin->w_cursor, VIsual))
3223 ++VIsual.col;
3224 else
3225 ++curwin->w_cursor.col;
3226 }
3227 }
3228 }
3229
3230 if (pos == NULL && (is_click || is_drag))
3231 {
3232 /* When not found a match or when dragging: extend to include
3233 * a word. */
3234 if (lt(curwin->w_cursor, orig_cursor))
3235 {
3236 find_start_of_word(&curwin->w_cursor);
3237 find_end_of_word(&VIsual);
3238 }
3239 else
3240 {
3241 find_start_of_word(&VIsual);
3242 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3243#ifdef FEAT_MBYTE
3244 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003245 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246#else
3247 ++curwin->w_cursor.col;
3248#endif
3249 find_end_of_word(&curwin->w_cursor);
3250 }
3251 }
3252 curwin->w_set_curswant = TRUE;
3253 }
3254 if (is_click)
3255 redraw_curbuf_later(INVERTED); /* update the inversion */
3256 }
3257 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003258 {
3259 if (mod_mask & MOD_MASK_ALT)
3260 VIsual_mode = Ctrl_V;
3261 else
3262 VIsual_mode = 'v';
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003266 if ((!VIsual_active && old_active && mode_displayed)
3267 || (VIsual_active && p_smd && msg_silent == 0
3268 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 redraw_cmdline = TRUE;
3270#endif
3271
3272 return moved;
3273}
3274
3275#ifdef FEAT_VISUAL
3276/*
3277 * Move "pos" back to the start of the word it's in.
3278 */
3279 static void
3280find_start_of_word(pos)
3281 pos_T *pos;
3282{
3283 char_u *line;
3284 int cclass;
3285 int col;
3286
3287 line = ml_get(pos->lnum);
3288 cclass = get_mouse_class(line + pos->col);
3289
3290 while (pos->col > 0)
3291 {
3292 col = pos->col - 1;
3293#ifdef FEAT_MBYTE
3294 col -= (*mb_head_off)(line, line + col);
3295#endif
3296 if (get_mouse_class(line + col) != cclass)
3297 break;
3298 pos->col = col;
3299 }
3300}
3301
3302/*
3303 * Move "pos" forward to the end of the word it's in.
3304 * When 'selection' is "exclusive", the position is just after the word.
3305 */
3306 static void
3307find_end_of_word(pos)
3308 pos_T *pos;
3309{
3310 char_u *line;
3311 int cclass;
3312 int col;
3313
3314 line = ml_get(pos->lnum);
3315 if (*p_sel == 'e' && pos->col > 0)
3316 {
3317 --pos->col;
3318#ifdef FEAT_MBYTE
3319 pos->col -= (*mb_head_off)(line, line + pos->col);
3320#endif
3321 }
3322 cclass = get_mouse_class(line + pos->col);
3323 while (line[pos->col] != NUL)
3324 {
3325#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003326 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#else
3328 col = pos->col + 1;
3329#endif
3330 if (get_mouse_class(line + col) != cclass)
3331 {
3332 if (*p_sel == 'e')
3333 pos->col = col;
3334 break;
3335 }
3336 pos->col = col;
3337 }
3338}
3339
3340/*
3341 * Get class of a character for selection: same class means same word.
3342 * 0: blank
3343 * 1: punctuation groups
3344 * 2: normal word character
3345 * >2: multi-byte word character.
3346 */
3347 static int
3348get_mouse_class(p)
3349 char_u *p;
3350{
3351 int c;
3352
3353#ifdef FEAT_MBYTE
3354 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3355 return mb_get_class(p);
3356#endif
3357
3358 c = *p;
3359 if (c == ' ' || c == '\t')
3360 return 0;
3361
3362 if (vim_iswordc(c))
3363 return 2;
3364
3365 /*
3366 * There are a few special cases where we want certain combinations of
3367 * characters to be considered as a single word. These are things like
3368 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003369 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 */
3371 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3372 return 1;
3373 return c;
3374}
3375#endif /* FEAT_VISUAL */
3376#endif /* FEAT_MOUSE */
3377
3378#if defined(FEAT_VISUAL) || defined(PROTO)
3379/*
3380 * Check if highlighting for visual mode is possible, give a warning message
3381 * if not.
3382 */
3383 void
3384check_visual_highlight()
3385{
3386 static int did_check = FALSE;
3387
3388 if (full_screen)
3389 {
3390 if (!did_check && hl_attr(HLF_V) == 0)
3391 MSG(_("Warning: terminal cannot highlight"));
3392 did_check = TRUE;
3393 }
3394}
3395
3396/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003397 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 * This function should ALWAYS be called to end Visual mode, except from
3399 * do_pending_operator().
3400 */
3401 void
3402end_visual_mode()
3403{
3404#ifdef FEAT_CLIPBOARD
3405 /*
3406 * If we are using the clipboard, then remember what was selected in case
3407 * we need to paste it somewhere while we still own the selection.
3408 * Only do this when the clipboard is already owned. Don't want to grab
3409 * the selection when hitting ESC.
3410 */
3411 if (clip_star.available && clip_star.owned)
3412 clip_auto_select();
3413#endif
3414
3415 VIsual_active = FALSE;
3416#ifdef FEAT_MOUSE
3417 setmouse();
3418 mouse_dragging = 0;
3419#endif
3420
3421 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003422 curbuf->b_visual.vi_mode = VIsual_mode;
3423 curbuf->b_visual.vi_start = VIsual;
3424 curbuf->b_visual.vi_end = curwin->w_cursor;
3425 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#ifdef FEAT_EVAL
3427 curbuf->b_visual_mode_eval = VIsual_mode;
3428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#ifdef FEAT_VIRTUALEDIT
3430 if (!virtual_active())
3431 curwin->w_cursor.coladd = 0;
3432#endif
3433
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003434 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 clear_cmdline = TRUE; /* unshow visual mode later */
3436#ifdef FEAT_CMDL_INFO
3437 else
3438 clear_showcmd();
3439#endif
3440
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003441 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442}
3443
3444/*
3445 * Reset VIsual_active and VIsual_reselect.
3446 */
3447 void
3448reset_VIsual_and_resel()
3449{
3450 if (VIsual_active)
3451 {
3452 end_visual_mode();
3453 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3454 }
3455 VIsual_reselect = FALSE;
3456}
3457
3458/*
3459 * Reset VIsual_active and VIsual_reselect if it's set.
3460 */
3461 void
3462reset_VIsual()
3463{
3464 if (VIsual_active)
3465 {
3466 end_visual_mode();
3467 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3468 VIsual_reselect = FALSE;
3469 }
3470}
3471#endif /* FEAT_VISUAL */
3472
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3475
3476/*
3477 * Check for a balloon-eval special item to include when searching for an
3478 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3479 * Returns TRUE if the character at "*ptr" should be included.
3480 * "dir" is FORWARD or BACKWARD, the direction of searching.
3481 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3482 * "bnp" points to a counter for square brackets.
3483 */
3484 static int
3485find_is_eval_item(ptr, colp, bnp, dir)
3486 char_u *ptr;
3487 int *colp;
3488 int *bnp;
3489 int dir;
3490{
3491 /* Accept everything inside []. */
3492 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3493 ++*bnp;
3494 if (*bnp > 0)
3495 {
3496 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3497 --*bnp;
3498 return TRUE;
3499 }
3500
3501 /* skip over "s.var" */
3502 if (*ptr == '.')
3503 return TRUE;
3504
3505 /* two-character item: s->var */
3506 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3507 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3508 {
3509 *colp += dir;
3510 return TRUE;
3511 }
3512 return FALSE;
3513}
3514#endif
3515
3516/*
3517 * Find the identifier under or to the right of the cursor.
3518 * "find_type" can have one of three values:
3519 * FIND_IDENT: find an identifier (keyword)
3520 * FIND_STRING: find any non-white string
3521 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 *
3524 * There are three steps:
3525 * 1. Search forward for the start of an identifier/string. Doesn't move if
3526 * already on one.
3527 * 2. Search backward for the start of this identifier/string.
3528 * This doesn't match the real Vi but I like it a little better and it
3529 * shouldn't bother anyone.
3530 * 3. Search forward to the end of this identifier/string.
3531 * When FIND_IDENT isn't defined, we backup until a blank.
3532 *
3533 * Returns the length of the string, or zero if no string is found.
3534 * If a string is found, a pointer to the string is put in "*string". This
3535 * string is not always NUL terminated.
3536 */
3537 int
3538find_ident_under_cursor(string, find_type)
3539 char_u **string;
3540 int find_type;
3541{
3542 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3543 curwin->w_cursor.col, string, find_type);
3544}
3545
3546/*
3547 * Like find_ident_under_cursor(), but for any window and any position.
3548 * However: Uses 'iskeyword' from the current window!.
3549 */
3550 int
3551find_ident_at_pos(wp, lnum, startcol, string, find_type)
3552 win_T *wp;
3553 linenr_T lnum;
3554 colnr_T startcol;
3555 char_u **string;
3556 int find_type;
3557{
3558 char_u *ptr;
3559 int col = 0; /* init to shut up GCC */
3560 int i;
3561#ifdef FEAT_MBYTE
3562 int this_class = 0;
3563 int prev_class;
3564 int prevcol;
3565#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003566#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int bn = 0; /* bracket nesting */
3568#endif
3569
3570 /*
3571 * if i == 0: try to find an identifier
3572 * if i == 1: try to find any non-white string
3573 */
3574 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3575 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3576 {
3577 /*
3578 * 1. skip to start of identifier/string
3579 */
3580 col = startcol;
3581#ifdef FEAT_MBYTE
3582 if (has_mbyte)
3583 {
3584 while (ptr[col] != NUL)
3585 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003586# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 /* Stop at a ']' to evaluate "a[x]". */
3588 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3589 break;
3590# endif
3591 this_class = mb_get_class(ptr + col);
3592 if (this_class != 0 && (i == 1 || this_class != 1))
3593 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003594 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 }
3597 else
3598#endif
3599 while (ptr[col] != NUL
3600 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003601# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3603# endif
3604 )
3605 ++col;
3606
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003607#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 /* When starting on a ']' count it, so that we include the '['. */
3609 bn = ptr[col] == ']';
3610#endif
3611
3612 /*
3613 * 2. Back up to start of identifier/string.
3614 */
3615#ifdef FEAT_MBYTE
3616 if (has_mbyte)
3617 {
3618 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003619# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3621 this_class = mb_get_class((char_u *)"a");
3622 else
3623# endif
3624 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003625 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
3627 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3628 prev_class = mb_get_class(ptr + prevcol);
3629 if (this_class != prev_class
3630 && (i == 0
3631 || prev_class == 0
3632 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003633# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 && (!(find_type & FIND_EVAL)
3635 || prevcol == 0
3636 || !find_is_eval_item(ptr + prevcol, &prevcol,
3637 &bn, BACKWARD))
3638# endif
3639 )
3640 break;
3641 col = prevcol;
3642 }
3643
3644 /* If we don't want just any old string, or we've found an
3645 * identifier, stop searching. */
3646 if (this_class > 2)
3647 this_class = 2;
3648 if (!(find_type & FIND_STRING) || this_class == 2)
3649 break;
3650 }
3651 else
3652#endif
3653 {
3654 while (col > 0
3655 && ((i == 0
3656 ? vim_iswordc(ptr[col - 1])
3657 : (!vim_iswhite(ptr[col - 1])
3658 && (!(find_type & FIND_IDENT)
3659 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003660#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || ((find_type & FIND_EVAL)
3662 && col > 1
3663 && find_is_eval_item(ptr + col - 1, &col,
3664 &bn, BACKWARD))
3665#endif
3666 ))
3667 --col;
3668
3669 /* If we don't want just any old string, or we've found an
3670 * identifier, stop searching. */
3671 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3672 break;
3673 }
3674 }
3675
3676 if (ptr[col] == NUL || (i == 0 && (
3677#ifdef FEAT_MBYTE
3678 has_mbyte ? this_class != 2 :
3679#endif
3680 !vim_iswordc(ptr[col]))))
3681 {
3682 /*
3683 * didn't find an identifier or string
3684 */
3685 if (find_type & FIND_STRING)
3686 EMSG(_("E348: No string under cursor"));
3687 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003688 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return 0;
3690 }
3691 ptr += col;
3692 *string = ptr;
3693
3694 /*
3695 * 3. Find the end if the identifier/string.
3696 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003697#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 bn = 0;
3699 startcol -= col;
3700#endif
3701 col = 0;
3702#ifdef FEAT_MBYTE
3703 if (has_mbyte)
3704 {
3705 /* Search for point of changing multibyte character class. */
3706 this_class = mb_get_class(ptr);
3707 while (ptr[col] != NUL
3708 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3709 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003710# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 || ((find_type & FIND_EVAL)
3712 && col <= (int)startcol
3713 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3714# endif
3715 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003716 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 else
3719#endif
3720 while ((i == 0 ? vim_iswordc(ptr[col])
3721 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003722# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 || ((find_type & FIND_EVAL)
3724 && col <= (int)startcol
3725 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3726# endif
3727 )
3728 {
3729 ++col;
3730 }
3731
3732 return col;
3733}
3734
3735/*
3736 * Prepare for redo of a normal command.
3737 */
3738 static void
3739prep_redo_cmd(cap)
3740 cmdarg_T *cap;
3741{
3742 prep_redo(cap->oap->regname, cap->count0,
3743 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3744}
3745
3746/*
3747 * Prepare for redo of any command.
3748 * Note that only the last argument can be a multi-byte char.
3749 */
3750 static void
3751prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3752 int regname;
3753 long num;
3754 int cmd1;
3755 int cmd2;
3756 int cmd3;
3757 int cmd4;
3758 int cmd5;
3759{
3760 ResetRedobuff();
3761 if (regname != 0) /* yank from specified buffer */
3762 {
3763 AppendCharToRedobuff('"');
3764 AppendCharToRedobuff(regname);
3765 }
3766 if (num)
3767 AppendNumberToRedobuff(num);
3768
3769 if (cmd1 != NUL)
3770 AppendCharToRedobuff(cmd1);
3771 if (cmd2 != NUL)
3772 AppendCharToRedobuff(cmd2);
3773 if (cmd3 != NUL)
3774 AppendCharToRedobuff(cmd3);
3775 if (cmd4 != NUL)
3776 AppendCharToRedobuff(cmd4);
3777 if (cmd5 != NUL)
3778 AppendCharToRedobuff(cmd5);
3779}
3780
3781/*
3782 * check for operator active and clear it
3783 *
3784 * return TRUE if operator was active
3785 */
3786 static int
3787checkclearop(oap)
3788 oparg_T *oap;
3789{
3790 if (oap->op_type == OP_NOP)
3791 return FALSE;
3792 clearopbeep(oap);
3793 return TRUE;
3794}
3795
3796/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003797 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003799 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 */
3801 static int
3802checkclearopq(oap)
3803 oparg_T *oap;
3804{
3805 if (oap->op_type == OP_NOP
3806#ifdef FEAT_VISUAL
3807 && !VIsual_active
3808#endif
3809 )
3810 return FALSE;
3811 clearopbeep(oap);
3812 return TRUE;
3813}
3814
3815 static void
3816clearop(oap)
3817 oparg_T *oap;
3818{
3819 oap->op_type = OP_NOP;
3820 oap->regname = 0;
3821 oap->motion_force = NUL;
3822 oap->use_reg_one = FALSE;
3823}
3824
3825 static void
3826clearopbeep(oap)
3827 oparg_T *oap;
3828{
3829 clearop(oap);
3830 beep_flush();
3831}
3832
3833#ifdef FEAT_VISUAL
3834/*
3835 * Remove the shift modifier from a special key.
3836 */
3837 static void
3838unshift_special(cap)
3839 cmdarg_T *cap;
3840{
3841 switch (cap->cmdchar)
3842 {
3843 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3844 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3845 case K_S_UP: cap->cmdchar = K_UP; break;
3846 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3847 case K_S_HOME: cap->cmdchar = K_HOME; break;
3848 case K_S_END: cap->cmdchar = K_END; break;
3849 }
3850 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3851}
3852#endif
3853
3854#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3855/*
3856 * Routines for displaying a partly typed command
3857 */
3858
3859#ifdef FEAT_VISUAL /* need room for size of Visual area */
3860# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3861#else
3862# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3863#endif
3864static char_u showcmd_buf[SHOWCMD_BUFLEN];
3865static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3866static int showcmd_is_clear = TRUE;
3867static int showcmd_visual = FALSE;
3868
3869static void display_showcmd __ARGS((void));
3870
3871 void
3872clear_showcmd()
3873{
3874 if (!p_sc)
3875 return;
3876
3877#ifdef FEAT_VISUAL
3878 if (VIsual_active && !char_avail())
3879 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003880 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 long lines;
3882 colnr_T leftcol, rightcol;
3883 linenr_T top, bot;
3884
3885 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003886 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 {
3888 top = VIsual.lnum;
3889 bot = curwin->w_cursor.lnum;
3890 }
3891 else
3892 {
3893 top = curwin->w_cursor.lnum;
3894 bot = VIsual.lnum;
3895 }
3896# ifdef FEAT_FOLDING
3897 /* Include closed folds as a whole. */
3898 hasFolding(top, &top, NULL);
3899 hasFolding(bot, NULL, &bot);
3900# endif
3901 lines = bot - top + 1;
3902
3903 if (VIsual_mode == Ctrl_V)
3904 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003905# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003906 char_u *saved_sbr = p_sbr;
3907
3908 /* Make 'sbr' empty for a moment to get the correct size. */
3909 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003910# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003912# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003913 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3916 (long)(rightcol - leftcol + 1));
3917 }
3918 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3919 sprintf((char *)showcmd_buf, "%ld", lines);
3920 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003921 {
3922 char_u *s, *e;
3923 int l;
3924 int bytes = 0;
3925 int chars = 0;
3926
3927 if (cursor_bot)
3928 {
3929 s = ml_get_pos(&VIsual);
3930 e = ml_get_cursor();
3931 }
3932 else
3933 {
3934 s = ml_get_cursor();
3935 e = ml_get_pos(&VIsual);
3936 }
3937 while ((*p_sel != 'e') ? s <= e : s < e)
3938 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003939# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003940 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003941# else
3942 l = (*s == NUL) ? 0 : 1;
3943# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003944 if (l == 0)
3945 {
3946 ++bytes;
3947 ++chars;
3948 break; /* end of line */
3949 }
3950 bytes += l;
3951 ++chars;
3952 s += l;
3953 }
3954 if (bytes == chars)
3955 sprintf((char *)showcmd_buf, "%d", chars);
3956 else
3957 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3960 showcmd_visual = TRUE;
3961 }
3962 else
3963#endif
3964 {
3965 showcmd_buf[0] = NUL;
3966 showcmd_visual = FALSE;
3967
3968 /* Don't actually display something if there is nothing to clear. */
3969 if (showcmd_is_clear)
3970 return;
3971 }
3972
3973 display_showcmd();
3974}
3975
3976/*
3977 * Add 'c' to string of shown command chars.
3978 * Return TRUE if output has been written (and setcursor() has been called).
3979 */
3980 int
3981add_to_showcmd(c)
3982 int c;
3983{
3984 char_u *p;
3985 int old_len;
3986 int extra_len;
3987 int overflow;
3988#if defined(FEAT_MOUSE)
3989 int i;
3990 static int ignore[] =
3991 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003992# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3994 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 K_IGNORE,
3997 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3998 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3999 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004000 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004002 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 0
4004 };
4005#endif
4006
Bram Moolenaar09df3122006-01-23 22:23:09 +00004007 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 return FALSE;
4009
4010 if (showcmd_visual)
4011 {
4012 showcmd_buf[0] = NUL;
4013 showcmd_visual = FALSE;
4014 }
4015
4016#if defined(FEAT_MOUSE)
4017 /* Ignore keys that are scrollbar updates and mouse clicks */
4018 if (IS_SPECIAL(c))
4019 for (i = 0; ignore[i] != 0; ++i)
4020 if (ignore[i] == c)
4021 return FALSE;
4022#endif
4023
4024 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01004025 if (*p == ' ')
4026 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 old_len = (int)STRLEN(showcmd_buf);
4028 extra_len = (int)STRLEN(p);
4029 overflow = old_len + extra_len - SHOWCMD_COLS;
4030 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004031 mch_memmove(showcmd_buf, showcmd_buf + overflow,
4032 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 STRCAT(showcmd_buf, p);
4034
4035 if (char_avail())
4036 return FALSE;
4037
4038 display_showcmd();
4039
4040 return TRUE;
4041}
4042
4043 void
4044add_to_showcmd_c(c)
4045 int c;
4046{
4047 if (!add_to_showcmd(c))
4048 setcursor();
4049}
4050
4051/*
4052 * Delete 'len' characters from the end of the shown command.
4053 */
4054 static void
4055del_from_showcmd(len)
4056 int len;
4057{
4058 int old_len;
4059
4060 if (!p_sc)
4061 return;
4062
4063 old_len = (int)STRLEN(showcmd_buf);
4064 if (len > old_len)
4065 len = old_len;
4066 showcmd_buf[old_len - len] = NUL;
4067
4068 if (!char_avail())
4069 display_showcmd();
4070}
4071
4072/*
4073 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4074 * something and there is a partial mapping.
4075 */
4076 void
4077push_showcmd()
4078{
4079 if (p_sc)
4080 STRCPY(old_showcmd_buf, showcmd_buf);
4081}
4082
4083 void
4084pop_showcmd()
4085{
4086 if (!p_sc)
4087 return;
4088
4089 STRCPY(showcmd_buf, old_showcmd_buf);
4090
4091 display_showcmd();
4092}
4093
4094 static void
4095display_showcmd()
4096{
4097 int len;
4098
4099 cursor_off();
4100
4101 len = (int)STRLEN(showcmd_buf);
4102 if (len == 0)
4103 showcmd_is_clear = TRUE;
4104 else
4105 {
4106 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4107 showcmd_is_clear = FALSE;
4108 }
4109
4110 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004111 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4112 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 */
4114 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4115
4116 setcursor(); /* put cursor back where it belongs */
4117}
4118#endif
4119
4120#ifdef FEAT_SCROLLBIND
4121/*
4122 * When "check" is FALSE, prepare for commands that scroll the window.
4123 * When "check" is TRUE, take care of scroll-binding after the window has
4124 * scrolled. Called from normal_cmd() and edit().
4125 */
4126 void
4127do_check_scrollbind(check)
4128 int check;
4129{
4130 static win_T *old_curwin = NULL;
4131 static linenr_T old_topline = 0;
4132#ifdef FEAT_DIFF
4133 static int old_topfill = 0;
4134#endif
4135 static buf_T *old_buf = NULL;
4136 static colnr_T old_leftcol = 0;
4137
4138 if (check && curwin->w_p_scb)
4139 {
4140 /* If a ":syncbind" command was just used, don't scroll, only reset
4141 * the values. */
4142 if (did_syncbind)
4143 did_syncbind = FALSE;
4144 else if (curwin == old_curwin)
4145 {
4146 /*
4147 * Synchronize other windows, as necessary according to
4148 * 'scrollbind'. Don't do this after an ":edit" command, except
4149 * when 'diff' is set.
4150 */
4151 if ((curwin->w_buffer == old_buf
4152#ifdef FEAT_DIFF
4153 || curwin->w_p_diff
4154#endif
4155 )
4156 && (curwin->w_topline != old_topline
4157#ifdef FEAT_DIFF
4158 || curwin->w_topfill != old_topfill
4159#endif
4160 || curwin->w_leftcol != old_leftcol))
4161 {
4162 check_scrollbind(curwin->w_topline - old_topline,
4163 (long)(curwin->w_leftcol - old_leftcol));
4164 }
4165 }
4166 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4167 {
4168 /*
4169 * When switching between windows, make sure that the relative
4170 * vertical offset is valid for the new window. The relative
4171 * offset is invalid whenever another 'scrollbind' window has
4172 * scrolled to a point that would force the current window to
4173 * scroll past the beginning or end of its buffer. When the
4174 * resync is performed, some of the other 'scrollbind' windows may
4175 * need to jump so that the current window's relative position is
4176 * visible on-screen.
4177 */
4178 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4179 }
4180 curwin->w_scbind_pos = curwin->w_topline;
4181 }
4182
4183 old_curwin = curwin;
4184 old_topline = curwin->w_topline;
4185#ifdef FEAT_DIFF
4186 old_topfill = curwin->w_topfill;
4187#endif
4188 old_buf = curwin->w_buffer;
4189 old_leftcol = curwin->w_leftcol;
4190}
4191
4192/*
4193 * Synchronize any windows that have "scrollbind" set, based on the
4194 * number of rows by which the current window has changed
4195 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4196 */
4197 void
4198check_scrollbind(topline_diff, leftcol_diff)
4199 linenr_T topline_diff;
4200 long leftcol_diff;
4201{
4202 int want_ver;
4203 int want_hor;
4204 win_T *old_curwin = curwin;
4205 buf_T *old_curbuf = curbuf;
4206#ifdef FEAT_VISUAL
4207 int old_VIsual_select = VIsual_select;
4208 int old_VIsual_active = VIsual_active;
4209#endif
4210 colnr_T tgt_leftcol = curwin->w_leftcol;
4211 long topline;
4212 long y;
4213
4214 /*
4215 * check 'scrollopt' string for vertical and horizontal scroll options
4216 */
4217 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4218#ifdef FEAT_DIFF
4219 want_ver |= old_curwin->w_p_diff;
4220#endif
4221 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4222
4223 /*
4224 * loop through the scrollbound windows and scroll accordingly
4225 */
4226#ifdef FEAT_VISUAL
4227 VIsual_select = VIsual_active = 0;
4228#endif
4229 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4230 {
4231 curbuf = curwin->w_buffer;
4232 /* skip original window and windows with 'noscrollbind' */
4233 if (curwin != old_curwin && curwin->w_p_scb)
4234 {
4235 /*
4236 * do the vertical scroll
4237 */
4238 if (want_ver)
4239 {
4240#ifdef FEAT_DIFF
4241 if (old_curwin->w_p_diff && curwin->w_p_diff)
4242 {
4243 diff_set_topline(old_curwin, curwin);
4244 }
4245 else
4246#endif
4247 {
4248 curwin->w_scbind_pos += topline_diff;
4249 topline = curwin->w_scbind_pos;
4250 if (topline > curbuf->b_ml.ml_line_count)
4251 topline = curbuf->b_ml.ml_line_count;
4252 if (topline < 1)
4253 topline = 1;
4254
4255 y = topline - curwin->w_topline;
4256 if (y > 0)
4257 scrollup(y, FALSE);
4258 else
4259 scrolldown(-y, FALSE);
4260 }
4261
4262 redraw_later(VALID);
4263 cursor_correct();
4264#ifdef FEAT_WINDOWS
4265 curwin->w_redr_status = TRUE;
4266#endif
4267 }
4268
4269 /*
4270 * do the horizontal scroll
4271 */
4272 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4273 {
4274 curwin->w_leftcol = tgt_leftcol;
4275 leftcol_changed();
4276 }
4277 }
4278 }
4279
4280 /*
4281 * reset current-window
4282 */
4283#ifdef FEAT_VISUAL
4284 VIsual_select = old_VIsual_select;
4285 VIsual_active = old_VIsual_active;
4286#endif
4287 curwin = old_curwin;
4288 curbuf = old_curbuf;
4289}
4290#endif /* #ifdef FEAT_SCROLLBIND */
4291
4292/*
4293 * Command character that's ignored.
4294 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004295 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 static void
4298nv_ignore(cap)
4299 cmdarg_T *cap;
4300{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004301 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302}
4303
4304/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004305 * Command character that doesn't do anything, but unlike nv_ignore() does
4306 * start edit(). Used for "startinsert" executed while starting up.
4307 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004308 static void
4309nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004310 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004311{
4312}
4313
4314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 * Command character doesn't exist.
4316 */
4317 static void
4318nv_error(cap)
4319 cmdarg_T *cap;
4320{
4321 clearopbeep(cap->oap);
4322}
4323
4324/*
4325 * <Help> and <F1> commands.
4326 */
4327 static void
4328nv_help(cap)
4329 cmdarg_T *cap;
4330{
4331 if (!checkclearopq(cap->oap))
4332 ex_help(NULL);
4333}
4334
4335/*
4336 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4337 */
4338 static void
4339nv_addsub(cap)
4340 cmdarg_T *cap;
4341{
4342 if (!checkclearopq(cap->oap)
4343 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4344 prep_redo_cmd(cap);
4345}
4346
4347/*
4348 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4349 */
4350 static void
4351nv_page(cap)
4352 cmdarg_T *cap;
4353{
4354 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004355 {
4356#ifdef FEAT_WINDOWS
4357 if (mod_mask & MOD_MASK_CTRL)
4358 {
4359 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4360 if (cap->arg == BACKWARD)
4361 goto_tabpage(-(int)cap->count1);
4362 else
4363 goto_tabpage((int)cap->count0);
4364 }
4365 else
4366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370
4371/*
4372 * Implementation of "gd" and "gD" command.
4373 */
4374 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004375nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 oparg_T *oap;
4377 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004378 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
4380 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *ptr;
4382
Bram Moolenaard9d30582005-05-18 22:10:28 +00004383 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004384 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004386#ifdef FEAT_FOLDING
4387 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4388 foldOpenCursor();
4389#endif
4390}
4391
4392/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004393 * Search for variable declaration of "ptr[len]".
4394 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4395 * current file ("gD").
4396 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004397 * Return FAIL when not found.
4398 */
4399 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004400find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 char_u *ptr;
4402 int len;
4403 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004404 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004405 int searchflags; /* flags passed to searchit() */
4406{
4407 char_u *pat;
4408 pos_T old_pos;
4409 pos_T par_pos;
4410 pos_T found_pos;
4411 int t;
4412 int save_p_ws;
4413 int save_p_scs;
4414 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004415 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004416
4417 if ((pat = alloc(len + 7)) == NULL)
4418 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004419
4420 /* Put "\V" before the pattern to avoid that the special meaning of "."
4421 * and "~" causes trouble. */
4422 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4423 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 old_pos = curwin->w_cursor;
4425 save_p_ws = p_ws;
4426 save_p_scs = p_scs;
4427 p_ws = FALSE; /* don't wrap around end of file now */
4428 p_scs = FALSE; /* don't switch ignorecase off now */
4429
4430 /*
4431 * With "gD" go to line 1.
4432 * With "gd" Search back for the start of the current function, then go
4433 * back until a blank line. If this fails go to line 1.
4434 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004435 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 setpcmark(); /* Set in findpar() otherwise */
4438 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004439 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 else
4442 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004443 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4445 --curwin->w_cursor.lnum;
4446 }
4447 curwin->w_cursor.col = 0;
4448
4449 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004450 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004451 for (;;)
4452 {
4453 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004454 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004455 if (curwin->w_cursor.lnum >= old_pos.lnum)
4456 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004457
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004458 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004459 {
4460 pos_T *pos;
4461
4462 /* Check that the block the match is in doesn't end before the
4463 * position where we started the search from. */
4464 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4465 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4466 && pos->lnum < old_pos.lnum)
4467 continue;
4468 }
4469
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004470 if (t == FAIL)
4471 {
4472 /* If we previously found a valid position, use it. */
4473 if (found_pos.lnum != 0)
4474 {
4475 curwin->w_cursor = found_pos;
4476 t = OK;
4477 }
4478 break;
4479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004481 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004482 {
4483 /* Ignore this line, continue at start of next line. */
4484 ++curwin->w_cursor.lnum;
4485 curwin->w_cursor.col = 0;
4486 continue;
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004489 if (!locally) /* global search: use first match found */
4490 break;
4491 if (curwin->w_cursor.lnum >= par_pos.lnum)
4492 {
4493 /* If we previously found a valid position, use it. */
4494 if (found_pos.lnum != 0)
4495 curwin->w_cursor = found_pos;
4496 break;
4497 }
4498
4499 /* For finding a local variable and the match is before the "{" search
4500 * to find a later match. For K&R style function declarations this
4501 * skips the function header without types. */
4502 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004504
4505 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004507 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 curwin->w_cursor = old_pos;
4509 }
4510 else
4511 {
4512 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* "n" searches forward now */
4514 reset_search_dir();
4515 }
4516
4517 vim_free(pat);
4518 p_ws = save_p_ws;
4519 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004520
4521 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
4524/*
4525 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4526 * lines rather than lines in the file.
4527 * 'dist' must be positive.
4528 *
4529 * Return OK if able to move cursor, FAIL otherwise.
4530 */
4531 static int
4532nv_screengo(oap, dir, dist)
4533 oparg_T *oap;
4534 int dir;
4535 long dist;
4536{
4537 int linelen = linetabsize(ml_get_curline());
4538 int retval = OK;
4539 int atend = FALSE;
4540 int n;
4541 int col_off1; /* margin offset for first screen line */
4542 int col_off2; /* margin offset for wrapped screen line */
4543 int width1; /* text width for first screen line */
4544 int width2; /* test width for wrapped screen line */
4545
4546 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004547 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 col_off1 = curwin_col_off();
4550 col_off2 = col_off1 - curwin_col_off2();
4551 width1 = W_WIDTH(curwin) - col_off1;
4552 width2 = W_WIDTH(curwin) - col_off2;
4553
4554#ifdef FEAT_VERTSPLIT
4555 if (curwin->w_width != 0)
4556 {
4557#endif
4558 /*
4559 * Instead of sticking at the last character of the buffer line we
4560 * try to stick in the last column of the screen.
4561 */
4562 if (curwin->w_curswant == MAXCOL)
4563 {
4564 atend = TRUE;
4565 validate_virtcol();
4566 if (width1 <= 0)
4567 curwin->w_curswant = 0;
4568 else
4569 {
4570 curwin->w_curswant = width1 - 1;
4571 if (curwin->w_virtcol > curwin->w_curswant)
4572 curwin->w_curswant += ((curwin->w_virtcol
4573 - curwin->w_curswant - 1) / width2 + 1) * width2;
4574 }
4575 }
4576 else
4577 {
4578 if (linelen > width1)
4579 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4580 else
4581 n = width1;
4582 if (curwin->w_curswant > (colnr_T)n + 1)
4583 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4584 * width2;
4585 }
4586
4587 while (dist--)
4588 {
4589 if (dir == BACKWARD)
4590 {
4591 if ((long)curwin->w_curswant >= width2)
4592 /* move back within line */
4593 curwin->w_curswant -= width2;
4594 else
4595 {
4596 /* to previous line */
4597 if (curwin->w_cursor.lnum == 1)
4598 {
4599 retval = FAIL;
4600 break;
4601 }
4602 --curwin->w_cursor.lnum;
4603#ifdef FEAT_FOLDING
4604 /* Move to the start of a closed fold. Don't do that when
4605 * 'foldopen' contains "all": it will open in a moment. */
4606 if (!(fdo_flags & FDO_ALL))
4607 (void)hasFolding(curwin->w_cursor.lnum,
4608 &curwin->w_cursor.lnum, NULL);
4609#endif
4610 linelen = linetabsize(ml_get_curline());
4611 if (linelen > width1)
4612 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4613 + 1) * width2;
4614 }
4615 }
4616 else /* dir == FORWARD */
4617 {
4618 if (linelen > width1)
4619 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4620 else
4621 n = width1;
4622 if (curwin->w_curswant + width2 < (colnr_T)n)
4623 /* move forward within line */
4624 curwin->w_curswant += width2;
4625 else
4626 {
4627 /* to next line */
4628#ifdef FEAT_FOLDING
4629 /* Move to the end of a closed fold. */
4630 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4631 &curwin->w_cursor.lnum);
4632#endif
4633 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4634 {
4635 retval = FAIL;
4636 break;
4637 }
4638 curwin->w_cursor.lnum++;
4639 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004640 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 }
4643 }
4644#ifdef FEAT_VERTSPLIT
4645 }
4646#endif
4647
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004648 if (virtual_active() && atend)
4649 coladvance(MAXCOL);
4650 else
4651 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4654 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4655 {
4656 /*
4657 * Check for landing on a character that got split at the end of the
4658 * last line. We want to advance a screenline, not end up in the same
4659 * screenline or move two screenlines.
4660 */
4661 validate_virtcol();
4662 if (curwin->w_virtcol > curwin->w_curswant
4663 && (curwin->w_curswant < (colnr_T)width1
4664 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4665 : ((curwin->w_curswant - width1) % width2
4666 > (colnr_T)width2 / 2)))
4667 --curwin->w_cursor.col;
4668 }
4669#endif
4670
4671 if (atend)
4672 curwin->w_curswant = MAXCOL; /* stick in the last column */
4673
4674 return retval;
4675}
4676
4677#ifdef FEAT_MOUSE
4678/*
4679 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4680 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004681 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4682 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
4684 static void
4685nv_mousescroll(cap)
4686 cmdarg_T *cap;
4687{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004688# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004689 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004691 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 int row, col;
4694
4695 row = mouse_row;
4696 col = mouse_col;
4697
4698 /* find the window at the pointer coordinates */
4699 curwin = mouse_find_win(&row, &col);
4700 curbuf = curwin->w_buffer;
4701 }
4702# endif
4703
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004704 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004706 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4707 {
4708 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4709 }
4710 else
4711 {
4712 cap->count1 = 3;
4713 cap->count0 = 3;
4714 nv_scroll_line(cap);
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004717# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 else
4719 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004720 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4721 if (!curwin->w_p_wrap)
4722 {
4723 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004724
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004725 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4726 step = W_WIDTH(curwin);
4727 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4728 if (val < 0)
4729 val = 0;
4730
4731 gui_do_horiz_scroll(val, TRUE);
4732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004734# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004736# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 curwin->w_redr_status = TRUE;
4738
4739 curwin = old_curwin;
4740 curbuf = curwin->w_buffer;
4741# endif
4742}
4743
4744/*
4745 * Mouse clicks and drags.
4746 */
4747 static void
4748nv_mouse(cap)
4749 cmdarg_T *cap;
4750{
4751 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4752}
4753#endif
4754
4755/*
4756 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4757 * cap->arg must be TRUE for CTRL-E.
4758 */
4759 static void
4760nv_scroll_line(cap)
4761 cmdarg_T *cap;
4762{
4763 if (!checkclearop(cap->oap))
4764 scroll_redraw(cap->arg, cap->count1);
4765}
4766
4767/*
4768 * Scroll "count" lines up or down, and redraw.
4769 */
4770 void
4771scroll_redraw(up, count)
4772 int up;
4773 long count;
4774{
4775 linenr_T prev_topline = curwin->w_topline;
4776#ifdef FEAT_DIFF
4777 int prev_topfill = curwin->w_topfill;
4778#endif
4779 linenr_T prev_lnum = curwin->w_cursor.lnum;
4780
4781 if (up)
4782 scrollup(count, TRUE);
4783 else
4784 scrolldown(count, TRUE);
4785 if (p_so)
4786 {
4787 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4788 * valid, otherwise the screen jumps back at the end of the file. */
4789 cursor_correct();
4790 check_cursor_moved(curwin);
4791 curwin->w_valid |= VALID_TOPLINE;
4792
4793 /* If moved back to where we were, at least move the cursor, otherwise
4794 * we get stuck at one position. Don't move the cursor up if the
4795 * first line of the buffer is already on the screen */
4796 while (curwin->w_topline == prev_topline
4797#ifdef FEAT_DIFF
4798 && curwin->w_topfill == prev_topfill
4799#endif
4800 )
4801 {
4802 if (up)
4803 {
4804 if (curwin->w_cursor.lnum > prev_lnum
4805 || cursor_down(1L, FALSE) == FAIL)
4806 break;
4807 }
4808 else
4809 {
4810 if (curwin->w_cursor.lnum < prev_lnum
4811 || prev_topline == 1L
4812 || cursor_up(1L, FALSE) == FAIL)
4813 break;
4814 }
4815 /* Mark w_topline as valid, otherwise the screen jumps back at the
4816 * end of the file. */
4817 check_cursor_moved(curwin);
4818 curwin->w_valid |= VALID_TOPLINE;
4819 }
4820 }
4821 if (curwin->w_cursor.lnum != prev_lnum)
4822 coladvance(curwin->w_curswant);
4823 redraw_later(VALID);
4824}
4825
4826/*
4827 * Commands that start with "z".
4828 */
4829 static void
4830nv_zet(cap)
4831 cmdarg_T *cap;
4832{
4833 long n;
4834 colnr_T col;
4835 int nchar = cap->nchar;
4836#ifdef FEAT_FOLDING
4837 long old_fdl = curwin->w_p_fdl;
4838 int old_fen = curwin->w_p_fen;
4839#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004840#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004841 int undo = FALSE;
4842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 if (VIM_ISDIGIT(nchar))
4845 {
4846 /*
4847 * "z123{nchar}": edit the count before obtaining {nchar}
4848 */
4849 if (checkclearop(cap->oap))
4850 return;
4851 n = nchar - '0';
4852 for (;;)
4853 {
4854#ifdef USE_ON_FLY_SCROLL
4855 dont_scroll = TRUE; /* disallow scrolling here */
4856#endif
4857 ++no_mapping;
4858 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004859 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 --no_mapping;
4862 --allow_keys;
4863#ifdef FEAT_CMDL_INFO
4864 (void)add_to_showcmd(nchar);
4865#endif
4866 if (nchar == K_DEL || nchar == K_KDEL)
4867 n /= 10;
4868 else if (VIM_ISDIGIT(nchar))
4869 n = n * 10 + (nchar - '0');
4870 else if (nchar == CAR)
4871 {
4872#ifdef FEAT_GUI
4873 need_mouse_correct = TRUE;
4874#endif
4875 win_setheight((int)n);
4876 break;
4877 }
4878 else if (nchar == 'l'
4879 || nchar == 'h'
4880 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004881 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
4883 cap->count1 = n ? n * cap->count1 : cap->count1;
4884 goto dozet;
4885 }
4886 else
4887 {
4888 clearopbeep(cap->oap);
4889 break;
4890 }
4891 }
4892 cap->oap->op_type = OP_NOP;
4893 return;
4894 }
4895
4896dozet:
4897 if (
4898#ifdef FEAT_FOLDING
4899 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4900 * and "zC" only in Visual mode. "zj" and "zk" are motion
4901 * commands. */
4902 cap->nchar != 'f' && cap->nchar != 'F'
4903 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4904 && cap->nchar != 'j' && cap->nchar != 'k'
4905 &&
4906#endif
4907 checkclearop(cap->oap))
4908 return;
4909
4910 /*
4911 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4912 * If line number given, set cursor.
4913 */
4914 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4915 && cap->count0
4916 && cap->count0 != curwin->w_cursor.lnum)
4917 {
4918 setpcmark();
4919 if (cap->count0 > curbuf->b_ml.ml_line_count)
4920 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4921 else
4922 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004923 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925
4926 switch (nchar)
4927 {
4928 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4929 case '+':
4930 if (cap->count0 == 0)
4931 {
4932 /* No count given: put cursor at the line below screen */
4933 validate_botline(); /* make sure w_botline is valid */
4934 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4935 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4936 else
4937 curwin->w_cursor.lnum = curwin->w_botline;
4938 }
4939 /* FALLTHROUGH */
4940 case NL:
4941 case CAR:
4942 case K_KENTER:
4943 beginline(BL_WHITE | BL_FIX);
4944 /* FALLTHROUGH */
4945
4946 case 't': scroll_cursor_top(0, TRUE);
4947 redraw_later(VALID);
4948 break;
4949
4950 /* "z." and "zz": put cursor in middle of screen */
4951 case '.': beginline(BL_WHITE | BL_FIX);
4952 /* FALLTHROUGH */
4953
4954 case 'z': scroll_cursor_halfway(TRUE);
4955 redraw_later(VALID);
4956 break;
4957
4958 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4959 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4960 * when <count> is at bottom of window, and puts that one at
4961 * bottom of window. */
4962 if (cap->count0 != 0)
4963 {
4964 scroll_cursor_bot(0, TRUE);
4965 curwin->w_cursor.lnum = curwin->w_topline;
4966 }
4967 else if (curwin->w_topline == 1)
4968 curwin->w_cursor.lnum = 1;
4969 else
4970 curwin->w_cursor.lnum = curwin->w_topline - 1;
4971 /* FALLTHROUGH */
4972 case '-':
4973 beginline(BL_WHITE | BL_FIX);
4974 /* FALLTHROUGH */
4975
4976 case 'b': scroll_cursor_bot(0, TRUE);
4977 redraw_later(VALID);
4978 break;
4979
4980 /* "zH" - scroll screen right half-page */
4981 case 'H':
4982 cap->count1 *= W_WIDTH(curwin) / 2;
4983 /* FALLTHROUGH */
4984
4985 /* "zh" - scroll screen to the right */
4986 case 'h':
4987 case K_LEFT:
4988 if (!curwin->w_p_wrap)
4989 {
4990 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4991 curwin->w_leftcol = 0;
4992 else
4993 curwin->w_leftcol -= (colnr_T)cap->count1;
4994 leftcol_changed();
4995 }
4996 break;
4997
4998 /* "zL" - scroll screen left half-page */
4999 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
5000 /* FALLTHROUGH */
5001
5002 /* "zl" - scroll screen to the left */
5003 case 'l':
5004 case K_RIGHT:
5005 if (!curwin->w_p_wrap)
5006 {
5007 /* scroll the window left */
5008 curwin->w_leftcol += (colnr_T)cap->count1;
5009 leftcol_changed();
5010 }
5011 break;
5012
5013 /* "zs" - scroll screen, cursor at the start */
5014 case 's': if (!curwin->w_p_wrap)
5015 {
5016#ifdef FEAT_FOLDING
5017 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5018 col = 0; /* like the cursor is in col 0 */
5019 else
5020#endif
5021 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
5022 if ((long)col > p_siso)
5023 col -= p_siso;
5024 else
5025 col = 0;
5026 if (curwin->w_leftcol != col)
5027 {
5028 curwin->w_leftcol = col;
5029 redraw_later(NOT_VALID);
5030 }
5031 }
5032 break;
5033
5034 /* "ze" - scroll screen, cursor at the end */
5035 case 'e': if (!curwin->w_p_wrap)
5036 {
5037#ifdef FEAT_FOLDING
5038 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5039 col = 0; /* like the cursor is in col 0 */
5040 else
5041#endif
5042 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5043 n = W_WIDTH(curwin) - curwin_col_off();
5044 if ((long)col + p_siso < n)
5045 col = 0;
5046 else
5047 col = col + p_siso - n + 1;
5048 if (curwin->w_leftcol != col)
5049 {
5050 curwin->w_leftcol = col;
5051 redraw_later(NOT_VALID);
5052 }
5053 }
5054 break;
5055
5056#ifdef FEAT_FOLDING
5057 /* "zF": create fold command */
5058 /* "zf": create fold operator */
5059 case 'F':
5060 case 'f': if (foldManualAllowed(TRUE))
5061 {
5062 cap->nchar = 'f';
5063 nv_operator(cap);
5064 curwin->w_p_fen = TRUE;
5065
5066 /* "zF" is like "zfzf" */
5067 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5068 {
5069 nv_operator(cap);
5070 finish_op = TRUE;
5071 }
5072 }
5073 else
5074 clearopbeep(cap->oap);
5075 break;
5076
5077 /* "zd": delete fold at cursor */
5078 /* "zD": delete fold at cursor recursively */
5079 case 'd':
5080 case 'D': if (foldManualAllowed(FALSE))
5081 {
5082 if (VIsual_active)
5083 nv_operator(cap);
5084 else
5085 deleteFold(curwin->w_cursor.lnum,
5086 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5087 }
5088 break;
5089
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005090 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 case 'E': if (foldmethodIsManual(curwin))
5092 {
5093 clearFolding(curwin);
5094 changed_window_setting();
5095 }
5096 else if (foldmethodIsMarker(curwin))
5097 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5098 TRUE, FALSE);
5099 else
5100 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5101 break;
5102
5103 /* "zn": fold none: reset 'foldenable' */
5104 case 'n': curwin->w_p_fen = FALSE;
5105 break;
5106
5107 /* "zN": fold Normal: set 'foldenable' */
5108 case 'N': curwin->w_p_fen = TRUE;
5109 break;
5110
5111 /* "zi": invert folding: toggle 'foldenable' */
5112 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5113 break;
5114
5115 /* "za": open closed fold or close open fold at cursor */
5116 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5117 openFold(curwin->w_cursor.lnum, cap->count1);
5118 else
5119 {
5120 closeFold(curwin->w_cursor.lnum, cap->count1);
5121 curwin->w_p_fen = TRUE;
5122 }
5123 break;
5124
5125 /* "zA": open fold at cursor recursively */
5126 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5127 openFoldRecurse(curwin->w_cursor.lnum);
5128 else
5129 {
5130 closeFoldRecurse(curwin->w_cursor.lnum);
5131 curwin->w_p_fen = TRUE;
5132 }
5133 break;
5134
5135 /* "zo": open fold at cursor or Visual area */
5136 case 'o': if (VIsual_active)
5137 nv_operator(cap);
5138 else
5139 openFold(curwin->w_cursor.lnum, cap->count1);
5140 break;
5141
5142 /* "zO": open fold recursively */
5143 case 'O': if (VIsual_active)
5144 nv_operator(cap);
5145 else
5146 openFoldRecurse(curwin->w_cursor.lnum);
5147 break;
5148
5149 /* "zc": close fold at cursor or Visual area */
5150 case 'c': if (VIsual_active)
5151 nv_operator(cap);
5152 else
5153 closeFold(curwin->w_cursor.lnum, cap->count1);
5154 curwin->w_p_fen = TRUE;
5155 break;
5156
5157 /* "zC": close fold recursively */
5158 case 'C': if (VIsual_active)
5159 nv_operator(cap);
5160 else
5161 closeFoldRecurse(curwin->w_cursor.lnum);
5162 curwin->w_p_fen = TRUE;
5163 break;
5164
5165 /* "zv": open folds at the cursor */
5166 case 'v': foldOpenCursor();
5167 break;
5168
5169 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5170 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005171 curwin->w_foldinvalid = TRUE; /* recompute folds */
5172 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 foldOpenCursor();
5174 break;
5175
5176 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5177 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005178 curwin->w_foldinvalid = TRUE; /* recompute folds */
5179 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /* "zm": fold more */
5183 case 'm': if (curwin->w_p_fdl > 0)
5184 --curwin->w_p_fdl;
5185 old_fdl = -1; /* force an update */
5186 curwin->w_p_fen = TRUE;
5187 break;
5188
5189 /* "zM": close all folds */
5190 case 'M': curwin->w_p_fdl = 0;
5191 old_fdl = -1; /* force an update */
5192 curwin->w_p_fen = TRUE;
5193 break;
5194
5195 /* "zr": reduce folding */
5196 case 'r': ++curwin->w_p_fdl;
5197 break;
5198
5199 /* "zR": open all folds */
5200 case 'R': curwin->w_p_fdl = getDeepestNesting();
5201 old_fdl = -1; /* force an update */
5202 break;
5203
5204 case 'j': /* "zj" move to next fold downwards */
5205 case 'k': /* "zk" move to next fold upwards */
5206 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5207 cap->count1) == FAIL)
5208 clearopbeep(cap->oap);
5209 break;
5210
5211#endif /* FEAT_FOLDING */
5212
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005213#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005214 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5215 ++no_mapping;
5216 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005217 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005218 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005219 --no_mapping;
5220 --allow_keys;
5221#ifdef FEAT_CMDL_INFO
5222 (void)add_to_showcmd(nchar);
5223#endif
5224 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5225 {
5226 clearopbeep(cap->oap);
5227 break;
5228 }
5229 undo = TRUE;
5230 /*FALLTHROUGH*/
5231
Bram Moolenaarb765d632005-06-07 21:00:02 +00005232 case 'g': /* "zg": add good word to word list */
5233 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005234 case 'G': /* "zG": add good word to temp word list */
5235 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005236 {
5237 char_u *ptr = NULL;
5238 int len;
5239
5240 if (checkclearop(cap->oap))
5241 break;
5242# ifdef FEAT_VISUAL
5243 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5244 == FAIL)
5245 return;
5246# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005247 if (ptr == NULL)
5248 {
5249 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005250
Bram Moolenaar134bf072013-09-25 18:54:24 +02005251 /* Find bad word under the cursor. When 'spell' is
5252 * off this fails and find_ident_under_cursor() is
5253 * used below. */
5254 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005255 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005256 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005257 if (len != 0 && curwin->w_cursor.col <= pos.col)
5258 ptr = ml_get_pos(&curwin->w_cursor);
5259 curwin->w_cursor = pos;
5260 }
5261
Bram Moolenaarb765d632005-06-07 21:00:02 +00005262 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5263 FIND_IDENT)) == 0)
5264 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005265 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005266 (nchar == 'G' || nchar == 'W')
5267 ? 0 : (int)cap->count1,
5268 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005269 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005270 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271
Bram Moolenaar43abc522005-12-10 20:15:02 +00005272 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005273 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005274 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005275 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005276#endif
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 default: clearopbeep(cap->oap);
5279 }
5280
5281#ifdef FEAT_FOLDING
5282 /* Redraw when 'foldenable' changed */
5283 if (old_fen != curwin->w_p_fen)
5284 {
5285# ifdef FEAT_DIFF
5286 win_T *wp;
5287
5288 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5289 {
5290 /* Adjust 'foldenable' in diff-synced windows. */
5291 FOR_ALL_WINDOWS(wp)
5292 {
5293 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5294 {
5295 wp->w_p_fen = curwin->w_p_fen;
5296 changed_window_setting_win(wp);
5297 }
5298 }
5299 }
5300# endif
5301 changed_window_setting();
5302 }
5303
5304 /* Redraw when 'foldlevel' changed. */
5305 if (old_fdl != curwin->w_p_fdl)
5306 newFoldLevel();
5307#endif
5308}
5309
5310#ifdef FEAT_GUI
5311/*
5312 * Vertical scrollbar movement.
5313 */
5314 static void
5315nv_ver_scrollbar(cap)
5316 cmdarg_T *cap;
5317{
5318 if (cap->oap->op_type != OP_NOP)
5319 clearopbeep(cap->oap);
5320
5321 /* Even if an operator was pending, we still want to scroll */
5322 gui_do_scroll();
5323}
5324
5325/*
5326 * Horizontal scrollbar movement.
5327 */
5328 static void
5329nv_hor_scrollbar(cap)
5330 cmdarg_T *cap;
5331{
5332 if (cap->oap->op_type != OP_NOP)
5333 clearopbeep(cap->oap);
5334
5335 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005336 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337}
5338#endif
5339
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005340#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005341/*
5342 * Click in GUI tab.
5343 */
5344 static void
5345nv_tabline(cap)
5346 cmdarg_T *cap;
5347{
5348 if (cap->oap->op_type != OP_NOP)
5349 clearopbeep(cap->oap);
5350
5351 /* Even if an operator was pending, we still want to jump tabs. */
5352 goto_tabpage(current_tab);
5353}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005354
5355/*
5356 * Selected item in tab line menu.
5357 */
5358 static void
5359nv_tabmenu(cap)
5360 cmdarg_T *cap;
5361{
5362 if (cap->oap->op_type != OP_NOP)
5363 clearopbeep(cap->oap);
5364
5365 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005366 handle_tabmenu();
5367}
5368
5369/*
5370 * Handle selecting an item of the GUI tab line menu.
5371 * Used in Normal and Insert mode.
5372 */
5373 void
5374handle_tabmenu()
5375{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005376 switch (current_tabmenu)
5377 {
5378 case TABLINE_MENU_CLOSE:
5379 if (current_tab == 0)
5380 do_cmdline_cmd((char_u *)"tabclose");
5381 else
5382 {
5383 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5384 current_tab);
5385 do_cmdline_cmd(IObuff);
5386 }
5387 break;
5388
5389 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005390 vim_snprintf((char *)IObuff, IOSIZE, "%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 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005396 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5397 current_tab > 0 ? current_tab - 1 : 999);
5398 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005399 break;
5400 }
5401}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005402#endif
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404/*
5405 * "Q" command.
5406 */
5407 static void
5408nv_exmode(cap)
5409 cmdarg_T *cap;
5410{
5411 /*
5412 * Ignore 'Q' in Visual mode, just give a beep.
5413 */
5414#ifdef FEAT_VISUAL
5415 if (VIsual_active)
5416 vim_beep();
5417 else
5418#endif
5419 if (!checkclearop(cap->oap))
5420 do_exmode(FALSE);
5421}
5422
5423/*
5424 * Handle a ":" command.
5425 */
5426 static void
5427nv_colon(cap)
5428 cmdarg_T *cap;
5429{
5430 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005431 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
5433#ifdef FEAT_VISUAL
5434 if (VIsual_active)
5435 nv_operator(cap);
5436 else
5437#endif
5438 {
5439 if (cap->oap->op_type != OP_NOP)
5440 {
5441 /* Using ":" as a movement is characterwise exclusive. */
5442 cap->oap->motion_type = MCHAR;
5443 cap->oap->inclusive = FALSE;
5444 }
5445 else if (cap->count0)
5446 {
5447 /* translate "count:" into ":.,.+(count - 1)" */
5448 stuffcharReadbuff('.');
5449 if (cap->count0 > 1)
5450 {
5451 stuffReadbuff((char_u *)",.+");
5452 stuffnumReadbuff((long)cap->count0 - 1L);
5453 }
5454 }
5455
5456 /* When typing, don't type below an old message */
5457 if (KeyTyped)
5458 compute_cmdrow();
5459
5460 old_p_im = p_im;
5461
5462 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005463 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5465
5466 /* If 'insertmode' changed, enter or exit Insert mode */
5467 if (p_im != old_p_im)
5468 {
5469 if (p_im)
5470 restart_edit = 'i';
5471 else
5472 restart_edit = 0;
5473 }
5474
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005475 if (cmd_result == FAIL)
5476 /* The Ex command failed, do not execute the operator. */
5477 clearop(cap->oap);
5478 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5480 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005481 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5482 || did_emsg
5483 ))
5484 /* The start of the operator has become invalid by the Ex command.
5485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 clearopbeep(cap->oap);
5487 }
5488}
5489
5490/*
5491 * Handle CTRL-G command.
5492 */
5493 static void
5494nv_ctrlg(cap)
5495 cmdarg_T *cap;
5496{
5497#ifdef FEAT_VISUAL
5498 if (VIsual_active) /* toggle Selection/Visual mode */
5499 {
5500 VIsual_select = !VIsual_select;
5501 showmode();
5502 }
5503 else
5504#endif
5505 if (!checkclearop(cap->oap))
5506 /* print full name if count given or :cd used */
5507 fileinfo((int)cap->count0, FALSE, TRUE);
5508}
5509
5510/*
5511 * Handle CTRL-H <Backspace> command.
5512 */
5513 static void
5514nv_ctrlh(cap)
5515 cmdarg_T *cap;
5516{
5517#ifdef FEAT_VISUAL
5518 if (VIsual_active && VIsual_select)
5519 {
5520 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5521 v_visop(cap);
5522 }
5523 else
5524#endif
5525 nv_left(cap);
5526}
5527
5528/*
5529 * CTRL-L: clear screen and redraw.
5530 */
5531 static void
5532nv_clear(cap)
5533 cmdarg_T *cap;
5534{
5535 if (!checkclearop(cap->oap))
5536 {
5537#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5538 /*
5539 * Right now, the BeBox doesn't seem to have an easy way to detect
5540 * window resizing, so we cheat and make the user detect it
5541 * manually with CTRL-L instead
5542 */
5543 ui_get_shellsize();
5544#endif
5545#ifdef FEAT_SYN_HL
5546 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005547 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548#endif
5549 redraw_later(CLEAR);
5550 }
5551}
5552
5553/*
5554 * CTRL-O: In Select mode: switch to Visual mode for one command.
5555 * Otherwise: Go to older pcmark.
5556 */
5557 static void
5558nv_ctrlo(cap)
5559 cmdarg_T *cap;
5560{
5561#ifdef FEAT_VISUAL
5562 if (VIsual_active && VIsual_select)
5563 {
5564 VIsual_select = FALSE;
5565 showmode();
5566 restart_VIsual_select = 2; /* restart Select mode later */
5567 }
5568 else
5569#endif
5570 {
5571 cap->count1 = -cap->count1;
5572 nv_pcmark(cap);
5573 }
5574}
5575
5576/*
5577 * CTRL-^ command, short for ":e #"
5578 */
5579 static void
5580nv_hat(cap)
5581 cmdarg_T *cap;
5582{
5583 if (!checkclearopq(cap->oap))
5584 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5585 GETF_SETMARK|GETF_ALT, FALSE);
5586}
5587
5588/*
5589 * "Z" commands.
5590 */
5591 static void
5592nv_Zet(cap)
5593 cmdarg_T *cap;
5594{
5595 if (!checkclearopq(cap->oap))
5596 {
5597 switch (cap->nchar)
5598 {
5599 /* "ZZ": equivalent to ":x". */
5600 case 'Z': do_cmdline_cmd((char_u *)"x");
5601 break;
5602
5603 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5604 case 'Q': do_cmdline_cmd((char_u *)"q!");
5605 break;
5606
5607 default: clearopbeep(cap->oap);
5608 }
5609 }
5610}
5611
5612#if defined(FEAT_WINDOWS) || defined(PROTO)
5613/*
5614 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5615 */
5616 void
5617do_nv_ident(c1, c2)
5618 int c1;
5619 int c2;
5620{
5621 oparg_T oa;
5622 cmdarg_T ca;
5623
5624 clear_oparg(&oa);
5625 vim_memset(&ca, 0, sizeof(ca));
5626 ca.oap = &oa;
5627 ca.cmdchar = c1;
5628 ca.nchar = c2;
5629 nv_ident(&ca);
5630}
5631#endif
5632
5633/*
5634 * Handle the commands that use the word under the cursor.
5635 * [g] CTRL-] :ta to current identifier
5636 * [g] 'K' run program for current identifier
5637 * [g] '*' / to current identifier or string
5638 * [g] '#' ? to current identifier or string
5639 * g ']' :tselect for current identifier
5640 */
5641 static void
5642nv_ident(cap)
5643 cmdarg_T *cap;
5644{
5645 char_u *ptr = NULL;
5646 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005647 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 char_u *p;
5649 char_u *kp; /* value of 'keywordprg' */
5650 int kp_help; /* 'keywordprg' is ":help" */
5651 int n = 0; /* init for GCC */
5652 int cmdchar;
5653 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005654 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 char_u *aux_ptr;
5656 int isman;
5657 int isman_s;
5658
5659 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5660 {
5661 cmdchar = cap->nchar;
5662 g_cmd = TRUE;
5663 }
5664 else
5665 {
5666 cmdchar = cap->cmdchar;
5667 g_cmd = FALSE;
5668 }
5669
5670 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5671 cmdchar = '#';
5672
5673 /*
5674 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5675 */
5676 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5677 {
5678#ifdef FEAT_VISUAL
5679 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5680 return;
5681#endif
5682 if (checkclearopq(cap->oap))
5683 return;
5684 }
5685
5686 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5687 (cmdchar == '*' || cmdchar == '#')
5688 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5689 {
5690 clearop(cap->oap);
5691 return;
5692 }
5693
5694 /* Allocate buffer to put the command in. Inserting backslashes can
5695 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5696 * and some numbers. */
5697 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5698 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5699 || STRCMP(kp, ":help") == 0);
5700 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5701 if (buf == NULL)
5702 return;
5703 buf[0] = NUL;
5704
5705 switch (cmdchar)
5706 {
5707 case '*':
5708 case '#':
5709 /*
5710 * Put cursor at start of word, makes search skip the word
5711 * under the cursor.
5712 * Call setpcmark() first, so "*``" puts the cursor back where
5713 * it was.
5714 */
5715 setpcmark();
5716 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5717
5718 if (!g_cmd && vim_iswordp(ptr))
5719 STRCPY(buf, "\\<");
5720 no_smartcase = TRUE; /* don't use 'smartcase' now */
5721 break;
5722
5723 case 'K':
5724 if (kp_help)
5725 STRCPY(buf, "he! ");
5726 else
5727 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005728 /* An external command will probably use an argument starting
5729 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005730 while (*ptr == '-' && n > 0)
5731 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005732 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005733 --n;
5734 }
5735 if (n == 0)
5736 {
5737 EMSG(_(e_noident)); /* found dashes only */
5738 vim_free(buf);
5739 return;
5740 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 /* When a count is given, turn it into a range. Is this
5743 * really what we want? */
5744 isman = (STRCMP(kp, "man") == 0);
5745 isman_s = (STRCMP(kp, "man -s") == 0);
5746 if (cap->count0 != 0 && !(isman || isman_s))
5747 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5748
5749 STRCAT(buf, "! ");
5750 if (cap->count0 == 0 && isman_s)
5751 STRCAT(buf, "man");
5752 else
5753 STRCAT(buf, kp);
5754 STRCAT(buf, " ");
5755 if (cap->count0 != 0 && (isman || isman_s))
5756 {
5757 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5758 STRCAT(buf, " ");
5759 }
5760 }
5761 break;
5762
5763 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005764 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#ifdef FEAT_CSCOPE
5766 if (p_cst)
5767 STRCPY(buf, "cstag ");
5768 else
5769#endif
5770 STRCPY(buf, "ts ");
5771 break;
5772
5773 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005774 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (curbuf->b_help)
5776 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005778 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005779 if (g_cmd)
5780 STRCPY(buf, "tj ");
5781 else
5782 sprintf((char *)buf, "%ldta ", cap->count0);
5783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785
5786 /*
5787 * Now grab the chars in the identifier
5788 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005789 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005791 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005792 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005793 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005794 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005795 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005797 vim_free(buf);
5798 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005800 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5801 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005802 {
5803 vim_free(buf);
5804 vim_free(p);
5805 return;
5806 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005807 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005808 STRCAT(buf, p);
5809 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005811 else
5812 {
5813 if (cmdchar == '*')
5814 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5815 else if (cmdchar == '#')
5816 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005817 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005818 {
5819 if (curbuf->b_help)
5820 /* ":help" handles unescaped argument */
5821 aux_ptr = (char_u *)"";
5822 else
5823 aux_ptr = (char_u *)"\\|\"\n[";
5824 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005825 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005826 aux_ptr = (char_u *)"\\|\"\n*?[";
5827
5828 p = buf + STRLEN(buf);
5829 while (n-- > 0)
5830 {
5831 /* put a backslash before \ and some others */
5832 if (vim_strchr(aux_ptr, *ptr) != NULL)
5833 *p++ = '\\';
5834#ifdef FEAT_MBYTE
5835 /* When current byte is a part of multibyte character, copy all
5836 * bytes of that character. */
5837 if (has_mbyte)
5838 {
5839 int i;
5840 int len = (*mb_ptr2len)(ptr) - 1;
5841
5842 for (i = 0; i < len && n >= 1; ++i, --n)
5843 *p++ = *ptr++;
5844 }
5845#endif
5846 *p++ = *ptr++;
5847 }
5848 *p = NUL;
5849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 /*
5852 * Execute the command.
5853 */
5854 if (cmdchar == '*' || cmdchar == '#')
5855 {
5856 if (!g_cmd && (
5857#ifdef FEAT_MBYTE
5858 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5859#endif
5860 vim_iswordc(ptr[-1])))
5861 STRCAT(buf, "\\>");
5862#ifdef FEAT_CMDHIST
5863 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005864 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5866#endif
5867 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5868 }
5869 else
5870 do_cmdline_cmd(buf);
5871
5872 vim_free(buf);
5873}
5874
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005875#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876/*
5877 * Get visually selected text, within one line only.
5878 * Returns FAIL if more than one line selected.
5879 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005880 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881get_visual_text(cap, pp, lenp)
5882 cmdarg_T *cap;
5883 char_u **pp; /* return: start of selected text */
5884 int *lenp; /* return: length of selected text */
5885{
5886 if (VIsual_mode != 'V')
5887 unadjust_for_sel();
5888 if (VIsual.lnum != curwin->w_cursor.lnum)
5889 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005890 if (cap != NULL)
5891 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 return FAIL;
5893 }
5894 if (VIsual_mode == 'V')
5895 {
5896 *pp = ml_get_curline();
5897 *lenp = (int)STRLEN(*pp);
5898 }
5899 else
5900 {
5901 if (lt(curwin->w_cursor, VIsual))
5902 {
5903 *pp = ml_get_pos(&curwin->w_cursor);
5904 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5905 }
5906 else
5907 {
5908 *pp = ml_get_pos(&VIsual);
5909 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5910 }
5911#ifdef FEAT_MBYTE
5912 if (has_mbyte)
5913 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005914 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#endif
5916 }
5917 reset_VIsual_and_resel();
5918 return OK;
5919}
5920#endif
5921
5922/*
5923 * CTRL-T: backwards in tag stack
5924 */
5925 static void
5926nv_tagpop(cap)
5927 cmdarg_T *cap;
5928{
5929 if (!checkclearopq(cap->oap))
5930 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5931}
5932
5933/*
5934 * Handle scrolling command 'H', 'L' and 'M'.
5935 */
5936 static void
5937nv_scroll(cap)
5938 cmdarg_T *cap;
5939{
5940 int used = 0;
5941 long n;
5942#ifdef FEAT_FOLDING
5943 linenr_T lnum;
5944#endif
5945 int half;
5946
5947 cap->oap->motion_type = MLINE;
5948 setpcmark();
5949
5950 if (cap->cmdchar == 'L')
5951 {
5952 validate_botline(); /* make sure curwin->w_botline is valid */
5953 curwin->w_cursor.lnum = curwin->w_botline - 1;
5954 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5955 curwin->w_cursor.lnum = 1;
5956 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005957 {
5958#ifdef FEAT_FOLDING
5959 if (hasAnyFolding(curwin))
5960 {
5961 /* Count a fold for one screen line. */
5962 for (n = cap->count1 - 1; n > 0
5963 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5964 {
5965 (void)hasFolding(curwin->w_cursor.lnum,
5966 &curwin->w_cursor.lnum, NULL);
5967 --curwin->w_cursor.lnum;
5968 }
5969 }
5970 else
5971#endif
5972 curwin->w_cursor.lnum -= cap->count1 - 1;
5973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 }
5975 else
5976 {
5977 if (cap->cmdchar == 'M')
5978 {
5979#ifdef FEAT_DIFF
5980 /* Don't count filler lines above the window. */
5981 used -= diff_check_fill(curwin, curwin->w_topline)
5982 - curwin->w_topfill;
5983#endif
5984 validate_botline(); /* make sure w_empty_rows is valid */
5985 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5986 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5987 {
5988#ifdef FEAT_DIFF
5989 /* Count half he number of filler lines to be "below this
5990 * line" and half to be "above the next line". */
5991 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5992 + n) / 2 >= half)
5993 {
5994 --n;
5995 break;
5996 }
5997#endif
5998 used += plines(curwin->w_topline + n);
5999 if (used >= half)
6000 break;
6001#ifdef FEAT_FOLDING
6002 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
6003 n = lnum - curwin->w_topline;
6004#endif
6005 }
6006 if (n > 0 && used > curwin->w_height)
6007 --n;
6008 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006009 else /* (cap->cmdchar == 'H') */
6010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006012#ifdef FEAT_FOLDING
6013 if (hasAnyFolding(curwin))
6014 {
6015 /* Count a fold for one screen line. */
6016 lnum = curwin->w_topline;
6017 while (n-- > 0 && lnum < curwin->w_botline - 1)
6018 {
6019 hasFolding(lnum, NULL, &lnum);
6020 ++lnum;
6021 }
6022 n = lnum - curwin->w_topline;
6023 }
6024#endif
6025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 curwin->w_cursor.lnum = curwin->w_topline + n;
6027 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6028 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6029 }
6030
6031 cursor_correct(); /* correct for 'so' */
6032 beginline(BL_SOL | BL_FIX);
6033}
6034
6035/*
6036 * Cursor right commands.
6037 */
6038 static void
6039nv_right(cap)
6040 cmdarg_T *cap;
6041{
6042 long n;
6043#ifdef FEAT_VISUAL
6044 int PAST_LINE;
6045#else
6046# define PAST_LINE 0
6047#endif
6048
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006049 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6050 {
6051 /* <C-Right> and <S-Right> move a word or WORD right */
6052 if (mod_mask & MOD_MASK_CTRL)
6053 cap->arg = TRUE;
6054 nv_wordcmd(cap);
6055 return;
6056 }
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 cap->oap->motion_type = MCHAR;
6059 cap->oap->inclusive = FALSE;
6060#ifdef FEAT_VISUAL
6061 PAST_LINE = (VIsual_active && *p_sel != 'o');
6062
6063# ifdef FEAT_VIRTUALEDIT
6064 /*
6065 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006066 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 */
6068 if (virtual_active())
6069 PAST_LINE = 0;
6070# endif
6071#endif
6072
6073 for (n = cap->count1; n > 0; --n)
6074 {
6075 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006076#ifdef FEAT_VISUAL
6077 || (PAST_LINE && *ml_get_cursor() == NUL)
6078#endif
6079 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 {
6081 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006082 * <Space> wraps to next line if 'whichwrap' has 's'.
6083 * 'l' wraps to next line if 'whichwrap' has 'l'.
6084 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
6086 if ( ((cap->cmdchar == ' '
6087 && vim_strchr(p_ww, 's') != NULL)
6088 || (cap->cmdchar == 'l'
6089 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006090 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 && vim_strchr(p_ww, '>') != NULL))
6092 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6093 {
6094 /* When deleting we also count the NL as a character.
6095 * Set cap->oap->inclusive when last char in the line is
6096 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006097 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 && !cap->oap->inclusive
6099 && !lineempty(curwin->w_cursor.lnum))
6100 cap->oap->inclusive = TRUE;
6101 else
6102 {
6103 ++curwin->w_cursor.lnum;
6104 curwin->w_cursor.col = 0;
6105#ifdef FEAT_VIRTUALEDIT
6106 curwin->w_cursor.coladd = 0;
6107#endif
6108 curwin->w_set_curswant = TRUE;
6109 cap->oap->inclusive = FALSE;
6110 }
6111 continue;
6112 }
6113 if (cap->oap->op_type == OP_NOP)
6114 {
6115 /* Only beep and flush if not moved at all */
6116 if (n == cap->count1)
6117 beep_flush();
6118 }
6119 else
6120 {
6121 if (!lineempty(curwin->w_cursor.lnum))
6122 cap->oap->inclusive = TRUE;
6123 }
6124 break;
6125 }
6126#ifdef FEAT_VISUAL
6127 else if (PAST_LINE)
6128 {
6129 curwin->w_set_curswant = TRUE;
6130# ifdef FEAT_VIRTUALEDIT
6131 if (virtual_active())
6132 oneright();
6133 else
6134# endif
6135 {
6136# ifdef FEAT_MBYTE
6137 if (has_mbyte)
6138 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006139 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 else
6141# endif
6142 ++curwin->w_cursor.col;
6143 }
6144 }
6145#endif
6146 }
6147#ifdef FEAT_FOLDING
6148 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6149 && cap->oap->op_type == OP_NOP)
6150 foldOpenCursor();
6151#endif
6152}
6153
6154/*
6155 * Cursor left commands.
6156 *
6157 * Returns TRUE when operator end should not be adjusted.
6158 */
6159 static void
6160nv_left(cap)
6161 cmdarg_T *cap;
6162{
6163 long n;
6164
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006165 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6166 {
6167 /* <C-Left> and <S-Left> move a word or WORD left */
6168 if (mod_mask & MOD_MASK_CTRL)
6169 cap->arg = 1;
6170 nv_bck_word(cap);
6171 return;
6172 }
6173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 cap->oap->motion_type = MCHAR;
6175 cap->oap->inclusive = FALSE;
6176 for (n = cap->count1; n > 0; --n)
6177 {
6178 if (oneleft() == FAIL)
6179 {
6180 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6181 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6182 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6183 */
6184 if ( (((cap->cmdchar == K_BS
6185 || cap->cmdchar == Ctrl_H)
6186 && vim_strchr(p_ww, 'b') != NULL)
6187 || (cap->cmdchar == 'h'
6188 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006189 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 && vim_strchr(p_ww, '<') != NULL))
6191 && curwin->w_cursor.lnum > 1)
6192 {
6193 --(curwin->w_cursor.lnum);
6194 coladvance((colnr_T)MAXCOL);
6195 curwin->w_set_curswant = TRUE;
6196
6197 /* When the NL before the first char has to be deleted we
6198 * put the cursor on the NUL after the previous line.
6199 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006200 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if ( (cap->oap->op_type == OP_DELETE
6202 || cap->oap->op_type == OP_CHANGE)
6203 && !lineempty(curwin->w_cursor.lnum))
6204 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006205 if (*ml_get_cursor() != NUL)
6206 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 cap->retval |= CA_NO_ADJ_OP_END;
6208 }
6209 continue;
6210 }
6211 /* Only beep and flush if not moved at all */
6212 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6213 beep_flush();
6214 break;
6215 }
6216 }
6217#ifdef FEAT_FOLDING
6218 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6219 && cap->oap->op_type == OP_NOP)
6220 foldOpenCursor();
6221#endif
6222}
6223
6224/*
6225 * Cursor up commands.
6226 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6227 */
6228 static void
6229nv_up(cap)
6230 cmdarg_T *cap;
6231{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006232 if (mod_mask & MOD_MASK_SHIFT)
6233 {
6234 /* <S-Up> is page up */
6235 cap->arg = BACKWARD;
6236 nv_page(cap);
6237 }
6238 else
6239 {
6240 cap->oap->motion_type = MLINE;
6241 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6242 clearopbeep(cap->oap);
6243 else if (cap->arg)
6244 beginline(BL_WHITE | BL_FIX);
6245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246}
6247
6248/*
6249 * Cursor down commands.
6250 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6251 */
6252 static void
6253nv_down(cap)
6254 cmdarg_T *cap;
6255{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006256 if (mod_mask & MOD_MASK_SHIFT)
6257 {
6258 /* <S-Down> is page down */
6259 cap->arg = FORWARD;
6260 nv_page(cap);
6261 }
6262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6264 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006265 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006266 if (curwin->w_llist_ref == NULL)
6267 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6268 else
6269 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 else
6271#endif
6272 {
6273#ifdef FEAT_CMDWIN
6274 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006275 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 cmdwin_result = CAR;
6277 else
6278#endif
6279 {
6280 cap->oap->motion_type = MLINE;
6281 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6282 clearopbeep(cap->oap);
6283 else if (cap->arg)
6284 beginline(BL_WHITE | BL_FIX);
6285 }
6286 }
6287}
6288
6289#ifdef FEAT_SEARCHPATH
6290/*
6291 * Grab the file name under the cursor and edit it.
6292 */
6293 static void
6294nv_gotofile(cap)
6295 cmdarg_T *cap;
6296{
6297 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006298 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006300 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
6302 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006303 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 return;
6305 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006306#ifdef FEAT_AUTOCMD
6307 if (curbuf_locked())
6308 {
6309 clearop(cap->oap);
6310 return;
6311 }
6312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006314 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 if (ptr != NULL)
6317 {
6318 /* do autowrite if necessary */
6319 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6320 autowrite(curbuf, FALSE);
6321 setpcmark();
6322 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006323 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006324 if (cap->nchar == 'F' && lnum >= 0)
6325 {
6326 curwin->w_cursor.lnum = lnum;
6327 check_cursor_lnum();
6328 beginline(BL_SOL | BL_FIX);
6329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 vim_free(ptr);
6331 }
6332 else
6333 clearop(cap->oap);
6334}
6335#endif
6336
6337/*
6338 * <End> command: to end of current line or last line.
6339 */
6340 static void
6341nv_end(cap)
6342 cmdarg_T *cap;
6343{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006344 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006346 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 nv_goto(cap);
6348 cap->count1 = 1; /* to end of current line */
6349 }
6350 nv_dollar(cap);
6351}
6352
6353/*
6354 * Handle the "$" command.
6355 */
6356 static void
6357nv_dollar(cap)
6358 cmdarg_T *cap;
6359{
6360 cap->oap->motion_type = MCHAR;
6361 cap->oap->inclusive = TRUE;
6362#ifdef FEAT_VIRTUALEDIT
6363 /* In virtual mode when off the edge of a line and an operator
6364 * is pending (whew!) keep the cursor where it is.
6365 * Otherwise, send it to the end of the line. */
6366 if (!virtual_active() || gchar_cursor() != NUL
6367 || cap->oap->op_type == OP_NOP)
6368#endif
6369 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6370 if (cursor_down((long)(cap->count1 - 1),
6371 cap->oap->op_type == OP_NOP) == FAIL)
6372 clearopbeep(cap->oap);
6373#ifdef FEAT_FOLDING
6374 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6375 foldOpenCursor();
6376#endif
6377}
6378
6379/*
6380 * Implementation of '?' and '/' commands.
6381 * If cap->arg is TRUE don't set PC mark.
6382 */
6383 static void
6384nv_search(cap)
6385 cmdarg_T *cap;
6386{
6387 oparg_T *oap = cap->oap;
6388
6389 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6390 {
6391 /* Translate "g??" to "g?g?" */
6392 cap->cmdchar = 'g';
6393 cap->nchar = '?';
6394 nv_operator(cap);
6395 return;
6396 }
6397
6398 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6399
6400 if (cap->searchbuf == NULL)
6401 {
6402 clearop(oap);
6403 return;
6404 }
6405
6406 normal_search(cap, cap->cmdchar, cap->searchbuf,
6407 (cap->arg ? 0 : SEARCH_MARK));
6408}
6409
6410/*
6411 * Handle "N" and "n" commands.
6412 * cap->arg is SEARCH_REV for "N", 0 for "n".
6413 */
6414 static void
6415nv_next(cap)
6416 cmdarg_T *cap;
6417{
6418 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6419}
6420
6421/*
6422 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6423 * Uses only cap->count1 and cap->oap from "cap".
6424 */
6425 static void
6426normal_search(cap, dir, pat, opt)
6427 cmdarg_T *cap;
6428 int dir;
6429 char_u *pat;
6430 int opt; /* extra flags for do_search() */
6431{
6432 int i;
6433
6434 cap->oap->motion_type = MCHAR;
6435 cap->oap->inclusive = FALSE;
6436 cap->oap->use_reg_one = TRUE;
6437 curwin->w_set_curswant = TRUE;
6438
6439 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006440 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (i == 0)
6442 clearop(cap->oap);
6443 else
6444 {
6445 if (i == 2)
6446 cap->oap->motion_type = MLINE;
6447#ifdef FEAT_VIRTUALEDIT
6448 curwin->w_cursor.coladd = 0;
6449#endif
6450#ifdef FEAT_FOLDING
6451 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6452 foldOpenCursor();
6453#endif
6454 }
6455
6456 /* "/$" will put the cursor after the end of the line, may need to
6457 * correct that here */
6458 check_cursor();
6459}
6460
6461/*
6462 * Character search commands.
6463 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6464 * ',' and FALSE for ';'.
6465 * cap->nchar is NUL for ',' and ';' (repeat the search)
6466 */
6467 static void
6468nv_csearch(cap)
6469 cmdarg_T *cap;
6470{
6471 int t_cmd;
6472
6473 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6474 t_cmd = TRUE;
6475 else
6476 t_cmd = FALSE;
6477
6478 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6480 clearopbeep(cap->oap);
6481 else
6482 {
6483 curwin->w_set_curswant = TRUE;
6484#ifdef FEAT_VIRTUALEDIT
6485 /* Include a Tab for "tx" and for "dfx". */
6486 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6487 && (t_cmd || cap->oap->op_type != OP_NOP))
6488 {
6489 colnr_T scol, ecol;
6490
6491 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6492 curwin->w_cursor.coladd = ecol - scol;
6493 }
6494 else
6495 curwin->w_cursor.coladd = 0;
6496#endif
6497#ifdef FEAT_VISUAL
6498 adjust_for_sel(cap);
6499#endif
6500#ifdef FEAT_FOLDING
6501 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6502 foldOpenCursor();
6503#endif
6504 }
6505}
6506
6507/*
6508 * "[" and "]" commands.
6509 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6510 */
6511 static void
6512nv_brackets(cap)
6513 cmdarg_T *cap;
6514{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006515 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 pos_T prev_pos;
6517 pos_T *pos = NULL; /* init for GCC */
6518 pos_T old_pos; /* cursor position before command */
6519 int flag;
6520 long n;
6521 int findc;
6522 int c;
6523
6524 cap->oap->motion_type = MCHAR;
6525 cap->oap->inclusive = FALSE;
6526 old_pos = curwin->w_cursor;
6527#ifdef FEAT_VIRTUALEDIT
6528 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6529#endif
6530
6531#ifdef FEAT_SEARCHPATH
6532 /*
6533 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6534 */
6535 if (cap->nchar == 'f')
6536 nv_gotofile(cap);
6537 else
6538#endif
6539
6540#ifdef FEAT_FIND_ID
6541 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006542 * Find the occurrence(s) of the identifier or define under cursor
6543 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 *
6545 * search list jump
6546 * fwd bwd fwd bwd fwd bwd
6547 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6548 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6549 */
6550 if (vim_strchr((char_u *)
6551#ifdef EBCDIC
6552 "iI\005dD\067",
6553#else
6554 "iI\011dD\004",
6555#endif
6556 cap->nchar) != NULL)
6557 {
6558 char_u *ptr;
6559 int len;
6560
6561 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6562 clearop(cap->oap);
6563 else
6564 {
6565 find_pattern_in_path(ptr, 0, len, TRUE,
6566 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6567 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6568 cap->count1,
6569 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6570 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6571 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6572 (linenr_T)MAXLNUM);
6573 curwin->w_set_curswant = TRUE;
6574 }
6575 }
6576 else
6577#endif
6578
6579 /*
6580 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6581 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6582 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6583 * "[m" or "]m" search for prev/next start of (Java) method.
6584 * "[M" or "]M" search for prev/next end of (Java) method.
6585 */
6586 if ( (cap->cmdchar == '['
6587 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6588 || (cap->cmdchar == ']'
6589 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6590 {
6591 if (cap->nchar == '*')
6592 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 prev_pos.lnum = 0;
6594 if (cap->nchar == 'm' || cap->nchar == 'M')
6595 {
6596 if (cap->cmdchar == '[')
6597 findc = '{';
6598 else
6599 findc = '}';
6600 n = 9999;
6601 }
6602 else
6603 {
6604 findc = cap->nchar;
6605 n = cap->count1;
6606 }
6607 for ( ; n > 0; --n)
6608 {
6609 if ((pos = findmatchlimit(cap->oap, findc,
6610 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6611 {
6612 if (new_pos.lnum == 0) /* nothing found */
6613 {
6614 if (cap->nchar != 'm' && cap->nchar != 'M')
6615 clearopbeep(cap->oap);
6616 }
6617 else
6618 pos = &new_pos; /* use last one found */
6619 break;
6620 }
6621 prev_pos = new_pos;
6622 curwin->w_cursor = *pos;
6623 new_pos = *pos;
6624 }
6625 curwin->w_cursor = old_pos;
6626
6627 /*
6628 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6629 * brought us to the match for "[m" and "]M" when inside a method.
6630 * Try finding the '{' or '}' we want to be at.
6631 * Also repeat for the given count.
6632 */
6633 if (cap->nchar == 'm' || cap->nchar == 'M')
6634 {
6635 /* norm is TRUE for "]M" and "[m" */
6636 int norm = ((findc == '{') == (cap->nchar == 'm'));
6637
6638 n = cap->count1;
6639 /* found a match: we were inside a method */
6640 if (prev_pos.lnum != 0)
6641 {
6642 pos = &prev_pos;
6643 curwin->w_cursor = prev_pos;
6644 if (norm)
6645 --n;
6646 }
6647 else
6648 pos = NULL;
6649 while (n > 0)
6650 {
6651 for (;;)
6652 {
6653 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6654 {
6655 /* if not found anything, that's an error */
6656 if (pos == NULL)
6657 clearopbeep(cap->oap);
6658 n = 0;
6659 break;
6660 }
6661 c = gchar_cursor();
6662 if (c == '{' || c == '}')
6663 {
6664 /* Must have found end/start of class: use it.
6665 * Or found the place to be at. */
6666 if ((c == findc && norm) || (n == 1 && !norm))
6667 {
6668 new_pos = curwin->w_cursor;
6669 pos = &new_pos;
6670 n = 0;
6671 }
6672 /* if no match found at all, we started outside of the
6673 * class and we're inside now. Just go on. */
6674 else if (new_pos.lnum == 0)
6675 {
6676 new_pos = curwin->w_cursor;
6677 pos = &new_pos;
6678 }
6679 /* found start/end of other method: go to match */
6680 else if ((pos = findmatchlimit(cap->oap, findc,
6681 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6682 0)) == NULL)
6683 n = 0;
6684 else
6685 curwin->w_cursor = *pos;
6686 break;
6687 }
6688 }
6689 --n;
6690 }
6691 curwin->w_cursor = old_pos;
6692 if (pos == NULL && new_pos.lnum != 0)
6693 clearopbeep(cap->oap);
6694 }
6695 if (pos != NULL)
6696 {
6697 setpcmark();
6698 curwin->w_cursor = *pos;
6699 curwin->w_set_curswant = TRUE;
6700#ifdef FEAT_FOLDING
6701 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6702 && cap->oap->op_type == OP_NOP)
6703 foldOpenCursor();
6704#endif
6705 }
6706 }
6707
6708 /*
6709 * "[[", "[]", "]]" and "][": move to start or end of function
6710 */
6711 else if (cap->nchar == '[' || cap->nchar == ']')
6712 {
6713 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6714 flag = '{';
6715 else
6716 flag = '}'; /* "][" or "[]" */
6717
6718 curwin->w_set_curswant = TRUE;
6719 /*
6720 * Imitate strange Vi behaviour: When using "]]" with an operator
6721 * we also stop at '}'.
6722 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006723 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 (cap->oap->op_type != OP_NOP
6725 && cap->arg == FORWARD && flag == '{')))
6726 clearopbeep(cap->oap);
6727 else
6728 {
6729 if (cap->oap->op_type == OP_NOP)
6730 beginline(BL_WHITE | BL_FIX);
6731#ifdef FEAT_FOLDING
6732 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6733 foldOpenCursor();
6734#endif
6735 }
6736 }
6737
6738 /*
6739 * "[p", "[P", "]P" and "]p": put with indent adjustment
6740 */
6741 else if (cap->nchar == 'p' || cap->nchar == 'P')
6742 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006743 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {
6745 prep_redo_cmd(cap);
6746 do_put(cap->oap->regname,
6747 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6748 cap->count1, PUT_FIXINDENT);
6749 }
6750 }
6751
6752 /*
6753 * "['", "[`", "]'" and "]`": jump to next mark
6754 */
6755 else if (cap->nchar == '\'' || cap->nchar == '`')
6756 {
6757 pos = &curwin->w_cursor;
6758 for (n = cap->count1; n > 0; --n)
6759 {
6760 prev_pos = *pos;
6761 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6762 cap->nchar == '\'');
6763 if (pos == NULL)
6764 break;
6765 }
6766 if (pos == NULL)
6767 pos = &prev_pos;
6768 nv_cursormark(cap, cap->nchar == '\'', pos);
6769 }
6770
6771#ifdef FEAT_MOUSE
6772 /*
6773 * [ or ] followed by a middle mouse click: put selected text with
6774 * indent adjustment. Any other button just does as usual.
6775 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006776 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 (void)do_mouse(cap->oap, cap->nchar,
6779 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6780 cap->count1, PUT_FIXINDENT);
6781 }
6782#endif /* FEAT_MOUSE */
6783
6784#ifdef FEAT_FOLDING
6785 /*
6786 * "[z" and "]z": move to start or end of open fold.
6787 */
6788 else if (cap->nchar == 'z')
6789 {
6790 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6791 cap->count1) == FAIL)
6792 clearopbeep(cap->oap);
6793 }
6794#endif
6795
6796#ifdef FEAT_DIFF
6797 /*
6798 * "[c" and "]c": move to next or previous diff-change.
6799 */
6800 else if (cap->nchar == 'c')
6801 {
6802 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6803 cap->count1) == FAIL)
6804 clearopbeep(cap->oap);
6805 }
6806#endif
6807
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006808#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006809 /*
6810 * "[s", "[S", "]s" and "]S": move to next spell error.
6811 */
6812 else if (cap->nchar == 's' || cap->nchar == 'S')
6813 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006814 setpcmark();
6815 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006816 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6817 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006818 {
6819 clearopbeep(cap->oap);
6820 break;
6821 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006822# ifdef FEAT_FOLDING
6823 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6824 foldOpenCursor();
6825# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006826 }
6827#endif
6828
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 /* Not a valid cap->nchar. */
6830 else
6831 clearopbeep(cap->oap);
6832}
6833
6834/*
6835 * Handle Normal mode "%" command.
6836 */
6837 static void
6838nv_percent(cap)
6839 cmdarg_T *cap;
6840{
6841 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006842#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 linenr_T lnum = curwin->w_cursor.lnum;
6844#endif
6845
6846 cap->oap->inclusive = TRUE;
6847 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6848 {
6849 if (cap->count0 > 100)
6850 clearopbeep(cap->oap);
6851 else
6852 {
6853 cap->oap->motion_type = MLINE;
6854 setpcmark();
6855 /* Round up, so CTRL-G will give same value. Watch out for a
6856 * large line count, the line number must not go negative! */
6857 if (curbuf->b_ml.ml_line_count > 1000000)
6858 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6859 / 100L * cap->count0;
6860 else
6861 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6862 cap->count0 + 99L) / 100L;
6863 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6864 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6865 beginline(BL_SOL | BL_FIX);
6866 }
6867 }
6868 else /* "%" : go to matching paren */
6869 {
6870 cap->oap->motion_type = MCHAR;
6871 cap->oap->use_reg_one = TRUE;
6872 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6873 clearopbeep(cap->oap);
6874 else
6875 {
6876 setpcmark();
6877 curwin->w_cursor = *pos;
6878 curwin->w_set_curswant = TRUE;
6879#ifdef FEAT_VIRTUALEDIT
6880 curwin->w_cursor.coladd = 0;
6881#endif
6882#ifdef FEAT_VISUAL
6883 adjust_for_sel(cap);
6884#endif
6885 }
6886 }
6887#ifdef FEAT_FOLDING
6888 if (cap->oap->op_type == OP_NOP
6889 && lnum != curwin->w_cursor.lnum
6890 && (fdo_flags & FDO_PERCENT)
6891 && KeyTyped)
6892 foldOpenCursor();
6893#endif
6894}
6895
6896/*
6897 * Handle "(" and ")" commands.
6898 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6899 */
6900 static void
6901nv_brace(cap)
6902 cmdarg_T *cap;
6903{
6904 cap->oap->motion_type = MCHAR;
6905 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006906 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6907 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 curwin->w_set_curswant = TRUE;
6909
6910 if (findsent(cap->arg, cap->count1) == FAIL)
6911 clearopbeep(cap->oap);
6912 else
6913 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006914 /* Don't leave the cursor on the NUL past end of line. */
6915 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916#ifdef FEAT_VIRTUALEDIT
6917 curwin->w_cursor.coladd = 0;
6918#endif
6919#ifdef FEAT_FOLDING
6920 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6921 foldOpenCursor();
6922#endif
6923 }
6924}
6925
6926/*
6927 * "m" command: Mark a position.
6928 */
6929 static void
6930nv_mark(cap)
6931 cmdarg_T *cap;
6932{
6933 if (!checkclearop(cap->oap))
6934 {
6935 if (setmark(cap->nchar) == FAIL)
6936 clearopbeep(cap->oap);
6937 }
6938}
6939
6940/*
6941 * "{" and "}" commands.
6942 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6943 */
6944 static void
6945nv_findpar(cap)
6946 cmdarg_T *cap;
6947{
6948 cap->oap->motion_type = MCHAR;
6949 cap->oap->inclusive = FALSE;
6950 cap->oap->use_reg_one = TRUE;
6951 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006952 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 clearopbeep(cap->oap);
6954 else
6955 {
6956#ifdef FEAT_VIRTUALEDIT
6957 curwin->w_cursor.coladd = 0;
6958#endif
6959#ifdef FEAT_FOLDING
6960 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6961 foldOpenCursor();
6962#endif
6963 }
6964}
6965
6966/*
6967 * "u" command: Undo or make lower case.
6968 */
6969 static void
6970nv_undo(cap)
6971 cmdarg_T *cap;
6972{
6973 if (cap->oap->op_type == OP_LOWER
6974#ifdef FEAT_VISUAL
6975 || VIsual_active
6976#endif
6977 )
6978 {
6979 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6980 cap->cmdchar = 'g';
6981 cap->nchar = 'u';
6982 nv_operator(cap);
6983 }
6984 else
6985 nv_kundo(cap);
6986}
6987
6988/*
6989 * <Undo> command.
6990 */
6991 static void
6992nv_kundo(cap)
6993 cmdarg_T *cap;
6994{
6995 if (!checkclearopq(cap->oap))
6996 {
6997 u_undo((int)cap->count1);
6998 curwin->w_set_curswant = TRUE;
6999 }
7000}
7001
7002/*
7003 * Handle the "r" command.
7004 */
7005 static void
7006nv_replace(cap)
7007 cmdarg_T *cap;
7008{
7009 char_u *ptr;
7010 int had_ctrl_v;
7011 long n;
7012
7013 if (checkclearop(cap->oap))
7014 return;
7015
7016 /* get another character */
7017 if (cap->nchar == Ctrl_V)
7018 {
7019 had_ctrl_v = Ctrl_V;
7020 cap->nchar = get_literal();
7021 /* Don't redo a multibyte character with CTRL-V. */
7022 if (cap->nchar > DEL)
7023 had_ctrl_v = NUL;
7024 }
7025 else
7026 had_ctrl_v = NUL;
7027
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007028 /* Abort if the character is a special key. */
7029 if (IS_SPECIAL(cap->nchar))
7030 {
7031 clearopbeep(cap->oap);
7032 return;
7033 }
7034
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035#ifdef FEAT_VISUAL
7036 /* Visual mode "r" */
7037 if (VIsual_active)
7038 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007039 if (got_int)
7040 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007041 if (had_ctrl_v)
7042 {
7043 if (cap->nchar == '\r')
7044 cap->nchar = -1;
7045 else if (cap->nchar == '\n')
7046 cap->nchar = -2;
7047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 nv_operator(cap);
7049 return;
7050 }
7051#endif
7052
7053#ifdef FEAT_VIRTUALEDIT
7054 /* Break tabs, etc. */
7055 if (virtual_active())
7056 {
7057 if (u_save_cursor() == FAIL)
7058 return;
7059 if (gchar_cursor() == NUL)
7060 {
7061 /* Add extra space and put the cursor on the first one. */
7062 coladvance_force((colnr_T)(getviscol() + cap->count1));
7063 curwin->w_cursor.col -= cap->count1;
7064 }
7065 else if (gchar_cursor() == TAB)
7066 coladvance_force(getviscol());
7067 }
7068#endif
7069
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007070 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007072 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#ifdef FEAT_MBYTE
7074 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7075#endif
7076 )
7077 {
7078 clearopbeep(cap->oap);
7079 return;
7080 }
7081
7082 /*
7083 * Replacing with a TAB is done by edit() when it is complicated because
7084 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7085 * Other characters are done below to avoid problems with things like
7086 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7087 */
7088 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7089 {
7090 stuffnumReadbuff(cap->count1);
7091 stuffcharReadbuff('R');
7092 stuffcharReadbuff('\t');
7093 stuffcharReadbuff(ESC);
7094 return;
7095 }
7096
7097 /* save line for undo */
7098 if (u_save_cursor() == FAIL)
7099 return;
7100
7101 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7102 {
7103 /*
7104 * Replace character(s) by a single newline.
7105 * Strange vi behaviour: Only one newline is inserted.
7106 * Delete the characters here.
7107 * Insert the newline with an insert command, takes care of
7108 * autoindent. The insert command depends on being on the last
7109 * character of a line or not.
7110 */
7111#ifdef FEAT_MBYTE
7112 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7113#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007114 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#endif
7116 stuffcharReadbuff('\r');
7117 stuffcharReadbuff(ESC);
7118
7119 /* Give 'r' to edit(), to get the redo command right. */
7120 invoke_edit(cap, TRUE, 'r', FALSE);
7121 }
7122 else
7123 {
7124 prep_redo(cap->oap->regname, cap->count1,
7125 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7126
7127 curbuf->b_op_start = curwin->w_cursor;
7128#ifdef FEAT_MBYTE
7129 if (has_mbyte)
7130 {
7131 int old_State = State;
7132
7133 if (cap->ncharC1 != 0)
7134 AppendCharToRedobuff(cap->ncharC1);
7135 if (cap->ncharC2 != 0)
7136 AppendCharToRedobuff(cap->ncharC2);
7137
7138 /* This is slow, but it handles replacing a single-byte with a
7139 * multi-byte and the other way around. Also handles adding
7140 * composing characters for utf-8. */
7141 for (n = cap->count1; n > 0; --n)
7142 {
7143 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007144 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7145 {
7146 int c = ins_copychar(curwin->w_cursor.lnum
7147 + (cap->nchar == Ctrl_Y ? -1 : 1));
7148 if (c != NUL)
7149 ins_char(c);
7150 else
7151 /* will be decremented further down */
7152 ++curwin->w_cursor.col;
7153 }
7154 else
7155 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 State = old_State;
7157 if (cap->ncharC1 != 0)
7158 ins_char(cap->ncharC1);
7159 if (cap->ncharC2 != 0)
7160 ins_char(cap->ncharC2);
7161 }
7162 }
7163 else
7164#endif
7165 {
7166 /*
7167 * Replace the characters within one line.
7168 */
7169 for (n = cap->count1; n > 0; --n)
7170 {
7171 /*
7172 * Get ptr again, because u_save and/or showmatch() will have
7173 * released the line. At the same time we let know that the
7174 * line will be changed.
7175 */
7176 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007177 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7178 {
7179 int c = ins_copychar(curwin->w_cursor.lnum
7180 + (cap->nchar == Ctrl_Y ? -1 : 1));
7181 if (c != NUL)
7182 ptr[curwin->w_cursor.col] = c;
7183 }
7184 else
7185 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (p_sm && msg_silent == 0)
7187 showmatch(cap->nchar);
7188 ++curwin->w_cursor.col;
7189 }
7190#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007191 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007193 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar009b2592004-10-24 19:18:58 +00007195 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007196 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007198 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 }
7200#endif
7201
7202 /* mark the buffer as changed and prepare for displaying */
7203 changed_bytes(curwin->w_cursor.lnum,
7204 (colnr_T)(curwin->w_cursor.col - cap->count1));
7205 }
7206 --curwin->w_cursor.col; /* cursor on the last replaced char */
7207#ifdef FEAT_MBYTE
7208 /* if the character on the left of the current cursor is a multi-byte
7209 * character, move two characters left */
7210 if (has_mbyte)
7211 mb_adjust_cursor();
7212#endif
7213 curbuf->b_op_end = curwin->w_cursor;
7214 curwin->w_set_curswant = TRUE;
7215 set_last_insert(cap->nchar);
7216 }
7217}
7218
7219#ifdef FEAT_VISUAL
7220/*
7221 * 'o': Exchange start and end of Visual area.
7222 * 'O': same, but in block mode exchange left and right corners.
7223 */
7224 static void
7225v_swap_corners(cmdchar)
7226 int cmdchar;
7227{
7228 pos_T old_cursor;
7229 colnr_T left, right;
7230
7231 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7232 {
7233 old_cursor = curwin->w_cursor;
7234 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7235 curwin->w_cursor.lnum = VIsual.lnum;
7236 coladvance(left);
7237 VIsual = curwin->w_cursor;
7238
7239 curwin->w_cursor.lnum = old_cursor.lnum;
7240 curwin->w_curswant = right;
7241 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7242 * right one column */
7243 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7244 ++curwin->w_curswant;
7245 coladvance(curwin->w_curswant);
7246 if (curwin->w_cursor.col == old_cursor.col
7247#ifdef FEAT_VIRTUALEDIT
7248 && (!virtual_active()
7249 || curwin->w_cursor.coladd == old_cursor.coladd)
7250#endif
7251 )
7252 {
7253 curwin->w_cursor.lnum = VIsual.lnum;
7254 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7255 ++right;
7256 coladvance(right);
7257 VIsual = curwin->w_cursor;
7258
7259 curwin->w_cursor.lnum = old_cursor.lnum;
7260 coladvance(left);
7261 curwin->w_curswant = left;
7262 }
7263 }
7264 else
7265 {
7266 old_cursor = curwin->w_cursor;
7267 curwin->w_cursor = VIsual;
7268 VIsual = old_cursor;
7269 curwin->w_set_curswant = TRUE;
7270 }
7271}
7272#endif /* FEAT_VISUAL */
7273
7274/*
7275 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7276 */
7277 static void
7278nv_Replace(cap)
7279 cmdarg_T *cap;
7280{
7281#ifdef FEAT_VISUAL
7282 if (VIsual_active) /* "R" is replace lines */
7283 {
7284 cap->cmdchar = 'c';
7285 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007286 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 VIsual_mode = 'V';
7288 nv_operator(cap);
7289 }
7290 else
7291#endif
7292 if (!checkclearopq(cap->oap))
7293 {
7294 if (!curbuf->b_p_ma)
7295 EMSG(_(e_modifiable));
7296 else
7297 {
7298#ifdef FEAT_VIRTUALEDIT
7299 if (virtual_active())
7300 coladvance(getviscol());
7301#endif
7302 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7303 }
7304 }
7305}
7306
7307#ifdef FEAT_VREPLACE
7308/*
7309 * "gr".
7310 */
7311 static void
7312nv_vreplace(cap)
7313 cmdarg_T *cap;
7314{
7315# ifdef FEAT_VISUAL
7316 if (VIsual_active)
7317 {
7318 cap->cmdchar = 'r';
7319 cap->nchar = cap->extra_char;
7320 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7321 }
7322 else
7323# endif
7324 if (!checkclearopq(cap->oap))
7325 {
7326 if (!curbuf->b_p_ma)
7327 EMSG(_(e_modifiable));
7328 else
7329 {
7330 if (cap->extra_char == Ctrl_V) /* get another character */
7331 cap->extra_char = get_literal();
7332 stuffcharReadbuff(cap->extra_char);
7333 stuffcharReadbuff(ESC);
7334# ifdef FEAT_VIRTUALEDIT
7335 if (virtual_active())
7336 coladvance(getviscol());
7337# endif
7338 invoke_edit(cap, TRUE, 'v', FALSE);
7339 }
7340 }
7341}
7342#endif
7343
7344/*
7345 * Swap case for "~" command, when it does not work like an operator.
7346 */
7347 static void
7348n_swapchar(cap)
7349 cmdarg_T *cap;
7350{
7351 long n;
7352 pos_T startpos;
7353 int did_change = 0;
7354#ifdef FEAT_NETBEANS_INTG
7355 pos_T pos;
7356 char_u *ptr;
7357 int count;
7358#endif
7359
7360 if (checkclearopq(cap->oap))
7361 return;
7362
7363 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7364 {
7365 clearopbeep(cap->oap);
7366 return;
7367 }
7368
7369 prep_redo_cmd(cap);
7370
7371 if (u_save_cursor() == FAIL)
7372 return;
7373
7374 startpos = curwin->w_cursor;
7375#ifdef FEAT_NETBEANS_INTG
7376 pos = startpos;
7377#endif
7378 for (n = cap->count1; n > 0; --n)
7379 {
7380 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7381 inc_cursor();
7382 if (gchar_cursor() == NUL)
7383 {
7384 if (vim_strchr(p_ww, '~') != NULL
7385 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7386 {
7387#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007388 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
7390 if (did_change)
7391 {
7392 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007393 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007394 netbeans_removed(curbuf, pos.lnum, pos.col,
7395 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007397 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399 pos.col = 0;
7400 pos.lnum++;
7401 }
7402#endif
7403 ++curwin->w_cursor.lnum;
7404 curwin->w_cursor.col = 0;
7405 if (n > 1)
7406 {
7407 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7408 break;
7409 u_clearline();
7410 }
7411 }
7412 else
7413 break;
7414 }
7415 }
7416#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007417 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
7419 ptr = ml_get(pos.lnum);
7420 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007421 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7422 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424#endif
7425
7426
7427 check_cursor();
7428 curwin->w_set_curswant = TRUE;
7429 if (did_change)
7430 {
7431 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7432 0L);
7433 curbuf->b_op_start = startpos;
7434 curbuf->b_op_end = curwin->w_cursor;
7435 if (curbuf->b_op_end.col > 0)
7436 --curbuf->b_op_end.col;
7437 }
7438}
7439
7440/*
7441 * Move cursor to mark.
7442 */
7443 static void
7444nv_cursormark(cap, flag, pos)
7445 cmdarg_T *cap;
7446 int flag;
7447 pos_T *pos;
7448{
7449 if (check_mark(pos) == FAIL)
7450 clearop(cap->oap);
7451 else
7452 {
7453 if (cap->cmdchar == '\''
7454 || cap->cmdchar == '`'
7455 || cap->cmdchar == '['
7456 || cap->cmdchar == ']')
7457 setpcmark();
7458 curwin->w_cursor = *pos;
7459 if (flag)
7460 beginline(BL_WHITE | BL_FIX);
7461 else
7462 check_cursor();
7463 }
7464 cap->oap->motion_type = flag ? MLINE : MCHAR;
7465 if (cap->cmdchar == '`')
7466 cap->oap->use_reg_one = TRUE;
7467 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7468 curwin->w_set_curswant = TRUE;
7469}
7470
7471#ifdef FEAT_VISUAL
7472/*
7473 * Handle commands that are operators in Visual mode.
7474 */
7475 static void
7476v_visop(cap)
7477 cmdarg_T *cap;
7478{
7479 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7480
7481 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007482 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (isupper(cap->cmdchar))
7484 {
7485 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007486 {
7487 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7491 curwin->w_curswant = MAXCOL;
7492 }
7493 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7494 nv_operator(cap);
7495}
7496#endif
7497
7498/*
7499 * "s" and "S" commands.
7500 */
7501 static void
7502nv_subst(cap)
7503 cmdarg_T *cap;
7504{
7505#ifdef FEAT_VISUAL
7506 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7507 {
7508 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007509 {
7510 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 cap->cmdchar = 'c';
7514 nv_operator(cap);
7515 }
7516 else
7517#endif
7518 nv_optrans(cap);
7519}
7520
7521/*
7522 * Abbreviated commands.
7523 */
7524 static void
7525nv_abbrev(cap)
7526 cmdarg_T *cap;
7527{
7528 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7529 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7530
7531#ifdef FEAT_VISUAL
7532 /* in Visual mode these commands are operators */
7533 if (VIsual_active)
7534 v_visop(cap);
7535 else
7536#endif
7537 nv_optrans(cap);
7538}
7539
7540/*
7541 * Translate a command into another command.
7542 */
7543 static void
7544nv_optrans(cap)
7545 cmdarg_T *cap;
7546{
7547 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7548 (char_u *)"d$", (char_u *)"c$",
7549 (char_u *)"cl", (char_u *)"cc",
7550 (char_u *)"yy", (char_u *)":s\r"};
7551 static char_u *str = (char_u *)"xXDCsSY&";
7552
7553 if (!checkclearopq(cap->oap))
7554 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007555 /* In Vi "2D" doesn't delete the next line. Can't translate it
7556 * either, because "2." should also not use the count. */
7557 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7558 {
7559 cap->oap->start = curwin->w_cursor;
7560 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007561#ifdef FEAT_EVAL
7562 set_op_var(OP_DELETE);
7563#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007564 cap->count1 = 1;
7565 nv_dollar(cap);
7566 finish_op = TRUE;
7567 ResetRedobuff();
7568 AppendCharToRedobuff('D');
7569 }
7570 else
7571 {
7572 if (cap->count0)
7573 stuffnumReadbuff(cap->count0);
7574 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 cap->opcount = 0;
7578}
7579
7580/*
7581 * "'" and "`" commands. Also for "g'" and "g`".
7582 * cap->arg is TRUE for "'" and "g'".
7583 */
7584 static void
7585nv_gomark(cap)
7586 cmdarg_T *cap;
7587{
7588 pos_T *pos;
7589 int c;
7590#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007591 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7593#endif
7594
7595 if (cap->cmdchar == 'g')
7596 c = cap->extra_char;
7597 else
7598 c = cap->nchar;
7599 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7600 if (pos == (pos_T *)-1) /* jumped to other file */
7601 {
7602 if (cap->arg)
7603 {
7604 check_cursor_lnum();
7605 beginline(BL_WHITE | BL_FIX);
7606 }
7607 else
7608 check_cursor();
7609 }
7610 else
7611 nv_cursormark(cap, cap->arg, pos);
7612
7613#ifdef FEAT_VIRTUALEDIT
7614 /* May need to clear the coladd that a mark includes. */
7615 if (!virtual_active())
7616 curwin->w_cursor.coladd = 0;
7617#endif
7618#ifdef FEAT_FOLDING
7619 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007620 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007621 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 && (fdo_flags & FDO_MARK)
7623 && old_KeyTyped)
7624 foldOpenCursor();
7625#endif
7626}
7627
7628/*
7629 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7630 */
7631 static void
7632nv_pcmark(cap)
7633 cmdarg_T *cap;
7634{
7635#ifdef FEAT_JUMPLIST
7636 pos_T *pos;
7637# ifdef FEAT_FOLDING
7638 linenr_T lnum = curwin->w_cursor.lnum;
7639 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7640# endif
7641
7642 if (!checkclearopq(cap->oap))
7643 {
7644 if (cap->cmdchar == 'g')
7645 pos = movechangelist((int)cap->count1);
7646 else
7647 pos = movemark((int)cap->count1);
7648 if (pos == (pos_T *)-1) /* jump to other file */
7649 {
7650 curwin->w_set_curswant = TRUE;
7651 check_cursor();
7652 }
7653 else if (pos != NULL) /* can jump */
7654 nv_cursormark(cap, FALSE, pos);
7655 else if (cap->cmdchar == 'g')
7656 {
7657 if (curbuf->b_changelistlen == 0)
7658 EMSG(_("E664: changelist is empty"));
7659 else if (cap->count1 < 0)
7660 EMSG(_("E662: At start of changelist"));
7661 else
7662 EMSG(_("E663: At end of changelist"));
7663 }
7664 else
7665 clearopbeep(cap->oap);
7666# ifdef FEAT_FOLDING
7667 if (cap->oap->op_type == OP_NOP
7668 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7669 && (fdo_flags & FDO_MARK)
7670 && old_KeyTyped)
7671 foldOpenCursor();
7672# endif
7673 }
7674#else
7675 clearopbeep(cap->oap);
7676#endif
7677}
7678
7679/*
7680 * Handle '"' command.
7681 */
7682 static void
7683nv_regname(cap)
7684 cmdarg_T *cap;
7685{
7686 if (checkclearop(cap->oap))
7687 return;
7688#ifdef FEAT_EVAL
7689 if (cap->nchar == '=')
7690 cap->nchar = get_expr_register();
7691#endif
7692 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7693 {
7694 cap->oap->regname = cap->nchar;
7695 cap->opcount = cap->count0; /* remember count before '"' */
7696#ifdef FEAT_EVAL
7697 set_reg_var(cap->oap->regname);
7698#endif
7699 }
7700 else
7701 clearopbeep(cap->oap);
7702}
7703
7704#ifdef FEAT_VISUAL
7705/*
7706 * Handle "v", "V" and "CTRL-V" commands.
7707 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7708 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007709 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 static void
7712nv_visual(cap)
7713 cmdarg_T *cap;
7714{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007715 if (cap->cmdchar == Ctrl_Q)
7716 cap->cmdchar = Ctrl_V;
7717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7719 * characterwise, linewise, or blockwise. */
7720 if (cap->oap->op_type != OP_NOP)
7721 {
7722 cap->oap->motion_force = cap->cmdchar;
7723 finish_op = FALSE; /* operator doesn't finish now but later */
7724 return;
7725 }
7726
7727 VIsual_select = cap->arg;
7728 if (VIsual_active) /* change Visual mode */
7729 {
7730 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7731 end_visual_mode();
7732 else /* toggle char/block mode */
7733 { /* or char/line mode */
7734 VIsual_mode = cap->cmdchar;
7735 showmode();
7736 }
7737 redraw_curbuf_later(INVERTED); /* update the inversion */
7738 }
7739 else /* start Visual mode */
7740 {
7741 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007742 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007744 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 VIsual = curwin->w_cursor;
7746
7747 VIsual_active = TRUE;
7748 VIsual_reselect = TRUE;
7749 if (!cap->arg)
7750 /* start Select mode when 'selectmode' contains "cmd" */
7751 may_start_select('c');
7752#ifdef FEAT_MOUSE
7753 setmouse();
7754#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007755 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 redraw_cmdline = TRUE; /* show visual mode later */
7757 /*
7758 * For V and ^V, we multiply the number of lines even if there
7759 * was only one -- webb
7760 */
7761 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7762 {
7763 curwin->w_cursor.lnum +=
7764 resel_VIsual_line_count * cap->count0 - 1;
7765 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7766 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7767 }
7768 VIsual_mode = resel_VIsual_mode;
7769 if (VIsual_mode == 'v')
7770 {
7771 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007772 {
7773 validate_virtcol();
7774 curwin->w_curswant = curwin->w_virtcol
7775 + resel_VIsual_vcol * cap->count0 - 1;
7776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007778 curwin->w_curswant = resel_VIsual_vcol;
7779 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007781 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {
7783 curwin->w_curswant = MAXCOL;
7784 coladvance((colnr_T)MAXCOL);
7785 }
7786 else if (VIsual_mode == Ctrl_V)
7787 {
7788 validate_virtcol();
7789 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007790 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 coladvance(curwin->w_curswant);
7792 }
7793 else
7794 curwin->w_set_curswant = TRUE;
7795 redraw_curbuf_later(INVERTED); /* show the inversion */
7796 }
7797 else
7798 {
7799 if (!cap->arg)
7800 /* start Select mode when 'selectmode' contains "cmd" */
7801 may_start_select('c');
7802 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007803 if (VIsual_mode != 'V' && *p_sel == 'e')
7804 ++cap->count1; /* include one more char */
7805 if (cap->count0 > 0 && --cap->count1 > 0)
7806 {
7807 /* With a count select that many characters or lines. */
7808 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7809 nv_right(cap);
7810 else if (VIsual_mode == 'V')
7811 nv_down(cap);
7812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814 }
7815}
7816
7817/*
7818 * Start selection for Shift-movement keys.
7819 */
7820 void
7821start_selection()
7822{
7823 /* if 'selectmode' contains "key", start Select mode */
7824 may_start_select('k');
7825 n_start_visual_mode('v');
7826}
7827
7828/*
7829 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7830 */
7831 void
7832may_start_select(c)
7833 int c;
7834{
7835 VIsual_select = (stuff_empty() && typebuf_typed()
7836 && (vim_strchr(p_slm, c) != NULL));
7837}
7838
7839/*
7840 * Start Visual mode "c".
7841 * Should set VIsual_select before calling this.
7842 */
7843 static void
7844n_start_visual_mode(c)
7845 int c;
7846{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007847#ifdef FEAT_CONCEAL
7848 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007849 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007850#endif
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 VIsual_mode = c;
7853 VIsual_active = TRUE;
7854 VIsual_reselect = TRUE;
7855#ifdef FEAT_VIRTUALEDIT
7856 /* Corner case: the 0 position in a tab may change when going into
7857 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7858 */
7859 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007860 {
7861 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864#endif
7865 VIsual = curwin->w_cursor;
7866
7867#ifdef FEAT_FOLDING
7868 foldAdjustVisual();
7869#endif
7870
7871#ifdef FEAT_MOUSE
7872 setmouse();
7873#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007874#ifdef FEAT_CONCEAL
7875 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007876 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007877#endif
7878
Bram Moolenaar09df3122006-01-23 22:23:09 +00007879 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 redraw_cmdline = TRUE; /* show visual mode later */
7881#ifdef FEAT_CLIPBOARD
7882 /* Make sure the clipboard gets updated. Needed because start and
7883 * end may still be the same, and the selection needs to be owned */
7884 clip_star.vmode = NUL;
7885#endif
7886
7887 /* Only need to redraw this line, unless still need to redraw an old
7888 * Visual area (when 'lazyredraw' is set). */
7889 if (curwin->w_redr_type < INVERTED)
7890 {
7891 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7892 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7893 }
7894}
7895
7896#endif /* FEAT_VISUAL */
7897
7898/*
7899 * CTRL-W: Window commands
7900 */
7901 static void
7902nv_window(cap)
7903 cmdarg_T *cap;
7904{
7905#ifdef FEAT_WINDOWS
7906 if (!checkclearop(cap->oap))
7907 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7908#else
7909 (void)checkclearop(cap->oap);
7910#endif
7911}
7912
7913/*
7914 * CTRL-Z: Suspend
7915 */
7916 static void
7917nv_suspend(cap)
7918 cmdarg_T *cap;
7919{
7920 clearop(cap->oap);
7921#ifdef FEAT_VISUAL
7922 if (VIsual_active)
7923 end_visual_mode(); /* stop Visual mode */
7924#endif
7925 do_cmdline_cmd((char_u *)"st");
7926}
7927
7928/*
7929 * Commands starting with "g".
7930 */
7931 static void
7932nv_g_cmd(cap)
7933 cmdarg_T *cap;
7934{
7935 oparg_T *oap = cap->oap;
7936#ifdef FEAT_VISUAL
7937 pos_T tpos;
7938#endif
7939 int i;
7940 int flag = FALSE;
7941
7942 switch (cap->nchar)
7943 {
7944#ifdef MEM_PROFILE
7945 /*
7946 * "g^A": dump log of used memory.
7947 */
7948 case Ctrl_A:
7949 vim_mem_profile_dump();
7950 break;
7951#endif
7952
7953#ifdef FEAT_VREPLACE
7954 /*
7955 * "gR": Enter virtual replace mode.
7956 */
7957 case 'R':
7958 cap->arg = TRUE;
7959 nv_Replace(cap);
7960 break;
7961
7962 case 'r':
7963 nv_vreplace(cap);
7964 break;
7965#endif
7966
7967 case '&':
7968 do_cmdline_cmd((char_u *)"%s//~/&");
7969 break;
7970
7971#ifdef FEAT_VISUAL
7972 /*
7973 * "gv": Reselect the previous Visual area. If Visual already active,
7974 * exchange previous and current Visual area.
7975 */
7976 case 'v':
7977 if (checkclearop(oap))
7978 break;
7979
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007980 if ( curbuf->b_visual.vi_start.lnum == 0
7981 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7982 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 beep_flush();
7984 else
7985 {
7986 /* set w_cursor to the start of the Visual area, tpos to the end */
7987 if (VIsual_active)
7988 {
7989 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007990 VIsual_mode = curbuf->b_visual.vi_mode;
7991 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992# ifdef FEAT_EVAL
7993 curbuf->b_visual_mode_eval = i;
7994# endif
7995 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007996 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7997 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007999 tpos = curbuf->b_visual.vi_end;
8000 curbuf->b_visual.vi_end = curwin->w_cursor;
8001 curwin->w_cursor = curbuf->b_visual.vi_start;
8002 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 }
8004 else
8005 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008006 VIsual_mode = curbuf->b_visual.vi_mode;
8007 curwin->w_curswant = curbuf->b_visual.vi_curswant;
8008 tpos = curbuf->b_visual.vi_end;
8009 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011
8012 VIsual_active = TRUE;
8013 VIsual_reselect = TRUE;
8014
8015 /* Set Visual to the start and w_cursor to the end of the Visual
8016 * area. Make sure they are on an existing character. */
8017 check_cursor();
8018 VIsual = curwin->w_cursor;
8019 curwin->w_cursor = tpos;
8020 check_cursor();
8021 update_topline();
8022 /*
8023 * When called from normal "g" command: start Select mode when
8024 * 'selectmode' contains "cmd". When called for K_SELECT, always
8025 * start Select mode.
8026 */
8027 if (cap->arg)
8028 VIsual_select = TRUE;
8029 else
8030 may_start_select('c');
8031#ifdef FEAT_MOUSE
8032 setmouse();
8033#endif
8034#ifdef FEAT_CLIPBOARD
8035 /* Make sure the clipboard gets updated. Needed because start and
8036 * end are still the same, and the selection needs to be owned */
8037 clip_star.vmode = NUL;
8038#endif
8039 redraw_curbuf_later(INVERTED);
8040 showmode();
8041 }
8042 break;
8043 /*
8044 * "gV": Don't reselect the previous Visual area after a Select mode
8045 * mapping of menu.
8046 */
8047 case 'V':
8048 VIsual_reselect = FALSE;
8049 break;
8050
8051 /*
8052 * "gh": start Select mode.
8053 * "gH": start Select line mode.
8054 * "g^H": start Select block mode.
8055 */
8056 case K_BS:
8057 cap->nchar = Ctrl_H;
8058 /* FALLTHROUGH */
8059 case 'h':
8060 case 'H':
8061 case Ctrl_H:
8062# ifdef EBCDIC
8063 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8064 if (cap->nchar == Ctrl_H)
8065 cap->cmdchar = Ctrl_V;
8066 else
8067# endif
8068 cap->cmdchar = cap->nchar + ('v' - 'h');
8069 cap->arg = TRUE;
8070 nv_visual(cap);
8071 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008072#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008073
8074 /* "gn", "gN" visually select next/previous search match
8075 * "gn" selects next match
8076 * "gN" selects previous match
8077 */
8078 case 'N':
8079 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008080#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008081 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008082#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008083 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008084 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
8086 /*
8087 * "gj" and "gk" two new funny movement keys -- up and down
8088 * movement based on *screen* line rather than *file* line.
8089 */
8090 case 'j':
8091 case K_DOWN:
8092 /* with 'nowrap' it works just like the normal "j" command; also when
8093 * in a closed fold */
8094 if (!curwin->w_p_wrap
8095#ifdef FEAT_FOLDING
8096 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8097#endif
8098 )
8099 {
8100 oap->motion_type = MLINE;
8101 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8102 }
8103 else
8104 i = nv_screengo(oap, FORWARD, cap->count1);
8105 if (i == FAIL)
8106 clearopbeep(oap);
8107 break;
8108
8109 case 'k':
8110 case K_UP:
8111 /* with 'nowrap' it works just like the normal "k" command; also when
8112 * in a closed fold */
8113 if (!curwin->w_p_wrap
8114#ifdef FEAT_FOLDING
8115 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8116#endif
8117 )
8118 {
8119 oap->motion_type = MLINE;
8120 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8121 }
8122 else
8123 i = nv_screengo(oap, BACKWARD, cap->count1);
8124 if (i == FAIL)
8125 clearopbeep(oap);
8126 break;
8127
8128 /*
8129 * "gJ": join two lines without inserting a space.
8130 */
8131 case 'J':
8132 nv_join(cap);
8133 break;
8134
8135 /*
8136 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8137 * "gm": middle of "g0" and "g$".
8138 */
8139 case '^':
8140 flag = TRUE;
8141 /* FALLTHROUGH */
8142
8143 case '0':
8144 case 'm':
8145 case K_HOME:
8146 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 oap->motion_type = MCHAR;
8148 oap->inclusive = FALSE;
8149 if (curwin->w_p_wrap
8150#ifdef FEAT_VERTSPLIT
8151 && curwin->w_width != 0
8152#endif
8153 )
8154 {
8155 int width1 = W_WIDTH(curwin) - curwin_col_off();
8156 int width2 = width1 + curwin_col_off2();
8157
8158 validate_virtcol();
8159 i = 0;
8160 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8161 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8162 }
8163 else
8164 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008165 /* Go to the middle of the screen line. When 'number' or
8166 * 'relativenumber' is on and lines are wrapping the middle can be more
8167 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (cap->nchar == 'm')
8169 i += (W_WIDTH(curwin) - curwin_col_off()
8170 + ((curwin->w_p_wrap && i > 0)
8171 ? curwin_col_off2() : 0)) / 2;
8172 coladvance((colnr_T)i);
8173 if (flag)
8174 {
8175 do
8176 i = gchar_cursor();
8177 while (vim_iswhite(i) && oneright() == OK);
8178 }
8179 curwin->w_set_curswant = TRUE;
8180 break;
8181
8182 case '_':
8183 /* "g_": to the last non-blank character in the line or <count> lines
8184 * downward. */
8185 cap->oap->motion_type = MCHAR;
8186 cap->oap->inclusive = TRUE;
8187 curwin->w_curswant = MAXCOL;
8188 if (cursor_down((long)(cap->count1 - 1),
8189 cap->oap->op_type == OP_NOP) == FAIL)
8190 clearopbeep(cap->oap);
8191 else
8192 {
8193 char_u *ptr = ml_get_curline();
8194
8195 /* In Visual mode we may end up after the line. */
8196 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8197 --curwin->w_cursor.col;
8198
8199 /* Decrease the cursor column until it's on a non-blank. */
8200 while (curwin->w_cursor.col > 0
8201 && vim_iswhite(ptr[curwin->w_cursor.col]))
8202 --curwin->w_cursor.col;
8203 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008204#ifdef FEAT_VISUAL
8205 adjust_for_sel(cap);
8206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
8208 break;
8209
8210 case '$':
8211 case K_END:
8212 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {
8214 int col_off = curwin_col_off();
8215
8216 oap->motion_type = MCHAR;
8217 oap->inclusive = TRUE;
8218 if (curwin->w_p_wrap
8219#ifdef FEAT_VERTSPLIT
8220 && curwin->w_width != 0
8221#endif
8222 )
8223 {
8224 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8225 if (cap->count1 == 1)
8226 {
8227 int width1 = W_WIDTH(curwin) - col_off;
8228 int width2 = width1 + curwin_col_off2();
8229
8230 validate_virtcol();
8231 i = width1 - 1;
8232 if (curwin->w_virtcol >= (colnr_T)width1)
8233 i += ((curwin->w_virtcol - width1) / width2 + 1)
8234 * width2;
8235 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008236
8237 /* Make sure we stick in this column. */
8238 validate_virtcol();
8239 curwin->w_curswant = curwin->w_virtcol;
8240 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8242 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8243 {
8244 /*
8245 * Check for landing on a character that got split at
8246 * the end of the line. We do not want to advance to
8247 * the next screen line.
8248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (curwin->w_virtcol > (colnr_T)i)
8250 --curwin->w_cursor.col;
8251 }
8252#endif
8253 }
8254 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8255 clearopbeep(oap);
8256 }
8257 else
8258 {
8259 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8260 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008261
8262 /* Make sure we stick in this column. */
8263 validate_virtcol();
8264 curwin->w_curswant = curwin->w_virtcol;
8265 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 }
8267 }
8268 break;
8269
8270 /*
8271 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8272 */
8273 case '*':
8274 case '#':
8275#if POUND != '#'
8276 case POUND: /* pound sign (sometimes equal to '#') */
8277#endif
8278 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8279 case ']': /* :tselect for current identifier */
8280 nv_ident(cap);
8281 break;
8282
8283 /*
8284 * ge and gE: go back to end of word
8285 */
8286 case 'e':
8287 case 'E':
8288 oap->motion_type = MCHAR;
8289 curwin->w_set_curswant = TRUE;
8290 oap->inclusive = TRUE;
8291 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8292 clearopbeep(oap);
8293 break;
8294
8295 /*
8296 * "g CTRL-G": display info about cursor position
8297 */
8298 case Ctrl_G:
8299 cursor_pos_info();
8300 break;
8301
8302 /*
8303 * "gi": start Insert at the last position.
8304 */
8305 case 'i':
8306 if (curbuf->b_last_insert.lnum != 0)
8307 {
8308 curwin->w_cursor = curbuf->b_last_insert;
8309 check_cursor_lnum();
8310 i = (int)STRLEN(ml_get_curline());
8311 if (curwin->w_cursor.col > (colnr_T)i)
8312 {
8313#ifdef FEAT_VIRTUALEDIT
8314 if (virtual_active())
8315 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8316#endif
8317 curwin->w_cursor.col = i;
8318 }
8319 }
8320 cap->cmdchar = 'i';
8321 nv_edit(cap);
8322 break;
8323
8324 /*
8325 * "gI": Start insert in column 1.
8326 */
8327 case 'I':
8328 beginline(0);
8329 if (!checkclearopq(oap))
8330 invoke_edit(cap, FALSE, 'g', FALSE);
8331 break;
8332
8333#ifdef FEAT_SEARCHPATH
8334 /*
8335 * "gf": goto file, edit file under cursor
8336 * "]f" and "[f": can also be used.
8337 */
8338 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008339 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 nv_gotofile(cap);
8341 break;
8342#endif
8343
8344 /* "g'm" and "g`m": jump to mark without setting pcmark */
8345 case '\'':
8346 cap->arg = TRUE;
8347 /*FALLTHROUGH*/
8348 case '`':
8349 nv_gomark(cap);
8350 break;
8351
8352 /*
8353 * "gs": Goto sleep.
8354 */
8355 case 's':
8356 do_sleep(cap->count1 * 1000L);
8357 break;
8358
8359 /*
8360 * "ga": Display the ascii value of the character under the
8361 * cursor. It is displayed in decimal, hex, and octal. -- webb
8362 */
8363 case 'a':
8364 do_ascii(NULL);
8365 break;
8366
8367#ifdef FEAT_MBYTE
8368 /*
8369 * "g8": Display the bytes used for the UTF-8 character under the
8370 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008371 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 */
8373 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008374 if (cap->count0 == 8)
8375 utf_find_illegal();
8376 else
8377 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
8379#endif
8380
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008381 case '<':
8382 show_sb_text();
8383 break;
8384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 /*
8386 * "gg": Goto the first line in file. With a count it goes to
8387 * that line number like for "G". -- webb
8388 */
8389 case 'g':
8390 cap->arg = FALSE;
8391 nv_goto(cap);
8392 break;
8393
8394 /*
8395 * Two-character operators:
8396 * "gq" Format text
8397 * "gw" Format text and keep cursor position
8398 * "g~" Toggle the case of the text.
8399 * "gu" Change text to lower case.
8400 * "gU" Change text to upper case.
8401 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008402 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 */
8404 case 'q':
8405 case 'w':
8406 oap->cursor_start = curwin->w_cursor;
8407 /*FALLTHROUGH*/
8408 case '~':
8409 case 'u':
8410 case 'U':
8411 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008412 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 nv_operator(cap);
8414 break;
8415
8416 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008417 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 * current function
8419 * "gD": idem, but in the current file.
8420 */
8421 case 'd':
8422 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008423 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 break;
8425
8426#ifdef FEAT_MOUSE
8427 /*
8428 * g<*Mouse> : <C-*mouse>
8429 */
8430 case K_MIDDLEMOUSE:
8431 case K_MIDDLEDRAG:
8432 case K_MIDDLERELEASE:
8433 case K_LEFTMOUSE:
8434 case K_LEFTDRAG:
8435 case K_LEFTRELEASE:
8436 case K_RIGHTMOUSE:
8437 case K_RIGHTDRAG:
8438 case K_RIGHTRELEASE:
8439 case K_X1MOUSE:
8440 case K_X1DRAG:
8441 case K_X1RELEASE:
8442 case K_X2MOUSE:
8443 case K_X2DRAG:
8444 case K_X2RELEASE:
8445 mod_mask = MOD_MASK_CTRL;
8446 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8447 break;
8448#endif
8449
8450 case K_IGNORE:
8451 break;
8452
8453 /*
8454 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8455 */
8456 case 'p':
8457 case 'P':
8458 nv_put(cap);
8459 break;
8460
8461#ifdef FEAT_BYTEOFF
8462 /* "go": goto byte count from start of buffer */
8463 case 'o':
8464 goto_byte(cap->count0);
8465 break;
8466#endif
8467
8468 /* "gQ": improved Ex mode */
8469 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008470 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008473 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 break;
8475 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (!checkclearopq(oap))
8478 do_exmode(TRUE);
8479 break;
8480
8481#ifdef FEAT_JUMPLIST
8482 case ',':
8483 nv_pcmark(cap);
8484 break;
8485
8486 case ';':
8487 cap->count1 = -cap->count1;
8488 nv_pcmark(cap);
8489 break;
8490#endif
8491
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008492#ifdef FEAT_WINDOWS
8493 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008494 if (!checkclearop(oap))
8495 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008496 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008497 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008498 if (!checkclearop(oap))
8499 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008500 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008501#endif
8502
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008503 case '+':
8504 case '-': /* "g+" and "g-": undo or redo along the timeline */
8505 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008506 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008507 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008508 break;
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 default:
8511 clearopbeep(oap);
8512 break;
8513 }
8514}
8515
8516/*
8517 * Handle "o" and "O" commands.
8518 */
8519 static void
8520n_opencmd(cap)
8521 cmdarg_T *cap;
8522{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008523#ifdef FEAT_CONCEAL
8524 linenr_T oldline = curwin->w_cursor.lnum;
8525#endif
8526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (!checkclearopq(cap->oap))
8528 {
8529#ifdef FEAT_FOLDING
8530 if (cap->cmdchar == 'O')
8531 /* Open above the first line of a folded sequence of lines */
8532 (void)hasFolding(curwin->w_cursor.lnum,
8533 &curwin->w_cursor.lnum, NULL);
8534 else
8535 /* Open below the last line of a folded sequence of lines */
8536 (void)hasFolding(curwin->w_cursor.lnum,
8537 NULL, &curwin->w_cursor.lnum);
8538#endif
8539 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8540 (cap->cmdchar == 'O' ? 1 : 0)),
8541 (linenr_T)(curwin->w_cursor.lnum +
8542 (cap->cmdchar == 'o' ? 1 : 0))
8543 ) == OK
8544 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8545#ifdef FEAT_COMMENTS
8546 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8547#endif
8548 0, 0))
8549 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008550#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008551 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008552 update_single_line(curwin, oldline);
8553#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008554 /* When '#' is in 'cpoptions' ignore the count. */
8555 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8556 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8558 }
8559 }
8560}
8561
8562/*
8563 * "." command: redo last change.
8564 */
8565 static void
8566nv_dot(cap)
8567 cmdarg_T *cap;
8568{
8569 if (!checkclearopq(cap->oap))
8570 {
8571 /*
8572 * If "restart_edit" is TRUE, the last but one command is repeated
8573 * instead of the last command (inserting text). This is used for
8574 * CTRL-O <.> in insert mode.
8575 */
8576 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8577 clearopbeep(cap->oap);
8578 }
8579}
8580
8581/*
8582 * CTRL-R: undo undo
8583 */
8584 static void
8585nv_redo(cap)
8586 cmdarg_T *cap;
8587{
8588 if (!checkclearopq(cap->oap))
8589 {
8590 u_redo((int)cap->count1);
8591 curwin->w_set_curswant = TRUE;
8592 }
8593}
8594
8595/*
8596 * Handle "U" command.
8597 */
8598 static void
8599nv_Undo(cap)
8600 cmdarg_T *cap;
8601{
8602 /* In Visual mode and typing "gUU" triggers an operator */
8603 if (cap->oap->op_type == OP_UPPER
8604#ifdef FEAT_VISUAL
8605 || VIsual_active
8606#endif
8607 )
8608 {
8609 /* translate "gUU" to "gUgU" */
8610 cap->cmdchar = 'g';
8611 cap->nchar = 'U';
8612 nv_operator(cap);
8613 }
8614 else if (!checkclearopq(cap->oap))
8615 {
8616 u_undoline();
8617 curwin->w_set_curswant = TRUE;
8618 }
8619}
8620
8621/*
8622 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8623 * single character.
8624 */
8625 static void
8626nv_tilde(cap)
8627 cmdarg_T *cap;
8628{
8629 if (!p_to
8630#ifdef FEAT_VISUAL
8631 && !VIsual_active
8632#endif
8633 && cap->oap->op_type != OP_TILDE)
8634 n_swapchar(cap);
8635 else
8636 nv_operator(cap);
8637}
8638
8639/*
8640 * Handle an operator command.
8641 * The actual work is done by do_pending_operator().
8642 */
8643 static void
8644nv_operator(cap)
8645 cmdarg_T *cap;
8646{
8647 int op_type;
8648
8649 op_type = get_op_type(cap->cmdchar, cap->nchar);
8650
8651 if (op_type == cap->oap->op_type) /* double operator works on lines */
8652 nv_lineop(cap);
8653 else if (!checkclearop(cap->oap))
8654 {
8655 cap->oap->start = curwin->w_cursor;
8656 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008657#ifdef FEAT_EVAL
8658 set_op_var(op_type);
8659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 }
8661}
8662
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008663#ifdef FEAT_EVAL
8664/*
8665 * Set v:operator to the characters for "optype".
8666 */
8667 static void
8668set_op_var(optype)
8669 int optype;
8670{
8671 char_u opchars[3];
8672
8673 if (optype == OP_NOP)
8674 set_vim_var_string(VV_OP, NULL, 0);
8675 else
8676 {
8677 opchars[0] = get_op_char(optype);
8678 opchars[1] = get_extra_op_char(optype);
8679 opchars[2] = NUL;
8680 set_vim_var_string(VV_OP, opchars, -1);
8681 }
8682}
8683#endif
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685/*
8686 * Handle linewise operator "dd", "yy", etc.
8687 *
8688 * "_" is is a strange motion command that helps make operators more logical.
8689 * It is actually implemented, but not documented in the real Vi. This motion
8690 * command actually refers to "the current line". Commands like "dd" and "yy"
8691 * are really an alternate form of "d_" and "y_". It does accept a count, so
8692 * "d3_" works to delete 3 lines.
8693 */
8694 static void
8695nv_lineop(cap)
8696 cmdarg_T *cap;
8697{
8698 cap->oap->motion_type = MLINE;
8699 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8700 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008701 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8702 && cap->oap->motion_force != 'v'
8703 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 || cap->oap->op_type == OP_LSHIFT
8705 || cap->oap->op_type == OP_RSHIFT)
8706 beginline(BL_SOL | BL_FIX);
8707 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8708 beginline(BL_WHITE | BL_FIX);
8709}
8710
8711/*
8712 * <Home> command.
8713 */
8714 static void
8715nv_home(cap)
8716 cmdarg_T *cap;
8717{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008718 /* CTRL-HOME is like "gg" */
8719 if (mod_mask & MOD_MASK_CTRL)
8720 nv_goto(cap);
8721 else
8722 {
8723 cap->count0 = 1;
8724 nv_pipe(cap);
8725 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008726 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8727 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728}
8729
8730/*
8731 * "|" command.
8732 */
8733 static void
8734nv_pipe(cap)
8735 cmdarg_T *cap;
8736{
8737 cap->oap->motion_type = MCHAR;
8738 cap->oap->inclusive = FALSE;
8739 beginline(0);
8740 if (cap->count0 > 0)
8741 {
8742 coladvance((colnr_T)(cap->count0 - 1));
8743 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8744 }
8745 else
8746 curwin->w_curswant = 0;
8747 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008748 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 curwin->w_set_curswant = FALSE;
8750}
8751
8752/*
8753 * Handle back-word command "b" and "B".
8754 * cap->arg is 1 for "B"
8755 */
8756 static void
8757nv_bck_word(cap)
8758 cmdarg_T *cap;
8759{
8760 cap->oap->motion_type = MCHAR;
8761 cap->oap->inclusive = FALSE;
8762 curwin->w_set_curswant = TRUE;
8763 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8764 clearopbeep(cap->oap);
8765#ifdef FEAT_FOLDING
8766 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8767 foldOpenCursor();
8768#endif
8769}
8770
8771/*
8772 * Handle word motion commands "e", "E", "w" and "W".
8773 * cap->arg is TRUE for "E" and "W".
8774 */
8775 static void
8776nv_wordcmd(cap)
8777 cmdarg_T *cap;
8778{
8779 int n;
8780 int word_end;
8781 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008782 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 /*
8785 * Set inclusive for the "E" and "e" command.
8786 */
8787 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8788 word_end = TRUE;
8789 else
8790 word_end = FALSE;
8791 cap->oap->inclusive = word_end;
8792
8793 /*
8794 * "cw" and "cW" are a special case.
8795 */
8796 if (!word_end && cap->oap->op_type == OP_CHANGE)
8797 {
8798 n = gchar_cursor();
8799 if (n != NUL) /* not an empty line */
8800 {
8801 if (vim_iswhite(n))
8802 {
8803 /*
8804 * Reproduce a funny Vi behaviour: "cw" on a blank only
8805 * changes one character, not all blanks until the start of
8806 * the next word. Only do this when the 'w' flag is included
8807 * in 'cpoptions'.
8808 */
8809 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8810 {
8811 cap->oap->inclusive = TRUE;
8812 cap->oap->motion_type = MCHAR;
8813 return;
8814 }
8815 }
8816 else
8817 {
8818 /*
8819 * This is a little strange. To match what the real Vi does,
8820 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8821 * that we are not on a space or a TAB. This seems impolite
8822 * at first, but it's really more what we mean when we say
8823 * 'cw'.
8824 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008825 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 * will change only one character! This is done by setting
8827 * flag.
8828 */
8829 cap->oap->inclusive = TRUE;
8830 word_end = TRUE;
8831 flag = TRUE;
8832 }
8833 }
8834 }
8835
8836 cap->oap->motion_type = MCHAR;
8837 curwin->w_set_curswant = TRUE;
8838 if (word_end)
8839 n = end_word(cap->count1, cap->arg, flag, FALSE);
8840 else
8841 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8842
Bram Moolenaardfefb982008-04-01 10:06:39 +00008843 /* Don't leave the cursor on the NUL past the end of line. Unless we
8844 * didn't move it forward. */
8845 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008846 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 if (n == FAIL && cap->oap->op_type == OP_NOP)
8849 clearopbeep(cap->oap);
8850 else
8851 {
8852#ifdef FEAT_VISUAL
8853 adjust_for_sel(cap);
8854#endif
8855#ifdef FEAT_FOLDING
8856 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8857 foldOpenCursor();
8858#endif
8859 }
8860}
8861
8862/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008863 * Used after a movement command: If the cursor ends up on the NUL after the
8864 * end of the line, may move it back to the last character and make the motion
8865 * inclusive.
8866 */
8867 static void
8868adjust_cursor(oap)
8869 oparg_T *oap;
8870{
8871 /* The cursor cannot remain on the NUL when:
8872 * - the column is > 0
8873 * - not in Visual mode or 'selection' is "o"
8874 * - 'virtualedit' is not "all" and not "onemore".
8875 */
8876 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8877#ifdef FEAT_VISUAL
8878 && (!VIsual_active || *p_sel == 'o')
8879#endif
8880#ifdef FEAT_VIRTUALEDIT
8881 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8882#endif
8883 )
8884 {
8885 --curwin->w_cursor.col;
8886#ifdef FEAT_MBYTE
8887 /* prevent cursor from moving on the trail byte */
8888 if (has_mbyte)
8889 mb_adjust_cursor();
8890#endif
8891 oap->inclusive = TRUE;
8892 }
8893}
8894
8895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 * "0" and "^" commands.
8897 * cap->arg is the argument for beginline().
8898 */
8899 static void
8900nv_beginline(cap)
8901 cmdarg_T *cap;
8902{
8903 cap->oap->motion_type = MCHAR;
8904 cap->oap->inclusive = FALSE;
8905 beginline(cap->arg);
8906#ifdef FEAT_FOLDING
8907 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8908 foldOpenCursor();
8909#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008910 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8911 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913
8914#ifdef FEAT_VISUAL
8915/*
8916 * In exclusive Visual mode, may include the last character.
8917 */
8918 static void
8919adjust_for_sel(cap)
8920 cmdarg_T *cap;
8921{
8922 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8923 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8924 {
8925# ifdef FEAT_MBYTE
8926 if (has_mbyte)
8927 inc_cursor();
8928 else
8929# endif
8930 ++curwin->w_cursor.col;
8931 cap->oap->inclusive = FALSE;
8932 }
8933}
8934
8935/*
8936 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8937 * Should check VIsual_mode before calling this.
8938 * Returns TRUE when backed up to the previous line.
8939 */
8940 static int
8941unadjust_for_sel()
8942{
8943 pos_T *pp;
8944
8945 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8946 {
8947 if (lt(VIsual, curwin->w_cursor))
8948 pp = &curwin->w_cursor;
8949 else
8950 pp = &VIsual;
8951#ifdef FEAT_VIRTUALEDIT
8952 if (pp->coladd > 0)
8953 --pp->coladd;
8954 else
8955#endif
8956 if (pp->col > 0)
8957 {
8958 --pp->col;
8959#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008960 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#endif
8962 }
8963 else if (pp->lnum > 1)
8964 {
8965 --pp->lnum;
8966 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8967 return TRUE;
8968 }
8969 }
8970 return FALSE;
8971}
8972
8973/*
8974 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8975 */
8976 static void
8977nv_select(cap)
8978 cmdarg_T *cap;
8979{
8980 if (VIsual_active)
8981 VIsual_select = TRUE;
8982 else if (VIsual_reselect)
8983 {
8984 cap->nchar = 'v'; /* fake "gv" command */
8985 cap->arg = TRUE;
8986 nv_g_cmd(cap);
8987 }
8988}
8989
8990#endif
8991
8992/*
8993 * "G", "gg", CTRL-END, CTRL-HOME.
8994 * cap->arg is TRUE for "G".
8995 */
8996 static void
8997nv_goto(cap)
8998 cmdarg_T *cap;
8999{
9000 linenr_T lnum;
9001
9002 if (cap->arg)
9003 lnum = curbuf->b_ml.ml_line_count;
9004 else
9005 lnum = 1L;
9006 cap->oap->motion_type = MLINE;
9007 setpcmark();
9008
9009 /* When a count is given, use it instead of the default lnum */
9010 if (cap->count0 != 0)
9011 lnum = cap->count0;
9012 if (lnum < 1L)
9013 lnum = 1L;
9014 else if (lnum > curbuf->b_ml.ml_line_count)
9015 lnum = curbuf->b_ml.ml_line_count;
9016 curwin->w_cursor.lnum = lnum;
9017 beginline(BL_SOL | BL_FIX);
9018#ifdef FEAT_FOLDING
9019 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
9020 foldOpenCursor();
9021#endif
9022}
9023
9024/*
9025 * CTRL-\ in Normal mode.
9026 */
9027 static void
9028nv_normal(cap)
9029 cmdarg_T *cap;
9030{
9031 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
9032 {
9033 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00009034 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 clear_cmdline = TRUE; /* unshow mode later */
9036 restart_edit = 0;
9037#ifdef FEAT_CMDWIN
9038 if (cmdwin_type != 0)
9039 cmdwin_result = Ctrl_C;
9040#endif
9041#ifdef FEAT_VISUAL
9042 if (VIsual_active)
9043 {
9044 end_visual_mode(); /* stop Visual */
9045 redraw_curbuf_later(INVERTED);
9046 }
9047#endif
9048 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9049 if (cap->nchar == Ctrl_G && p_im)
9050 restart_edit = 'a';
9051 }
9052 else
9053 clearopbeep(cap->oap);
9054}
9055
9056/*
9057 * ESC in Normal mode: beep, but don't flush buffers.
9058 * Don't even beep if we are canceling a command.
9059 */
9060 static void
9061nv_esc(cap)
9062 cmdarg_T *cap;
9063{
9064 int no_reason;
9065
9066 no_reason = (cap->oap->op_type == OP_NOP
9067 && cap->opcount == 0
9068 && cap->count0 == 0
9069 && cap->oap->regname == 0
9070 && !p_im);
9071
9072 if (cap->arg) /* TRUE for CTRL-C */
9073 {
9074 if (restart_edit == 0
9075#ifdef FEAT_CMDWIN
9076 && cmdwin_type == 0
9077#endif
9078#ifdef FEAT_VISUAL
9079 && !VIsual_active
9080#endif
9081 && no_reason)
9082 MSG(_("Type :quit<Enter> to exit Vim"));
9083
9084 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9085 * set again below when halfway a mapping. */
9086 if (!p_im)
9087 restart_edit = 0;
9088#ifdef FEAT_CMDWIN
9089 if (cmdwin_type != 0)
9090 {
9091 cmdwin_result = K_IGNORE;
9092 got_int = FALSE; /* don't stop executing autocommands et al. */
9093 return;
9094 }
9095#endif
9096 }
9097
9098#ifdef FEAT_VISUAL
9099 if (VIsual_active)
9100 {
9101 end_visual_mode(); /* stop Visual */
9102 check_cursor_col(); /* make sure cursor is not beyond EOL */
9103 curwin->w_set_curswant = TRUE;
9104 redraw_curbuf_later(INVERTED);
9105 }
9106 else
9107#endif
9108 if (no_reason)
9109 vim_beep();
9110 clearop(cap->oap);
9111
9112 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9113 * set return to Insert mode afterwards. */
9114 if (restart_edit == 0 && goto_im()
9115#ifdef FEAT_EX_EXTRA
9116 && ex_normal_busy == 0
9117#endif
9118 )
9119 restart_edit = 'a';
9120}
9121
9122/*
9123 * Handle "A", "a", "I", "i" and <Insert> commands.
9124 */
9125 static void
9126nv_edit(cap)
9127 cmdarg_T *cap;
9128{
9129 /* <Insert> is equal to "i" */
9130 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9131 cap->cmdchar = 'i';
9132
9133#ifdef FEAT_VISUAL
9134 /* in Visual mode "A" and "I" are an operator */
9135 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9136 v_visop(cap);
9137
9138 /* in Visual mode and after an operator "a" and "i" are for text objects */
9139 else
9140#endif
9141 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9142 && (cap->oap->op_type != OP_NOP
9143#ifdef FEAT_VISUAL
9144 || VIsual_active
9145#endif
9146 ))
9147 {
9148#ifdef FEAT_TEXTOBJ
9149 nv_object(cap);
9150#else
9151 clearopbeep(cap->oap);
9152#endif
9153 }
9154 else if (!curbuf->b_p_ma && !p_im)
9155 {
9156 /* Only give this error when 'insertmode' is off. */
9157 EMSG(_(e_modifiable));
9158 clearop(cap->oap);
9159 }
9160 else if (!checkclearopq(cap->oap))
9161 {
9162 switch (cap->cmdchar)
9163 {
9164 case 'A': /* "A"ppend after the line */
9165 curwin->w_set_curswant = TRUE;
9166#ifdef FEAT_VIRTUALEDIT
9167 if (ve_flags == VE_ALL)
9168 {
9169 int save_State = State;
9170
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009171 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 * character past the end of the line */
9173 State = INSERT;
9174 coladvance((colnr_T)MAXCOL);
9175 State = save_State;
9176 }
9177 else
9178#endif
9179 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9180 break;
9181
9182 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009183 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9184 beginline(BL_WHITE);
9185 else
9186 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 break;
9188
9189 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9190#ifdef FEAT_VIRTUALEDIT
9191 /* increment coladd when in virtual space, increment the
9192 * column otherwise, also to append after an unprintable char */
9193 if (virtual_active()
9194 && (curwin->w_cursor.coladd > 0
9195 || *ml_get_cursor() == NUL
9196 || *ml_get_cursor() == TAB))
9197 curwin->w_cursor.coladd++;
9198 else
9199#endif
9200 if (*ml_get_cursor() != NUL)
9201 inc_cursor();
9202 break;
9203 }
9204
9205#ifdef FEAT_VIRTUALEDIT
9206 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9207 {
9208 int save_State = State;
9209
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009210 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 * character past the end of the line */
9212 State = INSERT;
9213 coladvance(getviscol());
9214 State = save_State;
9215 }
9216#endif
9217
9218 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9219 }
9220}
9221
9222/*
9223 * Invoke edit() and take care of "restart_edit" and the return value.
9224 */
9225 static void
9226invoke_edit(cap, repl, cmd, startln)
9227 cmdarg_T *cap;
9228 int repl; /* "r" or "gr" command */
9229 int cmd;
9230 int startln;
9231{
9232 int restart_edit_save = 0;
9233
9234 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9235 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9236 * it. */
9237 if (repl || !stuff_empty())
9238 restart_edit_save = restart_edit;
9239 else
9240 restart_edit_save = 0;
9241
9242 /* Always reset "restart_edit", this is not a restarted edit. */
9243 restart_edit = 0;
9244
9245 if (edit(cmd, startln, cap->count1))
9246 cap->retval |= CA_COMMAND_BUSY;
9247
9248 if (restart_edit == 0)
9249 restart_edit = restart_edit_save;
9250}
9251
9252#ifdef FEAT_TEXTOBJ
9253/*
9254 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9255 */
9256 static void
9257nv_object(cap)
9258 cmdarg_T *cap;
9259{
9260 int flag;
9261 int include;
9262 char_u *mps_save;
9263
9264 if (cap->cmdchar == 'i')
9265 include = FALSE; /* "ix" = inner object: exclude white space */
9266 else
9267 include = TRUE; /* "ax" = an object: include white space */
9268
9269 /* Make sure (), [], {} and <> are in 'matchpairs' */
9270 mps_save = curbuf->b_p_mps;
9271 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9272
9273 switch (cap->nchar)
9274 {
9275 case 'w': /* "aw" = a word */
9276 flag = current_word(cap->oap, cap->count1, include, FALSE);
9277 break;
9278 case 'W': /* "aW" = a WORD */
9279 flag = current_word(cap->oap, cap->count1, include, TRUE);
9280 break;
9281 case 'b': /* "ab" = a braces block */
9282 case '(':
9283 case ')':
9284 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9285 break;
9286 case 'B': /* "aB" = a Brackets block */
9287 case '{':
9288 case '}':
9289 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9290 break;
9291 case '[': /* "a[" = a [] block */
9292 case ']':
9293 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9294 break;
9295 case '<': /* "a<" = a <> block */
9296 case '>':
9297 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9298 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009299 case 't': /* "at" = a tag block (xml and html) */
9300 flag = current_tagblock(cap->oap, cap->count1, include);
9301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 case 'p': /* "ap" = a paragraph */
9303 flag = current_par(cap->oap, cap->count1, include, 'p');
9304 break;
9305 case 's': /* "as" = a sentence */
9306 flag = current_sent(cap->oap, cap->count1, include);
9307 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009308 case '"': /* "a"" = a double quoted string */
9309 case '\'': /* "a'" = a single quoted string */
9310 case '`': /* "a`" = a backtick quoted string */
9311 flag = current_quote(cap->oap, cap->count1, include,
9312 cap->nchar);
9313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314#if 0 /* TODO */
9315 case 'S': /* "aS" = a section */
9316 case 'f': /* "af" = a filename */
9317 case 'u': /* "au" = a URL */
9318#endif
9319 default:
9320 flag = FAIL;
9321 break;
9322 }
9323
9324 curbuf->b_p_mps = mps_save;
9325 if (flag == FAIL)
9326 clearopbeep(cap->oap);
9327 adjust_cursor_col();
9328 curwin->w_set_curswant = TRUE;
9329}
9330#endif
9331
9332/*
9333 * "q" command: Start/stop recording.
9334 * "q:", "q/", "q?": edit command-line in command-line window.
9335 */
9336 static void
9337nv_record(cap)
9338 cmdarg_T *cap;
9339{
9340 if (cap->oap->op_type == OP_FORMAT)
9341 {
9342 /* "gqq" is the same as "gqgq": format line */
9343 cap->cmdchar = 'g';
9344 cap->nchar = 'q';
9345 nv_operator(cap);
9346 }
9347 else if (!checkclearop(cap->oap))
9348 {
9349#ifdef FEAT_CMDWIN
9350 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9351 {
9352 stuffcharReadbuff(cap->nchar);
9353 stuffcharReadbuff(K_CMDWIN);
9354 }
9355 else
9356#endif
9357 /* (stop) recording into a named register, unless executing a
9358 * register */
9359 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9360 clearopbeep(cap->oap);
9361 }
9362}
9363
9364/*
9365 * Handle the "@r" command.
9366 */
9367 static void
9368nv_at(cap)
9369 cmdarg_T *cap;
9370{
9371 if (checkclearop(cap->oap))
9372 return;
9373#ifdef FEAT_EVAL
9374 if (cap->nchar == '=')
9375 {
9376 if (get_expr_register() == NUL)
9377 return;
9378 }
9379#endif
9380 while (cap->count1-- && !got_int)
9381 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009382 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 {
9384 clearopbeep(cap->oap);
9385 break;
9386 }
9387 line_breakcheck();
9388 }
9389}
9390
9391/*
9392 * Handle the CTRL-U and CTRL-D commands.
9393 */
9394 static void
9395nv_halfpage(cap)
9396 cmdarg_T *cap;
9397{
9398 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9399 || (cap->cmdchar == Ctrl_D
9400 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9401 clearopbeep(cap->oap);
9402 else if (!checkclearop(cap->oap))
9403 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9404}
9405
9406/*
9407 * Handle "J" or "gJ" command.
9408 */
9409 static void
9410nv_join(cap)
9411 cmdarg_T *cap;
9412{
9413#ifdef FEAT_VISUAL
9414 if (VIsual_active) /* join the visual lines */
9415 nv_operator(cap);
9416 else
9417#endif
9418 if (!checkclearop(cap->oap))
9419 {
9420 if (cap->count0 <= 1)
9421 cap->count0 = 2; /* default for join is two lines! */
9422 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9423 curbuf->b_ml.ml_line_count)
9424 clearopbeep(cap->oap); /* beyond last line */
9425 else
9426 {
9427 prep_redo(cap->oap->regname, cap->count0,
9428 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009429 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 }
9431 }
9432}
9433
9434/*
9435 * "P", "gP", "p" and "gp" commands.
9436 */
9437 static void
9438nv_put(cap)
9439 cmdarg_T *cap;
9440{
9441#ifdef FEAT_VISUAL
9442 int regname = 0;
9443 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009444 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009445 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446#endif
9447 int dir;
9448 int flags = 0;
9449
9450 if (cap->oap->op_type != OP_NOP)
9451 {
9452#ifdef FEAT_DIFF
9453 /* "dp" is ":diffput" */
9454 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9455 {
9456 clearop(cap->oap);
9457 nv_diffgetput(TRUE);
9458 }
9459 else
9460#endif
9461 clearopbeep(cap->oap);
9462 }
9463 else
9464 {
9465 dir = (cap->cmdchar == 'P'
9466 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9467 ? BACKWARD : FORWARD;
9468 prep_redo_cmd(cap);
9469 if (cap->cmdchar == 'g')
9470 flags |= PUT_CURSEND;
9471
9472#ifdef FEAT_VISUAL
9473 if (VIsual_active)
9474 {
9475 /* Putting in Visual mode: The put text replaces the selected
9476 * text. First delete the selected text, then put the new text.
9477 * Need to save and restore the registers that the delete
9478 * overwrites if the old contents is being put.
9479 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009480 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 regname = cap->oap->regname;
9482# ifdef FEAT_CLIPBOARD
9483 adjust_clip_reg(&regname);
9484# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009485 if (regname == 0 || regname == '"'
9486 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487# ifdef FEAT_CLIPBOARD
9488 || (clip_unnamed && (regname == '*' || regname == '+'))
9489# endif
9490
9491 )
9492 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009493 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 * put, save it first. */
9495 reg1 = get_register(regname, TRUE);
9496 }
9497
9498 /* Now delete the selected text. */
9499 cap->cmdchar = 'd';
9500 cap->nchar = NUL;
9501 cap->oap->regname = NUL;
9502 nv_operator(cap);
9503 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009504 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
9506 /* delete PUT_LINE_BACKWARD; */
9507 cap->oap->regname = regname;
9508
9509 if (reg1 != NULL)
9510 {
9511 /* Delete probably changed the register we want to put, save
9512 * it first. Then put back what was there before the delete. */
9513 reg2 = get_register(regname, FALSE);
9514 put_register(regname, reg1);
9515 }
9516
9517 /* When deleted a linewise Visual area, put the register as
9518 * lines to avoid it joined with the next line. When deletion was
9519 * characterwise, split a line when putting lines. */
9520 if (VIsual_mode == 'V')
9521 flags |= PUT_LINE;
9522 else if (VIsual_mode == 'v')
9523 flags |= PUT_LINE_SPLIT;
9524 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9525 flags |= PUT_LINE_FORWARD;
9526 dir = BACKWARD;
9527 if ((VIsual_mode != 'V'
9528 && curwin->w_cursor.col < curbuf->b_op_start.col)
9529 || (VIsual_mode == 'V'
9530 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9531 /* cursor is at the end of the line or end of file, put
9532 * forward. */
9533 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009534 /* May have been reset in do_put(). */
9535 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537#endif
9538 do_put(cap->oap->regname, dir, cap->count1, flags);
9539
9540#ifdef FEAT_VISUAL
9541 /* If a register was saved, put it back now. */
9542 if (reg2 != NULL)
9543 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009544
9545 /* What to reselect with "gv"? Selecting the just put text seems to
9546 * be the most useful, since the original text was removed. */
9547 if (was_visual)
9548 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009549 curbuf->b_visual.vi_start = curbuf->b_op_start;
9550 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009551 }
9552
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009553 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009554 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009555 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009556 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009557 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009558
9559 /* If the cursor was in that line, move it to the end of the last
9560 * line. */
9561 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9562 {
9563 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9564 coladvance((colnr_T)MAXCOL);
9565 }
9566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567#endif
9568 auto_format(FALSE, TRUE);
9569 }
9570}
9571
9572/*
9573 * "o" and "O" commands.
9574 */
9575 static void
9576nv_open(cap)
9577 cmdarg_T *cap;
9578{
9579#ifdef FEAT_DIFF
9580 /* "do" is ":diffget" */
9581 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9582 {
9583 clearop(cap->oap);
9584 nv_diffgetput(FALSE);
9585 }
9586 else
9587#endif
9588#ifdef FEAT_VISUAL
9589 if (VIsual_active) /* switch start and end of visual */
9590 v_swap_corners(cap->cmdchar);
9591 else
9592#endif
9593 n_opencmd(cap);
9594}
9595
9596#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 static void
9598nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009599 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 ProcessSniffRequests();
9602}
9603#endif
9604
9605#ifdef FEAT_NETBEANS_INTG
9606 static void
9607nv_nbcmd(cap)
9608 cmdarg_T *cap;
9609{
9610 netbeans_keycommand(cap->nchar);
9611}
9612#endif
9613
9614#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 static void
9616nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009617 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
9619 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9620}
9621#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009622
9623#ifdef FEAT_AUTOCMD
9624/*
9625 * Trigger CursorHold event.
9626 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9627 * input buffer. "did_cursorhold" is set to avoid retriggering.
9628 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009629 static void
9630nv_cursorhold(cap)
9631 cmdarg_T *cap;
9632{
9633 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9634 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009635 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009636}
9637#endif