blob: 3de170d6e2a55de8cba99126fd7a20da51382f9f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020023static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaara390bb62013-03-13 19:02:41 +010024static int VIsual_mode_orig = NUL; /* type of Visual mode, that user entered */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
26static int restart_VIsual_select = 0;
27#endif
28
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#ifdef FEAT_EVAL
30static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
31#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020033#ifdef __BORLANDC__
34 _RTLENTRYF
35#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 nv_compare __ARGS((const void *s1, const void *s2));
37static int find_command __ARGS((int cmdchar));
38static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000039static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
41static void find_start_of_word __ARGS((pos_T *));
42static void find_end_of_word __ARGS((pos_T *));
43static int get_mouse_class __ARGS((char_u *p));
44#endif
45static void prep_redo_cmd __ARGS((cmdarg_T *cap));
46static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
47static int checkclearop __ARGS((oparg_T *oap));
48static int checkclearopq __ARGS((oparg_T *oap));
49static void clearop __ARGS((oparg_T *oap));
50static void clearopbeep __ARGS((oparg_T *oap));
51#ifdef FEAT_VISUAL
52static void unshift_special __ARGS((cmdarg_T *cap));
53#endif
54#ifdef FEAT_CMDL_INFO
55static void del_from_showcmd __ARGS((int));
56#endif
57
58/*
59 * nv_*(): functions called to handle Normal and Visual mode commands.
60 * n_*(): functions called to handle Normal mode commands.
61 * v_*(): functions called to handle Visual mode commands.
62 */
63static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000064static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static void nv_error __ARGS((cmdarg_T *cap));
66static void nv_help __ARGS((cmdarg_T *cap));
67static void nv_addsub __ARGS((cmdarg_T *cap));
68static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000069static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
71#ifdef FEAT_MOUSE
72static void nv_mousescroll __ARGS((cmdarg_T *cap));
73static void nv_mouse __ARGS((cmdarg_T *cap));
74#endif
75static void nv_scroll_line __ARGS((cmdarg_T *cap));
76static void nv_zet __ARGS((cmdarg_T *cap));
77#ifdef FEAT_GUI
78static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
79static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
80#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#ifdef FEAT_GUI_TABLINE
82static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000083static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000085static void nv_exmode __ARGS((cmdarg_T *cap));
86static void nv_colon __ARGS((cmdarg_T *cap));
87static void nv_ctrlg __ARGS((cmdarg_T *cap));
88static void nv_ctrlh __ARGS((cmdarg_T *cap));
89static void nv_clear __ARGS((cmdarg_T *cap));
90static void nv_ctrlo __ARGS((cmdarg_T *cap));
91static void nv_hat __ARGS((cmdarg_T *cap));
92static void nv_Zet __ARGS((cmdarg_T *cap));
93static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000094static void nv_tagpop __ARGS((cmdarg_T *cap));
95static void nv_scroll __ARGS((cmdarg_T *cap));
96static void nv_right __ARGS((cmdarg_T *cap));
97static void nv_left __ARGS((cmdarg_T *cap));
98static void nv_up __ARGS((cmdarg_T *cap));
99static void nv_down __ARGS((cmdarg_T *cap));
100#ifdef FEAT_SEARCHPATH
101static void nv_gotofile __ARGS((cmdarg_T *cap));
102#endif
103static void nv_end __ARGS((cmdarg_T *cap));
104static void nv_dollar __ARGS((cmdarg_T *cap));
105static void nv_search __ARGS((cmdarg_T *cap));
106static void nv_next __ARGS((cmdarg_T *cap));
107static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
108static void nv_csearch __ARGS((cmdarg_T *cap));
109static void nv_brackets __ARGS((cmdarg_T *cap));
110static void nv_percent __ARGS((cmdarg_T *cap));
111static void nv_brace __ARGS((cmdarg_T *cap));
112static void nv_mark __ARGS((cmdarg_T *cap));
113static void nv_findpar __ARGS((cmdarg_T *cap));
114static void nv_undo __ARGS((cmdarg_T *cap));
115static void nv_kundo __ARGS((cmdarg_T *cap));
116static void nv_Replace __ARGS((cmdarg_T *cap));
117#ifdef FEAT_VREPLACE
118static void nv_vreplace __ARGS((cmdarg_T *cap));
119#endif
120#ifdef FEAT_VISUAL
121static void v_swap_corners __ARGS((int cmdchar));
122#endif
123static void nv_replace __ARGS((cmdarg_T *cap));
124static void n_swapchar __ARGS((cmdarg_T *cap));
125static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
126#ifdef FEAT_VISUAL
127static void v_visop __ARGS((cmdarg_T *cap));
128#endif
129static void nv_subst __ARGS((cmdarg_T *cap));
130static void nv_abbrev __ARGS((cmdarg_T *cap));
131static void nv_optrans __ARGS((cmdarg_T *cap));
132static void nv_gomark __ARGS((cmdarg_T *cap));
133static void nv_pcmark __ARGS((cmdarg_T *cap));
134static void nv_regname __ARGS((cmdarg_T *cap));
135#ifdef FEAT_VISUAL
136static void nv_visual __ARGS((cmdarg_T *cap));
137static void n_start_visual_mode __ARGS((int c));
138#endif
139static void nv_window __ARGS((cmdarg_T *cap));
140static void nv_suspend __ARGS((cmdarg_T *cap));
141static void nv_g_cmd __ARGS((cmdarg_T *cap));
142static void n_opencmd __ARGS((cmdarg_T *cap));
143static void nv_dot __ARGS((cmdarg_T *cap));
144static void nv_redo __ARGS((cmdarg_T *cap));
145static void nv_Undo __ARGS((cmdarg_T *cap));
146static void nv_tilde __ARGS((cmdarg_T *cap));
147static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000148#ifdef FEAT_EVAL
149static void set_op_var __ARGS((int optype));
150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void nv_lineop __ARGS((cmdarg_T *cap));
152static void nv_home __ARGS((cmdarg_T *cap));
153static void nv_pipe __ARGS((cmdarg_T *cap));
154static void nv_bck_word __ARGS((cmdarg_T *cap));
155static void nv_wordcmd __ARGS((cmdarg_T *cap));
156static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000157static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#ifdef FEAT_VISUAL
159static void adjust_for_sel __ARGS((cmdarg_T *cap));
160static int unadjust_for_sel __ARGS((void));
161static void nv_select __ARGS((cmdarg_T *cap));
162#endif
163static void nv_goto __ARGS((cmdarg_T *cap));
164static void nv_normal __ARGS((cmdarg_T *cap));
165static void nv_esc __ARGS((cmdarg_T *oap));
166static void nv_edit __ARGS((cmdarg_T *cap));
167static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
168#ifdef FEAT_TEXTOBJ
169static void nv_object __ARGS((cmdarg_T *cap));
170#endif
171static void nv_record __ARGS((cmdarg_T *cap));
172static void nv_at __ARGS((cmdarg_T *cap));
173static void nv_halfpage __ARGS((cmdarg_T *cap));
174static void nv_join __ARGS((cmdarg_T *cap));
175static void nv_put __ARGS((cmdarg_T *cap));
176static void nv_open __ARGS((cmdarg_T *cap));
177#ifdef FEAT_SNIFF
178static void nv_sniff __ARGS((cmdarg_T *cap));
179#endif
180#ifdef FEAT_NETBEANS_INTG
181static void nv_nbcmd __ARGS((cmdarg_T *cap));
182#endif
183#ifdef FEAT_DND
184static void nv_drop __ARGS((cmdarg_T *cap));
185#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000186#ifdef FEAT_AUTOCMD
187static void nv_cursorhold __ARGS((cmdarg_T *cap));
188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000190static char *e_noident = N_("E349: No identifier under cursor");
191
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192/*
193 * Function to be called for a Normal or Visual mode command.
194 * The argument is a cmdarg_T.
195 */
196typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
197
198/* Values for cmd_flags. */
199#define NV_NCH 0x01 /* may need to get a second char */
200#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
201#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
202#define NV_LANG 0x08 /* second char needs language adjustment */
203
204#define NV_SS 0x10 /* may start selection */
205#define NV_SSS 0x20 /* may start selection with shift modifier */
206#define NV_STS 0x40 /* may stop selection without shift modif. */
207#define NV_RL 0x80 /* 'rightleft' modifies command */
208#define NV_KEEPREG 0x100 /* don't clear regname */
209#define NV_NCW 0x200 /* not allowed in command-line window */
210
211/*
212 * Generally speaking, every Normal mode command should either clear any
213 * pending operator (with *clearop*()), or set the motion type variable
214 * oap->motion_type.
215 *
216 * When a cursor motion command is made, it is marked as being a character or
217 * line oriented motion. Then, if an operator is in effect, the operation
218 * becomes character or line oriented accordingly.
219 */
220
221/*
222 * This table contains one entry for every Normal or Visual mode command.
223 * The order doesn't matter, init_normal_cmds() will create a sorted index.
224 * It is faster when all keys from zero to '~' are present.
225 */
226static const struct nv_cmd
227{
228 int cmd_char; /* (first) command character */
229 nv_func_T cmd_func; /* function for this command */
230 short_u cmd_flags; /* NV_ flags */
231 short cmd_arg; /* value for ca.arg */
232} nv_cmds[] =
233{
234 {NUL, nv_error, 0, 0},
235 {Ctrl_A, nv_addsub, 0, 0},
236 {Ctrl_B, nv_page, NV_STS, BACKWARD},
237 {Ctrl_C, nv_esc, 0, TRUE},
238 {Ctrl_D, nv_halfpage, 0, 0},
239 {Ctrl_E, nv_scroll_line, 0, TRUE},
240 {Ctrl_F, nv_page, NV_STS, FORWARD},
241 {Ctrl_G, nv_ctrlg, 0, 0},
242 {Ctrl_H, nv_ctrlh, 0, 0},
243 {Ctrl_I, nv_pcmark, 0, 0},
244 {NL, nv_down, 0, FALSE},
245 {Ctrl_K, nv_error, 0, 0},
246 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000247 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 {Ctrl_N, nv_down, NV_STS, FALSE},
249 {Ctrl_O, nv_ctrlo, 0, 0},
250 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000251#ifdef FEAT_VISUAL
252 {Ctrl_Q, nv_visual, 0, FALSE},
253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 {Ctrl_R, nv_redo, 0, 0},
257 {Ctrl_S, nv_ignore, 0, 0},
258 {Ctrl_T, nv_tagpop, NV_NCW, 0},
259 {Ctrl_U, nv_halfpage, 0, 0},
260#ifdef FEAT_VISUAL
261 {Ctrl_V, nv_visual, 0, FALSE},
262 {'V', nv_visual, 0, FALSE},
263 {'v', nv_visual, 0, FALSE},
264#else
265 {Ctrl_V, nv_error, 0, 0},
266 {'V', nv_error, 0, 0},
267 {'v', nv_error, 0, 0},
268#endif
269 {Ctrl_W, nv_window, 0, 0},
270 {Ctrl_X, nv_addsub, 0, 0},
271 {Ctrl_Y, nv_scroll_line, 0, FALSE},
272 {Ctrl_Z, nv_suspend, 0, 0},
273 {ESC, nv_esc, 0, FALSE},
274 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
275 {Ctrl_RSB, nv_ident, NV_NCW, 0},
276 {Ctrl_HAT, nv_hat, NV_NCW, 0},
277 {Ctrl__, nv_error, 0, 0},
278 {' ', nv_right, 0, 0},
279 {'!', nv_operator, 0, 0},
280 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
281 {'#', nv_ident, 0, 0},
282 {'$', nv_dollar, 0, 0},
283 {'%', nv_percent, 0, 0},
284 {'&', nv_optrans, 0, 0},
285 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
286 {'(', nv_brace, 0, BACKWARD},
287 {')', nv_brace, 0, FORWARD},
288 {'*', nv_ident, 0, 0},
289 {'+', nv_down, 0, TRUE},
290 {',', nv_csearch, 0, TRUE},
291 {'-', nv_up, 0, TRUE},
292 {'.', nv_dot, NV_KEEPREG, 0},
293 {'/', nv_search, 0, FALSE},
294 {'0', nv_beginline, 0, 0},
295 {'1', nv_ignore, 0, 0},
296 {'2', nv_ignore, 0, 0},
297 {'3', nv_ignore, 0, 0},
298 {'4', nv_ignore, 0, 0},
299 {'5', nv_ignore, 0, 0},
300 {'6', nv_ignore, 0, 0},
301 {'7', nv_ignore, 0, 0},
302 {'8', nv_ignore, 0, 0},
303 {'9', nv_ignore, 0, 0},
304 {':', nv_colon, 0, 0},
305 {';', nv_csearch, 0, FALSE},
306 {'<', nv_operator, NV_RL, 0},
307 {'=', nv_operator, 0, 0},
308 {'>', nv_operator, NV_RL, 0},
309 {'?', nv_search, 0, FALSE},
310 {'@', nv_at, NV_NCH_NOP, FALSE},
311 {'A', nv_edit, 0, 0},
312 {'B', nv_bck_word, 0, 1},
313 {'C', nv_abbrev, NV_KEEPREG, 0},
314 {'D', nv_abbrev, NV_KEEPREG, 0},
315 {'E', nv_wordcmd, 0, TRUE},
316 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
317 {'G', nv_goto, 0, TRUE},
318 {'H', nv_scroll, 0, 0},
319 {'I', nv_edit, 0, 0},
320 {'J', nv_join, 0, 0},
321 {'K', nv_ident, 0, 0},
322 {'L', nv_scroll, 0, 0},
323 {'M', nv_scroll, 0, 0},
324 {'N', nv_next, 0, SEARCH_REV},
325 {'O', nv_open, 0, 0},
326 {'P', nv_put, 0, 0},
327 {'Q', nv_exmode, NV_NCW, 0},
328 {'R', nv_Replace, 0, FALSE},
329 {'S', nv_subst, NV_KEEPREG, 0},
330 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
331 {'U', nv_Undo, 0, 0},
332 {'W', nv_wordcmd, 0, TRUE},
333 {'X', nv_abbrev, NV_KEEPREG, 0},
334 {'Y', nv_abbrev, NV_KEEPREG, 0},
335 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
336 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
337 {'\\', nv_error, 0, 0},
338 {']', nv_brackets, NV_NCH_ALW, FORWARD},
339 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
340 {'_', nv_lineop, 0, 0},
341 {'`', nv_gomark, NV_NCH_ALW, FALSE},
342 {'a', nv_edit, NV_NCH, 0},
343 {'b', nv_bck_word, 0, 0},
344 {'c', nv_operator, 0, 0},
345 {'d', nv_operator, 0, 0},
346 {'e', nv_wordcmd, 0, FALSE},
347 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
348 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
349 {'h', nv_left, NV_RL, 0},
350 {'i', nv_edit, NV_NCH, 0},
351 {'j', nv_down, 0, FALSE},
352 {'k', nv_up, 0, FALSE},
353 {'l', nv_right, NV_RL, 0},
354 {'m', nv_mark, NV_NCH_NOP, 0},
355 {'n', nv_next, 0, 0},
356 {'o', nv_open, 0, 0},
357 {'p', nv_put, 0, 0},
358 {'q', nv_record, NV_NCH, 0},
359 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
360 {'s', nv_subst, NV_KEEPREG, 0},
361 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
362 {'u', nv_undo, 0, 0},
363 {'w', nv_wordcmd, 0, FALSE},
364 {'x', nv_abbrev, NV_KEEPREG, 0},
365 {'y', nv_operator, 0, 0},
366 {'z', nv_zet, NV_NCH_ALW, 0},
367 {'{', nv_findpar, 0, BACKWARD},
368 {'|', nv_pipe, 0, 0},
369 {'}', nv_findpar, 0, FORWARD},
370 {'~', nv_tilde, 0, 0},
371
372 /* pound sign */
373 {POUND, nv_ident, 0, 0},
374#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200375 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
376 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
377 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
378 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_LEFTMOUSE, nv_mouse, 0, 0},
380 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
381 {K_LEFTDRAG, nv_mouse, 0, 0},
382 {K_LEFTRELEASE, nv_mouse, 0, 0},
383 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
384 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
385 {K_MIDDLEDRAG, nv_mouse, 0, 0},
386 {K_MIDDLERELEASE, nv_mouse, 0, 0},
387 {K_RIGHTMOUSE, nv_mouse, 0, 0},
388 {K_RIGHTDRAG, nv_mouse, 0, 0},
389 {K_RIGHTRELEASE, nv_mouse, 0, 0},
390 {K_X1MOUSE, nv_mouse, 0, 0},
391 {K_X1DRAG, nv_mouse, 0, 0},
392 {K_X1RELEASE, nv_mouse, 0, 0},
393 {K_X2MOUSE, nv_mouse, 0, 0},
394 {K_X2DRAG, nv_mouse, 0, 0},
395 {K_X2RELEASE, nv_mouse, 0, 0},
396#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000398 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {K_INS, nv_edit, 0, 0},
400 {K_KINS, nv_edit, 0, 0},
401 {K_BS, nv_ctrlh, 0, 0},
402 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
403 {K_S_UP, nv_page, NV_SS, BACKWARD},
404 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
405 {K_S_DOWN, nv_page, NV_SS, FORWARD},
406 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
407 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
408 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
409 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
410 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
411 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
412 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
413 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
414 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
415 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
416 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
417 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {K_S_END, nv_end, NV_SS, FALSE},
419 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
420 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
421 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {K_S_HOME, nv_home, NV_SS, 0},
423 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
424 {K_DEL, nv_abbrev, 0, 0},
425 {K_KDEL, nv_abbrev, 0, 0},
426 {K_UNDO, nv_kundo, 0, 0},
427 {K_HELP, nv_help, NV_NCW, 0},
428 {K_F1, nv_help, NV_NCW, 0},
429 {K_XF1, nv_help, NV_NCW, 0},
430#ifdef FEAT_VISUAL
431 {K_SELECT, nv_select, 0, 0},
432#endif
433#ifdef FEAT_GUI
434 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
435 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
436#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000437#ifdef FEAT_GUI_TABLINE
438 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000439 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441#ifdef FEAT_FKMAP
442 {K_F8, farsi_fkey, 0, 0},
443 {K_F9, farsi_fkey, 0, 0},
444#endif
445#ifdef FEAT_SNIFF
446 {K_SNIFF, nv_sniff, 0, 0},
447#endif
448#ifdef FEAT_NETBEANS_INTG
449 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
450#endif
451#ifdef FEAT_DND
452 {K_DROP, nv_drop, NV_STS, 0},
453#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000454#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000455 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457};
458
459/* Number of commands in nv_cmds[]. */
460#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
461
462/* Sorted index of commands in nv_cmds[]. */
463static short nv_cmd_idx[NV_CMDS_SIZE];
464
465/* The highest index for which
466 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
467static int nv_max_linear;
468
469/*
470 * Compare functions for qsort() below, that checks the command character
471 * through the index in nv_cmd_idx[].
472 */
473 static int
474#ifdef __BORLANDC__
475_RTLENTRYF
476#endif
477nv_compare(s1, s2)
478 const void *s1;
479 const void *s2;
480{
481 int c1, c2;
482
483 /* The commands are sorted on absolute value. */
484 c1 = nv_cmds[*(const short *)s1].cmd_char;
485 c2 = nv_cmds[*(const short *)s2].cmd_char;
486 if (c1 < 0)
487 c1 = -c1;
488 if (c2 < 0)
489 c2 = -c2;
490 return c1 - c2;
491}
492
493/*
494 * Initialize the nv_cmd_idx[] table.
495 */
496 void
497init_normal_cmds()
498{
499 int i;
500
501 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000502 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 nv_cmd_idx[i] = i;
504
505 /* Sort the commands by the command character. */
506 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
507
508 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000509 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
511 break;
512 nv_max_linear = i - 1;
513}
514
515/*
516 * Search for a command in the commands table.
517 * Returns -1 for invalid command.
518 */
519 static int
520find_command(cmdchar)
521 int cmdchar;
522{
523 int i;
524 int idx;
525 int top, bot;
526 int c;
527
528#ifdef FEAT_MBYTE
529 /* A multi-byte character is never a command. */
530 if (cmdchar >= 0x100)
531 return -1;
532#endif
533
534 /* We use the absolute value of the character. Special keys have a
535 * negative value, but are sorted on their absolute value. */
536 if (cmdchar < 0)
537 cmdchar = -cmdchar;
538
539 /* If the character is in the first part: The character is the index into
540 * nv_cmd_idx[]. */
541 if (cmdchar <= nv_max_linear)
542 return nv_cmd_idx[cmdchar];
543
544 /* Perform a binary search. */
545 bot = nv_max_linear + 1;
546 top = NV_CMDS_SIZE - 1;
547 idx = -1;
548 while (bot <= top)
549 {
550 i = (top + bot) / 2;
551 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
552 if (c < 0)
553 c = -c;
554 if (cmdchar == c)
555 {
556 idx = nv_cmd_idx[i];
557 break;
558 }
559 if (cmdchar > c)
560 bot = i + 1;
561 else
562 top = i - 1;
563 }
564 return idx;
565}
566
567/*
568 * Execute a command in Normal mode.
569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 void
571normal_cmd(oap, toplevel)
572 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000573 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 cmdarg_T ca; /* command arguments */
576 int c;
577 int ctrl_w = FALSE; /* got CTRL-W command */
578 int old_col = curwin->w_curswant;
579#ifdef FEAT_CMDL_INFO
580 int need_flushbuf; /* need to call out_flush() */
581#endif
582#ifdef FEAT_VISUAL
583 pos_T old_pos; /* cursor position before command */
584 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000588#ifdef FEAT_EVAL
589 int set_prevcount = FALSE;
590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
593 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000594
595 /* Use a count remembered from before entering an operator. After typing
596 * "3d" we return from normal_cmd() and come back here, the "3" is
597 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 ca.opcount = opcount;
599
600#ifdef FEAT_SNIFF
601 want_sniff_request = sniff_connected;
602#endif
603
604 /*
605 * If there is an operator pending, then the command we take this time
606 * will terminate it. Finish_op tells us to finish the operation before
607 * returning this time (unless the operation was cancelled).
608 */
609#ifdef CURSOR_SHAPE
610 c = finish_op;
611#endif
612 finish_op = (oap->op_type != OP_NOP);
613#ifdef CURSOR_SHAPE
614 if (finish_op != c)
615 {
616 ui_cursor_shape(); /* may show different cursor shape */
617# ifdef FEAT_MOUSESHAPE
618 update_mouseshape(-1);
619# endif
620 }
621#endif
622
Bram Moolenaara983fe92008-07-31 20:04:27 +0000623 /* When not finishing an operator and no register name typed, reset the
624 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000628#ifdef FEAT_EVAL
629 set_prevcount = TRUE;
630#endif
631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaara983fe92008-07-31 20:04:27 +0000633#ifdef FEAT_AUTOCMD
634 /* Restore counts from before receiving K_CURSORHOLD. This means after
635 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
636 * "3 * 2". */
637 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
638 {
639 ca.opcount = oap->prev_opcount;
640 ca.count0 = oap->prev_count0;
641 oap->prev_opcount = 0;
642 oap->prev_count0 = 0;
643 }
644#endif
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#ifdef FEAT_VISUAL
647 mapped_len = typebuf_maplen();
648#endif
649
650 State = NORMAL_BUSY;
651#ifdef USE_ON_FLY_SCROLL
652 dont_scroll = FALSE; /* allow scrolling here */
653#endif
654
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100655#ifdef FEAT_EVAL
656 /* Set v:count here, when called from main() and not a stuffed
657 * command, so that v:count can be used in an expression mapping
658 * when there is no count. */
659 if (toplevel && stuff_empty())
660 set_vcount_ca(&ca, &set_prevcount);
661#endif
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 /*
664 * Get the command character from the user.
665 */
666 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000669#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 /*
671 * If a mapping was started in Visual or Select mode, remember the length
672 * of the mapping. This is used below to not return to Insert mode for as
673 * long as the mapping is being executed.
674 */
675 if (restart_edit == 0)
676 old_mapped_len = 0;
677 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000678 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682 if (c == NUL)
683 c = K_ZERO;
684
685#ifdef FEAT_VISUAL
686 /*
687 * In Select mode, typed text replaces the selection.
688 */
689 if (VIsual_active
690 && VIsual_select
691 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
692 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000693 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
694 * 'insertmode' is set) fake a "d"elete command, Insert mode will
695 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000696 * Insert the typed character in the typeahead buffer, so that it can
697 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000698 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000699 if (restart_edit != 0)
700 c = 'd';
701 else
702 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000703 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
705#endif
706
707#ifdef FEAT_CMDL_INFO
708 need_flushbuf = add_to_showcmd(c);
709#endif
710
711getcount:
712#ifdef FEAT_VISUAL
713 if (!(VIsual_active && VIsual_select))
714#endif
715 {
716 /*
717 * Handle a count before a command and compute ca.count0.
718 * Note that '0' is a command and not the start of a count, but it's
719 * part of a count after other digits.
720 */
721 while ( (c >= '1' && c <= '9')
722 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
723 {
724 if (c == K_DEL || c == K_KDEL)
725 {
726 ca.count0 /= 10;
727#ifdef FEAT_CMDL_INFO
728 del_from_showcmd(4); /* delete the digit and ~@% */
729#endif
730 }
731 else
732 ca.count0 = ca.count0 * 10 + (c - '0');
733 if (ca.count0 < 0) /* got too large! */
734 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000735#ifdef FEAT_EVAL
736 /* Set v:count here, when called from main() and not a stuffed
737 * command, so that v:count can be used in an expression mapping
738 * right after the count. */
739 if (toplevel && stuff_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100740 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 if (ctrl_w)
743 {
744 ++no_mapping;
745 ++allow_keys; /* no mapping for nchar, but keys */
746 }
747 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000748 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 --no_zero_mapping;
751 if (ctrl_w)
752 {
753 --no_mapping;
754 --allow_keys;
755 }
756#ifdef FEAT_CMDL_INFO
757 need_flushbuf |= add_to_showcmd(c);
758#endif
759 }
760
761 /*
762 * If we got CTRL-W there may be a/another count
763 */
764 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
765 {
766 ctrl_w = TRUE;
767 ca.opcount = ca.count0; /* remember first count */
768 ca.count0 = 0;
769 ++no_mapping;
770 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000771 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 --no_mapping;
774 --allow_keys;
775#ifdef FEAT_CMDL_INFO
776 need_flushbuf |= add_to_showcmd(c);
777#endif
778 goto getcount; /* jump back */
779 }
780 }
781
Bram Moolenaara983fe92008-07-31 20:04:27 +0000782#ifdef FEAT_AUTOCMD
783 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000785 /* Save the count values so that ca.opcount and ca.count0 are exactly
786 * the same when coming back here after handling K_CURSORHOLD. */
787 oap->prev_opcount = ca.opcount;
788 oap->prev_count0 = ca.count0;
789 }
790 else
791#endif
792 if (ca.opcount != 0)
793 {
794 /*
795 * If we're in the middle of an operator (including after entering a
796 * yank buffer with '"') AND we had a count before the operator, then
797 * that count overrides the current value of ca.count0.
798 * What this means effectively, is that commands like "3dw" get turned
799 * into "d3w" which makes things fall into place pretty neatly.
800 * If you give a count before AND after the operator, they are
801 * multiplied.
802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (ca.count0)
804 ca.count0 *= ca.opcount;
805 else
806 ca.count0 = ca.opcount;
807 }
808
809 /*
810 * Always remember the count. It will be set to zero (on the next call,
811 * above) when there is no pending operator.
812 * When called from main(), save the count for use by the "count" built-in
813 * variable.
814 */
815 ca.opcount = ca.count0;
816 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
817
818#ifdef FEAT_EVAL
819 /*
820 * Only set v:count when called from main() and not a stuffed command.
821 */
822 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000823 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#endif
825
826 /*
827 * Find the command character in the table of commands.
828 * For CTRL-W we already got nchar when looking for a count.
829 */
830 if (ctrl_w)
831 {
832 ca.nchar = c;
833 ca.cmdchar = Ctrl_W;
834 }
835 else
836 ca.cmdchar = c;
837 idx = find_command(ca.cmdchar);
838 if (idx < 0)
839 {
840 /* Not a known command: beep. */
841 clearopbeep(oap);
842 goto normal_end;
843 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000844
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000845 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000847 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000849 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 goto normal_end;
851 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000852#ifdef FEAT_AUTOCMD
853 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
854 goto normal_end;
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857#ifdef FEAT_VISUAL
858 /*
859 * In Visual/Select mode, a few keys are handled in a special way.
860 */
861 if (VIsual_active)
862 {
863 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
864 if (km_stopsel
865 && (nv_cmds[idx].cmd_flags & NV_STS)
866 && !(mod_mask & MOD_MASK_SHIFT))
867 {
868 end_visual_mode();
869 redraw_curbuf_later(INVERTED);
870 }
871
872 /* Keys that work different when 'keymodel' contains "startsel" */
873 if (km_startsel)
874 {
875 if (nv_cmds[idx].cmd_flags & NV_SS)
876 {
877 unshift_special(&ca);
878 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000879 if (idx < 0)
880 {
881 /* Just in case */
882 clearopbeep(oap);
883 goto normal_end;
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
887 && (mod_mask & MOD_MASK_SHIFT))
888 {
889 mod_mask &= ~MOD_MASK_SHIFT;
890 }
891 }
892 }
893#endif
894
895#ifdef FEAT_RIGHTLEFT
896 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
897 && (nv_cmds[idx].cmd_flags & NV_RL))
898 {
899 /* Invert horizontal movements and operations. Only when typed by the
900 * user directly, not when the result of a mapping or "x" translated
901 * to "dl". */
902 switch (ca.cmdchar)
903 {
904 case 'l': ca.cmdchar = 'h'; break;
905 case K_RIGHT: ca.cmdchar = K_LEFT; break;
906 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
907 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
908 case 'h': ca.cmdchar = 'l'; break;
909 case K_LEFT: ca.cmdchar = K_RIGHT; break;
910 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
911 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
912 case '>': ca.cmdchar = '<'; break;
913 case '<': ca.cmdchar = '>'; break;
914 }
915 idx = find_command(ca.cmdchar);
916 }
917#endif
918
919 /*
920 * Get an additional character if we need one.
921 */
922 if ((nv_cmds[idx].cmd_flags & NV_NCH)
923 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
924 && oap->op_type == OP_NOP)
925 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
926 || (ca.cmdchar == 'q'
927 && oap->op_type == OP_NOP
928 && !Recording
929 && !Exec_reg)
930 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
931 && (oap->op_type != OP_NOP
932#ifdef FEAT_VISUAL
933 || VIsual_active
934#endif
935 ))))
936 {
937 int *cp;
938 int repl = FALSE; /* get character for replace mode */
939 int lit = FALSE; /* get extra character literally */
940 int langmap_active = FALSE; /* using :lmap mappings */
941 int lang; /* getting a text character */
942#ifdef USE_IM_CONTROL
943 int save_smd; /* saved value of p_smd */
944#endif
945
946 ++no_mapping;
947 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000948#ifdef FEAT_AUTOCMD
949 /* Don't generate a CursorHold event here, most commands can't handle
950 * it, e.g., nv_replace(), nv_csearch(). */
951 did_cursorhold = TRUE;
952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (ca.cmdchar == 'g')
954 {
955 /*
956 * For 'g' get the next character now, so that we can check for
957 * "gr", "g'" and "g`".
958 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000959 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_CMDL_INFO
962 need_flushbuf |= add_to_showcmd(ca.nchar);
963#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200964 /* For "gn" from redo, need to get one more char to determine the
965 * operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200967 || ca.nchar == Ctrl_BSL
968 || ((ca.nchar == 'n' || ca.nchar == 'N') && !stuff_empty()))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
970 cp = &ca.extra_char; /* need to get a third character */
971 if (ca.nchar != 'r')
972 lit = TRUE; /* get it literally */
973 else
974 repl = TRUE; /* get it in replace mode */
975 }
976 else
977 cp = NULL; /* no third character needed */
978 }
979 else
980 {
981 if (ca.cmdchar == 'r') /* get it in replace mode */
982 repl = TRUE;
983 cp = &ca.nchar;
984 }
985 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
986
987 /*
988 * Get a second or third character.
989 */
990 if (cp != NULL)
991 {
992#ifdef CURSOR_SHAPE
993 if (repl)
994 {
995 State = REPLACE; /* pretend Replace mode */
996 ui_cursor_shape(); /* show different cursor shape */
997 }
998#endif
999 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
1000 {
1001 /* Allow mappings defined with ":lmap". */
1002 --no_mapping;
1003 --allow_keys;
1004 if (repl)
1005 State = LREPLACE;
1006 else
1007 State = LANGMAP;
1008 langmap_active = TRUE;
1009 }
1010#ifdef USE_IM_CONTROL
1011 save_smd = p_smd;
1012 p_smd = FALSE; /* Don't let the IM code show the mode here */
1013 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1014 im_set_active(TRUE);
1015#endif
1016
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001017 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019 if (langmap_active)
1020 {
1021 /* Undo the decrement done above */
1022 ++no_mapping;
1023 ++allow_keys;
1024 State = NORMAL_BUSY;
1025 }
1026#ifdef USE_IM_CONTROL
1027 if (lang)
1028 {
1029 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1030 im_save_status(&curbuf->b_p_iminsert);
1031 im_set_active(FALSE);
1032 }
1033 p_smd = save_smd;
1034#endif
1035#ifdef CURSOR_SHAPE
1036 State = NORMAL_BUSY;
1037#endif
1038#ifdef FEAT_CMDL_INFO
1039 need_flushbuf |= add_to_showcmd(*cp);
1040#endif
1041
1042 if (!lit)
1043 {
1044#ifdef FEAT_DIGRAPHS
1045 /* Typing CTRL-K gets a digraph. */
1046 if (*cp == Ctrl_K
1047 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1048 || cp == &ca.extra_char)
1049 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1050 {
1051 c = get_digraph(FALSE);
1052 if (c > 0)
1053 {
1054 *cp = c;
1055# ifdef FEAT_CMDL_INFO
1056 /* Guessing how to update showcmd here... */
1057 del_from_showcmd(3);
1058 need_flushbuf |= add_to_showcmd(*cp);
1059# endif
1060 }
1061 }
1062#endif
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 /* adjust chars > 127, except after "tTfFr" commands */
1065 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#ifdef FEAT_RIGHTLEFT
1067 /* adjust Hebrew mapped char */
1068 if (p_hkmap && lang && KeyTyped)
1069 *cp = hkmap(*cp);
1070# ifdef FEAT_FKMAP
1071 /* adjust Farsi mapped char */
1072 if (p_fkmap && lang && KeyTyped)
1073 *cp = fkmap(*cp);
1074# endif
1075#endif
1076 }
1077
1078 /*
1079 * When the next character is CTRL-\ a following CTRL-N means the
1080 * command is aborted and we go to Normal mode.
1081 */
1082 if (cp == &ca.extra_char
1083 && ca.nchar == Ctrl_BSL
1084 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1085 {
1086 ca.cmdchar = Ctrl_BSL;
1087 ca.nchar = ca.extra_char;
1088 idx = find_command(ca.cmdchar);
1089 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001090 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001091 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else if (*cp == Ctrl_BSL)
1093 {
1094 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1095
1096 /* There is a busy wait here when typing "f<C-\>" and then
1097 * something different from CTRL-N. Can't be avoided. */
1098 while ((c = vpeekc()) <= 0 && towait > 0L)
1099 {
1100 do_sleep(towait > 50L ? 50L : towait);
1101 towait -= 50L;
1102 }
1103 if (c > 0)
1104 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001105 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (c != Ctrl_N && c != Ctrl_G)
1107 vungetc(c);
1108 else
1109 {
1110 ca.cmdchar = Ctrl_BSL;
1111 ca.nchar = c;
1112 idx = find_command(ca.cmdchar);
1113 }
1114 }
1115 }
1116
1117#ifdef FEAT_MBYTE
1118 /* When getting a text character and the next character is a
1119 * multi-byte character, it could be a composing character.
1120 * However, don't wait for it to arrive. */
1121 while (enc_utf8 && lang && (c = vpeekc()) > 0
1122 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1123 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001124 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (!utf_iscomposing(c))
1126 {
1127 vungetc(c); /* it wasn't, put it back */
1128 break;
1129 }
1130 else if (ca.ncharC1 == 0)
1131 ca.ncharC1 = c;
1132 else
1133 ca.ncharC2 = c;
1134 }
1135#endif
1136 }
1137 --no_mapping;
1138 --allow_keys;
1139 }
1140
1141#ifdef FEAT_CMDL_INFO
1142 /*
1143 * Flush the showcmd characters onto the screen so we can see them while
1144 * the command is being executed. Only do this when the shown command was
1145 * actually displayed, otherwise this will slow down a lot when executing
1146 * mappings.
1147 */
1148 if (need_flushbuf)
1149 out_flush();
1150#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001151#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001152 if (ca.cmdchar != K_IGNORE)
1153 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156 State = NORMAL;
1157
1158 if (ca.nchar == ESC)
1159 {
1160 clearop(oap);
1161 if (restart_edit == 0 && goto_im())
1162 restart_edit = 'a';
1163 goto normal_end;
1164 }
1165
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001166 if (ca.cmdchar != K_IGNORE)
1167 {
1168 msg_didout = FALSE; /* don't scroll screen up for normal command */
1169 msg_col = 0;
1170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
1172#ifdef FEAT_VISUAL
1173 old_pos = curwin->w_cursor; /* remember where cursor was */
1174
1175 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1176 * mode. */
1177 if (!VIsual_active && km_startsel)
1178 {
1179 if (nv_cmds[idx].cmd_flags & NV_SS)
1180 {
1181 start_selection();
1182 unshift_special(&ca);
1183 idx = find_command(ca.cmdchar);
1184 }
1185 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1186 && (mod_mask & MOD_MASK_SHIFT))
1187 {
1188 start_selection();
1189 mod_mask &= ~MOD_MASK_SHIFT;
1190 }
1191 }
1192#endif
1193
1194 /*
1195 * Execute the command!
1196 * Call the command function found in the commands table.
1197 */
1198 ca.arg = nv_cmds[idx].cmd_arg;
1199 (nv_cmds[idx].cmd_func)(&ca);
1200
1201 /*
1202 * If we didn't start or finish an operator, reset oap->regname, unless we
1203 * need it later.
1204 */
1205 if (!finish_op
1206 && !oap->op_type
1207 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1208 {
1209 clearop(oap);
1210#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001211 {
1212 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001213
Bram Moolenaar536681b2011-05-10 16:12:45 +02001214 /* Adjust the register according to 'clipboard', so that when
1215 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001216# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001217 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001218# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001219 set_reg_var(regname);
1220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221#endif
1222 }
1223
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001224#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001225 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001226 * character or "z333<cr>". */
1227 if (old_mapped_len > 0)
1228 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001229#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 /*
1232 * If an operation is pending, handle it...
1233 */
1234 do_pending_operator(&ca, old_col, FALSE);
1235
1236 /*
1237 * Wait for a moment when a message is displayed that will be overwritten
1238 * by the mode message.
1239 * In Visual mode and with "^O" in Insert mode, a short message will be
1240 * overwritten by the mode message. Wait a bit, until a key is hit.
1241 * In Visual mode, it's more important to keep the Visual area updated
1242 * than keeping a message (e.g. from a /pat search).
1243 * Only do this if the command was typed, not from a mapping.
1244 * Don't wait when emsg_silent is non-zero.
1245 * Also wait a bit after an error message, e.g. for "^O:".
1246 * Don't redraw the screen, it would remove the message.
1247 */
1248 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001249 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 && (restart_edit != 0
1251#ifdef FEAT_VISUAL
1252 || (VIsual_active
1253 && old_pos.lnum == curwin->w_cursor.lnum
1254 && old_pos.col == curwin->w_cursor.col)
1255#endif
1256 )
1257 && (clear_cmdline
1258 || redraw_cmdline)
1259 && (msg_didout || (msg_didany && msg_scroll))
1260 && !msg_nowait
1261 && KeyTyped)
1262 || (restart_edit != 0
1263#ifdef FEAT_VISUAL
1264 && !VIsual_active
1265#endif
1266 && (msg_scroll
1267 || emsg_on_display)))
1268 && oap->regname == 0
1269 && !(ca.retval & CA_COMMAND_BUSY)
1270 && stuff_empty()
1271 && typebuf_typed()
1272 && emsg_silent == 0
1273 && !did_wait_return
1274 && oap->op_type == OP_NOP)
1275 {
1276 int save_State = State;
1277
1278 /* Draw the cursor with the right shape here */
1279 if (restart_edit != 0)
1280 State = INSERT;
1281
1282 /* If need to redraw, and there is a "keep_msg", redraw before the
1283 * delay */
1284 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1285 {
1286 char_u *kmsg;
1287
1288 kmsg = keep_msg;
1289 keep_msg = NULL;
1290 /* showmode() will clear keep_msg, but we want to use it anyway */
1291 update_screen(0);
1292 /* now reset it, otherwise it's put in the history again */
1293 keep_msg = kmsg;
1294 msg_attr(kmsg, keep_msg_attr);
1295 vim_free(kmsg);
1296 }
1297 setcursor();
1298 cursor_on();
1299 out_flush();
1300 if (msg_scroll || emsg_on_display)
1301 ui_delay(1000L, TRUE); /* wait at least one second */
1302 ui_delay(3000L, FALSE); /* wait up to three seconds */
1303 State = save_State;
1304
1305 msg_scroll = FALSE;
1306 emsg_on_display = FALSE;
1307 }
1308
1309 /*
1310 * Finish up after executing a Normal mode command.
1311 */
1312normal_end:
1313
1314 msg_nowait = FALSE;
1315
1316 /* Reset finish_op, in case it was set */
1317#ifdef CURSOR_SHAPE
1318 c = finish_op;
1319#endif
1320 finish_op = FALSE;
1321#ifdef CURSOR_SHAPE
1322 /* Redraw the cursor with another shape, if we were in Operator-pending
1323 * mode or did a replace command. */
1324 if (c || ca.cmdchar == 'r')
1325 {
1326 ui_cursor_shape(); /* may show different cursor shape */
1327# ifdef FEAT_MOUSESHAPE
1328 update_mouseshape(-1);
1329# endif
1330 }
1331#endif
1332
1333#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001334 if (oap->op_type == OP_NOP && oap->regname == 0
1335# ifdef FEAT_AUTOCMD
1336 && ca.cmdchar != K_CURSORHOLD
1337# endif
1338 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 clear_showcmd();
1340#endif
1341
1342 checkpcmark(); /* check if we moved since setting pcmark */
1343 vim_free(ca.searchbuf);
1344
1345#ifdef FEAT_MBYTE
1346 if (has_mbyte)
1347 mb_adjust_cursor();
1348#endif
1349
1350#ifdef FEAT_SCROLLBIND
1351 if (curwin->w_p_scb && toplevel)
1352 {
1353 validate_cursor(); /* may need to update w_leftcol */
1354 do_check_scrollbind(TRUE);
1355 }
1356#endif
1357
Bram Moolenaar860cae12010-06-05 23:22:07 +02001358#ifdef FEAT_CURSORBIND
1359 if (curwin->w_p_crb && toplevel)
1360 {
1361 validate_cursor(); /* may need to update w_leftcol */
1362 do_check_cursorbind();
1363 }
1364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /*
1367 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1368 * if still inside a mapping that started in Visual mode).
1369 * May switch from Visual to Select mode after CTRL-O command.
1370 */
1371 if ( oap->op_type == OP_NOP
1372#ifdef FEAT_VISUAL
1373 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1374 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001375#else
1376 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#endif
1378 && !(ca.retval & CA_COMMAND_BUSY)
1379 && stuff_empty()
1380 && oap->regname == 0)
1381 {
1382#ifdef FEAT_VISUAL
1383 if (restart_VIsual_select == 1)
1384 {
1385 VIsual_select = TRUE;
1386 showmode();
1387 restart_VIsual_select = 0;
1388 }
1389#endif
1390 if (restart_edit != 0
1391#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001392 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001394 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 (void)edit(restart_edit, FALSE, 1L);
1396 }
1397
1398#ifdef FEAT_VISUAL
1399 if (restart_VIsual_select == 2)
1400 restart_VIsual_select = 1;
1401#endif
1402
1403 /* Save count before an operator for next time. */
1404 opcount = ca.opcount;
1405}
1406
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001407#ifdef FEAT_EVAL
1408/*
1409 * Set v:count and v:count1 according to "cap".
1410 * Set v:prevcount only when "set_prevcount" is TRUE.
1411 */
1412 static void
1413set_vcount_ca(cap, set_prevcount)
1414 cmdarg_T *cap;
1415 int *set_prevcount;
1416{
1417 long count = cap->count0;
1418
1419 /* multiply with cap->opcount the same way as above */
1420 if (cap->opcount != 0)
1421 count = cap->opcount * (count == 0 ? 1 : count);
1422 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1423 *set_prevcount = FALSE; /* only set v:prevcount once */
1424}
1425#endif
1426
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427/*
1428 * Handle an operator after visual mode or when the movement is finished
1429 */
1430 void
1431do_pending_operator(cap, old_col, gui_yank)
1432 cmdarg_T *cap;
1433 int old_col;
1434 int gui_yank;
1435{
1436 oparg_T *oap = cap->oap;
1437 pos_T old_cursor;
1438 int empty_region_error;
1439 int restart_edit_save;
1440
1441#ifdef FEAT_VISUAL
1442 /* The visual area is remembered for redo */
1443 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1444 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001445 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 static long redo_VIsual_count; /* count for Visual operator */
1447# ifdef FEAT_VIRTUALEDIT
1448 int include_line_break = FALSE;
1449# endif
1450#endif
1451
1452#if defined(FEAT_CLIPBOARD)
1453 /*
1454 * Yank the visual area into the GUI selection register before we operate
1455 * on it and lose it forever.
1456 * Don't do it if a specific register was specified, so that ""x"*P works.
1457 * This could call do_pending_operator() recursively, but that's OK
1458 * because gui_yank will be TRUE for the nested call.
1459 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001460 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 && oap->op_type != OP_NOP
1462 && !gui_yank
1463# ifdef FEAT_VISUAL
1464 && VIsual_active
1465 && !redo_VIsual_busy
1466# endif
1467 && oap->regname == 0)
1468 clip_auto_select();
1469#endif
1470 old_cursor = curwin->w_cursor;
1471
1472 /*
1473 * If an operation is pending, handle it...
1474 */
1475 if ((finish_op
1476#ifdef FEAT_VISUAL
1477 || VIsual_active
1478#endif
1479 ) && oap->op_type != OP_NOP)
1480 {
1481#ifdef FEAT_VISUAL
1482 oap->is_VIsual = VIsual_active;
1483 if (oap->motion_force == 'V')
1484 oap->motion_type = MLINE;
1485 else if (oap->motion_force == 'v')
1486 {
1487 /* If the motion was linewise, "inclusive" will not have been set.
1488 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1489 if (oap->motion_type == MLINE)
1490 oap->inclusive = FALSE;
1491 /* If the motion already was characterwise, toggle "inclusive" */
1492 else if (oap->motion_type == MCHAR)
1493 oap->inclusive = !oap->inclusive;
1494 oap->motion_type = MCHAR;
1495 }
1496 else if (oap->motion_force == Ctrl_V)
1497 {
1498 /* Change line- or characterwise motion into Visual block mode. */
1499 VIsual_active = TRUE;
1500 VIsual = oap->start;
1501 VIsual_mode = Ctrl_V;
1502 VIsual_select = FALSE;
1503 VIsual_reselect = FALSE;
1504 }
1505#endif
1506
1507 /* only redo yank when 'y' flag is in 'cpoptions' */
1508 /* never redo "zf" (define fold) */
1509 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1510#ifdef FEAT_VISUAL
1511 && (!VIsual_active || oap->motion_force)
1512#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001513 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#ifdef FEAT_FOLDING
1515 && oap->op_type != OP_FOLD
1516 && oap->op_type != OP_FOLDOPEN
1517 && oap->op_type != OP_FOLDOPENREC
1518 && oap->op_type != OP_FOLDCLOSE
1519 && oap->op_type != OP_FOLDCLOSEREC
1520 && oap->op_type != OP_FOLDDEL
1521 && oap->op_type != OP_FOLDDELREC
1522#endif
1523 )
1524 {
1525 prep_redo(oap->regname, cap->count0,
1526 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1527 oap->motion_force, cap->cmdchar, cap->nchar);
1528 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1529 {
1530 /*
1531 * If 'cpoptions' does not contain 'r', insert the search
1532 * pattern to really repeat the same command.
1533 */
1534 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001535 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 AppendToRedobuff(NL_STR);
1537 }
1538 else if (cap->cmdchar == ':')
1539 {
1540 /* do_cmdline() has stored the first typed line in
1541 * "repeat_cmdline". When several lines are typed repeating
1542 * won't be possible. */
1543 if (repeat_cmdline == NULL)
1544 ResetRedobuff();
1545 else
1546 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001547 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 AppendToRedobuff(NL_STR);
1549 vim_free(repeat_cmdline);
1550 repeat_cmdline = NULL;
1551 }
1552 }
1553 }
1554
1555#ifdef FEAT_VISUAL
1556 if (redo_VIsual_busy)
1557 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001558 /* Redo of an operation on a Visual area. Use the same size from
1559 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 oap->start = curwin->w_cursor;
1561 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1562 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1563 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1564 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001565 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001567 if (VIsual_mode == 'v')
1568 {
1569 if (redo_VIsual_line_count <= 1)
1570 {
1571 validate_virtcol();
1572 curwin->w_curswant =
1573 curwin->w_virtcol + redo_VIsual_vcol - 1;
1574 }
1575 else
1576 curwin->w_curswant = redo_VIsual_vcol;
1577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001579 {
1580 curwin->w_curswant = MAXCOL;
1581 }
1582 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 cap->count0 = redo_VIsual_count;
1585 if (redo_VIsual_count != 0)
1586 cap->count1 = redo_VIsual_count;
1587 else
1588 cap->count1 = 1;
1589 }
1590 else if (VIsual_active)
1591 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001592 if (!gui_yank)
1593 {
1594 /* Save the current VIsual area for '< and '> marks, and "gv" */
1595 curbuf->b_visual.vi_start = VIsual;
1596 curbuf->b_visual.vi_end = curwin->w_cursor;
1597 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001598 if (VIsual_mode_orig != NUL)
1599 {
1600 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1601 VIsual_mode_orig = NUL;
1602 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001603 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001605 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 /* In Select mode, a linewise selection is operated upon like a
1610 * characterwise selection. */
1611 if (VIsual_select && VIsual_mode == 'V')
1612 {
1613 if (lt(VIsual, curwin->w_cursor))
1614 {
1615 VIsual.col = 0;
1616 curwin->w_cursor.col =
1617 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1618 }
1619 else
1620 {
1621 curwin->w_cursor.col = 0;
1622 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1623 }
1624 VIsual_mode = 'v';
1625 }
1626 /* If 'selection' is "exclusive", backup one character for
1627 * charwise selections. */
1628 else if (VIsual_mode == 'v')
1629 {
1630# ifdef FEAT_VIRTUALEDIT
1631 include_line_break =
1632# endif
1633 unadjust_for_sel();
1634 }
1635
1636 oap->start = VIsual;
1637 if (VIsual_mode == 'V')
1638 oap->start.col = 0;
1639 }
1640#endif /* FEAT_VISUAL */
1641
1642 /*
1643 * Set oap->start to the first position of the operated text, oap->end
1644 * to the end of the operated text. w_cursor is equal to oap->start.
1645 */
1646 if (lt(oap->start, curwin->w_cursor))
1647 {
1648#ifdef FEAT_FOLDING
1649 /* Include folded lines completely. */
1650 if (!VIsual_active)
1651 {
1652 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1653 oap->start.col = 0;
1654 if (hasFolding(curwin->w_cursor.lnum, NULL,
1655 &curwin->w_cursor.lnum))
1656 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1657 }
1658#endif
1659 oap->end = curwin->w_cursor;
1660 curwin->w_cursor = oap->start;
1661
1662 /* w_virtcol may have been updated; if the cursor goes back to its
1663 * previous position w_virtcol becomes invalid and isn't updated
1664 * automatically. */
1665 curwin->w_valid &= ~VALID_VIRTCOL;
1666 }
1667 else
1668 {
1669#ifdef FEAT_FOLDING
1670 /* Include folded lines completely. */
1671 if (!VIsual_active && oap->motion_type == MLINE)
1672 {
1673 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1674 NULL))
1675 curwin->w_cursor.col = 0;
1676 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1677 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1678 }
1679#endif
1680 oap->end = oap->start;
1681 oap->start = curwin->w_cursor;
1682 }
1683
1684 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1685
1686#ifdef FEAT_VIRTUALEDIT
1687 /* Set "virtual_op" before resetting VIsual_active. */
1688 virtual_op = virtual_active();
1689#endif
1690
1691#ifdef FEAT_VISUAL
1692 if (VIsual_active || redo_VIsual_busy)
1693 {
1694 if (VIsual_mode == Ctrl_V) /* block mode */
1695 {
1696 colnr_T start, end;
1697
1698 oap->block_mode = TRUE;
1699
1700 getvvcol(curwin, &(oap->start),
1701 &oap->start_vcol, NULL, &oap->end_vcol);
1702 if (!redo_VIsual_busy)
1703 {
1704 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1705
1706 if (start < oap->start_vcol)
1707 oap->start_vcol = start;
1708 if (end > oap->end_vcol)
1709 {
1710 if (*p_sel == 'e' && start >= 1
1711 && start - 1 >= oap->end_vcol)
1712 oap->end_vcol = start - 1;
1713 else
1714 oap->end_vcol = end;
1715 }
1716 }
1717
1718 /* if '$' was used, get oap->end_vcol from longest line */
1719 if (curwin->w_curswant == MAXCOL)
1720 {
1721 curwin->w_cursor.col = MAXCOL;
1722 oap->end_vcol = 0;
1723 for (curwin->w_cursor.lnum = oap->start.lnum;
1724 curwin->w_cursor.lnum <= oap->end.lnum;
1725 ++curwin->w_cursor.lnum)
1726 {
1727 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1728 if (end > oap->end_vcol)
1729 oap->end_vcol = end;
1730 }
1731 }
1732 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001733 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * Correct oap->end.col and oap->start.col to be the
1736 * upper-left and lower-right corner of the block area.
1737 *
1738 * (Actually, this does convert column positions into character
1739 * positions)
1740 */
1741 curwin->w_cursor.lnum = oap->end.lnum;
1742 coladvance(oap->end_vcol);
1743 oap->end = curwin->w_cursor;
1744
1745 curwin->w_cursor = oap->start;
1746 coladvance(oap->start_vcol);
1747 oap->start = curwin->w_cursor;
1748 }
1749
1750 if (!redo_VIsual_busy && !gui_yank)
1751 {
1752 /*
1753 * Prepare to reselect and redo Visual: this is based on the
1754 * size of the Visual text
1755 */
1756 resel_VIsual_mode = VIsual_mode;
1757 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001758 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001760 {
1761 if (VIsual_mode != Ctrl_V)
1762 getvvcol(curwin, &(oap->end),
1763 NULL, NULL, &oap->end_vcol);
1764 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1765 {
1766 if (VIsual_mode != Ctrl_V)
1767 getvvcol(curwin, &(oap->start),
1768 &oap->start_vcol, NULL, NULL);
1769 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1770 }
1771 else
1772 resel_VIsual_vcol = oap->end_vcol;
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 resel_VIsual_line_count = oap->line_count;
1775 }
1776
1777 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1778 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1779 && oap->op_type != OP_COLON
1780#ifdef FEAT_FOLDING
1781 && oap->op_type != OP_FOLD
1782 && oap->op_type != OP_FOLDOPEN
1783 && oap->op_type != OP_FOLDOPENREC
1784 && oap->op_type != OP_FOLDCLOSE
1785 && oap->op_type != OP_FOLDCLOSEREC
1786 && oap->op_type != OP_FOLDDEL
1787 && oap->op_type != OP_FOLDDELREC
1788#endif
1789 && oap->motion_force == NUL
1790 )
1791 {
1792 /* Prepare for redoing. Only use the nchar field for "r",
1793 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001794 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1795 || cap->nchar == 'N'))
1796 /* "gn" and "gN" are a bit different */
1797 prep_redo(oap->regname, 0L, NUL, cap->cmdchar, cap->nchar,
1798 get_op_char(oap->op_type),
1799 get_extra_op_char(oap->op_type));
1800 else
1801 prep_redo(oap->regname, 0L, NUL, 'v',
1802 get_op_char(oap->op_type),
1803 get_extra_op_char(oap->op_type),
1804 oap->op_type == OP_REPLACE
1805 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (!redo_VIsual_busy)
1807 {
1808 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001809 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 redo_VIsual_line_count = resel_VIsual_line_count;
1811 redo_VIsual_count = cap->count0;
1812 }
1813 }
1814
1815 /*
1816 * oap->inclusive defaults to TRUE.
1817 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1818 * FALSE. This makes "d}P" and "v}dP" work the same.
1819 */
1820 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1821 oap->inclusive = TRUE;
1822 if (VIsual_mode == 'V')
1823 oap->motion_type = MLINE;
1824 else
1825 {
1826 oap->motion_type = MCHAR;
1827 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1828# ifdef FEAT_VIRTUALEDIT
1829 && (include_line_break || !virtual_op)
1830# endif
1831 )
1832 {
1833 oap->inclusive = FALSE;
1834 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001835 * that works on lines only. */
1836 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001838 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1839 {
1840 ++oap->end.lnum;
1841 oap->end.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001843 oap->end.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844# endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001845 ++oap->line_count;
1846 }
1847 else
1848 {
1849 /* Cannot move below the last line, make the op
1850 * inclusive to tell the operation to include the
1851 * line break. */
1852 oap->inclusive = TRUE;
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855 }
1856 }
1857
1858 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 /*
1861 * Switch Visual off now, so screen updating does
1862 * not show inverted text when the screen is redrawn.
1863 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1864 * no screen redraw, so it is done here to remove the inverted
1865 * part.
1866 */
1867 if (!gui_yank)
1868 {
1869 VIsual_active = FALSE;
1870# ifdef FEAT_MOUSE
1871 setmouse();
1872 mouse_dragging = 0;
1873# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001874 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 clear_cmdline = TRUE; /* unshow visual mode later */
1876#ifdef FEAT_CMDL_INFO
1877 else
1878 clear_showcmd();
1879#endif
1880 if ((oap->op_type == OP_YANK
1881 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001882 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 || oap->op_type == OP_FILTER)
1884 && oap->motion_force == NUL)
1885 redraw_curbuf_later(INVERTED);
1886 }
1887 }
1888#endif
1889
1890#ifdef FEAT_MBYTE
1891 /* Include the trailing byte of a multi-byte char. */
1892 if (has_mbyte && oap->inclusive)
1893 {
1894 int l;
1895
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001896 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (l > 1)
1898 oap->end.col += l - 1;
1899 }
1900#endif
1901 curwin->w_set_curswant = TRUE;
1902
1903 /*
1904 * oap->empty is set when start and end are the same. The inclusive
1905 * flag affects this too, unless yanking and the end is on a NUL.
1906 */
1907 oap->empty = (oap->motion_type == MCHAR
1908 && (!oap->inclusive
1909 || (oap->op_type == OP_YANK
1910 && gchar_pos(&oap->end) == NUL))
1911 && equalpos(oap->start, oap->end)
1912#ifdef FEAT_VIRTUALEDIT
1913 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1914#endif
1915 );
1916 /*
1917 * For delete, change and yank, it's an error to operate on an
1918 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1919 */
1920 empty_region_error = (oap->empty
1921 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1922
1923#ifdef FEAT_VISUAL
1924 /* Force a redraw when operating on an empty Visual region, when
1925 * 'modifiable is off or creating a fold. */
1926 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1927# ifdef FEAT_FOLDING
1928 || oap->op_type == OP_FOLD
1929# endif
1930 ))
1931 redraw_curbuf_later(INVERTED);
1932#endif
1933
1934 /*
1935 * If the end of an operator is in column one while oap->motion_type
1936 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1937 * character in the previous line. If op_start is on or before the
1938 * first non-blank in the line, the operator becomes linewise
1939 * (strange, but that's the way vi does it).
1940 */
1941 if ( oap->motion_type == MCHAR
1942 && oap->inclusive == FALSE
1943 && !(cap->retval & CA_NO_ADJ_OP_END)
1944 && oap->end.col == 0
1945#ifdef FEAT_VISUAL
1946 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001947 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#endif
1949 && oap->line_count > 1)
1950 {
1951 oap->end_adjusted = TRUE; /* remember that we did this */
1952 --oap->line_count;
1953 --oap->end.lnum;
1954 if (inindent(0))
1955 oap->motion_type = MLINE;
1956 else
1957 {
1958 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1959 if (oap->end.col)
1960 {
1961 --oap->end.col;
1962 oap->inclusive = TRUE;
1963 }
1964 }
1965 }
1966 else
1967 oap->end_adjusted = FALSE;
1968
1969 switch (oap->op_type)
1970 {
1971 case OP_LSHIFT:
1972 case OP_RSHIFT:
1973 op_shift(oap, TRUE,
1974#ifdef FEAT_VISUAL
1975 oap->is_VIsual ? (int)cap->count1 :
1976#endif
1977 1);
1978 auto_format(FALSE, TRUE);
1979 break;
1980
1981 case OP_JOIN_NS:
1982 case OP_JOIN:
1983 if (oap->line_count < 2)
1984 oap->line_count = 2;
1985 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1986 curbuf->b_ml.ml_line_count)
1987 beep_flush();
1988 else
1989 {
Bram Moolenaar81340392012-06-06 16:12:59 +02001990 (void)do_join(oap->line_count, oap->op_type == OP_JOIN, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 auto_format(FALSE, TRUE);
1992 }
1993 break;
1994
1995 case OP_DELETE:
1996#ifdef FEAT_VISUAL
1997 VIsual_reselect = FALSE; /* don't reselect now */
1998#endif
1999 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002002 CancelRedo();
2003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 else
2005 {
2006 (void)op_delete(oap);
2007 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
2008 u_save_cursor(); /* cursor line wasn't saved yet */
2009 auto_format(FALSE, TRUE);
2010 }
2011 break;
2012
2013 case OP_YANK:
2014 if (empty_region_error)
2015 {
2016 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002019 CancelRedo();
2020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 }
2022 else
2023 (void)op_yank(oap, FALSE, !gui_yank);
2024 check_cursor_col();
2025 break;
2026
2027 case OP_CHANGE:
2028#ifdef FEAT_VISUAL
2029 VIsual_reselect = FALSE; /* don't reselect now */
2030#endif
2031 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002032 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002034 CancelRedo();
2035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 else
2037 {
2038 /* This is a new edit command, not a restart. Need to
2039 * remember it to make 'insertmode' work with mappings for
2040 * Visual mode. But do this only once and not when typed and
2041 * 'insertmode' isn't set. */
2042 if (p_im || !KeyTyped)
2043 restart_edit_save = restart_edit;
2044 else
2045 restart_edit_save = 0;
2046 restart_edit = 0;
2047 /* Reset finish_op now, don't want it set inside edit(). */
2048 finish_op = FALSE;
2049 if (op_change(oap)) /* will call edit() */
2050 cap->retval |= CA_COMMAND_BUSY;
2051 if (restart_edit == 0)
2052 restart_edit = restart_edit_save;
2053 }
2054 break;
2055
2056 case OP_FILTER:
2057 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2058 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2059 else
2060 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2061
2062 case OP_INDENT:
2063 case OP_COLON:
2064
2065#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2066 /*
2067 * If 'equalprg' is empty, do the indenting internally.
2068 */
2069 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2070 {
2071# ifdef FEAT_LISP
2072 if (curbuf->b_p_lisp)
2073 {
2074 op_reindent(oap, get_lisp_indent);
2075 break;
2076 }
2077# endif
2078# ifdef FEAT_CINDENT
2079 op_reindent(oap,
2080# ifdef FEAT_EVAL
2081 *curbuf->b_p_inde != NUL ? get_expr_indent :
2082# endif
2083 get_c_indent);
2084 break;
2085# endif
2086 }
2087#endif
2088
2089 op_colon(oap);
2090 break;
2091
2092 case OP_TILDE:
2093 case OP_UPPER:
2094 case OP_LOWER:
2095 case OP_ROT13:
2096 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002099 CancelRedo();
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 op_tilde(oap);
2103 check_cursor_col();
2104 break;
2105
2106 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002107#if defined(FEAT_EVAL)
2108 if (*curbuf->b_p_fex != NUL)
2109 op_formatexpr(oap); /* use expression */
2110 else
2111#endif
2112 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 op_colon(oap); /* use external command */
2114 else
2115 op_format(oap, FALSE); /* use internal function */
2116 break;
2117
2118 case OP_FORMAT2:
2119 op_format(oap, TRUE); /* use internal function */
2120 break;
2121
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002122 case OP_FUNCTION:
2123 op_function(oap); /* call 'operatorfunc' */
2124 break;
2125
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 case OP_INSERT:
2127 case OP_APPEND:
2128#ifdef FEAT_VISUAL
2129 VIsual_reselect = FALSE; /* don't reselect now */
2130#endif
2131#ifdef FEAT_VISUALEXTRA
2132 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002135 CancelRedo();
2136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 else
2138 {
2139 /* This is a new edit command, not a restart. Need to
2140 * remember it to make 'insertmode' work with mappings for
2141 * Visual mode. But do this only once. */
2142 restart_edit_save = restart_edit;
2143 restart_edit = 0;
2144
2145 op_insert(oap, cap->count1);
2146
2147 /* TODO: when inserting in several lines, should format all
2148 * the lines. */
2149 auto_format(FALSE, TRUE);
2150
2151 if (restart_edit == 0)
2152 restart_edit = restart_edit_save;
2153 }
2154#else
2155 vim_beep();
2156#endif
2157 break;
2158
2159 case OP_REPLACE:
2160#ifdef FEAT_VISUAL
2161 VIsual_reselect = FALSE; /* don't reselect now */
2162#endif
2163#ifdef FEAT_VISUALEXTRA
2164 if (empty_region_error)
2165#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002168 CancelRedo();
2169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#ifdef FEAT_VISUALEXTRA
2171 else
2172 op_replace(oap, cap->nchar);
2173#endif
2174 break;
2175
2176#ifdef FEAT_FOLDING
2177 case OP_FOLD:
2178 VIsual_reselect = FALSE; /* don't reselect now */
2179 foldCreate(oap->start.lnum, oap->end.lnum);
2180 break;
2181
2182 case OP_FOLDOPEN:
2183 case OP_FOLDOPENREC:
2184 case OP_FOLDCLOSE:
2185 case OP_FOLDCLOSEREC:
2186 VIsual_reselect = FALSE; /* don't reselect now */
2187 opFoldRange(oap->start.lnum, oap->end.lnum,
2188 oap->op_type == OP_FOLDOPEN
2189 || oap->op_type == OP_FOLDOPENREC,
2190 oap->op_type == OP_FOLDOPENREC
2191 || oap->op_type == OP_FOLDCLOSEREC,
2192 oap->is_VIsual);
2193 break;
2194
2195 case OP_FOLDDEL:
2196 case OP_FOLDDELREC:
2197 VIsual_reselect = FALSE; /* don't reselect now */
2198 deleteFold(oap->start.lnum, oap->end.lnum,
2199 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2200 break;
2201#endif
2202 default:
2203 clearopbeep(oap);
2204 }
2205#ifdef FEAT_VIRTUALEDIT
2206 virtual_op = MAYBE;
2207#endif
2208 if (!gui_yank)
2209 {
2210 /*
2211 * if 'sol' not set, go back to old column for some commands
2212 */
2213 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2214 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2215 || oap->op_type == OP_DELETE))
2216 coladvance(curwin->w_curswant = old_col);
2217 }
2218 else
2219 {
2220 curwin->w_cursor = old_cursor;
2221 }
2222#ifdef FEAT_VISUAL
2223 oap->block_mode = FALSE;
2224#endif
2225 clearop(oap);
2226 }
2227}
2228
2229/*
2230 * Handle indent and format operators and visual mode ":".
2231 */
2232 static void
2233op_colon(oap)
2234 oparg_T *oap;
2235{
2236 stuffcharReadbuff(':');
2237#ifdef FEAT_VISUAL
2238 if (oap->is_VIsual)
2239 stuffReadbuff((char_u *)"'<,'>");
2240 else
2241#endif
2242 {
2243 /*
2244 * Make the range look nice, so it can be repeated.
2245 */
2246 if (oap->start.lnum == curwin->w_cursor.lnum)
2247 stuffcharReadbuff('.');
2248 else
2249 stuffnumReadbuff((long)oap->start.lnum);
2250 if (oap->end.lnum != oap->start.lnum)
2251 {
2252 stuffcharReadbuff(',');
2253 if (oap->end.lnum == curwin->w_cursor.lnum)
2254 stuffcharReadbuff('.');
2255 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2256 stuffcharReadbuff('$');
2257 else if (oap->start.lnum == curwin->w_cursor.lnum)
2258 {
2259 stuffReadbuff((char_u *)".+");
2260 stuffnumReadbuff((long)oap->line_count - 1);
2261 }
2262 else
2263 stuffnumReadbuff((long)oap->end.lnum);
2264 }
2265 }
2266 if (oap->op_type != OP_COLON)
2267 stuffReadbuff((char_u *)"!");
2268 if (oap->op_type == OP_INDENT)
2269 {
2270#ifndef FEAT_CINDENT
2271 if (*get_equalprg() == NUL)
2272 stuffReadbuff((char_u *)"indent");
2273 else
2274#endif
2275 stuffReadbuff(get_equalprg());
2276 stuffReadbuff((char_u *)"\n");
2277 }
2278 else if (oap->op_type == OP_FORMAT)
2279 {
2280 if (*p_fp == NUL)
2281 stuffReadbuff((char_u *)"fmt");
2282 else
2283 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002284 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286
2287 /*
2288 * do_cmdline() does the rest
2289 */
2290}
2291
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002292/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002293 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002294 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002295 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002296op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002297 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002298{
2299#ifdef FEAT_EVAL
2300 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002301# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002302 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002303# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002304
2305 if (*p_opfunc == NUL)
2306 EMSG(_("E774: 'operatorfunc' is empty"));
2307 else
2308 {
2309 /* Set '[ and '] marks to text to be operated on. */
2310 curbuf->b_op_start = oap->start;
2311 curbuf->b_op_end = oap->end;
2312 if (oap->motion_type != MLINE && !oap->inclusive)
2313 /* Exclude the end position. */
2314 decl(&curbuf->b_op_end);
2315
2316 if (oap->block_mode)
2317 argv[0] = (char_u *)"block";
2318 else if (oap->motion_type == MLINE)
2319 argv[0] = (char_u *)"line";
2320 else
2321 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002322
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002323# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002324 /* Reset virtual_op so that 'virtualedit' can be changed in the
2325 * function. */
2326 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002327# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002328
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002329 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002330
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002331# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002332 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002333# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002334 }
2335#else
2336 EMSG(_("E775: Eval feature not available"));
2337#endif
2338}
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#if defined(FEAT_MOUSE) || defined(PROTO)
2341/*
2342 * Do the appropriate action for the current mouse click in the current mode.
2343 * Not used for Command-line mode.
2344 *
2345 * Normal Mode:
2346 * event modi- position visual change action
2347 * fier cursor window
2348 * left press - yes end yes
2349 * left press C yes end yes "^]" (2)
2350 * left press S yes end yes "*" (2)
2351 * left drag - yes start if moved no
2352 * left relse - yes start if moved no
2353 * middle press - yes if not active no put register
2354 * middle press - yes if active no yank and put
2355 * right press - yes start or extend yes
2356 * right press S yes no change yes "#" (2)
2357 * right drag - yes extend no
2358 * right relse - yes extend no
2359 *
2360 * Insert or Replace Mode:
2361 * event modi- position visual change action
2362 * fier cursor window
2363 * left press - yes (cannot be active) yes
2364 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2365 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2366 * left drag - yes start or extend (1) no CTRL-O (1)
2367 * left relse - yes start or extend (1) no CTRL-O (1)
2368 * middle press - no (cannot be active) no put register
2369 * right press - yes start or extend yes CTRL-O
2370 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2371 *
2372 * (1) only if mouse pointer moved since press
2373 * (2) only if click is in same buffer
2374 *
2375 * Return TRUE if start_arrow() should be called for edit mode.
2376 */
2377 int
2378do_mouse(oap, c, dir, count, fixindent)
2379 oparg_T *oap; /* operator argument, can be NULL */
2380 int c; /* K_LEFTMOUSE, etc */
2381 int dir; /* Direction to 'put' if necessary */
2382 long count;
2383 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2384{
2385 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2386 static int got_click = FALSE; /* got a click some time back */
2387
2388 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2389 int is_click; /* If FALSE it's a drag or release event */
2390 int is_drag; /* If TRUE it's a drag event */
2391 int jump_flags = 0; /* flags for jump_to_mouse() */
2392 pos_T start_visual;
2393 int moved; /* Has cursor moved? */
2394 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002395#ifdef FEAT_WINDOWS
2396 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398#ifdef FEAT_VERTSPLIT
2399 int in_sep_line; /* mouse in vertical separator line */
2400#endif
2401 int c1, c2;
2402#if defined(FEAT_FOLDING)
2403 pos_T save_cursor;
2404#endif
2405 win_T *old_curwin = curwin;
2406#ifdef FEAT_VISUAL
2407 static pos_T orig_cursor;
2408 colnr_T leftcol, rightcol;
2409 pos_T end_visual;
2410 int diff;
2411 int old_active = VIsual_active;
2412 int old_mode = VIsual_mode;
2413#endif
2414 int regname;
2415
2416#if defined(FEAT_FOLDING)
2417 save_cursor = curwin->w_cursor;
2418#endif
2419
2420 /*
2421 * When GUI is active, always recognize mouse events, otherwise:
2422 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2423 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2424 * - For command line and insert mode 'mouse' is checked before calling
2425 * do_mouse().
2426 */
2427 if (do_always)
2428 do_always = FALSE;
2429 else
2430#ifdef FEAT_GUI
2431 if (!gui.in_use)
2432#endif
2433 {
2434#ifdef FEAT_VISUAL
2435 if (VIsual_active)
2436 {
2437 if (!mouse_has(MOUSE_VISUAL))
2438 return FALSE;
2439 }
2440 else
2441#endif
2442 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2443 return FALSE;
2444 }
2445
2446 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2447
2448#ifdef FEAT_MOUSESHAPE
2449 /* May have stopped dragging the status or separator line. The pointer is
2450 * most likely still on the status or separator line. */
2451 if (!is_drag && drag_status_line)
2452 {
2453 drag_status_line = FALSE;
2454 update_mouseshape(SHAPE_IDX_STATUS);
2455 }
2456# ifdef FEAT_VERTSPLIT
2457 if (!is_drag && drag_sep_line)
2458 {
2459 drag_sep_line = FALSE;
2460 update_mouseshape(SHAPE_IDX_VSEP);
2461 }
2462# endif
2463#endif
2464
2465 /*
2466 * Ignore drag and release events if we didn't get a click.
2467 */
2468 if (is_click)
2469 got_click = TRUE;
2470 else
2471 {
2472 if (!got_click) /* didn't get click, ignore */
2473 return FALSE;
2474 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002477#ifdef FEAT_WINDOWS
2478 if (in_tab_line)
2479 {
2480 in_tab_line = FALSE;
2481 return FALSE;
2482 }
2483#endif
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486
Bram Moolenaar64969662005-12-14 21:59:55 +00002487#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002489 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
2491 if ((mod_mask & MOD_MASK_ALT))
2492 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 /*
2496 * CTRL right mouse button does CTRL-T
2497 */
2498 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2499 {
2500 if (State & INSERT)
2501 stuffcharReadbuff(Ctrl_O);
2502 if (count > 1)
2503 stuffnumReadbuff(count);
2504 stuffcharReadbuff(Ctrl_T);
2505 got_click = FALSE; /* ignore drag&release now */
2506 return FALSE;
2507 }
2508
2509 /*
2510 * CTRL only works with left mouse button
2511 */
2512 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2513 return FALSE;
2514
2515 /*
2516 * When a modifier is down, ignore drag and release events, as well as
2517 * multiple clicks and the middle mouse button.
2518 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2519 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002520 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2521 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 && (!is_click
2523 || (mod_mask & MOD_MASK_MULTI_CLICK)
2524 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002525 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 && mouse_model_popup()
2527 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002528 && !((mod_mask & MOD_MASK_ALT)
2529 && !mouse_model_popup()
2530 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 )
2532 return FALSE;
2533
2534 /*
2535 * If the button press was used as the movement command for an operator
2536 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2537 * drag/release events.
2538 */
2539 if (!is_click && which_button == MOUSE_MIDDLE)
2540 return FALSE;
2541
2542 if (oap != NULL)
2543 regname = oap->regname;
2544 else
2545 regname = 0;
2546
2547 /*
2548 * Middle mouse button does a 'put' of the selected text
2549 */
2550 if (which_button == MOUSE_MIDDLE)
2551 {
2552 if (State == NORMAL)
2553 {
2554 /*
2555 * If an operator was pending, we don't know what the user wanted
2556 * to do. Go back to normal mode: Clear the operator and beep().
2557 */
2558 if (oap != NULL && oap->op_type != OP_NOP)
2559 {
2560 clearopbeep(oap);
2561 return FALSE;
2562 }
2563
2564#ifdef FEAT_VISUAL
2565 /*
2566 * If visual was active, yank the highlighted text and put it
2567 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002568 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
2570 if (VIsual_active)
2571 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002572 if (VIsual_select)
2573 {
2574 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002575 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002576 }
2577 else
2578 {
2579 stuffcharReadbuff('y');
2580 stuffcharReadbuff(K_MIDDLEMOUSE);
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 do_always = TRUE; /* ignore 'mouse' setting next time */
2583 return FALSE;
2584 }
2585#endif
2586 /*
2587 * The rest is below jump_to_mouse()
2588 */
2589 }
2590
2591 else if ((State & INSERT) == 0)
2592 return FALSE;
2593
2594 /*
2595 * Middle click in insert mode doesn't move the mouse, just insert the
2596 * contents of a register. '.' register is special, can't insert that
2597 * with do_put().
2598 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2599 * happens for the GUI).
2600 */
2601 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2602 {
2603 if (regname == '.')
2604 insert_reg(regname, TRUE);
2605 else
2606 {
2607#ifdef FEAT_CLIPBOARD
2608 if (clip_star.available && regname == 0)
2609 regname = '*';
2610#endif
2611 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2612 insert_reg(regname, TRUE);
2613 else
2614 {
2615 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2616
2617 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2618 AppendCharToRedobuff(Ctrl_R);
2619 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2620 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2621 }
2622 }
2623 return FALSE;
2624 }
2625 }
2626
2627 /* When dragging or button-up stay in the same window. */
2628 if (!is_click)
2629 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2630
2631 start_visual.lnum = 0;
2632
Bram Moolenaarf740b292006-02-16 22:11:02 +00002633#ifdef FEAT_WINDOWS
2634 /* Check for clicking in the tab page line. */
2635 if (mouse_row == 0 && firstwin->w_winrow > 0)
2636 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002637 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002638 {
2639 if (in_tab_line)
2640 {
2641 c1 = TabPageIdxs[mouse_col];
2642 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2643 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002644 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002645 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002646
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647 /* click in a tab selects that tab page */
2648 if (is_click
2649# ifdef FEAT_CMDWIN
2650 && cmdwin_type == 0
2651# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002652 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002653 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002654 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002655 c1 = TabPageIdxs[mouse_col];
2656 if (c1 >= 0)
2657 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002658 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2659 {
2660 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002661 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002662 tabpage_new();
2663 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2664 }
2665 else
2666 {
2667 /* Go to specified tab page, or next one if not clicking
2668 * on a label. */
2669 goto_tabpage(c1);
2670
2671 /* It's like clicking on the status line of a window. */
2672 if (curwin != old_curwin)
2673 end_visual_mode();
2674 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 }
2676 else if (c1 < 0)
2677 {
2678 tabpage_T *tp;
2679
2680 /* Close the current or specified tab page. */
2681 if (c1 == -999)
2682 tp = curtab;
2683 else
2684 tp = find_tabpage(-c1);
2685 if (tp == curtab)
2686 {
2687 if (first_tabpage->tp_next != NULL)
2688 tabpage_close(FALSE);
2689 }
2690 else if (tp != NULL)
2691 tabpage_close_other(tp, FALSE);
2692 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002693 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002694 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002695 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002696 else if (is_drag && in_tab_line)
2697 {
2698 c1 = TabPageIdxs[mouse_col];
2699 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2700 return FALSE;
2701 }
2702
Bram Moolenaarf740b292006-02-16 22:11:02 +00002703#endif
2704
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 /*
2706 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2707 * right button up -> pop-up menu
2708 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002709 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 */
2711 if (mouse_model_popup())
2712 {
2713 if (which_button == MOUSE_RIGHT
2714 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2715 {
2716 /*
2717 * NOTE: Ignore right button down and drag mouse events.
2718 * Windows only shows the popup menu on the button up event.
2719 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002720#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2721 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (!is_click)
2723 return FALSE;
2724#endif
2725#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2726 if (is_click || is_drag)
2727 return FALSE;
2728#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002729#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2731 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2732 if (gui.in_use)
2733 {
2734 jump_flags = 0;
2735 if (STRCMP(p_mousem, "popup_setpos") == 0)
2736 {
2737 /* First set the cursor position before showing the popup
2738 * menu. */
2739#ifdef FEAT_VISUAL
2740 if (VIsual_active)
2741 {
2742 pos_T m_pos;
2743
2744 /*
2745 * set MOUSE_MAY_STOP_VIS if we are outside the
2746 * selection or the current window (might have false
2747 * negative here)
2748 */
2749 if (mouse_row < W_WINROW(curwin)
2750 || mouse_row
2751 > (W_WINROW(curwin) + curwin->w_height))
2752 jump_flags = MOUSE_MAY_STOP_VIS;
2753 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2754 jump_flags = MOUSE_MAY_STOP_VIS;
2755 else
2756 {
2757 if ((lt(curwin->w_cursor, VIsual)
2758 && (lt(m_pos, curwin->w_cursor)
2759 || lt(VIsual, m_pos)))
2760 || (lt(VIsual, curwin->w_cursor)
2761 && (lt(m_pos, VIsual)
2762 || lt(curwin->w_cursor, m_pos))))
2763 {
2764 jump_flags = MOUSE_MAY_STOP_VIS;
2765 }
2766 else if (VIsual_mode == Ctrl_V)
2767 {
2768 getvcols(curwin, &curwin->w_cursor, &VIsual,
2769 &leftcol, &rightcol);
2770 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2771 if (m_pos.col < leftcol || m_pos.col > rightcol)
2772 jump_flags = MOUSE_MAY_STOP_VIS;
2773 }
2774 }
2775 }
2776 else
2777 jump_flags = MOUSE_MAY_STOP_VIS;
2778#endif
2779 }
2780 if (jump_flags)
2781 {
2782 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2783 update_curbuf(
2784#ifdef FEAT_VISUAL
2785 VIsual_active ? INVERTED :
2786#endif
2787 VALID);
2788 setcursor();
2789 out_flush(); /* Update before showing popup menu */
2790 }
2791# ifdef FEAT_MENU
2792 gui_show_popupmenu();
2793# endif
2794 return (jump_flags & CURSOR_MOVED) != 0;
2795 }
2796 else
2797 return FALSE;
2798#else
2799 return FALSE;
2800#endif
2801 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002802 if (which_button == MOUSE_LEFT
2803 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
2805 which_button = MOUSE_RIGHT;
2806 mod_mask &= ~MOD_MASK_SHIFT;
2807 }
2808 }
2809
2810#ifdef FEAT_VISUAL
2811 if ((State & (NORMAL | INSERT))
2812 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2813 {
2814 if (which_button == MOUSE_LEFT)
2815 {
2816 if (is_click)
2817 {
2818 /* stop Visual mode for a left click in a window, but not when
2819 * on a status line */
2820 if (VIsual_active)
2821 jump_flags |= MOUSE_MAY_STOP_VIS;
2822 }
2823 else if (mouse_has(MOUSE_VISUAL))
2824 jump_flags |= MOUSE_MAY_VIS;
2825 }
2826 else if (which_button == MOUSE_RIGHT)
2827 {
2828 if (is_click && VIsual_active)
2829 {
2830 /*
2831 * Remember the start and end of visual before moving the
2832 * cursor.
2833 */
2834 if (lt(curwin->w_cursor, VIsual))
2835 {
2836 start_visual = curwin->w_cursor;
2837 end_visual = VIsual;
2838 }
2839 else
2840 {
2841 start_visual = VIsual;
2842 end_visual = curwin->w_cursor;
2843 }
2844 }
2845 jump_flags |= MOUSE_FOCUS;
2846 if (mouse_has(MOUSE_VISUAL))
2847 jump_flags |= MOUSE_MAY_VIS;
2848 }
2849 }
2850#endif
2851
2852 /*
2853 * If an operator is pending, ignore all drags and releases until the
2854 * next mouse click.
2855 */
2856 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2857 {
2858 got_click = FALSE;
2859 oap->motion_type = MCHAR;
2860 }
2861
2862 /* When releasing the button let jump_to_mouse() know. */
2863 if (!is_click && !is_drag)
2864 jump_flags |= MOUSE_RELEASED;
2865
2866 /*
2867 * JUMP!
2868 */
2869 jump_flags = jump_to_mouse(jump_flags,
2870 oap == NULL ? NULL : &(oap->inclusive), which_button);
2871 moved = (jump_flags & CURSOR_MOVED);
2872 in_status_line = (jump_flags & IN_STATUS_LINE);
2873#ifdef FEAT_VERTSPLIT
2874 in_sep_line = (jump_flags & IN_SEP_LINE);
2875#endif
2876
2877#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002878 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2880 {
2881 int key = KEY2TERMCAP1(c);
2882
2883 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2884 || key == (int)KE_RIGHTRELEASE)
2885 netbeans_button_release(which_button);
2886 }
2887#endif
2888
2889 /* When jumping to another window, clear a pending operator. That's a bit
2890 * friendlier than beeping and not jumping to that window. */
2891 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2892 clearop(oap);
2893
2894#ifdef FEAT_FOLDING
2895 if (mod_mask == 0
2896 && !is_drag
2897 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2898 && which_button == MOUSE_LEFT)
2899 {
2900 /* open or close a fold at this line */
2901 if (jump_flags & MOUSE_FOLD_OPEN)
2902 openFold(curwin->w_cursor.lnum, 1L);
2903 else
2904 closeFold(curwin->w_cursor.lnum, 1L);
2905 /* don't move the cursor if still in the same window */
2906 if (curwin == old_curwin)
2907 curwin->w_cursor = save_cursor;
2908 }
2909#endif
2910
2911#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2912 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2913 {
2914 clip_modeless(which_button, is_click, is_drag);
2915 return FALSE;
2916 }
2917#endif
2918
2919#ifdef FEAT_VISUAL
2920 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002921 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (VIsual_active && is_drag && p_so)
2923 {
2924 /* In the very first line, allow scrolling one line */
2925 if (mouse_row == 0)
2926 mouse_dragging = 2;
2927 else
2928 mouse_dragging = 1;
2929 }
2930
2931 /* When dragging the mouse above the window, scroll down. */
2932 if (is_drag && mouse_row < 0 && !in_status_line)
2933 {
2934 scroll_redraw(FALSE, 1L);
2935 mouse_row = 0;
2936 }
2937
2938 if (start_visual.lnum) /* right click in visual mode */
2939 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002940 /* When ALT is pressed make Visual mode blockwise. */
2941 if (mod_mask & MOD_MASK_ALT)
2942 VIsual_mode = Ctrl_V;
2943
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 /*
2945 * In Visual-block mode, divide the area in four, pick up the corner
2946 * that is in the quarter that the cursor is in.
2947 */
2948 if (VIsual_mode == Ctrl_V)
2949 {
2950 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2951 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2952 end_visual.col = leftcol;
2953 else
2954 end_visual.col = rightcol;
2955 if (curwin->w_cursor.lnum <
2956 (start_visual.lnum + end_visual.lnum) / 2)
2957 end_visual.lnum = end_visual.lnum;
2958 else
2959 end_visual.lnum = start_visual.lnum;
2960
2961 /* move VIsual to the right column */
2962 start_visual = curwin->w_cursor; /* save the cursor pos */
2963 curwin->w_cursor = end_visual;
2964 coladvance(end_visual.col);
2965 VIsual = curwin->w_cursor;
2966 curwin->w_cursor = start_visual; /* restore the cursor */
2967 }
2968 else
2969 {
2970 /*
2971 * If the click is before the start of visual, change the start.
2972 * If the click is after the end of visual, change the end. If
2973 * the click is inside the visual, change the closest side.
2974 */
2975 if (lt(curwin->w_cursor, start_visual))
2976 VIsual = end_visual;
2977 else if (lt(end_visual, curwin->w_cursor))
2978 VIsual = start_visual;
2979 else
2980 {
2981 /* In the same line, compare column number */
2982 if (end_visual.lnum == start_visual.lnum)
2983 {
2984 if (curwin->w_cursor.col - start_visual.col >
2985 end_visual.col - curwin->w_cursor.col)
2986 VIsual = start_visual;
2987 else
2988 VIsual = end_visual;
2989 }
2990
2991 /* In different lines, compare line number */
2992 else
2993 {
2994 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2995 (end_visual.lnum - curwin->w_cursor.lnum);
2996
2997 if (diff > 0) /* closest to end */
2998 VIsual = start_visual;
2999 else if (diff < 0) /* closest to start */
3000 VIsual = end_visual;
3001 else /* in the middle line */
3002 {
3003 if (curwin->w_cursor.col <
3004 (start_visual.col + end_visual.col) / 2)
3005 VIsual = end_visual;
3006 else
3007 VIsual = start_visual;
3008 }
3009 }
3010 }
3011 }
3012 }
3013 /*
3014 * If Visual mode started in insert mode, execute "CTRL-O"
3015 */
3016 else if ((State & INSERT) && VIsual_active)
3017 stuffcharReadbuff(Ctrl_O);
3018#endif
3019
3020 /*
3021 * Middle mouse click: Put text before cursor.
3022 */
3023 if (which_button == MOUSE_MIDDLE)
3024 {
3025#ifdef FEAT_CLIPBOARD
3026 if (clip_star.available && regname == 0)
3027 regname = '*';
3028#endif
3029 if (yank_register_mline(regname))
3030 {
3031 if (mouse_past_bottom)
3032 dir = FORWARD;
3033 }
3034 else if (mouse_past_eol)
3035 dir = FORWARD;
3036
3037 if (fixindent)
3038 {
3039 c1 = (dir == BACKWARD) ? '[' : ']';
3040 c2 = 'p';
3041 }
3042 else
3043 {
3044 c1 = (dir == FORWARD) ? 'p' : 'P';
3045 c2 = NUL;
3046 }
3047 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3048
3049 /*
3050 * Remember where the paste started, so in edit() Insstart can be set
3051 * to this position
3052 */
3053 if (restart_edit != 0)
3054 where_paste_started = curwin->w_cursor;
3055 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3056 }
3057
3058#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3059 /*
3060 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3061 * error under the mouse pointer.
3062 */
3063 else if (((mod_mask & MOD_MASK_CTRL)
3064 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3065 && bt_quickfix(curbuf))
3066 {
3067 if (State & INSERT)
3068 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003069 if (curwin->w_llist_ref == NULL) /* quickfix window */
3070 stuffReadbuff((char_u *)":.cc\n");
3071 else /* location list window */
3072 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 got_click = FALSE; /* ignore drag&release now */
3074 }
3075#endif
3076
3077 /*
3078 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3079 * under the mouse pointer.
3080 */
3081 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3082 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3083 {
3084 if (State & INSERT)
3085 stuffcharReadbuff(Ctrl_O);
3086 stuffcharReadbuff(Ctrl_RSB);
3087 got_click = FALSE; /* ignore drag&release now */
3088 }
3089
3090 /*
3091 * Shift-Mouse click searches for the next occurrence of the word under
3092 * the mouse pointer
3093 */
3094 else if ((mod_mask & MOD_MASK_SHIFT))
3095 {
3096 if (State & INSERT
3097#ifdef FEAT_VISUAL
3098 || (VIsual_active && VIsual_select)
3099#endif
3100 )
3101 stuffcharReadbuff(Ctrl_O);
3102 if (which_button == MOUSE_LEFT)
3103 stuffcharReadbuff('*');
3104 else /* MOUSE_RIGHT */
3105 stuffcharReadbuff('#');
3106 }
3107
3108 /* Handle double clicks, unless on status line */
3109 else if (in_status_line)
3110 {
3111#ifdef FEAT_MOUSESHAPE
3112 if ((is_drag || is_click) && !drag_status_line)
3113 {
3114 drag_status_line = TRUE;
3115 update_mouseshape(-1);
3116 }
3117#endif
3118 }
3119#ifdef FEAT_VERTSPLIT
3120 else if (in_sep_line)
3121 {
3122# ifdef FEAT_MOUSESHAPE
3123 if ((is_drag || is_click) && !drag_sep_line)
3124 {
3125 drag_sep_line = TRUE;
3126 update_mouseshape(-1);
3127 }
3128# endif
3129 }
3130#endif
3131#ifdef FEAT_VISUAL
3132 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3133 && mouse_has(MOUSE_VISUAL))
3134 {
3135 if (is_click || !VIsual_active)
3136 {
3137 if (VIsual_active)
3138 orig_cursor = VIsual;
3139 else
3140 {
3141 check_visual_highlight();
3142 VIsual = curwin->w_cursor;
3143 orig_cursor = VIsual;
3144 VIsual_active = TRUE;
3145 VIsual_reselect = TRUE;
3146 /* start Select mode if 'selectmode' contains "mouse" */
3147 may_start_select('o');
3148 setmouse();
3149 }
3150 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003151 {
3152 /* Double click with ALT pressed makes it blockwise. */
3153 if (mod_mask & MOD_MASK_ALT)
3154 VIsual_mode = Ctrl_V;
3155 else
3156 VIsual_mode = 'v';
3157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3159 VIsual_mode = 'V';
3160 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3161 VIsual_mode = Ctrl_V;
3162#ifdef FEAT_CLIPBOARD
3163 /* Make sure the clipboard gets updated. Needed because start and
3164 * end may still be the same, and the selection needs to be owned */
3165 clip_star.vmode = NUL;
3166#endif
3167 }
3168 /*
3169 * A double click selects a word or a block.
3170 */
3171 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3172 {
3173 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003174 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 if (is_click)
3177 {
3178 /* If the character under the cursor (skipping white space) is
3179 * not a word character, try finding a match and select a (),
3180 * {}, [], #if/#endif, etc. block. */
3181 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003182 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 inc(&end_visual);
3184 if (oap != NULL)
3185 oap->motion_type = MCHAR;
3186 if (oap != NULL
3187 && VIsual_mode == 'v'
3188 && !vim_iswordc(gchar_pos(&end_visual))
3189 && equalpos(curwin->w_cursor, VIsual)
3190 && (pos = findmatch(oap, NUL)) != NULL)
3191 {
3192 curwin->w_cursor = *pos;
3193 if (oap->motion_type == MLINE)
3194 VIsual_mode = 'V';
3195 else if (*p_sel == 'e')
3196 {
3197 if (lt(curwin->w_cursor, VIsual))
3198 ++VIsual.col;
3199 else
3200 ++curwin->w_cursor.col;
3201 }
3202 }
3203 }
3204
3205 if (pos == NULL && (is_click || is_drag))
3206 {
3207 /* When not found a match or when dragging: extend to include
3208 * a word. */
3209 if (lt(curwin->w_cursor, orig_cursor))
3210 {
3211 find_start_of_word(&curwin->w_cursor);
3212 find_end_of_word(&VIsual);
3213 }
3214 else
3215 {
3216 find_start_of_word(&VIsual);
3217 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3218#ifdef FEAT_MBYTE
3219 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003220 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#else
3222 ++curwin->w_cursor.col;
3223#endif
3224 find_end_of_word(&curwin->w_cursor);
3225 }
3226 }
3227 curwin->w_set_curswant = TRUE;
3228 }
3229 if (is_click)
3230 redraw_curbuf_later(INVERTED); /* update the inversion */
3231 }
3232 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003233 {
3234 if (mod_mask & MOD_MASK_ALT)
3235 VIsual_mode = Ctrl_V;
3236 else
3237 VIsual_mode = 'v';
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003241 if ((!VIsual_active && old_active && mode_displayed)
3242 || (VIsual_active && p_smd && msg_silent == 0
3243 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 redraw_cmdline = TRUE;
3245#endif
3246
3247 return moved;
3248}
3249
3250#ifdef FEAT_VISUAL
3251/*
3252 * Move "pos" back to the start of the word it's in.
3253 */
3254 static void
3255find_start_of_word(pos)
3256 pos_T *pos;
3257{
3258 char_u *line;
3259 int cclass;
3260 int col;
3261
3262 line = ml_get(pos->lnum);
3263 cclass = get_mouse_class(line + pos->col);
3264
3265 while (pos->col > 0)
3266 {
3267 col = pos->col - 1;
3268#ifdef FEAT_MBYTE
3269 col -= (*mb_head_off)(line, line + col);
3270#endif
3271 if (get_mouse_class(line + col) != cclass)
3272 break;
3273 pos->col = col;
3274 }
3275}
3276
3277/*
3278 * Move "pos" forward to the end of the word it's in.
3279 * When 'selection' is "exclusive", the position is just after the word.
3280 */
3281 static void
3282find_end_of_word(pos)
3283 pos_T *pos;
3284{
3285 char_u *line;
3286 int cclass;
3287 int col;
3288
3289 line = ml_get(pos->lnum);
3290 if (*p_sel == 'e' && pos->col > 0)
3291 {
3292 --pos->col;
3293#ifdef FEAT_MBYTE
3294 pos->col -= (*mb_head_off)(line, line + pos->col);
3295#endif
3296 }
3297 cclass = get_mouse_class(line + pos->col);
3298 while (line[pos->col] != NUL)
3299 {
3300#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003301 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#else
3303 col = pos->col + 1;
3304#endif
3305 if (get_mouse_class(line + col) != cclass)
3306 {
3307 if (*p_sel == 'e')
3308 pos->col = col;
3309 break;
3310 }
3311 pos->col = col;
3312 }
3313}
3314
3315/*
3316 * Get class of a character for selection: same class means same word.
3317 * 0: blank
3318 * 1: punctuation groups
3319 * 2: normal word character
3320 * >2: multi-byte word character.
3321 */
3322 static int
3323get_mouse_class(p)
3324 char_u *p;
3325{
3326 int c;
3327
3328#ifdef FEAT_MBYTE
3329 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3330 return mb_get_class(p);
3331#endif
3332
3333 c = *p;
3334 if (c == ' ' || c == '\t')
3335 return 0;
3336
3337 if (vim_iswordc(c))
3338 return 2;
3339
3340 /*
3341 * There are a few special cases where we want certain combinations of
3342 * characters to be considered as a single word. These are things like
3343 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003344 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3347 return 1;
3348 return c;
3349}
3350#endif /* FEAT_VISUAL */
3351#endif /* FEAT_MOUSE */
3352
3353#if defined(FEAT_VISUAL) || defined(PROTO)
3354/*
3355 * Check if highlighting for visual mode is possible, give a warning message
3356 * if not.
3357 */
3358 void
3359check_visual_highlight()
3360{
3361 static int did_check = FALSE;
3362
3363 if (full_screen)
3364 {
3365 if (!did_check && hl_attr(HLF_V) == 0)
3366 MSG(_("Warning: terminal cannot highlight"));
3367 did_check = TRUE;
3368 }
3369}
3370
3371/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003372 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 * This function should ALWAYS be called to end Visual mode, except from
3374 * do_pending_operator().
3375 */
3376 void
3377end_visual_mode()
3378{
3379#ifdef FEAT_CLIPBOARD
3380 /*
3381 * If we are using the clipboard, then remember what was selected in case
3382 * we need to paste it somewhere while we still own the selection.
3383 * Only do this when the clipboard is already owned. Don't want to grab
3384 * the selection when hitting ESC.
3385 */
3386 if (clip_star.available && clip_star.owned)
3387 clip_auto_select();
3388#endif
3389
3390 VIsual_active = FALSE;
3391#ifdef FEAT_MOUSE
3392 setmouse();
3393 mouse_dragging = 0;
3394#endif
3395
3396 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003397 curbuf->b_visual.vi_mode = VIsual_mode;
3398 curbuf->b_visual.vi_start = VIsual;
3399 curbuf->b_visual.vi_end = curwin->w_cursor;
3400 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#ifdef FEAT_EVAL
3402 curbuf->b_visual_mode_eval = VIsual_mode;
3403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#ifdef FEAT_VIRTUALEDIT
3405 if (!virtual_active())
3406 curwin->w_cursor.coladd = 0;
3407#endif
3408
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003409 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 clear_cmdline = TRUE; /* unshow visual mode later */
3411#ifdef FEAT_CMDL_INFO
3412 else
3413 clear_showcmd();
3414#endif
3415
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003416 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417}
3418
3419/*
3420 * Reset VIsual_active and VIsual_reselect.
3421 */
3422 void
3423reset_VIsual_and_resel()
3424{
3425 if (VIsual_active)
3426 {
3427 end_visual_mode();
3428 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3429 }
3430 VIsual_reselect = FALSE;
3431}
3432
3433/*
3434 * Reset VIsual_active and VIsual_reselect if it's set.
3435 */
3436 void
3437reset_VIsual()
3438{
3439 if (VIsual_active)
3440 {
3441 end_visual_mode();
3442 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3443 VIsual_reselect = FALSE;
3444 }
3445}
3446#endif /* FEAT_VISUAL */
3447
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3450
3451/*
3452 * Check for a balloon-eval special item to include when searching for an
3453 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3454 * Returns TRUE if the character at "*ptr" should be included.
3455 * "dir" is FORWARD or BACKWARD, the direction of searching.
3456 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3457 * "bnp" points to a counter for square brackets.
3458 */
3459 static int
3460find_is_eval_item(ptr, colp, bnp, dir)
3461 char_u *ptr;
3462 int *colp;
3463 int *bnp;
3464 int dir;
3465{
3466 /* Accept everything inside []. */
3467 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3468 ++*bnp;
3469 if (*bnp > 0)
3470 {
3471 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3472 --*bnp;
3473 return TRUE;
3474 }
3475
3476 /* skip over "s.var" */
3477 if (*ptr == '.')
3478 return TRUE;
3479
3480 /* two-character item: s->var */
3481 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3482 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3483 {
3484 *colp += dir;
3485 return TRUE;
3486 }
3487 return FALSE;
3488}
3489#endif
3490
3491/*
3492 * Find the identifier under or to the right of the cursor.
3493 * "find_type" can have one of three values:
3494 * FIND_IDENT: find an identifier (keyword)
3495 * FIND_STRING: find any non-white string
3496 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 *
3499 * There are three steps:
3500 * 1. Search forward for the start of an identifier/string. Doesn't move if
3501 * already on one.
3502 * 2. Search backward for the start of this identifier/string.
3503 * This doesn't match the real Vi but I like it a little better and it
3504 * shouldn't bother anyone.
3505 * 3. Search forward to the end of this identifier/string.
3506 * When FIND_IDENT isn't defined, we backup until a blank.
3507 *
3508 * Returns the length of the string, or zero if no string is found.
3509 * If a string is found, a pointer to the string is put in "*string". This
3510 * string is not always NUL terminated.
3511 */
3512 int
3513find_ident_under_cursor(string, find_type)
3514 char_u **string;
3515 int find_type;
3516{
3517 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3518 curwin->w_cursor.col, string, find_type);
3519}
3520
3521/*
3522 * Like find_ident_under_cursor(), but for any window and any position.
3523 * However: Uses 'iskeyword' from the current window!.
3524 */
3525 int
3526find_ident_at_pos(wp, lnum, startcol, string, find_type)
3527 win_T *wp;
3528 linenr_T lnum;
3529 colnr_T startcol;
3530 char_u **string;
3531 int find_type;
3532{
3533 char_u *ptr;
3534 int col = 0; /* init to shut up GCC */
3535 int i;
3536#ifdef FEAT_MBYTE
3537 int this_class = 0;
3538 int prev_class;
3539 int prevcol;
3540#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003541#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 int bn = 0; /* bracket nesting */
3543#endif
3544
3545 /*
3546 * if i == 0: try to find an identifier
3547 * if i == 1: try to find any non-white string
3548 */
3549 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3550 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3551 {
3552 /*
3553 * 1. skip to start of identifier/string
3554 */
3555 col = startcol;
3556#ifdef FEAT_MBYTE
3557 if (has_mbyte)
3558 {
3559 while (ptr[col] != NUL)
3560 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 /* Stop at a ']' to evaluate "a[x]". */
3563 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3564 break;
3565# endif
3566 this_class = mb_get_class(ptr + col);
3567 if (this_class != 0 && (i == 1 || this_class != 1))
3568 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003569 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 }
3572 else
3573#endif
3574 while (ptr[col] != NUL
3575 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3578# endif
3579 )
3580 ++col;
3581
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003582#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 /* When starting on a ']' count it, so that we include the '['. */
3584 bn = ptr[col] == ']';
3585#endif
3586
3587 /*
3588 * 2. Back up to start of identifier/string.
3589 */
3590#ifdef FEAT_MBYTE
3591 if (has_mbyte)
3592 {
3593 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003594# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3596 this_class = mb_get_class((char_u *)"a");
3597 else
3598# endif
3599 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003600 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3603 prev_class = mb_get_class(ptr + prevcol);
3604 if (this_class != prev_class
3605 && (i == 0
3606 || prev_class == 0
3607 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003608# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 && (!(find_type & FIND_EVAL)
3610 || prevcol == 0
3611 || !find_is_eval_item(ptr + prevcol, &prevcol,
3612 &bn, BACKWARD))
3613# endif
3614 )
3615 break;
3616 col = prevcol;
3617 }
3618
3619 /* If we don't want just any old string, or we've found an
3620 * identifier, stop searching. */
3621 if (this_class > 2)
3622 this_class = 2;
3623 if (!(find_type & FIND_STRING) || this_class == 2)
3624 break;
3625 }
3626 else
3627#endif
3628 {
3629 while (col > 0
3630 && ((i == 0
3631 ? vim_iswordc(ptr[col - 1])
3632 : (!vim_iswhite(ptr[col - 1])
3633 && (!(find_type & FIND_IDENT)
3634 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 || ((find_type & FIND_EVAL)
3637 && col > 1
3638 && find_is_eval_item(ptr + col - 1, &col,
3639 &bn, BACKWARD))
3640#endif
3641 ))
3642 --col;
3643
3644 /* If we don't want just any old string, or we've found an
3645 * identifier, stop searching. */
3646 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3647 break;
3648 }
3649 }
3650
3651 if (ptr[col] == NUL || (i == 0 && (
3652#ifdef FEAT_MBYTE
3653 has_mbyte ? this_class != 2 :
3654#endif
3655 !vim_iswordc(ptr[col]))))
3656 {
3657 /*
3658 * didn't find an identifier or string
3659 */
3660 if (find_type & FIND_STRING)
3661 EMSG(_("E348: No string under cursor"));
3662 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003663 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return 0;
3665 }
3666 ptr += col;
3667 *string = ptr;
3668
3669 /*
3670 * 3. Find the end if the identifier/string.
3671 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003672#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 bn = 0;
3674 startcol -= col;
3675#endif
3676 col = 0;
3677#ifdef FEAT_MBYTE
3678 if (has_mbyte)
3679 {
3680 /* Search for point of changing multibyte character class. */
3681 this_class = mb_get_class(ptr);
3682 while (ptr[col] != NUL
3683 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3684 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003685# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 || ((find_type & FIND_EVAL)
3687 && col <= (int)startcol
3688 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3689# endif
3690 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003691 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693 else
3694#endif
3695 while ((i == 0 ? vim_iswordc(ptr[col])
3696 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003697# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 || ((find_type & FIND_EVAL)
3699 && col <= (int)startcol
3700 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3701# endif
3702 )
3703 {
3704 ++col;
3705 }
3706
3707 return col;
3708}
3709
3710/*
3711 * Prepare for redo of a normal command.
3712 */
3713 static void
3714prep_redo_cmd(cap)
3715 cmdarg_T *cap;
3716{
3717 prep_redo(cap->oap->regname, cap->count0,
3718 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3719}
3720
3721/*
3722 * Prepare for redo of any command.
3723 * Note that only the last argument can be a multi-byte char.
3724 */
3725 static void
3726prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3727 int regname;
3728 long num;
3729 int cmd1;
3730 int cmd2;
3731 int cmd3;
3732 int cmd4;
3733 int cmd5;
3734{
3735 ResetRedobuff();
3736 if (regname != 0) /* yank from specified buffer */
3737 {
3738 AppendCharToRedobuff('"');
3739 AppendCharToRedobuff(regname);
3740 }
3741 if (num)
3742 AppendNumberToRedobuff(num);
3743
3744 if (cmd1 != NUL)
3745 AppendCharToRedobuff(cmd1);
3746 if (cmd2 != NUL)
3747 AppendCharToRedobuff(cmd2);
3748 if (cmd3 != NUL)
3749 AppendCharToRedobuff(cmd3);
3750 if (cmd4 != NUL)
3751 AppendCharToRedobuff(cmd4);
3752 if (cmd5 != NUL)
3753 AppendCharToRedobuff(cmd5);
3754}
3755
3756/*
3757 * check for operator active and clear it
3758 *
3759 * return TRUE if operator was active
3760 */
3761 static int
3762checkclearop(oap)
3763 oparg_T *oap;
3764{
3765 if (oap->op_type == OP_NOP)
3766 return FALSE;
3767 clearopbeep(oap);
3768 return TRUE;
3769}
3770
3771/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003772 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003774 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 static int
3777checkclearopq(oap)
3778 oparg_T *oap;
3779{
3780 if (oap->op_type == OP_NOP
3781#ifdef FEAT_VISUAL
3782 && !VIsual_active
3783#endif
3784 )
3785 return FALSE;
3786 clearopbeep(oap);
3787 return TRUE;
3788}
3789
3790 static void
3791clearop(oap)
3792 oparg_T *oap;
3793{
3794 oap->op_type = OP_NOP;
3795 oap->regname = 0;
3796 oap->motion_force = NUL;
3797 oap->use_reg_one = FALSE;
3798}
3799
3800 static void
3801clearopbeep(oap)
3802 oparg_T *oap;
3803{
3804 clearop(oap);
3805 beep_flush();
3806}
3807
3808#ifdef FEAT_VISUAL
3809/*
3810 * Remove the shift modifier from a special key.
3811 */
3812 static void
3813unshift_special(cap)
3814 cmdarg_T *cap;
3815{
3816 switch (cap->cmdchar)
3817 {
3818 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3819 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3820 case K_S_UP: cap->cmdchar = K_UP; break;
3821 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3822 case K_S_HOME: cap->cmdchar = K_HOME; break;
3823 case K_S_END: cap->cmdchar = K_END; break;
3824 }
3825 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3826}
3827#endif
3828
3829#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3830/*
3831 * Routines for displaying a partly typed command
3832 */
3833
3834#ifdef FEAT_VISUAL /* need room for size of Visual area */
3835# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3836#else
3837# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3838#endif
3839static char_u showcmd_buf[SHOWCMD_BUFLEN];
3840static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3841static int showcmd_is_clear = TRUE;
3842static int showcmd_visual = FALSE;
3843
3844static void display_showcmd __ARGS((void));
3845
3846 void
3847clear_showcmd()
3848{
3849 if (!p_sc)
3850 return;
3851
3852#ifdef FEAT_VISUAL
3853 if (VIsual_active && !char_avail())
3854 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003855 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 long lines;
3857 colnr_T leftcol, rightcol;
3858 linenr_T top, bot;
3859
3860 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003861 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
3863 top = VIsual.lnum;
3864 bot = curwin->w_cursor.lnum;
3865 }
3866 else
3867 {
3868 top = curwin->w_cursor.lnum;
3869 bot = VIsual.lnum;
3870 }
3871# ifdef FEAT_FOLDING
3872 /* Include closed folds as a whole. */
3873 hasFolding(top, &top, NULL);
3874 hasFolding(bot, NULL, &bot);
3875# endif
3876 lines = bot - top + 1;
3877
3878 if (VIsual_mode == Ctrl_V)
3879 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003880# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003881 char_u *saved_sbr = p_sbr;
3882
3883 /* Make 'sbr' empty for a moment to get the correct size. */
3884 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003887# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003888 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3891 (long)(rightcol - leftcol + 1));
3892 }
3893 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3894 sprintf((char *)showcmd_buf, "%ld", lines);
3895 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003896 {
3897 char_u *s, *e;
3898 int l;
3899 int bytes = 0;
3900 int chars = 0;
3901
3902 if (cursor_bot)
3903 {
3904 s = ml_get_pos(&VIsual);
3905 e = ml_get_cursor();
3906 }
3907 else
3908 {
3909 s = ml_get_cursor();
3910 e = ml_get_pos(&VIsual);
3911 }
3912 while ((*p_sel != 'e') ? s <= e : s < e)
3913 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003914# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003915 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003916# else
3917 l = (*s == NUL) ? 0 : 1;
3918# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003919 if (l == 0)
3920 {
3921 ++bytes;
3922 ++chars;
3923 break; /* end of line */
3924 }
3925 bytes += l;
3926 ++chars;
3927 s += l;
3928 }
3929 if (bytes == chars)
3930 sprintf((char *)showcmd_buf, "%d", chars);
3931 else
3932 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3935 showcmd_visual = TRUE;
3936 }
3937 else
3938#endif
3939 {
3940 showcmd_buf[0] = NUL;
3941 showcmd_visual = FALSE;
3942
3943 /* Don't actually display something if there is nothing to clear. */
3944 if (showcmd_is_clear)
3945 return;
3946 }
3947
3948 display_showcmd();
3949}
3950
3951/*
3952 * Add 'c' to string of shown command chars.
3953 * Return TRUE if output has been written (and setcursor() has been called).
3954 */
3955 int
3956add_to_showcmd(c)
3957 int c;
3958{
3959 char_u *p;
3960 int old_len;
3961 int extra_len;
3962 int overflow;
3963#if defined(FEAT_MOUSE)
3964 int i;
3965 static int ignore[] =
3966 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003967# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3969 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 K_IGNORE,
3972 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3973 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3974 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003975 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003977 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 0
3979 };
3980#endif
3981
Bram Moolenaar09df3122006-01-23 22:23:09 +00003982 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 return FALSE;
3984
3985 if (showcmd_visual)
3986 {
3987 showcmd_buf[0] = NUL;
3988 showcmd_visual = FALSE;
3989 }
3990
3991#if defined(FEAT_MOUSE)
3992 /* Ignore keys that are scrollbar updates and mouse clicks */
3993 if (IS_SPECIAL(c))
3994 for (i = 0; ignore[i] != 0; ++i)
3995 if (ignore[i] == c)
3996 return FALSE;
3997#endif
3998
3999 p = transchar(c);
4000 old_len = (int)STRLEN(showcmd_buf);
4001 extra_len = (int)STRLEN(p);
4002 overflow = old_len + extra_len - SHOWCMD_COLS;
4003 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004004 mch_memmove(showcmd_buf, showcmd_buf + overflow,
4005 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 STRCAT(showcmd_buf, p);
4007
4008 if (char_avail())
4009 return FALSE;
4010
4011 display_showcmd();
4012
4013 return TRUE;
4014}
4015
4016 void
4017add_to_showcmd_c(c)
4018 int c;
4019{
4020 if (!add_to_showcmd(c))
4021 setcursor();
4022}
4023
4024/*
4025 * Delete 'len' characters from the end of the shown command.
4026 */
4027 static void
4028del_from_showcmd(len)
4029 int len;
4030{
4031 int old_len;
4032
4033 if (!p_sc)
4034 return;
4035
4036 old_len = (int)STRLEN(showcmd_buf);
4037 if (len > old_len)
4038 len = old_len;
4039 showcmd_buf[old_len - len] = NUL;
4040
4041 if (!char_avail())
4042 display_showcmd();
4043}
4044
4045/*
4046 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
4047 * something and there is a partial mapping.
4048 */
4049 void
4050push_showcmd()
4051{
4052 if (p_sc)
4053 STRCPY(old_showcmd_buf, showcmd_buf);
4054}
4055
4056 void
4057pop_showcmd()
4058{
4059 if (!p_sc)
4060 return;
4061
4062 STRCPY(showcmd_buf, old_showcmd_buf);
4063
4064 display_showcmd();
4065}
4066
4067 static void
4068display_showcmd()
4069{
4070 int len;
4071
4072 cursor_off();
4073
4074 len = (int)STRLEN(showcmd_buf);
4075 if (len == 0)
4076 showcmd_is_clear = TRUE;
4077 else
4078 {
4079 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4080 showcmd_is_clear = FALSE;
4081 }
4082
4083 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004084 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4085 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 */
4087 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4088
4089 setcursor(); /* put cursor back where it belongs */
4090}
4091#endif
4092
4093#ifdef FEAT_SCROLLBIND
4094/*
4095 * When "check" is FALSE, prepare for commands that scroll the window.
4096 * When "check" is TRUE, take care of scroll-binding after the window has
4097 * scrolled. Called from normal_cmd() and edit().
4098 */
4099 void
4100do_check_scrollbind(check)
4101 int check;
4102{
4103 static win_T *old_curwin = NULL;
4104 static linenr_T old_topline = 0;
4105#ifdef FEAT_DIFF
4106 static int old_topfill = 0;
4107#endif
4108 static buf_T *old_buf = NULL;
4109 static colnr_T old_leftcol = 0;
4110
4111 if (check && curwin->w_p_scb)
4112 {
4113 /* If a ":syncbind" command was just used, don't scroll, only reset
4114 * the values. */
4115 if (did_syncbind)
4116 did_syncbind = FALSE;
4117 else if (curwin == old_curwin)
4118 {
4119 /*
4120 * Synchronize other windows, as necessary according to
4121 * 'scrollbind'. Don't do this after an ":edit" command, except
4122 * when 'diff' is set.
4123 */
4124 if ((curwin->w_buffer == old_buf
4125#ifdef FEAT_DIFF
4126 || curwin->w_p_diff
4127#endif
4128 )
4129 && (curwin->w_topline != old_topline
4130#ifdef FEAT_DIFF
4131 || curwin->w_topfill != old_topfill
4132#endif
4133 || curwin->w_leftcol != old_leftcol))
4134 {
4135 check_scrollbind(curwin->w_topline - old_topline,
4136 (long)(curwin->w_leftcol - old_leftcol));
4137 }
4138 }
4139 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4140 {
4141 /*
4142 * When switching between windows, make sure that the relative
4143 * vertical offset is valid for the new window. The relative
4144 * offset is invalid whenever another 'scrollbind' window has
4145 * scrolled to a point that would force the current window to
4146 * scroll past the beginning or end of its buffer. When the
4147 * resync is performed, some of the other 'scrollbind' windows may
4148 * need to jump so that the current window's relative position is
4149 * visible on-screen.
4150 */
4151 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4152 }
4153 curwin->w_scbind_pos = curwin->w_topline;
4154 }
4155
4156 old_curwin = curwin;
4157 old_topline = curwin->w_topline;
4158#ifdef FEAT_DIFF
4159 old_topfill = curwin->w_topfill;
4160#endif
4161 old_buf = curwin->w_buffer;
4162 old_leftcol = curwin->w_leftcol;
4163}
4164
4165/*
4166 * Synchronize any windows that have "scrollbind" set, based on the
4167 * number of rows by which the current window has changed
4168 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4169 */
4170 void
4171check_scrollbind(topline_diff, leftcol_diff)
4172 linenr_T topline_diff;
4173 long leftcol_diff;
4174{
4175 int want_ver;
4176 int want_hor;
4177 win_T *old_curwin = curwin;
4178 buf_T *old_curbuf = curbuf;
4179#ifdef FEAT_VISUAL
4180 int old_VIsual_select = VIsual_select;
4181 int old_VIsual_active = VIsual_active;
4182#endif
4183 colnr_T tgt_leftcol = curwin->w_leftcol;
4184 long topline;
4185 long y;
4186
4187 /*
4188 * check 'scrollopt' string for vertical and horizontal scroll options
4189 */
4190 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4191#ifdef FEAT_DIFF
4192 want_ver |= old_curwin->w_p_diff;
4193#endif
4194 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4195
4196 /*
4197 * loop through the scrollbound windows and scroll accordingly
4198 */
4199#ifdef FEAT_VISUAL
4200 VIsual_select = VIsual_active = 0;
4201#endif
4202 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4203 {
4204 curbuf = curwin->w_buffer;
4205 /* skip original window and windows with 'noscrollbind' */
4206 if (curwin != old_curwin && curwin->w_p_scb)
4207 {
4208 /*
4209 * do the vertical scroll
4210 */
4211 if (want_ver)
4212 {
4213#ifdef FEAT_DIFF
4214 if (old_curwin->w_p_diff && curwin->w_p_diff)
4215 {
4216 diff_set_topline(old_curwin, curwin);
4217 }
4218 else
4219#endif
4220 {
4221 curwin->w_scbind_pos += topline_diff;
4222 topline = curwin->w_scbind_pos;
4223 if (topline > curbuf->b_ml.ml_line_count)
4224 topline = curbuf->b_ml.ml_line_count;
4225 if (topline < 1)
4226 topline = 1;
4227
4228 y = topline - curwin->w_topline;
4229 if (y > 0)
4230 scrollup(y, FALSE);
4231 else
4232 scrolldown(-y, FALSE);
4233 }
4234
4235 redraw_later(VALID);
4236 cursor_correct();
4237#ifdef FEAT_WINDOWS
4238 curwin->w_redr_status = TRUE;
4239#endif
4240 }
4241
4242 /*
4243 * do the horizontal scroll
4244 */
4245 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4246 {
4247 curwin->w_leftcol = tgt_leftcol;
4248 leftcol_changed();
4249 }
4250 }
4251 }
4252
4253 /*
4254 * reset current-window
4255 */
4256#ifdef FEAT_VISUAL
4257 VIsual_select = old_VIsual_select;
4258 VIsual_active = old_VIsual_active;
4259#endif
4260 curwin = old_curwin;
4261 curbuf = old_curbuf;
4262}
4263#endif /* #ifdef FEAT_SCROLLBIND */
4264
4265/*
4266 * Command character that's ignored.
4267 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004268 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 static void
4271nv_ignore(cap)
4272 cmdarg_T *cap;
4273{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004274 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275}
4276
4277/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004278 * Command character that doesn't do anything, but unlike nv_ignore() does
4279 * start edit(). Used for "startinsert" executed while starting up.
4280 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004281 static void
4282nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004283 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004284{
4285}
4286
4287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 * Command character doesn't exist.
4289 */
4290 static void
4291nv_error(cap)
4292 cmdarg_T *cap;
4293{
4294 clearopbeep(cap->oap);
4295}
4296
4297/*
4298 * <Help> and <F1> commands.
4299 */
4300 static void
4301nv_help(cap)
4302 cmdarg_T *cap;
4303{
4304 if (!checkclearopq(cap->oap))
4305 ex_help(NULL);
4306}
4307
4308/*
4309 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4310 */
4311 static void
4312nv_addsub(cap)
4313 cmdarg_T *cap;
4314{
4315 if (!checkclearopq(cap->oap)
4316 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4317 prep_redo_cmd(cap);
4318}
4319
4320/*
4321 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4322 */
4323 static void
4324nv_page(cap)
4325 cmdarg_T *cap;
4326{
4327 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004328 {
4329#ifdef FEAT_WINDOWS
4330 if (mod_mask & MOD_MASK_CTRL)
4331 {
4332 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4333 if (cap->arg == BACKWARD)
4334 goto_tabpage(-(int)cap->count1);
4335 else
4336 goto_tabpage((int)cap->count0);
4337 }
4338 else
4339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342}
4343
4344/*
4345 * Implementation of "gd" and "gD" command.
4346 */
4347 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004348nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004349 oparg_T *oap;
4350 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004351 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
4353 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 char_u *ptr;
4355
Bram Moolenaard9d30582005-05-18 22:10:28 +00004356 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004357 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004359#ifdef FEAT_FOLDING
4360 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4361 foldOpenCursor();
4362#endif
4363}
4364
4365/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004366 * Search for variable declaration of "ptr[len]".
4367 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4368 * current file ("gD").
4369 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004370 * Return FAIL when not found.
4371 */
4372 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004373find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004374 char_u *ptr;
4375 int len;
4376 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004377 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004378 int searchflags; /* flags passed to searchit() */
4379{
4380 char_u *pat;
4381 pos_T old_pos;
4382 pos_T par_pos;
4383 pos_T found_pos;
4384 int t;
4385 int save_p_ws;
4386 int save_p_scs;
4387 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004388 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004389
4390 if ((pat = alloc(len + 7)) == NULL)
4391 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004392
4393 /* Put "\V" before the pattern to avoid that the special meaning of "."
4394 * and "~" causes trouble. */
4395 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4396 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 old_pos = curwin->w_cursor;
4398 save_p_ws = p_ws;
4399 save_p_scs = p_scs;
4400 p_ws = FALSE; /* don't wrap around end of file now */
4401 p_scs = FALSE; /* don't switch ignorecase off now */
4402
4403 /*
4404 * With "gD" go to line 1.
4405 * With "gd" Search back for the start of the current function, then go
4406 * back until a blank line. If this fails go to line 1.
4407 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004408 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
4410 setpcmark(); /* Set in findpar() otherwise */
4411 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004412 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 else
4415 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004416 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4418 --curwin->w_cursor.lnum;
4419 }
4420 curwin->w_cursor.col = 0;
4421
4422 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004423 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004424 for (;;)
4425 {
4426 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004427 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 if (curwin->w_cursor.lnum >= old_pos.lnum)
4429 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004430
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004431 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004432 {
4433 pos_T *pos;
4434
4435 /* Check that the block the match is in doesn't end before the
4436 * position where we started the search from. */
4437 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4438 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4439 && pos->lnum < old_pos.lnum)
4440 continue;
4441 }
4442
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004443 if (t == FAIL)
4444 {
4445 /* If we previously found a valid position, use it. */
4446 if (found_pos.lnum != 0)
4447 {
4448 curwin->w_cursor = found_pos;
4449 t = OK;
4450 }
4451 break;
4452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004454 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004455 {
4456 /* Ignore this line, continue at start of next line. */
4457 ++curwin->w_cursor.lnum;
4458 curwin->w_cursor.col = 0;
4459 continue;
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004462 if (!locally) /* global search: use first match found */
4463 break;
4464 if (curwin->w_cursor.lnum >= par_pos.lnum)
4465 {
4466 /* If we previously found a valid position, use it. */
4467 if (found_pos.lnum != 0)
4468 curwin->w_cursor = found_pos;
4469 break;
4470 }
4471
4472 /* For finding a local variable and the match is before the "{" search
4473 * to find a later match. For K&R style function declarations this
4474 * skips the function header without types. */
4475 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004477
4478 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004480 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 curwin->w_cursor = old_pos;
4482 }
4483 else
4484 {
4485 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 /* "n" searches forward now */
4487 reset_search_dir();
4488 }
4489
4490 vim_free(pat);
4491 p_ws = save_p_ws;
4492 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004493
4494 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
4496
4497/*
4498 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4499 * lines rather than lines in the file.
4500 * 'dist' must be positive.
4501 *
4502 * Return OK if able to move cursor, FAIL otherwise.
4503 */
4504 static int
4505nv_screengo(oap, dir, dist)
4506 oparg_T *oap;
4507 int dir;
4508 long dist;
4509{
4510 int linelen = linetabsize(ml_get_curline());
4511 int retval = OK;
4512 int atend = FALSE;
4513 int n;
4514 int col_off1; /* margin offset for first screen line */
4515 int col_off2; /* margin offset for wrapped screen line */
4516 int width1; /* text width for first screen line */
4517 int width2; /* test width for wrapped screen line */
4518
4519 oap->motion_type = MCHAR;
4520 oap->inclusive = FALSE;
4521
4522 col_off1 = curwin_col_off();
4523 col_off2 = col_off1 - curwin_col_off2();
4524 width1 = W_WIDTH(curwin) - col_off1;
4525 width2 = W_WIDTH(curwin) - col_off2;
4526
4527#ifdef FEAT_VERTSPLIT
4528 if (curwin->w_width != 0)
4529 {
4530#endif
4531 /*
4532 * Instead of sticking at the last character of the buffer line we
4533 * try to stick in the last column of the screen.
4534 */
4535 if (curwin->w_curswant == MAXCOL)
4536 {
4537 atend = TRUE;
4538 validate_virtcol();
4539 if (width1 <= 0)
4540 curwin->w_curswant = 0;
4541 else
4542 {
4543 curwin->w_curswant = width1 - 1;
4544 if (curwin->w_virtcol > curwin->w_curswant)
4545 curwin->w_curswant += ((curwin->w_virtcol
4546 - curwin->w_curswant - 1) / width2 + 1) * width2;
4547 }
4548 }
4549 else
4550 {
4551 if (linelen > width1)
4552 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4553 else
4554 n = width1;
4555 if (curwin->w_curswant > (colnr_T)n + 1)
4556 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4557 * width2;
4558 }
4559
4560 while (dist--)
4561 {
4562 if (dir == BACKWARD)
4563 {
4564 if ((long)curwin->w_curswant >= width2)
4565 /* move back within line */
4566 curwin->w_curswant -= width2;
4567 else
4568 {
4569 /* to previous line */
4570 if (curwin->w_cursor.lnum == 1)
4571 {
4572 retval = FAIL;
4573 break;
4574 }
4575 --curwin->w_cursor.lnum;
4576#ifdef FEAT_FOLDING
4577 /* Move to the start of a closed fold. Don't do that when
4578 * 'foldopen' contains "all": it will open in a moment. */
4579 if (!(fdo_flags & FDO_ALL))
4580 (void)hasFolding(curwin->w_cursor.lnum,
4581 &curwin->w_cursor.lnum, NULL);
4582#endif
4583 linelen = linetabsize(ml_get_curline());
4584 if (linelen > width1)
4585 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4586 + 1) * width2;
4587 }
4588 }
4589 else /* dir == FORWARD */
4590 {
4591 if (linelen > width1)
4592 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4593 else
4594 n = width1;
4595 if (curwin->w_curswant + width2 < (colnr_T)n)
4596 /* move forward within line */
4597 curwin->w_curswant += width2;
4598 else
4599 {
4600 /* to next line */
4601#ifdef FEAT_FOLDING
4602 /* Move to the end of a closed fold. */
4603 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4604 &curwin->w_cursor.lnum);
4605#endif
4606 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4607 {
4608 retval = FAIL;
4609 break;
4610 }
4611 curwin->w_cursor.lnum++;
4612 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004613 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 }
4616 }
4617#ifdef FEAT_VERTSPLIT
4618 }
4619#endif
4620
4621 coladvance(curwin->w_curswant);
4622
4623#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4624 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4625 {
4626 /*
4627 * Check for landing on a character that got split at the end of the
4628 * last line. We want to advance a screenline, not end up in the same
4629 * screenline or move two screenlines.
4630 */
4631 validate_virtcol();
4632 if (curwin->w_virtcol > curwin->w_curswant
4633 && (curwin->w_curswant < (colnr_T)width1
4634 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4635 : ((curwin->w_curswant - width1) % width2
4636 > (colnr_T)width2 / 2)))
4637 --curwin->w_cursor.col;
4638 }
4639#endif
4640
4641 if (atend)
4642 curwin->w_curswant = MAXCOL; /* stick in the last column */
4643
4644 return retval;
4645}
4646
4647#ifdef FEAT_MOUSE
4648/*
4649 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4650 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004651 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4652 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 */
4654 static void
4655nv_mousescroll(cap)
4656 cmdarg_T *cap;
4657{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004658# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004659 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004661 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
4663 int row, col;
4664
4665 row = mouse_row;
4666 col = mouse_col;
4667
4668 /* find the window at the pointer coordinates */
4669 curwin = mouse_find_win(&row, &col);
4670 curbuf = curwin->w_buffer;
4671 }
4672# endif
4673
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004674 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004676 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4677 {
4678 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4679 }
4680 else
4681 {
4682 cap->count1 = 3;
4683 cap->count0 = 3;
4684 nv_scroll_line(cap);
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004687# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else
4689 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004690 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4691 if (!curwin->w_p_wrap)
4692 {
4693 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004694
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004695 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4696 step = W_WIDTH(curwin);
4697 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4698 if (val < 0)
4699 val = 0;
4700
4701 gui_do_horiz_scroll(val, TRUE);
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004706# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 curwin->w_redr_status = TRUE;
4708
4709 curwin = old_curwin;
4710 curbuf = curwin->w_buffer;
4711# endif
4712}
4713
4714/*
4715 * Mouse clicks and drags.
4716 */
4717 static void
4718nv_mouse(cap)
4719 cmdarg_T *cap;
4720{
4721 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4722}
4723#endif
4724
4725/*
4726 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4727 * cap->arg must be TRUE for CTRL-E.
4728 */
4729 static void
4730nv_scroll_line(cap)
4731 cmdarg_T *cap;
4732{
4733 if (!checkclearop(cap->oap))
4734 scroll_redraw(cap->arg, cap->count1);
4735}
4736
4737/*
4738 * Scroll "count" lines up or down, and redraw.
4739 */
4740 void
4741scroll_redraw(up, count)
4742 int up;
4743 long count;
4744{
4745 linenr_T prev_topline = curwin->w_topline;
4746#ifdef FEAT_DIFF
4747 int prev_topfill = curwin->w_topfill;
4748#endif
4749 linenr_T prev_lnum = curwin->w_cursor.lnum;
4750
4751 if (up)
4752 scrollup(count, TRUE);
4753 else
4754 scrolldown(count, TRUE);
4755 if (p_so)
4756 {
4757 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4758 * valid, otherwise the screen jumps back at the end of the file. */
4759 cursor_correct();
4760 check_cursor_moved(curwin);
4761 curwin->w_valid |= VALID_TOPLINE;
4762
4763 /* If moved back to where we were, at least move the cursor, otherwise
4764 * we get stuck at one position. Don't move the cursor up if the
4765 * first line of the buffer is already on the screen */
4766 while (curwin->w_topline == prev_topline
4767#ifdef FEAT_DIFF
4768 && curwin->w_topfill == prev_topfill
4769#endif
4770 )
4771 {
4772 if (up)
4773 {
4774 if (curwin->w_cursor.lnum > prev_lnum
4775 || cursor_down(1L, FALSE) == FAIL)
4776 break;
4777 }
4778 else
4779 {
4780 if (curwin->w_cursor.lnum < prev_lnum
4781 || prev_topline == 1L
4782 || cursor_up(1L, FALSE) == FAIL)
4783 break;
4784 }
4785 /* Mark w_topline as valid, otherwise the screen jumps back at the
4786 * end of the file. */
4787 check_cursor_moved(curwin);
4788 curwin->w_valid |= VALID_TOPLINE;
4789 }
4790 }
4791 if (curwin->w_cursor.lnum != prev_lnum)
4792 coladvance(curwin->w_curswant);
4793 redraw_later(VALID);
4794}
4795
4796/*
4797 * Commands that start with "z".
4798 */
4799 static void
4800nv_zet(cap)
4801 cmdarg_T *cap;
4802{
4803 long n;
4804 colnr_T col;
4805 int nchar = cap->nchar;
4806#ifdef FEAT_FOLDING
4807 long old_fdl = curwin->w_p_fdl;
4808 int old_fen = curwin->w_p_fen;
4809#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004810#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004811 int undo = FALSE;
4812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 if (VIM_ISDIGIT(nchar))
4815 {
4816 /*
4817 * "z123{nchar}": edit the count before obtaining {nchar}
4818 */
4819 if (checkclearop(cap->oap))
4820 return;
4821 n = nchar - '0';
4822 for (;;)
4823 {
4824#ifdef USE_ON_FLY_SCROLL
4825 dont_scroll = TRUE; /* disallow scrolling here */
4826#endif
4827 ++no_mapping;
4828 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004829 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 --no_mapping;
4832 --allow_keys;
4833#ifdef FEAT_CMDL_INFO
4834 (void)add_to_showcmd(nchar);
4835#endif
4836 if (nchar == K_DEL || nchar == K_KDEL)
4837 n /= 10;
4838 else if (VIM_ISDIGIT(nchar))
4839 n = n * 10 + (nchar - '0');
4840 else if (nchar == CAR)
4841 {
4842#ifdef FEAT_GUI
4843 need_mouse_correct = TRUE;
4844#endif
4845 win_setheight((int)n);
4846 break;
4847 }
4848 else if (nchar == 'l'
4849 || nchar == 'h'
4850 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004851 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 {
4853 cap->count1 = n ? n * cap->count1 : cap->count1;
4854 goto dozet;
4855 }
4856 else
4857 {
4858 clearopbeep(cap->oap);
4859 break;
4860 }
4861 }
4862 cap->oap->op_type = OP_NOP;
4863 return;
4864 }
4865
4866dozet:
4867 if (
4868#ifdef FEAT_FOLDING
4869 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4870 * and "zC" only in Visual mode. "zj" and "zk" are motion
4871 * commands. */
4872 cap->nchar != 'f' && cap->nchar != 'F'
4873 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4874 && cap->nchar != 'j' && cap->nchar != 'k'
4875 &&
4876#endif
4877 checkclearop(cap->oap))
4878 return;
4879
4880 /*
4881 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4882 * If line number given, set cursor.
4883 */
4884 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4885 && cap->count0
4886 && cap->count0 != curwin->w_cursor.lnum)
4887 {
4888 setpcmark();
4889 if (cap->count0 > curbuf->b_ml.ml_line_count)
4890 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4891 else
4892 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004893 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895
4896 switch (nchar)
4897 {
4898 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4899 case '+':
4900 if (cap->count0 == 0)
4901 {
4902 /* No count given: put cursor at the line below screen */
4903 validate_botline(); /* make sure w_botline is valid */
4904 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4905 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4906 else
4907 curwin->w_cursor.lnum = curwin->w_botline;
4908 }
4909 /* FALLTHROUGH */
4910 case NL:
4911 case CAR:
4912 case K_KENTER:
4913 beginline(BL_WHITE | BL_FIX);
4914 /* FALLTHROUGH */
4915
4916 case 't': scroll_cursor_top(0, TRUE);
4917 redraw_later(VALID);
4918 break;
4919
4920 /* "z." and "zz": put cursor in middle of screen */
4921 case '.': beginline(BL_WHITE | BL_FIX);
4922 /* FALLTHROUGH */
4923
4924 case 'z': scroll_cursor_halfway(TRUE);
4925 redraw_later(VALID);
4926 break;
4927
4928 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4929 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4930 * when <count> is at bottom of window, and puts that one at
4931 * bottom of window. */
4932 if (cap->count0 != 0)
4933 {
4934 scroll_cursor_bot(0, TRUE);
4935 curwin->w_cursor.lnum = curwin->w_topline;
4936 }
4937 else if (curwin->w_topline == 1)
4938 curwin->w_cursor.lnum = 1;
4939 else
4940 curwin->w_cursor.lnum = curwin->w_topline - 1;
4941 /* FALLTHROUGH */
4942 case '-':
4943 beginline(BL_WHITE | BL_FIX);
4944 /* FALLTHROUGH */
4945
4946 case 'b': scroll_cursor_bot(0, TRUE);
4947 redraw_later(VALID);
4948 break;
4949
4950 /* "zH" - scroll screen right half-page */
4951 case 'H':
4952 cap->count1 *= W_WIDTH(curwin) / 2;
4953 /* FALLTHROUGH */
4954
4955 /* "zh" - scroll screen to the right */
4956 case 'h':
4957 case K_LEFT:
4958 if (!curwin->w_p_wrap)
4959 {
4960 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4961 curwin->w_leftcol = 0;
4962 else
4963 curwin->w_leftcol -= (colnr_T)cap->count1;
4964 leftcol_changed();
4965 }
4966 break;
4967
4968 /* "zL" - scroll screen left half-page */
4969 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4970 /* FALLTHROUGH */
4971
4972 /* "zl" - scroll screen to the left */
4973 case 'l':
4974 case K_RIGHT:
4975 if (!curwin->w_p_wrap)
4976 {
4977 /* scroll the window left */
4978 curwin->w_leftcol += (colnr_T)cap->count1;
4979 leftcol_changed();
4980 }
4981 break;
4982
4983 /* "zs" - scroll screen, cursor at the start */
4984 case 's': if (!curwin->w_p_wrap)
4985 {
4986#ifdef FEAT_FOLDING
4987 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4988 col = 0; /* like the cursor is in col 0 */
4989 else
4990#endif
4991 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4992 if ((long)col > p_siso)
4993 col -= p_siso;
4994 else
4995 col = 0;
4996 if (curwin->w_leftcol != col)
4997 {
4998 curwin->w_leftcol = col;
4999 redraw_later(NOT_VALID);
5000 }
5001 }
5002 break;
5003
5004 /* "ze" - scroll screen, cursor at the end */
5005 case 'e': if (!curwin->w_p_wrap)
5006 {
5007#ifdef FEAT_FOLDING
5008 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5009 col = 0; /* like the cursor is in col 0 */
5010 else
5011#endif
5012 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
5013 n = W_WIDTH(curwin) - curwin_col_off();
5014 if ((long)col + p_siso < n)
5015 col = 0;
5016 else
5017 col = col + p_siso - n + 1;
5018 if (curwin->w_leftcol != col)
5019 {
5020 curwin->w_leftcol = col;
5021 redraw_later(NOT_VALID);
5022 }
5023 }
5024 break;
5025
5026#ifdef FEAT_FOLDING
5027 /* "zF": create fold command */
5028 /* "zf": create fold operator */
5029 case 'F':
5030 case 'f': if (foldManualAllowed(TRUE))
5031 {
5032 cap->nchar = 'f';
5033 nv_operator(cap);
5034 curwin->w_p_fen = TRUE;
5035
5036 /* "zF" is like "zfzf" */
5037 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5038 {
5039 nv_operator(cap);
5040 finish_op = TRUE;
5041 }
5042 }
5043 else
5044 clearopbeep(cap->oap);
5045 break;
5046
5047 /* "zd": delete fold at cursor */
5048 /* "zD": delete fold at cursor recursively */
5049 case 'd':
5050 case 'D': if (foldManualAllowed(FALSE))
5051 {
5052 if (VIsual_active)
5053 nv_operator(cap);
5054 else
5055 deleteFold(curwin->w_cursor.lnum,
5056 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5057 }
5058 break;
5059
5060 /* "zE": erease all folds */
5061 case 'E': if (foldmethodIsManual(curwin))
5062 {
5063 clearFolding(curwin);
5064 changed_window_setting();
5065 }
5066 else if (foldmethodIsMarker(curwin))
5067 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5068 TRUE, FALSE);
5069 else
5070 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5071 break;
5072
5073 /* "zn": fold none: reset 'foldenable' */
5074 case 'n': curwin->w_p_fen = FALSE;
5075 break;
5076
5077 /* "zN": fold Normal: set 'foldenable' */
5078 case 'N': curwin->w_p_fen = TRUE;
5079 break;
5080
5081 /* "zi": invert folding: toggle 'foldenable' */
5082 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5083 break;
5084
5085 /* "za": open closed fold or close open fold at cursor */
5086 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5087 openFold(curwin->w_cursor.lnum, cap->count1);
5088 else
5089 {
5090 closeFold(curwin->w_cursor.lnum, cap->count1);
5091 curwin->w_p_fen = TRUE;
5092 }
5093 break;
5094
5095 /* "zA": open fold at cursor recursively */
5096 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5097 openFoldRecurse(curwin->w_cursor.lnum);
5098 else
5099 {
5100 closeFoldRecurse(curwin->w_cursor.lnum);
5101 curwin->w_p_fen = TRUE;
5102 }
5103 break;
5104
5105 /* "zo": open fold at cursor or Visual area */
5106 case 'o': if (VIsual_active)
5107 nv_operator(cap);
5108 else
5109 openFold(curwin->w_cursor.lnum, cap->count1);
5110 break;
5111
5112 /* "zO": open fold recursively */
5113 case 'O': if (VIsual_active)
5114 nv_operator(cap);
5115 else
5116 openFoldRecurse(curwin->w_cursor.lnum);
5117 break;
5118
5119 /* "zc": close fold at cursor or Visual area */
5120 case 'c': if (VIsual_active)
5121 nv_operator(cap);
5122 else
5123 closeFold(curwin->w_cursor.lnum, cap->count1);
5124 curwin->w_p_fen = TRUE;
5125 break;
5126
5127 /* "zC": close fold recursively */
5128 case 'C': if (VIsual_active)
5129 nv_operator(cap);
5130 else
5131 closeFoldRecurse(curwin->w_cursor.lnum);
5132 curwin->w_p_fen = TRUE;
5133 break;
5134
5135 /* "zv": open folds at the cursor */
5136 case 'v': foldOpenCursor();
5137 break;
5138
5139 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5140 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005141 curwin->w_foldinvalid = TRUE; /* recompute folds */
5142 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 foldOpenCursor();
5144 break;
5145
5146 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5147 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005148 curwin->w_foldinvalid = TRUE; /* recompute folds */
5149 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151
5152 /* "zm": fold more */
5153 case 'm': if (curwin->w_p_fdl > 0)
5154 --curwin->w_p_fdl;
5155 old_fdl = -1; /* force an update */
5156 curwin->w_p_fen = TRUE;
5157 break;
5158
5159 /* "zM": close all folds */
5160 case 'M': curwin->w_p_fdl = 0;
5161 old_fdl = -1; /* force an update */
5162 curwin->w_p_fen = TRUE;
5163 break;
5164
5165 /* "zr": reduce folding */
5166 case 'r': ++curwin->w_p_fdl;
5167 break;
5168
5169 /* "zR": open all folds */
5170 case 'R': curwin->w_p_fdl = getDeepestNesting();
5171 old_fdl = -1; /* force an update */
5172 break;
5173
5174 case 'j': /* "zj" move to next fold downwards */
5175 case 'k': /* "zk" move to next fold upwards */
5176 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5177 cap->count1) == FAIL)
5178 clearopbeep(cap->oap);
5179 break;
5180
5181#endif /* FEAT_FOLDING */
5182
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005183#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005184 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5185 ++no_mapping;
5186 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005187 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005188 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005189 --no_mapping;
5190 --allow_keys;
5191#ifdef FEAT_CMDL_INFO
5192 (void)add_to_showcmd(nchar);
5193#endif
5194 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5195 {
5196 clearopbeep(cap->oap);
5197 break;
5198 }
5199 undo = TRUE;
5200 /*FALLTHROUGH*/
5201
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202 case 'g': /* "zg": add good word to word list */
5203 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005204 case 'G': /* "zG": add good word to temp word list */
5205 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 {
5207 char_u *ptr = NULL;
5208 int len;
5209
5210 if (checkclearop(cap->oap))
5211 break;
5212# ifdef FEAT_VISUAL
5213 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5214 == FAIL)
5215 return;
5216# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005217 if (ptr == NULL)
5218 {
5219 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005220
5221 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005222 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005223 if (len != 0 && curwin->w_cursor.col <= pos.col)
5224 ptr = ml_get_pos(&curwin->w_cursor);
5225 curwin->w_cursor = pos;
5226 }
5227
Bram Moolenaarb765d632005-06-07 21:00:02 +00005228 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5229 FIND_IDENT)) == 0)
5230 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005231 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005232 (nchar == 'G' || nchar == 'W')
5233 ? 0 : (int)cap->count1,
5234 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005235 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005236 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237
Bram Moolenaar43abc522005-12-10 20:15:02 +00005238 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005239 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005240 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005242#endif
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 default: clearopbeep(cap->oap);
5245 }
5246
5247#ifdef FEAT_FOLDING
5248 /* Redraw when 'foldenable' changed */
5249 if (old_fen != curwin->w_p_fen)
5250 {
5251# ifdef FEAT_DIFF
5252 win_T *wp;
5253
5254 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5255 {
5256 /* Adjust 'foldenable' in diff-synced windows. */
5257 FOR_ALL_WINDOWS(wp)
5258 {
5259 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5260 {
5261 wp->w_p_fen = curwin->w_p_fen;
5262 changed_window_setting_win(wp);
5263 }
5264 }
5265 }
5266# endif
5267 changed_window_setting();
5268 }
5269
5270 /* Redraw when 'foldlevel' changed. */
5271 if (old_fdl != curwin->w_p_fdl)
5272 newFoldLevel();
5273#endif
5274}
5275
5276#ifdef FEAT_GUI
5277/*
5278 * Vertical scrollbar movement.
5279 */
5280 static void
5281nv_ver_scrollbar(cap)
5282 cmdarg_T *cap;
5283{
5284 if (cap->oap->op_type != OP_NOP)
5285 clearopbeep(cap->oap);
5286
5287 /* Even if an operator was pending, we still want to scroll */
5288 gui_do_scroll();
5289}
5290
5291/*
5292 * Horizontal scrollbar movement.
5293 */
5294 static void
5295nv_hor_scrollbar(cap)
5296 cmdarg_T *cap;
5297{
5298 if (cap->oap->op_type != OP_NOP)
5299 clearopbeep(cap->oap);
5300
5301 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005302 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303}
5304#endif
5305
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005306#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005307/*
5308 * Click in GUI tab.
5309 */
5310 static void
5311nv_tabline(cap)
5312 cmdarg_T *cap;
5313{
5314 if (cap->oap->op_type != OP_NOP)
5315 clearopbeep(cap->oap);
5316
5317 /* Even if an operator was pending, we still want to jump tabs. */
5318 goto_tabpage(current_tab);
5319}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005320
5321/*
5322 * Selected item in tab line menu.
5323 */
5324 static void
5325nv_tabmenu(cap)
5326 cmdarg_T *cap;
5327{
5328 if (cap->oap->op_type != OP_NOP)
5329 clearopbeep(cap->oap);
5330
5331 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005332 handle_tabmenu();
5333}
5334
5335/*
5336 * Handle selecting an item of the GUI tab line menu.
5337 * Used in Normal and Insert mode.
5338 */
5339 void
5340handle_tabmenu()
5341{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005342 switch (current_tabmenu)
5343 {
5344 case TABLINE_MENU_CLOSE:
5345 if (current_tab == 0)
5346 do_cmdline_cmd((char_u *)"tabclose");
5347 else
5348 {
5349 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5350 current_tab);
5351 do_cmdline_cmd(IObuff);
5352 }
5353 break;
5354
5355 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005356 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5357 current_tab > 0 ? current_tab - 1 : 999);
5358 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005359 break;
5360
5361 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005362 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5363 current_tab > 0 ? current_tab - 1 : 999);
5364 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005365 break;
5366 }
5367}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005368#endif
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370/*
5371 * "Q" command.
5372 */
5373 static void
5374nv_exmode(cap)
5375 cmdarg_T *cap;
5376{
5377 /*
5378 * Ignore 'Q' in Visual mode, just give a beep.
5379 */
5380#ifdef FEAT_VISUAL
5381 if (VIsual_active)
5382 vim_beep();
5383 else
5384#endif
5385 if (!checkclearop(cap->oap))
5386 do_exmode(FALSE);
5387}
5388
5389/*
5390 * Handle a ":" command.
5391 */
5392 static void
5393nv_colon(cap)
5394 cmdarg_T *cap;
5395{
5396 int old_p_im;
5397
5398#ifdef FEAT_VISUAL
5399 if (VIsual_active)
5400 nv_operator(cap);
5401 else
5402#endif
5403 {
5404 if (cap->oap->op_type != OP_NOP)
5405 {
5406 /* Using ":" as a movement is characterwise exclusive. */
5407 cap->oap->motion_type = MCHAR;
5408 cap->oap->inclusive = FALSE;
5409 }
5410 else if (cap->count0)
5411 {
5412 /* translate "count:" into ":.,.+(count - 1)" */
5413 stuffcharReadbuff('.');
5414 if (cap->count0 > 1)
5415 {
5416 stuffReadbuff((char_u *)",.+");
5417 stuffnumReadbuff((long)cap->count0 - 1L);
5418 }
5419 }
5420
5421 /* When typing, don't type below an old message */
5422 if (KeyTyped)
5423 compute_cmdrow();
5424
5425 old_p_im = p_im;
5426
5427 /* get a command line and execute it */
5428 do_cmdline(NULL, getexline, NULL,
5429 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5430
5431 /* If 'insertmode' changed, enter or exit Insert mode */
5432 if (p_im != old_p_im)
5433 {
5434 if (p_im)
5435 restart_edit = 'i';
5436 else
5437 restart_edit = 0;
5438 }
5439
5440 /* The start of the operator may have become invalid by the Ex
5441 * command. */
5442 if (cap->oap->op_type != OP_NOP
5443 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5444 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005445 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 clearopbeep(cap->oap);
5447 }
5448}
5449
5450/*
5451 * Handle CTRL-G command.
5452 */
5453 static void
5454nv_ctrlg(cap)
5455 cmdarg_T *cap;
5456{
5457#ifdef FEAT_VISUAL
5458 if (VIsual_active) /* toggle Selection/Visual mode */
5459 {
5460 VIsual_select = !VIsual_select;
5461 showmode();
5462 }
5463 else
5464#endif
5465 if (!checkclearop(cap->oap))
5466 /* print full name if count given or :cd used */
5467 fileinfo((int)cap->count0, FALSE, TRUE);
5468}
5469
5470/*
5471 * Handle CTRL-H <Backspace> command.
5472 */
5473 static void
5474nv_ctrlh(cap)
5475 cmdarg_T *cap;
5476{
5477#ifdef FEAT_VISUAL
5478 if (VIsual_active && VIsual_select)
5479 {
5480 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5481 v_visop(cap);
5482 }
5483 else
5484#endif
5485 nv_left(cap);
5486}
5487
5488/*
5489 * CTRL-L: clear screen and redraw.
5490 */
5491 static void
5492nv_clear(cap)
5493 cmdarg_T *cap;
5494{
5495 if (!checkclearop(cap->oap))
5496 {
5497#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5498 /*
5499 * Right now, the BeBox doesn't seem to have an easy way to detect
5500 * window resizing, so we cheat and make the user detect it
5501 * manually with CTRL-L instead
5502 */
5503 ui_get_shellsize();
5504#endif
5505#ifdef FEAT_SYN_HL
5506 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005507 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508#endif
5509 redraw_later(CLEAR);
5510 }
5511}
5512
5513/*
5514 * CTRL-O: In Select mode: switch to Visual mode for one command.
5515 * Otherwise: Go to older pcmark.
5516 */
5517 static void
5518nv_ctrlo(cap)
5519 cmdarg_T *cap;
5520{
5521#ifdef FEAT_VISUAL
5522 if (VIsual_active && VIsual_select)
5523 {
5524 VIsual_select = FALSE;
5525 showmode();
5526 restart_VIsual_select = 2; /* restart Select mode later */
5527 }
5528 else
5529#endif
5530 {
5531 cap->count1 = -cap->count1;
5532 nv_pcmark(cap);
5533 }
5534}
5535
5536/*
5537 * CTRL-^ command, short for ":e #"
5538 */
5539 static void
5540nv_hat(cap)
5541 cmdarg_T *cap;
5542{
5543 if (!checkclearopq(cap->oap))
5544 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5545 GETF_SETMARK|GETF_ALT, FALSE);
5546}
5547
5548/*
5549 * "Z" commands.
5550 */
5551 static void
5552nv_Zet(cap)
5553 cmdarg_T *cap;
5554{
5555 if (!checkclearopq(cap->oap))
5556 {
5557 switch (cap->nchar)
5558 {
5559 /* "ZZ": equivalent to ":x". */
5560 case 'Z': do_cmdline_cmd((char_u *)"x");
5561 break;
5562
5563 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5564 case 'Q': do_cmdline_cmd((char_u *)"q!");
5565 break;
5566
5567 default: clearopbeep(cap->oap);
5568 }
5569 }
5570}
5571
5572#if defined(FEAT_WINDOWS) || defined(PROTO)
5573/*
5574 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5575 */
5576 void
5577do_nv_ident(c1, c2)
5578 int c1;
5579 int c2;
5580{
5581 oparg_T oa;
5582 cmdarg_T ca;
5583
5584 clear_oparg(&oa);
5585 vim_memset(&ca, 0, sizeof(ca));
5586 ca.oap = &oa;
5587 ca.cmdchar = c1;
5588 ca.nchar = c2;
5589 nv_ident(&ca);
5590}
5591#endif
5592
5593/*
5594 * Handle the commands that use the word under the cursor.
5595 * [g] CTRL-] :ta to current identifier
5596 * [g] 'K' run program for current identifier
5597 * [g] '*' / to current identifier or string
5598 * [g] '#' ? to current identifier or string
5599 * g ']' :tselect for current identifier
5600 */
5601 static void
5602nv_ident(cap)
5603 cmdarg_T *cap;
5604{
5605 char_u *ptr = NULL;
5606 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005607 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u *p;
5609 char_u *kp; /* value of 'keywordprg' */
5610 int kp_help; /* 'keywordprg' is ":help" */
5611 int n = 0; /* init for GCC */
5612 int cmdchar;
5613 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005614 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 char_u *aux_ptr;
5616 int isman;
5617 int isman_s;
5618
5619 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5620 {
5621 cmdchar = cap->nchar;
5622 g_cmd = TRUE;
5623 }
5624 else
5625 {
5626 cmdchar = cap->cmdchar;
5627 g_cmd = FALSE;
5628 }
5629
5630 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5631 cmdchar = '#';
5632
5633 /*
5634 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5635 */
5636 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5637 {
5638#ifdef FEAT_VISUAL
5639 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5640 return;
5641#endif
5642 if (checkclearopq(cap->oap))
5643 return;
5644 }
5645
5646 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5647 (cmdchar == '*' || cmdchar == '#')
5648 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5649 {
5650 clearop(cap->oap);
5651 return;
5652 }
5653
5654 /* Allocate buffer to put the command in. Inserting backslashes can
5655 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5656 * and some numbers. */
5657 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5658 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5659 || STRCMP(kp, ":help") == 0);
5660 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5661 if (buf == NULL)
5662 return;
5663 buf[0] = NUL;
5664
5665 switch (cmdchar)
5666 {
5667 case '*':
5668 case '#':
5669 /*
5670 * Put cursor at start of word, makes search skip the word
5671 * under the cursor.
5672 * Call setpcmark() first, so "*``" puts the cursor back where
5673 * it was.
5674 */
5675 setpcmark();
5676 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5677
5678 if (!g_cmd && vim_iswordp(ptr))
5679 STRCPY(buf, "\\<");
5680 no_smartcase = TRUE; /* don't use 'smartcase' now */
5681 break;
5682
5683 case 'K':
5684 if (kp_help)
5685 STRCPY(buf, "he! ");
5686 else
5687 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005688 /* An external command will probably use an argument starting
5689 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005690 while (*ptr == '-' && n > 0)
5691 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005692 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005693 --n;
5694 }
5695 if (n == 0)
5696 {
5697 EMSG(_(e_noident)); /* found dashes only */
5698 vim_free(buf);
5699 return;
5700 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005701
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 /* When a count is given, turn it into a range. Is this
5703 * really what we want? */
5704 isman = (STRCMP(kp, "man") == 0);
5705 isman_s = (STRCMP(kp, "man -s") == 0);
5706 if (cap->count0 != 0 && !(isman || isman_s))
5707 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5708
5709 STRCAT(buf, "! ");
5710 if (cap->count0 == 0 && isman_s)
5711 STRCAT(buf, "man");
5712 else
5713 STRCAT(buf, kp);
5714 STRCAT(buf, " ");
5715 if (cap->count0 != 0 && (isman || isman_s))
5716 {
5717 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5718 STRCAT(buf, " ");
5719 }
5720 }
5721 break;
5722
5723 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005724 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725#ifdef FEAT_CSCOPE
5726 if (p_cst)
5727 STRCPY(buf, "cstag ");
5728 else
5729#endif
5730 STRCPY(buf, "ts ");
5731 break;
5732
5733 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005734 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 if (curbuf->b_help)
5736 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005738 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005739 if (g_cmd)
5740 STRCPY(buf, "tj ");
5741 else
5742 sprintf((char *)buf, "%ldta ", cap->count0);
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745
5746 /*
5747 * Now grab the chars in the identifier
5748 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005749 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005752 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005753 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005754 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005755 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 vim_free(buf);
5758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005760 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5761 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005762 {
5763 vim_free(buf);
5764 vim_free(p);
5765 return;
5766 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005767 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005768 STRCAT(buf, p);
5769 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005771 else
5772 {
5773 if (cmdchar == '*')
5774 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5775 else if (cmdchar == '#')
5776 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005777 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005778 {
5779 if (curbuf->b_help)
5780 /* ":help" handles unescaped argument */
5781 aux_ptr = (char_u *)"";
5782 else
5783 aux_ptr = (char_u *)"\\|\"\n[";
5784 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005785 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005786 aux_ptr = (char_u *)"\\|\"\n*?[";
5787
5788 p = buf + STRLEN(buf);
5789 while (n-- > 0)
5790 {
5791 /* put a backslash before \ and some others */
5792 if (vim_strchr(aux_ptr, *ptr) != NULL)
5793 *p++ = '\\';
5794#ifdef FEAT_MBYTE
5795 /* When current byte is a part of multibyte character, copy all
5796 * bytes of that character. */
5797 if (has_mbyte)
5798 {
5799 int i;
5800 int len = (*mb_ptr2len)(ptr) - 1;
5801
5802 for (i = 0; i < len && n >= 1; ++i, --n)
5803 *p++ = *ptr++;
5804 }
5805#endif
5806 *p++ = *ptr++;
5807 }
5808 *p = NUL;
5809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 /*
5812 * Execute the command.
5813 */
5814 if (cmdchar == '*' || cmdchar == '#')
5815 {
5816 if (!g_cmd && (
5817#ifdef FEAT_MBYTE
5818 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5819#endif
5820 vim_iswordc(ptr[-1])))
5821 STRCAT(buf, "\\>");
5822#ifdef FEAT_CMDHIST
5823 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005824 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5826#endif
5827 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5828 }
5829 else
5830 do_cmdline_cmd(buf);
5831
5832 vim_free(buf);
5833}
5834
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005835#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836/*
5837 * Get visually selected text, within one line only.
5838 * Returns FAIL if more than one line selected.
5839 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005840 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841get_visual_text(cap, pp, lenp)
5842 cmdarg_T *cap;
5843 char_u **pp; /* return: start of selected text */
5844 int *lenp; /* return: length of selected text */
5845{
5846 if (VIsual_mode != 'V')
5847 unadjust_for_sel();
5848 if (VIsual.lnum != curwin->w_cursor.lnum)
5849 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005850 if (cap != NULL)
5851 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 return FAIL;
5853 }
5854 if (VIsual_mode == 'V')
5855 {
5856 *pp = ml_get_curline();
5857 *lenp = (int)STRLEN(*pp);
5858 }
5859 else
5860 {
5861 if (lt(curwin->w_cursor, VIsual))
5862 {
5863 *pp = ml_get_pos(&curwin->w_cursor);
5864 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5865 }
5866 else
5867 {
5868 *pp = ml_get_pos(&VIsual);
5869 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5870 }
5871#ifdef FEAT_MBYTE
5872 if (has_mbyte)
5873 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005874 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#endif
5876 }
5877 reset_VIsual_and_resel();
5878 return OK;
5879}
5880#endif
5881
5882/*
5883 * CTRL-T: backwards in tag stack
5884 */
5885 static void
5886nv_tagpop(cap)
5887 cmdarg_T *cap;
5888{
5889 if (!checkclearopq(cap->oap))
5890 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5891}
5892
5893/*
5894 * Handle scrolling command 'H', 'L' and 'M'.
5895 */
5896 static void
5897nv_scroll(cap)
5898 cmdarg_T *cap;
5899{
5900 int used = 0;
5901 long n;
5902#ifdef FEAT_FOLDING
5903 linenr_T lnum;
5904#endif
5905 int half;
5906
5907 cap->oap->motion_type = MLINE;
5908 setpcmark();
5909
5910 if (cap->cmdchar == 'L')
5911 {
5912 validate_botline(); /* make sure curwin->w_botline is valid */
5913 curwin->w_cursor.lnum = curwin->w_botline - 1;
5914 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5915 curwin->w_cursor.lnum = 1;
5916 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005917 {
5918#ifdef FEAT_FOLDING
5919 if (hasAnyFolding(curwin))
5920 {
5921 /* Count a fold for one screen line. */
5922 for (n = cap->count1 - 1; n > 0
5923 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5924 {
5925 (void)hasFolding(curwin->w_cursor.lnum,
5926 &curwin->w_cursor.lnum, NULL);
5927 --curwin->w_cursor.lnum;
5928 }
5929 }
5930 else
5931#endif
5932 curwin->w_cursor.lnum -= cap->count1 - 1;
5933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 else
5936 {
5937 if (cap->cmdchar == 'M')
5938 {
5939#ifdef FEAT_DIFF
5940 /* Don't count filler lines above the window. */
5941 used -= diff_check_fill(curwin, curwin->w_topline)
5942 - curwin->w_topfill;
5943#endif
5944 validate_botline(); /* make sure w_empty_rows is valid */
5945 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5946 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5947 {
5948#ifdef FEAT_DIFF
5949 /* Count half he number of filler lines to be "below this
5950 * line" and half to be "above the next line". */
5951 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5952 + n) / 2 >= half)
5953 {
5954 --n;
5955 break;
5956 }
5957#endif
5958 used += plines(curwin->w_topline + n);
5959 if (used >= half)
5960 break;
5961#ifdef FEAT_FOLDING
5962 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5963 n = lnum - curwin->w_topline;
5964#endif
5965 }
5966 if (n > 0 && used > curwin->w_height)
5967 --n;
5968 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005969 else /* (cap->cmdchar == 'H') */
5970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005972#ifdef FEAT_FOLDING
5973 if (hasAnyFolding(curwin))
5974 {
5975 /* Count a fold for one screen line. */
5976 lnum = curwin->w_topline;
5977 while (n-- > 0 && lnum < curwin->w_botline - 1)
5978 {
5979 hasFolding(lnum, NULL, &lnum);
5980 ++lnum;
5981 }
5982 n = lnum - curwin->w_topline;
5983 }
5984#endif
5985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 curwin->w_cursor.lnum = curwin->w_topline + n;
5987 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5988 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5989 }
5990
5991 cursor_correct(); /* correct for 'so' */
5992 beginline(BL_SOL | BL_FIX);
5993}
5994
5995/*
5996 * Cursor right commands.
5997 */
5998 static void
5999nv_right(cap)
6000 cmdarg_T *cap;
6001{
6002 long n;
6003#ifdef FEAT_VISUAL
6004 int PAST_LINE;
6005#else
6006# define PAST_LINE 0
6007#endif
6008
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006009 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6010 {
6011 /* <C-Right> and <S-Right> move a word or WORD right */
6012 if (mod_mask & MOD_MASK_CTRL)
6013 cap->arg = TRUE;
6014 nv_wordcmd(cap);
6015 return;
6016 }
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 cap->oap->motion_type = MCHAR;
6019 cap->oap->inclusive = FALSE;
6020#ifdef FEAT_VISUAL
6021 PAST_LINE = (VIsual_active && *p_sel != 'o');
6022
6023# ifdef FEAT_VIRTUALEDIT
6024 /*
6025 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006026 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 */
6028 if (virtual_active())
6029 PAST_LINE = 0;
6030# endif
6031#endif
6032
6033 for (n = cap->count1; n > 0; --n)
6034 {
6035 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006036#ifdef FEAT_VISUAL
6037 || (PAST_LINE && *ml_get_cursor() == NUL)
6038#endif
6039 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 * <Space> wraps to next line if 'whichwrap' has 's'.
6043 * 'l' wraps to next line if 'whichwrap' has 'l'.
6044 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 */
6046 if ( ((cap->cmdchar == ' '
6047 && vim_strchr(p_ww, 's') != NULL)
6048 || (cap->cmdchar == 'l'
6049 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006050 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 && vim_strchr(p_ww, '>') != NULL))
6052 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6053 {
6054 /* When deleting we also count the NL as a character.
6055 * Set cap->oap->inclusive when last char in the line is
6056 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006057 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 && !cap->oap->inclusive
6059 && !lineempty(curwin->w_cursor.lnum))
6060 cap->oap->inclusive = TRUE;
6061 else
6062 {
6063 ++curwin->w_cursor.lnum;
6064 curwin->w_cursor.col = 0;
6065#ifdef FEAT_VIRTUALEDIT
6066 curwin->w_cursor.coladd = 0;
6067#endif
6068 curwin->w_set_curswant = TRUE;
6069 cap->oap->inclusive = FALSE;
6070 }
6071 continue;
6072 }
6073 if (cap->oap->op_type == OP_NOP)
6074 {
6075 /* Only beep and flush if not moved at all */
6076 if (n == cap->count1)
6077 beep_flush();
6078 }
6079 else
6080 {
6081 if (!lineempty(curwin->w_cursor.lnum))
6082 cap->oap->inclusive = TRUE;
6083 }
6084 break;
6085 }
6086#ifdef FEAT_VISUAL
6087 else if (PAST_LINE)
6088 {
6089 curwin->w_set_curswant = TRUE;
6090# ifdef FEAT_VIRTUALEDIT
6091 if (virtual_active())
6092 oneright();
6093 else
6094# endif
6095 {
6096# ifdef FEAT_MBYTE
6097 if (has_mbyte)
6098 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006099 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 else
6101# endif
6102 ++curwin->w_cursor.col;
6103 }
6104 }
6105#endif
6106 }
6107#ifdef FEAT_FOLDING
6108 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6109 && cap->oap->op_type == OP_NOP)
6110 foldOpenCursor();
6111#endif
6112}
6113
6114/*
6115 * Cursor left commands.
6116 *
6117 * Returns TRUE when operator end should not be adjusted.
6118 */
6119 static void
6120nv_left(cap)
6121 cmdarg_T *cap;
6122{
6123 long n;
6124
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006125 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6126 {
6127 /* <C-Left> and <S-Left> move a word or WORD left */
6128 if (mod_mask & MOD_MASK_CTRL)
6129 cap->arg = 1;
6130 nv_bck_word(cap);
6131 return;
6132 }
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 cap->oap->motion_type = MCHAR;
6135 cap->oap->inclusive = FALSE;
6136 for (n = cap->count1; n > 0; --n)
6137 {
6138 if (oneleft() == FAIL)
6139 {
6140 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6141 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6142 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6143 */
6144 if ( (((cap->cmdchar == K_BS
6145 || cap->cmdchar == Ctrl_H)
6146 && vim_strchr(p_ww, 'b') != NULL)
6147 || (cap->cmdchar == 'h'
6148 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006149 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 && vim_strchr(p_ww, '<') != NULL))
6151 && curwin->w_cursor.lnum > 1)
6152 {
6153 --(curwin->w_cursor.lnum);
6154 coladvance((colnr_T)MAXCOL);
6155 curwin->w_set_curswant = TRUE;
6156
6157 /* When the NL before the first char has to be deleted we
6158 * put the cursor on the NUL after the previous line.
6159 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006160 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if ( (cap->oap->op_type == OP_DELETE
6162 || cap->oap->op_type == OP_CHANGE)
6163 && !lineempty(curwin->w_cursor.lnum))
6164 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006165 if (*ml_get_cursor() != NUL)
6166 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 cap->retval |= CA_NO_ADJ_OP_END;
6168 }
6169 continue;
6170 }
6171 /* Only beep and flush if not moved at all */
6172 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6173 beep_flush();
6174 break;
6175 }
6176 }
6177#ifdef FEAT_FOLDING
6178 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6179 && cap->oap->op_type == OP_NOP)
6180 foldOpenCursor();
6181#endif
6182}
6183
6184/*
6185 * Cursor up commands.
6186 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6187 */
6188 static void
6189nv_up(cap)
6190 cmdarg_T *cap;
6191{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006192 if (mod_mask & MOD_MASK_SHIFT)
6193 {
6194 /* <S-Up> is page up */
6195 cap->arg = BACKWARD;
6196 nv_page(cap);
6197 }
6198 else
6199 {
6200 cap->oap->motion_type = MLINE;
6201 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6202 clearopbeep(cap->oap);
6203 else if (cap->arg)
6204 beginline(BL_WHITE | BL_FIX);
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206}
6207
6208/*
6209 * Cursor down commands.
6210 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6211 */
6212 static void
6213nv_down(cap)
6214 cmdarg_T *cap;
6215{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006216 if (mod_mask & MOD_MASK_SHIFT)
6217 {
6218 /* <S-Down> is page down */
6219 cap->arg = FORWARD;
6220 nv_page(cap);
6221 }
6222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6224 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006225 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006226 if (curwin->w_llist_ref == NULL)
6227 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6228 else
6229 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 else
6231#endif
6232 {
6233#ifdef FEAT_CMDWIN
6234 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006235 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 cmdwin_result = CAR;
6237 else
6238#endif
6239 {
6240 cap->oap->motion_type = MLINE;
6241 if (cursor_down(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 }
6246 }
6247}
6248
6249#ifdef FEAT_SEARCHPATH
6250/*
6251 * Grab the file name under the cursor and edit it.
6252 */
6253 static void
6254nv_gotofile(cap)
6255 cmdarg_T *cap;
6256{
6257 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006260 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
6262 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006263 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 return;
6265 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006266#ifdef FEAT_AUTOCMD
6267 if (curbuf_locked())
6268 {
6269 clearop(cap->oap);
6270 return;
6271 }
6272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006274 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 if (ptr != NULL)
6277 {
6278 /* do autowrite if necessary */
6279 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6280 autowrite(curbuf, FALSE);
6281 setpcmark();
6282 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006283 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006284 if (cap->nchar == 'F' && lnum >= 0)
6285 {
6286 curwin->w_cursor.lnum = lnum;
6287 check_cursor_lnum();
6288 beginline(BL_SOL | BL_FIX);
6289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 vim_free(ptr);
6291 }
6292 else
6293 clearop(cap->oap);
6294}
6295#endif
6296
6297/*
6298 * <End> command: to end of current line or last line.
6299 */
6300 static void
6301nv_end(cap)
6302 cmdarg_T *cap;
6303{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006304 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006306 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 nv_goto(cap);
6308 cap->count1 = 1; /* to end of current line */
6309 }
6310 nv_dollar(cap);
6311}
6312
6313/*
6314 * Handle the "$" command.
6315 */
6316 static void
6317nv_dollar(cap)
6318 cmdarg_T *cap;
6319{
6320 cap->oap->motion_type = MCHAR;
6321 cap->oap->inclusive = TRUE;
6322#ifdef FEAT_VIRTUALEDIT
6323 /* In virtual mode when off the edge of a line and an operator
6324 * is pending (whew!) keep the cursor where it is.
6325 * Otherwise, send it to the end of the line. */
6326 if (!virtual_active() || gchar_cursor() != NUL
6327 || cap->oap->op_type == OP_NOP)
6328#endif
6329 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6330 if (cursor_down((long)(cap->count1 - 1),
6331 cap->oap->op_type == OP_NOP) == FAIL)
6332 clearopbeep(cap->oap);
6333#ifdef FEAT_FOLDING
6334 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6335 foldOpenCursor();
6336#endif
6337}
6338
6339/*
6340 * Implementation of '?' and '/' commands.
6341 * If cap->arg is TRUE don't set PC mark.
6342 */
6343 static void
6344nv_search(cap)
6345 cmdarg_T *cap;
6346{
6347 oparg_T *oap = cap->oap;
6348
6349 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6350 {
6351 /* Translate "g??" to "g?g?" */
6352 cap->cmdchar = 'g';
6353 cap->nchar = '?';
6354 nv_operator(cap);
6355 return;
6356 }
6357
6358 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6359
6360 if (cap->searchbuf == NULL)
6361 {
6362 clearop(oap);
6363 return;
6364 }
6365
6366 normal_search(cap, cap->cmdchar, cap->searchbuf,
6367 (cap->arg ? 0 : SEARCH_MARK));
6368}
6369
6370/*
6371 * Handle "N" and "n" commands.
6372 * cap->arg is SEARCH_REV for "N", 0 for "n".
6373 */
6374 static void
6375nv_next(cap)
6376 cmdarg_T *cap;
6377{
6378 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6379}
6380
6381/*
6382 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6383 * Uses only cap->count1 and cap->oap from "cap".
6384 */
6385 static void
6386normal_search(cap, dir, pat, opt)
6387 cmdarg_T *cap;
6388 int dir;
6389 char_u *pat;
6390 int opt; /* extra flags for do_search() */
6391{
6392 int i;
6393
6394 cap->oap->motion_type = MCHAR;
6395 cap->oap->inclusive = FALSE;
6396 cap->oap->use_reg_one = TRUE;
6397 curwin->w_set_curswant = TRUE;
6398
6399 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006400 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (i == 0)
6402 clearop(cap->oap);
6403 else
6404 {
6405 if (i == 2)
6406 cap->oap->motion_type = MLINE;
6407#ifdef FEAT_VIRTUALEDIT
6408 curwin->w_cursor.coladd = 0;
6409#endif
6410#ifdef FEAT_FOLDING
6411 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6412 foldOpenCursor();
6413#endif
6414 }
6415
6416 /* "/$" will put the cursor after the end of the line, may need to
6417 * correct that here */
6418 check_cursor();
6419}
6420
6421/*
6422 * Character search commands.
6423 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6424 * ',' and FALSE for ';'.
6425 * cap->nchar is NUL for ',' and ';' (repeat the search)
6426 */
6427 static void
6428nv_csearch(cap)
6429 cmdarg_T *cap;
6430{
6431 int t_cmd;
6432
6433 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6434 t_cmd = TRUE;
6435 else
6436 t_cmd = FALSE;
6437
6438 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6440 clearopbeep(cap->oap);
6441 else
6442 {
6443 curwin->w_set_curswant = TRUE;
6444#ifdef FEAT_VIRTUALEDIT
6445 /* Include a Tab for "tx" and for "dfx". */
6446 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6447 && (t_cmd || cap->oap->op_type != OP_NOP))
6448 {
6449 colnr_T scol, ecol;
6450
6451 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6452 curwin->w_cursor.coladd = ecol - scol;
6453 }
6454 else
6455 curwin->w_cursor.coladd = 0;
6456#endif
6457#ifdef FEAT_VISUAL
6458 adjust_for_sel(cap);
6459#endif
6460#ifdef FEAT_FOLDING
6461 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6462 foldOpenCursor();
6463#endif
6464 }
6465}
6466
6467/*
6468 * "[" and "]" commands.
6469 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6470 */
6471 static void
6472nv_brackets(cap)
6473 cmdarg_T *cap;
6474{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006475 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 pos_T prev_pos;
6477 pos_T *pos = NULL; /* init for GCC */
6478 pos_T old_pos; /* cursor position before command */
6479 int flag;
6480 long n;
6481 int findc;
6482 int c;
6483
6484 cap->oap->motion_type = MCHAR;
6485 cap->oap->inclusive = FALSE;
6486 old_pos = curwin->w_cursor;
6487#ifdef FEAT_VIRTUALEDIT
6488 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6489#endif
6490
6491#ifdef FEAT_SEARCHPATH
6492 /*
6493 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6494 */
6495 if (cap->nchar == 'f')
6496 nv_gotofile(cap);
6497 else
6498#endif
6499
6500#ifdef FEAT_FIND_ID
6501 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006502 * Find the occurrence(s) of the identifier or define under cursor
6503 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 *
6505 * search list jump
6506 * fwd bwd fwd bwd fwd bwd
6507 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6508 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6509 */
6510 if (vim_strchr((char_u *)
6511#ifdef EBCDIC
6512 "iI\005dD\067",
6513#else
6514 "iI\011dD\004",
6515#endif
6516 cap->nchar) != NULL)
6517 {
6518 char_u *ptr;
6519 int len;
6520
6521 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6522 clearop(cap->oap);
6523 else
6524 {
6525 find_pattern_in_path(ptr, 0, len, TRUE,
6526 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6527 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6528 cap->count1,
6529 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6530 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6531 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6532 (linenr_T)MAXLNUM);
6533 curwin->w_set_curswant = TRUE;
6534 }
6535 }
6536 else
6537#endif
6538
6539 /*
6540 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6541 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6542 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6543 * "[m" or "]m" search for prev/next start of (Java) method.
6544 * "[M" or "]M" search for prev/next end of (Java) method.
6545 */
6546 if ( (cap->cmdchar == '['
6547 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6548 || (cap->cmdchar == ']'
6549 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6550 {
6551 if (cap->nchar == '*')
6552 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 prev_pos.lnum = 0;
6554 if (cap->nchar == 'm' || cap->nchar == 'M')
6555 {
6556 if (cap->cmdchar == '[')
6557 findc = '{';
6558 else
6559 findc = '}';
6560 n = 9999;
6561 }
6562 else
6563 {
6564 findc = cap->nchar;
6565 n = cap->count1;
6566 }
6567 for ( ; n > 0; --n)
6568 {
6569 if ((pos = findmatchlimit(cap->oap, findc,
6570 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6571 {
6572 if (new_pos.lnum == 0) /* nothing found */
6573 {
6574 if (cap->nchar != 'm' && cap->nchar != 'M')
6575 clearopbeep(cap->oap);
6576 }
6577 else
6578 pos = &new_pos; /* use last one found */
6579 break;
6580 }
6581 prev_pos = new_pos;
6582 curwin->w_cursor = *pos;
6583 new_pos = *pos;
6584 }
6585 curwin->w_cursor = old_pos;
6586
6587 /*
6588 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6589 * brought us to the match for "[m" and "]M" when inside a method.
6590 * Try finding the '{' or '}' we want to be at.
6591 * Also repeat for the given count.
6592 */
6593 if (cap->nchar == 'm' || cap->nchar == 'M')
6594 {
6595 /* norm is TRUE for "]M" and "[m" */
6596 int norm = ((findc == '{') == (cap->nchar == 'm'));
6597
6598 n = cap->count1;
6599 /* found a match: we were inside a method */
6600 if (prev_pos.lnum != 0)
6601 {
6602 pos = &prev_pos;
6603 curwin->w_cursor = prev_pos;
6604 if (norm)
6605 --n;
6606 }
6607 else
6608 pos = NULL;
6609 while (n > 0)
6610 {
6611 for (;;)
6612 {
6613 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6614 {
6615 /* if not found anything, that's an error */
6616 if (pos == NULL)
6617 clearopbeep(cap->oap);
6618 n = 0;
6619 break;
6620 }
6621 c = gchar_cursor();
6622 if (c == '{' || c == '}')
6623 {
6624 /* Must have found end/start of class: use it.
6625 * Or found the place to be at. */
6626 if ((c == findc && norm) || (n == 1 && !norm))
6627 {
6628 new_pos = curwin->w_cursor;
6629 pos = &new_pos;
6630 n = 0;
6631 }
6632 /* if no match found at all, we started outside of the
6633 * class and we're inside now. Just go on. */
6634 else if (new_pos.lnum == 0)
6635 {
6636 new_pos = curwin->w_cursor;
6637 pos = &new_pos;
6638 }
6639 /* found start/end of other method: go to match */
6640 else if ((pos = findmatchlimit(cap->oap, findc,
6641 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6642 0)) == NULL)
6643 n = 0;
6644 else
6645 curwin->w_cursor = *pos;
6646 break;
6647 }
6648 }
6649 --n;
6650 }
6651 curwin->w_cursor = old_pos;
6652 if (pos == NULL && new_pos.lnum != 0)
6653 clearopbeep(cap->oap);
6654 }
6655 if (pos != NULL)
6656 {
6657 setpcmark();
6658 curwin->w_cursor = *pos;
6659 curwin->w_set_curswant = TRUE;
6660#ifdef FEAT_FOLDING
6661 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6662 && cap->oap->op_type == OP_NOP)
6663 foldOpenCursor();
6664#endif
6665 }
6666 }
6667
6668 /*
6669 * "[[", "[]", "]]" and "][": move to start or end of function
6670 */
6671 else if (cap->nchar == '[' || cap->nchar == ']')
6672 {
6673 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6674 flag = '{';
6675 else
6676 flag = '}'; /* "][" or "[]" */
6677
6678 curwin->w_set_curswant = TRUE;
6679 /*
6680 * Imitate strange Vi behaviour: When using "]]" with an operator
6681 * we also stop at '}'.
6682 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006683 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 (cap->oap->op_type != OP_NOP
6685 && cap->arg == FORWARD && flag == '{')))
6686 clearopbeep(cap->oap);
6687 else
6688 {
6689 if (cap->oap->op_type == OP_NOP)
6690 beginline(BL_WHITE | BL_FIX);
6691#ifdef FEAT_FOLDING
6692 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6693 foldOpenCursor();
6694#endif
6695 }
6696 }
6697
6698 /*
6699 * "[p", "[P", "]P" and "]p": put with indent adjustment
6700 */
6701 else if (cap->nchar == 'p' || cap->nchar == 'P')
6702 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006703 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 prep_redo_cmd(cap);
6706 do_put(cap->oap->regname,
6707 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6708 cap->count1, PUT_FIXINDENT);
6709 }
6710 }
6711
6712 /*
6713 * "['", "[`", "]'" and "]`": jump to next mark
6714 */
6715 else if (cap->nchar == '\'' || cap->nchar == '`')
6716 {
6717 pos = &curwin->w_cursor;
6718 for (n = cap->count1; n > 0; --n)
6719 {
6720 prev_pos = *pos;
6721 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6722 cap->nchar == '\'');
6723 if (pos == NULL)
6724 break;
6725 }
6726 if (pos == NULL)
6727 pos = &prev_pos;
6728 nv_cursormark(cap, cap->nchar == '\'', pos);
6729 }
6730
6731#ifdef FEAT_MOUSE
6732 /*
6733 * [ or ] followed by a middle mouse click: put selected text with
6734 * indent adjustment. Any other button just does as usual.
6735 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006736 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {
6738 (void)do_mouse(cap->oap, cap->nchar,
6739 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6740 cap->count1, PUT_FIXINDENT);
6741 }
6742#endif /* FEAT_MOUSE */
6743
6744#ifdef FEAT_FOLDING
6745 /*
6746 * "[z" and "]z": move to start or end of open fold.
6747 */
6748 else if (cap->nchar == 'z')
6749 {
6750 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6751 cap->count1) == FAIL)
6752 clearopbeep(cap->oap);
6753 }
6754#endif
6755
6756#ifdef FEAT_DIFF
6757 /*
6758 * "[c" and "]c": move to next or previous diff-change.
6759 */
6760 else if (cap->nchar == 'c')
6761 {
6762 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6763 cap->count1) == FAIL)
6764 clearopbeep(cap->oap);
6765 }
6766#endif
6767
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006768#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006769 /*
6770 * "[s", "[S", "]s" and "]S": move to next spell error.
6771 */
6772 else if (cap->nchar == 's' || cap->nchar == 'S')
6773 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006774 setpcmark();
6775 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006776 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6777 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006778 {
6779 clearopbeep(cap->oap);
6780 break;
6781 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006782# ifdef FEAT_FOLDING
6783 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6784 foldOpenCursor();
6785# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006786 }
6787#endif
6788
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 /* Not a valid cap->nchar. */
6790 else
6791 clearopbeep(cap->oap);
6792}
6793
6794/*
6795 * Handle Normal mode "%" command.
6796 */
6797 static void
6798nv_percent(cap)
6799 cmdarg_T *cap;
6800{
6801 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006802#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 linenr_T lnum = curwin->w_cursor.lnum;
6804#endif
6805
6806 cap->oap->inclusive = TRUE;
6807 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6808 {
6809 if (cap->count0 > 100)
6810 clearopbeep(cap->oap);
6811 else
6812 {
6813 cap->oap->motion_type = MLINE;
6814 setpcmark();
6815 /* Round up, so CTRL-G will give same value. Watch out for a
6816 * large line count, the line number must not go negative! */
6817 if (curbuf->b_ml.ml_line_count > 1000000)
6818 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6819 / 100L * cap->count0;
6820 else
6821 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6822 cap->count0 + 99L) / 100L;
6823 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6824 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6825 beginline(BL_SOL | BL_FIX);
6826 }
6827 }
6828 else /* "%" : go to matching paren */
6829 {
6830 cap->oap->motion_type = MCHAR;
6831 cap->oap->use_reg_one = TRUE;
6832 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6833 clearopbeep(cap->oap);
6834 else
6835 {
6836 setpcmark();
6837 curwin->w_cursor = *pos;
6838 curwin->w_set_curswant = TRUE;
6839#ifdef FEAT_VIRTUALEDIT
6840 curwin->w_cursor.coladd = 0;
6841#endif
6842#ifdef FEAT_VISUAL
6843 adjust_for_sel(cap);
6844#endif
6845 }
6846 }
6847#ifdef FEAT_FOLDING
6848 if (cap->oap->op_type == OP_NOP
6849 && lnum != curwin->w_cursor.lnum
6850 && (fdo_flags & FDO_PERCENT)
6851 && KeyTyped)
6852 foldOpenCursor();
6853#endif
6854}
6855
6856/*
6857 * Handle "(" and ")" commands.
6858 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6859 */
6860 static void
6861nv_brace(cap)
6862 cmdarg_T *cap;
6863{
6864 cap->oap->motion_type = MCHAR;
6865 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006866 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6867 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 curwin->w_set_curswant = TRUE;
6869
6870 if (findsent(cap->arg, cap->count1) == FAIL)
6871 clearopbeep(cap->oap);
6872 else
6873 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006874 /* Don't leave the cursor on the NUL past end of line. */
6875 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#ifdef FEAT_VIRTUALEDIT
6877 curwin->w_cursor.coladd = 0;
6878#endif
6879#ifdef FEAT_FOLDING
6880 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6881 foldOpenCursor();
6882#endif
6883 }
6884}
6885
6886/*
6887 * "m" command: Mark a position.
6888 */
6889 static void
6890nv_mark(cap)
6891 cmdarg_T *cap;
6892{
6893 if (!checkclearop(cap->oap))
6894 {
6895 if (setmark(cap->nchar) == FAIL)
6896 clearopbeep(cap->oap);
6897 }
6898}
6899
6900/*
6901 * "{" and "}" commands.
6902 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6903 */
6904 static void
6905nv_findpar(cap)
6906 cmdarg_T *cap;
6907{
6908 cap->oap->motion_type = MCHAR;
6909 cap->oap->inclusive = FALSE;
6910 cap->oap->use_reg_one = TRUE;
6911 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006912 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 clearopbeep(cap->oap);
6914 else
6915 {
6916#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 * "u" command: Undo or make lower case.
6928 */
6929 static void
6930nv_undo(cap)
6931 cmdarg_T *cap;
6932{
6933 if (cap->oap->op_type == OP_LOWER
6934#ifdef FEAT_VISUAL
6935 || VIsual_active
6936#endif
6937 )
6938 {
6939 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6940 cap->cmdchar = 'g';
6941 cap->nchar = 'u';
6942 nv_operator(cap);
6943 }
6944 else
6945 nv_kundo(cap);
6946}
6947
6948/*
6949 * <Undo> command.
6950 */
6951 static void
6952nv_kundo(cap)
6953 cmdarg_T *cap;
6954{
6955 if (!checkclearopq(cap->oap))
6956 {
6957 u_undo((int)cap->count1);
6958 curwin->w_set_curswant = TRUE;
6959 }
6960}
6961
6962/*
6963 * Handle the "r" command.
6964 */
6965 static void
6966nv_replace(cap)
6967 cmdarg_T *cap;
6968{
6969 char_u *ptr;
6970 int had_ctrl_v;
6971 long n;
6972
6973 if (checkclearop(cap->oap))
6974 return;
6975
6976 /* get another character */
6977 if (cap->nchar == Ctrl_V)
6978 {
6979 had_ctrl_v = Ctrl_V;
6980 cap->nchar = get_literal();
6981 /* Don't redo a multibyte character with CTRL-V. */
6982 if (cap->nchar > DEL)
6983 had_ctrl_v = NUL;
6984 }
6985 else
6986 had_ctrl_v = NUL;
6987
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006988 /* Abort if the character is a special key. */
6989 if (IS_SPECIAL(cap->nchar))
6990 {
6991 clearopbeep(cap->oap);
6992 return;
6993 }
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#ifdef FEAT_VISUAL
6996 /* Visual mode "r" */
6997 if (VIsual_active)
6998 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006999 if (got_int)
7000 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 nv_operator(cap);
7002 return;
7003 }
7004#endif
7005
7006#ifdef FEAT_VIRTUALEDIT
7007 /* Break tabs, etc. */
7008 if (virtual_active())
7009 {
7010 if (u_save_cursor() == FAIL)
7011 return;
7012 if (gchar_cursor() == NUL)
7013 {
7014 /* Add extra space and put the cursor on the first one. */
7015 coladvance_force((colnr_T)(getviscol() + cap->count1));
7016 curwin->w_cursor.col -= cap->count1;
7017 }
7018 else if (gchar_cursor() == TAB)
7019 coladvance_force(getviscol());
7020 }
7021#endif
7022
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007023 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007025 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026#ifdef FEAT_MBYTE
7027 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7028#endif
7029 )
7030 {
7031 clearopbeep(cap->oap);
7032 return;
7033 }
7034
7035 /*
7036 * Replacing with a TAB is done by edit() when it is complicated because
7037 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7038 * Other characters are done below to avoid problems with things like
7039 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7040 */
7041 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7042 {
7043 stuffnumReadbuff(cap->count1);
7044 stuffcharReadbuff('R');
7045 stuffcharReadbuff('\t');
7046 stuffcharReadbuff(ESC);
7047 return;
7048 }
7049
7050 /* save line for undo */
7051 if (u_save_cursor() == FAIL)
7052 return;
7053
7054 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7055 {
7056 /*
7057 * Replace character(s) by a single newline.
7058 * Strange vi behaviour: Only one newline is inserted.
7059 * Delete the characters here.
7060 * Insert the newline with an insert command, takes care of
7061 * autoindent. The insert command depends on being on the last
7062 * character of a line or not.
7063 */
7064#ifdef FEAT_MBYTE
7065 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7066#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007067 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068#endif
7069 stuffcharReadbuff('\r');
7070 stuffcharReadbuff(ESC);
7071
7072 /* Give 'r' to edit(), to get the redo command right. */
7073 invoke_edit(cap, TRUE, 'r', FALSE);
7074 }
7075 else
7076 {
7077 prep_redo(cap->oap->regname, cap->count1,
7078 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7079
7080 curbuf->b_op_start = curwin->w_cursor;
7081#ifdef FEAT_MBYTE
7082 if (has_mbyte)
7083 {
7084 int old_State = State;
7085
7086 if (cap->ncharC1 != 0)
7087 AppendCharToRedobuff(cap->ncharC1);
7088 if (cap->ncharC2 != 0)
7089 AppendCharToRedobuff(cap->ncharC2);
7090
7091 /* This is slow, but it handles replacing a single-byte with a
7092 * multi-byte and the other way around. Also handles adding
7093 * composing characters for utf-8. */
7094 for (n = cap->count1; n > 0; --n)
7095 {
7096 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007097 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7098 {
7099 int c = ins_copychar(curwin->w_cursor.lnum
7100 + (cap->nchar == Ctrl_Y ? -1 : 1));
7101 if (c != NUL)
7102 ins_char(c);
7103 else
7104 /* will be decremented further down */
7105 ++curwin->w_cursor.col;
7106 }
7107 else
7108 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 State = old_State;
7110 if (cap->ncharC1 != 0)
7111 ins_char(cap->ncharC1);
7112 if (cap->ncharC2 != 0)
7113 ins_char(cap->ncharC2);
7114 }
7115 }
7116 else
7117#endif
7118 {
7119 /*
7120 * Replace the characters within one line.
7121 */
7122 for (n = cap->count1; n > 0; --n)
7123 {
7124 /*
7125 * Get ptr again, because u_save and/or showmatch() will have
7126 * released the line. At the same time we let know that the
7127 * line will be changed.
7128 */
7129 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007130 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7131 {
7132 int c = ins_copychar(curwin->w_cursor.lnum
7133 + (cap->nchar == Ctrl_Y ? -1 : 1));
7134 if (c != NUL)
7135 ptr[curwin->w_cursor.col] = c;
7136 }
7137 else
7138 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (p_sm && msg_silent == 0)
7140 showmatch(cap->nchar);
7141 ++curwin->w_cursor.col;
7142 }
7143#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007144 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007146 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
Bram Moolenaar009b2592004-10-24 19:18:58 +00007148 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007149 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007151 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153#endif
7154
7155 /* mark the buffer as changed and prepare for displaying */
7156 changed_bytes(curwin->w_cursor.lnum,
7157 (colnr_T)(curwin->w_cursor.col - cap->count1));
7158 }
7159 --curwin->w_cursor.col; /* cursor on the last replaced char */
7160#ifdef FEAT_MBYTE
7161 /* if the character on the left of the current cursor is a multi-byte
7162 * character, move two characters left */
7163 if (has_mbyte)
7164 mb_adjust_cursor();
7165#endif
7166 curbuf->b_op_end = curwin->w_cursor;
7167 curwin->w_set_curswant = TRUE;
7168 set_last_insert(cap->nchar);
7169 }
7170}
7171
7172#ifdef FEAT_VISUAL
7173/*
7174 * 'o': Exchange start and end of Visual area.
7175 * 'O': same, but in block mode exchange left and right corners.
7176 */
7177 static void
7178v_swap_corners(cmdchar)
7179 int cmdchar;
7180{
7181 pos_T old_cursor;
7182 colnr_T left, right;
7183
7184 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7185 {
7186 old_cursor = curwin->w_cursor;
7187 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7188 curwin->w_cursor.lnum = VIsual.lnum;
7189 coladvance(left);
7190 VIsual = curwin->w_cursor;
7191
7192 curwin->w_cursor.lnum = old_cursor.lnum;
7193 curwin->w_curswant = right;
7194 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7195 * right one column */
7196 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7197 ++curwin->w_curswant;
7198 coladvance(curwin->w_curswant);
7199 if (curwin->w_cursor.col == old_cursor.col
7200#ifdef FEAT_VIRTUALEDIT
7201 && (!virtual_active()
7202 || curwin->w_cursor.coladd == old_cursor.coladd)
7203#endif
7204 )
7205 {
7206 curwin->w_cursor.lnum = VIsual.lnum;
7207 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7208 ++right;
7209 coladvance(right);
7210 VIsual = curwin->w_cursor;
7211
7212 curwin->w_cursor.lnum = old_cursor.lnum;
7213 coladvance(left);
7214 curwin->w_curswant = left;
7215 }
7216 }
7217 else
7218 {
7219 old_cursor = curwin->w_cursor;
7220 curwin->w_cursor = VIsual;
7221 VIsual = old_cursor;
7222 curwin->w_set_curswant = TRUE;
7223 }
7224}
7225#endif /* FEAT_VISUAL */
7226
7227/*
7228 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7229 */
7230 static void
7231nv_Replace(cap)
7232 cmdarg_T *cap;
7233{
7234#ifdef FEAT_VISUAL
7235 if (VIsual_active) /* "R" is replace lines */
7236 {
7237 cap->cmdchar = 'c';
7238 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007239 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 VIsual_mode = 'V';
7241 nv_operator(cap);
7242 }
7243 else
7244#endif
7245 if (!checkclearopq(cap->oap))
7246 {
7247 if (!curbuf->b_p_ma)
7248 EMSG(_(e_modifiable));
7249 else
7250 {
7251#ifdef FEAT_VIRTUALEDIT
7252 if (virtual_active())
7253 coladvance(getviscol());
7254#endif
7255 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7256 }
7257 }
7258}
7259
7260#ifdef FEAT_VREPLACE
7261/*
7262 * "gr".
7263 */
7264 static void
7265nv_vreplace(cap)
7266 cmdarg_T *cap;
7267{
7268# ifdef FEAT_VISUAL
7269 if (VIsual_active)
7270 {
7271 cap->cmdchar = 'r';
7272 cap->nchar = cap->extra_char;
7273 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7274 }
7275 else
7276# endif
7277 if (!checkclearopq(cap->oap))
7278 {
7279 if (!curbuf->b_p_ma)
7280 EMSG(_(e_modifiable));
7281 else
7282 {
7283 if (cap->extra_char == Ctrl_V) /* get another character */
7284 cap->extra_char = get_literal();
7285 stuffcharReadbuff(cap->extra_char);
7286 stuffcharReadbuff(ESC);
7287# ifdef FEAT_VIRTUALEDIT
7288 if (virtual_active())
7289 coladvance(getviscol());
7290# endif
7291 invoke_edit(cap, TRUE, 'v', FALSE);
7292 }
7293 }
7294}
7295#endif
7296
7297/*
7298 * Swap case for "~" command, when it does not work like an operator.
7299 */
7300 static void
7301n_swapchar(cap)
7302 cmdarg_T *cap;
7303{
7304 long n;
7305 pos_T startpos;
7306 int did_change = 0;
7307#ifdef FEAT_NETBEANS_INTG
7308 pos_T pos;
7309 char_u *ptr;
7310 int count;
7311#endif
7312
7313 if (checkclearopq(cap->oap))
7314 return;
7315
7316 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7317 {
7318 clearopbeep(cap->oap);
7319 return;
7320 }
7321
7322 prep_redo_cmd(cap);
7323
7324 if (u_save_cursor() == FAIL)
7325 return;
7326
7327 startpos = curwin->w_cursor;
7328#ifdef FEAT_NETBEANS_INTG
7329 pos = startpos;
7330#endif
7331 for (n = cap->count1; n > 0; --n)
7332 {
7333 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7334 inc_cursor();
7335 if (gchar_cursor() == NUL)
7336 {
7337 if (vim_strchr(p_ww, '~') != NULL
7338 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7339 {
7340#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007341 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
7343 if (did_change)
7344 {
7345 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007346 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007347 netbeans_removed(curbuf, pos.lnum, pos.col,
7348 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007350 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352 pos.col = 0;
7353 pos.lnum++;
7354 }
7355#endif
7356 ++curwin->w_cursor.lnum;
7357 curwin->w_cursor.col = 0;
7358 if (n > 1)
7359 {
7360 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7361 break;
7362 u_clearline();
7363 }
7364 }
7365 else
7366 break;
7367 }
7368 }
7369#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007370 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
7372 ptr = ml_get(pos.lnum);
7373 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007374 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7375 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 }
7377#endif
7378
7379
7380 check_cursor();
7381 curwin->w_set_curswant = TRUE;
7382 if (did_change)
7383 {
7384 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7385 0L);
7386 curbuf->b_op_start = startpos;
7387 curbuf->b_op_end = curwin->w_cursor;
7388 if (curbuf->b_op_end.col > 0)
7389 --curbuf->b_op_end.col;
7390 }
7391}
7392
7393/*
7394 * Move cursor to mark.
7395 */
7396 static void
7397nv_cursormark(cap, flag, pos)
7398 cmdarg_T *cap;
7399 int flag;
7400 pos_T *pos;
7401{
7402 if (check_mark(pos) == FAIL)
7403 clearop(cap->oap);
7404 else
7405 {
7406 if (cap->cmdchar == '\''
7407 || cap->cmdchar == '`'
7408 || cap->cmdchar == '['
7409 || cap->cmdchar == ']')
7410 setpcmark();
7411 curwin->w_cursor = *pos;
7412 if (flag)
7413 beginline(BL_WHITE | BL_FIX);
7414 else
7415 check_cursor();
7416 }
7417 cap->oap->motion_type = flag ? MLINE : MCHAR;
7418 if (cap->cmdchar == '`')
7419 cap->oap->use_reg_one = TRUE;
7420 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7421 curwin->w_set_curswant = TRUE;
7422}
7423
7424#ifdef FEAT_VISUAL
7425/*
7426 * Handle commands that are operators in Visual mode.
7427 */
7428 static void
7429v_visop(cap)
7430 cmdarg_T *cap;
7431{
7432 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7433
7434 /* Uppercase means linewise, except in block mode, then "D" deletes till
7435 * the end of the line, and "C" replaces til EOL */
7436 if (isupper(cap->cmdchar))
7437 {
7438 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007439 {
7440 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7444 curwin->w_curswant = MAXCOL;
7445 }
7446 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7447 nv_operator(cap);
7448}
7449#endif
7450
7451/*
7452 * "s" and "S" commands.
7453 */
7454 static void
7455nv_subst(cap)
7456 cmdarg_T *cap;
7457{
7458#ifdef FEAT_VISUAL
7459 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7460 {
7461 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007462 {
7463 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 cap->cmdchar = 'c';
7467 nv_operator(cap);
7468 }
7469 else
7470#endif
7471 nv_optrans(cap);
7472}
7473
7474/*
7475 * Abbreviated commands.
7476 */
7477 static void
7478nv_abbrev(cap)
7479 cmdarg_T *cap;
7480{
7481 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7482 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7483
7484#ifdef FEAT_VISUAL
7485 /* in Visual mode these commands are operators */
7486 if (VIsual_active)
7487 v_visop(cap);
7488 else
7489#endif
7490 nv_optrans(cap);
7491}
7492
7493/*
7494 * Translate a command into another command.
7495 */
7496 static void
7497nv_optrans(cap)
7498 cmdarg_T *cap;
7499{
7500 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7501 (char_u *)"d$", (char_u *)"c$",
7502 (char_u *)"cl", (char_u *)"cc",
7503 (char_u *)"yy", (char_u *)":s\r"};
7504 static char_u *str = (char_u *)"xXDCsSY&";
7505
7506 if (!checkclearopq(cap->oap))
7507 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007508 /* In Vi "2D" doesn't delete the next line. Can't translate it
7509 * either, because "2." should also not use the count. */
7510 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7511 {
7512 cap->oap->start = curwin->w_cursor;
7513 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007514#ifdef FEAT_EVAL
7515 set_op_var(OP_DELETE);
7516#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007517 cap->count1 = 1;
7518 nv_dollar(cap);
7519 finish_op = TRUE;
7520 ResetRedobuff();
7521 AppendCharToRedobuff('D');
7522 }
7523 else
7524 {
7525 if (cap->count0)
7526 stuffnumReadbuff(cap->count0);
7527 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 cap->opcount = 0;
7531}
7532
7533/*
7534 * "'" and "`" commands. Also for "g'" and "g`".
7535 * cap->arg is TRUE for "'" and "g'".
7536 */
7537 static void
7538nv_gomark(cap)
7539 cmdarg_T *cap;
7540{
7541 pos_T *pos;
7542 int c;
7543#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007544 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7546#endif
7547
7548 if (cap->cmdchar == 'g')
7549 c = cap->extra_char;
7550 else
7551 c = cap->nchar;
7552 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7553 if (pos == (pos_T *)-1) /* jumped to other file */
7554 {
7555 if (cap->arg)
7556 {
7557 check_cursor_lnum();
7558 beginline(BL_WHITE | BL_FIX);
7559 }
7560 else
7561 check_cursor();
7562 }
7563 else
7564 nv_cursormark(cap, cap->arg, pos);
7565
7566#ifdef FEAT_VIRTUALEDIT
7567 /* May need to clear the coladd that a mark includes. */
7568 if (!virtual_active())
7569 curwin->w_cursor.coladd = 0;
7570#endif
7571#ifdef FEAT_FOLDING
7572 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007573 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007574 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 && (fdo_flags & FDO_MARK)
7576 && old_KeyTyped)
7577 foldOpenCursor();
7578#endif
7579}
7580
7581/*
7582 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7583 */
7584 static void
7585nv_pcmark(cap)
7586 cmdarg_T *cap;
7587{
7588#ifdef FEAT_JUMPLIST
7589 pos_T *pos;
7590# ifdef FEAT_FOLDING
7591 linenr_T lnum = curwin->w_cursor.lnum;
7592 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7593# endif
7594
7595 if (!checkclearopq(cap->oap))
7596 {
7597 if (cap->cmdchar == 'g')
7598 pos = movechangelist((int)cap->count1);
7599 else
7600 pos = movemark((int)cap->count1);
7601 if (pos == (pos_T *)-1) /* jump to other file */
7602 {
7603 curwin->w_set_curswant = TRUE;
7604 check_cursor();
7605 }
7606 else if (pos != NULL) /* can jump */
7607 nv_cursormark(cap, FALSE, pos);
7608 else if (cap->cmdchar == 'g')
7609 {
7610 if (curbuf->b_changelistlen == 0)
7611 EMSG(_("E664: changelist is empty"));
7612 else if (cap->count1 < 0)
7613 EMSG(_("E662: At start of changelist"));
7614 else
7615 EMSG(_("E663: At end of changelist"));
7616 }
7617 else
7618 clearopbeep(cap->oap);
7619# ifdef FEAT_FOLDING
7620 if (cap->oap->op_type == OP_NOP
7621 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7622 && (fdo_flags & FDO_MARK)
7623 && old_KeyTyped)
7624 foldOpenCursor();
7625# endif
7626 }
7627#else
7628 clearopbeep(cap->oap);
7629#endif
7630}
7631
7632/*
7633 * Handle '"' command.
7634 */
7635 static void
7636nv_regname(cap)
7637 cmdarg_T *cap;
7638{
7639 if (checkclearop(cap->oap))
7640 return;
7641#ifdef FEAT_EVAL
7642 if (cap->nchar == '=')
7643 cap->nchar = get_expr_register();
7644#endif
7645 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7646 {
7647 cap->oap->regname = cap->nchar;
7648 cap->opcount = cap->count0; /* remember count before '"' */
7649#ifdef FEAT_EVAL
7650 set_reg_var(cap->oap->regname);
7651#endif
7652 }
7653 else
7654 clearopbeep(cap->oap);
7655}
7656
7657#ifdef FEAT_VISUAL
7658/*
7659 * Handle "v", "V" and "CTRL-V" commands.
7660 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7661 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007662 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 */
7664 static void
7665nv_visual(cap)
7666 cmdarg_T *cap;
7667{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007668 if (cap->cmdchar == Ctrl_Q)
7669 cap->cmdchar = Ctrl_V;
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7672 * characterwise, linewise, or blockwise. */
7673 if (cap->oap->op_type != OP_NOP)
7674 {
7675 cap->oap->motion_force = cap->cmdchar;
7676 finish_op = FALSE; /* operator doesn't finish now but later */
7677 return;
7678 }
7679
7680 VIsual_select = cap->arg;
7681 if (VIsual_active) /* change Visual mode */
7682 {
7683 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7684 end_visual_mode();
7685 else /* toggle char/block mode */
7686 { /* or char/line mode */
7687 VIsual_mode = cap->cmdchar;
7688 showmode();
7689 }
7690 redraw_curbuf_later(INVERTED); /* update the inversion */
7691 }
7692 else /* start Visual mode */
7693 {
7694 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007695 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007697 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 VIsual = curwin->w_cursor;
7699
7700 VIsual_active = TRUE;
7701 VIsual_reselect = TRUE;
7702 if (!cap->arg)
7703 /* start Select mode when 'selectmode' contains "cmd" */
7704 may_start_select('c');
7705#ifdef FEAT_MOUSE
7706 setmouse();
7707#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007708 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 redraw_cmdline = TRUE; /* show visual mode later */
7710 /*
7711 * For V and ^V, we multiply the number of lines even if there
7712 * was only one -- webb
7713 */
7714 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7715 {
7716 curwin->w_cursor.lnum +=
7717 resel_VIsual_line_count * cap->count0 - 1;
7718 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7719 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7720 }
7721 VIsual_mode = resel_VIsual_mode;
7722 if (VIsual_mode == 'v')
7723 {
7724 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007725 {
7726 validate_virtcol();
7727 curwin->w_curswant = curwin->w_virtcol
7728 + resel_VIsual_vcol * cap->count0 - 1;
7729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007731 curwin->w_curswant = resel_VIsual_vcol;
7732 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007734 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {
7736 curwin->w_curswant = MAXCOL;
7737 coladvance((colnr_T)MAXCOL);
7738 }
7739 else if (VIsual_mode == Ctrl_V)
7740 {
7741 validate_virtcol();
7742 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007743 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 coladvance(curwin->w_curswant);
7745 }
7746 else
7747 curwin->w_set_curswant = TRUE;
7748 redraw_curbuf_later(INVERTED); /* show the inversion */
7749 }
7750 else
7751 {
7752 if (!cap->arg)
7753 /* start Select mode when 'selectmode' contains "cmd" */
7754 may_start_select('c');
7755 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007756 if (VIsual_mode != 'V' && *p_sel == 'e')
7757 ++cap->count1; /* include one more char */
7758 if (cap->count0 > 0 && --cap->count1 > 0)
7759 {
7760 /* With a count select that many characters or lines. */
7761 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7762 nv_right(cap);
7763 else if (VIsual_mode == 'V')
7764 nv_down(cap);
7765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767 }
7768}
7769
7770/*
7771 * Start selection for Shift-movement keys.
7772 */
7773 void
7774start_selection()
7775{
7776 /* if 'selectmode' contains "key", start Select mode */
7777 may_start_select('k');
7778 n_start_visual_mode('v');
7779}
7780
7781/*
7782 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7783 */
7784 void
7785may_start_select(c)
7786 int c;
7787{
7788 VIsual_select = (stuff_empty() && typebuf_typed()
7789 && (vim_strchr(p_slm, c) != NULL));
7790}
7791
7792/*
7793 * Start Visual mode "c".
7794 * Should set VIsual_select before calling this.
7795 */
7796 static void
7797n_start_visual_mode(c)
7798 int c;
7799{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007800#ifdef FEAT_CONCEAL
7801 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007802 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007803#endif
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 VIsual_mode = c;
7806 VIsual_active = TRUE;
7807 VIsual_reselect = TRUE;
7808#ifdef FEAT_VIRTUALEDIT
7809 /* Corner case: the 0 position in a tab may change when going into
7810 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7811 */
7812 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007813 {
7814 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817#endif
7818 VIsual = curwin->w_cursor;
7819
7820#ifdef FEAT_FOLDING
7821 foldAdjustVisual();
7822#endif
7823
7824#ifdef FEAT_MOUSE
7825 setmouse();
7826#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007827#ifdef FEAT_CONCEAL
7828 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007829 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007830#endif
7831
Bram Moolenaar09df3122006-01-23 22:23:09 +00007832 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 redraw_cmdline = TRUE; /* show visual mode later */
7834#ifdef FEAT_CLIPBOARD
7835 /* Make sure the clipboard gets updated. Needed because start and
7836 * end may still be the same, and the selection needs to be owned */
7837 clip_star.vmode = NUL;
7838#endif
7839
7840 /* Only need to redraw this line, unless still need to redraw an old
7841 * Visual area (when 'lazyredraw' is set). */
7842 if (curwin->w_redr_type < INVERTED)
7843 {
7844 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7845 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7846 }
7847}
7848
7849#endif /* FEAT_VISUAL */
7850
7851/*
7852 * CTRL-W: Window commands
7853 */
7854 static void
7855nv_window(cap)
7856 cmdarg_T *cap;
7857{
7858#ifdef FEAT_WINDOWS
7859 if (!checkclearop(cap->oap))
7860 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7861#else
7862 (void)checkclearop(cap->oap);
7863#endif
7864}
7865
7866/*
7867 * CTRL-Z: Suspend
7868 */
7869 static void
7870nv_suspend(cap)
7871 cmdarg_T *cap;
7872{
7873 clearop(cap->oap);
7874#ifdef FEAT_VISUAL
7875 if (VIsual_active)
7876 end_visual_mode(); /* stop Visual mode */
7877#endif
7878 do_cmdline_cmd((char_u *)"st");
7879}
7880
7881/*
7882 * Commands starting with "g".
7883 */
7884 static void
7885nv_g_cmd(cap)
7886 cmdarg_T *cap;
7887{
7888 oparg_T *oap = cap->oap;
7889#ifdef FEAT_VISUAL
7890 pos_T tpos;
7891#endif
7892 int i;
7893 int flag = FALSE;
7894
7895 switch (cap->nchar)
7896 {
7897#ifdef MEM_PROFILE
7898 /*
7899 * "g^A": dump log of used memory.
7900 */
7901 case Ctrl_A:
7902 vim_mem_profile_dump();
7903 break;
7904#endif
7905
7906#ifdef FEAT_VREPLACE
7907 /*
7908 * "gR": Enter virtual replace mode.
7909 */
7910 case 'R':
7911 cap->arg = TRUE;
7912 nv_Replace(cap);
7913 break;
7914
7915 case 'r':
7916 nv_vreplace(cap);
7917 break;
7918#endif
7919
7920 case '&':
7921 do_cmdline_cmd((char_u *)"%s//~/&");
7922 break;
7923
7924#ifdef FEAT_VISUAL
7925 /*
7926 * "gv": Reselect the previous Visual area. If Visual already active,
7927 * exchange previous and current Visual area.
7928 */
7929 case 'v':
7930 if (checkclearop(oap))
7931 break;
7932
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007933 if ( curbuf->b_visual.vi_start.lnum == 0
7934 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7935 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 beep_flush();
7937 else
7938 {
7939 /* set w_cursor to the start of the Visual area, tpos to the end */
7940 if (VIsual_active)
7941 {
7942 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007943 VIsual_mode = curbuf->b_visual.vi_mode;
7944 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945# ifdef FEAT_EVAL
7946 curbuf->b_visual_mode_eval = i;
7947# endif
7948 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007949 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7950 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007952 tpos = curbuf->b_visual.vi_end;
7953 curbuf->b_visual.vi_end = curwin->w_cursor;
7954 curwin->w_cursor = curbuf->b_visual.vi_start;
7955 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957 else
7958 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007959 VIsual_mode = curbuf->b_visual.vi_mode;
7960 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7961 tpos = curbuf->b_visual.vi_end;
7962 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964
7965 VIsual_active = TRUE;
7966 VIsual_reselect = TRUE;
7967
7968 /* Set Visual to the start and w_cursor to the end of the Visual
7969 * area. Make sure they are on an existing character. */
7970 check_cursor();
7971 VIsual = curwin->w_cursor;
7972 curwin->w_cursor = tpos;
7973 check_cursor();
7974 update_topline();
7975 /*
7976 * When called from normal "g" command: start Select mode when
7977 * 'selectmode' contains "cmd". When called for K_SELECT, always
7978 * start Select mode.
7979 */
7980 if (cap->arg)
7981 VIsual_select = TRUE;
7982 else
7983 may_start_select('c');
7984#ifdef FEAT_MOUSE
7985 setmouse();
7986#endif
7987#ifdef FEAT_CLIPBOARD
7988 /* Make sure the clipboard gets updated. Needed because start and
7989 * end are still the same, and the selection needs to be owned */
7990 clip_star.vmode = NUL;
7991#endif
7992 redraw_curbuf_later(INVERTED);
7993 showmode();
7994 }
7995 break;
7996 /*
7997 * "gV": Don't reselect the previous Visual area after a Select mode
7998 * mapping of menu.
7999 */
8000 case 'V':
8001 VIsual_reselect = FALSE;
8002 break;
8003
8004 /*
8005 * "gh": start Select mode.
8006 * "gH": start Select line mode.
8007 * "g^H": start Select block mode.
8008 */
8009 case K_BS:
8010 cap->nchar = Ctrl_H;
8011 /* FALLTHROUGH */
8012 case 'h':
8013 case 'H':
8014 case Ctrl_H:
8015# ifdef EBCDIC
8016 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8017 if (cap->nchar == Ctrl_H)
8018 cap->cmdchar = Ctrl_V;
8019 else
8020# endif
8021 cap->cmdchar = cap->nchar + ('v' - 'h');
8022 cap->arg = TRUE;
8023 nv_visual(cap);
8024 break;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02008025#endif /* FEAT_VISUAL */
Bram Moolenaar641e2862012-07-25 15:06:34 +02008026
8027 /* "gn", "gN" visually select next/previous search match
8028 * "gn" selects next match
8029 * "gN" selects previous match
8030 */
8031 case 'N':
8032 case 'n':
Bram Moolenaardad937f2012-07-27 21:05:54 +02008033#ifdef FEAT_VISUAL
Bram Moolenaar641e2862012-07-25 15:06:34 +02008034 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaardad937f2012-07-27 21:05:54 +02008035#endif
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008036 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 /*
8040 * "gj" and "gk" two new funny movement keys -- up and down
8041 * movement based on *screen* line rather than *file* line.
8042 */
8043 case 'j':
8044 case K_DOWN:
8045 /* with 'nowrap' it works just like the normal "j" command; also when
8046 * in a closed fold */
8047 if (!curwin->w_p_wrap
8048#ifdef FEAT_FOLDING
8049 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8050#endif
8051 )
8052 {
8053 oap->motion_type = MLINE;
8054 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8055 }
8056 else
8057 i = nv_screengo(oap, FORWARD, cap->count1);
8058 if (i == FAIL)
8059 clearopbeep(oap);
8060 break;
8061
8062 case 'k':
8063 case K_UP:
8064 /* with 'nowrap' it works just like the normal "k" command; also when
8065 * in a closed fold */
8066 if (!curwin->w_p_wrap
8067#ifdef FEAT_FOLDING
8068 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8069#endif
8070 )
8071 {
8072 oap->motion_type = MLINE;
8073 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8074 }
8075 else
8076 i = nv_screengo(oap, BACKWARD, cap->count1);
8077 if (i == FAIL)
8078 clearopbeep(oap);
8079 break;
8080
8081 /*
8082 * "gJ": join two lines without inserting a space.
8083 */
8084 case 'J':
8085 nv_join(cap);
8086 break;
8087
8088 /*
8089 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8090 * "gm": middle of "g0" and "g$".
8091 */
8092 case '^':
8093 flag = TRUE;
8094 /* FALLTHROUGH */
8095
8096 case '0':
8097 case 'm':
8098 case K_HOME:
8099 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 oap->motion_type = MCHAR;
8101 oap->inclusive = FALSE;
8102 if (curwin->w_p_wrap
8103#ifdef FEAT_VERTSPLIT
8104 && curwin->w_width != 0
8105#endif
8106 )
8107 {
8108 int width1 = W_WIDTH(curwin) - curwin_col_off();
8109 int width2 = width1 + curwin_col_off2();
8110
8111 validate_virtcol();
8112 i = 0;
8113 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8114 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8115 }
8116 else
8117 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008118 /* Go to the middle of the screen line. When 'number' or
8119 * 'relativenumber' is on and lines are wrapping the middle can be more
8120 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (cap->nchar == 'm')
8122 i += (W_WIDTH(curwin) - curwin_col_off()
8123 + ((curwin->w_p_wrap && i > 0)
8124 ? curwin_col_off2() : 0)) / 2;
8125 coladvance((colnr_T)i);
8126 if (flag)
8127 {
8128 do
8129 i = gchar_cursor();
8130 while (vim_iswhite(i) && oneright() == OK);
8131 }
8132 curwin->w_set_curswant = TRUE;
8133 break;
8134
8135 case '_':
8136 /* "g_": to the last non-blank character in the line or <count> lines
8137 * downward. */
8138 cap->oap->motion_type = MCHAR;
8139 cap->oap->inclusive = TRUE;
8140 curwin->w_curswant = MAXCOL;
8141 if (cursor_down((long)(cap->count1 - 1),
8142 cap->oap->op_type == OP_NOP) == FAIL)
8143 clearopbeep(cap->oap);
8144 else
8145 {
8146 char_u *ptr = ml_get_curline();
8147
8148 /* In Visual mode we may end up after the line. */
8149 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8150 --curwin->w_cursor.col;
8151
8152 /* Decrease the cursor column until it's on a non-blank. */
8153 while (curwin->w_cursor.col > 0
8154 && vim_iswhite(ptr[curwin->w_cursor.col]))
8155 --curwin->w_cursor.col;
8156 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008157#ifdef FEAT_VISUAL
8158 adjust_for_sel(cap);
8159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 }
8161 break;
8162
8163 case '$':
8164 case K_END:
8165 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
8167 int col_off = curwin_col_off();
8168
8169 oap->motion_type = MCHAR;
8170 oap->inclusive = TRUE;
8171 if (curwin->w_p_wrap
8172#ifdef FEAT_VERTSPLIT
8173 && curwin->w_width != 0
8174#endif
8175 )
8176 {
8177 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8178 if (cap->count1 == 1)
8179 {
8180 int width1 = W_WIDTH(curwin) - col_off;
8181 int width2 = width1 + curwin_col_off2();
8182
8183 validate_virtcol();
8184 i = width1 - 1;
8185 if (curwin->w_virtcol >= (colnr_T)width1)
8186 i += ((curwin->w_virtcol - width1) / width2 + 1)
8187 * width2;
8188 coladvance((colnr_T)i);
8189#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8190 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8191 {
8192 /*
8193 * Check for landing on a character that got split at
8194 * the end of the line. We do not want to advance to
8195 * the next screen line.
8196 */
8197 validate_virtcol();
8198 if (curwin->w_virtcol > (colnr_T)i)
8199 --curwin->w_cursor.col;
8200 }
8201#endif
8202 }
8203 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8204 clearopbeep(oap);
8205 }
8206 else
8207 {
8208 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8209 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008210
8211 /* Make sure we stick in this column. */
8212 validate_virtcol();
8213 curwin->w_curswant = curwin->w_virtcol;
8214 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
8216 }
8217 break;
8218
8219 /*
8220 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8221 */
8222 case '*':
8223 case '#':
8224#if POUND != '#'
8225 case POUND: /* pound sign (sometimes equal to '#') */
8226#endif
8227 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8228 case ']': /* :tselect for current identifier */
8229 nv_ident(cap);
8230 break;
8231
8232 /*
8233 * ge and gE: go back to end of word
8234 */
8235 case 'e':
8236 case 'E':
8237 oap->motion_type = MCHAR;
8238 curwin->w_set_curswant = TRUE;
8239 oap->inclusive = TRUE;
8240 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8241 clearopbeep(oap);
8242 break;
8243
8244 /*
8245 * "g CTRL-G": display info about cursor position
8246 */
8247 case Ctrl_G:
8248 cursor_pos_info();
8249 break;
8250
8251 /*
8252 * "gi": start Insert at the last position.
8253 */
8254 case 'i':
8255 if (curbuf->b_last_insert.lnum != 0)
8256 {
8257 curwin->w_cursor = curbuf->b_last_insert;
8258 check_cursor_lnum();
8259 i = (int)STRLEN(ml_get_curline());
8260 if (curwin->w_cursor.col > (colnr_T)i)
8261 {
8262#ifdef FEAT_VIRTUALEDIT
8263 if (virtual_active())
8264 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8265#endif
8266 curwin->w_cursor.col = i;
8267 }
8268 }
8269 cap->cmdchar = 'i';
8270 nv_edit(cap);
8271 break;
8272
8273 /*
8274 * "gI": Start insert in column 1.
8275 */
8276 case 'I':
8277 beginline(0);
8278 if (!checkclearopq(oap))
8279 invoke_edit(cap, FALSE, 'g', FALSE);
8280 break;
8281
8282#ifdef FEAT_SEARCHPATH
8283 /*
8284 * "gf": goto file, edit file under cursor
8285 * "]f" and "[f": can also be used.
8286 */
8287 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008288 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 nv_gotofile(cap);
8290 break;
8291#endif
8292
8293 /* "g'm" and "g`m": jump to mark without setting pcmark */
8294 case '\'':
8295 cap->arg = TRUE;
8296 /*FALLTHROUGH*/
8297 case '`':
8298 nv_gomark(cap);
8299 break;
8300
8301 /*
8302 * "gs": Goto sleep.
8303 */
8304 case 's':
8305 do_sleep(cap->count1 * 1000L);
8306 break;
8307
8308 /*
8309 * "ga": Display the ascii value of the character under the
8310 * cursor. It is displayed in decimal, hex, and octal. -- webb
8311 */
8312 case 'a':
8313 do_ascii(NULL);
8314 break;
8315
8316#ifdef FEAT_MBYTE
8317 /*
8318 * "g8": Display the bytes used for the UTF-8 character under the
8319 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008320 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 */
8322 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008323 if (cap->count0 == 8)
8324 utf_find_illegal();
8325 else
8326 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 break;
8328#endif
8329
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008330 case '<':
8331 show_sb_text();
8332 break;
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 /*
8335 * "gg": Goto the first line in file. With a count it goes to
8336 * that line number like for "G". -- webb
8337 */
8338 case 'g':
8339 cap->arg = FALSE;
8340 nv_goto(cap);
8341 break;
8342
8343 /*
8344 * Two-character operators:
8345 * "gq" Format text
8346 * "gw" Format text and keep cursor position
8347 * "g~" Toggle the case of the text.
8348 * "gu" Change text to lower case.
8349 * "gU" Change text to upper case.
8350 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008351 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 */
8353 case 'q':
8354 case 'w':
8355 oap->cursor_start = curwin->w_cursor;
8356 /*FALLTHROUGH*/
8357 case '~':
8358 case 'u':
8359 case 'U':
8360 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008361 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 nv_operator(cap);
8363 break;
8364
8365 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008366 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 * current function
8368 * "gD": idem, but in the current file.
8369 */
8370 case 'd':
8371 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008372 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
8374
8375#ifdef FEAT_MOUSE
8376 /*
8377 * g<*Mouse> : <C-*mouse>
8378 */
8379 case K_MIDDLEMOUSE:
8380 case K_MIDDLEDRAG:
8381 case K_MIDDLERELEASE:
8382 case K_LEFTMOUSE:
8383 case K_LEFTDRAG:
8384 case K_LEFTRELEASE:
8385 case K_RIGHTMOUSE:
8386 case K_RIGHTDRAG:
8387 case K_RIGHTRELEASE:
8388 case K_X1MOUSE:
8389 case K_X1DRAG:
8390 case K_X1RELEASE:
8391 case K_X2MOUSE:
8392 case K_X2DRAG:
8393 case K_X2RELEASE:
8394 mod_mask = MOD_MASK_CTRL;
8395 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8396 break;
8397#endif
8398
8399 case K_IGNORE:
8400 break;
8401
8402 /*
8403 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8404 */
8405 case 'p':
8406 case 'P':
8407 nv_put(cap);
8408 break;
8409
8410#ifdef FEAT_BYTEOFF
8411 /* "go": goto byte count from start of buffer */
8412 case 'o':
8413 goto_byte(cap->count0);
8414 break;
8415#endif
8416
8417 /* "gQ": improved Ex mode */
8418 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008419 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {
8421 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008422 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 break;
8424 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008425
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 if (!checkclearopq(oap))
8427 do_exmode(TRUE);
8428 break;
8429
8430#ifdef FEAT_JUMPLIST
8431 case ',':
8432 nv_pcmark(cap);
8433 break;
8434
8435 case ';':
8436 cap->count1 = -cap->count1;
8437 nv_pcmark(cap);
8438 break;
8439#endif
8440
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441#ifdef FEAT_WINDOWS
8442 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008443 if (!checkclearop(oap))
8444 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008445 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008446 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008447 if (!checkclearop(oap))
8448 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008449 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008450#endif
8451
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008452 case '+':
8453 case '-': /* "g+" and "g-": undo or redo along the timeline */
8454 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008455 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008456 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008457 break;
8458
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 default:
8460 clearopbeep(oap);
8461 break;
8462 }
8463}
8464
8465/*
8466 * Handle "o" and "O" commands.
8467 */
8468 static void
8469n_opencmd(cap)
8470 cmdarg_T *cap;
8471{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008472#ifdef FEAT_CONCEAL
8473 linenr_T oldline = curwin->w_cursor.lnum;
8474#endif
8475
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if (!checkclearopq(cap->oap))
8477 {
8478#ifdef FEAT_FOLDING
8479 if (cap->cmdchar == 'O')
8480 /* Open above the first line of a folded sequence of lines */
8481 (void)hasFolding(curwin->w_cursor.lnum,
8482 &curwin->w_cursor.lnum, NULL);
8483 else
8484 /* Open below the last line of a folded sequence of lines */
8485 (void)hasFolding(curwin->w_cursor.lnum,
8486 NULL, &curwin->w_cursor.lnum);
8487#endif
8488 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8489 (cap->cmdchar == 'O' ? 1 : 0)),
8490 (linenr_T)(curwin->w_cursor.lnum +
8491 (cap->cmdchar == 'o' ? 1 : 0))
8492 ) == OK
8493 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8494#ifdef FEAT_COMMENTS
8495 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8496#endif
8497 0, 0))
8498 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008499#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008500 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008501 update_single_line(curwin, oldline);
8502#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008503 /* When '#' is in 'cpoptions' ignore the count. */
8504 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8505 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8507 }
8508 }
8509}
8510
8511/*
8512 * "." command: redo last change.
8513 */
8514 static void
8515nv_dot(cap)
8516 cmdarg_T *cap;
8517{
8518 if (!checkclearopq(cap->oap))
8519 {
8520 /*
8521 * If "restart_edit" is TRUE, the last but one command is repeated
8522 * instead of the last command (inserting text). This is used for
8523 * CTRL-O <.> in insert mode.
8524 */
8525 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8526 clearopbeep(cap->oap);
8527 }
8528}
8529
8530/*
8531 * CTRL-R: undo undo
8532 */
8533 static void
8534nv_redo(cap)
8535 cmdarg_T *cap;
8536{
8537 if (!checkclearopq(cap->oap))
8538 {
8539 u_redo((int)cap->count1);
8540 curwin->w_set_curswant = TRUE;
8541 }
8542}
8543
8544/*
8545 * Handle "U" command.
8546 */
8547 static void
8548nv_Undo(cap)
8549 cmdarg_T *cap;
8550{
8551 /* In Visual mode and typing "gUU" triggers an operator */
8552 if (cap->oap->op_type == OP_UPPER
8553#ifdef FEAT_VISUAL
8554 || VIsual_active
8555#endif
8556 )
8557 {
8558 /* translate "gUU" to "gUgU" */
8559 cap->cmdchar = 'g';
8560 cap->nchar = 'U';
8561 nv_operator(cap);
8562 }
8563 else if (!checkclearopq(cap->oap))
8564 {
8565 u_undoline();
8566 curwin->w_set_curswant = TRUE;
8567 }
8568}
8569
8570/*
8571 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8572 * single character.
8573 */
8574 static void
8575nv_tilde(cap)
8576 cmdarg_T *cap;
8577{
8578 if (!p_to
8579#ifdef FEAT_VISUAL
8580 && !VIsual_active
8581#endif
8582 && cap->oap->op_type != OP_TILDE)
8583 n_swapchar(cap);
8584 else
8585 nv_operator(cap);
8586}
8587
8588/*
8589 * Handle an operator command.
8590 * The actual work is done by do_pending_operator().
8591 */
8592 static void
8593nv_operator(cap)
8594 cmdarg_T *cap;
8595{
8596 int op_type;
8597
8598 op_type = get_op_type(cap->cmdchar, cap->nchar);
8599
8600 if (op_type == cap->oap->op_type) /* double operator works on lines */
8601 nv_lineop(cap);
8602 else if (!checkclearop(cap->oap))
8603 {
8604 cap->oap->start = curwin->w_cursor;
8605 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008606#ifdef FEAT_EVAL
8607 set_op_var(op_type);
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 }
8610}
8611
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008612#ifdef FEAT_EVAL
8613/*
8614 * Set v:operator to the characters for "optype".
8615 */
8616 static void
8617set_op_var(optype)
8618 int optype;
8619{
8620 char_u opchars[3];
8621
8622 if (optype == OP_NOP)
8623 set_vim_var_string(VV_OP, NULL, 0);
8624 else
8625 {
8626 opchars[0] = get_op_char(optype);
8627 opchars[1] = get_extra_op_char(optype);
8628 opchars[2] = NUL;
8629 set_vim_var_string(VV_OP, opchars, -1);
8630 }
8631}
8632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634/*
8635 * Handle linewise operator "dd", "yy", etc.
8636 *
8637 * "_" is is a strange motion command that helps make operators more logical.
8638 * It is actually implemented, but not documented in the real Vi. This motion
8639 * command actually refers to "the current line". Commands like "dd" and "yy"
8640 * are really an alternate form of "d_" and "y_". It does accept a count, so
8641 * "d3_" works to delete 3 lines.
8642 */
8643 static void
8644nv_lineop(cap)
8645 cmdarg_T *cap;
8646{
8647 cap->oap->motion_type = MLINE;
8648 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8649 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008650 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8651 && cap->oap->motion_force != 'v'
8652 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 || cap->oap->op_type == OP_LSHIFT
8654 || cap->oap->op_type == OP_RSHIFT)
8655 beginline(BL_SOL | BL_FIX);
8656 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8657 beginline(BL_WHITE | BL_FIX);
8658}
8659
8660/*
8661 * <Home> command.
8662 */
8663 static void
8664nv_home(cap)
8665 cmdarg_T *cap;
8666{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008667 /* CTRL-HOME is like "gg" */
8668 if (mod_mask & MOD_MASK_CTRL)
8669 nv_goto(cap);
8670 else
8671 {
8672 cap->count0 = 1;
8673 nv_pipe(cap);
8674 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008675 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8676 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677}
8678
8679/*
8680 * "|" command.
8681 */
8682 static void
8683nv_pipe(cap)
8684 cmdarg_T *cap;
8685{
8686 cap->oap->motion_type = MCHAR;
8687 cap->oap->inclusive = FALSE;
8688 beginline(0);
8689 if (cap->count0 > 0)
8690 {
8691 coladvance((colnr_T)(cap->count0 - 1));
8692 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8693 }
8694 else
8695 curwin->w_curswant = 0;
8696 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008697 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 curwin->w_set_curswant = FALSE;
8699}
8700
8701/*
8702 * Handle back-word command "b" and "B".
8703 * cap->arg is 1 for "B"
8704 */
8705 static void
8706nv_bck_word(cap)
8707 cmdarg_T *cap;
8708{
8709 cap->oap->motion_type = MCHAR;
8710 cap->oap->inclusive = FALSE;
8711 curwin->w_set_curswant = TRUE;
8712 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8713 clearopbeep(cap->oap);
8714#ifdef FEAT_FOLDING
8715 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8716 foldOpenCursor();
8717#endif
8718}
8719
8720/*
8721 * Handle word motion commands "e", "E", "w" and "W".
8722 * cap->arg is TRUE for "E" and "W".
8723 */
8724 static void
8725nv_wordcmd(cap)
8726 cmdarg_T *cap;
8727{
8728 int n;
8729 int word_end;
8730 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008731 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732
8733 /*
8734 * Set inclusive for the "E" and "e" command.
8735 */
8736 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8737 word_end = TRUE;
8738 else
8739 word_end = FALSE;
8740 cap->oap->inclusive = word_end;
8741
8742 /*
8743 * "cw" and "cW" are a special case.
8744 */
8745 if (!word_end && cap->oap->op_type == OP_CHANGE)
8746 {
8747 n = gchar_cursor();
8748 if (n != NUL) /* not an empty line */
8749 {
8750 if (vim_iswhite(n))
8751 {
8752 /*
8753 * Reproduce a funny Vi behaviour: "cw" on a blank only
8754 * changes one character, not all blanks until the start of
8755 * the next word. Only do this when the 'w' flag is included
8756 * in 'cpoptions'.
8757 */
8758 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8759 {
8760 cap->oap->inclusive = TRUE;
8761 cap->oap->motion_type = MCHAR;
8762 return;
8763 }
8764 }
8765 else
8766 {
8767 /*
8768 * This is a little strange. To match what the real Vi does,
8769 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8770 * that we are not on a space or a TAB. This seems impolite
8771 * at first, but it's really more what we mean when we say
8772 * 'cw'.
8773 * Another strangeness: When standing on the end of a word
8774 * "ce" will change until the end of the next wordt, but "cw"
8775 * will change only one character! This is done by setting
8776 * flag.
8777 */
8778 cap->oap->inclusive = TRUE;
8779 word_end = TRUE;
8780 flag = TRUE;
8781 }
8782 }
8783 }
8784
8785 cap->oap->motion_type = MCHAR;
8786 curwin->w_set_curswant = TRUE;
8787 if (word_end)
8788 n = end_word(cap->count1, cap->arg, flag, FALSE);
8789 else
8790 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8791
Bram Moolenaardfefb982008-04-01 10:06:39 +00008792 /* Don't leave the cursor on the NUL past the end of line. Unless we
8793 * didn't move it forward. */
8794 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008795 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796
8797 if (n == FAIL && cap->oap->op_type == OP_NOP)
8798 clearopbeep(cap->oap);
8799 else
8800 {
8801#ifdef FEAT_VISUAL
8802 adjust_for_sel(cap);
8803#endif
8804#ifdef FEAT_FOLDING
8805 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8806 foldOpenCursor();
8807#endif
8808 }
8809}
8810
8811/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008812 * Used after a movement command: If the cursor ends up on the NUL after the
8813 * end of the line, may move it back to the last character and make the motion
8814 * inclusive.
8815 */
8816 static void
8817adjust_cursor(oap)
8818 oparg_T *oap;
8819{
8820 /* The cursor cannot remain on the NUL when:
8821 * - the column is > 0
8822 * - not in Visual mode or 'selection' is "o"
8823 * - 'virtualedit' is not "all" and not "onemore".
8824 */
8825 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8826#ifdef FEAT_VISUAL
8827 && (!VIsual_active || *p_sel == 'o')
8828#endif
8829#ifdef FEAT_VIRTUALEDIT
8830 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8831#endif
8832 )
8833 {
8834 --curwin->w_cursor.col;
8835#ifdef FEAT_MBYTE
8836 /* prevent cursor from moving on the trail byte */
8837 if (has_mbyte)
8838 mb_adjust_cursor();
8839#endif
8840 oap->inclusive = TRUE;
8841 }
8842}
8843
8844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 * "0" and "^" commands.
8846 * cap->arg is the argument for beginline().
8847 */
8848 static void
8849nv_beginline(cap)
8850 cmdarg_T *cap;
8851{
8852 cap->oap->motion_type = MCHAR;
8853 cap->oap->inclusive = FALSE;
8854 beginline(cap->arg);
8855#ifdef FEAT_FOLDING
8856 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8857 foldOpenCursor();
8858#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008859 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8860 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
8863#ifdef FEAT_VISUAL
8864/*
8865 * In exclusive Visual mode, may include the last character.
8866 */
8867 static void
8868adjust_for_sel(cap)
8869 cmdarg_T *cap;
8870{
8871 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8872 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8873 {
8874# ifdef FEAT_MBYTE
8875 if (has_mbyte)
8876 inc_cursor();
8877 else
8878# endif
8879 ++curwin->w_cursor.col;
8880 cap->oap->inclusive = FALSE;
8881 }
8882}
8883
8884/*
8885 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8886 * Should check VIsual_mode before calling this.
8887 * Returns TRUE when backed up to the previous line.
8888 */
8889 static int
8890unadjust_for_sel()
8891{
8892 pos_T *pp;
8893
8894 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8895 {
8896 if (lt(VIsual, curwin->w_cursor))
8897 pp = &curwin->w_cursor;
8898 else
8899 pp = &VIsual;
8900#ifdef FEAT_VIRTUALEDIT
8901 if (pp->coladd > 0)
8902 --pp->coladd;
8903 else
8904#endif
8905 if (pp->col > 0)
8906 {
8907 --pp->col;
8908#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008909 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910#endif
8911 }
8912 else if (pp->lnum > 1)
8913 {
8914 --pp->lnum;
8915 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8916 return TRUE;
8917 }
8918 }
8919 return FALSE;
8920}
8921
8922/*
8923 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8924 */
8925 static void
8926nv_select(cap)
8927 cmdarg_T *cap;
8928{
8929 if (VIsual_active)
8930 VIsual_select = TRUE;
8931 else if (VIsual_reselect)
8932 {
8933 cap->nchar = 'v'; /* fake "gv" command */
8934 cap->arg = TRUE;
8935 nv_g_cmd(cap);
8936 }
8937}
8938
8939#endif
8940
8941/*
8942 * "G", "gg", CTRL-END, CTRL-HOME.
8943 * cap->arg is TRUE for "G".
8944 */
8945 static void
8946nv_goto(cap)
8947 cmdarg_T *cap;
8948{
8949 linenr_T lnum;
8950
8951 if (cap->arg)
8952 lnum = curbuf->b_ml.ml_line_count;
8953 else
8954 lnum = 1L;
8955 cap->oap->motion_type = MLINE;
8956 setpcmark();
8957
8958 /* When a count is given, use it instead of the default lnum */
8959 if (cap->count0 != 0)
8960 lnum = cap->count0;
8961 if (lnum < 1L)
8962 lnum = 1L;
8963 else if (lnum > curbuf->b_ml.ml_line_count)
8964 lnum = curbuf->b_ml.ml_line_count;
8965 curwin->w_cursor.lnum = lnum;
8966 beginline(BL_SOL | BL_FIX);
8967#ifdef FEAT_FOLDING
8968 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8969 foldOpenCursor();
8970#endif
8971}
8972
8973/*
8974 * CTRL-\ in Normal mode.
8975 */
8976 static void
8977nv_normal(cap)
8978 cmdarg_T *cap;
8979{
8980 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8981 {
8982 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008983 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 clear_cmdline = TRUE; /* unshow mode later */
8985 restart_edit = 0;
8986#ifdef FEAT_CMDWIN
8987 if (cmdwin_type != 0)
8988 cmdwin_result = Ctrl_C;
8989#endif
8990#ifdef FEAT_VISUAL
8991 if (VIsual_active)
8992 {
8993 end_visual_mode(); /* stop Visual */
8994 redraw_curbuf_later(INVERTED);
8995 }
8996#endif
8997 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8998 if (cap->nchar == Ctrl_G && p_im)
8999 restart_edit = 'a';
9000 }
9001 else
9002 clearopbeep(cap->oap);
9003}
9004
9005/*
9006 * ESC in Normal mode: beep, but don't flush buffers.
9007 * Don't even beep if we are canceling a command.
9008 */
9009 static void
9010nv_esc(cap)
9011 cmdarg_T *cap;
9012{
9013 int no_reason;
9014
9015 no_reason = (cap->oap->op_type == OP_NOP
9016 && cap->opcount == 0
9017 && cap->count0 == 0
9018 && cap->oap->regname == 0
9019 && !p_im);
9020
9021 if (cap->arg) /* TRUE for CTRL-C */
9022 {
9023 if (restart_edit == 0
9024#ifdef FEAT_CMDWIN
9025 && cmdwin_type == 0
9026#endif
9027#ifdef FEAT_VISUAL
9028 && !VIsual_active
9029#endif
9030 && no_reason)
9031 MSG(_("Type :quit<Enter> to exit Vim"));
9032
9033 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9034 * set again below when halfway a mapping. */
9035 if (!p_im)
9036 restart_edit = 0;
9037#ifdef FEAT_CMDWIN
9038 if (cmdwin_type != 0)
9039 {
9040 cmdwin_result = K_IGNORE;
9041 got_int = FALSE; /* don't stop executing autocommands et al. */
9042 return;
9043 }
9044#endif
9045 }
9046
9047#ifdef FEAT_VISUAL
9048 if (VIsual_active)
9049 {
9050 end_visual_mode(); /* stop Visual */
9051 check_cursor_col(); /* make sure cursor is not beyond EOL */
9052 curwin->w_set_curswant = TRUE;
9053 redraw_curbuf_later(INVERTED);
9054 }
9055 else
9056#endif
9057 if (no_reason)
9058 vim_beep();
9059 clearop(cap->oap);
9060
9061 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9062 * set return to Insert mode afterwards. */
9063 if (restart_edit == 0 && goto_im()
9064#ifdef FEAT_EX_EXTRA
9065 && ex_normal_busy == 0
9066#endif
9067 )
9068 restart_edit = 'a';
9069}
9070
9071/*
9072 * Handle "A", "a", "I", "i" and <Insert> commands.
9073 */
9074 static void
9075nv_edit(cap)
9076 cmdarg_T *cap;
9077{
9078 /* <Insert> is equal to "i" */
9079 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9080 cap->cmdchar = 'i';
9081
9082#ifdef FEAT_VISUAL
9083 /* in Visual mode "A" and "I" are an operator */
9084 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9085 v_visop(cap);
9086
9087 /* in Visual mode and after an operator "a" and "i" are for text objects */
9088 else
9089#endif
9090 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9091 && (cap->oap->op_type != OP_NOP
9092#ifdef FEAT_VISUAL
9093 || VIsual_active
9094#endif
9095 ))
9096 {
9097#ifdef FEAT_TEXTOBJ
9098 nv_object(cap);
9099#else
9100 clearopbeep(cap->oap);
9101#endif
9102 }
9103 else if (!curbuf->b_p_ma && !p_im)
9104 {
9105 /* Only give this error when 'insertmode' is off. */
9106 EMSG(_(e_modifiable));
9107 clearop(cap->oap);
9108 }
9109 else if (!checkclearopq(cap->oap))
9110 {
9111 switch (cap->cmdchar)
9112 {
9113 case 'A': /* "A"ppend after the line */
9114 curwin->w_set_curswant = TRUE;
9115#ifdef FEAT_VIRTUALEDIT
9116 if (ve_flags == VE_ALL)
9117 {
9118 int save_State = State;
9119
9120 /* Pretent Insert mode here to allow the cursor on the
9121 * character past the end of the line */
9122 State = INSERT;
9123 coladvance((colnr_T)MAXCOL);
9124 State = save_State;
9125 }
9126 else
9127#endif
9128 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9129 break;
9130
9131 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009132 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9133 beginline(BL_WHITE);
9134 else
9135 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 break;
9137
9138 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9139#ifdef FEAT_VIRTUALEDIT
9140 /* increment coladd when in virtual space, increment the
9141 * column otherwise, also to append after an unprintable char */
9142 if (virtual_active()
9143 && (curwin->w_cursor.coladd > 0
9144 || *ml_get_cursor() == NUL
9145 || *ml_get_cursor() == TAB))
9146 curwin->w_cursor.coladd++;
9147 else
9148#endif
9149 if (*ml_get_cursor() != NUL)
9150 inc_cursor();
9151 break;
9152 }
9153
9154#ifdef FEAT_VIRTUALEDIT
9155 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9156 {
9157 int save_State = State;
9158
9159 /* Pretent Insert mode here to allow the cursor on the
9160 * character past the end of the line */
9161 State = INSERT;
9162 coladvance(getviscol());
9163 State = save_State;
9164 }
9165#endif
9166
9167 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9168 }
9169}
9170
9171/*
9172 * Invoke edit() and take care of "restart_edit" and the return value.
9173 */
9174 static void
9175invoke_edit(cap, repl, cmd, startln)
9176 cmdarg_T *cap;
9177 int repl; /* "r" or "gr" command */
9178 int cmd;
9179 int startln;
9180{
9181 int restart_edit_save = 0;
9182
9183 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9184 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9185 * it. */
9186 if (repl || !stuff_empty())
9187 restart_edit_save = restart_edit;
9188 else
9189 restart_edit_save = 0;
9190
9191 /* Always reset "restart_edit", this is not a restarted edit. */
9192 restart_edit = 0;
9193
9194 if (edit(cmd, startln, cap->count1))
9195 cap->retval |= CA_COMMAND_BUSY;
9196
9197 if (restart_edit == 0)
9198 restart_edit = restart_edit_save;
9199}
9200
9201#ifdef FEAT_TEXTOBJ
9202/*
9203 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9204 */
9205 static void
9206nv_object(cap)
9207 cmdarg_T *cap;
9208{
9209 int flag;
9210 int include;
9211 char_u *mps_save;
9212
9213 if (cap->cmdchar == 'i')
9214 include = FALSE; /* "ix" = inner object: exclude white space */
9215 else
9216 include = TRUE; /* "ax" = an object: include white space */
9217
9218 /* Make sure (), [], {} and <> are in 'matchpairs' */
9219 mps_save = curbuf->b_p_mps;
9220 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9221
9222 switch (cap->nchar)
9223 {
9224 case 'w': /* "aw" = a word */
9225 flag = current_word(cap->oap, cap->count1, include, FALSE);
9226 break;
9227 case 'W': /* "aW" = a WORD */
9228 flag = current_word(cap->oap, cap->count1, include, TRUE);
9229 break;
9230 case 'b': /* "ab" = a braces block */
9231 case '(':
9232 case ')':
9233 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9234 break;
9235 case 'B': /* "aB" = a Brackets block */
9236 case '{':
9237 case '}':
9238 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9239 break;
9240 case '[': /* "a[" = a [] block */
9241 case ']':
9242 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9243 break;
9244 case '<': /* "a<" = a <> block */
9245 case '>':
9246 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9247 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009248 case 't': /* "at" = a tag block (xml and html) */
9249 flag = current_tagblock(cap->oap, cap->count1, include);
9250 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 case 'p': /* "ap" = a paragraph */
9252 flag = current_par(cap->oap, cap->count1, include, 'p');
9253 break;
9254 case 's': /* "as" = a sentence */
9255 flag = current_sent(cap->oap, cap->count1, include);
9256 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009257 case '"': /* "a"" = a double quoted string */
9258 case '\'': /* "a'" = a single quoted string */
9259 case '`': /* "a`" = a backtick quoted string */
9260 flag = current_quote(cap->oap, cap->count1, include,
9261 cap->nchar);
9262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263#if 0 /* TODO */
9264 case 'S': /* "aS" = a section */
9265 case 'f': /* "af" = a filename */
9266 case 'u': /* "au" = a URL */
9267#endif
9268 default:
9269 flag = FAIL;
9270 break;
9271 }
9272
9273 curbuf->b_p_mps = mps_save;
9274 if (flag == FAIL)
9275 clearopbeep(cap->oap);
9276 adjust_cursor_col();
9277 curwin->w_set_curswant = TRUE;
9278}
9279#endif
9280
9281/*
9282 * "q" command: Start/stop recording.
9283 * "q:", "q/", "q?": edit command-line in command-line window.
9284 */
9285 static void
9286nv_record(cap)
9287 cmdarg_T *cap;
9288{
9289 if (cap->oap->op_type == OP_FORMAT)
9290 {
9291 /* "gqq" is the same as "gqgq": format line */
9292 cap->cmdchar = 'g';
9293 cap->nchar = 'q';
9294 nv_operator(cap);
9295 }
9296 else if (!checkclearop(cap->oap))
9297 {
9298#ifdef FEAT_CMDWIN
9299 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9300 {
9301 stuffcharReadbuff(cap->nchar);
9302 stuffcharReadbuff(K_CMDWIN);
9303 }
9304 else
9305#endif
9306 /* (stop) recording into a named register, unless executing a
9307 * register */
9308 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9309 clearopbeep(cap->oap);
9310 }
9311}
9312
9313/*
9314 * Handle the "@r" command.
9315 */
9316 static void
9317nv_at(cap)
9318 cmdarg_T *cap;
9319{
9320 if (checkclearop(cap->oap))
9321 return;
9322#ifdef FEAT_EVAL
9323 if (cap->nchar == '=')
9324 {
9325 if (get_expr_register() == NUL)
9326 return;
9327 }
9328#endif
9329 while (cap->count1-- && !got_int)
9330 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009331 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 {
9333 clearopbeep(cap->oap);
9334 break;
9335 }
9336 line_breakcheck();
9337 }
9338}
9339
9340/*
9341 * Handle the CTRL-U and CTRL-D commands.
9342 */
9343 static void
9344nv_halfpage(cap)
9345 cmdarg_T *cap;
9346{
9347 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9348 || (cap->cmdchar == Ctrl_D
9349 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9350 clearopbeep(cap->oap);
9351 else if (!checkclearop(cap->oap))
9352 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9353}
9354
9355/*
9356 * Handle "J" or "gJ" command.
9357 */
9358 static void
9359nv_join(cap)
9360 cmdarg_T *cap;
9361{
9362#ifdef FEAT_VISUAL
9363 if (VIsual_active) /* join the visual lines */
9364 nv_operator(cap);
9365 else
9366#endif
9367 if (!checkclearop(cap->oap))
9368 {
9369 if (cap->count0 <= 1)
9370 cap->count0 = 2; /* default for join is two lines! */
9371 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9372 curbuf->b_ml.ml_line_count)
9373 clearopbeep(cap->oap); /* beyond last line */
9374 else
9375 {
9376 prep_redo(cap->oap->regname, cap->count0,
9377 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaar81340392012-06-06 16:12:59 +02009378 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380 }
9381}
9382
9383/*
9384 * "P", "gP", "p" and "gp" commands.
9385 */
9386 static void
9387nv_put(cap)
9388 cmdarg_T *cap;
9389{
9390#ifdef FEAT_VISUAL
9391 int regname = 0;
9392 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009393 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009394 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#endif
9396 int dir;
9397 int flags = 0;
9398
9399 if (cap->oap->op_type != OP_NOP)
9400 {
9401#ifdef FEAT_DIFF
9402 /* "dp" is ":diffput" */
9403 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9404 {
9405 clearop(cap->oap);
9406 nv_diffgetput(TRUE);
9407 }
9408 else
9409#endif
9410 clearopbeep(cap->oap);
9411 }
9412 else
9413 {
9414 dir = (cap->cmdchar == 'P'
9415 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9416 ? BACKWARD : FORWARD;
9417 prep_redo_cmd(cap);
9418 if (cap->cmdchar == 'g')
9419 flags |= PUT_CURSEND;
9420
9421#ifdef FEAT_VISUAL
9422 if (VIsual_active)
9423 {
9424 /* Putting in Visual mode: The put text replaces the selected
9425 * text. First delete the selected text, then put the new text.
9426 * Need to save and restore the registers that the delete
9427 * overwrites if the old contents is being put.
9428 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009429 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 regname = cap->oap->regname;
9431# ifdef FEAT_CLIPBOARD
9432 adjust_clip_reg(&regname);
9433# endif
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009434 if (regname == 0 || regname == '"'
9435 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436# ifdef FEAT_CLIPBOARD
9437 || (clip_unnamed && (regname == '*' || regname == '+'))
9438# endif
9439
9440 )
9441 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009442 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 * put, save it first. */
9444 reg1 = get_register(regname, TRUE);
9445 }
9446
9447 /* Now delete the selected text. */
9448 cap->cmdchar = 'd';
9449 cap->nchar = NUL;
9450 cap->oap->regname = NUL;
9451 nv_operator(cap);
9452 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009453 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
9455 /* delete PUT_LINE_BACKWARD; */
9456 cap->oap->regname = regname;
9457
9458 if (reg1 != NULL)
9459 {
9460 /* Delete probably changed the register we want to put, save
9461 * it first. Then put back what was there before the delete. */
9462 reg2 = get_register(regname, FALSE);
9463 put_register(regname, reg1);
9464 }
9465
9466 /* When deleted a linewise Visual area, put the register as
9467 * lines to avoid it joined with the next line. When deletion was
9468 * characterwise, split a line when putting lines. */
9469 if (VIsual_mode == 'V')
9470 flags |= PUT_LINE;
9471 else if (VIsual_mode == 'v')
9472 flags |= PUT_LINE_SPLIT;
9473 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9474 flags |= PUT_LINE_FORWARD;
9475 dir = BACKWARD;
9476 if ((VIsual_mode != 'V'
9477 && curwin->w_cursor.col < curbuf->b_op_start.col)
9478 || (VIsual_mode == 'V'
9479 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9480 /* cursor is at the end of the line or end of file, put
9481 * forward. */
9482 dir = FORWARD;
9483 }
9484#endif
9485 do_put(cap->oap->regname, dir, cap->count1, flags);
9486
9487#ifdef FEAT_VISUAL
9488 /* If a register was saved, put it back now. */
9489 if (reg2 != NULL)
9490 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009491
9492 /* What to reselect with "gv"? Selecting the just put text seems to
9493 * be the most useful, since the original text was removed. */
9494 if (was_visual)
9495 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009496 curbuf->b_visual.vi_start = curbuf->b_op_start;
9497 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009498 }
9499
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009500 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009501 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009502 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009503 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009504 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009505
9506 /* If the cursor was in that line, move it to the end of the last
9507 * line. */
9508 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9509 {
9510 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9511 coladvance((colnr_T)MAXCOL);
9512 }
9513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514#endif
9515 auto_format(FALSE, TRUE);
9516 }
9517}
9518
9519/*
9520 * "o" and "O" commands.
9521 */
9522 static void
9523nv_open(cap)
9524 cmdarg_T *cap;
9525{
9526#ifdef FEAT_DIFF
9527 /* "do" is ":diffget" */
9528 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9529 {
9530 clearop(cap->oap);
9531 nv_diffgetput(FALSE);
9532 }
9533 else
9534#endif
9535#ifdef FEAT_VISUAL
9536 if (VIsual_active) /* switch start and end of visual */
9537 v_swap_corners(cap->cmdchar);
9538 else
9539#endif
9540 n_opencmd(cap);
9541}
9542
9543#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 static void
9545nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009546 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 ProcessSniffRequests();
9549}
9550#endif
9551
9552#ifdef FEAT_NETBEANS_INTG
9553 static void
9554nv_nbcmd(cap)
9555 cmdarg_T *cap;
9556{
9557 netbeans_keycommand(cap->nchar);
9558}
9559#endif
9560
9561#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 static void
9563nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009564 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9567}
9568#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009569
9570#ifdef FEAT_AUTOCMD
9571/*
9572 * Trigger CursorHold event.
9573 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9574 * input buffer. "did_cursorhold" is set to avoid retriggering.
9575 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009576 static void
9577nv_cursorhold(cap)
9578 cmdarg_T *cap;
9579{
9580 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9581 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009582 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009583}
9584#endif