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