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