blob: 9ad32f7d8654f286f552b83892229120b272859f [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 {
1911 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1912 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 */
2284#ifdef FEAT_VERTSPLIT
2285 int in_sep_line; /* mouse in vertical separator line */
2286#endif
2287 int c1, c2;
2288#if defined(FEAT_FOLDING)
2289 pos_T save_cursor;
2290#endif
2291 win_T *old_curwin = curwin;
2292#ifdef FEAT_VISUAL
2293 static pos_T orig_cursor;
2294 colnr_T leftcol, rightcol;
2295 pos_T end_visual;
2296 int diff;
2297 int old_active = VIsual_active;
2298 int old_mode = VIsual_mode;
2299#endif
2300 int regname;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002301#ifdef FEAT_CONCEAL
2302 linenr_T oldline = curwin->w_cursor.lnum;
2303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
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 */
2364 got_click = FALSE;
2365 }
2366
Bram Moolenaar64969662005-12-14 21:59:55 +00002367#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002369 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 */
2371 if ((mod_mask & MOD_MASK_ALT))
2372 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /*
2376 * CTRL right mouse button does CTRL-T
2377 */
2378 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2379 {
2380 if (State & INSERT)
2381 stuffcharReadbuff(Ctrl_O);
2382 if (count > 1)
2383 stuffnumReadbuff(count);
2384 stuffcharReadbuff(Ctrl_T);
2385 got_click = FALSE; /* ignore drag&release now */
2386 return FALSE;
2387 }
2388
2389 /*
2390 * CTRL only works with left mouse button
2391 */
2392 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2393 return FALSE;
2394
2395 /*
2396 * When a modifier is down, ignore drag and release events, as well as
2397 * multiple clicks and the middle mouse button.
2398 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2399 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002400 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2401 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 && (!is_click
2403 || (mod_mask & MOD_MASK_MULTI_CLICK)
2404 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002405 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 && mouse_model_popup()
2407 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002408 && !((mod_mask & MOD_MASK_ALT)
2409 && !mouse_model_popup()
2410 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 )
2412 return FALSE;
2413
2414 /*
2415 * If the button press was used as the movement command for an operator
2416 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2417 * drag/release events.
2418 */
2419 if (!is_click && which_button == MOUSE_MIDDLE)
2420 return FALSE;
2421
2422 if (oap != NULL)
2423 regname = oap->regname;
2424 else
2425 regname = 0;
2426
2427 /*
2428 * Middle mouse button does a 'put' of the selected text
2429 */
2430 if (which_button == MOUSE_MIDDLE)
2431 {
2432 if (State == NORMAL)
2433 {
2434 /*
2435 * If an operator was pending, we don't know what the user wanted
2436 * to do. Go back to normal mode: Clear the operator and beep().
2437 */
2438 if (oap != NULL && oap->op_type != OP_NOP)
2439 {
2440 clearopbeep(oap);
2441 return FALSE;
2442 }
2443
2444#ifdef FEAT_VISUAL
2445 /*
2446 * If visual was active, yank the highlighted text and put it
2447 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002448 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 */
2450 if (VIsual_active)
2451 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002452 if (VIsual_select)
2453 {
2454 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002455 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002456 }
2457 else
2458 {
2459 stuffcharReadbuff('y');
2460 stuffcharReadbuff(K_MIDDLEMOUSE);
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 do_always = TRUE; /* ignore 'mouse' setting next time */
2463 return FALSE;
2464 }
2465#endif
2466 /*
2467 * The rest is below jump_to_mouse()
2468 */
2469 }
2470
2471 else if ((State & INSERT) == 0)
2472 return FALSE;
2473
2474 /*
2475 * Middle click in insert mode doesn't move the mouse, just insert the
2476 * contents of a register. '.' register is special, can't insert that
2477 * with do_put().
2478 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2479 * happens for the GUI).
2480 */
2481 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2482 {
2483 if (regname == '.')
2484 insert_reg(regname, TRUE);
2485 else
2486 {
2487#ifdef FEAT_CLIPBOARD
2488 if (clip_star.available && regname == 0)
2489 regname = '*';
2490#endif
2491 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2492 insert_reg(regname, TRUE);
2493 else
2494 {
2495 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2496
2497 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2498 AppendCharToRedobuff(Ctrl_R);
2499 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2500 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2501 }
2502 }
2503 return FALSE;
2504 }
2505 }
2506
2507 /* When dragging or button-up stay in the same window. */
2508 if (!is_click)
2509 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2510
2511 start_visual.lnum = 0;
2512
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513#ifdef FEAT_WINDOWS
2514 /* Check for clicking in the tab page line. */
2515 if (mouse_row == 0 && firstwin->w_winrow > 0)
2516 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002517 if (is_drag)
2518 return FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519 got_click = FALSE; /* ignore mouse-up and drag events */
2520
Bram Moolenaarf740b292006-02-16 22:11:02 +00002521 /* click in a tab selects that tab page */
2522 if (is_click
2523# ifdef FEAT_CMDWIN
2524 && cmdwin_type == 0
2525# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002526 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002527 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002528 c1 = TabPageIdxs[mouse_col];
2529 if (c1 >= 0)
2530 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002531 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2532 {
2533 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002535 tabpage_new();
2536 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2537 }
2538 else
2539 {
2540 /* Go to specified tab page, or next one if not clicking
2541 * on a label. */
2542 goto_tabpage(c1);
2543
2544 /* It's like clicking on the status line of a window. */
2545 if (curwin != old_curwin)
2546 end_visual_mode();
2547 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002548 }
2549 else if (c1 < 0)
2550 {
2551 tabpage_T *tp;
2552
2553 /* Close the current or specified tab page. */
2554 if (c1 == -999)
2555 tp = curtab;
2556 else
2557 tp = find_tabpage(-c1);
2558 if (tp == curtab)
2559 {
2560 if (first_tabpage->tp_next != NULL)
2561 tabpage_close(FALSE);
2562 }
2563 else if (tp != NULL)
2564 tabpage_close_other(tp, FALSE);
2565 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 }
2569#endif
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 /*
2572 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2573 * right button up -> pop-up menu
2574 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002575 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 */
2577 if (mouse_model_popup())
2578 {
2579 if (which_button == MOUSE_RIGHT
2580 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2581 {
2582 /*
2583 * NOTE: Ignore right button down and drag mouse events.
2584 * Windows only shows the popup menu on the button up event.
2585 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002586#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2587 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 if (!is_click)
2589 return FALSE;
2590#endif
2591#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2592 if (is_click || is_drag)
2593 return FALSE;
2594#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002595#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2597 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2598 if (gui.in_use)
2599 {
2600 jump_flags = 0;
2601 if (STRCMP(p_mousem, "popup_setpos") == 0)
2602 {
2603 /* First set the cursor position before showing the popup
2604 * menu. */
2605#ifdef FEAT_VISUAL
2606 if (VIsual_active)
2607 {
2608 pos_T m_pos;
2609
2610 /*
2611 * set MOUSE_MAY_STOP_VIS if we are outside the
2612 * selection or the current window (might have false
2613 * negative here)
2614 */
2615 if (mouse_row < W_WINROW(curwin)
2616 || mouse_row
2617 > (W_WINROW(curwin) + curwin->w_height))
2618 jump_flags = MOUSE_MAY_STOP_VIS;
2619 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2620 jump_flags = MOUSE_MAY_STOP_VIS;
2621 else
2622 {
2623 if ((lt(curwin->w_cursor, VIsual)
2624 && (lt(m_pos, curwin->w_cursor)
2625 || lt(VIsual, m_pos)))
2626 || (lt(VIsual, curwin->w_cursor)
2627 && (lt(m_pos, VIsual)
2628 || lt(curwin->w_cursor, m_pos))))
2629 {
2630 jump_flags = MOUSE_MAY_STOP_VIS;
2631 }
2632 else if (VIsual_mode == Ctrl_V)
2633 {
2634 getvcols(curwin, &curwin->w_cursor, &VIsual,
2635 &leftcol, &rightcol);
2636 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2637 if (m_pos.col < leftcol || m_pos.col > rightcol)
2638 jump_flags = MOUSE_MAY_STOP_VIS;
2639 }
2640 }
2641 }
2642 else
2643 jump_flags = MOUSE_MAY_STOP_VIS;
2644#endif
2645 }
2646 if (jump_flags)
2647 {
2648 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2649 update_curbuf(
2650#ifdef FEAT_VISUAL
2651 VIsual_active ? INVERTED :
2652#endif
2653 VALID);
2654 setcursor();
2655 out_flush(); /* Update before showing popup menu */
2656 }
2657# ifdef FEAT_MENU
2658 gui_show_popupmenu();
2659# endif
2660 return (jump_flags & CURSOR_MOVED) != 0;
2661 }
2662 else
2663 return FALSE;
2664#else
2665 return FALSE;
2666#endif
2667 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002668 if (which_button == MOUSE_LEFT
2669 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
2671 which_button = MOUSE_RIGHT;
2672 mod_mask &= ~MOD_MASK_SHIFT;
2673 }
2674 }
2675
2676#ifdef FEAT_VISUAL
2677 if ((State & (NORMAL | INSERT))
2678 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2679 {
2680 if (which_button == MOUSE_LEFT)
2681 {
2682 if (is_click)
2683 {
2684 /* stop Visual mode for a left click in a window, but not when
2685 * on a status line */
2686 if (VIsual_active)
2687 jump_flags |= MOUSE_MAY_STOP_VIS;
2688 }
2689 else if (mouse_has(MOUSE_VISUAL))
2690 jump_flags |= MOUSE_MAY_VIS;
2691 }
2692 else if (which_button == MOUSE_RIGHT)
2693 {
2694 if (is_click && VIsual_active)
2695 {
2696 /*
2697 * Remember the start and end of visual before moving the
2698 * cursor.
2699 */
2700 if (lt(curwin->w_cursor, VIsual))
2701 {
2702 start_visual = curwin->w_cursor;
2703 end_visual = VIsual;
2704 }
2705 else
2706 {
2707 start_visual = VIsual;
2708 end_visual = curwin->w_cursor;
2709 }
2710 }
2711 jump_flags |= MOUSE_FOCUS;
2712 if (mouse_has(MOUSE_VISUAL))
2713 jump_flags |= MOUSE_MAY_VIS;
2714 }
2715 }
2716#endif
2717
2718 /*
2719 * If an operator is pending, ignore all drags and releases until the
2720 * next mouse click.
2721 */
2722 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2723 {
2724 got_click = FALSE;
2725 oap->motion_type = MCHAR;
2726 }
2727
2728 /* When releasing the button let jump_to_mouse() know. */
2729 if (!is_click && !is_drag)
2730 jump_flags |= MOUSE_RELEASED;
2731
2732 /*
2733 * JUMP!
2734 */
2735 jump_flags = jump_to_mouse(jump_flags,
2736 oap == NULL ? NULL : &(oap->inclusive), which_button);
2737 moved = (jump_flags & CURSOR_MOVED);
2738 in_status_line = (jump_flags & IN_STATUS_LINE);
2739#ifdef FEAT_VERTSPLIT
2740 in_sep_line = (jump_flags & IN_SEP_LINE);
2741#endif
2742
2743#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002744 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2746 {
2747 int key = KEY2TERMCAP1(c);
2748
2749 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2750 || key == (int)KE_RIGHTRELEASE)
2751 netbeans_button_release(which_button);
2752 }
2753#endif
2754
2755 /* When jumping to another window, clear a pending operator. That's a bit
2756 * friendlier than beeping and not jumping to that window. */
2757 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2758 clearop(oap);
2759
2760#ifdef FEAT_FOLDING
2761 if (mod_mask == 0
2762 && !is_drag
2763 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2764 && which_button == MOUSE_LEFT)
2765 {
2766 /* open or close a fold at this line */
2767 if (jump_flags & MOUSE_FOLD_OPEN)
2768 openFold(curwin->w_cursor.lnum, 1L);
2769 else
2770 closeFold(curwin->w_cursor.lnum, 1L);
2771 /* don't move the cursor if still in the same window */
2772 if (curwin == old_curwin)
2773 curwin->w_cursor = save_cursor;
2774 }
2775#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02002776#ifdef FEAT_CONCEAL
2777 if (curwin->w_p_conceal && moved
2778 && (old_curwin != curwin || oldline != curwin->w_cursor.lnum))
2779 {
2780 update_single_line(old_curwin, oldline);
2781 update_single_line(curwin, curwin->w_cursor.lnum);
2782 }
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2786 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2787 {
2788 clip_modeless(which_button, is_click, is_drag);
2789 return FALSE;
2790 }
2791#endif
2792
2793#ifdef FEAT_VISUAL
2794 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002795 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (VIsual_active && is_drag && p_so)
2797 {
2798 /* In the very first line, allow scrolling one line */
2799 if (mouse_row == 0)
2800 mouse_dragging = 2;
2801 else
2802 mouse_dragging = 1;
2803 }
2804
2805 /* When dragging the mouse above the window, scroll down. */
2806 if (is_drag && mouse_row < 0 && !in_status_line)
2807 {
2808 scroll_redraw(FALSE, 1L);
2809 mouse_row = 0;
2810 }
2811
2812 if (start_visual.lnum) /* right click in visual mode */
2813 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002814 /* When ALT is pressed make Visual mode blockwise. */
2815 if (mod_mask & MOD_MASK_ALT)
2816 VIsual_mode = Ctrl_V;
2817
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 /*
2819 * In Visual-block mode, divide the area in four, pick up the corner
2820 * that is in the quarter that the cursor is in.
2821 */
2822 if (VIsual_mode == Ctrl_V)
2823 {
2824 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2825 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2826 end_visual.col = leftcol;
2827 else
2828 end_visual.col = rightcol;
2829 if (curwin->w_cursor.lnum <
2830 (start_visual.lnum + end_visual.lnum) / 2)
2831 end_visual.lnum = end_visual.lnum;
2832 else
2833 end_visual.lnum = start_visual.lnum;
2834
2835 /* move VIsual to the right column */
2836 start_visual = curwin->w_cursor; /* save the cursor pos */
2837 curwin->w_cursor = end_visual;
2838 coladvance(end_visual.col);
2839 VIsual = curwin->w_cursor;
2840 curwin->w_cursor = start_visual; /* restore the cursor */
2841 }
2842 else
2843 {
2844 /*
2845 * If the click is before the start of visual, change the start.
2846 * If the click is after the end of visual, change the end. If
2847 * the click is inside the visual, change the closest side.
2848 */
2849 if (lt(curwin->w_cursor, start_visual))
2850 VIsual = end_visual;
2851 else if (lt(end_visual, curwin->w_cursor))
2852 VIsual = start_visual;
2853 else
2854 {
2855 /* In the same line, compare column number */
2856 if (end_visual.lnum == start_visual.lnum)
2857 {
2858 if (curwin->w_cursor.col - start_visual.col >
2859 end_visual.col - curwin->w_cursor.col)
2860 VIsual = start_visual;
2861 else
2862 VIsual = end_visual;
2863 }
2864
2865 /* In different lines, compare line number */
2866 else
2867 {
2868 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2869 (end_visual.lnum - curwin->w_cursor.lnum);
2870
2871 if (diff > 0) /* closest to end */
2872 VIsual = start_visual;
2873 else if (diff < 0) /* closest to start */
2874 VIsual = end_visual;
2875 else /* in the middle line */
2876 {
2877 if (curwin->w_cursor.col <
2878 (start_visual.col + end_visual.col) / 2)
2879 VIsual = end_visual;
2880 else
2881 VIsual = start_visual;
2882 }
2883 }
2884 }
2885 }
2886 }
2887 /*
2888 * If Visual mode started in insert mode, execute "CTRL-O"
2889 */
2890 else if ((State & INSERT) && VIsual_active)
2891 stuffcharReadbuff(Ctrl_O);
2892#endif
2893
2894 /*
2895 * Middle mouse click: Put text before cursor.
2896 */
2897 if (which_button == MOUSE_MIDDLE)
2898 {
2899#ifdef FEAT_CLIPBOARD
2900 if (clip_star.available && regname == 0)
2901 regname = '*';
2902#endif
2903 if (yank_register_mline(regname))
2904 {
2905 if (mouse_past_bottom)
2906 dir = FORWARD;
2907 }
2908 else if (mouse_past_eol)
2909 dir = FORWARD;
2910
2911 if (fixindent)
2912 {
2913 c1 = (dir == BACKWARD) ? '[' : ']';
2914 c2 = 'p';
2915 }
2916 else
2917 {
2918 c1 = (dir == FORWARD) ? 'p' : 'P';
2919 c2 = NUL;
2920 }
2921 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2922
2923 /*
2924 * Remember where the paste started, so in edit() Insstart can be set
2925 * to this position
2926 */
2927 if (restart_edit != 0)
2928 where_paste_started = curwin->w_cursor;
2929 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2930 }
2931
2932#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2933 /*
2934 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2935 * error under the mouse pointer.
2936 */
2937 else if (((mod_mask & MOD_MASK_CTRL)
2938 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2939 && bt_quickfix(curbuf))
2940 {
2941 if (State & INSERT)
2942 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002943 if (curwin->w_llist_ref == NULL) /* quickfix window */
2944 stuffReadbuff((char_u *)":.cc\n");
2945 else /* location list window */
2946 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 got_click = FALSE; /* ignore drag&release now */
2948 }
2949#endif
2950
2951 /*
2952 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2953 * under the mouse pointer.
2954 */
2955 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2956 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2957 {
2958 if (State & INSERT)
2959 stuffcharReadbuff(Ctrl_O);
2960 stuffcharReadbuff(Ctrl_RSB);
2961 got_click = FALSE; /* ignore drag&release now */
2962 }
2963
2964 /*
2965 * Shift-Mouse click searches for the next occurrence of the word under
2966 * the mouse pointer
2967 */
2968 else if ((mod_mask & MOD_MASK_SHIFT))
2969 {
2970 if (State & INSERT
2971#ifdef FEAT_VISUAL
2972 || (VIsual_active && VIsual_select)
2973#endif
2974 )
2975 stuffcharReadbuff(Ctrl_O);
2976 if (which_button == MOUSE_LEFT)
2977 stuffcharReadbuff('*');
2978 else /* MOUSE_RIGHT */
2979 stuffcharReadbuff('#');
2980 }
2981
2982 /* Handle double clicks, unless on status line */
2983 else if (in_status_line)
2984 {
2985#ifdef FEAT_MOUSESHAPE
2986 if ((is_drag || is_click) && !drag_status_line)
2987 {
2988 drag_status_line = TRUE;
2989 update_mouseshape(-1);
2990 }
2991#endif
2992 }
2993#ifdef FEAT_VERTSPLIT
2994 else if (in_sep_line)
2995 {
2996# ifdef FEAT_MOUSESHAPE
2997 if ((is_drag || is_click) && !drag_sep_line)
2998 {
2999 drag_sep_line = TRUE;
3000 update_mouseshape(-1);
3001 }
3002# endif
3003 }
3004#endif
3005#ifdef FEAT_VISUAL
3006 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3007 && mouse_has(MOUSE_VISUAL))
3008 {
3009 if (is_click || !VIsual_active)
3010 {
3011 if (VIsual_active)
3012 orig_cursor = VIsual;
3013 else
3014 {
3015 check_visual_highlight();
3016 VIsual = curwin->w_cursor;
3017 orig_cursor = VIsual;
3018 VIsual_active = TRUE;
3019 VIsual_reselect = TRUE;
3020 /* start Select mode if 'selectmode' contains "mouse" */
3021 may_start_select('o');
3022 setmouse();
3023 }
3024 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003025 {
3026 /* Double click with ALT pressed makes it blockwise. */
3027 if (mod_mask & MOD_MASK_ALT)
3028 VIsual_mode = Ctrl_V;
3029 else
3030 VIsual_mode = 'v';
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3033 VIsual_mode = 'V';
3034 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3035 VIsual_mode = Ctrl_V;
3036#ifdef FEAT_CLIPBOARD
3037 /* Make sure the clipboard gets updated. Needed because start and
3038 * end may still be the same, and the selection needs to be owned */
3039 clip_star.vmode = NUL;
3040#endif
3041 }
3042 /*
3043 * A double click selects a word or a block.
3044 */
3045 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3046 {
3047 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003048 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050 if (is_click)
3051 {
3052 /* If the character under the cursor (skipping white space) is
3053 * not a word character, try finding a match and select a (),
3054 * {}, [], #if/#endif, etc. block. */
3055 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003056 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 inc(&end_visual);
3058 if (oap != NULL)
3059 oap->motion_type = MCHAR;
3060 if (oap != NULL
3061 && VIsual_mode == 'v'
3062 && !vim_iswordc(gchar_pos(&end_visual))
3063 && equalpos(curwin->w_cursor, VIsual)
3064 && (pos = findmatch(oap, NUL)) != NULL)
3065 {
3066 curwin->w_cursor = *pos;
3067 if (oap->motion_type == MLINE)
3068 VIsual_mode = 'V';
3069 else if (*p_sel == 'e')
3070 {
3071 if (lt(curwin->w_cursor, VIsual))
3072 ++VIsual.col;
3073 else
3074 ++curwin->w_cursor.col;
3075 }
3076 }
3077 }
3078
3079 if (pos == NULL && (is_click || is_drag))
3080 {
3081 /* When not found a match or when dragging: extend to include
3082 * a word. */
3083 if (lt(curwin->w_cursor, orig_cursor))
3084 {
3085 find_start_of_word(&curwin->w_cursor);
3086 find_end_of_word(&VIsual);
3087 }
3088 else
3089 {
3090 find_start_of_word(&VIsual);
3091 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3092#ifdef FEAT_MBYTE
3093 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095#else
3096 ++curwin->w_cursor.col;
3097#endif
3098 find_end_of_word(&curwin->w_cursor);
3099 }
3100 }
3101 curwin->w_set_curswant = TRUE;
3102 }
3103 if (is_click)
3104 redraw_curbuf_later(INVERTED); /* update the inversion */
3105 }
3106 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003107 {
3108 if (mod_mask & MOD_MASK_ALT)
3109 VIsual_mode = Ctrl_V;
3110 else
3111 VIsual_mode = 'v';
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003115 if ((!VIsual_active && old_active && mode_displayed)
3116 || (VIsual_active && p_smd && msg_silent == 0
3117 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 redraw_cmdline = TRUE;
3119#endif
3120
3121 return moved;
3122}
3123
3124#ifdef FEAT_VISUAL
3125/*
3126 * Move "pos" back to the start of the word it's in.
3127 */
3128 static void
3129find_start_of_word(pos)
3130 pos_T *pos;
3131{
3132 char_u *line;
3133 int cclass;
3134 int col;
3135
3136 line = ml_get(pos->lnum);
3137 cclass = get_mouse_class(line + pos->col);
3138
3139 while (pos->col > 0)
3140 {
3141 col = pos->col - 1;
3142#ifdef FEAT_MBYTE
3143 col -= (*mb_head_off)(line, line + col);
3144#endif
3145 if (get_mouse_class(line + col) != cclass)
3146 break;
3147 pos->col = col;
3148 }
3149}
3150
3151/*
3152 * Move "pos" forward to the end of the word it's in.
3153 * When 'selection' is "exclusive", the position is just after the word.
3154 */
3155 static void
3156find_end_of_word(pos)
3157 pos_T *pos;
3158{
3159 char_u *line;
3160 int cclass;
3161 int col;
3162
3163 line = ml_get(pos->lnum);
3164 if (*p_sel == 'e' && pos->col > 0)
3165 {
3166 --pos->col;
3167#ifdef FEAT_MBYTE
3168 pos->col -= (*mb_head_off)(line, line + pos->col);
3169#endif
3170 }
3171 cclass = get_mouse_class(line + pos->col);
3172 while (line[pos->col] != NUL)
3173 {
3174#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003175 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#else
3177 col = pos->col + 1;
3178#endif
3179 if (get_mouse_class(line + col) != cclass)
3180 {
3181 if (*p_sel == 'e')
3182 pos->col = col;
3183 break;
3184 }
3185 pos->col = col;
3186 }
3187}
3188
3189/*
3190 * Get class of a character for selection: same class means same word.
3191 * 0: blank
3192 * 1: punctuation groups
3193 * 2: normal word character
3194 * >2: multi-byte word character.
3195 */
3196 static int
3197get_mouse_class(p)
3198 char_u *p;
3199{
3200 int c;
3201
3202#ifdef FEAT_MBYTE
3203 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3204 return mb_get_class(p);
3205#endif
3206
3207 c = *p;
3208 if (c == ' ' || c == '\t')
3209 return 0;
3210
3211 if (vim_iswordc(c))
3212 return 2;
3213
3214 /*
3215 * There are a few special cases where we want certain combinations of
3216 * characters to be considered as a single word. These are things like
3217 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003218 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
3220 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3221 return 1;
3222 return c;
3223}
3224#endif /* FEAT_VISUAL */
3225#endif /* FEAT_MOUSE */
3226
3227#if defined(FEAT_VISUAL) || defined(PROTO)
3228/*
3229 * Check if highlighting for visual mode is possible, give a warning message
3230 * if not.
3231 */
3232 void
3233check_visual_highlight()
3234{
3235 static int did_check = FALSE;
3236
3237 if (full_screen)
3238 {
3239 if (!did_check && hl_attr(HLF_V) == 0)
3240 MSG(_("Warning: terminal cannot highlight"));
3241 did_check = TRUE;
3242 }
3243}
3244
3245/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003246 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 * This function should ALWAYS be called to end Visual mode, except from
3248 * do_pending_operator().
3249 */
3250 void
3251end_visual_mode()
3252{
3253#ifdef FEAT_CLIPBOARD
3254 /*
3255 * If we are using the clipboard, then remember what was selected in case
3256 * we need to paste it somewhere while we still own the selection.
3257 * Only do this when the clipboard is already owned. Don't want to grab
3258 * the selection when hitting ESC.
3259 */
3260 if (clip_star.available && clip_star.owned)
3261 clip_auto_select();
3262#endif
3263
3264 VIsual_active = FALSE;
3265#ifdef FEAT_MOUSE
3266 setmouse();
3267 mouse_dragging = 0;
3268#endif
3269
3270 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003271 curbuf->b_visual.vi_mode = VIsual_mode;
3272 curbuf->b_visual.vi_start = VIsual;
3273 curbuf->b_visual.vi_end = curwin->w_cursor;
3274 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_EVAL
3276 curbuf->b_visual_mode_eval = VIsual_mode;
3277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#ifdef FEAT_VIRTUALEDIT
3279 if (!virtual_active())
3280 curwin->w_cursor.coladd = 0;
3281#endif
3282
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003283 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 clear_cmdline = TRUE; /* unshow visual mode later */
3285#ifdef FEAT_CMDL_INFO
3286 else
3287 clear_showcmd();
3288#endif
3289
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003290 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291}
3292
3293/*
3294 * Reset VIsual_active and VIsual_reselect.
3295 */
3296 void
3297reset_VIsual_and_resel()
3298{
3299 if (VIsual_active)
3300 {
3301 end_visual_mode();
3302 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3303 }
3304 VIsual_reselect = FALSE;
3305}
3306
3307/*
3308 * Reset VIsual_active and VIsual_reselect if it's set.
3309 */
3310 void
3311reset_VIsual()
3312{
3313 if (VIsual_active)
3314 {
3315 end_visual_mode();
3316 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3317 VIsual_reselect = FALSE;
3318 }
3319}
3320#endif /* FEAT_VISUAL */
3321
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3324
3325/*
3326 * Check for a balloon-eval special item to include when searching for an
3327 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3328 * Returns TRUE if the character at "*ptr" should be included.
3329 * "dir" is FORWARD or BACKWARD, the direction of searching.
3330 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3331 * "bnp" points to a counter for square brackets.
3332 */
3333 static int
3334find_is_eval_item(ptr, colp, bnp, dir)
3335 char_u *ptr;
3336 int *colp;
3337 int *bnp;
3338 int dir;
3339{
3340 /* Accept everything inside []. */
3341 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3342 ++*bnp;
3343 if (*bnp > 0)
3344 {
3345 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3346 --*bnp;
3347 return TRUE;
3348 }
3349
3350 /* skip over "s.var" */
3351 if (*ptr == '.')
3352 return TRUE;
3353
3354 /* two-character item: s->var */
3355 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3356 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3357 {
3358 *colp += dir;
3359 return TRUE;
3360 }
3361 return FALSE;
3362}
3363#endif
3364
3365/*
3366 * Find the identifier under or to the right of the cursor.
3367 * "find_type" can have one of three values:
3368 * FIND_IDENT: find an identifier (keyword)
3369 * FIND_STRING: find any non-white string
3370 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *
3373 * There are three steps:
3374 * 1. Search forward for the start of an identifier/string. Doesn't move if
3375 * already on one.
3376 * 2. Search backward for the start of this identifier/string.
3377 * This doesn't match the real Vi but I like it a little better and it
3378 * shouldn't bother anyone.
3379 * 3. Search forward to the end of this identifier/string.
3380 * When FIND_IDENT isn't defined, we backup until a blank.
3381 *
3382 * Returns the length of the string, or zero if no string is found.
3383 * If a string is found, a pointer to the string is put in "*string". This
3384 * string is not always NUL terminated.
3385 */
3386 int
3387find_ident_under_cursor(string, find_type)
3388 char_u **string;
3389 int find_type;
3390{
3391 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3392 curwin->w_cursor.col, string, find_type);
3393}
3394
3395/*
3396 * Like find_ident_under_cursor(), but for any window and any position.
3397 * However: Uses 'iskeyword' from the current window!.
3398 */
3399 int
3400find_ident_at_pos(wp, lnum, startcol, string, find_type)
3401 win_T *wp;
3402 linenr_T lnum;
3403 colnr_T startcol;
3404 char_u **string;
3405 int find_type;
3406{
3407 char_u *ptr;
3408 int col = 0; /* init to shut up GCC */
3409 int i;
3410#ifdef FEAT_MBYTE
3411 int this_class = 0;
3412 int prev_class;
3413 int prevcol;
3414#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 int bn = 0; /* bracket nesting */
3417#endif
3418
3419 /*
3420 * if i == 0: try to find an identifier
3421 * if i == 1: try to find any non-white string
3422 */
3423 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3424 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3425 {
3426 /*
3427 * 1. skip to start of identifier/string
3428 */
3429 col = startcol;
3430#ifdef FEAT_MBYTE
3431 if (has_mbyte)
3432 {
3433 while (ptr[col] != NUL)
3434 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 /* Stop at a ']' to evaluate "a[x]". */
3437 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3438 break;
3439# endif
3440 this_class = mb_get_class(ptr + col);
3441 if (this_class != 0 && (i == 1 || this_class != 1))
3442 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003443 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445 }
3446 else
3447#endif
3448 while (ptr[col] != NUL
3449 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3452# endif
3453 )
3454 ++col;
3455
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* When starting on a ']' count it, so that we include the '['. */
3458 bn = ptr[col] == ']';
3459#endif
3460
3461 /*
3462 * 2. Back up to start of identifier/string.
3463 */
3464#ifdef FEAT_MBYTE
3465 if (has_mbyte)
3466 {
3467 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3470 this_class = mb_get_class((char_u *)"a");
3471 else
3472# endif
3473 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003474 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3477 prev_class = mb_get_class(ptr + prevcol);
3478 if (this_class != prev_class
3479 && (i == 0
3480 || prev_class == 0
3481 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 && (!(find_type & FIND_EVAL)
3484 || prevcol == 0
3485 || !find_is_eval_item(ptr + prevcol, &prevcol,
3486 &bn, BACKWARD))
3487# endif
3488 )
3489 break;
3490 col = prevcol;
3491 }
3492
3493 /* If we don't want just any old string, or we've found an
3494 * identifier, stop searching. */
3495 if (this_class > 2)
3496 this_class = 2;
3497 if (!(find_type & FIND_STRING) || this_class == 2)
3498 break;
3499 }
3500 else
3501#endif
3502 {
3503 while (col > 0
3504 && ((i == 0
3505 ? vim_iswordc(ptr[col - 1])
3506 : (!vim_iswhite(ptr[col - 1])
3507 && (!(find_type & FIND_IDENT)
3508 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 || ((find_type & FIND_EVAL)
3511 && col > 1
3512 && find_is_eval_item(ptr + col - 1, &col,
3513 &bn, BACKWARD))
3514#endif
3515 ))
3516 --col;
3517
3518 /* If we don't want just any old string, or we've found an
3519 * identifier, stop searching. */
3520 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3521 break;
3522 }
3523 }
3524
3525 if (ptr[col] == NUL || (i == 0 && (
3526#ifdef FEAT_MBYTE
3527 has_mbyte ? this_class != 2 :
3528#endif
3529 !vim_iswordc(ptr[col]))))
3530 {
3531 /*
3532 * didn't find an identifier or string
3533 */
3534 if (find_type & FIND_STRING)
3535 EMSG(_("E348: No string under cursor"));
3536 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003537 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 return 0;
3539 }
3540 ptr += col;
3541 *string = ptr;
3542
3543 /*
3544 * 3. Find the end if the identifier/string.
3545 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 bn = 0;
3548 startcol -= col;
3549#endif
3550 col = 0;
3551#ifdef FEAT_MBYTE
3552 if (has_mbyte)
3553 {
3554 /* Search for point of changing multibyte character class. */
3555 this_class = mb_get_class(ptr);
3556 while (ptr[col] != NUL
3557 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3558 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003559# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 || ((find_type & FIND_EVAL)
3561 && col <= (int)startcol
3562 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3563# endif
3564 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003565 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 else
3568#endif
3569 while ((i == 0 ? vim_iswordc(ptr[col])
3570 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 || ((find_type & FIND_EVAL)
3573 && col <= (int)startcol
3574 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3575# endif
3576 )
3577 {
3578 ++col;
3579 }
3580
3581 return col;
3582}
3583
3584/*
3585 * Prepare for redo of a normal command.
3586 */
3587 static void
3588prep_redo_cmd(cap)
3589 cmdarg_T *cap;
3590{
3591 prep_redo(cap->oap->regname, cap->count0,
3592 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3593}
3594
3595/*
3596 * Prepare for redo of any command.
3597 * Note that only the last argument can be a multi-byte char.
3598 */
3599 static void
3600prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3601 int regname;
3602 long num;
3603 int cmd1;
3604 int cmd2;
3605 int cmd3;
3606 int cmd4;
3607 int cmd5;
3608{
3609 ResetRedobuff();
3610 if (regname != 0) /* yank from specified buffer */
3611 {
3612 AppendCharToRedobuff('"');
3613 AppendCharToRedobuff(regname);
3614 }
3615 if (num)
3616 AppendNumberToRedobuff(num);
3617
3618 if (cmd1 != NUL)
3619 AppendCharToRedobuff(cmd1);
3620 if (cmd2 != NUL)
3621 AppendCharToRedobuff(cmd2);
3622 if (cmd3 != NUL)
3623 AppendCharToRedobuff(cmd3);
3624 if (cmd4 != NUL)
3625 AppendCharToRedobuff(cmd4);
3626 if (cmd5 != NUL)
3627 AppendCharToRedobuff(cmd5);
3628}
3629
3630/*
3631 * check for operator active and clear it
3632 *
3633 * return TRUE if operator was active
3634 */
3635 static int
3636checkclearop(oap)
3637 oparg_T *oap;
3638{
3639 if (oap->op_type == OP_NOP)
3640 return FALSE;
3641 clearopbeep(oap);
3642 return TRUE;
3643}
3644
3645/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003646 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003648 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 */
3650 static int
3651checkclearopq(oap)
3652 oparg_T *oap;
3653{
3654 if (oap->op_type == OP_NOP
3655#ifdef FEAT_VISUAL
3656 && !VIsual_active
3657#endif
3658 )
3659 return FALSE;
3660 clearopbeep(oap);
3661 return TRUE;
3662}
3663
3664 static void
3665clearop(oap)
3666 oparg_T *oap;
3667{
3668 oap->op_type = OP_NOP;
3669 oap->regname = 0;
3670 oap->motion_force = NUL;
3671 oap->use_reg_one = FALSE;
3672}
3673
3674 static void
3675clearopbeep(oap)
3676 oparg_T *oap;
3677{
3678 clearop(oap);
3679 beep_flush();
3680}
3681
3682#ifdef FEAT_VISUAL
3683/*
3684 * Remove the shift modifier from a special key.
3685 */
3686 static void
3687unshift_special(cap)
3688 cmdarg_T *cap;
3689{
3690 switch (cap->cmdchar)
3691 {
3692 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3693 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3694 case K_S_UP: cap->cmdchar = K_UP; break;
3695 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3696 case K_S_HOME: cap->cmdchar = K_HOME; break;
3697 case K_S_END: cap->cmdchar = K_END; break;
3698 }
3699 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3700}
3701#endif
3702
3703#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3704/*
3705 * Routines for displaying a partly typed command
3706 */
3707
3708#ifdef FEAT_VISUAL /* need room for size of Visual area */
3709# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3710#else
3711# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3712#endif
3713static char_u showcmd_buf[SHOWCMD_BUFLEN];
3714static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3715static int showcmd_is_clear = TRUE;
3716static int showcmd_visual = FALSE;
3717
3718static void display_showcmd __ARGS((void));
3719
3720 void
3721clear_showcmd()
3722{
3723 if (!p_sc)
3724 return;
3725
3726#ifdef FEAT_VISUAL
3727 if (VIsual_active && !char_avail())
3728 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003729 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 long lines;
3731 colnr_T leftcol, rightcol;
3732 linenr_T top, bot;
3733
3734 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003735 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
3737 top = VIsual.lnum;
3738 bot = curwin->w_cursor.lnum;
3739 }
3740 else
3741 {
3742 top = curwin->w_cursor.lnum;
3743 bot = VIsual.lnum;
3744 }
3745# ifdef FEAT_FOLDING
3746 /* Include closed folds as a whole. */
3747 hasFolding(top, &top, NULL);
3748 hasFolding(bot, NULL, &bot);
3749# endif
3750 lines = bot - top + 1;
3751
3752 if (VIsual_mode == Ctrl_V)
3753 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003754#ifdef FEAT_LINEBREAK
3755 char_u *saved_sbr = p_sbr;
3756
3757 /* Make 'sbr' empty for a moment to get the correct size. */
3758 p_sbr = empty_option;
3759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaar81d00072009-04-29 15:41:40 +00003761#ifdef FEAT_LINEBREAK
3762 p_sbr = saved_sbr;
3763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3765 (long)(rightcol - leftcol + 1));
3766 }
3767 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3768 sprintf((char *)showcmd_buf, "%ld", lines);
3769 else
Bram Moolenaar81d00072009-04-29 15:41:40 +00003770 sprintf((char *)showcmd_buf, "%ld", (long)(cursor_bot
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 ? curwin->w_cursor.col - VIsual.col
3772 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3773 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3774 showcmd_visual = TRUE;
3775 }
3776 else
3777#endif
3778 {
3779 showcmd_buf[0] = NUL;
3780 showcmd_visual = FALSE;
3781
3782 /* Don't actually display something if there is nothing to clear. */
3783 if (showcmd_is_clear)
3784 return;
3785 }
3786
3787 display_showcmd();
3788}
3789
3790/*
3791 * Add 'c' to string of shown command chars.
3792 * Return TRUE if output has been written (and setcursor() has been called).
3793 */
3794 int
3795add_to_showcmd(c)
3796 int c;
3797{
3798 char_u *p;
3799 int old_len;
3800 int extra_len;
3801 int overflow;
3802#if defined(FEAT_MOUSE)
3803 int i;
3804 static int ignore[] =
3805 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003806# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3808 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 K_IGNORE,
3811 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3812 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3813 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3814 K_MOUSEDOWN, K_MOUSEUP,
3815 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003816 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 0
3818 };
3819#endif
3820
Bram Moolenaar09df3122006-01-23 22:23:09 +00003821 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FALSE;
3823
3824 if (showcmd_visual)
3825 {
3826 showcmd_buf[0] = NUL;
3827 showcmd_visual = FALSE;
3828 }
3829
3830#if defined(FEAT_MOUSE)
3831 /* Ignore keys that are scrollbar updates and mouse clicks */
3832 if (IS_SPECIAL(c))
3833 for (i = 0; ignore[i] != 0; ++i)
3834 if (ignore[i] == c)
3835 return FALSE;
3836#endif
3837
3838 p = transchar(c);
3839 old_len = (int)STRLEN(showcmd_buf);
3840 extra_len = (int)STRLEN(p);
3841 overflow = old_len + extra_len - SHOWCMD_COLS;
3842 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003843 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3844 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 STRCAT(showcmd_buf, p);
3846
3847 if (char_avail())
3848 return FALSE;
3849
3850 display_showcmd();
3851
3852 return TRUE;
3853}
3854
3855 void
3856add_to_showcmd_c(c)
3857 int c;
3858{
3859 if (!add_to_showcmd(c))
3860 setcursor();
3861}
3862
3863/*
3864 * Delete 'len' characters from the end of the shown command.
3865 */
3866 static void
3867del_from_showcmd(len)
3868 int len;
3869{
3870 int old_len;
3871
3872 if (!p_sc)
3873 return;
3874
3875 old_len = (int)STRLEN(showcmd_buf);
3876 if (len > old_len)
3877 len = old_len;
3878 showcmd_buf[old_len - len] = NUL;
3879
3880 if (!char_avail())
3881 display_showcmd();
3882}
3883
3884/*
3885 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3886 * something and there is a partial mapping.
3887 */
3888 void
3889push_showcmd()
3890{
3891 if (p_sc)
3892 STRCPY(old_showcmd_buf, showcmd_buf);
3893}
3894
3895 void
3896pop_showcmd()
3897{
3898 if (!p_sc)
3899 return;
3900
3901 STRCPY(showcmd_buf, old_showcmd_buf);
3902
3903 display_showcmd();
3904}
3905
3906 static void
3907display_showcmd()
3908{
3909 int len;
3910
3911 cursor_off();
3912
3913 len = (int)STRLEN(showcmd_buf);
3914 if (len == 0)
3915 showcmd_is_clear = TRUE;
3916 else
3917 {
3918 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3919 showcmd_is_clear = FALSE;
3920 }
3921
3922 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003923 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3924 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 */
3926 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3927
3928 setcursor(); /* put cursor back where it belongs */
3929}
3930#endif
3931
3932#ifdef FEAT_SCROLLBIND
3933/*
3934 * When "check" is FALSE, prepare for commands that scroll the window.
3935 * When "check" is TRUE, take care of scroll-binding after the window has
3936 * scrolled. Called from normal_cmd() and edit().
3937 */
3938 void
3939do_check_scrollbind(check)
3940 int check;
3941{
3942 static win_T *old_curwin = NULL;
3943 static linenr_T old_topline = 0;
3944#ifdef FEAT_DIFF
3945 static int old_topfill = 0;
3946#endif
3947 static buf_T *old_buf = NULL;
3948 static colnr_T old_leftcol = 0;
3949
3950 if (check && curwin->w_p_scb)
3951 {
3952 /* If a ":syncbind" command was just used, don't scroll, only reset
3953 * the values. */
3954 if (did_syncbind)
3955 did_syncbind = FALSE;
3956 else if (curwin == old_curwin)
3957 {
3958 /*
3959 * Synchronize other windows, as necessary according to
3960 * 'scrollbind'. Don't do this after an ":edit" command, except
3961 * when 'diff' is set.
3962 */
3963 if ((curwin->w_buffer == old_buf
3964#ifdef FEAT_DIFF
3965 || curwin->w_p_diff
3966#endif
3967 )
3968 && (curwin->w_topline != old_topline
3969#ifdef FEAT_DIFF
3970 || curwin->w_topfill != old_topfill
3971#endif
3972 || curwin->w_leftcol != old_leftcol))
3973 {
3974 check_scrollbind(curwin->w_topline - old_topline,
3975 (long)(curwin->w_leftcol - old_leftcol));
3976 }
3977 }
3978 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3979 {
3980 /*
3981 * When switching between windows, make sure that the relative
3982 * vertical offset is valid for the new window. The relative
3983 * offset is invalid whenever another 'scrollbind' window has
3984 * scrolled to a point that would force the current window to
3985 * scroll past the beginning or end of its buffer. When the
3986 * resync is performed, some of the other 'scrollbind' windows may
3987 * need to jump so that the current window's relative position is
3988 * visible on-screen.
3989 */
3990 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3991 }
3992 curwin->w_scbind_pos = curwin->w_topline;
3993 }
3994
3995 old_curwin = curwin;
3996 old_topline = curwin->w_topline;
3997#ifdef FEAT_DIFF
3998 old_topfill = curwin->w_topfill;
3999#endif
4000 old_buf = curwin->w_buffer;
4001 old_leftcol = curwin->w_leftcol;
4002}
4003
4004/*
4005 * Synchronize any windows that have "scrollbind" set, based on the
4006 * number of rows by which the current window has changed
4007 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4008 */
4009 void
4010check_scrollbind(topline_diff, leftcol_diff)
4011 linenr_T topline_diff;
4012 long leftcol_diff;
4013{
4014 int want_ver;
4015 int want_hor;
4016 win_T *old_curwin = curwin;
4017 buf_T *old_curbuf = curbuf;
4018#ifdef FEAT_VISUAL
4019 int old_VIsual_select = VIsual_select;
4020 int old_VIsual_active = VIsual_active;
4021#endif
4022 colnr_T tgt_leftcol = curwin->w_leftcol;
4023 long topline;
4024 long y;
4025
4026 /*
4027 * check 'scrollopt' string for vertical and horizontal scroll options
4028 */
4029 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4030#ifdef FEAT_DIFF
4031 want_ver |= old_curwin->w_p_diff;
4032#endif
4033 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4034
4035 /*
4036 * loop through the scrollbound windows and scroll accordingly
4037 */
4038#ifdef FEAT_VISUAL
4039 VIsual_select = VIsual_active = 0;
4040#endif
4041 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4042 {
4043 curbuf = curwin->w_buffer;
4044 /* skip original window and windows with 'noscrollbind' */
4045 if (curwin != old_curwin && curwin->w_p_scb)
4046 {
4047 /*
4048 * do the vertical scroll
4049 */
4050 if (want_ver)
4051 {
4052#ifdef FEAT_DIFF
4053 if (old_curwin->w_p_diff && curwin->w_p_diff)
4054 {
4055 diff_set_topline(old_curwin, curwin);
4056 }
4057 else
4058#endif
4059 {
4060 curwin->w_scbind_pos += topline_diff;
4061 topline = curwin->w_scbind_pos;
4062 if (topline > curbuf->b_ml.ml_line_count)
4063 topline = curbuf->b_ml.ml_line_count;
4064 if (topline < 1)
4065 topline = 1;
4066
4067 y = topline - curwin->w_topline;
4068 if (y > 0)
4069 scrollup(y, FALSE);
4070 else
4071 scrolldown(-y, FALSE);
4072 }
4073
4074 redraw_later(VALID);
4075 cursor_correct();
4076#ifdef FEAT_WINDOWS
4077 curwin->w_redr_status = TRUE;
4078#endif
4079 }
4080
4081 /*
4082 * do the horizontal scroll
4083 */
4084 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4085 {
4086 curwin->w_leftcol = tgt_leftcol;
4087 leftcol_changed();
4088 }
4089 }
4090 }
4091
4092 /*
4093 * reset current-window
4094 */
4095#ifdef FEAT_VISUAL
4096 VIsual_select = old_VIsual_select;
4097 VIsual_active = old_VIsual_active;
4098#endif
4099 curwin = old_curwin;
4100 curbuf = old_curbuf;
4101}
4102#endif /* #ifdef FEAT_SCROLLBIND */
4103
4104/*
4105 * Command character that's ignored.
4106 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004107 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 static void
4110nv_ignore(cap)
4111 cmdarg_T *cap;
4112{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004113 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114}
4115
4116/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004117 * Command character that doesn't do anything, but unlike nv_ignore() does
4118 * start edit(). Used for "startinsert" executed while starting up.
4119 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004120 static void
4121nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004122 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004123{
4124}
4125
4126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * Command character doesn't exist.
4128 */
4129 static void
4130nv_error(cap)
4131 cmdarg_T *cap;
4132{
4133 clearopbeep(cap->oap);
4134}
4135
4136/*
4137 * <Help> and <F1> commands.
4138 */
4139 static void
4140nv_help(cap)
4141 cmdarg_T *cap;
4142{
4143 if (!checkclearopq(cap->oap))
4144 ex_help(NULL);
4145}
4146
4147/*
4148 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4149 */
4150 static void
4151nv_addsub(cap)
4152 cmdarg_T *cap;
4153{
4154 if (!checkclearopq(cap->oap)
4155 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4156 prep_redo_cmd(cap);
4157}
4158
4159/*
4160 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4161 */
4162 static void
4163nv_page(cap)
4164 cmdarg_T *cap;
4165{
4166 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004167 {
4168#ifdef FEAT_WINDOWS
4169 if (mod_mask & MOD_MASK_CTRL)
4170 {
4171 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4172 if (cap->arg == BACKWARD)
4173 goto_tabpage(-(int)cap->count1);
4174 else
4175 goto_tabpage((int)cap->count0);
4176 }
4177 else
4178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181}
4182
4183/*
4184 * Implementation of "gd" and "gD" command.
4185 */
4186 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004187nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004188 oparg_T *oap;
4189 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004190 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 char_u *ptr;
4194
Bram Moolenaard9d30582005-05-18 22:10:28 +00004195 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004196 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004198#ifdef FEAT_FOLDING
4199 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4200 foldOpenCursor();
4201#endif
4202}
4203
4204/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004205 * Search for variable declaration of "ptr[len]".
4206 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4207 * current file ("gD").
4208 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004209 * Return FAIL when not found.
4210 */
4211 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004212find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004213 char_u *ptr;
4214 int len;
4215 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004216 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 int searchflags; /* flags passed to searchit() */
4218{
4219 char_u *pat;
4220 pos_T old_pos;
4221 pos_T par_pos;
4222 pos_T found_pos;
4223 int t;
4224 int save_p_ws;
4225 int save_p_scs;
4226 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004227 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004228
4229 if ((pat = alloc(len + 7)) == NULL)
4230 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004231
4232 /* Put "\V" before the pattern to avoid that the special meaning of "."
4233 * and "~" causes trouble. */
4234 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4235 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 old_pos = curwin->w_cursor;
4237 save_p_ws = p_ws;
4238 save_p_scs = p_scs;
4239 p_ws = FALSE; /* don't wrap around end of file now */
4240 p_scs = FALSE; /* don't switch ignorecase off now */
4241
4242 /*
4243 * With "gD" go to line 1.
4244 * With "gd" Search back for the start of the current function, then go
4245 * back until a blank line. If this fails go to line 1.
4246 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004247 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 setpcmark(); /* Set in findpar() otherwise */
4250 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004251 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253 else
4254 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004255 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4257 --curwin->w_cursor.lnum;
4258 }
4259 curwin->w_cursor.col = 0;
4260
4261 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004262 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004263 for (;;)
4264 {
4265 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004266 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 if (curwin->w_cursor.lnum >= old_pos.lnum)
4268 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004269
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004270 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271 {
4272 pos_T *pos;
4273
4274 /* Check that the block the match is in doesn't end before the
4275 * position where we started the search from. */
4276 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4277 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4278 && pos->lnum < old_pos.lnum)
4279 continue;
4280 }
4281
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004282 if (t == FAIL)
4283 {
4284 /* If we previously found a valid position, use it. */
4285 if (found_pos.lnum != 0)
4286 {
4287 curwin->w_cursor = found_pos;
4288 t = OK;
4289 }
4290 break;
4291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4294 {
4295 /* Ignore this line, continue at start of next line. */
4296 ++curwin->w_cursor.lnum;
4297 curwin->w_cursor.col = 0;
4298 continue;
4299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 if (!locally) /* global search: use first match found */
4302 break;
4303 if (curwin->w_cursor.lnum >= par_pos.lnum)
4304 {
4305 /* If we previously found a valid position, use it. */
4306 if (found_pos.lnum != 0)
4307 curwin->w_cursor = found_pos;
4308 break;
4309 }
4310
4311 /* For finding a local variable and the match is before the "{" search
4312 * to find a later match. For K&R style function declarations this
4313 * skips the function header without types. */
4314 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316
4317 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004319 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 curwin->w_cursor = old_pos;
4321 }
4322 else
4323 {
4324 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 /* "n" searches forward now */
4326 reset_search_dir();
4327 }
4328
4329 vim_free(pat);
4330 p_ws = save_p_ws;
4331 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332
4333 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334}
4335
4336/*
4337 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4338 * lines rather than lines in the file.
4339 * 'dist' must be positive.
4340 *
4341 * Return OK if able to move cursor, FAIL otherwise.
4342 */
4343 static int
4344nv_screengo(oap, dir, dist)
4345 oparg_T *oap;
4346 int dir;
4347 long dist;
4348{
4349 int linelen = linetabsize(ml_get_curline());
4350 int retval = OK;
4351 int atend = FALSE;
4352 int n;
4353 int col_off1; /* margin offset for first screen line */
4354 int col_off2; /* margin offset for wrapped screen line */
4355 int width1; /* text width for first screen line */
4356 int width2; /* test width for wrapped screen line */
4357
4358 oap->motion_type = MCHAR;
4359 oap->inclusive = FALSE;
4360
4361 col_off1 = curwin_col_off();
4362 col_off2 = col_off1 - curwin_col_off2();
4363 width1 = W_WIDTH(curwin) - col_off1;
4364 width2 = W_WIDTH(curwin) - col_off2;
4365
4366#ifdef FEAT_VERTSPLIT
4367 if (curwin->w_width != 0)
4368 {
4369#endif
4370 /*
4371 * Instead of sticking at the last character of the buffer line we
4372 * try to stick in the last column of the screen.
4373 */
4374 if (curwin->w_curswant == MAXCOL)
4375 {
4376 atend = TRUE;
4377 validate_virtcol();
4378 if (width1 <= 0)
4379 curwin->w_curswant = 0;
4380 else
4381 {
4382 curwin->w_curswant = width1 - 1;
4383 if (curwin->w_virtcol > curwin->w_curswant)
4384 curwin->w_curswant += ((curwin->w_virtcol
4385 - curwin->w_curswant - 1) / width2 + 1) * width2;
4386 }
4387 }
4388 else
4389 {
4390 if (linelen > width1)
4391 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4392 else
4393 n = width1;
4394 if (curwin->w_curswant > (colnr_T)n + 1)
4395 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4396 * width2;
4397 }
4398
4399 while (dist--)
4400 {
4401 if (dir == BACKWARD)
4402 {
4403 if ((long)curwin->w_curswant >= width2)
4404 /* move back within line */
4405 curwin->w_curswant -= width2;
4406 else
4407 {
4408 /* to previous line */
4409 if (curwin->w_cursor.lnum == 1)
4410 {
4411 retval = FAIL;
4412 break;
4413 }
4414 --curwin->w_cursor.lnum;
4415#ifdef FEAT_FOLDING
4416 /* Move to the start of a closed fold. Don't do that when
4417 * 'foldopen' contains "all": it will open in a moment. */
4418 if (!(fdo_flags & FDO_ALL))
4419 (void)hasFolding(curwin->w_cursor.lnum,
4420 &curwin->w_cursor.lnum, NULL);
4421#endif
4422 linelen = linetabsize(ml_get_curline());
4423 if (linelen > width1)
4424 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4425 + 1) * width2;
4426 }
4427 }
4428 else /* dir == FORWARD */
4429 {
4430 if (linelen > width1)
4431 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4432 else
4433 n = width1;
4434 if (curwin->w_curswant + width2 < (colnr_T)n)
4435 /* move forward within line */
4436 curwin->w_curswant += width2;
4437 else
4438 {
4439 /* to next line */
4440#ifdef FEAT_FOLDING
4441 /* Move to the end of a closed fold. */
4442 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4443 &curwin->w_cursor.lnum);
4444#endif
4445 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4446 {
4447 retval = FAIL;
4448 break;
4449 }
4450 curwin->w_cursor.lnum++;
4451 curwin->w_curswant %= width2;
4452 }
4453 }
4454 }
4455#ifdef FEAT_VERTSPLIT
4456 }
4457#endif
4458
4459 coladvance(curwin->w_curswant);
4460
4461#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4462 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4463 {
4464 /*
4465 * Check for landing on a character that got split at the end of the
4466 * last line. We want to advance a screenline, not end up in the same
4467 * screenline or move two screenlines.
4468 */
4469 validate_virtcol();
4470 if (curwin->w_virtcol > curwin->w_curswant
4471 && (curwin->w_curswant < (colnr_T)width1
4472 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4473 : ((curwin->w_curswant - width1) % width2
4474 > (colnr_T)width2 / 2)))
4475 --curwin->w_cursor.col;
4476 }
4477#endif
4478
4479 if (atend)
4480 curwin->w_curswant = MAXCOL; /* stick in the last column */
4481
4482 return retval;
4483}
4484
4485#ifdef FEAT_MOUSE
4486/*
4487 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4488 * when Shift or Ctrl is used.
4489 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4490 */
4491 static void
4492nv_mousescroll(cap)
4493 cmdarg_T *cap;
4494{
4495# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004496 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 /* Currently we only get the mouse coordinates in the GUI. */
4499 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4500 {
4501 int row, col;
4502
4503 row = mouse_row;
4504 col = mouse_col;
4505
4506 /* find the window at the pointer coordinates */
4507 curwin = mouse_find_win(&row, &col);
4508 curbuf = curwin->w_buffer;
4509 }
4510# endif
4511
4512 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4513 {
4514 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4515 }
4516 else
4517 {
4518 cap->count1 = 3;
4519 cap->count0 = 3;
4520 nv_scroll_line(cap);
4521 }
4522
4523# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4524 curwin->w_redr_status = TRUE;
4525
4526 curwin = old_curwin;
4527 curbuf = curwin->w_buffer;
4528# endif
4529}
4530
4531/*
4532 * Mouse clicks and drags.
4533 */
4534 static void
4535nv_mouse(cap)
4536 cmdarg_T *cap;
4537{
4538 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4539}
4540#endif
4541
4542/*
4543 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4544 * cap->arg must be TRUE for CTRL-E.
4545 */
4546 static void
4547nv_scroll_line(cap)
4548 cmdarg_T *cap;
4549{
4550 if (!checkclearop(cap->oap))
4551 scroll_redraw(cap->arg, cap->count1);
4552}
4553
4554/*
4555 * Scroll "count" lines up or down, and redraw.
4556 */
4557 void
4558scroll_redraw(up, count)
4559 int up;
4560 long count;
4561{
4562 linenr_T prev_topline = curwin->w_topline;
4563#ifdef FEAT_DIFF
4564 int prev_topfill = curwin->w_topfill;
4565#endif
4566 linenr_T prev_lnum = curwin->w_cursor.lnum;
4567
4568 if (up)
4569 scrollup(count, TRUE);
4570 else
4571 scrolldown(count, TRUE);
4572 if (p_so)
4573 {
4574 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4575 * valid, otherwise the screen jumps back at the end of the file. */
4576 cursor_correct();
4577 check_cursor_moved(curwin);
4578 curwin->w_valid |= VALID_TOPLINE;
4579
4580 /* If moved back to where we were, at least move the cursor, otherwise
4581 * we get stuck at one position. Don't move the cursor up if the
4582 * first line of the buffer is already on the screen */
4583 while (curwin->w_topline == prev_topline
4584#ifdef FEAT_DIFF
4585 && curwin->w_topfill == prev_topfill
4586#endif
4587 )
4588 {
4589 if (up)
4590 {
4591 if (curwin->w_cursor.lnum > prev_lnum
4592 || cursor_down(1L, FALSE) == FAIL)
4593 break;
4594 }
4595 else
4596 {
4597 if (curwin->w_cursor.lnum < prev_lnum
4598 || prev_topline == 1L
4599 || cursor_up(1L, FALSE) == FAIL)
4600 break;
4601 }
4602 /* Mark w_topline as valid, otherwise the screen jumps back at the
4603 * end of the file. */
4604 check_cursor_moved(curwin);
4605 curwin->w_valid |= VALID_TOPLINE;
4606 }
4607 }
4608 if (curwin->w_cursor.lnum != prev_lnum)
4609 coladvance(curwin->w_curswant);
4610 redraw_later(VALID);
4611}
4612
4613/*
4614 * Commands that start with "z".
4615 */
4616 static void
4617nv_zet(cap)
4618 cmdarg_T *cap;
4619{
4620 long n;
4621 colnr_T col;
4622 int nchar = cap->nchar;
4623#ifdef FEAT_FOLDING
4624 long old_fdl = curwin->w_p_fdl;
4625 int old_fen = curwin->w_p_fen;
4626#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004627#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004628 int undo = FALSE;
4629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 if (VIM_ISDIGIT(nchar))
4632 {
4633 /*
4634 * "z123{nchar}": edit the count before obtaining {nchar}
4635 */
4636 if (checkclearop(cap->oap))
4637 return;
4638 n = nchar - '0';
4639 for (;;)
4640 {
4641#ifdef USE_ON_FLY_SCROLL
4642 dont_scroll = TRUE; /* disallow scrolling here */
4643#endif
4644 ++no_mapping;
4645 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004646 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 --no_mapping;
4649 --allow_keys;
4650#ifdef FEAT_CMDL_INFO
4651 (void)add_to_showcmd(nchar);
4652#endif
4653 if (nchar == K_DEL || nchar == K_KDEL)
4654 n /= 10;
4655 else if (VIM_ISDIGIT(nchar))
4656 n = n * 10 + (nchar - '0');
4657 else if (nchar == CAR)
4658 {
4659#ifdef FEAT_GUI
4660 need_mouse_correct = TRUE;
4661#endif
4662 win_setheight((int)n);
4663 break;
4664 }
4665 else if (nchar == 'l'
4666 || nchar == 'h'
4667 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004668 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 cap->count1 = n ? n * cap->count1 : cap->count1;
4671 goto dozet;
4672 }
4673 else
4674 {
4675 clearopbeep(cap->oap);
4676 break;
4677 }
4678 }
4679 cap->oap->op_type = OP_NOP;
4680 return;
4681 }
4682
4683dozet:
4684 if (
4685#ifdef FEAT_FOLDING
4686 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4687 * and "zC" only in Visual mode. "zj" and "zk" are motion
4688 * commands. */
4689 cap->nchar != 'f' && cap->nchar != 'F'
4690 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4691 && cap->nchar != 'j' && cap->nchar != 'k'
4692 &&
4693#endif
4694 checkclearop(cap->oap))
4695 return;
4696
4697 /*
4698 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4699 * If line number given, set cursor.
4700 */
4701 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4702 && cap->count0
4703 && cap->count0 != curwin->w_cursor.lnum)
4704 {
4705 setpcmark();
4706 if (cap->count0 > curbuf->b_ml.ml_line_count)
4707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4708 else
4709 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004710 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712
4713 switch (nchar)
4714 {
4715 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4716 case '+':
4717 if (cap->count0 == 0)
4718 {
4719 /* No count given: put cursor at the line below screen */
4720 validate_botline(); /* make sure w_botline is valid */
4721 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4723 else
4724 curwin->w_cursor.lnum = curwin->w_botline;
4725 }
4726 /* FALLTHROUGH */
4727 case NL:
4728 case CAR:
4729 case K_KENTER:
4730 beginline(BL_WHITE | BL_FIX);
4731 /* FALLTHROUGH */
4732
4733 case 't': scroll_cursor_top(0, TRUE);
4734 redraw_later(VALID);
4735 break;
4736
4737 /* "z." and "zz": put cursor in middle of screen */
4738 case '.': beginline(BL_WHITE | BL_FIX);
4739 /* FALLTHROUGH */
4740
4741 case 'z': scroll_cursor_halfway(TRUE);
4742 redraw_later(VALID);
4743 break;
4744
4745 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4746 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4747 * when <count> is at bottom of window, and puts that one at
4748 * bottom of window. */
4749 if (cap->count0 != 0)
4750 {
4751 scroll_cursor_bot(0, TRUE);
4752 curwin->w_cursor.lnum = curwin->w_topline;
4753 }
4754 else if (curwin->w_topline == 1)
4755 curwin->w_cursor.lnum = 1;
4756 else
4757 curwin->w_cursor.lnum = curwin->w_topline - 1;
4758 /* FALLTHROUGH */
4759 case '-':
4760 beginline(BL_WHITE | BL_FIX);
4761 /* FALLTHROUGH */
4762
4763 case 'b': scroll_cursor_bot(0, TRUE);
4764 redraw_later(VALID);
4765 break;
4766
4767 /* "zH" - scroll screen right half-page */
4768 case 'H':
4769 cap->count1 *= W_WIDTH(curwin) / 2;
4770 /* FALLTHROUGH */
4771
4772 /* "zh" - scroll screen to the right */
4773 case 'h':
4774 case K_LEFT:
4775 if (!curwin->w_p_wrap)
4776 {
4777 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4778 curwin->w_leftcol = 0;
4779 else
4780 curwin->w_leftcol -= (colnr_T)cap->count1;
4781 leftcol_changed();
4782 }
4783 break;
4784
4785 /* "zL" - scroll screen left half-page */
4786 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4787 /* FALLTHROUGH */
4788
4789 /* "zl" - scroll screen to the left */
4790 case 'l':
4791 case K_RIGHT:
4792 if (!curwin->w_p_wrap)
4793 {
4794 /* scroll the window left */
4795 curwin->w_leftcol += (colnr_T)cap->count1;
4796 leftcol_changed();
4797 }
4798 break;
4799
4800 /* "zs" - scroll screen, cursor at the start */
4801 case 's': if (!curwin->w_p_wrap)
4802 {
4803#ifdef FEAT_FOLDING
4804 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4805 col = 0; /* like the cursor is in col 0 */
4806 else
4807#endif
4808 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4809 if ((long)col > p_siso)
4810 col -= p_siso;
4811 else
4812 col = 0;
4813 if (curwin->w_leftcol != col)
4814 {
4815 curwin->w_leftcol = col;
4816 redraw_later(NOT_VALID);
4817 }
4818 }
4819 break;
4820
4821 /* "ze" - scroll screen, cursor at the end */
4822 case 'e': if (!curwin->w_p_wrap)
4823 {
4824#ifdef FEAT_FOLDING
4825 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4826 col = 0; /* like the cursor is in col 0 */
4827 else
4828#endif
4829 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4830 n = W_WIDTH(curwin) - curwin_col_off();
4831 if ((long)col + p_siso < n)
4832 col = 0;
4833 else
4834 col = col + p_siso - n + 1;
4835 if (curwin->w_leftcol != col)
4836 {
4837 curwin->w_leftcol = col;
4838 redraw_later(NOT_VALID);
4839 }
4840 }
4841 break;
4842
4843#ifdef FEAT_FOLDING
4844 /* "zF": create fold command */
4845 /* "zf": create fold operator */
4846 case 'F':
4847 case 'f': if (foldManualAllowed(TRUE))
4848 {
4849 cap->nchar = 'f';
4850 nv_operator(cap);
4851 curwin->w_p_fen = TRUE;
4852
4853 /* "zF" is like "zfzf" */
4854 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4855 {
4856 nv_operator(cap);
4857 finish_op = TRUE;
4858 }
4859 }
4860 else
4861 clearopbeep(cap->oap);
4862 break;
4863
4864 /* "zd": delete fold at cursor */
4865 /* "zD": delete fold at cursor recursively */
4866 case 'd':
4867 case 'D': if (foldManualAllowed(FALSE))
4868 {
4869 if (VIsual_active)
4870 nv_operator(cap);
4871 else
4872 deleteFold(curwin->w_cursor.lnum,
4873 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4874 }
4875 break;
4876
4877 /* "zE": erease all folds */
4878 case 'E': if (foldmethodIsManual(curwin))
4879 {
4880 clearFolding(curwin);
4881 changed_window_setting();
4882 }
4883 else if (foldmethodIsMarker(curwin))
4884 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4885 TRUE, FALSE);
4886 else
4887 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4888 break;
4889
4890 /* "zn": fold none: reset 'foldenable' */
4891 case 'n': curwin->w_p_fen = FALSE;
4892 break;
4893
4894 /* "zN": fold Normal: set 'foldenable' */
4895 case 'N': curwin->w_p_fen = TRUE;
4896 break;
4897
4898 /* "zi": invert folding: toggle 'foldenable' */
4899 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4900 break;
4901
4902 /* "za": open closed fold or close open fold at cursor */
4903 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4904 openFold(curwin->w_cursor.lnum, cap->count1);
4905 else
4906 {
4907 closeFold(curwin->w_cursor.lnum, cap->count1);
4908 curwin->w_p_fen = TRUE;
4909 }
4910 break;
4911
4912 /* "zA": open fold at cursor recursively */
4913 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4914 openFoldRecurse(curwin->w_cursor.lnum);
4915 else
4916 {
4917 closeFoldRecurse(curwin->w_cursor.lnum);
4918 curwin->w_p_fen = TRUE;
4919 }
4920 break;
4921
4922 /* "zo": open fold at cursor or Visual area */
4923 case 'o': if (VIsual_active)
4924 nv_operator(cap);
4925 else
4926 openFold(curwin->w_cursor.lnum, cap->count1);
4927 break;
4928
4929 /* "zO": open fold recursively */
4930 case 'O': if (VIsual_active)
4931 nv_operator(cap);
4932 else
4933 openFoldRecurse(curwin->w_cursor.lnum);
4934 break;
4935
4936 /* "zc": close fold at cursor or Visual area */
4937 case 'c': if (VIsual_active)
4938 nv_operator(cap);
4939 else
4940 closeFold(curwin->w_cursor.lnum, cap->count1);
4941 curwin->w_p_fen = TRUE;
4942 break;
4943
4944 /* "zC": close fold recursively */
4945 case 'C': if (VIsual_active)
4946 nv_operator(cap);
4947 else
4948 closeFoldRecurse(curwin->w_cursor.lnum);
4949 curwin->w_p_fen = TRUE;
4950 break;
4951
4952 /* "zv": open folds at the cursor */
4953 case 'v': foldOpenCursor();
4954 break;
4955
4956 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4957 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02004958 curwin->w_foldinvalid = TRUE; /* recompute folds */
4959 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 foldOpenCursor();
4961 break;
4962
4963 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4964 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02004965 curwin->w_foldinvalid = TRUE; /* recompute folds */
4966 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 break;
4968
4969 /* "zm": fold more */
4970 case 'm': if (curwin->w_p_fdl > 0)
4971 --curwin->w_p_fdl;
4972 old_fdl = -1; /* force an update */
4973 curwin->w_p_fen = TRUE;
4974 break;
4975
4976 /* "zM": close all folds */
4977 case 'M': curwin->w_p_fdl = 0;
4978 old_fdl = -1; /* force an update */
4979 curwin->w_p_fen = TRUE;
4980 break;
4981
4982 /* "zr": reduce folding */
4983 case 'r': ++curwin->w_p_fdl;
4984 break;
4985
4986 /* "zR": open all folds */
4987 case 'R': curwin->w_p_fdl = getDeepestNesting();
4988 old_fdl = -1; /* force an update */
4989 break;
4990
4991 case 'j': /* "zj" move to next fold downwards */
4992 case 'k': /* "zk" move to next fold upwards */
4993 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4994 cap->count1) == FAIL)
4995 clearopbeep(cap->oap);
4996 break;
4997
4998#endif /* FEAT_FOLDING */
4999
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005000#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005001 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5002 ++no_mapping;
5003 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005004 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005005 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005006 --no_mapping;
5007 --allow_keys;
5008#ifdef FEAT_CMDL_INFO
5009 (void)add_to_showcmd(nchar);
5010#endif
5011 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5012 {
5013 clearopbeep(cap->oap);
5014 break;
5015 }
5016 undo = TRUE;
5017 /*FALLTHROUGH*/
5018
Bram Moolenaarb765d632005-06-07 21:00:02 +00005019 case 'g': /* "zg": add good word to word list */
5020 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005021 case 'G': /* "zG": add good word to temp word list */
5022 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005023 {
5024 char_u *ptr = NULL;
5025 int len;
5026
5027 if (checkclearop(cap->oap))
5028 break;
5029# ifdef FEAT_VISUAL
5030 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5031 == FAIL)
5032 return;
5033# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005034 if (ptr == NULL)
5035 {
5036 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005037
5038 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005039 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005040 if (len != 0 && curwin->w_cursor.col <= pos.col)
5041 ptr = ml_get_pos(&curwin->w_cursor);
5042 curwin->w_cursor = pos;
5043 }
5044
Bram Moolenaarb765d632005-06-07 21:00:02 +00005045 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5046 FIND_IDENT)) == 0)
5047 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005048 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005049 (nchar == 'G' || nchar == 'W')
5050 ? 0 : (int)cap->count1,
5051 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005052 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005053 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005054
Bram Moolenaar43abc522005-12-10 20:15:02 +00005055 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005056 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005057 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005058 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005059#endif
5060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 default: clearopbeep(cap->oap);
5062 }
5063
5064#ifdef FEAT_FOLDING
5065 /* Redraw when 'foldenable' changed */
5066 if (old_fen != curwin->w_p_fen)
5067 {
5068# ifdef FEAT_DIFF
5069 win_T *wp;
5070
5071 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5072 {
5073 /* Adjust 'foldenable' in diff-synced windows. */
5074 FOR_ALL_WINDOWS(wp)
5075 {
5076 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5077 {
5078 wp->w_p_fen = curwin->w_p_fen;
5079 changed_window_setting_win(wp);
5080 }
5081 }
5082 }
5083# endif
5084 changed_window_setting();
5085 }
5086
5087 /* Redraw when 'foldlevel' changed. */
5088 if (old_fdl != curwin->w_p_fdl)
5089 newFoldLevel();
5090#endif
5091}
5092
5093#ifdef FEAT_GUI
5094/*
5095 * Vertical scrollbar movement.
5096 */
5097 static void
5098nv_ver_scrollbar(cap)
5099 cmdarg_T *cap;
5100{
5101 if (cap->oap->op_type != OP_NOP)
5102 clearopbeep(cap->oap);
5103
5104 /* Even if an operator was pending, we still want to scroll */
5105 gui_do_scroll();
5106}
5107
5108/*
5109 * Horizontal scrollbar movement.
5110 */
5111 static void
5112nv_hor_scrollbar(cap)
5113 cmdarg_T *cap;
5114{
5115 if (cap->oap->op_type != OP_NOP)
5116 clearopbeep(cap->oap);
5117
5118 /* Even if an operator was pending, we still want to scroll */
5119 gui_do_horiz_scroll();
5120}
5121#endif
5122
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005123#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005124/*
5125 * Click in GUI tab.
5126 */
5127 static void
5128nv_tabline(cap)
5129 cmdarg_T *cap;
5130{
5131 if (cap->oap->op_type != OP_NOP)
5132 clearopbeep(cap->oap);
5133
5134 /* Even if an operator was pending, we still want to jump tabs. */
5135 goto_tabpage(current_tab);
5136}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005137
5138/*
5139 * Selected item in tab line menu.
5140 */
5141 static void
5142nv_tabmenu(cap)
5143 cmdarg_T *cap;
5144{
5145 if (cap->oap->op_type != OP_NOP)
5146 clearopbeep(cap->oap);
5147
5148 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005149 handle_tabmenu();
5150}
5151
5152/*
5153 * Handle selecting an item of the GUI tab line menu.
5154 * Used in Normal and Insert mode.
5155 */
5156 void
5157handle_tabmenu()
5158{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005159 switch (current_tabmenu)
5160 {
5161 case TABLINE_MENU_CLOSE:
5162 if (current_tab == 0)
5163 do_cmdline_cmd((char_u *)"tabclose");
5164 else
5165 {
5166 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5167 current_tab);
5168 do_cmdline_cmd(IObuff);
5169 }
5170 break;
5171
5172 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005173 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5174 current_tab > 0 ? current_tab - 1 : 999);
5175 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005176 break;
5177
5178 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005179 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5180 current_tab > 0 ? current_tab - 1 : 999);
5181 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005182 break;
5183 }
5184}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005185#endif
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187/*
5188 * "Q" command.
5189 */
5190 static void
5191nv_exmode(cap)
5192 cmdarg_T *cap;
5193{
5194 /*
5195 * Ignore 'Q' in Visual mode, just give a beep.
5196 */
5197#ifdef FEAT_VISUAL
5198 if (VIsual_active)
5199 vim_beep();
5200 else
5201#endif
5202 if (!checkclearop(cap->oap))
5203 do_exmode(FALSE);
5204}
5205
5206/*
5207 * Handle a ":" command.
5208 */
5209 static void
5210nv_colon(cap)
5211 cmdarg_T *cap;
5212{
5213 int old_p_im;
5214
5215#ifdef FEAT_VISUAL
5216 if (VIsual_active)
5217 nv_operator(cap);
5218 else
5219#endif
5220 {
5221 if (cap->oap->op_type != OP_NOP)
5222 {
5223 /* Using ":" as a movement is characterwise exclusive. */
5224 cap->oap->motion_type = MCHAR;
5225 cap->oap->inclusive = FALSE;
5226 }
5227 else if (cap->count0)
5228 {
5229 /* translate "count:" into ":.,.+(count - 1)" */
5230 stuffcharReadbuff('.');
5231 if (cap->count0 > 1)
5232 {
5233 stuffReadbuff((char_u *)",.+");
5234 stuffnumReadbuff((long)cap->count0 - 1L);
5235 }
5236 }
5237
5238 /* When typing, don't type below an old message */
5239 if (KeyTyped)
5240 compute_cmdrow();
5241
5242 old_p_im = p_im;
5243
5244 /* get a command line and execute it */
5245 do_cmdline(NULL, getexline, NULL,
5246 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5247
5248 /* If 'insertmode' changed, enter or exit Insert mode */
5249 if (p_im != old_p_im)
5250 {
5251 if (p_im)
5252 restart_edit = 'i';
5253 else
5254 restart_edit = 0;
5255 }
5256
5257 /* The start of the operator may have become invalid by the Ex
5258 * command. */
5259 if (cap->oap->op_type != OP_NOP
5260 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5261 || cap->oap->start.col >
Bram Moolenaar78a15312009-05-15 19:33:18 +00005262 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 clearopbeep(cap->oap);
5264 }
5265}
5266
5267/*
5268 * Handle CTRL-G command.
5269 */
5270 static void
5271nv_ctrlg(cap)
5272 cmdarg_T *cap;
5273{
5274#ifdef FEAT_VISUAL
5275 if (VIsual_active) /* toggle Selection/Visual mode */
5276 {
5277 VIsual_select = !VIsual_select;
5278 showmode();
5279 }
5280 else
5281#endif
5282 if (!checkclearop(cap->oap))
5283 /* print full name if count given or :cd used */
5284 fileinfo((int)cap->count0, FALSE, TRUE);
5285}
5286
5287/*
5288 * Handle CTRL-H <Backspace> command.
5289 */
5290 static void
5291nv_ctrlh(cap)
5292 cmdarg_T *cap;
5293{
5294#ifdef FEAT_VISUAL
5295 if (VIsual_active && VIsual_select)
5296 {
5297 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5298 v_visop(cap);
5299 }
5300 else
5301#endif
5302 nv_left(cap);
5303}
5304
5305/*
5306 * CTRL-L: clear screen and redraw.
5307 */
5308 static void
5309nv_clear(cap)
5310 cmdarg_T *cap;
5311{
5312 if (!checkclearop(cap->oap))
5313 {
5314#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5315 /*
5316 * Right now, the BeBox doesn't seem to have an easy way to detect
5317 * window resizing, so we cheat and make the user detect it
5318 * manually with CTRL-L instead
5319 */
5320 ui_get_shellsize();
5321#endif
5322#ifdef FEAT_SYN_HL
5323 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005324 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325#endif
5326 redraw_later(CLEAR);
5327 }
5328}
5329
5330/*
5331 * CTRL-O: In Select mode: switch to Visual mode for one command.
5332 * Otherwise: Go to older pcmark.
5333 */
5334 static void
5335nv_ctrlo(cap)
5336 cmdarg_T *cap;
5337{
5338#ifdef FEAT_VISUAL
5339 if (VIsual_active && VIsual_select)
5340 {
5341 VIsual_select = FALSE;
5342 showmode();
5343 restart_VIsual_select = 2; /* restart Select mode later */
5344 }
5345 else
5346#endif
5347 {
5348 cap->count1 = -cap->count1;
5349 nv_pcmark(cap);
5350 }
5351}
5352
5353/*
5354 * CTRL-^ command, short for ":e #"
5355 */
5356 static void
5357nv_hat(cap)
5358 cmdarg_T *cap;
5359{
5360 if (!checkclearopq(cap->oap))
5361 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5362 GETF_SETMARK|GETF_ALT, FALSE);
5363}
5364
5365/*
5366 * "Z" commands.
5367 */
5368 static void
5369nv_Zet(cap)
5370 cmdarg_T *cap;
5371{
5372 if (!checkclearopq(cap->oap))
5373 {
5374 switch (cap->nchar)
5375 {
5376 /* "ZZ": equivalent to ":x". */
5377 case 'Z': do_cmdline_cmd((char_u *)"x");
5378 break;
5379
5380 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5381 case 'Q': do_cmdline_cmd((char_u *)"q!");
5382 break;
5383
5384 default: clearopbeep(cap->oap);
5385 }
5386 }
5387}
5388
5389#if defined(FEAT_WINDOWS) || defined(PROTO)
5390/*
5391 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5392 */
5393 void
5394do_nv_ident(c1, c2)
5395 int c1;
5396 int c2;
5397{
5398 oparg_T oa;
5399 cmdarg_T ca;
5400
5401 clear_oparg(&oa);
5402 vim_memset(&ca, 0, sizeof(ca));
5403 ca.oap = &oa;
5404 ca.cmdchar = c1;
5405 ca.nchar = c2;
5406 nv_ident(&ca);
5407}
5408#endif
5409
5410/*
5411 * Handle the commands that use the word under the cursor.
5412 * [g] CTRL-] :ta to current identifier
5413 * [g] 'K' run program for current identifier
5414 * [g] '*' / to current identifier or string
5415 * [g] '#' ? to current identifier or string
5416 * g ']' :tselect for current identifier
5417 */
5418 static void
5419nv_ident(cap)
5420 cmdarg_T *cap;
5421{
5422 char_u *ptr = NULL;
5423 char_u *buf;
5424 char_u *p;
5425 char_u *kp; /* value of 'keywordprg' */
5426 int kp_help; /* 'keywordprg' is ":help" */
5427 int n = 0; /* init for GCC */
5428 int cmdchar;
5429 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005430 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 char_u *aux_ptr;
5432 int isman;
5433 int isman_s;
5434
5435 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5436 {
5437 cmdchar = cap->nchar;
5438 g_cmd = TRUE;
5439 }
5440 else
5441 {
5442 cmdchar = cap->cmdchar;
5443 g_cmd = FALSE;
5444 }
5445
5446 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5447 cmdchar = '#';
5448
5449 /*
5450 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5451 */
5452 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5453 {
5454#ifdef FEAT_VISUAL
5455 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5456 return;
5457#endif
5458 if (checkclearopq(cap->oap))
5459 return;
5460 }
5461
5462 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5463 (cmdchar == '*' || cmdchar == '#')
5464 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5465 {
5466 clearop(cap->oap);
5467 return;
5468 }
5469
5470 /* Allocate buffer to put the command in. Inserting backslashes can
5471 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5472 * and some numbers. */
5473 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5474 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5475 || STRCMP(kp, ":help") == 0);
5476 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5477 if (buf == NULL)
5478 return;
5479 buf[0] = NUL;
5480
5481 switch (cmdchar)
5482 {
5483 case '*':
5484 case '#':
5485 /*
5486 * Put cursor at start of word, makes search skip the word
5487 * under the cursor.
5488 * Call setpcmark() first, so "*``" puts the cursor back where
5489 * it was.
5490 */
5491 setpcmark();
5492 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5493
5494 if (!g_cmd && vim_iswordp(ptr))
5495 STRCPY(buf, "\\<");
5496 no_smartcase = TRUE; /* don't use 'smartcase' now */
5497 break;
5498
5499 case 'K':
5500 if (kp_help)
5501 STRCPY(buf, "he! ");
5502 else
5503 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005504 /* An external command will probably use an argument starting
5505 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005506 while (*ptr == '-' && n > 0)
5507 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005508 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005509 --n;
5510 }
5511 if (n == 0)
5512 {
5513 EMSG(_(e_noident)); /* found dashes only */
5514 vim_free(buf);
5515 return;
5516 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005517
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 /* When a count is given, turn it into a range. Is this
5519 * really what we want? */
5520 isman = (STRCMP(kp, "man") == 0);
5521 isman_s = (STRCMP(kp, "man -s") == 0);
5522 if (cap->count0 != 0 && !(isman || isman_s))
5523 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5524
5525 STRCAT(buf, "! ");
5526 if (cap->count0 == 0 && isman_s)
5527 STRCAT(buf, "man");
5528 else
5529 STRCAT(buf, kp);
5530 STRCAT(buf, " ");
5531 if (cap->count0 != 0 && (isman || isman_s))
5532 {
5533 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5534 STRCAT(buf, " ");
5535 }
5536 }
5537 break;
5538
5539 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005540 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_CSCOPE
5542 if (p_cst)
5543 STRCPY(buf, "cstag ");
5544 else
5545#endif
5546 STRCPY(buf, "ts ");
5547 break;
5548
5549 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005550 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (curbuf->b_help)
5552 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005554 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005555 if (g_cmd)
5556 STRCPY(buf, "tj ");
5557 else
5558 sprintf((char *)buf, "%ldta ", cap->count0);
5559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561
5562 /*
5563 * Now grab the chars in the identifier
5564 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005565 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005567 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005568 ptr = vim_strnsave(ptr, n);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005569 p = vim_strsave_shellescape(ptr, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005570 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005573 vim_free(buf);
5574 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005576 buf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5577 if (buf == NULL)
5578 {
5579 vim_free(buf);
5580 vim_free(p);
5581 return;
5582 }
5583 STRCAT(buf, p);
5584 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005586 else
5587 {
5588 if (cmdchar == '*')
5589 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5590 else if (cmdchar == '#')
5591 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005592 else if (tag_cmd)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005593 /* Don't escape spaces and Tabs in a tag with a backslash */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005594 aux_ptr = (char_u *)"\\|\"\n[";
5595 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005596 aux_ptr = (char_u *)"\\|\"\n*?[";
5597
5598 p = buf + STRLEN(buf);
5599 while (n-- > 0)
5600 {
5601 /* put a backslash before \ and some others */
5602 if (vim_strchr(aux_ptr, *ptr) != NULL)
5603 *p++ = '\\';
5604#ifdef FEAT_MBYTE
5605 /* When current byte is a part of multibyte character, copy all
5606 * bytes of that character. */
5607 if (has_mbyte)
5608 {
5609 int i;
5610 int len = (*mb_ptr2len)(ptr) - 1;
5611
5612 for (i = 0; i < len && n >= 1; ++i, --n)
5613 *p++ = *ptr++;
5614 }
5615#endif
5616 *p++ = *ptr++;
5617 }
5618 *p = NUL;
5619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 /*
5622 * Execute the command.
5623 */
5624 if (cmdchar == '*' || cmdchar == '#')
5625 {
5626 if (!g_cmd && (
5627#ifdef FEAT_MBYTE
5628 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5629#endif
5630 vim_iswordc(ptr[-1])))
5631 STRCAT(buf, "\\>");
5632#ifdef FEAT_CMDHIST
5633 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005634 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5636#endif
5637 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5638 }
5639 else
5640 do_cmdline_cmd(buf);
5641
5642 vim_free(buf);
5643}
5644
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005645#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646/*
5647 * Get visually selected text, within one line only.
5648 * Returns FAIL if more than one line selected.
5649 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005650 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651get_visual_text(cap, pp, lenp)
5652 cmdarg_T *cap;
5653 char_u **pp; /* return: start of selected text */
5654 int *lenp; /* return: length of selected text */
5655{
5656 if (VIsual_mode != 'V')
5657 unadjust_for_sel();
5658 if (VIsual.lnum != curwin->w_cursor.lnum)
5659 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005660 if (cap != NULL)
5661 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 return FAIL;
5663 }
5664 if (VIsual_mode == 'V')
5665 {
5666 *pp = ml_get_curline();
5667 *lenp = (int)STRLEN(*pp);
5668 }
5669 else
5670 {
5671 if (lt(curwin->w_cursor, VIsual))
5672 {
5673 *pp = ml_get_pos(&curwin->w_cursor);
5674 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5675 }
5676 else
5677 {
5678 *pp = ml_get_pos(&VIsual);
5679 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5680 }
5681#ifdef FEAT_MBYTE
5682 if (has_mbyte)
5683 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005684 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685#endif
5686 }
5687 reset_VIsual_and_resel();
5688 return OK;
5689}
5690#endif
5691
5692/*
5693 * CTRL-T: backwards in tag stack
5694 */
5695 static void
5696nv_tagpop(cap)
5697 cmdarg_T *cap;
5698{
5699 if (!checkclearopq(cap->oap))
5700 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5701}
5702
5703/*
5704 * Handle scrolling command 'H', 'L' and 'M'.
5705 */
5706 static void
5707nv_scroll(cap)
5708 cmdarg_T *cap;
5709{
5710 int used = 0;
5711 long n;
5712#ifdef FEAT_FOLDING
5713 linenr_T lnum;
5714#endif
5715 int half;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005716#ifdef FEAT_CONCEAL
5717 linenr_T oldline = curwin->w_cursor.lnum;
5718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 cap->oap->motion_type = MLINE;
5721 setpcmark();
5722
5723 if (cap->cmdchar == 'L')
5724 {
5725 validate_botline(); /* make sure curwin->w_botline is valid */
5726 curwin->w_cursor.lnum = curwin->w_botline - 1;
5727 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5728 curwin->w_cursor.lnum = 1;
5729 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005730 {
5731#ifdef FEAT_FOLDING
5732 if (hasAnyFolding(curwin))
5733 {
5734 /* Count a fold for one screen line. */
5735 for (n = cap->count1 - 1; n > 0
5736 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5737 {
5738 (void)hasFolding(curwin->w_cursor.lnum,
5739 &curwin->w_cursor.lnum, NULL);
5740 --curwin->w_cursor.lnum;
5741 }
5742 }
5743 else
5744#endif
5745 curwin->w_cursor.lnum -= cap->count1 - 1;
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 else
5749 {
5750 if (cap->cmdchar == 'M')
5751 {
5752#ifdef FEAT_DIFF
5753 /* Don't count filler lines above the window. */
5754 used -= diff_check_fill(curwin, curwin->w_topline)
5755 - curwin->w_topfill;
5756#endif
5757 validate_botline(); /* make sure w_empty_rows is valid */
5758 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5759 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5760 {
5761#ifdef FEAT_DIFF
5762 /* Count half he number of filler lines to be "below this
5763 * line" and half to be "above the next line". */
5764 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5765 + n) / 2 >= half)
5766 {
5767 --n;
5768 break;
5769 }
5770#endif
5771 used += plines(curwin->w_topline + n);
5772 if (used >= half)
5773 break;
5774#ifdef FEAT_FOLDING
5775 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5776 n = lnum - curwin->w_topline;
5777#endif
5778 }
5779 if (n > 0 && used > curwin->w_height)
5780 --n;
5781 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005782 else /* (cap->cmdchar == 'H') */
5783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005785#ifdef FEAT_FOLDING
5786 if (hasAnyFolding(curwin))
5787 {
5788 /* Count a fold for one screen line. */
5789 lnum = curwin->w_topline;
5790 while (n-- > 0 && lnum < curwin->w_botline - 1)
5791 {
5792 hasFolding(lnum, NULL, &lnum);
5793 ++lnum;
5794 }
5795 n = lnum - curwin->w_topline;
5796 }
5797#endif
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 curwin->w_cursor.lnum = curwin->w_topline + n;
5800 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5801 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5802 }
5803
5804 cursor_correct(); /* correct for 'so' */
5805 beginline(BL_SOL | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005806#ifdef FEAT_CONCEAL
5807 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
5808 {
5809 update_single_line(curwin, oldline);
5810 update_single_line(curwin, curwin->w_cursor.lnum);
5811 }
5812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
5816 * Cursor right commands.
5817 */
5818 static void
5819nv_right(cap)
5820 cmdarg_T *cap;
5821{
5822 long n;
5823#ifdef FEAT_VISUAL
5824 int PAST_LINE;
5825#else
5826# define PAST_LINE 0
5827#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005828#ifdef FEAT_CONCEAL
5829 linenr_T oldline = curwin->w_cursor.lnum;
5830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005832 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5833 {
5834 /* <C-Right> and <S-Right> move a word or WORD right */
5835 if (mod_mask & MOD_MASK_CTRL)
5836 cap->arg = TRUE;
5837 nv_wordcmd(cap);
5838 return;
5839 }
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 cap->oap->motion_type = MCHAR;
5842 cap->oap->inclusive = FALSE;
5843#ifdef FEAT_VISUAL
5844 PAST_LINE = (VIsual_active && *p_sel != 'o');
5845
5846# ifdef FEAT_VIRTUALEDIT
5847 /*
5848 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005849 * (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 */
5851 if (virtual_active())
5852 PAST_LINE = 0;
5853# endif
5854#endif
5855
5856 for (n = cap->count1; n > 0; --n)
5857 {
5858 if ((!PAST_LINE && oneright() == FAIL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005859#ifdef FEAT_VISUAL
5860 || (PAST_LINE && *ml_get_cursor() == NUL)
5861#endif
5862 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
5864 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005865 * <Space> wraps to next line if 'whichwrap' has 's'.
5866 * 'l' wraps to next line if 'whichwrap' has 'l'.
5867 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 */
5869 if ( ((cap->cmdchar == ' '
5870 && vim_strchr(p_ww, 's') != NULL)
5871 || (cap->cmdchar == 'l'
5872 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005873 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 && vim_strchr(p_ww, '>') != NULL))
5875 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5876 {
5877 /* When deleting we also count the NL as a character.
5878 * Set cap->oap->inclusive when last char in the line is
5879 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005880 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 && !cap->oap->inclusive
5882 && !lineempty(curwin->w_cursor.lnum))
5883 cap->oap->inclusive = TRUE;
5884 else
5885 {
5886 ++curwin->w_cursor.lnum;
5887 curwin->w_cursor.col = 0;
5888#ifdef FEAT_VIRTUALEDIT
5889 curwin->w_cursor.coladd = 0;
5890#endif
5891 curwin->w_set_curswant = TRUE;
5892 cap->oap->inclusive = FALSE;
5893 }
5894 continue;
5895 }
5896 if (cap->oap->op_type == OP_NOP)
5897 {
5898 /* Only beep and flush if not moved at all */
5899 if (n == cap->count1)
5900 beep_flush();
5901 }
5902 else
5903 {
5904 if (!lineempty(curwin->w_cursor.lnum))
5905 cap->oap->inclusive = TRUE;
5906 }
5907 break;
5908 }
5909#ifdef FEAT_VISUAL
5910 else if (PAST_LINE)
5911 {
5912 curwin->w_set_curswant = TRUE;
5913# ifdef FEAT_VIRTUALEDIT
5914 if (virtual_active())
5915 oneright();
5916 else
5917# endif
5918 {
5919# ifdef FEAT_MBYTE
5920 if (has_mbyte)
5921 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005922 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 else
5924# endif
5925 ++curwin->w_cursor.col;
5926 }
5927 }
5928#endif
5929 }
5930#ifdef FEAT_FOLDING
5931 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5932 && cap->oap->op_type == OP_NOP)
5933 foldOpenCursor();
5934#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005935#ifdef FEAT_CONCEAL
5936 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
5937 {
5938 update_single_line(curwin, oldline);
5939 update_single_line(curwin, curwin->w_cursor.lnum);
5940 }
5941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942}
5943
5944/*
5945 * Cursor left commands.
5946 *
5947 * Returns TRUE when operator end should not be adjusted.
5948 */
5949 static void
5950nv_left(cap)
5951 cmdarg_T *cap;
5952{
5953 long n;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954#ifdef FEAT_CONCEAL
5955 linenr_T oldline = curwin->w_cursor.lnum;
5956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005958 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5959 {
5960 /* <C-Left> and <S-Left> move a word or WORD left */
5961 if (mod_mask & MOD_MASK_CTRL)
5962 cap->arg = 1;
5963 nv_bck_word(cap);
5964 return;
5965 }
5966
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cap->oap->motion_type = MCHAR;
5968 cap->oap->inclusive = FALSE;
5969 for (n = cap->count1; n > 0; --n)
5970 {
5971 if (oneleft() == FAIL)
5972 {
5973 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5974 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5975 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5976 */
5977 if ( (((cap->cmdchar == K_BS
5978 || cap->cmdchar == Ctrl_H)
5979 && vim_strchr(p_ww, 'b') != NULL)
5980 || (cap->cmdchar == 'h'
5981 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005982 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 && vim_strchr(p_ww, '<') != NULL))
5984 && curwin->w_cursor.lnum > 1)
5985 {
5986 --(curwin->w_cursor.lnum);
5987 coladvance((colnr_T)MAXCOL);
5988 curwin->w_set_curswant = TRUE;
5989
5990 /* When the NL before the first char has to be deleted we
5991 * put the cursor on the NUL after the previous line.
5992 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005993 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if ( (cap->oap->op_type == OP_DELETE
5995 || cap->oap->op_type == OP_CHANGE)
5996 && !lineempty(curwin->w_cursor.lnum))
5997 {
Bram Moolenaarad8958b2008-01-02 15:26:04 +00005998 if (*ml_get_cursor() != NUL)
5999 ++curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 cap->retval |= CA_NO_ADJ_OP_END;
6001 }
6002 continue;
6003 }
6004 /* Only beep and flush if not moved at all */
6005 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6006 beep_flush();
6007 break;
6008 }
6009 }
6010#ifdef FEAT_FOLDING
6011 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6012 && cap->oap->op_type == OP_NOP)
6013 foldOpenCursor();
6014#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006015#ifdef FEAT_CONCEAL
6016 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6017 {
6018 update_single_line(curwin, oldline);
6019 update_single_line(curwin, curwin->w_cursor.lnum);
6020 }
6021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022}
6023
6024/*
6025 * Cursor up commands.
6026 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6027 */
6028 static void
6029nv_up(cap)
6030 cmdarg_T *cap;
6031{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006032 if (mod_mask & MOD_MASK_SHIFT)
6033 {
6034 /* <S-Up> is page up */
6035 cap->arg = BACKWARD;
6036 nv_page(cap);
6037 }
6038 else
6039 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006040#ifdef FEAT_CONCEAL
6041 linenr_T oldline = curwin->w_cursor.lnum;
6042#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006043 cap->oap->motion_type = MLINE;
6044 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6045 clearopbeep(cap->oap);
6046 else if (cap->arg)
6047 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006048#ifdef FEAT_CONCEAL
6049 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6050 {
6051 update_single_line(curwin, oldline);
6052 update_single_line(curwin, curwin->w_cursor.lnum);
6053 }
6054#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056}
6057
6058/*
6059 * Cursor down commands.
6060 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6061 */
6062 static void
6063nv_down(cap)
6064 cmdarg_T *cap;
6065{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006066 if (mod_mask & MOD_MASK_SHIFT)
6067 {
6068 /* <S-Down> is page down */
6069 cap->arg = FORWARD;
6070 nv_page(cap);
6071 }
6072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6074 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006075 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006076 if (curwin->w_llist_ref == NULL)
6077 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6078 else
6079 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 else
6081#endif
6082 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006083#ifdef FEAT_CONCEAL
6084 linenr_T oldline = curwin->w_cursor.lnum;
6085 linenr_T oldbotline = curwin->w_botline;
6086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#ifdef FEAT_CMDWIN
6088 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006089 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 cmdwin_result = CAR;
6091 else
6092#endif
6093 {
6094 cap->oap->motion_type = MLINE;
6095 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6096 clearopbeep(cap->oap);
6097 else if (cap->arg)
6098 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006099#ifdef FEAT_CONCEAL
6100 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6101 {
6102 update_single_line(curwin, oldline);
6103 /* Don't do this if we've scrolled, the line is already
6104 * drawn */
6105 if (oldbotline == curwin->w_botline)
6106 update_single_line(curwin, curwin->w_cursor.lnum);
6107 }
6108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110 }
6111}
6112
6113#ifdef FEAT_SEARCHPATH
6114/*
6115 * Grab the file name under the cursor and edit it.
6116 */
6117 static void
6118nv_gotofile(cap)
6119 cmdarg_T *cap;
6120{
6121 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006122 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006124 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
6126 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006127 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 return;
6129 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006130#ifdef FEAT_AUTOCMD
6131 if (curbuf_locked())
6132 {
6133 clearop(cap->oap);
6134 return;
6135 }
6136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006138 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 if (ptr != NULL)
6141 {
6142 /* do autowrite if necessary */
6143 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6144 autowrite(curbuf, FALSE);
6145 setpcmark();
6146 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006147 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006148 if (cap->nchar == 'F' && lnum >= 0)
6149 {
6150 curwin->w_cursor.lnum = lnum;
6151 check_cursor_lnum();
6152 beginline(BL_SOL | BL_FIX);
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 vim_free(ptr);
6155 }
6156 else
6157 clearop(cap->oap);
6158}
6159#endif
6160
6161/*
6162 * <End> command: to end of current line or last line.
6163 */
6164 static void
6165nv_end(cap)
6166 cmdarg_T *cap;
6167{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006168 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006170 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 nv_goto(cap);
6172 cap->count1 = 1; /* to end of current line */
6173 }
6174 nv_dollar(cap);
6175}
6176
6177/*
6178 * Handle the "$" command.
6179 */
6180 static void
6181nv_dollar(cap)
6182 cmdarg_T *cap;
6183{
6184 cap->oap->motion_type = MCHAR;
6185 cap->oap->inclusive = TRUE;
6186#ifdef FEAT_VIRTUALEDIT
6187 /* In virtual mode when off the edge of a line and an operator
6188 * is pending (whew!) keep the cursor where it is.
6189 * Otherwise, send it to the end of the line. */
6190 if (!virtual_active() || gchar_cursor() != NUL
6191 || cap->oap->op_type == OP_NOP)
6192#endif
6193 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6194 if (cursor_down((long)(cap->count1 - 1),
6195 cap->oap->op_type == OP_NOP) == FAIL)
6196 clearopbeep(cap->oap);
6197#ifdef FEAT_FOLDING
6198 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6199 foldOpenCursor();
6200#endif
6201}
6202
6203/*
6204 * Implementation of '?' and '/' commands.
6205 * If cap->arg is TRUE don't set PC mark.
6206 */
6207 static void
6208nv_search(cap)
6209 cmdarg_T *cap;
6210{
6211 oparg_T *oap = cap->oap;
6212
6213 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6214 {
6215 /* Translate "g??" to "g?g?" */
6216 cap->cmdchar = 'g';
6217 cap->nchar = '?';
6218 nv_operator(cap);
6219 return;
6220 }
6221
6222 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6223
6224 if (cap->searchbuf == NULL)
6225 {
6226 clearop(oap);
6227 return;
6228 }
6229
6230 normal_search(cap, cap->cmdchar, cap->searchbuf,
6231 (cap->arg ? 0 : SEARCH_MARK));
6232}
6233
6234/*
6235 * Handle "N" and "n" commands.
6236 * cap->arg is SEARCH_REV for "N", 0 for "n".
6237 */
6238 static void
6239nv_next(cap)
6240 cmdarg_T *cap;
6241{
6242 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6243}
6244
6245/*
6246 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6247 * Uses only cap->count1 and cap->oap from "cap".
6248 */
6249 static void
6250normal_search(cap, dir, pat, opt)
6251 cmdarg_T *cap;
6252 int dir;
6253 char_u *pat;
6254 int opt; /* extra flags for do_search() */
6255{
6256 int i;
6257
6258 cap->oap->motion_type = MCHAR;
6259 cap->oap->inclusive = FALSE;
6260 cap->oap->use_reg_one = TRUE;
6261 curwin->w_set_curswant = TRUE;
6262
6263 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006264 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 if (i == 0)
6266 clearop(cap->oap);
6267 else
6268 {
6269 if (i == 2)
6270 cap->oap->motion_type = MLINE;
6271#ifdef FEAT_VIRTUALEDIT
6272 curwin->w_cursor.coladd = 0;
6273#endif
6274#ifdef FEAT_FOLDING
6275 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6276 foldOpenCursor();
6277#endif
6278 }
6279
6280 /* "/$" will put the cursor after the end of the line, may need to
6281 * correct that here */
6282 check_cursor();
6283}
6284
6285/*
6286 * Character search commands.
6287 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6288 * ',' and FALSE for ';'.
6289 * cap->nchar is NUL for ',' and ';' (repeat the search)
6290 */
6291 static void
6292nv_csearch(cap)
6293 cmdarg_T *cap;
6294{
6295 int t_cmd;
6296
6297 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6298 t_cmd = TRUE;
6299 else
6300 t_cmd = FALSE;
6301
6302 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6304 clearopbeep(cap->oap);
6305 else
6306 {
6307 curwin->w_set_curswant = TRUE;
6308#ifdef FEAT_VIRTUALEDIT
6309 /* Include a Tab for "tx" and for "dfx". */
6310 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6311 && (t_cmd || cap->oap->op_type != OP_NOP))
6312 {
6313 colnr_T scol, ecol;
6314
6315 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6316 curwin->w_cursor.coladd = ecol - scol;
6317 }
6318 else
6319 curwin->w_cursor.coladd = 0;
6320#endif
6321#ifdef FEAT_VISUAL
6322 adjust_for_sel(cap);
6323#endif
6324#ifdef FEAT_FOLDING
6325 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6326 foldOpenCursor();
6327#endif
6328 }
6329}
6330
6331/*
6332 * "[" and "]" commands.
6333 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6334 */
6335 static void
6336nv_brackets(cap)
6337 cmdarg_T *cap;
6338{
6339 pos_T new_pos;
6340 pos_T prev_pos;
6341 pos_T *pos = NULL; /* init for GCC */
6342 pos_T old_pos; /* cursor position before command */
6343 int flag;
6344 long n;
6345 int findc;
6346 int c;
6347
6348 cap->oap->motion_type = MCHAR;
6349 cap->oap->inclusive = FALSE;
6350 old_pos = curwin->w_cursor;
6351#ifdef FEAT_VIRTUALEDIT
6352 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6353#endif
6354
6355#ifdef FEAT_SEARCHPATH
6356 /*
6357 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6358 */
6359 if (cap->nchar == 'f')
6360 nv_gotofile(cap);
6361 else
6362#endif
6363
6364#ifdef FEAT_FIND_ID
6365 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006366 * Find the occurrence(s) of the identifier or define under cursor
6367 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 *
6369 * search list jump
6370 * fwd bwd fwd bwd fwd bwd
6371 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6372 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6373 */
6374 if (vim_strchr((char_u *)
6375#ifdef EBCDIC
6376 "iI\005dD\067",
6377#else
6378 "iI\011dD\004",
6379#endif
6380 cap->nchar) != NULL)
6381 {
6382 char_u *ptr;
6383 int len;
6384
6385 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6386 clearop(cap->oap);
6387 else
6388 {
6389 find_pattern_in_path(ptr, 0, len, TRUE,
6390 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6391 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6392 cap->count1,
6393 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6394 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6395 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6396 (linenr_T)MAXLNUM);
6397 curwin->w_set_curswant = TRUE;
6398 }
6399 }
6400 else
6401#endif
6402
6403 /*
6404 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6405 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6406 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6407 * "[m" or "]m" search for prev/next start of (Java) method.
6408 * "[M" or "]M" search for prev/next end of (Java) method.
6409 */
6410 if ( (cap->cmdchar == '['
6411 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6412 || (cap->cmdchar == ']'
6413 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6414 {
6415 if (cap->nchar == '*')
6416 cap->nchar = '/';
6417 new_pos.lnum = 0;
6418 prev_pos.lnum = 0;
6419 if (cap->nchar == 'm' || cap->nchar == 'M')
6420 {
6421 if (cap->cmdchar == '[')
6422 findc = '{';
6423 else
6424 findc = '}';
6425 n = 9999;
6426 }
6427 else
6428 {
6429 findc = cap->nchar;
6430 n = cap->count1;
6431 }
6432 for ( ; n > 0; --n)
6433 {
6434 if ((pos = findmatchlimit(cap->oap, findc,
6435 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6436 {
6437 if (new_pos.lnum == 0) /* nothing found */
6438 {
6439 if (cap->nchar != 'm' && cap->nchar != 'M')
6440 clearopbeep(cap->oap);
6441 }
6442 else
6443 pos = &new_pos; /* use last one found */
6444 break;
6445 }
6446 prev_pos = new_pos;
6447 curwin->w_cursor = *pos;
6448 new_pos = *pos;
6449 }
6450 curwin->w_cursor = old_pos;
6451
6452 /*
6453 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6454 * brought us to the match for "[m" and "]M" when inside a method.
6455 * Try finding the '{' or '}' we want to be at.
6456 * Also repeat for the given count.
6457 */
6458 if (cap->nchar == 'm' || cap->nchar == 'M')
6459 {
6460 /* norm is TRUE for "]M" and "[m" */
6461 int norm = ((findc == '{') == (cap->nchar == 'm'));
6462
6463 n = cap->count1;
6464 /* found a match: we were inside a method */
6465 if (prev_pos.lnum != 0)
6466 {
6467 pos = &prev_pos;
6468 curwin->w_cursor = prev_pos;
6469 if (norm)
6470 --n;
6471 }
6472 else
6473 pos = NULL;
6474 while (n > 0)
6475 {
6476 for (;;)
6477 {
6478 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6479 {
6480 /* if not found anything, that's an error */
6481 if (pos == NULL)
6482 clearopbeep(cap->oap);
6483 n = 0;
6484 break;
6485 }
6486 c = gchar_cursor();
6487 if (c == '{' || c == '}')
6488 {
6489 /* Must have found end/start of class: use it.
6490 * Or found the place to be at. */
6491 if ((c == findc && norm) || (n == 1 && !norm))
6492 {
6493 new_pos = curwin->w_cursor;
6494 pos = &new_pos;
6495 n = 0;
6496 }
6497 /* if no match found at all, we started outside of the
6498 * class and we're inside now. Just go on. */
6499 else if (new_pos.lnum == 0)
6500 {
6501 new_pos = curwin->w_cursor;
6502 pos = &new_pos;
6503 }
6504 /* found start/end of other method: go to match */
6505 else if ((pos = findmatchlimit(cap->oap, findc,
6506 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6507 0)) == NULL)
6508 n = 0;
6509 else
6510 curwin->w_cursor = *pos;
6511 break;
6512 }
6513 }
6514 --n;
6515 }
6516 curwin->w_cursor = old_pos;
6517 if (pos == NULL && new_pos.lnum != 0)
6518 clearopbeep(cap->oap);
6519 }
6520 if (pos != NULL)
6521 {
6522 setpcmark();
6523 curwin->w_cursor = *pos;
6524 curwin->w_set_curswant = TRUE;
6525#ifdef FEAT_FOLDING
6526 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6527 && cap->oap->op_type == OP_NOP)
6528 foldOpenCursor();
6529#endif
6530 }
6531 }
6532
6533 /*
6534 * "[[", "[]", "]]" and "][": move to start or end of function
6535 */
6536 else if (cap->nchar == '[' || cap->nchar == ']')
6537 {
6538 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6539 flag = '{';
6540 else
6541 flag = '}'; /* "][" or "[]" */
6542
6543 curwin->w_set_curswant = TRUE;
6544 /*
6545 * Imitate strange Vi behaviour: When using "]]" with an operator
6546 * we also stop at '}'.
6547 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006548 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 (cap->oap->op_type != OP_NOP
6550 && cap->arg == FORWARD && flag == '{')))
6551 clearopbeep(cap->oap);
6552 else
6553 {
6554 if (cap->oap->op_type == OP_NOP)
6555 beginline(BL_WHITE | BL_FIX);
6556#ifdef FEAT_FOLDING
6557 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6558 foldOpenCursor();
6559#endif
6560 }
6561 }
6562
6563 /*
6564 * "[p", "[P", "]P" and "]p": put with indent adjustment
6565 */
6566 else if (cap->nchar == 'p' || cap->nchar == 'P')
6567 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006568 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570 prep_redo_cmd(cap);
6571 do_put(cap->oap->regname,
6572 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6573 cap->count1, PUT_FIXINDENT);
6574 }
6575 }
6576
6577 /*
6578 * "['", "[`", "]'" and "]`": jump to next mark
6579 */
6580 else if (cap->nchar == '\'' || cap->nchar == '`')
6581 {
6582 pos = &curwin->w_cursor;
6583 for (n = cap->count1; n > 0; --n)
6584 {
6585 prev_pos = *pos;
6586 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6587 cap->nchar == '\'');
6588 if (pos == NULL)
6589 break;
6590 }
6591 if (pos == NULL)
6592 pos = &prev_pos;
6593 nv_cursormark(cap, cap->nchar == '\'', pos);
6594 }
6595
6596#ifdef FEAT_MOUSE
6597 /*
6598 * [ or ] followed by a middle mouse click: put selected text with
6599 * indent adjustment. Any other button just does as usual.
6600 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006601 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 {
6603 (void)do_mouse(cap->oap, cap->nchar,
6604 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6605 cap->count1, PUT_FIXINDENT);
6606 }
6607#endif /* FEAT_MOUSE */
6608
6609#ifdef FEAT_FOLDING
6610 /*
6611 * "[z" and "]z": move to start or end of open fold.
6612 */
6613 else if (cap->nchar == 'z')
6614 {
6615 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6616 cap->count1) == FAIL)
6617 clearopbeep(cap->oap);
6618 }
6619#endif
6620
6621#ifdef FEAT_DIFF
6622 /*
6623 * "[c" and "]c": move to next or previous diff-change.
6624 */
6625 else if (cap->nchar == 'c')
6626 {
6627 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6628 cap->count1) == FAIL)
6629 clearopbeep(cap->oap);
6630 }
6631#endif
6632
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006633#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634 /*
6635 * "[s", "[S", "]s" and "]S": move to next spell error.
6636 */
6637 else if (cap->nchar == 's' || cap->nchar == 'S')
6638 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006639 setpcmark();
6640 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006641 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6642 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006643 {
6644 clearopbeep(cap->oap);
6645 break;
6646 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006647# ifdef FEAT_FOLDING
6648 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6649 foldOpenCursor();
6650# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006651 }
6652#endif
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 /* Not a valid cap->nchar. */
6655 else
6656 clearopbeep(cap->oap);
6657}
6658
6659/*
6660 * Handle Normal mode "%" command.
6661 */
6662 static void
6663nv_percent(cap)
6664 cmdarg_T *cap;
6665{
6666 pos_T *pos;
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02006667#if defined(FEAT_FOLDING) || defined(FEAT_CONCEAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 linenr_T lnum = curwin->w_cursor.lnum;
6669#endif
6670
6671 cap->oap->inclusive = TRUE;
6672 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6673 {
6674 if (cap->count0 > 100)
6675 clearopbeep(cap->oap);
6676 else
6677 {
6678 cap->oap->motion_type = MLINE;
6679 setpcmark();
6680 /* Round up, so CTRL-G will give same value. Watch out for a
6681 * large line count, the line number must not go negative! */
6682 if (curbuf->b_ml.ml_line_count > 1000000)
6683 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6684 / 100L * cap->count0;
6685 else
6686 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6687 cap->count0 + 99L) / 100L;
6688 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6689 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6690 beginline(BL_SOL | BL_FIX);
6691 }
6692 }
6693 else /* "%" : go to matching paren */
6694 {
6695 cap->oap->motion_type = MCHAR;
6696 cap->oap->use_reg_one = TRUE;
6697 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6698 clearopbeep(cap->oap);
6699 else
6700 {
6701 setpcmark();
6702 curwin->w_cursor = *pos;
6703 curwin->w_set_curswant = TRUE;
6704#ifdef FEAT_VIRTUALEDIT
6705 curwin->w_cursor.coladd = 0;
6706#endif
6707#ifdef FEAT_VISUAL
6708 adjust_for_sel(cap);
6709#endif
6710 }
6711 }
6712#ifdef FEAT_FOLDING
6713 if (cap->oap->op_type == OP_NOP
6714 && lnum != curwin->w_cursor.lnum
6715 && (fdo_flags & FDO_PERCENT)
6716 && KeyTyped)
6717 foldOpenCursor();
6718#endif
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02006719#ifdef FEAT_CONCEAL
6720 if (curwin->w_p_conceal && lnum != curwin->w_cursor.lnum)
6721 {
6722 update_single_line(curwin, lnum);
6723 update_single_line(curwin, curwin->w_cursor.lnum);
6724 }
6725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726}
6727
6728/*
6729 * Handle "(" and ")" commands.
6730 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6731 */
6732 static void
6733nv_brace(cap)
6734 cmdarg_T *cap;
6735{
6736 cap->oap->motion_type = MCHAR;
6737 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006738 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6739 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 curwin->w_set_curswant = TRUE;
6741
6742 if (findsent(cap->arg, cap->count1) == FAIL)
6743 clearopbeep(cap->oap);
6744 else
6745 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006746 /* Don't leave the cursor on the NUL past end of line. */
6747 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#ifdef FEAT_VIRTUALEDIT
6749 curwin->w_cursor.coladd = 0;
6750#endif
6751#ifdef FEAT_FOLDING
6752 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6753 foldOpenCursor();
6754#endif
6755 }
6756}
6757
6758/*
6759 * "m" command: Mark a position.
6760 */
6761 static void
6762nv_mark(cap)
6763 cmdarg_T *cap;
6764{
6765 if (!checkclearop(cap->oap))
6766 {
6767 if (setmark(cap->nchar) == FAIL)
6768 clearopbeep(cap->oap);
6769 }
6770}
6771
6772/*
6773 * "{" and "}" commands.
6774 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6775 */
6776 static void
6777nv_findpar(cap)
6778 cmdarg_T *cap;
6779{
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02006780#ifdef FEAT_CONCEAL
6781 linenr_T oldline = curwin->w_cursor.lnum;
6782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 cap->oap->motion_type = MCHAR;
6784 cap->oap->inclusive = FALSE;
6785 cap->oap->use_reg_one = TRUE;
6786 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006787 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 clearopbeep(cap->oap);
6789 else
6790 {
6791#ifdef FEAT_VIRTUALEDIT
6792 curwin->w_cursor.coladd = 0;
6793#endif
6794#ifdef FEAT_FOLDING
6795 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6796 foldOpenCursor();
6797#endif
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02006798#ifdef FEAT_CONCEAL
6799 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
6800 {
6801 update_single_line(curwin, oldline);
6802 update_single_line(curwin, curwin->w_cursor.lnum);
6803 }
6804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 }
6806}
6807
6808/*
6809 * "u" command: Undo or make lower case.
6810 */
6811 static void
6812nv_undo(cap)
6813 cmdarg_T *cap;
6814{
6815 if (cap->oap->op_type == OP_LOWER
6816#ifdef FEAT_VISUAL
6817 || VIsual_active
6818#endif
6819 )
6820 {
6821 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6822 cap->cmdchar = 'g';
6823 cap->nchar = 'u';
6824 nv_operator(cap);
6825 }
6826 else
6827 nv_kundo(cap);
6828}
6829
6830/*
6831 * <Undo> command.
6832 */
6833 static void
6834nv_kundo(cap)
6835 cmdarg_T *cap;
6836{
6837 if (!checkclearopq(cap->oap))
6838 {
6839 u_undo((int)cap->count1);
6840 curwin->w_set_curswant = TRUE;
6841 }
6842}
6843
6844/*
6845 * Handle the "r" command.
6846 */
6847 static void
6848nv_replace(cap)
6849 cmdarg_T *cap;
6850{
6851 char_u *ptr;
6852 int had_ctrl_v;
6853 long n;
6854
6855 if (checkclearop(cap->oap))
6856 return;
6857
6858 /* get another character */
6859 if (cap->nchar == Ctrl_V)
6860 {
6861 had_ctrl_v = Ctrl_V;
6862 cap->nchar = get_literal();
6863 /* Don't redo a multibyte character with CTRL-V. */
6864 if (cap->nchar > DEL)
6865 had_ctrl_v = NUL;
6866 }
6867 else
6868 had_ctrl_v = NUL;
6869
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006870 /* Abort if the character is a special key. */
6871 if (IS_SPECIAL(cap->nchar))
6872 {
6873 clearopbeep(cap->oap);
6874 return;
6875 }
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877#ifdef FEAT_VISUAL
6878 /* Visual mode "r" */
6879 if (VIsual_active)
6880 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006881 if (got_int)
6882 reset_VIsual();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 nv_operator(cap);
6884 return;
6885 }
6886#endif
6887
6888#ifdef FEAT_VIRTUALEDIT
6889 /* Break tabs, etc. */
6890 if (virtual_active())
6891 {
6892 if (u_save_cursor() == FAIL)
6893 return;
6894 if (gchar_cursor() == NUL)
6895 {
6896 /* Add extra space and put the cursor on the first one. */
6897 coladvance_force((colnr_T)(getviscol() + cap->count1));
6898 curwin->w_cursor.col -= cap->count1;
6899 }
6900 else if (gchar_cursor() == TAB)
6901 coladvance_force(getviscol());
6902 }
6903#endif
6904
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006905 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006907 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908#ifdef FEAT_MBYTE
6909 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6910#endif
6911 )
6912 {
6913 clearopbeep(cap->oap);
6914 return;
6915 }
6916
6917 /*
6918 * Replacing with a TAB is done by edit() when it is complicated because
6919 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6920 * Other characters are done below to avoid problems with things like
6921 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6922 */
6923 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6924 {
6925 stuffnumReadbuff(cap->count1);
6926 stuffcharReadbuff('R');
6927 stuffcharReadbuff('\t');
6928 stuffcharReadbuff(ESC);
6929 return;
6930 }
6931
6932 /* save line for undo */
6933 if (u_save_cursor() == FAIL)
6934 return;
6935
6936 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6937 {
6938 /*
6939 * Replace character(s) by a single newline.
6940 * Strange vi behaviour: Only one newline is inserted.
6941 * Delete the characters here.
6942 * Insert the newline with an insert command, takes care of
6943 * autoindent. The insert command depends on being on the last
6944 * character of a line or not.
6945 */
6946#ifdef FEAT_MBYTE
6947 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6948#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006949 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950#endif
6951 stuffcharReadbuff('\r');
6952 stuffcharReadbuff(ESC);
6953
6954 /* Give 'r' to edit(), to get the redo command right. */
6955 invoke_edit(cap, TRUE, 'r', FALSE);
6956 }
6957 else
6958 {
6959 prep_redo(cap->oap->regname, cap->count1,
6960 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6961
6962 curbuf->b_op_start = curwin->w_cursor;
6963#ifdef FEAT_MBYTE
6964 if (has_mbyte)
6965 {
6966 int old_State = State;
6967
6968 if (cap->ncharC1 != 0)
6969 AppendCharToRedobuff(cap->ncharC1);
6970 if (cap->ncharC2 != 0)
6971 AppendCharToRedobuff(cap->ncharC2);
6972
6973 /* This is slow, but it handles replacing a single-byte with a
6974 * multi-byte and the other way around. Also handles adding
6975 * composing characters for utf-8. */
6976 for (n = cap->count1; n > 0; --n)
6977 {
6978 State = REPLACE;
6979 ins_char(cap->nchar);
6980 State = old_State;
6981 if (cap->ncharC1 != 0)
6982 ins_char(cap->ncharC1);
6983 if (cap->ncharC2 != 0)
6984 ins_char(cap->ncharC2);
6985 }
6986 }
6987 else
6988#endif
6989 {
6990 /*
6991 * Replace the characters within one line.
6992 */
6993 for (n = cap->count1; n > 0; --n)
6994 {
6995 /*
6996 * Get ptr again, because u_save and/or showmatch() will have
6997 * released the line. At the same time we let know that the
6998 * line will be changed.
6999 */
7000 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
7001 ptr[curwin->w_cursor.col] = cap->nchar;
7002 if (p_sm && msg_silent == 0)
7003 showmatch(cap->nchar);
7004 ++curwin->w_cursor.col;
7005 }
7006#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007007 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007009 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
Bram Moolenaar009b2592004-10-24 19:18:58 +00007011 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007012 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007014 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 }
7016#endif
7017
7018 /* mark the buffer as changed and prepare for displaying */
7019 changed_bytes(curwin->w_cursor.lnum,
7020 (colnr_T)(curwin->w_cursor.col - cap->count1));
7021 }
7022 --curwin->w_cursor.col; /* cursor on the last replaced char */
7023#ifdef FEAT_MBYTE
7024 /* if the character on the left of the current cursor is a multi-byte
7025 * character, move two characters left */
7026 if (has_mbyte)
7027 mb_adjust_cursor();
7028#endif
7029 curbuf->b_op_end = curwin->w_cursor;
7030 curwin->w_set_curswant = TRUE;
7031 set_last_insert(cap->nchar);
7032 }
7033}
7034
7035#ifdef FEAT_VISUAL
7036/*
7037 * 'o': Exchange start and end of Visual area.
7038 * 'O': same, but in block mode exchange left and right corners.
7039 */
7040 static void
7041v_swap_corners(cmdchar)
7042 int cmdchar;
7043{
7044 pos_T old_cursor;
7045 colnr_T left, right;
7046
7047 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7048 {
7049 old_cursor = curwin->w_cursor;
7050 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7051 curwin->w_cursor.lnum = VIsual.lnum;
7052 coladvance(left);
7053 VIsual = curwin->w_cursor;
7054
7055 curwin->w_cursor.lnum = old_cursor.lnum;
7056 curwin->w_curswant = right;
7057 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7058 * right one column */
7059 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7060 ++curwin->w_curswant;
7061 coladvance(curwin->w_curswant);
7062 if (curwin->w_cursor.col == old_cursor.col
7063#ifdef FEAT_VIRTUALEDIT
7064 && (!virtual_active()
7065 || curwin->w_cursor.coladd == old_cursor.coladd)
7066#endif
7067 )
7068 {
7069 curwin->w_cursor.lnum = VIsual.lnum;
7070 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7071 ++right;
7072 coladvance(right);
7073 VIsual = curwin->w_cursor;
7074
7075 curwin->w_cursor.lnum = old_cursor.lnum;
7076 coladvance(left);
7077 curwin->w_curswant = left;
7078 }
7079 }
7080 else
7081 {
7082 old_cursor = curwin->w_cursor;
7083 curwin->w_cursor = VIsual;
7084 VIsual = old_cursor;
7085 curwin->w_set_curswant = TRUE;
7086 }
7087}
7088#endif /* FEAT_VISUAL */
7089
7090/*
7091 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7092 */
7093 static void
7094nv_Replace(cap)
7095 cmdarg_T *cap;
7096{
7097#ifdef FEAT_VISUAL
7098 if (VIsual_active) /* "R" is replace lines */
7099 {
7100 cap->cmdchar = 'c';
7101 cap->nchar = NUL;
7102 VIsual_mode = 'V';
7103 nv_operator(cap);
7104 }
7105 else
7106#endif
7107 if (!checkclearopq(cap->oap))
7108 {
7109 if (!curbuf->b_p_ma)
7110 EMSG(_(e_modifiable));
7111 else
7112 {
7113#ifdef FEAT_VIRTUALEDIT
7114 if (virtual_active())
7115 coladvance(getviscol());
7116#endif
7117 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7118 }
7119 }
7120}
7121
7122#ifdef FEAT_VREPLACE
7123/*
7124 * "gr".
7125 */
7126 static void
7127nv_vreplace(cap)
7128 cmdarg_T *cap;
7129{
7130# ifdef FEAT_VISUAL
7131 if (VIsual_active)
7132 {
7133 cap->cmdchar = 'r';
7134 cap->nchar = cap->extra_char;
7135 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7136 }
7137 else
7138# endif
7139 if (!checkclearopq(cap->oap))
7140 {
7141 if (!curbuf->b_p_ma)
7142 EMSG(_(e_modifiable));
7143 else
7144 {
7145 if (cap->extra_char == Ctrl_V) /* get another character */
7146 cap->extra_char = get_literal();
7147 stuffcharReadbuff(cap->extra_char);
7148 stuffcharReadbuff(ESC);
7149# ifdef FEAT_VIRTUALEDIT
7150 if (virtual_active())
7151 coladvance(getviscol());
7152# endif
7153 invoke_edit(cap, TRUE, 'v', FALSE);
7154 }
7155 }
7156}
7157#endif
7158
7159/*
7160 * Swap case for "~" command, when it does not work like an operator.
7161 */
7162 static void
7163n_swapchar(cap)
7164 cmdarg_T *cap;
7165{
7166 long n;
7167 pos_T startpos;
7168 int did_change = 0;
7169#ifdef FEAT_NETBEANS_INTG
7170 pos_T pos;
7171 char_u *ptr;
7172 int count;
7173#endif
7174
7175 if (checkclearopq(cap->oap))
7176 return;
7177
7178 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7179 {
7180 clearopbeep(cap->oap);
7181 return;
7182 }
7183
7184 prep_redo_cmd(cap);
7185
7186 if (u_save_cursor() == FAIL)
7187 return;
7188
7189 startpos = curwin->w_cursor;
7190#ifdef FEAT_NETBEANS_INTG
7191 pos = startpos;
7192#endif
7193 for (n = cap->count1; n > 0; --n)
7194 {
7195 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7196 inc_cursor();
7197 if (gchar_cursor() == NUL)
7198 {
7199 if (vim_strchr(p_ww, '~') != NULL
7200 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7201 {
7202#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007203 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {
7205 if (did_change)
7206 {
7207 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007208 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007209 netbeans_removed(curbuf, pos.lnum, pos.col,
7210 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007212 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214 pos.col = 0;
7215 pos.lnum++;
7216 }
7217#endif
7218 ++curwin->w_cursor.lnum;
7219 curwin->w_cursor.col = 0;
7220 if (n > 1)
7221 {
7222 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7223 break;
7224 u_clearline();
7225 }
7226 }
7227 else
7228 break;
7229 }
7230 }
7231#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007232 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 ptr = ml_get(pos.lnum);
7235 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007236 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7237 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239#endif
7240
7241
7242 check_cursor();
7243 curwin->w_set_curswant = TRUE;
7244 if (did_change)
7245 {
7246 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7247 0L);
7248 curbuf->b_op_start = startpos;
7249 curbuf->b_op_end = curwin->w_cursor;
7250 if (curbuf->b_op_end.col > 0)
7251 --curbuf->b_op_end.col;
7252 }
7253}
7254
7255/*
7256 * Move cursor to mark.
7257 */
7258 static void
7259nv_cursormark(cap, flag, pos)
7260 cmdarg_T *cap;
7261 int flag;
7262 pos_T *pos;
7263{
7264 if (check_mark(pos) == FAIL)
7265 clearop(cap->oap);
7266 else
7267 {
7268 if (cap->cmdchar == '\''
7269 || cap->cmdchar == '`'
7270 || cap->cmdchar == '['
7271 || cap->cmdchar == ']')
7272 setpcmark();
7273 curwin->w_cursor = *pos;
7274 if (flag)
7275 beginline(BL_WHITE | BL_FIX);
7276 else
7277 check_cursor();
7278 }
7279 cap->oap->motion_type = flag ? MLINE : MCHAR;
7280 if (cap->cmdchar == '`')
7281 cap->oap->use_reg_one = TRUE;
7282 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7283 curwin->w_set_curswant = TRUE;
7284}
7285
7286#ifdef FEAT_VISUAL
7287/*
7288 * Handle commands that are operators in Visual mode.
7289 */
7290 static void
7291v_visop(cap)
7292 cmdarg_T *cap;
7293{
7294 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7295
7296 /* Uppercase means linewise, except in block mode, then "D" deletes till
7297 * the end of the line, and "C" replaces til EOL */
7298 if (isupper(cap->cmdchar))
7299 {
7300 if (VIsual_mode != Ctrl_V)
7301 VIsual_mode = 'V';
7302 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7303 curwin->w_curswant = MAXCOL;
7304 }
7305 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7306 nv_operator(cap);
7307}
7308#endif
7309
7310/*
7311 * "s" and "S" commands.
7312 */
7313 static void
7314nv_subst(cap)
7315 cmdarg_T *cap;
7316{
7317#ifdef FEAT_VISUAL
7318 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7319 {
7320 if (cap->cmdchar == 'S')
7321 VIsual_mode = 'V';
7322 cap->cmdchar = 'c';
7323 nv_operator(cap);
7324 }
7325 else
7326#endif
7327 nv_optrans(cap);
7328}
7329
7330/*
7331 * Abbreviated commands.
7332 */
7333 static void
7334nv_abbrev(cap)
7335 cmdarg_T *cap;
7336{
7337 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7338 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7339
7340#ifdef FEAT_VISUAL
7341 /* in Visual mode these commands are operators */
7342 if (VIsual_active)
7343 v_visop(cap);
7344 else
7345#endif
7346 nv_optrans(cap);
7347}
7348
7349/*
7350 * Translate a command into another command.
7351 */
7352 static void
7353nv_optrans(cap)
7354 cmdarg_T *cap;
7355{
7356 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7357 (char_u *)"d$", (char_u *)"c$",
7358 (char_u *)"cl", (char_u *)"cc",
7359 (char_u *)"yy", (char_u *)":s\r"};
7360 static char_u *str = (char_u *)"xXDCsSY&";
7361
7362 if (!checkclearopq(cap->oap))
7363 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007364 /* In Vi "2D" doesn't delete the next line. Can't translate it
7365 * either, because "2." should also not use the count. */
7366 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7367 {
7368 cap->oap->start = curwin->w_cursor;
7369 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007370#ifdef FEAT_EVAL
7371 set_op_var(OP_DELETE);
7372#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007373 cap->count1 = 1;
7374 nv_dollar(cap);
7375 finish_op = TRUE;
7376 ResetRedobuff();
7377 AppendCharToRedobuff('D');
7378 }
7379 else
7380 {
7381 if (cap->count0)
7382 stuffnumReadbuff(cap->count0);
7383 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386 cap->opcount = 0;
7387}
7388
7389/*
7390 * "'" and "`" commands. Also for "g'" and "g`".
7391 * cap->arg is TRUE for "'" and "g'".
7392 */
7393 static void
7394nv_gomark(cap)
7395 cmdarg_T *cap;
7396{
7397 pos_T *pos;
7398 int c;
7399#ifdef FEAT_FOLDING
7400 linenr_T lnum = curwin->w_cursor.lnum;
7401 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7402#endif
7403
7404 if (cap->cmdchar == 'g')
7405 c = cap->extra_char;
7406 else
7407 c = cap->nchar;
7408 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7409 if (pos == (pos_T *)-1) /* jumped to other file */
7410 {
7411 if (cap->arg)
7412 {
7413 check_cursor_lnum();
7414 beginline(BL_WHITE | BL_FIX);
7415 }
7416 else
7417 check_cursor();
7418 }
7419 else
7420 nv_cursormark(cap, cap->arg, pos);
7421
7422#ifdef FEAT_VIRTUALEDIT
7423 /* May need to clear the coladd that a mark includes. */
7424 if (!virtual_active())
7425 curwin->w_cursor.coladd = 0;
7426#endif
7427#ifdef FEAT_FOLDING
7428 if (cap->oap->op_type == OP_NOP
7429 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7430 && (fdo_flags & FDO_MARK)
7431 && old_KeyTyped)
7432 foldOpenCursor();
7433#endif
7434}
7435
7436/*
7437 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7438 */
7439 static void
7440nv_pcmark(cap)
7441 cmdarg_T *cap;
7442{
7443#ifdef FEAT_JUMPLIST
7444 pos_T *pos;
7445# ifdef FEAT_FOLDING
7446 linenr_T lnum = curwin->w_cursor.lnum;
7447 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7448# endif
7449
7450 if (!checkclearopq(cap->oap))
7451 {
7452 if (cap->cmdchar == 'g')
7453 pos = movechangelist((int)cap->count1);
7454 else
7455 pos = movemark((int)cap->count1);
7456 if (pos == (pos_T *)-1) /* jump to other file */
7457 {
7458 curwin->w_set_curswant = TRUE;
7459 check_cursor();
7460 }
7461 else if (pos != NULL) /* can jump */
7462 nv_cursormark(cap, FALSE, pos);
7463 else if (cap->cmdchar == 'g')
7464 {
7465 if (curbuf->b_changelistlen == 0)
7466 EMSG(_("E664: changelist is empty"));
7467 else if (cap->count1 < 0)
7468 EMSG(_("E662: At start of changelist"));
7469 else
7470 EMSG(_("E663: At end of changelist"));
7471 }
7472 else
7473 clearopbeep(cap->oap);
7474# ifdef FEAT_FOLDING
7475 if (cap->oap->op_type == OP_NOP
7476 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7477 && (fdo_flags & FDO_MARK)
7478 && old_KeyTyped)
7479 foldOpenCursor();
7480# endif
7481 }
7482#else
7483 clearopbeep(cap->oap);
7484#endif
7485}
7486
7487/*
7488 * Handle '"' command.
7489 */
7490 static void
7491nv_regname(cap)
7492 cmdarg_T *cap;
7493{
7494 if (checkclearop(cap->oap))
7495 return;
7496#ifdef FEAT_EVAL
7497 if (cap->nchar == '=')
7498 cap->nchar = get_expr_register();
7499#endif
7500 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7501 {
7502 cap->oap->regname = cap->nchar;
7503 cap->opcount = cap->count0; /* remember count before '"' */
7504#ifdef FEAT_EVAL
7505 set_reg_var(cap->oap->regname);
7506#endif
7507 }
7508 else
7509 clearopbeep(cap->oap);
7510}
7511
7512#ifdef FEAT_VISUAL
7513/*
7514 * Handle "v", "V" and "CTRL-V" commands.
7515 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7516 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007517 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 */
7519 static void
7520nv_visual(cap)
7521 cmdarg_T *cap;
7522{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523 if (cap->cmdchar == Ctrl_Q)
7524 cap->cmdchar = Ctrl_V;
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7527 * characterwise, linewise, or blockwise. */
7528 if (cap->oap->op_type != OP_NOP)
7529 {
7530 cap->oap->motion_force = cap->cmdchar;
7531 finish_op = FALSE; /* operator doesn't finish now but later */
7532 return;
7533 }
7534
7535 VIsual_select = cap->arg;
7536 if (VIsual_active) /* change Visual mode */
7537 {
7538 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7539 end_visual_mode();
7540 else /* toggle char/block mode */
7541 { /* or char/line mode */
7542 VIsual_mode = cap->cmdchar;
7543 showmode();
7544 }
7545 redraw_curbuf_later(INVERTED); /* update the inversion */
7546 }
7547 else /* start Visual mode */
7548 {
7549 check_visual_highlight();
7550 if (cap->count0) /* use previously selected part */
7551 {
7552 if (resel_VIsual_mode == NUL) /* there is none */
7553 {
7554 beep_flush();
7555 return;
7556 }
7557 VIsual = curwin->w_cursor;
7558
7559 VIsual_active = TRUE;
7560 VIsual_reselect = TRUE;
7561 if (!cap->arg)
7562 /* start Select mode when 'selectmode' contains "cmd" */
7563 may_start_select('c');
7564#ifdef FEAT_MOUSE
7565 setmouse();
7566#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007567 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 redraw_cmdline = TRUE; /* show visual mode later */
7569 /*
7570 * For V and ^V, we multiply the number of lines even if there
7571 * was only one -- webb
7572 */
7573 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7574 {
7575 curwin->w_cursor.lnum +=
7576 resel_VIsual_line_count * cap->count0 - 1;
7577 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7578 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7579 }
7580 VIsual_mode = resel_VIsual_mode;
7581 if (VIsual_mode == 'v')
7582 {
7583 if (resel_VIsual_line_count <= 1)
7584 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7585 else
7586 curwin->w_cursor.col = resel_VIsual_col;
7587 check_cursor_col();
7588 }
7589 if (resel_VIsual_col == MAXCOL)
7590 {
7591 curwin->w_curswant = MAXCOL;
7592 coladvance((colnr_T)MAXCOL);
7593 }
7594 else if (VIsual_mode == Ctrl_V)
7595 {
7596 validate_virtcol();
7597 curwin->w_curswant = curwin->w_virtcol
7598 + resel_VIsual_col * cap->count0 - 1;
7599 coladvance(curwin->w_curswant);
7600 }
7601 else
7602 curwin->w_set_curswant = TRUE;
7603 redraw_curbuf_later(INVERTED); /* show the inversion */
7604 }
7605 else
7606 {
7607 if (!cap->arg)
7608 /* start Select mode when 'selectmode' contains "cmd" */
7609 may_start_select('c');
7610 n_start_visual_mode(cap->cmdchar);
7611 }
7612 }
7613}
7614
7615/*
7616 * Start selection for Shift-movement keys.
7617 */
7618 void
7619start_selection()
7620{
7621 /* if 'selectmode' contains "key", start Select mode */
7622 may_start_select('k');
7623 n_start_visual_mode('v');
7624}
7625
7626/*
7627 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7628 */
7629 void
7630may_start_select(c)
7631 int c;
7632{
7633 VIsual_select = (stuff_empty() && typebuf_typed()
7634 && (vim_strchr(p_slm, c) != NULL));
7635}
7636
7637/*
7638 * Start Visual mode "c".
7639 * Should set VIsual_select before calling this.
7640 */
7641 static void
7642n_start_visual_mode(c)
7643 int c;
7644{
7645 VIsual_mode = c;
7646 VIsual_active = TRUE;
7647 VIsual_reselect = TRUE;
7648#ifdef FEAT_VIRTUALEDIT
7649 /* Corner case: the 0 position in a tab may change when going into
7650 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7651 */
7652 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7653 coladvance(curwin->w_virtcol);
7654#endif
7655 VIsual = curwin->w_cursor;
7656
7657#ifdef FEAT_FOLDING
7658 foldAdjustVisual();
7659#endif
7660
7661#ifdef FEAT_MOUSE
7662 setmouse();
7663#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007664 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 redraw_cmdline = TRUE; /* show visual mode later */
7666#ifdef FEAT_CLIPBOARD
7667 /* Make sure the clipboard gets updated. Needed because start and
7668 * end may still be the same, and the selection needs to be owned */
7669 clip_star.vmode = NUL;
7670#endif
7671
7672 /* Only need to redraw this line, unless still need to redraw an old
7673 * Visual area (when 'lazyredraw' is set). */
7674 if (curwin->w_redr_type < INVERTED)
7675 {
7676 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7677 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7678 }
7679}
7680
7681#endif /* FEAT_VISUAL */
7682
7683/*
7684 * CTRL-W: Window commands
7685 */
7686 static void
7687nv_window(cap)
7688 cmdarg_T *cap;
7689{
7690#ifdef FEAT_WINDOWS
7691 if (!checkclearop(cap->oap))
7692 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7693#else
7694 (void)checkclearop(cap->oap);
7695#endif
7696}
7697
7698/*
7699 * CTRL-Z: Suspend
7700 */
7701 static void
7702nv_suspend(cap)
7703 cmdarg_T *cap;
7704{
7705 clearop(cap->oap);
7706#ifdef FEAT_VISUAL
7707 if (VIsual_active)
7708 end_visual_mode(); /* stop Visual mode */
7709#endif
7710 do_cmdline_cmd((char_u *)"st");
7711}
7712
7713/*
7714 * Commands starting with "g".
7715 */
7716 static void
7717nv_g_cmd(cap)
7718 cmdarg_T *cap;
7719{
7720 oparg_T *oap = cap->oap;
7721#ifdef FEAT_VISUAL
7722 pos_T tpos;
7723#endif
7724 int i;
7725 int flag = FALSE;
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02007726#ifdef FEAT_CONCEAL
7727 linenr_T oldline;
7728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
7730 switch (cap->nchar)
7731 {
7732#ifdef MEM_PROFILE
7733 /*
7734 * "g^A": dump log of used memory.
7735 */
7736 case Ctrl_A:
7737 vim_mem_profile_dump();
7738 break;
7739#endif
7740
7741#ifdef FEAT_VREPLACE
7742 /*
7743 * "gR": Enter virtual replace mode.
7744 */
7745 case 'R':
7746 cap->arg = TRUE;
7747 nv_Replace(cap);
7748 break;
7749
7750 case 'r':
7751 nv_vreplace(cap);
7752 break;
7753#endif
7754
7755 case '&':
7756 do_cmdline_cmd((char_u *)"%s//~/&");
7757 break;
7758
7759#ifdef FEAT_VISUAL
7760 /*
7761 * "gv": Reselect the previous Visual area. If Visual already active,
7762 * exchange previous and current Visual area.
7763 */
7764 case 'v':
7765 if (checkclearop(oap))
7766 break;
7767
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007768 if ( curbuf->b_visual.vi_start.lnum == 0
7769 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7770 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 beep_flush();
7772 else
7773 {
7774 /* set w_cursor to the start of the Visual area, tpos to the end */
7775 if (VIsual_active)
7776 {
7777 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007778 VIsual_mode = curbuf->b_visual.vi_mode;
7779 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780# ifdef FEAT_EVAL
7781 curbuf->b_visual_mode_eval = i;
7782# endif
7783 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007784 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7785 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007787 tpos = curbuf->b_visual.vi_end;
7788 curbuf->b_visual.vi_end = curwin->w_cursor;
7789 curwin->w_cursor = curbuf->b_visual.vi_start;
7790 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
7792 else
7793 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007794 VIsual_mode = curbuf->b_visual.vi_mode;
7795 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7796 tpos = curbuf->b_visual.vi_end;
7797 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799
7800 VIsual_active = TRUE;
7801 VIsual_reselect = TRUE;
7802
7803 /* Set Visual to the start and w_cursor to the end of the Visual
7804 * area. Make sure they are on an existing character. */
7805 check_cursor();
7806 VIsual = curwin->w_cursor;
7807 curwin->w_cursor = tpos;
7808 check_cursor();
7809 update_topline();
7810 /*
7811 * When called from normal "g" command: start Select mode when
7812 * 'selectmode' contains "cmd". When called for K_SELECT, always
7813 * start Select mode.
7814 */
7815 if (cap->arg)
7816 VIsual_select = TRUE;
7817 else
7818 may_start_select('c');
7819#ifdef FEAT_MOUSE
7820 setmouse();
7821#endif
7822#ifdef FEAT_CLIPBOARD
7823 /* Make sure the clipboard gets updated. Needed because start and
7824 * end are still the same, and the selection needs to be owned */
7825 clip_star.vmode = NUL;
7826#endif
7827 redraw_curbuf_later(INVERTED);
7828 showmode();
7829 }
7830 break;
7831 /*
7832 * "gV": Don't reselect the previous Visual area after a Select mode
7833 * mapping of menu.
7834 */
7835 case 'V':
7836 VIsual_reselect = FALSE;
7837 break;
7838
7839 /*
7840 * "gh": start Select mode.
7841 * "gH": start Select line mode.
7842 * "g^H": start Select block mode.
7843 */
7844 case K_BS:
7845 cap->nchar = Ctrl_H;
7846 /* FALLTHROUGH */
7847 case 'h':
7848 case 'H':
7849 case Ctrl_H:
7850# ifdef EBCDIC
7851 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7852 if (cap->nchar == Ctrl_H)
7853 cap->cmdchar = Ctrl_V;
7854 else
7855# endif
7856 cap->cmdchar = cap->nchar + ('v' - 'h');
7857 cap->arg = TRUE;
7858 nv_visual(cap);
7859 break;
7860#endif /* FEAT_VISUAL */
7861
7862 /*
7863 * "gj" and "gk" two new funny movement keys -- up and down
7864 * movement based on *screen* line rather than *file* line.
7865 */
7866 case 'j':
7867 case K_DOWN:
7868 /* with 'nowrap' it works just like the normal "j" command; also when
7869 * in a closed fold */
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02007870#ifdef FEAT_CONCEAL
7871 oldline = curwin->w_cursor.lnum;
7872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (!curwin->w_p_wrap
7874#ifdef FEAT_FOLDING
7875 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7876#endif
7877 )
7878 {
7879 oap->motion_type = MLINE;
7880 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7881 }
7882 else
7883 i = nv_screengo(oap, FORWARD, cap->count1);
7884 if (i == FAIL)
7885 clearopbeep(oap);
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02007886#ifdef FEAT_CONCEAL
7887 else if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
7888 {
7889 update_single_line(curwin, oldline);
7890 update_single_line(curwin, curwin->w_cursor.lnum);
7891 }
7892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 break;
7894
7895 case 'k':
7896 case K_UP:
7897 /* with 'nowrap' it works just like the normal "k" command; also when
7898 * in a closed fold */
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02007899#ifdef FEAT_CONCEAL
7900 oldline = curwin->w_cursor.lnum;
7901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (!curwin->w_p_wrap
7903#ifdef FEAT_FOLDING
7904 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7905#endif
7906 )
7907 {
7908 oap->motion_type = MLINE;
7909 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7910 }
7911 else
7912 i = nv_screengo(oap, BACKWARD, cap->count1);
7913 if (i == FAIL)
7914 clearopbeep(oap);
Bram Moolenaard5b3a2c2010-06-08 22:59:42 +02007915#ifdef FEAT_CONCEAL
7916 else if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
7917 {
7918 update_single_line(curwin, oldline);
7919 update_single_line(curwin, curwin->w_cursor.lnum);
7920 }
7921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 break;
7923
7924 /*
7925 * "gJ": join two lines without inserting a space.
7926 */
7927 case 'J':
7928 nv_join(cap);
7929 break;
7930
7931 /*
7932 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7933 * "gm": middle of "g0" and "g$".
7934 */
7935 case '^':
7936 flag = TRUE;
7937 /* FALLTHROUGH */
7938
7939 case '0':
7940 case 'm':
7941 case K_HOME:
7942 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 oap->motion_type = MCHAR;
7944 oap->inclusive = FALSE;
7945 if (curwin->w_p_wrap
7946#ifdef FEAT_VERTSPLIT
7947 && curwin->w_width != 0
7948#endif
7949 )
7950 {
7951 int width1 = W_WIDTH(curwin) - curwin_col_off();
7952 int width2 = width1 + curwin_col_off2();
7953
7954 validate_virtcol();
7955 i = 0;
7956 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7957 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7958 }
7959 else
7960 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007961 /* Go to the middle of the screen line. When 'number' or
7962 * 'relativenumber' is on and lines are wrapping the middle can be more
7963 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (cap->nchar == 'm')
7965 i += (W_WIDTH(curwin) - curwin_col_off()
7966 + ((curwin->w_p_wrap && i > 0)
7967 ? curwin_col_off2() : 0)) / 2;
7968 coladvance((colnr_T)i);
7969 if (flag)
7970 {
7971 do
7972 i = gchar_cursor();
7973 while (vim_iswhite(i) && oneright() == OK);
7974 }
7975 curwin->w_set_curswant = TRUE;
7976 break;
7977
7978 case '_':
7979 /* "g_": to the last non-blank character in the line or <count> lines
7980 * downward. */
7981 cap->oap->motion_type = MCHAR;
7982 cap->oap->inclusive = TRUE;
7983 curwin->w_curswant = MAXCOL;
7984 if (cursor_down((long)(cap->count1 - 1),
7985 cap->oap->op_type == OP_NOP) == FAIL)
7986 clearopbeep(cap->oap);
7987 else
7988 {
7989 char_u *ptr = ml_get_curline();
7990
7991 /* In Visual mode we may end up after the line. */
7992 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7993 --curwin->w_cursor.col;
7994
7995 /* Decrease the cursor column until it's on a non-blank. */
7996 while (curwin->w_cursor.col > 0
7997 && vim_iswhite(ptr[curwin->w_cursor.col]))
7998 --curwin->w_cursor.col;
7999 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008000#ifdef FEAT_VISUAL
8001 adjust_for_sel(cap);
8002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 }
8004 break;
8005
8006 case '$':
8007 case K_END:
8008 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
8010 int col_off = curwin_col_off();
8011
8012 oap->motion_type = MCHAR;
8013 oap->inclusive = TRUE;
8014 if (curwin->w_p_wrap
8015#ifdef FEAT_VERTSPLIT
8016 && curwin->w_width != 0
8017#endif
8018 )
8019 {
8020 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8021 if (cap->count1 == 1)
8022 {
8023 int width1 = W_WIDTH(curwin) - col_off;
8024 int width2 = width1 + curwin_col_off2();
8025
8026 validate_virtcol();
8027 i = width1 - 1;
8028 if (curwin->w_virtcol >= (colnr_T)width1)
8029 i += ((curwin->w_virtcol - width1) / width2 + 1)
8030 * width2;
8031 coladvance((colnr_T)i);
8032#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8033 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8034 {
8035 /*
8036 * Check for landing on a character that got split at
8037 * the end of the line. We do not want to advance to
8038 * the next screen line.
8039 */
8040 validate_virtcol();
8041 if (curwin->w_virtcol > (colnr_T)i)
8042 --curwin->w_cursor.col;
8043 }
8044#endif
8045 }
8046 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8047 clearopbeep(oap);
8048 }
8049 else
8050 {
8051 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8052 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008053
8054 /* Make sure we stick in this column. */
8055 validate_virtcol();
8056 curwin->w_curswant = curwin->w_virtcol;
8057 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059 }
8060 break;
8061
8062 /*
8063 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8064 */
8065 case '*':
8066 case '#':
8067#if POUND != '#'
8068 case POUND: /* pound sign (sometimes equal to '#') */
8069#endif
8070 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8071 case ']': /* :tselect for current identifier */
8072 nv_ident(cap);
8073 break;
8074
8075 /*
8076 * ge and gE: go back to end of word
8077 */
8078 case 'e':
8079 case 'E':
8080 oap->motion_type = MCHAR;
8081 curwin->w_set_curswant = TRUE;
8082 oap->inclusive = TRUE;
8083 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8084 clearopbeep(oap);
8085 break;
8086
8087 /*
8088 * "g CTRL-G": display info about cursor position
8089 */
8090 case Ctrl_G:
8091 cursor_pos_info();
8092 break;
8093
8094 /*
8095 * "gi": start Insert at the last position.
8096 */
8097 case 'i':
8098 if (curbuf->b_last_insert.lnum != 0)
8099 {
8100 curwin->w_cursor = curbuf->b_last_insert;
8101 check_cursor_lnum();
8102 i = (int)STRLEN(ml_get_curline());
8103 if (curwin->w_cursor.col > (colnr_T)i)
8104 {
8105#ifdef FEAT_VIRTUALEDIT
8106 if (virtual_active())
8107 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8108#endif
8109 curwin->w_cursor.col = i;
8110 }
8111 }
8112 cap->cmdchar = 'i';
8113 nv_edit(cap);
8114 break;
8115
8116 /*
8117 * "gI": Start insert in column 1.
8118 */
8119 case 'I':
8120 beginline(0);
8121 if (!checkclearopq(oap))
8122 invoke_edit(cap, FALSE, 'g', FALSE);
8123 break;
8124
8125#ifdef FEAT_SEARCHPATH
8126 /*
8127 * "gf": goto file, edit file under cursor
8128 * "]f" and "[f": can also be used.
8129 */
8130 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008131 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 nv_gotofile(cap);
8133 break;
8134#endif
8135
8136 /* "g'm" and "g`m": jump to mark without setting pcmark */
8137 case '\'':
8138 cap->arg = TRUE;
8139 /*FALLTHROUGH*/
8140 case '`':
8141 nv_gomark(cap);
8142 break;
8143
8144 /*
8145 * "gs": Goto sleep.
8146 */
8147 case 's':
8148 do_sleep(cap->count1 * 1000L);
8149 break;
8150
8151 /*
8152 * "ga": Display the ascii value of the character under the
8153 * cursor. It is displayed in decimal, hex, and octal. -- webb
8154 */
8155 case 'a':
8156 do_ascii(NULL);
8157 break;
8158
8159#ifdef FEAT_MBYTE
8160 /*
8161 * "g8": Display the bytes used for the UTF-8 character under the
8162 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008163 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 */
8165 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008166 if (cap->count0 == 8)
8167 utf_find_illegal();
8168 else
8169 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 break;
8171#endif
8172
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008173 case '<':
8174 show_sb_text();
8175 break;
8176
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 /*
8178 * "gg": Goto the first line in file. With a count it goes to
8179 * that line number like for "G". -- webb
8180 */
8181 case 'g':
8182 cap->arg = FALSE;
8183 nv_goto(cap);
8184 break;
8185
8186 /*
8187 * Two-character operators:
8188 * "gq" Format text
8189 * "gw" Format text and keep cursor position
8190 * "g~" Toggle the case of the text.
8191 * "gu" Change text to lower case.
8192 * "gU" Change text to upper case.
8193 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008194 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 */
8196 case 'q':
8197 case 'w':
8198 oap->cursor_start = curwin->w_cursor;
8199 /*FALLTHROUGH*/
8200 case '~':
8201 case 'u':
8202 case 'U':
8203 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008204 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 nv_operator(cap);
8206 break;
8207
8208 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008209 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 * current function
8211 * "gD": idem, but in the current file.
8212 */
8213 case 'd':
8214 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008215 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 break;
8217
8218#ifdef FEAT_MOUSE
8219 /*
8220 * g<*Mouse> : <C-*mouse>
8221 */
8222 case K_MIDDLEMOUSE:
8223 case K_MIDDLEDRAG:
8224 case K_MIDDLERELEASE:
8225 case K_LEFTMOUSE:
8226 case K_LEFTDRAG:
8227 case K_LEFTRELEASE:
8228 case K_RIGHTMOUSE:
8229 case K_RIGHTDRAG:
8230 case K_RIGHTRELEASE:
8231 case K_X1MOUSE:
8232 case K_X1DRAG:
8233 case K_X1RELEASE:
8234 case K_X2MOUSE:
8235 case K_X2DRAG:
8236 case K_X2RELEASE:
8237 mod_mask = MOD_MASK_CTRL;
8238 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8239 break;
8240#endif
8241
8242 case K_IGNORE:
8243 break;
8244
8245 /*
8246 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8247 */
8248 case 'p':
8249 case 'P':
8250 nv_put(cap);
8251 break;
8252
8253#ifdef FEAT_BYTEOFF
8254 /* "go": goto byte count from start of buffer */
8255 case 'o':
8256 goto_byte(cap->count0);
8257 break;
8258#endif
8259
8260 /* "gQ": improved Ex mode */
8261 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008262 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
8264 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008265 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 break;
8267 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008268
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 if (!checkclearopq(oap))
8270 do_exmode(TRUE);
8271 break;
8272
8273#ifdef FEAT_JUMPLIST
8274 case ',':
8275 nv_pcmark(cap);
8276 break;
8277
8278 case ';':
8279 cap->count1 = -cap->count1;
8280 nv_pcmark(cap);
8281 break;
8282#endif
8283
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008284#ifdef FEAT_WINDOWS
8285 case 't':
8286 goto_tabpage((int)cap->count0);
8287 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008288 case 'T':
8289 goto_tabpage(-(int)cap->count1);
8290 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008291#endif
8292
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008293 case '+':
8294 case '-': /* "g+" and "g-": undo or redo along the timeline */
8295 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008296 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008297 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008298 break;
8299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 default:
8301 clearopbeep(oap);
8302 break;
8303 }
8304}
8305
8306/*
8307 * Handle "o" and "O" commands.
8308 */
8309 static void
8310n_opencmd(cap)
8311 cmdarg_T *cap;
8312{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008313#ifdef FEAT_CONCEAL
8314 linenr_T oldline = curwin->w_cursor.lnum;
8315#endif
8316
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (!checkclearopq(cap->oap))
8318 {
8319#ifdef FEAT_FOLDING
8320 if (cap->cmdchar == 'O')
8321 /* Open above the first line of a folded sequence of lines */
8322 (void)hasFolding(curwin->w_cursor.lnum,
8323 &curwin->w_cursor.lnum, NULL);
8324 else
8325 /* Open below the last line of a folded sequence of lines */
8326 (void)hasFolding(curwin->w_cursor.lnum,
8327 NULL, &curwin->w_cursor.lnum);
8328#endif
8329 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8330 (cap->cmdchar == 'O' ? 1 : 0)),
8331 (linenr_T)(curwin->w_cursor.lnum +
8332 (cap->cmdchar == 'o' ? 1 : 0))
8333 ) == OK
8334 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8335#ifdef FEAT_COMMENTS
8336 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8337#endif
8338 0, 0))
8339 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008340#ifdef FEAT_CONCEAL
8341 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8342 update_single_line(curwin, oldline);
8343#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008344 /* When '#' is in 'cpoptions' ignore the count. */
8345 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8346 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8348 }
8349 }
8350}
8351
8352/*
8353 * "." command: redo last change.
8354 */
8355 static void
8356nv_dot(cap)
8357 cmdarg_T *cap;
8358{
8359 if (!checkclearopq(cap->oap))
8360 {
8361 /*
8362 * If "restart_edit" is TRUE, the last but one command is repeated
8363 * instead of the last command (inserting text). This is used for
8364 * CTRL-O <.> in insert mode.
8365 */
8366 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8367 clearopbeep(cap->oap);
8368 }
8369}
8370
8371/*
8372 * CTRL-R: undo undo
8373 */
8374 static void
8375nv_redo(cap)
8376 cmdarg_T *cap;
8377{
8378 if (!checkclearopq(cap->oap))
8379 {
8380 u_redo((int)cap->count1);
8381 curwin->w_set_curswant = TRUE;
8382 }
8383}
8384
8385/*
8386 * Handle "U" command.
8387 */
8388 static void
8389nv_Undo(cap)
8390 cmdarg_T *cap;
8391{
8392 /* In Visual mode and typing "gUU" triggers an operator */
8393 if (cap->oap->op_type == OP_UPPER
8394#ifdef FEAT_VISUAL
8395 || VIsual_active
8396#endif
8397 )
8398 {
8399 /* translate "gUU" to "gUgU" */
8400 cap->cmdchar = 'g';
8401 cap->nchar = 'U';
8402 nv_operator(cap);
8403 }
8404 else if (!checkclearopq(cap->oap))
8405 {
8406 u_undoline();
8407 curwin->w_set_curswant = TRUE;
8408 }
8409}
8410
8411/*
8412 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8413 * single character.
8414 */
8415 static void
8416nv_tilde(cap)
8417 cmdarg_T *cap;
8418{
8419 if (!p_to
8420#ifdef FEAT_VISUAL
8421 && !VIsual_active
8422#endif
8423 && cap->oap->op_type != OP_TILDE)
8424 n_swapchar(cap);
8425 else
8426 nv_operator(cap);
8427}
8428
8429/*
8430 * Handle an operator command.
8431 * The actual work is done by do_pending_operator().
8432 */
8433 static void
8434nv_operator(cap)
8435 cmdarg_T *cap;
8436{
8437 int op_type;
8438
8439 op_type = get_op_type(cap->cmdchar, cap->nchar);
8440
8441 if (op_type == cap->oap->op_type) /* double operator works on lines */
8442 nv_lineop(cap);
8443 else if (!checkclearop(cap->oap))
8444 {
8445 cap->oap->start = curwin->w_cursor;
8446 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008447#ifdef FEAT_EVAL
8448 set_op_var(op_type);
8449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 }
8451}
8452
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008453#ifdef FEAT_EVAL
8454/*
8455 * Set v:operator to the characters for "optype".
8456 */
8457 static void
8458set_op_var(optype)
8459 int optype;
8460{
8461 char_u opchars[3];
8462
8463 if (optype == OP_NOP)
8464 set_vim_var_string(VV_OP, NULL, 0);
8465 else
8466 {
8467 opchars[0] = get_op_char(optype);
8468 opchars[1] = get_extra_op_char(optype);
8469 opchars[2] = NUL;
8470 set_vim_var_string(VV_OP, opchars, -1);
8471 }
8472}
8473#endif
8474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475/*
8476 * Handle linewise operator "dd", "yy", etc.
8477 *
8478 * "_" is is a strange motion command that helps make operators more logical.
8479 * It is actually implemented, but not documented in the real Vi. This motion
8480 * command actually refers to "the current line". Commands like "dd" and "yy"
8481 * are really an alternate form of "d_" and "y_". It does accept a count, so
8482 * "d3_" works to delete 3 lines.
8483 */
8484 static void
8485nv_lineop(cap)
8486 cmdarg_T *cap;
8487{
8488 cap->oap->motion_type = MLINE;
8489 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8490 clearopbeep(cap->oap);
8491 else if ( cap->oap->op_type == OP_DELETE
8492 || cap->oap->op_type == OP_LSHIFT
8493 || cap->oap->op_type == OP_RSHIFT)
8494 beginline(BL_SOL | BL_FIX);
8495 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8496 beginline(BL_WHITE | BL_FIX);
8497}
8498
8499/*
8500 * <Home> command.
8501 */
8502 static void
8503nv_home(cap)
8504 cmdarg_T *cap;
8505{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008506 /* CTRL-HOME is like "gg" */
8507 if (mod_mask & MOD_MASK_CTRL)
8508 nv_goto(cap);
8509 else
8510 {
8511 cap->count0 = 1;
8512 nv_pipe(cap);
8513 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008514 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8515 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516}
8517
8518/*
8519 * "|" command.
8520 */
8521 static void
8522nv_pipe(cap)
8523 cmdarg_T *cap;
8524{
8525 cap->oap->motion_type = MCHAR;
8526 cap->oap->inclusive = FALSE;
8527 beginline(0);
8528 if (cap->count0 > 0)
8529 {
8530 coladvance((colnr_T)(cap->count0 - 1));
8531 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8532 }
8533 else
8534 curwin->w_curswant = 0;
8535 /* keep curswant at the column where we wanted to go, not where
8536 we ended; differs if line is too short */
8537 curwin->w_set_curswant = FALSE;
8538}
8539
8540/*
8541 * Handle back-word command "b" and "B".
8542 * cap->arg is 1 for "B"
8543 */
8544 static void
8545nv_bck_word(cap)
8546 cmdarg_T *cap;
8547{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008548#ifdef FEAT_CONCEAL
8549 linenr_T oldline = curwin->w_cursor.lnum;
8550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 cap->oap->motion_type = MCHAR;
8552 cap->oap->inclusive = FALSE;
8553 curwin->w_set_curswant = TRUE;
8554 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8555 clearopbeep(cap->oap);
8556#ifdef FEAT_FOLDING
8557 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8558 foldOpenCursor();
8559#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02008560#ifdef FEAT_CONCEAL
8561 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8562 {
8563 update_single_line(curwin, oldline);
8564 update_single_line(curwin, curwin->w_cursor.lnum);
8565 }
8566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569/*
8570 * Handle word motion commands "e", "E", "w" and "W".
8571 * cap->arg is TRUE for "E" and "W".
8572 */
8573 static void
8574nv_wordcmd(cap)
8575 cmdarg_T *cap;
8576{
8577 int n;
8578 int word_end;
8579 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008580 pos_T startpos = curwin->w_cursor;
Bram Moolenaar860cae12010-06-05 23:22:07 +02008581#ifdef FEAT_CONCEAL
8582 linenr_T oldline = startpos.lnum;
8583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584
8585 /*
8586 * Set inclusive for the "E" and "e" command.
8587 */
8588 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8589 word_end = TRUE;
8590 else
8591 word_end = FALSE;
8592 cap->oap->inclusive = word_end;
8593
8594 /*
8595 * "cw" and "cW" are a special case.
8596 */
8597 if (!word_end && cap->oap->op_type == OP_CHANGE)
8598 {
8599 n = gchar_cursor();
8600 if (n != NUL) /* not an empty line */
8601 {
8602 if (vim_iswhite(n))
8603 {
8604 /*
8605 * Reproduce a funny Vi behaviour: "cw" on a blank only
8606 * changes one character, not all blanks until the start of
8607 * the next word. Only do this when the 'w' flag is included
8608 * in 'cpoptions'.
8609 */
8610 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8611 {
8612 cap->oap->inclusive = TRUE;
8613 cap->oap->motion_type = MCHAR;
8614 return;
8615 }
8616 }
8617 else
8618 {
8619 /*
8620 * This is a little strange. To match what the real Vi does,
8621 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8622 * that we are not on a space or a TAB. This seems impolite
8623 * at first, but it's really more what we mean when we say
8624 * 'cw'.
8625 * Another strangeness: When standing on the end of a word
8626 * "ce" will change until the end of the next wordt, but "cw"
8627 * will change only one character! This is done by setting
8628 * flag.
8629 */
8630 cap->oap->inclusive = TRUE;
8631 word_end = TRUE;
8632 flag = TRUE;
8633 }
8634 }
8635 }
8636
8637 cap->oap->motion_type = MCHAR;
8638 curwin->w_set_curswant = TRUE;
8639 if (word_end)
8640 n = end_word(cap->count1, cap->arg, flag, FALSE);
8641 else
8642 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8643
Bram Moolenaardfefb982008-04-01 10:06:39 +00008644 /* Don't leave the cursor on the NUL past the end of line. Unless we
8645 * didn't move it forward. */
8646 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008647 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 if (n == FAIL && cap->oap->op_type == OP_NOP)
8650 clearopbeep(cap->oap);
8651 else
8652 {
8653#ifdef FEAT_VISUAL
8654 adjust_for_sel(cap);
8655#endif
8656#ifdef FEAT_FOLDING
8657 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8658 foldOpenCursor();
8659#endif
8660 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02008661#ifdef FEAT_CONCEAL
8662 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
8663 {
8664 update_single_line(curwin, oldline);
8665 update_single_line(curwin, curwin->w_cursor.lnum);
8666 }
8667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668}
8669
8670/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008671 * Used after a movement command: If the cursor ends up on the NUL after the
8672 * end of the line, may move it back to the last character and make the motion
8673 * inclusive.
8674 */
8675 static void
8676adjust_cursor(oap)
8677 oparg_T *oap;
8678{
8679 /* The cursor cannot remain on the NUL when:
8680 * - the column is > 0
8681 * - not in Visual mode or 'selection' is "o"
8682 * - 'virtualedit' is not "all" and not "onemore".
8683 */
8684 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
8685#ifdef FEAT_VISUAL
8686 && (!VIsual_active || *p_sel == 'o')
8687#endif
8688#ifdef FEAT_VIRTUALEDIT
8689 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8690#endif
8691 )
8692 {
8693 --curwin->w_cursor.col;
8694#ifdef FEAT_MBYTE
8695 /* prevent cursor from moving on the trail byte */
8696 if (has_mbyte)
8697 mb_adjust_cursor();
8698#endif
8699 oap->inclusive = TRUE;
8700 }
8701}
8702
8703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 * "0" and "^" commands.
8705 * cap->arg is the argument for beginline().
8706 */
8707 static void
8708nv_beginline(cap)
8709 cmdarg_T *cap;
8710{
8711 cap->oap->motion_type = MCHAR;
8712 cap->oap->inclusive = FALSE;
8713 beginline(cap->arg);
8714#ifdef FEAT_FOLDING
8715 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8716 foldOpenCursor();
8717#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008718 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8719 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720}
8721
8722#ifdef FEAT_VISUAL
8723/*
8724 * In exclusive Visual mode, may include the last character.
8725 */
8726 static void
8727adjust_for_sel(cap)
8728 cmdarg_T *cap;
8729{
8730 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8731 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8732 {
8733# ifdef FEAT_MBYTE
8734 if (has_mbyte)
8735 inc_cursor();
8736 else
8737# endif
8738 ++curwin->w_cursor.col;
8739 cap->oap->inclusive = FALSE;
8740 }
8741}
8742
8743/*
8744 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8745 * Should check VIsual_mode before calling this.
8746 * Returns TRUE when backed up to the previous line.
8747 */
8748 static int
8749unadjust_for_sel()
8750{
8751 pos_T *pp;
8752
8753 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8754 {
8755 if (lt(VIsual, curwin->w_cursor))
8756 pp = &curwin->w_cursor;
8757 else
8758 pp = &VIsual;
8759#ifdef FEAT_VIRTUALEDIT
8760 if (pp->coladd > 0)
8761 --pp->coladd;
8762 else
8763#endif
8764 if (pp->col > 0)
8765 {
8766 --pp->col;
8767#ifdef FEAT_MBYTE
8768 mb_adjustpos(pp);
8769#endif
8770 }
8771 else if (pp->lnum > 1)
8772 {
8773 --pp->lnum;
8774 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8775 return TRUE;
8776 }
8777 }
8778 return FALSE;
8779}
8780
8781/*
8782 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8783 */
8784 static void
8785nv_select(cap)
8786 cmdarg_T *cap;
8787{
8788 if (VIsual_active)
8789 VIsual_select = TRUE;
8790 else if (VIsual_reselect)
8791 {
8792 cap->nchar = 'v'; /* fake "gv" command */
8793 cap->arg = TRUE;
8794 nv_g_cmd(cap);
8795 }
8796}
8797
8798#endif
8799
8800/*
8801 * "G", "gg", CTRL-END, CTRL-HOME.
8802 * cap->arg is TRUE for "G".
8803 */
8804 static void
8805nv_goto(cap)
8806 cmdarg_T *cap;
8807{
8808 linenr_T lnum;
8809
8810 if (cap->arg)
8811 lnum = curbuf->b_ml.ml_line_count;
8812 else
8813 lnum = 1L;
8814 cap->oap->motion_type = MLINE;
8815 setpcmark();
8816
8817 /* When a count is given, use it instead of the default lnum */
8818 if (cap->count0 != 0)
8819 lnum = cap->count0;
8820 if (lnum < 1L)
8821 lnum = 1L;
8822 else if (lnum > curbuf->b_ml.ml_line_count)
8823 lnum = curbuf->b_ml.ml_line_count;
8824 curwin->w_cursor.lnum = lnum;
8825 beginline(BL_SOL | BL_FIX);
8826#ifdef FEAT_FOLDING
8827 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8828 foldOpenCursor();
8829#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02008830#ifdef FEAT_CONCEAL
8831 if (curwin->w_p_conceal)
8832 changed_window_setting();
8833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834}
8835
8836/*
8837 * CTRL-\ in Normal mode.
8838 */
8839 static void
8840nv_normal(cap)
8841 cmdarg_T *cap;
8842{
8843 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8844 {
8845 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008846 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 clear_cmdline = TRUE; /* unshow mode later */
8848 restart_edit = 0;
8849#ifdef FEAT_CMDWIN
8850 if (cmdwin_type != 0)
8851 cmdwin_result = Ctrl_C;
8852#endif
8853#ifdef FEAT_VISUAL
8854 if (VIsual_active)
8855 {
8856 end_visual_mode(); /* stop Visual */
8857 redraw_curbuf_later(INVERTED);
8858 }
8859#endif
8860 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8861 if (cap->nchar == Ctrl_G && p_im)
8862 restart_edit = 'a';
8863 }
8864 else
8865 clearopbeep(cap->oap);
8866}
8867
8868/*
8869 * ESC in Normal mode: beep, but don't flush buffers.
8870 * Don't even beep if we are canceling a command.
8871 */
8872 static void
8873nv_esc(cap)
8874 cmdarg_T *cap;
8875{
8876 int no_reason;
8877
8878 no_reason = (cap->oap->op_type == OP_NOP
8879 && cap->opcount == 0
8880 && cap->count0 == 0
8881 && cap->oap->regname == 0
8882 && !p_im);
8883
8884 if (cap->arg) /* TRUE for CTRL-C */
8885 {
8886 if (restart_edit == 0
8887#ifdef FEAT_CMDWIN
8888 && cmdwin_type == 0
8889#endif
8890#ifdef FEAT_VISUAL
8891 && !VIsual_active
8892#endif
8893 && no_reason)
8894 MSG(_("Type :quit<Enter> to exit Vim"));
8895
8896 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8897 * set again below when halfway a mapping. */
8898 if (!p_im)
8899 restart_edit = 0;
8900#ifdef FEAT_CMDWIN
8901 if (cmdwin_type != 0)
8902 {
8903 cmdwin_result = K_IGNORE;
8904 got_int = FALSE; /* don't stop executing autocommands et al. */
8905 return;
8906 }
8907#endif
8908 }
8909
8910#ifdef FEAT_VISUAL
8911 if (VIsual_active)
8912 {
8913 end_visual_mode(); /* stop Visual */
8914 check_cursor_col(); /* make sure cursor is not beyond EOL */
8915 curwin->w_set_curswant = TRUE;
8916 redraw_curbuf_later(INVERTED);
8917 }
8918 else
8919#endif
8920 if (no_reason)
8921 vim_beep();
8922 clearop(cap->oap);
8923
8924 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8925 * set return to Insert mode afterwards. */
8926 if (restart_edit == 0 && goto_im()
8927#ifdef FEAT_EX_EXTRA
8928 && ex_normal_busy == 0
8929#endif
8930 )
8931 restart_edit = 'a';
8932}
8933
8934/*
8935 * Handle "A", "a", "I", "i" and <Insert> commands.
8936 */
8937 static void
8938nv_edit(cap)
8939 cmdarg_T *cap;
8940{
8941 /* <Insert> is equal to "i" */
8942 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8943 cap->cmdchar = 'i';
8944
8945#ifdef FEAT_VISUAL
8946 /* in Visual mode "A" and "I" are an operator */
8947 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8948 v_visop(cap);
8949
8950 /* in Visual mode and after an operator "a" and "i" are for text objects */
8951 else
8952#endif
8953 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8954 && (cap->oap->op_type != OP_NOP
8955#ifdef FEAT_VISUAL
8956 || VIsual_active
8957#endif
8958 ))
8959 {
8960#ifdef FEAT_TEXTOBJ
8961 nv_object(cap);
8962#else
8963 clearopbeep(cap->oap);
8964#endif
8965 }
8966 else if (!curbuf->b_p_ma && !p_im)
8967 {
8968 /* Only give this error when 'insertmode' is off. */
8969 EMSG(_(e_modifiable));
8970 clearop(cap->oap);
8971 }
8972 else if (!checkclearopq(cap->oap))
8973 {
8974 switch (cap->cmdchar)
8975 {
8976 case 'A': /* "A"ppend after the line */
8977 curwin->w_set_curswant = TRUE;
8978#ifdef FEAT_VIRTUALEDIT
8979 if (ve_flags == VE_ALL)
8980 {
8981 int save_State = State;
8982
8983 /* Pretent Insert mode here to allow the cursor on the
8984 * character past the end of the line */
8985 State = INSERT;
8986 coladvance((colnr_T)MAXCOL);
8987 State = save_State;
8988 }
8989 else
8990#endif
8991 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8992 break;
8993
8994 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008995 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8996 beginline(BL_WHITE);
8997 else
8998 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 break;
9000
9001 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9002#ifdef FEAT_VIRTUALEDIT
9003 /* increment coladd when in virtual space, increment the
9004 * column otherwise, also to append after an unprintable char */
9005 if (virtual_active()
9006 && (curwin->w_cursor.coladd > 0
9007 || *ml_get_cursor() == NUL
9008 || *ml_get_cursor() == TAB))
9009 curwin->w_cursor.coladd++;
9010 else
9011#endif
9012 if (*ml_get_cursor() != NUL)
9013 inc_cursor();
9014 break;
9015 }
9016
9017#ifdef FEAT_VIRTUALEDIT
9018 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9019 {
9020 int save_State = State;
9021
9022 /* Pretent Insert mode here to allow the cursor on the
9023 * character past the end of the line */
9024 State = INSERT;
9025 coladvance(getviscol());
9026 State = save_State;
9027 }
9028#endif
9029
9030 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9031 }
9032}
9033
9034/*
9035 * Invoke edit() and take care of "restart_edit" and the return value.
9036 */
9037 static void
9038invoke_edit(cap, repl, cmd, startln)
9039 cmdarg_T *cap;
9040 int repl; /* "r" or "gr" command */
9041 int cmd;
9042 int startln;
9043{
9044 int restart_edit_save = 0;
9045
9046 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9047 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9048 * it. */
9049 if (repl || !stuff_empty())
9050 restart_edit_save = restart_edit;
9051 else
9052 restart_edit_save = 0;
9053
9054 /* Always reset "restart_edit", this is not a restarted edit. */
9055 restart_edit = 0;
9056
9057 if (edit(cmd, startln, cap->count1))
9058 cap->retval |= CA_COMMAND_BUSY;
9059
9060 if (restart_edit == 0)
9061 restart_edit = restart_edit_save;
9062}
9063
9064#ifdef FEAT_TEXTOBJ
9065/*
9066 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9067 */
9068 static void
9069nv_object(cap)
9070 cmdarg_T *cap;
9071{
9072 int flag;
9073 int include;
9074 char_u *mps_save;
9075
9076 if (cap->cmdchar == 'i')
9077 include = FALSE; /* "ix" = inner object: exclude white space */
9078 else
9079 include = TRUE; /* "ax" = an object: include white space */
9080
9081 /* Make sure (), [], {} and <> are in 'matchpairs' */
9082 mps_save = curbuf->b_p_mps;
9083 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9084
9085 switch (cap->nchar)
9086 {
9087 case 'w': /* "aw" = a word */
9088 flag = current_word(cap->oap, cap->count1, include, FALSE);
9089 break;
9090 case 'W': /* "aW" = a WORD */
9091 flag = current_word(cap->oap, cap->count1, include, TRUE);
9092 break;
9093 case 'b': /* "ab" = a braces block */
9094 case '(':
9095 case ')':
9096 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9097 break;
9098 case 'B': /* "aB" = a Brackets block */
9099 case '{':
9100 case '}':
9101 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9102 break;
9103 case '[': /* "a[" = a [] block */
9104 case ']':
9105 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9106 break;
9107 case '<': /* "a<" = a <> block */
9108 case '>':
9109 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9110 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009111 case 't': /* "at" = a tag block (xml and html) */
9112 flag = current_tagblock(cap->oap, cap->count1, include);
9113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 case 'p': /* "ap" = a paragraph */
9115 flag = current_par(cap->oap, cap->count1, include, 'p');
9116 break;
9117 case 's': /* "as" = a sentence */
9118 flag = current_sent(cap->oap, cap->count1, include);
9119 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009120 case '"': /* "a"" = a double quoted string */
9121 case '\'': /* "a'" = a single quoted string */
9122 case '`': /* "a`" = a backtick quoted string */
9123 flag = current_quote(cap->oap, cap->count1, include,
9124 cap->nchar);
9125 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#if 0 /* TODO */
9127 case 'S': /* "aS" = a section */
9128 case 'f': /* "af" = a filename */
9129 case 'u': /* "au" = a URL */
9130#endif
9131 default:
9132 flag = FAIL;
9133 break;
9134 }
9135
9136 curbuf->b_p_mps = mps_save;
9137 if (flag == FAIL)
9138 clearopbeep(cap->oap);
9139 adjust_cursor_col();
9140 curwin->w_set_curswant = TRUE;
9141}
9142#endif
9143
9144/*
9145 * "q" command: Start/stop recording.
9146 * "q:", "q/", "q?": edit command-line in command-line window.
9147 */
9148 static void
9149nv_record(cap)
9150 cmdarg_T *cap;
9151{
9152 if (cap->oap->op_type == OP_FORMAT)
9153 {
9154 /* "gqq" is the same as "gqgq": format line */
9155 cap->cmdchar = 'g';
9156 cap->nchar = 'q';
9157 nv_operator(cap);
9158 }
9159 else if (!checkclearop(cap->oap))
9160 {
9161#ifdef FEAT_CMDWIN
9162 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9163 {
9164 stuffcharReadbuff(cap->nchar);
9165 stuffcharReadbuff(K_CMDWIN);
9166 }
9167 else
9168#endif
9169 /* (stop) recording into a named register, unless executing a
9170 * register */
9171 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9172 clearopbeep(cap->oap);
9173 }
9174}
9175
9176/*
9177 * Handle the "@r" command.
9178 */
9179 static void
9180nv_at(cap)
9181 cmdarg_T *cap;
9182{
9183 if (checkclearop(cap->oap))
9184 return;
9185#ifdef FEAT_EVAL
9186 if (cap->nchar == '=')
9187 {
9188 if (get_expr_register() == NUL)
9189 return;
9190 }
9191#endif
9192 while (cap->count1-- && !got_int)
9193 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009194 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
9196 clearopbeep(cap->oap);
9197 break;
9198 }
9199 line_breakcheck();
9200 }
9201}
9202
9203/*
9204 * Handle the CTRL-U and CTRL-D commands.
9205 */
9206 static void
9207nv_halfpage(cap)
9208 cmdarg_T *cap;
9209{
9210 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9211 || (cap->cmdchar == Ctrl_D
9212 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9213 clearopbeep(cap->oap);
9214 else if (!checkclearop(cap->oap))
9215 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9216}
9217
9218/*
9219 * Handle "J" or "gJ" command.
9220 */
9221 static void
9222nv_join(cap)
9223 cmdarg_T *cap;
9224{
9225#ifdef FEAT_VISUAL
9226 if (VIsual_active) /* join the visual lines */
9227 nv_operator(cap);
9228 else
9229#endif
9230 if (!checkclearop(cap->oap))
9231 {
9232 if (cap->count0 <= 1)
9233 cap->count0 = 2; /* default for join is two lines! */
9234 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9235 curbuf->b_ml.ml_line_count)
9236 clearopbeep(cap->oap); /* beyond last line */
9237 else
9238 {
9239 prep_redo(cap->oap->regname, cap->count0,
9240 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9241 do_do_join(cap->count0, cap->nchar == NUL);
9242 }
9243 }
9244}
9245
9246/*
9247 * "P", "gP", "p" and "gp" commands.
9248 */
9249 static void
9250nv_put(cap)
9251 cmdarg_T *cap;
9252{
9253#ifdef FEAT_VISUAL
9254 int regname = 0;
9255 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009256 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009257 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02009259#ifdef FEAT_CONCEAL
9260 linenr_T oldline = curwin->w_cursor.lnum;
9261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 int dir;
9263 int flags = 0;
9264
9265 if (cap->oap->op_type != OP_NOP)
9266 {
9267#ifdef FEAT_DIFF
9268 /* "dp" is ":diffput" */
9269 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9270 {
9271 clearop(cap->oap);
9272 nv_diffgetput(TRUE);
9273 }
9274 else
9275#endif
9276 clearopbeep(cap->oap);
9277 }
9278 else
9279 {
9280 dir = (cap->cmdchar == 'P'
9281 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9282 ? BACKWARD : FORWARD;
9283 prep_redo_cmd(cap);
9284 if (cap->cmdchar == 'g')
9285 flags |= PUT_CURSEND;
9286
9287#ifdef FEAT_VISUAL
9288 if (VIsual_active)
9289 {
9290 /* Putting in Visual mode: The put text replaces the selected
9291 * text. First delete the selected text, then put the new text.
9292 * Need to save and restore the registers that the delete
9293 * overwrites if the old contents is being put.
9294 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009295 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 regname = cap->oap->regname;
9297# ifdef FEAT_CLIPBOARD
9298 adjust_clip_reg(&regname);
9299# endif
9300 if (regname == 0 || VIM_ISDIGIT(regname)
9301# ifdef FEAT_CLIPBOARD
9302 || (clip_unnamed && (regname == '*' || regname == '+'))
9303# endif
9304
9305 )
9306 {
9307 /* the delete is going to overwrite the register we want to
9308 * put, save it first. */
9309 reg1 = get_register(regname, TRUE);
9310 }
9311
9312 /* Now delete the selected text. */
9313 cap->cmdchar = 'd';
9314 cap->nchar = NUL;
9315 cap->oap->regname = NUL;
9316 nv_operator(cap);
9317 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009318 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 /* delete PUT_LINE_BACKWARD; */
9321 cap->oap->regname = regname;
9322
9323 if (reg1 != NULL)
9324 {
9325 /* Delete probably changed the register we want to put, save
9326 * it first. Then put back what was there before the delete. */
9327 reg2 = get_register(regname, FALSE);
9328 put_register(regname, reg1);
9329 }
9330
9331 /* When deleted a linewise Visual area, put the register as
9332 * lines to avoid it joined with the next line. When deletion was
9333 * characterwise, split a line when putting lines. */
9334 if (VIsual_mode == 'V')
9335 flags |= PUT_LINE;
9336 else if (VIsual_mode == 'v')
9337 flags |= PUT_LINE_SPLIT;
9338 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9339 flags |= PUT_LINE_FORWARD;
9340 dir = BACKWARD;
9341 if ((VIsual_mode != 'V'
9342 && curwin->w_cursor.col < curbuf->b_op_start.col)
9343 || (VIsual_mode == 'V'
9344 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9345 /* cursor is at the end of the line or end of file, put
9346 * forward. */
9347 dir = FORWARD;
9348 }
9349#endif
9350 do_put(cap->oap->regname, dir, cap->count1, flags);
9351
9352#ifdef FEAT_VISUAL
9353 /* If a register was saved, put it back now. */
9354 if (reg2 != NULL)
9355 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009356
9357 /* What to reselect with "gv"? Selecting the just put text seems to
9358 * be the most useful, since the original text was removed. */
9359 if (was_visual)
9360 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009361 curbuf->b_visual.vi_start = curbuf->b_op_start;
9362 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009363 }
9364
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009365 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009366 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009367 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009368 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009369 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009370
9371 /* If the cursor was in that line, move it to the end of the last
9372 * line. */
9373 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9374 {
9375 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9376 coladvance((colnr_T)MAXCOL);
9377 }
9378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#endif
9380 auto_format(FALSE, TRUE);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009381#ifdef FEAT_CONCEAL
9382 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9383 {
9384 update_single_line(curwin, oldline);
9385 update_single_line(curwin, curwin->w_cursor.lnum);
9386 }
9387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 }
9389}
9390
9391/*
9392 * "o" and "O" commands.
9393 */
9394 static void
9395nv_open(cap)
9396 cmdarg_T *cap;
9397{
9398#ifdef FEAT_DIFF
9399 /* "do" is ":diffget" */
9400 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9401 {
9402 clearop(cap->oap);
9403 nv_diffgetput(FALSE);
9404 }
9405 else
9406#endif
9407#ifdef FEAT_VISUAL
9408 if (VIsual_active) /* switch start and end of visual */
9409 v_swap_corners(cap->cmdchar);
9410 else
9411#endif
9412 n_opencmd(cap);
9413}
9414
9415#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 static void
9417nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009418 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
9420 ProcessSniffRequests();
9421}
9422#endif
9423
9424#ifdef FEAT_NETBEANS_INTG
9425 static void
9426nv_nbcmd(cap)
9427 cmdarg_T *cap;
9428{
9429 netbeans_keycommand(cap->nchar);
9430}
9431#endif
9432
9433#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 static void
9435nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009436 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437{
9438 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9439}
9440#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009441
9442#ifdef FEAT_AUTOCMD
9443/*
9444 * Trigger CursorHold event.
9445 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9446 * input buffer. "did_cursorhold" is set to avoid retriggering.
9447 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009448 static void
9449nv_cursorhold(cap)
9450 cmdarg_T *cap;
9451{
9452 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9453 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009454 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009455}
9456#endif