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