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