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