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