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