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