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