blob: 16ab0c1a283fd27135188bd5bb3b0fe1ff660d6c [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 Moolenaar8b96d642005-09-05 22:05:30 +00004150 found_pos.lnum = 0;
4151 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
4515
4516 if (VIM_ISDIGIT(nchar))
4517 {
4518 /*
4519 * "z123{nchar}": edit the count before obtaining {nchar}
4520 */
4521 if (checkclearop(cap->oap))
4522 return;
4523 n = nchar - '0';
4524 for (;;)
4525 {
4526#ifdef USE_ON_FLY_SCROLL
4527 dont_scroll = TRUE; /* disallow scrolling here */
4528#endif
4529 ++no_mapping;
4530 ++allow_keys; /* no mapping for nchar, but allow key codes */
4531 nchar = safe_vgetc();
4532#ifdef FEAT_LANGMAP
4533 LANGMAP_ADJUST(nchar, TRUE);
4534#endif
4535 --no_mapping;
4536 --allow_keys;
4537#ifdef FEAT_CMDL_INFO
4538 (void)add_to_showcmd(nchar);
4539#endif
4540 if (nchar == K_DEL || nchar == K_KDEL)
4541 n /= 10;
4542 else if (VIM_ISDIGIT(nchar))
4543 n = n * 10 + (nchar - '0');
4544 else if (nchar == CAR)
4545 {
4546#ifdef FEAT_GUI
4547 need_mouse_correct = TRUE;
4548#endif
4549 win_setheight((int)n);
4550 break;
4551 }
4552 else if (nchar == 'l'
4553 || nchar == 'h'
4554 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004555 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 cap->count1 = n ? n * cap->count1 : cap->count1;
4558 goto dozet;
4559 }
4560 else
4561 {
4562 clearopbeep(cap->oap);
4563 break;
4564 }
4565 }
4566 cap->oap->op_type = OP_NOP;
4567 return;
4568 }
4569
4570dozet:
4571 if (
4572#ifdef FEAT_FOLDING
4573 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4574 * and "zC" only in Visual mode. "zj" and "zk" are motion
4575 * commands. */
4576 cap->nchar != 'f' && cap->nchar != 'F'
4577 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4578 && cap->nchar != 'j' && cap->nchar != 'k'
4579 &&
4580#endif
4581 checkclearop(cap->oap))
4582 return;
4583
4584 /*
4585 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4586 * If line number given, set cursor.
4587 */
4588 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4589 && cap->count0
4590 && cap->count0 != curwin->w_cursor.lnum)
4591 {
4592 setpcmark();
4593 if (cap->count0 > curbuf->b_ml.ml_line_count)
4594 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4595 else
4596 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004597 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 }
4599
4600 switch (nchar)
4601 {
4602 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4603 case '+':
4604 if (cap->count0 == 0)
4605 {
4606 /* No count given: put cursor at the line below screen */
4607 validate_botline(); /* make sure w_botline is valid */
4608 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4609 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4610 else
4611 curwin->w_cursor.lnum = curwin->w_botline;
4612 }
4613 /* FALLTHROUGH */
4614 case NL:
4615 case CAR:
4616 case K_KENTER:
4617 beginline(BL_WHITE | BL_FIX);
4618 /* FALLTHROUGH */
4619
4620 case 't': scroll_cursor_top(0, TRUE);
4621 redraw_later(VALID);
4622 break;
4623
4624 /* "z." and "zz": put cursor in middle of screen */
4625 case '.': beginline(BL_WHITE | BL_FIX);
4626 /* FALLTHROUGH */
4627
4628 case 'z': scroll_cursor_halfway(TRUE);
4629 redraw_later(VALID);
4630 break;
4631
4632 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4633 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4634 * when <count> is at bottom of window, and puts that one at
4635 * bottom of window. */
4636 if (cap->count0 != 0)
4637 {
4638 scroll_cursor_bot(0, TRUE);
4639 curwin->w_cursor.lnum = curwin->w_topline;
4640 }
4641 else if (curwin->w_topline == 1)
4642 curwin->w_cursor.lnum = 1;
4643 else
4644 curwin->w_cursor.lnum = curwin->w_topline - 1;
4645 /* FALLTHROUGH */
4646 case '-':
4647 beginline(BL_WHITE | BL_FIX);
4648 /* FALLTHROUGH */
4649
4650 case 'b': scroll_cursor_bot(0, TRUE);
4651 redraw_later(VALID);
4652 break;
4653
4654 /* "zH" - scroll screen right half-page */
4655 case 'H':
4656 cap->count1 *= W_WIDTH(curwin) / 2;
4657 /* FALLTHROUGH */
4658
4659 /* "zh" - scroll screen to the right */
4660 case 'h':
4661 case K_LEFT:
4662 if (!curwin->w_p_wrap)
4663 {
4664 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4665 curwin->w_leftcol = 0;
4666 else
4667 curwin->w_leftcol -= (colnr_T)cap->count1;
4668 leftcol_changed();
4669 }
4670 break;
4671
4672 /* "zL" - scroll screen left half-page */
4673 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4674 /* FALLTHROUGH */
4675
4676 /* "zl" - scroll screen to the left */
4677 case 'l':
4678 case K_RIGHT:
4679 if (!curwin->w_p_wrap)
4680 {
4681 /* scroll the window left */
4682 curwin->w_leftcol += (colnr_T)cap->count1;
4683 leftcol_changed();
4684 }
4685 break;
4686
4687 /* "zs" - scroll screen, cursor at the start */
4688 case 's': if (!curwin->w_p_wrap)
4689 {
4690#ifdef FEAT_FOLDING
4691 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4692 col = 0; /* like the cursor is in col 0 */
4693 else
4694#endif
4695 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4696 if ((long)col > p_siso)
4697 col -= p_siso;
4698 else
4699 col = 0;
4700 if (curwin->w_leftcol != col)
4701 {
4702 curwin->w_leftcol = col;
4703 redraw_later(NOT_VALID);
4704 }
4705 }
4706 break;
4707
4708 /* "ze" - scroll screen, cursor at the end */
4709 case 'e': if (!curwin->w_p_wrap)
4710 {
4711#ifdef FEAT_FOLDING
4712 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4713 col = 0; /* like the cursor is in col 0 */
4714 else
4715#endif
4716 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4717 n = W_WIDTH(curwin) - curwin_col_off();
4718 if ((long)col + p_siso < n)
4719 col = 0;
4720 else
4721 col = col + p_siso - n + 1;
4722 if (curwin->w_leftcol != col)
4723 {
4724 curwin->w_leftcol = col;
4725 redraw_later(NOT_VALID);
4726 }
4727 }
4728 break;
4729
4730#ifdef FEAT_FOLDING
4731 /* "zF": create fold command */
4732 /* "zf": create fold operator */
4733 case 'F':
4734 case 'f': if (foldManualAllowed(TRUE))
4735 {
4736 cap->nchar = 'f';
4737 nv_operator(cap);
4738 curwin->w_p_fen = TRUE;
4739
4740 /* "zF" is like "zfzf" */
4741 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4742 {
4743 nv_operator(cap);
4744 finish_op = TRUE;
4745 }
4746 }
4747 else
4748 clearopbeep(cap->oap);
4749 break;
4750
4751 /* "zd": delete fold at cursor */
4752 /* "zD": delete fold at cursor recursively */
4753 case 'd':
4754 case 'D': if (foldManualAllowed(FALSE))
4755 {
4756 if (VIsual_active)
4757 nv_operator(cap);
4758 else
4759 deleteFold(curwin->w_cursor.lnum,
4760 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4761 }
4762 break;
4763
4764 /* "zE": erease all folds */
4765 case 'E': if (foldmethodIsManual(curwin))
4766 {
4767 clearFolding(curwin);
4768 changed_window_setting();
4769 }
4770 else if (foldmethodIsMarker(curwin))
4771 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4772 TRUE, FALSE);
4773 else
4774 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4775 break;
4776
4777 /* "zn": fold none: reset 'foldenable' */
4778 case 'n': curwin->w_p_fen = FALSE;
4779 break;
4780
4781 /* "zN": fold Normal: set 'foldenable' */
4782 case 'N': curwin->w_p_fen = TRUE;
4783 break;
4784
4785 /* "zi": invert folding: toggle 'foldenable' */
4786 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4787 break;
4788
4789 /* "za": open closed fold or close open fold at cursor */
4790 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4791 openFold(curwin->w_cursor.lnum, cap->count1);
4792 else
4793 {
4794 closeFold(curwin->w_cursor.lnum, cap->count1);
4795 curwin->w_p_fen = TRUE;
4796 }
4797 break;
4798
4799 /* "zA": open fold at cursor recursively */
4800 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4801 openFoldRecurse(curwin->w_cursor.lnum);
4802 else
4803 {
4804 closeFoldRecurse(curwin->w_cursor.lnum);
4805 curwin->w_p_fen = TRUE;
4806 }
4807 break;
4808
4809 /* "zo": open fold at cursor or Visual area */
4810 case 'o': if (VIsual_active)
4811 nv_operator(cap);
4812 else
4813 openFold(curwin->w_cursor.lnum, cap->count1);
4814 break;
4815
4816 /* "zO": open fold recursively */
4817 case 'O': if (VIsual_active)
4818 nv_operator(cap);
4819 else
4820 openFoldRecurse(curwin->w_cursor.lnum);
4821 break;
4822
4823 /* "zc": close fold at cursor or Visual area */
4824 case 'c': if (VIsual_active)
4825 nv_operator(cap);
4826 else
4827 closeFold(curwin->w_cursor.lnum, cap->count1);
4828 curwin->w_p_fen = TRUE;
4829 break;
4830
4831 /* "zC": close fold recursively */
4832 case 'C': if (VIsual_active)
4833 nv_operator(cap);
4834 else
4835 closeFoldRecurse(curwin->w_cursor.lnum);
4836 curwin->w_p_fen = TRUE;
4837 break;
4838
4839 /* "zv": open folds at the cursor */
4840 case 'v': foldOpenCursor();
4841 break;
4842
4843 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4844 case 'x': curwin->w_p_fen = TRUE;
4845 newFoldLevel(); /* update right now */
4846 foldOpenCursor();
4847 break;
4848
4849 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4850 case 'X': curwin->w_p_fen = TRUE;
4851 old_fdl = -1; /* force an update */
4852 break;
4853
4854 /* "zm": fold more */
4855 case 'm': if (curwin->w_p_fdl > 0)
4856 --curwin->w_p_fdl;
4857 old_fdl = -1; /* force an update */
4858 curwin->w_p_fen = TRUE;
4859 break;
4860
4861 /* "zM": close all folds */
4862 case 'M': curwin->w_p_fdl = 0;
4863 old_fdl = -1; /* force an update */
4864 curwin->w_p_fen = TRUE;
4865 break;
4866
4867 /* "zr": reduce folding */
4868 case 'r': ++curwin->w_p_fdl;
4869 break;
4870
4871 /* "zR": open all folds */
4872 case 'R': curwin->w_p_fdl = getDeepestNesting();
4873 old_fdl = -1; /* force an update */
4874 break;
4875
4876 case 'j': /* "zj" move to next fold downwards */
4877 case 'k': /* "zk" move to next fold upwards */
4878 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4879 cap->count1) == FAIL)
4880 clearopbeep(cap->oap);
4881 break;
4882
4883#endif /* FEAT_FOLDING */
4884
Bram Moolenaarb765d632005-06-07 21:00:02 +00004885#ifdef FEAT_SYN_HL
4886 case 'g': /* "zg": add good word to word list */
4887 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00004888 case 'G': /* "zG": add good word to temp word list */
4889 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004890 {
4891 char_u *ptr = NULL;
4892 int len;
4893
4894 if (checkclearop(cap->oap))
4895 break;
4896# ifdef FEAT_VISUAL
4897 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4898 == FAIL)
4899 return;
4900# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004901 if (ptr == NULL)
4902 {
4903 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004904
4905 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004906 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004907 if (len != 0 && curwin->w_cursor.col <= pos.col)
4908 ptr = ml_get_pos(&curwin->w_cursor);
4909 curwin->w_cursor = pos;
4910 }
4911
Bram Moolenaarb765d632005-06-07 21:00:02 +00004912 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4913 FIND_IDENT)) == 0)
4914 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004915 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004916 (nchar == 'G' || nchar == 'W') ? 0
4917 : (int)cap->count1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004918 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004919 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920
Bram Moolenaar43abc522005-12-10 20:15:02 +00004921 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00004922 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004923 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004924 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004925#endif
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 default: clearopbeep(cap->oap);
4928 }
4929
4930#ifdef FEAT_FOLDING
4931 /* Redraw when 'foldenable' changed */
4932 if (old_fen != curwin->w_p_fen)
4933 {
4934# ifdef FEAT_DIFF
4935 win_T *wp;
4936
4937 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4938 {
4939 /* Adjust 'foldenable' in diff-synced windows. */
4940 FOR_ALL_WINDOWS(wp)
4941 {
4942 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4943 {
4944 wp->w_p_fen = curwin->w_p_fen;
4945 changed_window_setting_win(wp);
4946 }
4947 }
4948 }
4949# endif
4950 changed_window_setting();
4951 }
4952
4953 /* Redraw when 'foldlevel' changed. */
4954 if (old_fdl != curwin->w_p_fdl)
4955 newFoldLevel();
4956#endif
4957}
4958
4959#ifdef FEAT_GUI
4960/*
4961 * Vertical scrollbar movement.
4962 */
4963 static void
4964nv_ver_scrollbar(cap)
4965 cmdarg_T *cap;
4966{
4967 if (cap->oap->op_type != OP_NOP)
4968 clearopbeep(cap->oap);
4969
4970 /* Even if an operator was pending, we still want to scroll */
4971 gui_do_scroll();
4972}
4973
4974/*
4975 * Horizontal scrollbar movement.
4976 */
4977 static void
4978nv_hor_scrollbar(cap)
4979 cmdarg_T *cap;
4980{
4981 if (cap->oap->op_type != OP_NOP)
4982 clearopbeep(cap->oap);
4983
4984 /* Even if an operator was pending, we still want to scroll */
4985 gui_do_horiz_scroll();
4986}
4987#endif
4988
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004989#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004990/*
4991 * Click in GUI tab.
4992 */
4993 static void
4994nv_tabline(cap)
4995 cmdarg_T *cap;
4996{
4997 if (cap->oap->op_type != OP_NOP)
4998 clearopbeep(cap->oap);
4999
5000 /* Even if an operator was pending, we still want to jump tabs. */
5001 goto_tabpage(current_tab);
5002}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005003
5004/*
5005 * Selected item in tab line menu.
5006 */
5007 static void
5008nv_tabmenu(cap)
5009 cmdarg_T *cap;
5010{
5011 if (cap->oap->op_type != OP_NOP)
5012 clearopbeep(cap->oap);
5013
5014 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005015 handle_tabmenu();
5016}
5017
5018/*
5019 * Handle selecting an item of the GUI tab line menu.
5020 * Used in Normal and Insert mode.
5021 */
5022 void
5023handle_tabmenu()
5024{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005025 switch (current_tabmenu)
5026 {
5027 case TABLINE_MENU_CLOSE:
5028 if (current_tab == 0)
5029 do_cmdline_cmd((char_u *)"tabclose");
5030 else
5031 {
5032 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5033 current_tab);
5034 do_cmdline_cmd(IObuff);
5035 }
5036 break;
5037
5038 case TABLINE_MENU_NEW:
5039 if (current_tab > 0)
5040 goto_tabpage(current_tab);
5041 do_cmdline_cmd((char_u *)"tabnew");
5042 break;
5043
5044 case TABLINE_MENU_OPEN:
5045 if (current_tab > 0)
5046 goto_tabpage(current_tab);
5047 do_cmdline_cmd((char_u *)"browse tabnew");
5048 break;
5049 }
5050}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005051#endif
5052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053/*
5054 * "Q" command.
5055 */
5056 static void
5057nv_exmode(cap)
5058 cmdarg_T *cap;
5059{
5060 /*
5061 * Ignore 'Q' in Visual mode, just give a beep.
5062 */
5063#ifdef FEAT_VISUAL
5064 if (VIsual_active)
5065 vim_beep();
5066 else
5067#endif
5068 if (!checkclearop(cap->oap))
5069 do_exmode(FALSE);
5070}
5071
5072/*
5073 * Handle a ":" command.
5074 */
5075 static void
5076nv_colon(cap)
5077 cmdarg_T *cap;
5078{
5079 int old_p_im;
5080
5081#ifdef FEAT_VISUAL
5082 if (VIsual_active)
5083 nv_operator(cap);
5084 else
5085#endif
5086 {
5087 if (cap->oap->op_type != OP_NOP)
5088 {
5089 /* Using ":" as a movement is characterwise exclusive. */
5090 cap->oap->motion_type = MCHAR;
5091 cap->oap->inclusive = FALSE;
5092 }
5093 else if (cap->count0)
5094 {
5095 /* translate "count:" into ":.,.+(count - 1)" */
5096 stuffcharReadbuff('.');
5097 if (cap->count0 > 1)
5098 {
5099 stuffReadbuff((char_u *)",.+");
5100 stuffnumReadbuff((long)cap->count0 - 1L);
5101 }
5102 }
5103
5104 /* When typing, don't type below an old message */
5105 if (KeyTyped)
5106 compute_cmdrow();
5107
5108 old_p_im = p_im;
5109
5110 /* get a command line and execute it */
5111 do_cmdline(NULL, getexline, NULL,
5112 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5113
5114 /* If 'insertmode' changed, enter or exit Insert mode */
5115 if (p_im != old_p_im)
5116 {
5117 if (p_im)
5118 restart_edit = 'i';
5119 else
5120 restart_edit = 0;
5121 }
5122
5123 /* The start of the operator may have become invalid by the Ex
5124 * command. */
5125 if (cap->oap->op_type != OP_NOP
5126 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5127 || cap->oap->start.col >
5128 STRLEN(ml_get(cap->oap->start.lnum))))
5129 clearopbeep(cap->oap);
5130 }
5131}
5132
5133/*
5134 * Handle CTRL-G command.
5135 */
5136 static void
5137nv_ctrlg(cap)
5138 cmdarg_T *cap;
5139{
5140#ifdef FEAT_VISUAL
5141 if (VIsual_active) /* toggle Selection/Visual mode */
5142 {
5143 VIsual_select = !VIsual_select;
5144 showmode();
5145 }
5146 else
5147#endif
5148 if (!checkclearop(cap->oap))
5149 /* print full name if count given or :cd used */
5150 fileinfo((int)cap->count0, FALSE, TRUE);
5151}
5152
5153/*
5154 * Handle CTRL-H <Backspace> command.
5155 */
5156 static void
5157nv_ctrlh(cap)
5158 cmdarg_T *cap;
5159{
5160#ifdef FEAT_VISUAL
5161 if (VIsual_active && VIsual_select)
5162 {
5163 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5164 v_visop(cap);
5165 }
5166 else
5167#endif
5168 nv_left(cap);
5169}
5170
5171/*
5172 * CTRL-L: clear screen and redraw.
5173 */
5174 static void
5175nv_clear(cap)
5176 cmdarg_T *cap;
5177{
5178 if (!checkclearop(cap->oap))
5179 {
5180#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5181 /*
5182 * Right now, the BeBox doesn't seem to have an easy way to detect
5183 * window resizing, so we cheat and make the user detect it
5184 * manually with CTRL-L instead
5185 */
5186 ui_get_shellsize();
5187#endif
5188#ifdef FEAT_SYN_HL
5189 /* Clear all syntax states to force resyncing. */
5190 syn_stack_free_all(curbuf);
5191#endif
5192 redraw_later(CLEAR);
5193 }
5194}
5195
5196/*
5197 * CTRL-O: In Select mode: switch to Visual mode for one command.
5198 * Otherwise: Go to older pcmark.
5199 */
5200 static void
5201nv_ctrlo(cap)
5202 cmdarg_T *cap;
5203{
5204#ifdef FEAT_VISUAL
5205 if (VIsual_active && VIsual_select)
5206 {
5207 VIsual_select = FALSE;
5208 showmode();
5209 restart_VIsual_select = 2; /* restart Select mode later */
5210 }
5211 else
5212#endif
5213 {
5214 cap->count1 = -cap->count1;
5215 nv_pcmark(cap);
5216 }
5217}
5218
5219/*
5220 * CTRL-^ command, short for ":e #"
5221 */
5222 static void
5223nv_hat(cap)
5224 cmdarg_T *cap;
5225{
5226 if (!checkclearopq(cap->oap))
5227 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5228 GETF_SETMARK|GETF_ALT, FALSE);
5229}
5230
5231/*
5232 * "Z" commands.
5233 */
5234 static void
5235nv_Zet(cap)
5236 cmdarg_T *cap;
5237{
5238 if (!checkclearopq(cap->oap))
5239 {
5240 switch (cap->nchar)
5241 {
5242 /* "ZZ": equivalent to ":x". */
5243 case 'Z': do_cmdline_cmd((char_u *)"x");
5244 break;
5245
5246 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5247 case 'Q': do_cmdline_cmd((char_u *)"q!");
5248 break;
5249
5250 default: clearopbeep(cap->oap);
5251 }
5252 }
5253}
5254
5255#if defined(FEAT_WINDOWS) || defined(PROTO)
5256/*
5257 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5258 */
5259 void
5260do_nv_ident(c1, c2)
5261 int c1;
5262 int c2;
5263{
5264 oparg_T oa;
5265 cmdarg_T ca;
5266
5267 clear_oparg(&oa);
5268 vim_memset(&ca, 0, sizeof(ca));
5269 ca.oap = &oa;
5270 ca.cmdchar = c1;
5271 ca.nchar = c2;
5272 nv_ident(&ca);
5273}
5274#endif
5275
5276/*
5277 * Handle the commands that use the word under the cursor.
5278 * [g] CTRL-] :ta to current identifier
5279 * [g] 'K' run program for current identifier
5280 * [g] '*' / to current identifier or string
5281 * [g] '#' ? to current identifier or string
5282 * g ']' :tselect for current identifier
5283 */
5284 static void
5285nv_ident(cap)
5286 cmdarg_T *cap;
5287{
5288 char_u *ptr = NULL;
5289 char_u *buf;
5290 char_u *p;
5291 char_u *kp; /* value of 'keywordprg' */
5292 int kp_help; /* 'keywordprg' is ":help" */
5293 int n = 0; /* init for GCC */
5294 int cmdchar;
5295 int g_cmd; /* "g" command */
5296 char_u *aux_ptr;
5297 int isman;
5298 int isman_s;
5299
5300 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5301 {
5302 cmdchar = cap->nchar;
5303 g_cmd = TRUE;
5304 }
5305 else
5306 {
5307 cmdchar = cap->cmdchar;
5308 g_cmd = FALSE;
5309 }
5310
5311 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5312 cmdchar = '#';
5313
5314 /*
5315 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5316 */
5317 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5318 {
5319#ifdef FEAT_VISUAL
5320 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5321 return;
5322#endif
5323 if (checkclearopq(cap->oap))
5324 return;
5325 }
5326
5327 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5328 (cmdchar == '*' || cmdchar == '#')
5329 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5330 {
5331 clearop(cap->oap);
5332 return;
5333 }
5334
5335 /* Allocate buffer to put the command in. Inserting backslashes can
5336 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5337 * and some numbers. */
5338 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5339 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5340 || STRCMP(kp, ":help") == 0);
5341 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5342 if (buf == NULL)
5343 return;
5344 buf[0] = NUL;
5345
5346 switch (cmdchar)
5347 {
5348 case '*':
5349 case '#':
5350 /*
5351 * Put cursor at start of word, makes search skip the word
5352 * under the cursor.
5353 * Call setpcmark() first, so "*``" puts the cursor back where
5354 * it was.
5355 */
5356 setpcmark();
5357 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5358
5359 if (!g_cmd && vim_iswordp(ptr))
5360 STRCPY(buf, "\\<");
5361 no_smartcase = TRUE; /* don't use 'smartcase' now */
5362 break;
5363
5364 case 'K':
5365 if (kp_help)
5366 STRCPY(buf, "he! ");
5367 else
5368 {
5369 /* When a count is given, turn it into a range. Is this
5370 * really what we want? */
5371 isman = (STRCMP(kp, "man") == 0);
5372 isman_s = (STRCMP(kp, "man -s") == 0);
5373 if (cap->count0 != 0 && !(isman || isman_s))
5374 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5375
5376 STRCAT(buf, "! ");
5377 if (cap->count0 == 0 && isman_s)
5378 STRCAT(buf, "man");
5379 else
5380 STRCAT(buf, kp);
5381 STRCAT(buf, " ");
5382 if (cap->count0 != 0 && (isman || isman_s))
5383 {
5384 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5385 STRCAT(buf, " ");
5386 }
5387 }
5388 break;
5389
5390 case ']':
5391#ifdef FEAT_CSCOPE
5392 if (p_cst)
5393 STRCPY(buf, "cstag ");
5394 else
5395#endif
5396 STRCPY(buf, "ts ");
5397 break;
5398
5399 default:
5400 if (curbuf->b_help)
5401 STRCPY(buf, "he! ");
5402 else if (g_cmd)
5403 STRCPY(buf, "tj ");
5404 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005405 sprintf((char *)buf, "%ldta ", cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407
5408 /*
5409 * Now grab the chars in the identifier
5410 */
5411 if (cmdchar == '*')
5412 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5413 else if (cmdchar == '#')
5414 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5415 else if (cmdchar == 'K' && !kp_help)
5416 aux_ptr = (char_u *)" \t\\\"|!";
5417 else
5418 /* Don't escape spaces and Tabs in a tag with a backslash */
5419 aux_ptr = (char_u *)"\\|\"";
5420
5421 p = buf + STRLEN(buf);
5422 while (n-- > 0)
5423 {
5424 /* put a backslash before \ and some others */
5425 if (vim_strchr(aux_ptr, *ptr) != NULL)
5426 *p++ = '\\';
5427#ifdef FEAT_MBYTE
5428 /* When current byte is a part of multibyte character, copy all bytes
5429 * of that character. */
5430 if (has_mbyte)
5431 {
5432 int i;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005433 int len = (*mb_ptr2len)(ptr) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 for (i = 0; i < len && n >= 1; ++i, --n)
5436 *p++ = *ptr++;
5437 }
5438#endif
5439 *p++ = *ptr++;
5440 }
5441 *p = NUL;
5442
5443 /*
5444 * Execute the command.
5445 */
5446 if (cmdchar == '*' || cmdchar == '#')
5447 {
5448 if (!g_cmd && (
5449#ifdef FEAT_MBYTE
5450 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5451#endif
5452 vim_iswordc(ptr[-1])))
5453 STRCAT(buf, "\\>");
5454#ifdef FEAT_CMDHIST
5455 /* put pattern in search history */
5456 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5457#endif
5458 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5459 }
5460 else
5461 do_cmdline_cmd(buf);
5462
5463 vim_free(buf);
5464}
5465
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005466#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467/*
5468 * Get visually selected text, within one line only.
5469 * Returns FAIL if more than one line selected.
5470 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005471 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472get_visual_text(cap, pp, lenp)
5473 cmdarg_T *cap;
5474 char_u **pp; /* return: start of selected text */
5475 int *lenp; /* return: length of selected text */
5476{
5477 if (VIsual_mode != 'V')
5478 unadjust_for_sel();
5479 if (VIsual.lnum != curwin->w_cursor.lnum)
5480 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005481 if (cap != NULL)
5482 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 return FAIL;
5484 }
5485 if (VIsual_mode == 'V')
5486 {
5487 *pp = ml_get_curline();
5488 *lenp = (int)STRLEN(*pp);
5489 }
5490 else
5491 {
5492 if (lt(curwin->w_cursor, VIsual))
5493 {
5494 *pp = ml_get_pos(&curwin->w_cursor);
5495 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5496 }
5497 else
5498 {
5499 *pp = ml_get_pos(&VIsual);
5500 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5501 }
5502#ifdef FEAT_MBYTE
5503 if (has_mbyte)
5504 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005505 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506#endif
5507 }
5508 reset_VIsual_and_resel();
5509 return OK;
5510}
5511#endif
5512
5513/*
5514 * CTRL-T: backwards in tag stack
5515 */
5516 static void
5517nv_tagpop(cap)
5518 cmdarg_T *cap;
5519{
5520 if (!checkclearopq(cap->oap))
5521 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5522}
5523
5524/*
5525 * Handle scrolling command 'H', 'L' and 'M'.
5526 */
5527 static void
5528nv_scroll(cap)
5529 cmdarg_T *cap;
5530{
5531 int used = 0;
5532 long n;
5533#ifdef FEAT_FOLDING
5534 linenr_T lnum;
5535#endif
5536 int half;
5537
5538 cap->oap->motion_type = MLINE;
5539 setpcmark();
5540
5541 if (cap->cmdchar == 'L')
5542 {
5543 validate_botline(); /* make sure curwin->w_botline is valid */
5544 curwin->w_cursor.lnum = curwin->w_botline - 1;
5545 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5546 curwin->w_cursor.lnum = 1;
5547 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005548 {
5549#ifdef FEAT_FOLDING
5550 if (hasAnyFolding(curwin))
5551 {
5552 /* Count a fold for one screen line. */
5553 for (n = cap->count1 - 1; n > 0
5554 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5555 {
5556 (void)hasFolding(curwin->w_cursor.lnum,
5557 &curwin->w_cursor.lnum, NULL);
5558 --curwin->w_cursor.lnum;
5559 }
5560 }
5561 else
5562#endif
5563 curwin->w_cursor.lnum -= cap->count1 - 1;
5564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 }
5566 else
5567 {
5568 if (cap->cmdchar == 'M')
5569 {
5570#ifdef FEAT_DIFF
5571 /* Don't count filler lines above the window. */
5572 used -= diff_check_fill(curwin, curwin->w_topline)
5573 - curwin->w_topfill;
5574#endif
5575 validate_botline(); /* make sure w_empty_rows is valid */
5576 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5577 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5578 {
5579#ifdef FEAT_DIFF
5580 /* Count half he number of filler lines to be "below this
5581 * line" and half to be "above the next line". */
5582 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5583 + n) / 2 >= half)
5584 {
5585 --n;
5586 break;
5587 }
5588#endif
5589 used += plines(curwin->w_topline + n);
5590 if (used >= half)
5591 break;
5592#ifdef FEAT_FOLDING
5593 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5594 n = lnum - curwin->w_topline;
5595#endif
5596 }
5597 if (n > 0 && used > curwin->w_height)
5598 --n;
5599 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005600 else /* (cap->cmdchar == 'H') */
5601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005603#ifdef FEAT_FOLDING
5604 if (hasAnyFolding(curwin))
5605 {
5606 /* Count a fold for one screen line. */
5607 lnum = curwin->w_topline;
5608 while (n-- > 0 && lnum < curwin->w_botline - 1)
5609 {
5610 hasFolding(lnum, NULL, &lnum);
5611 ++lnum;
5612 }
5613 n = lnum - curwin->w_topline;
5614 }
5615#endif
5616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 curwin->w_cursor.lnum = curwin->w_topline + n;
5618 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5619 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5620 }
5621
5622 cursor_correct(); /* correct for 'so' */
5623 beginline(BL_SOL | BL_FIX);
5624}
5625
5626/*
5627 * Cursor right commands.
5628 */
5629 static void
5630nv_right(cap)
5631 cmdarg_T *cap;
5632{
5633 long n;
5634#ifdef FEAT_VISUAL
5635 int PAST_LINE;
5636#else
5637# define PAST_LINE 0
5638#endif
5639
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005640 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5641 {
5642 /* <C-Right> and <S-Right> move a word or WORD right */
5643 if (mod_mask & MOD_MASK_CTRL)
5644 cap->arg = TRUE;
5645 nv_wordcmd(cap);
5646 return;
5647 }
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 cap->oap->motion_type = MCHAR;
5650 cap->oap->inclusive = FALSE;
5651#ifdef FEAT_VISUAL
5652 PAST_LINE = (VIsual_active && *p_sel != 'o');
5653
5654# ifdef FEAT_VIRTUALEDIT
5655 /*
5656 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5657 * (theoretically) infinitly long.
5658 */
5659 if (virtual_active())
5660 PAST_LINE = 0;
5661# endif
5662#endif
5663
5664 for (n = cap->count1; n > 0; --n)
5665 {
5666 if ((!PAST_LINE && oneright() == FAIL)
5667 || (PAST_LINE && *ml_get_cursor() == NUL))
5668 {
5669 /*
5670 * <Space> wraps to next line if 'whichwrap' bit 1 set.
5671 * 'l' wraps to next line if 'whichwrap' bit 2 set.
5672 * CURS_RIGHT wraps to next line if 'whichwrap' bit 3 set
5673 */
5674 if ( ((cap->cmdchar == ' '
5675 && vim_strchr(p_ww, 's') != NULL)
5676 || (cap->cmdchar == 'l'
5677 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005678 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 && vim_strchr(p_ww, '>') != NULL))
5680 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5681 {
5682 /* When deleting we also count the NL as a character.
5683 * Set cap->oap->inclusive when last char in the line is
5684 * included, move to next line after that */
5685 if ( (cap->oap->op_type == OP_DELETE
5686 || cap->oap->op_type == OP_CHANGE)
5687 && !cap->oap->inclusive
5688 && !lineempty(curwin->w_cursor.lnum))
5689 cap->oap->inclusive = TRUE;
5690 else
5691 {
5692 ++curwin->w_cursor.lnum;
5693 curwin->w_cursor.col = 0;
5694#ifdef FEAT_VIRTUALEDIT
5695 curwin->w_cursor.coladd = 0;
5696#endif
5697 curwin->w_set_curswant = TRUE;
5698 cap->oap->inclusive = FALSE;
5699 }
5700 continue;
5701 }
5702 if (cap->oap->op_type == OP_NOP)
5703 {
5704 /* Only beep and flush if not moved at all */
5705 if (n == cap->count1)
5706 beep_flush();
5707 }
5708 else
5709 {
5710 if (!lineempty(curwin->w_cursor.lnum))
5711 cap->oap->inclusive = TRUE;
5712 }
5713 break;
5714 }
5715#ifdef FEAT_VISUAL
5716 else if (PAST_LINE)
5717 {
5718 curwin->w_set_curswant = TRUE;
5719# ifdef FEAT_VIRTUALEDIT
5720 if (virtual_active())
5721 oneright();
5722 else
5723# endif
5724 {
5725# ifdef FEAT_MBYTE
5726 if (has_mbyte)
5727 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005728 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 else
5730# endif
5731 ++curwin->w_cursor.col;
5732 }
5733 }
5734#endif
5735 }
5736#ifdef FEAT_FOLDING
5737 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5738 && cap->oap->op_type == OP_NOP)
5739 foldOpenCursor();
5740#endif
5741}
5742
5743/*
5744 * Cursor left commands.
5745 *
5746 * Returns TRUE when operator end should not be adjusted.
5747 */
5748 static void
5749nv_left(cap)
5750 cmdarg_T *cap;
5751{
5752 long n;
5753
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005754 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5755 {
5756 /* <C-Left> and <S-Left> move a word or WORD left */
5757 if (mod_mask & MOD_MASK_CTRL)
5758 cap->arg = 1;
5759 nv_bck_word(cap);
5760 return;
5761 }
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 cap->oap->motion_type = MCHAR;
5764 cap->oap->inclusive = FALSE;
5765 for (n = cap->count1; n > 0; --n)
5766 {
5767 if (oneleft() == FAIL)
5768 {
5769 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5770 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5771 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5772 */
5773 if ( (((cap->cmdchar == K_BS
5774 || cap->cmdchar == Ctrl_H)
5775 && vim_strchr(p_ww, 'b') != NULL)
5776 || (cap->cmdchar == 'h'
5777 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005778 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 && vim_strchr(p_ww, '<') != NULL))
5780 && curwin->w_cursor.lnum > 1)
5781 {
5782 --(curwin->w_cursor.lnum);
5783 coladvance((colnr_T)MAXCOL);
5784 curwin->w_set_curswant = TRUE;
5785
5786 /* When the NL before the first char has to be deleted we
5787 * put the cursor on the NUL after the previous line.
5788 * This is a very special case, be careful!
5789 * don't adjust op_end now, otherwise it won't work */
5790 if ( (cap->oap->op_type == OP_DELETE
5791 || cap->oap->op_type == OP_CHANGE)
5792 && !lineempty(curwin->w_cursor.lnum))
5793 {
5794 ++curwin->w_cursor.col;
5795 cap->retval |= CA_NO_ADJ_OP_END;
5796 }
5797 continue;
5798 }
5799 /* Only beep and flush if not moved at all */
5800 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5801 beep_flush();
5802 break;
5803 }
5804 }
5805#ifdef FEAT_FOLDING
5806 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5807 && cap->oap->op_type == OP_NOP)
5808 foldOpenCursor();
5809#endif
5810}
5811
5812/*
5813 * Cursor up commands.
5814 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5815 */
5816 static void
5817nv_up(cap)
5818 cmdarg_T *cap;
5819{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005820 if (mod_mask & MOD_MASK_SHIFT)
5821 {
5822 /* <S-Up> is page up */
5823 cap->arg = BACKWARD;
5824 nv_page(cap);
5825 }
5826 else
5827 {
5828 cap->oap->motion_type = MLINE;
5829 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5830 clearopbeep(cap->oap);
5831 else if (cap->arg)
5832 beginline(BL_WHITE | BL_FIX);
5833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834}
5835
5836/*
5837 * Cursor down commands.
5838 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5839 */
5840 static void
5841nv_down(cap)
5842 cmdarg_T *cap;
5843{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005844 if (mod_mask & MOD_MASK_SHIFT)
5845 {
5846 /* <S-Down> is page down */
5847 cap->arg = FORWARD;
5848 nv_page(cap);
5849 }
5850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5852 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005853 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005854 if (curwin->w_llist_ref == NULL)
5855 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
5856 else
5857 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 else
5859#endif
5860 {
5861#ifdef FEAT_CMDWIN
5862 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005863 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 cmdwin_result = CAR;
5865 else
5866#endif
5867 {
5868 cap->oap->motion_type = MLINE;
5869 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5870 clearopbeep(cap->oap);
5871 else if (cap->arg)
5872 beginline(BL_WHITE | BL_FIX);
5873 }
5874 }
5875}
5876
5877#ifdef FEAT_SEARCHPATH
5878/*
5879 * Grab the file name under the cursor and edit it.
5880 */
5881 static void
5882nv_gotofile(cap)
5883 cmdarg_T *cap;
5884{
5885 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005886 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005888 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
5890 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005891 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 return;
5893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005895 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 if (ptr != NULL)
5898 {
5899 /* do autowrite if necessary */
5900 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5901 autowrite(curbuf, FALSE);
5902 setpcmark();
5903 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5904 P_HID(curbuf) ? ECMD_HIDE : 0);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005905 if (cap->nchar == 'F' && lnum >= 0)
5906 {
5907 curwin->w_cursor.lnum = lnum;
5908 check_cursor_lnum();
5909 beginline(BL_SOL | BL_FIX);
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 vim_free(ptr);
5912 }
5913 else
5914 clearop(cap->oap);
5915}
5916#endif
5917
5918/*
5919 * <End> command: to end of current line or last line.
5920 */
5921 static void
5922nv_end(cap)
5923 cmdarg_T *cap;
5924{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005925 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005927 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 nv_goto(cap);
5929 cap->count1 = 1; /* to end of current line */
5930 }
5931 nv_dollar(cap);
5932}
5933
5934/*
5935 * Handle the "$" command.
5936 */
5937 static void
5938nv_dollar(cap)
5939 cmdarg_T *cap;
5940{
5941 cap->oap->motion_type = MCHAR;
5942 cap->oap->inclusive = TRUE;
5943#ifdef FEAT_VIRTUALEDIT
5944 /* In virtual mode when off the edge of a line and an operator
5945 * is pending (whew!) keep the cursor where it is.
5946 * Otherwise, send it to the end of the line. */
5947 if (!virtual_active() || gchar_cursor() != NUL
5948 || cap->oap->op_type == OP_NOP)
5949#endif
5950 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5951 if (cursor_down((long)(cap->count1 - 1),
5952 cap->oap->op_type == OP_NOP) == FAIL)
5953 clearopbeep(cap->oap);
5954#ifdef FEAT_FOLDING
5955 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5956 foldOpenCursor();
5957#endif
5958}
5959
5960/*
5961 * Implementation of '?' and '/' commands.
5962 * If cap->arg is TRUE don't set PC mark.
5963 */
5964 static void
5965nv_search(cap)
5966 cmdarg_T *cap;
5967{
5968 oparg_T *oap = cap->oap;
5969
5970 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5971 {
5972 /* Translate "g??" to "g?g?" */
5973 cap->cmdchar = 'g';
5974 cap->nchar = '?';
5975 nv_operator(cap);
5976 return;
5977 }
5978
5979 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5980
5981 if (cap->searchbuf == NULL)
5982 {
5983 clearop(oap);
5984 return;
5985 }
5986
5987 normal_search(cap, cap->cmdchar, cap->searchbuf,
5988 (cap->arg ? 0 : SEARCH_MARK));
5989}
5990
5991/*
5992 * Handle "N" and "n" commands.
5993 * cap->arg is SEARCH_REV for "N", 0 for "n".
5994 */
5995 static void
5996nv_next(cap)
5997 cmdarg_T *cap;
5998{
5999 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6000}
6001
6002/*
6003 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6004 * Uses only cap->count1 and cap->oap from "cap".
6005 */
6006 static void
6007normal_search(cap, dir, pat, opt)
6008 cmdarg_T *cap;
6009 int dir;
6010 char_u *pat;
6011 int opt; /* extra flags for do_search() */
6012{
6013 int i;
6014
6015 cap->oap->motion_type = MCHAR;
6016 cap->oap->inclusive = FALSE;
6017 cap->oap->use_reg_one = TRUE;
6018 curwin->w_set_curswant = TRUE;
6019
6020 i = do_search(cap->oap, dir, pat, cap->count1,
6021 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
6022 if (i == 0)
6023 clearop(cap->oap);
6024 else
6025 {
6026 if (i == 2)
6027 cap->oap->motion_type = MLINE;
6028#ifdef FEAT_VIRTUALEDIT
6029 curwin->w_cursor.coladd = 0;
6030#endif
6031#ifdef FEAT_FOLDING
6032 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6033 foldOpenCursor();
6034#endif
6035 }
6036
6037 /* "/$" will put the cursor after the end of the line, may need to
6038 * correct that here */
6039 check_cursor();
6040}
6041
6042/*
6043 * Character search commands.
6044 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6045 * ',' and FALSE for ';'.
6046 * cap->nchar is NUL for ',' and ';' (repeat the search)
6047 */
6048 static void
6049nv_csearch(cap)
6050 cmdarg_T *cap;
6051{
6052 int t_cmd;
6053
6054 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6055 t_cmd = TRUE;
6056 else
6057 t_cmd = FALSE;
6058
6059 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6061 clearopbeep(cap->oap);
6062 else
6063 {
6064 curwin->w_set_curswant = TRUE;
6065#ifdef FEAT_VIRTUALEDIT
6066 /* Include a Tab for "tx" and for "dfx". */
6067 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6068 && (t_cmd || cap->oap->op_type != OP_NOP))
6069 {
6070 colnr_T scol, ecol;
6071
6072 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6073 curwin->w_cursor.coladd = ecol - scol;
6074 }
6075 else
6076 curwin->w_cursor.coladd = 0;
6077#endif
6078#ifdef FEAT_VISUAL
6079 adjust_for_sel(cap);
6080#endif
6081#ifdef FEAT_FOLDING
6082 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6083 foldOpenCursor();
6084#endif
6085 }
6086}
6087
6088/*
6089 * "[" and "]" commands.
6090 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6091 */
6092 static void
6093nv_brackets(cap)
6094 cmdarg_T *cap;
6095{
6096 pos_T new_pos;
6097 pos_T prev_pos;
6098 pos_T *pos = NULL; /* init for GCC */
6099 pos_T old_pos; /* cursor position before command */
6100 int flag;
6101 long n;
6102 int findc;
6103 int c;
6104
6105 cap->oap->motion_type = MCHAR;
6106 cap->oap->inclusive = FALSE;
6107 old_pos = curwin->w_cursor;
6108#ifdef FEAT_VIRTUALEDIT
6109 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6110#endif
6111
6112#ifdef FEAT_SEARCHPATH
6113 /*
6114 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6115 */
6116 if (cap->nchar == 'f')
6117 nv_gotofile(cap);
6118 else
6119#endif
6120
6121#ifdef FEAT_FIND_ID
6122 /*
6123 * Find the occurence(s) of the identifier or define under cursor
6124 * in current and included files or jump to the first occurence.
6125 *
6126 * search list jump
6127 * fwd bwd fwd bwd fwd bwd
6128 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6129 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6130 */
6131 if (vim_strchr((char_u *)
6132#ifdef EBCDIC
6133 "iI\005dD\067",
6134#else
6135 "iI\011dD\004",
6136#endif
6137 cap->nchar) != NULL)
6138 {
6139 char_u *ptr;
6140 int len;
6141
6142 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6143 clearop(cap->oap);
6144 else
6145 {
6146 find_pattern_in_path(ptr, 0, len, TRUE,
6147 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6148 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6149 cap->count1,
6150 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6151 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6152 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6153 (linenr_T)MAXLNUM);
6154 curwin->w_set_curswant = TRUE;
6155 }
6156 }
6157 else
6158#endif
6159
6160 /*
6161 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6162 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6163 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6164 * "[m" or "]m" search for prev/next start of (Java) method.
6165 * "[M" or "]M" search for prev/next end of (Java) method.
6166 */
6167 if ( (cap->cmdchar == '['
6168 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6169 || (cap->cmdchar == ']'
6170 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6171 {
6172 if (cap->nchar == '*')
6173 cap->nchar = '/';
6174 new_pos.lnum = 0;
6175 prev_pos.lnum = 0;
6176 if (cap->nchar == 'm' || cap->nchar == 'M')
6177 {
6178 if (cap->cmdchar == '[')
6179 findc = '{';
6180 else
6181 findc = '}';
6182 n = 9999;
6183 }
6184 else
6185 {
6186 findc = cap->nchar;
6187 n = cap->count1;
6188 }
6189 for ( ; n > 0; --n)
6190 {
6191 if ((pos = findmatchlimit(cap->oap, findc,
6192 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6193 {
6194 if (new_pos.lnum == 0) /* nothing found */
6195 {
6196 if (cap->nchar != 'm' && cap->nchar != 'M')
6197 clearopbeep(cap->oap);
6198 }
6199 else
6200 pos = &new_pos; /* use last one found */
6201 break;
6202 }
6203 prev_pos = new_pos;
6204 curwin->w_cursor = *pos;
6205 new_pos = *pos;
6206 }
6207 curwin->w_cursor = old_pos;
6208
6209 /*
6210 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6211 * brought us to the match for "[m" and "]M" when inside a method.
6212 * Try finding the '{' or '}' we want to be at.
6213 * Also repeat for the given count.
6214 */
6215 if (cap->nchar == 'm' || cap->nchar == 'M')
6216 {
6217 /* norm is TRUE for "]M" and "[m" */
6218 int norm = ((findc == '{') == (cap->nchar == 'm'));
6219
6220 n = cap->count1;
6221 /* found a match: we were inside a method */
6222 if (prev_pos.lnum != 0)
6223 {
6224 pos = &prev_pos;
6225 curwin->w_cursor = prev_pos;
6226 if (norm)
6227 --n;
6228 }
6229 else
6230 pos = NULL;
6231 while (n > 0)
6232 {
6233 for (;;)
6234 {
6235 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6236 {
6237 /* if not found anything, that's an error */
6238 if (pos == NULL)
6239 clearopbeep(cap->oap);
6240 n = 0;
6241 break;
6242 }
6243 c = gchar_cursor();
6244 if (c == '{' || c == '}')
6245 {
6246 /* Must have found end/start of class: use it.
6247 * Or found the place to be at. */
6248 if ((c == findc && norm) || (n == 1 && !norm))
6249 {
6250 new_pos = curwin->w_cursor;
6251 pos = &new_pos;
6252 n = 0;
6253 }
6254 /* if no match found at all, we started outside of the
6255 * class and we're inside now. Just go on. */
6256 else if (new_pos.lnum == 0)
6257 {
6258 new_pos = curwin->w_cursor;
6259 pos = &new_pos;
6260 }
6261 /* found start/end of other method: go to match */
6262 else if ((pos = findmatchlimit(cap->oap, findc,
6263 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6264 0)) == NULL)
6265 n = 0;
6266 else
6267 curwin->w_cursor = *pos;
6268 break;
6269 }
6270 }
6271 --n;
6272 }
6273 curwin->w_cursor = old_pos;
6274 if (pos == NULL && new_pos.lnum != 0)
6275 clearopbeep(cap->oap);
6276 }
6277 if (pos != NULL)
6278 {
6279 setpcmark();
6280 curwin->w_cursor = *pos;
6281 curwin->w_set_curswant = TRUE;
6282#ifdef FEAT_FOLDING
6283 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6284 && cap->oap->op_type == OP_NOP)
6285 foldOpenCursor();
6286#endif
6287 }
6288 }
6289
6290 /*
6291 * "[[", "[]", "]]" and "][": move to start or end of function
6292 */
6293 else if (cap->nchar == '[' || cap->nchar == ']')
6294 {
6295 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6296 flag = '{';
6297 else
6298 flag = '}'; /* "][" or "[]" */
6299
6300 curwin->w_set_curswant = TRUE;
6301 /*
6302 * Imitate strange Vi behaviour: When using "]]" with an operator
6303 * we also stop at '}'.
6304 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006305 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 (cap->oap->op_type != OP_NOP
6307 && cap->arg == FORWARD && flag == '{')))
6308 clearopbeep(cap->oap);
6309 else
6310 {
6311 if (cap->oap->op_type == OP_NOP)
6312 beginline(BL_WHITE | BL_FIX);
6313#ifdef FEAT_FOLDING
6314 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6315 foldOpenCursor();
6316#endif
6317 }
6318 }
6319
6320 /*
6321 * "[p", "[P", "]P" and "]p": put with indent adjustment
6322 */
6323 else if (cap->nchar == 'p' || cap->nchar == 'P')
6324 {
6325 if (!checkclearopq(cap->oap))
6326 {
6327 prep_redo_cmd(cap);
6328 do_put(cap->oap->regname,
6329 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6330 cap->count1, PUT_FIXINDENT);
6331 }
6332 }
6333
6334 /*
6335 * "['", "[`", "]'" and "]`": jump to next mark
6336 */
6337 else if (cap->nchar == '\'' || cap->nchar == '`')
6338 {
6339 pos = &curwin->w_cursor;
6340 for (n = cap->count1; n > 0; --n)
6341 {
6342 prev_pos = *pos;
6343 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6344 cap->nchar == '\'');
6345 if (pos == NULL)
6346 break;
6347 }
6348 if (pos == NULL)
6349 pos = &prev_pos;
6350 nv_cursormark(cap, cap->nchar == '\'', pos);
6351 }
6352
6353#ifdef FEAT_MOUSE
6354 /*
6355 * [ or ] followed by a middle mouse click: put selected text with
6356 * indent adjustment. Any other button just does as usual.
6357 */
6358 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6359 {
6360 (void)do_mouse(cap->oap, cap->nchar,
6361 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6362 cap->count1, PUT_FIXINDENT);
6363 }
6364#endif /* FEAT_MOUSE */
6365
6366#ifdef FEAT_FOLDING
6367 /*
6368 * "[z" and "]z": move to start or end of open fold.
6369 */
6370 else if (cap->nchar == 'z')
6371 {
6372 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6373 cap->count1) == FAIL)
6374 clearopbeep(cap->oap);
6375 }
6376#endif
6377
6378#ifdef FEAT_DIFF
6379 /*
6380 * "[c" and "]c": move to next or previous diff-change.
6381 */
6382 else if (cap->nchar == 'c')
6383 {
6384 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6385 cap->count1) == FAIL)
6386 clearopbeep(cap->oap);
6387 }
6388#endif
6389
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006390#ifdef FEAT_SYN_HL
6391 /*
6392 * "[s", "[S", "]s" and "]S": move to next spell error.
6393 */
6394 else if (cap->nchar == 's' || cap->nchar == 'S')
6395 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006396 setpcmark();
6397 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006398 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6399 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006400 {
6401 clearopbeep(cap->oap);
6402 break;
6403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006404 }
6405#endif
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 /* Not a valid cap->nchar. */
6408 else
6409 clearopbeep(cap->oap);
6410}
6411
6412/*
6413 * Handle Normal mode "%" command.
6414 */
6415 static void
6416nv_percent(cap)
6417 cmdarg_T *cap;
6418{
6419 pos_T *pos;
6420#ifdef FEAT_FOLDING
6421 linenr_T lnum = curwin->w_cursor.lnum;
6422#endif
6423
6424 cap->oap->inclusive = TRUE;
6425 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6426 {
6427 if (cap->count0 > 100)
6428 clearopbeep(cap->oap);
6429 else
6430 {
6431 cap->oap->motion_type = MLINE;
6432 setpcmark();
6433 /* Round up, so CTRL-G will give same value. Watch out for a
6434 * large line count, the line number must not go negative! */
6435 if (curbuf->b_ml.ml_line_count > 1000000)
6436 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6437 / 100L * cap->count0;
6438 else
6439 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6440 cap->count0 + 99L) / 100L;
6441 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6442 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6443 beginline(BL_SOL | BL_FIX);
6444 }
6445 }
6446 else /* "%" : go to matching paren */
6447 {
6448 cap->oap->motion_type = MCHAR;
6449 cap->oap->use_reg_one = TRUE;
6450 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6451 clearopbeep(cap->oap);
6452 else
6453 {
6454 setpcmark();
6455 curwin->w_cursor = *pos;
6456 curwin->w_set_curswant = TRUE;
6457#ifdef FEAT_VIRTUALEDIT
6458 curwin->w_cursor.coladd = 0;
6459#endif
6460#ifdef FEAT_VISUAL
6461 adjust_for_sel(cap);
6462#endif
6463 }
6464 }
6465#ifdef FEAT_FOLDING
6466 if (cap->oap->op_type == OP_NOP
6467 && lnum != curwin->w_cursor.lnum
6468 && (fdo_flags & FDO_PERCENT)
6469 && KeyTyped)
6470 foldOpenCursor();
6471#endif
6472}
6473
6474/*
6475 * Handle "(" and ")" commands.
6476 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6477 */
6478 static void
6479nv_brace(cap)
6480 cmdarg_T *cap;
6481{
6482 cap->oap->motion_type = MCHAR;
6483 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006484 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6485 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 curwin->w_set_curswant = TRUE;
6487
6488 if (findsent(cap->arg, cap->count1) == FAIL)
6489 clearopbeep(cap->oap);
6490 else
6491 {
6492#ifdef FEAT_VIRTUALEDIT
6493 curwin->w_cursor.coladd = 0;
6494#endif
6495#ifdef FEAT_FOLDING
6496 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6497 foldOpenCursor();
6498#endif
6499 }
6500}
6501
6502/*
6503 * "m" command: Mark a position.
6504 */
6505 static void
6506nv_mark(cap)
6507 cmdarg_T *cap;
6508{
6509 if (!checkclearop(cap->oap))
6510 {
6511 if (setmark(cap->nchar) == FAIL)
6512 clearopbeep(cap->oap);
6513 }
6514}
6515
6516/*
6517 * "{" and "}" commands.
6518 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6519 */
6520 static void
6521nv_findpar(cap)
6522 cmdarg_T *cap;
6523{
6524 cap->oap->motion_type = MCHAR;
6525 cap->oap->inclusive = FALSE;
6526 cap->oap->use_reg_one = TRUE;
6527 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006528 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 clearopbeep(cap->oap);
6530 else
6531 {
6532#ifdef FEAT_VIRTUALEDIT
6533 curwin->w_cursor.coladd = 0;
6534#endif
6535#ifdef FEAT_FOLDING
6536 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6537 foldOpenCursor();
6538#endif
6539 }
6540}
6541
6542/*
6543 * "u" command: Undo or make lower case.
6544 */
6545 static void
6546nv_undo(cap)
6547 cmdarg_T *cap;
6548{
6549 if (cap->oap->op_type == OP_LOWER
6550#ifdef FEAT_VISUAL
6551 || VIsual_active
6552#endif
6553 )
6554 {
6555 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6556 cap->cmdchar = 'g';
6557 cap->nchar = 'u';
6558 nv_operator(cap);
6559 }
6560 else
6561 nv_kundo(cap);
6562}
6563
6564/*
6565 * <Undo> command.
6566 */
6567 static void
6568nv_kundo(cap)
6569 cmdarg_T *cap;
6570{
6571 if (!checkclearopq(cap->oap))
6572 {
6573 u_undo((int)cap->count1);
6574 curwin->w_set_curswant = TRUE;
6575 }
6576}
6577
6578/*
6579 * Handle the "r" command.
6580 */
6581 static void
6582nv_replace(cap)
6583 cmdarg_T *cap;
6584{
6585 char_u *ptr;
6586 int had_ctrl_v;
6587 long n;
6588
6589 if (checkclearop(cap->oap))
6590 return;
6591
6592 /* get another character */
6593 if (cap->nchar == Ctrl_V)
6594 {
6595 had_ctrl_v = Ctrl_V;
6596 cap->nchar = get_literal();
6597 /* Don't redo a multibyte character with CTRL-V. */
6598 if (cap->nchar > DEL)
6599 had_ctrl_v = NUL;
6600 }
6601 else
6602 had_ctrl_v = NUL;
6603
6604#ifdef FEAT_VISUAL
6605 /* Visual mode "r" */
6606 if (VIsual_active)
6607 {
6608 nv_operator(cap);
6609 return;
6610 }
6611#endif
6612
6613#ifdef FEAT_VIRTUALEDIT
6614 /* Break tabs, etc. */
6615 if (virtual_active())
6616 {
6617 if (u_save_cursor() == FAIL)
6618 return;
6619 if (gchar_cursor() == NUL)
6620 {
6621 /* Add extra space and put the cursor on the first one. */
6622 coladvance_force((colnr_T)(getviscol() + cap->count1));
6623 curwin->w_cursor.col -= cap->count1;
6624 }
6625 else if (gchar_cursor() == TAB)
6626 coladvance_force(getviscol());
6627 }
6628#endif
6629
6630 /*
6631 * Check for a special key or not enough characters to replace.
6632 */
6633 ptr = ml_get_cursor();
6634 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6635#ifdef FEAT_MBYTE
6636 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6637#endif
6638 )
6639 {
6640 clearopbeep(cap->oap);
6641 return;
6642 }
6643
6644 /*
6645 * Replacing with a TAB is done by edit() when it is complicated because
6646 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6647 * Other characters are done below to avoid problems with things like
6648 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6649 */
6650 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6651 {
6652 stuffnumReadbuff(cap->count1);
6653 stuffcharReadbuff('R');
6654 stuffcharReadbuff('\t');
6655 stuffcharReadbuff(ESC);
6656 return;
6657 }
6658
6659 /* save line for undo */
6660 if (u_save_cursor() == FAIL)
6661 return;
6662
6663 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6664 {
6665 /*
6666 * Replace character(s) by a single newline.
6667 * Strange vi behaviour: Only one newline is inserted.
6668 * Delete the characters here.
6669 * Insert the newline with an insert command, takes care of
6670 * autoindent. The insert command depends on being on the last
6671 * character of a line or not.
6672 */
6673#ifdef FEAT_MBYTE
6674 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6675#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006676 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#endif
6678 stuffcharReadbuff('\r');
6679 stuffcharReadbuff(ESC);
6680
6681 /* Give 'r' to edit(), to get the redo command right. */
6682 invoke_edit(cap, TRUE, 'r', FALSE);
6683 }
6684 else
6685 {
6686 prep_redo(cap->oap->regname, cap->count1,
6687 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6688
6689 curbuf->b_op_start = curwin->w_cursor;
6690#ifdef FEAT_MBYTE
6691 if (has_mbyte)
6692 {
6693 int old_State = State;
6694
6695 if (cap->ncharC1 != 0)
6696 AppendCharToRedobuff(cap->ncharC1);
6697 if (cap->ncharC2 != 0)
6698 AppendCharToRedobuff(cap->ncharC2);
6699
6700 /* This is slow, but it handles replacing a single-byte with a
6701 * multi-byte and the other way around. Also handles adding
6702 * composing characters for utf-8. */
6703 for (n = cap->count1; n > 0; --n)
6704 {
6705 State = REPLACE;
6706 ins_char(cap->nchar);
6707 State = old_State;
6708 if (cap->ncharC1 != 0)
6709 ins_char(cap->ncharC1);
6710 if (cap->ncharC2 != 0)
6711 ins_char(cap->ncharC2);
6712 }
6713 }
6714 else
6715#endif
6716 {
6717 /*
6718 * Replace the characters within one line.
6719 */
6720 for (n = cap->count1; n > 0; --n)
6721 {
6722 /*
6723 * Get ptr again, because u_save and/or showmatch() will have
6724 * released the line. At the same time we let know that the
6725 * line will be changed.
6726 */
6727 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6728 ptr[curwin->w_cursor.col] = cap->nchar;
6729 if (p_sm && msg_silent == 0)
6730 showmatch(cap->nchar);
6731 ++curwin->w_cursor.col;
6732 }
6733#ifdef FEAT_NETBEANS_INTG
6734 if (usingNetbeans)
6735 {
6736 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6737
Bram Moolenaar009b2592004-10-24 19:18:58 +00006738 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6739 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006741 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743#endif
6744
6745 /* mark the buffer as changed and prepare for displaying */
6746 changed_bytes(curwin->w_cursor.lnum,
6747 (colnr_T)(curwin->w_cursor.col - cap->count1));
6748 }
6749 --curwin->w_cursor.col; /* cursor on the last replaced char */
6750#ifdef FEAT_MBYTE
6751 /* if the character on the left of the current cursor is a multi-byte
6752 * character, move two characters left */
6753 if (has_mbyte)
6754 mb_adjust_cursor();
6755#endif
6756 curbuf->b_op_end = curwin->w_cursor;
6757 curwin->w_set_curswant = TRUE;
6758 set_last_insert(cap->nchar);
6759 }
6760}
6761
6762#ifdef FEAT_VISUAL
6763/*
6764 * 'o': Exchange start and end of Visual area.
6765 * 'O': same, but in block mode exchange left and right corners.
6766 */
6767 static void
6768v_swap_corners(cmdchar)
6769 int cmdchar;
6770{
6771 pos_T old_cursor;
6772 colnr_T left, right;
6773
6774 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6775 {
6776 old_cursor = curwin->w_cursor;
6777 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6778 curwin->w_cursor.lnum = VIsual.lnum;
6779 coladvance(left);
6780 VIsual = curwin->w_cursor;
6781
6782 curwin->w_cursor.lnum = old_cursor.lnum;
6783 curwin->w_curswant = right;
6784 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6785 * right one column */
6786 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6787 ++curwin->w_curswant;
6788 coladvance(curwin->w_curswant);
6789 if (curwin->w_cursor.col == old_cursor.col
6790#ifdef FEAT_VIRTUALEDIT
6791 && (!virtual_active()
6792 || curwin->w_cursor.coladd == old_cursor.coladd)
6793#endif
6794 )
6795 {
6796 curwin->w_cursor.lnum = VIsual.lnum;
6797 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6798 ++right;
6799 coladvance(right);
6800 VIsual = curwin->w_cursor;
6801
6802 curwin->w_cursor.lnum = old_cursor.lnum;
6803 coladvance(left);
6804 curwin->w_curswant = left;
6805 }
6806 }
6807 else
6808 {
6809 old_cursor = curwin->w_cursor;
6810 curwin->w_cursor = VIsual;
6811 VIsual = old_cursor;
6812 curwin->w_set_curswant = TRUE;
6813 }
6814}
6815#endif /* FEAT_VISUAL */
6816
6817/*
6818 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6819 */
6820 static void
6821nv_Replace(cap)
6822 cmdarg_T *cap;
6823{
6824#ifdef FEAT_VISUAL
6825 if (VIsual_active) /* "R" is replace lines */
6826 {
6827 cap->cmdchar = 'c';
6828 cap->nchar = NUL;
6829 VIsual_mode = 'V';
6830 nv_operator(cap);
6831 }
6832 else
6833#endif
6834 if (!checkclearopq(cap->oap))
6835 {
6836 if (!curbuf->b_p_ma)
6837 EMSG(_(e_modifiable));
6838 else
6839 {
6840#ifdef FEAT_VIRTUALEDIT
6841 if (virtual_active())
6842 coladvance(getviscol());
6843#endif
6844 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6845 }
6846 }
6847}
6848
6849#ifdef FEAT_VREPLACE
6850/*
6851 * "gr".
6852 */
6853 static void
6854nv_vreplace(cap)
6855 cmdarg_T *cap;
6856{
6857# ifdef FEAT_VISUAL
6858 if (VIsual_active)
6859 {
6860 cap->cmdchar = 'r';
6861 cap->nchar = cap->extra_char;
6862 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6863 }
6864 else
6865# endif
6866 if (!checkclearopq(cap->oap))
6867 {
6868 if (!curbuf->b_p_ma)
6869 EMSG(_(e_modifiable));
6870 else
6871 {
6872 if (cap->extra_char == Ctrl_V) /* get another character */
6873 cap->extra_char = get_literal();
6874 stuffcharReadbuff(cap->extra_char);
6875 stuffcharReadbuff(ESC);
6876# ifdef FEAT_VIRTUALEDIT
6877 if (virtual_active())
6878 coladvance(getviscol());
6879# endif
6880 invoke_edit(cap, TRUE, 'v', FALSE);
6881 }
6882 }
6883}
6884#endif
6885
6886/*
6887 * Swap case for "~" command, when it does not work like an operator.
6888 */
6889 static void
6890n_swapchar(cap)
6891 cmdarg_T *cap;
6892{
6893 long n;
6894 pos_T startpos;
6895 int did_change = 0;
6896#ifdef FEAT_NETBEANS_INTG
6897 pos_T pos;
6898 char_u *ptr;
6899 int count;
6900#endif
6901
6902 if (checkclearopq(cap->oap))
6903 return;
6904
6905 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6906 {
6907 clearopbeep(cap->oap);
6908 return;
6909 }
6910
6911 prep_redo_cmd(cap);
6912
6913 if (u_save_cursor() == FAIL)
6914 return;
6915
6916 startpos = curwin->w_cursor;
6917#ifdef FEAT_NETBEANS_INTG
6918 pos = startpos;
6919#endif
6920 for (n = cap->count1; n > 0; --n)
6921 {
6922 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6923 inc_cursor();
6924 if (gchar_cursor() == NUL)
6925 {
6926 if (vim_strchr(p_ww, '~') != NULL
6927 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6928 {
6929#ifdef FEAT_NETBEANS_INTG
6930 if (usingNetbeans)
6931 {
6932 if (did_change)
6933 {
6934 ptr = ml_get(pos.lnum);
6935 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006936 netbeans_removed(curbuf, pos.lnum, pos.col,
6937 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006939 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 pos.col = 0;
6942 pos.lnum++;
6943 }
6944#endif
6945 ++curwin->w_cursor.lnum;
6946 curwin->w_cursor.col = 0;
6947 if (n > 1)
6948 {
6949 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6950 break;
6951 u_clearline();
6952 }
6953 }
6954 else
6955 break;
6956 }
6957 }
6958#ifdef FEAT_NETBEANS_INTG
6959 if (did_change && usingNetbeans)
6960 {
6961 ptr = ml_get(pos.lnum);
6962 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006963 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6964 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 }
6966#endif
6967
6968
6969 check_cursor();
6970 curwin->w_set_curswant = TRUE;
6971 if (did_change)
6972 {
6973 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6974 0L);
6975 curbuf->b_op_start = startpos;
6976 curbuf->b_op_end = curwin->w_cursor;
6977 if (curbuf->b_op_end.col > 0)
6978 --curbuf->b_op_end.col;
6979 }
6980}
6981
6982/*
6983 * Move cursor to mark.
6984 */
6985 static void
6986nv_cursormark(cap, flag, pos)
6987 cmdarg_T *cap;
6988 int flag;
6989 pos_T *pos;
6990{
6991 if (check_mark(pos) == FAIL)
6992 clearop(cap->oap);
6993 else
6994 {
6995 if (cap->cmdchar == '\''
6996 || cap->cmdchar == '`'
6997 || cap->cmdchar == '['
6998 || cap->cmdchar == ']')
6999 setpcmark();
7000 curwin->w_cursor = *pos;
7001 if (flag)
7002 beginline(BL_WHITE | BL_FIX);
7003 else
7004 check_cursor();
7005 }
7006 cap->oap->motion_type = flag ? MLINE : MCHAR;
7007 if (cap->cmdchar == '`')
7008 cap->oap->use_reg_one = TRUE;
7009 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7010 curwin->w_set_curswant = TRUE;
7011}
7012
7013#ifdef FEAT_VISUAL
7014/*
7015 * Handle commands that are operators in Visual mode.
7016 */
7017 static void
7018v_visop(cap)
7019 cmdarg_T *cap;
7020{
7021 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7022
7023 /* Uppercase means linewise, except in block mode, then "D" deletes till
7024 * the end of the line, and "C" replaces til EOL */
7025 if (isupper(cap->cmdchar))
7026 {
7027 if (VIsual_mode != Ctrl_V)
7028 VIsual_mode = 'V';
7029 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7030 curwin->w_curswant = MAXCOL;
7031 }
7032 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7033 nv_operator(cap);
7034}
7035#endif
7036
7037/*
7038 * "s" and "S" commands.
7039 */
7040 static void
7041nv_subst(cap)
7042 cmdarg_T *cap;
7043{
7044#ifdef FEAT_VISUAL
7045 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7046 {
7047 if (cap->cmdchar == 'S')
7048 VIsual_mode = 'V';
7049 cap->cmdchar = 'c';
7050 nv_operator(cap);
7051 }
7052 else
7053#endif
7054 nv_optrans(cap);
7055}
7056
7057/*
7058 * Abbreviated commands.
7059 */
7060 static void
7061nv_abbrev(cap)
7062 cmdarg_T *cap;
7063{
7064 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7065 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7066
7067#ifdef FEAT_VISUAL
7068 /* in Visual mode these commands are operators */
7069 if (VIsual_active)
7070 v_visop(cap);
7071 else
7072#endif
7073 nv_optrans(cap);
7074}
7075
7076/*
7077 * Translate a command into another command.
7078 */
7079 static void
7080nv_optrans(cap)
7081 cmdarg_T *cap;
7082{
7083 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7084 (char_u *)"d$", (char_u *)"c$",
7085 (char_u *)"cl", (char_u *)"cc",
7086 (char_u *)"yy", (char_u *)":s\r"};
7087 static char_u *str = (char_u *)"xXDCsSY&";
7088
7089 if (!checkclearopq(cap->oap))
7090 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007091 /* In Vi "2D" doesn't delete the next line. Can't translate it
7092 * either, because "2." should also not use the count. */
7093 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7094 {
7095 cap->oap->start = curwin->w_cursor;
7096 cap->oap->op_type = OP_DELETE;
7097 cap->count1 = 1;
7098 nv_dollar(cap);
7099 finish_op = TRUE;
7100 ResetRedobuff();
7101 AppendCharToRedobuff('D');
7102 }
7103 else
7104 {
7105 if (cap->count0)
7106 stuffnumReadbuff(cap->count0);
7107 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110 cap->opcount = 0;
7111}
7112
7113/*
7114 * "'" and "`" commands. Also for "g'" and "g`".
7115 * cap->arg is TRUE for "'" and "g'".
7116 */
7117 static void
7118nv_gomark(cap)
7119 cmdarg_T *cap;
7120{
7121 pos_T *pos;
7122 int c;
7123#ifdef FEAT_FOLDING
7124 linenr_T lnum = curwin->w_cursor.lnum;
7125 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7126#endif
7127
7128 if (cap->cmdchar == 'g')
7129 c = cap->extra_char;
7130 else
7131 c = cap->nchar;
7132 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7133 if (pos == (pos_T *)-1) /* jumped to other file */
7134 {
7135 if (cap->arg)
7136 {
7137 check_cursor_lnum();
7138 beginline(BL_WHITE | BL_FIX);
7139 }
7140 else
7141 check_cursor();
7142 }
7143 else
7144 nv_cursormark(cap, cap->arg, pos);
7145
7146#ifdef FEAT_VIRTUALEDIT
7147 /* May need to clear the coladd that a mark includes. */
7148 if (!virtual_active())
7149 curwin->w_cursor.coladd = 0;
7150#endif
7151#ifdef FEAT_FOLDING
7152 if (cap->oap->op_type == OP_NOP
7153 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7154 && (fdo_flags & FDO_MARK)
7155 && old_KeyTyped)
7156 foldOpenCursor();
7157#endif
7158}
7159
7160/*
7161 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7162 */
7163 static void
7164nv_pcmark(cap)
7165 cmdarg_T *cap;
7166{
7167#ifdef FEAT_JUMPLIST
7168 pos_T *pos;
7169# ifdef FEAT_FOLDING
7170 linenr_T lnum = curwin->w_cursor.lnum;
7171 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7172# endif
7173
7174 if (!checkclearopq(cap->oap))
7175 {
7176 if (cap->cmdchar == 'g')
7177 pos = movechangelist((int)cap->count1);
7178 else
7179 pos = movemark((int)cap->count1);
7180 if (pos == (pos_T *)-1) /* jump to other file */
7181 {
7182 curwin->w_set_curswant = TRUE;
7183 check_cursor();
7184 }
7185 else if (pos != NULL) /* can jump */
7186 nv_cursormark(cap, FALSE, pos);
7187 else if (cap->cmdchar == 'g')
7188 {
7189 if (curbuf->b_changelistlen == 0)
7190 EMSG(_("E664: changelist is empty"));
7191 else if (cap->count1 < 0)
7192 EMSG(_("E662: At start of changelist"));
7193 else
7194 EMSG(_("E663: At end of changelist"));
7195 }
7196 else
7197 clearopbeep(cap->oap);
7198# ifdef FEAT_FOLDING
7199 if (cap->oap->op_type == OP_NOP
7200 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7201 && (fdo_flags & FDO_MARK)
7202 && old_KeyTyped)
7203 foldOpenCursor();
7204# endif
7205 }
7206#else
7207 clearopbeep(cap->oap);
7208#endif
7209}
7210
7211/*
7212 * Handle '"' command.
7213 */
7214 static void
7215nv_regname(cap)
7216 cmdarg_T *cap;
7217{
7218 if (checkclearop(cap->oap))
7219 return;
7220#ifdef FEAT_EVAL
7221 if (cap->nchar == '=')
7222 cap->nchar = get_expr_register();
7223#endif
7224 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7225 {
7226 cap->oap->regname = cap->nchar;
7227 cap->opcount = cap->count0; /* remember count before '"' */
7228#ifdef FEAT_EVAL
7229 set_reg_var(cap->oap->regname);
7230#endif
7231 }
7232 else
7233 clearopbeep(cap->oap);
7234}
7235
7236#ifdef FEAT_VISUAL
7237/*
7238 * Handle "v", "V" and "CTRL-V" commands.
7239 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7240 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007241 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 */
7243 static void
7244nv_visual(cap)
7245 cmdarg_T *cap;
7246{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007247 if (cap->cmdchar == Ctrl_Q)
7248 cap->cmdchar = Ctrl_V;
7249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7251 * characterwise, linewise, or blockwise. */
7252 if (cap->oap->op_type != OP_NOP)
7253 {
7254 cap->oap->motion_force = cap->cmdchar;
7255 finish_op = FALSE; /* operator doesn't finish now but later */
7256 return;
7257 }
7258
7259 VIsual_select = cap->arg;
7260 if (VIsual_active) /* change Visual mode */
7261 {
7262 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7263 end_visual_mode();
7264 else /* toggle char/block mode */
7265 { /* or char/line mode */
7266 VIsual_mode = cap->cmdchar;
7267 showmode();
7268 }
7269 redraw_curbuf_later(INVERTED); /* update the inversion */
7270 }
7271 else /* start Visual mode */
7272 {
7273 check_visual_highlight();
7274 if (cap->count0) /* use previously selected part */
7275 {
7276 if (resel_VIsual_mode == NUL) /* there is none */
7277 {
7278 beep_flush();
7279 return;
7280 }
7281 VIsual = curwin->w_cursor;
7282
7283 VIsual_active = TRUE;
7284 VIsual_reselect = TRUE;
7285 if (!cap->arg)
7286 /* start Select mode when 'selectmode' contains "cmd" */
7287 may_start_select('c');
7288#ifdef FEAT_MOUSE
7289 setmouse();
7290#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007291 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 redraw_cmdline = TRUE; /* show visual mode later */
7293 /*
7294 * For V and ^V, we multiply the number of lines even if there
7295 * was only one -- webb
7296 */
7297 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7298 {
7299 curwin->w_cursor.lnum +=
7300 resel_VIsual_line_count * cap->count0 - 1;
7301 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7302 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7303 }
7304 VIsual_mode = resel_VIsual_mode;
7305 if (VIsual_mode == 'v')
7306 {
7307 if (resel_VIsual_line_count <= 1)
7308 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7309 else
7310 curwin->w_cursor.col = resel_VIsual_col;
7311 check_cursor_col();
7312 }
7313 if (resel_VIsual_col == MAXCOL)
7314 {
7315 curwin->w_curswant = MAXCOL;
7316 coladvance((colnr_T)MAXCOL);
7317 }
7318 else if (VIsual_mode == Ctrl_V)
7319 {
7320 validate_virtcol();
7321 curwin->w_curswant = curwin->w_virtcol
7322 + resel_VIsual_col * cap->count0 - 1;
7323 coladvance(curwin->w_curswant);
7324 }
7325 else
7326 curwin->w_set_curswant = TRUE;
7327 redraw_curbuf_later(INVERTED); /* show the inversion */
7328 }
7329 else
7330 {
7331 if (!cap->arg)
7332 /* start Select mode when 'selectmode' contains "cmd" */
7333 may_start_select('c');
7334 n_start_visual_mode(cap->cmdchar);
7335 }
7336 }
7337}
7338
7339/*
7340 * Start selection for Shift-movement keys.
7341 */
7342 void
7343start_selection()
7344{
7345 /* if 'selectmode' contains "key", start Select mode */
7346 may_start_select('k');
7347 n_start_visual_mode('v');
7348}
7349
7350/*
7351 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7352 */
7353 void
7354may_start_select(c)
7355 int c;
7356{
7357 VIsual_select = (stuff_empty() && typebuf_typed()
7358 && (vim_strchr(p_slm, c) != NULL));
7359}
7360
7361/*
7362 * Start Visual mode "c".
7363 * Should set VIsual_select before calling this.
7364 */
7365 static void
7366n_start_visual_mode(c)
7367 int c;
7368{
7369 VIsual_mode = c;
7370 VIsual_active = TRUE;
7371 VIsual_reselect = TRUE;
7372#ifdef FEAT_VIRTUALEDIT
7373 /* Corner case: the 0 position in a tab may change when going into
7374 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7375 */
7376 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7377 coladvance(curwin->w_virtcol);
7378#endif
7379 VIsual = curwin->w_cursor;
7380
7381#ifdef FEAT_FOLDING
7382 foldAdjustVisual();
7383#endif
7384
7385#ifdef FEAT_MOUSE
7386 setmouse();
7387#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007388 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 redraw_cmdline = TRUE; /* show visual mode later */
7390#ifdef FEAT_CLIPBOARD
7391 /* Make sure the clipboard gets updated. Needed because start and
7392 * end may still be the same, and the selection needs to be owned */
7393 clip_star.vmode = NUL;
7394#endif
7395
7396 /* Only need to redraw this line, unless still need to redraw an old
7397 * Visual area (when 'lazyredraw' is set). */
7398 if (curwin->w_redr_type < INVERTED)
7399 {
7400 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7401 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7402 }
7403}
7404
7405#endif /* FEAT_VISUAL */
7406
7407/*
7408 * CTRL-W: Window commands
7409 */
7410 static void
7411nv_window(cap)
7412 cmdarg_T *cap;
7413{
7414#ifdef FEAT_WINDOWS
7415 if (!checkclearop(cap->oap))
7416 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7417#else
7418 (void)checkclearop(cap->oap);
7419#endif
7420}
7421
7422/*
7423 * CTRL-Z: Suspend
7424 */
7425 static void
7426nv_suspend(cap)
7427 cmdarg_T *cap;
7428{
7429 clearop(cap->oap);
7430#ifdef FEAT_VISUAL
7431 if (VIsual_active)
7432 end_visual_mode(); /* stop Visual mode */
7433#endif
7434 do_cmdline_cmd((char_u *)"st");
7435}
7436
7437/*
7438 * Commands starting with "g".
7439 */
7440 static void
7441nv_g_cmd(cap)
7442 cmdarg_T *cap;
7443{
7444 oparg_T *oap = cap->oap;
7445#ifdef FEAT_VISUAL
7446 pos_T tpos;
7447#endif
7448 int i;
7449 int flag = FALSE;
7450
7451 switch (cap->nchar)
7452 {
7453#ifdef MEM_PROFILE
7454 /*
7455 * "g^A": dump log of used memory.
7456 */
7457 case Ctrl_A:
7458 vim_mem_profile_dump();
7459 break;
7460#endif
7461
7462#ifdef FEAT_VREPLACE
7463 /*
7464 * "gR": Enter virtual replace mode.
7465 */
7466 case 'R':
7467 cap->arg = TRUE;
7468 nv_Replace(cap);
7469 break;
7470
7471 case 'r':
7472 nv_vreplace(cap);
7473 break;
7474#endif
7475
7476 case '&':
7477 do_cmdline_cmd((char_u *)"%s//~/&");
7478 break;
7479
7480#ifdef FEAT_VISUAL
7481 /*
7482 * "gv": Reselect the previous Visual area. If Visual already active,
7483 * exchange previous and current Visual area.
7484 */
7485 case 'v':
7486 if (checkclearop(oap))
7487 break;
7488
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007489 if ( curbuf->b_visual.vi_start.lnum == 0
7490 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7491 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 beep_flush();
7493 else
7494 {
7495 /* set w_cursor to the start of the Visual area, tpos to the end */
7496 if (VIsual_active)
7497 {
7498 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007499 VIsual_mode = curbuf->b_visual.vi_mode;
7500 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501# ifdef FEAT_EVAL
7502 curbuf->b_visual_mode_eval = i;
7503# endif
7504 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007505 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7506 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007508 tpos = curbuf->b_visual.vi_end;
7509 curbuf->b_visual.vi_end = curwin->w_cursor;
7510 curwin->w_cursor = curbuf->b_visual.vi_start;
7511 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 else
7514 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007515 VIsual_mode = curbuf->b_visual.vi_mode;
7516 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7517 tpos = curbuf->b_visual.vi_end;
7518 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520
7521 VIsual_active = TRUE;
7522 VIsual_reselect = TRUE;
7523
7524 /* Set Visual to the start and w_cursor to the end of the Visual
7525 * area. Make sure they are on an existing character. */
7526 check_cursor();
7527 VIsual = curwin->w_cursor;
7528 curwin->w_cursor = tpos;
7529 check_cursor();
7530 update_topline();
7531 /*
7532 * When called from normal "g" command: start Select mode when
7533 * 'selectmode' contains "cmd". When called for K_SELECT, always
7534 * start Select mode.
7535 */
7536 if (cap->arg)
7537 VIsual_select = TRUE;
7538 else
7539 may_start_select('c');
7540#ifdef FEAT_MOUSE
7541 setmouse();
7542#endif
7543#ifdef FEAT_CLIPBOARD
7544 /* Make sure the clipboard gets updated. Needed because start and
7545 * end are still the same, and the selection needs to be owned */
7546 clip_star.vmode = NUL;
7547#endif
7548 redraw_curbuf_later(INVERTED);
7549 showmode();
7550 }
7551 break;
7552 /*
7553 * "gV": Don't reselect the previous Visual area after a Select mode
7554 * mapping of menu.
7555 */
7556 case 'V':
7557 VIsual_reselect = FALSE;
7558 break;
7559
7560 /*
7561 * "gh": start Select mode.
7562 * "gH": start Select line mode.
7563 * "g^H": start Select block mode.
7564 */
7565 case K_BS:
7566 cap->nchar = Ctrl_H;
7567 /* FALLTHROUGH */
7568 case 'h':
7569 case 'H':
7570 case Ctrl_H:
7571# ifdef EBCDIC
7572 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7573 if (cap->nchar == Ctrl_H)
7574 cap->cmdchar = Ctrl_V;
7575 else
7576# endif
7577 cap->cmdchar = cap->nchar + ('v' - 'h');
7578 cap->arg = TRUE;
7579 nv_visual(cap);
7580 break;
7581#endif /* FEAT_VISUAL */
7582
7583 /*
7584 * "gj" and "gk" two new funny movement keys -- up and down
7585 * movement based on *screen* line rather than *file* line.
7586 */
7587 case 'j':
7588 case K_DOWN:
7589 /* with 'nowrap' it works just like the normal "j" command; also when
7590 * in a closed fold */
7591 if (!curwin->w_p_wrap
7592#ifdef FEAT_FOLDING
7593 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7594#endif
7595 )
7596 {
7597 oap->motion_type = MLINE;
7598 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7599 }
7600 else
7601 i = nv_screengo(oap, FORWARD, cap->count1);
7602 if (i == FAIL)
7603 clearopbeep(oap);
7604 break;
7605
7606 case 'k':
7607 case K_UP:
7608 /* with 'nowrap' it works just like the normal "k" command; also when
7609 * in a closed fold */
7610 if (!curwin->w_p_wrap
7611#ifdef FEAT_FOLDING
7612 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7613#endif
7614 )
7615 {
7616 oap->motion_type = MLINE;
7617 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7618 }
7619 else
7620 i = nv_screengo(oap, BACKWARD, cap->count1);
7621 if (i == FAIL)
7622 clearopbeep(oap);
7623 break;
7624
7625 /*
7626 * "gJ": join two lines without inserting a space.
7627 */
7628 case 'J':
7629 nv_join(cap);
7630 break;
7631
7632 /*
7633 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7634 * "gm": middle of "g0" and "g$".
7635 */
7636 case '^':
7637 flag = TRUE;
7638 /* FALLTHROUGH */
7639
7640 case '0':
7641 case 'm':
7642 case K_HOME:
7643 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 oap->motion_type = MCHAR;
7645 oap->inclusive = FALSE;
7646 if (curwin->w_p_wrap
7647#ifdef FEAT_VERTSPLIT
7648 && curwin->w_width != 0
7649#endif
7650 )
7651 {
7652 int width1 = W_WIDTH(curwin) - curwin_col_off();
7653 int width2 = width1 + curwin_col_off2();
7654
7655 validate_virtcol();
7656 i = 0;
7657 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7658 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7659 }
7660 else
7661 i = curwin->w_leftcol;
7662 /* Go to the middle of the screen line. When 'number' is on and lines
7663 * are wrapping the middle can be more to the left.*/
7664 if (cap->nchar == 'm')
7665 i += (W_WIDTH(curwin) - curwin_col_off()
7666 + ((curwin->w_p_wrap && i > 0)
7667 ? curwin_col_off2() : 0)) / 2;
7668 coladvance((colnr_T)i);
7669 if (flag)
7670 {
7671 do
7672 i = gchar_cursor();
7673 while (vim_iswhite(i) && oneright() == OK);
7674 }
7675 curwin->w_set_curswant = TRUE;
7676 break;
7677
7678 case '_':
7679 /* "g_": to the last non-blank character in the line or <count> lines
7680 * downward. */
7681 cap->oap->motion_type = MCHAR;
7682 cap->oap->inclusive = TRUE;
7683 curwin->w_curswant = MAXCOL;
7684 if (cursor_down((long)(cap->count1 - 1),
7685 cap->oap->op_type == OP_NOP) == FAIL)
7686 clearopbeep(cap->oap);
7687 else
7688 {
7689 char_u *ptr = ml_get_curline();
7690
7691 /* In Visual mode we may end up after the line. */
7692 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7693 --curwin->w_cursor.col;
7694
7695 /* Decrease the cursor column until it's on a non-blank. */
7696 while (curwin->w_cursor.col > 0
7697 && vim_iswhite(ptr[curwin->w_cursor.col]))
7698 --curwin->w_cursor.col;
7699 curwin->w_set_curswant = TRUE;
7700 }
7701 break;
7702
7703 case '$':
7704 case K_END:
7705 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
7707 int col_off = curwin_col_off();
7708
7709 oap->motion_type = MCHAR;
7710 oap->inclusive = TRUE;
7711 if (curwin->w_p_wrap
7712#ifdef FEAT_VERTSPLIT
7713 && curwin->w_width != 0
7714#endif
7715 )
7716 {
7717 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7718 if (cap->count1 == 1)
7719 {
7720 int width1 = W_WIDTH(curwin) - col_off;
7721 int width2 = width1 + curwin_col_off2();
7722
7723 validate_virtcol();
7724 i = width1 - 1;
7725 if (curwin->w_virtcol >= (colnr_T)width1)
7726 i += ((curwin->w_virtcol - width1) / width2 + 1)
7727 * width2;
7728 coladvance((colnr_T)i);
7729#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7730 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7731 {
7732 /*
7733 * Check for landing on a character that got split at
7734 * the end of the line. We do not want to advance to
7735 * the next screen line.
7736 */
7737 validate_virtcol();
7738 if (curwin->w_virtcol > (colnr_T)i)
7739 --curwin->w_cursor.col;
7740 }
7741#endif
7742 }
7743 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7744 clearopbeep(oap);
7745 }
7746 else
7747 {
7748 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7749 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007750
7751 /* Make sure we stick in this column. */
7752 validate_virtcol();
7753 curwin->w_curswant = curwin->w_virtcol;
7754 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 }
7757 break;
7758
7759 /*
7760 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7761 */
7762 case '*':
7763 case '#':
7764#if POUND != '#'
7765 case POUND: /* pound sign (sometimes equal to '#') */
7766#endif
7767 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7768 case ']': /* :tselect for current identifier */
7769 nv_ident(cap);
7770 break;
7771
7772 /*
7773 * ge and gE: go back to end of word
7774 */
7775 case 'e':
7776 case 'E':
7777 oap->motion_type = MCHAR;
7778 curwin->w_set_curswant = TRUE;
7779 oap->inclusive = TRUE;
7780 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7781 clearopbeep(oap);
7782 break;
7783
7784 /*
7785 * "g CTRL-G": display info about cursor position
7786 */
7787 case Ctrl_G:
7788 cursor_pos_info();
7789 break;
7790
7791 /*
7792 * "gi": start Insert at the last position.
7793 */
7794 case 'i':
7795 if (curbuf->b_last_insert.lnum != 0)
7796 {
7797 curwin->w_cursor = curbuf->b_last_insert;
7798 check_cursor_lnum();
7799 i = (int)STRLEN(ml_get_curline());
7800 if (curwin->w_cursor.col > (colnr_T)i)
7801 {
7802#ifdef FEAT_VIRTUALEDIT
7803 if (virtual_active())
7804 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7805#endif
7806 curwin->w_cursor.col = i;
7807 }
7808 }
7809 cap->cmdchar = 'i';
7810 nv_edit(cap);
7811 break;
7812
7813 /*
7814 * "gI": Start insert in column 1.
7815 */
7816 case 'I':
7817 beginline(0);
7818 if (!checkclearopq(oap))
7819 invoke_edit(cap, FALSE, 'g', FALSE);
7820 break;
7821
7822#ifdef FEAT_SEARCHPATH
7823 /*
7824 * "gf": goto file, edit file under cursor
7825 * "]f" and "[f": can also be used.
7826 */
7827 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007828 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 nv_gotofile(cap);
7830 break;
7831#endif
7832
7833 /* "g'm" and "g`m": jump to mark without setting pcmark */
7834 case '\'':
7835 cap->arg = TRUE;
7836 /*FALLTHROUGH*/
7837 case '`':
7838 nv_gomark(cap);
7839 break;
7840
7841 /*
7842 * "gs": Goto sleep.
7843 */
7844 case 's':
7845 do_sleep(cap->count1 * 1000L);
7846 break;
7847
7848 /*
7849 * "ga": Display the ascii value of the character under the
7850 * cursor. It is displayed in decimal, hex, and octal. -- webb
7851 */
7852 case 'a':
7853 do_ascii(NULL);
7854 break;
7855
7856#ifdef FEAT_MBYTE
7857 /*
7858 * "g8": Display the bytes used for the UTF-8 character under the
7859 * cursor. It is displayed in hex.
7860 */
7861 case '8':
7862 show_utf8();
7863 break;
7864#endif
7865
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007866 case '<':
7867 show_sb_text();
7868 break;
7869
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 /*
7871 * "gg": Goto the first line in file. With a count it goes to
7872 * that line number like for "G". -- webb
7873 */
7874 case 'g':
7875 cap->arg = FALSE;
7876 nv_goto(cap);
7877 break;
7878
7879 /*
7880 * Two-character operators:
7881 * "gq" Format text
7882 * "gw" Format text and keep cursor position
7883 * "g~" Toggle the case of the text.
7884 * "gu" Change text to lower case.
7885 * "gU" Change text to upper case.
7886 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007887 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 */
7889 case 'q':
7890 case 'w':
7891 oap->cursor_start = curwin->w_cursor;
7892 /*FALLTHROUGH*/
7893 case '~':
7894 case 'u':
7895 case 'U':
7896 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007897 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 nv_operator(cap);
7899 break;
7900
7901 /*
7902 * "gd": Find first occurence of pattern under the cursor in the
7903 * current function
7904 * "gD": idem, but in the current file.
7905 */
7906 case 'd':
7907 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007908 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 break;
7910
7911#ifdef FEAT_MOUSE
7912 /*
7913 * g<*Mouse> : <C-*mouse>
7914 */
7915 case K_MIDDLEMOUSE:
7916 case K_MIDDLEDRAG:
7917 case K_MIDDLERELEASE:
7918 case K_LEFTMOUSE:
7919 case K_LEFTDRAG:
7920 case K_LEFTRELEASE:
7921 case K_RIGHTMOUSE:
7922 case K_RIGHTDRAG:
7923 case K_RIGHTRELEASE:
7924 case K_X1MOUSE:
7925 case K_X1DRAG:
7926 case K_X1RELEASE:
7927 case K_X2MOUSE:
7928 case K_X2DRAG:
7929 case K_X2RELEASE:
7930 mod_mask = MOD_MASK_CTRL;
7931 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7932 break;
7933#endif
7934
7935 case K_IGNORE:
7936 break;
7937
7938 /*
7939 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7940 */
7941 case 'p':
7942 case 'P':
7943 nv_put(cap);
7944 break;
7945
7946#ifdef FEAT_BYTEOFF
7947 /* "go": goto byte count from start of buffer */
7948 case 'o':
7949 goto_byte(cap->count0);
7950 break;
7951#endif
7952
7953 /* "gQ": improved Ex mode */
7954 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007955 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 {
7957 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007958 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 break;
7960 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007961
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (!checkclearopq(oap))
7963 do_exmode(TRUE);
7964 break;
7965
7966#ifdef FEAT_JUMPLIST
7967 case ',':
7968 nv_pcmark(cap);
7969 break;
7970
7971 case ';':
7972 cap->count1 = -cap->count1;
7973 nv_pcmark(cap);
7974 break;
7975#endif
7976
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007977#ifdef FEAT_WINDOWS
7978 case 't':
7979 goto_tabpage((int)cap->count0);
7980 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007981 case 'T':
7982 goto_tabpage(-(int)cap->count1);
7983 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007984#endif
7985
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 default:
7987 clearopbeep(oap);
7988 break;
7989 }
7990}
7991
7992/*
7993 * Handle "o" and "O" commands.
7994 */
7995 static void
7996n_opencmd(cap)
7997 cmdarg_T *cap;
7998{
7999 if (!checkclearopq(cap->oap))
8000 {
8001#ifdef FEAT_FOLDING
8002 if (cap->cmdchar == 'O')
8003 /* Open above the first line of a folded sequence of lines */
8004 (void)hasFolding(curwin->w_cursor.lnum,
8005 &curwin->w_cursor.lnum, NULL);
8006 else
8007 /* Open below the last line of a folded sequence of lines */
8008 (void)hasFolding(curwin->w_cursor.lnum,
8009 NULL, &curwin->w_cursor.lnum);
8010#endif
8011 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8012 (cap->cmdchar == 'O' ? 1 : 0)),
8013 (linenr_T)(curwin->w_cursor.lnum +
8014 (cap->cmdchar == 'o' ? 1 : 0))
8015 ) == OK
8016 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8017#ifdef FEAT_COMMENTS
8018 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8019#endif
8020 0, 0))
8021 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008022 /* When '#' is in 'cpoptions' ignore the count. */
8023 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8024 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8026 }
8027 }
8028}
8029
8030/*
8031 * "." command: redo last change.
8032 */
8033 static void
8034nv_dot(cap)
8035 cmdarg_T *cap;
8036{
8037 if (!checkclearopq(cap->oap))
8038 {
8039 /*
8040 * If "restart_edit" is TRUE, the last but one command is repeated
8041 * instead of the last command (inserting text). This is used for
8042 * CTRL-O <.> in insert mode.
8043 */
8044 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8045 clearopbeep(cap->oap);
8046 }
8047}
8048
8049/*
8050 * CTRL-R: undo undo
8051 */
8052 static void
8053nv_redo(cap)
8054 cmdarg_T *cap;
8055{
8056 if (!checkclearopq(cap->oap))
8057 {
8058 u_redo((int)cap->count1);
8059 curwin->w_set_curswant = TRUE;
8060 }
8061}
8062
8063/*
8064 * Handle "U" command.
8065 */
8066 static void
8067nv_Undo(cap)
8068 cmdarg_T *cap;
8069{
8070 /* In Visual mode and typing "gUU" triggers an operator */
8071 if (cap->oap->op_type == OP_UPPER
8072#ifdef FEAT_VISUAL
8073 || VIsual_active
8074#endif
8075 )
8076 {
8077 /* translate "gUU" to "gUgU" */
8078 cap->cmdchar = 'g';
8079 cap->nchar = 'U';
8080 nv_operator(cap);
8081 }
8082 else if (!checkclearopq(cap->oap))
8083 {
8084 u_undoline();
8085 curwin->w_set_curswant = TRUE;
8086 }
8087}
8088
8089/*
8090 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8091 * single character.
8092 */
8093 static void
8094nv_tilde(cap)
8095 cmdarg_T *cap;
8096{
8097 if (!p_to
8098#ifdef FEAT_VISUAL
8099 && !VIsual_active
8100#endif
8101 && cap->oap->op_type != OP_TILDE)
8102 n_swapchar(cap);
8103 else
8104 nv_operator(cap);
8105}
8106
8107/*
8108 * Handle an operator command.
8109 * The actual work is done by do_pending_operator().
8110 */
8111 static void
8112nv_operator(cap)
8113 cmdarg_T *cap;
8114{
8115 int op_type;
8116
8117 op_type = get_op_type(cap->cmdchar, cap->nchar);
8118
8119 if (op_type == cap->oap->op_type) /* double operator works on lines */
8120 nv_lineop(cap);
8121 else if (!checkclearop(cap->oap))
8122 {
8123 cap->oap->start = curwin->w_cursor;
8124 cap->oap->op_type = op_type;
8125 }
8126}
8127
8128/*
8129 * Handle linewise operator "dd", "yy", etc.
8130 *
8131 * "_" is is a strange motion command that helps make operators more logical.
8132 * It is actually implemented, but not documented in the real Vi. This motion
8133 * command actually refers to "the current line". Commands like "dd" and "yy"
8134 * are really an alternate form of "d_" and "y_". It does accept a count, so
8135 * "d3_" works to delete 3 lines.
8136 */
8137 static void
8138nv_lineop(cap)
8139 cmdarg_T *cap;
8140{
8141 cap->oap->motion_type = MLINE;
8142 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8143 clearopbeep(cap->oap);
8144 else if ( cap->oap->op_type == OP_DELETE
8145 || cap->oap->op_type == OP_LSHIFT
8146 || cap->oap->op_type == OP_RSHIFT)
8147 beginline(BL_SOL | BL_FIX);
8148 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8149 beginline(BL_WHITE | BL_FIX);
8150}
8151
8152/*
8153 * <Home> command.
8154 */
8155 static void
8156nv_home(cap)
8157 cmdarg_T *cap;
8158{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008159 /* CTRL-HOME is like "gg" */
8160 if (mod_mask & MOD_MASK_CTRL)
8161 nv_goto(cap);
8162 else
8163 {
8164 cap->count0 = 1;
8165 nv_pipe(cap);
8166 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008167 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8168 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171/*
8172 * "|" command.
8173 */
8174 static void
8175nv_pipe(cap)
8176 cmdarg_T *cap;
8177{
8178 cap->oap->motion_type = MCHAR;
8179 cap->oap->inclusive = FALSE;
8180 beginline(0);
8181 if (cap->count0 > 0)
8182 {
8183 coladvance((colnr_T)(cap->count0 - 1));
8184 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8185 }
8186 else
8187 curwin->w_curswant = 0;
8188 /* keep curswant at the column where we wanted to go, not where
8189 we ended; differs if line is too short */
8190 curwin->w_set_curswant = FALSE;
8191}
8192
8193/*
8194 * Handle back-word command "b" and "B".
8195 * cap->arg is 1 for "B"
8196 */
8197 static void
8198nv_bck_word(cap)
8199 cmdarg_T *cap;
8200{
8201 cap->oap->motion_type = MCHAR;
8202 cap->oap->inclusive = FALSE;
8203 curwin->w_set_curswant = TRUE;
8204 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8205 clearopbeep(cap->oap);
8206#ifdef FEAT_FOLDING
8207 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8208 foldOpenCursor();
8209#endif
8210}
8211
8212/*
8213 * Handle word motion commands "e", "E", "w" and "W".
8214 * cap->arg is TRUE for "E" and "W".
8215 */
8216 static void
8217nv_wordcmd(cap)
8218 cmdarg_T *cap;
8219{
8220 int n;
8221 int word_end;
8222 int flag = FALSE;
8223
8224 /*
8225 * Set inclusive for the "E" and "e" command.
8226 */
8227 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8228 word_end = TRUE;
8229 else
8230 word_end = FALSE;
8231 cap->oap->inclusive = word_end;
8232
8233 /*
8234 * "cw" and "cW" are a special case.
8235 */
8236 if (!word_end && cap->oap->op_type == OP_CHANGE)
8237 {
8238 n = gchar_cursor();
8239 if (n != NUL) /* not an empty line */
8240 {
8241 if (vim_iswhite(n))
8242 {
8243 /*
8244 * Reproduce a funny Vi behaviour: "cw" on a blank only
8245 * changes one character, not all blanks until the start of
8246 * the next word. Only do this when the 'w' flag is included
8247 * in 'cpoptions'.
8248 */
8249 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8250 {
8251 cap->oap->inclusive = TRUE;
8252 cap->oap->motion_type = MCHAR;
8253 return;
8254 }
8255 }
8256 else
8257 {
8258 /*
8259 * This is a little strange. To match what the real Vi does,
8260 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8261 * that we are not on a space or a TAB. This seems impolite
8262 * at first, but it's really more what we mean when we say
8263 * 'cw'.
8264 * Another strangeness: When standing on the end of a word
8265 * "ce" will change until the end of the next wordt, but "cw"
8266 * will change only one character! This is done by setting
8267 * flag.
8268 */
8269 cap->oap->inclusive = TRUE;
8270 word_end = TRUE;
8271 flag = TRUE;
8272 }
8273 }
8274 }
8275
8276 cap->oap->motion_type = MCHAR;
8277 curwin->w_set_curswant = TRUE;
8278 if (word_end)
8279 n = end_word(cap->count1, cap->arg, flag, FALSE);
8280 else
8281 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8282
8283 /* Don't leave the cursor on the NUL past a line */
8284 if (curwin->w_cursor.col && gchar_cursor() == NUL)
8285 {
8286 --curwin->w_cursor.col;
8287 cap->oap->inclusive = TRUE;
8288 }
8289
8290 if (n == FAIL && cap->oap->op_type == OP_NOP)
8291 clearopbeep(cap->oap);
8292 else
8293 {
8294#ifdef FEAT_VISUAL
8295 adjust_for_sel(cap);
8296#endif
8297#ifdef FEAT_FOLDING
8298 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8299 foldOpenCursor();
8300#endif
8301 }
8302}
8303
8304/*
8305 * "0" and "^" commands.
8306 * cap->arg is the argument for beginline().
8307 */
8308 static void
8309nv_beginline(cap)
8310 cmdarg_T *cap;
8311{
8312 cap->oap->motion_type = MCHAR;
8313 cap->oap->inclusive = FALSE;
8314 beginline(cap->arg);
8315#ifdef FEAT_FOLDING
8316 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8317 foldOpenCursor();
8318#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008319 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8320 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
8322
8323#ifdef FEAT_VISUAL
8324/*
8325 * In exclusive Visual mode, may include the last character.
8326 */
8327 static void
8328adjust_for_sel(cap)
8329 cmdarg_T *cap;
8330{
8331 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8332 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8333 {
8334# ifdef FEAT_MBYTE
8335 if (has_mbyte)
8336 inc_cursor();
8337 else
8338# endif
8339 ++curwin->w_cursor.col;
8340 cap->oap->inclusive = FALSE;
8341 }
8342}
8343
8344/*
8345 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8346 * Should check VIsual_mode before calling this.
8347 * Returns TRUE when backed up to the previous line.
8348 */
8349 static int
8350unadjust_for_sel()
8351{
8352 pos_T *pp;
8353
8354 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8355 {
8356 if (lt(VIsual, curwin->w_cursor))
8357 pp = &curwin->w_cursor;
8358 else
8359 pp = &VIsual;
8360#ifdef FEAT_VIRTUALEDIT
8361 if (pp->coladd > 0)
8362 --pp->coladd;
8363 else
8364#endif
8365 if (pp->col > 0)
8366 {
8367 --pp->col;
8368#ifdef FEAT_MBYTE
8369 mb_adjustpos(pp);
8370#endif
8371 }
8372 else if (pp->lnum > 1)
8373 {
8374 --pp->lnum;
8375 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8376 return TRUE;
8377 }
8378 }
8379 return FALSE;
8380}
8381
8382/*
8383 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8384 */
8385 static void
8386nv_select(cap)
8387 cmdarg_T *cap;
8388{
8389 if (VIsual_active)
8390 VIsual_select = TRUE;
8391 else if (VIsual_reselect)
8392 {
8393 cap->nchar = 'v'; /* fake "gv" command */
8394 cap->arg = TRUE;
8395 nv_g_cmd(cap);
8396 }
8397}
8398
8399#endif
8400
8401/*
8402 * "G", "gg", CTRL-END, CTRL-HOME.
8403 * cap->arg is TRUE for "G".
8404 */
8405 static void
8406nv_goto(cap)
8407 cmdarg_T *cap;
8408{
8409 linenr_T lnum;
8410
8411 if (cap->arg)
8412 lnum = curbuf->b_ml.ml_line_count;
8413 else
8414 lnum = 1L;
8415 cap->oap->motion_type = MLINE;
8416 setpcmark();
8417
8418 /* When a count is given, use it instead of the default lnum */
8419 if (cap->count0 != 0)
8420 lnum = cap->count0;
8421 if (lnum < 1L)
8422 lnum = 1L;
8423 else if (lnum > curbuf->b_ml.ml_line_count)
8424 lnum = curbuf->b_ml.ml_line_count;
8425 curwin->w_cursor.lnum = lnum;
8426 beginline(BL_SOL | BL_FIX);
8427#ifdef FEAT_FOLDING
8428 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8429 foldOpenCursor();
8430#endif
8431}
8432
8433/*
8434 * CTRL-\ in Normal mode.
8435 */
8436 static void
8437nv_normal(cap)
8438 cmdarg_T *cap;
8439{
8440 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8441 {
8442 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008443 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 clear_cmdline = TRUE; /* unshow mode later */
8445 restart_edit = 0;
8446#ifdef FEAT_CMDWIN
8447 if (cmdwin_type != 0)
8448 cmdwin_result = Ctrl_C;
8449#endif
8450#ifdef FEAT_VISUAL
8451 if (VIsual_active)
8452 {
8453 end_visual_mode(); /* stop Visual */
8454 redraw_curbuf_later(INVERTED);
8455 }
8456#endif
8457 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8458 if (cap->nchar == Ctrl_G && p_im)
8459 restart_edit = 'a';
8460 }
8461 else
8462 clearopbeep(cap->oap);
8463}
8464
8465/*
8466 * ESC in Normal mode: beep, but don't flush buffers.
8467 * Don't even beep if we are canceling a command.
8468 */
8469 static void
8470nv_esc(cap)
8471 cmdarg_T *cap;
8472{
8473 int no_reason;
8474
8475 no_reason = (cap->oap->op_type == OP_NOP
8476 && cap->opcount == 0
8477 && cap->count0 == 0
8478 && cap->oap->regname == 0
8479 && !p_im);
8480
8481 if (cap->arg) /* TRUE for CTRL-C */
8482 {
8483 if (restart_edit == 0
8484#ifdef FEAT_CMDWIN
8485 && cmdwin_type == 0
8486#endif
8487#ifdef FEAT_VISUAL
8488 && !VIsual_active
8489#endif
8490 && no_reason)
8491 MSG(_("Type :quit<Enter> to exit Vim"));
8492
8493 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8494 * set again below when halfway a mapping. */
8495 if (!p_im)
8496 restart_edit = 0;
8497#ifdef FEAT_CMDWIN
8498 if (cmdwin_type != 0)
8499 {
8500 cmdwin_result = K_IGNORE;
8501 got_int = FALSE; /* don't stop executing autocommands et al. */
8502 return;
8503 }
8504#endif
8505 }
8506
8507#ifdef FEAT_VISUAL
8508 if (VIsual_active)
8509 {
8510 end_visual_mode(); /* stop Visual */
8511 check_cursor_col(); /* make sure cursor is not beyond EOL */
8512 curwin->w_set_curswant = TRUE;
8513 redraw_curbuf_later(INVERTED);
8514 }
8515 else
8516#endif
8517 if (no_reason)
8518 vim_beep();
8519 clearop(cap->oap);
8520
8521 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8522 * set return to Insert mode afterwards. */
8523 if (restart_edit == 0 && goto_im()
8524#ifdef FEAT_EX_EXTRA
8525 && ex_normal_busy == 0
8526#endif
8527 )
8528 restart_edit = 'a';
8529}
8530
8531/*
8532 * Handle "A", "a", "I", "i" and <Insert> commands.
8533 */
8534 static void
8535nv_edit(cap)
8536 cmdarg_T *cap;
8537{
8538 /* <Insert> is equal to "i" */
8539 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8540 cap->cmdchar = 'i';
8541
8542#ifdef FEAT_VISUAL
8543 /* in Visual mode "A" and "I" are an operator */
8544 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8545 v_visop(cap);
8546
8547 /* in Visual mode and after an operator "a" and "i" are for text objects */
8548 else
8549#endif
8550 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8551 && (cap->oap->op_type != OP_NOP
8552#ifdef FEAT_VISUAL
8553 || VIsual_active
8554#endif
8555 ))
8556 {
8557#ifdef FEAT_TEXTOBJ
8558 nv_object(cap);
8559#else
8560 clearopbeep(cap->oap);
8561#endif
8562 }
8563 else if (!curbuf->b_p_ma && !p_im)
8564 {
8565 /* Only give this error when 'insertmode' is off. */
8566 EMSG(_(e_modifiable));
8567 clearop(cap->oap);
8568 }
8569 else if (!checkclearopq(cap->oap))
8570 {
8571 switch (cap->cmdchar)
8572 {
8573 case 'A': /* "A"ppend after the line */
8574 curwin->w_set_curswant = TRUE;
8575#ifdef FEAT_VIRTUALEDIT
8576 if (ve_flags == VE_ALL)
8577 {
8578 int save_State = State;
8579
8580 /* Pretent Insert mode here to allow the cursor on the
8581 * character past the end of the line */
8582 State = INSERT;
8583 coladvance((colnr_T)MAXCOL);
8584 State = save_State;
8585 }
8586 else
8587#endif
8588 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8589 break;
8590
8591 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008592 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8593 beginline(BL_WHITE);
8594 else
8595 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 break;
8597
8598 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8599#ifdef FEAT_VIRTUALEDIT
8600 /* increment coladd when in virtual space, increment the
8601 * column otherwise, also to append after an unprintable char */
8602 if (virtual_active()
8603 && (curwin->w_cursor.coladd > 0
8604 || *ml_get_cursor() == NUL
8605 || *ml_get_cursor() == TAB))
8606 curwin->w_cursor.coladd++;
8607 else
8608#endif
8609 if (*ml_get_cursor() != NUL)
8610 inc_cursor();
8611 break;
8612 }
8613
8614#ifdef FEAT_VIRTUALEDIT
8615 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8616 {
8617 int save_State = State;
8618
8619 /* Pretent Insert mode here to allow the cursor on the
8620 * character past the end of the line */
8621 State = INSERT;
8622 coladvance(getviscol());
8623 State = save_State;
8624 }
8625#endif
8626
8627 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8628 }
8629}
8630
8631/*
8632 * Invoke edit() and take care of "restart_edit" and the return value.
8633 */
8634 static void
8635invoke_edit(cap, repl, cmd, startln)
8636 cmdarg_T *cap;
8637 int repl; /* "r" or "gr" command */
8638 int cmd;
8639 int startln;
8640{
8641 int restart_edit_save = 0;
8642
8643 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8644 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8645 * it. */
8646 if (repl || !stuff_empty())
8647 restart_edit_save = restart_edit;
8648 else
8649 restart_edit_save = 0;
8650
8651 /* Always reset "restart_edit", this is not a restarted edit. */
8652 restart_edit = 0;
8653
8654 if (edit(cmd, startln, cap->count1))
8655 cap->retval |= CA_COMMAND_BUSY;
8656
8657 if (restart_edit == 0)
8658 restart_edit = restart_edit_save;
8659}
8660
8661#ifdef FEAT_TEXTOBJ
8662/*
8663 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8664 */
8665 static void
8666nv_object(cap)
8667 cmdarg_T *cap;
8668{
8669 int flag;
8670 int include;
8671 char_u *mps_save;
8672
8673 if (cap->cmdchar == 'i')
8674 include = FALSE; /* "ix" = inner object: exclude white space */
8675 else
8676 include = TRUE; /* "ax" = an object: include white space */
8677
8678 /* Make sure (), [], {} and <> are in 'matchpairs' */
8679 mps_save = curbuf->b_p_mps;
8680 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8681
8682 switch (cap->nchar)
8683 {
8684 case 'w': /* "aw" = a word */
8685 flag = current_word(cap->oap, cap->count1, include, FALSE);
8686 break;
8687 case 'W': /* "aW" = a WORD */
8688 flag = current_word(cap->oap, cap->count1, include, TRUE);
8689 break;
8690 case 'b': /* "ab" = a braces block */
8691 case '(':
8692 case ')':
8693 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8694 break;
8695 case 'B': /* "aB" = a Brackets block */
8696 case '{':
8697 case '}':
8698 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8699 break;
8700 case '[': /* "a[" = a [] block */
8701 case ']':
8702 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8703 break;
8704 case '<': /* "a<" = a <> block */
8705 case '>':
8706 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8707 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008708 case 't': /* "at" = a tag block (xml and html) */
8709 flag = current_tagblock(cap->oap, cap->count1, include);
8710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 case 'p': /* "ap" = a paragraph */
8712 flag = current_par(cap->oap, cap->count1, include, 'p');
8713 break;
8714 case 's': /* "as" = a sentence */
8715 flag = current_sent(cap->oap, cap->count1, include);
8716 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008717 case '"': /* "a"" = a double quoted string */
8718 case '\'': /* "a'" = a single quoted string */
8719 case '`': /* "a`" = a backtick quoted string */
8720 flag = current_quote(cap->oap, cap->count1, include,
8721 cap->nchar);
8722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#if 0 /* TODO */
8724 case 'S': /* "aS" = a section */
8725 case 'f': /* "af" = a filename */
8726 case 'u': /* "au" = a URL */
8727#endif
8728 default:
8729 flag = FAIL;
8730 break;
8731 }
8732
8733 curbuf->b_p_mps = mps_save;
8734 if (flag == FAIL)
8735 clearopbeep(cap->oap);
8736 adjust_cursor_col();
8737 curwin->w_set_curswant = TRUE;
8738}
8739#endif
8740
8741/*
8742 * "q" command: Start/stop recording.
8743 * "q:", "q/", "q?": edit command-line in command-line window.
8744 */
8745 static void
8746nv_record(cap)
8747 cmdarg_T *cap;
8748{
8749 if (cap->oap->op_type == OP_FORMAT)
8750 {
8751 /* "gqq" is the same as "gqgq": format line */
8752 cap->cmdchar = 'g';
8753 cap->nchar = 'q';
8754 nv_operator(cap);
8755 }
8756 else if (!checkclearop(cap->oap))
8757 {
8758#ifdef FEAT_CMDWIN
8759 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8760 {
8761 stuffcharReadbuff(cap->nchar);
8762 stuffcharReadbuff(K_CMDWIN);
8763 }
8764 else
8765#endif
8766 /* (stop) recording into a named register, unless executing a
8767 * register */
8768 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8769 clearopbeep(cap->oap);
8770 }
8771}
8772
8773/*
8774 * Handle the "@r" command.
8775 */
8776 static void
8777nv_at(cap)
8778 cmdarg_T *cap;
8779{
8780 if (checkclearop(cap->oap))
8781 return;
8782#ifdef FEAT_EVAL
8783 if (cap->nchar == '=')
8784 {
8785 if (get_expr_register() == NUL)
8786 return;
8787 }
8788#endif
8789 while (cap->count1-- && !got_int)
8790 {
8791 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8792 {
8793 clearopbeep(cap->oap);
8794 break;
8795 }
8796 line_breakcheck();
8797 }
8798}
8799
8800/*
8801 * Handle the CTRL-U and CTRL-D commands.
8802 */
8803 static void
8804nv_halfpage(cap)
8805 cmdarg_T *cap;
8806{
8807 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8808 || (cap->cmdchar == Ctrl_D
8809 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8810 clearopbeep(cap->oap);
8811 else if (!checkclearop(cap->oap))
8812 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8813}
8814
8815/*
8816 * Handle "J" or "gJ" command.
8817 */
8818 static void
8819nv_join(cap)
8820 cmdarg_T *cap;
8821{
8822#ifdef FEAT_VISUAL
8823 if (VIsual_active) /* join the visual lines */
8824 nv_operator(cap);
8825 else
8826#endif
8827 if (!checkclearop(cap->oap))
8828 {
8829 if (cap->count0 <= 1)
8830 cap->count0 = 2; /* default for join is two lines! */
8831 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8832 curbuf->b_ml.ml_line_count)
8833 clearopbeep(cap->oap); /* beyond last line */
8834 else
8835 {
8836 prep_redo(cap->oap->regname, cap->count0,
8837 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8838 do_do_join(cap->count0, cap->nchar == NUL);
8839 }
8840 }
8841}
8842
8843/*
8844 * "P", "gP", "p" and "gp" commands.
8845 */
8846 static void
8847nv_put(cap)
8848 cmdarg_T *cap;
8849{
8850#ifdef FEAT_VISUAL
8851 int regname = 0;
8852 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008853 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008854 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855#endif
8856 int dir;
8857 int flags = 0;
8858
8859 if (cap->oap->op_type != OP_NOP)
8860 {
8861#ifdef FEAT_DIFF
8862 /* "dp" is ":diffput" */
8863 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8864 {
8865 clearop(cap->oap);
8866 nv_diffgetput(TRUE);
8867 }
8868 else
8869#endif
8870 clearopbeep(cap->oap);
8871 }
8872 else
8873 {
8874 dir = (cap->cmdchar == 'P'
8875 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8876 ? BACKWARD : FORWARD;
8877 prep_redo_cmd(cap);
8878 if (cap->cmdchar == 'g')
8879 flags |= PUT_CURSEND;
8880
8881#ifdef FEAT_VISUAL
8882 if (VIsual_active)
8883 {
8884 /* Putting in Visual mode: The put text replaces the selected
8885 * text. First delete the selected text, then put the new text.
8886 * Need to save and restore the registers that the delete
8887 * overwrites if the old contents is being put.
8888 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008889 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 regname = cap->oap->regname;
8891# ifdef FEAT_CLIPBOARD
8892 adjust_clip_reg(&regname);
8893# endif
8894 if (regname == 0 || VIM_ISDIGIT(regname)
8895# ifdef FEAT_CLIPBOARD
8896 || (clip_unnamed && (regname == '*' || regname == '+'))
8897# endif
8898
8899 )
8900 {
8901 /* the delete is going to overwrite the register we want to
8902 * put, save it first. */
8903 reg1 = get_register(regname, TRUE);
8904 }
8905
8906 /* Now delete the selected text. */
8907 cap->cmdchar = 'd';
8908 cap->nchar = NUL;
8909 cap->oap->regname = NUL;
8910 nv_operator(cap);
8911 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008912 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
8914 /* delete PUT_LINE_BACKWARD; */
8915 cap->oap->regname = regname;
8916
8917 if (reg1 != NULL)
8918 {
8919 /* Delete probably changed the register we want to put, save
8920 * it first. Then put back what was there before the delete. */
8921 reg2 = get_register(regname, FALSE);
8922 put_register(regname, reg1);
8923 }
8924
8925 /* When deleted a linewise Visual area, put the register as
8926 * lines to avoid it joined with the next line. When deletion was
8927 * characterwise, split a line when putting lines. */
8928 if (VIsual_mode == 'V')
8929 flags |= PUT_LINE;
8930 else if (VIsual_mode == 'v')
8931 flags |= PUT_LINE_SPLIT;
8932 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8933 flags |= PUT_LINE_FORWARD;
8934 dir = BACKWARD;
8935 if ((VIsual_mode != 'V'
8936 && curwin->w_cursor.col < curbuf->b_op_start.col)
8937 || (VIsual_mode == 'V'
8938 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8939 /* cursor is at the end of the line or end of file, put
8940 * forward. */
8941 dir = FORWARD;
8942 }
8943#endif
8944 do_put(cap->oap->regname, dir, cap->count1, flags);
8945
8946#ifdef FEAT_VISUAL
8947 /* If a register was saved, put it back now. */
8948 if (reg2 != NULL)
8949 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008950
8951 /* What to reselect with "gv"? Selecting the just put text seems to
8952 * be the most useful, since the original text was removed. */
8953 if (was_visual)
8954 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008955 curbuf->b_visual.vi_start = curbuf->b_op_start;
8956 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008957 }
8958
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008959 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008960 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008961 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8962 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963#endif
8964 auto_format(FALSE, TRUE);
8965 }
8966}
8967
8968/*
8969 * "o" and "O" commands.
8970 */
8971 static void
8972nv_open(cap)
8973 cmdarg_T *cap;
8974{
8975#ifdef FEAT_DIFF
8976 /* "do" is ":diffget" */
8977 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8978 {
8979 clearop(cap->oap);
8980 nv_diffgetput(FALSE);
8981 }
8982 else
8983#endif
8984#ifdef FEAT_VISUAL
8985 if (VIsual_active) /* switch start and end of visual */
8986 v_swap_corners(cap->cmdchar);
8987 else
8988#endif
8989 n_opencmd(cap);
8990}
8991
8992#ifdef FEAT_SNIFF
8993/*ARGSUSED*/
8994 static void
8995nv_sniff(cap)
8996 cmdarg_T *cap;
8997{
8998 ProcessSniffRequests();
8999}
9000#endif
9001
9002#ifdef FEAT_NETBEANS_INTG
9003 static void
9004nv_nbcmd(cap)
9005 cmdarg_T *cap;
9006{
9007 netbeans_keycommand(cap->nchar);
9008}
9009#endif
9010
9011#ifdef FEAT_DND
9012/*ARGSUSED*/
9013 static void
9014nv_drop(cap)
9015 cmdarg_T *cap;
9016{
9017 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9018}
9019#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009020
9021#ifdef FEAT_AUTOCMD
9022/*
9023 * Trigger CursorHold event.
9024 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9025 * input buffer. "did_cursorhold" is set to avoid retriggering.
9026 */
9027/*ARGSUSED*/
9028 static void
9029nv_cursorhold(cap)
9030 cmdarg_T *cap;
9031{
9032 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9033 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009034 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009035}
9036#endif