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