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