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