blob: cee008e63e2d7f2d577646a04bc0dd3602ea6e71 [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/*
2111 * Handle the "gy" operator: call 'operatorfunc'.
2112 */
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
2280 /*
2281 * ALT is currently ignored
2282 */
2283 if ((mod_mask & MOD_MASK_ALT))
2284 return FALSE;
2285
2286 /*
2287 * CTRL right mouse button does CTRL-T
2288 */
2289 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2290 {
2291 if (State & INSERT)
2292 stuffcharReadbuff(Ctrl_O);
2293 if (count > 1)
2294 stuffnumReadbuff(count);
2295 stuffcharReadbuff(Ctrl_T);
2296 got_click = FALSE; /* ignore drag&release now */
2297 return FALSE;
2298 }
2299
2300 /*
2301 * CTRL only works with left mouse button
2302 */
2303 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2304 return FALSE;
2305
2306 /*
2307 * When a modifier is down, ignore drag and release events, as well as
2308 * multiple clicks and the middle mouse button.
2309 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2310 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002311 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2312 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && (!is_click
2314 || (mod_mask & MOD_MASK_MULTI_CLICK)
2315 || which_button == MOUSE_MIDDLE)
2316 && !((mod_mask & MOD_MASK_SHIFT)
2317 && mouse_model_popup()
2318 && which_button == MOUSE_LEFT)
2319 )
2320 return FALSE;
2321
2322 /*
2323 * If the button press was used as the movement command for an operator
2324 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2325 * drag/release events.
2326 */
2327 if (!is_click && which_button == MOUSE_MIDDLE)
2328 return FALSE;
2329
2330 if (oap != NULL)
2331 regname = oap->regname;
2332 else
2333 regname = 0;
2334
2335 /*
2336 * Middle mouse button does a 'put' of the selected text
2337 */
2338 if (which_button == MOUSE_MIDDLE)
2339 {
2340 if (State == NORMAL)
2341 {
2342 /*
2343 * If an operator was pending, we don't know what the user wanted
2344 * to do. Go back to normal mode: Clear the operator and beep().
2345 */
2346 if (oap != NULL && oap->op_type != OP_NOP)
2347 {
2348 clearopbeep(oap);
2349 return FALSE;
2350 }
2351
2352#ifdef FEAT_VISUAL
2353 /*
2354 * If visual was active, yank the highlighted text and put it
2355 * before the mouse pointer position.
2356 */
2357 if (VIsual_active)
2358 {
2359 stuffcharReadbuff('y');
2360 stuffcharReadbuff(K_MIDDLEMOUSE);
2361 do_always = TRUE; /* ignore 'mouse' setting next time */
2362 return FALSE;
2363 }
2364#endif
2365 /*
2366 * The rest is below jump_to_mouse()
2367 */
2368 }
2369
2370 else if ((State & INSERT) == 0)
2371 return FALSE;
2372
2373 /*
2374 * Middle click in insert mode doesn't move the mouse, just insert the
2375 * contents of a register. '.' register is special, can't insert that
2376 * with do_put().
2377 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2378 * happens for the GUI).
2379 */
2380 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2381 {
2382 if (regname == '.')
2383 insert_reg(regname, TRUE);
2384 else
2385 {
2386#ifdef FEAT_CLIPBOARD
2387 if (clip_star.available && regname == 0)
2388 regname = '*';
2389#endif
2390 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2391 insert_reg(regname, TRUE);
2392 else
2393 {
2394 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2395
2396 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2397 AppendCharToRedobuff(Ctrl_R);
2398 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2399 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2400 }
2401 }
2402 return FALSE;
2403 }
2404 }
2405
2406 /* When dragging or button-up stay in the same window. */
2407 if (!is_click)
2408 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2409
2410 start_visual.lnum = 0;
2411
2412 /*
2413 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2414 * right button up -> pop-up menu
2415 * shift-left button -> right button
2416 */
2417 if (mouse_model_popup())
2418 {
2419 if (which_button == MOUSE_RIGHT
2420 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2421 {
2422 /*
2423 * NOTE: Ignore right button down and drag mouse events.
2424 * Windows only shows the popup menu on the button up event.
2425 */
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002426#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (!is_click)
2428 return FALSE;
2429#endif
2430#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2431 if (is_click || is_drag)
2432 return FALSE;
2433#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002434#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2436 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2437 if (gui.in_use)
2438 {
2439 jump_flags = 0;
2440 if (STRCMP(p_mousem, "popup_setpos") == 0)
2441 {
2442 /* First set the cursor position before showing the popup
2443 * menu. */
2444#ifdef FEAT_VISUAL
2445 if (VIsual_active)
2446 {
2447 pos_T m_pos;
2448
2449 /*
2450 * set MOUSE_MAY_STOP_VIS if we are outside the
2451 * selection or the current window (might have false
2452 * negative here)
2453 */
2454 if (mouse_row < W_WINROW(curwin)
2455 || mouse_row
2456 > (W_WINROW(curwin) + curwin->w_height))
2457 jump_flags = MOUSE_MAY_STOP_VIS;
2458 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2459 jump_flags = MOUSE_MAY_STOP_VIS;
2460 else
2461 {
2462 if ((lt(curwin->w_cursor, VIsual)
2463 && (lt(m_pos, curwin->w_cursor)
2464 || lt(VIsual, m_pos)))
2465 || (lt(VIsual, curwin->w_cursor)
2466 && (lt(m_pos, VIsual)
2467 || lt(curwin->w_cursor, m_pos))))
2468 {
2469 jump_flags = MOUSE_MAY_STOP_VIS;
2470 }
2471 else if (VIsual_mode == Ctrl_V)
2472 {
2473 getvcols(curwin, &curwin->w_cursor, &VIsual,
2474 &leftcol, &rightcol);
2475 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2476 if (m_pos.col < leftcol || m_pos.col > rightcol)
2477 jump_flags = MOUSE_MAY_STOP_VIS;
2478 }
2479 }
2480 }
2481 else
2482 jump_flags = MOUSE_MAY_STOP_VIS;
2483#endif
2484 }
2485 if (jump_flags)
2486 {
2487 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2488 update_curbuf(
2489#ifdef FEAT_VISUAL
2490 VIsual_active ? INVERTED :
2491#endif
2492 VALID);
2493 setcursor();
2494 out_flush(); /* Update before showing popup menu */
2495 }
2496# ifdef FEAT_MENU
2497 gui_show_popupmenu();
2498# endif
2499 return (jump_flags & CURSOR_MOVED) != 0;
2500 }
2501 else
2502 return FALSE;
2503#else
2504 return FALSE;
2505#endif
2506 }
2507 if (which_button == MOUSE_LEFT && (mod_mask & MOD_MASK_SHIFT))
2508 {
2509 which_button = MOUSE_RIGHT;
2510 mod_mask &= ~MOD_MASK_SHIFT;
2511 }
2512 }
2513
2514#ifdef FEAT_VISUAL
2515 if ((State & (NORMAL | INSERT))
2516 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2517 {
2518 if (which_button == MOUSE_LEFT)
2519 {
2520 if (is_click)
2521 {
2522 /* stop Visual mode for a left click in a window, but not when
2523 * on a status line */
2524 if (VIsual_active)
2525 jump_flags |= MOUSE_MAY_STOP_VIS;
2526 }
2527 else if (mouse_has(MOUSE_VISUAL))
2528 jump_flags |= MOUSE_MAY_VIS;
2529 }
2530 else if (which_button == MOUSE_RIGHT)
2531 {
2532 if (is_click && VIsual_active)
2533 {
2534 /*
2535 * Remember the start and end of visual before moving the
2536 * cursor.
2537 */
2538 if (lt(curwin->w_cursor, VIsual))
2539 {
2540 start_visual = curwin->w_cursor;
2541 end_visual = VIsual;
2542 }
2543 else
2544 {
2545 start_visual = VIsual;
2546 end_visual = curwin->w_cursor;
2547 }
2548 }
2549 jump_flags |= MOUSE_FOCUS;
2550 if (mouse_has(MOUSE_VISUAL))
2551 jump_flags |= MOUSE_MAY_VIS;
2552 }
2553 }
2554#endif
2555
2556 /*
2557 * If an operator is pending, ignore all drags and releases until the
2558 * next mouse click.
2559 */
2560 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2561 {
2562 got_click = FALSE;
2563 oap->motion_type = MCHAR;
2564 }
2565
2566 /* When releasing the button let jump_to_mouse() know. */
2567 if (!is_click && !is_drag)
2568 jump_flags |= MOUSE_RELEASED;
2569
2570 /*
2571 * JUMP!
2572 */
2573 jump_flags = jump_to_mouse(jump_flags,
2574 oap == NULL ? NULL : &(oap->inclusive), which_button);
2575 moved = (jump_flags & CURSOR_MOVED);
2576 in_status_line = (jump_flags & IN_STATUS_LINE);
2577#ifdef FEAT_VERTSPLIT
2578 in_sep_line = (jump_flags & IN_SEP_LINE);
2579#endif
2580
2581#ifdef FEAT_NETBEANS_INTG
2582 if (usingNetbeans && isNetbeansBuffer(curbuf)
2583 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2584 {
2585 int key = KEY2TERMCAP1(c);
2586
2587 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2588 || key == (int)KE_RIGHTRELEASE)
2589 netbeans_button_release(which_button);
2590 }
2591#endif
2592
2593 /* When jumping to another window, clear a pending operator. That's a bit
2594 * friendlier than beeping and not jumping to that window. */
2595 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2596 clearop(oap);
2597
2598#ifdef FEAT_FOLDING
2599 if (mod_mask == 0
2600 && !is_drag
2601 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2602 && which_button == MOUSE_LEFT)
2603 {
2604 /* open or close a fold at this line */
2605 if (jump_flags & MOUSE_FOLD_OPEN)
2606 openFold(curwin->w_cursor.lnum, 1L);
2607 else
2608 closeFold(curwin->w_cursor.lnum, 1L);
2609 /* don't move the cursor if still in the same window */
2610 if (curwin == old_curwin)
2611 curwin->w_cursor = save_cursor;
2612 }
2613#endif
2614
2615#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2616 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2617 {
2618 clip_modeless(which_button, is_click, is_drag);
2619 return FALSE;
2620 }
2621#endif
2622
2623#ifdef FEAT_VISUAL
2624 /* Set global flag that we are extending the Visual area with mouse
2625 * dragging; temporarily mimimize 'scrolloff'. */
2626 if (VIsual_active && is_drag && p_so)
2627 {
2628 /* In the very first line, allow scrolling one line */
2629 if (mouse_row == 0)
2630 mouse_dragging = 2;
2631 else
2632 mouse_dragging = 1;
2633 }
2634
2635 /* When dragging the mouse above the window, scroll down. */
2636 if (is_drag && mouse_row < 0 && !in_status_line)
2637 {
2638 scroll_redraw(FALSE, 1L);
2639 mouse_row = 0;
2640 }
2641
2642 if (start_visual.lnum) /* right click in visual mode */
2643 {
2644 /*
2645 * In Visual-block mode, divide the area in four, pick up the corner
2646 * that is in the quarter that the cursor is in.
2647 */
2648 if (VIsual_mode == Ctrl_V)
2649 {
2650 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2651 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2652 end_visual.col = leftcol;
2653 else
2654 end_visual.col = rightcol;
2655 if (curwin->w_cursor.lnum <
2656 (start_visual.lnum + end_visual.lnum) / 2)
2657 end_visual.lnum = end_visual.lnum;
2658 else
2659 end_visual.lnum = start_visual.lnum;
2660
2661 /* move VIsual to the right column */
2662 start_visual = curwin->w_cursor; /* save the cursor pos */
2663 curwin->w_cursor = end_visual;
2664 coladvance(end_visual.col);
2665 VIsual = curwin->w_cursor;
2666 curwin->w_cursor = start_visual; /* restore the cursor */
2667 }
2668 else
2669 {
2670 /*
2671 * If the click is before the start of visual, change the start.
2672 * If the click is after the end of visual, change the end. If
2673 * the click is inside the visual, change the closest side.
2674 */
2675 if (lt(curwin->w_cursor, start_visual))
2676 VIsual = end_visual;
2677 else if (lt(end_visual, curwin->w_cursor))
2678 VIsual = start_visual;
2679 else
2680 {
2681 /* In the same line, compare column number */
2682 if (end_visual.lnum == start_visual.lnum)
2683 {
2684 if (curwin->w_cursor.col - start_visual.col >
2685 end_visual.col - curwin->w_cursor.col)
2686 VIsual = start_visual;
2687 else
2688 VIsual = end_visual;
2689 }
2690
2691 /* In different lines, compare line number */
2692 else
2693 {
2694 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2695 (end_visual.lnum - curwin->w_cursor.lnum);
2696
2697 if (diff > 0) /* closest to end */
2698 VIsual = start_visual;
2699 else if (diff < 0) /* closest to start */
2700 VIsual = end_visual;
2701 else /* in the middle line */
2702 {
2703 if (curwin->w_cursor.col <
2704 (start_visual.col + end_visual.col) / 2)
2705 VIsual = end_visual;
2706 else
2707 VIsual = start_visual;
2708 }
2709 }
2710 }
2711 }
2712 }
2713 /*
2714 * If Visual mode started in insert mode, execute "CTRL-O"
2715 */
2716 else if ((State & INSERT) && VIsual_active)
2717 stuffcharReadbuff(Ctrl_O);
2718#endif
2719
2720 /*
2721 * Middle mouse click: Put text before cursor.
2722 */
2723 if (which_button == MOUSE_MIDDLE)
2724 {
2725#ifdef FEAT_CLIPBOARD
2726 if (clip_star.available && regname == 0)
2727 regname = '*';
2728#endif
2729 if (yank_register_mline(regname))
2730 {
2731 if (mouse_past_bottom)
2732 dir = FORWARD;
2733 }
2734 else if (mouse_past_eol)
2735 dir = FORWARD;
2736
2737 if (fixindent)
2738 {
2739 c1 = (dir == BACKWARD) ? '[' : ']';
2740 c2 = 'p';
2741 }
2742 else
2743 {
2744 c1 = (dir == FORWARD) ? 'p' : 'P';
2745 c2 = NUL;
2746 }
2747 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2748
2749 /*
2750 * Remember where the paste started, so in edit() Insstart can be set
2751 * to this position
2752 */
2753 if (restart_edit != 0)
2754 where_paste_started = curwin->w_cursor;
2755 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2756 }
2757
2758#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2759 /*
2760 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2761 * error under the mouse pointer.
2762 */
2763 else if (((mod_mask & MOD_MASK_CTRL)
2764 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2765 && bt_quickfix(curbuf))
2766 {
2767 if (State & INSERT)
2768 stuffcharReadbuff(Ctrl_O);
2769 stuffReadbuff((char_u *)":.cc\n");
2770 got_click = FALSE; /* ignore drag&release now */
2771 }
2772#endif
2773
2774 /*
2775 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2776 * under the mouse pointer.
2777 */
2778 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2779 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2780 {
2781 if (State & INSERT)
2782 stuffcharReadbuff(Ctrl_O);
2783 stuffcharReadbuff(Ctrl_RSB);
2784 got_click = FALSE; /* ignore drag&release now */
2785 }
2786
2787 /*
2788 * Shift-Mouse click searches for the next occurrence of the word under
2789 * the mouse pointer
2790 */
2791 else if ((mod_mask & MOD_MASK_SHIFT))
2792 {
2793 if (State & INSERT
2794#ifdef FEAT_VISUAL
2795 || (VIsual_active && VIsual_select)
2796#endif
2797 )
2798 stuffcharReadbuff(Ctrl_O);
2799 if (which_button == MOUSE_LEFT)
2800 stuffcharReadbuff('*');
2801 else /* MOUSE_RIGHT */
2802 stuffcharReadbuff('#');
2803 }
2804
2805 /* Handle double clicks, unless on status line */
2806 else if (in_status_line)
2807 {
2808#ifdef FEAT_MOUSESHAPE
2809 if ((is_drag || is_click) && !drag_status_line)
2810 {
2811 drag_status_line = TRUE;
2812 update_mouseshape(-1);
2813 }
2814#endif
2815 }
2816#ifdef FEAT_VERTSPLIT
2817 else if (in_sep_line)
2818 {
2819# ifdef FEAT_MOUSESHAPE
2820 if ((is_drag || is_click) && !drag_sep_line)
2821 {
2822 drag_sep_line = TRUE;
2823 update_mouseshape(-1);
2824 }
2825# endif
2826 }
2827#endif
2828#ifdef FEAT_VISUAL
2829 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2830 && mouse_has(MOUSE_VISUAL))
2831 {
2832 if (is_click || !VIsual_active)
2833 {
2834 if (VIsual_active)
2835 orig_cursor = VIsual;
2836 else
2837 {
2838 check_visual_highlight();
2839 VIsual = curwin->w_cursor;
2840 orig_cursor = VIsual;
2841 VIsual_active = TRUE;
2842 VIsual_reselect = TRUE;
2843 /* start Select mode if 'selectmode' contains "mouse" */
2844 may_start_select('o');
2845 setmouse();
2846 }
2847 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2848 VIsual_mode = 'v';
2849 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2850 VIsual_mode = 'V';
2851 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2852 VIsual_mode = Ctrl_V;
2853#ifdef FEAT_CLIPBOARD
2854 /* Make sure the clipboard gets updated. Needed because start and
2855 * end may still be the same, and the selection needs to be owned */
2856 clip_star.vmode = NUL;
2857#endif
2858 }
2859 /*
2860 * A double click selects a word or a block.
2861 */
2862 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2863 {
2864 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002865 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
2867 if (is_click)
2868 {
2869 /* If the character under the cursor (skipping white space) is
2870 * not a word character, try finding a match and select a (),
2871 * {}, [], #if/#endif, etc. block. */
2872 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002873 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 inc(&end_visual);
2875 if (oap != NULL)
2876 oap->motion_type = MCHAR;
2877 if (oap != NULL
2878 && VIsual_mode == 'v'
2879 && !vim_iswordc(gchar_pos(&end_visual))
2880 && equalpos(curwin->w_cursor, VIsual)
2881 && (pos = findmatch(oap, NUL)) != NULL)
2882 {
2883 curwin->w_cursor = *pos;
2884 if (oap->motion_type == MLINE)
2885 VIsual_mode = 'V';
2886 else if (*p_sel == 'e')
2887 {
2888 if (lt(curwin->w_cursor, VIsual))
2889 ++VIsual.col;
2890 else
2891 ++curwin->w_cursor.col;
2892 }
2893 }
2894 }
2895
2896 if (pos == NULL && (is_click || is_drag))
2897 {
2898 /* When not found a match or when dragging: extend to include
2899 * a word. */
2900 if (lt(curwin->w_cursor, orig_cursor))
2901 {
2902 find_start_of_word(&curwin->w_cursor);
2903 find_end_of_word(&VIsual);
2904 }
2905 else
2906 {
2907 find_start_of_word(&VIsual);
2908 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
2909#ifdef FEAT_MBYTE
2910 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002911 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912#else
2913 ++curwin->w_cursor.col;
2914#endif
2915 find_end_of_word(&curwin->w_cursor);
2916 }
2917 }
2918 curwin->w_set_curswant = TRUE;
2919 }
2920 if (is_click)
2921 redraw_curbuf_later(INVERTED); /* update the inversion */
2922 }
2923 else if (VIsual_active && !old_active)
2924 VIsual_mode = 'v';
2925
2926 /* If Visual mode changed show it later. */
2927 if (p_smd && (VIsual_active != old_active || VIsual_mode != old_mode))
2928 redraw_cmdline = TRUE;
2929#endif
2930
2931 return moved;
2932}
2933
2934#ifdef FEAT_VISUAL
2935/*
2936 * Move "pos" back to the start of the word it's in.
2937 */
2938 static void
2939find_start_of_word(pos)
2940 pos_T *pos;
2941{
2942 char_u *line;
2943 int cclass;
2944 int col;
2945
2946 line = ml_get(pos->lnum);
2947 cclass = get_mouse_class(line + pos->col);
2948
2949 while (pos->col > 0)
2950 {
2951 col = pos->col - 1;
2952#ifdef FEAT_MBYTE
2953 col -= (*mb_head_off)(line, line + col);
2954#endif
2955 if (get_mouse_class(line + col) != cclass)
2956 break;
2957 pos->col = col;
2958 }
2959}
2960
2961/*
2962 * Move "pos" forward to the end of the word it's in.
2963 * When 'selection' is "exclusive", the position is just after the word.
2964 */
2965 static void
2966find_end_of_word(pos)
2967 pos_T *pos;
2968{
2969 char_u *line;
2970 int cclass;
2971 int col;
2972
2973 line = ml_get(pos->lnum);
2974 if (*p_sel == 'e' && pos->col > 0)
2975 {
2976 --pos->col;
2977#ifdef FEAT_MBYTE
2978 pos->col -= (*mb_head_off)(line, line + pos->col);
2979#endif
2980 }
2981 cclass = get_mouse_class(line + pos->col);
2982 while (line[pos->col] != NUL)
2983 {
2984#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002985 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986#else
2987 col = pos->col + 1;
2988#endif
2989 if (get_mouse_class(line + col) != cclass)
2990 {
2991 if (*p_sel == 'e')
2992 pos->col = col;
2993 break;
2994 }
2995 pos->col = col;
2996 }
2997}
2998
2999/*
3000 * Get class of a character for selection: same class means same word.
3001 * 0: blank
3002 * 1: punctuation groups
3003 * 2: normal word character
3004 * >2: multi-byte word character.
3005 */
3006 static int
3007get_mouse_class(p)
3008 char_u *p;
3009{
3010 int c;
3011
3012#ifdef FEAT_MBYTE
3013 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3014 return mb_get_class(p);
3015#endif
3016
3017 c = *p;
3018 if (c == ' ' || c == '\t')
3019 return 0;
3020
3021 if (vim_iswordc(c))
3022 return 2;
3023
3024 /*
3025 * There are a few special cases where we want certain combinations of
3026 * characters to be considered as a single word. These are things like
3027 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3028 * character is in it's own class.
3029 */
3030 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3031 return 1;
3032 return c;
3033}
3034#endif /* FEAT_VISUAL */
3035#endif /* FEAT_MOUSE */
3036
3037#if defined(FEAT_VISUAL) || defined(PROTO)
3038/*
3039 * Check if highlighting for visual mode is possible, give a warning message
3040 * if not.
3041 */
3042 void
3043check_visual_highlight()
3044{
3045 static int did_check = FALSE;
3046
3047 if (full_screen)
3048 {
3049 if (!did_check && hl_attr(HLF_V) == 0)
3050 MSG(_("Warning: terminal cannot highlight"));
3051 did_check = TRUE;
3052 }
3053}
3054
3055/*
3056 * End visual mode.
3057 * This function should ALWAYS be called to end Visual mode, except from
3058 * do_pending_operator().
3059 */
3060 void
3061end_visual_mode()
3062{
3063#ifdef FEAT_CLIPBOARD
3064 /*
3065 * If we are using the clipboard, then remember what was selected in case
3066 * we need to paste it somewhere while we still own the selection.
3067 * Only do this when the clipboard is already owned. Don't want to grab
3068 * the selection when hitting ESC.
3069 */
3070 if (clip_star.available && clip_star.owned)
3071 clip_auto_select();
3072#endif
3073
3074 VIsual_active = FALSE;
3075#ifdef FEAT_MOUSE
3076 setmouse();
3077 mouse_dragging = 0;
3078#endif
3079
3080 /* Save the current VIsual area for '< and '> marks, and "gv" */
3081 curbuf->b_visual_mode = VIsual_mode;
3082#ifdef FEAT_EVAL
3083 curbuf->b_visual_mode_eval = VIsual_mode;
3084#endif
3085 curbuf->b_visual_start = VIsual;
3086 curbuf->b_visual_end = curwin->w_cursor;
3087 curbuf->b_visual_curswant = curwin->w_curswant;
3088#ifdef FEAT_VIRTUALEDIT
3089 if (!virtual_active())
3090 curwin->w_cursor.coladd = 0;
3091#endif
3092
3093 if (p_smd)
3094 clear_cmdline = TRUE; /* unshow visual mode later */
3095#ifdef FEAT_CMDL_INFO
3096 else
3097 clear_showcmd();
3098#endif
3099
3100 /* Don't leave the cursor past the end of the line */
3101 if (curwin->w_cursor.col > 0 && *ml_get_cursor() == NUL)
3102 --curwin->w_cursor.col;
3103}
3104
3105/*
3106 * Reset VIsual_active and VIsual_reselect.
3107 */
3108 void
3109reset_VIsual_and_resel()
3110{
3111 if (VIsual_active)
3112 {
3113 end_visual_mode();
3114 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3115 }
3116 VIsual_reselect = FALSE;
3117}
3118
3119/*
3120 * Reset VIsual_active and VIsual_reselect if it's set.
3121 */
3122 void
3123reset_VIsual()
3124{
3125 if (VIsual_active)
3126 {
3127 end_visual_mode();
3128 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3129 VIsual_reselect = FALSE;
3130 }
3131}
3132#endif /* FEAT_VISUAL */
3133
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003134#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3136
3137/*
3138 * Check for a balloon-eval special item to include when searching for an
3139 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3140 * Returns TRUE if the character at "*ptr" should be included.
3141 * "dir" is FORWARD or BACKWARD, the direction of searching.
3142 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3143 * "bnp" points to a counter for square brackets.
3144 */
3145 static int
3146find_is_eval_item(ptr, colp, bnp, dir)
3147 char_u *ptr;
3148 int *colp;
3149 int *bnp;
3150 int dir;
3151{
3152 /* Accept everything inside []. */
3153 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3154 ++*bnp;
3155 if (*bnp > 0)
3156 {
3157 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3158 --*bnp;
3159 return TRUE;
3160 }
3161
3162 /* skip over "s.var" */
3163 if (*ptr == '.')
3164 return TRUE;
3165
3166 /* two-character item: s->var */
3167 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3168 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3169 {
3170 *colp += dir;
3171 return TRUE;
3172 }
3173 return FALSE;
3174}
3175#endif
3176
3177/*
3178 * Find the identifier under or to the right of the cursor.
3179 * "find_type" can have one of three values:
3180 * FIND_IDENT: find an identifier (keyword)
3181 * FIND_STRING: find any non-white string
3182 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 *
3185 * There are three steps:
3186 * 1. Search forward for the start of an identifier/string. Doesn't move if
3187 * already on one.
3188 * 2. Search backward for the start of this identifier/string.
3189 * This doesn't match the real Vi but I like it a little better and it
3190 * shouldn't bother anyone.
3191 * 3. Search forward to the end of this identifier/string.
3192 * When FIND_IDENT isn't defined, we backup until a blank.
3193 *
3194 * Returns the length of the string, or zero if no string is found.
3195 * If a string is found, a pointer to the string is put in "*string". This
3196 * string is not always NUL terminated.
3197 */
3198 int
3199find_ident_under_cursor(string, find_type)
3200 char_u **string;
3201 int find_type;
3202{
3203 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3204 curwin->w_cursor.col, string, find_type);
3205}
3206
3207/*
3208 * Like find_ident_under_cursor(), but for any window and any position.
3209 * However: Uses 'iskeyword' from the current window!.
3210 */
3211 int
3212find_ident_at_pos(wp, lnum, startcol, string, find_type)
3213 win_T *wp;
3214 linenr_T lnum;
3215 colnr_T startcol;
3216 char_u **string;
3217 int find_type;
3218{
3219 char_u *ptr;
3220 int col = 0; /* init to shut up GCC */
3221 int i;
3222#ifdef FEAT_MBYTE
3223 int this_class = 0;
3224 int prev_class;
3225 int prevcol;
3226#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 int bn = 0; /* bracket nesting */
3229#endif
3230
3231 /*
3232 * if i == 0: try to find an identifier
3233 * if i == 1: try to find any non-white string
3234 */
3235 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3236 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3237 {
3238 /*
3239 * 1. skip to start of identifier/string
3240 */
3241 col = startcol;
3242#ifdef FEAT_MBYTE
3243 if (has_mbyte)
3244 {
3245 while (ptr[col] != NUL)
3246 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 /* Stop at a ']' to evaluate "a[x]". */
3249 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3250 break;
3251# endif
3252 this_class = mb_get_class(ptr + col);
3253 if (this_class != 0 && (i == 1 || this_class != 1))
3254 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003255 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 }
3258 else
3259#endif
3260 while (ptr[col] != NUL
3261 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3264# endif
3265 )
3266 ++col;
3267
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003268#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /* When starting on a ']' count it, so that we include the '['. */
3270 bn = ptr[col] == ']';
3271#endif
3272
3273 /*
3274 * 2. Back up to start of identifier/string.
3275 */
3276#ifdef FEAT_MBYTE
3277 if (has_mbyte)
3278 {
3279 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003280# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3282 this_class = mb_get_class((char_u *)"a");
3283 else
3284# endif
3285 this_class = mb_get_class(ptr + col);
3286 while (col > 0)
3287 {
3288 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3289 prev_class = mb_get_class(ptr + prevcol);
3290 if (this_class != prev_class
3291 && (i == 0
3292 || prev_class == 0
3293 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003294# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 && (!(find_type & FIND_EVAL)
3296 || prevcol == 0
3297 || !find_is_eval_item(ptr + prevcol, &prevcol,
3298 &bn, BACKWARD))
3299# endif
3300 )
3301 break;
3302 col = prevcol;
3303 }
3304
3305 /* If we don't want just any old string, or we've found an
3306 * identifier, stop searching. */
3307 if (this_class > 2)
3308 this_class = 2;
3309 if (!(find_type & FIND_STRING) || this_class == 2)
3310 break;
3311 }
3312 else
3313#endif
3314 {
3315 while (col > 0
3316 && ((i == 0
3317 ? vim_iswordc(ptr[col - 1])
3318 : (!vim_iswhite(ptr[col - 1])
3319 && (!(find_type & FIND_IDENT)
3320 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 || ((find_type & FIND_EVAL)
3323 && col > 1
3324 && find_is_eval_item(ptr + col - 1, &col,
3325 &bn, BACKWARD))
3326#endif
3327 ))
3328 --col;
3329
3330 /* If we don't want just any old string, or we've found an
3331 * identifier, stop searching. */
3332 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3333 break;
3334 }
3335 }
3336
3337 if (ptr[col] == NUL || (i == 0 && (
3338#ifdef FEAT_MBYTE
3339 has_mbyte ? this_class != 2 :
3340#endif
3341 !vim_iswordc(ptr[col]))))
3342 {
3343 /*
3344 * didn't find an identifier or string
3345 */
3346 if (find_type & FIND_STRING)
3347 EMSG(_("E348: No string under cursor"));
3348 else
3349 EMSG(_("E349: No identifier under cursor"));
3350 return 0;
3351 }
3352 ptr += col;
3353 *string = ptr;
3354
3355 /*
3356 * 3. Find the end if the identifier/string.
3357 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 bn = 0;
3360 startcol -= col;
3361#endif
3362 col = 0;
3363#ifdef FEAT_MBYTE
3364 if (has_mbyte)
3365 {
3366 /* Search for point of changing multibyte character class. */
3367 this_class = mb_get_class(ptr);
3368 while (ptr[col] != NUL
3369 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3370 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003371# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 || ((find_type & FIND_EVAL)
3373 && col <= (int)startcol
3374 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3375# endif
3376 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003377 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else
3380#endif
3381 while ((i == 0 ? vim_iswordc(ptr[col])
3382 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 || ((find_type & FIND_EVAL)
3385 && col <= (int)startcol
3386 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3387# endif
3388 )
3389 {
3390 ++col;
3391 }
3392
3393 return col;
3394}
3395
3396/*
3397 * Prepare for redo of a normal command.
3398 */
3399 static void
3400prep_redo_cmd(cap)
3401 cmdarg_T *cap;
3402{
3403 prep_redo(cap->oap->regname, cap->count0,
3404 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3405}
3406
3407/*
3408 * Prepare for redo of any command.
3409 * Note that only the last argument can be a multi-byte char.
3410 */
3411 static void
3412prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3413 int regname;
3414 long num;
3415 int cmd1;
3416 int cmd2;
3417 int cmd3;
3418 int cmd4;
3419 int cmd5;
3420{
3421 ResetRedobuff();
3422 if (regname != 0) /* yank from specified buffer */
3423 {
3424 AppendCharToRedobuff('"');
3425 AppendCharToRedobuff(regname);
3426 }
3427 if (num)
3428 AppendNumberToRedobuff(num);
3429
3430 if (cmd1 != NUL)
3431 AppendCharToRedobuff(cmd1);
3432 if (cmd2 != NUL)
3433 AppendCharToRedobuff(cmd2);
3434 if (cmd3 != NUL)
3435 AppendCharToRedobuff(cmd3);
3436 if (cmd4 != NUL)
3437 AppendCharToRedobuff(cmd4);
3438 if (cmd5 != NUL)
3439 AppendCharToRedobuff(cmd5);
3440}
3441
3442/*
3443 * check for operator active and clear it
3444 *
3445 * return TRUE if operator was active
3446 */
3447 static int
3448checkclearop(oap)
3449 oparg_T *oap;
3450{
3451 if (oap->op_type == OP_NOP)
3452 return FALSE;
3453 clearopbeep(oap);
3454 return TRUE;
3455}
3456
3457/*
3458 * check for operator or Visual active and clear it
3459 *
3460 * return TRUE if operator was active
3461 */
3462 static int
3463checkclearopq(oap)
3464 oparg_T *oap;
3465{
3466 if (oap->op_type == OP_NOP
3467#ifdef FEAT_VISUAL
3468 && !VIsual_active
3469#endif
3470 )
3471 return FALSE;
3472 clearopbeep(oap);
3473 return TRUE;
3474}
3475
3476 static void
3477clearop(oap)
3478 oparg_T *oap;
3479{
3480 oap->op_type = OP_NOP;
3481 oap->regname = 0;
3482 oap->motion_force = NUL;
3483 oap->use_reg_one = FALSE;
3484}
3485
3486 static void
3487clearopbeep(oap)
3488 oparg_T *oap;
3489{
3490 clearop(oap);
3491 beep_flush();
3492}
3493
3494#ifdef FEAT_VISUAL
3495/*
3496 * Remove the shift modifier from a special key.
3497 */
3498 static void
3499unshift_special(cap)
3500 cmdarg_T *cap;
3501{
3502 switch (cap->cmdchar)
3503 {
3504 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3505 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3506 case K_S_UP: cap->cmdchar = K_UP; break;
3507 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3508 case K_S_HOME: cap->cmdchar = K_HOME; break;
3509 case K_S_END: cap->cmdchar = K_END; break;
3510 }
3511 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3512}
3513#endif
3514
3515#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3516/*
3517 * Routines for displaying a partly typed command
3518 */
3519
3520#ifdef FEAT_VISUAL /* need room for size of Visual area */
3521# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3522#else
3523# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3524#endif
3525static char_u showcmd_buf[SHOWCMD_BUFLEN];
3526static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3527static int showcmd_is_clear = TRUE;
3528static int showcmd_visual = FALSE;
3529
3530static void display_showcmd __ARGS((void));
3531
3532 void
3533clear_showcmd()
3534{
3535 if (!p_sc)
3536 return;
3537
3538#ifdef FEAT_VISUAL
3539 if (VIsual_active && !char_avail())
3540 {
3541 int i = lt(VIsual, curwin->w_cursor);
3542 long lines;
3543 colnr_T leftcol, rightcol;
3544 linenr_T top, bot;
3545
3546 /* Show the size of the Visual area. */
3547 if (i)
3548 {
3549 top = VIsual.lnum;
3550 bot = curwin->w_cursor.lnum;
3551 }
3552 else
3553 {
3554 top = curwin->w_cursor.lnum;
3555 bot = VIsual.lnum;
3556 }
3557# ifdef FEAT_FOLDING
3558 /* Include closed folds as a whole. */
3559 hasFolding(top, &top, NULL);
3560 hasFolding(bot, NULL, &bot);
3561# endif
3562 lines = bot - top + 1;
3563
3564 if (VIsual_mode == Ctrl_V)
3565 {
3566 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3567 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3568 (long)(rightcol - leftcol + 1));
3569 }
3570 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3571 sprintf((char *)showcmd_buf, "%ld", lines);
3572 else
3573 sprintf((char *)showcmd_buf, "%ld", (long)(i
3574 ? curwin->w_cursor.col - VIsual.col
3575 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3576 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3577 showcmd_visual = TRUE;
3578 }
3579 else
3580#endif
3581 {
3582 showcmd_buf[0] = NUL;
3583 showcmd_visual = FALSE;
3584
3585 /* Don't actually display something if there is nothing to clear. */
3586 if (showcmd_is_clear)
3587 return;
3588 }
3589
3590 display_showcmd();
3591}
3592
3593/*
3594 * Add 'c' to string of shown command chars.
3595 * Return TRUE if output has been written (and setcursor() has been called).
3596 */
3597 int
3598add_to_showcmd(c)
3599 int c;
3600{
3601 char_u *p;
3602 int old_len;
3603 int extra_len;
3604 int overflow;
3605#if defined(FEAT_MOUSE)
3606 int i;
3607 static int ignore[] =
3608 {
3609#ifdef FEAT_GUI
3610 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3611 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3612#endif
3613 K_IGNORE,
3614 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3615 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3616 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3617 K_MOUSEDOWN, K_MOUSEUP,
3618 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
3619 0
3620 };
3621#endif
3622
3623 if (!p_sc)
3624 return FALSE;
3625
3626 if (showcmd_visual)
3627 {
3628 showcmd_buf[0] = NUL;
3629 showcmd_visual = FALSE;
3630 }
3631
3632#if defined(FEAT_MOUSE)
3633 /* Ignore keys that are scrollbar updates and mouse clicks */
3634 if (IS_SPECIAL(c))
3635 for (i = 0; ignore[i] != 0; ++i)
3636 if (ignore[i] == c)
3637 return FALSE;
3638#endif
3639
3640 p = transchar(c);
3641 old_len = (int)STRLEN(showcmd_buf);
3642 extra_len = (int)STRLEN(p);
3643 overflow = old_len + extra_len - SHOWCMD_COLS;
3644 if (overflow > 0)
3645 STRCPY(showcmd_buf, showcmd_buf + overflow);
3646 STRCAT(showcmd_buf, p);
3647
3648 if (char_avail())
3649 return FALSE;
3650
3651 display_showcmd();
3652
3653 return TRUE;
3654}
3655
3656 void
3657add_to_showcmd_c(c)
3658 int c;
3659{
3660 if (!add_to_showcmd(c))
3661 setcursor();
3662}
3663
3664/*
3665 * Delete 'len' characters from the end of the shown command.
3666 */
3667 static void
3668del_from_showcmd(len)
3669 int len;
3670{
3671 int old_len;
3672
3673 if (!p_sc)
3674 return;
3675
3676 old_len = (int)STRLEN(showcmd_buf);
3677 if (len > old_len)
3678 len = old_len;
3679 showcmd_buf[old_len - len] = NUL;
3680
3681 if (!char_avail())
3682 display_showcmd();
3683}
3684
3685/*
3686 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3687 * something and there is a partial mapping.
3688 */
3689 void
3690push_showcmd()
3691{
3692 if (p_sc)
3693 STRCPY(old_showcmd_buf, showcmd_buf);
3694}
3695
3696 void
3697pop_showcmd()
3698{
3699 if (!p_sc)
3700 return;
3701
3702 STRCPY(showcmd_buf, old_showcmd_buf);
3703
3704 display_showcmd();
3705}
3706
3707 static void
3708display_showcmd()
3709{
3710 int len;
3711
3712 cursor_off();
3713
3714 len = (int)STRLEN(showcmd_buf);
3715 if (len == 0)
3716 showcmd_is_clear = TRUE;
3717 else
3718 {
3719 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3720 showcmd_is_clear = FALSE;
3721 }
3722
3723 /*
3724 * clear the rest of an old message by outputing up to SHOWCMD_COLS spaces
3725 */
3726 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3727
3728 setcursor(); /* put cursor back where it belongs */
3729}
3730#endif
3731
3732#ifdef FEAT_SCROLLBIND
3733/*
3734 * When "check" is FALSE, prepare for commands that scroll the window.
3735 * When "check" is TRUE, take care of scroll-binding after the window has
3736 * scrolled. Called from normal_cmd() and edit().
3737 */
3738 void
3739do_check_scrollbind(check)
3740 int check;
3741{
3742 static win_T *old_curwin = NULL;
3743 static linenr_T old_topline = 0;
3744#ifdef FEAT_DIFF
3745 static int old_topfill = 0;
3746#endif
3747 static buf_T *old_buf = NULL;
3748 static colnr_T old_leftcol = 0;
3749
3750 if (check && curwin->w_p_scb)
3751 {
3752 /* If a ":syncbind" command was just used, don't scroll, only reset
3753 * the values. */
3754 if (did_syncbind)
3755 did_syncbind = FALSE;
3756 else if (curwin == old_curwin)
3757 {
3758 /*
3759 * Synchronize other windows, as necessary according to
3760 * 'scrollbind'. Don't do this after an ":edit" command, except
3761 * when 'diff' is set.
3762 */
3763 if ((curwin->w_buffer == old_buf
3764#ifdef FEAT_DIFF
3765 || curwin->w_p_diff
3766#endif
3767 )
3768 && (curwin->w_topline != old_topline
3769#ifdef FEAT_DIFF
3770 || curwin->w_topfill != old_topfill
3771#endif
3772 || curwin->w_leftcol != old_leftcol))
3773 {
3774 check_scrollbind(curwin->w_topline - old_topline,
3775 (long)(curwin->w_leftcol - old_leftcol));
3776 }
3777 }
3778 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3779 {
3780 /*
3781 * When switching between windows, make sure that the relative
3782 * vertical offset is valid for the new window. The relative
3783 * offset is invalid whenever another 'scrollbind' window has
3784 * scrolled to a point that would force the current window to
3785 * scroll past the beginning or end of its buffer. When the
3786 * resync is performed, some of the other 'scrollbind' windows may
3787 * need to jump so that the current window's relative position is
3788 * visible on-screen.
3789 */
3790 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3791 }
3792 curwin->w_scbind_pos = curwin->w_topline;
3793 }
3794
3795 old_curwin = curwin;
3796 old_topline = curwin->w_topline;
3797#ifdef FEAT_DIFF
3798 old_topfill = curwin->w_topfill;
3799#endif
3800 old_buf = curwin->w_buffer;
3801 old_leftcol = curwin->w_leftcol;
3802}
3803
3804/*
3805 * Synchronize any windows that have "scrollbind" set, based on the
3806 * number of rows by which the current window has changed
3807 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3808 */
3809 void
3810check_scrollbind(topline_diff, leftcol_diff)
3811 linenr_T topline_diff;
3812 long leftcol_diff;
3813{
3814 int want_ver;
3815 int want_hor;
3816 win_T *old_curwin = curwin;
3817 buf_T *old_curbuf = curbuf;
3818#ifdef FEAT_VISUAL
3819 int old_VIsual_select = VIsual_select;
3820 int old_VIsual_active = VIsual_active;
3821#endif
3822 colnr_T tgt_leftcol = curwin->w_leftcol;
3823 long topline;
3824 long y;
3825
3826 /*
3827 * check 'scrollopt' string for vertical and horizontal scroll options
3828 */
3829 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3830#ifdef FEAT_DIFF
3831 want_ver |= old_curwin->w_p_diff;
3832#endif
3833 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3834
3835 /*
3836 * loop through the scrollbound windows and scroll accordingly
3837 */
3838#ifdef FEAT_VISUAL
3839 VIsual_select = VIsual_active = 0;
3840#endif
3841 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3842 {
3843 curbuf = curwin->w_buffer;
3844 /* skip original window and windows with 'noscrollbind' */
3845 if (curwin != old_curwin && curwin->w_p_scb)
3846 {
3847 /*
3848 * do the vertical scroll
3849 */
3850 if (want_ver)
3851 {
3852#ifdef FEAT_DIFF
3853 if (old_curwin->w_p_diff && curwin->w_p_diff)
3854 {
3855 diff_set_topline(old_curwin, curwin);
3856 }
3857 else
3858#endif
3859 {
3860 curwin->w_scbind_pos += topline_diff;
3861 topline = curwin->w_scbind_pos;
3862 if (topline > curbuf->b_ml.ml_line_count)
3863 topline = curbuf->b_ml.ml_line_count;
3864 if (topline < 1)
3865 topline = 1;
3866
3867 y = topline - curwin->w_topline;
3868 if (y > 0)
3869 scrollup(y, FALSE);
3870 else
3871 scrolldown(-y, FALSE);
3872 }
3873
3874 redraw_later(VALID);
3875 cursor_correct();
3876#ifdef FEAT_WINDOWS
3877 curwin->w_redr_status = TRUE;
3878#endif
3879 }
3880
3881 /*
3882 * do the horizontal scroll
3883 */
3884 if (want_hor && curwin->w_leftcol != tgt_leftcol)
3885 {
3886 curwin->w_leftcol = tgt_leftcol;
3887 leftcol_changed();
3888 }
3889 }
3890 }
3891
3892 /*
3893 * reset current-window
3894 */
3895#ifdef FEAT_VISUAL
3896 VIsual_select = old_VIsual_select;
3897 VIsual_active = old_VIsual_active;
3898#endif
3899 curwin = old_curwin;
3900 curbuf = old_curbuf;
3901}
3902#endif /* #ifdef FEAT_SCROLLBIND */
3903
3904/*
3905 * Command character that's ignored.
3906 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
3907 * xon/xoff
3908 */
3909/*ARGSUSED */
3910 static void
3911nv_ignore(cap)
3912 cmdarg_T *cap;
3913{
Bram Moolenaarfc735152005-03-22 22:54:12 +00003914 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915}
3916
3917/*
3918 * Command character doesn't exist.
3919 */
3920 static void
3921nv_error(cap)
3922 cmdarg_T *cap;
3923{
3924 clearopbeep(cap->oap);
3925}
3926
3927/*
3928 * <Help> and <F1> commands.
3929 */
3930 static void
3931nv_help(cap)
3932 cmdarg_T *cap;
3933{
3934 if (!checkclearopq(cap->oap))
3935 ex_help(NULL);
3936}
3937
3938/*
3939 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
3940 */
3941 static void
3942nv_addsub(cap)
3943 cmdarg_T *cap;
3944{
3945 if (!checkclearopq(cap->oap)
3946 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
3947 prep_redo_cmd(cap);
3948}
3949
3950/*
3951 * CTRL-F, CTRL-B, etc: Scroll page up or down.
3952 */
3953 static void
3954nv_page(cap)
3955 cmdarg_T *cap;
3956{
3957 if (!checkclearop(cap->oap))
3958 (void)onepage(cap->arg, cap->count1);
3959}
3960
3961/*
3962 * Implementation of "gd" and "gD" command.
3963 */
3964 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003965nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003966 oparg_T *oap;
3967 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003968 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
3970 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 char_u *ptr;
3972
Bram Moolenaard9d30582005-05-18 22:10:28 +00003973 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003974 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003976#ifdef FEAT_FOLDING
3977 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
3978 foldOpenCursor();
3979#endif
3980}
3981
3982/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003983 * Search for variable declaration of "ptr[len]".
3984 * When "locally" is TRUE in the current function ("gd"), otherwise in the
3985 * current file ("gD").
3986 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003987 * Return FAIL when not found.
3988 */
3989 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003990find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003991 char_u *ptr;
3992 int len;
3993 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003994 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003995 int searchflags; /* flags passed to searchit() */
3996{
3997 char_u *pat;
3998 pos_T old_pos;
3999 pos_T par_pos;
4000 pos_T found_pos;
4001 int t;
4002 int save_p_ws;
4003 int save_p_scs;
4004 int retval = OK;
4005 int incl;
4006
4007 if ((pat = alloc(len + 7)) == NULL)
4008 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004009
4010 /* Put "\V" before the pattern to avoid that the special meaning of "."
4011 * and "~" causes trouble. */
4012 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4013 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 old_pos = curwin->w_cursor;
4015 save_p_ws = p_ws;
4016 save_p_scs = p_scs;
4017 p_ws = FALSE; /* don't wrap around end of file now */
4018 p_scs = FALSE; /* don't switch ignorecase off now */
4019
4020 /*
4021 * With "gD" go to line 1.
4022 * With "gd" Search back for the start of the current function, then go
4023 * back until a blank line. If this fails go to line 1.
4024 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004025 if (!locally || !findpar(&incl, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
4027 setpcmark(); /* Set in findpar() otherwise */
4028 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004029 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 }
4031 else
4032 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004033 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4035 --curwin->w_cursor.lnum;
4036 }
4037 curwin->w_cursor.col = 0;
4038
4039 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004040 found_pos.lnum = 0;
4041 for (;;)
4042 {
4043 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4044 pat, 1L, searchflags, RE_LAST);
4045 if (curwin->w_cursor.lnum >= old_pos.lnum)
4046 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004047
4048 if (thisblock)
4049 {
4050 pos_T *pos;
4051
4052 /* Check that the block the match is in doesn't end before the
4053 * position where we started the search from. */
4054 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4055 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4056 && pos->lnum < old_pos.lnum)
4057 continue;
4058 }
4059
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004060 if (t == FAIL)
4061 {
4062 /* If we previously found a valid position, use it. */
4063 if (found_pos.lnum != 0)
4064 {
4065 curwin->w_cursor = found_pos;
4066 t = OK;
4067 }
4068 break;
4069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004071 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4072 {
4073 /* Ignore this line, continue at start of next line. */
4074 ++curwin->w_cursor.lnum;
4075 curwin->w_cursor.col = 0;
4076 continue;
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004079 if (!locally) /* global search: use first match found */
4080 break;
4081 if (curwin->w_cursor.lnum >= par_pos.lnum)
4082 {
4083 /* If we previously found a valid position, use it. */
4084 if (found_pos.lnum != 0)
4085 curwin->w_cursor = found_pos;
4086 break;
4087 }
4088
4089 /* For finding a local variable and the match is before the "{" search
4090 * to find a later match. For K&R style function declarations this
4091 * skips the function header without types. */
4092 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004094
4095 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004097 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 curwin->w_cursor = old_pos;
4099 }
4100 else
4101 {
4102 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 /* "n" searches forward now */
4104 reset_search_dir();
4105 }
4106
4107 vim_free(pat);
4108 p_ws = save_p_ws;
4109 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004110
4111 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112}
4113
4114/*
4115 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4116 * lines rather than lines in the file.
4117 * 'dist' must be positive.
4118 *
4119 * Return OK if able to move cursor, FAIL otherwise.
4120 */
4121 static int
4122nv_screengo(oap, dir, dist)
4123 oparg_T *oap;
4124 int dir;
4125 long dist;
4126{
4127 int linelen = linetabsize(ml_get_curline());
4128 int retval = OK;
4129 int atend = FALSE;
4130 int n;
4131 int col_off1; /* margin offset for first screen line */
4132 int col_off2; /* margin offset for wrapped screen line */
4133 int width1; /* text width for first screen line */
4134 int width2; /* test width for wrapped screen line */
4135
4136 oap->motion_type = MCHAR;
4137 oap->inclusive = FALSE;
4138
4139 col_off1 = curwin_col_off();
4140 col_off2 = col_off1 - curwin_col_off2();
4141 width1 = W_WIDTH(curwin) - col_off1;
4142 width2 = W_WIDTH(curwin) - col_off2;
4143
4144#ifdef FEAT_VERTSPLIT
4145 if (curwin->w_width != 0)
4146 {
4147#endif
4148 /*
4149 * Instead of sticking at the last character of the buffer line we
4150 * try to stick in the last column of the screen.
4151 */
4152 if (curwin->w_curswant == MAXCOL)
4153 {
4154 atend = TRUE;
4155 validate_virtcol();
4156 if (width1 <= 0)
4157 curwin->w_curswant = 0;
4158 else
4159 {
4160 curwin->w_curswant = width1 - 1;
4161 if (curwin->w_virtcol > curwin->w_curswant)
4162 curwin->w_curswant += ((curwin->w_virtcol
4163 - curwin->w_curswant - 1) / width2 + 1) * width2;
4164 }
4165 }
4166 else
4167 {
4168 if (linelen > width1)
4169 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4170 else
4171 n = width1;
4172 if (curwin->w_curswant > (colnr_T)n + 1)
4173 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4174 * width2;
4175 }
4176
4177 while (dist--)
4178 {
4179 if (dir == BACKWARD)
4180 {
4181 if ((long)curwin->w_curswant >= width2)
4182 /* move back within line */
4183 curwin->w_curswant -= width2;
4184 else
4185 {
4186 /* to previous line */
4187 if (curwin->w_cursor.lnum == 1)
4188 {
4189 retval = FAIL;
4190 break;
4191 }
4192 --curwin->w_cursor.lnum;
4193#ifdef FEAT_FOLDING
4194 /* Move to the start of a closed fold. Don't do that when
4195 * 'foldopen' contains "all": it will open in a moment. */
4196 if (!(fdo_flags & FDO_ALL))
4197 (void)hasFolding(curwin->w_cursor.lnum,
4198 &curwin->w_cursor.lnum, NULL);
4199#endif
4200 linelen = linetabsize(ml_get_curline());
4201 if (linelen > width1)
4202 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4203 + 1) * width2;
4204 }
4205 }
4206 else /* dir == FORWARD */
4207 {
4208 if (linelen > width1)
4209 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4210 else
4211 n = width1;
4212 if (curwin->w_curswant + width2 < (colnr_T)n)
4213 /* move forward within line */
4214 curwin->w_curswant += width2;
4215 else
4216 {
4217 /* to next line */
4218#ifdef FEAT_FOLDING
4219 /* Move to the end of a closed fold. */
4220 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4221 &curwin->w_cursor.lnum);
4222#endif
4223 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4224 {
4225 retval = FAIL;
4226 break;
4227 }
4228 curwin->w_cursor.lnum++;
4229 curwin->w_curswant %= width2;
4230 }
4231 }
4232 }
4233#ifdef FEAT_VERTSPLIT
4234 }
4235#endif
4236
4237 coladvance(curwin->w_curswant);
4238
4239#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4240 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4241 {
4242 /*
4243 * Check for landing on a character that got split at the end of the
4244 * last line. We want to advance a screenline, not end up in the same
4245 * screenline or move two screenlines.
4246 */
4247 validate_virtcol();
4248 if (curwin->w_virtcol > curwin->w_curswant
4249 && (curwin->w_curswant < (colnr_T)width1
4250 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4251 : ((curwin->w_curswant - width1) % width2
4252 > (colnr_T)width2 / 2)))
4253 --curwin->w_cursor.col;
4254 }
4255#endif
4256
4257 if (atend)
4258 curwin->w_curswant = MAXCOL; /* stick in the last column */
4259
4260 return retval;
4261}
4262
4263#ifdef FEAT_MOUSE
4264/*
4265 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4266 * when Shift or Ctrl is used.
4267 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4268 */
4269 static void
4270nv_mousescroll(cap)
4271 cmdarg_T *cap;
4272{
4273# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004274 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
4276 /* Currently we only get the mouse coordinates in the GUI. */
4277 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4278 {
4279 int row, col;
4280
4281 row = mouse_row;
4282 col = mouse_col;
4283
4284 /* find the window at the pointer coordinates */
4285 curwin = mouse_find_win(&row, &col);
4286 curbuf = curwin->w_buffer;
4287 }
4288# endif
4289
4290 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4291 {
4292 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4293 }
4294 else
4295 {
4296 cap->count1 = 3;
4297 cap->count0 = 3;
4298 nv_scroll_line(cap);
4299 }
4300
4301# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4302 curwin->w_redr_status = TRUE;
4303
4304 curwin = old_curwin;
4305 curbuf = curwin->w_buffer;
4306# endif
4307}
4308
4309/*
4310 * Mouse clicks and drags.
4311 */
4312 static void
4313nv_mouse(cap)
4314 cmdarg_T *cap;
4315{
4316 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4317}
4318#endif
4319
4320/*
4321 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4322 * cap->arg must be TRUE for CTRL-E.
4323 */
4324 static void
4325nv_scroll_line(cap)
4326 cmdarg_T *cap;
4327{
4328 if (!checkclearop(cap->oap))
4329 scroll_redraw(cap->arg, cap->count1);
4330}
4331
4332/*
4333 * Scroll "count" lines up or down, and redraw.
4334 */
4335 void
4336scroll_redraw(up, count)
4337 int up;
4338 long count;
4339{
4340 linenr_T prev_topline = curwin->w_topline;
4341#ifdef FEAT_DIFF
4342 int prev_topfill = curwin->w_topfill;
4343#endif
4344 linenr_T prev_lnum = curwin->w_cursor.lnum;
4345
4346 if (up)
4347 scrollup(count, TRUE);
4348 else
4349 scrolldown(count, TRUE);
4350 if (p_so)
4351 {
4352 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4353 * valid, otherwise the screen jumps back at the end of the file. */
4354 cursor_correct();
4355 check_cursor_moved(curwin);
4356 curwin->w_valid |= VALID_TOPLINE;
4357
4358 /* If moved back to where we were, at least move the cursor, otherwise
4359 * we get stuck at one position. Don't move the cursor up if the
4360 * first line of the buffer is already on the screen */
4361 while (curwin->w_topline == prev_topline
4362#ifdef FEAT_DIFF
4363 && curwin->w_topfill == prev_topfill
4364#endif
4365 )
4366 {
4367 if (up)
4368 {
4369 if (curwin->w_cursor.lnum > prev_lnum
4370 || cursor_down(1L, FALSE) == FAIL)
4371 break;
4372 }
4373 else
4374 {
4375 if (curwin->w_cursor.lnum < prev_lnum
4376 || prev_topline == 1L
4377 || cursor_up(1L, FALSE) == FAIL)
4378 break;
4379 }
4380 /* Mark w_topline as valid, otherwise the screen jumps back at the
4381 * end of the file. */
4382 check_cursor_moved(curwin);
4383 curwin->w_valid |= VALID_TOPLINE;
4384 }
4385 }
4386 if (curwin->w_cursor.lnum != prev_lnum)
4387 coladvance(curwin->w_curswant);
4388 redraw_later(VALID);
4389}
4390
4391/*
4392 * Commands that start with "z".
4393 */
4394 static void
4395nv_zet(cap)
4396 cmdarg_T *cap;
4397{
4398 long n;
4399 colnr_T col;
4400 int nchar = cap->nchar;
4401#ifdef FEAT_FOLDING
4402 long old_fdl = curwin->w_p_fdl;
4403 int old_fen = curwin->w_p_fen;
4404#endif
4405
4406 if (VIM_ISDIGIT(nchar))
4407 {
4408 /*
4409 * "z123{nchar}": edit the count before obtaining {nchar}
4410 */
4411 if (checkclearop(cap->oap))
4412 return;
4413 n = nchar - '0';
4414 for (;;)
4415 {
4416#ifdef USE_ON_FLY_SCROLL
4417 dont_scroll = TRUE; /* disallow scrolling here */
4418#endif
4419 ++no_mapping;
4420 ++allow_keys; /* no mapping for nchar, but allow key codes */
4421 nchar = safe_vgetc();
4422#ifdef FEAT_LANGMAP
4423 LANGMAP_ADJUST(nchar, TRUE);
4424#endif
4425 --no_mapping;
4426 --allow_keys;
4427#ifdef FEAT_CMDL_INFO
4428 (void)add_to_showcmd(nchar);
4429#endif
4430 if (nchar == K_DEL || nchar == K_KDEL)
4431 n /= 10;
4432 else if (VIM_ISDIGIT(nchar))
4433 n = n * 10 + (nchar - '0');
4434 else if (nchar == CAR)
4435 {
4436#ifdef FEAT_GUI
4437 need_mouse_correct = TRUE;
4438#endif
4439 win_setheight((int)n);
4440 break;
4441 }
4442 else if (nchar == 'l'
4443 || nchar == 'h'
4444 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004445 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 cap->count1 = n ? n * cap->count1 : cap->count1;
4448 goto dozet;
4449 }
4450 else
4451 {
4452 clearopbeep(cap->oap);
4453 break;
4454 }
4455 }
4456 cap->oap->op_type = OP_NOP;
4457 return;
4458 }
4459
4460dozet:
4461 if (
4462#ifdef FEAT_FOLDING
4463 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4464 * and "zC" only in Visual mode. "zj" and "zk" are motion
4465 * commands. */
4466 cap->nchar != 'f' && cap->nchar != 'F'
4467 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4468 && cap->nchar != 'j' && cap->nchar != 'k'
4469 &&
4470#endif
4471 checkclearop(cap->oap))
4472 return;
4473
4474 /*
4475 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4476 * If line number given, set cursor.
4477 */
4478 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4479 && cap->count0
4480 && cap->count0 != curwin->w_cursor.lnum)
4481 {
4482 setpcmark();
4483 if (cap->count0 > curbuf->b_ml.ml_line_count)
4484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4485 else
4486 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004487 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489
4490 switch (nchar)
4491 {
4492 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4493 case '+':
4494 if (cap->count0 == 0)
4495 {
4496 /* No count given: put cursor at the line below screen */
4497 validate_botline(); /* make sure w_botline is valid */
4498 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4499 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4500 else
4501 curwin->w_cursor.lnum = curwin->w_botline;
4502 }
4503 /* FALLTHROUGH */
4504 case NL:
4505 case CAR:
4506 case K_KENTER:
4507 beginline(BL_WHITE | BL_FIX);
4508 /* FALLTHROUGH */
4509
4510 case 't': scroll_cursor_top(0, TRUE);
4511 redraw_later(VALID);
4512 break;
4513
4514 /* "z." and "zz": put cursor in middle of screen */
4515 case '.': beginline(BL_WHITE | BL_FIX);
4516 /* FALLTHROUGH */
4517
4518 case 'z': scroll_cursor_halfway(TRUE);
4519 redraw_later(VALID);
4520 break;
4521
4522 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4523 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4524 * when <count> is at bottom of window, and puts that one at
4525 * bottom of window. */
4526 if (cap->count0 != 0)
4527 {
4528 scroll_cursor_bot(0, TRUE);
4529 curwin->w_cursor.lnum = curwin->w_topline;
4530 }
4531 else if (curwin->w_topline == 1)
4532 curwin->w_cursor.lnum = 1;
4533 else
4534 curwin->w_cursor.lnum = curwin->w_topline - 1;
4535 /* FALLTHROUGH */
4536 case '-':
4537 beginline(BL_WHITE | BL_FIX);
4538 /* FALLTHROUGH */
4539
4540 case 'b': scroll_cursor_bot(0, TRUE);
4541 redraw_later(VALID);
4542 break;
4543
4544 /* "zH" - scroll screen right half-page */
4545 case 'H':
4546 cap->count1 *= W_WIDTH(curwin) / 2;
4547 /* FALLTHROUGH */
4548
4549 /* "zh" - scroll screen to the right */
4550 case 'h':
4551 case K_LEFT:
4552 if (!curwin->w_p_wrap)
4553 {
4554 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4555 curwin->w_leftcol = 0;
4556 else
4557 curwin->w_leftcol -= (colnr_T)cap->count1;
4558 leftcol_changed();
4559 }
4560 break;
4561
4562 /* "zL" - scroll screen left half-page */
4563 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4564 /* FALLTHROUGH */
4565
4566 /* "zl" - scroll screen to the left */
4567 case 'l':
4568 case K_RIGHT:
4569 if (!curwin->w_p_wrap)
4570 {
4571 /* scroll the window left */
4572 curwin->w_leftcol += (colnr_T)cap->count1;
4573 leftcol_changed();
4574 }
4575 break;
4576
4577 /* "zs" - scroll screen, cursor at the start */
4578 case 's': if (!curwin->w_p_wrap)
4579 {
4580#ifdef FEAT_FOLDING
4581 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4582 col = 0; /* like the cursor is in col 0 */
4583 else
4584#endif
4585 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4586 if ((long)col > p_siso)
4587 col -= p_siso;
4588 else
4589 col = 0;
4590 if (curwin->w_leftcol != col)
4591 {
4592 curwin->w_leftcol = col;
4593 redraw_later(NOT_VALID);
4594 }
4595 }
4596 break;
4597
4598 /* "ze" - scroll screen, cursor at the end */
4599 case 'e': if (!curwin->w_p_wrap)
4600 {
4601#ifdef FEAT_FOLDING
4602 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4603 col = 0; /* like the cursor is in col 0 */
4604 else
4605#endif
4606 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4607 n = W_WIDTH(curwin) - curwin_col_off();
4608 if ((long)col + p_siso < n)
4609 col = 0;
4610 else
4611 col = col + p_siso - n + 1;
4612 if (curwin->w_leftcol != col)
4613 {
4614 curwin->w_leftcol = col;
4615 redraw_later(NOT_VALID);
4616 }
4617 }
4618 break;
4619
4620#ifdef FEAT_FOLDING
4621 /* "zF": create fold command */
4622 /* "zf": create fold operator */
4623 case 'F':
4624 case 'f': if (foldManualAllowed(TRUE))
4625 {
4626 cap->nchar = 'f';
4627 nv_operator(cap);
4628 curwin->w_p_fen = TRUE;
4629
4630 /* "zF" is like "zfzf" */
4631 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4632 {
4633 nv_operator(cap);
4634 finish_op = TRUE;
4635 }
4636 }
4637 else
4638 clearopbeep(cap->oap);
4639 break;
4640
4641 /* "zd": delete fold at cursor */
4642 /* "zD": delete fold at cursor recursively */
4643 case 'd':
4644 case 'D': if (foldManualAllowed(FALSE))
4645 {
4646 if (VIsual_active)
4647 nv_operator(cap);
4648 else
4649 deleteFold(curwin->w_cursor.lnum,
4650 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4651 }
4652 break;
4653
4654 /* "zE": erease all folds */
4655 case 'E': if (foldmethodIsManual(curwin))
4656 {
4657 clearFolding(curwin);
4658 changed_window_setting();
4659 }
4660 else if (foldmethodIsMarker(curwin))
4661 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4662 TRUE, FALSE);
4663 else
4664 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4665 break;
4666
4667 /* "zn": fold none: reset 'foldenable' */
4668 case 'n': curwin->w_p_fen = FALSE;
4669 break;
4670
4671 /* "zN": fold Normal: set 'foldenable' */
4672 case 'N': curwin->w_p_fen = TRUE;
4673 break;
4674
4675 /* "zi": invert folding: toggle 'foldenable' */
4676 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4677 break;
4678
4679 /* "za": open closed fold or close open fold at cursor */
4680 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4681 openFold(curwin->w_cursor.lnum, cap->count1);
4682 else
4683 {
4684 closeFold(curwin->w_cursor.lnum, cap->count1);
4685 curwin->w_p_fen = TRUE;
4686 }
4687 break;
4688
4689 /* "zA": open fold at cursor recursively */
4690 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4691 openFoldRecurse(curwin->w_cursor.lnum);
4692 else
4693 {
4694 closeFoldRecurse(curwin->w_cursor.lnum);
4695 curwin->w_p_fen = TRUE;
4696 }
4697 break;
4698
4699 /* "zo": open fold at cursor or Visual area */
4700 case 'o': if (VIsual_active)
4701 nv_operator(cap);
4702 else
4703 openFold(curwin->w_cursor.lnum, cap->count1);
4704 break;
4705
4706 /* "zO": open fold recursively */
4707 case 'O': if (VIsual_active)
4708 nv_operator(cap);
4709 else
4710 openFoldRecurse(curwin->w_cursor.lnum);
4711 break;
4712
4713 /* "zc": close fold at cursor or Visual area */
4714 case 'c': if (VIsual_active)
4715 nv_operator(cap);
4716 else
4717 closeFold(curwin->w_cursor.lnum, cap->count1);
4718 curwin->w_p_fen = TRUE;
4719 break;
4720
4721 /* "zC": close fold recursively */
4722 case 'C': if (VIsual_active)
4723 nv_operator(cap);
4724 else
4725 closeFoldRecurse(curwin->w_cursor.lnum);
4726 curwin->w_p_fen = TRUE;
4727 break;
4728
4729 /* "zv": open folds at the cursor */
4730 case 'v': foldOpenCursor();
4731 break;
4732
4733 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4734 case 'x': curwin->w_p_fen = TRUE;
4735 newFoldLevel(); /* update right now */
4736 foldOpenCursor();
4737 break;
4738
4739 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4740 case 'X': curwin->w_p_fen = TRUE;
4741 old_fdl = -1; /* force an update */
4742 break;
4743
4744 /* "zm": fold more */
4745 case 'm': if (curwin->w_p_fdl > 0)
4746 --curwin->w_p_fdl;
4747 old_fdl = -1; /* force an update */
4748 curwin->w_p_fen = TRUE;
4749 break;
4750
4751 /* "zM": close all folds */
4752 case 'M': curwin->w_p_fdl = 0;
4753 old_fdl = -1; /* force an update */
4754 curwin->w_p_fen = TRUE;
4755 break;
4756
4757 /* "zr": reduce folding */
4758 case 'r': ++curwin->w_p_fdl;
4759 break;
4760
4761 /* "zR": open all folds */
4762 case 'R': curwin->w_p_fdl = getDeepestNesting();
4763 old_fdl = -1; /* force an update */
4764 break;
4765
4766 case 'j': /* "zj" move to next fold downwards */
4767 case 'k': /* "zk" move to next fold upwards */
4768 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4769 cap->count1) == FAIL)
4770 clearopbeep(cap->oap);
4771 break;
4772
4773#endif /* FEAT_FOLDING */
4774
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775#ifdef FEAT_SYN_HL
4776 case 'g': /* "zg": add good word to word list */
4777 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00004778 case 'G': /* "zG": add good word to temp word list */
4779 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004780 {
4781 char_u *ptr = NULL;
4782 int len;
4783
4784 if (checkclearop(cap->oap))
4785 break;
4786# ifdef FEAT_VISUAL
4787 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4788 == FAIL)
4789 return;
4790# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004791 if (ptr == NULL)
4792 {
4793 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004794
4795 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004796 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004797 if (len != 0 && curwin->w_cursor.col <= pos.col)
4798 ptr = ml_get_pos(&curwin->w_cursor);
4799 curwin->w_cursor = pos;
4800 }
4801
Bram Moolenaarb765d632005-06-07 21:00:02 +00004802 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4803 FIND_IDENT)) == 0)
4804 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004805 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004806 (nchar == 'G' || nchar == 'W') ? 0
4807 : (int)cap->count1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004808 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004809 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810
Bram Moolenaar43abc522005-12-10 20:15:02 +00004811 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 if (!checkclearopq(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004813 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004815#endif
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 default: clearopbeep(cap->oap);
4818 }
4819
4820#ifdef FEAT_FOLDING
4821 /* Redraw when 'foldenable' changed */
4822 if (old_fen != curwin->w_p_fen)
4823 {
4824# ifdef FEAT_DIFF
4825 win_T *wp;
4826
4827 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4828 {
4829 /* Adjust 'foldenable' in diff-synced windows. */
4830 FOR_ALL_WINDOWS(wp)
4831 {
4832 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4833 {
4834 wp->w_p_fen = curwin->w_p_fen;
4835 changed_window_setting_win(wp);
4836 }
4837 }
4838 }
4839# endif
4840 changed_window_setting();
4841 }
4842
4843 /* Redraw when 'foldlevel' changed. */
4844 if (old_fdl != curwin->w_p_fdl)
4845 newFoldLevel();
4846#endif
4847}
4848
4849#ifdef FEAT_GUI
4850/*
4851 * Vertical scrollbar movement.
4852 */
4853 static void
4854nv_ver_scrollbar(cap)
4855 cmdarg_T *cap;
4856{
4857 if (cap->oap->op_type != OP_NOP)
4858 clearopbeep(cap->oap);
4859
4860 /* Even if an operator was pending, we still want to scroll */
4861 gui_do_scroll();
4862}
4863
4864/*
4865 * Horizontal scrollbar movement.
4866 */
4867 static void
4868nv_hor_scrollbar(cap)
4869 cmdarg_T *cap;
4870{
4871 if (cap->oap->op_type != OP_NOP)
4872 clearopbeep(cap->oap);
4873
4874 /* Even if an operator was pending, we still want to scroll */
4875 gui_do_horiz_scroll();
4876}
4877#endif
4878
4879/*
4880 * "Q" command.
4881 */
4882 static void
4883nv_exmode(cap)
4884 cmdarg_T *cap;
4885{
4886 /*
4887 * Ignore 'Q' in Visual mode, just give a beep.
4888 */
4889#ifdef FEAT_VISUAL
4890 if (VIsual_active)
4891 vim_beep();
4892 else
4893#endif
4894 if (!checkclearop(cap->oap))
4895 do_exmode(FALSE);
4896}
4897
4898/*
4899 * Handle a ":" command.
4900 */
4901 static void
4902nv_colon(cap)
4903 cmdarg_T *cap;
4904{
4905 int old_p_im;
4906
4907#ifdef FEAT_VISUAL
4908 if (VIsual_active)
4909 nv_operator(cap);
4910 else
4911#endif
4912 {
4913 if (cap->oap->op_type != OP_NOP)
4914 {
4915 /* Using ":" as a movement is characterwise exclusive. */
4916 cap->oap->motion_type = MCHAR;
4917 cap->oap->inclusive = FALSE;
4918 }
4919 else if (cap->count0)
4920 {
4921 /* translate "count:" into ":.,.+(count - 1)" */
4922 stuffcharReadbuff('.');
4923 if (cap->count0 > 1)
4924 {
4925 stuffReadbuff((char_u *)",.+");
4926 stuffnumReadbuff((long)cap->count0 - 1L);
4927 }
4928 }
4929
4930 /* When typing, don't type below an old message */
4931 if (KeyTyped)
4932 compute_cmdrow();
4933
4934 old_p_im = p_im;
4935
4936 /* get a command line and execute it */
4937 do_cmdline(NULL, getexline, NULL,
4938 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
4939
4940 /* If 'insertmode' changed, enter or exit Insert mode */
4941 if (p_im != old_p_im)
4942 {
4943 if (p_im)
4944 restart_edit = 'i';
4945 else
4946 restart_edit = 0;
4947 }
4948
4949 /* The start of the operator may have become invalid by the Ex
4950 * command. */
4951 if (cap->oap->op_type != OP_NOP
4952 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
4953 || cap->oap->start.col >
4954 STRLEN(ml_get(cap->oap->start.lnum))))
4955 clearopbeep(cap->oap);
4956 }
4957}
4958
4959/*
4960 * Handle CTRL-G command.
4961 */
4962 static void
4963nv_ctrlg(cap)
4964 cmdarg_T *cap;
4965{
4966#ifdef FEAT_VISUAL
4967 if (VIsual_active) /* toggle Selection/Visual mode */
4968 {
4969 VIsual_select = !VIsual_select;
4970 showmode();
4971 }
4972 else
4973#endif
4974 if (!checkclearop(cap->oap))
4975 /* print full name if count given or :cd used */
4976 fileinfo((int)cap->count0, FALSE, TRUE);
4977}
4978
4979/*
4980 * Handle CTRL-H <Backspace> command.
4981 */
4982 static void
4983nv_ctrlh(cap)
4984 cmdarg_T *cap;
4985{
4986#ifdef FEAT_VISUAL
4987 if (VIsual_active && VIsual_select)
4988 {
4989 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
4990 v_visop(cap);
4991 }
4992 else
4993#endif
4994 nv_left(cap);
4995}
4996
4997/*
4998 * CTRL-L: clear screen and redraw.
4999 */
5000 static void
5001nv_clear(cap)
5002 cmdarg_T *cap;
5003{
5004 if (!checkclearop(cap->oap))
5005 {
5006#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5007 /*
5008 * Right now, the BeBox doesn't seem to have an easy way to detect
5009 * window resizing, so we cheat and make the user detect it
5010 * manually with CTRL-L instead
5011 */
5012 ui_get_shellsize();
5013#endif
5014#ifdef FEAT_SYN_HL
5015 /* Clear all syntax states to force resyncing. */
5016 syn_stack_free_all(curbuf);
5017#endif
5018 redraw_later(CLEAR);
5019 }
5020}
5021
5022/*
5023 * CTRL-O: In Select mode: switch to Visual mode for one command.
5024 * Otherwise: Go to older pcmark.
5025 */
5026 static void
5027nv_ctrlo(cap)
5028 cmdarg_T *cap;
5029{
5030#ifdef FEAT_VISUAL
5031 if (VIsual_active && VIsual_select)
5032 {
5033 VIsual_select = FALSE;
5034 showmode();
5035 restart_VIsual_select = 2; /* restart Select mode later */
5036 }
5037 else
5038#endif
5039 {
5040 cap->count1 = -cap->count1;
5041 nv_pcmark(cap);
5042 }
5043}
5044
5045/*
5046 * CTRL-^ command, short for ":e #"
5047 */
5048 static void
5049nv_hat(cap)
5050 cmdarg_T *cap;
5051{
5052 if (!checkclearopq(cap->oap))
5053 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5054 GETF_SETMARK|GETF_ALT, FALSE);
5055}
5056
5057/*
5058 * "Z" commands.
5059 */
5060 static void
5061nv_Zet(cap)
5062 cmdarg_T *cap;
5063{
5064 if (!checkclearopq(cap->oap))
5065 {
5066 switch (cap->nchar)
5067 {
5068 /* "ZZ": equivalent to ":x". */
5069 case 'Z': do_cmdline_cmd((char_u *)"x");
5070 break;
5071
5072 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5073 case 'Q': do_cmdline_cmd((char_u *)"q!");
5074 break;
5075
5076 default: clearopbeep(cap->oap);
5077 }
5078 }
5079}
5080
5081#if defined(FEAT_WINDOWS) || defined(PROTO)
5082/*
5083 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5084 */
5085 void
5086do_nv_ident(c1, c2)
5087 int c1;
5088 int c2;
5089{
5090 oparg_T oa;
5091 cmdarg_T ca;
5092
5093 clear_oparg(&oa);
5094 vim_memset(&ca, 0, sizeof(ca));
5095 ca.oap = &oa;
5096 ca.cmdchar = c1;
5097 ca.nchar = c2;
5098 nv_ident(&ca);
5099}
5100#endif
5101
5102/*
5103 * Handle the commands that use the word under the cursor.
5104 * [g] CTRL-] :ta to current identifier
5105 * [g] 'K' run program for current identifier
5106 * [g] '*' / to current identifier or string
5107 * [g] '#' ? to current identifier or string
5108 * g ']' :tselect for current identifier
5109 */
5110 static void
5111nv_ident(cap)
5112 cmdarg_T *cap;
5113{
5114 char_u *ptr = NULL;
5115 char_u *buf;
5116 char_u *p;
5117 char_u *kp; /* value of 'keywordprg' */
5118 int kp_help; /* 'keywordprg' is ":help" */
5119 int n = 0; /* init for GCC */
5120 int cmdchar;
5121 int g_cmd; /* "g" command */
5122 char_u *aux_ptr;
5123 int isman;
5124 int isman_s;
5125
5126 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5127 {
5128 cmdchar = cap->nchar;
5129 g_cmd = TRUE;
5130 }
5131 else
5132 {
5133 cmdchar = cap->cmdchar;
5134 g_cmd = FALSE;
5135 }
5136
5137 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5138 cmdchar = '#';
5139
5140 /*
5141 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5142 */
5143 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5144 {
5145#ifdef FEAT_VISUAL
5146 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5147 return;
5148#endif
5149 if (checkclearopq(cap->oap))
5150 return;
5151 }
5152
5153 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5154 (cmdchar == '*' || cmdchar == '#')
5155 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5156 {
5157 clearop(cap->oap);
5158 return;
5159 }
5160
5161 /* Allocate buffer to put the command in. Inserting backslashes can
5162 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5163 * and some numbers. */
5164 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5165 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5166 || STRCMP(kp, ":help") == 0);
5167 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5168 if (buf == NULL)
5169 return;
5170 buf[0] = NUL;
5171
5172 switch (cmdchar)
5173 {
5174 case '*':
5175 case '#':
5176 /*
5177 * Put cursor at start of word, makes search skip the word
5178 * under the cursor.
5179 * Call setpcmark() first, so "*``" puts the cursor back where
5180 * it was.
5181 */
5182 setpcmark();
5183 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5184
5185 if (!g_cmd && vim_iswordp(ptr))
5186 STRCPY(buf, "\\<");
5187 no_smartcase = TRUE; /* don't use 'smartcase' now */
5188 break;
5189
5190 case 'K':
5191 if (kp_help)
5192 STRCPY(buf, "he! ");
5193 else
5194 {
5195 /* When a count is given, turn it into a range. Is this
5196 * really what we want? */
5197 isman = (STRCMP(kp, "man") == 0);
5198 isman_s = (STRCMP(kp, "man -s") == 0);
5199 if (cap->count0 != 0 && !(isman || isman_s))
5200 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5201
5202 STRCAT(buf, "! ");
5203 if (cap->count0 == 0 && isman_s)
5204 STRCAT(buf, "man");
5205 else
5206 STRCAT(buf, kp);
5207 STRCAT(buf, " ");
5208 if (cap->count0 != 0 && (isman || isman_s))
5209 {
5210 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5211 STRCAT(buf, " ");
5212 }
5213 }
5214 break;
5215
5216 case ']':
5217#ifdef FEAT_CSCOPE
5218 if (p_cst)
5219 STRCPY(buf, "cstag ");
5220 else
5221#endif
5222 STRCPY(buf, "ts ");
5223 break;
5224
5225 default:
5226 if (curbuf->b_help)
5227 STRCPY(buf, "he! ");
5228 else if (g_cmd)
5229 STRCPY(buf, "tj ");
5230 else
5231 STRCPY(buf, "ta ");
5232 }
5233
5234 /*
5235 * Now grab the chars in the identifier
5236 */
5237 if (cmdchar == '*')
5238 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5239 else if (cmdchar == '#')
5240 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5241 else if (cmdchar == 'K' && !kp_help)
5242 aux_ptr = (char_u *)" \t\\\"|!";
5243 else
5244 /* Don't escape spaces and Tabs in a tag with a backslash */
5245 aux_ptr = (char_u *)"\\|\"";
5246
5247 p = buf + STRLEN(buf);
5248 while (n-- > 0)
5249 {
5250 /* put a backslash before \ and some others */
5251 if (vim_strchr(aux_ptr, *ptr) != NULL)
5252 *p++ = '\\';
5253#ifdef FEAT_MBYTE
5254 /* When current byte is a part of multibyte character, copy all bytes
5255 * of that character. */
5256 if (has_mbyte)
5257 {
5258 int i;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005259 int len = (*mb_ptr2len)(ptr) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
5261 for (i = 0; i < len && n >= 1; ++i, --n)
5262 *p++ = *ptr++;
5263 }
5264#endif
5265 *p++ = *ptr++;
5266 }
5267 *p = NUL;
5268
5269 /*
5270 * Execute the command.
5271 */
5272 if (cmdchar == '*' || cmdchar == '#')
5273 {
5274 if (!g_cmd && (
5275#ifdef FEAT_MBYTE
5276 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5277#endif
5278 vim_iswordc(ptr[-1])))
5279 STRCAT(buf, "\\>");
5280#ifdef FEAT_CMDHIST
5281 /* put pattern in search history */
5282 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5283#endif
5284 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5285 }
5286 else
5287 do_cmdline_cmd(buf);
5288
5289 vim_free(buf);
5290}
5291
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005292#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293/*
5294 * Get visually selected text, within one line only.
5295 * Returns FAIL if more than one line selected.
5296 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005297 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298get_visual_text(cap, pp, lenp)
5299 cmdarg_T *cap;
5300 char_u **pp; /* return: start of selected text */
5301 int *lenp; /* return: length of selected text */
5302{
5303 if (VIsual_mode != 'V')
5304 unadjust_for_sel();
5305 if (VIsual.lnum != curwin->w_cursor.lnum)
5306 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005307 if (cap != NULL)
5308 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return FAIL;
5310 }
5311 if (VIsual_mode == 'V')
5312 {
5313 *pp = ml_get_curline();
5314 *lenp = (int)STRLEN(*pp);
5315 }
5316 else
5317 {
5318 if (lt(curwin->w_cursor, VIsual))
5319 {
5320 *pp = ml_get_pos(&curwin->w_cursor);
5321 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5322 }
5323 else
5324 {
5325 *pp = ml_get_pos(&VIsual);
5326 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5327 }
5328#ifdef FEAT_MBYTE
5329 if (has_mbyte)
5330 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005331 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332#endif
5333 }
5334 reset_VIsual_and_resel();
5335 return OK;
5336}
5337#endif
5338
5339/*
5340 * CTRL-T: backwards in tag stack
5341 */
5342 static void
5343nv_tagpop(cap)
5344 cmdarg_T *cap;
5345{
5346 if (!checkclearopq(cap->oap))
5347 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5348}
5349
5350/*
5351 * Handle scrolling command 'H', 'L' and 'M'.
5352 */
5353 static void
5354nv_scroll(cap)
5355 cmdarg_T *cap;
5356{
5357 int used = 0;
5358 long n;
5359#ifdef FEAT_FOLDING
5360 linenr_T lnum;
5361#endif
5362 int half;
5363
5364 cap->oap->motion_type = MLINE;
5365 setpcmark();
5366
5367 if (cap->cmdchar == 'L')
5368 {
5369 validate_botline(); /* make sure curwin->w_botline is valid */
5370 curwin->w_cursor.lnum = curwin->w_botline - 1;
5371 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5372 curwin->w_cursor.lnum = 1;
5373 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005374 {
5375#ifdef FEAT_FOLDING
5376 if (hasAnyFolding(curwin))
5377 {
5378 /* Count a fold for one screen line. */
5379 for (n = cap->count1 - 1; n > 0
5380 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5381 {
5382 (void)hasFolding(curwin->w_cursor.lnum,
5383 &curwin->w_cursor.lnum, NULL);
5384 --curwin->w_cursor.lnum;
5385 }
5386 }
5387 else
5388#endif
5389 curwin->w_cursor.lnum -= cap->count1 - 1;
5390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
5392 else
5393 {
5394 if (cap->cmdchar == 'M')
5395 {
5396#ifdef FEAT_DIFF
5397 /* Don't count filler lines above the window. */
5398 used -= diff_check_fill(curwin, curwin->w_topline)
5399 - curwin->w_topfill;
5400#endif
5401 validate_botline(); /* make sure w_empty_rows is valid */
5402 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5403 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5404 {
5405#ifdef FEAT_DIFF
5406 /* Count half he number of filler lines to be "below this
5407 * line" and half to be "above the next line". */
5408 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5409 + n) / 2 >= half)
5410 {
5411 --n;
5412 break;
5413 }
5414#endif
5415 used += plines(curwin->w_topline + n);
5416 if (used >= half)
5417 break;
5418#ifdef FEAT_FOLDING
5419 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5420 n = lnum - curwin->w_topline;
5421#endif
5422 }
5423 if (n > 0 && used > curwin->w_height)
5424 --n;
5425 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005426 else /* (cap->cmdchar == 'H') */
5427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005429#ifdef FEAT_FOLDING
5430 if (hasAnyFolding(curwin))
5431 {
5432 /* Count a fold for one screen line. */
5433 lnum = curwin->w_topline;
5434 while (n-- > 0 && lnum < curwin->w_botline - 1)
5435 {
5436 hasFolding(lnum, NULL, &lnum);
5437 ++lnum;
5438 }
5439 n = lnum - curwin->w_topline;
5440 }
5441#endif
5442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 curwin->w_cursor.lnum = curwin->w_topline + n;
5444 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5445 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5446 }
5447
5448 cursor_correct(); /* correct for 'so' */
5449 beginline(BL_SOL | BL_FIX);
5450}
5451
5452/*
5453 * Cursor right commands.
5454 */
5455 static void
5456nv_right(cap)
5457 cmdarg_T *cap;
5458{
5459 long n;
5460#ifdef FEAT_VISUAL
5461 int PAST_LINE;
5462#else
5463# define PAST_LINE 0
5464#endif
5465
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005466 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5467 {
5468 /* <C-Right> and <S-Right> move a word or WORD right */
5469 if (mod_mask & MOD_MASK_CTRL)
5470 cap->arg = TRUE;
5471 nv_wordcmd(cap);
5472 return;
5473 }
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 cap->oap->motion_type = MCHAR;
5476 cap->oap->inclusive = FALSE;
5477#ifdef FEAT_VISUAL
5478 PAST_LINE = (VIsual_active && *p_sel != 'o');
5479
5480# ifdef FEAT_VIRTUALEDIT
5481 /*
5482 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5483 * (theoretically) infinitly long.
5484 */
5485 if (virtual_active())
5486 PAST_LINE = 0;
5487# endif
5488#endif
5489
5490 for (n = cap->count1; n > 0; --n)
5491 {
5492 if ((!PAST_LINE && oneright() == FAIL)
5493 || (PAST_LINE && *ml_get_cursor() == NUL))
5494 {
5495 /*
5496 * <Space> wraps to next line if 'whichwrap' bit 1 set.
5497 * 'l' wraps to next line if 'whichwrap' bit 2 set.
5498 * CURS_RIGHT wraps to next line if 'whichwrap' bit 3 set
5499 */
5500 if ( ((cap->cmdchar == ' '
5501 && vim_strchr(p_ww, 's') != NULL)
5502 || (cap->cmdchar == 'l'
5503 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005504 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 && vim_strchr(p_ww, '>') != NULL))
5506 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5507 {
5508 /* When deleting we also count the NL as a character.
5509 * Set cap->oap->inclusive when last char in the line is
5510 * included, move to next line after that */
5511 if ( (cap->oap->op_type == OP_DELETE
5512 || cap->oap->op_type == OP_CHANGE)
5513 && !cap->oap->inclusive
5514 && !lineempty(curwin->w_cursor.lnum))
5515 cap->oap->inclusive = TRUE;
5516 else
5517 {
5518 ++curwin->w_cursor.lnum;
5519 curwin->w_cursor.col = 0;
5520#ifdef FEAT_VIRTUALEDIT
5521 curwin->w_cursor.coladd = 0;
5522#endif
5523 curwin->w_set_curswant = TRUE;
5524 cap->oap->inclusive = FALSE;
5525 }
5526 continue;
5527 }
5528 if (cap->oap->op_type == OP_NOP)
5529 {
5530 /* Only beep and flush if not moved at all */
5531 if (n == cap->count1)
5532 beep_flush();
5533 }
5534 else
5535 {
5536 if (!lineempty(curwin->w_cursor.lnum))
5537 cap->oap->inclusive = TRUE;
5538 }
5539 break;
5540 }
5541#ifdef FEAT_VISUAL
5542 else if (PAST_LINE)
5543 {
5544 curwin->w_set_curswant = TRUE;
5545# ifdef FEAT_VIRTUALEDIT
5546 if (virtual_active())
5547 oneright();
5548 else
5549# endif
5550 {
5551# ifdef FEAT_MBYTE
5552 if (has_mbyte)
5553 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 else
5556# endif
5557 ++curwin->w_cursor.col;
5558 }
5559 }
5560#endif
5561 }
5562#ifdef FEAT_FOLDING
5563 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5564 && cap->oap->op_type == OP_NOP)
5565 foldOpenCursor();
5566#endif
5567}
5568
5569/*
5570 * Cursor left commands.
5571 *
5572 * Returns TRUE when operator end should not be adjusted.
5573 */
5574 static void
5575nv_left(cap)
5576 cmdarg_T *cap;
5577{
5578 long n;
5579
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005580 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5581 {
5582 /* <C-Left> and <S-Left> move a word or WORD left */
5583 if (mod_mask & MOD_MASK_CTRL)
5584 cap->arg = 1;
5585 nv_bck_word(cap);
5586 return;
5587 }
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 cap->oap->motion_type = MCHAR;
5590 cap->oap->inclusive = FALSE;
5591 for (n = cap->count1; n > 0; --n)
5592 {
5593 if (oneleft() == FAIL)
5594 {
5595 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5596 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5597 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5598 */
5599 if ( (((cap->cmdchar == K_BS
5600 || cap->cmdchar == Ctrl_H)
5601 && vim_strchr(p_ww, 'b') != NULL)
5602 || (cap->cmdchar == 'h'
5603 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005604 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 && vim_strchr(p_ww, '<') != NULL))
5606 && curwin->w_cursor.lnum > 1)
5607 {
5608 --(curwin->w_cursor.lnum);
5609 coladvance((colnr_T)MAXCOL);
5610 curwin->w_set_curswant = TRUE;
5611
5612 /* When the NL before the first char has to be deleted we
5613 * put the cursor on the NUL after the previous line.
5614 * This is a very special case, be careful!
5615 * don't adjust op_end now, otherwise it won't work */
5616 if ( (cap->oap->op_type == OP_DELETE
5617 || cap->oap->op_type == OP_CHANGE)
5618 && !lineempty(curwin->w_cursor.lnum))
5619 {
5620 ++curwin->w_cursor.col;
5621 cap->retval |= CA_NO_ADJ_OP_END;
5622 }
5623 continue;
5624 }
5625 /* Only beep and flush if not moved at all */
5626 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5627 beep_flush();
5628 break;
5629 }
5630 }
5631#ifdef FEAT_FOLDING
5632 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5633 && cap->oap->op_type == OP_NOP)
5634 foldOpenCursor();
5635#endif
5636}
5637
5638/*
5639 * Cursor up commands.
5640 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5641 */
5642 static void
5643nv_up(cap)
5644 cmdarg_T *cap;
5645{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005646 if (mod_mask & MOD_MASK_SHIFT)
5647 {
5648 /* <S-Up> is page up */
5649 cap->arg = BACKWARD;
5650 nv_page(cap);
5651 }
5652 else
5653 {
5654 cap->oap->motion_type = MLINE;
5655 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5656 clearopbeep(cap->oap);
5657 else if (cap->arg)
5658 beginline(BL_WHITE | BL_FIX);
5659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660}
5661
5662/*
5663 * Cursor down commands.
5664 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5665 */
5666 static void
5667nv_down(cap)
5668 cmdarg_T *cap;
5669{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005670 if (mod_mask & MOD_MASK_SHIFT)
5671 {
5672 /* <S-Down> is page down */
5673 cap->arg = FORWARD;
5674 nv_page(cap);
5675 }
5676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5678 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005679 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 do_cmdline_cmd((char_u *)".cc");
5681 else
5682#endif
5683 {
5684#ifdef FEAT_CMDWIN
5685 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005686 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 cmdwin_result = CAR;
5688 else
5689#endif
5690 {
5691 cap->oap->motion_type = MLINE;
5692 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5693 clearopbeep(cap->oap);
5694 else if (cap->arg)
5695 beginline(BL_WHITE | BL_FIX);
5696 }
5697 }
5698}
5699
5700#ifdef FEAT_SEARCHPATH
5701/*
5702 * Grab the file name under the cursor and edit it.
5703 */
5704 static void
5705nv_gotofile(cap)
5706 cmdarg_T *cap;
5707{
5708 char_u *ptr;
5709
5710#ifdef FEAT_CMDWIN
5711 if (cmdwin_type != 0)
5712 {
5713 clearopbeep(cap->oap);
5714 return;
5715 }
5716#endif
5717
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005718 ptr = grab_file_name(cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (ptr != NULL)
5721 {
5722 /* do autowrite if necessary */
5723 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5724 autowrite(curbuf, FALSE);
5725 setpcmark();
5726 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5727 P_HID(curbuf) ? ECMD_HIDE : 0);
5728 vim_free(ptr);
5729 }
5730 else
5731 clearop(cap->oap);
5732}
5733#endif
5734
5735/*
5736 * <End> command: to end of current line or last line.
5737 */
5738 static void
5739nv_end(cap)
5740 cmdarg_T *cap;
5741{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005742 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005744 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 nv_goto(cap);
5746 cap->count1 = 1; /* to end of current line */
5747 }
5748 nv_dollar(cap);
5749}
5750
5751/*
5752 * Handle the "$" command.
5753 */
5754 static void
5755nv_dollar(cap)
5756 cmdarg_T *cap;
5757{
5758 cap->oap->motion_type = MCHAR;
5759 cap->oap->inclusive = TRUE;
5760#ifdef FEAT_VIRTUALEDIT
5761 /* In virtual mode when off the edge of a line and an operator
5762 * is pending (whew!) keep the cursor where it is.
5763 * Otherwise, send it to the end of the line. */
5764 if (!virtual_active() || gchar_cursor() != NUL
5765 || cap->oap->op_type == OP_NOP)
5766#endif
5767 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5768 if (cursor_down((long)(cap->count1 - 1),
5769 cap->oap->op_type == OP_NOP) == FAIL)
5770 clearopbeep(cap->oap);
5771#ifdef FEAT_FOLDING
5772 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5773 foldOpenCursor();
5774#endif
5775}
5776
5777/*
5778 * Implementation of '?' and '/' commands.
5779 * If cap->arg is TRUE don't set PC mark.
5780 */
5781 static void
5782nv_search(cap)
5783 cmdarg_T *cap;
5784{
5785 oparg_T *oap = cap->oap;
5786
5787 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5788 {
5789 /* Translate "g??" to "g?g?" */
5790 cap->cmdchar = 'g';
5791 cap->nchar = '?';
5792 nv_operator(cap);
5793 return;
5794 }
5795
5796 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5797
5798 if (cap->searchbuf == NULL)
5799 {
5800 clearop(oap);
5801 return;
5802 }
5803
5804 normal_search(cap, cap->cmdchar, cap->searchbuf,
5805 (cap->arg ? 0 : SEARCH_MARK));
5806}
5807
5808/*
5809 * Handle "N" and "n" commands.
5810 * cap->arg is SEARCH_REV for "N", 0 for "n".
5811 */
5812 static void
5813nv_next(cap)
5814 cmdarg_T *cap;
5815{
5816 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5817}
5818
5819/*
5820 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5821 * Uses only cap->count1 and cap->oap from "cap".
5822 */
5823 static void
5824normal_search(cap, dir, pat, opt)
5825 cmdarg_T *cap;
5826 int dir;
5827 char_u *pat;
5828 int opt; /* extra flags for do_search() */
5829{
5830 int i;
5831
5832 cap->oap->motion_type = MCHAR;
5833 cap->oap->inclusive = FALSE;
5834 cap->oap->use_reg_one = TRUE;
5835 curwin->w_set_curswant = TRUE;
5836
5837 i = do_search(cap->oap, dir, pat, cap->count1,
5838 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
5839 if (i == 0)
5840 clearop(cap->oap);
5841 else
5842 {
5843 if (i == 2)
5844 cap->oap->motion_type = MLINE;
5845#ifdef FEAT_VIRTUALEDIT
5846 curwin->w_cursor.coladd = 0;
5847#endif
5848#ifdef FEAT_FOLDING
5849 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5850 foldOpenCursor();
5851#endif
5852 }
5853
5854 /* "/$" will put the cursor after the end of the line, may need to
5855 * correct that here */
5856 check_cursor();
5857}
5858
5859/*
5860 * Character search commands.
5861 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5862 * ',' and FALSE for ';'.
5863 * cap->nchar is NUL for ',' and ';' (repeat the search)
5864 */
5865 static void
5866nv_csearch(cap)
5867 cmdarg_T *cap;
5868{
5869 int t_cmd;
5870
5871 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5872 t_cmd = TRUE;
5873 else
5874 t_cmd = FALSE;
5875
5876 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5878 clearopbeep(cap->oap);
5879 else
5880 {
5881 curwin->w_set_curswant = TRUE;
5882#ifdef FEAT_VIRTUALEDIT
5883 /* Include a Tab for "tx" and for "dfx". */
5884 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5885 && (t_cmd || cap->oap->op_type != OP_NOP))
5886 {
5887 colnr_T scol, ecol;
5888
5889 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5890 curwin->w_cursor.coladd = ecol - scol;
5891 }
5892 else
5893 curwin->w_cursor.coladd = 0;
5894#endif
5895#ifdef FEAT_VISUAL
5896 adjust_for_sel(cap);
5897#endif
5898#ifdef FEAT_FOLDING
5899 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5900 foldOpenCursor();
5901#endif
5902 }
5903}
5904
5905/*
5906 * "[" and "]" commands.
5907 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5908 */
5909 static void
5910nv_brackets(cap)
5911 cmdarg_T *cap;
5912{
5913 pos_T new_pos;
5914 pos_T prev_pos;
5915 pos_T *pos = NULL; /* init for GCC */
5916 pos_T old_pos; /* cursor position before command */
5917 int flag;
5918 long n;
5919 int findc;
5920 int c;
5921
5922 cap->oap->motion_type = MCHAR;
5923 cap->oap->inclusive = FALSE;
5924 old_pos = curwin->w_cursor;
5925#ifdef FEAT_VIRTUALEDIT
5926 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
5927#endif
5928
5929#ifdef FEAT_SEARCHPATH
5930 /*
5931 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5932 */
5933 if (cap->nchar == 'f')
5934 nv_gotofile(cap);
5935 else
5936#endif
5937
5938#ifdef FEAT_FIND_ID
5939 /*
5940 * Find the occurence(s) of the identifier or define under cursor
5941 * in current and included files or jump to the first occurence.
5942 *
5943 * search list jump
5944 * fwd bwd fwd bwd fwd bwd
5945 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5946 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5947 */
5948 if (vim_strchr((char_u *)
5949#ifdef EBCDIC
5950 "iI\005dD\067",
5951#else
5952 "iI\011dD\004",
5953#endif
5954 cap->nchar) != NULL)
5955 {
5956 char_u *ptr;
5957 int len;
5958
5959 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5960 clearop(cap->oap);
5961 else
5962 {
5963 find_pattern_in_path(ptr, 0, len, TRUE,
5964 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
5965 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
5966 cap->count1,
5967 isupper(cap->nchar) ? ACTION_SHOW_ALL :
5968 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
5969 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
5970 (linenr_T)MAXLNUM);
5971 curwin->w_set_curswant = TRUE;
5972 }
5973 }
5974 else
5975#endif
5976
5977 /*
5978 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
5979 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
5980 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
5981 * "[m" or "]m" search for prev/next start of (Java) method.
5982 * "[M" or "]M" search for prev/next end of (Java) method.
5983 */
5984 if ( (cap->cmdchar == '['
5985 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
5986 || (cap->cmdchar == ']'
5987 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
5988 {
5989 if (cap->nchar == '*')
5990 cap->nchar = '/';
5991 new_pos.lnum = 0;
5992 prev_pos.lnum = 0;
5993 if (cap->nchar == 'm' || cap->nchar == 'M')
5994 {
5995 if (cap->cmdchar == '[')
5996 findc = '{';
5997 else
5998 findc = '}';
5999 n = 9999;
6000 }
6001 else
6002 {
6003 findc = cap->nchar;
6004 n = cap->count1;
6005 }
6006 for ( ; n > 0; --n)
6007 {
6008 if ((pos = findmatchlimit(cap->oap, findc,
6009 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6010 {
6011 if (new_pos.lnum == 0) /* nothing found */
6012 {
6013 if (cap->nchar != 'm' && cap->nchar != 'M')
6014 clearopbeep(cap->oap);
6015 }
6016 else
6017 pos = &new_pos; /* use last one found */
6018 break;
6019 }
6020 prev_pos = new_pos;
6021 curwin->w_cursor = *pos;
6022 new_pos = *pos;
6023 }
6024 curwin->w_cursor = old_pos;
6025
6026 /*
6027 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6028 * brought us to the match for "[m" and "]M" when inside a method.
6029 * Try finding the '{' or '}' we want to be at.
6030 * Also repeat for the given count.
6031 */
6032 if (cap->nchar == 'm' || cap->nchar == 'M')
6033 {
6034 /* norm is TRUE for "]M" and "[m" */
6035 int norm = ((findc == '{') == (cap->nchar == 'm'));
6036
6037 n = cap->count1;
6038 /* found a match: we were inside a method */
6039 if (prev_pos.lnum != 0)
6040 {
6041 pos = &prev_pos;
6042 curwin->w_cursor = prev_pos;
6043 if (norm)
6044 --n;
6045 }
6046 else
6047 pos = NULL;
6048 while (n > 0)
6049 {
6050 for (;;)
6051 {
6052 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6053 {
6054 /* if not found anything, that's an error */
6055 if (pos == NULL)
6056 clearopbeep(cap->oap);
6057 n = 0;
6058 break;
6059 }
6060 c = gchar_cursor();
6061 if (c == '{' || c == '}')
6062 {
6063 /* Must have found end/start of class: use it.
6064 * Or found the place to be at. */
6065 if ((c == findc && norm) || (n == 1 && !norm))
6066 {
6067 new_pos = curwin->w_cursor;
6068 pos = &new_pos;
6069 n = 0;
6070 }
6071 /* if no match found at all, we started outside of the
6072 * class and we're inside now. Just go on. */
6073 else if (new_pos.lnum == 0)
6074 {
6075 new_pos = curwin->w_cursor;
6076 pos = &new_pos;
6077 }
6078 /* found start/end of other method: go to match */
6079 else if ((pos = findmatchlimit(cap->oap, findc,
6080 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6081 0)) == NULL)
6082 n = 0;
6083 else
6084 curwin->w_cursor = *pos;
6085 break;
6086 }
6087 }
6088 --n;
6089 }
6090 curwin->w_cursor = old_pos;
6091 if (pos == NULL && new_pos.lnum != 0)
6092 clearopbeep(cap->oap);
6093 }
6094 if (pos != NULL)
6095 {
6096 setpcmark();
6097 curwin->w_cursor = *pos;
6098 curwin->w_set_curswant = TRUE;
6099#ifdef FEAT_FOLDING
6100 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6101 && cap->oap->op_type == OP_NOP)
6102 foldOpenCursor();
6103#endif
6104 }
6105 }
6106
6107 /*
6108 * "[[", "[]", "]]" and "][": move to start or end of function
6109 */
6110 else if (cap->nchar == '[' || cap->nchar == ']')
6111 {
6112 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6113 flag = '{';
6114 else
6115 flag = '}'; /* "][" or "[]" */
6116
6117 curwin->w_set_curswant = TRUE;
6118 /*
6119 * Imitate strange Vi behaviour: When using "]]" with an operator
6120 * we also stop at '}'.
6121 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006122 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 (cap->oap->op_type != OP_NOP
6124 && cap->arg == FORWARD && flag == '{')))
6125 clearopbeep(cap->oap);
6126 else
6127 {
6128 if (cap->oap->op_type == OP_NOP)
6129 beginline(BL_WHITE | BL_FIX);
6130#ifdef FEAT_FOLDING
6131 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6132 foldOpenCursor();
6133#endif
6134 }
6135 }
6136
6137 /*
6138 * "[p", "[P", "]P" and "]p": put with indent adjustment
6139 */
6140 else if (cap->nchar == 'p' || cap->nchar == 'P')
6141 {
6142 if (!checkclearopq(cap->oap))
6143 {
6144 prep_redo_cmd(cap);
6145 do_put(cap->oap->regname,
6146 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6147 cap->count1, PUT_FIXINDENT);
6148 }
6149 }
6150
6151 /*
6152 * "['", "[`", "]'" and "]`": jump to next mark
6153 */
6154 else if (cap->nchar == '\'' || cap->nchar == '`')
6155 {
6156 pos = &curwin->w_cursor;
6157 for (n = cap->count1; n > 0; --n)
6158 {
6159 prev_pos = *pos;
6160 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6161 cap->nchar == '\'');
6162 if (pos == NULL)
6163 break;
6164 }
6165 if (pos == NULL)
6166 pos = &prev_pos;
6167 nv_cursormark(cap, cap->nchar == '\'', pos);
6168 }
6169
6170#ifdef FEAT_MOUSE
6171 /*
6172 * [ or ] followed by a middle mouse click: put selected text with
6173 * indent adjustment. Any other button just does as usual.
6174 */
6175 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6176 {
6177 (void)do_mouse(cap->oap, cap->nchar,
6178 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6179 cap->count1, PUT_FIXINDENT);
6180 }
6181#endif /* FEAT_MOUSE */
6182
6183#ifdef FEAT_FOLDING
6184 /*
6185 * "[z" and "]z": move to start or end of open fold.
6186 */
6187 else if (cap->nchar == 'z')
6188 {
6189 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6190 cap->count1) == FAIL)
6191 clearopbeep(cap->oap);
6192 }
6193#endif
6194
6195#ifdef FEAT_DIFF
6196 /*
6197 * "[c" and "]c": move to next or previous diff-change.
6198 */
6199 else if (cap->nchar == 'c')
6200 {
6201 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6202 cap->count1) == FAIL)
6203 clearopbeep(cap->oap);
6204 }
6205#endif
6206
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006207#ifdef FEAT_SYN_HL
6208 /*
6209 * "[s", "[S", "]s" and "]S": move to next spell error.
6210 */
6211 else if (cap->nchar == 's' || cap->nchar == 'S')
6212 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006213 setpcmark();
6214 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006215 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6216 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006217 {
6218 clearopbeep(cap->oap);
6219 break;
6220 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006221 }
6222#endif
6223
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 /* Not a valid cap->nchar. */
6225 else
6226 clearopbeep(cap->oap);
6227}
6228
6229/*
6230 * Handle Normal mode "%" command.
6231 */
6232 static void
6233nv_percent(cap)
6234 cmdarg_T *cap;
6235{
6236 pos_T *pos;
6237#ifdef FEAT_FOLDING
6238 linenr_T lnum = curwin->w_cursor.lnum;
6239#endif
6240
6241 cap->oap->inclusive = TRUE;
6242 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6243 {
6244 if (cap->count0 > 100)
6245 clearopbeep(cap->oap);
6246 else
6247 {
6248 cap->oap->motion_type = MLINE;
6249 setpcmark();
6250 /* Round up, so CTRL-G will give same value. Watch out for a
6251 * large line count, the line number must not go negative! */
6252 if (curbuf->b_ml.ml_line_count > 1000000)
6253 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6254 / 100L * cap->count0;
6255 else
6256 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6257 cap->count0 + 99L) / 100L;
6258 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6259 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6260 beginline(BL_SOL | BL_FIX);
6261 }
6262 }
6263 else /* "%" : go to matching paren */
6264 {
6265 cap->oap->motion_type = MCHAR;
6266 cap->oap->use_reg_one = TRUE;
6267 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6268 clearopbeep(cap->oap);
6269 else
6270 {
6271 setpcmark();
6272 curwin->w_cursor = *pos;
6273 curwin->w_set_curswant = TRUE;
6274#ifdef FEAT_VIRTUALEDIT
6275 curwin->w_cursor.coladd = 0;
6276#endif
6277#ifdef FEAT_VISUAL
6278 adjust_for_sel(cap);
6279#endif
6280 }
6281 }
6282#ifdef FEAT_FOLDING
6283 if (cap->oap->op_type == OP_NOP
6284 && lnum != curwin->w_cursor.lnum
6285 && (fdo_flags & FDO_PERCENT)
6286 && KeyTyped)
6287 foldOpenCursor();
6288#endif
6289}
6290
6291/*
6292 * Handle "(" and ")" commands.
6293 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6294 */
6295 static void
6296nv_brace(cap)
6297 cmdarg_T *cap;
6298{
6299 cap->oap->motion_type = MCHAR;
6300 cap->oap->use_reg_one = TRUE;
6301 if (cap->cmdchar == ')')
6302 cap->oap->inclusive = FALSE;
6303 else
6304 cap->oap->inclusive = TRUE;
6305 curwin->w_set_curswant = TRUE;
6306
6307 if (findsent(cap->arg, cap->count1) == FAIL)
6308 clearopbeep(cap->oap);
6309 else
6310 {
6311#ifdef FEAT_VIRTUALEDIT
6312 curwin->w_cursor.coladd = 0;
6313#endif
6314#ifdef FEAT_FOLDING
6315 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6316 foldOpenCursor();
6317#endif
6318 }
6319}
6320
6321/*
6322 * "m" command: Mark a position.
6323 */
6324 static void
6325nv_mark(cap)
6326 cmdarg_T *cap;
6327{
6328 if (!checkclearop(cap->oap))
6329 {
6330 if (setmark(cap->nchar) == FAIL)
6331 clearopbeep(cap->oap);
6332 }
6333}
6334
6335/*
6336 * "{" and "}" commands.
6337 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6338 */
6339 static void
6340nv_findpar(cap)
6341 cmdarg_T *cap;
6342{
6343 cap->oap->motion_type = MCHAR;
6344 cap->oap->inclusive = FALSE;
6345 cap->oap->use_reg_one = TRUE;
6346 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006347 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 clearopbeep(cap->oap);
6349 else
6350 {
6351#ifdef FEAT_VIRTUALEDIT
6352 curwin->w_cursor.coladd = 0;
6353#endif
6354#ifdef FEAT_FOLDING
6355 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6356 foldOpenCursor();
6357#endif
6358 }
6359}
6360
6361/*
6362 * "u" command: Undo or make lower case.
6363 */
6364 static void
6365nv_undo(cap)
6366 cmdarg_T *cap;
6367{
6368 if (cap->oap->op_type == OP_LOWER
6369#ifdef FEAT_VISUAL
6370 || VIsual_active
6371#endif
6372 )
6373 {
6374 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6375 cap->cmdchar = 'g';
6376 cap->nchar = 'u';
6377 nv_operator(cap);
6378 }
6379 else
6380 nv_kundo(cap);
6381}
6382
6383/*
6384 * <Undo> command.
6385 */
6386 static void
6387nv_kundo(cap)
6388 cmdarg_T *cap;
6389{
6390 if (!checkclearopq(cap->oap))
6391 {
6392 u_undo((int)cap->count1);
6393 curwin->w_set_curswant = TRUE;
6394 }
6395}
6396
6397/*
6398 * Handle the "r" command.
6399 */
6400 static void
6401nv_replace(cap)
6402 cmdarg_T *cap;
6403{
6404 char_u *ptr;
6405 int had_ctrl_v;
6406 long n;
6407
6408 if (checkclearop(cap->oap))
6409 return;
6410
6411 /* get another character */
6412 if (cap->nchar == Ctrl_V)
6413 {
6414 had_ctrl_v = Ctrl_V;
6415 cap->nchar = get_literal();
6416 /* Don't redo a multibyte character with CTRL-V. */
6417 if (cap->nchar > DEL)
6418 had_ctrl_v = NUL;
6419 }
6420 else
6421 had_ctrl_v = NUL;
6422
6423#ifdef FEAT_VISUAL
6424 /* Visual mode "r" */
6425 if (VIsual_active)
6426 {
6427 nv_operator(cap);
6428 return;
6429 }
6430#endif
6431
6432#ifdef FEAT_VIRTUALEDIT
6433 /* Break tabs, etc. */
6434 if (virtual_active())
6435 {
6436 if (u_save_cursor() == FAIL)
6437 return;
6438 if (gchar_cursor() == NUL)
6439 {
6440 /* Add extra space and put the cursor on the first one. */
6441 coladvance_force((colnr_T)(getviscol() + cap->count1));
6442 curwin->w_cursor.col -= cap->count1;
6443 }
6444 else if (gchar_cursor() == TAB)
6445 coladvance_force(getviscol());
6446 }
6447#endif
6448
6449 /*
6450 * Check for a special key or not enough characters to replace.
6451 */
6452 ptr = ml_get_cursor();
6453 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6454#ifdef FEAT_MBYTE
6455 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6456#endif
6457 )
6458 {
6459 clearopbeep(cap->oap);
6460 return;
6461 }
6462
6463 /*
6464 * Replacing with a TAB is done by edit() when it is complicated because
6465 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6466 * Other characters are done below to avoid problems with things like
6467 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6468 */
6469 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6470 {
6471 stuffnumReadbuff(cap->count1);
6472 stuffcharReadbuff('R');
6473 stuffcharReadbuff('\t');
6474 stuffcharReadbuff(ESC);
6475 return;
6476 }
6477
6478 /* save line for undo */
6479 if (u_save_cursor() == FAIL)
6480 return;
6481
6482 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6483 {
6484 /*
6485 * Replace character(s) by a single newline.
6486 * Strange vi behaviour: Only one newline is inserted.
6487 * Delete the characters here.
6488 * Insert the newline with an insert command, takes care of
6489 * autoindent. The insert command depends on being on the last
6490 * character of a line or not.
6491 */
6492#ifdef FEAT_MBYTE
6493 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6494#else
6495 (void)del_bytes(cap->count1, FALSE); /* delete the characters */
6496#endif
6497 stuffcharReadbuff('\r');
6498 stuffcharReadbuff(ESC);
6499
6500 /* Give 'r' to edit(), to get the redo command right. */
6501 invoke_edit(cap, TRUE, 'r', FALSE);
6502 }
6503 else
6504 {
6505 prep_redo(cap->oap->regname, cap->count1,
6506 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6507
6508 curbuf->b_op_start = curwin->w_cursor;
6509#ifdef FEAT_MBYTE
6510 if (has_mbyte)
6511 {
6512 int old_State = State;
6513
6514 if (cap->ncharC1 != 0)
6515 AppendCharToRedobuff(cap->ncharC1);
6516 if (cap->ncharC2 != 0)
6517 AppendCharToRedobuff(cap->ncharC2);
6518
6519 /* This is slow, but it handles replacing a single-byte with a
6520 * multi-byte and the other way around. Also handles adding
6521 * composing characters for utf-8. */
6522 for (n = cap->count1; n > 0; --n)
6523 {
6524 State = REPLACE;
6525 ins_char(cap->nchar);
6526 State = old_State;
6527 if (cap->ncharC1 != 0)
6528 ins_char(cap->ncharC1);
6529 if (cap->ncharC2 != 0)
6530 ins_char(cap->ncharC2);
6531 }
6532 }
6533 else
6534#endif
6535 {
6536 /*
6537 * Replace the characters within one line.
6538 */
6539 for (n = cap->count1; n > 0; --n)
6540 {
6541 /*
6542 * Get ptr again, because u_save and/or showmatch() will have
6543 * released the line. At the same time we let know that the
6544 * line will be changed.
6545 */
6546 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6547 ptr[curwin->w_cursor.col] = cap->nchar;
6548 if (p_sm && msg_silent == 0)
6549 showmatch(cap->nchar);
6550 ++curwin->w_cursor.col;
6551 }
6552#ifdef FEAT_NETBEANS_INTG
6553 if (usingNetbeans)
6554 {
6555 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6556
Bram Moolenaar009b2592004-10-24 19:18:58 +00006557 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6558 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006560 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562#endif
6563
6564 /* mark the buffer as changed and prepare for displaying */
6565 changed_bytes(curwin->w_cursor.lnum,
6566 (colnr_T)(curwin->w_cursor.col - cap->count1));
6567 }
6568 --curwin->w_cursor.col; /* cursor on the last replaced char */
6569#ifdef FEAT_MBYTE
6570 /* if the character on the left of the current cursor is a multi-byte
6571 * character, move two characters left */
6572 if (has_mbyte)
6573 mb_adjust_cursor();
6574#endif
6575 curbuf->b_op_end = curwin->w_cursor;
6576 curwin->w_set_curswant = TRUE;
6577 set_last_insert(cap->nchar);
6578 }
6579}
6580
6581#ifdef FEAT_VISUAL
6582/*
6583 * 'o': Exchange start and end of Visual area.
6584 * 'O': same, but in block mode exchange left and right corners.
6585 */
6586 static void
6587v_swap_corners(cmdchar)
6588 int cmdchar;
6589{
6590 pos_T old_cursor;
6591 colnr_T left, right;
6592
6593 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6594 {
6595 old_cursor = curwin->w_cursor;
6596 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6597 curwin->w_cursor.lnum = VIsual.lnum;
6598 coladvance(left);
6599 VIsual = curwin->w_cursor;
6600
6601 curwin->w_cursor.lnum = old_cursor.lnum;
6602 curwin->w_curswant = right;
6603 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6604 * right one column */
6605 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6606 ++curwin->w_curswant;
6607 coladvance(curwin->w_curswant);
6608 if (curwin->w_cursor.col == old_cursor.col
6609#ifdef FEAT_VIRTUALEDIT
6610 && (!virtual_active()
6611 || curwin->w_cursor.coladd == old_cursor.coladd)
6612#endif
6613 )
6614 {
6615 curwin->w_cursor.lnum = VIsual.lnum;
6616 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6617 ++right;
6618 coladvance(right);
6619 VIsual = curwin->w_cursor;
6620
6621 curwin->w_cursor.lnum = old_cursor.lnum;
6622 coladvance(left);
6623 curwin->w_curswant = left;
6624 }
6625 }
6626 else
6627 {
6628 old_cursor = curwin->w_cursor;
6629 curwin->w_cursor = VIsual;
6630 VIsual = old_cursor;
6631 curwin->w_set_curswant = TRUE;
6632 }
6633}
6634#endif /* FEAT_VISUAL */
6635
6636/*
6637 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6638 */
6639 static void
6640nv_Replace(cap)
6641 cmdarg_T *cap;
6642{
6643#ifdef FEAT_VISUAL
6644 if (VIsual_active) /* "R" is replace lines */
6645 {
6646 cap->cmdchar = 'c';
6647 cap->nchar = NUL;
6648 VIsual_mode = 'V';
6649 nv_operator(cap);
6650 }
6651 else
6652#endif
6653 if (!checkclearopq(cap->oap))
6654 {
6655 if (!curbuf->b_p_ma)
6656 EMSG(_(e_modifiable));
6657 else
6658 {
6659#ifdef FEAT_VIRTUALEDIT
6660 if (virtual_active())
6661 coladvance(getviscol());
6662#endif
6663 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6664 }
6665 }
6666}
6667
6668#ifdef FEAT_VREPLACE
6669/*
6670 * "gr".
6671 */
6672 static void
6673nv_vreplace(cap)
6674 cmdarg_T *cap;
6675{
6676# ifdef FEAT_VISUAL
6677 if (VIsual_active)
6678 {
6679 cap->cmdchar = 'r';
6680 cap->nchar = cap->extra_char;
6681 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6682 }
6683 else
6684# endif
6685 if (!checkclearopq(cap->oap))
6686 {
6687 if (!curbuf->b_p_ma)
6688 EMSG(_(e_modifiable));
6689 else
6690 {
6691 if (cap->extra_char == Ctrl_V) /* get another character */
6692 cap->extra_char = get_literal();
6693 stuffcharReadbuff(cap->extra_char);
6694 stuffcharReadbuff(ESC);
6695# ifdef FEAT_VIRTUALEDIT
6696 if (virtual_active())
6697 coladvance(getviscol());
6698# endif
6699 invoke_edit(cap, TRUE, 'v', FALSE);
6700 }
6701 }
6702}
6703#endif
6704
6705/*
6706 * Swap case for "~" command, when it does not work like an operator.
6707 */
6708 static void
6709n_swapchar(cap)
6710 cmdarg_T *cap;
6711{
6712 long n;
6713 pos_T startpos;
6714 int did_change = 0;
6715#ifdef FEAT_NETBEANS_INTG
6716 pos_T pos;
6717 char_u *ptr;
6718 int count;
6719#endif
6720
6721 if (checkclearopq(cap->oap))
6722 return;
6723
6724 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6725 {
6726 clearopbeep(cap->oap);
6727 return;
6728 }
6729
6730 prep_redo_cmd(cap);
6731
6732 if (u_save_cursor() == FAIL)
6733 return;
6734
6735 startpos = curwin->w_cursor;
6736#ifdef FEAT_NETBEANS_INTG
6737 pos = startpos;
6738#endif
6739 for (n = cap->count1; n > 0; --n)
6740 {
6741 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6742 inc_cursor();
6743 if (gchar_cursor() == NUL)
6744 {
6745 if (vim_strchr(p_ww, '~') != NULL
6746 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6747 {
6748#ifdef FEAT_NETBEANS_INTG
6749 if (usingNetbeans)
6750 {
6751 if (did_change)
6752 {
6753 ptr = ml_get(pos.lnum);
6754 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006755 netbeans_removed(curbuf, pos.lnum, pos.col,
6756 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006758 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
6760 pos.col = 0;
6761 pos.lnum++;
6762 }
6763#endif
6764 ++curwin->w_cursor.lnum;
6765 curwin->w_cursor.col = 0;
6766 if (n > 1)
6767 {
6768 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6769 break;
6770 u_clearline();
6771 }
6772 }
6773 else
6774 break;
6775 }
6776 }
6777#ifdef FEAT_NETBEANS_INTG
6778 if (did_change && usingNetbeans)
6779 {
6780 ptr = ml_get(pos.lnum);
6781 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006782 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6783 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
6785#endif
6786
6787
6788 check_cursor();
6789 curwin->w_set_curswant = TRUE;
6790 if (did_change)
6791 {
6792 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6793 0L);
6794 curbuf->b_op_start = startpos;
6795 curbuf->b_op_end = curwin->w_cursor;
6796 if (curbuf->b_op_end.col > 0)
6797 --curbuf->b_op_end.col;
6798 }
6799}
6800
6801/*
6802 * Move cursor to mark.
6803 */
6804 static void
6805nv_cursormark(cap, flag, pos)
6806 cmdarg_T *cap;
6807 int flag;
6808 pos_T *pos;
6809{
6810 if (check_mark(pos) == FAIL)
6811 clearop(cap->oap);
6812 else
6813 {
6814 if (cap->cmdchar == '\''
6815 || cap->cmdchar == '`'
6816 || cap->cmdchar == '['
6817 || cap->cmdchar == ']')
6818 setpcmark();
6819 curwin->w_cursor = *pos;
6820 if (flag)
6821 beginline(BL_WHITE | BL_FIX);
6822 else
6823 check_cursor();
6824 }
6825 cap->oap->motion_type = flag ? MLINE : MCHAR;
6826 if (cap->cmdchar == '`')
6827 cap->oap->use_reg_one = TRUE;
6828 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6829 curwin->w_set_curswant = TRUE;
6830}
6831
6832#ifdef FEAT_VISUAL
6833/*
6834 * Handle commands that are operators in Visual mode.
6835 */
6836 static void
6837v_visop(cap)
6838 cmdarg_T *cap;
6839{
6840 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6841
6842 /* Uppercase means linewise, except in block mode, then "D" deletes till
6843 * the end of the line, and "C" replaces til EOL */
6844 if (isupper(cap->cmdchar))
6845 {
6846 if (VIsual_mode != Ctrl_V)
6847 VIsual_mode = 'V';
6848 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6849 curwin->w_curswant = MAXCOL;
6850 }
6851 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6852 nv_operator(cap);
6853}
6854#endif
6855
6856/*
6857 * "s" and "S" commands.
6858 */
6859 static void
6860nv_subst(cap)
6861 cmdarg_T *cap;
6862{
6863#ifdef FEAT_VISUAL
6864 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6865 {
6866 if (cap->cmdchar == 'S')
6867 VIsual_mode = 'V';
6868 cap->cmdchar = 'c';
6869 nv_operator(cap);
6870 }
6871 else
6872#endif
6873 nv_optrans(cap);
6874}
6875
6876/*
6877 * Abbreviated commands.
6878 */
6879 static void
6880nv_abbrev(cap)
6881 cmdarg_T *cap;
6882{
6883 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6884 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6885
6886#ifdef FEAT_VISUAL
6887 /* in Visual mode these commands are operators */
6888 if (VIsual_active)
6889 v_visop(cap);
6890 else
6891#endif
6892 nv_optrans(cap);
6893}
6894
6895/*
6896 * Translate a command into another command.
6897 */
6898 static void
6899nv_optrans(cap)
6900 cmdarg_T *cap;
6901{
6902 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6903 (char_u *)"d$", (char_u *)"c$",
6904 (char_u *)"cl", (char_u *)"cc",
6905 (char_u *)"yy", (char_u *)":s\r"};
6906 static char_u *str = (char_u *)"xXDCsSY&";
6907
6908 if (!checkclearopq(cap->oap))
6909 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006910 /* In Vi "2D" doesn't delete the next line. Can't translate it
6911 * either, because "2." should also not use the count. */
6912 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6913 {
6914 cap->oap->start = curwin->w_cursor;
6915 cap->oap->op_type = OP_DELETE;
6916 cap->count1 = 1;
6917 nv_dollar(cap);
6918 finish_op = TRUE;
6919 ResetRedobuff();
6920 AppendCharToRedobuff('D');
6921 }
6922 else
6923 {
6924 if (cap->count0)
6925 stuffnumReadbuff(cap->count0);
6926 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
6927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929 cap->opcount = 0;
6930}
6931
6932/*
6933 * "'" and "`" commands. Also for "g'" and "g`".
6934 * cap->arg is TRUE for "'" and "g'".
6935 */
6936 static void
6937nv_gomark(cap)
6938 cmdarg_T *cap;
6939{
6940 pos_T *pos;
6941 int c;
6942#ifdef FEAT_FOLDING
6943 linenr_T lnum = curwin->w_cursor.lnum;
6944 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6945#endif
6946
6947 if (cap->cmdchar == 'g')
6948 c = cap->extra_char;
6949 else
6950 c = cap->nchar;
6951 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6952 if (pos == (pos_T *)-1) /* jumped to other file */
6953 {
6954 if (cap->arg)
6955 {
6956 check_cursor_lnum();
6957 beginline(BL_WHITE | BL_FIX);
6958 }
6959 else
6960 check_cursor();
6961 }
6962 else
6963 nv_cursormark(cap, cap->arg, pos);
6964
6965#ifdef FEAT_VIRTUALEDIT
6966 /* May need to clear the coladd that a mark includes. */
6967 if (!virtual_active())
6968 curwin->w_cursor.coladd = 0;
6969#endif
6970#ifdef FEAT_FOLDING
6971 if (cap->oap->op_type == OP_NOP
6972 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6973 && (fdo_flags & FDO_MARK)
6974 && old_KeyTyped)
6975 foldOpenCursor();
6976#endif
6977}
6978
6979/*
6980 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
6981 */
6982 static void
6983nv_pcmark(cap)
6984 cmdarg_T *cap;
6985{
6986#ifdef FEAT_JUMPLIST
6987 pos_T *pos;
6988# ifdef FEAT_FOLDING
6989 linenr_T lnum = curwin->w_cursor.lnum;
6990 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6991# endif
6992
6993 if (!checkclearopq(cap->oap))
6994 {
6995 if (cap->cmdchar == 'g')
6996 pos = movechangelist((int)cap->count1);
6997 else
6998 pos = movemark((int)cap->count1);
6999 if (pos == (pos_T *)-1) /* jump to other file */
7000 {
7001 curwin->w_set_curswant = TRUE;
7002 check_cursor();
7003 }
7004 else if (pos != NULL) /* can jump */
7005 nv_cursormark(cap, FALSE, pos);
7006 else if (cap->cmdchar == 'g')
7007 {
7008 if (curbuf->b_changelistlen == 0)
7009 EMSG(_("E664: changelist is empty"));
7010 else if (cap->count1 < 0)
7011 EMSG(_("E662: At start of changelist"));
7012 else
7013 EMSG(_("E663: At end of changelist"));
7014 }
7015 else
7016 clearopbeep(cap->oap);
7017# ifdef FEAT_FOLDING
7018 if (cap->oap->op_type == OP_NOP
7019 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7020 && (fdo_flags & FDO_MARK)
7021 && old_KeyTyped)
7022 foldOpenCursor();
7023# endif
7024 }
7025#else
7026 clearopbeep(cap->oap);
7027#endif
7028}
7029
7030/*
7031 * Handle '"' command.
7032 */
7033 static void
7034nv_regname(cap)
7035 cmdarg_T *cap;
7036{
7037 if (checkclearop(cap->oap))
7038 return;
7039#ifdef FEAT_EVAL
7040 if (cap->nchar == '=')
7041 cap->nchar = get_expr_register();
7042#endif
7043 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7044 {
7045 cap->oap->regname = cap->nchar;
7046 cap->opcount = cap->count0; /* remember count before '"' */
7047#ifdef FEAT_EVAL
7048 set_reg_var(cap->oap->regname);
7049#endif
7050 }
7051 else
7052 clearopbeep(cap->oap);
7053}
7054
7055#ifdef FEAT_VISUAL
7056/*
7057 * Handle "v", "V" and "CTRL-V" commands.
7058 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7059 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007060 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 */
7062 static void
7063nv_visual(cap)
7064 cmdarg_T *cap;
7065{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007066 if (cap->cmdchar == Ctrl_Q)
7067 cap->cmdchar = Ctrl_V;
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7070 * characterwise, linewise, or blockwise. */
7071 if (cap->oap->op_type != OP_NOP)
7072 {
7073 cap->oap->motion_force = cap->cmdchar;
7074 finish_op = FALSE; /* operator doesn't finish now but later */
7075 return;
7076 }
7077
7078 VIsual_select = cap->arg;
7079 if (VIsual_active) /* change Visual mode */
7080 {
7081 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7082 end_visual_mode();
7083 else /* toggle char/block mode */
7084 { /* or char/line mode */
7085 VIsual_mode = cap->cmdchar;
7086 showmode();
7087 }
7088 redraw_curbuf_later(INVERTED); /* update the inversion */
7089 }
7090 else /* start Visual mode */
7091 {
7092 check_visual_highlight();
7093 if (cap->count0) /* use previously selected part */
7094 {
7095 if (resel_VIsual_mode == NUL) /* there is none */
7096 {
7097 beep_flush();
7098 return;
7099 }
7100 VIsual = curwin->w_cursor;
7101
7102 VIsual_active = TRUE;
7103 VIsual_reselect = TRUE;
7104 if (!cap->arg)
7105 /* start Select mode when 'selectmode' contains "cmd" */
7106 may_start_select('c');
7107#ifdef FEAT_MOUSE
7108 setmouse();
7109#endif
7110 if (p_smd)
7111 redraw_cmdline = TRUE; /* show visual mode later */
7112 /*
7113 * For V and ^V, we multiply the number of lines even if there
7114 * was only one -- webb
7115 */
7116 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7117 {
7118 curwin->w_cursor.lnum +=
7119 resel_VIsual_line_count * cap->count0 - 1;
7120 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7121 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7122 }
7123 VIsual_mode = resel_VIsual_mode;
7124 if (VIsual_mode == 'v')
7125 {
7126 if (resel_VIsual_line_count <= 1)
7127 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7128 else
7129 curwin->w_cursor.col = resel_VIsual_col;
7130 check_cursor_col();
7131 }
7132 if (resel_VIsual_col == MAXCOL)
7133 {
7134 curwin->w_curswant = MAXCOL;
7135 coladvance((colnr_T)MAXCOL);
7136 }
7137 else if (VIsual_mode == Ctrl_V)
7138 {
7139 validate_virtcol();
7140 curwin->w_curswant = curwin->w_virtcol
7141 + resel_VIsual_col * cap->count0 - 1;
7142 coladvance(curwin->w_curswant);
7143 }
7144 else
7145 curwin->w_set_curswant = TRUE;
7146 redraw_curbuf_later(INVERTED); /* show the inversion */
7147 }
7148 else
7149 {
7150 if (!cap->arg)
7151 /* start Select mode when 'selectmode' contains "cmd" */
7152 may_start_select('c');
7153 n_start_visual_mode(cap->cmdchar);
7154 }
7155 }
7156}
7157
7158/*
7159 * Start selection for Shift-movement keys.
7160 */
7161 void
7162start_selection()
7163{
7164 /* if 'selectmode' contains "key", start Select mode */
7165 may_start_select('k');
7166 n_start_visual_mode('v');
7167}
7168
7169/*
7170 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7171 */
7172 void
7173may_start_select(c)
7174 int c;
7175{
7176 VIsual_select = (stuff_empty() && typebuf_typed()
7177 && (vim_strchr(p_slm, c) != NULL));
7178}
7179
7180/*
7181 * Start Visual mode "c".
7182 * Should set VIsual_select before calling this.
7183 */
7184 static void
7185n_start_visual_mode(c)
7186 int c;
7187{
7188 VIsual_mode = c;
7189 VIsual_active = TRUE;
7190 VIsual_reselect = TRUE;
7191#ifdef FEAT_VIRTUALEDIT
7192 /* Corner case: the 0 position in a tab may change when going into
7193 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7194 */
7195 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7196 coladvance(curwin->w_virtcol);
7197#endif
7198 VIsual = curwin->w_cursor;
7199
7200#ifdef FEAT_FOLDING
7201 foldAdjustVisual();
7202#endif
7203
7204#ifdef FEAT_MOUSE
7205 setmouse();
7206#endif
7207 if (p_smd)
7208 redraw_cmdline = TRUE; /* show visual mode later */
7209#ifdef FEAT_CLIPBOARD
7210 /* Make sure the clipboard gets updated. Needed because start and
7211 * end may still be the same, and the selection needs to be owned */
7212 clip_star.vmode = NUL;
7213#endif
7214
7215 /* Only need to redraw this line, unless still need to redraw an old
7216 * Visual area (when 'lazyredraw' is set). */
7217 if (curwin->w_redr_type < INVERTED)
7218 {
7219 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7220 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7221 }
7222}
7223
7224#endif /* FEAT_VISUAL */
7225
7226/*
7227 * CTRL-W: Window commands
7228 */
7229 static void
7230nv_window(cap)
7231 cmdarg_T *cap;
7232{
7233#ifdef FEAT_WINDOWS
7234 if (!checkclearop(cap->oap))
7235 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7236#else
7237 (void)checkclearop(cap->oap);
7238#endif
7239}
7240
7241/*
7242 * CTRL-Z: Suspend
7243 */
7244 static void
7245nv_suspend(cap)
7246 cmdarg_T *cap;
7247{
7248 clearop(cap->oap);
7249#ifdef FEAT_VISUAL
7250 if (VIsual_active)
7251 end_visual_mode(); /* stop Visual mode */
7252#endif
7253 do_cmdline_cmd((char_u *)"st");
7254}
7255
7256/*
7257 * Commands starting with "g".
7258 */
7259 static void
7260nv_g_cmd(cap)
7261 cmdarg_T *cap;
7262{
7263 oparg_T *oap = cap->oap;
7264#ifdef FEAT_VISUAL
7265 pos_T tpos;
7266#endif
7267 int i;
7268 int flag = FALSE;
7269
7270 switch (cap->nchar)
7271 {
7272#ifdef MEM_PROFILE
7273 /*
7274 * "g^A": dump log of used memory.
7275 */
7276 case Ctrl_A:
7277 vim_mem_profile_dump();
7278 break;
7279#endif
7280
7281#ifdef FEAT_VREPLACE
7282 /*
7283 * "gR": Enter virtual replace mode.
7284 */
7285 case 'R':
7286 cap->arg = TRUE;
7287 nv_Replace(cap);
7288 break;
7289
7290 case 'r':
7291 nv_vreplace(cap);
7292 break;
7293#endif
7294
7295 case '&':
7296 do_cmdline_cmd((char_u *)"%s//~/&");
7297 break;
7298
7299#ifdef FEAT_VISUAL
7300 /*
7301 * "gv": Reselect the previous Visual area. If Visual already active,
7302 * exchange previous and current Visual area.
7303 */
7304 case 'v':
7305 if (checkclearop(oap))
7306 break;
7307
7308 if ( curbuf->b_visual_start.lnum == 0
7309 || curbuf->b_visual_start.lnum > curbuf->b_ml.ml_line_count
7310 || curbuf->b_visual_end.lnum == 0)
7311 beep_flush();
7312 else
7313 {
7314 /* set w_cursor to the start of the Visual area, tpos to the end */
7315 if (VIsual_active)
7316 {
7317 i = VIsual_mode;
7318 VIsual_mode = curbuf->b_visual_mode;
7319 curbuf->b_visual_mode = i;
7320# ifdef FEAT_EVAL
7321 curbuf->b_visual_mode_eval = i;
7322# endif
7323 i = curwin->w_curswant;
7324 curwin->w_curswant = curbuf->b_visual_curswant;
7325 curbuf->b_visual_curswant = i;
7326
7327 tpos = curbuf->b_visual_end;
7328 curbuf->b_visual_end = curwin->w_cursor;
7329 curwin->w_cursor = curbuf->b_visual_start;
7330 curbuf->b_visual_start = VIsual;
7331 }
7332 else
7333 {
7334 VIsual_mode = curbuf->b_visual_mode;
7335 curwin->w_curswant = curbuf->b_visual_curswant;
7336 tpos = curbuf->b_visual_end;
7337 curwin->w_cursor = curbuf->b_visual_start;
7338 }
7339
7340 VIsual_active = TRUE;
7341 VIsual_reselect = TRUE;
7342
7343 /* Set Visual to the start and w_cursor to the end of the Visual
7344 * area. Make sure they are on an existing character. */
7345 check_cursor();
7346 VIsual = curwin->w_cursor;
7347 curwin->w_cursor = tpos;
7348 check_cursor();
7349 update_topline();
7350 /*
7351 * When called from normal "g" command: start Select mode when
7352 * 'selectmode' contains "cmd". When called for K_SELECT, always
7353 * start Select mode.
7354 */
7355 if (cap->arg)
7356 VIsual_select = TRUE;
7357 else
7358 may_start_select('c');
7359#ifdef FEAT_MOUSE
7360 setmouse();
7361#endif
7362#ifdef FEAT_CLIPBOARD
7363 /* Make sure the clipboard gets updated. Needed because start and
7364 * end are still the same, and the selection needs to be owned */
7365 clip_star.vmode = NUL;
7366#endif
7367 redraw_curbuf_later(INVERTED);
7368 showmode();
7369 }
7370 break;
7371 /*
7372 * "gV": Don't reselect the previous Visual area after a Select mode
7373 * mapping of menu.
7374 */
7375 case 'V':
7376 VIsual_reselect = FALSE;
7377 break;
7378
7379 /*
7380 * "gh": start Select mode.
7381 * "gH": start Select line mode.
7382 * "g^H": start Select block mode.
7383 */
7384 case K_BS:
7385 cap->nchar = Ctrl_H;
7386 /* FALLTHROUGH */
7387 case 'h':
7388 case 'H':
7389 case Ctrl_H:
7390# ifdef EBCDIC
7391 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7392 if (cap->nchar == Ctrl_H)
7393 cap->cmdchar = Ctrl_V;
7394 else
7395# endif
7396 cap->cmdchar = cap->nchar + ('v' - 'h');
7397 cap->arg = TRUE;
7398 nv_visual(cap);
7399 break;
7400#endif /* FEAT_VISUAL */
7401
7402 /*
7403 * "gj" and "gk" two new funny movement keys -- up and down
7404 * movement based on *screen* line rather than *file* line.
7405 */
7406 case 'j':
7407 case K_DOWN:
7408 /* with 'nowrap' it works just like the normal "j" command; also when
7409 * in a closed fold */
7410 if (!curwin->w_p_wrap
7411#ifdef FEAT_FOLDING
7412 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7413#endif
7414 )
7415 {
7416 oap->motion_type = MLINE;
7417 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7418 }
7419 else
7420 i = nv_screengo(oap, FORWARD, cap->count1);
7421 if (i == FAIL)
7422 clearopbeep(oap);
7423 break;
7424
7425 case 'k':
7426 case K_UP:
7427 /* with 'nowrap' it works just like the normal "k" command; also when
7428 * in a closed fold */
7429 if (!curwin->w_p_wrap
7430#ifdef FEAT_FOLDING
7431 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7432#endif
7433 )
7434 {
7435 oap->motion_type = MLINE;
7436 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7437 }
7438 else
7439 i = nv_screengo(oap, BACKWARD, cap->count1);
7440 if (i == FAIL)
7441 clearopbeep(oap);
7442 break;
7443
7444 /*
7445 * "gJ": join two lines without inserting a space.
7446 */
7447 case 'J':
7448 nv_join(cap);
7449 break;
7450
7451 /*
7452 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7453 * "gm": middle of "g0" and "g$".
7454 */
7455 case '^':
7456 flag = TRUE;
7457 /* FALLTHROUGH */
7458
7459 case '0':
7460 case 'm':
7461 case K_HOME:
7462 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 oap->motion_type = MCHAR;
7464 oap->inclusive = FALSE;
7465 if (curwin->w_p_wrap
7466#ifdef FEAT_VERTSPLIT
7467 && curwin->w_width != 0
7468#endif
7469 )
7470 {
7471 int width1 = W_WIDTH(curwin) - curwin_col_off();
7472 int width2 = width1 + curwin_col_off2();
7473
7474 validate_virtcol();
7475 i = 0;
7476 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7477 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7478 }
7479 else
7480 i = curwin->w_leftcol;
7481 /* Go to the middle of the screen line. When 'number' is on and lines
7482 * are wrapping the middle can be more to the left.*/
7483 if (cap->nchar == 'm')
7484 i += (W_WIDTH(curwin) - curwin_col_off()
7485 + ((curwin->w_p_wrap && i > 0)
7486 ? curwin_col_off2() : 0)) / 2;
7487 coladvance((colnr_T)i);
7488 if (flag)
7489 {
7490 do
7491 i = gchar_cursor();
7492 while (vim_iswhite(i) && oneright() == OK);
7493 }
7494 curwin->w_set_curswant = TRUE;
7495 break;
7496
7497 case '_':
7498 /* "g_": to the last non-blank character in the line or <count> lines
7499 * downward. */
7500 cap->oap->motion_type = MCHAR;
7501 cap->oap->inclusive = TRUE;
7502 curwin->w_curswant = MAXCOL;
7503 if (cursor_down((long)(cap->count1 - 1),
7504 cap->oap->op_type == OP_NOP) == FAIL)
7505 clearopbeep(cap->oap);
7506 else
7507 {
7508 char_u *ptr = ml_get_curline();
7509
7510 /* In Visual mode we may end up after the line. */
7511 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7512 --curwin->w_cursor.col;
7513
7514 /* Decrease the cursor column until it's on a non-blank. */
7515 while (curwin->w_cursor.col > 0
7516 && vim_iswhite(ptr[curwin->w_cursor.col]))
7517 --curwin->w_cursor.col;
7518 curwin->w_set_curswant = TRUE;
7519 }
7520 break;
7521
7522 case '$':
7523 case K_END:
7524 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 int col_off = curwin_col_off();
7527
7528 oap->motion_type = MCHAR;
7529 oap->inclusive = TRUE;
7530 if (curwin->w_p_wrap
7531#ifdef FEAT_VERTSPLIT
7532 && curwin->w_width != 0
7533#endif
7534 )
7535 {
7536 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7537 if (cap->count1 == 1)
7538 {
7539 int width1 = W_WIDTH(curwin) - col_off;
7540 int width2 = width1 + curwin_col_off2();
7541
7542 validate_virtcol();
7543 i = width1 - 1;
7544 if (curwin->w_virtcol >= (colnr_T)width1)
7545 i += ((curwin->w_virtcol - width1) / width2 + 1)
7546 * width2;
7547 coladvance((colnr_T)i);
7548#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7549 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7550 {
7551 /*
7552 * Check for landing on a character that got split at
7553 * the end of the line. We do not want to advance to
7554 * the next screen line.
7555 */
7556 validate_virtcol();
7557 if (curwin->w_virtcol > (colnr_T)i)
7558 --curwin->w_cursor.col;
7559 }
7560#endif
7561 }
7562 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7563 clearopbeep(oap);
7564 }
7565 else
7566 {
7567 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7568 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007569
7570 /* Make sure we stick in this column. */
7571 validate_virtcol();
7572 curwin->w_curswant = curwin->w_virtcol;
7573 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 }
7576 break;
7577
7578 /*
7579 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7580 */
7581 case '*':
7582 case '#':
7583#if POUND != '#'
7584 case POUND: /* pound sign (sometimes equal to '#') */
7585#endif
7586 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7587 case ']': /* :tselect for current identifier */
7588 nv_ident(cap);
7589 break;
7590
7591 /*
7592 * ge and gE: go back to end of word
7593 */
7594 case 'e':
7595 case 'E':
7596 oap->motion_type = MCHAR;
7597 curwin->w_set_curswant = TRUE;
7598 oap->inclusive = TRUE;
7599 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7600 clearopbeep(oap);
7601 break;
7602
7603 /*
7604 * "g CTRL-G": display info about cursor position
7605 */
7606 case Ctrl_G:
7607 cursor_pos_info();
7608 break;
7609
7610 /*
7611 * "gi": start Insert at the last position.
7612 */
7613 case 'i':
7614 if (curbuf->b_last_insert.lnum != 0)
7615 {
7616 curwin->w_cursor = curbuf->b_last_insert;
7617 check_cursor_lnum();
7618 i = (int)STRLEN(ml_get_curline());
7619 if (curwin->w_cursor.col > (colnr_T)i)
7620 {
7621#ifdef FEAT_VIRTUALEDIT
7622 if (virtual_active())
7623 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7624#endif
7625 curwin->w_cursor.col = i;
7626 }
7627 }
7628 cap->cmdchar = 'i';
7629 nv_edit(cap);
7630 break;
7631
7632 /*
7633 * "gI": Start insert in column 1.
7634 */
7635 case 'I':
7636 beginline(0);
7637 if (!checkclearopq(oap))
7638 invoke_edit(cap, FALSE, 'g', FALSE);
7639 break;
7640
7641#ifdef FEAT_SEARCHPATH
7642 /*
7643 * "gf": goto file, edit file under cursor
7644 * "]f" and "[f": can also be used.
7645 */
7646 case 'f':
7647 nv_gotofile(cap);
7648 break;
7649#endif
7650
7651 /* "g'm" and "g`m": jump to mark without setting pcmark */
7652 case '\'':
7653 cap->arg = TRUE;
7654 /*FALLTHROUGH*/
7655 case '`':
7656 nv_gomark(cap);
7657 break;
7658
7659 /*
7660 * "gs": Goto sleep.
7661 */
7662 case 's':
7663 do_sleep(cap->count1 * 1000L);
7664 break;
7665
7666 /*
7667 * "ga": Display the ascii value of the character under the
7668 * cursor. It is displayed in decimal, hex, and octal. -- webb
7669 */
7670 case 'a':
7671 do_ascii(NULL);
7672 break;
7673
7674#ifdef FEAT_MBYTE
7675 /*
7676 * "g8": Display the bytes used for the UTF-8 character under the
7677 * cursor. It is displayed in hex.
7678 */
7679 case '8':
7680 show_utf8();
7681 break;
7682#endif
7683
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007684 case '<':
7685 show_sb_text();
7686 break;
7687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 /*
7689 * "gg": Goto the first line in file. With a count it goes to
7690 * that line number like for "G". -- webb
7691 */
7692 case 'g':
7693 cap->arg = FALSE;
7694 nv_goto(cap);
7695 break;
7696
7697 /*
7698 * Two-character operators:
7699 * "gq" Format text
7700 * "gw" Format text and keep cursor position
7701 * "g~" Toggle the case of the text.
7702 * "gu" Change text to lower case.
7703 * "gU" Change text to upper case.
7704 * "g?" rot13 encoding
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00007705 * "gy" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 */
7707 case 'q':
7708 case 'w':
7709 oap->cursor_start = curwin->w_cursor;
7710 /*FALLTHROUGH*/
7711 case '~':
7712 case 'u':
7713 case 'U':
7714 case '?':
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00007715 case 'y':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 nv_operator(cap);
7717 break;
7718
7719 /*
7720 * "gd": Find first occurence of pattern under the cursor in the
7721 * current function
7722 * "gD": idem, but in the current file.
7723 */
7724 case 'd':
7725 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007726 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 break;
7728
7729#ifdef FEAT_MOUSE
7730 /*
7731 * g<*Mouse> : <C-*mouse>
7732 */
7733 case K_MIDDLEMOUSE:
7734 case K_MIDDLEDRAG:
7735 case K_MIDDLERELEASE:
7736 case K_LEFTMOUSE:
7737 case K_LEFTDRAG:
7738 case K_LEFTRELEASE:
7739 case K_RIGHTMOUSE:
7740 case K_RIGHTDRAG:
7741 case K_RIGHTRELEASE:
7742 case K_X1MOUSE:
7743 case K_X1DRAG:
7744 case K_X1RELEASE:
7745 case K_X2MOUSE:
7746 case K_X2DRAG:
7747 case K_X2RELEASE:
7748 mod_mask = MOD_MASK_CTRL;
7749 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7750 break;
7751#endif
7752
7753 case K_IGNORE:
7754 break;
7755
7756 /*
7757 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7758 */
7759 case 'p':
7760 case 'P':
7761 nv_put(cap);
7762 break;
7763
7764#ifdef FEAT_BYTEOFF
7765 /* "go": goto byte count from start of buffer */
7766 case 'o':
7767 goto_byte(cap->count0);
7768 break;
7769#endif
7770
7771 /* "gQ": improved Ex mode */
7772 case 'Q':
7773#ifdef FEAT_CMDWIN
7774 if (cmdwin_type != 0)
7775 {
7776 clearopbeep(cap->oap);
7777 break;
7778 }
7779#endif
7780 if (!checkclearopq(oap))
7781 do_exmode(TRUE);
7782 break;
7783
7784#ifdef FEAT_JUMPLIST
7785 case ',':
7786 nv_pcmark(cap);
7787 break;
7788
7789 case ';':
7790 cap->count1 = -cap->count1;
7791 nv_pcmark(cap);
7792 break;
7793#endif
7794
7795 default:
7796 clearopbeep(oap);
7797 break;
7798 }
7799}
7800
7801/*
7802 * Handle "o" and "O" commands.
7803 */
7804 static void
7805n_opencmd(cap)
7806 cmdarg_T *cap;
7807{
7808 if (!checkclearopq(cap->oap))
7809 {
7810#ifdef FEAT_FOLDING
7811 if (cap->cmdchar == 'O')
7812 /* Open above the first line of a folded sequence of lines */
7813 (void)hasFolding(curwin->w_cursor.lnum,
7814 &curwin->w_cursor.lnum, NULL);
7815 else
7816 /* Open below the last line of a folded sequence of lines */
7817 (void)hasFolding(curwin->w_cursor.lnum,
7818 NULL, &curwin->w_cursor.lnum);
7819#endif
7820 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7821 (cap->cmdchar == 'O' ? 1 : 0)),
7822 (linenr_T)(curwin->w_cursor.lnum +
7823 (cap->cmdchar == 'o' ? 1 : 0))
7824 ) == OK
7825 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7826#ifdef FEAT_COMMENTS
7827 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7828#endif
7829 0, 0))
7830 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007831 /* When '#' is in 'cpoptions' ignore the count. */
7832 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7833 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7835 }
7836 }
7837}
7838
7839/*
7840 * "." command: redo last change.
7841 */
7842 static void
7843nv_dot(cap)
7844 cmdarg_T *cap;
7845{
7846 if (!checkclearopq(cap->oap))
7847 {
7848 /*
7849 * If "restart_edit" is TRUE, the last but one command is repeated
7850 * instead of the last command (inserting text). This is used for
7851 * CTRL-O <.> in insert mode.
7852 */
7853 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7854 clearopbeep(cap->oap);
7855 }
7856}
7857
7858/*
7859 * CTRL-R: undo undo
7860 */
7861 static void
7862nv_redo(cap)
7863 cmdarg_T *cap;
7864{
7865 if (!checkclearopq(cap->oap))
7866 {
7867 u_redo((int)cap->count1);
7868 curwin->w_set_curswant = TRUE;
7869 }
7870}
7871
7872/*
7873 * Handle "U" command.
7874 */
7875 static void
7876nv_Undo(cap)
7877 cmdarg_T *cap;
7878{
7879 /* In Visual mode and typing "gUU" triggers an operator */
7880 if (cap->oap->op_type == OP_UPPER
7881#ifdef FEAT_VISUAL
7882 || VIsual_active
7883#endif
7884 )
7885 {
7886 /* translate "gUU" to "gUgU" */
7887 cap->cmdchar = 'g';
7888 cap->nchar = 'U';
7889 nv_operator(cap);
7890 }
7891 else if (!checkclearopq(cap->oap))
7892 {
7893 u_undoline();
7894 curwin->w_set_curswant = TRUE;
7895 }
7896}
7897
7898/*
7899 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7900 * single character.
7901 */
7902 static void
7903nv_tilde(cap)
7904 cmdarg_T *cap;
7905{
7906 if (!p_to
7907#ifdef FEAT_VISUAL
7908 && !VIsual_active
7909#endif
7910 && cap->oap->op_type != OP_TILDE)
7911 n_swapchar(cap);
7912 else
7913 nv_operator(cap);
7914}
7915
7916/*
7917 * Handle an operator command.
7918 * The actual work is done by do_pending_operator().
7919 */
7920 static void
7921nv_operator(cap)
7922 cmdarg_T *cap;
7923{
7924 int op_type;
7925
7926 op_type = get_op_type(cap->cmdchar, cap->nchar);
7927
7928 if (op_type == cap->oap->op_type) /* double operator works on lines */
7929 nv_lineop(cap);
7930 else if (!checkclearop(cap->oap))
7931 {
7932 cap->oap->start = curwin->w_cursor;
7933 cap->oap->op_type = op_type;
7934 }
7935}
7936
7937/*
7938 * Handle linewise operator "dd", "yy", etc.
7939 *
7940 * "_" is is a strange motion command that helps make operators more logical.
7941 * It is actually implemented, but not documented in the real Vi. This motion
7942 * command actually refers to "the current line". Commands like "dd" and "yy"
7943 * are really an alternate form of "d_" and "y_". It does accept a count, so
7944 * "d3_" works to delete 3 lines.
7945 */
7946 static void
7947nv_lineop(cap)
7948 cmdarg_T *cap;
7949{
7950 cap->oap->motion_type = MLINE;
7951 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7952 clearopbeep(cap->oap);
7953 else if ( cap->oap->op_type == OP_DELETE
7954 || cap->oap->op_type == OP_LSHIFT
7955 || cap->oap->op_type == OP_RSHIFT)
7956 beginline(BL_SOL | BL_FIX);
7957 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7958 beginline(BL_WHITE | BL_FIX);
7959}
7960
7961/*
7962 * <Home> command.
7963 */
7964 static void
7965nv_home(cap)
7966 cmdarg_T *cap;
7967{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007968 /* CTRL-HOME is like "gg" */
7969 if (mod_mask & MOD_MASK_CTRL)
7970 nv_goto(cap);
7971 else
7972 {
7973 cap->count0 = 1;
7974 nv_pipe(cap);
7975 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00007976 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
7977 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978}
7979
7980/*
7981 * "|" command.
7982 */
7983 static void
7984nv_pipe(cap)
7985 cmdarg_T *cap;
7986{
7987 cap->oap->motion_type = MCHAR;
7988 cap->oap->inclusive = FALSE;
7989 beginline(0);
7990 if (cap->count0 > 0)
7991 {
7992 coladvance((colnr_T)(cap->count0 - 1));
7993 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
7994 }
7995 else
7996 curwin->w_curswant = 0;
7997 /* keep curswant at the column where we wanted to go, not where
7998 we ended; differs if line is too short */
7999 curwin->w_set_curswant = FALSE;
8000}
8001
8002/*
8003 * Handle back-word command "b" and "B".
8004 * cap->arg is 1 for "B"
8005 */
8006 static void
8007nv_bck_word(cap)
8008 cmdarg_T *cap;
8009{
8010 cap->oap->motion_type = MCHAR;
8011 cap->oap->inclusive = FALSE;
8012 curwin->w_set_curswant = TRUE;
8013 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8014 clearopbeep(cap->oap);
8015#ifdef FEAT_FOLDING
8016 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8017 foldOpenCursor();
8018#endif
8019}
8020
8021/*
8022 * Handle word motion commands "e", "E", "w" and "W".
8023 * cap->arg is TRUE for "E" and "W".
8024 */
8025 static void
8026nv_wordcmd(cap)
8027 cmdarg_T *cap;
8028{
8029 int n;
8030 int word_end;
8031 int flag = FALSE;
8032
8033 /*
8034 * Set inclusive for the "E" and "e" command.
8035 */
8036 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8037 word_end = TRUE;
8038 else
8039 word_end = FALSE;
8040 cap->oap->inclusive = word_end;
8041
8042 /*
8043 * "cw" and "cW" are a special case.
8044 */
8045 if (!word_end && cap->oap->op_type == OP_CHANGE)
8046 {
8047 n = gchar_cursor();
8048 if (n != NUL) /* not an empty line */
8049 {
8050 if (vim_iswhite(n))
8051 {
8052 /*
8053 * Reproduce a funny Vi behaviour: "cw" on a blank only
8054 * changes one character, not all blanks until the start of
8055 * the next word. Only do this when the 'w' flag is included
8056 * in 'cpoptions'.
8057 */
8058 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8059 {
8060 cap->oap->inclusive = TRUE;
8061 cap->oap->motion_type = MCHAR;
8062 return;
8063 }
8064 }
8065 else
8066 {
8067 /*
8068 * This is a little strange. To match what the real Vi does,
8069 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8070 * that we are not on a space or a TAB. This seems impolite
8071 * at first, but it's really more what we mean when we say
8072 * 'cw'.
8073 * Another strangeness: When standing on the end of a word
8074 * "ce" will change until the end of the next wordt, but "cw"
8075 * will change only one character! This is done by setting
8076 * flag.
8077 */
8078 cap->oap->inclusive = TRUE;
8079 word_end = TRUE;
8080 flag = TRUE;
8081 }
8082 }
8083 }
8084
8085 cap->oap->motion_type = MCHAR;
8086 curwin->w_set_curswant = TRUE;
8087 if (word_end)
8088 n = end_word(cap->count1, cap->arg, flag, FALSE);
8089 else
8090 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8091
8092 /* Don't leave the cursor on the NUL past a line */
8093 if (curwin->w_cursor.col && gchar_cursor() == NUL)
8094 {
8095 --curwin->w_cursor.col;
8096 cap->oap->inclusive = TRUE;
8097 }
8098
8099 if (n == FAIL && cap->oap->op_type == OP_NOP)
8100 clearopbeep(cap->oap);
8101 else
8102 {
8103#ifdef FEAT_VISUAL
8104 adjust_for_sel(cap);
8105#endif
8106#ifdef FEAT_FOLDING
8107 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8108 foldOpenCursor();
8109#endif
8110 }
8111}
8112
8113/*
8114 * "0" and "^" commands.
8115 * cap->arg is the argument for beginline().
8116 */
8117 static void
8118nv_beginline(cap)
8119 cmdarg_T *cap;
8120{
8121 cap->oap->motion_type = MCHAR;
8122 cap->oap->inclusive = FALSE;
8123 beginline(cap->arg);
8124#ifdef FEAT_FOLDING
8125 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8126 foldOpenCursor();
8127#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008128 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8129 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130}
8131
8132#ifdef FEAT_VISUAL
8133/*
8134 * In exclusive Visual mode, may include the last character.
8135 */
8136 static void
8137adjust_for_sel(cap)
8138 cmdarg_T *cap;
8139{
8140 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8141 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8142 {
8143# ifdef FEAT_MBYTE
8144 if (has_mbyte)
8145 inc_cursor();
8146 else
8147# endif
8148 ++curwin->w_cursor.col;
8149 cap->oap->inclusive = FALSE;
8150 }
8151}
8152
8153/*
8154 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8155 * Should check VIsual_mode before calling this.
8156 * Returns TRUE when backed up to the previous line.
8157 */
8158 static int
8159unadjust_for_sel()
8160{
8161 pos_T *pp;
8162
8163 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8164 {
8165 if (lt(VIsual, curwin->w_cursor))
8166 pp = &curwin->w_cursor;
8167 else
8168 pp = &VIsual;
8169#ifdef FEAT_VIRTUALEDIT
8170 if (pp->coladd > 0)
8171 --pp->coladd;
8172 else
8173#endif
8174 if (pp->col > 0)
8175 {
8176 --pp->col;
8177#ifdef FEAT_MBYTE
8178 mb_adjustpos(pp);
8179#endif
8180 }
8181 else if (pp->lnum > 1)
8182 {
8183 --pp->lnum;
8184 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8185 return TRUE;
8186 }
8187 }
8188 return FALSE;
8189}
8190
8191/*
8192 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8193 */
8194 static void
8195nv_select(cap)
8196 cmdarg_T *cap;
8197{
8198 if (VIsual_active)
8199 VIsual_select = TRUE;
8200 else if (VIsual_reselect)
8201 {
8202 cap->nchar = 'v'; /* fake "gv" command */
8203 cap->arg = TRUE;
8204 nv_g_cmd(cap);
8205 }
8206}
8207
8208#endif
8209
8210/*
8211 * "G", "gg", CTRL-END, CTRL-HOME.
8212 * cap->arg is TRUE for "G".
8213 */
8214 static void
8215nv_goto(cap)
8216 cmdarg_T *cap;
8217{
8218 linenr_T lnum;
8219
8220 if (cap->arg)
8221 lnum = curbuf->b_ml.ml_line_count;
8222 else
8223 lnum = 1L;
8224 cap->oap->motion_type = MLINE;
8225 setpcmark();
8226
8227 /* When a count is given, use it instead of the default lnum */
8228 if (cap->count0 != 0)
8229 lnum = cap->count0;
8230 if (lnum < 1L)
8231 lnum = 1L;
8232 else if (lnum > curbuf->b_ml.ml_line_count)
8233 lnum = curbuf->b_ml.ml_line_count;
8234 curwin->w_cursor.lnum = lnum;
8235 beginline(BL_SOL | BL_FIX);
8236#ifdef FEAT_FOLDING
8237 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8238 foldOpenCursor();
8239#endif
8240}
8241
8242/*
8243 * CTRL-\ in Normal mode.
8244 */
8245 static void
8246nv_normal(cap)
8247 cmdarg_T *cap;
8248{
8249 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8250 {
8251 clearop(cap->oap);
8252 if (restart_edit != 0 && p_smd)
8253 clear_cmdline = TRUE; /* unshow mode later */
8254 restart_edit = 0;
8255#ifdef FEAT_CMDWIN
8256 if (cmdwin_type != 0)
8257 cmdwin_result = Ctrl_C;
8258#endif
8259#ifdef FEAT_VISUAL
8260 if (VIsual_active)
8261 {
8262 end_visual_mode(); /* stop Visual */
8263 redraw_curbuf_later(INVERTED);
8264 }
8265#endif
8266 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8267 if (cap->nchar == Ctrl_G && p_im)
8268 restart_edit = 'a';
8269 }
8270 else
8271 clearopbeep(cap->oap);
8272}
8273
8274/*
8275 * ESC in Normal mode: beep, but don't flush buffers.
8276 * Don't even beep if we are canceling a command.
8277 */
8278 static void
8279nv_esc(cap)
8280 cmdarg_T *cap;
8281{
8282 int no_reason;
8283
8284 no_reason = (cap->oap->op_type == OP_NOP
8285 && cap->opcount == 0
8286 && cap->count0 == 0
8287 && cap->oap->regname == 0
8288 && !p_im);
8289
8290 if (cap->arg) /* TRUE for CTRL-C */
8291 {
8292 if (restart_edit == 0
8293#ifdef FEAT_CMDWIN
8294 && cmdwin_type == 0
8295#endif
8296#ifdef FEAT_VISUAL
8297 && !VIsual_active
8298#endif
8299 && no_reason)
8300 MSG(_("Type :quit<Enter> to exit Vim"));
8301
8302 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8303 * set again below when halfway a mapping. */
8304 if (!p_im)
8305 restart_edit = 0;
8306#ifdef FEAT_CMDWIN
8307 if (cmdwin_type != 0)
8308 {
8309 cmdwin_result = K_IGNORE;
8310 got_int = FALSE; /* don't stop executing autocommands et al. */
8311 return;
8312 }
8313#endif
8314 }
8315
8316#ifdef FEAT_VISUAL
8317 if (VIsual_active)
8318 {
8319 end_visual_mode(); /* stop Visual */
8320 check_cursor_col(); /* make sure cursor is not beyond EOL */
8321 curwin->w_set_curswant = TRUE;
8322 redraw_curbuf_later(INVERTED);
8323 }
8324 else
8325#endif
8326 if (no_reason)
8327 vim_beep();
8328 clearop(cap->oap);
8329
8330 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8331 * set return to Insert mode afterwards. */
8332 if (restart_edit == 0 && goto_im()
8333#ifdef FEAT_EX_EXTRA
8334 && ex_normal_busy == 0
8335#endif
8336 )
8337 restart_edit = 'a';
8338}
8339
8340/*
8341 * Handle "A", "a", "I", "i" and <Insert> commands.
8342 */
8343 static void
8344nv_edit(cap)
8345 cmdarg_T *cap;
8346{
8347 /* <Insert> is equal to "i" */
8348 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8349 cap->cmdchar = 'i';
8350
8351#ifdef FEAT_VISUAL
8352 /* in Visual mode "A" and "I" are an operator */
8353 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8354 v_visop(cap);
8355
8356 /* in Visual mode and after an operator "a" and "i" are for text objects */
8357 else
8358#endif
8359 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8360 && (cap->oap->op_type != OP_NOP
8361#ifdef FEAT_VISUAL
8362 || VIsual_active
8363#endif
8364 ))
8365 {
8366#ifdef FEAT_TEXTOBJ
8367 nv_object(cap);
8368#else
8369 clearopbeep(cap->oap);
8370#endif
8371 }
8372 else if (!curbuf->b_p_ma && !p_im)
8373 {
8374 /* Only give this error when 'insertmode' is off. */
8375 EMSG(_(e_modifiable));
8376 clearop(cap->oap);
8377 }
8378 else if (!checkclearopq(cap->oap))
8379 {
8380 switch (cap->cmdchar)
8381 {
8382 case 'A': /* "A"ppend after the line */
8383 curwin->w_set_curswant = TRUE;
8384#ifdef FEAT_VIRTUALEDIT
8385 if (ve_flags == VE_ALL)
8386 {
8387 int save_State = State;
8388
8389 /* Pretent Insert mode here to allow the cursor on the
8390 * character past the end of the line */
8391 State = INSERT;
8392 coladvance((colnr_T)MAXCOL);
8393 State = save_State;
8394 }
8395 else
8396#endif
8397 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8398 break;
8399
8400 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008401 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8402 beginline(BL_WHITE);
8403 else
8404 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 break;
8406
8407 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8408#ifdef FEAT_VIRTUALEDIT
8409 /* increment coladd when in virtual space, increment the
8410 * column otherwise, also to append after an unprintable char */
8411 if (virtual_active()
8412 && (curwin->w_cursor.coladd > 0
8413 || *ml_get_cursor() == NUL
8414 || *ml_get_cursor() == TAB))
8415 curwin->w_cursor.coladd++;
8416 else
8417#endif
8418 if (*ml_get_cursor() != NUL)
8419 inc_cursor();
8420 break;
8421 }
8422
8423#ifdef FEAT_VIRTUALEDIT
8424 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8425 {
8426 int save_State = State;
8427
8428 /* Pretent Insert mode here to allow the cursor on the
8429 * character past the end of the line */
8430 State = INSERT;
8431 coladvance(getviscol());
8432 State = save_State;
8433 }
8434#endif
8435
8436 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8437 }
8438}
8439
8440/*
8441 * Invoke edit() and take care of "restart_edit" and the return value.
8442 */
8443 static void
8444invoke_edit(cap, repl, cmd, startln)
8445 cmdarg_T *cap;
8446 int repl; /* "r" or "gr" command */
8447 int cmd;
8448 int startln;
8449{
8450 int restart_edit_save = 0;
8451
8452 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8453 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8454 * it. */
8455 if (repl || !stuff_empty())
8456 restart_edit_save = restart_edit;
8457 else
8458 restart_edit_save = 0;
8459
8460 /* Always reset "restart_edit", this is not a restarted edit. */
8461 restart_edit = 0;
8462
8463 if (edit(cmd, startln, cap->count1))
8464 cap->retval |= CA_COMMAND_BUSY;
8465
8466 if (restart_edit == 0)
8467 restart_edit = restart_edit_save;
8468}
8469
8470#ifdef FEAT_TEXTOBJ
8471/*
8472 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8473 */
8474 static void
8475nv_object(cap)
8476 cmdarg_T *cap;
8477{
8478 int flag;
8479 int include;
8480 char_u *mps_save;
8481
8482 if (cap->cmdchar == 'i')
8483 include = FALSE; /* "ix" = inner object: exclude white space */
8484 else
8485 include = TRUE; /* "ax" = an object: include white space */
8486
8487 /* Make sure (), [], {} and <> are in 'matchpairs' */
8488 mps_save = curbuf->b_p_mps;
8489 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8490
8491 switch (cap->nchar)
8492 {
8493 case 'w': /* "aw" = a word */
8494 flag = current_word(cap->oap, cap->count1, include, FALSE);
8495 break;
8496 case 'W': /* "aW" = a WORD */
8497 flag = current_word(cap->oap, cap->count1, include, TRUE);
8498 break;
8499 case 'b': /* "ab" = a braces block */
8500 case '(':
8501 case ')':
8502 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8503 break;
8504 case 'B': /* "aB" = a Brackets block */
8505 case '{':
8506 case '}':
8507 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8508 break;
8509 case '[': /* "a[" = a [] block */
8510 case ']':
8511 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8512 break;
8513 case '<': /* "a<" = a <> block */
8514 case '>':
8515 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8516 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008517 case 't': /* "at" = a tag block (xml and html) */
8518 flag = current_tagblock(cap->oap, cap->count1, include);
8519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 case 'p': /* "ap" = a paragraph */
8521 flag = current_par(cap->oap, cap->count1, include, 'p');
8522 break;
8523 case 's': /* "as" = a sentence */
8524 flag = current_sent(cap->oap, cap->count1, include);
8525 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008526 case '"': /* "a"" = a double quoted string */
8527 case '\'': /* "a'" = a single quoted string */
8528 case '`': /* "a`" = a backtick quoted string */
8529 flag = current_quote(cap->oap, cap->count1, include,
8530 cap->nchar);
8531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#if 0 /* TODO */
8533 case 'S': /* "aS" = a section */
8534 case 'f': /* "af" = a filename */
8535 case 'u': /* "au" = a URL */
8536#endif
8537 default:
8538 flag = FAIL;
8539 break;
8540 }
8541
8542 curbuf->b_p_mps = mps_save;
8543 if (flag == FAIL)
8544 clearopbeep(cap->oap);
8545 adjust_cursor_col();
8546 curwin->w_set_curswant = TRUE;
8547}
8548#endif
8549
8550/*
8551 * "q" command: Start/stop recording.
8552 * "q:", "q/", "q?": edit command-line in command-line window.
8553 */
8554 static void
8555nv_record(cap)
8556 cmdarg_T *cap;
8557{
8558 if (cap->oap->op_type == OP_FORMAT)
8559 {
8560 /* "gqq" is the same as "gqgq": format line */
8561 cap->cmdchar = 'g';
8562 cap->nchar = 'q';
8563 nv_operator(cap);
8564 }
8565 else if (!checkclearop(cap->oap))
8566 {
8567#ifdef FEAT_CMDWIN
8568 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8569 {
8570 stuffcharReadbuff(cap->nchar);
8571 stuffcharReadbuff(K_CMDWIN);
8572 }
8573 else
8574#endif
8575 /* (stop) recording into a named register, unless executing a
8576 * register */
8577 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8578 clearopbeep(cap->oap);
8579 }
8580}
8581
8582/*
8583 * Handle the "@r" command.
8584 */
8585 static void
8586nv_at(cap)
8587 cmdarg_T *cap;
8588{
8589 if (checkclearop(cap->oap))
8590 return;
8591#ifdef FEAT_EVAL
8592 if (cap->nchar == '=')
8593 {
8594 if (get_expr_register() == NUL)
8595 return;
8596 }
8597#endif
8598 while (cap->count1-- && !got_int)
8599 {
8600 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8601 {
8602 clearopbeep(cap->oap);
8603 break;
8604 }
8605 line_breakcheck();
8606 }
8607}
8608
8609/*
8610 * Handle the CTRL-U and CTRL-D commands.
8611 */
8612 static void
8613nv_halfpage(cap)
8614 cmdarg_T *cap;
8615{
8616 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8617 || (cap->cmdchar == Ctrl_D
8618 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8619 clearopbeep(cap->oap);
8620 else if (!checkclearop(cap->oap))
8621 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8622}
8623
8624/*
8625 * Handle "J" or "gJ" command.
8626 */
8627 static void
8628nv_join(cap)
8629 cmdarg_T *cap;
8630{
8631#ifdef FEAT_VISUAL
8632 if (VIsual_active) /* join the visual lines */
8633 nv_operator(cap);
8634 else
8635#endif
8636 if (!checkclearop(cap->oap))
8637 {
8638 if (cap->count0 <= 1)
8639 cap->count0 = 2; /* default for join is two lines! */
8640 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8641 curbuf->b_ml.ml_line_count)
8642 clearopbeep(cap->oap); /* beyond last line */
8643 else
8644 {
8645 prep_redo(cap->oap->regname, cap->count0,
8646 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8647 do_do_join(cap->count0, cap->nchar == NUL);
8648 }
8649 }
8650}
8651
8652/*
8653 * "P", "gP", "p" and "gp" commands.
8654 */
8655 static void
8656nv_put(cap)
8657 cmdarg_T *cap;
8658{
8659#ifdef FEAT_VISUAL
8660 int regname = 0;
8661 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008662 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008663 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664#endif
8665 int dir;
8666 int flags = 0;
8667
8668 if (cap->oap->op_type != OP_NOP)
8669 {
8670#ifdef FEAT_DIFF
8671 /* "dp" is ":diffput" */
8672 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8673 {
8674 clearop(cap->oap);
8675 nv_diffgetput(TRUE);
8676 }
8677 else
8678#endif
8679 clearopbeep(cap->oap);
8680 }
8681 else
8682 {
8683 dir = (cap->cmdchar == 'P'
8684 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8685 ? BACKWARD : FORWARD;
8686 prep_redo_cmd(cap);
8687 if (cap->cmdchar == 'g')
8688 flags |= PUT_CURSEND;
8689
8690#ifdef FEAT_VISUAL
8691 if (VIsual_active)
8692 {
8693 /* Putting in Visual mode: The put text replaces the selected
8694 * text. First delete the selected text, then put the new text.
8695 * Need to save and restore the registers that the delete
8696 * overwrites if the old contents is being put.
8697 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008698 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 regname = cap->oap->regname;
8700# ifdef FEAT_CLIPBOARD
8701 adjust_clip_reg(&regname);
8702# endif
8703 if (regname == 0 || VIM_ISDIGIT(regname)
8704# ifdef FEAT_CLIPBOARD
8705 || (clip_unnamed && (regname == '*' || regname == '+'))
8706# endif
8707
8708 )
8709 {
8710 /* the delete is going to overwrite the register we want to
8711 * put, save it first. */
8712 reg1 = get_register(regname, TRUE);
8713 }
8714
8715 /* Now delete the selected text. */
8716 cap->cmdchar = 'd';
8717 cap->nchar = NUL;
8718 cap->oap->regname = NUL;
8719 nv_operator(cap);
8720 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008721 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 /* delete PUT_LINE_BACKWARD; */
8724 cap->oap->regname = regname;
8725
8726 if (reg1 != NULL)
8727 {
8728 /* Delete probably changed the register we want to put, save
8729 * it first. Then put back what was there before the delete. */
8730 reg2 = get_register(regname, FALSE);
8731 put_register(regname, reg1);
8732 }
8733
8734 /* When deleted a linewise Visual area, put the register as
8735 * lines to avoid it joined with the next line. When deletion was
8736 * characterwise, split a line when putting lines. */
8737 if (VIsual_mode == 'V')
8738 flags |= PUT_LINE;
8739 else if (VIsual_mode == 'v')
8740 flags |= PUT_LINE_SPLIT;
8741 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8742 flags |= PUT_LINE_FORWARD;
8743 dir = BACKWARD;
8744 if ((VIsual_mode != 'V'
8745 && curwin->w_cursor.col < curbuf->b_op_start.col)
8746 || (VIsual_mode == 'V'
8747 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8748 /* cursor is at the end of the line or end of file, put
8749 * forward. */
8750 dir = FORWARD;
8751 }
8752#endif
8753 do_put(cap->oap->regname, dir, cap->count1, flags);
8754
8755#ifdef FEAT_VISUAL
8756 /* If a register was saved, put it back now. */
8757 if (reg2 != NULL)
8758 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008759
8760 /* What to reselect with "gv"? Selecting the just put text seems to
8761 * be the most useful, since the original text was removed. */
8762 if (was_visual)
8763 {
8764 curbuf->b_visual_start = curbuf->b_op_start;
8765 curbuf->b_visual_end = curbuf->b_op_end;
8766 }
8767
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008768 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008769 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008770 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8771 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772#endif
8773 auto_format(FALSE, TRUE);
8774 }
8775}
8776
8777/*
8778 * "o" and "O" commands.
8779 */
8780 static void
8781nv_open(cap)
8782 cmdarg_T *cap;
8783{
8784#ifdef FEAT_DIFF
8785 /* "do" is ":diffget" */
8786 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8787 {
8788 clearop(cap->oap);
8789 nv_diffgetput(FALSE);
8790 }
8791 else
8792#endif
8793#ifdef FEAT_VISUAL
8794 if (VIsual_active) /* switch start and end of visual */
8795 v_swap_corners(cap->cmdchar);
8796 else
8797#endif
8798 n_opencmd(cap);
8799}
8800
8801#ifdef FEAT_SNIFF
8802/*ARGSUSED*/
8803 static void
8804nv_sniff(cap)
8805 cmdarg_T *cap;
8806{
8807 ProcessSniffRequests();
8808}
8809#endif
8810
8811#ifdef FEAT_NETBEANS_INTG
8812 static void
8813nv_nbcmd(cap)
8814 cmdarg_T *cap;
8815{
8816 netbeans_keycommand(cap->nchar);
8817}
8818#endif
8819
8820#ifdef FEAT_DND
8821/*ARGSUSED*/
8822 static void
8823nv_drop(cap)
8824 cmdarg_T *cap;
8825{
8826 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8827}
8828#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008829
8830#ifdef FEAT_AUTOCMD
8831/*
8832 * Trigger CursorHold event.
8833 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8834 * input buffer. "did_cursorhold" is set to avoid retriggering.
8835 */
8836/*ARGSUSED*/
8837 static void
8838nv_cursorhold(cap)
8839 cmdarg_T *cap;
8840{
8841 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8842 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00008843 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008844}
8845#endif