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