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