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