blob: e866250e92b200bfc77635ae3ea9bcfe3b1c4418 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
23static colnr_T resel_VIsual_col; /* nr of cols or end col */
24
25static int restart_VIsual_select = 0;
26#endif
27
28static int
29# ifdef __BORLANDC__
30_RTLENTRYF
31# endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33static int find_command __ARGS((int cmdchar));
34static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000035static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37static void find_start_of_word __ARGS((pos_T *));
38static void find_end_of_word __ARGS((pos_T *));
39static int get_mouse_class __ARGS((char_u *p));
40#endif
41static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43static int checkclearop __ARGS((oparg_T *oap));
44static int checkclearopq __ARGS((oparg_T *oap));
45static void clearop __ARGS((oparg_T *oap));
46static void clearopbeep __ARGS((oparg_T *oap));
47#ifdef FEAT_VISUAL
48static void unshift_special __ARGS((cmdarg_T *cap));
49#endif
50#ifdef FEAT_CMDL_INFO
51static void del_from_showcmd __ARGS((int));
52#endif
53
54/*
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
58 */
59static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000060static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void nv_error __ARGS((cmdarg_T *cap));
62static void nv_help __ARGS((cmdarg_T *cap));
63static void nv_addsub __ARGS((cmdarg_T *cap));
64static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000065static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67#ifdef FEAT_MOUSE
68static void nv_mousescroll __ARGS((cmdarg_T *cap));
69static void nv_mouse __ARGS((cmdarg_T *cap));
70#endif
71static void nv_scroll_line __ARGS((cmdarg_T *cap));
72static void nv_zet __ARGS((cmdarg_T *cap));
73#ifdef FEAT_GUI
74static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76#endif
77static void nv_exmode __ARGS((cmdarg_T *cap));
78static void nv_colon __ARGS((cmdarg_T *cap));
79static void nv_ctrlg __ARGS((cmdarg_T *cap));
80static void nv_ctrlh __ARGS((cmdarg_T *cap));
81static void nv_clear __ARGS((cmdarg_T *cap));
82static void nv_ctrlo __ARGS((cmdarg_T *cap));
83static void nv_hat __ARGS((cmdarg_T *cap));
84static void nv_Zet __ARGS((cmdarg_T *cap));
85static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000086static void nv_tagpop __ARGS((cmdarg_T *cap));
87static void nv_scroll __ARGS((cmdarg_T *cap));
88static void nv_right __ARGS((cmdarg_T *cap));
89static void nv_left __ARGS((cmdarg_T *cap));
90static void nv_up __ARGS((cmdarg_T *cap));
91static void nv_down __ARGS((cmdarg_T *cap));
92#ifdef FEAT_SEARCHPATH
93static void nv_gotofile __ARGS((cmdarg_T *cap));
94#endif
95static void nv_end __ARGS((cmdarg_T *cap));
96static void nv_dollar __ARGS((cmdarg_T *cap));
97static void nv_search __ARGS((cmdarg_T *cap));
98static void nv_next __ARGS((cmdarg_T *cap));
99static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
100static void nv_csearch __ARGS((cmdarg_T *cap));
101static void nv_brackets __ARGS((cmdarg_T *cap));
102static void nv_percent __ARGS((cmdarg_T *cap));
103static void nv_brace __ARGS((cmdarg_T *cap));
104static void nv_mark __ARGS((cmdarg_T *cap));
105static void nv_findpar __ARGS((cmdarg_T *cap));
106static void nv_undo __ARGS((cmdarg_T *cap));
107static void nv_kundo __ARGS((cmdarg_T *cap));
108static void nv_Replace __ARGS((cmdarg_T *cap));
109#ifdef FEAT_VREPLACE
110static void nv_vreplace __ARGS((cmdarg_T *cap));
111#endif
112#ifdef FEAT_VISUAL
113static void v_swap_corners __ARGS((int cmdchar));
114#endif
115static void nv_replace __ARGS((cmdarg_T *cap));
116static void n_swapchar __ARGS((cmdarg_T *cap));
117static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
118#ifdef FEAT_VISUAL
119static void v_visop __ARGS((cmdarg_T *cap));
120#endif
121static void nv_subst __ARGS((cmdarg_T *cap));
122static void nv_abbrev __ARGS((cmdarg_T *cap));
123static void nv_optrans __ARGS((cmdarg_T *cap));
124static void nv_gomark __ARGS((cmdarg_T *cap));
125static void nv_pcmark __ARGS((cmdarg_T *cap));
126static void nv_regname __ARGS((cmdarg_T *cap));
127#ifdef FEAT_VISUAL
128static void nv_visual __ARGS((cmdarg_T *cap));
129static void n_start_visual_mode __ARGS((int c));
130#endif
131static void nv_window __ARGS((cmdarg_T *cap));
132static void nv_suspend __ARGS((cmdarg_T *cap));
133static void nv_g_cmd __ARGS((cmdarg_T *cap));
134static void n_opencmd __ARGS((cmdarg_T *cap));
135static void nv_dot __ARGS((cmdarg_T *cap));
136static void nv_redo __ARGS((cmdarg_T *cap));
137static void nv_Undo __ARGS((cmdarg_T *cap));
138static void nv_tilde __ARGS((cmdarg_T *cap));
139static void nv_operator __ARGS((cmdarg_T *cap));
140static void nv_lineop __ARGS((cmdarg_T *cap));
141static void nv_home __ARGS((cmdarg_T *cap));
142static void nv_pipe __ARGS((cmdarg_T *cap));
143static void nv_bck_word __ARGS((cmdarg_T *cap));
144static void nv_wordcmd __ARGS((cmdarg_T *cap));
145static void nv_beginline __ARGS((cmdarg_T *cap));
146#ifdef FEAT_VISUAL
147static void adjust_for_sel __ARGS((cmdarg_T *cap));
148static int unadjust_for_sel __ARGS((void));
149static void nv_select __ARGS((cmdarg_T *cap));
150#endif
151static void nv_goto __ARGS((cmdarg_T *cap));
152static void nv_normal __ARGS((cmdarg_T *cap));
153static void nv_esc __ARGS((cmdarg_T *oap));
154static void nv_edit __ARGS((cmdarg_T *cap));
155static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
156#ifdef FEAT_TEXTOBJ
157static void nv_object __ARGS((cmdarg_T *cap));
158#endif
159static void nv_record __ARGS((cmdarg_T *cap));
160static void nv_at __ARGS((cmdarg_T *cap));
161static void nv_halfpage __ARGS((cmdarg_T *cap));
162static void nv_join __ARGS((cmdarg_T *cap));
163static void nv_put __ARGS((cmdarg_T *cap));
164static void nv_open __ARGS((cmdarg_T *cap));
165#ifdef FEAT_SNIFF
166static void nv_sniff __ARGS((cmdarg_T *cap));
167#endif
168#ifdef FEAT_NETBEANS_INTG
169static void nv_nbcmd __ARGS((cmdarg_T *cap));
170#endif
171#ifdef FEAT_DND
172static void nv_drop __ARGS((cmdarg_T *cap));
173#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#ifdef FEAT_AUTOCMD
175static void nv_cursorhold __ARGS((cmdarg_T *cap));
176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178/*
179 * Function to be called for a Normal or Visual mode command.
180 * The argument is a cmdarg_T.
181 */
182typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
183
184/* Values for cmd_flags. */
185#define NV_NCH 0x01 /* may need to get a second char */
186#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
187#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
188#define NV_LANG 0x08 /* second char needs language adjustment */
189
190#define NV_SS 0x10 /* may start selection */
191#define NV_SSS 0x20 /* may start selection with shift modifier */
192#define NV_STS 0x40 /* may stop selection without shift modif. */
193#define NV_RL 0x80 /* 'rightleft' modifies command */
194#define NV_KEEPREG 0x100 /* don't clear regname */
195#define NV_NCW 0x200 /* not allowed in command-line window */
196
197/*
198 * Generally speaking, every Normal mode command should either clear any
199 * pending operator (with *clearop*()), or set the motion type variable
200 * oap->motion_type.
201 *
202 * When a cursor motion command is made, it is marked as being a character or
203 * line oriented motion. Then, if an operator is in effect, the operation
204 * becomes character or line oriented accordingly.
205 */
206
207/*
208 * This table contains one entry for every Normal or Visual mode command.
209 * The order doesn't matter, init_normal_cmds() will create a sorted index.
210 * It is faster when all keys from zero to '~' are present.
211 */
212static const struct nv_cmd
213{
214 int cmd_char; /* (first) command character */
215 nv_func_T cmd_func; /* function for this command */
216 short_u cmd_flags; /* NV_ flags */
217 short cmd_arg; /* value for ca.arg */
218} nv_cmds[] =
219{
220 {NUL, nv_error, 0, 0},
221 {Ctrl_A, nv_addsub, 0, 0},
222 {Ctrl_B, nv_page, NV_STS, BACKWARD},
223 {Ctrl_C, nv_esc, 0, TRUE},
224 {Ctrl_D, nv_halfpage, 0, 0},
225 {Ctrl_E, nv_scroll_line, 0, TRUE},
226 {Ctrl_F, nv_page, NV_STS, FORWARD},
227 {Ctrl_G, nv_ctrlg, 0, 0},
228 {Ctrl_H, nv_ctrlh, 0, 0},
229 {Ctrl_I, nv_pcmark, 0, 0},
230 {NL, nv_down, 0, FALSE},
231 {Ctrl_K, nv_error, 0, 0},
232 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000233 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 {Ctrl_N, nv_down, NV_STS, FALSE},
235 {Ctrl_O, nv_ctrlo, 0, 0},
236 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000237#ifdef FEAT_VISUAL
238 {Ctrl_Q, nv_visual, 0, FALSE},
239#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {Ctrl_R, nv_redo, 0, 0},
243 {Ctrl_S, nv_ignore, 0, 0},
244 {Ctrl_T, nv_tagpop, NV_NCW, 0},
245 {Ctrl_U, nv_halfpage, 0, 0},
246#ifdef FEAT_VISUAL
247 {Ctrl_V, nv_visual, 0, FALSE},
248 {'V', nv_visual, 0, FALSE},
249 {'v', nv_visual, 0, FALSE},
250#else
251 {Ctrl_V, nv_error, 0, 0},
252 {'V', nv_error, 0, 0},
253 {'v', nv_error, 0, 0},
254#endif
255 {Ctrl_W, nv_window, 0, 0},
256 {Ctrl_X, nv_addsub, 0, 0},
257 {Ctrl_Y, nv_scroll_line, 0, FALSE},
258 {Ctrl_Z, nv_suspend, 0, 0},
259 {ESC, nv_esc, 0, FALSE},
260 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
261 {Ctrl_RSB, nv_ident, NV_NCW, 0},
262 {Ctrl_HAT, nv_hat, NV_NCW, 0},
263 {Ctrl__, nv_error, 0, 0},
264 {' ', nv_right, 0, 0},
265 {'!', nv_operator, 0, 0},
266 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
267 {'#', nv_ident, 0, 0},
268 {'$', nv_dollar, 0, 0},
269 {'%', nv_percent, 0, 0},
270 {'&', nv_optrans, 0, 0},
271 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
272 {'(', nv_brace, 0, BACKWARD},
273 {')', nv_brace, 0, FORWARD},
274 {'*', nv_ident, 0, 0},
275 {'+', nv_down, 0, TRUE},
276 {',', nv_csearch, 0, TRUE},
277 {'-', nv_up, 0, TRUE},
278 {'.', nv_dot, NV_KEEPREG, 0},
279 {'/', nv_search, 0, FALSE},
280 {'0', nv_beginline, 0, 0},
281 {'1', nv_ignore, 0, 0},
282 {'2', nv_ignore, 0, 0},
283 {'3', nv_ignore, 0, 0},
284 {'4', nv_ignore, 0, 0},
285 {'5', nv_ignore, 0, 0},
286 {'6', nv_ignore, 0, 0},
287 {'7', nv_ignore, 0, 0},
288 {'8', nv_ignore, 0, 0},
289 {'9', nv_ignore, 0, 0},
290 {':', nv_colon, 0, 0},
291 {';', nv_csearch, 0, FALSE},
292 {'<', nv_operator, NV_RL, 0},
293 {'=', nv_operator, 0, 0},
294 {'>', nv_operator, NV_RL, 0},
295 {'?', nv_search, 0, FALSE},
296 {'@', nv_at, NV_NCH_NOP, FALSE},
297 {'A', nv_edit, 0, 0},
298 {'B', nv_bck_word, 0, 1},
299 {'C', nv_abbrev, NV_KEEPREG, 0},
300 {'D', nv_abbrev, NV_KEEPREG, 0},
301 {'E', nv_wordcmd, 0, TRUE},
302 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
303 {'G', nv_goto, 0, TRUE},
304 {'H', nv_scroll, 0, 0},
305 {'I', nv_edit, 0, 0},
306 {'J', nv_join, 0, 0},
307 {'K', nv_ident, 0, 0},
308 {'L', nv_scroll, 0, 0},
309 {'M', nv_scroll, 0, 0},
310 {'N', nv_next, 0, SEARCH_REV},
311 {'O', nv_open, 0, 0},
312 {'P', nv_put, 0, 0},
313 {'Q', nv_exmode, NV_NCW, 0},
314 {'R', nv_Replace, 0, FALSE},
315 {'S', nv_subst, NV_KEEPREG, 0},
316 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
317 {'U', nv_Undo, 0, 0},
318 {'W', nv_wordcmd, 0, TRUE},
319 {'X', nv_abbrev, NV_KEEPREG, 0},
320 {'Y', nv_abbrev, NV_KEEPREG, 0},
321 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
322 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
323 {'\\', nv_error, 0, 0},
324 {']', nv_brackets, NV_NCH_ALW, FORWARD},
325 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
326 {'_', nv_lineop, 0, 0},
327 {'`', nv_gomark, NV_NCH_ALW, FALSE},
328 {'a', nv_edit, NV_NCH, 0},
329 {'b', nv_bck_word, 0, 0},
330 {'c', nv_operator, 0, 0},
331 {'d', nv_operator, 0, 0},
332 {'e', nv_wordcmd, 0, FALSE},
333 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
334 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
335 {'h', nv_left, NV_RL, 0},
336 {'i', nv_edit, NV_NCH, 0},
337 {'j', nv_down, 0, FALSE},
338 {'k', nv_up, 0, FALSE},
339 {'l', nv_right, NV_RL, 0},
340 {'m', nv_mark, NV_NCH_NOP, 0},
341 {'n', nv_next, 0, 0},
342 {'o', nv_open, 0, 0},
343 {'p', nv_put, 0, 0},
344 {'q', nv_record, NV_NCH, 0},
345 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
346 {'s', nv_subst, NV_KEEPREG, 0},
347 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
348 {'u', nv_undo, 0, 0},
349 {'w', nv_wordcmd, 0, FALSE},
350 {'x', nv_abbrev, NV_KEEPREG, 0},
351 {'y', nv_operator, 0, 0},
352 {'z', nv_zet, NV_NCH_ALW, 0},
353 {'{', nv_findpar, 0, BACKWARD},
354 {'|', nv_pipe, 0, 0},
355 {'}', nv_findpar, 0, FORWARD},
356 {'~', nv_tilde, 0, 0},
357
358 /* pound sign */
359 {POUND, nv_ident, 0, 0},
360#ifdef FEAT_MOUSE
361 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
362 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
363 {K_LEFTMOUSE, nv_mouse, 0, 0},
364 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
365 {K_LEFTDRAG, nv_mouse, 0, 0},
366 {K_LEFTRELEASE, nv_mouse, 0, 0},
367 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
368 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
369 {K_MIDDLEDRAG, nv_mouse, 0, 0},
370 {K_MIDDLERELEASE, nv_mouse, 0, 0},
371 {K_RIGHTMOUSE, nv_mouse, 0, 0},
372 {K_RIGHTDRAG, nv_mouse, 0, 0},
373 {K_RIGHTRELEASE, nv_mouse, 0, 0},
374 {K_X1MOUSE, nv_mouse, 0, 0},
375 {K_X1DRAG, nv_mouse, 0, 0},
376 {K_X1RELEASE, nv_mouse, 0, 0},
377 {K_X2MOUSE, nv_mouse, 0, 0},
378 {K_X2DRAG, nv_mouse, 0, 0},
379 {K_X2RELEASE, nv_mouse, 0, 0},
380#endif
381 {K_IGNORE, nv_ignore, 0, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000382 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_INS, nv_edit, 0, 0},
384 {K_KINS, nv_edit, 0, 0},
385 {K_BS, nv_ctrlh, 0, 0},
386 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
387 {K_S_UP, nv_page, NV_SS, BACKWARD},
388 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
389 {K_S_DOWN, nv_page, NV_SS, FORWARD},
390 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
391 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
392 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
393 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
394 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
395 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
396 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
397 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
398 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
399 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
400 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
401 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {K_S_END, nv_end, NV_SS, FALSE},
403 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
404 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
405 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {K_S_HOME, nv_home, NV_SS, 0},
407 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
408 {K_DEL, nv_abbrev, 0, 0},
409 {K_KDEL, nv_abbrev, 0, 0},
410 {K_UNDO, nv_kundo, 0, 0},
411 {K_HELP, nv_help, NV_NCW, 0},
412 {K_F1, nv_help, NV_NCW, 0},
413 {K_XF1, nv_help, NV_NCW, 0},
414#ifdef FEAT_VISUAL
415 {K_SELECT, nv_select, 0, 0},
416#endif
417#ifdef FEAT_GUI
418 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
419 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
420#endif
421#ifdef FEAT_FKMAP
422 {K_F8, farsi_fkey, 0, 0},
423 {K_F9, farsi_fkey, 0, 0},
424#endif
425#ifdef FEAT_SNIFF
426 {K_SNIFF, nv_sniff, 0, 0},
427#endif
428#ifdef FEAT_NETBEANS_INTG
429 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
430#endif
431#ifdef FEAT_DND
432 {K_DROP, nv_drop, NV_STS, 0},
433#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000434#ifdef FEAT_AUTOCMD
435 {K_CURSORHOLD, nv_cursorhold, 0, 0},
436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437};
438
439/* Number of commands in nv_cmds[]. */
440#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
441
442/* Sorted index of commands in nv_cmds[]. */
443static short nv_cmd_idx[NV_CMDS_SIZE];
444
445/* The highest index for which
446 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
447static int nv_max_linear;
448
449/*
450 * Compare functions for qsort() below, that checks the command character
451 * through the index in nv_cmd_idx[].
452 */
453 static int
454#ifdef __BORLANDC__
455_RTLENTRYF
456#endif
457nv_compare(s1, s2)
458 const void *s1;
459 const void *s2;
460{
461 int c1, c2;
462
463 /* The commands are sorted on absolute value. */
464 c1 = nv_cmds[*(const short *)s1].cmd_char;
465 c2 = nv_cmds[*(const short *)s2].cmd_char;
466 if (c1 < 0)
467 c1 = -c1;
468 if (c2 < 0)
469 c2 = -c2;
470 return c1 - c2;
471}
472
473/*
474 * Initialize the nv_cmd_idx[] table.
475 */
476 void
477init_normal_cmds()
478{
479 int i;
480
481 /* Fill the index table with a one to one relation. */
482 for (i = 0; i < NV_CMDS_SIZE; ++i)
483 nv_cmd_idx[i] = i;
484
485 /* Sort the commands by the command character. */
486 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
487
488 /* Find the first entry that can't be indexed by the command character. */
489 for (i = 0; i < NV_CMDS_SIZE; ++i)
490 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
491 break;
492 nv_max_linear = i - 1;
493}
494
495/*
496 * Search for a command in the commands table.
497 * Returns -1 for invalid command.
498 */
499 static int
500find_command(cmdchar)
501 int cmdchar;
502{
503 int i;
504 int idx;
505 int top, bot;
506 int c;
507
508#ifdef FEAT_MBYTE
509 /* A multi-byte character is never a command. */
510 if (cmdchar >= 0x100)
511 return -1;
512#endif
513
514 /* We use the absolute value of the character. Special keys have a
515 * negative value, but are sorted on their absolute value. */
516 if (cmdchar < 0)
517 cmdchar = -cmdchar;
518
519 /* If the character is in the first part: The character is the index into
520 * nv_cmd_idx[]. */
521 if (cmdchar <= nv_max_linear)
522 return nv_cmd_idx[cmdchar];
523
524 /* Perform a binary search. */
525 bot = nv_max_linear + 1;
526 top = NV_CMDS_SIZE - 1;
527 idx = -1;
528 while (bot <= top)
529 {
530 i = (top + bot) / 2;
531 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
532 if (c < 0)
533 c = -c;
534 if (cmdchar == c)
535 {
536 idx = nv_cmd_idx[i];
537 break;
538 }
539 if (cmdchar > c)
540 bot = i + 1;
541 else
542 top = i - 1;
543 }
544 return idx;
545}
546
547/*
548 * Execute a command in Normal mode.
549 */
550/*ARGSUSED*/
551 void
552normal_cmd(oap, toplevel)
553 oparg_T *oap;
554 int toplevel; /* TRUE when called from main() */
555{
556 static long opcount = 0; /* ca.opcount saved here */
557 cmdarg_T ca; /* command arguments */
558 int c;
559 int ctrl_w = FALSE; /* got CTRL-W command */
560 int old_col = curwin->w_curswant;
561#ifdef FEAT_CMDL_INFO
562 int need_flushbuf; /* need to call out_flush() */
563#endif
564#ifdef FEAT_VISUAL
565 pos_T old_pos; /* cursor position before command */
566 int mapped_len;
567#endif
568 static int old_mapped_len = 0;
569 int idx;
570
571 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
572 ca.oap = oap;
573 ca.opcount = opcount;
574
575#ifdef FEAT_SNIFF
576 want_sniff_request = sniff_connected;
577#endif
578
579 /*
580 * If there is an operator pending, then the command we take this time
581 * will terminate it. Finish_op tells us to finish the operation before
582 * returning this time (unless the operation was cancelled).
583 */
584#ifdef CURSOR_SHAPE
585 c = finish_op;
586#endif
587 finish_op = (oap->op_type != OP_NOP);
588#ifdef CURSOR_SHAPE
589 if (finish_op != c)
590 {
591 ui_cursor_shape(); /* may show different cursor shape */
592# ifdef FEAT_MOUSESHAPE
593 update_mouseshape(-1);
594# endif
595 }
596#endif
597
598 if (!finish_op && !oap->regname)
599 ca.opcount = 0;
600
601#ifdef FEAT_VISUAL
602 mapped_len = typebuf_maplen();
603#endif
604
605 State = NORMAL_BUSY;
606#ifdef USE_ON_FLY_SCROLL
607 dont_scroll = FALSE; /* allow scrolling here */
608#endif
609
610 /*
611 * Get the command character from the user.
612 */
613 c = safe_vgetc();
614
615#ifdef FEAT_LANGMAP
616 LANGMAP_ADJUST(c, TRUE);
617#endif
618
619 /*
620 * If a mapping was started in Visual or Select mode, remember the length
621 * of the mapping. This is used below to not return to Insert mode for as
622 * long as the mapping is being executed.
623 */
624 if (restart_edit == 0)
625 old_mapped_len = 0;
626 else if (old_mapped_len
627#ifdef FEAT_VISUAL
628 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0)
629#endif
630 )
631 old_mapped_len = typebuf_maplen();
632
633 if (c == NUL)
634 c = K_ZERO;
635
636#ifdef FEAT_VISUAL
637 /*
638 * In Select mode, typed text replaces the selection.
639 */
640 if (VIsual_active
641 && VIsual_select
642 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
643 {
644# ifdef FEAT_MBYTE
645 char_u buf[MB_MAXBYTES + 1];
646
647 buf[(*mb_char2bytes)(c, buf)] = NUL;
648# else
649 char_u buf[2];
650
651 buf[0] = c;
652 buf[1] = NUL;
653# endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000654 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
655 * 'insertmode' is set) fake a "d"elete command, Insert mode will
656 * restart automatically.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 * Insert the typed character in the typeahead buffer, so that it will
658 * be mapped in Insert mode. Required for ":lmap" to work. May cause
659 * mapping a character from ":vnoremap"... */
660 (void)ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000661 if (restart_edit != 0)
662 c = 'd';
663 else
664 c = 'c';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666#endif
667
668#ifdef FEAT_CMDL_INFO
669 need_flushbuf = add_to_showcmd(c);
670#endif
671
672getcount:
673#ifdef FEAT_VISUAL
674 if (!(VIsual_active && VIsual_select))
675#endif
676 {
677 /*
678 * Handle a count before a command and compute ca.count0.
679 * Note that '0' is a command and not the start of a count, but it's
680 * part of a count after other digits.
681 */
682 while ( (c >= '1' && c <= '9')
683 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
684 {
685 if (c == K_DEL || c == K_KDEL)
686 {
687 ca.count0 /= 10;
688#ifdef FEAT_CMDL_INFO
689 del_from_showcmd(4); /* delete the digit and ~@% */
690#endif
691 }
692 else
693 ca.count0 = ca.count0 * 10 + (c - '0');
694 if (ca.count0 < 0) /* got too large! */
695 ca.count0 = 999999999L;
696 if (ctrl_w)
697 {
698 ++no_mapping;
699 ++allow_keys; /* no mapping for nchar, but keys */
700 }
701 ++no_zero_mapping; /* don't map zero here */
702 c = safe_vgetc();
703#ifdef FEAT_LANGMAP
704 LANGMAP_ADJUST(c, TRUE);
705#endif
706 --no_zero_mapping;
707 if (ctrl_w)
708 {
709 --no_mapping;
710 --allow_keys;
711 }
712#ifdef FEAT_CMDL_INFO
713 need_flushbuf |= add_to_showcmd(c);
714#endif
715 }
716
717 /*
718 * If we got CTRL-W there may be a/another count
719 */
720 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
721 {
722 ctrl_w = TRUE;
723 ca.opcount = ca.count0; /* remember first count */
724 ca.count0 = 0;
725 ++no_mapping;
726 ++allow_keys; /* no mapping for nchar, but keys */
727 c = safe_vgetc(); /* get next character */
728#ifdef FEAT_LANGMAP
729 LANGMAP_ADJUST(c, TRUE);
730#endif
731 --no_mapping;
732 --allow_keys;
733#ifdef FEAT_CMDL_INFO
734 need_flushbuf |= add_to_showcmd(c);
735#endif
736 goto getcount; /* jump back */
737 }
738 }
739
740 /*
741 * If we're in the middle of an operator (including after entering a yank
742 * buffer with '"') AND we had a count before the operator, then that
743 * count overrides the current value of ca.count0.
744 * What this means effectively, is that commands like "3dw" get turned
745 * into "d3w" which makes things fall into place pretty neatly.
746 * If you give a count before AND after the operator, they are multiplied.
747 */
748 if (ca.opcount != 0)
749 {
750 if (ca.count0)
751 ca.count0 *= ca.opcount;
752 else
753 ca.count0 = ca.opcount;
754 }
755
756 /*
757 * Always remember the count. It will be set to zero (on the next call,
758 * above) when there is no pending operator.
759 * When called from main(), save the count for use by the "count" built-in
760 * variable.
761 */
762 ca.opcount = ca.count0;
763 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
764
765#ifdef FEAT_EVAL
766 /*
767 * Only set v:count when called from main() and not a stuffed command.
768 */
769 if (toplevel && stuff_empty())
770 set_vcount(ca.count0, ca.count1);
771#endif
772
773 /*
774 * Find the command character in the table of commands.
775 * For CTRL-W we already got nchar when looking for a count.
776 */
777 if (ctrl_w)
778 {
779 ca.nchar = c;
780 ca.cmdchar = Ctrl_W;
781 }
782 else
783 ca.cmdchar = c;
784 idx = find_command(ca.cmdchar);
785 if (idx < 0)
786 {
787 /* Not a known command: beep. */
788 clearopbeep(oap);
789 goto normal_end;
790 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000791
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000792 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000794 /* This command is not allowed wile editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000796 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 goto normal_end;
798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800#ifdef FEAT_VISUAL
801 /*
802 * In Visual/Select mode, a few keys are handled in a special way.
803 */
804 if (VIsual_active)
805 {
806 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
807 if (km_stopsel
808 && (nv_cmds[idx].cmd_flags & NV_STS)
809 && !(mod_mask & MOD_MASK_SHIFT))
810 {
811 end_visual_mode();
812 redraw_curbuf_later(INVERTED);
813 }
814
815 /* Keys that work different when 'keymodel' contains "startsel" */
816 if (km_startsel)
817 {
818 if (nv_cmds[idx].cmd_flags & NV_SS)
819 {
820 unshift_special(&ca);
821 idx = find_command(ca.cmdchar);
822 }
823 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
824 && (mod_mask & MOD_MASK_SHIFT))
825 {
826 mod_mask &= ~MOD_MASK_SHIFT;
827 }
828 }
829 }
830#endif
831
832#ifdef FEAT_RIGHTLEFT
833 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
834 && (nv_cmds[idx].cmd_flags & NV_RL))
835 {
836 /* Invert horizontal movements and operations. Only when typed by the
837 * user directly, not when the result of a mapping or "x" translated
838 * to "dl". */
839 switch (ca.cmdchar)
840 {
841 case 'l': ca.cmdchar = 'h'; break;
842 case K_RIGHT: ca.cmdchar = K_LEFT; break;
843 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
844 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
845 case 'h': ca.cmdchar = 'l'; break;
846 case K_LEFT: ca.cmdchar = K_RIGHT; break;
847 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
848 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
849 case '>': ca.cmdchar = '<'; break;
850 case '<': ca.cmdchar = '>'; break;
851 }
852 idx = find_command(ca.cmdchar);
853 }
854#endif
855
856 /*
857 * Get an additional character if we need one.
858 */
859 if ((nv_cmds[idx].cmd_flags & NV_NCH)
860 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
861 && oap->op_type == OP_NOP)
862 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
863 || (ca.cmdchar == 'q'
864 && oap->op_type == OP_NOP
865 && !Recording
866 && !Exec_reg)
867 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
868 && (oap->op_type != OP_NOP
869#ifdef FEAT_VISUAL
870 || VIsual_active
871#endif
872 ))))
873 {
874 int *cp;
875 int repl = FALSE; /* get character for replace mode */
876 int lit = FALSE; /* get extra character literally */
877 int langmap_active = FALSE; /* using :lmap mappings */
878 int lang; /* getting a text character */
879#ifdef USE_IM_CONTROL
880 int save_smd; /* saved value of p_smd */
881#endif
882
883 ++no_mapping;
884 ++allow_keys; /* no mapping for nchar, but allow key codes */
885 if (ca.cmdchar == 'g')
886 {
887 /*
888 * For 'g' get the next character now, so that we can check for
889 * "gr", "g'" and "g`".
890 */
891 ca.nchar = safe_vgetc();
892#ifdef FEAT_LANGMAP
893 LANGMAP_ADJUST(ca.nchar, TRUE);
894#endif
895#ifdef FEAT_CMDL_INFO
896 need_flushbuf |= add_to_showcmd(ca.nchar);
897#endif
898 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
899 || ca.nchar == Ctrl_BSL)
900 {
901 cp = &ca.extra_char; /* need to get a third character */
902 if (ca.nchar != 'r')
903 lit = TRUE; /* get it literally */
904 else
905 repl = TRUE; /* get it in replace mode */
906 }
907 else
908 cp = NULL; /* no third character needed */
909 }
910 else
911 {
912 if (ca.cmdchar == 'r') /* get it in replace mode */
913 repl = TRUE;
914 cp = &ca.nchar;
915 }
916 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
917
918 /*
919 * Get a second or third character.
920 */
921 if (cp != NULL)
922 {
923#ifdef CURSOR_SHAPE
924 if (repl)
925 {
926 State = REPLACE; /* pretend Replace mode */
927 ui_cursor_shape(); /* show different cursor shape */
928 }
929#endif
930 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
931 {
932 /* Allow mappings defined with ":lmap". */
933 --no_mapping;
934 --allow_keys;
935 if (repl)
936 State = LREPLACE;
937 else
938 State = LANGMAP;
939 langmap_active = TRUE;
940 }
941#ifdef USE_IM_CONTROL
942 save_smd = p_smd;
943 p_smd = FALSE; /* Don't let the IM code show the mode here */
944 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
945 im_set_active(TRUE);
946#endif
947
948 *cp = safe_vgetc();
949
950 if (langmap_active)
951 {
952 /* Undo the decrement done above */
953 ++no_mapping;
954 ++allow_keys;
955 State = NORMAL_BUSY;
956 }
957#ifdef USE_IM_CONTROL
958 if (lang)
959 {
960 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
961 im_save_status(&curbuf->b_p_iminsert);
962 im_set_active(FALSE);
963 }
964 p_smd = save_smd;
965#endif
966#ifdef CURSOR_SHAPE
967 State = NORMAL_BUSY;
968#endif
969#ifdef FEAT_CMDL_INFO
970 need_flushbuf |= add_to_showcmd(*cp);
971#endif
972
973 if (!lit)
974 {
975#ifdef FEAT_DIGRAPHS
976 /* Typing CTRL-K gets a digraph. */
977 if (*cp == Ctrl_K
978 && ((nv_cmds[idx].cmd_flags & NV_LANG)
979 || cp == &ca.extra_char)
980 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
981 {
982 c = get_digraph(FALSE);
983 if (c > 0)
984 {
985 *cp = c;
986# ifdef FEAT_CMDL_INFO
987 /* Guessing how to update showcmd here... */
988 del_from_showcmd(3);
989 need_flushbuf |= add_to_showcmd(*cp);
990# endif
991 }
992 }
993#endif
994
995#ifdef FEAT_LANGMAP
996 /* adjust chars > 127, except after "tTfFr" commands */
997 LANGMAP_ADJUST(*cp, !lang);
998#endif
999#ifdef FEAT_RIGHTLEFT
1000 /* adjust Hebrew mapped char */
1001 if (p_hkmap && lang && KeyTyped)
1002 *cp = hkmap(*cp);
1003# ifdef FEAT_FKMAP
1004 /* adjust Farsi mapped char */
1005 if (p_fkmap && lang && KeyTyped)
1006 *cp = fkmap(*cp);
1007# endif
1008#endif
1009 }
1010
1011 /*
1012 * When the next character is CTRL-\ a following CTRL-N means the
1013 * command is aborted and we go to Normal mode.
1014 */
1015 if (cp == &ca.extra_char
1016 && ca.nchar == Ctrl_BSL
1017 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1018 {
1019 ca.cmdchar = Ctrl_BSL;
1020 ca.nchar = ca.extra_char;
1021 idx = find_command(ca.cmdchar);
1022 }
1023 else if (*cp == Ctrl_BSL)
1024 {
1025 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1026
1027 /* There is a busy wait here when typing "f<C-\>" and then
1028 * something different from CTRL-N. Can't be avoided. */
1029 while ((c = vpeekc()) <= 0 && towait > 0L)
1030 {
1031 do_sleep(towait > 50L ? 50L : towait);
1032 towait -= 50L;
1033 }
1034 if (c > 0)
1035 {
1036 c = safe_vgetc();
1037 if (c != Ctrl_N && c != Ctrl_G)
1038 vungetc(c);
1039 else
1040 {
1041 ca.cmdchar = Ctrl_BSL;
1042 ca.nchar = c;
1043 idx = find_command(ca.cmdchar);
1044 }
1045 }
1046 }
1047
1048#ifdef FEAT_MBYTE
1049 /* When getting a text character and the next character is a
1050 * multi-byte character, it could be a composing character.
1051 * However, don't wait for it to arrive. */
1052 while (enc_utf8 && lang && (c = vpeekc()) > 0
1053 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1054 {
1055 c = safe_vgetc();
1056 if (!utf_iscomposing(c))
1057 {
1058 vungetc(c); /* it wasn't, put it back */
1059 break;
1060 }
1061 else if (ca.ncharC1 == 0)
1062 ca.ncharC1 = c;
1063 else
1064 ca.ncharC2 = c;
1065 }
1066#endif
1067 }
1068 --no_mapping;
1069 --allow_keys;
1070 }
1071
1072#ifdef FEAT_CMDL_INFO
1073 /*
1074 * Flush the showcmd characters onto the screen so we can see them while
1075 * the command is being executed. Only do this when the shown command was
1076 * actually displayed, otherwise this will slow down a lot when executing
1077 * mappings.
1078 */
1079 if (need_flushbuf)
1080 out_flush();
1081#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001082#ifdef FEAT_AUTOCMD
1083 did_cursorhold = FALSE;
1084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 State = NORMAL;
1087
1088 if (ca.nchar == ESC)
1089 {
1090 clearop(oap);
1091 if (restart_edit == 0 && goto_im())
1092 restart_edit = 'a';
1093 goto normal_end;
1094 }
1095
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001096 if (ca.cmdchar != K_IGNORE)
1097 {
1098 msg_didout = FALSE; /* don't scroll screen up for normal command */
1099 msg_col = 0;
1100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102#ifdef FEAT_VISUAL
1103 old_pos = curwin->w_cursor; /* remember where cursor was */
1104
1105 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1106 * mode. */
1107 if (!VIsual_active && km_startsel)
1108 {
1109 if (nv_cmds[idx].cmd_flags & NV_SS)
1110 {
1111 start_selection();
1112 unshift_special(&ca);
1113 idx = find_command(ca.cmdchar);
1114 }
1115 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1116 && (mod_mask & MOD_MASK_SHIFT))
1117 {
1118 start_selection();
1119 mod_mask &= ~MOD_MASK_SHIFT;
1120 }
1121 }
1122#endif
1123
1124 /*
1125 * Execute the command!
1126 * Call the command function found in the commands table.
1127 */
1128 ca.arg = nv_cmds[idx].cmd_arg;
1129 (nv_cmds[idx].cmd_func)(&ca);
1130
1131 /*
1132 * If we didn't start or finish an operator, reset oap->regname, unless we
1133 * need it later.
1134 */
1135 if (!finish_op
1136 && !oap->op_type
1137 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1138 {
1139 clearop(oap);
1140#ifdef FEAT_EVAL
1141 set_reg_var('"');
1142#endif
1143 }
1144
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001145 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001146 * character or "z333<cr>". */
1147 if (old_mapped_len > 0)
1148 old_mapped_len = typebuf_maplen();
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /*
1151 * If an operation is pending, handle it...
1152 */
1153 do_pending_operator(&ca, old_col, FALSE);
1154
1155 /*
1156 * Wait for a moment when a message is displayed that will be overwritten
1157 * by the mode message.
1158 * In Visual mode and with "^O" in Insert mode, a short message will be
1159 * overwritten by the mode message. Wait a bit, until a key is hit.
1160 * In Visual mode, it's more important to keep the Visual area updated
1161 * than keeping a message (e.g. from a /pat search).
1162 * Only do this if the command was typed, not from a mapping.
1163 * Don't wait when emsg_silent is non-zero.
1164 * Also wait a bit after an error message, e.g. for "^O:".
1165 * Don't redraw the screen, it would remove the message.
1166 */
1167 if ( ((p_smd
1168 && (restart_edit != 0
1169#ifdef FEAT_VISUAL
1170 || (VIsual_active
1171 && old_pos.lnum == curwin->w_cursor.lnum
1172 && old_pos.col == curwin->w_cursor.col)
1173#endif
1174 )
1175 && (clear_cmdline
1176 || redraw_cmdline)
1177 && (msg_didout || (msg_didany && msg_scroll))
1178 && !msg_nowait
1179 && KeyTyped)
1180 || (restart_edit != 0
1181#ifdef FEAT_VISUAL
1182 && !VIsual_active
1183#endif
1184 && (msg_scroll
1185 || emsg_on_display)))
1186 && oap->regname == 0
1187 && !(ca.retval & CA_COMMAND_BUSY)
1188 && stuff_empty()
1189 && typebuf_typed()
1190 && emsg_silent == 0
1191 && !did_wait_return
1192 && oap->op_type == OP_NOP)
1193 {
1194 int save_State = State;
1195
1196 /* Draw the cursor with the right shape here */
1197 if (restart_edit != 0)
1198 State = INSERT;
1199
1200 /* If need to redraw, and there is a "keep_msg", redraw before the
1201 * delay */
1202 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1203 {
1204 char_u *kmsg;
1205
1206 kmsg = keep_msg;
1207 keep_msg = NULL;
1208 /* showmode() will clear keep_msg, but we want to use it anyway */
1209 update_screen(0);
1210 /* now reset it, otherwise it's put in the history again */
1211 keep_msg = kmsg;
1212 msg_attr(kmsg, keep_msg_attr);
1213 vim_free(kmsg);
1214 }
1215 setcursor();
1216 cursor_on();
1217 out_flush();
1218 if (msg_scroll || emsg_on_display)
1219 ui_delay(1000L, TRUE); /* wait at least one second */
1220 ui_delay(3000L, FALSE); /* wait up to three seconds */
1221 State = save_State;
1222
1223 msg_scroll = FALSE;
1224 emsg_on_display = FALSE;
1225 }
1226
1227 /*
1228 * Finish up after executing a Normal mode command.
1229 */
1230normal_end:
1231
1232 msg_nowait = FALSE;
1233
1234 /* Reset finish_op, in case it was set */
1235#ifdef CURSOR_SHAPE
1236 c = finish_op;
1237#endif
1238 finish_op = FALSE;
1239#ifdef CURSOR_SHAPE
1240 /* Redraw the cursor with another shape, if we were in Operator-pending
1241 * mode or did a replace command. */
1242 if (c || ca.cmdchar == 'r')
1243 {
1244 ui_cursor_shape(); /* may show different cursor shape */
1245# ifdef FEAT_MOUSESHAPE
1246 update_mouseshape(-1);
1247# endif
1248 }
1249#endif
1250
1251#ifdef FEAT_CMDL_INFO
1252 if (oap->op_type == OP_NOP && oap->regname == 0)
1253 clear_showcmd();
1254#endif
1255
1256 checkpcmark(); /* check if we moved since setting pcmark */
1257 vim_free(ca.searchbuf);
1258
1259#ifdef FEAT_MBYTE
1260 if (has_mbyte)
1261 mb_adjust_cursor();
1262#endif
1263
1264#ifdef FEAT_SCROLLBIND
1265 if (curwin->w_p_scb && toplevel)
1266 {
1267 validate_cursor(); /* may need to update w_leftcol */
1268 do_check_scrollbind(TRUE);
1269 }
1270#endif
1271
1272 /*
1273 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1274 * if still inside a mapping that started in Visual mode).
1275 * May switch from Visual to Select mode after CTRL-O command.
1276 */
1277 if ( oap->op_type == OP_NOP
1278#ifdef FEAT_VISUAL
1279 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1280 || restart_VIsual_select == 1)
1281#endif
1282 && !(ca.retval & CA_COMMAND_BUSY)
1283 && stuff_empty()
1284 && oap->regname == 0)
1285 {
1286#ifdef FEAT_VISUAL
1287 if (restart_VIsual_select == 1)
1288 {
1289 VIsual_select = TRUE;
1290 showmode();
1291 restart_VIsual_select = 0;
1292 }
1293#endif
1294 if (restart_edit != 0
1295#ifdef FEAT_VISUAL
1296 && !VIsual_active
1297#endif
1298 && old_mapped_len == 0)
1299 (void)edit(restart_edit, FALSE, 1L);
1300 }
1301
1302#ifdef FEAT_VISUAL
1303 if (restart_VIsual_select == 2)
1304 restart_VIsual_select = 1;
1305#endif
1306
1307 /* Save count before an operator for next time. */
1308 opcount = ca.opcount;
1309}
1310
1311/*
1312 * Handle an operator after visual mode or when the movement is finished
1313 */
1314 void
1315do_pending_operator(cap, old_col, gui_yank)
1316 cmdarg_T *cap;
1317 int old_col;
1318 int gui_yank;
1319{
1320 oparg_T *oap = cap->oap;
1321 pos_T old_cursor;
1322 int empty_region_error;
1323 int restart_edit_save;
1324
1325#ifdef FEAT_VISUAL
1326 /* The visual area is remembered for redo */
1327 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1328 static linenr_T redo_VIsual_line_count; /* number of lines */
1329 static colnr_T redo_VIsual_col; /* number of cols or end column */
1330 static long redo_VIsual_count; /* count for Visual operator */
1331# ifdef FEAT_VIRTUALEDIT
1332 int include_line_break = FALSE;
1333# endif
1334#endif
1335
1336#if defined(FEAT_CLIPBOARD)
1337 /*
1338 * Yank the visual area into the GUI selection register before we operate
1339 * on it and lose it forever.
1340 * Don't do it if a specific register was specified, so that ""x"*P works.
1341 * This could call do_pending_operator() recursively, but that's OK
1342 * because gui_yank will be TRUE for the nested call.
1343 */
1344 if (clip_star.available
1345 && oap->op_type != OP_NOP
1346 && !gui_yank
1347# ifdef FEAT_VISUAL
1348 && VIsual_active
1349 && !redo_VIsual_busy
1350# endif
1351 && oap->regname == 0)
1352 clip_auto_select();
1353#endif
1354 old_cursor = curwin->w_cursor;
1355
1356 /*
1357 * If an operation is pending, handle it...
1358 */
1359 if ((finish_op
1360#ifdef FEAT_VISUAL
1361 || VIsual_active
1362#endif
1363 ) && oap->op_type != OP_NOP)
1364 {
1365#ifdef FEAT_VISUAL
1366 oap->is_VIsual = VIsual_active;
1367 if (oap->motion_force == 'V')
1368 oap->motion_type = MLINE;
1369 else if (oap->motion_force == 'v')
1370 {
1371 /* If the motion was linewise, "inclusive" will not have been set.
1372 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1373 if (oap->motion_type == MLINE)
1374 oap->inclusive = FALSE;
1375 /* If the motion already was characterwise, toggle "inclusive" */
1376 else if (oap->motion_type == MCHAR)
1377 oap->inclusive = !oap->inclusive;
1378 oap->motion_type = MCHAR;
1379 }
1380 else if (oap->motion_force == Ctrl_V)
1381 {
1382 /* Change line- or characterwise motion into Visual block mode. */
1383 VIsual_active = TRUE;
1384 VIsual = oap->start;
1385 VIsual_mode = Ctrl_V;
1386 VIsual_select = FALSE;
1387 VIsual_reselect = FALSE;
1388 }
1389#endif
1390
1391 /* only redo yank when 'y' flag is in 'cpoptions' */
1392 /* never redo "zf" (define fold) */
1393 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1394#ifdef FEAT_VISUAL
1395 && (!VIsual_active || oap->motion_force)
1396#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001397 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_FOLDING
1399 && oap->op_type != OP_FOLD
1400 && oap->op_type != OP_FOLDOPEN
1401 && oap->op_type != OP_FOLDOPENREC
1402 && oap->op_type != OP_FOLDCLOSE
1403 && oap->op_type != OP_FOLDCLOSEREC
1404 && oap->op_type != OP_FOLDDEL
1405 && oap->op_type != OP_FOLDDELREC
1406#endif
1407 )
1408 {
1409 prep_redo(oap->regname, cap->count0,
1410 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1411 oap->motion_force, cap->cmdchar, cap->nchar);
1412 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1413 {
1414 /*
1415 * If 'cpoptions' does not contain 'r', insert the search
1416 * pattern to really repeat the same command.
1417 */
1418 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001419 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 AppendToRedobuff(NL_STR);
1421 }
1422 else if (cap->cmdchar == ':')
1423 {
1424 /* do_cmdline() has stored the first typed line in
1425 * "repeat_cmdline". When several lines are typed repeating
1426 * won't be possible. */
1427 if (repeat_cmdline == NULL)
1428 ResetRedobuff();
1429 else
1430 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001431 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 AppendToRedobuff(NL_STR);
1433 vim_free(repeat_cmdline);
1434 repeat_cmdline = NULL;
1435 }
1436 }
1437 }
1438
1439#ifdef FEAT_VISUAL
1440 if (redo_VIsual_busy)
1441 {
1442 oap->start = curwin->w_cursor;
1443 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1444 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1445 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1446 VIsual_mode = redo_VIsual_mode;
1447 if (VIsual_mode == 'v')
1448 {
1449 if (redo_VIsual_line_count <= 1)
1450 curwin->w_cursor.col += redo_VIsual_col - 1;
1451 else
1452 curwin->w_cursor.col = redo_VIsual_col;
1453 }
1454 if (redo_VIsual_col == MAXCOL)
1455 {
1456 curwin->w_curswant = MAXCOL;
1457 coladvance((colnr_T)MAXCOL);
1458 }
1459 cap->count0 = redo_VIsual_count;
1460 if (redo_VIsual_count != 0)
1461 cap->count1 = redo_VIsual_count;
1462 else
1463 cap->count1 = 1;
1464 }
1465 else if (VIsual_active)
1466 {
1467 /* Save the current VIsual area for '< and '> marks, and "gv" */
1468 curbuf->b_visual_start = VIsual;
1469 curbuf->b_visual_end = curwin->w_cursor;
1470 curbuf->b_visual_mode = VIsual_mode;
1471# ifdef FEAT_EVAL
1472 curbuf->b_visual_mode_eval = VIsual_mode;
1473# endif
1474 curbuf->b_visual_curswant = curwin->w_curswant;
1475
1476 /* In Select mode, a linewise selection is operated upon like a
1477 * characterwise selection. */
1478 if (VIsual_select && VIsual_mode == 'V')
1479 {
1480 if (lt(VIsual, curwin->w_cursor))
1481 {
1482 VIsual.col = 0;
1483 curwin->w_cursor.col =
1484 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1485 }
1486 else
1487 {
1488 curwin->w_cursor.col = 0;
1489 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1490 }
1491 VIsual_mode = 'v';
1492 }
1493 /* If 'selection' is "exclusive", backup one character for
1494 * charwise selections. */
1495 else if (VIsual_mode == 'v')
1496 {
1497# ifdef FEAT_VIRTUALEDIT
1498 include_line_break =
1499# endif
1500 unadjust_for_sel();
1501 }
1502
1503 oap->start = VIsual;
1504 if (VIsual_mode == 'V')
1505 oap->start.col = 0;
1506 }
1507#endif /* FEAT_VISUAL */
1508
1509 /*
1510 * Set oap->start to the first position of the operated text, oap->end
1511 * to the end of the operated text. w_cursor is equal to oap->start.
1512 */
1513 if (lt(oap->start, curwin->w_cursor))
1514 {
1515#ifdef FEAT_FOLDING
1516 /* Include folded lines completely. */
1517 if (!VIsual_active)
1518 {
1519 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1520 oap->start.col = 0;
1521 if (hasFolding(curwin->w_cursor.lnum, NULL,
1522 &curwin->w_cursor.lnum))
1523 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1524 }
1525#endif
1526 oap->end = curwin->w_cursor;
1527 curwin->w_cursor = oap->start;
1528
1529 /* w_virtcol may have been updated; if the cursor goes back to its
1530 * previous position w_virtcol becomes invalid and isn't updated
1531 * automatically. */
1532 curwin->w_valid &= ~VALID_VIRTCOL;
1533 }
1534 else
1535 {
1536#ifdef FEAT_FOLDING
1537 /* Include folded lines completely. */
1538 if (!VIsual_active && oap->motion_type == MLINE)
1539 {
1540 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1541 NULL))
1542 curwin->w_cursor.col = 0;
1543 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1544 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1545 }
1546#endif
1547 oap->end = oap->start;
1548 oap->start = curwin->w_cursor;
1549 }
1550
1551 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1552
1553#ifdef FEAT_VIRTUALEDIT
1554 /* Set "virtual_op" before resetting VIsual_active. */
1555 virtual_op = virtual_active();
1556#endif
1557
1558#ifdef FEAT_VISUAL
1559 if (VIsual_active || redo_VIsual_busy)
1560 {
1561 if (VIsual_mode == Ctrl_V) /* block mode */
1562 {
1563 colnr_T start, end;
1564
1565 oap->block_mode = TRUE;
1566
1567 getvvcol(curwin, &(oap->start),
1568 &oap->start_vcol, NULL, &oap->end_vcol);
1569 if (!redo_VIsual_busy)
1570 {
1571 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1572
1573 if (start < oap->start_vcol)
1574 oap->start_vcol = start;
1575 if (end > oap->end_vcol)
1576 {
1577 if (*p_sel == 'e' && start >= 1
1578 && start - 1 >= oap->end_vcol)
1579 oap->end_vcol = start - 1;
1580 else
1581 oap->end_vcol = end;
1582 }
1583 }
1584
1585 /* if '$' was used, get oap->end_vcol from longest line */
1586 if (curwin->w_curswant == MAXCOL)
1587 {
1588 curwin->w_cursor.col = MAXCOL;
1589 oap->end_vcol = 0;
1590 for (curwin->w_cursor.lnum = oap->start.lnum;
1591 curwin->w_cursor.lnum <= oap->end.lnum;
1592 ++curwin->w_cursor.lnum)
1593 {
1594 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1595 if (end > oap->end_vcol)
1596 oap->end_vcol = end;
1597 }
1598 }
1599 else if (redo_VIsual_busy)
1600 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1601 /*
1602 * Correct oap->end.col and oap->start.col to be the
1603 * upper-left and lower-right corner of the block area.
1604 *
1605 * (Actually, this does convert column positions into character
1606 * positions)
1607 */
1608 curwin->w_cursor.lnum = oap->end.lnum;
1609 coladvance(oap->end_vcol);
1610 oap->end = curwin->w_cursor;
1611
1612 curwin->w_cursor = oap->start;
1613 coladvance(oap->start_vcol);
1614 oap->start = curwin->w_cursor;
1615 }
1616
1617 if (!redo_VIsual_busy && !gui_yank)
1618 {
1619 /*
1620 * Prepare to reselect and redo Visual: this is based on the
1621 * size of the Visual text
1622 */
1623 resel_VIsual_mode = VIsual_mode;
1624 if (curwin->w_curswant == MAXCOL)
1625 resel_VIsual_col = MAXCOL;
1626 else if (VIsual_mode == Ctrl_V)
1627 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1628 else if (oap->line_count > 1)
1629 resel_VIsual_col = oap->end.col;
1630 else
1631 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1632 resel_VIsual_line_count = oap->line_count;
1633 }
1634
1635 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1636 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1637 && oap->op_type != OP_COLON
1638#ifdef FEAT_FOLDING
1639 && oap->op_type != OP_FOLD
1640 && oap->op_type != OP_FOLDOPEN
1641 && oap->op_type != OP_FOLDOPENREC
1642 && oap->op_type != OP_FOLDCLOSE
1643 && oap->op_type != OP_FOLDCLOSEREC
1644 && oap->op_type != OP_FOLDDEL
1645 && oap->op_type != OP_FOLDDELREC
1646#endif
1647 && oap->motion_force == NUL
1648 )
1649 {
1650 /* Prepare for redoing. Only use the nchar field for "r",
1651 * otherwise it might be the second char of the operator. */
1652 prep_redo(oap->regname, 0L, NUL, 'v',
1653 get_op_char(oap->op_type),
1654 get_extra_op_char(oap->op_type),
1655 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1656 if (!redo_VIsual_busy)
1657 {
1658 redo_VIsual_mode = resel_VIsual_mode;
1659 redo_VIsual_col = resel_VIsual_col;
1660 redo_VIsual_line_count = resel_VIsual_line_count;
1661 redo_VIsual_count = cap->count0;
1662 }
1663 }
1664
1665 /*
1666 * oap->inclusive defaults to TRUE.
1667 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1668 * FALSE. This makes "d}P" and "v}dP" work the same.
1669 */
1670 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1671 oap->inclusive = TRUE;
1672 if (VIsual_mode == 'V')
1673 oap->motion_type = MLINE;
1674 else
1675 {
1676 oap->motion_type = MCHAR;
1677 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1678# ifdef FEAT_VIRTUALEDIT
1679 && (include_line_break || !virtual_op)
1680# endif
1681 )
1682 {
1683 oap->inclusive = FALSE;
1684 /* Try to include the newline, unless it's an operator
1685 * that works on lines only */
1686 if (*p_sel != 'o'
1687 && !op_on_lines(oap->op_type)
1688 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1689 {
1690 ++oap->end.lnum;
1691 oap->end.col = 0;
1692# ifdef FEAT_VIRTUALEDIT
1693 oap->end.coladd = 0;
1694# endif
1695 ++oap->line_count;
1696 }
1697 }
1698 }
1699
1700 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 /*
1703 * Switch Visual off now, so screen updating does
1704 * not show inverted text when the screen is redrawn.
1705 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1706 * no screen redraw, so it is done here to remove the inverted
1707 * part.
1708 */
1709 if (!gui_yank)
1710 {
1711 VIsual_active = FALSE;
1712# ifdef FEAT_MOUSE
1713 setmouse();
1714 mouse_dragging = 0;
1715# endif
1716 if (p_smd)
1717 clear_cmdline = TRUE; /* unshow visual mode later */
1718#ifdef FEAT_CMDL_INFO
1719 else
1720 clear_showcmd();
1721#endif
1722 if ((oap->op_type == OP_YANK
1723 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001724 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 || oap->op_type == OP_FILTER)
1726 && oap->motion_force == NUL)
1727 redraw_curbuf_later(INVERTED);
1728 }
1729 }
1730#endif
1731
1732#ifdef FEAT_MBYTE
1733 /* Include the trailing byte of a multi-byte char. */
1734 if (has_mbyte && oap->inclusive)
1735 {
1736 int l;
1737
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001738 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (l > 1)
1740 oap->end.col += l - 1;
1741 }
1742#endif
1743 curwin->w_set_curswant = TRUE;
1744
1745 /*
1746 * oap->empty is set when start and end are the same. The inclusive
1747 * flag affects this too, unless yanking and the end is on a NUL.
1748 */
1749 oap->empty = (oap->motion_type == MCHAR
1750 && (!oap->inclusive
1751 || (oap->op_type == OP_YANK
1752 && gchar_pos(&oap->end) == NUL))
1753 && equalpos(oap->start, oap->end)
1754#ifdef FEAT_VIRTUALEDIT
1755 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1756#endif
1757 );
1758 /*
1759 * For delete, change and yank, it's an error to operate on an
1760 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1761 */
1762 empty_region_error = (oap->empty
1763 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1764
1765#ifdef FEAT_VISUAL
1766 /* Force a redraw when operating on an empty Visual region, when
1767 * 'modifiable is off or creating a fold. */
1768 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1769# ifdef FEAT_FOLDING
1770 || oap->op_type == OP_FOLD
1771# endif
1772 ))
1773 redraw_curbuf_later(INVERTED);
1774#endif
1775
1776 /*
1777 * If the end of an operator is in column one while oap->motion_type
1778 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1779 * character in the previous line. If op_start is on or before the
1780 * first non-blank in the line, the operator becomes linewise
1781 * (strange, but that's the way vi does it).
1782 */
1783 if ( oap->motion_type == MCHAR
1784 && oap->inclusive == FALSE
1785 && !(cap->retval & CA_NO_ADJ_OP_END)
1786 && oap->end.col == 0
1787#ifdef FEAT_VISUAL
1788 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001789 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#endif
1791 && oap->line_count > 1)
1792 {
1793 oap->end_adjusted = TRUE; /* remember that we did this */
1794 --oap->line_count;
1795 --oap->end.lnum;
1796 if (inindent(0))
1797 oap->motion_type = MLINE;
1798 else
1799 {
1800 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1801 if (oap->end.col)
1802 {
1803 --oap->end.col;
1804 oap->inclusive = TRUE;
1805 }
1806 }
1807 }
1808 else
1809 oap->end_adjusted = FALSE;
1810
1811 switch (oap->op_type)
1812 {
1813 case OP_LSHIFT:
1814 case OP_RSHIFT:
1815 op_shift(oap, TRUE,
1816#ifdef FEAT_VISUAL
1817 oap->is_VIsual ? (int)cap->count1 :
1818#endif
1819 1);
1820 auto_format(FALSE, TRUE);
1821 break;
1822
1823 case OP_JOIN_NS:
1824 case OP_JOIN:
1825 if (oap->line_count < 2)
1826 oap->line_count = 2;
1827 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1828 curbuf->b_ml.ml_line_count)
1829 beep_flush();
1830 else
1831 {
1832 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1833 auto_format(FALSE, TRUE);
1834 }
1835 break;
1836
1837 case OP_DELETE:
1838#ifdef FEAT_VISUAL
1839 VIsual_reselect = FALSE; /* don't reselect now */
1840#endif
1841 if (empty_region_error)
1842 vim_beep();
1843 else
1844 {
1845 (void)op_delete(oap);
1846 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1847 u_save_cursor(); /* cursor line wasn't saved yet */
1848 auto_format(FALSE, TRUE);
1849 }
1850 break;
1851
1852 case OP_YANK:
1853 if (empty_region_error)
1854 {
1855 if (!gui_yank)
1856 vim_beep();
1857 }
1858 else
1859 (void)op_yank(oap, FALSE, !gui_yank);
1860 check_cursor_col();
1861 break;
1862
1863 case OP_CHANGE:
1864#ifdef FEAT_VISUAL
1865 VIsual_reselect = FALSE; /* don't reselect now */
1866#endif
1867 if (empty_region_error)
1868 vim_beep();
1869 else
1870 {
1871 /* This is a new edit command, not a restart. Need to
1872 * remember it to make 'insertmode' work with mappings for
1873 * Visual mode. But do this only once and not when typed and
1874 * 'insertmode' isn't set. */
1875 if (p_im || !KeyTyped)
1876 restart_edit_save = restart_edit;
1877 else
1878 restart_edit_save = 0;
1879 restart_edit = 0;
1880 /* Reset finish_op now, don't want it set inside edit(). */
1881 finish_op = FALSE;
1882 if (op_change(oap)) /* will call edit() */
1883 cap->retval |= CA_COMMAND_BUSY;
1884 if (restart_edit == 0)
1885 restart_edit = restart_edit_save;
1886 }
1887 break;
1888
1889 case OP_FILTER:
1890 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1891 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1892 else
1893 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1894
1895 case OP_INDENT:
1896 case OP_COLON:
1897
1898#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1899 /*
1900 * If 'equalprg' is empty, do the indenting internally.
1901 */
1902 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1903 {
1904# ifdef FEAT_LISP
1905 if (curbuf->b_p_lisp)
1906 {
1907 op_reindent(oap, get_lisp_indent);
1908 break;
1909 }
1910# endif
1911# ifdef FEAT_CINDENT
1912 op_reindent(oap,
1913# ifdef FEAT_EVAL
1914 *curbuf->b_p_inde != NUL ? get_expr_indent :
1915# endif
1916 get_c_indent);
1917 break;
1918# endif
1919 }
1920#endif
1921
1922 op_colon(oap);
1923 break;
1924
1925 case OP_TILDE:
1926 case OP_UPPER:
1927 case OP_LOWER:
1928 case OP_ROT13:
1929 if (empty_region_error)
1930 vim_beep();
1931 else
1932 op_tilde(oap);
1933 check_cursor_col();
1934 break;
1935
1936 case OP_FORMAT:
1937 if (*p_fp != NUL)
1938 op_colon(oap); /* use external command */
1939 else
1940 op_format(oap, FALSE); /* use internal function */
1941 break;
1942
1943 case OP_FORMAT2:
1944 op_format(oap, TRUE); /* use internal function */
1945 break;
1946
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001947 case OP_FUNCTION:
1948 op_function(oap); /* call 'operatorfunc' */
1949 break;
1950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 case OP_INSERT:
1952 case OP_APPEND:
1953#ifdef FEAT_VISUAL
1954 VIsual_reselect = FALSE; /* don't reselect now */
1955#endif
1956#ifdef FEAT_VISUALEXTRA
1957 if (empty_region_error)
1958 vim_beep();
1959 else
1960 {
1961 /* This is a new edit command, not a restart. Need to
1962 * remember it to make 'insertmode' work with mappings for
1963 * Visual mode. But do this only once. */
1964 restart_edit_save = restart_edit;
1965 restart_edit = 0;
1966
1967 op_insert(oap, cap->count1);
1968
1969 /* TODO: when inserting in several lines, should format all
1970 * the lines. */
1971 auto_format(FALSE, TRUE);
1972
1973 if (restart_edit == 0)
1974 restart_edit = restart_edit_save;
1975 }
1976#else
1977 vim_beep();
1978#endif
1979 break;
1980
1981 case OP_REPLACE:
1982#ifdef FEAT_VISUAL
1983 VIsual_reselect = FALSE; /* don't reselect now */
1984#endif
1985#ifdef FEAT_VISUALEXTRA
1986 if (empty_region_error)
1987#endif
1988 vim_beep();
1989#ifdef FEAT_VISUALEXTRA
1990 else
1991 op_replace(oap, cap->nchar);
1992#endif
1993 break;
1994
1995#ifdef FEAT_FOLDING
1996 case OP_FOLD:
1997 VIsual_reselect = FALSE; /* don't reselect now */
1998 foldCreate(oap->start.lnum, oap->end.lnum);
1999 break;
2000
2001 case OP_FOLDOPEN:
2002 case OP_FOLDOPENREC:
2003 case OP_FOLDCLOSE:
2004 case OP_FOLDCLOSEREC:
2005 VIsual_reselect = FALSE; /* don't reselect now */
2006 opFoldRange(oap->start.lnum, oap->end.lnum,
2007 oap->op_type == OP_FOLDOPEN
2008 || oap->op_type == OP_FOLDOPENREC,
2009 oap->op_type == OP_FOLDOPENREC
2010 || oap->op_type == OP_FOLDCLOSEREC,
2011 oap->is_VIsual);
2012 break;
2013
2014 case OP_FOLDDEL:
2015 case OP_FOLDDELREC:
2016 VIsual_reselect = FALSE; /* don't reselect now */
2017 deleteFold(oap->start.lnum, oap->end.lnum,
2018 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2019 break;
2020#endif
2021 default:
2022 clearopbeep(oap);
2023 }
2024#ifdef FEAT_VIRTUALEDIT
2025 virtual_op = MAYBE;
2026#endif
2027 if (!gui_yank)
2028 {
2029 /*
2030 * if 'sol' not set, go back to old column for some commands
2031 */
2032 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2033 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2034 || oap->op_type == OP_DELETE))
2035 coladvance(curwin->w_curswant = old_col);
2036 }
2037 else
2038 {
2039 curwin->w_cursor = old_cursor;
2040 }
2041#ifdef FEAT_VISUAL
2042 oap->block_mode = FALSE;
2043#endif
2044 clearop(oap);
2045 }
2046}
2047
2048/*
2049 * Handle indent and format operators and visual mode ":".
2050 */
2051 static void
2052op_colon(oap)
2053 oparg_T *oap;
2054{
2055 stuffcharReadbuff(':');
2056#ifdef FEAT_VISUAL
2057 if (oap->is_VIsual)
2058 stuffReadbuff((char_u *)"'<,'>");
2059 else
2060#endif
2061 {
2062 /*
2063 * Make the range look nice, so it can be repeated.
2064 */
2065 if (oap->start.lnum == curwin->w_cursor.lnum)
2066 stuffcharReadbuff('.');
2067 else
2068 stuffnumReadbuff((long)oap->start.lnum);
2069 if (oap->end.lnum != oap->start.lnum)
2070 {
2071 stuffcharReadbuff(',');
2072 if (oap->end.lnum == curwin->w_cursor.lnum)
2073 stuffcharReadbuff('.');
2074 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2075 stuffcharReadbuff('$');
2076 else if (oap->start.lnum == curwin->w_cursor.lnum)
2077 {
2078 stuffReadbuff((char_u *)".+");
2079 stuffnumReadbuff((long)oap->line_count - 1);
2080 }
2081 else
2082 stuffnumReadbuff((long)oap->end.lnum);
2083 }
2084 }
2085 if (oap->op_type != OP_COLON)
2086 stuffReadbuff((char_u *)"!");
2087 if (oap->op_type == OP_INDENT)
2088 {
2089#ifndef FEAT_CINDENT
2090 if (*get_equalprg() == NUL)
2091 stuffReadbuff((char_u *)"indent");
2092 else
2093#endif
2094 stuffReadbuff(get_equalprg());
2095 stuffReadbuff((char_u *)"\n");
2096 }
2097 else if (oap->op_type == OP_FORMAT)
2098 {
2099 if (*p_fp == NUL)
2100 stuffReadbuff((char_u *)"fmt");
2101 else
2102 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002103 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
2106 /*
2107 * do_cmdline() does the rest
2108 */
2109}
2110
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002111/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002112 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002113 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002114/*ARGSUSED*/
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002115 void
2116op_function(oap)
2117 oparg_T *oap;
2118{
2119#ifdef FEAT_EVAL
2120 char_u *(argv[1]);
2121
2122 if (*p_opfunc == NUL)
2123 EMSG(_("E774: 'operatorfunc' is empty"));
2124 else
2125 {
2126 /* Set '[ and '] marks to text to be operated on. */
2127 curbuf->b_op_start = oap->start;
2128 curbuf->b_op_end = oap->end;
2129 if (oap->motion_type != MLINE && !oap->inclusive)
2130 /* Exclude the end position. */
2131 decl(&curbuf->b_op_end);
2132
2133 if (oap->block_mode)
2134 argv[0] = (char_u *)"block";
2135 else if (oap->motion_type == MLINE)
2136 argv[0] = (char_u *)"line";
2137 else
2138 argv[0] = (char_u *)"char";
2139 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2140 }
2141#else
2142 EMSG(_("E775: Eval feature not available"));
2143#endif
2144}
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#if defined(FEAT_MOUSE) || defined(PROTO)
2147/*
2148 * Do the appropriate action for the current mouse click in the current mode.
2149 * Not used for Command-line mode.
2150 *
2151 * Normal Mode:
2152 * event modi- position visual change action
2153 * fier cursor window
2154 * left press - yes end yes
2155 * left press C yes end yes "^]" (2)
2156 * left press S yes end yes "*" (2)
2157 * left drag - yes start if moved no
2158 * left relse - yes start if moved no
2159 * middle press - yes if not active no put register
2160 * middle press - yes if active no yank and put
2161 * right press - yes start or extend yes
2162 * right press S yes no change yes "#" (2)
2163 * right drag - yes extend no
2164 * right relse - yes extend no
2165 *
2166 * Insert or Replace Mode:
2167 * event modi- position visual change action
2168 * fier cursor window
2169 * left press - yes (cannot be active) yes
2170 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2171 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2172 * left drag - yes start or extend (1) no CTRL-O (1)
2173 * left relse - yes start or extend (1) no CTRL-O (1)
2174 * middle press - no (cannot be active) no put register
2175 * right press - yes start or extend yes CTRL-O
2176 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2177 *
2178 * (1) only if mouse pointer moved since press
2179 * (2) only if click is in same buffer
2180 *
2181 * Return TRUE if start_arrow() should be called for edit mode.
2182 */
2183 int
2184do_mouse(oap, c, dir, count, fixindent)
2185 oparg_T *oap; /* operator argument, can be NULL */
2186 int c; /* K_LEFTMOUSE, etc */
2187 int dir; /* Direction to 'put' if necessary */
2188 long count;
2189 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2190{
2191 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2192 static int got_click = FALSE; /* got a click some time back */
2193
2194 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2195 int is_click; /* If FALSE it's a drag or release event */
2196 int is_drag; /* If TRUE it's a drag event */
2197 int jump_flags = 0; /* flags for jump_to_mouse() */
2198 pos_T start_visual;
2199 int moved; /* Has cursor moved? */
2200 int in_status_line; /* mouse in status line */
2201#ifdef FEAT_VERTSPLIT
2202 int in_sep_line; /* mouse in vertical separator line */
2203#endif
2204 int c1, c2;
2205#if defined(FEAT_FOLDING)
2206 pos_T save_cursor;
2207#endif
2208 win_T *old_curwin = curwin;
2209#ifdef FEAT_VISUAL
2210 static pos_T orig_cursor;
2211 colnr_T leftcol, rightcol;
2212 pos_T end_visual;
2213 int diff;
2214 int old_active = VIsual_active;
2215 int old_mode = VIsual_mode;
2216#endif
2217 int regname;
2218
2219#if defined(FEAT_FOLDING)
2220 save_cursor = curwin->w_cursor;
2221#endif
2222
2223 /*
2224 * When GUI is active, always recognize mouse events, otherwise:
2225 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2226 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2227 * - For command line and insert mode 'mouse' is checked before calling
2228 * do_mouse().
2229 */
2230 if (do_always)
2231 do_always = FALSE;
2232 else
2233#ifdef FEAT_GUI
2234 if (!gui.in_use)
2235#endif
2236 {
2237#ifdef FEAT_VISUAL
2238 if (VIsual_active)
2239 {
2240 if (!mouse_has(MOUSE_VISUAL))
2241 return FALSE;
2242 }
2243 else
2244#endif
2245 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2246 return FALSE;
2247 }
2248
2249 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2250
2251#ifdef FEAT_MOUSESHAPE
2252 /* May have stopped dragging the status or separator line. The pointer is
2253 * most likely still on the status or separator line. */
2254 if (!is_drag && drag_status_line)
2255 {
2256 drag_status_line = FALSE;
2257 update_mouseshape(SHAPE_IDX_STATUS);
2258 }
2259# ifdef FEAT_VERTSPLIT
2260 if (!is_drag && drag_sep_line)
2261 {
2262 drag_sep_line = FALSE;
2263 update_mouseshape(SHAPE_IDX_VSEP);
2264 }
2265# endif
2266#endif
2267
2268 /*
2269 * Ignore drag and release events if we didn't get a click.
2270 */
2271 if (is_click)
2272 got_click = TRUE;
2273 else
2274 {
2275 if (!got_click) /* didn't get click, ignore */
2276 return FALSE;
2277 if (!is_drag) /* release, reset got_click */
2278 got_click = FALSE;
2279 }
2280
Bram Moolenaar64969662005-12-14 21:59:55 +00002281#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002283 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
2285 if ((mod_mask & MOD_MASK_ALT))
2286 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 /*
2290 * CTRL right mouse button does CTRL-T
2291 */
2292 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2293 {
2294 if (State & INSERT)
2295 stuffcharReadbuff(Ctrl_O);
2296 if (count > 1)
2297 stuffnumReadbuff(count);
2298 stuffcharReadbuff(Ctrl_T);
2299 got_click = FALSE; /* ignore drag&release now */
2300 return FALSE;
2301 }
2302
2303 /*
2304 * CTRL only works with left mouse button
2305 */
2306 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2307 return FALSE;
2308
2309 /*
2310 * When a modifier is down, ignore drag and release events, as well as
2311 * multiple clicks and the middle mouse button.
2312 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2313 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002314 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2315 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 && (!is_click
2317 || (mod_mask & MOD_MASK_MULTI_CLICK)
2318 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002319 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 && mouse_model_popup()
2321 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002322 && !((mod_mask & MOD_MASK_ALT)
2323 && !mouse_model_popup()
2324 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 )
2326 return FALSE;
2327
2328 /*
2329 * If the button press was used as the movement command for an operator
2330 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2331 * drag/release events.
2332 */
2333 if (!is_click && which_button == MOUSE_MIDDLE)
2334 return FALSE;
2335
2336 if (oap != NULL)
2337 regname = oap->regname;
2338 else
2339 regname = 0;
2340
2341 /*
2342 * Middle mouse button does a 'put' of the selected text
2343 */
2344 if (which_button == MOUSE_MIDDLE)
2345 {
2346 if (State == NORMAL)
2347 {
2348 /*
2349 * If an operator was pending, we don't know what the user wanted
2350 * to do. Go back to normal mode: Clear the operator and beep().
2351 */
2352 if (oap != NULL && oap->op_type != OP_NOP)
2353 {
2354 clearopbeep(oap);
2355 return FALSE;
2356 }
2357
2358#ifdef FEAT_VISUAL
2359 /*
2360 * If visual was active, yank the highlighted text and put it
2361 * before the mouse pointer position.
2362 */
2363 if (VIsual_active)
2364 {
2365 stuffcharReadbuff('y');
2366 stuffcharReadbuff(K_MIDDLEMOUSE);
2367 do_always = TRUE; /* ignore 'mouse' setting next time */
2368 return FALSE;
2369 }
2370#endif
2371 /*
2372 * The rest is below jump_to_mouse()
2373 */
2374 }
2375
2376 else if ((State & INSERT) == 0)
2377 return FALSE;
2378
2379 /*
2380 * Middle click in insert mode doesn't move the mouse, just insert the
2381 * contents of a register. '.' register is special, can't insert that
2382 * with do_put().
2383 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2384 * happens for the GUI).
2385 */
2386 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2387 {
2388 if (regname == '.')
2389 insert_reg(regname, TRUE);
2390 else
2391 {
2392#ifdef FEAT_CLIPBOARD
2393 if (clip_star.available && regname == 0)
2394 regname = '*';
2395#endif
2396 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2397 insert_reg(regname, TRUE);
2398 else
2399 {
2400 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2401
2402 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2403 AppendCharToRedobuff(Ctrl_R);
2404 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2405 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2406 }
2407 }
2408 return FALSE;
2409 }
2410 }
2411
2412 /* When dragging or button-up stay in the same window. */
2413 if (!is_click)
2414 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2415
2416 start_visual.lnum = 0;
2417
2418 /*
2419 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2420 * right button up -> pop-up menu
2421 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002422 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 */
2424 if (mouse_model_popup())
2425 {
2426 if (which_button == MOUSE_RIGHT
2427 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2428 {
2429 /*
2430 * NOTE: Ignore right button down and drag mouse events.
2431 * Windows only shows the popup menu on the button up event.
2432 */
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002433#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (!is_click)
2435 return FALSE;
2436#endif
2437#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2438 if (is_click || is_drag)
2439 return FALSE;
2440#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002441#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2443 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2444 if (gui.in_use)
2445 {
2446 jump_flags = 0;
2447 if (STRCMP(p_mousem, "popup_setpos") == 0)
2448 {
2449 /* First set the cursor position before showing the popup
2450 * menu. */
2451#ifdef FEAT_VISUAL
2452 if (VIsual_active)
2453 {
2454 pos_T m_pos;
2455
2456 /*
2457 * set MOUSE_MAY_STOP_VIS if we are outside the
2458 * selection or the current window (might have false
2459 * negative here)
2460 */
2461 if (mouse_row < W_WINROW(curwin)
2462 || mouse_row
2463 > (W_WINROW(curwin) + curwin->w_height))
2464 jump_flags = MOUSE_MAY_STOP_VIS;
2465 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2466 jump_flags = MOUSE_MAY_STOP_VIS;
2467 else
2468 {
2469 if ((lt(curwin->w_cursor, VIsual)
2470 && (lt(m_pos, curwin->w_cursor)
2471 || lt(VIsual, m_pos)))
2472 || (lt(VIsual, curwin->w_cursor)
2473 && (lt(m_pos, VIsual)
2474 || lt(curwin->w_cursor, m_pos))))
2475 {
2476 jump_flags = MOUSE_MAY_STOP_VIS;
2477 }
2478 else if (VIsual_mode == Ctrl_V)
2479 {
2480 getvcols(curwin, &curwin->w_cursor, &VIsual,
2481 &leftcol, &rightcol);
2482 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2483 if (m_pos.col < leftcol || m_pos.col > rightcol)
2484 jump_flags = MOUSE_MAY_STOP_VIS;
2485 }
2486 }
2487 }
2488 else
2489 jump_flags = MOUSE_MAY_STOP_VIS;
2490#endif
2491 }
2492 if (jump_flags)
2493 {
2494 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2495 update_curbuf(
2496#ifdef FEAT_VISUAL
2497 VIsual_active ? INVERTED :
2498#endif
2499 VALID);
2500 setcursor();
2501 out_flush(); /* Update before showing popup menu */
2502 }
2503# ifdef FEAT_MENU
2504 gui_show_popupmenu();
2505# endif
2506 return (jump_flags & CURSOR_MOVED) != 0;
2507 }
2508 else
2509 return FALSE;
2510#else
2511 return FALSE;
2512#endif
2513 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002514 if (which_button == MOUSE_LEFT
2515 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 which_button = MOUSE_RIGHT;
2518 mod_mask &= ~MOD_MASK_SHIFT;
2519 }
2520 }
2521
2522#ifdef FEAT_VISUAL
2523 if ((State & (NORMAL | INSERT))
2524 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2525 {
2526 if (which_button == MOUSE_LEFT)
2527 {
2528 if (is_click)
2529 {
2530 /* stop Visual mode for a left click in a window, but not when
2531 * on a status line */
2532 if (VIsual_active)
2533 jump_flags |= MOUSE_MAY_STOP_VIS;
2534 }
2535 else if (mouse_has(MOUSE_VISUAL))
2536 jump_flags |= MOUSE_MAY_VIS;
2537 }
2538 else if (which_button == MOUSE_RIGHT)
2539 {
2540 if (is_click && VIsual_active)
2541 {
2542 /*
2543 * Remember the start and end of visual before moving the
2544 * cursor.
2545 */
2546 if (lt(curwin->w_cursor, VIsual))
2547 {
2548 start_visual = curwin->w_cursor;
2549 end_visual = VIsual;
2550 }
2551 else
2552 {
2553 start_visual = VIsual;
2554 end_visual = curwin->w_cursor;
2555 }
2556 }
2557 jump_flags |= MOUSE_FOCUS;
2558 if (mouse_has(MOUSE_VISUAL))
2559 jump_flags |= MOUSE_MAY_VIS;
2560 }
2561 }
2562#endif
2563
2564 /*
2565 * If an operator is pending, ignore all drags and releases until the
2566 * next mouse click.
2567 */
2568 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2569 {
2570 got_click = FALSE;
2571 oap->motion_type = MCHAR;
2572 }
2573
2574 /* When releasing the button let jump_to_mouse() know. */
2575 if (!is_click && !is_drag)
2576 jump_flags |= MOUSE_RELEASED;
2577
2578 /*
2579 * JUMP!
2580 */
2581 jump_flags = jump_to_mouse(jump_flags,
2582 oap == NULL ? NULL : &(oap->inclusive), which_button);
2583 moved = (jump_flags & CURSOR_MOVED);
2584 in_status_line = (jump_flags & IN_STATUS_LINE);
2585#ifdef FEAT_VERTSPLIT
2586 in_sep_line = (jump_flags & IN_SEP_LINE);
2587#endif
2588
2589#ifdef FEAT_NETBEANS_INTG
2590 if (usingNetbeans && isNetbeansBuffer(curbuf)
2591 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2592 {
2593 int key = KEY2TERMCAP1(c);
2594
2595 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2596 || key == (int)KE_RIGHTRELEASE)
2597 netbeans_button_release(which_button);
2598 }
2599#endif
2600
2601 /* When jumping to another window, clear a pending operator. That's a bit
2602 * friendlier than beeping and not jumping to that window. */
2603 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2604 clearop(oap);
2605
2606#ifdef FEAT_FOLDING
2607 if (mod_mask == 0
2608 && !is_drag
2609 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2610 && which_button == MOUSE_LEFT)
2611 {
2612 /* open or close a fold at this line */
2613 if (jump_flags & MOUSE_FOLD_OPEN)
2614 openFold(curwin->w_cursor.lnum, 1L);
2615 else
2616 closeFold(curwin->w_cursor.lnum, 1L);
2617 /* don't move the cursor if still in the same window */
2618 if (curwin == old_curwin)
2619 curwin->w_cursor = save_cursor;
2620 }
2621#endif
2622
2623#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2624 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2625 {
2626 clip_modeless(which_button, is_click, is_drag);
2627 return FALSE;
2628 }
2629#endif
2630
2631#ifdef FEAT_VISUAL
2632 /* Set global flag that we are extending the Visual area with mouse
2633 * dragging; temporarily mimimize 'scrolloff'. */
2634 if (VIsual_active && is_drag && p_so)
2635 {
2636 /* In the very first line, allow scrolling one line */
2637 if (mouse_row == 0)
2638 mouse_dragging = 2;
2639 else
2640 mouse_dragging = 1;
2641 }
2642
2643 /* When dragging the mouse above the window, scroll down. */
2644 if (is_drag && mouse_row < 0 && !in_status_line)
2645 {
2646 scroll_redraw(FALSE, 1L);
2647 mouse_row = 0;
2648 }
2649
2650 if (start_visual.lnum) /* right click in visual mode */
2651 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002652 /* When ALT is pressed make Visual mode blockwise. */
2653 if (mod_mask & MOD_MASK_ALT)
2654 VIsual_mode = Ctrl_V;
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /*
2657 * In Visual-block mode, divide the area in four, pick up the corner
2658 * that is in the quarter that the cursor is in.
2659 */
2660 if (VIsual_mode == Ctrl_V)
2661 {
2662 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2663 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2664 end_visual.col = leftcol;
2665 else
2666 end_visual.col = rightcol;
2667 if (curwin->w_cursor.lnum <
2668 (start_visual.lnum + end_visual.lnum) / 2)
2669 end_visual.lnum = end_visual.lnum;
2670 else
2671 end_visual.lnum = start_visual.lnum;
2672
2673 /* move VIsual to the right column */
2674 start_visual = curwin->w_cursor; /* save the cursor pos */
2675 curwin->w_cursor = end_visual;
2676 coladvance(end_visual.col);
2677 VIsual = curwin->w_cursor;
2678 curwin->w_cursor = start_visual; /* restore the cursor */
2679 }
2680 else
2681 {
2682 /*
2683 * If the click is before the start of visual, change the start.
2684 * If the click is after the end of visual, change the end. If
2685 * the click is inside the visual, change the closest side.
2686 */
2687 if (lt(curwin->w_cursor, start_visual))
2688 VIsual = end_visual;
2689 else if (lt(end_visual, curwin->w_cursor))
2690 VIsual = start_visual;
2691 else
2692 {
2693 /* In the same line, compare column number */
2694 if (end_visual.lnum == start_visual.lnum)
2695 {
2696 if (curwin->w_cursor.col - start_visual.col >
2697 end_visual.col - curwin->w_cursor.col)
2698 VIsual = start_visual;
2699 else
2700 VIsual = end_visual;
2701 }
2702
2703 /* In different lines, compare line number */
2704 else
2705 {
2706 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2707 (end_visual.lnum - curwin->w_cursor.lnum);
2708
2709 if (diff > 0) /* closest to end */
2710 VIsual = start_visual;
2711 else if (diff < 0) /* closest to start */
2712 VIsual = end_visual;
2713 else /* in the middle line */
2714 {
2715 if (curwin->w_cursor.col <
2716 (start_visual.col + end_visual.col) / 2)
2717 VIsual = end_visual;
2718 else
2719 VIsual = start_visual;
2720 }
2721 }
2722 }
2723 }
2724 }
2725 /*
2726 * If Visual mode started in insert mode, execute "CTRL-O"
2727 */
2728 else if ((State & INSERT) && VIsual_active)
2729 stuffcharReadbuff(Ctrl_O);
2730#endif
2731
2732 /*
2733 * Middle mouse click: Put text before cursor.
2734 */
2735 if (which_button == MOUSE_MIDDLE)
2736 {
2737#ifdef FEAT_CLIPBOARD
2738 if (clip_star.available && regname == 0)
2739 regname = '*';
2740#endif
2741 if (yank_register_mline(regname))
2742 {
2743 if (mouse_past_bottom)
2744 dir = FORWARD;
2745 }
2746 else if (mouse_past_eol)
2747 dir = FORWARD;
2748
2749 if (fixindent)
2750 {
2751 c1 = (dir == BACKWARD) ? '[' : ']';
2752 c2 = 'p';
2753 }
2754 else
2755 {
2756 c1 = (dir == FORWARD) ? 'p' : 'P';
2757 c2 = NUL;
2758 }
2759 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2760
2761 /*
2762 * Remember where the paste started, so in edit() Insstart can be set
2763 * to this position
2764 */
2765 if (restart_edit != 0)
2766 where_paste_started = curwin->w_cursor;
2767 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2768 }
2769
2770#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2771 /*
2772 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2773 * error under the mouse pointer.
2774 */
2775 else if (((mod_mask & MOD_MASK_CTRL)
2776 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2777 && bt_quickfix(curbuf))
2778 {
2779 if (State & INSERT)
2780 stuffcharReadbuff(Ctrl_O);
2781 stuffReadbuff((char_u *)":.cc\n");
2782 got_click = FALSE; /* ignore drag&release now */
2783 }
2784#endif
2785
2786 /*
2787 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2788 * under the mouse pointer.
2789 */
2790 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2791 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2792 {
2793 if (State & INSERT)
2794 stuffcharReadbuff(Ctrl_O);
2795 stuffcharReadbuff(Ctrl_RSB);
2796 got_click = FALSE; /* ignore drag&release now */
2797 }
2798
2799 /*
2800 * Shift-Mouse click searches for the next occurrence of the word under
2801 * the mouse pointer
2802 */
2803 else if ((mod_mask & MOD_MASK_SHIFT))
2804 {
2805 if (State & INSERT
2806#ifdef FEAT_VISUAL
2807 || (VIsual_active && VIsual_select)
2808#endif
2809 )
2810 stuffcharReadbuff(Ctrl_O);
2811 if (which_button == MOUSE_LEFT)
2812 stuffcharReadbuff('*');
2813 else /* MOUSE_RIGHT */
2814 stuffcharReadbuff('#');
2815 }
2816
2817 /* Handle double clicks, unless on status line */
2818 else if (in_status_line)
2819 {
2820#ifdef FEAT_MOUSESHAPE
2821 if ((is_drag || is_click) && !drag_status_line)
2822 {
2823 drag_status_line = TRUE;
2824 update_mouseshape(-1);
2825 }
2826#endif
2827 }
2828#ifdef FEAT_VERTSPLIT
2829 else if (in_sep_line)
2830 {
2831# ifdef FEAT_MOUSESHAPE
2832 if ((is_drag || is_click) && !drag_sep_line)
2833 {
2834 drag_sep_line = TRUE;
2835 update_mouseshape(-1);
2836 }
2837# endif
2838 }
2839#endif
2840#ifdef FEAT_VISUAL
2841 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2842 && mouse_has(MOUSE_VISUAL))
2843 {
2844 if (is_click || !VIsual_active)
2845 {
2846 if (VIsual_active)
2847 orig_cursor = VIsual;
2848 else
2849 {
2850 check_visual_highlight();
2851 VIsual = curwin->w_cursor;
2852 orig_cursor = VIsual;
2853 VIsual_active = TRUE;
2854 VIsual_reselect = TRUE;
2855 /* start Select mode if 'selectmode' contains "mouse" */
2856 may_start_select('o');
2857 setmouse();
2858 }
2859 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00002860 {
2861 /* Double click with ALT pressed makes it blockwise. */
2862 if (mod_mask & MOD_MASK_ALT)
2863 VIsual_mode = Ctrl_V;
2864 else
2865 VIsual_mode = 'v';
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2868 VIsual_mode = 'V';
2869 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2870 VIsual_mode = Ctrl_V;
2871#ifdef FEAT_CLIPBOARD
2872 /* Make sure the clipboard gets updated. Needed because start and
2873 * end may still be the same, and the selection needs to be owned */
2874 clip_star.vmode = NUL;
2875#endif
2876 }
2877 /*
2878 * A double click selects a word or a block.
2879 */
2880 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2881 {
2882 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002883 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885 if (is_click)
2886 {
2887 /* If the character under the cursor (skipping white space) is
2888 * not a word character, try finding a match and select a (),
2889 * {}, [], #if/#endif, etc. block. */
2890 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002891 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 inc(&end_visual);
2893 if (oap != NULL)
2894 oap->motion_type = MCHAR;
2895 if (oap != NULL
2896 && VIsual_mode == 'v'
2897 && !vim_iswordc(gchar_pos(&end_visual))
2898 && equalpos(curwin->w_cursor, VIsual)
2899 && (pos = findmatch(oap, NUL)) != NULL)
2900 {
2901 curwin->w_cursor = *pos;
2902 if (oap->motion_type == MLINE)
2903 VIsual_mode = 'V';
2904 else if (*p_sel == 'e')
2905 {
2906 if (lt(curwin->w_cursor, VIsual))
2907 ++VIsual.col;
2908 else
2909 ++curwin->w_cursor.col;
2910 }
2911 }
2912 }
2913
2914 if (pos == NULL && (is_click || is_drag))
2915 {
2916 /* When not found a match or when dragging: extend to include
2917 * a word. */
2918 if (lt(curwin->w_cursor, orig_cursor))
2919 {
2920 find_start_of_word(&curwin->w_cursor);
2921 find_end_of_word(&VIsual);
2922 }
2923 else
2924 {
2925 find_start_of_word(&VIsual);
2926 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
2927#ifdef FEAT_MBYTE
2928 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002929 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930#else
2931 ++curwin->w_cursor.col;
2932#endif
2933 find_end_of_word(&curwin->w_cursor);
2934 }
2935 }
2936 curwin->w_set_curswant = TRUE;
2937 }
2938 if (is_click)
2939 redraw_curbuf_later(INVERTED); /* update the inversion */
2940 }
2941 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00002942 {
2943 if (mod_mask & MOD_MASK_ALT)
2944 VIsual_mode = Ctrl_V;
2945 else
2946 VIsual_mode = 'v';
2947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /* If Visual mode changed show it later. */
2950 if (p_smd && (VIsual_active != old_active || VIsual_mode != old_mode))
2951 redraw_cmdline = TRUE;
2952#endif
2953
2954 return moved;
2955}
2956
2957#ifdef FEAT_VISUAL
2958/*
2959 * Move "pos" back to the start of the word it's in.
2960 */
2961 static void
2962find_start_of_word(pos)
2963 pos_T *pos;
2964{
2965 char_u *line;
2966 int cclass;
2967 int col;
2968
2969 line = ml_get(pos->lnum);
2970 cclass = get_mouse_class(line + pos->col);
2971
2972 while (pos->col > 0)
2973 {
2974 col = pos->col - 1;
2975#ifdef FEAT_MBYTE
2976 col -= (*mb_head_off)(line, line + col);
2977#endif
2978 if (get_mouse_class(line + col) != cclass)
2979 break;
2980 pos->col = col;
2981 }
2982}
2983
2984/*
2985 * Move "pos" forward to the end of the word it's in.
2986 * When 'selection' is "exclusive", the position is just after the word.
2987 */
2988 static void
2989find_end_of_word(pos)
2990 pos_T *pos;
2991{
2992 char_u *line;
2993 int cclass;
2994 int col;
2995
2996 line = ml_get(pos->lnum);
2997 if (*p_sel == 'e' && pos->col > 0)
2998 {
2999 --pos->col;
3000#ifdef FEAT_MBYTE
3001 pos->col -= (*mb_head_off)(line, line + pos->col);
3002#endif
3003 }
3004 cclass = get_mouse_class(line + pos->col);
3005 while (line[pos->col] != NUL)
3006 {
3007#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003008 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#else
3010 col = pos->col + 1;
3011#endif
3012 if (get_mouse_class(line + col) != cclass)
3013 {
3014 if (*p_sel == 'e')
3015 pos->col = col;
3016 break;
3017 }
3018 pos->col = col;
3019 }
3020}
3021
3022/*
3023 * Get class of a character for selection: same class means same word.
3024 * 0: blank
3025 * 1: punctuation groups
3026 * 2: normal word character
3027 * >2: multi-byte word character.
3028 */
3029 static int
3030get_mouse_class(p)
3031 char_u *p;
3032{
3033 int c;
3034
3035#ifdef FEAT_MBYTE
3036 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3037 return mb_get_class(p);
3038#endif
3039
3040 c = *p;
3041 if (c == ' ' || c == '\t')
3042 return 0;
3043
3044 if (vim_iswordc(c))
3045 return 2;
3046
3047 /*
3048 * There are a few special cases where we want certain combinations of
3049 * characters to be considered as a single word. These are things like
3050 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3051 * character is in it's own class.
3052 */
3053 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3054 return 1;
3055 return c;
3056}
3057#endif /* FEAT_VISUAL */
3058#endif /* FEAT_MOUSE */
3059
3060#if defined(FEAT_VISUAL) || defined(PROTO)
3061/*
3062 * Check if highlighting for visual mode is possible, give a warning message
3063 * if not.
3064 */
3065 void
3066check_visual_highlight()
3067{
3068 static int did_check = FALSE;
3069
3070 if (full_screen)
3071 {
3072 if (!did_check && hl_attr(HLF_V) == 0)
3073 MSG(_("Warning: terminal cannot highlight"));
3074 did_check = TRUE;
3075 }
3076}
3077
3078/*
3079 * End visual mode.
3080 * This function should ALWAYS be called to end Visual mode, except from
3081 * do_pending_operator().
3082 */
3083 void
3084end_visual_mode()
3085{
3086#ifdef FEAT_CLIPBOARD
3087 /*
3088 * If we are using the clipboard, then remember what was selected in case
3089 * we need to paste it somewhere while we still own the selection.
3090 * Only do this when the clipboard is already owned. Don't want to grab
3091 * the selection when hitting ESC.
3092 */
3093 if (clip_star.available && clip_star.owned)
3094 clip_auto_select();
3095#endif
3096
3097 VIsual_active = FALSE;
3098#ifdef FEAT_MOUSE
3099 setmouse();
3100 mouse_dragging = 0;
3101#endif
3102
3103 /* Save the current VIsual area for '< and '> marks, and "gv" */
3104 curbuf->b_visual_mode = VIsual_mode;
3105#ifdef FEAT_EVAL
3106 curbuf->b_visual_mode_eval = VIsual_mode;
3107#endif
3108 curbuf->b_visual_start = VIsual;
3109 curbuf->b_visual_end = curwin->w_cursor;
3110 curbuf->b_visual_curswant = curwin->w_curswant;
3111#ifdef FEAT_VIRTUALEDIT
3112 if (!virtual_active())
3113 curwin->w_cursor.coladd = 0;
3114#endif
3115
3116 if (p_smd)
3117 clear_cmdline = TRUE; /* unshow visual mode later */
3118#ifdef FEAT_CMDL_INFO
3119 else
3120 clear_showcmd();
3121#endif
3122
3123 /* Don't leave the cursor past the end of the line */
3124 if (curwin->w_cursor.col > 0 && *ml_get_cursor() == NUL)
3125 --curwin->w_cursor.col;
3126}
3127
3128/*
3129 * Reset VIsual_active and VIsual_reselect.
3130 */
3131 void
3132reset_VIsual_and_resel()
3133{
3134 if (VIsual_active)
3135 {
3136 end_visual_mode();
3137 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3138 }
3139 VIsual_reselect = FALSE;
3140}
3141
3142/*
3143 * Reset VIsual_active and VIsual_reselect if it's set.
3144 */
3145 void
3146reset_VIsual()
3147{
3148 if (VIsual_active)
3149 {
3150 end_visual_mode();
3151 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3152 VIsual_reselect = FALSE;
3153 }
3154}
3155#endif /* FEAT_VISUAL */
3156
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003157#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3159
3160/*
3161 * Check for a balloon-eval special item to include when searching for an
3162 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3163 * Returns TRUE if the character at "*ptr" should be included.
3164 * "dir" is FORWARD or BACKWARD, the direction of searching.
3165 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3166 * "bnp" points to a counter for square brackets.
3167 */
3168 static int
3169find_is_eval_item(ptr, colp, bnp, dir)
3170 char_u *ptr;
3171 int *colp;
3172 int *bnp;
3173 int dir;
3174{
3175 /* Accept everything inside []. */
3176 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3177 ++*bnp;
3178 if (*bnp > 0)
3179 {
3180 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3181 --*bnp;
3182 return TRUE;
3183 }
3184
3185 /* skip over "s.var" */
3186 if (*ptr == '.')
3187 return TRUE;
3188
3189 /* two-character item: s->var */
3190 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3191 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3192 {
3193 *colp += dir;
3194 return TRUE;
3195 }
3196 return FALSE;
3197}
3198#endif
3199
3200/*
3201 * Find the identifier under or to the right of the cursor.
3202 * "find_type" can have one of three values:
3203 * FIND_IDENT: find an identifier (keyword)
3204 * FIND_STRING: find any non-white string
3205 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003206 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 *
3208 * There are three steps:
3209 * 1. Search forward for the start of an identifier/string. Doesn't move if
3210 * already on one.
3211 * 2. Search backward for the start of this identifier/string.
3212 * This doesn't match the real Vi but I like it a little better and it
3213 * shouldn't bother anyone.
3214 * 3. Search forward to the end of this identifier/string.
3215 * When FIND_IDENT isn't defined, we backup until a blank.
3216 *
3217 * Returns the length of the string, or zero if no string is found.
3218 * If a string is found, a pointer to the string is put in "*string". This
3219 * string is not always NUL terminated.
3220 */
3221 int
3222find_ident_under_cursor(string, find_type)
3223 char_u **string;
3224 int find_type;
3225{
3226 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3227 curwin->w_cursor.col, string, find_type);
3228}
3229
3230/*
3231 * Like find_ident_under_cursor(), but for any window and any position.
3232 * However: Uses 'iskeyword' from the current window!.
3233 */
3234 int
3235find_ident_at_pos(wp, lnum, startcol, string, find_type)
3236 win_T *wp;
3237 linenr_T lnum;
3238 colnr_T startcol;
3239 char_u **string;
3240 int find_type;
3241{
3242 char_u *ptr;
3243 int col = 0; /* init to shut up GCC */
3244 int i;
3245#ifdef FEAT_MBYTE
3246 int this_class = 0;
3247 int prev_class;
3248 int prevcol;
3249#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003250#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 int bn = 0; /* bracket nesting */
3252#endif
3253
3254 /*
3255 * if i == 0: try to find an identifier
3256 * if i == 1: try to find any non-white string
3257 */
3258 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3259 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3260 {
3261 /*
3262 * 1. skip to start of identifier/string
3263 */
3264 col = startcol;
3265#ifdef FEAT_MBYTE
3266 if (has_mbyte)
3267 {
3268 while (ptr[col] != NUL)
3269 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 /* Stop at a ']' to evaluate "a[x]". */
3272 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3273 break;
3274# endif
3275 this_class = mb_get_class(ptr + col);
3276 if (this_class != 0 && (i == 1 || this_class != 1))
3277 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003278 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280 }
3281 else
3282#endif
3283 while (ptr[col] != NUL
3284 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3287# endif
3288 )
3289 ++col;
3290
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003291#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 /* When starting on a ']' count it, so that we include the '['. */
3293 bn = ptr[col] == ']';
3294#endif
3295
3296 /*
3297 * 2. Back up to start of identifier/string.
3298 */
3299#ifdef FEAT_MBYTE
3300 if (has_mbyte)
3301 {
3302 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003303# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3305 this_class = mb_get_class((char_u *)"a");
3306 else
3307# endif
3308 this_class = mb_get_class(ptr + col);
3309 while (col > 0)
3310 {
3311 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3312 prev_class = mb_get_class(ptr + prevcol);
3313 if (this_class != prev_class
3314 && (i == 0
3315 || prev_class == 0
3316 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 && (!(find_type & FIND_EVAL)
3319 || prevcol == 0
3320 || !find_is_eval_item(ptr + prevcol, &prevcol,
3321 &bn, BACKWARD))
3322# endif
3323 )
3324 break;
3325 col = prevcol;
3326 }
3327
3328 /* If we don't want just any old string, or we've found an
3329 * identifier, stop searching. */
3330 if (this_class > 2)
3331 this_class = 2;
3332 if (!(find_type & FIND_STRING) || this_class == 2)
3333 break;
3334 }
3335 else
3336#endif
3337 {
3338 while (col > 0
3339 && ((i == 0
3340 ? vim_iswordc(ptr[col - 1])
3341 : (!vim_iswhite(ptr[col - 1])
3342 && (!(find_type & FIND_IDENT)
3343 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003344#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 || ((find_type & FIND_EVAL)
3346 && col > 1
3347 && find_is_eval_item(ptr + col - 1, &col,
3348 &bn, BACKWARD))
3349#endif
3350 ))
3351 --col;
3352
3353 /* If we don't want just any old string, or we've found an
3354 * identifier, stop searching. */
3355 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3356 break;
3357 }
3358 }
3359
3360 if (ptr[col] == NUL || (i == 0 && (
3361#ifdef FEAT_MBYTE
3362 has_mbyte ? this_class != 2 :
3363#endif
3364 !vim_iswordc(ptr[col]))))
3365 {
3366 /*
3367 * didn't find an identifier or string
3368 */
3369 if (find_type & FIND_STRING)
3370 EMSG(_("E348: No string under cursor"));
3371 else
3372 EMSG(_("E349: No identifier under cursor"));
3373 return 0;
3374 }
3375 ptr += col;
3376 *string = ptr;
3377
3378 /*
3379 * 3. Find the end if the identifier/string.
3380 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003381#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 bn = 0;
3383 startcol -= col;
3384#endif
3385 col = 0;
3386#ifdef FEAT_MBYTE
3387 if (has_mbyte)
3388 {
3389 /* Search for point of changing multibyte character class. */
3390 this_class = mb_get_class(ptr);
3391 while (ptr[col] != NUL
3392 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3393 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003394# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 || ((find_type & FIND_EVAL)
3396 && col <= (int)startcol
3397 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3398# endif
3399 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003400 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 else
3403#endif
3404 while ((i == 0 ? vim_iswordc(ptr[col])
3405 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 || ((find_type & FIND_EVAL)
3408 && col <= (int)startcol
3409 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3410# endif
3411 )
3412 {
3413 ++col;
3414 }
3415
3416 return col;
3417}
3418
3419/*
3420 * Prepare for redo of a normal command.
3421 */
3422 static void
3423prep_redo_cmd(cap)
3424 cmdarg_T *cap;
3425{
3426 prep_redo(cap->oap->regname, cap->count0,
3427 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3428}
3429
3430/*
3431 * Prepare for redo of any command.
3432 * Note that only the last argument can be a multi-byte char.
3433 */
3434 static void
3435prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3436 int regname;
3437 long num;
3438 int cmd1;
3439 int cmd2;
3440 int cmd3;
3441 int cmd4;
3442 int cmd5;
3443{
3444 ResetRedobuff();
3445 if (regname != 0) /* yank from specified buffer */
3446 {
3447 AppendCharToRedobuff('"');
3448 AppendCharToRedobuff(regname);
3449 }
3450 if (num)
3451 AppendNumberToRedobuff(num);
3452
3453 if (cmd1 != NUL)
3454 AppendCharToRedobuff(cmd1);
3455 if (cmd2 != NUL)
3456 AppendCharToRedobuff(cmd2);
3457 if (cmd3 != NUL)
3458 AppendCharToRedobuff(cmd3);
3459 if (cmd4 != NUL)
3460 AppendCharToRedobuff(cmd4);
3461 if (cmd5 != NUL)
3462 AppendCharToRedobuff(cmd5);
3463}
3464
3465/*
3466 * check for operator active and clear it
3467 *
3468 * return TRUE if operator was active
3469 */
3470 static int
3471checkclearop(oap)
3472 oparg_T *oap;
3473{
3474 if (oap->op_type == OP_NOP)
3475 return FALSE;
3476 clearopbeep(oap);
3477 return TRUE;
3478}
3479
3480/*
3481 * check for operator or Visual active and clear it
3482 *
3483 * return TRUE if operator was active
3484 */
3485 static int
3486checkclearopq(oap)
3487 oparg_T *oap;
3488{
3489 if (oap->op_type == OP_NOP
3490#ifdef FEAT_VISUAL
3491 && !VIsual_active
3492#endif
3493 )
3494 return FALSE;
3495 clearopbeep(oap);
3496 return TRUE;
3497}
3498
3499 static void
3500clearop(oap)
3501 oparg_T *oap;
3502{
3503 oap->op_type = OP_NOP;
3504 oap->regname = 0;
3505 oap->motion_force = NUL;
3506 oap->use_reg_one = FALSE;
3507}
3508
3509 static void
3510clearopbeep(oap)
3511 oparg_T *oap;
3512{
3513 clearop(oap);
3514 beep_flush();
3515}
3516
3517#ifdef FEAT_VISUAL
3518/*
3519 * Remove the shift modifier from a special key.
3520 */
3521 static void
3522unshift_special(cap)
3523 cmdarg_T *cap;
3524{
3525 switch (cap->cmdchar)
3526 {
3527 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3528 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3529 case K_S_UP: cap->cmdchar = K_UP; break;
3530 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3531 case K_S_HOME: cap->cmdchar = K_HOME; break;
3532 case K_S_END: cap->cmdchar = K_END; break;
3533 }
3534 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3535}
3536#endif
3537
3538#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3539/*
3540 * Routines for displaying a partly typed command
3541 */
3542
3543#ifdef FEAT_VISUAL /* need room for size of Visual area */
3544# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3545#else
3546# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3547#endif
3548static char_u showcmd_buf[SHOWCMD_BUFLEN];
3549static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3550static int showcmd_is_clear = TRUE;
3551static int showcmd_visual = FALSE;
3552
3553static void display_showcmd __ARGS((void));
3554
3555 void
3556clear_showcmd()
3557{
3558 if (!p_sc)
3559 return;
3560
3561#ifdef FEAT_VISUAL
3562 if (VIsual_active && !char_avail())
3563 {
3564 int i = lt(VIsual, curwin->w_cursor);
3565 long lines;
3566 colnr_T leftcol, rightcol;
3567 linenr_T top, bot;
3568
3569 /* Show the size of the Visual area. */
3570 if (i)
3571 {
3572 top = VIsual.lnum;
3573 bot = curwin->w_cursor.lnum;
3574 }
3575 else
3576 {
3577 top = curwin->w_cursor.lnum;
3578 bot = VIsual.lnum;
3579 }
3580# ifdef FEAT_FOLDING
3581 /* Include closed folds as a whole. */
3582 hasFolding(top, &top, NULL);
3583 hasFolding(bot, NULL, &bot);
3584# endif
3585 lines = bot - top + 1;
3586
3587 if (VIsual_mode == Ctrl_V)
3588 {
3589 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3590 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3591 (long)(rightcol - leftcol + 1));
3592 }
3593 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3594 sprintf((char *)showcmd_buf, "%ld", lines);
3595 else
3596 sprintf((char *)showcmd_buf, "%ld", (long)(i
3597 ? curwin->w_cursor.col - VIsual.col
3598 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3599 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3600 showcmd_visual = TRUE;
3601 }
3602 else
3603#endif
3604 {
3605 showcmd_buf[0] = NUL;
3606 showcmd_visual = FALSE;
3607
3608 /* Don't actually display something if there is nothing to clear. */
3609 if (showcmd_is_clear)
3610 return;
3611 }
3612
3613 display_showcmd();
3614}
3615
3616/*
3617 * Add 'c' to string of shown command chars.
3618 * Return TRUE if output has been written (and setcursor() has been called).
3619 */
3620 int
3621add_to_showcmd(c)
3622 int c;
3623{
3624 char_u *p;
3625 int old_len;
3626 int extra_len;
3627 int overflow;
3628#if defined(FEAT_MOUSE)
3629 int i;
3630 static int ignore[] =
3631 {
3632#ifdef FEAT_GUI
3633 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3634 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3635#endif
3636 K_IGNORE,
3637 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3638 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3639 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3640 K_MOUSEDOWN, K_MOUSEUP,
3641 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003642 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 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
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005744 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
5746 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005747 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 return;
5749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005751 ptr = grab_file_name(cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
5753 if (ptr != NULL)
5754 {
5755 /* do autowrite if necessary */
5756 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5757 autowrite(curbuf, FALSE);
5758 setpcmark();
5759 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5760 P_HID(curbuf) ? ECMD_HIDE : 0);
5761 vim_free(ptr);
5762 }
5763 else
5764 clearop(cap->oap);
5765}
5766#endif
5767
5768/*
5769 * <End> command: to end of current line or last line.
5770 */
5771 static void
5772nv_end(cap)
5773 cmdarg_T *cap;
5774{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005775 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005777 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 nv_goto(cap);
5779 cap->count1 = 1; /* to end of current line */
5780 }
5781 nv_dollar(cap);
5782}
5783
5784/*
5785 * Handle the "$" command.
5786 */
5787 static void
5788nv_dollar(cap)
5789 cmdarg_T *cap;
5790{
5791 cap->oap->motion_type = MCHAR;
5792 cap->oap->inclusive = TRUE;
5793#ifdef FEAT_VIRTUALEDIT
5794 /* In virtual mode when off the edge of a line and an operator
5795 * is pending (whew!) keep the cursor where it is.
5796 * Otherwise, send it to the end of the line. */
5797 if (!virtual_active() || gchar_cursor() != NUL
5798 || cap->oap->op_type == OP_NOP)
5799#endif
5800 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5801 if (cursor_down((long)(cap->count1 - 1),
5802 cap->oap->op_type == OP_NOP) == FAIL)
5803 clearopbeep(cap->oap);
5804#ifdef FEAT_FOLDING
5805 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5806 foldOpenCursor();
5807#endif
5808}
5809
5810/*
5811 * Implementation of '?' and '/' commands.
5812 * If cap->arg is TRUE don't set PC mark.
5813 */
5814 static void
5815nv_search(cap)
5816 cmdarg_T *cap;
5817{
5818 oparg_T *oap = cap->oap;
5819
5820 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5821 {
5822 /* Translate "g??" to "g?g?" */
5823 cap->cmdchar = 'g';
5824 cap->nchar = '?';
5825 nv_operator(cap);
5826 return;
5827 }
5828
5829 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5830
5831 if (cap->searchbuf == NULL)
5832 {
5833 clearop(oap);
5834 return;
5835 }
5836
5837 normal_search(cap, cap->cmdchar, cap->searchbuf,
5838 (cap->arg ? 0 : SEARCH_MARK));
5839}
5840
5841/*
5842 * Handle "N" and "n" commands.
5843 * cap->arg is SEARCH_REV for "N", 0 for "n".
5844 */
5845 static void
5846nv_next(cap)
5847 cmdarg_T *cap;
5848{
5849 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5850}
5851
5852/*
5853 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5854 * Uses only cap->count1 and cap->oap from "cap".
5855 */
5856 static void
5857normal_search(cap, dir, pat, opt)
5858 cmdarg_T *cap;
5859 int dir;
5860 char_u *pat;
5861 int opt; /* extra flags for do_search() */
5862{
5863 int i;
5864
5865 cap->oap->motion_type = MCHAR;
5866 cap->oap->inclusive = FALSE;
5867 cap->oap->use_reg_one = TRUE;
5868 curwin->w_set_curswant = TRUE;
5869
5870 i = do_search(cap->oap, dir, pat, cap->count1,
5871 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
5872 if (i == 0)
5873 clearop(cap->oap);
5874 else
5875 {
5876 if (i == 2)
5877 cap->oap->motion_type = MLINE;
5878#ifdef FEAT_VIRTUALEDIT
5879 curwin->w_cursor.coladd = 0;
5880#endif
5881#ifdef FEAT_FOLDING
5882 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5883 foldOpenCursor();
5884#endif
5885 }
5886
5887 /* "/$" will put the cursor after the end of the line, may need to
5888 * correct that here */
5889 check_cursor();
5890}
5891
5892/*
5893 * Character search commands.
5894 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5895 * ',' and FALSE for ';'.
5896 * cap->nchar is NUL for ',' and ';' (repeat the search)
5897 */
5898 static void
5899nv_csearch(cap)
5900 cmdarg_T *cap;
5901{
5902 int t_cmd;
5903
5904 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5905 t_cmd = TRUE;
5906 else
5907 t_cmd = FALSE;
5908
5909 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5911 clearopbeep(cap->oap);
5912 else
5913 {
5914 curwin->w_set_curswant = TRUE;
5915#ifdef FEAT_VIRTUALEDIT
5916 /* Include a Tab for "tx" and for "dfx". */
5917 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5918 && (t_cmd || cap->oap->op_type != OP_NOP))
5919 {
5920 colnr_T scol, ecol;
5921
5922 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5923 curwin->w_cursor.coladd = ecol - scol;
5924 }
5925 else
5926 curwin->w_cursor.coladd = 0;
5927#endif
5928#ifdef FEAT_VISUAL
5929 adjust_for_sel(cap);
5930#endif
5931#ifdef FEAT_FOLDING
5932 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5933 foldOpenCursor();
5934#endif
5935 }
5936}
5937
5938/*
5939 * "[" and "]" commands.
5940 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5941 */
5942 static void
5943nv_brackets(cap)
5944 cmdarg_T *cap;
5945{
5946 pos_T new_pos;
5947 pos_T prev_pos;
5948 pos_T *pos = NULL; /* init for GCC */
5949 pos_T old_pos; /* cursor position before command */
5950 int flag;
5951 long n;
5952 int findc;
5953 int c;
5954
5955 cap->oap->motion_type = MCHAR;
5956 cap->oap->inclusive = FALSE;
5957 old_pos = curwin->w_cursor;
5958#ifdef FEAT_VIRTUALEDIT
5959 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
5960#endif
5961
5962#ifdef FEAT_SEARCHPATH
5963 /*
5964 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5965 */
5966 if (cap->nchar == 'f')
5967 nv_gotofile(cap);
5968 else
5969#endif
5970
5971#ifdef FEAT_FIND_ID
5972 /*
5973 * Find the occurence(s) of the identifier or define under cursor
5974 * in current and included files or jump to the first occurence.
5975 *
5976 * search list jump
5977 * fwd bwd fwd bwd fwd bwd
5978 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5979 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5980 */
5981 if (vim_strchr((char_u *)
5982#ifdef EBCDIC
5983 "iI\005dD\067",
5984#else
5985 "iI\011dD\004",
5986#endif
5987 cap->nchar) != NULL)
5988 {
5989 char_u *ptr;
5990 int len;
5991
5992 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5993 clearop(cap->oap);
5994 else
5995 {
5996 find_pattern_in_path(ptr, 0, len, TRUE,
5997 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
5998 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
5999 cap->count1,
6000 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6001 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6002 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6003 (linenr_T)MAXLNUM);
6004 curwin->w_set_curswant = TRUE;
6005 }
6006 }
6007 else
6008#endif
6009
6010 /*
6011 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6012 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6013 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6014 * "[m" or "]m" search for prev/next start of (Java) method.
6015 * "[M" or "]M" search for prev/next end of (Java) method.
6016 */
6017 if ( (cap->cmdchar == '['
6018 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6019 || (cap->cmdchar == ']'
6020 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6021 {
6022 if (cap->nchar == '*')
6023 cap->nchar = '/';
6024 new_pos.lnum = 0;
6025 prev_pos.lnum = 0;
6026 if (cap->nchar == 'm' || cap->nchar == 'M')
6027 {
6028 if (cap->cmdchar == '[')
6029 findc = '{';
6030 else
6031 findc = '}';
6032 n = 9999;
6033 }
6034 else
6035 {
6036 findc = cap->nchar;
6037 n = cap->count1;
6038 }
6039 for ( ; n > 0; --n)
6040 {
6041 if ((pos = findmatchlimit(cap->oap, findc,
6042 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6043 {
6044 if (new_pos.lnum == 0) /* nothing found */
6045 {
6046 if (cap->nchar != 'm' && cap->nchar != 'M')
6047 clearopbeep(cap->oap);
6048 }
6049 else
6050 pos = &new_pos; /* use last one found */
6051 break;
6052 }
6053 prev_pos = new_pos;
6054 curwin->w_cursor = *pos;
6055 new_pos = *pos;
6056 }
6057 curwin->w_cursor = old_pos;
6058
6059 /*
6060 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6061 * brought us to the match for "[m" and "]M" when inside a method.
6062 * Try finding the '{' or '}' we want to be at.
6063 * Also repeat for the given count.
6064 */
6065 if (cap->nchar == 'm' || cap->nchar == 'M')
6066 {
6067 /* norm is TRUE for "]M" and "[m" */
6068 int norm = ((findc == '{') == (cap->nchar == 'm'));
6069
6070 n = cap->count1;
6071 /* found a match: we were inside a method */
6072 if (prev_pos.lnum != 0)
6073 {
6074 pos = &prev_pos;
6075 curwin->w_cursor = prev_pos;
6076 if (norm)
6077 --n;
6078 }
6079 else
6080 pos = NULL;
6081 while (n > 0)
6082 {
6083 for (;;)
6084 {
6085 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6086 {
6087 /* if not found anything, that's an error */
6088 if (pos == NULL)
6089 clearopbeep(cap->oap);
6090 n = 0;
6091 break;
6092 }
6093 c = gchar_cursor();
6094 if (c == '{' || c == '}')
6095 {
6096 /* Must have found end/start of class: use it.
6097 * Or found the place to be at. */
6098 if ((c == findc && norm) || (n == 1 && !norm))
6099 {
6100 new_pos = curwin->w_cursor;
6101 pos = &new_pos;
6102 n = 0;
6103 }
6104 /* if no match found at all, we started outside of the
6105 * class and we're inside now. Just go on. */
6106 else if (new_pos.lnum == 0)
6107 {
6108 new_pos = curwin->w_cursor;
6109 pos = &new_pos;
6110 }
6111 /* found start/end of other method: go to match */
6112 else if ((pos = findmatchlimit(cap->oap, findc,
6113 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6114 0)) == NULL)
6115 n = 0;
6116 else
6117 curwin->w_cursor = *pos;
6118 break;
6119 }
6120 }
6121 --n;
6122 }
6123 curwin->w_cursor = old_pos;
6124 if (pos == NULL && new_pos.lnum != 0)
6125 clearopbeep(cap->oap);
6126 }
6127 if (pos != NULL)
6128 {
6129 setpcmark();
6130 curwin->w_cursor = *pos;
6131 curwin->w_set_curswant = TRUE;
6132#ifdef FEAT_FOLDING
6133 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6134 && cap->oap->op_type == OP_NOP)
6135 foldOpenCursor();
6136#endif
6137 }
6138 }
6139
6140 /*
6141 * "[[", "[]", "]]" and "][": move to start or end of function
6142 */
6143 else if (cap->nchar == '[' || cap->nchar == ']')
6144 {
6145 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6146 flag = '{';
6147 else
6148 flag = '}'; /* "][" or "[]" */
6149
6150 curwin->w_set_curswant = TRUE;
6151 /*
6152 * Imitate strange Vi behaviour: When using "]]" with an operator
6153 * we also stop at '}'.
6154 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006155 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 (cap->oap->op_type != OP_NOP
6157 && cap->arg == FORWARD && flag == '{')))
6158 clearopbeep(cap->oap);
6159 else
6160 {
6161 if (cap->oap->op_type == OP_NOP)
6162 beginline(BL_WHITE | BL_FIX);
6163#ifdef FEAT_FOLDING
6164 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6165 foldOpenCursor();
6166#endif
6167 }
6168 }
6169
6170 /*
6171 * "[p", "[P", "]P" and "]p": put with indent adjustment
6172 */
6173 else if (cap->nchar == 'p' || cap->nchar == 'P')
6174 {
6175 if (!checkclearopq(cap->oap))
6176 {
6177 prep_redo_cmd(cap);
6178 do_put(cap->oap->regname,
6179 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6180 cap->count1, PUT_FIXINDENT);
6181 }
6182 }
6183
6184 /*
6185 * "['", "[`", "]'" and "]`": jump to next mark
6186 */
6187 else if (cap->nchar == '\'' || cap->nchar == '`')
6188 {
6189 pos = &curwin->w_cursor;
6190 for (n = cap->count1; n > 0; --n)
6191 {
6192 prev_pos = *pos;
6193 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6194 cap->nchar == '\'');
6195 if (pos == NULL)
6196 break;
6197 }
6198 if (pos == NULL)
6199 pos = &prev_pos;
6200 nv_cursormark(cap, cap->nchar == '\'', pos);
6201 }
6202
6203#ifdef FEAT_MOUSE
6204 /*
6205 * [ or ] followed by a middle mouse click: put selected text with
6206 * indent adjustment. Any other button just does as usual.
6207 */
6208 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6209 {
6210 (void)do_mouse(cap->oap, cap->nchar,
6211 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6212 cap->count1, PUT_FIXINDENT);
6213 }
6214#endif /* FEAT_MOUSE */
6215
6216#ifdef FEAT_FOLDING
6217 /*
6218 * "[z" and "]z": move to start or end of open fold.
6219 */
6220 else if (cap->nchar == 'z')
6221 {
6222 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6223 cap->count1) == FAIL)
6224 clearopbeep(cap->oap);
6225 }
6226#endif
6227
6228#ifdef FEAT_DIFF
6229 /*
6230 * "[c" and "]c": move to next or previous diff-change.
6231 */
6232 else if (cap->nchar == 'c')
6233 {
6234 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6235 cap->count1) == FAIL)
6236 clearopbeep(cap->oap);
6237 }
6238#endif
6239
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006240#ifdef FEAT_SYN_HL
6241 /*
6242 * "[s", "[S", "]s" and "]S": move to next spell error.
6243 */
6244 else if (cap->nchar == 's' || cap->nchar == 'S')
6245 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006246 setpcmark();
6247 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006248 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6249 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006250 {
6251 clearopbeep(cap->oap);
6252 break;
6253 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006254 }
6255#endif
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 /* Not a valid cap->nchar. */
6258 else
6259 clearopbeep(cap->oap);
6260}
6261
6262/*
6263 * Handle Normal mode "%" command.
6264 */
6265 static void
6266nv_percent(cap)
6267 cmdarg_T *cap;
6268{
6269 pos_T *pos;
6270#ifdef FEAT_FOLDING
6271 linenr_T lnum = curwin->w_cursor.lnum;
6272#endif
6273
6274 cap->oap->inclusive = TRUE;
6275 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6276 {
6277 if (cap->count0 > 100)
6278 clearopbeep(cap->oap);
6279 else
6280 {
6281 cap->oap->motion_type = MLINE;
6282 setpcmark();
6283 /* Round up, so CTRL-G will give same value. Watch out for a
6284 * large line count, the line number must not go negative! */
6285 if (curbuf->b_ml.ml_line_count > 1000000)
6286 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6287 / 100L * cap->count0;
6288 else
6289 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6290 cap->count0 + 99L) / 100L;
6291 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6292 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6293 beginline(BL_SOL | BL_FIX);
6294 }
6295 }
6296 else /* "%" : go to matching paren */
6297 {
6298 cap->oap->motion_type = MCHAR;
6299 cap->oap->use_reg_one = TRUE;
6300 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6301 clearopbeep(cap->oap);
6302 else
6303 {
6304 setpcmark();
6305 curwin->w_cursor = *pos;
6306 curwin->w_set_curswant = TRUE;
6307#ifdef FEAT_VIRTUALEDIT
6308 curwin->w_cursor.coladd = 0;
6309#endif
6310#ifdef FEAT_VISUAL
6311 adjust_for_sel(cap);
6312#endif
6313 }
6314 }
6315#ifdef FEAT_FOLDING
6316 if (cap->oap->op_type == OP_NOP
6317 && lnum != curwin->w_cursor.lnum
6318 && (fdo_flags & FDO_PERCENT)
6319 && KeyTyped)
6320 foldOpenCursor();
6321#endif
6322}
6323
6324/*
6325 * Handle "(" and ")" commands.
6326 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6327 */
6328 static void
6329nv_brace(cap)
6330 cmdarg_T *cap;
6331{
6332 cap->oap->motion_type = MCHAR;
6333 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006334 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6335 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 curwin->w_set_curswant = TRUE;
6337
6338 if (findsent(cap->arg, cap->count1) == FAIL)
6339 clearopbeep(cap->oap);
6340 else
6341 {
6342#ifdef FEAT_VIRTUALEDIT
6343 curwin->w_cursor.coladd = 0;
6344#endif
6345#ifdef FEAT_FOLDING
6346 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6347 foldOpenCursor();
6348#endif
6349 }
6350}
6351
6352/*
6353 * "m" command: Mark a position.
6354 */
6355 static void
6356nv_mark(cap)
6357 cmdarg_T *cap;
6358{
6359 if (!checkclearop(cap->oap))
6360 {
6361 if (setmark(cap->nchar) == FAIL)
6362 clearopbeep(cap->oap);
6363 }
6364}
6365
6366/*
6367 * "{" and "}" commands.
6368 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6369 */
6370 static void
6371nv_findpar(cap)
6372 cmdarg_T *cap;
6373{
6374 cap->oap->motion_type = MCHAR;
6375 cap->oap->inclusive = FALSE;
6376 cap->oap->use_reg_one = TRUE;
6377 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006378 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 clearopbeep(cap->oap);
6380 else
6381 {
6382#ifdef FEAT_VIRTUALEDIT
6383 curwin->w_cursor.coladd = 0;
6384#endif
6385#ifdef FEAT_FOLDING
6386 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6387 foldOpenCursor();
6388#endif
6389 }
6390}
6391
6392/*
6393 * "u" command: Undo or make lower case.
6394 */
6395 static void
6396nv_undo(cap)
6397 cmdarg_T *cap;
6398{
6399 if (cap->oap->op_type == OP_LOWER
6400#ifdef FEAT_VISUAL
6401 || VIsual_active
6402#endif
6403 )
6404 {
6405 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6406 cap->cmdchar = 'g';
6407 cap->nchar = 'u';
6408 nv_operator(cap);
6409 }
6410 else
6411 nv_kundo(cap);
6412}
6413
6414/*
6415 * <Undo> command.
6416 */
6417 static void
6418nv_kundo(cap)
6419 cmdarg_T *cap;
6420{
6421 if (!checkclearopq(cap->oap))
6422 {
6423 u_undo((int)cap->count1);
6424 curwin->w_set_curswant = TRUE;
6425 }
6426}
6427
6428/*
6429 * Handle the "r" command.
6430 */
6431 static void
6432nv_replace(cap)
6433 cmdarg_T *cap;
6434{
6435 char_u *ptr;
6436 int had_ctrl_v;
6437 long n;
6438
6439 if (checkclearop(cap->oap))
6440 return;
6441
6442 /* get another character */
6443 if (cap->nchar == Ctrl_V)
6444 {
6445 had_ctrl_v = Ctrl_V;
6446 cap->nchar = get_literal();
6447 /* Don't redo a multibyte character with CTRL-V. */
6448 if (cap->nchar > DEL)
6449 had_ctrl_v = NUL;
6450 }
6451 else
6452 had_ctrl_v = NUL;
6453
6454#ifdef FEAT_VISUAL
6455 /* Visual mode "r" */
6456 if (VIsual_active)
6457 {
6458 nv_operator(cap);
6459 return;
6460 }
6461#endif
6462
6463#ifdef FEAT_VIRTUALEDIT
6464 /* Break tabs, etc. */
6465 if (virtual_active())
6466 {
6467 if (u_save_cursor() == FAIL)
6468 return;
6469 if (gchar_cursor() == NUL)
6470 {
6471 /* Add extra space and put the cursor on the first one. */
6472 coladvance_force((colnr_T)(getviscol() + cap->count1));
6473 curwin->w_cursor.col -= cap->count1;
6474 }
6475 else if (gchar_cursor() == TAB)
6476 coladvance_force(getviscol());
6477 }
6478#endif
6479
6480 /*
6481 * Check for a special key or not enough characters to replace.
6482 */
6483 ptr = ml_get_cursor();
6484 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6485#ifdef FEAT_MBYTE
6486 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6487#endif
6488 )
6489 {
6490 clearopbeep(cap->oap);
6491 return;
6492 }
6493
6494 /*
6495 * Replacing with a TAB is done by edit() when it is complicated because
6496 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6497 * Other characters are done below to avoid problems with things like
6498 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6499 */
6500 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6501 {
6502 stuffnumReadbuff(cap->count1);
6503 stuffcharReadbuff('R');
6504 stuffcharReadbuff('\t');
6505 stuffcharReadbuff(ESC);
6506 return;
6507 }
6508
6509 /* save line for undo */
6510 if (u_save_cursor() == FAIL)
6511 return;
6512
6513 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6514 {
6515 /*
6516 * Replace character(s) by a single newline.
6517 * Strange vi behaviour: Only one newline is inserted.
6518 * Delete the characters here.
6519 * Insert the newline with an insert command, takes care of
6520 * autoindent. The insert command depends on being on the last
6521 * character of a line or not.
6522 */
6523#ifdef FEAT_MBYTE
6524 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6525#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006526 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527#endif
6528 stuffcharReadbuff('\r');
6529 stuffcharReadbuff(ESC);
6530
6531 /* Give 'r' to edit(), to get the redo command right. */
6532 invoke_edit(cap, TRUE, 'r', FALSE);
6533 }
6534 else
6535 {
6536 prep_redo(cap->oap->regname, cap->count1,
6537 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6538
6539 curbuf->b_op_start = curwin->w_cursor;
6540#ifdef FEAT_MBYTE
6541 if (has_mbyte)
6542 {
6543 int old_State = State;
6544
6545 if (cap->ncharC1 != 0)
6546 AppendCharToRedobuff(cap->ncharC1);
6547 if (cap->ncharC2 != 0)
6548 AppendCharToRedobuff(cap->ncharC2);
6549
6550 /* This is slow, but it handles replacing a single-byte with a
6551 * multi-byte and the other way around. Also handles adding
6552 * composing characters for utf-8. */
6553 for (n = cap->count1; n > 0; --n)
6554 {
6555 State = REPLACE;
6556 ins_char(cap->nchar);
6557 State = old_State;
6558 if (cap->ncharC1 != 0)
6559 ins_char(cap->ncharC1);
6560 if (cap->ncharC2 != 0)
6561 ins_char(cap->ncharC2);
6562 }
6563 }
6564 else
6565#endif
6566 {
6567 /*
6568 * Replace the characters within one line.
6569 */
6570 for (n = cap->count1; n > 0; --n)
6571 {
6572 /*
6573 * Get ptr again, because u_save and/or showmatch() will have
6574 * released the line. At the same time we let know that the
6575 * line will be changed.
6576 */
6577 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6578 ptr[curwin->w_cursor.col] = cap->nchar;
6579 if (p_sm && msg_silent == 0)
6580 showmatch(cap->nchar);
6581 ++curwin->w_cursor.col;
6582 }
6583#ifdef FEAT_NETBEANS_INTG
6584 if (usingNetbeans)
6585 {
6586 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6587
Bram Moolenaar009b2592004-10-24 19:18:58 +00006588 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6589 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006591 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593#endif
6594
6595 /* mark the buffer as changed and prepare for displaying */
6596 changed_bytes(curwin->w_cursor.lnum,
6597 (colnr_T)(curwin->w_cursor.col - cap->count1));
6598 }
6599 --curwin->w_cursor.col; /* cursor on the last replaced char */
6600#ifdef FEAT_MBYTE
6601 /* if the character on the left of the current cursor is a multi-byte
6602 * character, move two characters left */
6603 if (has_mbyte)
6604 mb_adjust_cursor();
6605#endif
6606 curbuf->b_op_end = curwin->w_cursor;
6607 curwin->w_set_curswant = TRUE;
6608 set_last_insert(cap->nchar);
6609 }
6610}
6611
6612#ifdef FEAT_VISUAL
6613/*
6614 * 'o': Exchange start and end of Visual area.
6615 * 'O': same, but in block mode exchange left and right corners.
6616 */
6617 static void
6618v_swap_corners(cmdchar)
6619 int cmdchar;
6620{
6621 pos_T old_cursor;
6622 colnr_T left, right;
6623
6624 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6625 {
6626 old_cursor = curwin->w_cursor;
6627 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6628 curwin->w_cursor.lnum = VIsual.lnum;
6629 coladvance(left);
6630 VIsual = curwin->w_cursor;
6631
6632 curwin->w_cursor.lnum = old_cursor.lnum;
6633 curwin->w_curswant = right;
6634 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6635 * right one column */
6636 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6637 ++curwin->w_curswant;
6638 coladvance(curwin->w_curswant);
6639 if (curwin->w_cursor.col == old_cursor.col
6640#ifdef FEAT_VIRTUALEDIT
6641 && (!virtual_active()
6642 || curwin->w_cursor.coladd == old_cursor.coladd)
6643#endif
6644 )
6645 {
6646 curwin->w_cursor.lnum = VIsual.lnum;
6647 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6648 ++right;
6649 coladvance(right);
6650 VIsual = curwin->w_cursor;
6651
6652 curwin->w_cursor.lnum = old_cursor.lnum;
6653 coladvance(left);
6654 curwin->w_curswant = left;
6655 }
6656 }
6657 else
6658 {
6659 old_cursor = curwin->w_cursor;
6660 curwin->w_cursor = VIsual;
6661 VIsual = old_cursor;
6662 curwin->w_set_curswant = TRUE;
6663 }
6664}
6665#endif /* FEAT_VISUAL */
6666
6667/*
6668 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6669 */
6670 static void
6671nv_Replace(cap)
6672 cmdarg_T *cap;
6673{
6674#ifdef FEAT_VISUAL
6675 if (VIsual_active) /* "R" is replace lines */
6676 {
6677 cap->cmdchar = 'c';
6678 cap->nchar = NUL;
6679 VIsual_mode = 'V';
6680 nv_operator(cap);
6681 }
6682 else
6683#endif
6684 if (!checkclearopq(cap->oap))
6685 {
6686 if (!curbuf->b_p_ma)
6687 EMSG(_(e_modifiable));
6688 else
6689 {
6690#ifdef FEAT_VIRTUALEDIT
6691 if (virtual_active())
6692 coladvance(getviscol());
6693#endif
6694 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6695 }
6696 }
6697}
6698
6699#ifdef FEAT_VREPLACE
6700/*
6701 * "gr".
6702 */
6703 static void
6704nv_vreplace(cap)
6705 cmdarg_T *cap;
6706{
6707# ifdef FEAT_VISUAL
6708 if (VIsual_active)
6709 {
6710 cap->cmdchar = 'r';
6711 cap->nchar = cap->extra_char;
6712 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6713 }
6714 else
6715# endif
6716 if (!checkclearopq(cap->oap))
6717 {
6718 if (!curbuf->b_p_ma)
6719 EMSG(_(e_modifiable));
6720 else
6721 {
6722 if (cap->extra_char == Ctrl_V) /* get another character */
6723 cap->extra_char = get_literal();
6724 stuffcharReadbuff(cap->extra_char);
6725 stuffcharReadbuff(ESC);
6726# ifdef FEAT_VIRTUALEDIT
6727 if (virtual_active())
6728 coladvance(getviscol());
6729# endif
6730 invoke_edit(cap, TRUE, 'v', FALSE);
6731 }
6732 }
6733}
6734#endif
6735
6736/*
6737 * Swap case for "~" command, when it does not work like an operator.
6738 */
6739 static void
6740n_swapchar(cap)
6741 cmdarg_T *cap;
6742{
6743 long n;
6744 pos_T startpos;
6745 int did_change = 0;
6746#ifdef FEAT_NETBEANS_INTG
6747 pos_T pos;
6748 char_u *ptr;
6749 int count;
6750#endif
6751
6752 if (checkclearopq(cap->oap))
6753 return;
6754
6755 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6756 {
6757 clearopbeep(cap->oap);
6758 return;
6759 }
6760
6761 prep_redo_cmd(cap);
6762
6763 if (u_save_cursor() == FAIL)
6764 return;
6765
6766 startpos = curwin->w_cursor;
6767#ifdef FEAT_NETBEANS_INTG
6768 pos = startpos;
6769#endif
6770 for (n = cap->count1; n > 0; --n)
6771 {
6772 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6773 inc_cursor();
6774 if (gchar_cursor() == NUL)
6775 {
6776 if (vim_strchr(p_ww, '~') != NULL
6777 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6778 {
6779#ifdef FEAT_NETBEANS_INTG
6780 if (usingNetbeans)
6781 {
6782 if (did_change)
6783 {
6784 ptr = ml_get(pos.lnum);
6785 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006786 netbeans_removed(curbuf, pos.lnum, pos.col,
6787 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006789 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
6791 pos.col = 0;
6792 pos.lnum++;
6793 }
6794#endif
6795 ++curwin->w_cursor.lnum;
6796 curwin->w_cursor.col = 0;
6797 if (n > 1)
6798 {
6799 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6800 break;
6801 u_clearline();
6802 }
6803 }
6804 else
6805 break;
6806 }
6807 }
6808#ifdef FEAT_NETBEANS_INTG
6809 if (did_change && usingNetbeans)
6810 {
6811 ptr = ml_get(pos.lnum);
6812 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006813 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6814 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816#endif
6817
6818
6819 check_cursor();
6820 curwin->w_set_curswant = TRUE;
6821 if (did_change)
6822 {
6823 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6824 0L);
6825 curbuf->b_op_start = startpos;
6826 curbuf->b_op_end = curwin->w_cursor;
6827 if (curbuf->b_op_end.col > 0)
6828 --curbuf->b_op_end.col;
6829 }
6830}
6831
6832/*
6833 * Move cursor to mark.
6834 */
6835 static void
6836nv_cursormark(cap, flag, pos)
6837 cmdarg_T *cap;
6838 int flag;
6839 pos_T *pos;
6840{
6841 if (check_mark(pos) == FAIL)
6842 clearop(cap->oap);
6843 else
6844 {
6845 if (cap->cmdchar == '\''
6846 || cap->cmdchar == '`'
6847 || cap->cmdchar == '['
6848 || cap->cmdchar == ']')
6849 setpcmark();
6850 curwin->w_cursor = *pos;
6851 if (flag)
6852 beginline(BL_WHITE | BL_FIX);
6853 else
6854 check_cursor();
6855 }
6856 cap->oap->motion_type = flag ? MLINE : MCHAR;
6857 if (cap->cmdchar == '`')
6858 cap->oap->use_reg_one = TRUE;
6859 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6860 curwin->w_set_curswant = TRUE;
6861}
6862
6863#ifdef FEAT_VISUAL
6864/*
6865 * Handle commands that are operators in Visual mode.
6866 */
6867 static void
6868v_visop(cap)
6869 cmdarg_T *cap;
6870{
6871 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6872
6873 /* Uppercase means linewise, except in block mode, then "D" deletes till
6874 * the end of the line, and "C" replaces til EOL */
6875 if (isupper(cap->cmdchar))
6876 {
6877 if (VIsual_mode != Ctrl_V)
6878 VIsual_mode = 'V';
6879 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6880 curwin->w_curswant = MAXCOL;
6881 }
6882 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6883 nv_operator(cap);
6884}
6885#endif
6886
6887/*
6888 * "s" and "S" commands.
6889 */
6890 static void
6891nv_subst(cap)
6892 cmdarg_T *cap;
6893{
6894#ifdef FEAT_VISUAL
6895 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6896 {
6897 if (cap->cmdchar == 'S')
6898 VIsual_mode = 'V';
6899 cap->cmdchar = 'c';
6900 nv_operator(cap);
6901 }
6902 else
6903#endif
6904 nv_optrans(cap);
6905}
6906
6907/*
6908 * Abbreviated commands.
6909 */
6910 static void
6911nv_abbrev(cap)
6912 cmdarg_T *cap;
6913{
6914 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6915 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6916
6917#ifdef FEAT_VISUAL
6918 /* in Visual mode these commands are operators */
6919 if (VIsual_active)
6920 v_visop(cap);
6921 else
6922#endif
6923 nv_optrans(cap);
6924}
6925
6926/*
6927 * Translate a command into another command.
6928 */
6929 static void
6930nv_optrans(cap)
6931 cmdarg_T *cap;
6932{
6933 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6934 (char_u *)"d$", (char_u *)"c$",
6935 (char_u *)"cl", (char_u *)"cc",
6936 (char_u *)"yy", (char_u *)":s\r"};
6937 static char_u *str = (char_u *)"xXDCsSY&";
6938
6939 if (!checkclearopq(cap->oap))
6940 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006941 /* In Vi "2D" doesn't delete the next line. Can't translate it
6942 * either, because "2." should also not use the count. */
6943 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6944 {
6945 cap->oap->start = curwin->w_cursor;
6946 cap->oap->op_type = OP_DELETE;
6947 cap->count1 = 1;
6948 nv_dollar(cap);
6949 finish_op = TRUE;
6950 ResetRedobuff();
6951 AppendCharToRedobuff('D');
6952 }
6953 else
6954 {
6955 if (cap->count0)
6956 stuffnumReadbuff(cap->count0);
6957 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
6958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960 cap->opcount = 0;
6961}
6962
6963/*
6964 * "'" and "`" commands. Also for "g'" and "g`".
6965 * cap->arg is TRUE for "'" and "g'".
6966 */
6967 static void
6968nv_gomark(cap)
6969 cmdarg_T *cap;
6970{
6971 pos_T *pos;
6972 int c;
6973#ifdef FEAT_FOLDING
6974 linenr_T lnum = curwin->w_cursor.lnum;
6975 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6976#endif
6977
6978 if (cap->cmdchar == 'g')
6979 c = cap->extra_char;
6980 else
6981 c = cap->nchar;
6982 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6983 if (pos == (pos_T *)-1) /* jumped to other file */
6984 {
6985 if (cap->arg)
6986 {
6987 check_cursor_lnum();
6988 beginline(BL_WHITE | BL_FIX);
6989 }
6990 else
6991 check_cursor();
6992 }
6993 else
6994 nv_cursormark(cap, cap->arg, pos);
6995
6996#ifdef FEAT_VIRTUALEDIT
6997 /* May need to clear the coladd that a mark includes. */
6998 if (!virtual_active())
6999 curwin->w_cursor.coladd = 0;
7000#endif
7001#ifdef FEAT_FOLDING
7002 if (cap->oap->op_type == OP_NOP
7003 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7004 && (fdo_flags & FDO_MARK)
7005 && old_KeyTyped)
7006 foldOpenCursor();
7007#endif
7008}
7009
7010/*
7011 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7012 */
7013 static void
7014nv_pcmark(cap)
7015 cmdarg_T *cap;
7016{
7017#ifdef FEAT_JUMPLIST
7018 pos_T *pos;
7019# ifdef FEAT_FOLDING
7020 linenr_T lnum = curwin->w_cursor.lnum;
7021 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7022# endif
7023
7024 if (!checkclearopq(cap->oap))
7025 {
7026 if (cap->cmdchar == 'g')
7027 pos = movechangelist((int)cap->count1);
7028 else
7029 pos = movemark((int)cap->count1);
7030 if (pos == (pos_T *)-1) /* jump to other file */
7031 {
7032 curwin->w_set_curswant = TRUE;
7033 check_cursor();
7034 }
7035 else if (pos != NULL) /* can jump */
7036 nv_cursormark(cap, FALSE, pos);
7037 else if (cap->cmdchar == 'g')
7038 {
7039 if (curbuf->b_changelistlen == 0)
7040 EMSG(_("E664: changelist is empty"));
7041 else if (cap->count1 < 0)
7042 EMSG(_("E662: At start of changelist"));
7043 else
7044 EMSG(_("E663: At end of changelist"));
7045 }
7046 else
7047 clearopbeep(cap->oap);
7048# ifdef FEAT_FOLDING
7049 if (cap->oap->op_type == OP_NOP
7050 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7051 && (fdo_flags & FDO_MARK)
7052 && old_KeyTyped)
7053 foldOpenCursor();
7054# endif
7055 }
7056#else
7057 clearopbeep(cap->oap);
7058#endif
7059}
7060
7061/*
7062 * Handle '"' command.
7063 */
7064 static void
7065nv_regname(cap)
7066 cmdarg_T *cap;
7067{
7068 if (checkclearop(cap->oap))
7069 return;
7070#ifdef FEAT_EVAL
7071 if (cap->nchar == '=')
7072 cap->nchar = get_expr_register();
7073#endif
7074 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7075 {
7076 cap->oap->regname = cap->nchar;
7077 cap->opcount = cap->count0; /* remember count before '"' */
7078#ifdef FEAT_EVAL
7079 set_reg_var(cap->oap->regname);
7080#endif
7081 }
7082 else
7083 clearopbeep(cap->oap);
7084}
7085
7086#ifdef FEAT_VISUAL
7087/*
7088 * Handle "v", "V" and "CTRL-V" commands.
7089 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7090 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007091 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 */
7093 static void
7094nv_visual(cap)
7095 cmdarg_T *cap;
7096{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007097 if (cap->cmdchar == Ctrl_Q)
7098 cap->cmdchar = Ctrl_V;
7099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7101 * characterwise, linewise, or blockwise. */
7102 if (cap->oap->op_type != OP_NOP)
7103 {
7104 cap->oap->motion_force = cap->cmdchar;
7105 finish_op = FALSE; /* operator doesn't finish now but later */
7106 return;
7107 }
7108
7109 VIsual_select = cap->arg;
7110 if (VIsual_active) /* change Visual mode */
7111 {
7112 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7113 end_visual_mode();
7114 else /* toggle char/block mode */
7115 { /* or char/line mode */
7116 VIsual_mode = cap->cmdchar;
7117 showmode();
7118 }
7119 redraw_curbuf_later(INVERTED); /* update the inversion */
7120 }
7121 else /* start Visual mode */
7122 {
7123 check_visual_highlight();
7124 if (cap->count0) /* use previously selected part */
7125 {
7126 if (resel_VIsual_mode == NUL) /* there is none */
7127 {
7128 beep_flush();
7129 return;
7130 }
7131 VIsual = curwin->w_cursor;
7132
7133 VIsual_active = TRUE;
7134 VIsual_reselect = TRUE;
7135 if (!cap->arg)
7136 /* start Select mode when 'selectmode' contains "cmd" */
7137 may_start_select('c');
7138#ifdef FEAT_MOUSE
7139 setmouse();
7140#endif
7141 if (p_smd)
7142 redraw_cmdline = TRUE; /* show visual mode later */
7143 /*
7144 * For V and ^V, we multiply the number of lines even if there
7145 * was only one -- webb
7146 */
7147 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7148 {
7149 curwin->w_cursor.lnum +=
7150 resel_VIsual_line_count * cap->count0 - 1;
7151 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7152 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7153 }
7154 VIsual_mode = resel_VIsual_mode;
7155 if (VIsual_mode == 'v')
7156 {
7157 if (resel_VIsual_line_count <= 1)
7158 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7159 else
7160 curwin->w_cursor.col = resel_VIsual_col;
7161 check_cursor_col();
7162 }
7163 if (resel_VIsual_col == MAXCOL)
7164 {
7165 curwin->w_curswant = MAXCOL;
7166 coladvance((colnr_T)MAXCOL);
7167 }
7168 else if (VIsual_mode == Ctrl_V)
7169 {
7170 validate_virtcol();
7171 curwin->w_curswant = curwin->w_virtcol
7172 + resel_VIsual_col * cap->count0 - 1;
7173 coladvance(curwin->w_curswant);
7174 }
7175 else
7176 curwin->w_set_curswant = TRUE;
7177 redraw_curbuf_later(INVERTED); /* show the inversion */
7178 }
7179 else
7180 {
7181 if (!cap->arg)
7182 /* start Select mode when 'selectmode' contains "cmd" */
7183 may_start_select('c');
7184 n_start_visual_mode(cap->cmdchar);
7185 }
7186 }
7187}
7188
7189/*
7190 * Start selection for Shift-movement keys.
7191 */
7192 void
7193start_selection()
7194{
7195 /* if 'selectmode' contains "key", start Select mode */
7196 may_start_select('k');
7197 n_start_visual_mode('v');
7198}
7199
7200/*
7201 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7202 */
7203 void
7204may_start_select(c)
7205 int c;
7206{
7207 VIsual_select = (stuff_empty() && typebuf_typed()
7208 && (vim_strchr(p_slm, c) != NULL));
7209}
7210
7211/*
7212 * Start Visual mode "c".
7213 * Should set VIsual_select before calling this.
7214 */
7215 static void
7216n_start_visual_mode(c)
7217 int c;
7218{
7219 VIsual_mode = c;
7220 VIsual_active = TRUE;
7221 VIsual_reselect = TRUE;
7222#ifdef FEAT_VIRTUALEDIT
7223 /* Corner case: the 0 position in a tab may change when going into
7224 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7225 */
7226 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7227 coladvance(curwin->w_virtcol);
7228#endif
7229 VIsual = curwin->w_cursor;
7230
7231#ifdef FEAT_FOLDING
7232 foldAdjustVisual();
7233#endif
7234
7235#ifdef FEAT_MOUSE
7236 setmouse();
7237#endif
7238 if (p_smd)
7239 redraw_cmdline = TRUE; /* show visual mode later */
7240#ifdef FEAT_CLIPBOARD
7241 /* Make sure the clipboard gets updated. Needed because start and
7242 * end may still be the same, and the selection needs to be owned */
7243 clip_star.vmode = NUL;
7244#endif
7245
7246 /* Only need to redraw this line, unless still need to redraw an old
7247 * Visual area (when 'lazyredraw' is set). */
7248 if (curwin->w_redr_type < INVERTED)
7249 {
7250 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7251 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7252 }
7253}
7254
7255#endif /* FEAT_VISUAL */
7256
7257/*
7258 * CTRL-W: Window commands
7259 */
7260 static void
7261nv_window(cap)
7262 cmdarg_T *cap;
7263{
7264#ifdef FEAT_WINDOWS
7265 if (!checkclearop(cap->oap))
7266 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7267#else
7268 (void)checkclearop(cap->oap);
7269#endif
7270}
7271
7272/*
7273 * CTRL-Z: Suspend
7274 */
7275 static void
7276nv_suspend(cap)
7277 cmdarg_T *cap;
7278{
7279 clearop(cap->oap);
7280#ifdef FEAT_VISUAL
7281 if (VIsual_active)
7282 end_visual_mode(); /* stop Visual mode */
7283#endif
7284 do_cmdline_cmd((char_u *)"st");
7285}
7286
7287/*
7288 * Commands starting with "g".
7289 */
7290 static void
7291nv_g_cmd(cap)
7292 cmdarg_T *cap;
7293{
7294 oparg_T *oap = cap->oap;
7295#ifdef FEAT_VISUAL
7296 pos_T tpos;
7297#endif
7298 int i;
7299 int flag = FALSE;
7300
7301 switch (cap->nchar)
7302 {
7303#ifdef MEM_PROFILE
7304 /*
7305 * "g^A": dump log of used memory.
7306 */
7307 case Ctrl_A:
7308 vim_mem_profile_dump();
7309 break;
7310#endif
7311
7312#ifdef FEAT_VREPLACE
7313 /*
7314 * "gR": Enter virtual replace mode.
7315 */
7316 case 'R':
7317 cap->arg = TRUE;
7318 nv_Replace(cap);
7319 break;
7320
7321 case 'r':
7322 nv_vreplace(cap);
7323 break;
7324#endif
7325
7326 case '&':
7327 do_cmdline_cmd((char_u *)"%s//~/&");
7328 break;
7329
7330#ifdef FEAT_VISUAL
7331 /*
7332 * "gv": Reselect the previous Visual area. If Visual already active,
7333 * exchange previous and current Visual area.
7334 */
7335 case 'v':
7336 if (checkclearop(oap))
7337 break;
7338
7339 if ( curbuf->b_visual_start.lnum == 0
7340 || curbuf->b_visual_start.lnum > curbuf->b_ml.ml_line_count
7341 || curbuf->b_visual_end.lnum == 0)
7342 beep_flush();
7343 else
7344 {
7345 /* set w_cursor to the start of the Visual area, tpos to the end */
7346 if (VIsual_active)
7347 {
7348 i = VIsual_mode;
7349 VIsual_mode = curbuf->b_visual_mode;
7350 curbuf->b_visual_mode = i;
7351# ifdef FEAT_EVAL
7352 curbuf->b_visual_mode_eval = i;
7353# endif
7354 i = curwin->w_curswant;
7355 curwin->w_curswant = curbuf->b_visual_curswant;
7356 curbuf->b_visual_curswant = i;
7357
7358 tpos = curbuf->b_visual_end;
7359 curbuf->b_visual_end = curwin->w_cursor;
7360 curwin->w_cursor = curbuf->b_visual_start;
7361 curbuf->b_visual_start = VIsual;
7362 }
7363 else
7364 {
7365 VIsual_mode = curbuf->b_visual_mode;
7366 curwin->w_curswant = curbuf->b_visual_curswant;
7367 tpos = curbuf->b_visual_end;
7368 curwin->w_cursor = curbuf->b_visual_start;
7369 }
7370
7371 VIsual_active = TRUE;
7372 VIsual_reselect = TRUE;
7373
7374 /* Set Visual to the start and w_cursor to the end of the Visual
7375 * area. Make sure they are on an existing character. */
7376 check_cursor();
7377 VIsual = curwin->w_cursor;
7378 curwin->w_cursor = tpos;
7379 check_cursor();
7380 update_topline();
7381 /*
7382 * When called from normal "g" command: start Select mode when
7383 * 'selectmode' contains "cmd". When called for K_SELECT, always
7384 * start Select mode.
7385 */
7386 if (cap->arg)
7387 VIsual_select = TRUE;
7388 else
7389 may_start_select('c');
7390#ifdef FEAT_MOUSE
7391 setmouse();
7392#endif
7393#ifdef FEAT_CLIPBOARD
7394 /* Make sure the clipboard gets updated. Needed because start and
7395 * end are still the same, and the selection needs to be owned */
7396 clip_star.vmode = NUL;
7397#endif
7398 redraw_curbuf_later(INVERTED);
7399 showmode();
7400 }
7401 break;
7402 /*
7403 * "gV": Don't reselect the previous Visual area after a Select mode
7404 * mapping of menu.
7405 */
7406 case 'V':
7407 VIsual_reselect = FALSE;
7408 break;
7409
7410 /*
7411 * "gh": start Select mode.
7412 * "gH": start Select line mode.
7413 * "g^H": start Select block mode.
7414 */
7415 case K_BS:
7416 cap->nchar = Ctrl_H;
7417 /* FALLTHROUGH */
7418 case 'h':
7419 case 'H':
7420 case Ctrl_H:
7421# ifdef EBCDIC
7422 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7423 if (cap->nchar == Ctrl_H)
7424 cap->cmdchar = Ctrl_V;
7425 else
7426# endif
7427 cap->cmdchar = cap->nchar + ('v' - 'h');
7428 cap->arg = TRUE;
7429 nv_visual(cap);
7430 break;
7431#endif /* FEAT_VISUAL */
7432
7433 /*
7434 * "gj" and "gk" two new funny movement keys -- up and down
7435 * movement based on *screen* line rather than *file* line.
7436 */
7437 case 'j':
7438 case K_DOWN:
7439 /* with 'nowrap' it works just like the normal "j" command; also when
7440 * in a closed fold */
7441 if (!curwin->w_p_wrap
7442#ifdef FEAT_FOLDING
7443 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7444#endif
7445 )
7446 {
7447 oap->motion_type = MLINE;
7448 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7449 }
7450 else
7451 i = nv_screengo(oap, FORWARD, cap->count1);
7452 if (i == FAIL)
7453 clearopbeep(oap);
7454 break;
7455
7456 case 'k':
7457 case K_UP:
7458 /* with 'nowrap' it works just like the normal "k" command; also when
7459 * in a closed fold */
7460 if (!curwin->w_p_wrap
7461#ifdef FEAT_FOLDING
7462 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7463#endif
7464 )
7465 {
7466 oap->motion_type = MLINE;
7467 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7468 }
7469 else
7470 i = nv_screengo(oap, BACKWARD, cap->count1);
7471 if (i == FAIL)
7472 clearopbeep(oap);
7473 break;
7474
7475 /*
7476 * "gJ": join two lines without inserting a space.
7477 */
7478 case 'J':
7479 nv_join(cap);
7480 break;
7481
7482 /*
7483 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7484 * "gm": middle of "g0" and "g$".
7485 */
7486 case '^':
7487 flag = TRUE;
7488 /* FALLTHROUGH */
7489
7490 case '0':
7491 case 'm':
7492 case K_HOME:
7493 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 oap->motion_type = MCHAR;
7495 oap->inclusive = FALSE;
7496 if (curwin->w_p_wrap
7497#ifdef FEAT_VERTSPLIT
7498 && curwin->w_width != 0
7499#endif
7500 )
7501 {
7502 int width1 = W_WIDTH(curwin) - curwin_col_off();
7503 int width2 = width1 + curwin_col_off2();
7504
7505 validate_virtcol();
7506 i = 0;
7507 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7508 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7509 }
7510 else
7511 i = curwin->w_leftcol;
7512 /* Go to the middle of the screen line. When 'number' is on and lines
7513 * are wrapping the middle can be more to the left.*/
7514 if (cap->nchar == 'm')
7515 i += (W_WIDTH(curwin) - curwin_col_off()
7516 + ((curwin->w_p_wrap && i > 0)
7517 ? curwin_col_off2() : 0)) / 2;
7518 coladvance((colnr_T)i);
7519 if (flag)
7520 {
7521 do
7522 i = gchar_cursor();
7523 while (vim_iswhite(i) && oneright() == OK);
7524 }
7525 curwin->w_set_curswant = TRUE;
7526 break;
7527
7528 case '_':
7529 /* "g_": to the last non-blank character in the line or <count> lines
7530 * downward. */
7531 cap->oap->motion_type = MCHAR;
7532 cap->oap->inclusive = TRUE;
7533 curwin->w_curswant = MAXCOL;
7534 if (cursor_down((long)(cap->count1 - 1),
7535 cap->oap->op_type == OP_NOP) == FAIL)
7536 clearopbeep(cap->oap);
7537 else
7538 {
7539 char_u *ptr = ml_get_curline();
7540
7541 /* In Visual mode we may end up after the line. */
7542 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7543 --curwin->w_cursor.col;
7544
7545 /* Decrease the cursor column until it's on a non-blank. */
7546 while (curwin->w_cursor.col > 0
7547 && vim_iswhite(ptr[curwin->w_cursor.col]))
7548 --curwin->w_cursor.col;
7549 curwin->w_set_curswant = TRUE;
7550 }
7551 break;
7552
7553 case '$':
7554 case K_END:
7555 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 {
7557 int col_off = curwin_col_off();
7558
7559 oap->motion_type = MCHAR;
7560 oap->inclusive = TRUE;
7561 if (curwin->w_p_wrap
7562#ifdef FEAT_VERTSPLIT
7563 && curwin->w_width != 0
7564#endif
7565 )
7566 {
7567 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7568 if (cap->count1 == 1)
7569 {
7570 int width1 = W_WIDTH(curwin) - col_off;
7571 int width2 = width1 + curwin_col_off2();
7572
7573 validate_virtcol();
7574 i = width1 - 1;
7575 if (curwin->w_virtcol >= (colnr_T)width1)
7576 i += ((curwin->w_virtcol - width1) / width2 + 1)
7577 * width2;
7578 coladvance((colnr_T)i);
7579#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7580 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7581 {
7582 /*
7583 * Check for landing on a character that got split at
7584 * the end of the line. We do not want to advance to
7585 * the next screen line.
7586 */
7587 validate_virtcol();
7588 if (curwin->w_virtcol > (colnr_T)i)
7589 --curwin->w_cursor.col;
7590 }
7591#endif
7592 }
7593 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7594 clearopbeep(oap);
7595 }
7596 else
7597 {
7598 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7599 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007600
7601 /* Make sure we stick in this column. */
7602 validate_virtcol();
7603 curwin->w_curswant = curwin->w_virtcol;
7604 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 }
7607 break;
7608
7609 /*
7610 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7611 */
7612 case '*':
7613 case '#':
7614#if POUND != '#'
7615 case POUND: /* pound sign (sometimes equal to '#') */
7616#endif
7617 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7618 case ']': /* :tselect for current identifier */
7619 nv_ident(cap);
7620 break;
7621
7622 /*
7623 * ge and gE: go back to end of word
7624 */
7625 case 'e':
7626 case 'E':
7627 oap->motion_type = MCHAR;
7628 curwin->w_set_curswant = TRUE;
7629 oap->inclusive = TRUE;
7630 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7631 clearopbeep(oap);
7632 break;
7633
7634 /*
7635 * "g CTRL-G": display info about cursor position
7636 */
7637 case Ctrl_G:
7638 cursor_pos_info();
7639 break;
7640
7641 /*
7642 * "gi": start Insert at the last position.
7643 */
7644 case 'i':
7645 if (curbuf->b_last_insert.lnum != 0)
7646 {
7647 curwin->w_cursor = curbuf->b_last_insert;
7648 check_cursor_lnum();
7649 i = (int)STRLEN(ml_get_curline());
7650 if (curwin->w_cursor.col > (colnr_T)i)
7651 {
7652#ifdef FEAT_VIRTUALEDIT
7653 if (virtual_active())
7654 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7655#endif
7656 curwin->w_cursor.col = i;
7657 }
7658 }
7659 cap->cmdchar = 'i';
7660 nv_edit(cap);
7661 break;
7662
7663 /*
7664 * "gI": Start insert in column 1.
7665 */
7666 case 'I':
7667 beginline(0);
7668 if (!checkclearopq(oap))
7669 invoke_edit(cap, FALSE, 'g', FALSE);
7670 break;
7671
7672#ifdef FEAT_SEARCHPATH
7673 /*
7674 * "gf": goto file, edit file under cursor
7675 * "]f" and "[f": can also be used.
7676 */
7677 case 'f':
7678 nv_gotofile(cap);
7679 break;
7680#endif
7681
7682 /* "g'm" and "g`m": jump to mark without setting pcmark */
7683 case '\'':
7684 cap->arg = TRUE;
7685 /*FALLTHROUGH*/
7686 case '`':
7687 nv_gomark(cap);
7688 break;
7689
7690 /*
7691 * "gs": Goto sleep.
7692 */
7693 case 's':
7694 do_sleep(cap->count1 * 1000L);
7695 break;
7696
7697 /*
7698 * "ga": Display the ascii value of the character under the
7699 * cursor. It is displayed in decimal, hex, and octal. -- webb
7700 */
7701 case 'a':
7702 do_ascii(NULL);
7703 break;
7704
7705#ifdef FEAT_MBYTE
7706 /*
7707 * "g8": Display the bytes used for the UTF-8 character under the
7708 * cursor. It is displayed in hex.
7709 */
7710 case '8':
7711 show_utf8();
7712 break;
7713#endif
7714
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007715 case '<':
7716 show_sb_text();
7717 break;
7718
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 /*
7720 * "gg": Goto the first line in file. With a count it goes to
7721 * that line number like for "G". -- webb
7722 */
7723 case 'g':
7724 cap->arg = FALSE;
7725 nv_goto(cap);
7726 break;
7727
7728 /*
7729 * Two-character operators:
7730 * "gq" Format text
7731 * "gw" Format text and keep cursor position
7732 * "g~" Toggle the case of the text.
7733 * "gu" Change text to lower case.
7734 * "gU" Change text to upper case.
7735 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007736 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738 case 'q':
7739 case 'w':
7740 oap->cursor_start = curwin->w_cursor;
7741 /*FALLTHROUGH*/
7742 case '~':
7743 case 'u':
7744 case 'U':
7745 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007746 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 nv_operator(cap);
7748 break;
7749
7750 /*
7751 * "gd": Find first occurence of pattern under the cursor in the
7752 * current function
7753 * "gD": idem, but in the current file.
7754 */
7755 case 'd':
7756 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007757 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 break;
7759
7760#ifdef FEAT_MOUSE
7761 /*
7762 * g<*Mouse> : <C-*mouse>
7763 */
7764 case K_MIDDLEMOUSE:
7765 case K_MIDDLEDRAG:
7766 case K_MIDDLERELEASE:
7767 case K_LEFTMOUSE:
7768 case K_LEFTDRAG:
7769 case K_LEFTRELEASE:
7770 case K_RIGHTMOUSE:
7771 case K_RIGHTDRAG:
7772 case K_RIGHTRELEASE:
7773 case K_X1MOUSE:
7774 case K_X1DRAG:
7775 case K_X1RELEASE:
7776 case K_X2MOUSE:
7777 case K_X2DRAG:
7778 case K_X2RELEASE:
7779 mod_mask = MOD_MASK_CTRL;
7780 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7781 break;
7782#endif
7783
7784 case K_IGNORE:
7785 break;
7786
7787 /*
7788 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7789 */
7790 case 'p':
7791 case 'P':
7792 nv_put(cap);
7793 break;
7794
7795#ifdef FEAT_BYTEOFF
7796 /* "go": goto byte count from start of buffer */
7797 case 'o':
7798 goto_byte(cap->count0);
7799 break;
7800#endif
7801
7802 /* "gQ": improved Ex mode */
7803 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007804 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007807 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 break;
7809 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 if (!checkclearopq(oap))
7812 do_exmode(TRUE);
7813 break;
7814
7815#ifdef FEAT_JUMPLIST
7816 case ',':
7817 nv_pcmark(cap);
7818 break;
7819
7820 case ';':
7821 cap->count1 = -cap->count1;
7822 nv_pcmark(cap);
7823 break;
7824#endif
7825
7826 default:
7827 clearopbeep(oap);
7828 break;
7829 }
7830}
7831
7832/*
7833 * Handle "o" and "O" commands.
7834 */
7835 static void
7836n_opencmd(cap)
7837 cmdarg_T *cap;
7838{
7839 if (!checkclearopq(cap->oap))
7840 {
7841#ifdef FEAT_FOLDING
7842 if (cap->cmdchar == 'O')
7843 /* Open above the first line of a folded sequence of lines */
7844 (void)hasFolding(curwin->w_cursor.lnum,
7845 &curwin->w_cursor.lnum, NULL);
7846 else
7847 /* Open below the last line of a folded sequence of lines */
7848 (void)hasFolding(curwin->w_cursor.lnum,
7849 NULL, &curwin->w_cursor.lnum);
7850#endif
7851 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7852 (cap->cmdchar == 'O' ? 1 : 0)),
7853 (linenr_T)(curwin->w_cursor.lnum +
7854 (cap->cmdchar == 'o' ? 1 : 0))
7855 ) == OK
7856 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7857#ifdef FEAT_COMMENTS
7858 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7859#endif
7860 0, 0))
7861 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007862 /* When '#' is in 'cpoptions' ignore the count. */
7863 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7864 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7866 }
7867 }
7868}
7869
7870/*
7871 * "." command: redo last change.
7872 */
7873 static void
7874nv_dot(cap)
7875 cmdarg_T *cap;
7876{
7877 if (!checkclearopq(cap->oap))
7878 {
7879 /*
7880 * If "restart_edit" is TRUE, the last but one command is repeated
7881 * instead of the last command (inserting text). This is used for
7882 * CTRL-O <.> in insert mode.
7883 */
7884 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7885 clearopbeep(cap->oap);
7886 }
7887}
7888
7889/*
7890 * CTRL-R: undo undo
7891 */
7892 static void
7893nv_redo(cap)
7894 cmdarg_T *cap;
7895{
7896 if (!checkclearopq(cap->oap))
7897 {
7898 u_redo((int)cap->count1);
7899 curwin->w_set_curswant = TRUE;
7900 }
7901}
7902
7903/*
7904 * Handle "U" command.
7905 */
7906 static void
7907nv_Undo(cap)
7908 cmdarg_T *cap;
7909{
7910 /* In Visual mode and typing "gUU" triggers an operator */
7911 if (cap->oap->op_type == OP_UPPER
7912#ifdef FEAT_VISUAL
7913 || VIsual_active
7914#endif
7915 )
7916 {
7917 /* translate "gUU" to "gUgU" */
7918 cap->cmdchar = 'g';
7919 cap->nchar = 'U';
7920 nv_operator(cap);
7921 }
7922 else if (!checkclearopq(cap->oap))
7923 {
7924 u_undoline();
7925 curwin->w_set_curswant = TRUE;
7926 }
7927}
7928
7929/*
7930 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7931 * single character.
7932 */
7933 static void
7934nv_tilde(cap)
7935 cmdarg_T *cap;
7936{
7937 if (!p_to
7938#ifdef FEAT_VISUAL
7939 && !VIsual_active
7940#endif
7941 && cap->oap->op_type != OP_TILDE)
7942 n_swapchar(cap);
7943 else
7944 nv_operator(cap);
7945}
7946
7947/*
7948 * Handle an operator command.
7949 * The actual work is done by do_pending_operator().
7950 */
7951 static void
7952nv_operator(cap)
7953 cmdarg_T *cap;
7954{
7955 int op_type;
7956
7957 op_type = get_op_type(cap->cmdchar, cap->nchar);
7958
7959 if (op_type == cap->oap->op_type) /* double operator works on lines */
7960 nv_lineop(cap);
7961 else if (!checkclearop(cap->oap))
7962 {
7963 cap->oap->start = curwin->w_cursor;
7964 cap->oap->op_type = op_type;
7965 }
7966}
7967
7968/*
7969 * Handle linewise operator "dd", "yy", etc.
7970 *
7971 * "_" is is a strange motion command that helps make operators more logical.
7972 * It is actually implemented, but not documented in the real Vi. This motion
7973 * command actually refers to "the current line". Commands like "dd" and "yy"
7974 * are really an alternate form of "d_" and "y_". It does accept a count, so
7975 * "d3_" works to delete 3 lines.
7976 */
7977 static void
7978nv_lineop(cap)
7979 cmdarg_T *cap;
7980{
7981 cap->oap->motion_type = MLINE;
7982 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7983 clearopbeep(cap->oap);
7984 else if ( cap->oap->op_type == OP_DELETE
7985 || cap->oap->op_type == OP_LSHIFT
7986 || cap->oap->op_type == OP_RSHIFT)
7987 beginline(BL_SOL | BL_FIX);
7988 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7989 beginline(BL_WHITE | BL_FIX);
7990}
7991
7992/*
7993 * <Home> command.
7994 */
7995 static void
7996nv_home(cap)
7997 cmdarg_T *cap;
7998{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007999 /* CTRL-HOME is like "gg" */
8000 if (mod_mask & MOD_MASK_CTRL)
8001 nv_goto(cap);
8002 else
8003 {
8004 cap->count0 = 1;
8005 nv_pipe(cap);
8006 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008007 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8008 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009}
8010
8011/*
8012 * "|" command.
8013 */
8014 static void
8015nv_pipe(cap)
8016 cmdarg_T *cap;
8017{
8018 cap->oap->motion_type = MCHAR;
8019 cap->oap->inclusive = FALSE;
8020 beginline(0);
8021 if (cap->count0 > 0)
8022 {
8023 coladvance((colnr_T)(cap->count0 - 1));
8024 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8025 }
8026 else
8027 curwin->w_curswant = 0;
8028 /* keep curswant at the column where we wanted to go, not where
8029 we ended; differs if line is too short */
8030 curwin->w_set_curswant = FALSE;
8031}
8032
8033/*
8034 * Handle back-word command "b" and "B".
8035 * cap->arg is 1 for "B"
8036 */
8037 static void
8038nv_bck_word(cap)
8039 cmdarg_T *cap;
8040{
8041 cap->oap->motion_type = MCHAR;
8042 cap->oap->inclusive = FALSE;
8043 curwin->w_set_curswant = TRUE;
8044 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8045 clearopbeep(cap->oap);
8046#ifdef FEAT_FOLDING
8047 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8048 foldOpenCursor();
8049#endif
8050}
8051
8052/*
8053 * Handle word motion commands "e", "E", "w" and "W".
8054 * cap->arg is TRUE for "E" and "W".
8055 */
8056 static void
8057nv_wordcmd(cap)
8058 cmdarg_T *cap;
8059{
8060 int n;
8061 int word_end;
8062 int flag = FALSE;
8063
8064 /*
8065 * Set inclusive for the "E" and "e" command.
8066 */
8067 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8068 word_end = TRUE;
8069 else
8070 word_end = FALSE;
8071 cap->oap->inclusive = word_end;
8072
8073 /*
8074 * "cw" and "cW" are a special case.
8075 */
8076 if (!word_end && cap->oap->op_type == OP_CHANGE)
8077 {
8078 n = gchar_cursor();
8079 if (n != NUL) /* not an empty line */
8080 {
8081 if (vim_iswhite(n))
8082 {
8083 /*
8084 * Reproduce a funny Vi behaviour: "cw" on a blank only
8085 * changes one character, not all blanks until the start of
8086 * the next word. Only do this when the 'w' flag is included
8087 * in 'cpoptions'.
8088 */
8089 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8090 {
8091 cap->oap->inclusive = TRUE;
8092 cap->oap->motion_type = MCHAR;
8093 return;
8094 }
8095 }
8096 else
8097 {
8098 /*
8099 * This is a little strange. To match what the real Vi does,
8100 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8101 * that we are not on a space or a TAB. This seems impolite
8102 * at first, but it's really more what we mean when we say
8103 * 'cw'.
8104 * Another strangeness: When standing on the end of a word
8105 * "ce" will change until the end of the next wordt, but "cw"
8106 * will change only one character! This is done by setting
8107 * flag.
8108 */
8109 cap->oap->inclusive = TRUE;
8110 word_end = TRUE;
8111 flag = TRUE;
8112 }
8113 }
8114 }
8115
8116 cap->oap->motion_type = MCHAR;
8117 curwin->w_set_curswant = TRUE;
8118 if (word_end)
8119 n = end_word(cap->count1, cap->arg, flag, FALSE);
8120 else
8121 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8122
8123 /* Don't leave the cursor on the NUL past a line */
8124 if (curwin->w_cursor.col && gchar_cursor() == NUL)
8125 {
8126 --curwin->w_cursor.col;
8127 cap->oap->inclusive = TRUE;
8128 }
8129
8130 if (n == FAIL && cap->oap->op_type == OP_NOP)
8131 clearopbeep(cap->oap);
8132 else
8133 {
8134#ifdef FEAT_VISUAL
8135 adjust_for_sel(cap);
8136#endif
8137#ifdef FEAT_FOLDING
8138 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8139 foldOpenCursor();
8140#endif
8141 }
8142}
8143
8144/*
8145 * "0" and "^" commands.
8146 * cap->arg is the argument for beginline().
8147 */
8148 static void
8149nv_beginline(cap)
8150 cmdarg_T *cap;
8151{
8152 cap->oap->motion_type = MCHAR;
8153 cap->oap->inclusive = FALSE;
8154 beginline(cap->arg);
8155#ifdef FEAT_FOLDING
8156 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8157 foldOpenCursor();
8158#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008159 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8160 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161}
8162
8163#ifdef FEAT_VISUAL
8164/*
8165 * In exclusive Visual mode, may include the last character.
8166 */
8167 static void
8168adjust_for_sel(cap)
8169 cmdarg_T *cap;
8170{
8171 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8172 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8173 {
8174# ifdef FEAT_MBYTE
8175 if (has_mbyte)
8176 inc_cursor();
8177 else
8178# endif
8179 ++curwin->w_cursor.col;
8180 cap->oap->inclusive = FALSE;
8181 }
8182}
8183
8184/*
8185 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8186 * Should check VIsual_mode before calling this.
8187 * Returns TRUE when backed up to the previous line.
8188 */
8189 static int
8190unadjust_for_sel()
8191{
8192 pos_T *pp;
8193
8194 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8195 {
8196 if (lt(VIsual, curwin->w_cursor))
8197 pp = &curwin->w_cursor;
8198 else
8199 pp = &VIsual;
8200#ifdef FEAT_VIRTUALEDIT
8201 if (pp->coladd > 0)
8202 --pp->coladd;
8203 else
8204#endif
8205 if (pp->col > 0)
8206 {
8207 --pp->col;
8208#ifdef FEAT_MBYTE
8209 mb_adjustpos(pp);
8210#endif
8211 }
8212 else if (pp->lnum > 1)
8213 {
8214 --pp->lnum;
8215 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8216 return TRUE;
8217 }
8218 }
8219 return FALSE;
8220}
8221
8222/*
8223 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8224 */
8225 static void
8226nv_select(cap)
8227 cmdarg_T *cap;
8228{
8229 if (VIsual_active)
8230 VIsual_select = TRUE;
8231 else if (VIsual_reselect)
8232 {
8233 cap->nchar = 'v'; /* fake "gv" command */
8234 cap->arg = TRUE;
8235 nv_g_cmd(cap);
8236 }
8237}
8238
8239#endif
8240
8241/*
8242 * "G", "gg", CTRL-END, CTRL-HOME.
8243 * cap->arg is TRUE for "G".
8244 */
8245 static void
8246nv_goto(cap)
8247 cmdarg_T *cap;
8248{
8249 linenr_T lnum;
8250
8251 if (cap->arg)
8252 lnum = curbuf->b_ml.ml_line_count;
8253 else
8254 lnum = 1L;
8255 cap->oap->motion_type = MLINE;
8256 setpcmark();
8257
8258 /* When a count is given, use it instead of the default lnum */
8259 if (cap->count0 != 0)
8260 lnum = cap->count0;
8261 if (lnum < 1L)
8262 lnum = 1L;
8263 else if (lnum > curbuf->b_ml.ml_line_count)
8264 lnum = curbuf->b_ml.ml_line_count;
8265 curwin->w_cursor.lnum = lnum;
8266 beginline(BL_SOL | BL_FIX);
8267#ifdef FEAT_FOLDING
8268 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8269 foldOpenCursor();
8270#endif
8271}
8272
8273/*
8274 * CTRL-\ in Normal mode.
8275 */
8276 static void
8277nv_normal(cap)
8278 cmdarg_T *cap;
8279{
8280 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8281 {
8282 clearop(cap->oap);
8283 if (restart_edit != 0 && p_smd)
8284 clear_cmdline = TRUE; /* unshow mode later */
8285 restart_edit = 0;
8286#ifdef FEAT_CMDWIN
8287 if (cmdwin_type != 0)
8288 cmdwin_result = Ctrl_C;
8289#endif
8290#ifdef FEAT_VISUAL
8291 if (VIsual_active)
8292 {
8293 end_visual_mode(); /* stop Visual */
8294 redraw_curbuf_later(INVERTED);
8295 }
8296#endif
8297 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8298 if (cap->nchar == Ctrl_G && p_im)
8299 restart_edit = 'a';
8300 }
8301 else
8302 clearopbeep(cap->oap);
8303}
8304
8305/*
8306 * ESC in Normal mode: beep, but don't flush buffers.
8307 * Don't even beep if we are canceling a command.
8308 */
8309 static void
8310nv_esc(cap)
8311 cmdarg_T *cap;
8312{
8313 int no_reason;
8314
8315 no_reason = (cap->oap->op_type == OP_NOP
8316 && cap->opcount == 0
8317 && cap->count0 == 0
8318 && cap->oap->regname == 0
8319 && !p_im);
8320
8321 if (cap->arg) /* TRUE for CTRL-C */
8322 {
8323 if (restart_edit == 0
8324#ifdef FEAT_CMDWIN
8325 && cmdwin_type == 0
8326#endif
8327#ifdef FEAT_VISUAL
8328 && !VIsual_active
8329#endif
8330 && no_reason)
8331 MSG(_("Type :quit<Enter> to exit Vim"));
8332
8333 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8334 * set again below when halfway a mapping. */
8335 if (!p_im)
8336 restart_edit = 0;
8337#ifdef FEAT_CMDWIN
8338 if (cmdwin_type != 0)
8339 {
8340 cmdwin_result = K_IGNORE;
8341 got_int = FALSE; /* don't stop executing autocommands et al. */
8342 return;
8343 }
8344#endif
8345 }
8346
8347#ifdef FEAT_VISUAL
8348 if (VIsual_active)
8349 {
8350 end_visual_mode(); /* stop Visual */
8351 check_cursor_col(); /* make sure cursor is not beyond EOL */
8352 curwin->w_set_curswant = TRUE;
8353 redraw_curbuf_later(INVERTED);
8354 }
8355 else
8356#endif
8357 if (no_reason)
8358 vim_beep();
8359 clearop(cap->oap);
8360
8361 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8362 * set return to Insert mode afterwards. */
8363 if (restart_edit == 0 && goto_im()
8364#ifdef FEAT_EX_EXTRA
8365 && ex_normal_busy == 0
8366#endif
8367 )
8368 restart_edit = 'a';
8369}
8370
8371/*
8372 * Handle "A", "a", "I", "i" and <Insert> commands.
8373 */
8374 static void
8375nv_edit(cap)
8376 cmdarg_T *cap;
8377{
8378 /* <Insert> is equal to "i" */
8379 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8380 cap->cmdchar = 'i';
8381
8382#ifdef FEAT_VISUAL
8383 /* in Visual mode "A" and "I" are an operator */
8384 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8385 v_visop(cap);
8386
8387 /* in Visual mode and after an operator "a" and "i" are for text objects */
8388 else
8389#endif
8390 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8391 && (cap->oap->op_type != OP_NOP
8392#ifdef FEAT_VISUAL
8393 || VIsual_active
8394#endif
8395 ))
8396 {
8397#ifdef FEAT_TEXTOBJ
8398 nv_object(cap);
8399#else
8400 clearopbeep(cap->oap);
8401#endif
8402 }
8403 else if (!curbuf->b_p_ma && !p_im)
8404 {
8405 /* Only give this error when 'insertmode' is off. */
8406 EMSG(_(e_modifiable));
8407 clearop(cap->oap);
8408 }
8409 else if (!checkclearopq(cap->oap))
8410 {
8411 switch (cap->cmdchar)
8412 {
8413 case 'A': /* "A"ppend after the line */
8414 curwin->w_set_curswant = TRUE;
8415#ifdef FEAT_VIRTUALEDIT
8416 if (ve_flags == VE_ALL)
8417 {
8418 int save_State = State;
8419
8420 /* Pretent Insert mode here to allow the cursor on the
8421 * character past the end of the line */
8422 State = INSERT;
8423 coladvance((colnr_T)MAXCOL);
8424 State = save_State;
8425 }
8426 else
8427#endif
8428 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8429 break;
8430
8431 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008432 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8433 beginline(BL_WHITE);
8434 else
8435 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 break;
8437
8438 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8439#ifdef FEAT_VIRTUALEDIT
8440 /* increment coladd when in virtual space, increment the
8441 * column otherwise, also to append after an unprintable char */
8442 if (virtual_active()
8443 && (curwin->w_cursor.coladd > 0
8444 || *ml_get_cursor() == NUL
8445 || *ml_get_cursor() == TAB))
8446 curwin->w_cursor.coladd++;
8447 else
8448#endif
8449 if (*ml_get_cursor() != NUL)
8450 inc_cursor();
8451 break;
8452 }
8453
8454#ifdef FEAT_VIRTUALEDIT
8455 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8456 {
8457 int save_State = State;
8458
8459 /* Pretent Insert mode here to allow the cursor on the
8460 * character past the end of the line */
8461 State = INSERT;
8462 coladvance(getviscol());
8463 State = save_State;
8464 }
8465#endif
8466
8467 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8468 }
8469}
8470
8471/*
8472 * Invoke edit() and take care of "restart_edit" and the return value.
8473 */
8474 static void
8475invoke_edit(cap, repl, cmd, startln)
8476 cmdarg_T *cap;
8477 int repl; /* "r" or "gr" command */
8478 int cmd;
8479 int startln;
8480{
8481 int restart_edit_save = 0;
8482
8483 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8484 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8485 * it. */
8486 if (repl || !stuff_empty())
8487 restart_edit_save = restart_edit;
8488 else
8489 restart_edit_save = 0;
8490
8491 /* Always reset "restart_edit", this is not a restarted edit. */
8492 restart_edit = 0;
8493
8494 if (edit(cmd, startln, cap->count1))
8495 cap->retval |= CA_COMMAND_BUSY;
8496
8497 if (restart_edit == 0)
8498 restart_edit = restart_edit_save;
8499}
8500
8501#ifdef FEAT_TEXTOBJ
8502/*
8503 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8504 */
8505 static void
8506nv_object(cap)
8507 cmdarg_T *cap;
8508{
8509 int flag;
8510 int include;
8511 char_u *mps_save;
8512
8513 if (cap->cmdchar == 'i')
8514 include = FALSE; /* "ix" = inner object: exclude white space */
8515 else
8516 include = TRUE; /* "ax" = an object: include white space */
8517
8518 /* Make sure (), [], {} and <> are in 'matchpairs' */
8519 mps_save = curbuf->b_p_mps;
8520 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8521
8522 switch (cap->nchar)
8523 {
8524 case 'w': /* "aw" = a word */
8525 flag = current_word(cap->oap, cap->count1, include, FALSE);
8526 break;
8527 case 'W': /* "aW" = a WORD */
8528 flag = current_word(cap->oap, cap->count1, include, TRUE);
8529 break;
8530 case 'b': /* "ab" = a braces block */
8531 case '(':
8532 case ')':
8533 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8534 break;
8535 case 'B': /* "aB" = a Brackets block */
8536 case '{':
8537 case '}':
8538 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8539 break;
8540 case '[': /* "a[" = a [] block */
8541 case ']':
8542 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8543 break;
8544 case '<': /* "a<" = a <> block */
8545 case '>':
8546 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8547 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008548 case 't': /* "at" = a tag block (xml and html) */
8549 flag = current_tagblock(cap->oap, cap->count1, include);
8550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 case 'p': /* "ap" = a paragraph */
8552 flag = current_par(cap->oap, cap->count1, include, 'p');
8553 break;
8554 case 's': /* "as" = a sentence */
8555 flag = current_sent(cap->oap, cap->count1, include);
8556 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008557 case '"': /* "a"" = a double quoted string */
8558 case '\'': /* "a'" = a single quoted string */
8559 case '`': /* "a`" = a backtick quoted string */
8560 flag = current_quote(cap->oap, cap->count1, include,
8561 cap->nchar);
8562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563#if 0 /* TODO */
8564 case 'S': /* "aS" = a section */
8565 case 'f': /* "af" = a filename */
8566 case 'u': /* "au" = a URL */
8567#endif
8568 default:
8569 flag = FAIL;
8570 break;
8571 }
8572
8573 curbuf->b_p_mps = mps_save;
8574 if (flag == FAIL)
8575 clearopbeep(cap->oap);
8576 adjust_cursor_col();
8577 curwin->w_set_curswant = TRUE;
8578}
8579#endif
8580
8581/*
8582 * "q" command: Start/stop recording.
8583 * "q:", "q/", "q?": edit command-line in command-line window.
8584 */
8585 static void
8586nv_record(cap)
8587 cmdarg_T *cap;
8588{
8589 if (cap->oap->op_type == OP_FORMAT)
8590 {
8591 /* "gqq" is the same as "gqgq": format line */
8592 cap->cmdchar = 'g';
8593 cap->nchar = 'q';
8594 nv_operator(cap);
8595 }
8596 else if (!checkclearop(cap->oap))
8597 {
8598#ifdef FEAT_CMDWIN
8599 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8600 {
8601 stuffcharReadbuff(cap->nchar);
8602 stuffcharReadbuff(K_CMDWIN);
8603 }
8604 else
8605#endif
8606 /* (stop) recording into a named register, unless executing a
8607 * register */
8608 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8609 clearopbeep(cap->oap);
8610 }
8611}
8612
8613/*
8614 * Handle the "@r" command.
8615 */
8616 static void
8617nv_at(cap)
8618 cmdarg_T *cap;
8619{
8620 if (checkclearop(cap->oap))
8621 return;
8622#ifdef FEAT_EVAL
8623 if (cap->nchar == '=')
8624 {
8625 if (get_expr_register() == NUL)
8626 return;
8627 }
8628#endif
8629 while (cap->count1-- && !got_int)
8630 {
8631 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8632 {
8633 clearopbeep(cap->oap);
8634 break;
8635 }
8636 line_breakcheck();
8637 }
8638}
8639
8640/*
8641 * Handle the CTRL-U and CTRL-D commands.
8642 */
8643 static void
8644nv_halfpage(cap)
8645 cmdarg_T *cap;
8646{
8647 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8648 || (cap->cmdchar == Ctrl_D
8649 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8650 clearopbeep(cap->oap);
8651 else if (!checkclearop(cap->oap))
8652 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8653}
8654
8655/*
8656 * Handle "J" or "gJ" command.
8657 */
8658 static void
8659nv_join(cap)
8660 cmdarg_T *cap;
8661{
8662#ifdef FEAT_VISUAL
8663 if (VIsual_active) /* join the visual lines */
8664 nv_operator(cap);
8665 else
8666#endif
8667 if (!checkclearop(cap->oap))
8668 {
8669 if (cap->count0 <= 1)
8670 cap->count0 = 2; /* default for join is two lines! */
8671 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8672 curbuf->b_ml.ml_line_count)
8673 clearopbeep(cap->oap); /* beyond last line */
8674 else
8675 {
8676 prep_redo(cap->oap->regname, cap->count0,
8677 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8678 do_do_join(cap->count0, cap->nchar == NUL);
8679 }
8680 }
8681}
8682
8683/*
8684 * "P", "gP", "p" and "gp" commands.
8685 */
8686 static void
8687nv_put(cap)
8688 cmdarg_T *cap;
8689{
8690#ifdef FEAT_VISUAL
8691 int regname = 0;
8692 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008693 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008694 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695#endif
8696 int dir;
8697 int flags = 0;
8698
8699 if (cap->oap->op_type != OP_NOP)
8700 {
8701#ifdef FEAT_DIFF
8702 /* "dp" is ":diffput" */
8703 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8704 {
8705 clearop(cap->oap);
8706 nv_diffgetput(TRUE);
8707 }
8708 else
8709#endif
8710 clearopbeep(cap->oap);
8711 }
8712 else
8713 {
8714 dir = (cap->cmdchar == 'P'
8715 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8716 ? BACKWARD : FORWARD;
8717 prep_redo_cmd(cap);
8718 if (cap->cmdchar == 'g')
8719 flags |= PUT_CURSEND;
8720
8721#ifdef FEAT_VISUAL
8722 if (VIsual_active)
8723 {
8724 /* Putting in Visual mode: The put text replaces the selected
8725 * text. First delete the selected text, then put the new text.
8726 * Need to save and restore the registers that the delete
8727 * overwrites if the old contents is being put.
8728 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008729 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 regname = cap->oap->regname;
8731# ifdef FEAT_CLIPBOARD
8732 adjust_clip_reg(&regname);
8733# endif
8734 if (regname == 0 || VIM_ISDIGIT(regname)
8735# ifdef FEAT_CLIPBOARD
8736 || (clip_unnamed && (regname == '*' || regname == '+'))
8737# endif
8738
8739 )
8740 {
8741 /* the delete is going to overwrite the register we want to
8742 * put, save it first. */
8743 reg1 = get_register(regname, TRUE);
8744 }
8745
8746 /* Now delete the selected text. */
8747 cap->cmdchar = 'd';
8748 cap->nchar = NUL;
8749 cap->oap->regname = NUL;
8750 nv_operator(cap);
8751 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008752 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753
8754 /* delete PUT_LINE_BACKWARD; */
8755 cap->oap->regname = regname;
8756
8757 if (reg1 != NULL)
8758 {
8759 /* Delete probably changed the register we want to put, save
8760 * it first. Then put back what was there before the delete. */
8761 reg2 = get_register(regname, FALSE);
8762 put_register(regname, reg1);
8763 }
8764
8765 /* When deleted a linewise Visual area, put the register as
8766 * lines to avoid it joined with the next line. When deletion was
8767 * characterwise, split a line when putting lines. */
8768 if (VIsual_mode == 'V')
8769 flags |= PUT_LINE;
8770 else if (VIsual_mode == 'v')
8771 flags |= PUT_LINE_SPLIT;
8772 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8773 flags |= PUT_LINE_FORWARD;
8774 dir = BACKWARD;
8775 if ((VIsual_mode != 'V'
8776 && curwin->w_cursor.col < curbuf->b_op_start.col)
8777 || (VIsual_mode == 'V'
8778 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8779 /* cursor is at the end of the line or end of file, put
8780 * forward. */
8781 dir = FORWARD;
8782 }
8783#endif
8784 do_put(cap->oap->regname, dir, cap->count1, flags);
8785
8786#ifdef FEAT_VISUAL
8787 /* If a register was saved, put it back now. */
8788 if (reg2 != NULL)
8789 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008790
8791 /* What to reselect with "gv"? Selecting the just put text seems to
8792 * be the most useful, since the original text was removed. */
8793 if (was_visual)
8794 {
8795 curbuf->b_visual_start = curbuf->b_op_start;
8796 curbuf->b_visual_end = curbuf->b_op_end;
8797 }
8798
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008799 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008800 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008801 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8802 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803#endif
8804 auto_format(FALSE, TRUE);
8805 }
8806}
8807
8808/*
8809 * "o" and "O" commands.
8810 */
8811 static void
8812nv_open(cap)
8813 cmdarg_T *cap;
8814{
8815#ifdef FEAT_DIFF
8816 /* "do" is ":diffget" */
8817 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8818 {
8819 clearop(cap->oap);
8820 nv_diffgetput(FALSE);
8821 }
8822 else
8823#endif
8824#ifdef FEAT_VISUAL
8825 if (VIsual_active) /* switch start and end of visual */
8826 v_swap_corners(cap->cmdchar);
8827 else
8828#endif
8829 n_opencmd(cap);
8830}
8831
8832#ifdef FEAT_SNIFF
8833/*ARGSUSED*/
8834 static void
8835nv_sniff(cap)
8836 cmdarg_T *cap;
8837{
8838 ProcessSniffRequests();
8839}
8840#endif
8841
8842#ifdef FEAT_NETBEANS_INTG
8843 static void
8844nv_nbcmd(cap)
8845 cmdarg_T *cap;
8846{
8847 netbeans_keycommand(cap->nchar);
8848}
8849#endif
8850
8851#ifdef FEAT_DND
8852/*ARGSUSED*/
8853 static void
8854nv_drop(cap)
8855 cmdarg_T *cap;
8856{
8857 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8858}
8859#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008860
8861#ifdef FEAT_AUTOCMD
8862/*
8863 * Trigger CursorHold event.
8864 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8865 * input buffer. "did_cursorhold" is set to avoid retriggering.
8866 */
8867/*ARGSUSED*/
8868 static void
8869nv_cursorhold(cap)
8870 cmdarg_T *cap;
8871{
8872 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8873 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00008874 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008875}
8876#endif