blob: e310a372514f125bad1efcd95f4ef5e6b47cb387 [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));
153#ifdef FEAT_VISUAL
154static void adjust_for_sel __ARGS((cmdarg_T *cap));
155static int unadjust_for_sel __ARGS((void));
156static void nv_select __ARGS((cmdarg_T *cap));
157#endif
158static void nv_goto __ARGS((cmdarg_T *cap));
159static void nv_normal __ARGS((cmdarg_T *cap));
160static void nv_esc __ARGS((cmdarg_T *oap));
161static void nv_edit __ARGS((cmdarg_T *cap));
162static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
163#ifdef FEAT_TEXTOBJ
164static void nv_object __ARGS((cmdarg_T *cap));
165#endif
166static void nv_record __ARGS((cmdarg_T *cap));
167static void nv_at __ARGS((cmdarg_T *cap));
168static void nv_halfpage __ARGS((cmdarg_T *cap));
169static void nv_join __ARGS((cmdarg_T *cap));
170static void nv_put __ARGS((cmdarg_T *cap));
171static void nv_open __ARGS((cmdarg_T *cap));
172#ifdef FEAT_SNIFF
173static void nv_sniff __ARGS((cmdarg_T *cap));
174#endif
175#ifdef FEAT_NETBEANS_INTG
176static void nv_nbcmd __ARGS((cmdarg_T *cap));
177#endif
178#ifdef FEAT_DND
179static void nv_drop __ARGS((cmdarg_T *cap));
180#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000181#ifdef FEAT_AUTOCMD
182static void nv_cursorhold __ARGS((cmdarg_T *cap));
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185/*
186 * Function to be called for a Normal or Visual mode command.
187 * The argument is a cmdarg_T.
188 */
189typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
190
191/* Values for cmd_flags. */
192#define NV_NCH 0x01 /* may need to get a second char */
193#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
194#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
195#define NV_LANG 0x08 /* second char needs language adjustment */
196
197#define NV_SS 0x10 /* may start selection */
198#define NV_SSS 0x20 /* may start selection with shift modifier */
199#define NV_STS 0x40 /* may stop selection without shift modif. */
200#define NV_RL 0x80 /* 'rightleft' modifies command */
201#define NV_KEEPREG 0x100 /* don't clear regname */
202#define NV_NCW 0x200 /* not allowed in command-line window */
203
204/*
205 * Generally speaking, every Normal mode command should either clear any
206 * pending operator (with *clearop*()), or set the motion type variable
207 * oap->motion_type.
208 *
209 * When a cursor motion command is made, it is marked as being a character or
210 * line oriented motion. Then, if an operator is in effect, the operation
211 * becomes character or line oriented accordingly.
212 */
213
214/*
215 * This table contains one entry for every Normal or Visual mode command.
216 * The order doesn't matter, init_normal_cmds() will create a sorted index.
217 * It is faster when all keys from zero to '~' are present.
218 */
219static const struct nv_cmd
220{
221 int cmd_char; /* (first) command character */
222 nv_func_T cmd_func; /* function for this command */
223 short_u cmd_flags; /* NV_ flags */
224 short cmd_arg; /* value for ca.arg */
225} nv_cmds[] =
226{
227 {NUL, nv_error, 0, 0},
228 {Ctrl_A, nv_addsub, 0, 0},
229 {Ctrl_B, nv_page, NV_STS, BACKWARD},
230 {Ctrl_C, nv_esc, 0, TRUE},
231 {Ctrl_D, nv_halfpage, 0, 0},
232 {Ctrl_E, nv_scroll_line, 0, TRUE},
233 {Ctrl_F, nv_page, NV_STS, FORWARD},
234 {Ctrl_G, nv_ctrlg, 0, 0},
235 {Ctrl_H, nv_ctrlh, 0, 0},
236 {Ctrl_I, nv_pcmark, 0, 0},
237 {NL, nv_down, 0, FALSE},
238 {Ctrl_K, nv_error, 0, 0},
239 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000240 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {Ctrl_N, nv_down, NV_STS, FALSE},
242 {Ctrl_O, nv_ctrlo, 0, 0},
243 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000244#ifdef FEAT_VISUAL
245 {Ctrl_Q, nv_visual, 0, FALSE},
246#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 {Ctrl_R, nv_redo, 0, 0},
250 {Ctrl_S, nv_ignore, 0, 0},
251 {Ctrl_T, nv_tagpop, NV_NCW, 0},
252 {Ctrl_U, nv_halfpage, 0, 0},
253#ifdef FEAT_VISUAL
254 {Ctrl_V, nv_visual, 0, FALSE},
255 {'V', nv_visual, 0, FALSE},
256 {'v', nv_visual, 0, FALSE},
257#else
258 {Ctrl_V, nv_error, 0, 0},
259 {'V', nv_error, 0, 0},
260 {'v', nv_error, 0, 0},
261#endif
262 {Ctrl_W, nv_window, 0, 0},
263 {Ctrl_X, nv_addsub, 0, 0},
264 {Ctrl_Y, nv_scroll_line, 0, FALSE},
265 {Ctrl_Z, nv_suspend, 0, 0},
266 {ESC, nv_esc, 0, FALSE},
267 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
268 {Ctrl_RSB, nv_ident, NV_NCW, 0},
269 {Ctrl_HAT, nv_hat, NV_NCW, 0},
270 {Ctrl__, nv_error, 0, 0},
271 {' ', nv_right, 0, 0},
272 {'!', nv_operator, 0, 0},
273 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
274 {'#', nv_ident, 0, 0},
275 {'$', nv_dollar, 0, 0},
276 {'%', nv_percent, 0, 0},
277 {'&', nv_optrans, 0, 0},
278 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
279 {'(', nv_brace, 0, BACKWARD},
280 {')', nv_brace, 0, FORWARD},
281 {'*', nv_ident, 0, 0},
282 {'+', nv_down, 0, TRUE},
283 {',', nv_csearch, 0, TRUE},
284 {'-', nv_up, 0, TRUE},
285 {'.', nv_dot, NV_KEEPREG, 0},
286 {'/', nv_search, 0, FALSE},
287 {'0', nv_beginline, 0, 0},
288 {'1', nv_ignore, 0, 0},
289 {'2', nv_ignore, 0, 0},
290 {'3', nv_ignore, 0, 0},
291 {'4', nv_ignore, 0, 0},
292 {'5', nv_ignore, 0, 0},
293 {'6', nv_ignore, 0, 0},
294 {'7', nv_ignore, 0, 0},
295 {'8', nv_ignore, 0, 0},
296 {'9', nv_ignore, 0, 0},
297 {':', nv_colon, 0, 0},
298 {';', nv_csearch, 0, FALSE},
299 {'<', nv_operator, NV_RL, 0},
300 {'=', nv_operator, 0, 0},
301 {'>', nv_operator, NV_RL, 0},
302 {'?', nv_search, 0, FALSE},
303 {'@', nv_at, NV_NCH_NOP, FALSE},
304 {'A', nv_edit, 0, 0},
305 {'B', nv_bck_word, 0, 1},
306 {'C', nv_abbrev, NV_KEEPREG, 0},
307 {'D', nv_abbrev, NV_KEEPREG, 0},
308 {'E', nv_wordcmd, 0, TRUE},
309 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
310 {'G', nv_goto, 0, TRUE},
311 {'H', nv_scroll, 0, 0},
312 {'I', nv_edit, 0, 0},
313 {'J', nv_join, 0, 0},
314 {'K', nv_ident, 0, 0},
315 {'L', nv_scroll, 0, 0},
316 {'M', nv_scroll, 0, 0},
317 {'N', nv_next, 0, SEARCH_REV},
318 {'O', nv_open, 0, 0},
319 {'P', nv_put, 0, 0},
320 {'Q', nv_exmode, NV_NCW, 0},
321 {'R', nv_Replace, 0, FALSE},
322 {'S', nv_subst, NV_KEEPREG, 0},
323 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
324 {'U', nv_Undo, 0, 0},
325 {'W', nv_wordcmd, 0, TRUE},
326 {'X', nv_abbrev, NV_KEEPREG, 0},
327 {'Y', nv_abbrev, NV_KEEPREG, 0},
328 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
329 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
330 {'\\', nv_error, 0, 0},
331 {']', nv_brackets, NV_NCH_ALW, FORWARD},
332 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
333 {'_', nv_lineop, 0, 0},
334 {'`', nv_gomark, NV_NCH_ALW, FALSE},
335 {'a', nv_edit, NV_NCH, 0},
336 {'b', nv_bck_word, 0, 0},
337 {'c', nv_operator, 0, 0},
338 {'d', nv_operator, 0, 0},
339 {'e', nv_wordcmd, 0, FALSE},
340 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
341 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
342 {'h', nv_left, NV_RL, 0},
343 {'i', nv_edit, NV_NCH, 0},
344 {'j', nv_down, 0, FALSE},
345 {'k', nv_up, 0, FALSE},
346 {'l', nv_right, NV_RL, 0},
347 {'m', nv_mark, NV_NCH_NOP, 0},
348 {'n', nv_next, 0, 0},
349 {'o', nv_open, 0, 0},
350 {'p', nv_put, 0, 0},
351 {'q', nv_record, NV_NCH, 0},
352 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
353 {'s', nv_subst, NV_KEEPREG, 0},
354 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
355 {'u', nv_undo, 0, 0},
356 {'w', nv_wordcmd, 0, FALSE},
357 {'x', nv_abbrev, NV_KEEPREG, 0},
358 {'y', nv_operator, 0, 0},
359 {'z', nv_zet, NV_NCH_ALW, 0},
360 {'{', nv_findpar, 0, BACKWARD},
361 {'|', nv_pipe, 0, 0},
362 {'}', nv_findpar, 0, FORWARD},
363 {'~', nv_tilde, 0, 0},
364
365 /* pound sign */
366 {POUND, nv_ident, 0, 0},
367#ifdef FEAT_MOUSE
368 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
369 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
370 {K_LEFTMOUSE, nv_mouse, 0, 0},
371 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
372 {K_LEFTDRAG, nv_mouse, 0, 0},
373 {K_LEFTRELEASE, nv_mouse, 0, 0},
374 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
375 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
376 {K_MIDDLEDRAG, nv_mouse, 0, 0},
377 {K_MIDDLERELEASE, nv_mouse, 0, 0},
378 {K_RIGHTMOUSE, nv_mouse, 0, 0},
379 {K_RIGHTDRAG, nv_mouse, 0, 0},
380 {K_RIGHTRELEASE, nv_mouse, 0, 0},
381 {K_X1MOUSE, nv_mouse, 0, 0},
382 {K_X1DRAG, nv_mouse, 0, 0},
383 {K_X1RELEASE, nv_mouse, 0, 0},
384 {K_X2MOUSE, nv_mouse, 0, 0},
385 {K_X2DRAG, nv_mouse, 0, 0},
386 {K_X2RELEASE, nv_mouse, 0, 0},
387#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000389 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {K_INS, nv_edit, 0, 0},
391 {K_KINS, nv_edit, 0, 0},
392 {K_BS, nv_ctrlh, 0, 0},
393 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
394 {K_S_UP, nv_page, NV_SS, BACKWARD},
395 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
396 {K_S_DOWN, nv_page, NV_SS, FORWARD},
397 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
398 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
399 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
400 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
401 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
402 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
403 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
404 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
405 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
406 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
407 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
408 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {K_S_END, nv_end, NV_SS, FALSE},
410 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
411 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
412 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {K_S_HOME, nv_home, NV_SS, 0},
414 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
415 {K_DEL, nv_abbrev, 0, 0},
416 {K_KDEL, nv_abbrev, 0, 0},
417 {K_UNDO, nv_kundo, 0, 0},
418 {K_HELP, nv_help, NV_NCW, 0},
419 {K_F1, nv_help, NV_NCW, 0},
420 {K_XF1, nv_help, NV_NCW, 0},
421#ifdef FEAT_VISUAL
422 {K_SELECT, nv_select, 0, 0},
423#endif
424#ifdef FEAT_GUI
425 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
426 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
427#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000428#ifdef FEAT_GUI_TABLINE
429 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000430 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_FKMAP
433 {K_F8, farsi_fkey, 0, 0},
434 {K_F9, farsi_fkey, 0, 0},
435#endif
436#ifdef FEAT_SNIFF
437 {K_SNIFF, nv_sniff, 0, 0},
438#endif
439#ifdef FEAT_NETBEANS_INTG
440 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
441#endif
442#ifdef FEAT_DND
443 {K_DROP, nv_drop, NV_STS, 0},
444#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000445#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000446 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448};
449
450/* Number of commands in nv_cmds[]. */
451#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
452
453/* Sorted index of commands in nv_cmds[]. */
454static short nv_cmd_idx[NV_CMDS_SIZE];
455
456/* The highest index for which
457 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
458static int nv_max_linear;
459
460/*
461 * Compare functions for qsort() below, that checks the command character
462 * through the index in nv_cmd_idx[].
463 */
464 static int
465#ifdef __BORLANDC__
466_RTLENTRYF
467#endif
468nv_compare(s1, s2)
469 const void *s1;
470 const void *s2;
471{
472 int c1, c2;
473
474 /* The commands are sorted on absolute value. */
475 c1 = nv_cmds[*(const short *)s1].cmd_char;
476 c2 = nv_cmds[*(const short *)s2].cmd_char;
477 if (c1 < 0)
478 c1 = -c1;
479 if (c2 < 0)
480 c2 = -c2;
481 return c1 - c2;
482}
483
484/*
485 * Initialize the nv_cmd_idx[] table.
486 */
487 void
488init_normal_cmds()
489{
490 int i;
491
492 /* Fill the index table with a one to one relation. */
493 for (i = 0; i < NV_CMDS_SIZE; ++i)
494 nv_cmd_idx[i] = i;
495
496 /* Sort the commands by the command character. */
497 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
498
499 /* Find the first entry that can't be indexed by the command character. */
500 for (i = 0; i < NV_CMDS_SIZE; ++i)
501 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
502 break;
503 nv_max_linear = i - 1;
504}
505
506/*
507 * Search for a command in the commands table.
508 * Returns -1 for invalid command.
509 */
510 static int
511find_command(cmdchar)
512 int cmdchar;
513{
514 int i;
515 int idx;
516 int top, bot;
517 int c;
518
519#ifdef FEAT_MBYTE
520 /* A multi-byte character is never a command. */
521 if (cmdchar >= 0x100)
522 return -1;
523#endif
524
525 /* We use the absolute value of the character. Special keys have a
526 * negative value, but are sorted on their absolute value. */
527 if (cmdchar < 0)
528 cmdchar = -cmdchar;
529
530 /* If the character is in the first part: The character is the index into
531 * nv_cmd_idx[]. */
532 if (cmdchar <= nv_max_linear)
533 return nv_cmd_idx[cmdchar];
534
535 /* Perform a binary search. */
536 bot = nv_max_linear + 1;
537 top = NV_CMDS_SIZE - 1;
538 idx = -1;
539 while (bot <= top)
540 {
541 i = (top + bot) / 2;
542 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
543 if (c < 0)
544 c = -c;
545 if (cmdchar == c)
546 {
547 idx = nv_cmd_idx[i];
548 break;
549 }
550 if (cmdchar > c)
551 bot = i + 1;
552 else
553 top = i - 1;
554 }
555 return idx;
556}
557
558/*
559 * Execute a command in Normal mode.
560 */
561/*ARGSUSED*/
562 void
563normal_cmd(oap, toplevel)
564 oparg_T *oap;
565 int toplevel; /* TRUE when called from main() */
566{
567 static long opcount = 0; /* ca.opcount saved here */
568 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 {
2466 got_click = FALSE; /* ignore mouse-up and drag events */
2467
Bram Moolenaarf740b292006-02-16 22:11:02 +00002468 /* click in a tab selects that tab page */
2469 if (is_click
2470# ifdef FEAT_CMDWIN
2471 && cmdwin_type == 0
2472# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002473 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002474 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002475 c1 = TabPageIdxs[mouse_col];
2476 if (c1 >= 0)
2477 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002478 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2479 {
2480 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002481 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002482 tabpage_new();
2483 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2484 }
2485 else
2486 {
2487 /* Go to specified tab page, or next one if not clicking
2488 * on a label. */
2489 goto_tabpage(c1);
2490
2491 /* It's like clicking on the status line of a window. */
2492 if (curwin != old_curwin)
2493 end_visual_mode();
2494 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002495 }
2496 else if (c1 < 0)
2497 {
2498 tabpage_T *tp;
2499
2500 /* Close the current or specified tab page. */
2501 if (c1 == -999)
2502 tp = curtab;
2503 else
2504 tp = find_tabpage(-c1);
2505 if (tp == curtab)
2506 {
2507 if (first_tabpage->tp_next != NULL)
2508 tabpage_close(FALSE);
2509 }
2510 else if (tp != NULL)
2511 tabpage_close_other(tp, FALSE);
2512 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002514 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 }
2516#endif
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2520 * right button up -> pop-up menu
2521 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002522 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 */
2524 if (mouse_model_popup())
2525 {
2526 if (which_button == MOUSE_RIGHT
2527 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2528 {
2529 /*
2530 * NOTE: Ignore right button down and drag mouse events.
2531 * Windows only shows the popup menu on the button up event.
2532 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002533#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2534 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (!is_click)
2536 return FALSE;
2537#endif
2538#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2539 if (is_click || is_drag)
2540 return FALSE;
2541#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002542#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2544 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2545 if (gui.in_use)
2546 {
2547 jump_flags = 0;
2548 if (STRCMP(p_mousem, "popup_setpos") == 0)
2549 {
2550 /* First set the cursor position before showing the popup
2551 * menu. */
2552#ifdef FEAT_VISUAL
2553 if (VIsual_active)
2554 {
2555 pos_T m_pos;
2556
2557 /*
2558 * set MOUSE_MAY_STOP_VIS if we are outside the
2559 * selection or the current window (might have false
2560 * negative here)
2561 */
2562 if (mouse_row < W_WINROW(curwin)
2563 || mouse_row
2564 > (W_WINROW(curwin) + curwin->w_height))
2565 jump_flags = MOUSE_MAY_STOP_VIS;
2566 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2567 jump_flags = MOUSE_MAY_STOP_VIS;
2568 else
2569 {
2570 if ((lt(curwin->w_cursor, VIsual)
2571 && (lt(m_pos, curwin->w_cursor)
2572 || lt(VIsual, m_pos)))
2573 || (lt(VIsual, curwin->w_cursor)
2574 && (lt(m_pos, VIsual)
2575 || lt(curwin->w_cursor, m_pos))))
2576 {
2577 jump_flags = MOUSE_MAY_STOP_VIS;
2578 }
2579 else if (VIsual_mode == Ctrl_V)
2580 {
2581 getvcols(curwin, &curwin->w_cursor, &VIsual,
2582 &leftcol, &rightcol);
2583 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2584 if (m_pos.col < leftcol || m_pos.col > rightcol)
2585 jump_flags = MOUSE_MAY_STOP_VIS;
2586 }
2587 }
2588 }
2589 else
2590 jump_flags = MOUSE_MAY_STOP_VIS;
2591#endif
2592 }
2593 if (jump_flags)
2594 {
2595 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2596 update_curbuf(
2597#ifdef FEAT_VISUAL
2598 VIsual_active ? INVERTED :
2599#endif
2600 VALID);
2601 setcursor();
2602 out_flush(); /* Update before showing popup menu */
2603 }
2604# ifdef FEAT_MENU
2605 gui_show_popupmenu();
2606# endif
2607 return (jump_flags & CURSOR_MOVED) != 0;
2608 }
2609 else
2610 return FALSE;
2611#else
2612 return FALSE;
2613#endif
2614 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002615 if (which_button == MOUSE_LEFT
2616 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 which_button = MOUSE_RIGHT;
2619 mod_mask &= ~MOD_MASK_SHIFT;
2620 }
2621 }
2622
2623#ifdef FEAT_VISUAL
2624 if ((State & (NORMAL | INSERT))
2625 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2626 {
2627 if (which_button == MOUSE_LEFT)
2628 {
2629 if (is_click)
2630 {
2631 /* stop Visual mode for a left click in a window, but not when
2632 * on a status line */
2633 if (VIsual_active)
2634 jump_flags |= MOUSE_MAY_STOP_VIS;
2635 }
2636 else if (mouse_has(MOUSE_VISUAL))
2637 jump_flags |= MOUSE_MAY_VIS;
2638 }
2639 else if (which_button == MOUSE_RIGHT)
2640 {
2641 if (is_click && VIsual_active)
2642 {
2643 /*
2644 * Remember the start and end of visual before moving the
2645 * cursor.
2646 */
2647 if (lt(curwin->w_cursor, VIsual))
2648 {
2649 start_visual = curwin->w_cursor;
2650 end_visual = VIsual;
2651 }
2652 else
2653 {
2654 start_visual = VIsual;
2655 end_visual = curwin->w_cursor;
2656 }
2657 }
2658 jump_flags |= MOUSE_FOCUS;
2659 if (mouse_has(MOUSE_VISUAL))
2660 jump_flags |= MOUSE_MAY_VIS;
2661 }
2662 }
2663#endif
2664
2665 /*
2666 * If an operator is pending, ignore all drags and releases until the
2667 * next mouse click.
2668 */
2669 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2670 {
2671 got_click = FALSE;
2672 oap->motion_type = MCHAR;
2673 }
2674
2675 /* When releasing the button let jump_to_mouse() know. */
2676 if (!is_click && !is_drag)
2677 jump_flags |= MOUSE_RELEASED;
2678
2679 /*
2680 * JUMP!
2681 */
2682 jump_flags = jump_to_mouse(jump_flags,
2683 oap == NULL ? NULL : &(oap->inclusive), which_button);
2684 moved = (jump_flags & CURSOR_MOVED);
2685 in_status_line = (jump_flags & IN_STATUS_LINE);
2686#ifdef FEAT_VERTSPLIT
2687 in_sep_line = (jump_flags & IN_SEP_LINE);
2688#endif
2689
2690#ifdef FEAT_NETBEANS_INTG
2691 if (usingNetbeans && isNetbeansBuffer(curbuf)
2692 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2693 {
2694 int key = KEY2TERMCAP1(c);
2695
2696 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2697 || key == (int)KE_RIGHTRELEASE)
2698 netbeans_button_release(which_button);
2699 }
2700#endif
2701
2702 /* When jumping to another window, clear a pending operator. That's a bit
2703 * friendlier than beeping and not jumping to that window. */
2704 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2705 clearop(oap);
2706
2707#ifdef FEAT_FOLDING
2708 if (mod_mask == 0
2709 && !is_drag
2710 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2711 && which_button == MOUSE_LEFT)
2712 {
2713 /* open or close a fold at this line */
2714 if (jump_flags & MOUSE_FOLD_OPEN)
2715 openFold(curwin->w_cursor.lnum, 1L);
2716 else
2717 closeFold(curwin->w_cursor.lnum, 1L);
2718 /* don't move the cursor if still in the same window */
2719 if (curwin == old_curwin)
2720 curwin->w_cursor = save_cursor;
2721 }
2722#endif
2723
2724#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2725 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2726 {
2727 clip_modeless(which_button, is_click, is_drag);
2728 return FALSE;
2729 }
2730#endif
2731
2732#ifdef FEAT_VISUAL
2733 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002734 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (VIsual_active && is_drag && p_so)
2736 {
2737 /* In the very first line, allow scrolling one line */
2738 if (mouse_row == 0)
2739 mouse_dragging = 2;
2740 else
2741 mouse_dragging = 1;
2742 }
2743
2744 /* When dragging the mouse above the window, scroll down. */
2745 if (is_drag && mouse_row < 0 && !in_status_line)
2746 {
2747 scroll_redraw(FALSE, 1L);
2748 mouse_row = 0;
2749 }
2750
2751 if (start_visual.lnum) /* right click in visual mode */
2752 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002753 /* When ALT is pressed make Visual mode blockwise. */
2754 if (mod_mask & MOD_MASK_ALT)
2755 VIsual_mode = Ctrl_V;
2756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 /*
2758 * In Visual-block mode, divide the area in four, pick up the corner
2759 * that is in the quarter that the cursor is in.
2760 */
2761 if (VIsual_mode == Ctrl_V)
2762 {
2763 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2764 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2765 end_visual.col = leftcol;
2766 else
2767 end_visual.col = rightcol;
2768 if (curwin->w_cursor.lnum <
2769 (start_visual.lnum + end_visual.lnum) / 2)
2770 end_visual.lnum = end_visual.lnum;
2771 else
2772 end_visual.lnum = start_visual.lnum;
2773
2774 /* move VIsual to the right column */
2775 start_visual = curwin->w_cursor; /* save the cursor pos */
2776 curwin->w_cursor = end_visual;
2777 coladvance(end_visual.col);
2778 VIsual = curwin->w_cursor;
2779 curwin->w_cursor = start_visual; /* restore the cursor */
2780 }
2781 else
2782 {
2783 /*
2784 * If the click is before the start of visual, change the start.
2785 * If the click is after the end of visual, change the end. If
2786 * the click is inside the visual, change the closest side.
2787 */
2788 if (lt(curwin->w_cursor, start_visual))
2789 VIsual = end_visual;
2790 else if (lt(end_visual, curwin->w_cursor))
2791 VIsual = start_visual;
2792 else
2793 {
2794 /* In the same line, compare column number */
2795 if (end_visual.lnum == start_visual.lnum)
2796 {
2797 if (curwin->w_cursor.col - start_visual.col >
2798 end_visual.col - curwin->w_cursor.col)
2799 VIsual = start_visual;
2800 else
2801 VIsual = end_visual;
2802 }
2803
2804 /* In different lines, compare line number */
2805 else
2806 {
2807 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2808 (end_visual.lnum - curwin->w_cursor.lnum);
2809
2810 if (diff > 0) /* closest to end */
2811 VIsual = start_visual;
2812 else if (diff < 0) /* closest to start */
2813 VIsual = end_visual;
2814 else /* in the middle line */
2815 {
2816 if (curwin->w_cursor.col <
2817 (start_visual.col + end_visual.col) / 2)
2818 VIsual = end_visual;
2819 else
2820 VIsual = start_visual;
2821 }
2822 }
2823 }
2824 }
2825 }
2826 /*
2827 * If Visual mode started in insert mode, execute "CTRL-O"
2828 */
2829 else if ((State & INSERT) && VIsual_active)
2830 stuffcharReadbuff(Ctrl_O);
2831#endif
2832
2833 /*
2834 * Middle mouse click: Put text before cursor.
2835 */
2836 if (which_button == MOUSE_MIDDLE)
2837 {
2838#ifdef FEAT_CLIPBOARD
2839 if (clip_star.available && regname == 0)
2840 regname = '*';
2841#endif
2842 if (yank_register_mline(regname))
2843 {
2844 if (mouse_past_bottom)
2845 dir = FORWARD;
2846 }
2847 else if (mouse_past_eol)
2848 dir = FORWARD;
2849
2850 if (fixindent)
2851 {
2852 c1 = (dir == BACKWARD) ? '[' : ']';
2853 c2 = 'p';
2854 }
2855 else
2856 {
2857 c1 = (dir == FORWARD) ? 'p' : 'P';
2858 c2 = NUL;
2859 }
2860 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2861
2862 /*
2863 * Remember where the paste started, so in edit() Insstart can be set
2864 * to this position
2865 */
2866 if (restart_edit != 0)
2867 where_paste_started = curwin->w_cursor;
2868 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2869 }
2870
2871#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2872 /*
2873 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2874 * error under the mouse pointer.
2875 */
2876 else if (((mod_mask & MOD_MASK_CTRL)
2877 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2878 && bt_quickfix(curbuf))
2879 {
2880 if (State & INSERT)
2881 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002882 if (curwin->w_llist_ref == NULL) /* quickfix window */
2883 stuffReadbuff((char_u *)":.cc\n");
2884 else /* location list window */
2885 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 got_click = FALSE; /* ignore drag&release now */
2887 }
2888#endif
2889
2890 /*
2891 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2892 * under the mouse pointer.
2893 */
2894 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2895 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2896 {
2897 if (State & INSERT)
2898 stuffcharReadbuff(Ctrl_O);
2899 stuffcharReadbuff(Ctrl_RSB);
2900 got_click = FALSE; /* ignore drag&release now */
2901 }
2902
2903 /*
2904 * Shift-Mouse click searches for the next occurrence of the word under
2905 * the mouse pointer
2906 */
2907 else if ((mod_mask & MOD_MASK_SHIFT))
2908 {
2909 if (State & INSERT
2910#ifdef FEAT_VISUAL
2911 || (VIsual_active && VIsual_select)
2912#endif
2913 )
2914 stuffcharReadbuff(Ctrl_O);
2915 if (which_button == MOUSE_LEFT)
2916 stuffcharReadbuff('*');
2917 else /* MOUSE_RIGHT */
2918 stuffcharReadbuff('#');
2919 }
2920
2921 /* Handle double clicks, unless on status line */
2922 else if (in_status_line)
2923 {
2924#ifdef FEAT_MOUSESHAPE
2925 if ((is_drag || is_click) && !drag_status_line)
2926 {
2927 drag_status_line = TRUE;
2928 update_mouseshape(-1);
2929 }
2930#endif
2931 }
2932#ifdef FEAT_VERTSPLIT
2933 else if (in_sep_line)
2934 {
2935# ifdef FEAT_MOUSESHAPE
2936 if ((is_drag || is_click) && !drag_sep_line)
2937 {
2938 drag_sep_line = TRUE;
2939 update_mouseshape(-1);
2940 }
2941# endif
2942 }
2943#endif
2944#ifdef FEAT_VISUAL
2945 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2946 && mouse_has(MOUSE_VISUAL))
2947 {
2948 if (is_click || !VIsual_active)
2949 {
2950 if (VIsual_active)
2951 orig_cursor = VIsual;
2952 else
2953 {
2954 check_visual_highlight();
2955 VIsual = curwin->w_cursor;
2956 orig_cursor = VIsual;
2957 VIsual_active = TRUE;
2958 VIsual_reselect = TRUE;
2959 /* start Select mode if 'selectmode' contains "mouse" */
2960 may_start_select('o');
2961 setmouse();
2962 }
2963 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00002964 {
2965 /* Double click with ALT pressed makes it blockwise. */
2966 if (mod_mask & MOD_MASK_ALT)
2967 VIsual_mode = Ctrl_V;
2968 else
2969 VIsual_mode = 'v';
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2972 VIsual_mode = 'V';
2973 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2974 VIsual_mode = Ctrl_V;
2975#ifdef FEAT_CLIPBOARD
2976 /* Make sure the clipboard gets updated. Needed because start and
2977 * end may still be the same, and the selection needs to be owned */
2978 clip_star.vmode = NUL;
2979#endif
2980 }
2981 /*
2982 * A double click selects a word or a block.
2983 */
2984 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2985 {
2986 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002987 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989 if (is_click)
2990 {
2991 /* If the character under the cursor (skipping white space) is
2992 * not a word character, try finding a match and select a (),
2993 * {}, [], #if/#endif, etc. block. */
2994 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002995 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 inc(&end_visual);
2997 if (oap != NULL)
2998 oap->motion_type = MCHAR;
2999 if (oap != NULL
3000 && VIsual_mode == 'v'
3001 && !vim_iswordc(gchar_pos(&end_visual))
3002 && equalpos(curwin->w_cursor, VIsual)
3003 && (pos = findmatch(oap, NUL)) != NULL)
3004 {
3005 curwin->w_cursor = *pos;
3006 if (oap->motion_type == MLINE)
3007 VIsual_mode = 'V';
3008 else if (*p_sel == 'e')
3009 {
3010 if (lt(curwin->w_cursor, VIsual))
3011 ++VIsual.col;
3012 else
3013 ++curwin->w_cursor.col;
3014 }
3015 }
3016 }
3017
3018 if (pos == NULL && (is_click || is_drag))
3019 {
3020 /* When not found a match or when dragging: extend to include
3021 * a word. */
3022 if (lt(curwin->w_cursor, orig_cursor))
3023 {
3024 find_start_of_word(&curwin->w_cursor);
3025 find_end_of_word(&VIsual);
3026 }
3027 else
3028 {
3029 find_start_of_word(&VIsual);
3030 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3031#ifdef FEAT_MBYTE
3032 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003033 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#else
3035 ++curwin->w_cursor.col;
3036#endif
3037 find_end_of_word(&curwin->w_cursor);
3038 }
3039 }
3040 curwin->w_set_curswant = TRUE;
3041 }
3042 if (is_click)
3043 redraw_curbuf_later(INVERTED); /* update the inversion */
3044 }
3045 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003046 {
3047 if (mod_mask & MOD_MASK_ALT)
3048 VIsual_mode = Ctrl_V;
3049 else
3050 VIsual_mode = 'v';
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
3053 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003054 if ((!VIsual_active && old_active && mode_displayed)
3055 || (VIsual_active && p_smd && msg_silent == 0
3056 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 redraw_cmdline = TRUE;
3058#endif
3059
3060 return moved;
3061}
3062
3063#ifdef FEAT_VISUAL
3064/*
3065 * Move "pos" back to the start of the word it's in.
3066 */
3067 static void
3068find_start_of_word(pos)
3069 pos_T *pos;
3070{
3071 char_u *line;
3072 int cclass;
3073 int col;
3074
3075 line = ml_get(pos->lnum);
3076 cclass = get_mouse_class(line + pos->col);
3077
3078 while (pos->col > 0)
3079 {
3080 col = pos->col - 1;
3081#ifdef FEAT_MBYTE
3082 col -= (*mb_head_off)(line, line + col);
3083#endif
3084 if (get_mouse_class(line + col) != cclass)
3085 break;
3086 pos->col = col;
3087 }
3088}
3089
3090/*
3091 * Move "pos" forward to the end of the word it's in.
3092 * When 'selection' is "exclusive", the position is just after the word.
3093 */
3094 static void
3095find_end_of_word(pos)
3096 pos_T *pos;
3097{
3098 char_u *line;
3099 int cclass;
3100 int col;
3101
3102 line = ml_get(pos->lnum);
3103 if (*p_sel == 'e' && pos->col > 0)
3104 {
3105 --pos->col;
3106#ifdef FEAT_MBYTE
3107 pos->col -= (*mb_head_off)(line, line + pos->col);
3108#endif
3109 }
3110 cclass = get_mouse_class(line + pos->col);
3111 while (line[pos->col] != NUL)
3112 {
3113#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003114 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#else
3116 col = pos->col + 1;
3117#endif
3118 if (get_mouse_class(line + col) != cclass)
3119 {
3120 if (*p_sel == 'e')
3121 pos->col = col;
3122 break;
3123 }
3124 pos->col = col;
3125 }
3126}
3127
3128/*
3129 * Get class of a character for selection: same class means same word.
3130 * 0: blank
3131 * 1: punctuation groups
3132 * 2: normal word character
3133 * >2: multi-byte word character.
3134 */
3135 static int
3136get_mouse_class(p)
3137 char_u *p;
3138{
3139 int c;
3140
3141#ifdef FEAT_MBYTE
3142 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3143 return mb_get_class(p);
3144#endif
3145
3146 c = *p;
3147 if (c == ' ' || c == '\t')
3148 return 0;
3149
3150 if (vim_iswordc(c))
3151 return 2;
3152
3153 /*
3154 * There are a few special cases where we want certain combinations of
3155 * characters to be considered as a single word. These are things like
3156 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3157 * character is in it's own class.
3158 */
3159 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3160 return 1;
3161 return c;
3162}
3163#endif /* FEAT_VISUAL */
3164#endif /* FEAT_MOUSE */
3165
3166#if defined(FEAT_VISUAL) || defined(PROTO)
3167/*
3168 * Check if highlighting for visual mode is possible, give a warning message
3169 * if not.
3170 */
3171 void
3172check_visual_highlight()
3173{
3174 static int did_check = FALSE;
3175
3176 if (full_screen)
3177 {
3178 if (!did_check && hl_attr(HLF_V) == 0)
3179 MSG(_("Warning: terminal cannot highlight"));
3180 did_check = TRUE;
3181 }
3182}
3183
3184/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003185 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 * This function should ALWAYS be called to end Visual mode, except from
3187 * do_pending_operator().
3188 */
3189 void
3190end_visual_mode()
3191{
3192#ifdef FEAT_CLIPBOARD
3193 /*
3194 * If we are using the clipboard, then remember what was selected in case
3195 * we need to paste it somewhere while we still own the selection.
3196 * Only do this when the clipboard is already owned. Don't want to grab
3197 * the selection when hitting ESC.
3198 */
3199 if (clip_star.available && clip_star.owned)
3200 clip_auto_select();
3201#endif
3202
3203 VIsual_active = FALSE;
3204#ifdef FEAT_MOUSE
3205 setmouse();
3206 mouse_dragging = 0;
3207#endif
3208
3209 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003210 curbuf->b_visual.vi_mode = VIsual_mode;
3211 curbuf->b_visual.vi_start = VIsual;
3212 curbuf->b_visual.vi_end = curwin->w_cursor;
3213 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#ifdef FEAT_EVAL
3215 curbuf->b_visual_mode_eval = VIsual_mode;
3216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#ifdef FEAT_VIRTUALEDIT
3218 if (!virtual_active())
3219 curwin->w_cursor.coladd = 0;
3220#endif
3221
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003222 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 clear_cmdline = TRUE; /* unshow visual mode later */
3224#ifdef FEAT_CMDL_INFO
3225 else
3226 clear_showcmd();
3227#endif
3228
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003229 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232/*
3233 * Reset VIsual_active and VIsual_reselect.
3234 */
3235 void
3236reset_VIsual_and_resel()
3237{
3238 if (VIsual_active)
3239 {
3240 end_visual_mode();
3241 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3242 }
3243 VIsual_reselect = FALSE;
3244}
3245
3246/*
3247 * Reset VIsual_active and VIsual_reselect if it's set.
3248 */
3249 void
3250reset_VIsual()
3251{
3252 if (VIsual_active)
3253 {
3254 end_visual_mode();
3255 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3256 VIsual_reselect = FALSE;
3257 }
3258}
3259#endif /* FEAT_VISUAL */
3260
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3263
3264/*
3265 * Check for a balloon-eval special item to include when searching for an
3266 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3267 * Returns TRUE if the character at "*ptr" should be included.
3268 * "dir" is FORWARD or BACKWARD, the direction of searching.
3269 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3270 * "bnp" points to a counter for square brackets.
3271 */
3272 static int
3273find_is_eval_item(ptr, colp, bnp, dir)
3274 char_u *ptr;
3275 int *colp;
3276 int *bnp;
3277 int dir;
3278{
3279 /* Accept everything inside []. */
3280 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3281 ++*bnp;
3282 if (*bnp > 0)
3283 {
3284 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3285 --*bnp;
3286 return TRUE;
3287 }
3288
3289 /* skip over "s.var" */
3290 if (*ptr == '.')
3291 return TRUE;
3292
3293 /* two-character item: s->var */
3294 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3295 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3296 {
3297 *colp += dir;
3298 return TRUE;
3299 }
3300 return FALSE;
3301}
3302#endif
3303
3304/*
3305 * Find the identifier under or to the right of the cursor.
3306 * "find_type" can have one of three values:
3307 * FIND_IDENT: find an identifier (keyword)
3308 * FIND_STRING: find any non-white string
3309 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003310 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 *
3312 * There are three steps:
3313 * 1. Search forward for the start of an identifier/string. Doesn't move if
3314 * already on one.
3315 * 2. Search backward for the start of this identifier/string.
3316 * This doesn't match the real Vi but I like it a little better and it
3317 * shouldn't bother anyone.
3318 * 3. Search forward to the end of this identifier/string.
3319 * When FIND_IDENT isn't defined, we backup until a blank.
3320 *
3321 * Returns the length of the string, or zero if no string is found.
3322 * If a string is found, a pointer to the string is put in "*string". This
3323 * string is not always NUL terminated.
3324 */
3325 int
3326find_ident_under_cursor(string, find_type)
3327 char_u **string;
3328 int find_type;
3329{
3330 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3331 curwin->w_cursor.col, string, find_type);
3332}
3333
3334/*
3335 * Like find_ident_under_cursor(), but for any window and any position.
3336 * However: Uses 'iskeyword' from the current window!.
3337 */
3338 int
3339find_ident_at_pos(wp, lnum, startcol, string, find_type)
3340 win_T *wp;
3341 linenr_T lnum;
3342 colnr_T startcol;
3343 char_u **string;
3344 int find_type;
3345{
3346 char_u *ptr;
3347 int col = 0; /* init to shut up GCC */
3348 int i;
3349#ifdef FEAT_MBYTE
3350 int this_class = 0;
3351 int prev_class;
3352 int prevcol;
3353#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003354#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int bn = 0; /* bracket nesting */
3356#endif
3357
3358 /*
3359 * if i == 0: try to find an identifier
3360 * if i == 1: try to find any non-white string
3361 */
3362 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3363 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3364 {
3365 /*
3366 * 1. skip to start of identifier/string
3367 */
3368 col = startcol;
3369#ifdef FEAT_MBYTE
3370 if (has_mbyte)
3371 {
3372 while (ptr[col] != NUL)
3373 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003374# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 /* Stop at a ']' to evaluate "a[x]". */
3376 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3377 break;
3378# endif
3379 this_class = mb_get_class(ptr + col);
3380 if (this_class != 0 && (i == 1 || this_class != 1))
3381 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003382 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 }
3385 else
3386#endif
3387 while (ptr[col] != NUL
3388 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003389# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3391# endif
3392 )
3393 ++col;
3394
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003395#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 /* When starting on a ']' count it, so that we include the '['. */
3397 bn = ptr[col] == ']';
3398#endif
3399
3400 /*
3401 * 2. Back up to start of identifier/string.
3402 */
3403#ifdef FEAT_MBYTE
3404 if (has_mbyte)
3405 {
3406 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3409 this_class = mb_get_class((char_u *)"a");
3410 else
3411# endif
3412 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003413 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
3415 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3416 prev_class = mb_get_class(ptr + prevcol);
3417 if (this_class != prev_class
3418 && (i == 0
3419 || prev_class == 0
3420 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 && (!(find_type & FIND_EVAL)
3423 || prevcol == 0
3424 || !find_is_eval_item(ptr + prevcol, &prevcol,
3425 &bn, BACKWARD))
3426# endif
3427 )
3428 break;
3429 col = prevcol;
3430 }
3431
3432 /* If we don't want just any old string, or we've found an
3433 * identifier, stop searching. */
3434 if (this_class > 2)
3435 this_class = 2;
3436 if (!(find_type & FIND_STRING) || this_class == 2)
3437 break;
3438 }
3439 else
3440#endif
3441 {
3442 while (col > 0
3443 && ((i == 0
3444 ? vim_iswordc(ptr[col - 1])
3445 : (!vim_iswhite(ptr[col - 1])
3446 && (!(find_type & FIND_IDENT)
3447 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 || ((find_type & FIND_EVAL)
3450 && col > 1
3451 && find_is_eval_item(ptr + col - 1, &col,
3452 &bn, BACKWARD))
3453#endif
3454 ))
3455 --col;
3456
3457 /* If we don't want just any old string, or we've found an
3458 * identifier, stop searching. */
3459 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3460 break;
3461 }
3462 }
3463
3464 if (ptr[col] == NUL || (i == 0 && (
3465#ifdef FEAT_MBYTE
3466 has_mbyte ? this_class != 2 :
3467#endif
3468 !vim_iswordc(ptr[col]))))
3469 {
3470 /*
3471 * didn't find an identifier or string
3472 */
3473 if (find_type & FIND_STRING)
3474 EMSG(_("E348: No string under cursor"));
3475 else
3476 EMSG(_("E349: No identifier under cursor"));
3477 return 0;
3478 }
3479 ptr += col;
3480 *string = ptr;
3481
3482 /*
3483 * 3. Find the end if the identifier/string.
3484 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 bn = 0;
3487 startcol -= col;
3488#endif
3489 col = 0;
3490#ifdef FEAT_MBYTE
3491 if (has_mbyte)
3492 {
3493 /* Search for point of changing multibyte character class. */
3494 this_class = mb_get_class(ptr);
3495 while (ptr[col] != NUL
3496 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3497 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 || ((find_type & FIND_EVAL)
3500 && col <= (int)startcol
3501 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3502# endif
3503 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003504 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 else
3507#endif
3508 while ((i == 0 ? vim_iswordc(ptr[col])
3509 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003510# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 || ((find_type & FIND_EVAL)
3512 && col <= (int)startcol
3513 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3514# endif
3515 )
3516 {
3517 ++col;
3518 }
3519
3520 return col;
3521}
3522
3523/*
3524 * Prepare for redo of a normal command.
3525 */
3526 static void
3527prep_redo_cmd(cap)
3528 cmdarg_T *cap;
3529{
3530 prep_redo(cap->oap->regname, cap->count0,
3531 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3532}
3533
3534/*
3535 * Prepare for redo of any command.
3536 * Note that only the last argument can be a multi-byte char.
3537 */
3538 static void
3539prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3540 int regname;
3541 long num;
3542 int cmd1;
3543 int cmd2;
3544 int cmd3;
3545 int cmd4;
3546 int cmd5;
3547{
3548 ResetRedobuff();
3549 if (regname != 0) /* yank from specified buffer */
3550 {
3551 AppendCharToRedobuff('"');
3552 AppendCharToRedobuff(regname);
3553 }
3554 if (num)
3555 AppendNumberToRedobuff(num);
3556
3557 if (cmd1 != NUL)
3558 AppendCharToRedobuff(cmd1);
3559 if (cmd2 != NUL)
3560 AppendCharToRedobuff(cmd2);
3561 if (cmd3 != NUL)
3562 AppendCharToRedobuff(cmd3);
3563 if (cmd4 != NUL)
3564 AppendCharToRedobuff(cmd4);
3565 if (cmd5 != NUL)
3566 AppendCharToRedobuff(cmd5);
3567}
3568
3569/*
3570 * check for operator active and clear it
3571 *
3572 * return TRUE if operator was active
3573 */
3574 static int
3575checkclearop(oap)
3576 oparg_T *oap;
3577{
3578 if (oap->op_type == OP_NOP)
3579 return FALSE;
3580 clearopbeep(oap);
3581 return TRUE;
3582}
3583
3584/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003585 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003587 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 */
3589 static int
3590checkclearopq(oap)
3591 oparg_T *oap;
3592{
3593 if (oap->op_type == OP_NOP
3594#ifdef FEAT_VISUAL
3595 && !VIsual_active
3596#endif
3597 )
3598 return FALSE;
3599 clearopbeep(oap);
3600 return TRUE;
3601}
3602
3603 static void
3604clearop(oap)
3605 oparg_T *oap;
3606{
3607 oap->op_type = OP_NOP;
3608 oap->regname = 0;
3609 oap->motion_force = NUL;
3610 oap->use_reg_one = FALSE;
3611}
3612
3613 static void
3614clearopbeep(oap)
3615 oparg_T *oap;
3616{
3617 clearop(oap);
3618 beep_flush();
3619}
3620
3621#ifdef FEAT_VISUAL
3622/*
3623 * Remove the shift modifier from a special key.
3624 */
3625 static void
3626unshift_special(cap)
3627 cmdarg_T *cap;
3628{
3629 switch (cap->cmdchar)
3630 {
3631 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3632 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3633 case K_S_UP: cap->cmdchar = K_UP; break;
3634 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3635 case K_S_HOME: cap->cmdchar = K_HOME; break;
3636 case K_S_END: cap->cmdchar = K_END; break;
3637 }
3638 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3639}
3640#endif
3641
3642#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3643/*
3644 * Routines for displaying a partly typed command
3645 */
3646
3647#ifdef FEAT_VISUAL /* need room for size of Visual area */
3648# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3649#else
3650# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3651#endif
3652static char_u showcmd_buf[SHOWCMD_BUFLEN];
3653static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3654static int showcmd_is_clear = TRUE;
3655static int showcmd_visual = FALSE;
3656
3657static void display_showcmd __ARGS((void));
3658
3659 void
3660clear_showcmd()
3661{
3662 if (!p_sc)
3663 return;
3664
3665#ifdef FEAT_VISUAL
3666 if (VIsual_active && !char_avail())
3667 {
3668 int i = lt(VIsual, curwin->w_cursor);
3669 long lines;
3670 colnr_T leftcol, rightcol;
3671 linenr_T top, bot;
3672
3673 /* Show the size of the Visual area. */
3674 if (i)
3675 {
3676 top = VIsual.lnum;
3677 bot = curwin->w_cursor.lnum;
3678 }
3679 else
3680 {
3681 top = curwin->w_cursor.lnum;
3682 bot = VIsual.lnum;
3683 }
3684# ifdef FEAT_FOLDING
3685 /* Include closed folds as a whole. */
3686 hasFolding(top, &top, NULL);
3687 hasFolding(bot, NULL, &bot);
3688# endif
3689 lines = bot - top + 1;
3690
3691 if (VIsual_mode == Ctrl_V)
3692 {
3693 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3694 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3695 (long)(rightcol - leftcol + 1));
3696 }
3697 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3698 sprintf((char *)showcmd_buf, "%ld", lines);
3699 else
3700 sprintf((char *)showcmd_buf, "%ld", (long)(i
3701 ? curwin->w_cursor.col - VIsual.col
3702 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3703 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3704 showcmd_visual = TRUE;
3705 }
3706 else
3707#endif
3708 {
3709 showcmd_buf[0] = NUL;
3710 showcmd_visual = FALSE;
3711
3712 /* Don't actually display something if there is nothing to clear. */
3713 if (showcmd_is_clear)
3714 return;
3715 }
3716
3717 display_showcmd();
3718}
3719
3720/*
3721 * Add 'c' to string of shown command chars.
3722 * Return TRUE if output has been written (and setcursor() has been called).
3723 */
3724 int
3725add_to_showcmd(c)
3726 int c;
3727{
3728 char_u *p;
3729 int old_len;
3730 int extra_len;
3731 int overflow;
3732#if defined(FEAT_MOUSE)
3733 int i;
3734 static int ignore[] =
3735 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003736# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3738 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 K_IGNORE,
3741 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3742 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3743 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3744 K_MOUSEDOWN, K_MOUSEUP,
3745 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003746 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 0
3748 };
3749#endif
3750
Bram Moolenaar09df3122006-01-23 22:23:09 +00003751 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 return FALSE;
3753
3754 if (showcmd_visual)
3755 {
3756 showcmd_buf[0] = NUL;
3757 showcmd_visual = FALSE;
3758 }
3759
3760#if defined(FEAT_MOUSE)
3761 /* Ignore keys that are scrollbar updates and mouse clicks */
3762 if (IS_SPECIAL(c))
3763 for (i = 0; ignore[i] != 0; ++i)
3764 if (ignore[i] == c)
3765 return FALSE;
3766#endif
3767
3768 p = transchar(c);
3769 old_len = (int)STRLEN(showcmd_buf);
3770 extra_len = (int)STRLEN(p);
3771 overflow = old_len + extra_len - SHOWCMD_COLS;
3772 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003773 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3774 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 STRCAT(showcmd_buf, p);
3776
3777 if (char_avail())
3778 return FALSE;
3779
3780 display_showcmd();
3781
3782 return TRUE;
3783}
3784
3785 void
3786add_to_showcmd_c(c)
3787 int c;
3788{
3789 if (!add_to_showcmd(c))
3790 setcursor();
3791}
3792
3793/*
3794 * Delete 'len' characters from the end of the shown command.
3795 */
3796 static void
3797del_from_showcmd(len)
3798 int len;
3799{
3800 int old_len;
3801
3802 if (!p_sc)
3803 return;
3804
3805 old_len = (int)STRLEN(showcmd_buf);
3806 if (len > old_len)
3807 len = old_len;
3808 showcmd_buf[old_len - len] = NUL;
3809
3810 if (!char_avail())
3811 display_showcmd();
3812}
3813
3814/*
3815 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3816 * something and there is a partial mapping.
3817 */
3818 void
3819push_showcmd()
3820{
3821 if (p_sc)
3822 STRCPY(old_showcmd_buf, showcmd_buf);
3823}
3824
3825 void
3826pop_showcmd()
3827{
3828 if (!p_sc)
3829 return;
3830
3831 STRCPY(showcmd_buf, old_showcmd_buf);
3832
3833 display_showcmd();
3834}
3835
3836 static void
3837display_showcmd()
3838{
3839 int len;
3840
3841 cursor_off();
3842
3843 len = (int)STRLEN(showcmd_buf);
3844 if (len == 0)
3845 showcmd_is_clear = TRUE;
3846 else
3847 {
3848 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3849 showcmd_is_clear = FALSE;
3850 }
3851
3852 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003853 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3854 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
3856 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3857
3858 setcursor(); /* put cursor back where it belongs */
3859}
3860#endif
3861
3862#ifdef FEAT_SCROLLBIND
3863/*
3864 * When "check" is FALSE, prepare for commands that scroll the window.
3865 * When "check" is TRUE, take care of scroll-binding after the window has
3866 * scrolled. Called from normal_cmd() and edit().
3867 */
3868 void
3869do_check_scrollbind(check)
3870 int check;
3871{
3872 static win_T *old_curwin = NULL;
3873 static linenr_T old_topline = 0;
3874#ifdef FEAT_DIFF
3875 static int old_topfill = 0;
3876#endif
3877 static buf_T *old_buf = NULL;
3878 static colnr_T old_leftcol = 0;
3879
3880 if (check && curwin->w_p_scb)
3881 {
3882 /* If a ":syncbind" command was just used, don't scroll, only reset
3883 * the values. */
3884 if (did_syncbind)
3885 did_syncbind = FALSE;
3886 else if (curwin == old_curwin)
3887 {
3888 /*
3889 * Synchronize other windows, as necessary according to
3890 * 'scrollbind'. Don't do this after an ":edit" command, except
3891 * when 'diff' is set.
3892 */
3893 if ((curwin->w_buffer == old_buf
3894#ifdef FEAT_DIFF
3895 || curwin->w_p_diff
3896#endif
3897 )
3898 && (curwin->w_topline != old_topline
3899#ifdef FEAT_DIFF
3900 || curwin->w_topfill != old_topfill
3901#endif
3902 || curwin->w_leftcol != old_leftcol))
3903 {
3904 check_scrollbind(curwin->w_topline - old_topline,
3905 (long)(curwin->w_leftcol - old_leftcol));
3906 }
3907 }
3908 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3909 {
3910 /*
3911 * When switching between windows, make sure that the relative
3912 * vertical offset is valid for the new window. The relative
3913 * offset is invalid whenever another 'scrollbind' window has
3914 * scrolled to a point that would force the current window to
3915 * scroll past the beginning or end of its buffer. When the
3916 * resync is performed, some of the other 'scrollbind' windows may
3917 * need to jump so that the current window's relative position is
3918 * visible on-screen.
3919 */
3920 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3921 }
3922 curwin->w_scbind_pos = curwin->w_topline;
3923 }
3924
3925 old_curwin = curwin;
3926 old_topline = curwin->w_topline;
3927#ifdef FEAT_DIFF
3928 old_topfill = curwin->w_topfill;
3929#endif
3930 old_buf = curwin->w_buffer;
3931 old_leftcol = curwin->w_leftcol;
3932}
3933
3934/*
3935 * Synchronize any windows that have "scrollbind" set, based on the
3936 * number of rows by which the current window has changed
3937 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3938 */
3939 void
3940check_scrollbind(topline_diff, leftcol_diff)
3941 linenr_T topline_diff;
3942 long leftcol_diff;
3943{
3944 int want_ver;
3945 int want_hor;
3946 win_T *old_curwin = curwin;
3947 buf_T *old_curbuf = curbuf;
3948#ifdef FEAT_VISUAL
3949 int old_VIsual_select = VIsual_select;
3950 int old_VIsual_active = VIsual_active;
3951#endif
3952 colnr_T tgt_leftcol = curwin->w_leftcol;
3953 long topline;
3954 long y;
3955
3956 /*
3957 * check 'scrollopt' string for vertical and horizontal scroll options
3958 */
3959 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3960#ifdef FEAT_DIFF
3961 want_ver |= old_curwin->w_p_diff;
3962#endif
3963 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3964
3965 /*
3966 * loop through the scrollbound windows and scroll accordingly
3967 */
3968#ifdef FEAT_VISUAL
3969 VIsual_select = VIsual_active = 0;
3970#endif
3971 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3972 {
3973 curbuf = curwin->w_buffer;
3974 /* skip original window and windows with 'noscrollbind' */
3975 if (curwin != old_curwin && curwin->w_p_scb)
3976 {
3977 /*
3978 * do the vertical scroll
3979 */
3980 if (want_ver)
3981 {
3982#ifdef FEAT_DIFF
3983 if (old_curwin->w_p_diff && curwin->w_p_diff)
3984 {
3985 diff_set_topline(old_curwin, curwin);
3986 }
3987 else
3988#endif
3989 {
3990 curwin->w_scbind_pos += topline_diff;
3991 topline = curwin->w_scbind_pos;
3992 if (topline > curbuf->b_ml.ml_line_count)
3993 topline = curbuf->b_ml.ml_line_count;
3994 if (topline < 1)
3995 topline = 1;
3996
3997 y = topline - curwin->w_topline;
3998 if (y > 0)
3999 scrollup(y, FALSE);
4000 else
4001 scrolldown(-y, FALSE);
4002 }
4003
4004 redraw_later(VALID);
4005 cursor_correct();
4006#ifdef FEAT_WINDOWS
4007 curwin->w_redr_status = TRUE;
4008#endif
4009 }
4010
4011 /*
4012 * do the horizontal scroll
4013 */
4014 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4015 {
4016 curwin->w_leftcol = tgt_leftcol;
4017 leftcol_changed();
4018 }
4019 }
4020 }
4021
4022 /*
4023 * reset current-window
4024 */
4025#ifdef FEAT_VISUAL
4026 VIsual_select = old_VIsual_select;
4027 VIsual_active = old_VIsual_active;
4028#endif
4029 curwin = old_curwin;
4030 curbuf = old_curbuf;
4031}
4032#endif /* #ifdef FEAT_SCROLLBIND */
4033
4034/*
4035 * Command character that's ignored.
4036 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4037 * xon/xoff
4038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 static void
4040nv_ignore(cap)
4041 cmdarg_T *cap;
4042{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004043 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044}
4045
4046/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004047 * Command character that doesn't do anything, but unlike nv_ignore() does
4048 * start edit(). Used for "startinsert" executed while starting up.
4049 */
4050/*ARGSUSED */
4051 static void
4052nv_nop(cap)
4053 cmdarg_T *cap;
4054{
4055}
4056
4057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 * Command character doesn't exist.
4059 */
4060 static void
4061nv_error(cap)
4062 cmdarg_T *cap;
4063{
4064 clearopbeep(cap->oap);
4065}
4066
4067/*
4068 * <Help> and <F1> commands.
4069 */
4070 static void
4071nv_help(cap)
4072 cmdarg_T *cap;
4073{
4074 if (!checkclearopq(cap->oap))
4075 ex_help(NULL);
4076}
4077
4078/*
4079 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4080 */
4081 static void
4082nv_addsub(cap)
4083 cmdarg_T *cap;
4084{
4085 if (!checkclearopq(cap->oap)
4086 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4087 prep_redo_cmd(cap);
4088}
4089
4090/*
4091 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4092 */
4093 static void
4094nv_page(cap)
4095 cmdarg_T *cap;
4096{
4097 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004098 {
4099#ifdef FEAT_WINDOWS
4100 if (mod_mask & MOD_MASK_CTRL)
4101 {
4102 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4103 if (cap->arg == BACKWARD)
4104 goto_tabpage(-(int)cap->count1);
4105 else
4106 goto_tabpage((int)cap->count0);
4107 }
4108 else
4109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112}
4113
4114/*
4115 * Implementation of "gd" and "gD" command.
4116 */
4117 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004118nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004119 oparg_T *oap;
4120 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004121 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 char_u *ptr;
4125
Bram Moolenaard9d30582005-05-18 22:10:28 +00004126 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004127 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004129#ifdef FEAT_FOLDING
4130 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4131 foldOpenCursor();
4132#endif
4133}
4134
4135/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004136 * Search for variable declaration of "ptr[len]".
4137 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4138 * current file ("gD").
4139 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004140 * Return FAIL when not found.
4141 */
4142 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004143find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004144 char_u *ptr;
4145 int len;
4146 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004147 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004148 int searchflags; /* flags passed to searchit() */
4149{
4150 char_u *pat;
4151 pos_T old_pos;
4152 pos_T par_pos;
4153 pos_T found_pos;
4154 int t;
4155 int save_p_ws;
4156 int save_p_scs;
4157 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004158 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004159
4160 if ((pat = alloc(len + 7)) == NULL)
4161 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004162
4163 /* Put "\V" before the pattern to avoid that the special meaning of "."
4164 * and "~" causes trouble. */
4165 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4166 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 old_pos = curwin->w_cursor;
4168 save_p_ws = p_ws;
4169 save_p_scs = p_scs;
4170 p_ws = FALSE; /* don't wrap around end of file now */
4171 p_scs = FALSE; /* don't switch ignorecase off now */
4172
4173 /*
4174 * With "gD" go to line 1.
4175 * With "gd" Search back for the start of the current function, then go
4176 * back until a blank line. If this fails go to line 1.
4177 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004178 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 setpcmark(); /* Set in findpar() otherwise */
4181 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004182 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184 else
4185 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004186 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4188 --curwin->w_cursor.lnum;
4189 }
4190 curwin->w_cursor.col = 0;
4191
4192 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004193 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 for (;;)
4195 {
4196 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004197 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004198 if (curwin->w_cursor.lnum >= old_pos.lnum)
4199 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004200
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004201 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004202 {
4203 pos_T *pos;
4204
4205 /* Check that the block the match is in doesn't end before the
4206 * position where we started the search from. */
4207 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4208 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4209 && pos->lnum < old_pos.lnum)
4210 continue;
4211 }
4212
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004213 if (t == FAIL)
4214 {
4215 /* If we previously found a valid position, use it. */
4216 if (found_pos.lnum != 0)
4217 {
4218 curwin->w_cursor = found_pos;
4219 t = OK;
4220 }
4221 break;
4222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004224 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4225 {
4226 /* Ignore this line, continue at start of next line. */
4227 ++curwin->w_cursor.lnum;
4228 curwin->w_cursor.col = 0;
4229 continue;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004232 if (!locally) /* global search: use first match found */
4233 break;
4234 if (curwin->w_cursor.lnum >= par_pos.lnum)
4235 {
4236 /* If we previously found a valid position, use it. */
4237 if (found_pos.lnum != 0)
4238 curwin->w_cursor = found_pos;
4239 break;
4240 }
4241
4242 /* For finding a local variable and the match is before the "{" search
4243 * to find a later match. For K&R style function declarations this
4244 * skips the function header without types. */
4245 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004247
4248 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004250 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 curwin->w_cursor = old_pos;
4252 }
4253 else
4254 {
4255 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* "n" searches forward now */
4257 reset_search_dir();
4258 }
4259
4260 vim_free(pat);
4261 p_ws = save_p_ws;
4262 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263
4264 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265}
4266
4267/*
4268 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4269 * lines rather than lines in the file.
4270 * 'dist' must be positive.
4271 *
4272 * Return OK if able to move cursor, FAIL otherwise.
4273 */
4274 static int
4275nv_screengo(oap, dir, dist)
4276 oparg_T *oap;
4277 int dir;
4278 long dist;
4279{
4280 int linelen = linetabsize(ml_get_curline());
4281 int retval = OK;
4282 int atend = FALSE;
4283 int n;
4284 int col_off1; /* margin offset for first screen line */
4285 int col_off2; /* margin offset for wrapped screen line */
4286 int width1; /* text width for first screen line */
4287 int width2; /* test width for wrapped screen line */
4288
4289 oap->motion_type = MCHAR;
4290 oap->inclusive = FALSE;
4291
4292 col_off1 = curwin_col_off();
4293 col_off2 = col_off1 - curwin_col_off2();
4294 width1 = W_WIDTH(curwin) - col_off1;
4295 width2 = W_WIDTH(curwin) - col_off2;
4296
4297#ifdef FEAT_VERTSPLIT
4298 if (curwin->w_width != 0)
4299 {
4300#endif
4301 /*
4302 * Instead of sticking at the last character of the buffer line we
4303 * try to stick in the last column of the screen.
4304 */
4305 if (curwin->w_curswant == MAXCOL)
4306 {
4307 atend = TRUE;
4308 validate_virtcol();
4309 if (width1 <= 0)
4310 curwin->w_curswant = 0;
4311 else
4312 {
4313 curwin->w_curswant = width1 - 1;
4314 if (curwin->w_virtcol > curwin->w_curswant)
4315 curwin->w_curswant += ((curwin->w_virtcol
4316 - curwin->w_curswant - 1) / width2 + 1) * width2;
4317 }
4318 }
4319 else
4320 {
4321 if (linelen > width1)
4322 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4323 else
4324 n = width1;
4325 if (curwin->w_curswant > (colnr_T)n + 1)
4326 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4327 * width2;
4328 }
4329
4330 while (dist--)
4331 {
4332 if (dir == BACKWARD)
4333 {
4334 if ((long)curwin->w_curswant >= width2)
4335 /* move back within line */
4336 curwin->w_curswant -= width2;
4337 else
4338 {
4339 /* to previous line */
4340 if (curwin->w_cursor.lnum == 1)
4341 {
4342 retval = FAIL;
4343 break;
4344 }
4345 --curwin->w_cursor.lnum;
4346#ifdef FEAT_FOLDING
4347 /* Move to the start of a closed fold. Don't do that when
4348 * 'foldopen' contains "all": it will open in a moment. */
4349 if (!(fdo_flags & FDO_ALL))
4350 (void)hasFolding(curwin->w_cursor.lnum,
4351 &curwin->w_cursor.lnum, NULL);
4352#endif
4353 linelen = linetabsize(ml_get_curline());
4354 if (linelen > width1)
4355 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4356 + 1) * width2;
4357 }
4358 }
4359 else /* dir == FORWARD */
4360 {
4361 if (linelen > width1)
4362 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4363 else
4364 n = width1;
4365 if (curwin->w_curswant + width2 < (colnr_T)n)
4366 /* move forward within line */
4367 curwin->w_curswant += width2;
4368 else
4369 {
4370 /* to next line */
4371#ifdef FEAT_FOLDING
4372 /* Move to the end of a closed fold. */
4373 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4374 &curwin->w_cursor.lnum);
4375#endif
4376 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4377 {
4378 retval = FAIL;
4379 break;
4380 }
4381 curwin->w_cursor.lnum++;
4382 curwin->w_curswant %= width2;
4383 }
4384 }
4385 }
4386#ifdef FEAT_VERTSPLIT
4387 }
4388#endif
4389
4390 coladvance(curwin->w_curswant);
4391
4392#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4393 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4394 {
4395 /*
4396 * Check for landing on a character that got split at the end of the
4397 * last line. We want to advance a screenline, not end up in the same
4398 * screenline or move two screenlines.
4399 */
4400 validate_virtcol();
4401 if (curwin->w_virtcol > curwin->w_curswant
4402 && (curwin->w_curswant < (colnr_T)width1
4403 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4404 : ((curwin->w_curswant - width1) % width2
4405 > (colnr_T)width2 / 2)))
4406 --curwin->w_cursor.col;
4407 }
4408#endif
4409
4410 if (atend)
4411 curwin->w_curswant = MAXCOL; /* stick in the last column */
4412
4413 return retval;
4414}
4415
4416#ifdef FEAT_MOUSE
4417/*
4418 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4419 * when Shift or Ctrl is used.
4420 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4421 */
4422 static void
4423nv_mousescroll(cap)
4424 cmdarg_T *cap;
4425{
4426# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004427 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 /* Currently we only get the mouse coordinates in the GUI. */
4430 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4431 {
4432 int row, col;
4433
4434 row = mouse_row;
4435 col = mouse_col;
4436
4437 /* find the window at the pointer coordinates */
4438 curwin = mouse_find_win(&row, &col);
4439 curbuf = curwin->w_buffer;
4440 }
4441# endif
4442
4443 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4444 {
4445 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4446 }
4447 else
4448 {
4449 cap->count1 = 3;
4450 cap->count0 = 3;
4451 nv_scroll_line(cap);
4452 }
4453
4454# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4455 curwin->w_redr_status = TRUE;
4456
4457 curwin = old_curwin;
4458 curbuf = curwin->w_buffer;
4459# endif
4460}
4461
4462/*
4463 * Mouse clicks and drags.
4464 */
4465 static void
4466nv_mouse(cap)
4467 cmdarg_T *cap;
4468{
4469 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4470}
4471#endif
4472
4473/*
4474 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4475 * cap->arg must be TRUE for CTRL-E.
4476 */
4477 static void
4478nv_scroll_line(cap)
4479 cmdarg_T *cap;
4480{
4481 if (!checkclearop(cap->oap))
4482 scroll_redraw(cap->arg, cap->count1);
4483}
4484
4485/*
4486 * Scroll "count" lines up or down, and redraw.
4487 */
4488 void
4489scroll_redraw(up, count)
4490 int up;
4491 long count;
4492{
4493 linenr_T prev_topline = curwin->w_topline;
4494#ifdef FEAT_DIFF
4495 int prev_topfill = curwin->w_topfill;
4496#endif
4497 linenr_T prev_lnum = curwin->w_cursor.lnum;
4498
4499 if (up)
4500 scrollup(count, TRUE);
4501 else
4502 scrolldown(count, TRUE);
4503 if (p_so)
4504 {
4505 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4506 * valid, otherwise the screen jumps back at the end of the file. */
4507 cursor_correct();
4508 check_cursor_moved(curwin);
4509 curwin->w_valid |= VALID_TOPLINE;
4510
4511 /* If moved back to where we were, at least move the cursor, otherwise
4512 * we get stuck at one position. Don't move the cursor up if the
4513 * first line of the buffer is already on the screen */
4514 while (curwin->w_topline == prev_topline
4515#ifdef FEAT_DIFF
4516 && curwin->w_topfill == prev_topfill
4517#endif
4518 )
4519 {
4520 if (up)
4521 {
4522 if (curwin->w_cursor.lnum > prev_lnum
4523 || cursor_down(1L, FALSE) == FAIL)
4524 break;
4525 }
4526 else
4527 {
4528 if (curwin->w_cursor.lnum < prev_lnum
4529 || prev_topline == 1L
4530 || cursor_up(1L, FALSE) == FAIL)
4531 break;
4532 }
4533 /* Mark w_topline as valid, otherwise the screen jumps back at the
4534 * end of the file. */
4535 check_cursor_moved(curwin);
4536 curwin->w_valid |= VALID_TOPLINE;
4537 }
4538 }
4539 if (curwin->w_cursor.lnum != prev_lnum)
4540 coladvance(curwin->w_curswant);
4541 redraw_later(VALID);
4542}
4543
4544/*
4545 * Commands that start with "z".
4546 */
4547 static void
4548nv_zet(cap)
4549 cmdarg_T *cap;
4550{
4551 long n;
4552 colnr_T col;
4553 int nchar = cap->nchar;
4554#ifdef FEAT_FOLDING
4555 long old_fdl = curwin->w_p_fdl;
4556 int old_fen = curwin->w_p_fen;
4557#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004558#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004559 int undo = FALSE;
4560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 if (VIM_ISDIGIT(nchar))
4563 {
4564 /*
4565 * "z123{nchar}": edit the count before obtaining {nchar}
4566 */
4567 if (checkclearop(cap->oap))
4568 return;
4569 n = nchar - '0';
4570 for (;;)
4571 {
4572#ifdef USE_ON_FLY_SCROLL
4573 dont_scroll = TRUE; /* disallow scrolling here */
4574#endif
4575 ++no_mapping;
4576 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004577 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#ifdef FEAT_LANGMAP
4579 LANGMAP_ADJUST(nchar, TRUE);
4580#endif
4581 --no_mapping;
4582 --allow_keys;
4583#ifdef FEAT_CMDL_INFO
4584 (void)add_to_showcmd(nchar);
4585#endif
4586 if (nchar == K_DEL || nchar == K_KDEL)
4587 n /= 10;
4588 else if (VIM_ISDIGIT(nchar))
4589 n = n * 10 + (nchar - '0');
4590 else if (nchar == CAR)
4591 {
4592#ifdef FEAT_GUI
4593 need_mouse_correct = TRUE;
4594#endif
4595 win_setheight((int)n);
4596 break;
4597 }
4598 else if (nchar == 'l'
4599 || nchar == 'h'
4600 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004601 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
4603 cap->count1 = n ? n * cap->count1 : cap->count1;
4604 goto dozet;
4605 }
4606 else
4607 {
4608 clearopbeep(cap->oap);
4609 break;
4610 }
4611 }
4612 cap->oap->op_type = OP_NOP;
4613 return;
4614 }
4615
4616dozet:
4617 if (
4618#ifdef FEAT_FOLDING
4619 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4620 * and "zC" only in Visual mode. "zj" and "zk" are motion
4621 * commands. */
4622 cap->nchar != 'f' && cap->nchar != 'F'
4623 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4624 && cap->nchar != 'j' && cap->nchar != 'k'
4625 &&
4626#endif
4627 checkclearop(cap->oap))
4628 return;
4629
4630 /*
4631 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4632 * If line number given, set cursor.
4633 */
4634 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4635 && cap->count0
4636 && cap->count0 != curwin->w_cursor.lnum)
4637 {
4638 setpcmark();
4639 if (cap->count0 > curbuf->b_ml.ml_line_count)
4640 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4641 else
4642 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004643 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645
4646 switch (nchar)
4647 {
4648 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4649 case '+':
4650 if (cap->count0 == 0)
4651 {
4652 /* No count given: put cursor at the line below screen */
4653 validate_botline(); /* make sure w_botline is valid */
4654 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4655 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4656 else
4657 curwin->w_cursor.lnum = curwin->w_botline;
4658 }
4659 /* FALLTHROUGH */
4660 case NL:
4661 case CAR:
4662 case K_KENTER:
4663 beginline(BL_WHITE | BL_FIX);
4664 /* FALLTHROUGH */
4665
4666 case 't': scroll_cursor_top(0, TRUE);
4667 redraw_later(VALID);
4668 break;
4669
4670 /* "z." and "zz": put cursor in middle of screen */
4671 case '.': beginline(BL_WHITE | BL_FIX);
4672 /* FALLTHROUGH */
4673
4674 case 'z': scroll_cursor_halfway(TRUE);
4675 redraw_later(VALID);
4676 break;
4677
4678 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4679 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4680 * when <count> is at bottom of window, and puts that one at
4681 * bottom of window. */
4682 if (cap->count0 != 0)
4683 {
4684 scroll_cursor_bot(0, TRUE);
4685 curwin->w_cursor.lnum = curwin->w_topline;
4686 }
4687 else if (curwin->w_topline == 1)
4688 curwin->w_cursor.lnum = 1;
4689 else
4690 curwin->w_cursor.lnum = curwin->w_topline - 1;
4691 /* FALLTHROUGH */
4692 case '-':
4693 beginline(BL_WHITE | BL_FIX);
4694 /* FALLTHROUGH */
4695
4696 case 'b': scroll_cursor_bot(0, TRUE);
4697 redraw_later(VALID);
4698 break;
4699
4700 /* "zH" - scroll screen right half-page */
4701 case 'H':
4702 cap->count1 *= W_WIDTH(curwin) / 2;
4703 /* FALLTHROUGH */
4704
4705 /* "zh" - scroll screen to the right */
4706 case 'h':
4707 case K_LEFT:
4708 if (!curwin->w_p_wrap)
4709 {
4710 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4711 curwin->w_leftcol = 0;
4712 else
4713 curwin->w_leftcol -= (colnr_T)cap->count1;
4714 leftcol_changed();
4715 }
4716 break;
4717
4718 /* "zL" - scroll screen left half-page */
4719 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4720 /* FALLTHROUGH */
4721
4722 /* "zl" - scroll screen to the left */
4723 case 'l':
4724 case K_RIGHT:
4725 if (!curwin->w_p_wrap)
4726 {
4727 /* scroll the window left */
4728 curwin->w_leftcol += (colnr_T)cap->count1;
4729 leftcol_changed();
4730 }
4731 break;
4732
4733 /* "zs" - scroll screen, cursor at the start */
4734 case 's': if (!curwin->w_p_wrap)
4735 {
4736#ifdef FEAT_FOLDING
4737 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4738 col = 0; /* like the cursor is in col 0 */
4739 else
4740#endif
4741 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4742 if ((long)col > p_siso)
4743 col -= p_siso;
4744 else
4745 col = 0;
4746 if (curwin->w_leftcol != col)
4747 {
4748 curwin->w_leftcol = col;
4749 redraw_later(NOT_VALID);
4750 }
4751 }
4752 break;
4753
4754 /* "ze" - scroll screen, cursor at the end */
4755 case 'e': if (!curwin->w_p_wrap)
4756 {
4757#ifdef FEAT_FOLDING
4758 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4759 col = 0; /* like the cursor is in col 0 */
4760 else
4761#endif
4762 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4763 n = W_WIDTH(curwin) - curwin_col_off();
4764 if ((long)col + p_siso < n)
4765 col = 0;
4766 else
4767 col = col + p_siso - n + 1;
4768 if (curwin->w_leftcol != col)
4769 {
4770 curwin->w_leftcol = col;
4771 redraw_later(NOT_VALID);
4772 }
4773 }
4774 break;
4775
4776#ifdef FEAT_FOLDING
4777 /* "zF": create fold command */
4778 /* "zf": create fold operator */
4779 case 'F':
4780 case 'f': if (foldManualAllowed(TRUE))
4781 {
4782 cap->nchar = 'f';
4783 nv_operator(cap);
4784 curwin->w_p_fen = TRUE;
4785
4786 /* "zF" is like "zfzf" */
4787 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4788 {
4789 nv_operator(cap);
4790 finish_op = TRUE;
4791 }
4792 }
4793 else
4794 clearopbeep(cap->oap);
4795 break;
4796
4797 /* "zd": delete fold at cursor */
4798 /* "zD": delete fold at cursor recursively */
4799 case 'd':
4800 case 'D': if (foldManualAllowed(FALSE))
4801 {
4802 if (VIsual_active)
4803 nv_operator(cap);
4804 else
4805 deleteFold(curwin->w_cursor.lnum,
4806 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4807 }
4808 break;
4809
4810 /* "zE": erease all folds */
4811 case 'E': if (foldmethodIsManual(curwin))
4812 {
4813 clearFolding(curwin);
4814 changed_window_setting();
4815 }
4816 else if (foldmethodIsMarker(curwin))
4817 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4818 TRUE, FALSE);
4819 else
4820 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4821 break;
4822
4823 /* "zn": fold none: reset 'foldenable' */
4824 case 'n': curwin->w_p_fen = FALSE;
4825 break;
4826
4827 /* "zN": fold Normal: set 'foldenable' */
4828 case 'N': curwin->w_p_fen = TRUE;
4829 break;
4830
4831 /* "zi": invert folding: toggle 'foldenable' */
4832 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4833 break;
4834
4835 /* "za": open closed fold or close open fold at cursor */
4836 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4837 openFold(curwin->w_cursor.lnum, cap->count1);
4838 else
4839 {
4840 closeFold(curwin->w_cursor.lnum, cap->count1);
4841 curwin->w_p_fen = TRUE;
4842 }
4843 break;
4844
4845 /* "zA": open fold at cursor recursively */
4846 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4847 openFoldRecurse(curwin->w_cursor.lnum);
4848 else
4849 {
4850 closeFoldRecurse(curwin->w_cursor.lnum);
4851 curwin->w_p_fen = TRUE;
4852 }
4853 break;
4854
4855 /* "zo": open fold at cursor or Visual area */
4856 case 'o': if (VIsual_active)
4857 nv_operator(cap);
4858 else
4859 openFold(curwin->w_cursor.lnum, cap->count1);
4860 break;
4861
4862 /* "zO": open fold recursively */
4863 case 'O': if (VIsual_active)
4864 nv_operator(cap);
4865 else
4866 openFoldRecurse(curwin->w_cursor.lnum);
4867 break;
4868
4869 /* "zc": close fold at cursor or Visual area */
4870 case 'c': if (VIsual_active)
4871 nv_operator(cap);
4872 else
4873 closeFold(curwin->w_cursor.lnum, cap->count1);
4874 curwin->w_p_fen = TRUE;
4875 break;
4876
4877 /* "zC": close fold recursively */
4878 case 'C': if (VIsual_active)
4879 nv_operator(cap);
4880 else
4881 closeFoldRecurse(curwin->w_cursor.lnum);
4882 curwin->w_p_fen = TRUE;
4883 break;
4884
4885 /* "zv": open folds at the cursor */
4886 case 'v': foldOpenCursor();
4887 break;
4888
4889 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4890 case 'x': curwin->w_p_fen = TRUE;
4891 newFoldLevel(); /* update right now */
4892 foldOpenCursor();
4893 break;
4894
4895 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4896 case 'X': curwin->w_p_fen = TRUE;
4897 old_fdl = -1; /* force an update */
4898 break;
4899
4900 /* "zm": fold more */
4901 case 'm': if (curwin->w_p_fdl > 0)
4902 --curwin->w_p_fdl;
4903 old_fdl = -1; /* force an update */
4904 curwin->w_p_fen = TRUE;
4905 break;
4906
4907 /* "zM": close all folds */
4908 case 'M': curwin->w_p_fdl = 0;
4909 old_fdl = -1; /* force an update */
4910 curwin->w_p_fen = TRUE;
4911 break;
4912
4913 /* "zr": reduce folding */
4914 case 'r': ++curwin->w_p_fdl;
4915 break;
4916
4917 /* "zR": open all folds */
4918 case 'R': curwin->w_p_fdl = getDeepestNesting();
4919 old_fdl = -1; /* force an update */
4920 break;
4921
4922 case 'j': /* "zj" move to next fold downwards */
4923 case 'k': /* "zk" move to next fold upwards */
4924 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4925 cap->count1) == FAIL)
4926 clearopbeep(cap->oap);
4927 break;
4928
4929#endif /* FEAT_FOLDING */
4930
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004931#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004932 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4933 ++no_mapping;
4934 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004935 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00004936#ifdef FEAT_LANGMAP
4937 LANGMAP_ADJUST(nchar, TRUE);
4938#endif
4939 --no_mapping;
4940 --allow_keys;
4941#ifdef FEAT_CMDL_INFO
4942 (void)add_to_showcmd(nchar);
4943#endif
4944 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
4945 {
4946 clearopbeep(cap->oap);
4947 break;
4948 }
4949 undo = TRUE;
4950 /*FALLTHROUGH*/
4951
Bram Moolenaarb765d632005-06-07 21:00:02 +00004952 case 'g': /* "zg": add good word to word list */
4953 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00004954 case 'G': /* "zG": add good word to temp word list */
4955 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004956 {
4957 char_u *ptr = NULL;
4958 int len;
4959
4960 if (checkclearop(cap->oap))
4961 break;
4962# ifdef FEAT_VISUAL
4963 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4964 == FAIL)
4965 return;
4966# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004967 if (ptr == NULL)
4968 {
4969 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004970
4971 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004972 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004973 if (len != 0 && curwin->w_cursor.col <= pos.col)
4974 ptr = ml_get_pos(&curwin->w_cursor);
4975 curwin->w_cursor = pos;
4976 }
4977
Bram Moolenaarb765d632005-06-07 21:00:02 +00004978 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4979 FIND_IDENT)) == 0)
4980 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004981 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00004982 (nchar == 'G' || nchar == 'W')
4983 ? 0 : (int)cap->count1,
4984 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004985 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004986 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004987
Bram Moolenaar43abc522005-12-10 20:15:02 +00004988 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00004989 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004990 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004991 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004992#endif
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 default: clearopbeep(cap->oap);
4995 }
4996
4997#ifdef FEAT_FOLDING
4998 /* Redraw when 'foldenable' changed */
4999 if (old_fen != curwin->w_p_fen)
5000 {
5001# ifdef FEAT_DIFF
5002 win_T *wp;
5003
5004 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5005 {
5006 /* Adjust 'foldenable' in diff-synced windows. */
5007 FOR_ALL_WINDOWS(wp)
5008 {
5009 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5010 {
5011 wp->w_p_fen = curwin->w_p_fen;
5012 changed_window_setting_win(wp);
5013 }
5014 }
5015 }
5016# endif
5017 changed_window_setting();
5018 }
5019
5020 /* Redraw when 'foldlevel' changed. */
5021 if (old_fdl != curwin->w_p_fdl)
5022 newFoldLevel();
5023#endif
5024}
5025
5026#ifdef FEAT_GUI
5027/*
5028 * Vertical scrollbar movement.
5029 */
5030 static void
5031nv_ver_scrollbar(cap)
5032 cmdarg_T *cap;
5033{
5034 if (cap->oap->op_type != OP_NOP)
5035 clearopbeep(cap->oap);
5036
5037 /* Even if an operator was pending, we still want to scroll */
5038 gui_do_scroll();
5039}
5040
5041/*
5042 * Horizontal scrollbar movement.
5043 */
5044 static void
5045nv_hor_scrollbar(cap)
5046 cmdarg_T *cap;
5047{
5048 if (cap->oap->op_type != OP_NOP)
5049 clearopbeep(cap->oap);
5050
5051 /* Even if an operator was pending, we still want to scroll */
5052 gui_do_horiz_scroll();
5053}
5054#endif
5055
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005056#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005057/*
5058 * Click in GUI tab.
5059 */
5060 static void
5061nv_tabline(cap)
5062 cmdarg_T *cap;
5063{
5064 if (cap->oap->op_type != OP_NOP)
5065 clearopbeep(cap->oap);
5066
5067 /* Even if an operator was pending, we still want to jump tabs. */
5068 goto_tabpage(current_tab);
5069}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005070
5071/*
5072 * Selected item in tab line menu.
5073 */
5074 static void
5075nv_tabmenu(cap)
5076 cmdarg_T *cap;
5077{
5078 if (cap->oap->op_type != OP_NOP)
5079 clearopbeep(cap->oap);
5080
5081 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005082 handle_tabmenu();
5083}
5084
5085/*
5086 * Handle selecting an item of the GUI tab line menu.
5087 * Used in Normal and Insert mode.
5088 */
5089 void
5090handle_tabmenu()
5091{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005092 switch (current_tabmenu)
5093 {
5094 case TABLINE_MENU_CLOSE:
5095 if (current_tab == 0)
5096 do_cmdline_cmd((char_u *)"tabclose");
5097 else
5098 {
5099 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5100 current_tab);
5101 do_cmdline_cmd(IObuff);
5102 }
5103 break;
5104
5105 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005106 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5107 current_tab > 0 ? current_tab - 1 : 999);
5108 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005109 break;
5110
5111 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005112 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5113 current_tab > 0 ? current_tab - 1 : 999);
5114 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005115 break;
5116 }
5117}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005118#endif
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120/*
5121 * "Q" command.
5122 */
5123 static void
5124nv_exmode(cap)
5125 cmdarg_T *cap;
5126{
5127 /*
5128 * Ignore 'Q' in Visual mode, just give a beep.
5129 */
5130#ifdef FEAT_VISUAL
5131 if (VIsual_active)
5132 vim_beep();
5133 else
5134#endif
5135 if (!checkclearop(cap->oap))
5136 do_exmode(FALSE);
5137}
5138
5139/*
5140 * Handle a ":" command.
5141 */
5142 static void
5143nv_colon(cap)
5144 cmdarg_T *cap;
5145{
5146 int old_p_im;
5147
5148#ifdef FEAT_VISUAL
5149 if (VIsual_active)
5150 nv_operator(cap);
5151 else
5152#endif
5153 {
5154 if (cap->oap->op_type != OP_NOP)
5155 {
5156 /* Using ":" as a movement is characterwise exclusive. */
5157 cap->oap->motion_type = MCHAR;
5158 cap->oap->inclusive = FALSE;
5159 }
5160 else if (cap->count0)
5161 {
5162 /* translate "count:" into ":.,.+(count - 1)" */
5163 stuffcharReadbuff('.');
5164 if (cap->count0 > 1)
5165 {
5166 stuffReadbuff((char_u *)",.+");
5167 stuffnumReadbuff((long)cap->count0 - 1L);
5168 }
5169 }
5170
5171 /* When typing, don't type below an old message */
5172 if (KeyTyped)
5173 compute_cmdrow();
5174
5175 old_p_im = p_im;
5176
5177 /* get a command line and execute it */
5178 do_cmdline(NULL, getexline, NULL,
5179 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5180
5181 /* If 'insertmode' changed, enter or exit Insert mode */
5182 if (p_im != old_p_im)
5183 {
5184 if (p_im)
5185 restart_edit = 'i';
5186 else
5187 restart_edit = 0;
5188 }
5189
5190 /* The start of the operator may have become invalid by the Ex
5191 * command. */
5192 if (cap->oap->op_type != OP_NOP
5193 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5194 || cap->oap->start.col >
5195 STRLEN(ml_get(cap->oap->start.lnum))))
5196 clearopbeep(cap->oap);
5197 }
5198}
5199
5200/*
5201 * Handle CTRL-G command.
5202 */
5203 static void
5204nv_ctrlg(cap)
5205 cmdarg_T *cap;
5206{
5207#ifdef FEAT_VISUAL
5208 if (VIsual_active) /* toggle Selection/Visual mode */
5209 {
5210 VIsual_select = !VIsual_select;
5211 showmode();
5212 }
5213 else
5214#endif
5215 if (!checkclearop(cap->oap))
5216 /* print full name if count given or :cd used */
5217 fileinfo((int)cap->count0, FALSE, TRUE);
5218}
5219
5220/*
5221 * Handle CTRL-H <Backspace> command.
5222 */
5223 static void
5224nv_ctrlh(cap)
5225 cmdarg_T *cap;
5226{
5227#ifdef FEAT_VISUAL
5228 if (VIsual_active && VIsual_select)
5229 {
5230 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5231 v_visop(cap);
5232 }
5233 else
5234#endif
5235 nv_left(cap);
5236}
5237
5238/*
5239 * CTRL-L: clear screen and redraw.
5240 */
5241 static void
5242nv_clear(cap)
5243 cmdarg_T *cap;
5244{
5245 if (!checkclearop(cap->oap))
5246 {
5247#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5248 /*
5249 * Right now, the BeBox doesn't seem to have an easy way to detect
5250 * window resizing, so we cheat and make the user detect it
5251 * manually with CTRL-L instead
5252 */
5253 ui_get_shellsize();
5254#endif
5255#ifdef FEAT_SYN_HL
5256 /* Clear all syntax states to force resyncing. */
5257 syn_stack_free_all(curbuf);
5258#endif
5259 redraw_later(CLEAR);
5260 }
5261}
5262
5263/*
5264 * CTRL-O: In Select mode: switch to Visual mode for one command.
5265 * Otherwise: Go to older pcmark.
5266 */
5267 static void
5268nv_ctrlo(cap)
5269 cmdarg_T *cap;
5270{
5271#ifdef FEAT_VISUAL
5272 if (VIsual_active && VIsual_select)
5273 {
5274 VIsual_select = FALSE;
5275 showmode();
5276 restart_VIsual_select = 2; /* restart Select mode later */
5277 }
5278 else
5279#endif
5280 {
5281 cap->count1 = -cap->count1;
5282 nv_pcmark(cap);
5283 }
5284}
5285
5286/*
5287 * CTRL-^ command, short for ":e #"
5288 */
5289 static void
5290nv_hat(cap)
5291 cmdarg_T *cap;
5292{
5293 if (!checkclearopq(cap->oap))
5294 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5295 GETF_SETMARK|GETF_ALT, FALSE);
5296}
5297
5298/*
5299 * "Z" commands.
5300 */
5301 static void
5302nv_Zet(cap)
5303 cmdarg_T *cap;
5304{
5305 if (!checkclearopq(cap->oap))
5306 {
5307 switch (cap->nchar)
5308 {
5309 /* "ZZ": equivalent to ":x". */
5310 case 'Z': do_cmdline_cmd((char_u *)"x");
5311 break;
5312
5313 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5314 case 'Q': do_cmdline_cmd((char_u *)"q!");
5315 break;
5316
5317 default: clearopbeep(cap->oap);
5318 }
5319 }
5320}
5321
5322#if defined(FEAT_WINDOWS) || defined(PROTO)
5323/*
5324 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5325 */
5326 void
5327do_nv_ident(c1, c2)
5328 int c1;
5329 int c2;
5330{
5331 oparg_T oa;
5332 cmdarg_T ca;
5333
5334 clear_oparg(&oa);
5335 vim_memset(&ca, 0, sizeof(ca));
5336 ca.oap = &oa;
5337 ca.cmdchar = c1;
5338 ca.nchar = c2;
5339 nv_ident(&ca);
5340}
5341#endif
5342
5343/*
5344 * Handle the commands that use the word under the cursor.
5345 * [g] CTRL-] :ta to current identifier
5346 * [g] 'K' run program for current identifier
5347 * [g] '*' / to current identifier or string
5348 * [g] '#' ? to current identifier or string
5349 * g ']' :tselect for current identifier
5350 */
5351 static void
5352nv_ident(cap)
5353 cmdarg_T *cap;
5354{
5355 char_u *ptr = NULL;
5356 char_u *buf;
5357 char_u *p;
5358 char_u *kp; /* value of 'keywordprg' */
5359 int kp_help; /* 'keywordprg' is ":help" */
5360 int n = 0; /* init for GCC */
5361 int cmdchar;
5362 int g_cmd; /* "g" command */
5363 char_u *aux_ptr;
5364 int isman;
5365 int isman_s;
5366
5367 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5368 {
5369 cmdchar = cap->nchar;
5370 g_cmd = TRUE;
5371 }
5372 else
5373 {
5374 cmdchar = cap->cmdchar;
5375 g_cmd = FALSE;
5376 }
5377
5378 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5379 cmdchar = '#';
5380
5381 /*
5382 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5383 */
5384 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5385 {
5386#ifdef FEAT_VISUAL
5387 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5388 return;
5389#endif
5390 if (checkclearopq(cap->oap))
5391 return;
5392 }
5393
5394 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5395 (cmdchar == '*' || cmdchar == '#')
5396 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5397 {
5398 clearop(cap->oap);
5399 return;
5400 }
5401
5402 /* Allocate buffer to put the command in. Inserting backslashes can
5403 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5404 * and some numbers. */
5405 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5406 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5407 || STRCMP(kp, ":help") == 0);
5408 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5409 if (buf == NULL)
5410 return;
5411 buf[0] = NUL;
5412
5413 switch (cmdchar)
5414 {
5415 case '*':
5416 case '#':
5417 /*
5418 * Put cursor at start of word, makes search skip the word
5419 * under the cursor.
5420 * Call setpcmark() first, so "*``" puts the cursor back where
5421 * it was.
5422 */
5423 setpcmark();
5424 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5425
5426 if (!g_cmd && vim_iswordp(ptr))
5427 STRCPY(buf, "\\<");
5428 no_smartcase = TRUE; /* don't use 'smartcase' now */
5429 break;
5430
5431 case 'K':
5432 if (kp_help)
5433 STRCPY(buf, "he! ");
5434 else
5435 {
5436 /* When a count is given, turn it into a range. Is this
5437 * really what we want? */
5438 isman = (STRCMP(kp, "man") == 0);
5439 isman_s = (STRCMP(kp, "man -s") == 0);
5440 if (cap->count0 != 0 && !(isman || isman_s))
5441 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5442
5443 STRCAT(buf, "! ");
5444 if (cap->count0 == 0 && isman_s)
5445 STRCAT(buf, "man");
5446 else
5447 STRCAT(buf, kp);
5448 STRCAT(buf, " ");
5449 if (cap->count0 != 0 && (isman || isman_s))
5450 {
5451 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5452 STRCAT(buf, " ");
5453 }
5454 }
5455 break;
5456
5457 case ']':
5458#ifdef FEAT_CSCOPE
5459 if (p_cst)
5460 STRCPY(buf, "cstag ");
5461 else
5462#endif
5463 STRCPY(buf, "ts ");
5464 break;
5465
5466 default:
5467 if (curbuf->b_help)
5468 STRCPY(buf, "he! ");
5469 else if (g_cmd)
5470 STRCPY(buf, "tj ");
5471 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005472 sprintf((char *)buf, "%ldta ", cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474
5475 /*
5476 * Now grab the chars in the identifier
5477 */
5478 if (cmdchar == '*')
5479 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5480 else if (cmdchar == '#')
5481 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5482 else if (cmdchar == 'K' && !kp_help)
5483 aux_ptr = (char_u *)" \t\\\"|!";
5484 else
5485 /* Don't escape spaces and Tabs in a tag with a backslash */
5486 aux_ptr = (char_u *)"\\|\"";
5487
5488 p = buf + STRLEN(buf);
5489 while (n-- > 0)
5490 {
5491 /* put a backslash before \ and some others */
5492 if (vim_strchr(aux_ptr, *ptr) != NULL)
5493 *p++ = '\\';
5494#ifdef FEAT_MBYTE
5495 /* When current byte is a part of multibyte character, copy all bytes
5496 * of that character. */
5497 if (has_mbyte)
5498 {
5499 int i;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005500 int len = (*mb_ptr2len)(ptr) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 for (i = 0; i < len && n >= 1; ++i, --n)
5503 *p++ = *ptr++;
5504 }
5505#endif
5506 *p++ = *ptr++;
5507 }
5508 *p = NUL;
5509
5510 /*
5511 * Execute the command.
5512 */
5513 if (cmdchar == '*' || cmdchar == '#')
5514 {
5515 if (!g_cmd && (
5516#ifdef FEAT_MBYTE
5517 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5518#endif
5519 vim_iswordc(ptr[-1])))
5520 STRCAT(buf, "\\>");
5521#ifdef FEAT_CMDHIST
5522 /* put pattern in search history */
5523 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5524#endif
5525 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5526 }
5527 else
5528 do_cmdline_cmd(buf);
5529
5530 vim_free(buf);
5531}
5532
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005533#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534/*
5535 * Get visually selected text, within one line only.
5536 * Returns FAIL if more than one line selected.
5537 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005538 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539get_visual_text(cap, pp, lenp)
5540 cmdarg_T *cap;
5541 char_u **pp; /* return: start of selected text */
5542 int *lenp; /* return: length of selected text */
5543{
5544 if (VIsual_mode != 'V')
5545 unadjust_for_sel();
5546 if (VIsual.lnum != curwin->w_cursor.lnum)
5547 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005548 if (cap != NULL)
5549 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return FAIL;
5551 }
5552 if (VIsual_mode == 'V')
5553 {
5554 *pp = ml_get_curline();
5555 *lenp = (int)STRLEN(*pp);
5556 }
5557 else
5558 {
5559 if (lt(curwin->w_cursor, VIsual))
5560 {
5561 *pp = ml_get_pos(&curwin->w_cursor);
5562 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5563 }
5564 else
5565 {
5566 *pp = ml_get_pos(&VIsual);
5567 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5568 }
5569#ifdef FEAT_MBYTE
5570 if (has_mbyte)
5571 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005572 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#endif
5574 }
5575 reset_VIsual_and_resel();
5576 return OK;
5577}
5578#endif
5579
5580/*
5581 * CTRL-T: backwards in tag stack
5582 */
5583 static void
5584nv_tagpop(cap)
5585 cmdarg_T *cap;
5586{
5587 if (!checkclearopq(cap->oap))
5588 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5589}
5590
5591/*
5592 * Handle scrolling command 'H', 'L' and 'M'.
5593 */
5594 static void
5595nv_scroll(cap)
5596 cmdarg_T *cap;
5597{
5598 int used = 0;
5599 long n;
5600#ifdef FEAT_FOLDING
5601 linenr_T lnum;
5602#endif
5603 int half;
5604
5605 cap->oap->motion_type = MLINE;
5606 setpcmark();
5607
5608 if (cap->cmdchar == 'L')
5609 {
5610 validate_botline(); /* make sure curwin->w_botline is valid */
5611 curwin->w_cursor.lnum = curwin->w_botline - 1;
5612 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5613 curwin->w_cursor.lnum = 1;
5614 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005615 {
5616#ifdef FEAT_FOLDING
5617 if (hasAnyFolding(curwin))
5618 {
5619 /* Count a fold for one screen line. */
5620 for (n = cap->count1 - 1; n > 0
5621 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5622 {
5623 (void)hasFolding(curwin->w_cursor.lnum,
5624 &curwin->w_cursor.lnum, NULL);
5625 --curwin->w_cursor.lnum;
5626 }
5627 }
5628 else
5629#endif
5630 curwin->w_cursor.lnum -= cap->count1 - 1;
5631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633 else
5634 {
5635 if (cap->cmdchar == 'M')
5636 {
5637#ifdef FEAT_DIFF
5638 /* Don't count filler lines above the window. */
5639 used -= diff_check_fill(curwin, curwin->w_topline)
5640 - curwin->w_topfill;
5641#endif
5642 validate_botline(); /* make sure w_empty_rows is valid */
5643 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5644 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5645 {
5646#ifdef FEAT_DIFF
5647 /* Count half he number of filler lines to be "below this
5648 * line" and half to be "above the next line". */
5649 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5650 + n) / 2 >= half)
5651 {
5652 --n;
5653 break;
5654 }
5655#endif
5656 used += plines(curwin->w_topline + n);
5657 if (used >= half)
5658 break;
5659#ifdef FEAT_FOLDING
5660 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5661 n = lnum - curwin->w_topline;
5662#endif
5663 }
5664 if (n > 0 && used > curwin->w_height)
5665 --n;
5666 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005667 else /* (cap->cmdchar == 'H') */
5668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005670#ifdef FEAT_FOLDING
5671 if (hasAnyFolding(curwin))
5672 {
5673 /* Count a fold for one screen line. */
5674 lnum = curwin->w_topline;
5675 while (n-- > 0 && lnum < curwin->w_botline - 1)
5676 {
5677 hasFolding(lnum, NULL, &lnum);
5678 ++lnum;
5679 }
5680 n = lnum - curwin->w_topline;
5681 }
5682#endif
5683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 curwin->w_cursor.lnum = curwin->w_topline + n;
5685 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5686 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5687 }
5688
5689 cursor_correct(); /* correct for 'so' */
5690 beginline(BL_SOL | BL_FIX);
5691}
5692
5693/*
5694 * Cursor right commands.
5695 */
5696 static void
5697nv_right(cap)
5698 cmdarg_T *cap;
5699{
5700 long n;
5701#ifdef FEAT_VISUAL
5702 int PAST_LINE;
5703#else
5704# define PAST_LINE 0
5705#endif
5706
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005707 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5708 {
5709 /* <C-Right> and <S-Right> move a word or WORD right */
5710 if (mod_mask & MOD_MASK_CTRL)
5711 cap->arg = TRUE;
5712 nv_wordcmd(cap);
5713 return;
5714 }
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 cap->oap->motion_type = MCHAR;
5717 cap->oap->inclusive = FALSE;
5718#ifdef FEAT_VISUAL
5719 PAST_LINE = (VIsual_active && *p_sel != 'o');
5720
5721# ifdef FEAT_VIRTUALEDIT
5722 /*
5723 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005724 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 */
5726 if (virtual_active())
5727 PAST_LINE = 0;
5728# endif
5729#endif
5730
5731 for (n = cap->count1; n > 0; --n)
5732 {
5733 if ((!PAST_LINE && oneright() == FAIL)
5734 || (PAST_LINE && *ml_get_cursor() == NUL))
5735 {
5736 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005737 * <Space> wraps to next line if 'whichwrap' has 's'.
5738 * 'l' wraps to next line if 'whichwrap' has 'l'.
5739 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 */
5741 if ( ((cap->cmdchar == ' '
5742 && vim_strchr(p_ww, 's') != NULL)
5743 || (cap->cmdchar == 'l'
5744 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005745 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 && vim_strchr(p_ww, '>') != NULL))
5747 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5748 {
5749 /* When deleting we also count the NL as a character.
5750 * Set cap->oap->inclusive when last char in the line is
5751 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005752 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 && !cap->oap->inclusive
5754 && !lineempty(curwin->w_cursor.lnum))
5755 cap->oap->inclusive = TRUE;
5756 else
5757 {
5758 ++curwin->w_cursor.lnum;
5759 curwin->w_cursor.col = 0;
5760#ifdef FEAT_VIRTUALEDIT
5761 curwin->w_cursor.coladd = 0;
5762#endif
5763 curwin->w_set_curswant = TRUE;
5764 cap->oap->inclusive = FALSE;
5765 }
5766 continue;
5767 }
5768 if (cap->oap->op_type == OP_NOP)
5769 {
5770 /* Only beep and flush if not moved at all */
5771 if (n == cap->count1)
5772 beep_flush();
5773 }
5774 else
5775 {
5776 if (!lineempty(curwin->w_cursor.lnum))
5777 cap->oap->inclusive = TRUE;
5778 }
5779 break;
5780 }
5781#ifdef FEAT_VISUAL
5782 else if (PAST_LINE)
5783 {
5784 curwin->w_set_curswant = TRUE;
5785# ifdef FEAT_VIRTUALEDIT
5786 if (virtual_active())
5787 oneright();
5788 else
5789# endif
5790 {
5791# ifdef FEAT_MBYTE
5792 if (has_mbyte)
5793 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005794 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796# endif
5797 ++curwin->w_cursor.col;
5798 }
5799 }
5800#endif
5801 }
5802#ifdef FEAT_FOLDING
5803 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5804 && cap->oap->op_type == OP_NOP)
5805 foldOpenCursor();
5806#endif
5807}
5808
5809/*
5810 * Cursor left commands.
5811 *
5812 * Returns TRUE when operator end should not be adjusted.
5813 */
5814 static void
5815nv_left(cap)
5816 cmdarg_T *cap;
5817{
5818 long n;
5819
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005820 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5821 {
5822 /* <C-Left> and <S-Left> move a word or WORD left */
5823 if (mod_mask & MOD_MASK_CTRL)
5824 cap->arg = 1;
5825 nv_bck_word(cap);
5826 return;
5827 }
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 cap->oap->motion_type = MCHAR;
5830 cap->oap->inclusive = FALSE;
5831 for (n = cap->count1; n > 0; --n)
5832 {
5833 if (oneleft() == FAIL)
5834 {
5835 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5836 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5837 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5838 */
5839 if ( (((cap->cmdchar == K_BS
5840 || cap->cmdchar == Ctrl_H)
5841 && vim_strchr(p_ww, 'b') != NULL)
5842 || (cap->cmdchar == 'h'
5843 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005844 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 && vim_strchr(p_ww, '<') != NULL))
5846 && curwin->w_cursor.lnum > 1)
5847 {
5848 --(curwin->w_cursor.lnum);
5849 coladvance((colnr_T)MAXCOL);
5850 curwin->w_set_curswant = TRUE;
5851
5852 /* When the NL before the first char has to be deleted we
5853 * put the cursor on the NUL after the previous line.
5854 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005855 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if ( (cap->oap->op_type == OP_DELETE
5857 || cap->oap->op_type == OP_CHANGE)
5858 && !lineempty(curwin->w_cursor.lnum))
5859 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005860 if (*ml_get_cursor() != NUL)
5861 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 cap->retval |= CA_NO_ADJ_OP_END;
5863 }
5864 continue;
5865 }
5866 /* Only beep and flush if not moved at all */
5867 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5868 beep_flush();
5869 break;
5870 }
5871 }
5872#ifdef FEAT_FOLDING
5873 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5874 && cap->oap->op_type == OP_NOP)
5875 foldOpenCursor();
5876#endif
5877}
5878
5879/*
5880 * Cursor up commands.
5881 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5882 */
5883 static void
5884nv_up(cap)
5885 cmdarg_T *cap;
5886{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005887 if (mod_mask & MOD_MASK_SHIFT)
5888 {
5889 /* <S-Up> is page up */
5890 cap->arg = BACKWARD;
5891 nv_page(cap);
5892 }
5893 else
5894 {
5895 cap->oap->motion_type = MLINE;
5896 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5897 clearopbeep(cap->oap);
5898 else if (cap->arg)
5899 beginline(BL_WHITE | BL_FIX);
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901}
5902
5903/*
5904 * Cursor down commands.
5905 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5906 */
5907 static void
5908nv_down(cap)
5909 cmdarg_T *cap;
5910{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005911 if (mod_mask & MOD_MASK_SHIFT)
5912 {
5913 /* <S-Down> is page down */
5914 cap->arg = FORWARD;
5915 nv_page(cap);
5916 }
5917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5919 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005920 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005921 if (curwin->w_llist_ref == NULL)
5922 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5923 else
5924 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 else
5926#endif
5927 {
5928#ifdef FEAT_CMDWIN
5929 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005930 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 cmdwin_result = CAR;
5932 else
5933#endif
5934 {
5935 cap->oap->motion_type = MLINE;
5936 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5937 clearopbeep(cap->oap);
5938 else if (cap->arg)
5939 beginline(BL_WHITE | BL_FIX);
5940 }
5941 }
5942}
5943
5944#ifdef FEAT_SEARCHPATH
5945/*
5946 * Grab the file name under the cursor and edit it.
5947 */
5948 static void
5949nv_gotofile(cap)
5950 cmdarg_T *cap;
5951{
5952 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005953 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005955 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 {
5957 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005958 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 return;
5960 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005961#ifdef FEAT_AUTOCMD
5962 if (curbuf_locked())
5963 {
5964 clearop(cap->oap);
5965 return;
5966 }
5967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005969 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 if (ptr != NULL)
5972 {
5973 /* do autowrite if necessary */
5974 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5975 autowrite(curbuf, FALSE);
5976 setpcmark();
5977 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5978 P_HID(curbuf) ? ECMD_HIDE : 0);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005979 if (cap->nchar == 'F' && lnum >= 0)
5980 {
5981 curwin->w_cursor.lnum = lnum;
5982 check_cursor_lnum();
5983 beginline(BL_SOL | BL_FIX);
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 vim_free(ptr);
5986 }
5987 else
5988 clearop(cap->oap);
5989}
5990#endif
5991
5992/*
5993 * <End> command: to end of current line or last line.
5994 */
5995 static void
5996nv_end(cap)
5997 cmdarg_T *cap;
5998{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005999 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006001 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 nv_goto(cap);
6003 cap->count1 = 1; /* to end of current line */
6004 }
6005 nv_dollar(cap);
6006}
6007
6008/*
6009 * Handle the "$" command.
6010 */
6011 static void
6012nv_dollar(cap)
6013 cmdarg_T *cap;
6014{
6015 cap->oap->motion_type = MCHAR;
6016 cap->oap->inclusive = TRUE;
6017#ifdef FEAT_VIRTUALEDIT
6018 /* In virtual mode when off the edge of a line and an operator
6019 * is pending (whew!) keep the cursor where it is.
6020 * Otherwise, send it to the end of the line. */
6021 if (!virtual_active() || gchar_cursor() != NUL
6022 || cap->oap->op_type == OP_NOP)
6023#endif
6024 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6025 if (cursor_down((long)(cap->count1 - 1),
6026 cap->oap->op_type == OP_NOP) == FAIL)
6027 clearopbeep(cap->oap);
6028#ifdef FEAT_FOLDING
6029 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6030 foldOpenCursor();
6031#endif
6032}
6033
6034/*
6035 * Implementation of '?' and '/' commands.
6036 * If cap->arg is TRUE don't set PC mark.
6037 */
6038 static void
6039nv_search(cap)
6040 cmdarg_T *cap;
6041{
6042 oparg_T *oap = cap->oap;
6043
6044 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6045 {
6046 /* Translate "g??" to "g?g?" */
6047 cap->cmdchar = 'g';
6048 cap->nchar = '?';
6049 nv_operator(cap);
6050 return;
6051 }
6052
6053 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6054
6055 if (cap->searchbuf == NULL)
6056 {
6057 clearop(oap);
6058 return;
6059 }
6060
6061 normal_search(cap, cap->cmdchar, cap->searchbuf,
6062 (cap->arg ? 0 : SEARCH_MARK));
6063}
6064
6065/*
6066 * Handle "N" and "n" commands.
6067 * cap->arg is SEARCH_REV for "N", 0 for "n".
6068 */
6069 static void
6070nv_next(cap)
6071 cmdarg_T *cap;
6072{
6073 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6074}
6075
6076/*
6077 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6078 * Uses only cap->count1 and cap->oap from "cap".
6079 */
6080 static void
6081normal_search(cap, dir, pat, opt)
6082 cmdarg_T *cap;
6083 int dir;
6084 char_u *pat;
6085 int opt; /* extra flags for do_search() */
6086{
6087 int i;
6088
6089 cap->oap->motion_type = MCHAR;
6090 cap->oap->inclusive = FALSE;
6091 cap->oap->use_reg_one = TRUE;
6092 curwin->w_set_curswant = TRUE;
6093
6094 i = do_search(cap->oap, dir, pat, cap->count1,
6095 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
6096 if (i == 0)
6097 clearop(cap->oap);
6098 else
6099 {
6100 if (i == 2)
6101 cap->oap->motion_type = MLINE;
6102#ifdef FEAT_VIRTUALEDIT
6103 curwin->w_cursor.coladd = 0;
6104#endif
6105#ifdef FEAT_FOLDING
6106 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6107 foldOpenCursor();
6108#endif
6109 }
6110
6111 /* "/$" will put the cursor after the end of the line, may need to
6112 * correct that here */
6113 check_cursor();
6114}
6115
6116/*
6117 * Character search commands.
6118 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6119 * ',' and FALSE for ';'.
6120 * cap->nchar is NUL for ',' and ';' (repeat the search)
6121 */
6122 static void
6123nv_csearch(cap)
6124 cmdarg_T *cap;
6125{
6126 int t_cmd;
6127
6128 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6129 t_cmd = TRUE;
6130 else
6131 t_cmd = FALSE;
6132
6133 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6135 clearopbeep(cap->oap);
6136 else
6137 {
6138 curwin->w_set_curswant = TRUE;
6139#ifdef FEAT_VIRTUALEDIT
6140 /* Include a Tab for "tx" and for "dfx". */
6141 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6142 && (t_cmd || cap->oap->op_type != OP_NOP))
6143 {
6144 colnr_T scol, ecol;
6145
6146 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6147 curwin->w_cursor.coladd = ecol - scol;
6148 }
6149 else
6150 curwin->w_cursor.coladd = 0;
6151#endif
6152#ifdef FEAT_VISUAL
6153 adjust_for_sel(cap);
6154#endif
6155#ifdef FEAT_FOLDING
6156 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6157 foldOpenCursor();
6158#endif
6159 }
6160}
6161
6162/*
6163 * "[" and "]" commands.
6164 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6165 */
6166 static void
6167nv_brackets(cap)
6168 cmdarg_T *cap;
6169{
6170 pos_T new_pos;
6171 pos_T prev_pos;
6172 pos_T *pos = NULL; /* init for GCC */
6173 pos_T old_pos; /* cursor position before command */
6174 int flag;
6175 long n;
6176 int findc;
6177 int c;
6178
6179 cap->oap->motion_type = MCHAR;
6180 cap->oap->inclusive = FALSE;
6181 old_pos = curwin->w_cursor;
6182#ifdef FEAT_VIRTUALEDIT
6183 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6184#endif
6185
6186#ifdef FEAT_SEARCHPATH
6187 /*
6188 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6189 */
6190 if (cap->nchar == 'f')
6191 nv_gotofile(cap);
6192 else
6193#endif
6194
6195#ifdef FEAT_FIND_ID
6196 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006197 * Find the occurrence(s) of the identifier or define under cursor
6198 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 *
6200 * search list jump
6201 * fwd bwd fwd bwd fwd bwd
6202 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6203 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6204 */
6205 if (vim_strchr((char_u *)
6206#ifdef EBCDIC
6207 "iI\005dD\067",
6208#else
6209 "iI\011dD\004",
6210#endif
6211 cap->nchar) != NULL)
6212 {
6213 char_u *ptr;
6214 int len;
6215
6216 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6217 clearop(cap->oap);
6218 else
6219 {
6220 find_pattern_in_path(ptr, 0, len, TRUE,
6221 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6222 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6223 cap->count1,
6224 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6225 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6226 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6227 (linenr_T)MAXLNUM);
6228 curwin->w_set_curswant = TRUE;
6229 }
6230 }
6231 else
6232#endif
6233
6234 /*
6235 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6236 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6237 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6238 * "[m" or "]m" search for prev/next start of (Java) method.
6239 * "[M" or "]M" search for prev/next end of (Java) method.
6240 */
6241 if ( (cap->cmdchar == '['
6242 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6243 || (cap->cmdchar == ']'
6244 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6245 {
6246 if (cap->nchar == '*')
6247 cap->nchar = '/';
6248 new_pos.lnum = 0;
6249 prev_pos.lnum = 0;
6250 if (cap->nchar == 'm' || cap->nchar == 'M')
6251 {
6252 if (cap->cmdchar == '[')
6253 findc = '{';
6254 else
6255 findc = '}';
6256 n = 9999;
6257 }
6258 else
6259 {
6260 findc = cap->nchar;
6261 n = cap->count1;
6262 }
6263 for ( ; n > 0; --n)
6264 {
6265 if ((pos = findmatchlimit(cap->oap, findc,
6266 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6267 {
6268 if (new_pos.lnum == 0) /* nothing found */
6269 {
6270 if (cap->nchar != 'm' && cap->nchar != 'M')
6271 clearopbeep(cap->oap);
6272 }
6273 else
6274 pos = &new_pos; /* use last one found */
6275 break;
6276 }
6277 prev_pos = new_pos;
6278 curwin->w_cursor = *pos;
6279 new_pos = *pos;
6280 }
6281 curwin->w_cursor = old_pos;
6282
6283 /*
6284 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6285 * brought us to the match for "[m" and "]M" when inside a method.
6286 * Try finding the '{' or '}' we want to be at.
6287 * Also repeat for the given count.
6288 */
6289 if (cap->nchar == 'm' || cap->nchar == 'M')
6290 {
6291 /* norm is TRUE for "]M" and "[m" */
6292 int norm = ((findc == '{') == (cap->nchar == 'm'));
6293
6294 n = cap->count1;
6295 /* found a match: we were inside a method */
6296 if (prev_pos.lnum != 0)
6297 {
6298 pos = &prev_pos;
6299 curwin->w_cursor = prev_pos;
6300 if (norm)
6301 --n;
6302 }
6303 else
6304 pos = NULL;
6305 while (n > 0)
6306 {
6307 for (;;)
6308 {
6309 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6310 {
6311 /* if not found anything, that's an error */
6312 if (pos == NULL)
6313 clearopbeep(cap->oap);
6314 n = 0;
6315 break;
6316 }
6317 c = gchar_cursor();
6318 if (c == '{' || c == '}')
6319 {
6320 /* Must have found end/start of class: use it.
6321 * Or found the place to be at. */
6322 if ((c == findc && norm) || (n == 1 && !norm))
6323 {
6324 new_pos = curwin->w_cursor;
6325 pos = &new_pos;
6326 n = 0;
6327 }
6328 /* if no match found at all, we started outside of the
6329 * class and we're inside now. Just go on. */
6330 else if (new_pos.lnum == 0)
6331 {
6332 new_pos = curwin->w_cursor;
6333 pos = &new_pos;
6334 }
6335 /* found start/end of other method: go to match */
6336 else if ((pos = findmatchlimit(cap->oap, findc,
6337 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6338 0)) == NULL)
6339 n = 0;
6340 else
6341 curwin->w_cursor = *pos;
6342 break;
6343 }
6344 }
6345 --n;
6346 }
6347 curwin->w_cursor = old_pos;
6348 if (pos == NULL && new_pos.lnum != 0)
6349 clearopbeep(cap->oap);
6350 }
6351 if (pos != NULL)
6352 {
6353 setpcmark();
6354 curwin->w_cursor = *pos;
6355 curwin->w_set_curswant = TRUE;
6356#ifdef FEAT_FOLDING
6357 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6358 && cap->oap->op_type == OP_NOP)
6359 foldOpenCursor();
6360#endif
6361 }
6362 }
6363
6364 /*
6365 * "[[", "[]", "]]" and "][": move to start or end of function
6366 */
6367 else if (cap->nchar == '[' || cap->nchar == ']')
6368 {
6369 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6370 flag = '{';
6371 else
6372 flag = '}'; /* "][" or "[]" */
6373
6374 curwin->w_set_curswant = TRUE;
6375 /*
6376 * Imitate strange Vi behaviour: When using "]]" with an operator
6377 * we also stop at '}'.
6378 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006379 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 (cap->oap->op_type != OP_NOP
6381 && cap->arg == FORWARD && flag == '{')))
6382 clearopbeep(cap->oap);
6383 else
6384 {
6385 if (cap->oap->op_type == OP_NOP)
6386 beginline(BL_WHITE | BL_FIX);
6387#ifdef FEAT_FOLDING
6388 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6389 foldOpenCursor();
6390#endif
6391 }
6392 }
6393
6394 /*
6395 * "[p", "[P", "]P" and "]p": put with indent adjustment
6396 */
6397 else if (cap->nchar == 'p' || cap->nchar == 'P')
6398 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006399 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 prep_redo_cmd(cap);
6402 do_put(cap->oap->regname,
6403 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6404 cap->count1, PUT_FIXINDENT);
6405 }
6406 }
6407
6408 /*
6409 * "['", "[`", "]'" and "]`": jump to next mark
6410 */
6411 else if (cap->nchar == '\'' || cap->nchar == '`')
6412 {
6413 pos = &curwin->w_cursor;
6414 for (n = cap->count1; n > 0; --n)
6415 {
6416 prev_pos = *pos;
6417 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6418 cap->nchar == '\'');
6419 if (pos == NULL)
6420 break;
6421 }
6422 if (pos == NULL)
6423 pos = &prev_pos;
6424 nv_cursormark(cap, cap->nchar == '\'', pos);
6425 }
6426
6427#ifdef FEAT_MOUSE
6428 /*
6429 * [ or ] followed by a middle mouse click: put selected text with
6430 * indent adjustment. Any other button just does as usual.
6431 */
6432 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6433 {
6434 (void)do_mouse(cap->oap, cap->nchar,
6435 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6436 cap->count1, PUT_FIXINDENT);
6437 }
6438#endif /* FEAT_MOUSE */
6439
6440#ifdef FEAT_FOLDING
6441 /*
6442 * "[z" and "]z": move to start or end of open fold.
6443 */
6444 else if (cap->nchar == 'z')
6445 {
6446 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6447 cap->count1) == FAIL)
6448 clearopbeep(cap->oap);
6449 }
6450#endif
6451
6452#ifdef FEAT_DIFF
6453 /*
6454 * "[c" and "]c": move to next or previous diff-change.
6455 */
6456 else if (cap->nchar == 'c')
6457 {
6458 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6459 cap->count1) == FAIL)
6460 clearopbeep(cap->oap);
6461 }
6462#endif
6463
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006464#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006465 /*
6466 * "[s", "[S", "]s" and "]S": move to next spell error.
6467 */
6468 else if (cap->nchar == 's' || cap->nchar == 'S')
6469 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006470 setpcmark();
6471 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006472 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6473 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006474 {
6475 clearopbeep(cap->oap);
6476 break;
6477 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006478# ifdef FEAT_FOLDING
6479 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6480 foldOpenCursor();
6481# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006482 }
6483#endif
6484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 /* Not a valid cap->nchar. */
6486 else
6487 clearopbeep(cap->oap);
6488}
6489
6490/*
6491 * Handle Normal mode "%" command.
6492 */
6493 static void
6494nv_percent(cap)
6495 cmdarg_T *cap;
6496{
6497 pos_T *pos;
6498#ifdef FEAT_FOLDING
6499 linenr_T lnum = curwin->w_cursor.lnum;
6500#endif
6501
6502 cap->oap->inclusive = TRUE;
6503 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6504 {
6505 if (cap->count0 > 100)
6506 clearopbeep(cap->oap);
6507 else
6508 {
6509 cap->oap->motion_type = MLINE;
6510 setpcmark();
6511 /* Round up, so CTRL-G will give same value. Watch out for a
6512 * large line count, the line number must not go negative! */
6513 if (curbuf->b_ml.ml_line_count > 1000000)
6514 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6515 / 100L * cap->count0;
6516 else
6517 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6518 cap->count0 + 99L) / 100L;
6519 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6520 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6521 beginline(BL_SOL | BL_FIX);
6522 }
6523 }
6524 else /* "%" : go to matching paren */
6525 {
6526 cap->oap->motion_type = MCHAR;
6527 cap->oap->use_reg_one = TRUE;
6528 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6529 clearopbeep(cap->oap);
6530 else
6531 {
6532 setpcmark();
6533 curwin->w_cursor = *pos;
6534 curwin->w_set_curswant = TRUE;
6535#ifdef FEAT_VIRTUALEDIT
6536 curwin->w_cursor.coladd = 0;
6537#endif
6538#ifdef FEAT_VISUAL
6539 adjust_for_sel(cap);
6540#endif
6541 }
6542 }
6543#ifdef FEAT_FOLDING
6544 if (cap->oap->op_type == OP_NOP
6545 && lnum != curwin->w_cursor.lnum
6546 && (fdo_flags & FDO_PERCENT)
6547 && KeyTyped)
6548 foldOpenCursor();
6549#endif
6550}
6551
6552/*
6553 * Handle "(" and ")" commands.
6554 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6555 */
6556 static void
6557nv_brace(cap)
6558 cmdarg_T *cap;
6559{
6560 cap->oap->motion_type = MCHAR;
6561 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006562 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6563 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 curwin->w_set_curswant = TRUE;
6565
6566 if (findsent(cap->arg, cap->count1) == FAIL)
6567 clearopbeep(cap->oap);
6568 else
6569 {
Bram Moolenaara0453242008-01-03 12:20:39 +00006570 /* Don't leave the cursor on the NUL past a line */
6571 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6572 {
6573 --curwin->w_cursor.col;
6574 cap->oap->inclusive = TRUE;
6575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576#ifdef FEAT_VIRTUALEDIT
6577 curwin->w_cursor.coladd = 0;
6578#endif
6579#ifdef FEAT_FOLDING
6580 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6581 foldOpenCursor();
6582#endif
6583 }
6584}
6585
6586/*
6587 * "m" command: Mark a position.
6588 */
6589 static void
6590nv_mark(cap)
6591 cmdarg_T *cap;
6592{
6593 if (!checkclearop(cap->oap))
6594 {
6595 if (setmark(cap->nchar) == FAIL)
6596 clearopbeep(cap->oap);
6597 }
6598}
6599
6600/*
6601 * "{" and "}" commands.
6602 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6603 */
6604 static void
6605nv_findpar(cap)
6606 cmdarg_T *cap;
6607{
6608 cap->oap->motion_type = MCHAR;
6609 cap->oap->inclusive = FALSE;
6610 cap->oap->use_reg_one = TRUE;
6611 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006612 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 clearopbeep(cap->oap);
6614 else
6615 {
6616#ifdef FEAT_VIRTUALEDIT
6617 curwin->w_cursor.coladd = 0;
6618#endif
6619#ifdef FEAT_FOLDING
6620 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6621 foldOpenCursor();
6622#endif
6623 }
6624}
6625
6626/*
6627 * "u" command: Undo or make lower case.
6628 */
6629 static void
6630nv_undo(cap)
6631 cmdarg_T *cap;
6632{
6633 if (cap->oap->op_type == OP_LOWER
6634#ifdef FEAT_VISUAL
6635 || VIsual_active
6636#endif
6637 )
6638 {
6639 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6640 cap->cmdchar = 'g';
6641 cap->nchar = 'u';
6642 nv_operator(cap);
6643 }
6644 else
6645 nv_kundo(cap);
6646}
6647
6648/*
6649 * <Undo> command.
6650 */
6651 static void
6652nv_kundo(cap)
6653 cmdarg_T *cap;
6654{
6655 if (!checkclearopq(cap->oap))
6656 {
6657 u_undo((int)cap->count1);
6658 curwin->w_set_curswant = TRUE;
6659 }
6660}
6661
6662/*
6663 * Handle the "r" command.
6664 */
6665 static void
6666nv_replace(cap)
6667 cmdarg_T *cap;
6668{
6669 char_u *ptr;
6670 int had_ctrl_v;
6671 long n;
6672
6673 if (checkclearop(cap->oap))
6674 return;
6675
6676 /* get another character */
6677 if (cap->nchar == Ctrl_V)
6678 {
6679 had_ctrl_v = Ctrl_V;
6680 cap->nchar = get_literal();
6681 /* Don't redo a multibyte character with CTRL-V. */
6682 if (cap->nchar > DEL)
6683 had_ctrl_v = NUL;
6684 }
6685 else
6686 had_ctrl_v = NUL;
6687
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006688 /* Abort if the character is a special key. */
6689 if (IS_SPECIAL(cap->nchar))
6690 {
6691 clearopbeep(cap->oap);
6692 return;
6693 }
6694
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695#ifdef FEAT_VISUAL
6696 /* Visual mode "r" */
6697 if (VIsual_active)
6698 {
6699 nv_operator(cap);
6700 return;
6701 }
6702#endif
6703
6704#ifdef FEAT_VIRTUALEDIT
6705 /* Break tabs, etc. */
6706 if (virtual_active())
6707 {
6708 if (u_save_cursor() == FAIL)
6709 return;
6710 if (gchar_cursor() == NUL)
6711 {
6712 /* Add extra space and put the cursor on the first one. */
6713 coladvance_force((colnr_T)(getviscol() + cap->count1));
6714 curwin->w_cursor.col -= cap->count1;
6715 }
6716 else if (gchar_cursor() == TAB)
6717 coladvance_force(getviscol());
6718 }
6719#endif
6720
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006721 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006723 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724#ifdef FEAT_MBYTE
6725 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6726#endif
6727 )
6728 {
6729 clearopbeep(cap->oap);
6730 return;
6731 }
6732
6733 /*
6734 * Replacing with a TAB is done by edit() when it is complicated because
6735 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6736 * Other characters are done below to avoid problems with things like
6737 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6738 */
6739 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6740 {
6741 stuffnumReadbuff(cap->count1);
6742 stuffcharReadbuff('R');
6743 stuffcharReadbuff('\t');
6744 stuffcharReadbuff(ESC);
6745 return;
6746 }
6747
6748 /* save line for undo */
6749 if (u_save_cursor() == FAIL)
6750 return;
6751
6752 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6753 {
6754 /*
6755 * Replace character(s) by a single newline.
6756 * Strange vi behaviour: Only one newline is inserted.
6757 * Delete the characters here.
6758 * Insert the newline with an insert command, takes care of
6759 * autoindent. The insert command depends on being on the last
6760 * character of a line or not.
6761 */
6762#ifdef FEAT_MBYTE
6763 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6764#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006765 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766#endif
6767 stuffcharReadbuff('\r');
6768 stuffcharReadbuff(ESC);
6769
6770 /* Give 'r' to edit(), to get the redo command right. */
6771 invoke_edit(cap, TRUE, 'r', FALSE);
6772 }
6773 else
6774 {
6775 prep_redo(cap->oap->regname, cap->count1,
6776 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6777
6778 curbuf->b_op_start = curwin->w_cursor;
6779#ifdef FEAT_MBYTE
6780 if (has_mbyte)
6781 {
6782 int old_State = State;
6783
6784 if (cap->ncharC1 != 0)
6785 AppendCharToRedobuff(cap->ncharC1);
6786 if (cap->ncharC2 != 0)
6787 AppendCharToRedobuff(cap->ncharC2);
6788
6789 /* This is slow, but it handles replacing a single-byte with a
6790 * multi-byte and the other way around. Also handles adding
6791 * composing characters for utf-8. */
6792 for (n = cap->count1; n > 0; --n)
6793 {
6794 State = REPLACE;
6795 ins_char(cap->nchar);
6796 State = old_State;
6797 if (cap->ncharC1 != 0)
6798 ins_char(cap->ncharC1);
6799 if (cap->ncharC2 != 0)
6800 ins_char(cap->ncharC2);
6801 }
6802 }
6803 else
6804#endif
6805 {
6806 /*
6807 * Replace the characters within one line.
6808 */
6809 for (n = cap->count1; n > 0; --n)
6810 {
6811 /*
6812 * Get ptr again, because u_save and/or showmatch() will have
6813 * released the line. At the same time we let know that the
6814 * line will be changed.
6815 */
6816 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6817 ptr[curwin->w_cursor.col] = cap->nchar;
6818 if (p_sm && msg_silent == 0)
6819 showmatch(cap->nchar);
6820 ++curwin->w_cursor.col;
6821 }
6822#ifdef FEAT_NETBEANS_INTG
6823 if (usingNetbeans)
6824 {
6825 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6826
Bram Moolenaar009b2592004-10-24 19:18:58 +00006827 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6828 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006830 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832#endif
6833
6834 /* mark the buffer as changed and prepare for displaying */
6835 changed_bytes(curwin->w_cursor.lnum,
6836 (colnr_T)(curwin->w_cursor.col - cap->count1));
6837 }
6838 --curwin->w_cursor.col; /* cursor on the last replaced char */
6839#ifdef FEAT_MBYTE
6840 /* if the character on the left of the current cursor is a multi-byte
6841 * character, move two characters left */
6842 if (has_mbyte)
6843 mb_adjust_cursor();
6844#endif
6845 curbuf->b_op_end = curwin->w_cursor;
6846 curwin->w_set_curswant = TRUE;
6847 set_last_insert(cap->nchar);
6848 }
6849}
6850
6851#ifdef FEAT_VISUAL
6852/*
6853 * 'o': Exchange start and end of Visual area.
6854 * 'O': same, but in block mode exchange left and right corners.
6855 */
6856 static void
6857v_swap_corners(cmdchar)
6858 int cmdchar;
6859{
6860 pos_T old_cursor;
6861 colnr_T left, right;
6862
6863 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6864 {
6865 old_cursor = curwin->w_cursor;
6866 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6867 curwin->w_cursor.lnum = VIsual.lnum;
6868 coladvance(left);
6869 VIsual = curwin->w_cursor;
6870
6871 curwin->w_cursor.lnum = old_cursor.lnum;
6872 curwin->w_curswant = right;
6873 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6874 * right one column */
6875 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6876 ++curwin->w_curswant;
6877 coladvance(curwin->w_curswant);
6878 if (curwin->w_cursor.col == old_cursor.col
6879#ifdef FEAT_VIRTUALEDIT
6880 && (!virtual_active()
6881 || curwin->w_cursor.coladd == old_cursor.coladd)
6882#endif
6883 )
6884 {
6885 curwin->w_cursor.lnum = VIsual.lnum;
6886 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6887 ++right;
6888 coladvance(right);
6889 VIsual = curwin->w_cursor;
6890
6891 curwin->w_cursor.lnum = old_cursor.lnum;
6892 coladvance(left);
6893 curwin->w_curswant = left;
6894 }
6895 }
6896 else
6897 {
6898 old_cursor = curwin->w_cursor;
6899 curwin->w_cursor = VIsual;
6900 VIsual = old_cursor;
6901 curwin->w_set_curswant = TRUE;
6902 }
6903}
6904#endif /* FEAT_VISUAL */
6905
6906/*
6907 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6908 */
6909 static void
6910nv_Replace(cap)
6911 cmdarg_T *cap;
6912{
6913#ifdef FEAT_VISUAL
6914 if (VIsual_active) /* "R" is replace lines */
6915 {
6916 cap->cmdchar = 'c';
6917 cap->nchar = NUL;
6918 VIsual_mode = 'V';
6919 nv_operator(cap);
6920 }
6921 else
6922#endif
6923 if (!checkclearopq(cap->oap))
6924 {
6925 if (!curbuf->b_p_ma)
6926 EMSG(_(e_modifiable));
6927 else
6928 {
6929#ifdef FEAT_VIRTUALEDIT
6930 if (virtual_active())
6931 coladvance(getviscol());
6932#endif
6933 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6934 }
6935 }
6936}
6937
6938#ifdef FEAT_VREPLACE
6939/*
6940 * "gr".
6941 */
6942 static void
6943nv_vreplace(cap)
6944 cmdarg_T *cap;
6945{
6946# ifdef FEAT_VISUAL
6947 if (VIsual_active)
6948 {
6949 cap->cmdchar = 'r';
6950 cap->nchar = cap->extra_char;
6951 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6952 }
6953 else
6954# endif
6955 if (!checkclearopq(cap->oap))
6956 {
6957 if (!curbuf->b_p_ma)
6958 EMSG(_(e_modifiable));
6959 else
6960 {
6961 if (cap->extra_char == Ctrl_V) /* get another character */
6962 cap->extra_char = get_literal();
6963 stuffcharReadbuff(cap->extra_char);
6964 stuffcharReadbuff(ESC);
6965# ifdef FEAT_VIRTUALEDIT
6966 if (virtual_active())
6967 coladvance(getviscol());
6968# endif
6969 invoke_edit(cap, TRUE, 'v', FALSE);
6970 }
6971 }
6972}
6973#endif
6974
6975/*
6976 * Swap case for "~" command, when it does not work like an operator.
6977 */
6978 static void
6979n_swapchar(cap)
6980 cmdarg_T *cap;
6981{
6982 long n;
6983 pos_T startpos;
6984 int did_change = 0;
6985#ifdef FEAT_NETBEANS_INTG
6986 pos_T pos;
6987 char_u *ptr;
6988 int count;
6989#endif
6990
6991 if (checkclearopq(cap->oap))
6992 return;
6993
6994 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6995 {
6996 clearopbeep(cap->oap);
6997 return;
6998 }
6999
7000 prep_redo_cmd(cap);
7001
7002 if (u_save_cursor() == FAIL)
7003 return;
7004
7005 startpos = curwin->w_cursor;
7006#ifdef FEAT_NETBEANS_INTG
7007 pos = startpos;
7008#endif
7009 for (n = cap->count1; n > 0; --n)
7010 {
7011 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7012 inc_cursor();
7013 if (gchar_cursor() == NUL)
7014 {
7015 if (vim_strchr(p_ww, '~') != NULL
7016 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7017 {
7018#ifdef FEAT_NETBEANS_INTG
7019 if (usingNetbeans)
7020 {
7021 if (did_change)
7022 {
7023 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007024 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007025 netbeans_removed(curbuf, pos.lnum, pos.col,
7026 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007028 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030 pos.col = 0;
7031 pos.lnum++;
7032 }
7033#endif
7034 ++curwin->w_cursor.lnum;
7035 curwin->w_cursor.col = 0;
7036 if (n > 1)
7037 {
7038 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7039 break;
7040 u_clearline();
7041 }
7042 }
7043 else
7044 break;
7045 }
7046 }
7047#ifdef FEAT_NETBEANS_INTG
7048 if (did_change && usingNetbeans)
7049 {
7050 ptr = ml_get(pos.lnum);
7051 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007052 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7053 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055#endif
7056
7057
7058 check_cursor();
7059 curwin->w_set_curswant = TRUE;
7060 if (did_change)
7061 {
7062 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7063 0L);
7064 curbuf->b_op_start = startpos;
7065 curbuf->b_op_end = curwin->w_cursor;
7066 if (curbuf->b_op_end.col > 0)
7067 --curbuf->b_op_end.col;
7068 }
7069}
7070
7071/*
7072 * Move cursor to mark.
7073 */
7074 static void
7075nv_cursormark(cap, flag, pos)
7076 cmdarg_T *cap;
7077 int flag;
7078 pos_T *pos;
7079{
7080 if (check_mark(pos) == FAIL)
7081 clearop(cap->oap);
7082 else
7083 {
7084 if (cap->cmdchar == '\''
7085 || cap->cmdchar == '`'
7086 || cap->cmdchar == '['
7087 || cap->cmdchar == ']')
7088 setpcmark();
7089 curwin->w_cursor = *pos;
7090 if (flag)
7091 beginline(BL_WHITE | BL_FIX);
7092 else
7093 check_cursor();
7094 }
7095 cap->oap->motion_type = flag ? MLINE : MCHAR;
7096 if (cap->cmdchar == '`')
7097 cap->oap->use_reg_one = TRUE;
7098 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7099 curwin->w_set_curswant = TRUE;
7100}
7101
7102#ifdef FEAT_VISUAL
7103/*
7104 * Handle commands that are operators in Visual mode.
7105 */
7106 static void
7107v_visop(cap)
7108 cmdarg_T *cap;
7109{
7110 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7111
7112 /* Uppercase means linewise, except in block mode, then "D" deletes till
7113 * the end of the line, and "C" replaces til EOL */
7114 if (isupper(cap->cmdchar))
7115 {
7116 if (VIsual_mode != Ctrl_V)
7117 VIsual_mode = 'V';
7118 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7119 curwin->w_curswant = MAXCOL;
7120 }
7121 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7122 nv_operator(cap);
7123}
7124#endif
7125
7126/*
7127 * "s" and "S" commands.
7128 */
7129 static void
7130nv_subst(cap)
7131 cmdarg_T *cap;
7132{
7133#ifdef FEAT_VISUAL
7134 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7135 {
7136 if (cap->cmdchar == 'S')
7137 VIsual_mode = 'V';
7138 cap->cmdchar = 'c';
7139 nv_operator(cap);
7140 }
7141 else
7142#endif
7143 nv_optrans(cap);
7144}
7145
7146/*
7147 * Abbreviated commands.
7148 */
7149 static void
7150nv_abbrev(cap)
7151 cmdarg_T *cap;
7152{
7153 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7154 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7155
7156#ifdef FEAT_VISUAL
7157 /* in Visual mode these commands are operators */
7158 if (VIsual_active)
7159 v_visop(cap);
7160 else
7161#endif
7162 nv_optrans(cap);
7163}
7164
7165/*
7166 * Translate a command into another command.
7167 */
7168 static void
7169nv_optrans(cap)
7170 cmdarg_T *cap;
7171{
7172 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7173 (char_u *)"d$", (char_u *)"c$",
7174 (char_u *)"cl", (char_u *)"cc",
7175 (char_u *)"yy", (char_u *)":s\r"};
7176 static char_u *str = (char_u *)"xXDCsSY&";
7177
7178 if (!checkclearopq(cap->oap))
7179 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007180 /* In Vi "2D" doesn't delete the next line. Can't translate it
7181 * either, because "2." should also not use the count. */
7182 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7183 {
7184 cap->oap->start = curwin->w_cursor;
7185 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007186#ifdef FEAT_EVAL
7187 set_op_var(OP_DELETE);
7188#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007189 cap->count1 = 1;
7190 nv_dollar(cap);
7191 finish_op = TRUE;
7192 ResetRedobuff();
7193 AppendCharToRedobuff('D');
7194 }
7195 else
7196 {
7197 if (cap->count0)
7198 stuffnumReadbuff(cap->count0);
7199 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202 cap->opcount = 0;
7203}
7204
7205/*
7206 * "'" and "`" commands. Also for "g'" and "g`".
7207 * cap->arg is TRUE for "'" and "g'".
7208 */
7209 static void
7210nv_gomark(cap)
7211 cmdarg_T *cap;
7212{
7213 pos_T *pos;
7214 int c;
7215#ifdef FEAT_FOLDING
7216 linenr_T lnum = curwin->w_cursor.lnum;
7217 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7218#endif
7219
7220 if (cap->cmdchar == 'g')
7221 c = cap->extra_char;
7222 else
7223 c = cap->nchar;
7224 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7225 if (pos == (pos_T *)-1) /* jumped to other file */
7226 {
7227 if (cap->arg)
7228 {
7229 check_cursor_lnum();
7230 beginline(BL_WHITE | BL_FIX);
7231 }
7232 else
7233 check_cursor();
7234 }
7235 else
7236 nv_cursormark(cap, cap->arg, pos);
7237
7238#ifdef FEAT_VIRTUALEDIT
7239 /* May need to clear the coladd that a mark includes. */
7240 if (!virtual_active())
7241 curwin->w_cursor.coladd = 0;
7242#endif
7243#ifdef FEAT_FOLDING
7244 if (cap->oap->op_type == OP_NOP
7245 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7246 && (fdo_flags & FDO_MARK)
7247 && old_KeyTyped)
7248 foldOpenCursor();
7249#endif
7250}
7251
7252/*
7253 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7254 */
7255 static void
7256nv_pcmark(cap)
7257 cmdarg_T *cap;
7258{
7259#ifdef FEAT_JUMPLIST
7260 pos_T *pos;
7261# ifdef FEAT_FOLDING
7262 linenr_T lnum = curwin->w_cursor.lnum;
7263 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7264# endif
7265
7266 if (!checkclearopq(cap->oap))
7267 {
7268 if (cap->cmdchar == 'g')
7269 pos = movechangelist((int)cap->count1);
7270 else
7271 pos = movemark((int)cap->count1);
7272 if (pos == (pos_T *)-1) /* jump to other file */
7273 {
7274 curwin->w_set_curswant = TRUE;
7275 check_cursor();
7276 }
7277 else if (pos != NULL) /* can jump */
7278 nv_cursormark(cap, FALSE, pos);
7279 else if (cap->cmdchar == 'g')
7280 {
7281 if (curbuf->b_changelistlen == 0)
7282 EMSG(_("E664: changelist is empty"));
7283 else if (cap->count1 < 0)
7284 EMSG(_("E662: At start of changelist"));
7285 else
7286 EMSG(_("E663: At end of changelist"));
7287 }
7288 else
7289 clearopbeep(cap->oap);
7290# ifdef FEAT_FOLDING
7291 if (cap->oap->op_type == OP_NOP
7292 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7293 && (fdo_flags & FDO_MARK)
7294 && old_KeyTyped)
7295 foldOpenCursor();
7296# endif
7297 }
7298#else
7299 clearopbeep(cap->oap);
7300#endif
7301}
7302
7303/*
7304 * Handle '"' command.
7305 */
7306 static void
7307nv_regname(cap)
7308 cmdarg_T *cap;
7309{
7310 if (checkclearop(cap->oap))
7311 return;
7312#ifdef FEAT_EVAL
7313 if (cap->nchar == '=')
7314 cap->nchar = get_expr_register();
7315#endif
7316 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7317 {
7318 cap->oap->regname = cap->nchar;
7319 cap->opcount = cap->count0; /* remember count before '"' */
7320#ifdef FEAT_EVAL
7321 set_reg_var(cap->oap->regname);
7322#endif
7323 }
7324 else
7325 clearopbeep(cap->oap);
7326}
7327
7328#ifdef FEAT_VISUAL
7329/*
7330 * Handle "v", "V" and "CTRL-V" commands.
7331 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7332 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007333 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 */
7335 static void
7336nv_visual(cap)
7337 cmdarg_T *cap;
7338{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007339 if (cap->cmdchar == Ctrl_Q)
7340 cap->cmdchar = Ctrl_V;
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7343 * characterwise, linewise, or blockwise. */
7344 if (cap->oap->op_type != OP_NOP)
7345 {
7346 cap->oap->motion_force = cap->cmdchar;
7347 finish_op = FALSE; /* operator doesn't finish now but later */
7348 return;
7349 }
7350
7351 VIsual_select = cap->arg;
7352 if (VIsual_active) /* change Visual mode */
7353 {
7354 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7355 end_visual_mode();
7356 else /* toggle char/block mode */
7357 { /* or char/line mode */
7358 VIsual_mode = cap->cmdchar;
7359 showmode();
7360 }
7361 redraw_curbuf_later(INVERTED); /* update the inversion */
7362 }
7363 else /* start Visual mode */
7364 {
7365 check_visual_highlight();
7366 if (cap->count0) /* use previously selected part */
7367 {
7368 if (resel_VIsual_mode == NUL) /* there is none */
7369 {
7370 beep_flush();
7371 return;
7372 }
7373 VIsual = curwin->w_cursor;
7374
7375 VIsual_active = TRUE;
7376 VIsual_reselect = TRUE;
7377 if (!cap->arg)
7378 /* start Select mode when 'selectmode' contains "cmd" */
7379 may_start_select('c');
7380#ifdef FEAT_MOUSE
7381 setmouse();
7382#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007383 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 redraw_cmdline = TRUE; /* show visual mode later */
7385 /*
7386 * For V and ^V, we multiply the number of lines even if there
7387 * was only one -- webb
7388 */
7389 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7390 {
7391 curwin->w_cursor.lnum +=
7392 resel_VIsual_line_count * cap->count0 - 1;
7393 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7394 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7395 }
7396 VIsual_mode = resel_VIsual_mode;
7397 if (VIsual_mode == 'v')
7398 {
7399 if (resel_VIsual_line_count <= 1)
7400 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7401 else
7402 curwin->w_cursor.col = resel_VIsual_col;
7403 check_cursor_col();
7404 }
7405 if (resel_VIsual_col == MAXCOL)
7406 {
7407 curwin->w_curswant = MAXCOL;
7408 coladvance((colnr_T)MAXCOL);
7409 }
7410 else if (VIsual_mode == Ctrl_V)
7411 {
7412 validate_virtcol();
7413 curwin->w_curswant = curwin->w_virtcol
7414 + resel_VIsual_col * cap->count0 - 1;
7415 coladvance(curwin->w_curswant);
7416 }
7417 else
7418 curwin->w_set_curswant = TRUE;
7419 redraw_curbuf_later(INVERTED); /* show the inversion */
7420 }
7421 else
7422 {
7423 if (!cap->arg)
7424 /* start Select mode when 'selectmode' contains "cmd" */
7425 may_start_select('c');
7426 n_start_visual_mode(cap->cmdchar);
7427 }
7428 }
7429}
7430
7431/*
7432 * Start selection for Shift-movement keys.
7433 */
7434 void
7435start_selection()
7436{
7437 /* if 'selectmode' contains "key", start Select mode */
7438 may_start_select('k');
7439 n_start_visual_mode('v');
7440}
7441
7442/*
7443 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7444 */
7445 void
7446may_start_select(c)
7447 int c;
7448{
7449 VIsual_select = (stuff_empty() && typebuf_typed()
7450 && (vim_strchr(p_slm, c) != NULL));
7451}
7452
7453/*
7454 * Start Visual mode "c".
7455 * Should set VIsual_select before calling this.
7456 */
7457 static void
7458n_start_visual_mode(c)
7459 int c;
7460{
7461 VIsual_mode = c;
7462 VIsual_active = TRUE;
7463 VIsual_reselect = TRUE;
7464#ifdef FEAT_VIRTUALEDIT
7465 /* Corner case: the 0 position in a tab may change when going into
7466 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7467 */
7468 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7469 coladvance(curwin->w_virtcol);
7470#endif
7471 VIsual = curwin->w_cursor;
7472
7473#ifdef FEAT_FOLDING
7474 foldAdjustVisual();
7475#endif
7476
7477#ifdef FEAT_MOUSE
7478 setmouse();
7479#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007480 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 redraw_cmdline = TRUE; /* show visual mode later */
7482#ifdef FEAT_CLIPBOARD
7483 /* Make sure the clipboard gets updated. Needed because start and
7484 * end may still be the same, and the selection needs to be owned */
7485 clip_star.vmode = NUL;
7486#endif
7487
7488 /* Only need to redraw this line, unless still need to redraw an old
7489 * Visual area (when 'lazyredraw' is set). */
7490 if (curwin->w_redr_type < INVERTED)
7491 {
7492 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7493 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7494 }
7495}
7496
7497#endif /* FEAT_VISUAL */
7498
7499/*
7500 * CTRL-W: Window commands
7501 */
7502 static void
7503nv_window(cap)
7504 cmdarg_T *cap;
7505{
7506#ifdef FEAT_WINDOWS
7507 if (!checkclearop(cap->oap))
7508 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7509#else
7510 (void)checkclearop(cap->oap);
7511#endif
7512}
7513
7514/*
7515 * CTRL-Z: Suspend
7516 */
7517 static void
7518nv_suspend(cap)
7519 cmdarg_T *cap;
7520{
7521 clearop(cap->oap);
7522#ifdef FEAT_VISUAL
7523 if (VIsual_active)
7524 end_visual_mode(); /* stop Visual mode */
7525#endif
7526 do_cmdline_cmd((char_u *)"st");
7527}
7528
7529/*
7530 * Commands starting with "g".
7531 */
7532 static void
7533nv_g_cmd(cap)
7534 cmdarg_T *cap;
7535{
7536 oparg_T *oap = cap->oap;
7537#ifdef FEAT_VISUAL
7538 pos_T tpos;
7539#endif
7540 int i;
7541 int flag = FALSE;
7542
7543 switch (cap->nchar)
7544 {
7545#ifdef MEM_PROFILE
7546 /*
7547 * "g^A": dump log of used memory.
7548 */
7549 case Ctrl_A:
7550 vim_mem_profile_dump();
7551 break;
7552#endif
7553
7554#ifdef FEAT_VREPLACE
7555 /*
7556 * "gR": Enter virtual replace mode.
7557 */
7558 case 'R':
7559 cap->arg = TRUE;
7560 nv_Replace(cap);
7561 break;
7562
7563 case 'r':
7564 nv_vreplace(cap);
7565 break;
7566#endif
7567
7568 case '&':
7569 do_cmdline_cmd((char_u *)"%s//~/&");
7570 break;
7571
7572#ifdef FEAT_VISUAL
7573 /*
7574 * "gv": Reselect the previous Visual area. If Visual already active,
7575 * exchange previous and current Visual area.
7576 */
7577 case 'v':
7578 if (checkclearop(oap))
7579 break;
7580
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007581 if ( curbuf->b_visual.vi_start.lnum == 0
7582 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7583 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 beep_flush();
7585 else
7586 {
7587 /* set w_cursor to the start of the Visual area, tpos to the end */
7588 if (VIsual_active)
7589 {
7590 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007591 VIsual_mode = curbuf->b_visual.vi_mode;
7592 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593# ifdef FEAT_EVAL
7594 curbuf->b_visual_mode_eval = i;
7595# endif
7596 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007597 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7598 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007600 tpos = curbuf->b_visual.vi_end;
7601 curbuf->b_visual.vi_end = curwin->w_cursor;
7602 curwin->w_cursor = curbuf->b_visual.vi_start;
7603 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 else
7606 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007607 VIsual_mode = curbuf->b_visual.vi_mode;
7608 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7609 tpos = curbuf->b_visual.vi_end;
7610 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612
7613 VIsual_active = TRUE;
7614 VIsual_reselect = TRUE;
7615
7616 /* Set Visual to the start and w_cursor to the end of the Visual
7617 * area. Make sure they are on an existing character. */
7618 check_cursor();
7619 VIsual = curwin->w_cursor;
7620 curwin->w_cursor = tpos;
7621 check_cursor();
7622 update_topline();
7623 /*
7624 * When called from normal "g" command: start Select mode when
7625 * 'selectmode' contains "cmd". When called for K_SELECT, always
7626 * start Select mode.
7627 */
7628 if (cap->arg)
7629 VIsual_select = TRUE;
7630 else
7631 may_start_select('c');
7632#ifdef FEAT_MOUSE
7633 setmouse();
7634#endif
7635#ifdef FEAT_CLIPBOARD
7636 /* Make sure the clipboard gets updated. Needed because start and
7637 * end are still the same, and the selection needs to be owned */
7638 clip_star.vmode = NUL;
7639#endif
7640 redraw_curbuf_later(INVERTED);
7641 showmode();
7642 }
7643 break;
7644 /*
7645 * "gV": Don't reselect the previous Visual area after a Select mode
7646 * mapping of menu.
7647 */
7648 case 'V':
7649 VIsual_reselect = FALSE;
7650 break;
7651
7652 /*
7653 * "gh": start Select mode.
7654 * "gH": start Select line mode.
7655 * "g^H": start Select block mode.
7656 */
7657 case K_BS:
7658 cap->nchar = Ctrl_H;
7659 /* FALLTHROUGH */
7660 case 'h':
7661 case 'H':
7662 case Ctrl_H:
7663# ifdef EBCDIC
7664 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7665 if (cap->nchar == Ctrl_H)
7666 cap->cmdchar = Ctrl_V;
7667 else
7668# endif
7669 cap->cmdchar = cap->nchar + ('v' - 'h');
7670 cap->arg = TRUE;
7671 nv_visual(cap);
7672 break;
7673#endif /* FEAT_VISUAL */
7674
7675 /*
7676 * "gj" and "gk" two new funny movement keys -- up and down
7677 * movement based on *screen* line rather than *file* line.
7678 */
7679 case 'j':
7680 case K_DOWN:
7681 /* with 'nowrap' it works just like the normal "j" command; also when
7682 * in a closed fold */
7683 if (!curwin->w_p_wrap
7684#ifdef FEAT_FOLDING
7685 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7686#endif
7687 )
7688 {
7689 oap->motion_type = MLINE;
7690 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7691 }
7692 else
7693 i = nv_screengo(oap, FORWARD, cap->count1);
7694 if (i == FAIL)
7695 clearopbeep(oap);
7696 break;
7697
7698 case 'k':
7699 case K_UP:
7700 /* with 'nowrap' it works just like the normal "k" command; also when
7701 * in a closed fold */
7702 if (!curwin->w_p_wrap
7703#ifdef FEAT_FOLDING
7704 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7705#endif
7706 )
7707 {
7708 oap->motion_type = MLINE;
7709 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7710 }
7711 else
7712 i = nv_screengo(oap, BACKWARD, cap->count1);
7713 if (i == FAIL)
7714 clearopbeep(oap);
7715 break;
7716
7717 /*
7718 * "gJ": join two lines without inserting a space.
7719 */
7720 case 'J':
7721 nv_join(cap);
7722 break;
7723
7724 /*
7725 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7726 * "gm": middle of "g0" and "g$".
7727 */
7728 case '^':
7729 flag = TRUE;
7730 /* FALLTHROUGH */
7731
7732 case '0':
7733 case 'm':
7734 case K_HOME:
7735 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 oap->motion_type = MCHAR;
7737 oap->inclusive = FALSE;
7738 if (curwin->w_p_wrap
7739#ifdef FEAT_VERTSPLIT
7740 && curwin->w_width != 0
7741#endif
7742 )
7743 {
7744 int width1 = W_WIDTH(curwin) - curwin_col_off();
7745 int width2 = width1 + curwin_col_off2();
7746
7747 validate_virtcol();
7748 i = 0;
7749 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7750 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7751 }
7752 else
7753 i = curwin->w_leftcol;
7754 /* Go to the middle of the screen line. When 'number' is on and lines
7755 * are wrapping the middle can be more to the left.*/
7756 if (cap->nchar == 'm')
7757 i += (W_WIDTH(curwin) - curwin_col_off()
7758 + ((curwin->w_p_wrap && i > 0)
7759 ? curwin_col_off2() : 0)) / 2;
7760 coladvance((colnr_T)i);
7761 if (flag)
7762 {
7763 do
7764 i = gchar_cursor();
7765 while (vim_iswhite(i) && oneright() == OK);
7766 }
7767 curwin->w_set_curswant = TRUE;
7768 break;
7769
7770 case '_':
7771 /* "g_": to the last non-blank character in the line or <count> lines
7772 * downward. */
7773 cap->oap->motion_type = MCHAR;
7774 cap->oap->inclusive = TRUE;
7775 curwin->w_curswant = MAXCOL;
7776 if (cursor_down((long)(cap->count1 - 1),
7777 cap->oap->op_type == OP_NOP) == FAIL)
7778 clearopbeep(cap->oap);
7779 else
7780 {
7781 char_u *ptr = ml_get_curline();
7782
7783 /* In Visual mode we may end up after the line. */
7784 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7785 --curwin->w_cursor.col;
7786
7787 /* Decrease the cursor column until it's on a non-blank. */
7788 while (curwin->w_cursor.col > 0
7789 && vim_iswhite(ptr[curwin->w_cursor.col]))
7790 --curwin->w_cursor.col;
7791 curwin->w_set_curswant = TRUE;
7792 }
7793 break;
7794
7795 case '$':
7796 case K_END:
7797 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 {
7799 int col_off = curwin_col_off();
7800
7801 oap->motion_type = MCHAR;
7802 oap->inclusive = TRUE;
7803 if (curwin->w_p_wrap
7804#ifdef FEAT_VERTSPLIT
7805 && curwin->w_width != 0
7806#endif
7807 )
7808 {
7809 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7810 if (cap->count1 == 1)
7811 {
7812 int width1 = W_WIDTH(curwin) - col_off;
7813 int width2 = width1 + curwin_col_off2();
7814
7815 validate_virtcol();
7816 i = width1 - 1;
7817 if (curwin->w_virtcol >= (colnr_T)width1)
7818 i += ((curwin->w_virtcol - width1) / width2 + 1)
7819 * width2;
7820 coladvance((colnr_T)i);
7821#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7822 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7823 {
7824 /*
7825 * Check for landing on a character that got split at
7826 * the end of the line. We do not want to advance to
7827 * the next screen line.
7828 */
7829 validate_virtcol();
7830 if (curwin->w_virtcol > (colnr_T)i)
7831 --curwin->w_cursor.col;
7832 }
7833#endif
7834 }
7835 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7836 clearopbeep(oap);
7837 }
7838 else
7839 {
7840 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7841 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007842
7843 /* Make sure we stick in this column. */
7844 validate_virtcol();
7845 curwin->w_curswant = curwin->w_virtcol;
7846 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 }
7849 break;
7850
7851 /*
7852 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7853 */
7854 case '*':
7855 case '#':
7856#if POUND != '#'
7857 case POUND: /* pound sign (sometimes equal to '#') */
7858#endif
7859 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7860 case ']': /* :tselect for current identifier */
7861 nv_ident(cap);
7862 break;
7863
7864 /*
7865 * ge and gE: go back to end of word
7866 */
7867 case 'e':
7868 case 'E':
7869 oap->motion_type = MCHAR;
7870 curwin->w_set_curswant = TRUE;
7871 oap->inclusive = TRUE;
7872 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7873 clearopbeep(oap);
7874 break;
7875
7876 /*
7877 * "g CTRL-G": display info about cursor position
7878 */
7879 case Ctrl_G:
7880 cursor_pos_info();
7881 break;
7882
7883 /*
7884 * "gi": start Insert at the last position.
7885 */
7886 case 'i':
7887 if (curbuf->b_last_insert.lnum != 0)
7888 {
7889 curwin->w_cursor = curbuf->b_last_insert;
7890 check_cursor_lnum();
7891 i = (int)STRLEN(ml_get_curline());
7892 if (curwin->w_cursor.col > (colnr_T)i)
7893 {
7894#ifdef FEAT_VIRTUALEDIT
7895 if (virtual_active())
7896 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7897#endif
7898 curwin->w_cursor.col = i;
7899 }
7900 }
7901 cap->cmdchar = 'i';
7902 nv_edit(cap);
7903 break;
7904
7905 /*
7906 * "gI": Start insert in column 1.
7907 */
7908 case 'I':
7909 beginline(0);
7910 if (!checkclearopq(oap))
7911 invoke_edit(cap, FALSE, 'g', FALSE);
7912 break;
7913
7914#ifdef FEAT_SEARCHPATH
7915 /*
7916 * "gf": goto file, edit file under cursor
7917 * "]f" and "[f": can also be used.
7918 */
7919 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007920 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 nv_gotofile(cap);
7922 break;
7923#endif
7924
7925 /* "g'm" and "g`m": jump to mark without setting pcmark */
7926 case '\'':
7927 cap->arg = TRUE;
7928 /*FALLTHROUGH*/
7929 case '`':
7930 nv_gomark(cap);
7931 break;
7932
7933 /*
7934 * "gs": Goto sleep.
7935 */
7936 case 's':
7937 do_sleep(cap->count1 * 1000L);
7938 break;
7939
7940 /*
7941 * "ga": Display the ascii value of the character under the
7942 * cursor. It is displayed in decimal, hex, and octal. -- webb
7943 */
7944 case 'a':
7945 do_ascii(NULL);
7946 break;
7947
7948#ifdef FEAT_MBYTE
7949 /*
7950 * "g8": Display the bytes used for the UTF-8 character under the
7951 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00007952 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 */
7954 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00007955 if (cap->count0 == 8)
7956 utf_find_illegal();
7957 else
7958 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 break;
7960#endif
7961
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007962 case '<':
7963 show_sb_text();
7964 break;
7965
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 /*
7967 * "gg": Goto the first line in file. With a count it goes to
7968 * that line number like for "G". -- webb
7969 */
7970 case 'g':
7971 cap->arg = FALSE;
7972 nv_goto(cap);
7973 break;
7974
7975 /*
7976 * Two-character operators:
7977 * "gq" Format text
7978 * "gw" Format text and keep cursor position
7979 * "g~" Toggle the case of the text.
7980 * "gu" Change text to lower case.
7981 * "gU" Change text to upper case.
7982 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007983 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 */
7985 case 'q':
7986 case 'w':
7987 oap->cursor_start = curwin->w_cursor;
7988 /*FALLTHROUGH*/
7989 case '~':
7990 case 'u':
7991 case 'U':
7992 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007993 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 nv_operator(cap);
7995 break;
7996
7997 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007998 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 * current function
8000 * "gD": idem, but in the current file.
8001 */
8002 case 'd':
8003 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008004 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 break;
8006
8007#ifdef FEAT_MOUSE
8008 /*
8009 * g<*Mouse> : <C-*mouse>
8010 */
8011 case K_MIDDLEMOUSE:
8012 case K_MIDDLEDRAG:
8013 case K_MIDDLERELEASE:
8014 case K_LEFTMOUSE:
8015 case K_LEFTDRAG:
8016 case K_LEFTRELEASE:
8017 case K_RIGHTMOUSE:
8018 case K_RIGHTDRAG:
8019 case K_RIGHTRELEASE:
8020 case K_X1MOUSE:
8021 case K_X1DRAG:
8022 case K_X1RELEASE:
8023 case K_X2MOUSE:
8024 case K_X2DRAG:
8025 case K_X2RELEASE:
8026 mod_mask = MOD_MASK_CTRL;
8027 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8028 break;
8029#endif
8030
8031 case K_IGNORE:
8032 break;
8033
8034 /*
8035 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8036 */
8037 case 'p':
8038 case 'P':
8039 nv_put(cap);
8040 break;
8041
8042#ifdef FEAT_BYTEOFF
8043 /* "go": goto byte count from start of buffer */
8044 case 'o':
8045 goto_byte(cap->count0);
8046 break;
8047#endif
8048
8049 /* "gQ": improved Ex mode */
8050 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008051 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
8053 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008054 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 break;
8056 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (!checkclearopq(oap))
8059 do_exmode(TRUE);
8060 break;
8061
8062#ifdef FEAT_JUMPLIST
8063 case ',':
8064 nv_pcmark(cap);
8065 break;
8066
8067 case ';':
8068 cap->count1 = -cap->count1;
8069 nv_pcmark(cap);
8070 break;
8071#endif
8072
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008073#ifdef FEAT_WINDOWS
8074 case 't':
8075 goto_tabpage((int)cap->count0);
8076 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008077 case 'T':
8078 goto_tabpage(-(int)cap->count1);
8079 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008080#endif
8081
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008082 case '+':
8083 case '-': /* "g+" and "g-": undo or redo along the timeline */
8084 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008085 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
8086 FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008087 break;
8088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 default:
8090 clearopbeep(oap);
8091 break;
8092 }
8093}
8094
8095/*
8096 * Handle "o" and "O" commands.
8097 */
8098 static void
8099n_opencmd(cap)
8100 cmdarg_T *cap;
8101{
8102 if (!checkclearopq(cap->oap))
8103 {
8104#ifdef FEAT_FOLDING
8105 if (cap->cmdchar == 'O')
8106 /* Open above the first line of a folded sequence of lines */
8107 (void)hasFolding(curwin->w_cursor.lnum,
8108 &curwin->w_cursor.lnum, NULL);
8109 else
8110 /* Open below the last line of a folded sequence of lines */
8111 (void)hasFolding(curwin->w_cursor.lnum,
8112 NULL, &curwin->w_cursor.lnum);
8113#endif
8114 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8115 (cap->cmdchar == 'O' ? 1 : 0)),
8116 (linenr_T)(curwin->w_cursor.lnum +
8117 (cap->cmdchar == 'o' ? 1 : 0))
8118 ) == OK
8119 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8120#ifdef FEAT_COMMENTS
8121 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8122#endif
8123 0, 0))
8124 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008125 /* When '#' is in 'cpoptions' ignore the count. */
8126 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8127 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8129 }
8130 }
8131}
8132
8133/*
8134 * "." command: redo last change.
8135 */
8136 static void
8137nv_dot(cap)
8138 cmdarg_T *cap;
8139{
8140 if (!checkclearopq(cap->oap))
8141 {
8142 /*
8143 * If "restart_edit" is TRUE, the last but one command is repeated
8144 * instead of the last command (inserting text). This is used for
8145 * CTRL-O <.> in insert mode.
8146 */
8147 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8148 clearopbeep(cap->oap);
8149 }
8150}
8151
8152/*
8153 * CTRL-R: undo undo
8154 */
8155 static void
8156nv_redo(cap)
8157 cmdarg_T *cap;
8158{
8159 if (!checkclearopq(cap->oap))
8160 {
8161 u_redo((int)cap->count1);
8162 curwin->w_set_curswant = TRUE;
8163 }
8164}
8165
8166/*
8167 * Handle "U" command.
8168 */
8169 static void
8170nv_Undo(cap)
8171 cmdarg_T *cap;
8172{
8173 /* In Visual mode and typing "gUU" triggers an operator */
8174 if (cap->oap->op_type == OP_UPPER
8175#ifdef FEAT_VISUAL
8176 || VIsual_active
8177#endif
8178 )
8179 {
8180 /* translate "gUU" to "gUgU" */
8181 cap->cmdchar = 'g';
8182 cap->nchar = 'U';
8183 nv_operator(cap);
8184 }
8185 else if (!checkclearopq(cap->oap))
8186 {
8187 u_undoline();
8188 curwin->w_set_curswant = TRUE;
8189 }
8190}
8191
8192/*
8193 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8194 * single character.
8195 */
8196 static void
8197nv_tilde(cap)
8198 cmdarg_T *cap;
8199{
8200 if (!p_to
8201#ifdef FEAT_VISUAL
8202 && !VIsual_active
8203#endif
8204 && cap->oap->op_type != OP_TILDE)
8205 n_swapchar(cap);
8206 else
8207 nv_operator(cap);
8208}
8209
8210/*
8211 * Handle an operator command.
8212 * The actual work is done by do_pending_operator().
8213 */
8214 static void
8215nv_operator(cap)
8216 cmdarg_T *cap;
8217{
8218 int op_type;
8219
8220 op_type = get_op_type(cap->cmdchar, cap->nchar);
8221
8222 if (op_type == cap->oap->op_type) /* double operator works on lines */
8223 nv_lineop(cap);
8224 else if (!checkclearop(cap->oap))
8225 {
8226 cap->oap->start = curwin->w_cursor;
8227 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008228#ifdef FEAT_EVAL
8229 set_op_var(op_type);
8230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
8232}
8233
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008234#ifdef FEAT_EVAL
8235/*
8236 * Set v:operator to the characters for "optype".
8237 */
8238 static void
8239set_op_var(optype)
8240 int optype;
8241{
8242 char_u opchars[3];
8243
8244 if (optype == OP_NOP)
8245 set_vim_var_string(VV_OP, NULL, 0);
8246 else
8247 {
8248 opchars[0] = get_op_char(optype);
8249 opchars[1] = get_extra_op_char(optype);
8250 opchars[2] = NUL;
8251 set_vim_var_string(VV_OP, opchars, -1);
8252 }
8253}
8254#endif
8255
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256/*
8257 * Handle linewise operator "dd", "yy", etc.
8258 *
8259 * "_" is is a strange motion command that helps make operators more logical.
8260 * It is actually implemented, but not documented in the real Vi. This motion
8261 * command actually refers to "the current line". Commands like "dd" and "yy"
8262 * are really an alternate form of "d_" and "y_". It does accept a count, so
8263 * "d3_" works to delete 3 lines.
8264 */
8265 static void
8266nv_lineop(cap)
8267 cmdarg_T *cap;
8268{
8269 cap->oap->motion_type = MLINE;
8270 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8271 clearopbeep(cap->oap);
8272 else if ( cap->oap->op_type == OP_DELETE
8273 || cap->oap->op_type == OP_LSHIFT
8274 || cap->oap->op_type == OP_RSHIFT)
8275 beginline(BL_SOL | BL_FIX);
8276 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8277 beginline(BL_WHITE | BL_FIX);
8278}
8279
8280/*
8281 * <Home> command.
8282 */
8283 static void
8284nv_home(cap)
8285 cmdarg_T *cap;
8286{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008287 /* CTRL-HOME is like "gg" */
8288 if (mod_mask & MOD_MASK_CTRL)
8289 nv_goto(cap);
8290 else
8291 {
8292 cap->count0 = 1;
8293 nv_pipe(cap);
8294 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008295 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8296 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297}
8298
8299/*
8300 * "|" command.
8301 */
8302 static void
8303nv_pipe(cap)
8304 cmdarg_T *cap;
8305{
8306 cap->oap->motion_type = MCHAR;
8307 cap->oap->inclusive = FALSE;
8308 beginline(0);
8309 if (cap->count0 > 0)
8310 {
8311 coladvance((colnr_T)(cap->count0 - 1));
8312 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8313 }
8314 else
8315 curwin->w_curswant = 0;
8316 /* keep curswant at the column where we wanted to go, not where
8317 we ended; differs if line is too short */
8318 curwin->w_set_curswant = FALSE;
8319}
8320
8321/*
8322 * Handle back-word command "b" and "B".
8323 * cap->arg is 1 for "B"
8324 */
8325 static void
8326nv_bck_word(cap)
8327 cmdarg_T *cap;
8328{
8329 cap->oap->motion_type = MCHAR;
8330 cap->oap->inclusive = FALSE;
8331 curwin->w_set_curswant = TRUE;
8332 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8333 clearopbeep(cap->oap);
8334#ifdef FEAT_FOLDING
8335 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8336 foldOpenCursor();
8337#endif
8338}
8339
8340/*
8341 * Handle word motion commands "e", "E", "w" and "W".
8342 * cap->arg is TRUE for "E" and "W".
8343 */
8344 static void
8345nv_wordcmd(cap)
8346 cmdarg_T *cap;
8347{
8348 int n;
8349 int word_end;
8350 int flag = FALSE;
8351
8352 /*
8353 * Set inclusive for the "E" and "e" command.
8354 */
8355 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8356 word_end = TRUE;
8357 else
8358 word_end = FALSE;
8359 cap->oap->inclusive = word_end;
8360
8361 /*
8362 * "cw" and "cW" are a special case.
8363 */
8364 if (!word_end && cap->oap->op_type == OP_CHANGE)
8365 {
8366 n = gchar_cursor();
8367 if (n != NUL) /* not an empty line */
8368 {
8369 if (vim_iswhite(n))
8370 {
8371 /*
8372 * Reproduce a funny Vi behaviour: "cw" on a blank only
8373 * changes one character, not all blanks until the start of
8374 * the next word. Only do this when the 'w' flag is included
8375 * in 'cpoptions'.
8376 */
8377 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8378 {
8379 cap->oap->inclusive = TRUE;
8380 cap->oap->motion_type = MCHAR;
8381 return;
8382 }
8383 }
8384 else
8385 {
8386 /*
8387 * This is a little strange. To match what the real Vi does,
8388 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8389 * that we are not on a space or a TAB. This seems impolite
8390 * at first, but it's really more what we mean when we say
8391 * 'cw'.
8392 * Another strangeness: When standing on the end of a word
8393 * "ce" will change until the end of the next wordt, but "cw"
8394 * will change only one character! This is done by setting
8395 * flag.
8396 */
8397 cap->oap->inclusive = TRUE;
8398 word_end = TRUE;
8399 flag = TRUE;
8400 }
8401 }
8402 }
8403
8404 cap->oap->motion_type = MCHAR;
8405 curwin->w_set_curswant = TRUE;
8406 if (word_end)
8407 n = end_word(cap->count1, cap->arg, flag, FALSE);
8408 else
8409 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8410
8411 /* Don't leave the cursor on the NUL past a line */
Bram Moolenaara84b1062007-09-13 13:34:02 +00008412 if (n != FAIL && curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {
8414 --curwin->w_cursor.col;
8415 cap->oap->inclusive = TRUE;
8416 }
8417
8418 if (n == FAIL && cap->oap->op_type == OP_NOP)
8419 clearopbeep(cap->oap);
8420 else
8421 {
8422#ifdef FEAT_VISUAL
8423 adjust_for_sel(cap);
8424#endif
8425#ifdef FEAT_FOLDING
8426 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8427 foldOpenCursor();
8428#endif
8429 }
8430}
8431
8432/*
8433 * "0" and "^" commands.
8434 * cap->arg is the argument for beginline().
8435 */
8436 static void
8437nv_beginline(cap)
8438 cmdarg_T *cap;
8439{
8440 cap->oap->motion_type = MCHAR;
8441 cap->oap->inclusive = FALSE;
8442 beginline(cap->arg);
8443#ifdef FEAT_FOLDING
8444 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8445 foldOpenCursor();
8446#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008447 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8448 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
8450
8451#ifdef FEAT_VISUAL
8452/*
8453 * In exclusive Visual mode, may include the last character.
8454 */
8455 static void
8456adjust_for_sel(cap)
8457 cmdarg_T *cap;
8458{
8459 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8460 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8461 {
8462# ifdef FEAT_MBYTE
8463 if (has_mbyte)
8464 inc_cursor();
8465 else
8466# endif
8467 ++curwin->w_cursor.col;
8468 cap->oap->inclusive = FALSE;
8469 }
8470}
8471
8472/*
8473 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8474 * Should check VIsual_mode before calling this.
8475 * Returns TRUE when backed up to the previous line.
8476 */
8477 static int
8478unadjust_for_sel()
8479{
8480 pos_T *pp;
8481
8482 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8483 {
8484 if (lt(VIsual, curwin->w_cursor))
8485 pp = &curwin->w_cursor;
8486 else
8487 pp = &VIsual;
8488#ifdef FEAT_VIRTUALEDIT
8489 if (pp->coladd > 0)
8490 --pp->coladd;
8491 else
8492#endif
8493 if (pp->col > 0)
8494 {
8495 --pp->col;
8496#ifdef FEAT_MBYTE
8497 mb_adjustpos(pp);
8498#endif
8499 }
8500 else if (pp->lnum > 1)
8501 {
8502 --pp->lnum;
8503 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8504 return TRUE;
8505 }
8506 }
8507 return FALSE;
8508}
8509
8510/*
8511 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8512 */
8513 static void
8514nv_select(cap)
8515 cmdarg_T *cap;
8516{
8517 if (VIsual_active)
8518 VIsual_select = TRUE;
8519 else if (VIsual_reselect)
8520 {
8521 cap->nchar = 'v'; /* fake "gv" command */
8522 cap->arg = TRUE;
8523 nv_g_cmd(cap);
8524 }
8525}
8526
8527#endif
8528
8529/*
8530 * "G", "gg", CTRL-END, CTRL-HOME.
8531 * cap->arg is TRUE for "G".
8532 */
8533 static void
8534nv_goto(cap)
8535 cmdarg_T *cap;
8536{
8537 linenr_T lnum;
8538
8539 if (cap->arg)
8540 lnum = curbuf->b_ml.ml_line_count;
8541 else
8542 lnum = 1L;
8543 cap->oap->motion_type = MLINE;
8544 setpcmark();
8545
8546 /* When a count is given, use it instead of the default lnum */
8547 if (cap->count0 != 0)
8548 lnum = cap->count0;
8549 if (lnum < 1L)
8550 lnum = 1L;
8551 else if (lnum > curbuf->b_ml.ml_line_count)
8552 lnum = curbuf->b_ml.ml_line_count;
8553 curwin->w_cursor.lnum = lnum;
8554 beginline(BL_SOL | BL_FIX);
8555#ifdef FEAT_FOLDING
8556 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8557 foldOpenCursor();
8558#endif
8559}
8560
8561/*
8562 * CTRL-\ in Normal mode.
8563 */
8564 static void
8565nv_normal(cap)
8566 cmdarg_T *cap;
8567{
8568 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8569 {
8570 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008571 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 clear_cmdline = TRUE; /* unshow mode later */
8573 restart_edit = 0;
8574#ifdef FEAT_CMDWIN
8575 if (cmdwin_type != 0)
8576 cmdwin_result = Ctrl_C;
8577#endif
8578#ifdef FEAT_VISUAL
8579 if (VIsual_active)
8580 {
8581 end_visual_mode(); /* stop Visual */
8582 redraw_curbuf_later(INVERTED);
8583 }
8584#endif
8585 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8586 if (cap->nchar == Ctrl_G && p_im)
8587 restart_edit = 'a';
8588 }
8589 else
8590 clearopbeep(cap->oap);
8591}
8592
8593/*
8594 * ESC in Normal mode: beep, but don't flush buffers.
8595 * Don't even beep if we are canceling a command.
8596 */
8597 static void
8598nv_esc(cap)
8599 cmdarg_T *cap;
8600{
8601 int no_reason;
8602
8603 no_reason = (cap->oap->op_type == OP_NOP
8604 && cap->opcount == 0
8605 && cap->count0 == 0
8606 && cap->oap->regname == 0
8607 && !p_im);
8608
8609 if (cap->arg) /* TRUE for CTRL-C */
8610 {
8611 if (restart_edit == 0
8612#ifdef FEAT_CMDWIN
8613 && cmdwin_type == 0
8614#endif
8615#ifdef FEAT_VISUAL
8616 && !VIsual_active
8617#endif
8618 && no_reason)
8619 MSG(_("Type :quit<Enter> to exit Vim"));
8620
8621 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8622 * set again below when halfway a mapping. */
8623 if (!p_im)
8624 restart_edit = 0;
8625#ifdef FEAT_CMDWIN
8626 if (cmdwin_type != 0)
8627 {
8628 cmdwin_result = K_IGNORE;
8629 got_int = FALSE; /* don't stop executing autocommands et al. */
8630 return;
8631 }
8632#endif
8633 }
8634
8635#ifdef FEAT_VISUAL
8636 if (VIsual_active)
8637 {
8638 end_visual_mode(); /* stop Visual */
8639 check_cursor_col(); /* make sure cursor is not beyond EOL */
8640 curwin->w_set_curswant = TRUE;
8641 redraw_curbuf_later(INVERTED);
8642 }
8643 else
8644#endif
8645 if (no_reason)
8646 vim_beep();
8647 clearop(cap->oap);
8648
8649 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8650 * set return to Insert mode afterwards. */
8651 if (restart_edit == 0 && goto_im()
8652#ifdef FEAT_EX_EXTRA
8653 && ex_normal_busy == 0
8654#endif
8655 )
8656 restart_edit = 'a';
8657}
8658
8659/*
8660 * Handle "A", "a", "I", "i" and <Insert> commands.
8661 */
8662 static void
8663nv_edit(cap)
8664 cmdarg_T *cap;
8665{
8666 /* <Insert> is equal to "i" */
8667 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8668 cap->cmdchar = 'i';
8669
8670#ifdef FEAT_VISUAL
8671 /* in Visual mode "A" and "I" are an operator */
8672 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8673 v_visop(cap);
8674
8675 /* in Visual mode and after an operator "a" and "i" are for text objects */
8676 else
8677#endif
8678 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8679 && (cap->oap->op_type != OP_NOP
8680#ifdef FEAT_VISUAL
8681 || VIsual_active
8682#endif
8683 ))
8684 {
8685#ifdef FEAT_TEXTOBJ
8686 nv_object(cap);
8687#else
8688 clearopbeep(cap->oap);
8689#endif
8690 }
8691 else if (!curbuf->b_p_ma && !p_im)
8692 {
8693 /* Only give this error when 'insertmode' is off. */
8694 EMSG(_(e_modifiable));
8695 clearop(cap->oap);
8696 }
8697 else if (!checkclearopq(cap->oap))
8698 {
8699 switch (cap->cmdchar)
8700 {
8701 case 'A': /* "A"ppend after the line */
8702 curwin->w_set_curswant = TRUE;
8703#ifdef FEAT_VIRTUALEDIT
8704 if (ve_flags == VE_ALL)
8705 {
8706 int save_State = State;
8707
8708 /* Pretent Insert mode here to allow the cursor on the
8709 * character past the end of the line */
8710 State = INSERT;
8711 coladvance((colnr_T)MAXCOL);
8712 State = save_State;
8713 }
8714 else
8715#endif
8716 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8717 break;
8718
8719 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008720 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8721 beginline(BL_WHITE);
8722 else
8723 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 break;
8725
8726 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8727#ifdef FEAT_VIRTUALEDIT
8728 /* increment coladd when in virtual space, increment the
8729 * column otherwise, also to append after an unprintable char */
8730 if (virtual_active()
8731 && (curwin->w_cursor.coladd > 0
8732 || *ml_get_cursor() == NUL
8733 || *ml_get_cursor() == TAB))
8734 curwin->w_cursor.coladd++;
8735 else
8736#endif
8737 if (*ml_get_cursor() != NUL)
8738 inc_cursor();
8739 break;
8740 }
8741
8742#ifdef FEAT_VIRTUALEDIT
8743 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8744 {
8745 int save_State = State;
8746
8747 /* Pretent Insert mode here to allow the cursor on the
8748 * character past the end of the line */
8749 State = INSERT;
8750 coladvance(getviscol());
8751 State = save_State;
8752 }
8753#endif
8754
8755 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8756 }
8757}
8758
8759/*
8760 * Invoke edit() and take care of "restart_edit" and the return value.
8761 */
8762 static void
8763invoke_edit(cap, repl, cmd, startln)
8764 cmdarg_T *cap;
8765 int repl; /* "r" or "gr" command */
8766 int cmd;
8767 int startln;
8768{
8769 int restart_edit_save = 0;
8770
8771 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8772 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8773 * it. */
8774 if (repl || !stuff_empty())
8775 restart_edit_save = restart_edit;
8776 else
8777 restart_edit_save = 0;
8778
8779 /* Always reset "restart_edit", this is not a restarted edit. */
8780 restart_edit = 0;
8781
8782 if (edit(cmd, startln, cap->count1))
8783 cap->retval |= CA_COMMAND_BUSY;
8784
8785 if (restart_edit == 0)
8786 restart_edit = restart_edit_save;
8787}
8788
8789#ifdef FEAT_TEXTOBJ
8790/*
8791 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8792 */
8793 static void
8794nv_object(cap)
8795 cmdarg_T *cap;
8796{
8797 int flag;
8798 int include;
8799 char_u *mps_save;
8800
8801 if (cap->cmdchar == 'i')
8802 include = FALSE; /* "ix" = inner object: exclude white space */
8803 else
8804 include = TRUE; /* "ax" = an object: include white space */
8805
8806 /* Make sure (), [], {} and <> are in 'matchpairs' */
8807 mps_save = curbuf->b_p_mps;
8808 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8809
8810 switch (cap->nchar)
8811 {
8812 case 'w': /* "aw" = a word */
8813 flag = current_word(cap->oap, cap->count1, include, FALSE);
8814 break;
8815 case 'W': /* "aW" = a WORD */
8816 flag = current_word(cap->oap, cap->count1, include, TRUE);
8817 break;
8818 case 'b': /* "ab" = a braces block */
8819 case '(':
8820 case ')':
8821 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8822 break;
8823 case 'B': /* "aB" = a Brackets block */
8824 case '{':
8825 case '}':
8826 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8827 break;
8828 case '[': /* "a[" = a [] block */
8829 case ']':
8830 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8831 break;
8832 case '<': /* "a<" = a <> block */
8833 case '>':
8834 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8835 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008836 case 't': /* "at" = a tag block (xml and html) */
8837 flag = current_tagblock(cap->oap, cap->count1, include);
8838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 case 'p': /* "ap" = a paragraph */
8840 flag = current_par(cap->oap, cap->count1, include, 'p');
8841 break;
8842 case 's': /* "as" = a sentence */
8843 flag = current_sent(cap->oap, cap->count1, include);
8844 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008845 case '"': /* "a"" = a double quoted string */
8846 case '\'': /* "a'" = a single quoted string */
8847 case '`': /* "a`" = a backtick quoted string */
8848 flag = current_quote(cap->oap, cap->count1, include,
8849 cap->nchar);
8850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#if 0 /* TODO */
8852 case 'S': /* "aS" = a section */
8853 case 'f': /* "af" = a filename */
8854 case 'u': /* "au" = a URL */
8855#endif
8856 default:
8857 flag = FAIL;
8858 break;
8859 }
8860
8861 curbuf->b_p_mps = mps_save;
8862 if (flag == FAIL)
8863 clearopbeep(cap->oap);
8864 adjust_cursor_col();
8865 curwin->w_set_curswant = TRUE;
8866}
8867#endif
8868
8869/*
8870 * "q" command: Start/stop recording.
8871 * "q:", "q/", "q?": edit command-line in command-line window.
8872 */
8873 static void
8874nv_record(cap)
8875 cmdarg_T *cap;
8876{
8877 if (cap->oap->op_type == OP_FORMAT)
8878 {
8879 /* "gqq" is the same as "gqgq": format line */
8880 cap->cmdchar = 'g';
8881 cap->nchar = 'q';
8882 nv_operator(cap);
8883 }
8884 else if (!checkclearop(cap->oap))
8885 {
8886#ifdef FEAT_CMDWIN
8887 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8888 {
8889 stuffcharReadbuff(cap->nchar);
8890 stuffcharReadbuff(K_CMDWIN);
8891 }
8892 else
8893#endif
8894 /* (stop) recording into a named register, unless executing a
8895 * register */
8896 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8897 clearopbeep(cap->oap);
8898 }
8899}
8900
8901/*
8902 * Handle the "@r" command.
8903 */
8904 static void
8905nv_at(cap)
8906 cmdarg_T *cap;
8907{
8908 if (checkclearop(cap->oap))
8909 return;
8910#ifdef FEAT_EVAL
8911 if (cap->nchar == '=')
8912 {
8913 if (get_expr_register() == NUL)
8914 return;
8915 }
8916#endif
8917 while (cap->count1-- && !got_int)
8918 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008919 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
8921 clearopbeep(cap->oap);
8922 break;
8923 }
8924 line_breakcheck();
8925 }
8926}
8927
8928/*
8929 * Handle the CTRL-U and CTRL-D commands.
8930 */
8931 static void
8932nv_halfpage(cap)
8933 cmdarg_T *cap;
8934{
8935 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8936 || (cap->cmdchar == Ctrl_D
8937 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8938 clearopbeep(cap->oap);
8939 else if (!checkclearop(cap->oap))
8940 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8941}
8942
8943/*
8944 * Handle "J" or "gJ" command.
8945 */
8946 static void
8947nv_join(cap)
8948 cmdarg_T *cap;
8949{
8950#ifdef FEAT_VISUAL
8951 if (VIsual_active) /* join the visual lines */
8952 nv_operator(cap);
8953 else
8954#endif
8955 if (!checkclearop(cap->oap))
8956 {
8957 if (cap->count0 <= 1)
8958 cap->count0 = 2; /* default for join is two lines! */
8959 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8960 curbuf->b_ml.ml_line_count)
8961 clearopbeep(cap->oap); /* beyond last line */
8962 else
8963 {
8964 prep_redo(cap->oap->regname, cap->count0,
8965 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8966 do_do_join(cap->count0, cap->nchar == NUL);
8967 }
8968 }
8969}
8970
8971/*
8972 * "P", "gP", "p" and "gp" commands.
8973 */
8974 static void
8975nv_put(cap)
8976 cmdarg_T *cap;
8977{
8978#ifdef FEAT_VISUAL
8979 int regname = 0;
8980 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008981 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008982 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#endif
8984 int dir;
8985 int flags = 0;
8986
8987 if (cap->oap->op_type != OP_NOP)
8988 {
8989#ifdef FEAT_DIFF
8990 /* "dp" is ":diffput" */
8991 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8992 {
8993 clearop(cap->oap);
8994 nv_diffgetput(TRUE);
8995 }
8996 else
8997#endif
8998 clearopbeep(cap->oap);
8999 }
9000 else
9001 {
9002 dir = (cap->cmdchar == 'P'
9003 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9004 ? BACKWARD : FORWARD;
9005 prep_redo_cmd(cap);
9006 if (cap->cmdchar == 'g')
9007 flags |= PUT_CURSEND;
9008
9009#ifdef FEAT_VISUAL
9010 if (VIsual_active)
9011 {
9012 /* Putting in Visual mode: The put text replaces the selected
9013 * text. First delete the selected text, then put the new text.
9014 * Need to save and restore the registers that the delete
9015 * overwrites if the old contents is being put.
9016 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009017 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 regname = cap->oap->regname;
9019# ifdef FEAT_CLIPBOARD
9020 adjust_clip_reg(&regname);
9021# endif
9022 if (regname == 0 || VIM_ISDIGIT(regname)
9023# ifdef FEAT_CLIPBOARD
9024 || (clip_unnamed && (regname == '*' || regname == '+'))
9025# endif
9026
9027 )
9028 {
9029 /* the delete is going to overwrite the register we want to
9030 * put, save it first. */
9031 reg1 = get_register(regname, TRUE);
9032 }
9033
9034 /* Now delete the selected text. */
9035 cap->cmdchar = 'd';
9036 cap->nchar = NUL;
9037 cap->oap->regname = NUL;
9038 nv_operator(cap);
9039 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009040 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
9042 /* delete PUT_LINE_BACKWARD; */
9043 cap->oap->regname = regname;
9044
9045 if (reg1 != NULL)
9046 {
9047 /* Delete probably changed the register we want to put, save
9048 * it first. Then put back what was there before the delete. */
9049 reg2 = get_register(regname, FALSE);
9050 put_register(regname, reg1);
9051 }
9052
9053 /* When deleted a linewise Visual area, put the register as
9054 * lines to avoid it joined with the next line. When deletion was
9055 * characterwise, split a line when putting lines. */
9056 if (VIsual_mode == 'V')
9057 flags |= PUT_LINE;
9058 else if (VIsual_mode == 'v')
9059 flags |= PUT_LINE_SPLIT;
9060 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9061 flags |= PUT_LINE_FORWARD;
9062 dir = BACKWARD;
9063 if ((VIsual_mode != 'V'
9064 && curwin->w_cursor.col < curbuf->b_op_start.col)
9065 || (VIsual_mode == 'V'
9066 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9067 /* cursor is at the end of the line or end of file, put
9068 * forward. */
9069 dir = FORWARD;
9070 }
9071#endif
9072 do_put(cap->oap->regname, dir, cap->count1, flags);
9073
9074#ifdef FEAT_VISUAL
9075 /* If a register was saved, put it back now. */
9076 if (reg2 != NULL)
9077 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009078
9079 /* What to reselect with "gv"? Selecting the just put text seems to
9080 * be the most useful, since the original text was removed. */
9081 if (was_visual)
9082 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009083 curbuf->b_visual.vi_start = curbuf->b_op_start;
9084 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009085 }
9086
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009087 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009088 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009089 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009090 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009091 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009092
9093 /* If the cursor was in that line, move it to the end of the last
9094 * line. */
9095 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9096 {
9097 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9098 coladvance((colnr_T)MAXCOL);
9099 }
9100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101#endif
9102 auto_format(FALSE, TRUE);
9103 }
9104}
9105
9106/*
9107 * "o" and "O" commands.
9108 */
9109 static void
9110nv_open(cap)
9111 cmdarg_T *cap;
9112{
9113#ifdef FEAT_DIFF
9114 /* "do" is ":diffget" */
9115 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9116 {
9117 clearop(cap->oap);
9118 nv_diffgetput(FALSE);
9119 }
9120 else
9121#endif
9122#ifdef FEAT_VISUAL
9123 if (VIsual_active) /* switch start and end of visual */
9124 v_swap_corners(cap->cmdchar);
9125 else
9126#endif
9127 n_opencmd(cap);
9128}
9129
9130#ifdef FEAT_SNIFF
9131/*ARGSUSED*/
9132 static void
9133nv_sniff(cap)
9134 cmdarg_T *cap;
9135{
9136 ProcessSniffRequests();
9137}
9138#endif
9139
9140#ifdef FEAT_NETBEANS_INTG
9141 static void
9142nv_nbcmd(cap)
9143 cmdarg_T *cap;
9144{
9145 netbeans_keycommand(cap->nchar);
9146}
9147#endif
9148
9149#ifdef FEAT_DND
9150/*ARGSUSED*/
9151 static void
9152nv_drop(cap)
9153 cmdarg_T *cap;
9154{
9155 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9156}
9157#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009158
9159#ifdef FEAT_AUTOCMD
9160/*
9161 * Trigger CursorHold event.
9162 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9163 * input buffer. "did_cursorhold" is set to avoid retriggering.
9164 */
9165/*ARGSUSED*/
9166 static void
9167nv_cursorhold(cap)
9168 cmdarg_T *cap;
9169{
9170 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9171 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009172 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009173}
9174#endif