blob: 002430d712dc7e5440ddd841c85a36abbb559858 [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 */
23static colnr_T resel_VIsual_col; /* nr of cols or end col */
24
25static int restart_VIsual_select = 0;
26#endif
27
28static int
29# ifdef __BORLANDC__
30_RTLENTRYF
31# endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33static int find_command __ARGS((int cmdchar));
34static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000035static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37static void find_start_of_word __ARGS((pos_T *));
38static void find_end_of_word __ARGS((pos_T *));
39static int get_mouse_class __ARGS((char_u *p));
40#endif
41static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43static int checkclearop __ARGS((oparg_T *oap));
44static int checkclearopq __ARGS((oparg_T *oap));
45static void clearop __ARGS((oparg_T *oap));
46static void clearopbeep __ARGS((oparg_T *oap));
47#ifdef FEAT_VISUAL
48static void unshift_special __ARGS((cmdarg_T *cap));
49#endif
50#ifdef FEAT_CMDL_INFO
51static void del_from_showcmd __ARGS((int));
52#endif
53
54/*
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
58 */
59static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000060static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void nv_error __ARGS((cmdarg_T *cap));
62static void nv_help __ARGS((cmdarg_T *cap));
63static void nv_addsub __ARGS((cmdarg_T *cap));
64static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000065static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67#ifdef FEAT_MOUSE
68static void nv_mousescroll __ARGS((cmdarg_T *cap));
69static void nv_mouse __ARGS((cmdarg_T *cap));
70#endif
71static void nv_scroll_line __ARGS((cmdarg_T *cap));
72static void nv_zet __ARGS((cmdarg_T *cap));
73#ifdef FEAT_GUI
74static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000077#ifdef FEAT_GUI_TABLINE
78static void nv_tabline __ARGS((cmdarg_T *cap));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000079static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static void nv_exmode __ARGS((cmdarg_T *cap));
82static void nv_colon __ARGS((cmdarg_T *cap));
83static void nv_ctrlg __ARGS((cmdarg_T *cap));
84static void nv_ctrlh __ARGS((cmdarg_T *cap));
85static void nv_clear __ARGS((cmdarg_T *cap));
86static void nv_ctrlo __ARGS((cmdarg_T *cap));
87static void nv_hat __ARGS((cmdarg_T *cap));
88static void nv_Zet __ARGS((cmdarg_T *cap));
89static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void nv_tagpop __ARGS((cmdarg_T *cap));
91static void nv_scroll __ARGS((cmdarg_T *cap));
92static void nv_right __ARGS((cmdarg_T *cap));
93static void nv_left __ARGS((cmdarg_T *cap));
94static void nv_up __ARGS((cmdarg_T *cap));
95static void nv_down __ARGS((cmdarg_T *cap));
96#ifdef FEAT_SEARCHPATH
97static void nv_gotofile __ARGS((cmdarg_T *cap));
98#endif
99static void nv_end __ARGS((cmdarg_T *cap));
100static void nv_dollar __ARGS((cmdarg_T *cap));
101static void nv_search __ARGS((cmdarg_T *cap));
102static void nv_next __ARGS((cmdarg_T *cap));
103static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104static void nv_csearch __ARGS((cmdarg_T *cap));
105static void nv_brackets __ARGS((cmdarg_T *cap));
106static void nv_percent __ARGS((cmdarg_T *cap));
107static void nv_brace __ARGS((cmdarg_T *cap));
108static void nv_mark __ARGS((cmdarg_T *cap));
109static void nv_findpar __ARGS((cmdarg_T *cap));
110static void nv_undo __ARGS((cmdarg_T *cap));
111static void nv_kundo __ARGS((cmdarg_T *cap));
112static void nv_Replace __ARGS((cmdarg_T *cap));
113#ifdef FEAT_VREPLACE
114static void nv_vreplace __ARGS((cmdarg_T *cap));
115#endif
116#ifdef FEAT_VISUAL
117static void v_swap_corners __ARGS((int cmdchar));
118#endif
119static void nv_replace __ARGS((cmdarg_T *cap));
120static void n_swapchar __ARGS((cmdarg_T *cap));
121static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
122#ifdef FEAT_VISUAL
123static void v_visop __ARGS((cmdarg_T *cap));
124#endif
125static void nv_subst __ARGS((cmdarg_T *cap));
126static void nv_abbrev __ARGS((cmdarg_T *cap));
127static void nv_optrans __ARGS((cmdarg_T *cap));
128static void nv_gomark __ARGS((cmdarg_T *cap));
129static void nv_pcmark __ARGS((cmdarg_T *cap));
130static void nv_regname __ARGS((cmdarg_T *cap));
131#ifdef FEAT_VISUAL
132static void nv_visual __ARGS((cmdarg_T *cap));
133static void n_start_visual_mode __ARGS((int c));
134#endif
135static void nv_window __ARGS((cmdarg_T *cap));
136static void nv_suspend __ARGS((cmdarg_T *cap));
137static void nv_g_cmd __ARGS((cmdarg_T *cap));
138static void n_opencmd __ARGS((cmdarg_T *cap));
139static void nv_dot __ARGS((cmdarg_T *cap));
140static void nv_redo __ARGS((cmdarg_T *cap));
141static void nv_Undo __ARGS((cmdarg_T *cap));
142static void nv_tilde __ARGS((cmdarg_T *cap));
143static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000144#ifdef FEAT_EVAL
145static void set_op_var __ARGS((int optype));
146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static void nv_lineop __ARGS((cmdarg_T *cap));
148static void nv_home __ARGS((cmdarg_T *cap));
149static void nv_pipe __ARGS((cmdarg_T *cap));
150static void nv_bck_word __ARGS((cmdarg_T *cap));
151static void nv_wordcmd __ARGS((cmdarg_T *cap));
152static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000153static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154#ifdef FEAT_VISUAL
155static void adjust_for_sel __ARGS((cmdarg_T *cap));
156static int unadjust_for_sel __ARGS((void));
157static void nv_select __ARGS((cmdarg_T *cap));
158#endif
159static void nv_goto __ARGS((cmdarg_T *cap));
160static void nv_normal __ARGS((cmdarg_T *cap));
161static void nv_esc __ARGS((cmdarg_T *oap));
162static void nv_edit __ARGS((cmdarg_T *cap));
163static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
164#ifdef FEAT_TEXTOBJ
165static void nv_object __ARGS((cmdarg_T *cap));
166#endif
167static void nv_record __ARGS((cmdarg_T *cap));
168static void nv_at __ARGS((cmdarg_T *cap));
169static void nv_halfpage __ARGS((cmdarg_T *cap));
170static void nv_join __ARGS((cmdarg_T *cap));
171static void nv_put __ARGS((cmdarg_T *cap));
172static void nv_open __ARGS((cmdarg_T *cap));
173#ifdef FEAT_SNIFF
174static void nv_sniff __ARGS((cmdarg_T *cap));
175#endif
176#ifdef FEAT_NETBEANS_INTG
177static void nv_nbcmd __ARGS((cmdarg_T *cap));
178#endif
179#ifdef FEAT_DND
180static void nv_drop __ARGS((cmdarg_T *cap));
181#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000182#ifdef FEAT_AUTOCMD
183static void nv_cursorhold __ARGS((cmdarg_T *cap));
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000186static char *e_noident = N_("E349: No identifier under cursor");
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
191 */
192typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
193
194/* Values for cmd_flags. */
195#define NV_NCH 0x01 /* may need to get a second char */
196#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198#define NV_LANG 0x08 /* second char needs language adjustment */
199
200#define NV_SS 0x10 /* may start selection */
201#define NV_SSS 0x20 /* may start selection with shift modifier */
202#define NV_STS 0x40 /* may stop selection without shift modif. */
203#define NV_RL 0x80 /* 'rightleft' modifies command */
204#define NV_KEEPREG 0x100 /* don't clear regname */
205#define NV_NCW 0x200 /* not allowed in command-line window */
206
207/*
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
210 * oap->motion_type.
211 *
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
215 */
216
217/*
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
221 */
222static const struct nv_cmd
223{
224 int cmd_char; /* (first) command character */
225 nv_func_T cmd_func; /* function for this command */
226 short_u cmd_flags; /* NV_ flags */
227 short cmd_arg; /* value for ca.arg */
228} nv_cmds[] =
229{
230 {NUL, nv_error, 0, 0},
231 {Ctrl_A, nv_addsub, 0, 0},
232 {Ctrl_B, nv_page, NV_STS, BACKWARD},
233 {Ctrl_C, nv_esc, 0, TRUE},
234 {Ctrl_D, nv_halfpage, 0, 0},
235 {Ctrl_E, nv_scroll_line, 0, TRUE},
236 {Ctrl_F, nv_page, NV_STS, FORWARD},
237 {Ctrl_G, nv_ctrlg, 0, 0},
238 {Ctrl_H, nv_ctrlh, 0, 0},
239 {Ctrl_I, nv_pcmark, 0, 0},
240 {NL, nv_down, 0, FALSE},
241 {Ctrl_K, nv_error, 0, 0},
242 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000243 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_N, nv_down, NV_STS, FALSE},
245 {Ctrl_O, nv_ctrlo, 0, 0},
246 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000247#ifdef FEAT_VISUAL
248 {Ctrl_Q, nv_visual, 0, FALSE},
249#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 {Ctrl_R, nv_redo, 0, 0},
253 {Ctrl_S, nv_ignore, 0, 0},
254 {Ctrl_T, nv_tagpop, NV_NCW, 0},
255 {Ctrl_U, nv_halfpage, 0, 0},
256#ifdef FEAT_VISUAL
257 {Ctrl_V, nv_visual, 0, FALSE},
258 {'V', nv_visual, 0, FALSE},
259 {'v', nv_visual, 0, FALSE},
260#else
261 {Ctrl_V, nv_error, 0, 0},
262 {'V', nv_error, 0, 0},
263 {'v', nv_error, 0, 0},
264#endif
265 {Ctrl_W, nv_window, 0, 0},
266 {Ctrl_X, nv_addsub, 0, 0},
267 {Ctrl_Y, nv_scroll_line, 0, FALSE},
268 {Ctrl_Z, nv_suspend, 0, 0},
269 {ESC, nv_esc, 0, FALSE},
270 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
271 {Ctrl_RSB, nv_ident, NV_NCW, 0},
272 {Ctrl_HAT, nv_hat, NV_NCW, 0},
273 {Ctrl__, nv_error, 0, 0},
274 {' ', nv_right, 0, 0},
275 {'!', nv_operator, 0, 0},
276 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
277 {'#', nv_ident, 0, 0},
278 {'$', nv_dollar, 0, 0},
279 {'%', nv_percent, 0, 0},
280 {'&', nv_optrans, 0, 0},
281 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
282 {'(', nv_brace, 0, BACKWARD},
283 {')', nv_brace, 0, FORWARD},
284 {'*', nv_ident, 0, 0},
285 {'+', nv_down, 0, TRUE},
286 {',', nv_csearch, 0, TRUE},
287 {'-', nv_up, 0, TRUE},
288 {'.', nv_dot, NV_KEEPREG, 0},
289 {'/', nv_search, 0, FALSE},
290 {'0', nv_beginline, 0, 0},
291 {'1', nv_ignore, 0, 0},
292 {'2', nv_ignore, 0, 0},
293 {'3', nv_ignore, 0, 0},
294 {'4', nv_ignore, 0, 0},
295 {'5', nv_ignore, 0, 0},
296 {'6', nv_ignore, 0, 0},
297 {'7', nv_ignore, 0, 0},
298 {'8', nv_ignore, 0, 0},
299 {'9', nv_ignore, 0, 0},
300 {':', nv_colon, 0, 0},
301 {';', nv_csearch, 0, FALSE},
302 {'<', nv_operator, NV_RL, 0},
303 {'=', nv_operator, 0, 0},
304 {'>', nv_operator, NV_RL, 0},
305 {'?', nv_search, 0, FALSE},
306 {'@', nv_at, NV_NCH_NOP, FALSE},
307 {'A', nv_edit, 0, 0},
308 {'B', nv_bck_word, 0, 1},
309 {'C', nv_abbrev, NV_KEEPREG, 0},
310 {'D', nv_abbrev, NV_KEEPREG, 0},
311 {'E', nv_wordcmd, 0, TRUE},
312 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
313 {'G', nv_goto, 0, TRUE},
314 {'H', nv_scroll, 0, 0},
315 {'I', nv_edit, 0, 0},
316 {'J', nv_join, 0, 0},
317 {'K', nv_ident, 0, 0},
318 {'L', nv_scroll, 0, 0},
319 {'M', nv_scroll, 0, 0},
320 {'N', nv_next, 0, SEARCH_REV},
321 {'O', nv_open, 0, 0},
322 {'P', nv_put, 0, 0},
323 {'Q', nv_exmode, NV_NCW, 0},
324 {'R', nv_Replace, 0, FALSE},
325 {'S', nv_subst, NV_KEEPREG, 0},
326 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
327 {'U', nv_Undo, 0, 0},
328 {'W', nv_wordcmd, 0, TRUE},
329 {'X', nv_abbrev, NV_KEEPREG, 0},
330 {'Y', nv_abbrev, NV_KEEPREG, 0},
331 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
332 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
333 {'\\', nv_error, 0, 0},
334 {']', nv_brackets, NV_NCH_ALW, FORWARD},
335 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
336 {'_', nv_lineop, 0, 0},
337 {'`', nv_gomark, NV_NCH_ALW, FALSE},
338 {'a', nv_edit, NV_NCH, 0},
339 {'b', nv_bck_word, 0, 0},
340 {'c', nv_operator, 0, 0},
341 {'d', nv_operator, 0, 0},
342 {'e', nv_wordcmd, 0, FALSE},
343 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
344 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
345 {'h', nv_left, NV_RL, 0},
346 {'i', nv_edit, NV_NCH, 0},
347 {'j', nv_down, 0, FALSE},
348 {'k', nv_up, 0, FALSE},
349 {'l', nv_right, NV_RL, 0},
350 {'m', nv_mark, NV_NCH_NOP, 0},
351 {'n', nv_next, 0, 0},
352 {'o', nv_open, 0, 0},
353 {'p', nv_put, 0, 0},
354 {'q', nv_record, NV_NCH, 0},
355 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
356 {'s', nv_subst, NV_KEEPREG, 0},
357 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
358 {'u', nv_undo, 0, 0},
359 {'w', nv_wordcmd, 0, FALSE},
360 {'x', nv_abbrev, NV_KEEPREG, 0},
361 {'y', nv_operator, 0, 0},
362 {'z', nv_zet, NV_NCH_ALW, 0},
363 {'{', nv_findpar, 0, BACKWARD},
364 {'|', nv_pipe, 0, 0},
365 {'}', nv_findpar, 0, FORWARD},
366 {'~', nv_tilde, 0, 0},
367
368 /* pound sign */
369 {POUND, nv_ident, 0, 0},
370#ifdef FEAT_MOUSE
371 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
372 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
373 {K_LEFTMOUSE, nv_mouse, 0, 0},
374 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
375 {K_LEFTDRAG, nv_mouse, 0, 0},
376 {K_LEFTRELEASE, nv_mouse, 0, 0},
377 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
378 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
379 {K_MIDDLEDRAG, nv_mouse, 0, 0},
380 {K_MIDDLERELEASE, nv_mouse, 0, 0},
381 {K_RIGHTMOUSE, nv_mouse, 0, 0},
382 {K_RIGHTDRAG, nv_mouse, 0, 0},
383 {K_RIGHTRELEASE, nv_mouse, 0, 0},
384 {K_X1MOUSE, nv_mouse, 0, 0},
385 {K_X1DRAG, nv_mouse, 0, 0},
386 {K_X1RELEASE, nv_mouse, 0, 0},
387 {K_X2MOUSE, nv_mouse, 0, 0},
388 {K_X2DRAG, nv_mouse, 0, 0},
389 {K_X2RELEASE, nv_mouse, 0, 0},
390#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000391 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000392 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 {K_INS, nv_edit, 0, 0},
394 {K_KINS, nv_edit, 0, 0},
395 {K_BS, nv_ctrlh, 0, 0},
396 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
397 {K_S_UP, nv_page, NV_SS, BACKWARD},
398 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
399 {K_S_DOWN, nv_page, NV_SS, FORWARD},
400 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
402 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
403 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
404 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
405 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
406 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
407 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
408 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
409 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
410 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
411 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {K_S_END, nv_end, NV_SS, FALSE},
413 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
414 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
415 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {K_S_HOME, nv_home, NV_SS, 0},
417 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
418 {K_DEL, nv_abbrev, 0, 0},
419 {K_KDEL, nv_abbrev, 0, 0},
420 {K_UNDO, nv_kundo, 0, 0},
421 {K_HELP, nv_help, NV_NCW, 0},
422 {K_F1, nv_help, NV_NCW, 0},
423 {K_XF1, nv_help, NV_NCW, 0},
424#ifdef FEAT_VISUAL
425 {K_SELECT, nv_select, 0, 0},
426#endif
427#ifdef FEAT_GUI
428 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
429 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
430#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000431#ifdef FEAT_GUI_TABLINE
432 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000433 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#ifdef FEAT_FKMAP
436 {K_F8, farsi_fkey, 0, 0},
437 {K_F9, farsi_fkey, 0, 0},
438#endif
439#ifdef FEAT_SNIFF
440 {K_SNIFF, nv_sniff, 0, 0},
441#endif
442#ifdef FEAT_NETBEANS_INTG
443 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
444#endif
445#ifdef FEAT_DND
446 {K_DROP, nv_drop, NV_STS, 0},
447#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000448#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000449 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451};
452
453/* Number of commands in nv_cmds[]. */
454#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
455
456/* Sorted index of commands in nv_cmds[]. */
457static short nv_cmd_idx[NV_CMDS_SIZE];
458
459/* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461static int nv_max_linear;
462
463/*
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
466 */
467 static int
468#ifdef __BORLANDC__
469_RTLENTRYF
470#endif
471nv_compare(s1, s2)
472 const void *s1;
473 const void *s2;
474{
475 int c1, c2;
476
477 /* The commands are sorted on absolute value. */
478 c1 = nv_cmds[*(const short *)s1].cmd_char;
479 c2 = nv_cmds[*(const short *)s2].cmd_char;
480 if (c1 < 0)
481 c1 = -c1;
482 if (c2 < 0)
483 c2 = -c2;
484 return c1 - c2;
485}
486
487/*
488 * Initialize the nv_cmd_idx[] table.
489 */
490 void
491init_normal_cmds()
492{
493 int i;
494
495 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000496 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 nv_cmd_idx[i] = i;
498
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
501
502 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000503 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
505 break;
506 nv_max_linear = i - 1;
507}
508
509/*
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
512 */
513 static int
514find_command(cmdchar)
515 int cmdchar;
516{
517 int i;
518 int idx;
519 int top, bot;
520 int c;
521
522#ifdef FEAT_MBYTE
523 /* A multi-byte character is never a command. */
524 if (cmdchar >= 0x100)
525 return -1;
526#endif
527
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
530 if (cmdchar < 0)
531 cmdchar = -cmdchar;
532
533 /* If the character is in the first part: The character is the index into
534 * nv_cmd_idx[]. */
535 if (cmdchar <= nv_max_linear)
536 return nv_cmd_idx[cmdchar];
537
538 /* Perform a binary search. */
539 bot = nv_max_linear + 1;
540 top = NV_CMDS_SIZE - 1;
541 idx = -1;
542 while (bot <= top)
543 {
544 i = (top + bot) / 2;
545 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
546 if (c < 0)
547 c = -c;
548 if (cmdchar == c)
549 {
550 idx = nv_cmd_idx[i];
551 break;
552 }
553 if (cmdchar > c)
554 bot = i + 1;
555 else
556 top = i - 1;
557 }
558 return idx;
559}
560
561/*
562 * Execute a command in Normal mode.
563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 void
565normal_cmd(oap, toplevel)
566 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000567 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 cmdarg_T ca; /* command arguments */
570 int c;
571 int ctrl_w = FALSE; /* got CTRL-W command */
572 int old_col = curwin->w_curswant;
573#ifdef FEAT_CMDL_INFO
574 int need_flushbuf; /* need to call out_flush() */
575#endif
576#ifdef FEAT_VISUAL
577 pos_T old_pos; /* cursor position before command */
578 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 static int old_mapped_len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000582#ifdef FEAT_EVAL
583 int set_prevcount = FALSE;
584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
587 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000588
589 /* Use a count remembered from before entering an operator. After typing
590 * "3d" we return from normal_cmd() and come back here, the "3" is
591 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 ca.opcount = opcount;
593
594#ifdef FEAT_SNIFF
595 want_sniff_request = sniff_connected;
596#endif
597
598 /*
599 * If there is an operator pending, then the command we take this time
600 * will terminate it. Finish_op tells us to finish the operation before
601 * returning this time (unless the operation was cancelled).
602 */
603#ifdef CURSOR_SHAPE
604 c = finish_op;
605#endif
606 finish_op = (oap->op_type != OP_NOP);
607#ifdef CURSOR_SHAPE
608 if (finish_op != c)
609 {
610 ui_cursor_shape(); /* may show different cursor shape */
611# ifdef FEAT_MOUSESHAPE
612 update_mouseshape(-1);
613# endif
614 }
615#endif
616
Bram Moolenaara983fe92008-07-31 20:04:27 +0000617 /* When not finishing an operator and no register name typed, reset the
618 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000622#ifdef FEAT_EVAL
623 set_prevcount = TRUE;
624#endif
625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaara983fe92008-07-31 20:04:27 +0000627#ifdef FEAT_AUTOCMD
628 /* Restore counts from before receiving K_CURSORHOLD. This means after
629 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
630 * "3 * 2". */
631 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
632 {
633 ca.opcount = oap->prev_opcount;
634 ca.count0 = oap->prev_count0;
635 oap->prev_opcount = 0;
636 oap->prev_count0 = 0;
637 }
638#endif
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#ifdef FEAT_VISUAL
641 mapped_len = typebuf_maplen();
642#endif
643
644 State = NORMAL_BUSY;
645#ifdef USE_ON_FLY_SCROLL
646 dont_scroll = FALSE; /* allow scrolling here */
647#endif
648
649 /*
650 * Get the command character from the user.
651 */
652 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000655#ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 /*
657 * If a mapping was started in Visual or Select mode, remember the length
658 * of the mapping. This is used below to not return to Insert mode for as
659 * long as the mapping is being executed.
660 */
661 if (restart_edit == 0)
662 old_mapped_len = 0;
663 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000664 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668 if (c == NUL)
669 c = K_ZERO;
670
671#ifdef FEAT_VISUAL
672 /*
673 * In Select mode, typed text replaces the selection.
674 */
675 if (VIsual_active
676 && VIsual_select
677 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
678 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000679 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
680 * 'insertmode' is set) fake a "d"elete command, Insert mode will
681 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000682 * Insert the typed character in the typeahead buffer, so that it can
683 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000684 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000685 if (restart_edit != 0)
686 c = 'd';
687 else
688 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000689 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 }
691#endif
692
693#ifdef FEAT_CMDL_INFO
694 need_flushbuf = add_to_showcmd(c);
695#endif
696
697getcount:
698#ifdef FEAT_VISUAL
699 if (!(VIsual_active && VIsual_select))
700#endif
701 {
702 /*
703 * Handle a count before a command and compute ca.count0.
704 * Note that '0' is a command and not the start of a count, but it's
705 * part of a count after other digits.
706 */
707 while ( (c >= '1' && c <= '9')
708 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
709 {
710 if (c == K_DEL || c == K_KDEL)
711 {
712 ca.count0 /= 10;
713#ifdef FEAT_CMDL_INFO
714 del_from_showcmd(4); /* delete the digit and ~@% */
715#endif
716 }
717 else
718 ca.count0 = ca.count0 * 10 + (c - '0');
719 if (ca.count0 < 0) /* got too large! */
720 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000721#ifdef FEAT_EVAL
722 /* Set v:count here, when called from main() and not a stuffed
723 * command, so that v:count can be used in an expression mapping
724 * right after the count. */
725 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000726 {
727 long count = ca.count0;
728
729 /* multiply with ca.opcount the same way as below */
730 if (ca.opcount != 0)
731 count = ca.opcount * (count == 0 ? 1 : count);
732 set_vcount(count, count == 0 ? 1 : count, set_prevcount);
733 set_prevcount = FALSE; /* only set v:prevcount once */
734 }
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 if (ctrl_w)
737 {
738 ++no_mapping;
739 ++allow_keys; /* no mapping for nchar, but keys */
740 }
741 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000742 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 --no_zero_mapping;
745 if (ctrl_w)
746 {
747 --no_mapping;
748 --allow_keys;
749 }
750#ifdef FEAT_CMDL_INFO
751 need_flushbuf |= add_to_showcmd(c);
752#endif
753 }
754
755 /*
756 * If we got CTRL-W there may be a/another count
757 */
758 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
759 {
760 ctrl_w = TRUE;
761 ca.opcount = ca.count0; /* remember first count */
762 ca.count0 = 0;
763 ++no_mapping;
764 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000765 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 --no_mapping;
768 --allow_keys;
769#ifdef FEAT_CMDL_INFO
770 need_flushbuf |= add_to_showcmd(c);
771#endif
772 goto getcount; /* jump back */
773 }
774 }
775
Bram Moolenaara983fe92008-07-31 20:04:27 +0000776#ifdef FEAT_AUTOCMD
777 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000779 /* Save the count values so that ca.opcount and ca.count0 are exactly
780 * the same when coming back here after handling K_CURSORHOLD. */
781 oap->prev_opcount = ca.opcount;
782 oap->prev_count0 = ca.count0;
783 }
784 else
785#endif
786 if (ca.opcount != 0)
787 {
788 /*
789 * If we're in the middle of an operator (including after entering a
790 * yank buffer with '"') AND we had a count before the operator, then
791 * that count overrides the current value of ca.count0.
792 * What this means effectively, is that commands like "3dw" get turned
793 * into "d3w" which makes things fall into place pretty neatly.
794 * If you give a count before AND after the operator, they are
795 * multiplied.
796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 if (ca.count0)
798 ca.count0 *= ca.opcount;
799 else
800 ca.count0 = ca.opcount;
801 }
802
803 /*
804 * Always remember the count. It will be set to zero (on the next call,
805 * above) when there is no pending operator.
806 * When called from main(), save the count for use by the "count" built-in
807 * variable.
808 */
809 ca.opcount = ca.count0;
810 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
811
812#ifdef FEAT_EVAL
813 /*
814 * Only set v:count when called from main() and not a stuffed command.
815 */
816 if (toplevel && stuff_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000817 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819
820 /*
821 * Find the command character in the table of commands.
822 * For CTRL-W we already got nchar when looking for a count.
823 */
824 if (ctrl_w)
825 {
826 ca.nchar = c;
827 ca.cmdchar = Ctrl_W;
828 }
829 else
830 ca.cmdchar = c;
831 idx = find_command(ca.cmdchar);
832 if (idx < 0)
833 {
834 /* Not a known command: beep. */
835 clearopbeep(oap);
836 goto normal_end;
837 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000838
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000839 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000841 /* This command is not allowed while editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000843 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 goto normal_end;
845 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000846#ifdef FEAT_AUTOCMD
847 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
848 goto normal_end;
849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851#ifdef FEAT_VISUAL
852 /*
853 * In Visual/Select mode, a few keys are handled in a special way.
854 */
855 if (VIsual_active)
856 {
857 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
858 if (km_stopsel
859 && (nv_cmds[idx].cmd_flags & NV_STS)
860 && !(mod_mask & MOD_MASK_SHIFT))
861 {
862 end_visual_mode();
863 redraw_curbuf_later(INVERTED);
864 }
865
866 /* Keys that work different when 'keymodel' contains "startsel" */
867 if (km_startsel)
868 {
869 if (nv_cmds[idx].cmd_flags & NV_SS)
870 {
871 unshift_special(&ca);
872 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000873 if (idx < 0)
874 {
875 /* Just in case */
876 clearopbeep(oap);
877 goto normal_end;
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 }
880 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
881 && (mod_mask & MOD_MASK_SHIFT))
882 {
883 mod_mask &= ~MOD_MASK_SHIFT;
884 }
885 }
886 }
887#endif
888
889#ifdef FEAT_RIGHTLEFT
890 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
891 && (nv_cmds[idx].cmd_flags & NV_RL))
892 {
893 /* Invert horizontal movements and operations. Only when typed by the
894 * user directly, not when the result of a mapping or "x" translated
895 * to "dl". */
896 switch (ca.cmdchar)
897 {
898 case 'l': ca.cmdchar = 'h'; break;
899 case K_RIGHT: ca.cmdchar = K_LEFT; break;
900 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
901 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
902 case 'h': ca.cmdchar = 'l'; break;
903 case K_LEFT: ca.cmdchar = K_RIGHT; break;
904 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
905 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
906 case '>': ca.cmdchar = '<'; break;
907 case '<': ca.cmdchar = '>'; break;
908 }
909 idx = find_command(ca.cmdchar);
910 }
911#endif
912
913 /*
914 * Get an additional character if we need one.
915 */
916 if ((nv_cmds[idx].cmd_flags & NV_NCH)
917 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
918 && oap->op_type == OP_NOP)
919 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
920 || (ca.cmdchar == 'q'
921 && oap->op_type == OP_NOP
922 && !Recording
923 && !Exec_reg)
924 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
925 && (oap->op_type != OP_NOP
926#ifdef FEAT_VISUAL
927 || VIsual_active
928#endif
929 ))))
930 {
931 int *cp;
932 int repl = FALSE; /* get character for replace mode */
933 int lit = FALSE; /* get extra character literally */
934 int langmap_active = FALSE; /* using :lmap mappings */
935 int lang; /* getting a text character */
936#ifdef USE_IM_CONTROL
937 int save_smd; /* saved value of p_smd */
938#endif
939
940 ++no_mapping;
941 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000942#ifdef FEAT_AUTOCMD
943 /* Don't generate a CursorHold event here, most commands can't handle
944 * it, e.g., nv_replace(), nv_csearch(). */
945 did_cursorhold = TRUE;
946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (ca.cmdchar == 'g')
948 {
949 /*
950 * For 'g' get the next character now, so that we can check for
951 * "gr", "g'" and "g`".
952 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000953 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#ifdef FEAT_CMDL_INFO
956 need_flushbuf |= add_to_showcmd(ca.nchar);
957#endif
958 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
959 || ca.nchar == Ctrl_BSL)
960 {
961 cp = &ca.extra_char; /* need to get a third character */
962 if (ca.nchar != 'r')
963 lit = TRUE; /* get it literally */
964 else
965 repl = TRUE; /* get it in replace mode */
966 }
967 else
968 cp = NULL; /* no third character needed */
969 }
970 else
971 {
972 if (ca.cmdchar == 'r') /* get it in replace mode */
973 repl = TRUE;
974 cp = &ca.nchar;
975 }
976 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
977
978 /*
979 * Get a second or third character.
980 */
981 if (cp != NULL)
982 {
983#ifdef CURSOR_SHAPE
984 if (repl)
985 {
986 State = REPLACE; /* pretend Replace mode */
987 ui_cursor_shape(); /* show different cursor shape */
988 }
989#endif
990 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
991 {
992 /* Allow mappings defined with ":lmap". */
993 --no_mapping;
994 --allow_keys;
995 if (repl)
996 State = LREPLACE;
997 else
998 State = LANGMAP;
999 langmap_active = TRUE;
1000 }
1001#ifdef USE_IM_CONTROL
1002 save_smd = p_smd;
1003 p_smd = FALSE; /* Don't let the IM code show the mode here */
1004 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
1005 im_set_active(TRUE);
1006#endif
1007
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001008 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010 if (langmap_active)
1011 {
1012 /* Undo the decrement done above */
1013 ++no_mapping;
1014 ++allow_keys;
1015 State = NORMAL_BUSY;
1016 }
1017#ifdef USE_IM_CONTROL
1018 if (lang)
1019 {
1020 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
1021 im_save_status(&curbuf->b_p_iminsert);
1022 im_set_active(FALSE);
1023 }
1024 p_smd = save_smd;
1025#endif
1026#ifdef CURSOR_SHAPE
1027 State = NORMAL_BUSY;
1028#endif
1029#ifdef FEAT_CMDL_INFO
1030 need_flushbuf |= add_to_showcmd(*cp);
1031#endif
1032
1033 if (!lit)
1034 {
1035#ifdef FEAT_DIGRAPHS
1036 /* Typing CTRL-K gets a digraph. */
1037 if (*cp == Ctrl_K
1038 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1039 || cp == &ca.extra_char)
1040 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1041 {
1042 c = get_digraph(FALSE);
1043 if (c > 0)
1044 {
1045 *cp = c;
1046# ifdef FEAT_CMDL_INFO
1047 /* Guessing how to update showcmd here... */
1048 del_from_showcmd(3);
1049 need_flushbuf |= add_to_showcmd(*cp);
1050# endif
1051 }
1052 }
1053#endif
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 /* adjust chars > 127, except after "tTfFr" commands */
1056 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#ifdef FEAT_RIGHTLEFT
1058 /* adjust Hebrew mapped char */
1059 if (p_hkmap && lang && KeyTyped)
1060 *cp = hkmap(*cp);
1061# ifdef FEAT_FKMAP
1062 /* adjust Farsi mapped char */
1063 if (p_fkmap && lang && KeyTyped)
1064 *cp = fkmap(*cp);
1065# endif
1066#endif
1067 }
1068
1069 /*
1070 * When the next character is CTRL-\ a following CTRL-N means the
1071 * command is aborted and we go to Normal mode.
1072 */
1073 if (cp == &ca.extra_char
1074 && ca.nchar == Ctrl_BSL
1075 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1076 {
1077 ca.cmdchar = Ctrl_BSL;
1078 ca.nchar = ca.extra_char;
1079 idx = find_command(ca.cmdchar);
1080 }
1081 else if (*cp == Ctrl_BSL)
1082 {
1083 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1084
1085 /* There is a busy wait here when typing "f<C-\>" and then
1086 * something different from CTRL-N. Can't be avoided. */
1087 while ((c = vpeekc()) <= 0 && towait > 0L)
1088 {
1089 do_sleep(towait > 50L ? 50L : towait);
1090 towait -= 50L;
1091 }
1092 if (c > 0)
1093 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001094 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (c != Ctrl_N && c != Ctrl_G)
1096 vungetc(c);
1097 else
1098 {
1099 ca.cmdchar = Ctrl_BSL;
1100 ca.nchar = c;
1101 idx = find_command(ca.cmdchar);
1102 }
1103 }
1104 }
1105
1106#ifdef FEAT_MBYTE
1107 /* When getting a text character and the next character is a
1108 * multi-byte character, it could be a composing character.
1109 * However, don't wait for it to arrive. */
1110 while (enc_utf8 && lang && (c = vpeekc()) > 0
1111 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1112 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001113 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (!utf_iscomposing(c))
1115 {
1116 vungetc(c); /* it wasn't, put it back */
1117 break;
1118 }
1119 else if (ca.ncharC1 == 0)
1120 ca.ncharC1 = c;
1121 else
1122 ca.ncharC2 = c;
1123 }
1124#endif
1125 }
1126 --no_mapping;
1127 --allow_keys;
1128 }
1129
1130#ifdef FEAT_CMDL_INFO
1131 /*
1132 * Flush the showcmd characters onto the screen so we can see them while
1133 * the command is being executed. Only do this when the shown command was
1134 * actually displayed, otherwise this will slow down a lot when executing
1135 * mappings.
1136 */
1137 if (need_flushbuf)
1138 out_flush();
1139#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001140#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001141 if (ca.cmdchar != K_IGNORE)
1142 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 State = NORMAL;
1146
1147 if (ca.nchar == ESC)
1148 {
1149 clearop(oap);
1150 if (restart_edit == 0 && goto_im())
1151 restart_edit = 'a';
1152 goto normal_end;
1153 }
1154
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001155 if (ca.cmdchar != K_IGNORE)
1156 {
1157 msg_didout = FALSE; /* don't scroll screen up for normal command */
1158 msg_col = 0;
1159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161#ifdef FEAT_VISUAL
1162 old_pos = curwin->w_cursor; /* remember where cursor was */
1163
1164 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1165 * mode. */
1166 if (!VIsual_active && km_startsel)
1167 {
1168 if (nv_cmds[idx].cmd_flags & NV_SS)
1169 {
1170 start_selection();
1171 unshift_special(&ca);
1172 idx = find_command(ca.cmdchar);
1173 }
1174 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1175 && (mod_mask & MOD_MASK_SHIFT))
1176 {
1177 start_selection();
1178 mod_mask &= ~MOD_MASK_SHIFT;
1179 }
1180 }
1181#endif
1182
1183 /*
1184 * Execute the command!
1185 * Call the command function found in the commands table.
1186 */
1187 ca.arg = nv_cmds[idx].cmd_arg;
1188 (nv_cmds[idx].cmd_func)(&ca);
1189
1190 /*
1191 * If we didn't start or finish an operator, reset oap->regname, unless we
1192 * need it later.
1193 */
1194 if (!finish_op
1195 && !oap->op_type
1196 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1197 {
1198 clearop(oap);
1199#ifdef FEAT_EVAL
1200 set_reg_var('"');
1201#endif
1202 }
1203
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001204#ifdef FEAT_VISUAL
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001205 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001206 * character or "z333<cr>". */
1207 if (old_mapped_len > 0)
1208 old_mapped_len = typebuf_maplen();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001209#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 /*
1212 * If an operation is pending, handle it...
1213 */
1214 do_pending_operator(&ca, old_col, FALSE);
1215
1216 /*
1217 * Wait for a moment when a message is displayed that will be overwritten
1218 * by the mode message.
1219 * In Visual mode and with "^O" in Insert mode, a short message will be
1220 * overwritten by the mode message. Wait a bit, until a key is hit.
1221 * In Visual mode, it's more important to keep the Visual area updated
1222 * than keeping a message (e.g. from a /pat search).
1223 * Only do this if the command was typed, not from a mapping.
1224 * Don't wait when emsg_silent is non-zero.
1225 * Also wait a bit after an error message, e.g. for "^O:".
1226 * Don't redraw the screen, it would remove the message.
1227 */
1228 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001229 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && (restart_edit != 0
1231#ifdef FEAT_VISUAL
1232 || (VIsual_active
1233 && old_pos.lnum == curwin->w_cursor.lnum
1234 && old_pos.col == curwin->w_cursor.col)
1235#endif
1236 )
1237 && (clear_cmdline
1238 || redraw_cmdline)
1239 && (msg_didout || (msg_didany && msg_scroll))
1240 && !msg_nowait
1241 && KeyTyped)
1242 || (restart_edit != 0
1243#ifdef FEAT_VISUAL
1244 && !VIsual_active
1245#endif
1246 && (msg_scroll
1247 || emsg_on_display)))
1248 && oap->regname == 0
1249 && !(ca.retval & CA_COMMAND_BUSY)
1250 && stuff_empty()
1251 && typebuf_typed()
1252 && emsg_silent == 0
1253 && !did_wait_return
1254 && oap->op_type == OP_NOP)
1255 {
1256 int save_State = State;
1257
1258 /* Draw the cursor with the right shape here */
1259 if (restart_edit != 0)
1260 State = INSERT;
1261
1262 /* If need to redraw, and there is a "keep_msg", redraw before the
1263 * delay */
1264 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1265 {
1266 char_u *kmsg;
1267
1268 kmsg = keep_msg;
1269 keep_msg = NULL;
1270 /* showmode() will clear keep_msg, but we want to use it anyway */
1271 update_screen(0);
1272 /* now reset it, otherwise it's put in the history again */
1273 keep_msg = kmsg;
1274 msg_attr(kmsg, keep_msg_attr);
1275 vim_free(kmsg);
1276 }
1277 setcursor();
1278 cursor_on();
1279 out_flush();
1280 if (msg_scroll || emsg_on_display)
1281 ui_delay(1000L, TRUE); /* wait at least one second */
1282 ui_delay(3000L, FALSE); /* wait up to three seconds */
1283 State = save_State;
1284
1285 msg_scroll = FALSE;
1286 emsg_on_display = FALSE;
1287 }
1288
1289 /*
1290 * Finish up after executing a Normal mode command.
1291 */
1292normal_end:
1293
1294 msg_nowait = FALSE;
1295
1296 /* Reset finish_op, in case it was set */
1297#ifdef CURSOR_SHAPE
1298 c = finish_op;
1299#endif
1300 finish_op = FALSE;
1301#ifdef CURSOR_SHAPE
1302 /* Redraw the cursor with another shape, if we were in Operator-pending
1303 * mode or did a replace command. */
1304 if (c || ca.cmdchar == 'r')
1305 {
1306 ui_cursor_shape(); /* may show different cursor shape */
1307# ifdef FEAT_MOUSESHAPE
1308 update_mouseshape(-1);
1309# endif
1310 }
1311#endif
1312
1313#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001314 if (oap->op_type == OP_NOP && oap->regname == 0
1315# ifdef FEAT_AUTOCMD
1316 && ca.cmdchar != K_CURSORHOLD
1317# endif
1318 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 clear_showcmd();
1320#endif
1321
1322 checkpcmark(); /* check if we moved since setting pcmark */
1323 vim_free(ca.searchbuf);
1324
1325#ifdef FEAT_MBYTE
1326 if (has_mbyte)
1327 mb_adjust_cursor();
1328#endif
1329
1330#ifdef FEAT_SCROLLBIND
1331 if (curwin->w_p_scb && toplevel)
1332 {
1333 validate_cursor(); /* may need to update w_leftcol */
1334 do_check_scrollbind(TRUE);
1335 }
1336#endif
1337
Bram Moolenaar860cae12010-06-05 23:22:07 +02001338#ifdef FEAT_CURSORBIND
1339 if (curwin->w_p_crb && toplevel)
1340 {
1341 validate_cursor(); /* may need to update w_leftcol */
1342 do_check_cursorbind();
1343 }
1344#endif
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /*
1347 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1348 * if still inside a mapping that started in Visual mode).
1349 * May switch from Visual to Select mode after CTRL-O command.
1350 */
1351 if ( oap->op_type == OP_NOP
1352#ifdef FEAT_VISUAL
1353 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1354 || restart_VIsual_select == 1)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001355#else
1356 && restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#endif
1358 && !(ca.retval & CA_COMMAND_BUSY)
1359 && stuff_empty()
1360 && oap->regname == 0)
1361 {
1362#ifdef FEAT_VISUAL
1363 if (restart_VIsual_select == 1)
1364 {
1365 VIsual_select = TRUE;
1366 showmode();
1367 restart_VIsual_select = 0;
1368 }
1369#endif
1370 if (restart_edit != 0
1371#ifdef FEAT_VISUAL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001372 && !VIsual_active && old_mapped_len == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001374 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 (void)edit(restart_edit, FALSE, 1L);
1376 }
1377
1378#ifdef FEAT_VISUAL
1379 if (restart_VIsual_select == 2)
1380 restart_VIsual_select = 1;
1381#endif
1382
1383 /* Save count before an operator for next time. */
1384 opcount = ca.opcount;
1385}
1386
1387/*
1388 * Handle an operator after visual mode or when the movement is finished
1389 */
1390 void
1391do_pending_operator(cap, old_col, gui_yank)
1392 cmdarg_T *cap;
1393 int old_col;
1394 int gui_yank;
1395{
1396 oparg_T *oap = cap->oap;
1397 pos_T old_cursor;
1398 int empty_region_error;
1399 int restart_edit_save;
1400
1401#ifdef FEAT_VISUAL
1402 /* The visual area is remembered for redo */
1403 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1404 static linenr_T redo_VIsual_line_count; /* number of lines */
1405 static colnr_T redo_VIsual_col; /* number of cols or end column */
1406 static long redo_VIsual_count; /* count for Visual operator */
1407# ifdef FEAT_VIRTUALEDIT
1408 int include_line_break = FALSE;
1409# endif
1410#endif
1411
1412#if defined(FEAT_CLIPBOARD)
1413 /*
1414 * Yank the visual area into the GUI selection register before we operate
1415 * on it and lose it forever.
1416 * Don't do it if a specific register was specified, so that ""x"*P works.
1417 * This could call do_pending_operator() recursively, but that's OK
1418 * because gui_yank will be TRUE for the nested call.
1419 */
1420 if (clip_star.available
1421 && oap->op_type != OP_NOP
1422 && !gui_yank
1423# ifdef FEAT_VISUAL
1424 && VIsual_active
1425 && !redo_VIsual_busy
1426# endif
1427 && oap->regname == 0)
1428 clip_auto_select();
1429#endif
1430 old_cursor = curwin->w_cursor;
1431
1432 /*
1433 * If an operation is pending, handle it...
1434 */
1435 if ((finish_op
1436#ifdef FEAT_VISUAL
1437 || VIsual_active
1438#endif
1439 ) && oap->op_type != OP_NOP)
1440 {
1441#ifdef FEAT_VISUAL
1442 oap->is_VIsual = VIsual_active;
1443 if (oap->motion_force == 'V')
1444 oap->motion_type = MLINE;
1445 else if (oap->motion_force == 'v')
1446 {
1447 /* If the motion was linewise, "inclusive" will not have been set.
1448 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1449 if (oap->motion_type == MLINE)
1450 oap->inclusive = FALSE;
1451 /* If the motion already was characterwise, toggle "inclusive" */
1452 else if (oap->motion_type == MCHAR)
1453 oap->inclusive = !oap->inclusive;
1454 oap->motion_type = MCHAR;
1455 }
1456 else if (oap->motion_force == Ctrl_V)
1457 {
1458 /* Change line- or characterwise motion into Visual block mode. */
1459 VIsual_active = TRUE;
1460 VIsual = oap->start;
1461 VIsual_mode = Ctrl_V;
1462 VIsual_select = FALSE;
1463 VIsual_reselect = FALSE;
1464 }
1465#endif
1466
1467 /* only redo yank when 'y' flag is in 'cpoptions' */
1468 /* never redo "zf" (define fold) */
1469 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1470#ifdef FEAT_VISUAL
1471 && (!VIsual_active || oap->motion_force)
1472#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001473 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474#ifdef FEAT_FOLDING
1475 && oap->op_type != OP_FOLD
1476 && oap->op_type != OP_FOLDOPEN
1477 && oap->op_type != OP_FOLDOPENREC
1478 && oap->op_type != OP_FOLDCLOSE
1479 && oap->op_type != OP_FOLDCLOSEREC
1480 && oap->op_type != OP_FOLDDEL
1481 && oap->op_type != OP_FOLDDELREC
1482#endif
1483 )
1484 {
1485 prep_redo(oap->regname, cap->count0,
1486 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1487 oap->motion_force, cap->cmdchar, cap->nchar);
1488 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1489 {
1490 /*
1491 * If 'cpoptions' does not contain 'r', insert the search
1492 * pattern to really repeat the same command.
1493 */
1494 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001495 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 AppendToRedobuff(NL_STR);
1497 }
1498 else if (cap->cmdchar == ':')
1499 {
1500 /* do_cmdline() has stored the first typed line in
1501 * "repeat_cmdline". When several lines are typed repeating
1502 * won't be possible. */
1503 if (repeat_cmdline == NULL)
1504 ResetRedobuff();
1505 else
1506 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001507 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 AppendToRedobuff(NL_STR);
1509 vim_free(repeat_cmdline);
1510 repeat_cmdline = NULL;
1511 }
1512 }
1513 }
1514
1515#ifdef FEAT_VISUAL
1516 if (redo_VIsual_busy)
1517 {
1518 oap->start = curwin->w_cursor;
1519 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1520 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1521 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1522 VIsual_mode = redo_VIsual_mode;
1523 if (VIsual_mode == 'v')
1524 {
1525 if (redo_VIsual_line_count <= 1)
1526 curwin->w_cursor.col += redo_VIsual_col - 1;
1527 else
1528 curwin->w_cursor.col = redo_VIsual_col;
1529 }
1530 if (redo_VIsual_col == MAXCOL)
1531 {
1532 curwin->w_curswant = MAXCOL;
1533 coladvance((colnr_T)MAXCOL);
1534 }
1535 cap->count0 = redo_VIsual_count;
1536 if (redo_VIsual_count != 0)
1537 cap->count1 = redo_VIsual_count;
1538 else
1539 cap->count1 = 1;
1540 }
1541 else if (VIsual_active)
1542 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001543 if (!gui_yank)
1544 {
1545 /* Save the current VIsual area for '< and '> marks, and "gv" */
1546 curbuf->b_visual.vi_start = VIsual;
1547 curbuf->b_visual.vi_end = curwin->w_cursor;
1548 curbuf->b_visual.vi_mode = VIsual_mode;
1549 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001551 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
1555 /* In Select mode, a linewise selection is operated upon like a
1556 * characterwise selection. */
1557 if (VIsual_select && VIsual_mode == 'V')
1558 {
1559 if (lt(VIsual, curwin->w_cursor))
1560 {
1561 VIsual.col = 0;
1562 curwin->w_cursor.col =
1563 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1564 }
1565 else
1566 {
1567 curwin->w_cursor.col = 0;
1568 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1569 }
1570 VIsual_mode = 'v';
1571 }
1572 /* If 'selection' is "exclusive", backup one character for
1573 * charwise selections. */
1574 else if (VIsual_mode == 'v')
1575 {
1576# ifdef FEAT_VIRTUALEDIT
1577 include_line_break =
1578# endif
1579 unadjust_for_sel();
1580 }
1581
1582 oap->start = VIsual;
1583 if (VIsual_mode == 'V')
1584 oap->start.col = 0;
1585 }
1586#endif /* FEAT_VISUAL */
1587
1588 /*
1589 * Set oap->start to the first position of the operated text, oap->end
1590 * to the end of the operated text. w_cursor is equal to oap->start.
1591 */
1592 if (lt(oap->start, curwin->w_cursor))
1593 {
1594#ifdef FEAT_FOLDING
1595 /* Include folded lines completely. */
1596 if (!VIsual_active)
1597 {
1598 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1599 oap->start.col = 0;
1600 if (hasFolding(curwin->w_cursor.lnum, NULL,
1601 &curwin->w_cursor.lnum))
1602 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1603 }
1604#endif
1605 oap->end = curwin->w_cursor;
1606 curwin->w_cursor = oap->start;
1607
1608 /* w_virtcol may have been updated; if the cursor goes back to its
1609 * previous position w_virtcol becomes invalid and isn't updated
1610 * automatically. */
1611 curwin->w_valid &= ~VALID_VIRTCOL;
1612 }
1613 else
1614 {
1615#ifdef FEAT_FOLDING
1616 /* Include folded lines completely. */
1617 if (!VIsual_active && oap->motion_type == MLINE)
1618 {
1619 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1620 NULL))
1621 curwin->w_cursor.col = 0;
1622 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1623 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1624 }
1625#endif
1626 oap->end = oap->start;
1627 oap->start = curwin->w_cursor;
1628 }
1629
1630 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1631
1632#ifdef FEAT_VIRTUALEDIT
1633 /* Set "virtual_op" before resetting VIsual_active. */
1634 virtual_op = virtual_active();
1635#endif
1636
1637#ifdef FEAT_VISUAL
1638 if (VIsual_active || redo_VIsual_busy)
1639 {
1640 if (VIsual_mode == Ctrl_V) /* block mode */
1641 {
1642 colnr_T start, end;
1643
1644 oap->block_mode = TRUE;
1645
1646 getvvcol(curwin, &(oap->start),
1647 &oap->start_vcol, NULL, &oap->end_vcol);
1648 if (!redo_VIsual_busy)
1649 {
1650 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1651
1652 if (start < oap->start_vcol)
1653 oap->start_vcol = start;
1654 if (end > oap->end_vcol)
1655 {
1656 if (*p_sel == 'e' && start >= 1
1657 && start - 1 >= oap->end_vcol)
1658 oap->end_vcol = start - 1;
1659 else
1660 oap->end_vcol = end;
1661 }
1662 }
1663
1664 /* if '$' was used, get oap->end_vcol from longest line */
1665 if (curwin->w_curswant == MAXCOL)
1666 {
1667 curwin->w_cursor.col = MAXCOL;
1668 oap->end_vcol = 0;
1669 for (curwin->w_cursor.lnum = oap->start.lnum;
1670 curwin->w_cursor.lnum <= oap->end.lnum;
1671 ++curwin->w_cursor.lnum)
1672 {
1673 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1674 if (end > oap->end_vcol)
1675 oap->end_vcol = end;
1676 }
1677 }
1678 else if (redo_VIsual_busy)
1679 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1680 /*
1681 * Correct oap->end.col and oap->start.col to be the
1682 * upper-left and lower-right corner of the block area.
1683 *
1684 * (Actually, this does convert column positions into character
1685 * positions)
1686 */
1687 curwin->w_cursor.lnum = oap->end.lnum;
1688 coladvance(oap->end_vcol);
1689 oap->end = curwin->w_cursor;
1690
1691 curwin->w_cursor = oap->start;
1692 coladvance(oap->start_vcol);
1693 oap->start = curwin->w_cursor;
1694 }
1695
1696 if (!redo_VIsual_busy && !gui_yank)
1697 {
1698 /*
1699 * Prepare to reselect and redo Visual: this is based on the
1700 * size of the Visual text
1701 */
1702 resel_VIsual_mode = VIsual_mode;
1703 if (curwin->w_curswant == MAXCOL)
1704 resel_VIsual_col = MAXCOL;
1705 else if (VIsual_mode == Ctrl_V)
1706 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1707 else if (oap->line_count > 1)
1708 resel_VIsual_col = oap->end.col;
1709 else
1710 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1711 resel_VIsual_line_count = oap->line_count;
1712 }
1713
1714 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1715 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1716 && oap->op_type != OP_COLON
1717#ifdef FEAT_FOLDING
1718 && oap->op_type != OP_FOLD
1719 && oap->op_type != OP_FOLDOPEN
1720 && oap->op_type != OP_FOLDOPENREC
1721 && oap->op_type != OP_FOLDCLOSE
1722 && oap->op_type != OP_FOLDCLOSEREC
1723 && oap->op_type != OP_FOLDDEL
1724 && oap->op_type != OP_FOLDDELREC
1725#endif
1726 && oap->motion_force == NUL
1727 )
1728 {
1729 /* Prepare for redoing. Only use the nchar field for "r",
1730 * otherwise it might be the second char of the operator. */
1731 prep_redo(oap->regname, 0L, NUL, 'v',
1732 get_op_char(oap->op_type),
1733 get_extra_op_char(oap->op_type),
1734 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1735 if (!redo_VIsual_busy)
1736 {
1737 redo_VIsual_mode = resel_VIsual_mode;
1738 redo_VIsual_col = resel_VIsual_col;
1739 redo_VIsual_line_count = resel_VIsual_line_count;
1740 redo_VIsual_count = cap->count0;
1741 }
1742 }
1743
1744 /*
1745 * oap->inclusive defaults to TRUE.
1746 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1747 * FALSE. This makes "d}P" and "v}dP" work the same.
1748 */
1749 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1750 oap->inclusive = TRUE;
1751 if (VIsual_mode == 'V')
1752 oap->motion_type = MLINE;
1753 else
1754 {
1755 oap->motion_type = MCHAR;
1756 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1757# ifdef FEAT_VIRTUALEDIT
1758 && (include_line_break || !virtual_op)
1759# endif
1760 )
1761 {
1762 oap->inclusive = FALSE;
1763 /* Try to include the newline, unless it's an operator
1764 * that works on lines only */
1765 if (*p_sel != 'o'
1766 && !op_on_lines(oap->op_type)
1767 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1768 {
1769 ++oap->end.lnum;
1770 oap->end.col = 0;
1771# ifdef FEAT_VIRTUALEDIT
1772 oap->end.coladd = 0;
1773# endif
1774 ++oap->line_count;
1775 }
1776 }
1777 }
1778
1779 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /*
1782 * Switch Visual off now, so screen updating does
1783 * not show inverted text when the screen is redrawn.
1784 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1785 * no screen redraw, so it is done here to remove the inverted
1786 * part.
1787 */
1788 if (!gui_yank)
1789 {
1790 VIsual_active = FALSE;
1791# ifdef FEAT_MOUSE
1792 setmouse();
1793 mouse_dragging = 0;
1794# endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001795 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 clear_cmdline = TRUE; /* unshow visual mode later */
1797#ifdef FEAT_CMDL_INFO
1798 else
1799 clear_showcmd();
1800#endif
1801 if ((oap->op_type == OP_YANK
1802 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001803 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 || oap->op_type == OP_FILTER)
1805 && oap->motion_force == NUL)
1806 redraw_curbuf_later(INVERTED);
1807 }
1808 }
1809#endif
1810
1811#ifdef FEAT_MBYTE
1812 /* Include the trailing byte of a multi-byte char. */
1813 if (has_mbyte && oap->inclusive)
1814 {
1815 int l;
1816
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001817 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (l > 1)
1819 oap->end.col += l - 1;
1820 }
1821#endif
1822 curwin->w_set_curswant = TRUE;
1823
1824 /*
1825 * oap->empty is set when start and end are the same. The inclusive
1826 * flag affects this too, unless yanking and the end is on a NUL.
1827 */
1828 oap->empty = (oap->motion_type == MCHAR
1829 && (!oap->inclusive
1830 || (oap->op_type == OP_YANK
1831 && gchar_pos(&oap->end) == NUL))
1832 && equalpos(oap->start, oap->end)
1833#ifdef FEAT_VIRTUALEDIT
1834 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1835#endif
1836 );
1837 /*
1838 * For delete, change and yank, it's an error to operate on an
1839 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1840 */
1841 empty_region_error = (oap->empty
1842 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1843
1844#ifdef FEAT_VISUAL
1845 /* Force a redraw when operating on an empty Visual region, when
1846 * 'modifiable is off or creating a fold. */
1847 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1848# ifdef FEAT_FOLDING
1849 || oap->op_type == OP_FOLD
1850# endif
1851 ))
1852 redraw_curbuf_later(INVERTED);
1853#endif
1854
1855 /*
1856 * If the end of an operator is in column one while oap->motion_type
1857 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1858 * character in the previous line. If op_start is on or before the
1859 * first non-blank in the line, the operator becomes linewise
1860 * (strange, but that's the way vi does it).
1861 */
1862 if ( oap->motion_type == MCHAR
1863 && oap->inclusive == FALSE
1864 && !(cap->retval & CA_NO_ADJ_OP_END)
1865 && oap->end.col == 0
1866#ifdef FEAT_VISUAL
1867 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001868 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#endif
1870 && oap->line_count > 1)
1871 {
1872 oap->end_adjusted = TRUE; /* remember that we did this */
1873 --oap->line_count;
1874 --oap->end.lnum;
1875 if (inindent(0))
1876 oap->motion_type = MLINE;
1877 else
1878 {
1879 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1880 if (oap->end.col)
1881 {
1882 --oap->end.col;
1883 oap->inclusive = TRUE;
1884 }
1885 }
1886 }
1887 else
1888 oap->end_adjusted = FALSE;
1889
1890 switch (oap->op_type)
1891 {
1892 case OP_LSHIFT:
1893 case OP_RSHIFT:
1894 op_shift(oap, TRUE,
1895#ifdef FEAT_VISUAL
1896 oap->is_VIsual ? (int)cap->count1 :
1897#endif
1898 1);
1899 auto_format(FALSE, TRUE);
1900 break;
1901
1902 case OP_JOIN_NS:
1903 case OP_JOIN:
1904 if (oap->line_count < 2)
1905 oap->line_count = 2;
1906 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1907 curbuf->b_ml.ml_line_count)
1908 beep_flush();
1909 else
1910 {
1911 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1912 auto_format(FALSE, TRUE);
1913 }
1914 break;
1915
1916 case OP_DELETE:
1917#ifdef FEAT_VISUAL
1918 VIsual_reselect = FALSE; /* don't reselect now */
1919#endif
1920 if (empty_region_error)
1921 vim_beep();
1922 else
1923 {
1924 (void)op_delete(oap);
1925 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1926 u_save_cursor(); /* cursor line wasn't saved yet */
1927 auto_format(FALSE, TRUE);
1928 }
1929 break;
1930
1931 case OP_YANK:
1932 if (empty_region_error)
1933 {
1934 if (!gui_yank)
1935 vim_beep();
1936 }
1937 else
1938 (void)op_yank(oap, FALSE, !gui_yank);
1939 check_cursor_col();
1940 break;
1941
1942 case OP_CHANGE:
1943#ifdef FEAT_VISUAL
1944 VIsual_reselect = FALSE; /* don't reselect now */
1945#endif
1946 if (empty_region_error)
1947 vim_beep();
1948 else
1949 {
1950 /* This is a new edit command, not a restart. Need to
1951 * remember it to make 'insertmode' work with mappings for
1952 * Visual mode. But do this only once and not when typed and
1953 * 'insertmode' isn't set. */
1954 if (p_im || !KeyTyped)
1955 restart_edit_save = restart_edit;
1956 else
1957 restart_edit_save = 0;
1958 restart_edit = 0;
1959 /* Reset finish_op now, don't want it set inside edit(). */
1960 finish_op = FALSE;
1961 if (op_change(oap)) /* will call edit() */
1962 cap->retval |= CA_COMMAND_BUSY;
1963 if (restart_edit == 0)
1964 restart_edit = restart_edit_save;
1965 }
1966 break;
1967
1968 case OP_FILTER:
1969 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1970 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1971 else
1972 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1973
1974 case OP_INDENT:
1975 case OP_COLON:
1976
1977#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1978 /*
1979 * If 'equalprg' is empty, do the indenting internally.
1980 */
1981 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1982 {
1983# ifdef FEAT_LISP
1984 if (curbuf->b_p_lisp)
1985 {
1986 op_reindent(oap, get_lisp_indent);
1987 break;
1988 }
1989# endif
1990# ifdef FEAT_CINDENT
1991 op_reindent(oap,
1992# ifdef FEAT_EVAL
1993 *curbuf->b_p_inde != NUL ? get_expr_indent :
1994# endif
1995 get_c_indent);
1996 break;
1997# endif
1998 }
1999#endif
2000
2001 op_colon(oap);
2002 break;
2003
2004 case OP_TILDE:
2005 case OP_UPPER:
2006 case OP_LOWER:
2007 case OP_ROT13:
2008 if (empty_region_error)
2009 vim_beep();
2010 else
2011 op_tilde(oap);
2012 check_cursor_col();
2013 break;
2014
2015 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002016#if defined(FEAT_EVAL)
2017 if (*curbuf->b_p_fex != NUL)
2018 op_formatexpr(oap); /* use expression */
2019 else
2020#endif
2021 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 op_colon(oap); /* use external command */
2023 else
2024 op_format(oap, FALSE); /* use internal function */
2025 break;
2026
2027 case OP_FORMAT2:
2028 op_format(oap, TRUE); /* use internal function */
2029 break;
2030
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002031 case OP_FUNCTION:
2032 op_function(oap); /* call 'operatorfunc' */
2033 break;
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 case OP_INSERT:
2036 case OP_APPEND:
2037#ifdef FEAT_VISUAL
2038 VIsual_reselect = FALSE; /* don't reselect now */
2039#endif
2040#ifdef FEAT_VISUALEXTRA
2041 if (empty_region_error)
2042 vim_beep();
2043 else
2044 {
2045 /* This is a new edit command, not a restart. Need to
2046 * remember it to make 'insertmode' work with mappings for
2047 * Visual mode. But do this only once. */
2048 restart_edit_save = restart_edit;
2049 restart_edit = 0;
2050
2051 op_insert(oap, cap->count1);
2052
2053 /* TODO: when inserting in several lines, should format all
2054 * the lines. */
2055 auto_format(FALSE, TRUE);
2056
2057 if (restart_edit == 0)
2058 restart_edit = restart_edit_save;
2059 }
2060#else
2061 vim_beep();
2062#endif
2063 break;
2064
2065 case OP_REPLACE:
2066#ifdef FEAT_VISUAL
2067 VIsual_reselect = FALSE; /* don't reselect now */
2068#endif
2069#ifdef FEAT_VISUALEXTRA
2070 if (empty_region_error)
2071#endif
2072 vim_beep();
2073#ifdef FEAT_VISUALEXTRA
2074 else
2075 op_replace(oap, cap->nchar);
2076#endif
2077 break;
2078
2079#ifdef FEAT_FOLDING
2080 case OP_FOLD:
2081 VIsual_reselect = FALSE; /* don't reselect now */
2082 foldCreate(oap->start.lnum, oap->end.lnum);
2083 break;
2084
2085 case OP_FOLDOPEN:
2086 case OP_FOLDOPENREC:
2087 case OP_FOLDCLOSE:
2088 case OP_FOLDCLOSEREC:
2089 VIsual_reselect = FALSE; /* don't reselect now */
2090 opFoldRange(oap->start.lnum, oap->end.lnum,
2091 oap->op_type == OP_FOLDOPEN
2092 || oap->op_type == OP_FOLDOPENREC,
2093 oap->op_type == OP_FOLDOPENREC
2094 || oap->op_type == OP_FOLDCLOSEREC,
2095 oap->is_VIsual);
2096 break;
2097
2098 case OP_FOLDDEL:
2099 case OP_FOLDDELREC:
2100 VIsual_reselect = FALSE; /* don't reselect now */
2101 deleteFold(oap->start.lnum, oap->end.lnum,
2102 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2103 break;
2104#endif
2105 default:
2106 clearopbeep(oap);
2107 }
2108#ifdef FEAT_VIRTUALEDIT
2109 virtual_op = MAYBE;
2110#endif
2111 if (!gui_yank)
2112 {
2113 /*
2114 * if 'sol' not set, go back to old column for some commands
2115 */
2116 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2117 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2118 || oap->op_type == OP_DELETE))
2119 coladvance(curwin->w_curswant = old_col);
2120 }
2121 else
2122 {
2123 curwin->w_cursor = old_cursor;
2124 }
2125#ifdef FEAT_VISUAL
2126 oap->block_mode = FALSE;
2127#endif
2128 clearop(oap);
2129 }
2130}
2131
2132/*
2133 * Handle indent and format operators and visual mode ":".
2134 */
2135 static void
2136op_colon(oap)
2137 oparg_T *oap;
2138{
2139 stuffcharReadbuff(':');
2140#ifdef FEAT_VISUAL
2141 if (oap->is_VIsual)
2142 stuffReadbuff((char_u *)"'<,'>");
2143 else
2144#endif
2145 {
2146 /*
2147 * Make the range look nice, so it can be repeated.
2148 */
2149 if (oap->start.lnum == curwin->w_cursor.lnum)
2150 stuffcharReadbuff('.');
2151 else
2152 stuffnumReadbuff((long)oap->start.lnum);
2153 if (oap->end.lnum != oap->start.lnum)
2154 {
2155 stuffcharReadbuff(',');
2156 if (oap->end.lnum == curwin->w_cursor.lnum)
2157 stuffcharReadbuff('.');
2158 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2159 stuffcharReadbuff('$');
2160 else if (oap->start.lnum == curwin->w_cursor.lnum)
2161 {
2162 stuffReadbuff((char_u *)".+");
2163 stuffnumReadbuff((long)oap->line_count - 1);
2164 }
2165 else
2166 stuffnumReadbuff((long)oap->end.lnum);
2167 }
2168 }
2169 if (oap->op_type != OP_COLON)
2170 stuffReadbuff((char_u *)"!");
2171 if (oap->op_type == OP_INDENT)
2172 {
2173#ifndef FEAT_CINDENT
2174 if (*get_equalprg() == NUL)
2175 stuffReadbuff((char_u *)"indent");
2176 else
2177#endif
2178 stuffReadbuff(get_equalprg());
2179 stuffReadbuff((char_u *)"\n");
2180 }
2181 else if (oap->op_type == OP_FORMAT)
2182 {
2183 if (*p_fp == NUL)
2184 stuffReadbuff((char_u *)"fmt");
2185 else
2186 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002187 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 }
2189
2190 /*
2191 * do_cmdline() does the rest
2192 */
2193}
2194
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002195/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002196 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002197 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002198 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002199op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002200 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002201{
2202#ifdef FEAT_EVAL
2203 char_u *(argv[1]);
2204
2205 if (*p_opfunc == NUL)
2206 EMSG(_("E774: 'operatorfunc' is empty"));
2207 else
2208 {
2209 /* Set '[ and '] marks to text to be operated on. */
2210 curbuf->b_op_start = oap->start;
2211 curbuf->b_op_end = oap->end;
2212 if (oap->motion_type != MLINE && !oap->inclusive)
2213 /* Exclude the end position. */
2214 decl(&curbuf->b_op_end);
2215
2216 if (oap->block_mode)
2217 argv[0] = (char_u *)"block";
2218 else if (oap->motion_type == MLINE)
2219 argv[0] = (char_u *)"line";
2220 else
2221 argv[0] = (char_u *)"char";
2222 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2223 }
2224#else
2225 EMSG(_("E775: Eval feature not available"));
2226#endif
2227}
2228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229#if defined(FEAT_MOUSE) || defined(PROTO)
2230/*
2231 * Do the appropriate action for the current mouse click in the current mode.
2232 * Not used for Command-line mode.
2233 *
2234 * Normal Mode:
2235 * event modi- position visual change action
2236 * fier cursor window
2237 * left press - yes end yes
2238 * left press C yes end yes "^]" (2)
2239 * left press S yes end yes "*" (2)
2240 * left drag - yes start if moved no
2241 * left relse - yes start if moved no
2242 * middle press - yes if not active no put register
2243 * middle press - yes if active no yank and put
2244 * right press - yes start or extend yes
2245 * right press S yes no change yes "#" (2)
2246 * right drag - yes extend no
2247 * right relse - yes extend no
2248 *
2249 * Insert or Replace Mode:
2250 * event modi- position visual change action
2251 * fier cursor window
2252 * left press - yes (cannot be active) yes
2253 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2254 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2255 * left drag - yes start or extend (1) no CTRL-O (1)
2256 * left relse - yes start or extend (1) no CTRL-O (1)
2257 * middle press - no (cannot be active) no put register
2258 * right press - yes start or extend yes CTRL-O
2259 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2260 *
2261 * (1) only if mouse pointer moved since press
2262 * (2) only if click is in same buffer
2263 *
2264 * Return TRUE if start_arrow() should be called for edit mode.
2265 */
2266 int
2267do_mouse(oap, c, dir, count, fixindent)
2268 oparg_T *oap; /* operator argument, can be NULL */
2269 int c; /* K_LEFTMOUSE, etc */
2270 int dir; /* Direction to 'put' if necessary */
2271 long count;
2272 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2273{
2274 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2275 static int got_click = FALSE; /* got a click some time back */
2276
2277 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2278 int is_click; /* If FALSE it's a drag or release event */
2279 int is_drag; /* If TRUE it's a drag event */
2280 int jump_flags = 0; /* flags for jump_to_mouse() */
2281 pos_T start_visual;
2282 int moved; /* Has cursor moved? */
2283 int in_status_line; /* mouse in status line */
2284#ifdef FEAT_VERTSPLIT
2285 int in_sep_line; /* mouse in vertical separator line */
2286#endif
2287 int c1, c2;
2288#if defined(FEAT_FOLDING)
2289 pos_T save_cursor;
2290#endif
2291 win_T *old_curwin = curwin;
2292#ifdef FEAT_VISUAL
2293 static pos_T orig_cursor;
2294 colnr_T leftcol, rightcol;
2295 pos_T end_visual;
2296 int diff;
2297 int old_active = VIsual_active;
2298 int old_mode = VIsual_mode;
2299#endif
2300 int regname;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002301#ifdef FEAT_CONCEAL
2302 linenr_T oldline = curwin->w_cursor.lnum;
2303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305#if defined(FEAT_FOLDING)
2306 save_cursor = curwin->w_cursor;
2307#endif
2308
2309 /*
2310 * When GUI is active, always recognize mouse events, otherwise:
2311 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2312 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2313 * - For command line and insert mode 'mouse' is checked before calling
2314 * do_mouse().
2315 */
2316 if (do_always)
2317 do_always = FALSE;
2318 else
2319#ifdef FEAT_GUI
2320 if (!gui.in_use)
2321#endif
2322 {
2323#ifdef FEAT_VISUAL
2324 if (VIsual_active)
2325 {
2326 if (!mouse_has(MOUSE_VISUAL))
2327 return FALSE;
2328 }
2329 else
2330#endif
2331 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2332 return FALSE;
2333 }
2334
2335 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2336
2337#ifdef FEAT_MOUSESHAPE
2338 /* May have stopped dragging the status or separator line. The pointer is
2339 * most likely still on the status or separator line. */
2340 if (!is_drag && drag_status_line)
2341 {
2342 drag_status_line = FALSE;
2343 update_mouseshape(SHAPE_IDX_STATUS);
2344 }
2345# ifdef FEAT_VERTSPLIT
2346 if (!is_drag && drag_sep_line)
2347 {
2348 drag_sep_line = FALSE;
2349 update_mouseshape(SHAPE_IDX_VSEP);
2350 }
2351# endif
2352#endif
2353
2354 /*
2355 * Ignore drag and release events if we didn't get a click.
2356 */
2357 if (is_click)
2358 got_click = TRUE;
2359 else
2360 {
2361 if (!got_click) /* didn't get click, ignore */
2362 return FALSE;
2363 if (!is_drag) /* release, reset got_click */
2364 got_click = FALSE;
2365 }
2366
Bram Moolenaar64969662005-12-14 21:59:55 +00002367#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002369 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 */
2371 if ((mod_mask & MOD_MASK_ALT))
2372 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /*
2376 * CTRL right mouse button does CTRL-T
2377 */
2378 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2379 {
2380 if (State & INSERT)
2381 stuffcharReadbuff(Ctrl_O);
2382 if (count > 1)
2383 stuffnumReadbuff(count);
2384 stuffcharReadbuff(Ctrl_T);
2385 got_click = FALSE; /* ignore drag&release now */
2386 return FALSE;
2387 }
2388
2389 /*
2390 * CTRL only works with left mouse button
2391 */
2392 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2393 return FALSE;
2394
2395 /*
2396 * When a modifier is down, ignore drag and release events, as well as
2397 * multiple clicks and the middle mouse button.
2398 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2399 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002400 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2401 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 && (!is_click
2403 || (mod_mask & MOD_MASK_MULTI_CLICK)
2404 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002405 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 && mouse_model_popup()
2407 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002408 && !((mod_mask & MOD_MASK_ALT)
2409 && !mouse_model_popup()
2410 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 )
2412 return FALSE;
2413
2414 /*
2415 * If the button press was used as the movement command for an operator
2416 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2417 * drag/release events.
2418 */
2419 if (!is_click && which_button == MOUSE_MIDDLE)
2420 return FALSE;
2421
2422 if (oap != NULL)
2423 regname = oap->regname;
2424 else
2425 regname = 0;
2426
2427 /*
2428 * Middle mouse button does a 'put' of the selected text
2429 */
2430 if (which_button == MOUSE_MIDDLE)
2431 {
2432 if (State == NORMAL)
2433 {
2434 /*
2435 * If an operator was pending, we don't know what the user wanted
2436 * to do. Go back to normal mode: Clear the operator and beep().
2437 */
2438 if (oap != NULL && oap->op_type != OP_NOP)
2439 {
2440 clearopbeep(oap);
2441 return FALSE;
2442 }
2443
2444#ifdef FEAT_VISUAL
2445 /*
2446 * If visual was active, yank the highlighted text and put it
2447 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002448 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 */
2450 if (VIsual_active)
2451 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002452 if (VIsual_select)
2453 {
2454 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002455 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002456 }
2457 else
2458 {
2459 stuffcharReadbuff('y');
2460 stuffcharReadbuff(K_MIDDLEMOUSE);
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 do_always = TRUE; /* ignore 'mouse' setting next time */
2463 return FALSE;
2464 }
2465#endif
2466 /*
2467 * The rest is below jump_to_mouse()
2468 */
2469 }
2470
2471 else if ((State & INSERT) == 0)
2472 return FALSE;
2473
2474 /*
2475 * Middle click in insert mode doesn't move the mouse, just insert the
2476 * contents of a register. '.' register is special, can't insert that
2477 * with do_put().
2478 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2479 * happens for the GUI).
2480 */
2481 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2482 {
2483 if (regname == '.')
2484 insert_reg(regname, TRUE);
2485 else
2486 {
2487#ifdef FEAT_CLIPBOARD
2488 if (clip_star.available && regname == 0)
2489 regname = '*';
2490#endif
2491 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2492 insert_reg(regname, TRUE);
2493 else
2494 {
2495 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2496
2497 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2498 AppendCharToRedobuff(Ctrl_R);
2499 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2500 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2501 }
2502 }
2503 return FALSE;
2504 }
2505 }
2506
2507 /* When dragging or button-up stay in the same window. */
2508 if (!is_click)
2509 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2510
2511 start_visual.lnum = 0;
2512
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513#ifdef FEAT_WINDOWS
2514 /* Check for clicking in the tab page line. */
2515 if (mouse_row == 0 && firstwin->w_winrow > 0)
2516 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002517 if (is_drag)
2518 return FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519 got_click = FALSE; /* ignore mouse-up and drag events */
2520
Bram Moolenaarf740b292006-02-16 22:11:02 +00002521 /* click in a tab selects that tab page */
2522 if (is_click
2523# ifdef FEAT_CMDWIN
2524 && cmdwin_type == 0
2525# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002527 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528 c1 = TabPageIdxs[mouse_col];
2529 if (c1 >= 0)
2530 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002531 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2532 {
2533 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002535 tabpage_new();
2536 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2537 }
2538 else
2539 {
2540 /* Go to specified tab page, or next one if not clicking
2541 * on a label. */
2542 goto_tabpage(c1);
2543
2544 /* It's like clicking on the status line of a window. */
2545 if (curwin != old_curwin)
2546 end_visual_mode();
2547 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002548 }
2549 else if (c1 < 0)
2550 {
2551 tabpage_T *tp;
2552
2553 /* Close the current or specified tab page. */
2554 if (c1 == -999)
2555 tp = curtab;
2556 else
2557 tp = find_tabpage(-c1);
2558 if (tp == curtab)
2559 {
2560 if (first_tabpage->tp_next != NULL)
2561 tabpage_close(FALSE);
2562 }
2563 else if (tp != NULL)
2564 tabpage_close_other(tp, FALSE);
2565 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 }
2569#endif
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 /*
2572 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2573 * right button up -> pop-up menu
2574 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002575 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
2577 if (mouse_model_popup())
2578 {
2579 if (which_button == MOUSE_RIGHT
2580 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2581 {
2582 /*
2583 * NOTE: Ignore right button down and drag mouse events.
2584 * Windows only shows the popup menu on the button up event.
2585 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002586#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2587 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (!is_click)
2589 return FALSE;
2590#endif
2591#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2592 if (is_click || is_drag)
2593 return FALSE;
2594#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002595#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2597 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2598 if (gui.in_use)
2599 {
2600 jump_flags = 0;
2601 if (STRCMP(p_mousem, "popup_setpos") == 0)
2602 {
2603 /* First set the cursor position before showing the popup
2604 * menu. */
2605#ifdef FEAT_VISUAL
2606 if (VIsual_active)
2607 {
2608 pos_T m_pos;
2609
2610 /*
2611 * set MOUSE_MAY_STOP_VIS if we are outside the
2612 * selection or the current window (might have false
2613 * negative here)
2614 */
2615 if (mouse_row < W_WINROW(curwin)
2616 || mouse_row
2617 > (W_WINROW(curwin) + curwin->w_height))
2618 jump_flags = MOUSE_MAY_STOP_VIS;
2619 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2620 jump_flags = MOUSE_MAY_STOP_VIS;
2621 else
2622 {
2623 if ((lt(curwin->w_cursor, VIsual)
2624 && (lt(m_pos, curwin->w_cursor)
2625 || lt(VIsual, m_pos)))
2626 || (lt(VIsual, curwin->w_cursor)
2627 && (lt(m_pos, VIsual)
2628 || lt(curwin->w_cursor, m_pos))))
2629 {
2630 jump_flags = MOUSE_MAY_STOP_VIS;
2631 }
2632 else if (VIsual_mode == Ctrl_V)
2633 {
2634 getvcols(curwin, &curwin->w_cursor, &VIsual,
2635 &leftcol, &rightcol);
2636 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2637 if (m_pos.col < leftcol || m_pos.col > rightcol)
2638 jump_flags = MOUSE_MAY_STOP_VIS;
2639 }
2640 }
2641 }
2642 else
2643 jump_flags = MOUSE_MAY_STOP_VIS;
2644#endif
2645 }
2646 if (jump_flags)
2647 {
2648 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2649 update_curbuf(
2650#ifdef FEAT_VISUAL
2651 VIsual_active ? INVERTED :
2652#endif
2653 VALID);
2654 setcursor();
2655 out_flush(); /* Update before showing popup menu */
2656 }
2657# ifdef FEAT_MENU
2658 gui_show_popupmenu();
2659# endif
2660 return (jump_flags & CURSOR_MOVED) != 0;
2661 }
2662 else
2663 return FALSE;
2664#else
2665 return FALSE;
2666#endif
2667 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002668 if (which_button == MOUSE_LEFT
2669 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 which_button = MOUSE_RIGHT;
2672 mod_mask &= ~MOD_MASK_SHIFT;
2673 }
2674 }
2675
2676#ifdef FEAT_VISUAL
2677 if ((State & (NORMAL | INSERT))
2678 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2679 {
2680 if (which_button == MOUSE_LEFT)
2681 {
2682 if (is_click)
2683 {
2684 /* stop Visual mode for a left click in a window, but not when
2685 * on a status line */
2686 if (VIsual_active)
2687 jump_flags |= MOUSE_MAY_STOP_VIS;
2688 }
2689 else if (mouse_has(MOUSE_VISUAL))
2690 jump_flags |= MOUSE_MAY_VIS;
2691 }
2692 else if (which_button == MOUSE_RIGHT)
2693 {
2694 if (is_click && VIsual_active)
2695 {
2696 /*
2697 * Remember the start and end of visual before moving the
2698 * cursor.
2699 */
2700 if (lt(curwin->w_cursor, VIsual))
2701 {
2702 start_visual = curwin->w_cursor;
2703 end_visual = VIsual;
2704 }
2705 else
2706 {
2707 start_visual = VIsual;
2708 end_visual = curwin->w_cursor;
2709 }
2710 }
2711 jump_flags |= MOUSE_FOCUS;
2712 if (mouse_has(MOUSE_VISUAL))
2713 jump_flags |= MOUSE_MAY_VIS;
2714 }
2715 }
2716#endif
2717
2718 /*
2719 * If an operator is pending, ignore all drags and releases until the
2720 * next mouse click.
2721 */
2722 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2723 {
2724 got_click = FALSE;
2725 oap->motion_type = MCHAR;
2726 }
2727
2728 /* When releasing the button let jump_to_mouse() know. */
2729 if (!is_click && !is_drag)
2730 jump_flags |= MOUSE_RELEASED;
2731
2732 /*
2733 * JUMP!
2734 */
2735 jump_flags = jump_to_mouse(jump_flags,
2736 oap == NULL ? NULL : &(oap->inclusive), which_button);
2737 moved = (jump_flags & CURSOR_MOVED);
2738 in_status_line = (jump_flags & IN_STATUS_LINE);
2739#ifdef FEAT_VERTSPLIT
2740 in_sep_line = (jump_flags & IN_SEP_LINE);
2741#endif
2742
2743#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002744 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2746 {
2747 int key = KEY2TERMCAP1(c);
2748
2749 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2750 || key == (int)KE_RIGHTRELEASE)
2751 netbeans_button_release(which_button);
2752 }
2753#endif
2754
2755 /* When jumping to another window, clear a pending operator. That's a bit
2756 * friendlier than beeping and not jumping to that window. */
2757 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2758 clearop(oap);
2759
2760#ifdef FEAT_FOLDING
2761 if (mod_mask == 0
2762 && !is_drag
2763 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2764 && which_button == MOUSE_LEFT)
2765 {
2766 /* open or close a fold at this line */
2767 if (jump_flags & MOUSE_FOLD_OPEN)
2768 openFold(curwin->w_cursor.lnum, 1L);
2769 else
2770 closeFold(curwin->w_cursor.lnum, 1L);
2771 /* don't move the cursor if still in the same window */
2772 if (curwin == old_curwin)
2773 curwin->w_cursor = save_cursor;
2774 }
2775#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776#ifdef FEAT_CONCEAL
2777 if (curwin->w_p_conceal && moved
2778 && (old_curwin != curwin || oldline != curwin->w_cursor.lnum))
2779 {
2780 update_single_line(old_curwin, oldline);
2781 update_single_line(curwin, curwin->w_cursor.lnum);
2782 }
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2786 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2787 {
2788 clip_modeless(which_button, is_click, is_drag);
2789 return FALSE;
2790 }
2791#endif
2792
2793#ifdef FEAT_VISUAL
2794 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002795 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (VIsual_active && is_drag && p_so)
2797 {
2798 /* In the very first line, allow scrolling one line */
2799 if (mouse_row == 0)
2800 mouse_dragging = 2;
2801 else
2802 mouse_dragging = 1;
2803 }
2804
2805 /* When dragging the mouse above the window, scroll down. */
2806 if (is_drag && mouse_row < 0 && !in_status_line)
2807 {
2808 scroll_redraw(FALSE, 1L);
2809 mouse_row = 0;
2810 }
2811
2812 if (start_visual.lnum) /* right click in visual mode */
2813 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002814 /* When ALT is pressed make Visual mode blockwise. */
2815 if (mod_mask & MOD_MASK_ALT)
2816 VIsual_mode = Ctrl_V;
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 /*
2819 * In Visual-block mode, divide the area in four, pick up the corner
2820 * that is in the quarter that the cursor is in.
2821 */
2822 if (VIsual_mode == Ctrl_V)
2823 {
2824 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2825 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2826 end_visual.col = leftcol;
2827 else
2828 end_visual.col = rightcol;
2829 if (curwin->w_cursor.lnum <
2830 (start_visual.lnum + end_visual.lnum) / 2)
2831 end_visual.lnum = end_visual.lnum;
2832 else
2833 end_visual.lnum = start_visual.lnum;
2834
2835 /* move VIsual to the right column */
2836 start_visual = curwin->w_cursor; /* save the cursor pos */
2837 curwin->w_cursor = end_visual;
2838 coladvance(end_visual.col);
2839 VIsual = curwin->w_cursor;
2840 curwin->w_cursor = start_visual; /* restore the cursor */
2841 }
2842 else
2843 {
2844 /*
2845 * If the click is before the start of visual, change the start.
2846 * If the click is after the end of visual, change the end. If
2847 * the click is inside the visual, change the closest side.
2848 */
2849 if (lt(curwin->w_cursor, start_visual))
2850 VIsual = end_visual;
2851 else if (lt(end_visual, curwin->w_cursor))
2852 VIsual = start_visual;
2853 else
2854 {
2855 /* In the same line, compare column number */
2856 if (end_visual.lnum == start_visual.lnum)
2857 {
2858 if (curwin->w_cursor.col - start_visual.col >
2859 end_visual.col - curwin->w_cursor.col)
2860 VIsual = start_visual;
2861 else
2862 VIsual = end_visual;
2863 }
2864
2865 /* In different lines, compare line number */
2866 else
2867 {
2868 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2869 (end_visual.lnum - curwin->w_cursor.lnum);
2870
2871 if (diff > 0) /* closest to end */
2872 VIsual = start_visual;
2873 else if (diff < 0) /* closest to start */
2874 VIsual = end_visual;
2875 else /* in the middle line */
2876 {
2877 if (curwin->w_cursor.col <
2878 (start_visual.col + end_visual.col) / 2)
2879 VIsual = end_visual;
2880 else
2881 VIsual = start_visual;
2882 }
2883 }
2884 }
2885 }
2886 }
2887 /*
2888 * If Visual mode started in insert mode, execute "CTRL-O"
2889 */
2890 else if ((State & INSERT) && VIsual_active)
2891 stuffcharReadbuff(Ctrl_O);
2892#endif
2893
2894 /*
2895 * Middle mouse click: Put text before cursor.
2896 */
2897 if (which_button == MOUSE_MIDDLE)
2898 {
2899#ifdef FEAT_CLIPBOARD
2900 if (clip_star.available && regname == 0)
2901 regname = '*';
2902#endif
2903 if (yank_register_mline(regname))
2904 {
2905 if (mouse_past_bottom)
2906 dir = FORWARD;
2907 }
2908 else if (mouse_past_eol)
2909 dir = FORWARD;
2910
2911 if (fixindent)
2912 {
2913 c1 = (dir == BACKWARD) ? '[' : ']';
2914 c2 = 'p';
2915 }
2916 else
2917 {
2918 c1 = (dir == FORWARD) ? 'p' : 'P';
2919 c2 = NUL;
2920 }
2921 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2922
2923 /*
2924 * Remember where the paste started, so in edit() Insstart can be set
2925 * to this position
2926 */
2927 if (restart_edit != 0)
2928 where_paste_started = curwin->w_cursor;
2929 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2930 }
2931
2932#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2933 /*
2934 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2935 * error under the mouse pointer.
2936 */
2937 else if (((mod_mask & MOD_MASK_CTRL)
2938 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2939 && bt_quickfix(curbuf))
2940 {
2941 if (State & INSERT)
2942 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002943 if (curwin->w_llist_ref == NULL) /* quickfix window */
2944 stuffReadbuff((char_u *)":.cc\n");
2945 else /* location list window */
2946 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 got_click = FALSE; /* ignore drag&release now */
2948 }
2949#endif
2950
2951 /*
2952 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2953 * under the mouse pointer.
2954 */
2955 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2956 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2957 {
2958 if (State & INSERT)
2959 stuffcharReadbuff(Ctrl_O);
2960 stuffcharReadbuff(Ctrl_RSB);
2961 got_click = FALSE; /* ignore drag&release now */
2962 }
2963
2964 /*
2965 * Shift-Mouse click searches for the next occurrence of the word under
2966 * the mouse pointer
2967 */
2968 else if ((mod_mask & MOD_MASK_SHIFT))
2969 {
2970 if (State & INSERT
2971#ifdef FEAT_VISUAL
2972 || (VIsual_active && VIsual_select)
2973#endif
2974 )
2975 stuffcharReadbuff(Ctrl_O);
2976 if (which_button == MOUSE_LEFT)
2977 stuffcharReadbuff('*');
2978 else /* MOUSE_RIGHT */
2979 stuffcharReadbuff('#');
2980 }
2981
2982 /* Handle double clicks, unless on status line */
2983 else if (in_status_line)
2984 {
2985#ifdef FEAT_MOUSESHAPE
2986 if ((is_drag || is_click) && !drag_status_line)
2987 {
2988 drag_status_line = TRUE;
2989 update_mouseshape(-1);
2990 }
2991#endif
2992 }
2993#ifdef FEAT_VERTSPLIT
2994 else if (in_sep_line)
2995 {
2996# ifdef FEAT_MOUSESHAPE
2997 if ((is_drag || is_click) && !drag_sep_line)
2998 {
2999 drag_sep_line = TRUE;
3000 update_mouseshape(-1);
3001 }
3002# endif
3003 }
3004#endif
3005#ifdef FEAT_VISUAL
3006 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3007 && mouse_has(MOUSE_VISUAL))
3008 {
3009 if (is_click || !VIsual_active)
3010 {
3011 if (VIsual_active)
3012 orig_cursor = VIsual;
3013 else
3014 {
3015 check_visual_highlight();
3016 VIsual = curwin->w_cursor;
3017 orig_cursor = VIsual;
3018 VIsual_active = TRUE;
3019 VIsual_reselect = TRUE;
3020 /* start Select mode if 'selectmode' contains "mouse" */
3021 may_start_select('o');
3022 setmouse();
3023 }
3024 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003025 {
3026 /* Double click with ALT pressed makes it blockwise. */
3027 if (mod_mask & MOD_MASK_ALT)
3028 VIsual_mode = Ctrl_V;
3029 else
3030 VIsual_mode = 'v';
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3033 VIsual_mode = 'V';
3034 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3035 VIsual_mode = Ctrl_V;
3036#ifdef FEAT_CLIPBOARD
3037 /* Make sure the clipboard gets updated. Needed because start and
3038 * end may still be the same, and the selection needs to be owned */
3039 clip_star.vmode = NUL;
3040#endif
3041 }
3042 /*
3043 * A double click selects a word or a block.
3044 */
3045 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3046 {
3047 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003048 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050 if (is_click)
3051 {
3052 /* If the character under the cursor (skipping white space) is
3053 * not a word character, try finding a match and select a (),
3054 * {}, [], #if/#endif, etc. block. */
3055 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003056 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 inc(&end_visual);
3058 if (oap != NULL)
3059 oap->motion_type = MCHAR;
3060 if (oap != NULL
3061 && VIsual_mode == 'v'
3062 && !vim_iswordc(gchar_pos(&end_visual))
3063 && equalpos(curwin->w_cursor, VIsual)
3064 && (pos = findmatch(oap, NUL)) != NULL)
3065 {
3066 curwin->w_cursor = *pos;
3067 if (oap->motion_type == MLINE)
3068 VIsual_mode = 'V';
3069 else if (*p_sel == 'e')
3070 {
3071 if (lt(curwin->w_cursor, VIsual))
3072 ++VIsual.col;
3073 else
3074 ++curwin->w_cursor.col;
3075 }
3076 }
3077 }
3078
3079 if (pos == NULL && (is_click || is_drag))
3080 {
3081 /* When not found a match or when dragging: extend to include
3082 * a word. */
3083 if (lt(curwin->w_cursor, orig_cursor))
3084 {
3085 find_start_of_word(&curwin->w_cursor);
3086 find_end_of_word(&VIsual);
3087 }
3088 else
3089 {
3090 find_start_of_word(&VIsual);
3091 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3092#ifdef FEAT_MBYTE
3093 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#else
3096 ++curwin->w_cursor.col;
3097#endif
3098 find_end_of_word(&curwin->w_cursor);
3099 }
3100 }
3101 curwin->w_set_curswant = TRUE;
3102 }
3103 if (is_click)
3104 redraw_curbuf_later(INVERTED); /* update the inversion */
3105 }
3106 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003107 {
3108 if (mod_mask & MOD_MASK_ALT)
3109 VIsual_mode = Ctrl_V;
3110 else
3111 VIsual_mode = 'v';
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003115 if ((!VIsual_active && old_active && mode_displayed)
3116 || (VIsual_active && p_smd && msg_silent == 0
3117 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 redraw_cmdline = TRUE;
3119#endif
3120
3121 return moved;
3122}
3123
3124#ifdef FEAT_VISUAL
3125/*
3126 * Move "pos" back to the start of the word it's in.
3127 */
3128 static void
3129find_start_of_word(pos)
3130 pos_T *pos;
3131{
3132 char_u *line;
3133 int cclass;
3134 int col;
3135
3136 line = ml_get(pos->lnum);
3137 cclass = get_mouse_class(line + pos->col);
3138
3139 while (pos->col > 0)
3140 {
3141 col = pos->col - 1;
3142#ifdef FEAT_MBYTE
3143 col -= (*mb_head_off)(line, line + col);
3144#endif
3145 if (get_mouse_class(line + col) != cclass)
3146 break;
3147 pos->col = col;
3148 }
3149}
3150
3151/*
3152 * Move "pos" forward to the end of the word it's in.
3153 * When 'selection' is "exclusive", the position is just after the word.
3154 */
3155 static void
3156find_end_of_word(pos)
3157 pos_T *pos;
3158{
3159 char_u *line;
3160 int cclass;
3161 int col;
3162
3163 line = ml_get(pos->lnum);
3164 if (*p_sel == 'e' && pos->col > 0)
3165 {
3166 --pos->col;
3167#ifdef FEAT_MBYTE
3168 pos->col -= (*mb_head_off)(line, line + pos->col);
3169#endif
3170 }
3171 cclass = get_mouse_class(line + pos->col);
3172 while (line[pos->col] != NUL)
3173 {
3174#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003175 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#else
3177 col = pos->col + 1;
3178#endif
3179 if (get_mouse_class(line + col) != cclass)
3180 {
3181 if (*p_sel == 'e')
3182 pos->col = col;
3183 break;
3184 }
3185 pos->col = col;
3186 }
3187}
3188
3189/*
3190 * Get class of a character for selection: same class means same word.
3191 * 0: blank
3192 * 1: punctuation groups
3193 * 2: normal word character
3194 * >2: multi-byte word character.
3195 */
3196 static int
3197get_mouse_class(p)
3198 char_u *p;
3199{
3200 int c;
3201
3202#ifdef FEAT_MBYTE
3203 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3204 return mb_get_class(p);
3205#endif
3206
3207 c = *p;
3208 if (c == ' ' || c == '\t')
3209 return 0;
3210
3211 if (vim_iswordc(c))
3212 return 2;
3213
3214 /*
3215 * There are a few special cases where we want certain combinations of
3216 * characters to be considered as a single word. These are things like
3217 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003218 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
3220 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3221 return 1;
3222 return c;
3223}
3224#endif /* FEAT_VISUAL */
3225#endif /* FEAT_MOUSE */
3226
3227#if defined(FEAT_VISUAL) || defined(PROTO)
3228/*
3229 * Check if highlighting for visual mode is possible, give a warning message
3230 * if not.
3231 */
3232 void
3233check_visual_highlight()
3234{
3235 static int did_check = FALSE;
3236
3237 if (full_screen)
3238 {
3239 if (!did_check && hl_attr(HLF_V) == 0)
3240 MSG(_("Warning: terminal cannot highlight"));
3241 did_check = TRUE;
3242 }
3243}
3244
3245/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003246 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * This function should ALWAYS be called to end Visual mode, except from
3248 * do_pending_operator().
3249 */
3250 void
3251end_visual_mode()
3252{
3253#ifdef FEAT_CLIPBOARD
3254 /*
3255 * If we are using the clipboard, then remember what was selected in case
3256 * we need to paste it somewhere while we still own the selection.
3257 * Only do this when the clipboard is already owned. Don't want to grab
3258 * the selection when hitting ESC.
3259 */
3260 if (clip_star.available && clip_star.owned)
3261 clip_auto_select();
3262#endif
3263
3264 VIsual_active = FALSE;
3265#ifdef FEAT_MOUSE
3266 setmouse();
3267 mouse_dragging = 0;
3268#endif
3269
3270 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003271 curbuf->b_visual.vi_mode = VIsual_mode;
3272 curbuf->b_visual.vi_start = VIsual;
3273 curbuf->b_visual.vi_end = curwin->w_cursor;
3274 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_EVAL
3276 curbuf->b_visual_mode_eval = VIsual_mode;
3277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_VIRTUALEDIT
3279 if (!virtual_active())
3280 curwin->w_cursor.coladd = 0;
3281#endif
3282
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003283 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 clear_cmdline = TRUE; /* unshow visual mode later */
3285#ifdef FEAT_CMDL_INFO
3286 else
3287 clear_showcmd();
3288#endif
3289
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003290 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291}
3292
3293/*
3294 * Reset VIsual_active and VIsual_reselect.
3295 */
3296 void
3297reset_VIsual_and_resel()
3298{
3299 if (VIsual_active)
3300 {
3301 end_visual_mode();
3302 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3303 }
3304 VIsual_reselect = FALSE;
3305}
3306
3307/*
3308 * Reset VIsual_active and VIsual_reselect if it's set.
3309 */
3310 void
3311reset_VIsual()
3312{
3313 if (VIsual_active)
3314 {
3315 end_visual_mode();
3316 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3317 VIsual_reselect = FALSE;
3318 }
3319}
3320#endif /* FEAT_VISUAL */
3321
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3324
3325/*
3326 * Check for a balloon-eval special item to include when searching for an
3327 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3328 * Returns TRUE if the character at "*ptr" should be included.
3329 * "dir" is FORWARD or BACKWARD, the direction of searching.
3330 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3331 * "bnp" points to a counter for square brackets.
3332 */
3333 static int
3334find_is_eval_item(ptr, colp, bnp, dir)
3335 char_u *ptr;
3336 int *colp;
3337 int *bnp;
3338 int dir;
3339{
3340 /* Accept everything inside []. */
3341 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3342 ++*bnp;
3343 if (*bnp > 0)
3344 {
3345 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3346 --*bnp;
3347 return TRUE;
3348 }
3349
3350 /* skip over "s.var" */
3351 if (*ptr == '.')
3352 return TRUE;
3353
3354 /* two-character item: s->var */
3355 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3356 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3357 {
3358 *colp += dir;
3359 return TRUE;
3360 }
3361 return FALSE;
3362}
3363#endif
3364
3365/*
3366 * Find the identifier under or to the right of the cursor.
3367 * "find_type" can have one of three values:
3368 * FIND_IDENT: find an identifier (keyword)
3369 * FIND_STRING: find any non-white string
3370 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *
3373 * There are three steps:
3374 * 1. Search forward for the start of an identifier/string. Doesn't move if
3375 * already on one.
3376 * 2. Search backward for the start of this identifier/string.
3377 * This doesn't match the real Vi but I like it a little better and it
3378 * shouldn't bother anyone.
3379 * 3. Search forward to the end of this identifier/string.
3380 * When FIND_IDENT isn't defined, we backup until a blank.
3381 *
3382 * Returns the length of the string, or zero if no string is found.
3383 * If a string is found, a pointer to the string is put in "*string". This
3384 * string is not always NUL terminated.
3385 */
3386 int
3387find_ident_under_cursor(string, find_type)
3388 char_u **string;
3389 int find_type;
3390{
3391 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3392 curwin->w_cursor.col, string, find_type);
3393}
3394
3395/*
3396 * Like find_ident_under_cursor(), but for any window and any position.
3397 * However: Uses 'iskeyword' from the current window!.
3398 */
3399 int
3400find_ident_at_pos(wp, lnum, startcol, string, find_type)
3401 win_T *wp;
3402 linenr_T lnum;
3403 colnr_T startcol;
3404 char_u **string;
3405 int find_type;
3406{
3407 char_u *ptr;
3408 int col = 0; /* init to shut up GCC */
3409 int i;
3410#ifdef FEAT_MBYTE
3411 int this_class = 0;
3412 int prev_class;
3413 int prevcol;
3414#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 int bn = 0; /* bracket nesting */
3417#endif
3418
3419 /*
3420 * if i == 0: try to find an identifier
3421 * if i == 1: try to find any non-white string
3422 */
3423 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3424 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3425 {
3426 /*
3427 * 1. skip to start of identifier/string
3428 */
3429 col = startcol;
3430#ifdef FEAT_MBYTE
3431 if (has_mbyte)
3432 {
3433 while (ptr[col] != NUL)
3434 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 /* Stop at a ']' to evaluate "a[x]". */
3437 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3438 break;
3439# endif
3440 this_class = mb_get_class(ptr + col);
3441 if (this_class != 0 && (i == 1 || this_class != 1))
3442 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003443 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 }
3446 else
3447#endif
3448 while (ptr[col] != NUL
3449 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3452# endif
3453 )
3454 ++col;
3455
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* When starting on a ']' count it, so that we include the '['. */
3458 bn = ptr[col] == ']';
3459#endif
3460
3461 /*
3462 * 2. Back up to start of identifier/string.
3463 */
3464#ifdef FEAT_MBYTE
3465 if (has_mbyte)
3466 {
3467 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3470 this_class = mb_get_class((char_u *)"a");
3471 else
3472# endif
3473 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3477 prev_class = mb_get_class(ptr + prevcol);
3478 if (this_class != prev_class
3479 && (i == 0
3480 || prev_class == 0
3481 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 && (!(find_type & FIND_EVAL)
3484 || prevcol == 0
3485 || !find_is_eval_item(ptr + prevcol, &prevcol,
3486 &bn, BACKWARD))
3487# endif
3488 )
3489 break;
3490 col = prevcol;
3491 }
3492
3493 /* If we don't want just any old string, or we've found an
3494 * identifier, stop searching. */
3495 if (this_class > 2)
3496 this_class = 2;
3497 if (!(find_type & FIND_STRING) || this_class == 2)
3498 break;
3499 }
3500 else
3501#endif
3502 {
3503 while (col > 0
3504 && ((i == 0
3505 ? vim_iswordc(ptr[col - 1])
3506 : (!vim_iswhite(ptr[col - 1])
3507 && (!(find_type & FIND_IDENT)
3508 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 || ((find_type & FIND_EVAL)
3511 && col > 1
3512 && find_is_eval_item(ptr + col - 1, &col,
3513 &bn, BACKWARD))
3514#endif
3515 ))
3516 --col;
3517
3518 /* If we don't want just any old string, or we've found an
3519 * identifier, stop searching. */
3520 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3521 break;
3522 }
3523 }
3524
3525 if (ptr[col] == NUL || (i == 0 && (
3526#ifdef FEAT_MBYTE
3527 has_mbyte ? this_class != 2 :
3528#endif
3529 !vim_iswordc(ptr[col]))))
3530 {
3531 /*
3532 * didn't find an identifier or string
3533 */
3534 if (find_type & FIND_STRING)
3535 EMSG(_("E348: No string under cursor"));
3536 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003537 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return 0;
3539 }
3540 ptr += col;
3541 *string = ptr;
3542
3543 /*
3544 * 3. Find the end if the identifier/string.
3545 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 bn = 0;
3548 startcol -= col;
3549#endif
3550 col = 0;
3551#ifdef FEAT_MBYTE
3552 if (has_mbyte)
3553 {
3554 /* Search for point of changing multibyte character class. */
3555 this_class = mb_get_class(ptr);
3556 while (ptr[col] != NUL
3557 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3558 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 || ((find_type & FIND_EVAL)
3561 && col <= (int)startcol
3562 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3563# endif
3564 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003565 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 else
3568#endif
3569 while ((i == 0 ? vim_iswordc(ptr[col])
3570 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 || ((find_type & FIND_EVAL)
3573 && col <= (int)startcol
3574 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3575# endif
3576 )
3577 {
3578 ++col;
3579 }
3580
3581 return col;
3582}
3583
3584/*
3585 * Prepare for redo of a normal command.
3586 */
3587 static void
3588prep_redo_cmd(cap)
3589 cmdarg_T *cap;
3590{
3591 prep_redo(cap->oap->regname, cap->count0,
3592 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3593}
3594
3595/*
3596 * Prepare for redo of any command.
3597 * Note that only the last argument can be a multi-byte char.
3598 */
3599 static void
3600prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3601 int regname;
3602 long num;
3603 int cmd1;
3604 int cmd2;
3605 int cmd3;
3606 int cmd4;
3607 int cmd5;
3608{
3609 ResetRedobuff();
3610 if (regname != 0) /* yank from specified buffer */
3611 {
3612 AppendCharToRedobuff('"');
3613 AppendCharToRedobuff(regname);
3614 }
3615 if (num)
3616 AppendNumberToRedobuff(num);
3617
3618 if (cmd1 != NUL)
3619 AppendCharToRedobuff(cmd1);
3620 if (cmd2 != NUL)
3621 AppendCharToRedobuff(cmd2);
3622 if (cmd3 != NUL)
3623 AppendCharToRedobuff(cmd3);
3624 if (cmd4 != NUL)
3625 AppendCharToRedobuff(cmd4);
3626 if (cmd5 != NUL)
3627 AppendCharToRedobuff(cmd5);
3628}
3629
3630/*
3631 * check for operator active and clear it
3632 *
3633 * return TRUE if operator was active
3634 */
3635 static int
3636checkclearop(oap)
3637 oparg_T *oap;
3638{
3639 if (oap->op_type == OP_NOP)
3640 return FALSE;
3641 clearopbeep(oap);
3642 return TRUE;
3643}
3644
3645/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003646 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003648 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 */
3650 static int
3651checkclearopq(oap)
3652 oparg_T *oap;
3653{
3654 if (oap->op_type == OP_NOP
3655#ifdef FEAT_VISUAL
3656 && !VIsual_active
3657#endif
3658 )
3659 return FALSE;
3660 clearopbeep(oap);
3661 return TRUE;
3662}
3663
3664 static void
3665clearop(oap)
3666 oparg_T *oap;
3667{
3668 oap->op_type = OP_NOP;
3669 oap->regname = 0;
3670 oap->motion_force = NUL;
3671 oap->use_reg_one = FALSE;
3672}
3673
3674 static void
3675clearopbeep(oap)
3676 oparg_T *oap;
3677{
3678 clearop(oap);
3679 beep_flush();
3680}
3681
3682#ifdef FEAT_VISUAL
3683/*
3684 * Remove the shift modifier from a special key.
3685 */
3686 static void
3687unshift_special(cap)
3688 cmdarg_T *cap;
3689{
3690 switch (cap->cmdchar)
3691 {
3692 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3693 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3694 case K_S_UP: cap->cmdchar = K_UP; break;
3695 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3696 case K_S_HOME: cap->cmdchar = K_HOME; break;
3697 case K_S_END: cap->cmdchar = K_END; break;
3698 }
3699 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3700}
3701#endif
3702
3703#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3704/*
3705 * Routines for displaying a partly typed command
3706 */
3707
3708#ifdef FEAT_VISUAL /* need room for size of Visual area */
3709# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3710#else
3711# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3712#endif
3713static char_u showcmd_buf[SHOWCMD_BUFLEN];
3714static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3715static int showcmd_is_clear = TRUE;
3716static int showcmd_visual = FALSE;
3717
3718static void display_showcmd __ARGS((void));
3719
3720 void
3721clear_showcmd()
3722{
3723 if (!p_sc)
3724 return;
3725
3726#ifdef FEAT_VISUAL
3727 if (VIsual_active && !char_avail())
3728 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003729 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 long lines;
3731 colnr_T leftcol, rightcol;
3732 linenr_T top, bot;
3733
3734 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003735 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
3737 top = VIsual.lnum;
3738 bot = curwin->w_cursor.lnum;
3739 }
3740 else
3741 {
3742 top = curwin->w_cursor.lnum;
3743 bot = VIsual.lnum;
3744 }
3745# ifdef FEAT_FOLDING
3746 /* Include closed folds as a whole. */
3747 hasFolding(top, &top, NULL);
3748 hasFolding(bot, NULL, &bot);
3749# endif
3750 lines = bot - top + 1;
3751
3752 if (VIsual_mode == Ctrl_V)
3753 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003754#ifdef FEAT_LINEBREAK
3755 char_u *saved_sbr = p_sbr;
3756
3757 /* Make 'sbr' empty for a moment to get the correct size. */
3758 p_sbr = empty_option;
3759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003761#ifdef FEAT_LINEBREAK
3762 p_sbr = saved_sbr;
3763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3765 (long)(rightcol - leftcol + 1));
3766 }
3767 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3768 sprintf((char *)showcmd_buf, "%ld", lines);
3769 else
Bram Moolenaar81d00072009-04-29 15:41:40 +00003770 sprintf((char *)showcmd_buf, "%ld", (long)(cursor_bot
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 ? curwin->w_cursor.col - VIsual.col
3772 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3773 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3774 showcmd_visual = TRUE;
3775 }
3776 else
3777#endif
3778 {
3779 showcmd_buf[0] = NUL;
3780 showcmd_visual = FALSE;
3781
3782 /* Don't actually display something if there is nothing to clear. */
3783 if (showcmd_is_clear)
3784 return;
3785 }
3786
3787 display_showcmd();
3788}
3789
3790/*
3791 * Add 'c' to string of shown command chars.
3792 * Return TRUE if output has been written (and setcursor() has been called).
3793 */
3794 int
3795add_to_showcmd(c)
3796 int c;
3797{
3798 char_u *p;
3799 int old_len;
3800 int extra_len;
3801 int overflow;
3802#if defined(FEAT_MOUSE)
3803 int i;
3804 static int ignore[] =
3805 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003806# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3808 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 K_IGNORE,
3811 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3812 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3813 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3814 K_MOUSEDOWN, K_MOUSEUP,
3815 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003816 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 0
3818 };
3819#endif
3820
Bram Moolenaar09df3122006-01-23 22:23:09 +00003821 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FALSE;
3823
3824 if (showcmd_visual)
3825 {
3826 showcmd_buf[0] = NUL;
3827 showcmd_visual = FALSE;
3828 }
3829
3830#if defined(FEAT_MOUSE)
3831 /* Ignore keys that are scrollbar updates and mouse clicks */
3832 if (IS_SPECIAL(c))
3833 for (i = 0; ignore[i] != 0; ++i)
3834 if (ignore[i] == c)
3835 return FALSE;
3836#endif
3837
3838 p = transchar(c);
3839 old_len = (int)STRLEN(showcmd_buf);
3840 extra_len = (int)STRLEN(p);
3841 overflow = old_len + extra_len - SHOWCMD_COLS;
3842 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003843 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3844 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 STRCAT(showcmd_buf, p);
3846
3847 if (char_avail())
3848 return FALSE;
3849
3850 display_showcmd();
3851
3852 return TRUE;
3853}
3854
3855 void
3856add_to_showcmd_c(c)
3857 int c;
3858{
3859 if (!add_to_showcmd(c))
3860 setcursor();
3861}
3862
3863/*
3864 * Delete 'len' characters from the end of the shown command.
3865 */
3866 static void
3867del_from_showcmd(len)
3868 int len;
3869{
3870 int old_len;
3871
3872 if (!p_sc)
3873 return;
3874
3875 old_len = (int)STRLEN(showcmd_buf);
3876 if (len > old_len)
3877 len = old_len;
3878 showcmd_buf[old_len - len] = NUL;
3879
3880 if (!char_avail())
3881 display_showcmd();
3882}
3883
3884/*
3885 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3886 * something and there is a partial mapping.
3887 */
3888 void
3889push_showcmd()
3890{
3891 if (p_sc)
3892 STRCPY(old_showcmd_buf, showcmd_buf);
3893}
3894
3895 void
3896pop_showcmd()
3897{
3898 if (!p_sc)
3899 return;
3900
3901 STRCPY(showcmd_buf, old_showcmd_buf);
3902
3903 display_showcmd();
3904}
3905
3906 static void
3907display_showcmd()
3908{
3909 int len;
3910
3911 cursor_off();
3912
3913 len = (int)STRLEN(showcmd_buf);
3914 if (len == 0)
3915 showcmd_is_clear = TRUE;
3916 else
3917 {
3918 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3919 showcmd_is_clear = FALSE;
3920 }
3921
3922 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003923 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3924 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 */
3926 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3927
3928 setcursor(); /* put cursor back where it belongs */
3929}
3930#endif
3931
3932#ifdef FEAT_SCROLLBIND
3933/*
3934 * When "check" is FALSE, prepare for commands that scroll the window.
3935 * When "check" is TRUE, take care of scroll-binding after the window has
3936 * scrolled. Called from normal_cmd() and edit().
3937 */
3938 void
3939do_check_scrollbind(check)
3940 int check;
3941{
3942 static win_T *old_curwin = NULL;
3943 static linenr_T old_topline = 0;
3944#ifdef FEAT_DIFF
3945 static int old_topfill = 0;
3946#endif
3947 static buf_T *old_buf = NULL;
3948 static colnr_T old_leftcol = 0;
3949
3950 if (check && curwin->w_p_scb)
3951 {
3952 /* If a ":syncbind" command was just used, don't scroll, only reset
3953 * the values. */
3954 if (did_syncbind)
3955 did_syncbind = FALSE;
3956 else if (curwin == old_curwin)
3957 {
3958 /*
3959 * Synchronize other windows, as necessary according to
3960 * 'scrollbind'. Don't do this after an ":edit" command, except
3961 * when 'diff' is set.
3962 */
3963 if ((curwin->w_buffer == old_buf
3964#ifdef FEAT_DIFF
3965 || curwin->w_p_diff
3966#endif
3967 )
3968 && (curwin->w_topline != old_topline
3969#ifdef FEAT_DIFF
3970 || curwin->w_topfill != old_topfill
3971#endif
3972 || curwin->w_leftcol != old_leftcol))
3973 {
3974 check_scrollbind(curwin->w_topline - old_topline,
3975 (long)(curwin->w_leftcol - old_leftcol));
3976 }
3977 }
3978 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3979 {
3980 /*
3981 * When switching between windows, make sure that the relative
3982 * vertical offset is valid for the new window. The relative
3983 * offset is invalid whenever another 'scrollbind' window has
3984 * scrolled to a point that would force the current window to
3985 * scroll past the beginning or end of its buffer. When the
3986 * resync is performed, some of the other 'scrollbind' windows may
3987 * need to jump so that the current window's relative position is
3988 * visible on-screen.
3989 */
3990 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3991 }
3992 curwin->w_scbind_pos = curwin->w_topline;
3993 }
3994
3995 old_curwin = curwin;
3996 old_topline = curwin->w_topline;
3997#ifdef FEAT_DIFF
3998 old_topfill = curwin->w_topfill;
3999#endif
4000 old_buf = curwin->w_buffer;
4001 old_leftcol = curwin->w_leftcol;
4002}
4003
4004/*
4005 * Synchronize any windows that have "scrollbind" set, based on the
4006 * number of rows by which the current window has changed
4007 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4008 */
4009 void
4010check_scrollbind(topline_diff, leftcol_diff)
4011 linenr_T topline_diff;
4012 long leftcol_diff;
4013{
4014 int want_ver;
4015 int want_hor;
4016 win_T *old_curwin = curwin;
4017 buf_T *old_curbuf = curbuf;
4018#ifdef FEAT_VISUAL
4019 int old_VIsual_select = VIsual_select;
4020 int old_VIsual_active = VIsual_active;
4021#endif
4022 colnr_T tgt_leftcol = curwin->w_leftcol;
4023 long topline;
4024 long y;
4025
4026 /*
4027 * check 'scrollopt' string for vertical and horizontal scroll options
4028 */
4029 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4030#ifdef FEAT_DIFF
4031 want_ver |= old_curwin->w_p_diff;
4032#endif
4033 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4034
4035 /*
4036 * loop through the scrollbound windows and scroll accordingly
4037 */
4038#ifdef FEAT_VISUAL
4039 VIsual_select = VIsual_active = 0;
4040#endif
4041 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4042 {
4043 curbuf = curwin->w_buffer;
4044 /* skip original window and windows with 'noscrollbind' */
4045 if (curwin != old_curwin && curwin->w_p_scb)
4046 {
4047 /*
4048 * do the vertical scroll
4049 */
4050 if (want_ver)
4051 {
4052#ifdef FEAT_DIFF
4053 if (old_curwin->w_p_diff && curwin->w_p_diff)
4054 {
4055 diff_set_topline(old_curwin, curwin);
4056 }
4057 else
4058#endif
4059 {
4060 curwin->w_scbind_pos += topline_diff;
4061 topline = curwin->w_scbind_pos;
4062 if (topline > curbuf->b_ml.ml_line_count)
4063 topline = curbuf->b_ml.ml_line_count;
4064 if (topline < 1)
4065 topline = 1;
4066
4067 y = topline - curwin->w_topline;
4068 if (y > 0)
4069 scrollup(y, FALSE);
4070 else
4071 scrolldown(-y, FALSE);
4072 }
4073
4074 redraw_later(VALID);
4075 cursor_correct();
4076#ifdef FEAT_WINDOWS
4077 curwin->w_redr_status = TRUE;
4078#endif
4079 }
4080
4081 /*
4082 * do the horizontal scroll
4083 */
4084 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4085 {
4086 curwin->w_leftcol = tgt_leftcol;
4087 leftcol_changed();
4088 }
4089 }
4090 }
4091
4092 /*
4093 * reset current-window
4094 */
4095#ifdef FEAT_VISUAL
4096 VIsual_select = old_VIsual_select;
4097 VIsual_active = old_VIsual_active;
4098#endif
4099 curwin = old_curwin;
4100 curbuf = old_curbuf;
4101}
4102#endif /* #ifdef FEAT_SCROLLBIND */
4103
4104/*
4105 * Command character that's ignored.
4106 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004107 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 static void
4110nv_ignore(cap)
4111 cmdarg_T *cap;
4112{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004113 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004117 * Command character that doesn't do anything, but unlike nv_ignore() does
4118 * start edit(). Used for "startinsert" executed while starting up.
4119 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004120 static void
4121nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004122 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004123{
4124}
4125
4126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * Command character doesn't exist.
4128 */
4129 static void
4130nv_error(cap)
4131 cmdarg_T *cap;
4132{
4133 clearopbeep(cap->oap);
4134}
4135
4136/*
4137 * <Help> and <F1> commands.
4138 */
4139 static void
4140nv_help(cap)
4141 cmdarg_T *cap;
4142{
4143 if (!checkclearopq(cap->oap))
4144 ex_help(NULL);
4145}
4146
4147/*
4148 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4149 */
4150 static void
4151nv_addsub(cap)
4152 cmdarg_T *cap;
4153{
4154 if (!checkclearopq(cap->oap)
4155 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4156 prep_redo_cmd(cap);
4157}
4158
4159/*
4160 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4161 */
4162 static void
4163nv_page(cap)
4164 cmdarg_T *cap;
4165{
4166 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004167 {
4168#ifdef FEAT_WINDOWS
4169 if (mod_mask & MOD_MASK_CTRL)
4170 {
4171 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4172 if (cap->arg == BACKWARD)
4173 goto_tabpage(-(int)cap->count1);
4174 else
4175 goto_tabpage((int)cap->count0);
4176 }
4177 else
4178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181}
4182
4183/*
4184 * Implementation of "gd" and "gD" command.
4185 */
4186 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004187nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004188 oparg_T *oap;
4189 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004190 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 char_u *ptr;
4194
Bram Moolenaard9d30582005-05-18 22:10:28 +00004195 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004196 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004198#ifdef FEAT_FOLDING
4199 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4200 foldOpenCursor();
4201#endif
4202}
4203
4204/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004205 * Search for variable declaration of "ptr[len]".
4206 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4207 * current file ("gD").
4208 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004209 * Return FAIL when not found.
4210 */
4211 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004212find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004213 char_u *ptr;
4214 int len;
4215 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004216 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 int searchflags; /* flags passed to searchit() */
4218{
4219 char_u *pat;
4220 pos_T old_pos;
4221 pos_T par_pos;
4222 pos_T found_pos;
4223 int t;
4224 int save_p_ws;
4225 int save_p_scs;
4226 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004227 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004228
4229 if ((pat = alloc(len + 7)) == NULL)
4230 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004231
4232 /* Put "\V" before the pattern to avoid that the special meaning of "."
4233 * and "~" causes trouble. */
4234 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4235 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 old_pos = curwin->w_cursor;
4237 save_p_ws = p_ws;
4238 save_p_scs = p_scs;
4239 p_ws = FALSE; /* don't wrap around end of file now */
4240 p_scs = FALSE; /* don't switch ignorecase off now */
4241
4242 /*
4243 * With "gD" go to line 1.
4244 * With "gd" Search back for the start of the current function, then go
4245 * back until a blank line. If this fails go to line 1.
4246 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004247 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 setpcmark(); /* Set in findpar() otherwise */
4250 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004251 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 else
4254 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004255 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4257 --curwin->w_cursor.lnum;
4258 }
4259 curwin->w_cursor.col = 0;
4260
4261 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004262 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263 for (;;)
4264 {
4265 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004266 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 if (curwin->w_cursor.lnum >= old_pos.lnum)
4268 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004269
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004270 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 {
4272 pos_T *pos;
4273
4274 /* Check that the block the match is in doesn't end before the
4275 * position where we started the search from. */
4276 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4277 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4278 && pos->lnum < old_pos.lnum)
4279 continue;
4280 }
4281
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004282 if (t == FAIL)
4283 {
4284 /* If we previously found a valid position, use it. */
4285 if (found_pos.lnum != 0)
4286 {
4287 curwin->w_cursor = found_pos;
4288 t = OK;
4289 }
4290 break;
4291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4294 {
4295 /* Ignore this line, continue at start of next line. */
4296 ++curwin->w_cursor.lnum;
4297 curwin->w_cursor.col = 0;
4298 continue;
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 if (!locally) /* global search: use first match found */
4302 break;
4303 if (curwin->w_cursor.lnum >= par_pos.lnum)
4304 {
4305 /* If we previously found a valid position, use it. */
4306 if (found_pos.lnum != 0)
4307 curwin->w_cursor = found_pos;
4308 break;
4309 }
4310
4311 /* For finding a local variable and the match is before the "{" search
4312 * to find a later match. For K&R style function declarations this
4313 * skips the function header without types. */
4314 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316
4317 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004319 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 curwin->w_cursor = old_pos;
4321 }
4322 else
4323 {
4324 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* "n" searches forward now */
4326 reset_search_dir();
4327 }
4328
4329 vim_free(pat);
4330 p_ws = save_p_ws;
4331 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332
4333 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335
4336/*
4337 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4338 * lines rather than lines in the file.
4339 * 'dist' must be positive.
4340 *
4341 * Return OK if able to move cursor, FAIL otherwise.
4342 */
4343 static int
4344nv_screengo(oap, dir, dist)
4345 oparg_T *oap;
4346 int dir;
4347 long dist;
4348{
4349 int linelen = linetabsize(ml_get_curline());
4350 int retval = OK;
4351 int atend = FALSE;
4352 int n;
4353 int col_off1; /* margin offset for first screen line */
4354 int col_off2; /* margin offset for wrapped screen line */
4355 int width1; /* text width for first screen line */
4356 int width2; /* test width for wrapped screen line */
4357
4358 oap->motion_type = MCHAR;
4359 oap->inclusive = FALSE;
4360
4361 col_off1 = curwin_col_off();
4362 col_off2 = col_off1 - curwin_col_off2();
4363 width1 = W_WIDTH(curwin) - col_off1;
4364 width2 = W_WIDTH(curwin) - col_off2;
4365
4366#ifdef FEAT_VERTSPLIT
4367 if (curwin->w_width != 0)
4368 {
4369#endif
4370 /*
4371 * Instead of sticking at the last character of the buffer line we
4372 * try to stick in the last column of the screen.
4373 */
4374 if (curwin->w_curswant == MAXCOL)
4375 {
4376 atend = TRUE;
4377 validate_virtcol();
4378 if (width1 <= 0)
4379 curwin->w_curswant = 0;
4380 else
4381 {
4382 curwin->w_curswant = width1 - 1;
4383 if (curwin->w_virtcol > curwin->w_curswant)
4384 curwin->w_curswant += ((curwin->w_virtcol
4385 - curwin->w_curswant - 1) / width2 + 1) * width2;
4386 }
4387 }
4388 else
4389 {
4390 if (linelen > width1)
4391 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4392 else
4393 n = width1;
4394 if (curwin->w_curswant > (colnr_T)n + 1)
4395 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4396 * width2;
4397 }
4398
4399 while (dist--)
4400 {
4401 if (dir == BACKWARD)
4402 {
4403 if ((long)curwin->w_curswant >= width2)
4404 /* move back within line */
4405 curwin->w_curswant -= width2;
4406 else
4407 {
4408 /* to previous line */
4409 if (curwin->w_cursor.lnum == 1)
4410 {
4411 retval = FAIL;
4412 break;
4413 }
4414 --curwin->w_cursor.lnum;
4415#ifdef FEAT_FOLDING
4416 /* Move to the start of a closed fold. Don't do that when
4417 * 'foldopen' contains "all": it will open in a moment. */
4418 if (!(fdo_flags & FDO_ALL))
4419 (void)hasFolding(curwin->w_cursor.lnum,
4420 &curwin->w_cursor.lnum, NULL);
4421#endif
4422 linelen = linetabsize(ml_get_curline());
4423 if (linelen > width1)
4424 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4425 + 1) * width2;
4426 }
4427 }
4428 else /* dir == FORWARD */
4429 {
4430 if (linelen > width1)
4431 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4432 else
4433 n = width1;
4434 if (curwin->w_curswant + width2 < (colnr_T)n)
4435 /* move forward within line */
4436 curwin->w_curswant += width2;
4437 else
4438 {
4439 /* to next line */
4440#ifdef FEAT_FOLDING
4441 /* Move to the end of a closed fold. */
4442 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4443 &curwin->w_cursor.lnum);
4444#endif
4445 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4446 {
4447 retval = FAIL;
4448 break;
4449 }
4450 curwin->w_cursor.lnum++;
4451 curwin->w_curswant %= width2;
4452 }
4453 }
4454 }
4455#ifdef FEAT_VERTSPLIT
4456 }
4457#endif
4458
4459 coladvance(curwin->w_curswant);
4460
4461#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4462 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4463 {
4464 /*
4465 * Check for landing on a character that got split at the end of the
4466 * last line. We want to advance a screenline, not end up in the same
4467 * screenline or move two screenlines.
4468 */
4469 validate_virtcol();
4470 if (curwin->w_virtcol > curwin->w_curswant
4471 && (curwin->w_curswant < (colnr_T)width1
4472 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4473 : ((curwin->w_curswant - width1) % width2
4474 > (colnr_T)width2 / 2)))
4475 --curwin->w_cursor.col;
4476 }
4477#endif
4478
4479 if (atend)
4480 curwin->w_curswant = MAXCOL; /* stick in the last column */
4481
4482 return retval;
4483}
4484
4485#ifdef FEAT_MOUSE
4486/*
4487 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4488 * when Shift or Ctrl is used.
4489 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4490 */
4491 static void
4492nv_mousescroll(cap)
4493 cmdarg_T *cap;
4494{
4495# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004496 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 /* Currently we only get the mouse coordinates in the GUI. */
4499 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4500 {
4501 int row, col;
4502
4503 row = mouse_row;
4504 col = mouse_col;
4505
4506 /* find the window at the pointer coordinates */
4507 curwin = mouse_find_win(&row, &col);
4508 curbuf = curwin->w_buffer;
4509 }
4510# endif
4511
4512 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4513 {
4514 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4515 }
4516 else
4517 {
4518 cap->count1 = 3;
4519 cap->count0 = 3;
4520 nv_scroll_line(cap);
4521 }
4522
4523# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4524 curwin->w_redr_status = TRUE;
4525
4526 curwin = old_curwin;
4527 curbuf = curwin->w_buffer;
4528# endif
4529}
4530
4531/*
4532 * Mouse clicks and drags.
4533 */
4534 static void
4535nv_mouse(cap)
4536 cmdarg_T *cap;
4537{
4538 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4539}
4540#endif
4541
4542/*
4543 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4544 * cap->arg must be TRUE for CTRL-E.
4545 */
4546 static void
4547nv_scroll_line(cap)
4548 cmdarg_T *cap;
4549{
4550 if (!checkclearop(cap->oap))
4551 scroll_redraw(cap->arg, cap->count1);
4552}
4553
4554/*
4555 * Scroll "count" lines up or down, and redraw.
4556 */
4557 void
4558scroll_redraw(up, count)
4559 int up;
4560 long count;
4561{
4562 linenr_T prev_topline = curwin->w_topline;
4563#ifdef FEAT_DIFF
4564 int prev_topfill = curwin->w_topfill;
4565#endif
4566 linenr_T prev_lnum = curwin->w_cursor.lnum;
4567
4568 if (up)
4569 scrollup(count, TRUE);
4570 else
4571 scrolldown(count, TRUE);
4572 if (p_so)
4573 {
4574 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4575 * valid, otherwise the screen jumps back at the end of the file. */
4576 cursor_correct();
4577 check_cursor_moved(curwin);
4578 curwin->w_valid |= VALID_TOPLINE;
4579
4580 /* If moved back to where we were, at least move the cursor, otherwise
4581 * we get stuck at one position. Don't move the cursor up if the
4582 * first line of the buffer is already on the screen */
4583 while (curwin->w_topline == prev_topline
4584#ifdef FEAT_DIFF
4585 && curwin->w_topfill == prev_topfill
4586#endif
4587 )
4588 {
4589 if (up)
4590 {
4591 if (curwin->w_cursor.lnum > prev_lnum
4592 || cursor_down(1L, FALSE) == FAIL)
4593 break;
4594 }
4595 else
4596 {
4597 if (curwin->w_cursor.lnum < prev_lnum
4598 || prev_topline == 1L
4599 || cursor_up(1L, FALSE) == FAIL)
4600 break;
4601 }
4602 /* Mark w_topline as valid, otherwise the screen jumps back at the
4603 * end of the file. */
4604 check_cursor_moved(curwin);
4605 curwin->w_valid |= VALID_TOPLINE;
4606 }
4607 }
4608 if (curwin->w_cursor.lnum != prev_lnum)
4609 coladvance(curwin->w_curswant);
4610 redraw_later(VALID);
4611}
4612
4613/*
4614 * Commands that start with "z".
4615 */
4616 static void
4617nv_zet(cap)
4618 cmdarg_T *cap;
4619{
4620 long n;
4621 colnr_T col;
4622 int nchar = cap->nchar;
4623#ifdef FEAT_FOLDING
4624 long old_fdl = curwin->w_p_fdl;
4625 int old_fen = curwin->w_p_fen;
4626#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004627#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004628 int undo = FALSE;
4629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 if (VIM_ISDIGIT(nchar))
4632 {
4633 /*
4634 * "z123{nchar}": edit the count before obtaining {nchar}
4635 */
4636 if (checkclearop(cap->oap))
4637 return;
4638 n = nchar - '0';
4639 for (;;)
4640 {
4641#ifdef USE_ON_FLY_SCROLL
4642 dont_scroll = TRUE; /* disallow scrolling here */
4643#endif
4644 ++no_mapping;
4645 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004646 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 --no_mapping;
4649 --allow_keys;
4650#ifdef FEAT_CMDL_INFO
4651 (void)add_to_showcmd(nchar);
4652#endif
4653 if (nchar == K_DEL || nchar == K_KDEL)
4654 n /= 10;
4655 else if (VIM_ISDIGIT(nchar))
4656 n = n * 10 + (nchar - '0');
4657 else if (nchar == CAR)
4658 {
4659#ifdef FEAT_GUI
4660 need_mouse_correct = TRUE;
4661#endif
4662 win_setheight((int)n);
4663 break;
4664 }
4665 else if (nchar == 'l'
4666 || nchar == 'h'
4667 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004668 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 cap->count1 = n ? n * cap->count1 : cap->count1;
4671 goto dozet;
4672 }
4673 else
4674 {
4675 clearopbeep(cap->oap);
4676 break;
4677 }
4678 }
4679 cap->oap->op_type = OP_NOP;
4680 return;
4681 }
4682
4683dozet:
4684 if (
4685#ifdef FEAT_FOLDING
4686 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4687 * and "zC" only in Visual mode. "zj" and "zk" are motion
4688 * commands. */
4689 cap->nchar != 'f' && cap->nchar != 'F'
4690 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4691 && cap->nchar != 'j' && cap->nchar != 'k'
4692 &&
4693#endif
4694 checkclearop(cap->oap))
4695 return;
4696
4697 /*
4698 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4699 * If line number given, set cursor.
4700 */
4701 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4702 && cap->count0
4703 && cap->count0 != curwin->w_cursor.lnum)
4704 {
4705 setpcmark();
4706 if (cap->count0 > curbuf->b_ml.ml_line_count)
4707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4708 else
4709 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004710 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712
4713 switch (nchar)
4714 {
4715 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4716 case '+':
4717 if (cap->count0 == 0)
4718 {
4719 /* No count given: put cursor at the line below screen */
4720 validate_botline(); /* make sure w_botline is valid */
4721 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4723 else
4724 curwin->w_cursor.lnum = curwin->w_botline;
4725 }
4726 /* FALLTHROUGH */
4727 case NL:
4728 case CAR:
4729 case K_KENTER:
4730 beginline(BL_WHITE | BL_FIX);
4731 /* FALLTHROUGH */
4732
4733 case 't': scroll_cursor_top(0, TRUE);
4734 redraw_later(VALID);
4735 break;
4736
4737 /* "z." and "zz": put cursor in middle of screen */
4738 case '.': beginline(BL_WHITE | BL_FIX);
4739 /* FALLTHROUGH */
4740
4741 case 'z': scroll_cursor_halfway(TRUE);
4742 redraw_later(VALID);
4743 break;
4744
4745 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4746 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4747 * when <count> is at bottom of window, and puts that one at
4748 * bottom of window. */
4749 if (cap->count0 != 0)
4750 {
4751 scroll_cursor_bot(0, TRUE);
4752 curwin->w_cursor.lnum = curwin->w_topline;
4753 }
4754 else if (curwin->w_topline == 1)
4755 curwin->w_cursor.lnum = 1;
4756 else
4757 curwin->w_cursor.lnum = curwin->w_topline - 1;
4758 /* FALLTHROUGH */
4759 case '-':
4760 beginline(BL_WHITE | BL_FIX);
4761 /* FALLTHROUGH */
4762
4763 case 'b': scroll_cursor_bot(0, TRUE);
4764 redraw_later(VALID);
4765 break;
4766
4767 /* "zH" - scroll screen right half-page */
4768 case 'H':
4769 cap->count1 *= W_WIDTH(curwin) / 2;
4770 /* FALLTHROUGH */
4771
4772 /* "zh" - scroll screen to the right */
4773 case 'h':
4774 case K_LEFT:
4775 if (!curwin->w_p_wrap)
4776 {
4777 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4778 curwin->w_leftcol = 0;
4779 else
4780 curwin->w_leftcol -= (colnr_T)cap->count1;
4781 leftcol_changed();
4782 }
4783 break;
4784
4785 /* "zL" - scroll screen left half-page */
4786 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4787 /* FALLTHROUGH */
4788
4789 /* "zl" - scroll screen to the left */
4790 case 'l':
4791 case K_RIGHT:
4792 if (!curwin->w_p_wrap)
4793 {
4794 /* scroll the window left */
4795 curwin->w_leftcol += (colnr_T)cap->count1;
4796 leftcol_changed();
4797 }
4798 break;
4799
4800 /* "zs" - scroll screen, cursor at the start */
4801 case 's': if (!curwin->w_p_wrap)
4802 {
4803#ifdef FEAT_FOLDING
4804 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4805 col = 0; /* like the cursor is in col 0 */
4806 else
4807#endif
4808 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4809 if ((long)col > p_siso)
4810 col -= p_siso;
4811 else
4812 col = 0;
4813 if (curwin->w_leftcol != col)
4814 {
4815 curwin->w_leftcol = col;
4816 redraw_later(NOT_VALID);
4817 }
4818 }
4819 break;
4820
4821 /* "ze" - scroll screen, cursor at the end */
4822 case 'e': if (!curwin->w_p_wrap)
4823 {
4824#ifdef FEAT_FOLDING
4825 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4826 col = 0; /* like the cursor is in col 0 */
4827 else
4828#endif
4829 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4830 n = W_WIDTH(curwin) - curwin_col_off();
4831 if ((long)col + p_siso < n)
4832 col = 0;
4833 else
4834 col = col + p_siso - n + 1;
4835 if (curwin->w_leftcol != col)
4836 {
4837 curwin->w_leftcol = col;
4838 redraw_later(NOT_VALID);
4839 }
4840 }
4841 break;
4842
4843#ifdef FEAT_FOLDING
4844 /* "zF": create fold command */
4845 /* "zf": create fold operator */
4846 case 'F':
4847 case 'f': if (foldManualAllowed(TRUE))
4848 {
4849 cap->nchar = 'f';
4850 nv_operator(cap);
4851 curwin->w_p_fen = TRUE;
4852
4853 /* "zF" is like "zfzf" */
4854 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4855 {
4856 nv_operator(cap);
4857 finish_op = TRUE;
4858 }
4859 }
4860 else
4861 clearopbeep(cap->oap);
4862 break;
4863
4864 /* "zd": delete fold at cursor */
4865 /* "zD": delete fold at cursor recursively */
4866 case 'd':
4867 case 'D': if (foldManualAllowed(FALSE))
4868 {
4869 if (VIsual_active)
4870 nv_operator(cap);
4871 else
4872 deleteFold(curwin->w_cursor.lnum,
4873 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4874 }
4875 break;
4876
4877 /* "zE": erease all folds */
4878 case 'E': if (foldmethodIsManual(curwin))
4879 {
4880 clearFolding(curwin);
4881 changed_window_setting();
4882 }
4883 else if (foldmethodIsMarker(curwin))
4884 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4885 TRUE, FALSE);
4886 else
4887 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4888 break;
4889
4890 /* "zn": fold none: reset 'foldenable' */
4891 case 'n': curwin->w_p_fen = FALSE;
4892 break;
4893
4894 /* "zN": fold Normal: set 'foldenable' */
4895 case 'N': curwin->w_p_fen = TRUE;
4896 break;
4897
4898 /* "zi": invert folding: toggle 'foldenable' */
4899 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4900 break;
4901
4902 /* "za": open closed fold or close open fold at cursor */
4903 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4904 openFold(curwin->w_cursor.lnum, cap->count1);
4905 else
4906 {
4907 closeFold(curwin->w_cursor.lnum, cap->count1);
4908 curwin->w_p_fen = TRUE;
4909 }
4910 break;
4911
4912 /* "zA": open fold at cursor recursively */
4913 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4914 openFoldRecurse(curwin->w_cursor.lnum);
4915 else
4916 {
4917 closeFoldRecurse(curwin->w_cursor.lnum);
4918 curwin->w_p_fen = TRUE;
4919 }
4920 break;
4921
4922 /* "zo": open fold at cursor or Visual area */
4923 case 'o': if (VIsual_active)
4924 nv_operator(cap);
4925 else
4926 openFold(curwin->w_cursor.lnum, cap->count1);
4927 break;
4928
4929 /* "zO": open fold recursively */
4930 case 'O': if (VIsual_active)
4931 nv_operator(cap);
4932 else
4933 openFoldRecurse(curwin->w_cursor.lnum);
4934 break;
4935
4936 /* "zc": close fold at cursor or Visual area */
4937 case 'c': if (VIsual_active)
4938 nv_operator(cap);
4939 else
4940 closeFold(curwin->w_cursor.lnum, cap->count1);
4941 curwin->w_p_fen = TRUE;
4942 break;
4943
4944 /* "zC": close fold recursively */
4945 case 'C': if (VIsual_active)
4946 nv_operator(cap);
4947 else
4948 closeFoldRecurse(curwin->w_cursor.lnum);
4949 curwin->w_p_fen = TRUE;
4950 break;
4951
4952 /* "zv": open folds at the cursor */
4953 case 'v': foldOpenCursor();
4954 break;
4955
4956 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4957 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02004958 curwin->w_foldinvalid = TRUE; /* recompute folds */
4959 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 foldOpenCursor();
4961 break;
4962
4963 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4964 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02004965 curwin->w_foldinvalid = TRUE; /* recompute folds */
4966 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 break;
4968
4969 /* "zm": fold more */
4970 case 'm': if (curwin->w_p_fdl > 0)
4971 --curwin->w_p_fdl;
4972 old_fdl = -1; /* force an update */
4973 curwin->w_p_fen = TRUE;
4974 break;
4975
4976 /* "zM": close all folds */
4977 case 'M': curwin->w_p_fdl = 0;
4978 old_fdl = -1; /* force an update */
4979 curwin->w_p_fen = TRUE;
4980 break;
4981
4982 /* "zr": reduce folding */
4983 case 'r': ++curwin->w_p_fdl;
4984 break;
4985
4986 /* "zR": open all folds */
4987 case 'R': curwin->w_p_fdl = getDeepestNesting();
4988 old_fdl = -1; /* force an update */
4989 break;
4990
4991 case 'j': /* "zj" move to next fold downwards */
4992 case 'k': /* "zk" move to next fold upwards */
4993 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4994 cap->count1) == FAIL)
4995 clearopbeep(cap->oap);
4996 break;
4997
4998#endif /* FEAT_FOLDING */
4999
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005000#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005001 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5002 ++no_mapping;
5003 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005004 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005005 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005006 --no_mapping;
5007 --allow_keys;
5008#ifdef FEAT_CMDL_INFO
5009 (void)add_to_showcmd(nchar);
5010#endif
5011 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5012 {
5013 clearopbeep(cap->oap);
5014 break;
5015 }
5016 undo = TRUE;
5017 /*FALLTHROUGH*/
5018
Bram Moolenaarb765d632005-06-07 21:00:02 +00005019 case 'g': /* "zg": add good word to word list */
5020 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005021 case 'G': /* "zG": add good word to temp word list */
5022 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005023 {
5024 char_u *ptr = NULL;
5025 int len;
5026
5027 if (checkclearop(cap->oap))
5028 break;
5029# ifdef FEAT_VISUAL
5030 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5031 == FAIL)
5032 return;
5033# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005034 if (ptr == NULL)
5035 {
5036 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005037
5038 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005039 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005040 if (len != 0 && curwin->w_cursor.col <= pos.col)
5041 ptr = ml_get_pos(&curwin->w_cursor);
5042 curwin->w_cursor = pos;
5043 }
5044
Bram Moolenaarb765d632005-06-07 21:00:02 +00005045 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5046 FIND_IDENT)) == 0)
5047 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005048 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005049 (nchar == 'G' || nchar == 'W')
5050 ? 0 : (int)cap->count1,
5051 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005052 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005053 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005054
Bram Moolenaar43abc522005-12-10 20:15:02 +00005055 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005056 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005057 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005059#endif
5060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 default: clearopbeep(cap->oap);
5062 }
5063
5064#ifdef FEAT_FOLDING
5065 /* Redraw when 'foldenable' changed */
5066 if (old_fen != curwin->w_p_fen)
5067 {
5068# ifdef FEAT_DIFF
5069 win_T *wp;
5070
5071 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5072 {
5073 /* Adjust 'foldenable' in diff-synced windows. */
5074 FOR_ALL_WINDOWS(wp)
5075 {
5076 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5077 {
5078 wp->w_p_fen = curwin->w_p_fen;
5079 changed_window_setting_win(wp);
5080 }
5081 }
5082 }
5083# endif
5084 changed_window_setting();
5085 }
5086
5087 /* Redraw when 'foldlevel' changed. */
5088 if (old_fdl != curwin->w_p_fdl)
5089 newFoldLevel();
5090#endif
5091}
5092
5093#ifdef FEAT_GUI
5094/*
5095 * Vertical scrollbar movement.
5096 */
5097 static void
5098nv_ver_scrollbar(cap)
5099 cmdarg_T *cap;
5100{
5101 if (cap->oap->op_type != OP_NOP)
5102 clearopbeep(cap->oap);
5103
5104 /* Even if an operator was pending, we still want to scroll */
5105 gui_do_scroll();
5106}
5107
5108/*
5109 * Horizontal scrollbar movement.
5110 */
5111 static void
5112nv_hor_scrollbar(cap)
5113 cmdarg_T *cap;
5114{
5115 if (cap->oap->op_type != OP_NOP)
5116 clearopbeep(cap->oap);
5117
5118 /* Even if an operator was pending, we still want to scroll */
5119 gui_do_horiz_scroll();
5120}
5121#endif
5122
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005123#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005124/*
5125 * Click in GUI tab.
5126 */
5127 static void
5128nv_tabline(cap)
5129 cmdarg_T *cap;
5130{
5131 if (cap->oap->op_type != OP_NOP)
5132 clearopbeep(cap->oap);
5133
5134 /* Even if an operator was pending, we still want to jump tabs. */
5135 goto_tabpage(current_tab);
5136}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005137
5138/*
5139 * Selected item in tab line menu.
5140 */
5141 static void
5142nv_tabmenu(cap)
5143 cmdarg_T *cap;
5144{
5145 if (cap->oap->op_type != OP_NOP)
5146 clearopbeep(cap->oap);
5147
5148 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005149 handle_tabmenu();
5150}
5151
5152/*
5153 * Handle selecting an item of the GUI tab line menu.
5154 * Used in Normal and Insert mode.
5155 */
5156 void
5157handle_tabmenu()
5158{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005159 switch (current_tabmenu)
5160 {
5161 case TABLINE_MENU_CLOSE:
5162 if (current_tab == 0)
5163 do_cmdline_cmd((char_u *)"tabclose");
5164 else
5165 {
5166 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5167 current_tab);
5168 do_cmdline_cmd(IObuff);
5169 }
5170 break;
5171
5172 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005173 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5174 current_tab > 0 ? current_tab - 1 : 999);
5175 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005176 break;
5177
5178 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005179 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5180 current_tab > 0 ? current_tab - 1 : 999);
5181 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005182 break;
5183 }
5184}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005185#endif
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187/*
5188 * "Q" command.
5189 */
5190 static void
5191nv_exmode(cap)
5192 cmdarg_T *cap;
5193{
5194 /*
5195 * Ignore 'Q' in Visual mode, just give a beep.
5196 */
5197#ifdef FEAT_VISUAL
5198 if (VIsual_active)
5199 vim_beep();
5200 else
5201#endif
5202 if (!checkclearop(cap->oap))
5203 do_exmode(FALSE);
5204}
5205
5206/*
5207 * Handle a ":" command.
5208 */
5209 static void
5210nv_colon(cap)
5211 cmdarg_T *cap;
5212{
5213 int old_p_im;
5214
5215#ifdef FEAT_VISUAL
5216 if (VIsual_active)
5217 nv_operator(cap);
5218 else
5219#endif
5220 {
5221 if (cap->oap->op_type != OP_NOP)
5222 {
5223 /* Using ":" as a movement is characterwise exclusive. */
5224 cap->oap->motion_type = MCHAR;
5225 cap->oap->inclusive = FALSE;
5226 }
5227 else if (cap->count0)
5228 {
5229 /* translate "count:" into ":.,.+(count - 1)" */
5230 stuffcharReadbuff('.');
5231 if (cap->count0 > 1)
5232 {
5233 stuffReadbuff((char_u *)",.+");
5234 stuffnumReadbuff((long)cap->count0 - 1L);
5235 }
5236 }
5237
5238 /* When typing, don't type below an old message */
5239 if (KeyTyped)
5240 compute_cmdrow();
5241
5242 old_p_im = p_im;
5243
5244 /* get a command line and execute it */
5245 do_cmdline(NULL, getexline, NULL,
5246 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5247
5248 /* If 'insertmode' changed, enter or exit Insert mode */
5249 if (p_im != old_p_im)
5250 {
5251 if (p_im)
5252 restart_edit = 'i';
5253 else
5254 restart_edit = 0;
5255 }
5256
5257 /* The start of the operator may have become invalid by the Ex
5258 * command. */
5259 if (cap->oap->op_type != OP_NOP
5260 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5261 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005262 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 clearopbeep(cap->oap);
5264 }
5265}
5266
5267/*
5268 * Handle CTRL-G command.
5269 */
5270 static void
5271nv_ctrlg(cap)
5272 cmdarg_T *cap;
5273{
5274#ifdef FEAT_VISUAL
5275 if (VIsual_active) /* toggle Selection/Visual mode */
5276 {
5277 VIsual_select = !VIsual_select;
5278 showmode();
5279 }
5280 else
5281#endif
5282 if (!checkclearop(cap->oap))
5283 /* print full name if count given or :cd used */
5284 fileinfo((int)cap->count0, FALSE, TRUE);
5285}
5286
5287/*
5288 * Handle CTRL-H <Backspace> command.
5289 */
5290 static void
5291nv_ctrlh(cap)
5292 cmdarg_T *cap;
5293{
5294#ifdef FEAT_VISUAL
5295 if (VIsual_active && VIsual_select)
5296 {
5297 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5298 v_visop(cap);
5299 }
5300 else
5301#endif
5302 nv_left(cap);
5303}
5304
5305/*
5306 * CTRL-L: clear screen and redraw.
5307 */
5308 static void
5309nv_clear(cap)
5310 cmdarg_T *cap;
5311{
5312 if (!checkclearop(cap->oap))
5313 {
5314#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5315 /*
5316 * Right now, the BeBox doesn't seem to have an easy way to detect
5317 * window resizing, so we cheat and make the user detect it
5318 * manually with CTRL-L instead
5319 */
5320 ui_get_shellsize();
5321#endif
5322#ifdef FEAT_SYN_HL
5323 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005324 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#endif
5326 redraw_later(CLEAR);
5327 }
5328}
5329
5330/*
5331 * CTRL-O: In Select mode: switch to Visual mode for one command.
5332 * Otherwise: Go to older pcmark.
5333 */
5334 static void
5335nv_ctrlo(cap)
5336 cmdarg_T *cap;
5337{
5338#ifdef FEAT_VISUAL
5339 if (VIsual_active && VIsual_select)
5340 {
5341 VIsual_select = FALSE;
5342 showmode();
5343 restart_VIsual_select = 2; /* restart Select mode later */
5344 }
5345 else
5346#endif
5347 {
5348 cap->count1 = -cap->count1;
5349 nv_pcmark(cap);
5350 }
5351}
5352
5353/*
5354 * CTRL-^ command, short for ":e #"
5355 */
5356 static void
5357nv_hat(cap)
5358 cmdarg_T *cap;
5359{
5360 if (!checkclearopq(cap->oap))
5361 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5362 GETF_SETMARK|GETF_ALT, FALSE);
5363}
5364
5365/*
5366 * "Z" commands.
5367 */
5368 static void
5369nv_Zet(cap)
5370 cmdarg_T *cap;
5371{
5372 if (!checkclearopq(cap->oap))
5373 {
5374 switch (cap->nchar)
5375 {
5376 /* "ZZ": equivalent to ":x". */
5377 case 'Z': do_cmdline_cmd((char_u *)"x");
5378 break;
5379
5380 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5381 case 'Q': do_cmdline_cmd((char_u *)"q!");
5382 break;
5383
5384 default: clearopbeep(cap->oap);
5385 }
5386 }
5387}
5388
5389#if defined(FEAT_WINDOWS) || defined(PROTO)
5390/*
5391 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5392 */
5393 void
5394do_nv_ident(c1, c2)
5395 int c1;
5396 int c2;
5397{
5398 oparg_T oa;
5399 cmdarg_T ca;
5400
5401 clear_oparg(&oa);
5402 vim_memset(&ca, 0, sizeof(ca));
5403 ca.oap = &oa;
5404 ca.cmdchar = c1;
5405 ca.nchar = c2;
5406 nv_ident(&ca);
5407}
5408#endif
5409
5410/*
5411 * Handle the commands that use the word under the cursor.
5412 * [g] CTRL-] :ta to current identifier
5413 * [g] 'K' run program for current identifier
5414 * [g] '*' / to current identifier or string
5415 * [g] '#' ? to current identifier or string
5416 * g ']' :tselect for current identifier
5417 */
5418 static void
5419nv_ident(cap)
5420 cmdarg_T *cap;
5421{
5422 char_u *ptr = NULL;
5423 char_u *buf;
5424 char_u *p;
5425 char_u *kp; /* value of 'keywordprg' */
5426 int kp_help; /* 'keywordprg' is ":help" */
5427 int n = 0; /* init for GCC */
5428 int cmdchar;
5429 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005430 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 char_u *aux_ptr;
5432 int isman;
5433 int isman_s;
5434
5435 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5436 {
5437 cmdchar = cap->nchar;
5438 g_cmd = TRUE;
5439 }
5440 else
5441 {
5442 cmdchar = cap->cmdchar;
5443 g_cmd = FALSE;
5444 }
5445
5446 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5447 cmdchar = '#';
5448
5449 /*
5450 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5451 */
5452 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5453 {
5454#ifdef FEAT_VISUAL
5455 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5456 return;
5457#endif
5458 if (checkclearopq(cap->oap))
5459 return;
5460 }
5461
5462 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5463 (cmdchar == '*' || cmdchar == '#')
5464 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5465 {
5466 clearop(cap->oap);
5467 return;
5468 }
5469
5470 /* Allocate buffer to put the command in. Inserting backslashes can
5471 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5472 * and some numbers. */
5473 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5474 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5475 || STRCMP(kp, ":help") == 0);
5476 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5477 if (buf == NULL)
5478 return;
5479 buf[0] = NUL;
5480
5481 switch (cmdchar)
5482 {
5483 case '*':
5484 case '#':
5485 /*
5486 * Put cursor at start of word, makes search skip the word
5487 * under the cursor.
5488 * Call setpcmark() first, so "*``" puts the cursor back where
5489 * it was.
5490 */
5491 setpcmark();
5492 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5493
5494 if (!g_cmd && vim_iswordp(ptr))
5495 STRCPY(buf, "\\<");
5496 no_smartcase = TRUE; /* don't use 'smartcase' now */
5497 break;
5498
5499 case 'K':
5500 if (kp_help)
5501 STRCPY(buf, "he! ");
5502 else
5503 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005504 /* An external command will probably use an argument starting
5505 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005506 while (*ptr == '-' && n > 0)
5507 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005508 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005509 --n;
5510 }
5511 if (n == 0)
5512 {
5513 EMSG(_(e_noident)); /* found dashes only */
5514 vim_free(buf);
5515 return;
5516 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 /* When a count is given, turn it into a range. Is this
5519 * really what we want? */
5520 isman = (STRCMP(kp, "man") == 0);
5521 isman_s = (STRCMP(kp, "man -s") == 0);
5522 if (cap->count0 != 0 && !(isman || isman_s))
5523 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5524
5525 STRCAT(buf, "! ");
5526 if (cap->count0 == 0 && isman_s)
5527 STRCAT(buf, "man");
5528 else
5529 STRCAT(buf, kp);
5530 STRCAT(buf, " ");
5531 if (cap->count0 != 0 && (isman || isman_s))
5532 {
5533 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5534 STRCAT(buf, " ");
5535 }
5536 }
5537 break;
5538
5539 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005540 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_CSCOPE
5542 if (p_cst)
5543 STRCPY(buf, "cstag ");
5544 else
5545#endif
5546 STRCPY(buf, "ts ");
5547 break;
5548
5549 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005550 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (curbuf->b_help)
5552 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005554 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005555 if (g_cmd)
5556 STRCPY(buf, "tj ");
5557 else
5558 sprintf((char *)buf, "%ldta ", cap->count0);
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561
5562 /*
5563 * Now grab the chars in the identifier
5564 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005565 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005567 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005568 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005569 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005570 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005573 vim_free(buf);
5574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005576 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5577 if (buf == NULL)
5578 {
5579 vim_free(buf);
5580 vim_free(p);
5581 return;
5582 }
5583 STRCAT(buf, p);
5584 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005586 else
5587 {
5588 if (cmdchar == '*')
5589 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5590 else if (cmdchar == '#')
5591 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005592 else if (tag_cmd)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005593 /* Don't escape spaces and Tabs in a tag with a backslash */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005594 aux_ptr = (char_u *)"\\|\"\n[";
5595 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005596 aux_ptr = (char_u *)"\\|\"\n*?[";
5597
5598 p = buf + STRLEN(buf);
5599 while (n-- > 0)
5600 {
5601 /* put a backslash before \ and some others */
5602 if (vim_strchr(aux_ptr, *ptr) != NULL)
5603 *p++ = '\\';
5604#ifdef FEAT_MBYTE
5605 /* When current byte is a part of multibyte character, copy all
5606 * bytes of that character. */
5607 if (has_mbyte)
5608 {
5609 int i;
5610 int len = (*mb_ptr2len)(ptr) - 1;
5611
5612 for (i = 0; i < len && n >= 1; ++i, --n)
5613 *p++ = *ptr++;
5614 }
5615#endif
5616 *p++ = *ptr++;
5617 }
5618 *p = NUL;
5619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 /*
5622 * Execute the command.
5623 */
5624 if (cmdchar == '*' || cmdchar == '#')
5625 {
5626 if (!g_cmd && (
5627#ifdef FEAT_MBYTE
5628 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5629#endif
5630 vim_iswordc(ptr[-1])))
5631 STRCAT(buf, "\\>");
5632#ifdef FEAT_CMDHIST
5633 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005634 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5636#endif
5637 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5638 }
5639 else
5640 do_cmdline_cmd(buf);
5641
5642 vim_free(buf);
5643}
5644
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005645#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646/*
5647 * Get visually selected text, within one line only.
5648 * Returns FAIL if more than one line selected.
5649 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005650 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651get_visual_text(cap, pp, lenp)
5652 cmdarg_T *cap;
5653 char_u **pp; /* return: start of selected text */
5654 int *lenp; /* return: length of selected text */
5655{
5656 if (VIsual_mode != 'V')
5657 unadjust_for_sel();
5658 if (VIsual.lnum != curwin->w_cursor.lnum)
5659 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005660 if (cap != NULL)
5661 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 return FAIL;
5663 }
5664 if (VIsual_mode == 'V')
5665 {
5666 *pp = ml_get_curline();
5667 *lenp = (int)STRLEN(*pp);
5668 }
5669 else
5670 {
5671 if (lt(curwin->w_cursor, VIsual))
5672 {
5673 *pp = ml_get_pos(&curwin->w_cursor);
5674 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5675 }
5676 else
5677 {
5678 *pp = ml_get_pos(&VIsual);
5679 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5680 }
5681#ifdef FEAT_MBYTE
5682 if (has_mbyte)
5683 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005684 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#endif
5686 }
5687 reset_VIsual_and_resel();
5688 return OK;
5689}
5690#endif
5691
5692/*
5693 * CTRL-T: backwards in tag stack
5694 */
5695 static void
5696nv_tagpop(cap)
5697 cmdarg_T *cap;
5698{
5699 if (!checkclearopq(cap->oap))
5700 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5701}
5702
5703/*
5704 * Handle scrolling command 'H', 'L' and 'M'.
5705 */
5706 static void
5707nv_scroll(cap)
5708 cmdarg_T *cap;
5709{
5710 int used = 0;
5711 long n;
5712#ifdef FEAT_FOLDING
5713 linenr_T lnum;
5714#endif
5715 int half;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005716#ifdef FEAT_CONCEAL
5717 linenr_T oldline = curwin->w_cursor.lnum;
5718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 cap->oap->motion_type = MLINE;
5721 setpcmark();
5722
5723 if (cap->cmdchar == 'L')
5724 {
5725 validate_botline(); /* make sure curwin->w_botline is valid */
5726 curwin->w_cursor.lnum = curwin->w_botline - 1;
5727 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5728 curwin->w_cursor.lnum = 1;
5729 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005730 {
5731#ifdef FEAT_FOLDING
5732 if (hasAnyFolding(curwin))
5733 {
5734 /* Count a fold for one screen line. */
5735 for (n = cap->count1 - 1; n > 0
5736 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5737 {
5738 (void)hasFolding(curwin->w_cursor.lnum,
5739 &curwin->w_cursor.lnum, NULL);
5740 --curwin->w_cursor.lnum;
5741 }
5742 }
5743 else
5744#endif
5745 curwin->w_cursor.lnum -= cap->count1 - 1;
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 else
5749 {
5750 if (cap->cmdchar == 'M')
5751 {
5752#ifdef FEAT_DIFF
5753 /* Don't count filler lines above the window. */
5754 used -= diff_check_fill(curwin, curwin->w_topline)
5755 - curwin->w_topfill;
5756#endif
5757 validate_botline(); /* make sure w_empty_rows is valid */
5758 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5759 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5760 {
5761#ifdef FEAT_DIFF
5762 /* Count half he number of filler lines to be "below this
5763 * line" and half to be "above the next line". */
5764 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5765 + n) / 2 >= half)
5766 {
5767 --n;
5768 break;
5769 }
5770#endif
5771 used += plines(curwin->w_topline + n);
5772 if (used >= half)
5773 break;
5774#ifdef FEAT_FOLDING
5775 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5776 n = lnum - curwin->w_topline;
5777#endif
5778 }
5779 if (n > 0 && used > curwin->w_height)
5780 --n;
5781 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005782 else /* (cap->cmdchar == 'H') */
5783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005785#ifdef FEAT_FOLDING
5786 if (hasAnyFolding(curwin))
5787 {
5788 /* Count a fold for one screen line. */
5789 lnum = curwin->w_topline;
5790 while (n-- > 0 && lnum < curwin->w_botline - 1)
5791 {
5792 hasFolding(lnum, NULL, &lnum);
5793 ++lnum;
5794 }
5795 n = lnum - curwin->w_topline;
5796 }
5797#endif
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 curwin->w_cursor.lnum = curwin->w_topline + n;
5800 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5801 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5802 }
5803
5804 cursor_correct(); /* correct for 'so' */
5805 beginline(BL_SOL | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005806#ifdef FEAT_CONCEAL
5807 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
5808 {
5809 update_single_line(curwin, oldline);
5810 update_single_line(curwin, curwin->w_cursor.lnum);
5811 }
5812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
5816 * Cursor right commands.
5817 */
5818 static void
5819nv_right(cap)
5820 cmdarg_T *cap;
5821{
5822 long n;
5823#ifdef FEAT_VISUAL
5824 int PAST_LINE;
5825#else
5826# define PAST_LINE 0
5827#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005828#ifdef FEAT_CONCEAL
5829 linenr_T oldline = curwin->w_cursor.lnum;
5830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005832 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5833 {
5834 /* <C-Right> and <S-Right> move a word or WORD right */
5835 if (mod_mask & MOD_MASK_CTRL)
5836 cap->arg = TRUE;
5837 nv_wordcmd(cap);
5838 return;
5839 }
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 cap->oap->motion_type = MCHAR;
5842 cap->oap->inclusive = FALSE;
5843#ifdef FEAT_VISUAL
5844 PAST_LINE = (VIsual_active && *p_sel != 'o');
5845
5846# ifdef FEAT_VIRTUALEDIT
5847 /*
5848 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005849 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 */
5851 if (virtual_active())
5852 PAST_LINE = 0;
5853# endif
5854#endif
5855
5856 for (n = cap->count1; n > 0; --n)
5857 {
5858 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005859#ifdef FEAT_VISUAL
5860 || (PAST_LINE && *ml_get_cursor() == NUL)
5861#endif
5862 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
5864 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005865 * <Space> wraps to next line if 'whichwrap' has 's'.
5866 * 'l' wraps to next line if 'whichwrap' has 'l'.
5867 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 */
5869 if ( ((cap->cmdchar == ' '
5870 && vim_strchr(p_ww, 's') != NULL)
5871 || (cap->cmdchar == 'l'
5872 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005873 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 && vim_strchr(p_ww, '>') != NULL))
5875 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5876 {
5877 /* When deleting we also count the NL as a character.
5878 * Set cap->oap->inclusive when last char in the line is
5879 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005880 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 && !cap->oap->inclusive
5882 && !lineempty(curwin->w_cursor.lnum))
5883 cap->oap->inclusive = TRUE;
5884 else
5885 {
5886 ++curwin->w_cursor.lnum;
5887 curwin->w_cursor.col = 0;
5888#ifdef FEAT_VIRTUALEDIT
5889 curwin->w_cursor.coladd = 0;
5890#endif
5891 curwin->w_set_curswant = TRUE;
5892 cap->oap->inclusive = FALSE;
5893 }
5894 continue;
5895 }
5896 if (cap->oap->op_type == OP_NOP)
5897 {
5898 /* Only beep and flush if not moved at all */
5899 if (n == cap->count1)
5900 beep_flush();
5901 }
5902 else
5903 {
5904 if (!lineempty(curwin->w_cursor.lnum))
5905 cap->oap->inclusive = TRUE;
5906 }
5907 break;
5908 }
5909#ifdef FEAT_VISUAL
5910 else if (PAST_LINE)
5911 {
5912 curwin->w_set_curswant = TRUE;
5913# ifdef FEAT_VIRTUALEDIT
5914 if (virtual_active())
5915 oneright();
5916 else
5917# endif
5918 {
5919# ifdef FEAT_MBYTE
5920 if (has_mbyte)
5921 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005922 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 else
5924# endif
5925 ++curwin->w_cursor.col;
5926 }
5927 }
5928#endif
5929 }
5930#ifdef FEAT_FOLDING
5931 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5932 && cap->oap->op_type == OP_NOP)
5933 foldOpenCursor();
5934#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005935#ifdef FEAT_CONCEAL
5936 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
5937 {
5938 update_single_line(curwin, oldline);
5939 update_single_line(curwin, curwin->w_cursor.lnum);
5940 }
5941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942}
5943
5944/*
5945 * Cursor left commands.
5946 *
5947 * Returns TRUE when operator end should not be adjusted.
5948 */
5949 static void
5950nv_left(cap)
5951 cmdarg_T *cap;
5952{
5953 long n;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954#ifdef FEAT_CONCEAL
5955 linenr_T oldline = curwin->w_cursor.lnum;
5956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005958 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5959 {
5960 /* <C-Left> and <S-Left> move a word or WORD left */
5961 if (mod_mask & MOD_MASK_CTRL)
5962 cap->arg = 1;
5963 nv_bck_word(cap);
5964 return;
5965 }
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cap->oap->motion_type = MCHAR;
5968 cap->oap->inclusive = FALSE;
5969 for (n = cap->count1; n > 0; --n)
5970 {
5971 if (oneleft() == FAIL)
5972 {
5973 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5974 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5975 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5976 */
5977 if ( (((cap->cmdchar == K_BS
5978 || cap->cmdchar == Ctrl_H)
5979 && vim_strchr(p_ww, 'b') != NULL)
5980 || (cap->cmdchar == 'h'
5981 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005982 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 && vim_strchr(p_ww, '<') != NULL))
5984 && curwin->w_cursor.lnum > 1)
5985 {
5986 --(curwin->w_cursor.lnum);
5987 coladvance((colnr_T)MAXCOL);
5988 curwin->w_set_curswant = TRUE;
5989
5990 /* When the NL before the first char has to be deleted we
5991 * put the cursor on the NUL after the previous line.
5992 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005993 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if ( (cap->oap->op_type == OP_DELETE
5995 || cap->oap->op_type == OP_CHANGE)
5996 && !lineempty(curwin->w_cursor.lnum))
5997 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005998 if (*ml_get_cursor() != NUL)
5999 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 cap->retval |= CA_NO_ADJ_OP_END;
6001 }
6002 continue;
6003 }
6004 /* Only beep and flush if not moved at all */
6005 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6006 beep_flush();
6007 break;
6008 }
6009 }
6010#ifdef FEAT_FOLDING
6011 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6012 && cap->oap->op_type == OP_NOP)
6013 foldOpenCursor();
6014#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006015#ifdef FEAT_CONCEAL
6016 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6017 {
6018 update_single_line(curwin, oldline);
6019 update_single_line(curwin, curwin->w_cursor.lnum);
6020 }
6021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
6024/*
6025 * Cursor up commands.
6026 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6027 */
6028 static void
6029nv_up(cap)
6030 cmdarg_T *cap;
6031{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006032 if (mod_mask & MOD_MASK_SHIFT)
6033 {
6034 /* <S-Up> is page up */
6035 cap->arg = BACKWARD;
6036 nv_page(cap);
6037 }
6038 else
6039 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006040#ifdef FEAT_CONCEAL
6041 linenr_T oldline = curwin->w_cursor.lnum;
6042#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006043 cap->oap->motion_type = MLINE;
6044 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6045 clearopbeep(cap->oap);
6046 else if (cap->arg)
6047 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006048#ifdef FEAT_CONCEAL
6049 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6050 {
6051 update_single_line(curwin, oldline);
6052 update_single_line(curwin, curwin->w_cursor.lnum);
6053 }
6054#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056}
6057
6058/*
6059 * Cursor down commands.
6060 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6061 */
6062 static void
6063nv_down(cap)
6064 cmdarg_T *cap;
6065{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006066 if (mod_mask & MOD_MASK_SHIFT)
6067 {
6068 /* <S-Down> is page down */
6069 cap->arg = FORWARD;
6070 nv_page(cap);
6071 }
6072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6074 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006075 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006076 if (curwin->w_llist_ref == NULL)
6077 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6078 else
6079 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 else
6081#endif
6082 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006083#ifdef FEAT_CONCEAL
6084 linenr_T oldline = curwin->w_cursor.lnum;
6085 linenr_T oldbotline = curwin->w_botline;
6086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#ifdef FEAT_CMDWIN
6088 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006089 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 cmdwin_result = CAR;
6091 else
6092#endif
6093 {
6094 cap->oap->motion_type = MLINE;
6095 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6096 clearopbeep(cap->oap);
6097 else if (cap->arg)
6098 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006099#ifdef FEAT_CONCEAL
6100 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6101 {
6102 update_single_line(curwin, oldline);
6103 /* Don't do this if we've scrolled, the line is already
6104 * drawn */
6105 if (oldbotline == curwin->w_botline)
6106 update_single_line(curwin, curwin->w_cursor.lnum);
6107 }
6108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110 }
6111}
6112
6113#ifdef FEAT_SEARCHPATH
6114/*
6115 * Grab the file name under the cursor and edit it.
6116 */
6117 static void
6118nv_gotofile(cap)
6119 cmdarg_T *cap;
6120{
6121 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006122 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006124 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
6126 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006127 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 return;
6129 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006130#ifdef FEAT_AUTOCMD
6131 if (curbuf_locked())
6132 {
6133 clearop(cap->oap);
6134 return;
6135 }
6136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006138 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 if (ptr != NULL)
6141 {
6142 /* do autowrite if necessary */
6143 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6144 autowrite(curbuf, FALSE);
6145 setpcmark();
6146 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006147 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006148 if (cap->nchar == 'F' && lnum >= 0)
6149 {
6150 curwin->w_cursor.lnum = lnum;
6151 check_cursor_lnum();
6152 beginline(BL_SOL | BL_FIX);
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 vim_free(ptr);
6155 }
6156 else
6157 clearop(cap->oap);
6158}
6159#endif
6160
6161/*
6162 * <End> command: to end of current line or last line.
6163 */
6164 static void
6165nv_end(cap)
6166 cmdarg_T *cap;
6167{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006168 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006170 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 nv_goto(cap);
6172 cap->count1 = 1; /* to end of current line */
6173 }
6174 nv_dollar(cap);
6175}
6176
6177/*
6178 * Handle the "$" command.
6179 */
6180 static void
6181nv_dollar(cap)
6182 cmdarg_T *cap;
6183{
6184 cap->oap->motion_type = MCHAR;
6185 cap->oap->inclusive = TRUE;
6186#ifdef FEAT_VIRTUALEDIT
6187 /* In virtual mode when off the edge of a line and an operator
6188 * is pending (whew!) keep the cursor where it is.
6189 * Otherwise, send it to the end of the line. */
6190 if (!virtual_active() || gchar_cursor() != NUL
6191 || cap->oap->op_type == OP_NOP)
6192#endif
6193 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6194 if (cursor_down((long)(cap->count1 - 1),
6195 cap->oap->op_type == OP_NOP) == FAIL)
6196 clearopbeep(cap->oap);
6197#ifdef FEAT_FOLDING
6198 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6199 foldOpenCursor();
6200#endif
6201}
6202
6203/*
6204 * Implementation of '?' and '/' commands.
6205 * If cap->arg is TRUE don't set PC mark.
6206 */
6207 static void
6208nv_search(cap)
6209 cmdarg_T *cap;
6210{
6211 oparg_T *oap = cap->oap;
6212
6213 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6214 {
6215 /* Translate "g??" to "g?g?" */
6216 cap->cmdchar = 'g';
6217 cap->nchar = '?';
6218 nv_operator(cap);
6219 return;
6220 }
6221
6222 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6223
6224 if (cap->searchbuf == NULL)
6225 {
6226 clearop(oap);
6227 return;
6228 }
6229
6230 normal_search(cap, cap->cmdchar, cap->searchbuf,
6231 (cap->arg ? 0 : SEARCH_MARK));
6232}
6233
6234/*
6235 * Handle "N" and "n" commands.
6236 * cap->arg is SEARCH_REV for "N", 0 for "n".
6237 */
6238 static void
6239nv_next(cap)
6240 cmdarg_T *cap;
6241{
6242 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6243}
6244
6245/*
6246 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6247 * Uses only cap->count1 and cap->oap from "cap".
6248 */
6249 static void
6250normal_search(cap, dir, pat, opt)
6251 cmdarg_T *cap;
6252 int dir;
6253 char_u *pat;
6254 int opt; /* extra flags for do_search() */
6255{
6256 int i;
6257
6258 cap->oap->motion_type = MCHAR;
6259 cap->oap->inclusive = FALSE;
6260 cap->oap->use_reg_one = TRUE;
6261 curwin->w_set_curswant = TRUE;
6262
6263 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006264 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (i == 0)
6266 clearop(cap->oap);
6267 else
6268 {
6269 if (i == 2)
6270 cap->oap->motion_type = MLINE;
6271#ifdef FEAT_VIRTUALEDIT
6272 curwin->w_cursor.coladd = 0;
6273#endif
6274#ifdef FEAT_FOLDING
6275 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6276 foldOpenCursor();
6277#endif
6278 }
6279
6280 /* "/$" will put the cursor after the end of the line, may need to
6281 * correct that here */
6282 check_cursor();
6283}
6284
6285/*
6286 * Character search commands.
6287 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6288 * ',' and FALSE for ';'.
6289 * cap->nchar is NUL for ',' and ';' (repeat the search)
6290 */
6291 static void
6292nv_csearch(cap)
6293 cmdarg_T *cap;
6294{
6295 int t_cmd;
6296
6297 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6298 t_cmd = TRUE;
6299 else
6300 t_cmd = FALSE;
6301
6302 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6304 clearopbeep(cap->oap);
6305 else
6306 {
6307 curwin->w_set_curswant = TRUE;
6308#ifdef FEAT_VIRTUALEDIT
6309 /* Include a Tab for "tx" and for "dfx". */
6310 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6311 && (t_cmd || cap->oap->op_type != OP_NOP))
6312 {
6313 colnr_T scol, ecol;
6314
6315 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6316 curwin->w_cursor.coladd = ecol - scol;
6317 }
6318 else
6319 curwin->w_cursor.coladd = 0;
6320#endif
6321#ifdef FEAT_VISUAL
6322 adjust_for_sel(cap);
6323#endif
6324#ifdef FEAT_FOLDING
6325 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6326 foldOpenCursor();
6327#endif
6328 }
6329}
6330
6331/*
6332 * "[" and "]" commands.
6333 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6334 */
6335 static void
6336nv_brackets(cap)
6337 cmdarg_T *cap;
6338{
6339 pos_T new_pos;
6340 pos_T prev_pos;
6341 pos_T *pos = NULL; /* init for GCC */
6342 pos_T old_pos; /* cursor position before command */
6343 int flag;
6344 long n;
6345 int findc;
6346 int c;
6347
6348 cap->oap->motion_type = MCHAR;
6349 cap->oap->inclusive = FALSE;
6350 old_pos = curwin->w_cursor;
6351#ifdef FEAT_VIRTUALEDIT
6352 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6353#endif
6354
6355#ifdef FEAT_SEARCHPATH
6356 /*
6357 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6358 */
6359 if (cap->nchar == 'f')
6360 nv_gotofile(cap);
6361 else
6362#endif
6363
6364#ifdef FEAT_FIND_ID
6365 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006366 * Find the occurrence(s) of the identifier or define under cursor
6367 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 *
6369 * search list jump
6370 * fwd bwd fwd bwd fwd bwd
6371 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6372 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6373 */
6374 if (vim_strchr((char_u *)
6375#ifdef EBCDIC
6376 "iI\005dD\067",
6377#else
6378 "iI\011dD\004",
6379#endif
6380 cap->nchar) != NULL)
6381 {
6382 char_u *ptr;
6383 int len;
6384
6385 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6386 clearop(cap->oap);
6387 else
6388 {
6389 find_pattern_in_path(ptr, 0, len, TRUE,
6390 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6391 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6392 cap->count1,
6393 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6394 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6395 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6396 (linenr_T)MAXLNUM);
6397 curwin->w_set_curswant = TRUE;
6398 }
6399 }
6400 else
6401#endif
6402
6403 /*
6404 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6405 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6406 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6407 * "[m" or "]m" search for prev/next start of (Java) method.
6408 * "[M" or "]M" search for prev/next end of (Java) method.
6409 */
6410 if ( (cap->cmdchar == '['
6411 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6412 || (cap->cmdchar == ']'
6413 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6414 {
6415 if (cap->nchar == '*')
6416 cap->nchar = '/';
6417 new_pos.lnum = 0;
6418 prev_pos.lnum = 0;
6419 if (cap->nchar == 'm' || cap->nchar == 'M')
6420 {
6421 if (cap->cmdchar == '[')
6422 findc = '{';
6423 else
6424 findc = '}';
6425 n = 9999;
6426 }
6427 else
6428 {
6429 findc = cap->nchar;
6430 n = cap->count1;
6431 }
6432 for ( ; n > 0; --n)
6433 {
6434 if ((pos = findmatchlimit(cap->oap, findc,
6435 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6436 {
6437 if (new_pos.lnum == 0) /* nothing found */
6438 {
6439 if (cap->nchar != 'm' && cap->nchar != 'M')
6440 clearopbeep(cap->oap);
6441 }
6442 else
6443 pos = &new_pos; /* use last one found */
6444 break;
6445 }
6446 prev_pos = new_pos;
6447 curwin->w_cursor = *pos;
6448 new_pos = *pos;
6449 }
6450 curwin->w_cursor = old_pos;
6451
6452 /*
6453 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6454 * brought us to the match for "[m" and "]M" when inside a method.
6455 * Try finding the '{' or '}' we want to be at.
6456 * Also repeat for the given count.
6457 */
6458 if (cap->nchar == 'm' || cap->nchar == 'M')
6459 {
6460 /* norm is TRUE for "]M" and "[m" */
6461 int norm = ((findc == '{') == (cap->nchar == 'm'));
6462
6463 n = cap->count1;
6464 /* found a match: we were inside a method */
6465 if (prev_pos.lnum != 0)
6466 {
6467 pos = &prev_pos;
6468 curwin->w_cursor = prev_pos;
6469 if (norm)
6470 --n;
6471 }
6472 else
6473 pos = NULL;
6474 while (n > 0)
6475 {
6476 for (;;)
6477 {
6478 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6479 {
6480 /* if not found anything, that's an error */
6481 if (pos == NULL)
6482 clearopbeep(cap->oap);
6483 n = 0;
6484 break;
6485 }
6486 c = gchar_cursor();
6487 if (c == '{' || c == '}')
6488 {
6489 /* Must have found end/start of class: use it.
6490 * Or found the place to be at. */
6491 if ((c == findc && norm) || (n == 1 && !norm))
6492 {
6493 new_pos = curwin->w_cursor;
6494 pos = &new_pos;
6495 n = 0;
6496 }
6497 /* if no match found at all, we started outside of the
6498 * class and we're inside now. Just go on. */
6499 else if (new_pos.lnum == 0)
6500 {
6501 new_pos = curwin->w_cursor;
6502 pos = &new_pos;
6503 }
6504 /* found start/end of other method: go to match */
6505 else if ((pos = findmatchlimit(cap->oap, findc,
6506 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6507 0)) == NULL)
6508 n = 0;
6509 else
6510 curwin->w_cursor = *pos;
6511 break;
6512 }
6513 }
6514 --n;
6515 }
6516 curwin->w_cursor = old_pos;
6517 if (pos == NULL && new_pos.lnum != 0)
6518 clearopbeep(cap->oap);
6519 }
6520 if (pos != NULL)
6521 {
6522 setpcmark();
6523 curwin->w_cursor = *pos;
6524 curwin->w_set_curswant = TRUE;
6525#ifdef FEAT_FOLDING
6526 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6527 && cap->oap->op_type == OP_NOP)
6528 foldOpenCursor();
6529#endif
6530 }
6531 }
6532
6533 /*
6534 * "[[", "[]", "]]" and "][": move to start or end of function
6535 */
6536 else if (cap->nchar == '[' || cap->nchar == ']')
6537 {
6538 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6539 flag = '{';
6540 else
6541 flag = '}'; /* "][" or "[]" */
6542
6543 curwin->w_set_curswant = TRUE;
6544 /*
6545 * Imitate strange Vi behaviour: When using "]]" with an operator
6546 * we also stop at '}'.
6547 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006548 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 (cap->oap->op_type != OP_NOP
6550 && cap->arg == FORWARD && flag == '{')))
6551 clearopbeep(cap->oap);
6552 else
6553 {
6554 if (cap->oap->op_type == OP_NOP)
6555 beginline(BL_WHITE | BL_FIX);
6556#ifdef FEAT_FOLDING
6557 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6558 foldOpenCursor();
6559#endif
6560 }
6561 }
6562
6563 /*
6564 * "[p", "[P", "]P" and "]p": put with indent adjustment
6565 */
6566 else if (cap->nchar == 'p' || cap->nchar == 'P')
6567 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006568 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570 prep_redo_cmd(cap);
6571 do_put(cap->oap->regname,
6572 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6573 cap->count1, PUT_FIXINDENT);
6574 }
6575 }
6576
6577 /*
6578 * "['", "[`", "]'" and "]`": jump to next mark
6579 */
6580 else if (cap->nchar == '\'' || cap->nchar == '`')
6581 {
6582 pos = &curwin->w_cursor;
6583 for (n = cap->count1; n > 0; --n)
6584 {
6585 prev_pos = *pos;
6586 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6587 cap->nchar == '\'');
6588 if (pos == NULL)
6589 break;
6590 }
6591 if (pos == NULL)
6592 pos = &prev_pos;
6593 nv_cursormark(cap, cap->nchar == '\'', pos);
6594 }
6595
6596#ifdef FEAT_MOUSE
6597 /*
6598 * [ or ] followed by a middle mouse click: put selected text with
6599 * indent adjustment. Any other button just does as usual.
6600 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006601 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 {
6603 (void)do_mouse(cap->oap, cap->nchar,
6604 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6605 cap->count1, PUT_FIXINDENT);
6606 }
6607#endif /* FEAT_MOUSE */
6608
6609#ifdef FEAT_FOLDING
6610 /*
6611 * "[z" and "]z": move to start or end of open fold.
6612 */
6613 else if (cap->nchar == 'z')
6614 {
6615 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6616 cap->count1) == FAIL)
6617 clearopbeep(cap->oap);
6618 }
6619#endif
6620
6621#ifdef FEAT_DIFF
6622 /*
6623 * "[c" and "]c": move to next or previous diff-change.
6624 */
6625 else if (cap->nchar == 'c')
6626 {
6627 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6628 cap->count1) == FAIL)
6629 clearopbeep(cap->oap);
6630 }
6631#endif
6632
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006633#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634 /*
6635 * "[s", "[S", "]s" and "]S": move to next spell error.
6636 */
6637 else if (cap->nchar == 's' || cap->nchar == 'S')
6638 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006639 setpcmark();
6640 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006641 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6642 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006643 {
6644 clearopbeep(cap->oap);
6645 break;
6646 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006647# ifdef FEAT_FOLDING
6648 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6649 foldOpenCursor();
6650# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006651 }
6652#endif
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 /* Not a valid cap->nchar. */
6655 else
6656 clearopbeep(cap->oap);
6657}
6658
6659/*
6660 * Handle Normal mode "%" command.
6661 */
6662 static void
6663nv_percent(cap)
6664 cmdarg_T *cap;
6665{
6666 pos_T *pos;
6667#ifdef FEAT_FOLDING
6668 linenr_T lnum = curwin->w_cursor.lnum;
6669#endif
6670
6671 cap->oap->inclusive = TRUE;
6672 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6673 {
6674 if (cap->count0 > 100)
6675 clearopbeep(cap->oap);
6676 else
6677 {
6678 cap->oap->motion_type = MLINE;
6679 setpcmark();
6680 /* Round up, so CTRL-G will give same value. Watch out for a
6681 * large line count, the line number must not go negative! */
6682 if (curbuf->b_ml.ml_line_count > 1000000)
6683 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6684 / 100L * cap->count0;
6685 else
6686 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6687 cap->count0 + 99L) / 100L;
6688 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6689 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6690 beginline(BL_SOL | BL_FIX);
6691 }
6692 }
6693 else /* "%" : go to matching paren */
6694 {
6695 cap->oap->motion_type = MCHAR;
6696 cap->oap->use_reg_one = TRUE;
6697 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6698 clearopbeep(cap->oap);
6699 else
6700 {
6701 setpcmark();
6702 curwin->w_cursor = *pos;
6703 curwin->w_set_curswant = TRUE;
6704#ifdef FEAT_VIRTUALEDIT
6705 curwin->w_cursor.coladd = 0;
6706#endif
6707#ifdef FEAT_VISUAL
6708 adjust_for_sel(cap);
6709#endif
6710 }
6711 }
6712#ifdef FEAT_FOLDING
6713 if (cap->oap->op_type == OP_NOP
6714 && lnum != curwin->w_cursor.lnum
6715 && (fdo_flags & FDO_PERCENT)
6716 && KeyTyped)
6717 foldOpenCursor();
6718#endif
6719}
6720
6721/*
6722 * Handle "(" and ")" commands.
6723 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6724 */
6725 static void
6726nv_brace(cap)
6727 cmdarg_T *cap;
6728{
6729 cap->oap->motion_type = MCHAR;
6730 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006731 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6732 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 curwin->w_set_curswant = TRUE;
6734
6735 if (findsent(cap->arg, cap->count1) == FAIL)
6736 clearopbeep(cap->oap);
6737 else
6738 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006739 /* Don't leave the cursor on the NUL past end of line. */
6740 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741#ifdef FEAT_VIRTUALEDIT
6742 curwin->w_cursor.coladd = 0;
6743#endif
6744#ifdef FEAT_FOLDING
6745 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6746 foldOpenCursor();
6747#endif
6748 }
6749}
6750
6751/*
6752 * "m" command: Mark a position.
6753 */
6754 static void
6755nv_mark(cap)
6756 cmdarg_T *cap;
6757{
6758 if (!checkclearop(cap->oap))
6759 {
6760 if (setmark(cap->nchar) == FAIL)
6761 clearopbeep(cap->oap);
6762 }
6763}
6764
6765/*
6766 * "{" and "}" commands.
6767 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6768 */
6769 static void
6770nv_findpar(cap)
6771 cmdarg_T *cap;
6772{
6773 cap->oap->motion_type = MCHAR;
6774 cap->oap->inclusive = FALSE;
6775 cap->oap->use_reg_one = TRUE;
6776 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006777 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 clearopbeep(cap->oap);
6779 else
6780 {
6781#ifdef FEAT_VIRTUALEDIT
6782 curwin->w_cursor.coladd = 0;
6783#endif
6784#ifdef FEAT_FOLDING
6785 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6786 foldOpenCursor();
6787#endif
6788 }
6789}
6790
6791/*
6792 * "u" command: Undo or make lower case.
6793 */
6794 static void
6795nv_undo(cap)
6796 cmdarg_T *cap;
6797{
6798 if (cap->oap->op_type == OP_LOWER
6799#ifdef FEAT_VISUAL
6800 || VIsual_active
6801#endif
6802 )
6803 {
6804 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6805 cap->cmdchar = 'g';
6806 cap->nchar = 'u';
6807 nv_operator(cap);
6808 }
6809 else
6810 nv_kundo(cap);
6811}
6812
6813/*
6814 * <Undo> command.
6815 */
6816 static void
6817nv_kundo(cap)
6818 cmdarg_T *cap;
6819{
6820 if (!checkclearopq(cap->oap))
6821 {
6822 u_undo((int)cap->count1);
6823 curwin->w_set_curswant = TRUE;
6824 }
6825}
6826
6827/*
6828 * Handle the "r" command.
6829 */
6830 static void
6831nv_replace(cap)
6832 cmdarg_T *cap;
6833{
6834 char_u *ptr;
6835 int had_ctrl_v;
6836 long n;
6837
6838 if (checkclearop(cap->oap))
6839 return;
6840
6841 /* get another character */
6842 if (cap->nchar == Ctrl_V)
6843 {
6844 had_ctrl_v = Ctrl_V;
6845 cap->nchar = get_literal();
6846 /* Don't redo a multibyte character with CTRL-V. */
6847 if (cap->nchar > DEL)
6848 had_ctrl_v = NUL;
6849 }
6850 else
6851 had_ctrl_v = NUL;
6852
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006853 /* Abort if the character is a special key. */
6854 if (IS_SPECIAL(cap->nchar))
6855 {
6856 clearopbeep(cap->oap);
6857 return;
6858 }
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#ifdef FEAT_VISUAL
6861 /* Visual mode "r" */
6862 if (VIsual_active)
6863 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006864 if (got_int)
6865 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 nv_operator(cap);
6867 return;
6868 }
6869#endif
6870
6871#ifdef FEAT_VIRTUALEDIT
6872 /* Break tabs, etc. */
6873 if (virtual_active())
6874 {
6875 if (u_save_cursor() == FAIL)
6876 return;
6877 if (gchar_cursor() == NUL)
6878 {
6879 /* Add extra space and put the cursor on the first one. */
6880 coladvance_force((colnr_T)(getviscol() + cap->count1));
6881 curwin->w_cursor.col -= cap->count1;
6882 }
6883 else if (gchar_cursor() == TAB)
6884 coladvance_force(getviscol());
6885 }
6886#endif
6887
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006888 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006890 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891#ifdef FEAT_MBYTE
6892 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6893#endif
6894 )
6895 {
6896 clearopbeep(cap->oap);
6897 return;
6898 }
6899
6900 /*
6901 * Replacing with a TAB is done by edit() when it is complicated because
6902 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6903 * Other characters are done below to avoid problems with things like
6904 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6905 */
6906 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6907 {
6908 stuffnumReadbuff(cap->count1);
6909 stuffcharReadbuff('R');
6910 stuffcharReadbuff('\t');
6911 stuffcharReadbuff(ESC);
6912 return;
6913 }
6914
6915 /* save line for undo */
6916 if (u_save_cursor() == FAIL)
6917 return;
6918
6919 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6920 {
6921 /*
6922 * Replace character(s) by a single newline.
6923 * Strange vi behaviour: Only one newline is inserted.
6924 * Delete the characters here.
6925 * Insert the newline with an insert command, takes care of
6926 * autoindent. The insert command depends on being on the last
6927 * character of a line or not.
6928 */
6929#ifdef FEAT_MBYTE
6930 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6931#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006932 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933#endif
6934 stuffcharReadbuff('\r');
6935 stuffcharReadbuff(ESC);
6936
6937 /* Give 'r' to edit(), to get the redo command right. */
6938 invoke_edit(cap, TRUE, 'r', FALSE);
6939 }
6940 else
6941 {
6942 prep_redo(cap->oap->regname, cap->count1,
6943 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6944
6945 curbuf->b_op_start = curwin->w_cursor;
6946#ifdef FEAT_MBYTE
6947 if (has_mbyte)
6948 {
6949 int old_State = State;
6950
6951 if (cap->ncharC1 != 0)
6952 AppendCharToRedobuff(cap->ncharC1);
6953 if (cap->ncharC2 != 0)
6954 AppendCharToRedobuff(cap->ncharC2);
6955
6956 /* This is slow, but it handles replacing a single-byte with a
6957 * multi-byte and the other way around. Also handles adding
6958 * composing characters for utf-8. */
6959 for (n = cap->count1; n > 0; --n)
6960 {
6961 State = REPLACE;
6962 ins_char(cap->nchar);
6963 State = old_State;
6964 if (cap->ncharC1 != 0)
6965 ins_char(cap->ncharC1);
6966 if (cap->ncharC2 != 0)
6967 ins_char(cap->ncharC2);
6968 }
6969 }
6970 else
6971#endif
6972 {
6973 /*
6974 * Replace the characters within one line.
6975 */
6976 for (n = cap->count1; n > 0; --n)
6977 {
6978 /*
6979 * Get ptr again, because u_save and/or showmatch() will have
6980 * released the line. At the same time we let know that the
6981 * line will be changed.
6982 */
6983 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6984 ptr[curwin->w_cursor.col] = cap->nchar;
6985 if (p_sm && msg_silent == 0)
6986 showmatch(cap->nchar);
6987 ++curwin->w_cursor.col;
6988 }
6989#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006990 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006992 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
Bram Moolenaar009b2592004-10-24 19:18:58 +00006994 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006995 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006997 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999#endif
7000
7001 /* mark the buffer as changed and prepare for displaying */
7002 changed_bytes(curwin->w_cursor.lnum,
7003 (colnr_T)(curwin->w_cursor.col - cap->count1));
7004 }
7005 --curwin->w_cursor.col; /* cursor on the last replaced char */
7006#ifdef FEAT_MBYTE
7007 /* if the character on the left of the current cursor is a multi-byte
7008 * character, move two characters left */
7009 if (has_mbyte)
7010 mb_adjust_cursor();
7011#endif
7012 curbuf->b_op_end = curwin->w_cursor;
7013 curwin->w_set_curswant = TRUE;
7014 set_last_insert(cap->nchar);
7015 }
7016}
7017
7018#ifdef FEAT_VISUAL
7019/*
7020 * 'o': Exchange start and end of Visual area.
7021 * 'O': same, but in block mode exchange left and right corners.
7022 */
7023 static void
7024v_swap_corners(cmdchar)
7025 int cmdchar;
7026{
7027 pos_T old_cursor;
7028 colnr_T left, right;
7029
7030 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7031 {
7032 old_cursor = curwin->w_cursor;
7033 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7034 curwin->w_cursor.lnum = VIsual.lnum;
7035 coladvance(left);
7036 VIsual = curwin->w_cursor;
7037
7038 curwin->w_cursor.lnum = old_cursor.lnum;
7039 curwin->w_curswant = right;
7040 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7041 * right one column */
7042 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7043 ++curwin->w_curswant;
7044 coladvance(curwin->w_curswant);
7045 if (curwin->w_cursor.col == old_cursor.col
7046#ifdef FEAT_VIRTUALEDIT
7047 && (!virtual_active()
7048 || curwin->w_cursor.coladd == old_cursor.coladd)
7049#endif
7050 )
7051 {
7052 curwin->w_cursor.lnum = VIsual.lnum;
7053 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7054 ++right;
7055 coladvance(right);
7056 VIsual = curwin->w_cursor;
7057
7058 curwin->w_cursor.lnum = old_cursor.lnum;
7059 coladvance(left);
7060 curwin->w_curswant = left;
7061 }
7062 }
7063 else
7064 {
7065 old_cursor = curwin->w_cursor;
7066 curwin->w_cursor = VIsual;
7067 VIsual = old_cursor;
7068 curwin->w_set_curswant = TRUE;
7069 }
7070}
7071#endif /* FEAT_VISUAL */
7072
7073/*
7074 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7075 */
7076 static void
7077nv_Replace(cap)
7078 cmdarg_T *cap;
7079{
7080#ifdef FEAT_VISUAL
7081 if (VIsual_active) /* "R" is replace lines */
7082 {
7083 cap->cmdchar = 'c';
7084 cap->nchar = NUL;
7085 VIsual_mode = 'V';
7086 nv_operator(cap);
7087 }
7088 else
7089#endif
7090 if (!checkclearopq(cap->oap))
7091 {
7092 if (!curbuf->b_p_ma)
7093 EMSG(_(e_modifiable));
7094 else
7095 {
7096#ifdef FEAT_VIRTUALEDIT
7097 if (virtual_active())
7098 coladvance(getviscol());
7099#endif
7100 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7101 }
7102 }
7103}
7104
7105#ifdef FEAT_VREPLACE
7106/*
7107 * "gr".
7108 */
7109 static void
7110nv_vreplace(cap)
7111 cmdarg_T *cap;
7112{
7113# ifdef FEAT_VISUAL
7114 if (VIsual_active)
7115 {
7116 cap->cmdchar = 'r';
7117 cap->nchar = cap->extra_char;
7118 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7119 }
7120 else
7121# endif
7122 if (!checkclearopq(cap->oap))
7123 {
7124 if (!curbuf->b_p_ma)
7125 EMSG(_(e_modifiable));
7126 else
7127 {
7128 if (cap->extra_char == Ctrl_V) /* get another character */
7129 cap->extra_char = get_literal();
7130 stuffcharReadbuff(cap->extra_char);
7131 stuffcharReadbuff(ESC);
7132# ifdef FEAT_VIRTUALEDIT
7133 if (virtual_active())
7134 coladvance(getviscol());
7135# endif
7136 invoke_edit(cap, TRUE, 'v', FALSE);
7137 }
7138 }
7139}
7140#endif
7141
7142/*
7143 * Swap case for "~" command, when it does not work like an operator.
7144 */
7145 static void
7146n_swapchar(cap)
7147 cmdarg_T *cap;
7148{
7149 long n;
7150 pos_T startpos;
7151 int did_change = 0;
7152#ifdef FEAT_NETBEANS_INTG
7153 pos_T pos;
7154 char_u *ptr;
7155 int count;
7156#endif
7157
7158 if (checkclearopq(cap->oap))
7159 return;
7160
7161 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7162 {
7163 clearopbeep(cap->oap);
7164 return;
7165 }
7166
7167 prep_redo_cmd(cap);
7168
7169 if (u_save_cursor() == FAIL)
7170 return;
7171
7172 startpos = curwin->w_cursor;
7173#ifdef FEAT_NETBEANS_INTG
7174 pos = startpos;
7175#endif
7176 for (n = cap->count1; n > 0; --n)
7177 {
7178 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7179 inc_cursor();
7180 if (gchar_cursor() == NUL)
7181 {
7182 if (vim_strchr(p_ww, '~') != NULL
7183 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7184 {
7185#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007186 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 if (did_change)
7189 {
7190 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007191 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007192 netbeans_removed(curbuf, pos.lnum, pos.col,
7193 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007195 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197 pos.col = 0;
7198 pos.lnum++;
7199 }
7200#endif
7201 ++curwin->w_cursor.lnum;
7202 curwin->w_cursor.col = 0;
7203 if (n > 1)
7204 {
7205 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7206 break;
7207 u_clearline();
7208 }
7209 }
7210 else
7211 break;
7212 }
7213 }
7214#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007215 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 ptr = ml_get(pos.lnum);
7218 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007219 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7220 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
7222#endif
7223
7224
7225 check_cursor();
7226 curwin->w_set_curswant = TRUE;
7227 if (did_change)
7228 {
7229 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7230 0L);
7231 curbuf->b_op_start = startpos;
7232 curbuf->b_op_end = curwin->w_cursor;
7233 if (curbuf->b_op_end.col > 0)
7234 --curbuf->b_op_end.col;
7235 }
7236}
7237
7238/*
7239 * Move cursor to mark.
7240 */
7241 static void
7242nv_cursormark(cap, flag, pos)
7243 cmdarg_T *cap;
7244 int flag;
7245 pos_T *pos;
7246{
7247 if (check_mark(pos) == FAIL)
7248 clearop(cap->oap);
7249 else
7250 {
7251 if (cap->cmdchar == '\''
7252 || cap->cmdchar == '`'
7253 || cap->cmdchar == '['
7254 || cap->cmdchar == ']')
7255 setpcmark();
7256 curwin->w_cursor = *pos;
7257 if (flag)
7258 beginline(BL_WHITE | BL_FIX);
7259 else
7260 check_cursor();
7261 }
7262 cap->oap->motion_type = flag ? MLINE : MCHAR;
7263 if (cap->cmdchar == '`')
7264 cap->oap->use_reg_one = TRUE;
7265 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7266 curwin->w_set_curswant = TRUE;
7267}
7268
7269#ifdef FEAT_VISUAL
7270/*
7271 * Handle commands that are operators in Visual mode.
7272 */
7273 static void
7274v_visop(cap)
7275 cmdarg_T *cap;
7276{
7277 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7278
7279 /* Uppercase means linewise, except in block mode, then "D" deletes till
7280 * the end of the line, and "C" replaces til EOL */
7281 if (isupper(cap->cmdchar))
7282 {
7283 if (VIsual_mode != Ctrl_V)
7284 VIsual_mode = 'V';
7285 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7286 curwin->w_curswant = MAXCOL;
7287 }
7288 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7289 nv_operator(cap);
7290}
7291#endif
7292
7293/*
7294 * "s" and "S" commands.
7295 */
7296 static void
7297nv_subst(cap)
7298 cmdarg_T *cap;
7299{
7300#ifdef FEAT_VISUAL
7301 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7302 {
7303 if (cap->cmdchar == 'S')
7304 VIsual_mode = 'V';
7305 cap->cmdchar = 'c';
7306 nv_operator(cap);
7307 }
7308 else
7309#endif
7310 nv_optrans(cap);
7311}
7312
7313/*
7314 * Abbreviated commands.
7315 */
7316 static void
7317nv_abbrev(cap)
7318 cmdarg_T *cap;
7319{
7320 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7321 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7322
7323#ifdef FEAT_VISUAL
7324 /* in Visual mode these commands are operators */
7325 if (VIsual_active)
7326 v_visop(cap);
7327 else
7328#endif
7329 nv_optrans(cap);
7330}
7331
7332/*
7333 * Translate a command into another command.
7334 */
7335 static void
7336nv_optrans(cap)
7337 cmdarg_T *cap;
7338{
7339 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7340 (char_u *)"d$", (char_u *)"c$",
7341 (char_u *)"cl", (char_u *)"cc",
7342 (char_u *)"yy", (char_u *)":s\r"};
7343 static char_u *str = (char_u *)"xXDCsSY&";
7344
7345 if (!checkclearopq(cap->oap))
7346 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007347 /* In Vi "2D" doesn't delete the next line. Can't translate it
7348 * either, because "2." should also not use the count. */
7349 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7350 {
7351 cap->oap->start = curwin->w_cursor;
7352 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007353#ifdef FEAT_EVAL
7354 set_op_var(OP_DELETE);
7355#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007356 cap->count1 = 1;
7357 nv_dollar(cap);
7358 finish_op = TRUE;
7359 ResetRedobuff();
7360 AppendCharToRedobuff('D');
7361 }
7362 else
7363 {
7364 if (cap->count0)
7365 stuffnumReadbuff(cap->count0);
7366 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369 cap->opcount = 0;
7370}
7371
7372/*
7373 * "'" and "`" commands. Also for "g'" and "g`".
7374 * cap->arg is TRUE for "'" and "g'".
7375 */
7376 static void
7377nv_gomark(cap)
7378 cmdarg_T *cap;
7379{
7380 pos_T *pos;
7381 int c;
7382#ifdef FEAT_FOLDING
7383 linenr_T lnum = curwin->w_cursor.lnum;
7384 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7385#endif
7386
7387 if (cap->cmdchar == 'g')
7388 c = cap->extra_char;
7389 else
7390 c = cap->nchar;
7391 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7392 if (pos == (pos_T *)-1) /* jumped to other file */
7393 {
7394 if (cap->arg)
7395 {
7396 check_cursor_lnum();
7397 beginline(BL_WHITE | BL_FIX);
7398 }
7399 else
7400 check_cursor();
7401 }
7402 else
7403 nv_cursormark(cap, cap->arg, pos);
7404
7405#ifdef FEAT_VIRTUALEDIT
7406 /* May need to clear the coladd that a mark includes. */
7407 if (!virtual_active())
7408 curwin->w_cursor.coladd = 0;
7409#endif
7410#ifdef FEAT_FOLDING
7411 if (cap->oap->op_type == OP_NOP
7412 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7413 && (fdo_flags & FDO_MARK)
7414 && old_KeyTyped)
7415 foldOpenCursor();
7416#endif
7417}
7418
7419/*
7420 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7421 */
7422 static void
7423nv_pcmark(cap)
7424 cmdarg_T *cap;
7425{
7426#ifdef FEAT_JUMPLIST
7427 pos_T *pos;
7428# ifdef FEAT_FOLDING
7429 linenr_T lnum = curwin->w_cursor.lnum;
7430 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7431# endif
7432
7433 if (!checkclearopq(cap->oap))
7434 {
7435 if (cap->cmdchar == 'g')
7436 pos = movechangelist((int)cap->count1);
7437 else
7438 pos = movemark((int)cap->count1);
7439 if (pos == (pos_T *)-1) /* jump to other file */
7440 {
7441 curwin->w_set_curswant = TRUE;
7442 check_cursor();
7443 }
7444 else if (pos != NULL) /* can jump */
7445 nv_cursormark(cap, FALSE, pos);
7446 else if (cap->cmdchar == 'g')
7447 {
7448 if (curbuf->b_changelistlen == 0)
7449 EMSG(_("E664: changelist is empty"));
7450 else if (cap->count1 < 0)
7451 EMSG(_("E662: At start of changelist"));
7452 else
7453 EMSG(_("E663: At end of changelist"));
7454 }
7455 else
7456 clearopbeep(cap->oap);
7457# ifdef FEAT_FOLDING
7458 if (cap->oap->op_type == OP_NOP
7459 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7460 && (fdo_flags & FDO_MARK)
7461 && old_KeyTyped)
7462 foldOpenCursor();
7463# endif
7464 }
7465#else
7466 clearopbeep(cap->oap);
7467#endif
7468}
7469
7470/*
7471 * Handle '"' command.
7472 */
7473 static void
7474nv_regname(cap)
7475 cmdarg_T *cap;
7476{
7477 if (checkclearop(cap->oap))
7478 return;
7479#ifdef FEAT_EVAL
7480 if (cap->nchar == '=')
7481 cap->nchar = get_expr_register();
7482#endif
7483 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7484 {
7485 cap->oap->regname = cap->nchar;
7486 cap->opcount = cap->count0; /* remember count before '"' */
7487#ifdef FEAT_EVAL
7488 set_reg_var(cap->oap->regname);
7489#endif
7490 }
7491 else
7492 clearopbeep(cap->oap);
7493}
7494
7495#ifdef FEAT_VISUAL
7496/*
7497 * Handle "v", "V" and "CTRL-V" commands.
7498 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7499 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 */
7502 static void
7503nv_visual(cap)
7504 cmdarg_T *cap;
7505{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007506 if (cap->cmdchar == Ctrl_Q)
7507 cap->cmdchar = Ctrl_V;
7508
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7510 * characterwise, linewise, or blockwise. */
7511 if (cap->oap->op_type != OP_NOP)
7512 {
7513 cap->oap->motion_force = cap->cmdchar;
7514 finish_op = FALSE; /* operator doesn't finish now but later */
7515 return;
7516 }
7517
7518 VIsual_select = cap->arg;
7519 if (VIsual_active) /* change Visual mode */
7520 {
7521 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7522 end_visual_mode();
7523 else /* toggle char/block mode */
7524 { /* or char/line mode */
7525 VIsual_mode = cap->cmdchar;
7526 showmode();
7527 }
7528 redraw_curbuf_later(INVERTED); /* update the inversion */
7529 }
7530 else /* start Visual mode */
7531 {
7532 check_visual_highlight();
7533 if (cap->count0) /* use previously selected part */
7534 {
7535 if (resel_VIsual_mode == NUL) /* there is none */
7536 {
7537 beep_flush();
7538 return;
7539 }
7540 VIsual = curwin->w_cursor;
7541
7542 VIsual_active = TRUE;
7543 VIsual_reselect = TRUE;
7544 if (!cap->arg)
7545 /* start Select mode when 'selectmode' contains "cmd" */
7546 may_start_select('c');
7547#ifdef FEAT_MOUSE
7548 setmouse();
7549#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007550 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 redraw_cmdline = TRUE; /* show visual mode later */
7552 /*
7553 * For V and ^V, we multiply the number of lines even if there
7554 * was only one -- webb
7555 */
7556 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7557 {
7558 curwin->w_cursor.lnum +=
7559 resel_VIsual_line_count * cap->count0 - 1;
7560 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7561 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7562 }
7563 VIsual_mode = resel_VIsual_mode;
7564 if (VIsual_mode == 'v')
7565 {
7566 if (resel_VIsual_line_count <= 1)
7567 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7568 else
7569 curwin->w_cursor.col = resel_VIsual_col;
7570 check_cursor_col();
7571 }
7572 if (resel_VIsual_col == MAXCOL)
7573 {
7574 curwin->w_curswant = MAXCOL;
7575 coladvance((colnr_T)MAXCOL);
7576 }
7577 else if (VIsual_mode == Ctrl_V)
7578 {
7579 validate_virtcol();
7580 curwin->w_curswant = curwin->w_virtcol
7581 + resel_VIsual_col * cap->count0 - 1;
7582 coladvance(curwin->w_curswant);
7583 }
7584 else
7585 curwin->w_set_curswant = TRUE;
7586 redraw_curbuf_later(INVERTED); /* show the inversion */
7587 }
7588 else
7589 {
7590 if (!cap->arg)
7591 /* start Select mode when 'selectmode' contains "cmd" */
7592 may_start_select('c');
7593 n_start_visual_mode(cap->cmdchar);
7594 }
7595 }
7596}
7597
7598/*
7599 * Start selection for Shift-movement keys.
7600 */
7601 void
7602start_selection()
7603{
7604 /* if 'selectmode' contains "key", start Select mode */
7605 may_start_select('k');
7606 n_start_visual_mode('v');
7607}
7608
7609/*
7610 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7611 */
7612 void
7613may_start_select(c)
7614 int c;
7615{
7616 VIsual_select = (stuff_empty() && typebuf_typed()
7617 && (vim_strchr(p_slm, c) != NULL));
7618}
7619
7620/*
7621 * Start Visual mode "c".
7622 * Should set VIsual_select before calling this.
7623 */
7624 static void
7625n_start_visual_mode(c)
7626 int c;
7627{
7628 VIsual_mode = c;
7629 VIsual_active = TRUE;
7630 VIsual_reselect = TRUE;
7631#ifdef FEAT_VIRTUALEDIT
7632 /* Corner case: the 0 position in a tab may change when going into
7633 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7634 */
7635 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7636 coladvance(curwin->w_virtcol);
7637#endif
7638 VIsual = curwin->w_cursor;
7639
7640#ifdef FEAT_FOLDING
7641 foldAdjustVisual();
7642#endif
7643
7644#ifdef FEAT_MOUSE
7645 setmouse();
7646#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007647 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 redraw_cmdline = TRUE; /* show visual mode later */
7649#ifdef FEAT_CLIPBOARD
7650 /* Make sure the clipboard gets updated. Needed because start and
7651 * end may still be the same, and the selection needs to be owned */
7652 clip_star.vmode = NUL;
7653#endif
7654
7655 /* Only need to redraw this line, unless still need to redraw an old
7656 * Visual area (when 'lazyredraw' is set). */
7657 if (curwin->w_redr_type < INVERTED)
7658 {
7659 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7660 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7661 }
7662}
7663
7664#endif /* FEAT_VISUAL */
7665
7666/*
7667 * CTRL-W: Window commands
7668 */
7669 static void
7670nv_window(cap)
7671 cmdarg_T *cap;
7672{
7673#ifdef FEAT_WINDOWS
7674 if (!checkclearop(cap->oap))
7675 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7676#else
7677 (void)checkclearop(cap->oap);
7678#endif
7679}
7680
7681/*
7682 * CTRL-Z: Suspend
7683 */
7684 static void
7685nv_suspend(cap)
7686 cmdarg_T *cap;
7687{
7688 clearop(cap->oap);
7689#ifdef FEAT_VISUAL
7690 if (VIsual_active)
7691 end_visual_mode(); /* stop Visual mode */
7692#endif
7693 do_cmdline_cmd((char_u *)"st");
7694}
7695
7696/*
7697 * Commands starting with "g".
7698 */
7699 static void
7700nv_g_cmd(cap)
7701 cmdarg_T *cap;
7702{
7703 oparg_T *oap = cap->oap;
7704#ifdef FEAT_VISUAL
7705 pos_T tpos;
7706#endif
7707 int i;
7708 int flag = FALSE;
7709
7710 switch (cap->nchar)
7711 {
7712#ifdef MEM_PROFILE
7713 /*
7714 * "g^A": dump log of used memory.
7715 */
7716 case Ctrl_A:
7717 vim_mem_profile_dump();
7718 break;
7719#endif
7720
7721#ifdef FEAT_VREPLACE
7722 /*
7723 * "gR": Enter virtual replace mode.
7724 */
7725 case 'R':
7726 cap->arg = TRUE;
7727 nv_Replace(cap);
7728 break;
7729
7730 case 'r':
7731 nv_vreplace(cap);
7732 break;
7733#endif
7734
7735 case '&':
7736 do_cmdline_cmd((char_u *)"%s//~/&");
7737 break;
7738
7739#ifdef FEAT_VISUAL
7740 /*
7741 * "gv": Reselect the previous Visual area. If Visual already active,
7742 * exchange previous and current Visual area.
7743 */
7744 case 'v':
7745 if (checkclearop(oap))
7746 break;
7747
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007748 if ( curbuf->b_visual.vi_start.lnum == 0
7749 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7750 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 beep_flush();
7752 else
7753 {
7754 /* set w_cursor to the start of the Visual area, tpos to the end */
7755 if (VIsual_active)
7756 {
7757 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007758 VIsual_mode = curbuf->b_visual.vi_mode;
7759 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760# ifdef FEAT_EVAL
7761 curbuf->b_visual_mode_eval = i;
7762# endif
7763 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007764 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7765 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007767 tpos = curbuf->b_visual.vi_end;
7768 curbuf->b_visual.vi_end = curwin->w_cursor;
7769 curwin->w_cursor = curbuf->b_visual.vi_start;
7770 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 else
7773 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007774 VIsual_mode = curbuf->b_visual.vi_mode;
7775 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7776 tpos = curbuf->b_visual.vi_end;
7777 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779
7780 VIsual_active = TRUE;
7781 VIsual_reselect = TRUE;
7782
7783 /* Set Visual to the start and w_cursor to the end of the Visual
7784 * area. Make sure they are on an existing character. */
7785 check_cursor();
7786 VIsual = curwin->w_cursor;
7787 curwin->w_cursor = tpos;
7788 check_cursor();
7789 update_topline();
7790 /*
7791 * When called from normal "g" command: start Select mode when
7792 * 'selectmode' contains "cmd". When called for K_SELECT, always
7793 * start Select mode.
7794 */
7795 if (cap->arg)
7796 VIsual_select = TRUE;
7797 else
7798 may_start_select('c');
7799#ifdef FEAT_MOUSE
7800 setmouse();
7801#endif
7802#ifdef FEAT_CLIPBOARD
7803 /* Make sure the clipboard gets updated. Needed because start and
7804 * end are still the same, and the selection needs to be owned */
7805 clip_star.vmode = NUL;
7806#endif
7807 redraw_curbuf_later(INVERTED);
7808 showmode();
7809 }
7810 break;
7811 /*
7812 * "gV": Don't reselect the previous Visual area after a Select mode
7813 * mapping of menu.
7814 */
7815 case 'V':
7816 VIsual_reselect = FALSE;
7817 break;
7818
7819 /*
7820 * "gh": start Select mode.
7821 * "gH": start Select line mode.
7822 * "g^H": start Select block mode.
7823 */
7824 case K_BS:
7825 cap->nchar = Ctrl_H;
7826 /* FALLTHROUGH */
7827 case 'h':
7828 case 'H':
7829 case Ctrl_H:
7830# ifdef EBCDIC
7831 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7832 if (cap->nchar == Ctrl_H)
7833 cap->cmdchar = Ctrl_V;
7834 else
7835# endif
7836 cap->cmdchar = cap->nchar + ('v' - 'h');
7837 cap->arg = TRUE;
7838 nv_visual(cap);
7839 break;
7840#endif /* FEAT_VISUAL */
7841
7842 /*
7843 * "gj" and "gk" two new funny movement keys -- up and down
7844 * movement based on *screen* line rather than *file* line.
7845 */
7846 case 'j':
7847 case K_DOWN:
7848 /* with 'nowrap' it works just like the normal "j" command; also when
7849 * in a closed fold */
7850 if (!curwin->w_p_wrap
7851#ifdef FEAT_FOLDING
7852 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7853#endif
7854 )
7855 {
7856 oap->motion_type = MLINE;
7857 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7858 }
7859 else
7860 i = nv_screengo(oap, FORWARD, cap->count1);
7861 if (i == FAIL)
7862 clearopbeep(oap);
7863 break;
7864
7865 case 'k':
7866 case K_UP:
7867 /* with 'nowrap' it works just like the normal "k" command; also when
7868 * in a closed fold */
7869 if (!curwin->w_p_wrap
7870#ifdef FEAT_FOLDING
7871 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7872#endif
7873 )
7874 {
7875 oap->motion_type = MLINE;
7876 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7877 }
7878 else
7879 i = nv_screengo(oap, BACKWARD, cap->count1);
7880 if (i == FAIL)
7881 clearopbeep(oap);
7882 break;
7883
7884 /*
7885 * "gJ": join two lines without inserting a space.
7886 */
7887 case 'J':
7888 nv_join(cap);
7889 break;
7890
7891 /*
7892 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7893 * "gm": middle of "g0" and "g$".
7894 */
7895 case '^':
7896 flag = TRUE;
7897 /* FALLTHROUGH */
7898
7899 case '0':
7900 case 'm':
7901 case K_HOME:
7902 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 oap->motion_type = MCHAR;
7904 oap->inclusive = FALSE;
7905 if (curwin->w_p_wrap
7906#ifdef FEAT_VERTSPLIT
7907 && curwin->w_width != 0
7908#endif
7909 )
7910 {
7911 int width1 = W_WIDTH(curwin) - curwin_col_off();
7912 int width2 = width1 + curwin_col_off2();
7913
7914 validate_virtcol();
7915 i = 0;
7916 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7917 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7918 }
7919 else
7920 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007921 /* Go to the middle of the screen line. When 'number' or
7922 * 'relativenumber' is on and lines are wrapping the middle can be more
7923 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 if (cap->nchar == 'm')
7925 i += (W_WIDTH(curwin) - curwin_col_off()
7926 + ((curwin->w_p_wrap && i > 0)
7927 ? curwin_col_off2() : 0)) / 2;
7928 coladvance((colnr_T)i);
7929 if (flag)
7930 {
7931 do
7932 i = gchar_cursor();
7933 while (vim_iswhite(i) && oneright() == OK);
7934 }
7935 curwin->w_set_curswant = TRUE;
7936 break;
7937
7938 case '_':
7939 /* "g_": to the last non-blank character in the line or <count> lines
7940 * downward. */
7941 cap->oap->motion_type = MCHAR;
7942 cap->oap->inclusive = TRUE;
7943 curwin->w_curswant = MAXCOL;
7944 if (cursor_down((long)(cap->count1 - 1),
7945 cap->oap->op_type == OP_NOP) == FAIL)
7946 clearopbeep(cap->oap);
7947 else
7948 {
7949 char_u *ptr = ml_get_curline();
7950
7951 /* In Visual mode we may end up after the line. */
7952 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7953 --curwin->w_cursor.col;
7954
7955 /* Decrease the cursor column until it's on a non-blank. */
7956 while (curwin->w_cursor.col > 0
7957 && vim_iswhite(ptr[curwin->w_cursor.col]))
7958 --curwin->w_cursor.col;
7959 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01007960#ifdef FEAT_VISUAL
7961 adjust_for_sel(cap);
7962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 break;
7965
7966 case '$':
7967 case K_END:
7968 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
7970 int col_off = curwin_col_off();
7971
7972 oap->motion_type = MCHAR;
7973 oap->inclusive = TRUE;
7974 if (curwin->w_p_wrap
7975#ifdef FEAT_VERTSPLIT
7976 && curwin->w_width != 0
7977#endif
7978 )
7979 {
7980 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7981 if (cap->count1 == 1)
7982 {
7983 int width1 = W_WIDTH(curwin) - col_off;
7984 int width2 = width1 + curwin_col_off2();
7985
7986 validate_virtcol();
7987 i = width1 - 1;
7988 if (curwin->w_virtcol >= (colnr_T)width1)
7989 i += ((curwin->w_virtcol - width1) / width2 + 1)
7990 * width2;
7991 coladvance((colnr_T)i);
7992#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7993 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7994 {
7995 /*
7996 * Check for landing on a character that got split at
7997 * the end of the line. We do not want to advance to
7998 * the next screen line.
7999 */
8000 validate_virtcol();
8001 if (curwin->w_virtcol > (colnr_T)i)
8002 --curwin->w_cursor.col;
8003 }
8004#endif
8005 }
8006 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8007 clearopbeep(oap);
8008 }
8009 else
8010 {
8011 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8012 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008013
8014 /* Make sure we stick in this column. */
8015 validate_virtcol();
8016 curwin->w_curswant = curwin->w_virtcol;
8017 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 }
8020 break;
8021
8022 /*
8023 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8024 */
8025 case '*':
8026 case '#':
8027#if POUND != '#'
8028 case POUND: /* pound sign (sometimes equal to '#') */
8029#endif
8030 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8031 case ']': /* :tselect for current identifier */
8032 nv_ident(cap);
8033 break;
8034
8035 /*
8036 * ge and gE: go back to end of word
8037 */
8038 case 'e':
8039 case 'E':
8040 oap->motion_type = MCHAR;
8041 curwin->w_set_curswant = TRUE;
8042 oap->inclusive = TRUE;
8043 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8044 clearopbeep(oap);
8045 break;
8046
8047 /*
8048 * "g CTRL-G": display info about cursor position
8049 */
8050 case Ctrl_G:
8051 cursor_pos_info();
8052 break;
8053
8054 /*
8055 * "gi": start Insert at the last position.
8056 */
8057 case 'i':
8058 if (curbuf->b_last_insert.lnum != 0)
8059 {
8060 curwin->w_cursor = curbuf->b_last_insert;
8061 check_cursor_lnum();
8062 i = (int)STRLEN(ml_get_curline());
8063 if (curwin->w_cursor.col > (colnr_T)i)
8064 {
8065#ifdef FEAT_VIRTUALEDIT
8066 if (virtual_active())
8067 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8068#endif
8069 curwin->w_cursor.col = i;
8070 }
8071 }
8072 cap->cmdchar = 'i';
8073 nv_edit(cap);
8074 break;
8075
8076 /*
8077 * "gI": Start insert in column 1.
8078 */
8079 case 'I':
8080 beginline(0);
8081 if (!checkclearopq(oap))
8082 invoke_edit(cap, FALSE, 'g', FALSE);
8083 break;
8084
8085#ifdef FEAT_SEARCHPATH
8086 /*
8087 * "gf": goto file, edit file under cursor
8088 * "]f" and "[f": can also be used.
8089 */
8090 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008091 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 nv_gotofile(cap);
8093 break;
8094#endif
8095
8096 /* "g'm" and "g`m": jump to mark without setting pcmark */
8097 case '\'':
8098 cap->arg = TRUE;
8099 /*FALLTHROUGH*/
8100 case '`':
8101 nv_gomark(cap);
8102 break;
8103
8104 /*
8105 * "gs": Goto sleep.
8106 */
8107 case 's':
8108 do_sleep(cap->count1 * 1000L);
8109 break;
8110
8111 /*
8112 * "ga": Display the ascii value of the character under the
8113 * cursor. It is displayed in decimal, hex, and octal. -- webb
8114 */
8115 case 'a':
8116 do_ascii(NULL);
8117 break;
8118
8119#ifdef FEAT_MBYTE
8120 /*
8121 * "g8": Display the bytes used for the UTF-8 character under the
8122 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008123 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 */
8125 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008126 if (cap->count0 == 8)
8127 utf_find_illegal();
8128 else
8129 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 break;
8131#endif
8132
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008133 case '<':
8134 show_sb_text();
8135 break;
8136
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 /*
8138 * "gg": Goto the first line in file. With a count it goes to
8139 * that line number like for "G". -- webb
8140 */
8141 case 'g':
8142 cap->arg = FALSE;
8143 nv_goto(cap);
8144 break;
8145
8146 /*
8147 * Two-character operators:
8148 * "gq" Format text
8149 * "gw" Format text and keep cursor position
8150 * "g~" Toggle the case of the text.
8151 * "gu" Change text to lower case.
8152 * "gU" Change text to upper case.
8153 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008154 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 */
8156 case 'q':
8157 case 'w':
8158 oap->cursor_start = curwin->w_cursor;
8159 /*FALLTHROUGH*/
8160 case '~':
8161 case 'u':
8162 case 'U':
8163 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008164 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 nv_operator(cap);
8166 break;
8167
8168 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008169 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 * current function
8171 * "gD": idem, but in the current file.
8172 */
8173 case 'd':
8174 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008175 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 break;
8177
8178#ifdef FEAT_MOUSE
8179 /*
8180 * g<*Mouse> : <C-*mouse>
8181 */
8182 case K_MIDDLEMOUSE:
8183 case K_MIDDLEDRAG:
8184 case K_MIDDLERELEASE:
8185 case K_LEFTMOUSE:
8186 case K_LEFTDRAG:
8187 case K_LEFTRELEASE:
8188 case K_RIGHTMOUSE:
8189 case K_RIGHTDRAG:
8190 case K_RIGHTRELEASE:
8191 case K_X1MOUSE:
8192 case K_X1DRAG:
8193 case K_X1RELEASE:
8194 case K_X2MOUSE:
8195 case K_X2DRAG:
8196 case K_X2RELEASE:
8197 mod_mask = MOD_MASK_CTRL;
8198 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8199 break;
8200#endif
8201
8202 case K_IGNORE:
8203 break;
8204
8205 /*
8206 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8207 */
8208 case 'p':
8209 case 'P':
8210 nv_put(cap);
8211 break;
8212
8213#ifdef FEAT_BYTEOFF
8214 /* "go": goto byte count from start of buffer */
8215 case 'o':
8216 goto_byte(cap->count0);
8217 break;
8218#endif
8219
8220 /* "gQ": improved Ex mode */
8221 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008222 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {
8224 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008225 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 break;
8227 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008228
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 if (!checkclearopq(oap))
8230 do_exmode(TRUE);
8231 break;
8232
8233#ifdef FEAT_JUMPLIST
8234 case ',':
8235 nv_pcmark(cap);
8236 break;
8237
8238 case ';':
8239 cap->count1 = -cap->count1;
8240 nv_pcmark(cap);
8241 break;
8242#endif
8243
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008244#ifdef FEAT_WINDOWS
8245 case 't':
8246 goto_tabpage((int)cap->count0);
8247 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008248 case 'T':
8249 goto_tabpage(-(int)cap->count1);
8250 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008251#endif
8252
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008253 case '+':
8254 case '-': /* "g+" and "g-": undo or redo along the timeline */
8255 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008256 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8257 FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008258 break;
8259
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 default:
8261 clearopbeep(oap);
8262 break;
8263 }
8264}
8265
8266/*
8267 * Handle "o" and "O" commands.
8268 */
8269 static void
8270n_opencmd(cap)
8271 cmdarg_T *cap;
8272{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008273#ifdef FEAT_CONCEAL
8274 linenr_T oldline = curwin->w_cursor.lnum;
8275#endif
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 if (!checkclearopq(cap->oap))
8278 {
8279#ifdef FEAT_FOLDING
8280 if (cap->cmdchar == 'O')
8281 /* Open above the first line of a folded sequence of lines */
8282 (void)hasFolding(curwin->w_cursor.lnum,
8283 &curwin->w_cursor.lnum, NULL);
8284 else
8285 /* Open below the last line of a folded sequence of lines */
8286 (void)hasFolding(curwin->w_cursor.lnum,
8287 NULL, &curwin->w_cursor.lnum);
8288#endif
8289 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8290 (cap->cmdchar == 'O' ? 1 : 0)),
8291 (linenr_T)(curwin->w_cursor.lnum +
8292 (cap->cmdchar == 'o' ? 1 : 0))
8293 ) == OK
8294 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8295#ifdef FEAT_COMMENTS
8296 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8297#endif
8298 0, 0))
8299 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008300#ifdef FEAT_CONCEAL
8301 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8302 update_single_line(curwin, oldline);
8303#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008304 /* When '#' is in 'cpoptions' ignore the count. */
8305 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8306 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8308 }
8309 }
8310}
8311
8312/*
8313 * "." command: redo last change.
8314 */
8315 static void
8316nv_dot(cap)
8317 cmdarg_T *cap;
8318{
8319 if (!checkclearopq(cap->oap))
8320 {
8321 /*
8322 * If "restart_edit" is TRUE, the last but one command is repeated
8323 * instead of the last command (inserting text). This is used for
8324 * CTRL-O <.> in insert mode.
8325 */
8326 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8327 clearopbeep(cap->oap);
8328 }
8329}
8330
8331/*
8332 * CTRL-R: undo undo
8333 */
8334 static void
8335nv_redo(cap)
8336 cmdarg_T *cap;
8337{
8338 if (!checkclearopq(cap->oap))
8339 {
8340 u_redo((int)cap->count1);
8341 curwin->w_set_curswant = TRUE;
8342 }
8343}
8344
8345/*
8346 * Handle "U" command.
8347 */
8348 static void
8349nv_Undo(cap)
8350 cmdarg_T *cap;
8351{
8352 /* In Visual mode and typing "gUU" triggers an operator */
8353 if (cap->oap->op_type == OP_UPPER
8354#ifdef FEAT_VISUAL
8355 || VIsual_active
8356#endif
8357 )
8358 {
8359 /* translate "gUU" to "gUgU" */
8360 cap->cmdchar = 'g';
8361 cap->nchar = 'U';
8362 nv_operator(cap);
8363 }
8364 else if (!checkclearopq(cap->oap))
8365 {
8366 u_undoline();
8367 curwin->w_set_curswant = TRUE;
8368 }
8369}
8370
8371/*
8372 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8373 * single character.
8374 */
8375 static void
8376nv_tilde(cap)
8377 cmdarg_T *cap;
8378{
8379 if (!p_to
8380#ifdef FEAT_VISUAL
8381 && !VIsual_active
8382#endif
8383 && cap->oap->op_type != OP_TILDE)
8384 n_swapchar(cap);
8385 else
8386 nv_operator(cap);
8387}
8388
8389/*
8390 * Handle an operator command.
8391 * The actual work is done by do_pending_operator().
8392 */
8393 static void
8394nv_operator(cap)
8395 cmdarg_T *cap;
8396{
8397 int op_type;
8398
8399 op_type = get_op_type(cap->cmdchar, cap->nchar);
8400
8401 if (op_type == cap->oap->op_type) /* double operator works on lines */
8402 nv_lineop(cap);
8403 else if (!checkclearop(cap->oap))
8404 {
8405 cap->oap->start = curwin->w_cursor;
8406 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008407#ifdef FEAT_EVAL
8408 set_op_var(op_type);
8409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 }
8411}
8412
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008413#ifdef FEAT_EVAL
8414/*
8415 * Set v:operator to the characters for "optype".
8416 */
8417 static void
8418set_op_var(optype)
8419 int optype;
8420{
8421 char_u opchars[3];
8422
8423 if (optype == OP_NOP)
8424 set_vim_var_string(VV_OP, NULL, 0);
8425 else
8426 {
8427 opchars[0] = get_op_char(optype);
8428 opchars[1] = get_extra_op_char(optype);
8429 opchars[2] = NUL;
8430 set_vim_var_string(VV_OP, opchars, -1);
8431 }
8432}
8433#endif
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435/*
8436 * Handle linewise operator "dd", "yy", etc.
8437 *
8438 * "_" is is a strange motion command that helps make operators more logical.
8439 * It is actually implemented, but not documented in the real Vi. This motion
8440 * command actually refers to "the current line". Commands like "dd" and "yy"
8441 * are really an alternate form of "d_" and "y_". It does accept a count, so
8442 * "d3_" works to delete 3 lines.
8443 */
8444 static void
8445nv_lineop(cap)
8446 cmdarg_T *cap;
8447{
8448 cap->oap->motion_type = MLINE;
8449 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8450 clearopbeep(cap->oap);
8451 else if ( cap->oap->op_type == OP_DELETE
8452 || cap->oap->op_type == OP_LSHIFT
8453 || cap->oap->op_type == OP_RSHIFT)
8454 beginline(BL_SOL | BL_FIX);
8455 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8456 beginline(BL_WHITE | BL_FIX);
8457}
8458
8459/*
8460 * <Home> command.
8461 */
8462 static void
8463nv_home(cap)
8464 cmdarg_T *cap;
8465{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008466 /* CTRL-HOME is like "gg" */
8467 if (mod_mask & MOD_MASK_CTRL)
8468 nv_goto(cap);
8469 else
8470 {
8471 cap->count0 = 1;
8472 nv_pipe(cap);
8473 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008474 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8475 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476}
8477
8478/*
8479 * "|" command.
8480 */
8481 static void
8482nv_pipe(cap)
8483 cmdarg_T *cap;
8484{
8485 cap->oap->motion_type = MCHAR;
8486 cap->oap->inclusive = FALSE;
8487 beginline(0);
8488 if (cap->count0 > 0)
8489 {
8490 coladvance((colnr_T)(cap->count0 - 1));
8491 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8492 }
8493 else
8494 curwin->w_curswant = 0;
8495 /* keep curswant at the column where we wanted to go, not where
8496 we ended; differs if line is too short */
8497 curwin->w_set_curswant = FALSE;
8498}
8499
8500/*
8501 * Handle back-word command "b" and "B".
8502 * cap->arg is 1 for "B"
8503 */
8504 static void
8505nv_bck_word(cap)
8506 cmdarg_T *cap;
8507{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008508#ifdef FEAT_CONCEAL
8509 linenr_T oldline = curwin->w_cursor.lnum;
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 cap->oap->motion_type = MCHAR;
8512 cap->oap->inclusive = FALSE;
8513 curwin->w_set_curswant = TRUE;
8514 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8515 clearopbeep(cap->oap);
8516#ifdef FEAT_FOLDING
8517 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8518 foldOpenCursor();
8519#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02008520#ifdef FEAT_CONCEAL
8521 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8522 {
8523 update_single_line(curwin, oldline);
8524 update_single_line(curwin, curwin->w_cursor.lnum);
8525 }
8526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527}
8528
8529/*
8530 * Handle word motion commands "e", "E", "w" and "W".
8531 * cap->arg is TRUE for "E" and "W".
8532 */
8533 static void
8534nv_wordcmd(cap)
8535 cmdarg_T *cap;
8536{
8537 int n;
8538 int word_end;
8539 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008540 pos_T startpos = curwin->w_cursor;
Bram Moolenaar860cae12010-06-05 23:22:07 +02008541#ifdef FEAT_CONCEAL
8542 linenr_T oldline = startpos.lnum;
8543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
8545 /*
8546 * Set inclusive for the "E" and "e" command.
8547 */
8548 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8549 word_end = TRUE;
8550 else
8551 word_end = FALSE;
8552 cap->oap->inclusive = word_end;
8553
8554 /*
8555 * "cw" and "cW" are a special case.
8556 */
8557 if (!word_end && cap->oap->op_type == OP_CHANGE)
8558 {
8559 n = gchar_cursor();
8560 if (n != NUL) /* not an empty line */
8561 {
8562 if (vim_iswhite(n))
8563 {
8564 /*
8565 * Reproduce a funny Vi behaviour: "cw" on a blank only
8566 * changes one character, not all blanks until the start of
8567 * the next word. Only do this when the 'w' flag is included
8568 * in 'cpoptions'.
8569 */
8570 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8571 {
8572 cap->oap->inclusive = TRUE;
8573 cap->oap->motion_type = MCHAR;
8574 return;
8575 }
8576 }
8577 else
8578 {
8579 /*
8580 * This is a little strange. To match what the real Vi does,
8581 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8582 * that we are not on a space or a TAB. This seems impolite
8583 * at first, but it's really more what we mean when we say
8584 * 'cw'.
8585 * Another strangeness: When standing on the end of a word
8586 * "ce" will change until the end of the next wordt, but "cw"
8587 * will change only one character! This is done by setting
8588 * flag.
8589 */
8590 cap->oap->inclusive = TRUE;
8591 word_end = TRUE;
8592 flag = TRUE;
8593 }
8594 }
8595 }
8596
8597 cap->oap->motion_type = MCHAR;
8598 curwin->w_set_curswant = TRUE;
8599 if (word_end)
8600 n = end_word(cap->count1, cap->arg, flag, FALSE);
8601 else
8602 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8603
Bram Moolenaardfefb982008-04-01 10:06:39 +00008604 /* Don't leave the cursor on the NUL past the end of line. Unless we
8605 * didn't move it forward. */
8606 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008607 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609 if (n == FAIL && cap->oap->op_type == OP_NOP)
8610 clearopbeep(cap->oap);
8611 else
8612 {
8613#ifdef FEAT_VISUAL
8614 adjust_for_sel(cap);
8615#endif
8616#ifdef FEAT_FOLDING
8617 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8618 foldOpenCursor();
8619#endif
8620 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02008621#ifdef FEAT_CONCEAL
8622 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8623 {
8624 update_single_line(curwin, oldline);
8625 update_single_line(curwin, curwin->w_cursor.lnum);
8626 }
8627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628}
8629
8630/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008631 * Used after a movement command: If the cursor ends up on the NUL after the
8632 * end of the line, may move it back to the last character and make the motion
8633 * inclusive.
8634 */
8635 static void
8636adjust_cursor(oap)
8637 oparg_T *oap;
8638{
8639 /* The cursor cannot remain on the NUL when:
8640 * - the column is > 0
8641 * - not in Visual mode or 'selection' is "o"
8642 * - 'virtualedit' is not "all" and not "onemore".
8643 */
8644 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8645#ifdef FEAT_VISUAL
8646 && (!VIsual_active || *p_sel == 'o')
8647#endif
8648#ifdef FEAT_VIRTUALEDIT
8649 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8650#endif
8651 )
8652 {
8653 --curwin->w_cursor.col;
8654#ifdef FEAT_MBYTE
8655 /* prevent cursor from moving on the trail byte */
8656 if (has_mbyte)
8657 mb_adjust_cursor();
8658#endif
8659 oap->inclusive = TRUE;
8660 }
8661}
8662
8663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 * "0" and "^" commands.
8665 * cap->arg is the argument for beginline().
8666 */
8667 static void
8668nv_beginline(cap)
8669 cmdarg_T *cap;
8670{
8671 cap->oap->motion_type = MCHAR;
8672 cap->oap->inclusive = FALSE;
8673 beginline(cap->arg);
8674#ifdef FEAT_FOLDING
8675 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8676 foldOpenCursor();
8677#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008678 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8679 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680}
8681
8682#ifdef FEAT_VISUAL
8683/*
8684 * In exclusive Visual mode, may include the last character.
8685 */
8686 static void
8687adjust_for_sel(cap)
8688 cmdarg_T *cap;
8689{
8690 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8691 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8692 {
8693# ifdef FEAT_MBYTE
8694 if (has_mbyte)
8695 inc_cursor();
8696 else
8697# endif
8698 ++curwin->w_cursor.col;
8699 cap->oap->inclusive = FALSE;
8700 }
8701}
8702
8703/*
8704 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8705 * Should check VIsual_mode before calling this.
8706 * Returns TRUE when backed up to the previous line.
8707 */
8708 static int
8709unadjust_for_sel()
8710{
8711 pos_T *pp;
8712
8713 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8714 {
8715 if (lt(VIsual, curwin->w_cursor))
8716 pp = &curwin->w_cursor;
8717 else
8718 pp = &VIsual;
8719#ifdef FEAT_VIRTUALEDIT
8720 if (pp->coladd > 0)
8721 --pp->coladd;
8722 else
8723#endif
8724 if (pp->col > 0)
8725 {
8726 --pp->col;
8727#ifdef FEAT_MBYTE
8728 mb_adjustpos(pp);
8729#endif
8730 }
8731 else if (pp->lnum > 1)
8732 {
8733 --pp->lnum;
8734 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8735 return TRUE;
8736 }
8737 }
8738 return FALSE;
8739}
8740
8741/*
8742 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8743 */
8744 static void
8745nv_select(cap)
8746 cmdarg_T *cap;
8747{
8748 if (VIsual_active)
8749 VIsual_select = TRUE;
8750 else if (VIsual_reselect)
8751 {
8752 cap->nchar = 'v'; /* fake "gv" command */
8753 cap->arg = TRUE;
8754 nv_g_cmd(cap);
8755 }
8756}
8757
8758#endif
8759
8760/*
8761 * "G", "gg", CTRL-END, CTRL-HOME.
8762 * cap->arg is TRUE for "G".
8763 */
8764 static void
8765nv_goto(cap)
8766 cmdarg_T *cap;
8767{
8768 linenr_T lnum;
8769
8770 if (cap->arg)
8771 lnum = curbuf->b_ml.ml_line_count;
8772 else
8773 lnum = 1L;
8774 cap->oap->motion_type = MLINE;
8775 setpcmark();
8776
8777 /* When a count is given, use it instead of the default lnum */
8778 if (cap->count0 != 0)
8779 lnum = cap->count0;
8780 if (lnum < 1L)
8781 lnum = 1L;
8782 else if (lnum > curbuf->b_ml.ml_line_count)
8783 lnum = curbuf->b_ml.ml_line_count;
8784 curwin->w_cursor.lnum = lnum;
8785 beginline(BL_SOL | BL_FIX);
8786#ifdef FEAT_FOLDING
8787 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8788 foldOpenCursor();
8789#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02008790#ifdef FEAT_CONCEAL
8791 if (curwin->w_p_conceal)
8792 changed_window_setting();
8793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794}
8795
8796/*
8797 * CTRL-\ in Normal mode.
8798 */
8799 static void
8800nv_normal(cap)
8801 cmdarg_T *cap;
8802{
8803 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8804 {
8805 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008806 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 clear_cmdline = TRUE; /* unshow mode later */
8808 restart_edit = 0;
8809#ifdef FEAT_CMDWIN
8810 if (cmdwin_type != 0)
8811 cmdwin_result = Ctrl_C;
8812#endif
8813#ifdef FEAT_VISUAL
8814 if (VIsual_active)
8815 {
8816 end_visual_mode(); /* stop Visual */
8817 redraw_curbuf_later(INVERTED);
8818 }
8819#endif
8820 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8821 if (cap->nchar == Ctrl_G && p_im)
8822 restart_edit = 'a';
8823 }
8824 else
8825 clearopbeep(cap->oap);
8826}
8827
8828/*
8829 * ESC in Normal mode: beep, but don't flush buffers.
8830 * Don't even beep if we are canceling a command.
8831 */
8832 static void
8833nv_esc(cap)
8834 cmdarg_T *cap;
8835{
8836 int no_reason;
8837
8838 no_reason = (cap->oap->op_type == OP_NOP
8839 && cap->opcount == 0
8840 && cap->count0 == 0
8841 && cap->oap->regname == 0
8842 && !p_im);
8843
8844 if (cap->arg) /* TRUE for CTRL-C */
8845 {
8846 if (restart_edit == 0
8847#ifdef FEAT_CMDWIN
8848 && cmdwin_type == 0
8849#endif
8850#ifdef FEAT_VISUAL
8851 && !VIsual_active
8852#endif
8853 && no_reason)
8854 MSG(_("Type :quit<Enter> to exit Vim"));
8855
8856 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8857 * set again below when halfway a mapping. */
8858 if (!p_im)
8859 restart_edit = 0;
8860#ifdef FEAT_CMDWIN
8861 if (cmdwin_type != 0)
8862 {
8863 cmdwin_result = K_IGNORE;
8864 got_int = FALSE; /* don't stop executing autocommands et al. */
8865 return;
8866 }
8867#endif
8868 }
8869
8870#ifdef FEAT_VISUAL
8871 if (VIsual_active)
8872 {
8873 end_visual_mode(); /* stop Visual */
8874 check_cursor_col(); /* make sure cursor is not beyond EOL */
8875 curwin->w_set_curswant = TRUE;
8876 redraw_curbuf_later(INVERTED);
8877 }
8878 else
8879#endif
8880 if (no_reason)
8881 vim_beep();
8882 clearop(cap->oap);
8883
8884 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8885 * set return to Insert mode afterwards. */
8886 if (restart_edit == 0 && goto_im()
8887#ifdef FEAT_EX_EXTRA
8888 && ex_normal_busy == 0
8889#endif
8890 )
8891 restart_edit = 'a';
8892}
8893
8894/*
8895 * Handle "A", "a", "I", "i" and <Insert> commands.
8896 */
8897 static void
8898nv_edit(cap)
8899 cmdarg_T *cap;
8900{
8901 /* <Insert> is equal to "i" */
8902 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8903 cap->cmdchar = 'i';
8904
8905#ifdef FEAT_VISUAL
8906 /* in Visual mode "A" and "I" are an operator */
8907 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8908 v_visop(cap);
8909
8910 /* in Visual mode and after an operator "a" and "i" are for text objects */
8911 else
8912#endif
8913 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8914 && (cap->oap->op_type != OP_NOP
8915#ifdef FEAT_VISUAL
8916 || VIsual_active
8917#endif
8918 ))
8919 {
8920#ifdef FEAT_TEXTOBJ
8921 nv_object(cap);
8922#else
8923 clearopbeep(cap->oap);
8924#endif
8925 }
8926 else if (!curbuf->b_p_ma && !p_im)
8927 {
8928 /* Only give this error when 'insertmode' is off. */
8929 EMSG(_(e_modifiable));
8930 clearop(cap->oap);
8931 }
8932 else if (!checkclearopq(cap->oap))
8933 {
8934 switch (cap->cmdchar)
8935 {
8936 case 'A': /* "A"ppend after the line */
8937 curwin->w_set_curswant = TRUE;
8938#ifdef FEAT_VIRTUALEDIT
8939 if (ve_flags == VE_ALL)
8940 {
8941 int save_State = State;
8942
8943 /* Pretent Insert mode here to allow the cursor on the
8944 * character past the end of the line */
8945 State = INSERT;
8946 coladvance((colnr_T)MAXCOL);
8947 State = save_State;
8948 }
8949 else
8950#endif
8951 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8952 break;
8953
8954 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008955 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8956 beginline(BL_WHITE);
8957 else
8958 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 break;
8960
8961 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8962#ifdef FEAT_VIRTUALEDIT
8963 /* increment coladd when in virtual space, increment the
8964 * column otherwise, also to append after an unprintable char */
8965 if (virtual_active()
8966 && (curwin->w_cursor.coladd > 0
8967 || *ml_get_cursor() == NUL
8968 || *ml_get_cursor() == TAB))
8969 curwin->w_cursor.coladd++;
8970 else
8971#endif
8972 if (*ml_get_cursor() != NUL)
8973 inc_cursor();
8974 break;
8975 }
8976
8977#ifdef FEAT_VIRTUALEDIT
8978 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8979 {
8980 int save_State = State;
8981
8982 /* Pretent Insert mode here to allow the cursor on the
8983 * character past the end of the line */
8984 State = INSERT;
8985 coladvance(getviscol());
8986 State = save_State;
8987 }
8988#endif
8989
8990 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8991 }
8992}
8993
8994/*
8995 * Invoke edit() and take care of "restart_edit" and the return value.
8996 */
8997 static void
8998invoke_edit(cap, repl, cmd, startln)
8999 cmdarg_T *cap;
9000 int repl; /* "r" or "gr" command */
9001 int cmd;
9002 int startln;
9003{
9004 int restart_edit_save = 0;
9005
9006 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9007 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9008 * it. */
9009 if (repl || !stuff_empty())
9010 restart_edit_save = restart_edit;
9011 else
9012 restart_edit_save = 0;
9013
9014 /* Always reset "restart_edit", this is not a restarted edit. */
9015 restart_edit = 0;
9016
9017 if (edit(cmd, startln, cap->count1))
9018 cap->retval |= CA_COMMAND_BUSY;
9019
9020 if (restart_edit == 0)
9021 restart_edit = restart_edit_save;
9022}
9023
9024#ifdef FEAT_TEXTOBJ
9025/*
9026 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9027 */
9028 static void
9029nv_object(cap)
9030 cmdarg_T *cap;
9031{
9032 int flag;
9033 int include;
9034 char_u *mps_save;
9035
9036 if (cap->cmdchar == 'i')
9037 include = FALSE; /* "ix" = inner object: exclude white space */
9038 else
9039 include = TRUE; /* "ax" = an object: include white space */
9040
9041 /* Make sure (), [], {} and <> are in 'matchpairs' */
9042 mps_save = curbuf->b_p_mps;
9043 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9044
9045 switch (cap->nchar)
9046 {
9047 case 'w': /* "aw" = a word */
9048 flag = current_word(cap->oap, cap->count1, include, FALSE);
9049 break;
9050 case 'W': /* "aW" = a WORD */
9051 flag = current_word(cap->oap, cap->count1, include, TRUE);
9052 break;
9053 case 'b': /* "ab" = a braces block */
9054 case '(':
9055 case ')':
9056 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9057 break;
9058 case 'B': /* "aB" = a Brackets block */
9059 case '{':
9060 case '}':
9061 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9062 break;
9063 case '[': /* "a[" = a [] block */
9064 case ']':
9065 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9066 break;
9067 case '<': /* "a<" = a <> block */
9068 case '>':
9069 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9070 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009071 case 't': /* "at" = a tag block (xml and html) */
9072 flag = current_tagblock(cap->oap, cap->count1, include);
9073 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 case 'p': /* "ap" = a paragraph */
9075 flag = current_par(cap->oap, cap->count1, include, 'p');
9076 break;
9077 case 's': /* "as" = a sentence */
9078 flag = current_sent(cap->oap, cap->count1, include);
9079 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009080 case '"': /* "a"" = a double quoted string */
9081 case '\'': /* "a'" = a single quoted string */
9082 case '`': /* "a`" = a backtick quoted string */
9083 flag = current_quote(cap->oap, cap->count1, include,
9084 cap->nchar);
9085 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086#if 0 /* TODO */
9087 case 'S': /* "aS" = a section */
9088 case 'f': /* "af" = a filename */
9089 case 'u': /* "au" = a URL */
9090#endif
9091 default:
9092 flag = FAIL;
9093 break;
9094 }
9095
9096 curbuf->b_p_mps = mps_save;
9097 if (flag == FAIL)
9098 clearopbeep(cap->oap);
9099 adjust_cursor_col();
9100 curwin->w_set_curswant = TRUE;
9101}
9102#endif
9103
9104/*
9105 * "q" command: Start/stop recording.
9106 * "q:", "q/", "q?": edit command-line in command-line window.
9107 */
9108 static void
9109nv_record(cap)
9110 cmdarg_T *cap;
9111{
9112 if (cap->oap->op_type == OP_FORMAT)
9113 {
9114 /* "gqq" is the same as "gqgq": format line */
9115 cap->cmdchar = 'g';
9116 cap->nchar = 'q';
9117 nv_operator(cap);
9118 }
9119 else if (!checkclearop(cap->oap))
9120 {
9121#ifdef FEAT_CMDWIN
9122 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9123 {
9124 stuffcharReadbuff(cap->nchar);
9125 stuffcharReadbuff(K_CMDWIN);
9126 }
9127 else
9128#endif
9129 /* (stop) recording into a named register, unless executing a
9130 * register */
9131 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9132 clearopbeep(cap->oap);
9133 }
9134}
9135
9136/*
9137 * Handle the "@r" command.
9138 */
9139 static void
9140nv_at(cap)
9141 cmdarg_T *cap;
9142{
9143 if (checkclearop(cap->oap))
9144 return;
9145#ifdef FEAT_EVAL
9146 if (cap->nchar == '=')
9147 {
9148 if (get_expr_register() == NUL)
9149 return;
9150 }
9151#endif
9152 while (cap->count1-- && !got_int)
9153 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009154 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
9156 clearopbeep(cap->oap);
9157 break;
9158 }
9159 line_breakcheck();
9160 }
9161}
9162
9163/*
9164 * Handle the CTRL-U and CTRL-D commands.
9165 */
9166 static void
9167nv_halfpage(cap)
9168 cmdarg_T *cap;
9169{
9170 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9171 || (cap->cmdchar == Ctrl_D
9172 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9173 clearopbeep(cap->oap);
9174 else if (!checkclearop(cap->oap))
9175 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9176}
9177
9178/*
9179 * Handle "J" or "gJ" command.
9180 */
9181 static void
9182nv_join(cap)
9183 cmdarg_T *cap;
9184{
9185#ifdef FEAT_VISUAL
9186 if (VIsual_active) /* join the visual lines */
9187 nv_operator(cap);
9188 else
9189#endif
9190 if (!checkclearop(cap->oap))
9191 {
9192 if (cap->count0 <= 1)
9193 cap->count0 = 2; /* default for join is two lines! */
9194 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9195 curbuf->b_ml.ml_line_count)
9196 clearopbeep(cap->oap); /* beyond last line */
9197 else
9198 {
9199 prep_redo(cap->oap->regname, cap->count0,
9200 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9201 do_do_join(cap->count0, cap->nchar == NUL);
9202 }
9203 }
9204}
9205
9206/*
9207 * "P", "gP", "p" and "gp" commands.
9208 */
9209 static void
9210nv_put(cap)
9211 cmdarg_T *cap;
9212{
9213#ifdef FEAT_VISUAL
9214 int regname = 0;
9215 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009216 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009217 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02009219#ifdef FEAT_CONCEAL
9220 linenr_T oldline = curwin->w_cursor.lnum;
9221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 int dir;
9223 int flags = 0;
9224
9225 if (cap->oap->op_type != OP_NOP)
9226 {
9227#ifdef FEAT_DIFF
9228 /* "dp" is ":diffput" */
9229 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9230 {
9231 clearop(cap->oap);
9232 nv_diffgetput(TRUE);
9233 }
9234 else
9235#endif
9236 clearopbeep(cap->oap);
9237 }
9238 else
9239 {
9240 dir = (cap->cmdchar == 'P'
9241 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9242 ? BACKWARD : FORWARD;
9243 prep_redo_cmd(cap);
9244 if (cap->cmdchar == 'g')
9245 flags |= PUT_CURSEND;
9246
9247#ifdef FEAT_VISUAL
9248 if (VIsual_active)
9249 {
9250 /* Putting in Visual mode: The put text replaces the selected
9251 * text. First delete the selected text, then put the new text.
9252 * Need to save and restore the registers that the delete
9253 * overwrites if the old contents is being put.
9254 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009255 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 regname = cap->oap->regname;
9257# ifdef FEAT_CLIPBOARD
9258 adjust_clip_reg(&regname);
9259# endif
9260 if (regname == 0 || VIM_ISDIGIT(regname)
9261# ifdef FEAT_CLIPBOARD
9262 || (clip_unnamed && (regname == '*' || regname == '+'))
9263# endif
9264
9265 )
9266 {
9267 /* the delete is going to overwrite the register we want to
9268 * put, save it first. */
9269 reg1 = get_register(regname, TRUE);
9270 }
9271
9272 /* Now delete the selected text. */
9273 cap->cmdchar = 'd';
9274 cap->nchar = NUL;
9275 cap->oap->regname = NUL;
9276 nv_operator(cap);
9277 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009278 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279
9280 /* delete PUT_LINE_BACKWARD; */
9281 cap->oap->regname = regname;
9282
9283 if (reg1 != NULL)
9284 {
9285 /* Delete probably changed the register we want to put, save
9286 * it first. Then put back what was there before the delete. */
9287 reg2 = get_register(regname, FALSE);
9288 put_register(regname, reg1);
9289 }
9290
9291 /* When deleted a linewise Visual area, put the register as
9292 * lines to avoid it joined with the next line. When deletion was
9293 * characterwise, split a line when putting lines. */
9294 if (VIsual_mode == 'V')
9295 flags |= PUT_LINE;
9296 else if (VIsual_mode == 'v')
9297 flags |= PUT_LINE_SPLIT;
9298 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9299 flags |= PUT_LINE_FORWARD;
9300 dir = BACKWARD;
9301 if ((VIsual_mode != 'V'
9302 && curwin->w_cursor.col < curbuf->b_op_start.col)
9303 || (VIsual_mode == 'V'
9304 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9305 /* cursor is at the end of the line or end of file, put
9306 * forward. */
9307 dir = FORWARD;
9308 }
9309#endif
9310 do_put(cap->oap->regname, dir, cap->count1, flags);
9311
9312#ifdef FEAT_VISUAL
9313 /* If a register was saved, put it back now. */
9314 if (reg2 != NULL)
9315 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009316
9317 /* What to reselect with "gv"? Selecting the just put text seems to
9318 * be the most useful, since the original text was removed. */
9319 if (was_visual)
9320 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009321 curbuf->b_visual.vi_start = curbuf->b_op_start;
9322 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009323 }
9324
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009325 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009326 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009327 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009328 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009329 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009330
9331 /* If the cursor was in that line, move it to the end of the last
9332 * line. */
9333 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9334 {
9335 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9336 coladvance((colnr_T)MAXCOL);
9337 }
9338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339#endif
9340 auto_format(FALSE, TRUE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009341#ifdef FEAT_CONCEAL
9342 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9343 {
9344 update_single_line(curwin, oldline);
9345 update_single_line(curwin, curwin->w_cursor.lnum);
9346 }
9347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 }
9349}
9350
9351/*
9352 * "o" and "O" commands.
9353 */
9354 static void
9355nv_open(cap)
9356 cmdarg_T *cap;
9357{
9358#ifdef FEAT_DIFF
9359 /* "do" is ":diffget" */
9360 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9361 {
9362 clearop(cap->oap);
9363 nv_diffgetput(FALSE);
9364 }
9365 else
9366#endif
9367#ifdef FEAT_VISUAL
9368 if (VIsual_active) /* switch start and end of visual */
9369 v_swap_corners(cap->cmdchar);
9370 else
9371#endif
9372 n_opencmd(cap);
9373}
9374
9375#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 static void
9377nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009378 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 ProcessSniffRequests();
9381}
9382#endif
9383
9384#ifdef FEAT_NETBEANS_INTG
9385 static void
9386nv_nbcmd(cap)
9387 cmdarg_T *cap;
9388{
9389 netbeans_keycommand(cap->nchar);
9390}
9391#endif
9392
9393#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 static void
9395nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009396 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9399}
9400#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009401
9402#ifdef FEAT_AUTOCMD
9403/*
9404 * Trigger CursorHold event.
9405 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9406 * input buffer. "did_cursorhold" is set to avoid retriggering.
9407 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009408 static void
9409nv_cursorhold(cap)
9410 cmdarg_T *cap;
9411{
9412 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9413 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009414 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009415}
9416#endif