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