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