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