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