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