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