blob: 4d105e731d96401cf70a56fc7ae58701ae93068b [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
17#ifdef FEAT_VISUAL
18/*
19 * The Visual area is remembered for reselection.
20 */
21static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
22static linenr_T resel_VIsual_line_count; /* number of lines */
23static colnr_T resel_VIsual_col; /* nr of cols or end col */
24
25static int restart_VIsual_select = 0;
26#endif
27
28static int
29# ifdef __BORLANDC__
30_RTLENTRYF
31# endif
32 nv_compare __ARGS((const void *s1, const void *s2));
33static int find_command __ARGS((int cmdchar));
34static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000035static void op_function __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37static void find_start_of_word __ARGS((pos_T *));
38static void find_end_of_word __ARGS((pos_T *));
39static int get_mouse_class __ARGS((char_u *p));
40#endif
41static void prep_redo_cmd __ARGS((cmdarg_T *cap));
42static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
43static int checkclearop __ARGS((oparg_T *oap));
44static int checkclearopq __ARGS((oparg_T *oap));
45static void clearop __ARGS((oparg_T *oap));
46static void clearopbeep __ARGS((oparg_T *oap));
47#ifdef FEAT_VISUAL
48static void unshift_special __ARGS((cmdarg_T *cap));
49#endif
50#ifdef FEAT_CMDL_INFO
51static void del_from_showcmd __ARGS((int));
52#endif
53
54/*
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
58 */
59static void nv_ignore __ARGS((cmdarg_T *cap));
Bram Moolenaarebefac62005-12-28 22:39:57 +000060static void nv_nop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void nv_error __ARGS((cmdarg_T *cap));
62static void nv_help __ARGS((cmdarg_T *cap));
63static void nv_addsub __ARGS((cmdarg_T *cap));
64static void nv_page __ARGS((cmdarg_T *cap));
Bram Moolenaarf75a9632005-09-13 21:20:47 +000065static void nv_gd __ARGS((oparg_T *oap, int nchar, int thisblock));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int nv_screengo __ARGS((oparg_T *oap, int dir, long dist));
67#ifdef FEAT_MOUSE
68static void nv_mousescroll __ARGS((cmdarg_T *cap));
69static void nv_mouse __ARGS((cmdarg_T *cap));
70#endif
71static void nv_scroll_line __ARGS((cmdarg_T *cap));
72static void nv_zet __ARGS((cmdarg_T *cap));
73#ifdef FEAT_GUI
74static void nv_ver_scrollbar __ARGS((cmdarg_T *cap));
75static void nv_hor_scrollbar __ARGS((cmdarg_T *cap));
76#endif
77static void nv_exmode __ARGS((cmdarg_T *cap));
78static void nv_colon __ARGS((cmdarg_T *cap));
79static void nv_ctrlg __ARGS((cmdarg_T *cap));
80static void nv_ctrlh __ARGS((cmdarg_T *cap));
81static void nv_clear __ARGS((cmdarg_T *cap));
82static void nv_ctrlo __ARGS((cmdarg_T *cap));
83static void nv_hat __ARGS((cmdarg_T *cap));
84static void nv_Zet __ARGS((cmdarg_T *cap));
85static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000086static void nv_tagpop __ARGS((cmdarg_T *cap));
87static void nv_scroll __ARGS((cmdarg_T *cap));
88static void nv_right __ARGS((cmdarg_T *cap));
89static void nv_left __ARGS((cmdarg_T *cap));
90static void nv_up __ARGS((cmdarg_T *cap));
91static void nv_down __ARGS((cmdarg_T *cap));
92#ifdef FEAT_SEARCHPATH
93static void nv_gotofile __ARGS((cmdarg_T *cap));
94#endif
95static void nv_end __ARGS((cmdarg_T *cap));
96static void nv_dollar __ARGS((cmdarg_T *cap));
97static void nv_search __ARGS((cmdarg_T *cap));
98static void nv_next __ARGS((cmdarg_T *cap));
99static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
100static void nv_csearch __ARGS((cmdarg_T *cap));
101static void nv_brackets __ARGS((cmdarg_T *cap));
102static void nv_percent __ARGS((cmdarg_T *cap));
103static void nv_brace __ARGS((cmdarg_T *cap));
104static void nv_mark __ARGS((cmdarg_T *cap));
105static void nv_findpar __ARGS((cmdarg_T *cap));
106static void nv_undo __ARGS((cmdarg_T *cap));
107static void nv_kundo __ARGS((cmdarg_T *cap));
108static void nv_Replace __ARGS((cmdarg_T *cap));
109#ifdef FEAT_VREPLACE
110static void nv_vreplace __ARGS((cmdarg_T *cap));
111#endif
112#ifdef FEAT_VISUAL
113static void v_swap_corners __ARGS((int cmdchar));
114#endif
115static void nv_replace __ARGS((cmdarg_T *cap));
116static void n_swapchar __ARGS((cmdarg_T *cap));
117static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
118#ifdef FEAT_VISUAL
119static void v_visop __ARGS((cmdarg_T *cap));
120#endif
121static void nv_subst __ARGS((cmdarg_T *cap));
122static void nv_abbrev __ARGS((cmdarg_T *cap));
123static void nv_optrans __ARGS((cmdarg_T *cap));
124static void nv_gomark __ARGS((cmdarg_T *cap));
125static void nv_pcmark __ARGS((cmdarg_T *cap));
126static void nv_regname __ARGS((cmdarg_T *cap));
127#ifdef FEAT_VISUAL
128static void nv_visual __ARGS((cmdarg_T *cap));
129static void n_start_visual_mode __ARGS((int c));
130#endif
131static void nv_window __ARGS((cmdarg_T *cap));
132static void nv_suspend __ARGS((cmdarg_T *cap));
133static void nv_g_cmd __ARGS((cmdarg_T *cap));
134static void n_opencmd __ARGS((cmdarg_T *cap));
135static void nv_dot __ARGS((cmdarg_T *cap));
136static void nv_redo __ARGS((cmdarg_T *cap));
137static void nv_Undo __ARGS((cmdarg_T *cap));
138static void nv_tilde __ARGS((cmdarg_T *cap));
139static void nv_operator __ARGS((cmdarg_T *cap));
140static void nv_lineop __ARGS((cmdarg_T *cap));
141static void nv_home __ARGS((cmdarg_T *cap));
142static void nv_pipe __ARGS((cmdarg_T *cap));
143static void nv_bck_word __ARGS((cmdarg_T *cap));
144static void nv_wordcmd __ARGS((cmdarg_T *cap));
145static void nv_beginline __ARGS((cmdarg_T *cap));
146#ifdef FEAT_VISUAL
147static void adjust_for_sel __ARGS((cmdarg_T *cap));
148static int unadjust_for_sel __ARGS((void));
149static void nv_select __ARGS((cmdarg_T *cap));
150#endif
151static void nv_goto __ARGS((cmdarg_T *cap));
152static void nv_normal __ARGS((cmdarg_T *cap));
153static void nv_esc __ARGS((cmdarg_T *oap));
154static void nv_edit __ARGS((cmdarg_T *cap));
155static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
156#ifdef FEAT_TEXTOBJ
157static void nv_object __ARGS((cmdarg_T *cap));
158#endif
159static void nv_record __ARGS((cmdarg_T *cap));
160static void nv_at __ARGS((cmdarg_T *cap));
161static void nv_halfpage __ARGS((cmdarg_T *cap));
162static void nv_join __ARGS((cmdarg_T *cap));
163static void nv_put __ARGS((cmdarg_T *cap));
164static void nv_open __ARGS((cmdarg_T *cap));
165#ifdef FEAT_SNIFF
166static void nv_sniff __ARGS((cmdarg_T *cap));
167#endif
168#ifdef FEAT_NETBEANS_INTG
169static void nv_nbcmd __ARGS((cmdarg_T *cap));
170#endif
171#ifdef FEAT_DND
172static void nv_drop __ARGS((cmdarg_T *cap));
173#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#ifdef FEAT_AUTOCMD
175static void nv_cursorhold __ARGS((cmdarg_T *cap));
176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178/*
179 * Function to be called for a Normal or Visual mode command.
180 * The argument is a cmdarg_T.
181 */
182typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
183
184/* Values for cmd_flags. */
185#define NV_NCH 0x01 /* may need to get a second char */
186#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
187#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
188#define NV_LANG 0x08 /* second char needs language adjustment */
189
190#define NV_SS 0x10 /* may start selection */
191#define NV_SSS 0x20 /* may start selection with shift modifier */
192#define NV_STS 0x40 /* may stop selection without shift modif. */
193#define NV_RL 0x80 /* 'rightleft' modifies command */
194#define NV_KEEPREG 0x100 /* don't clear regname */
195#define NV_NCW 0x200 /* not allowed in command-line window */
196
197/*
198 * Generally speaking, every Normal mode command should either clear any
199 * pending operator (with *clearop*()), or set the motion type variable
200 * oap->motion_type.
201 *
202 * When a cursor motion command is made, it is marked as being a character or
203 * line oriented motion. Then, if an operator is in effect, the operation
204 * becomes character or line oriented accordingly.
205 */
206
207/*
208 * This table contains one entry for every Normal or Visual mode command.
209 * The order doesn't matter, init_normal_cmds() will create a sorted index.
210 * It is faster when all keys from zero to '~' are present.
211 */
212static const struct nv_cmd
213{
214 int cmd_char; /* (first) command character */
215 nv_func_T cmd_func; /* function for this command */
216 short_u cmd_flags; /* NV_ flags */
217 short cmd_arg; /* value for ca.arg */
218} nv_cmds[] =
219{
220 {NUL, nv_error, 0, 0},
221 {Ctrl_A, nv_addsub, 0, 0},
222 {Ctrl_B, nv_page, NV_STS, BACKWARD},
223 {Ctrl_C, nv_esc, 0, TRUE},
224 {Ctrl_D, nv_halfpage, 0, 0},
225 {Ctrl_E, nv_scroll_line, 0, TRUE},
226 {Ctrl_F, nv_page, NV_STS, FORWARD},
227 {Ctrl_G, nv_ctrlg, 0, 0},
228 {Ctrl_H, nv_ctrlh, 0, 0},
229 {Ctrl_I, nv_pcmark, 0, 0},
230 {NL, nv_down, 0, FALSE},
231 {Ctrl_K, nv_error, 0, 0},
232 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000233 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 {Ctrl_N, nv_down, NV_STS, FALSE},
235 {Ctrl_O, nv_ctrlo, 0, 0},
236 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000237#ifdef FEAT_VISUAL
238 {Ctrl_Q, nv_visual, 0, FALSE},
239#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {Ctrl_Q, nv_ignore, 0, 0},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 {Ctrl_R, nv_redo, 0, 0},
243 {Ctrl_S, nv_ignore, 0, 0},
244 {Ctrl_T, nv_tagpop, NV_NCW, 0},
245 {Ctrl_U, nv_halfpage, 0, 0},
246#ifdef FEAT_VISUAL
247 {Ctrl_V, nv_visual, 0, FALSE},
248 {'V', nv_visual, 0, FALSE},
249 {'v', nv_visual, 0, FALSE},
250#else
251 {Ctrl_V, nv_error, 0, 0},
252 {'V', nv_error, 0, 0},
253 {'v', nv_error, 0, 0},
254#endif
255 {Ctrl_W, nv_window, 0, 0},
256 {Ctrl_X, nv_addsub, 0, 0},
257 {Ctrl_Y, nv_scroll_line, 0, FALSE},
258 {Ctrl_Z, nv_suspend, 0, 0},
259 {ESC, nv_esc, 0, FALSE},
260 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
261 {Ctrl_RSB, nv_ident, NV_NCW, 0},
262 {Ctrl_HAT, nv_hat, NV_NCW, 0},
263 {Ctrl__, nv_error, 0, 0},
264 {' ', nv_right, 0, 0},
265 {'!', nv_operator, 0, 0},
266 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
267 {'#', nv_ident, 0, 0},
268 {'$', nv_dollar, 0, 0},
269 {'%', nv_percent, 0, 0},
270 {'&', nv_optrans, 0, 0},
271 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
272 {'(', nv_brace, 0, BACKWARD},
273 {')', nv_brace, 0, FORWARD},
274 {'*', nv_ident, 0, 0},
275 {'+', nv_down, 0, TRUE},
276 {',', nv_csearch, 0, TRUE},
277 {'-', nv_up, 0, TRUE},
278 {'.', nv_dot, NV_KEEPREG, 0},
279 {'/', nv_search, 0, FALSE},
280 {'0', nv_beginline, 0, 0},
281 {'1', nv_ignore, 0, 0},
282 {'2', nv_ignore, 0, 0},
283 {'3', nv_ignore, 0, 0},
284 {'4', nv_ignore, 0, 0},
285 {'5', nv_ignore, 0, 0},
286 {'6', nv_ignore, 0, 0},
287 {'7', nv_ignore, 0, 0},
288 {'8', nv_ignore, 0, 0},
289 {'9', nv_ignore, 0, 0},
290 {':', nv_colon, 0, 0},
291 {';', nv_csearch, 0, FALSE},
292 {'<', nv_operator, NV_RL, 0},
293 {'=', nv_operator, 0, 0},
294 {'>', nv_operator, NV_RL, 0},
295 {'?', nv_search, 0, FALSE},
296 {'@', nv_at, NV_NCH_NOP, FALSE},
297 {'A', nv_edit, 0, 0},
298 {'B', nv_bck_word, 0, 1},
299 {'C', nv_abbrev, NV_KEEPREG, 0},
300 {'D', nv_abbrev, NV_KEEPREG, 0},
301 {'E', nv_wordcmd, 0, TRUE},
302 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
303 {'G', nv_goto, 0, TRUE},
304 {'H', nv_scroll, 0, 0},
305 {'I', nv_edit, 0, 0},
306 {'J', nv_join, 0, 0},
307 {'K', nv_ident, 0, 0},
308 {'L', nv_scroll, 0, 0},
309 {'M', nv_scroll, 0, 0},
310 {'N', nv_next, 0, SEARCH_REV},
311 {'O', nv_open, 0, 0},
312 {'P', nv_put, 0, 0},
313 {'Q', nv_exmode, NV_NCW, 0},
314 {'R', nv_Replace, 0, FALSE},
315 {'S', nv_subst, NV_KEEPREG, 0},
316 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
317 {'U', nv_Undo, 0, 0},
318 {'W', nv_wordcmd, 0, TRUE},
319 {'X', nv_abbrev, NV_KEEPREG, 0},
320 {'Y', nv_abbrev, NV_KEEPREG, 0},
321 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
322 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
323 {'\\', nv_error, 0, 0},
324 {']', nv_brackets, NV_NCH_ALW, FORWARD},
325 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
326 {'_', nv_lineop, 0, 0},
327 {'`', nv_gomark, NV_NCH_ALW, FALSE},
328 {'a', nv_edit, NV_NCH, 0},
329 {'b', nv_bck_word, 0, 0},
330 {'c', nv_operator, 0, 0},
331 {'d', nv_operator, 0, 0},
332 {'e', nv_wordcmd, 0, FALSE},
333 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
334 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
335 {'h', nv_left, NV_RL, 0},
336 {'i', nv_edit, NV_NCH, 0},
337 {'j', nv_down, 0, FALSE},
338 {'k', nv_up, 0, FALSE},
339 {'l', nv_right, NV_RL, 0},
340 {'m', nv_mark, NV_NCH_NOP, 0},
341 {'n', nv_next, 0, 0},
342 {'o', nv_open, 0, 0},
343 {'p', nv_put, 0, 0},
344 {'q', nv_record, NV_NCH, 0},
345 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
346 {'s', nv_subst, NV_KEEPREG, 0},
347 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
348 {'u', nv_undo, 0, 0},
349 {'w', nv_wordcmd, 0, FALSE},
350 {'x', nv_abbrev, NV_KEEPREG, 0},
351 {'y', nv_operator, 0, 0},
352 {'z', nv_zet, NV_NCH_ALW, 0},
353 {'{', nv_findpar, 0, BACKWARD},
354 {'|', nv_pipe, 0, 0},
355 {'}', nv_findpar, 0, FORWARD},
356 {'~', nv_tilde, 0, 0},
357
358 /* pound sign */
359 {POUND, nv_ident, 0, 0},
360#ifdef FEAT_MOUSE
361 {K_MOUSEUP, nv_mousescroll, 0, TRUE},
362 {K_MOUSEDOWN, nv_mousescroll, 0, FALSE},
363 {K_LEFTMOUSE, nv_mouse, 0, 0},
364 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
365 {K_LEFTDRAG, nv_mouse, 0, 0},
366 {K_LEFTRELEASE, nv_mouse, 0, 0},
367 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
368 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
369 {K_MIDDLEDRAG, nv_mouse, 0, 0},
370 {K_MIDDLERELEASE, nv_mouse, 0, 0},
371 {K_RIGHTMOUSE, nv_mouse, 0, 0},
372 {K_RIGHTDRAG, nv_mouse, 0, 0},
373 {K_RIGHTRELEASE, nv_mouse, 0, 0},
374 {K_X1MOUSE, nv_mouse, 0, 0},
375 {K_X1DRAG, nv_mouse, 0, 0},
376 {K_X1RELEASE, nv_mouse, 0, 0},
377 {K_X2MOUSE, nv_mouse, 0, 0},
378 {K_X2DRAG, nv_mouse, 0, 0},
379 {K_X2RELEASE, nv_mouse, 0, 0},
380#endif
381 {K_IGNORE, nv_ignore, 0, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000382 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_INS, nv_edit, 0, 0},
384 {K_KINS, nv_edit, 0, 0},
385 {K_BS, nv_ctrlh, 0, 0},
386 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
387 {K_S_UP, nv_page, NV_SS, BACKWARD},
388 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
389 {K_S_DOWN, nv_page, NV_SS, FORWARD},
390 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
391 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
392 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
393 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
394 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
395 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
396 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
397 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
398 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
399 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
400 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
401 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 {K_S_END, nv_end, NV_SS, FALSE},
403 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
404 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
405 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {K_S_HOME, nv_home, NV_SS, 0},
407 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
408 {K_DEL, nv_abbrev, 0, 0},
409 {K_KDEL, nv_abbrev, 0, 0},
410 {K_UNDO, nv_kundo, 0, 0},
411 {K_HELP, nv_help, NV_NCW, 0},
412 {K_F1, nv_help, NV_NCW, 0},
413 {K_XF1, nv_help, NV_NCW, 0},
414#ifdef FEAT_VISUAL
415 {K_SELECT, nv_select, 0, 0},
416#endif
417#ifdef FEAT_GUI
418 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
419 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
420#endif
421#ifdef FEAT_FKMAP
422 {K_F8, farsi_fkey, 0, 0},
423 {K_F9, farsi_fkey, 0, 0},
424#endif
425#ifdef FEAT_SNIFF
426 {K_SNIFF, nv_sniff, 0, 0},
427#endif
428#ifdef FEAT_NETBEANS_INTG
429 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
430#endif
431#ifdef FEAT_DND
432 {K_DROP, nv_drop, NV_STS, 0},
433#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000434#ifdef FEAT_AUTOCMD
435 {K_CURSORHOLD, nv_cursorhold, 0, 0},
436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437};
438
439/* Number of commands in nv_cmds[]. */
440#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
441
442/* Sorted index of commands in nv_cmds[]. */
443static short nv_cmd_idx[NV_CMDS_SIZE];
444
445/* The highest index for which
446 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
447static int nv_max_linear;
448
449/*
450 * Compare functions for qsort() below, that checks the command character
451 * through the index in nv_cmd_idx[].
452 */
453 static int
454#ifdef __BORLANDC__
455_RTLENTRYF
456#endif
457nv_compare(s1, s2)
458 const void *s1;
459 const void *s2;
460{
461 int c1, c2;
462
463 /* The commands are sorted on absolute value. */
464 c1 = nv_cmds[*(const short *)s1].cmd_char;
465 c2 = nv_cmds[*(const short *)s2].cmd_char;
466 if (c1 < 0)
467 c1 = -c1;
468 if (c2 < 0)
469 c2 = -c2;
470 return c1 - c2;
471}
472
473/*
474 * Initialize the nv_cmd_idx[] table.
475 */
476 void
477init_normal_cmds()
478{
479 int i;
480
481 /* Fill the index table with a one to one relation. */
482 for (i = 0; i < NV_CMDS_SIZE; ++i)
483 nv_cmd_idx[i] = i;
484
485 /* Sort the commands by the command character. */
486 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
487
488 /* Find the first entry that can't be indexed by the command character. */
489 for (i = 0; i < NV_CMDS_SIZE; ++i)
490 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
491 break;
492 nv_max_linear = i - 1;
493}
494
495/*
496 * Search for a command in the commands table.
497 * Returns -1 for invalid command.
498 */
499 static int
500find_command(cmdchar)
501 int cmdchar;
502{
503 int i;
504 int idx;
505 int top, bot;
506 int c;
507
508#ifdef FEAT_MBYTE
509 /* A multi-byte character is never a command. */
510 if (cmdchar >= 0x100)
511 return -1;
512#endif
513
514 /* We use the absolute value of the character. Special keys have a
515 * negative value, but are sorted on their absolute value. */
516 if (cmdchar < 0)
517 cmdchar = -cmdchar;
518
519 /* If the character is in the first part: The character is the index into
520 * nv_cmd_idx[]. */
521 if (cmdchar <= nv_max_linear)
522 return nv_cmd_idx[cmdchar];
523
524 /* Perform a binary search. */
525 bot = nv_max_linear + 1;
526 top = NV_CMDS_SIZE - 1;
527 idx = -1;
528 while (bot <= top)
529 {
530 i = (top + bot) / 2;
531 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
532 if (c < 0)
533 c = -c;
534 if (cmdchar == c)
535 {
536 idx = nv_cmd_idx[i];
537 break;
538 }
539 if (cmdchar > c)
540 bot = i + 1;
541 else
542 top = i - 1;
543 }
544 return idx;
545}
546
547/*
548 * Execute a command in Normal mode.
549 */
550/*ARGSUSED*/
551 void
552normal_cmd(oap, toplevel)
553 oparg_T *oap;
554 int toplevel; /* TRUE when called from main() */
555{
556 static long opcount = 0; /* ca.opcount saved here */
557 cmdarg_T ca; /* command arguments */
558 int c;
559 int ctrl_w = FALSE; /* got CTRL-W command */
560 int old_col = curwin->w_curswant;
561#ifdef FEAT_CMDL_INFO
562 int need_flushbuf; /* need to call out_flush() */
563#endif
564#ifdef FEAT_VISUAL
565 pos_T old_pos; /* cursor position before command */
566 int mapped_len;
567#endif
568 static int old_mapped_len = 0;
569 int idx;
570
571 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
572 ca.oap = oap;
573 ca.opcount = opcount;
574
575#ifdef FEAT_SNIFF
576 want_sniff_request = sniff_connected;
577#endif
578
579 /*
580 * If there is an operator pending, then the command we take this time
581 * will terminate it. Finish_op tells us to finish the operation before
582 * returning this time (unless the operation was cancelled).
583 */
584#ifdef CURSOR_SHAPE
585 c = finish_op;
586#endif
587 finish_op = (oap->op_type != OP_NOP);
588#ifdef CURSOR_SHAPE
589 if (finish_op != c)
590 {
591 ui_cursor_shape(); /* may show different cursor shape */
592# ifdef FEAT_MOUSESHAPE
593 update_mouseshape(-1);
594# endif
595 }
596#endif
597
598 if (!finish_op && !oap->regname)
599 ca.opcount = 0;
600
601#ifdef FEAT_VISUAL
602 mapped_len = typebuf_maplen();
603#endif
604
605 State = NORMAL_BUSY;
606#ifdef USE_ON_FLY_SCROLL
607 dont_scroll = FALSE; /* allow scrolling here */
608#endif
609
610 /*
611 * Get the command character from the user.
612 */
613 c = safe_vgetc();
614
615#ifdef FEAT_LANGMAP
616 LANGMAP_ADJUST(c, TRUE);
617#endif
618
619 /*
620 * If a mapping was started in Visual or Select mode, remember the length
621 * of the mapping. This is used below to not return to Insert mode for as
622 * long as the mapping is being executed.
623 */
624 if (restart_edit == 0)
625 old_mapped_len = 0;
626 else if (old_mapped_len
627#ifdef FEAT_VISUAL
628 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0)
629#endif
630 )
631 old_mapped_len = typebuf_maplen();
632
633 if (c == NUL)
634 c = K_ZERO;
635
636#ifdef FEAT_VISUAL
637 /*
638 * In Select mode, typed text replaces the selection.
639 */
640 if (VIsual_active
641 && VIsual_select
642 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
643 {
644# ifdef FEAT_MBYTE
645 char_u buf[MB_MAXBYTES + 1];
646
647 buf[(*mb_char2bytes)(c, buf)] = NUL;
648# else
649 char_u buf[2];
650
651 buf[0] = c;
652 buf[1] = NUL;
653# endif
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000654 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
655 * 'insertmode' is set) fake a "d"elete command, Insert mode will
656 * restart automatically.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 * Insert the typed character in the typeahead buffer, so that it will
658 * be mapped in Insert mode. Required for ":lmap" to work. May cause
659 * mapping a character from ":vnoremap"... */
660 (void)ins_typebuf(buf, REMAP_YES, 0, !KeyTyped, FALSE);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000661 if (restart_edit != 0)
662 c = 'd';
663 else
664 c = 'c';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666#endif
667
668#ifdef FEAT_CMDL_INFO
669 need_flushbuf = add_to_showcmd(c);
670#endif
671
672getcount:
673#ifdef FEAT_VISUAL
674 if (!(VIsual_active && VIsual_select))
675#endif
676 {
677 /*
678 * Handle a count before a command and compute ca.count0.
679 * Note that '0' is a command and not the start of a count, but it's
680 * part of a count after other digits.
681 */
682 while ( (c >= '1' && c <= '9')
683 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
684 {
685 if (c == K_DEL || c == K_KDEL)
686 {
687 ca.count0 /= 10;
688#ifdef FEAT_CMDL_INFO
689 del_from_showcmd(4); /* delete the digit and ~@% */
690#endif
691 }
692 else
693 ca.count0 = ca.count0 * 10 + (c - '0');
694 if (ca.count0 < 0) /* got too large! */
695 ca.count0 = 999999999L;
696 if (ctrl_w)
697 {
698 ++no_mapping;
699 ++allow_keys; /* no mapping for nchar, but keys */
700 }
701 ++no_zero_mapping; /* don't map zero here */
702 c = safe_vgetc();
703#ifdef FEAT_LANGMAP
704 LANGMAP_ADJUST(c, TRUE);
705#endif
706 --no_zero_mapping;
707 if (ctrl_w)
708 {
709 --no_mapping;
710 --allow_keys;
711 }
712#ifdef FEAT_CMDL_INFO
713 need_flushbuf |= add_to_showcmd(c);
714#endif
715 }
716
717 /*
718 * If we got CTRL-W there may be a/another count
719 */
720 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
721 {
722 ctrl_w = TRUE;
723 ca.opcount = ca.count0; /* remember first count */
724 ca.count0 = 0;
725 ++no_mapping;
726 ++allow_keys; /* no mapping for nchar, but keys */
727 c = safe_vgetc(); /* get next character */
728#ifdef FEAT_LANGMAP
729 LANGMAP_ADJUST(c, TRUE);
730#endif
731 --no_mapping;
732 --allow_keys;
733#ifdef FEAT_CMDL_INFO
734 need_flushbuf |= add_to_showcmd(c);
735#endif
736 goto getcount; /* jump back */
737 }
738 }
739
740 /*
741 * If we're in the middle of an operator (including after entering a yank
742 * buffer with '"') AND we had a count before the operator, then that
743 * count overrides the current value of ca.count0.
744 * What this means effectively, is that commands like "3dw" get turned
745 * into "d3w" which makes things fall into place pretty neatly.
746 * If you give a count before AND after the operator, they are multiplied.
747 */
748 if (ca.opcount != 0)
749 {
750 if (ca.count0)
751 ca.count0 *= ca.opcount;
752 else
753 ca.count0 = ca.opcount;
754 }
755
756 /*
757 * Always remember the count. It will be set to zero (on the next call,
758 * above) when there is no pending operator.
759 * When called from main(), save the count for use by the "count" built-in
760 * variable.
761 */
762 ca.opcount = ca.count0;
763 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
764
765#ifdef FEAT_EVAL
766 /*
767 * Only set v:count when called from main() and not a stuffed command.
768 */
769 if (toplevel && stuff_empty())
770 set_vcount(ca.count0, ca.count1);
771#endif
772
773 /*
774 * Find the command character in the table of commands.
775 * For CTRL-W we already got nchar when looking for a count.
776 */
777 if (ctrl_w)
778 {
779 ca.nchar = c;
780 ca.cmdchar = Ctrl_W;
781 }
782 else
783 ca.cmdchar = c;
784 idx = find_command(ca.cmdchar);
785 if (idx < 0)
786 {
787 /* Not a known command: beep. */
788 clearopbeep(oap);
789 goto normal_end;
790 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000791
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000792 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000794 /* This command is not allowed wile editing a ccmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000796 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 goto normal_end;
798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800#ifdef FEAT_VISUAL
801 /*
802 * In Visual/Select mode, a few keys are handled in a special way.
803 */
804 if (VIsual_active)
805 {
806 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
807 if (km_stopsel
808 && (nv_cmds[idx].cmd_flags & NV_STS)
809 && !(mod_mask & MOD_MASK_SHIFT))
810 {
811 end_visual_mode();
812 redraw_curbuf_later(INVERTED);
813 }
814
815 /* Keys that work different when 'keymodel' contains "startsel" */
816 if (km_startsel)
817 {
818 if (nv_cmds[idx].cmd_flags & NV_SS)
819 {
820 unshift_special(&ca);
821 idx = find_command(ca.cmdchar);
822 }
823 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
824 && (mod_mask & MOD_MASK_SHIFT))
825 {
826 mod_mask &= ~MOD_MASK_SHIFT;
827 }
828 }
829 }
830#endif
831
832#ifdef FEAT_RIGHTLEFT
833 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
834 && (nv_cmds[idx].cmd_flags & NV_RL))
835 {
836 /* Invert horizontal movements and operations. Only when typed by the
837 * user directly, not when the result of a mapping or "x" translated
838 * to "dl". */
839 switch (ca.cmdchar)
840 {
841 case 'l': ca.cmdchar = 'h'; break;
842 case K_RIGHT: ca.cmdchar = K_LEFT; break;
843 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
844 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
845 case 'h': ca.cmdchar = 'l'; break;
846 case K_LEFT: ca.cmdchar = K_RIGHT; break;
847 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
848 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
849 case '>': ca.cmdchar = '<'; break;
850 case '<': ca.cmdchar = '>'; break;
851 }
852 idx = find_command(ca.cmdchar);
853 }
854#endif
855
856 /*
857 * Get an additional character if we need one.
858 */
859 if ((nv_cmds[idx].cmd_flags & NV_NCH)
860 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
861 && oap->op_type == OP_NOP)
862 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
863 || (ca.cmdchar == 'q'
864 && oap->op_type == OP_NOP
865 && !Recording
866 && !Exec_reg)
867 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
868 && (oap->op_type != OP_NOP
869#ifdef FEAT_VISUAL
870 || VIsual_active
871#endif
872 ))))
873 {
874 int *cp;
875 int repl = FALSE; /* get character for replace mode */
876 int lit = FALSE; /* get extra character literally */
877 int langmap_active = FALSE; /* using :lmap mappings */
878 int lang; /* getting a text character */
879#ifdef USE_IM_CONTROL
880 int save_smd; /* saved value of p_smd */
881#endif
882
883 ++no_mapping;
884 ++allow_keys; /* no mapping for nchar, but allow key codes */
885 if (ca.cmdchar == 'g')
886 {
887 /*
888 * For 'g' get the next character now, so that we can check for
889 * "gr", "g'" and "g`".
890 */
891 ca.nchar = safe_vgetc();
892#ifdef FEAT_LANGMAP
893 LANGMAP_ADJUST(ca.nchar, TRUE);
894#endif
895#ifdef FEAT_CMDL_INFO
896 need_flushbuf |= add_to_showcmd(ca.nchar);
897#endif
898 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
899 || ca.nchar == Ctrl_BSL)
900 {
901 cp = &ca.extra_char; /* need to get a third character */
902 if (ca.nchar != 'r')
903 lit = TRUE; /* get it literally */
904 else
905 repl = TRUE; /* get it in replace mode */
906 }
907 else
908 cp = NULL; /* no third character needed */
909 }
910 else
911 {
912 if (ca.cmdchar == 'r') /* get it in replace mode */
913 repl = TRUE;
914 cp = &ca.nchar;
915 }
916 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
917
918 /*
919 * Get a second or third character.
920 */
921 if (cp != NULL)
922 {
923#ifdef CURSOR_SHAPE
924 if (repl)
925 {
926 State = REPLACE; /* pretend Replace mode */
927 ui_cursor_shape(); /* show different cursor shape */
928 }
929#endif
930 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
931 {
932 /* Allow mappings defined with ":lmap". */
933 --no_mapping;
934 --allow_keys;
935 if (repl)
936 State = LREPLACE;
937 else
938 State = LANGMAP;
939 langmap_active = TRUE;
940 }
941#ifdef USE_IM_CONTROL
942 save_smd = p_smd;
943 p_smd = FALSE; /* Don't let the IM code show the mode here */
944 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
945 im_set_active(TRUE);
946#endif
947
948 *cp = safe_vgetc();
949
950 if (langmap_active)
951 {
952 /* Undo the decrement done above */
953 ++no_mapping;
954 ++allow_keys;
955 State = NORMAL_BUSY;
956 }
957#ifdef USE_IM_CONTROL
958 if (lang)
959 {
960 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
961 im_save_status(&curbuf->b_p_iminsert);
962 im_set_active(FALSE);
963 }
964 p_smd = save_smd;
965#endif
966#ifdef CURSOR_SHAPE
967 State = NORMAL_BUSY;
968#endif
969#ifdef FEAT_CMDL_INFO
970 need_flushbuf |= add_to_showcmd(*cp);
971#endif
972
973 if (!lit)
974 {
975#ifdef FEAT_DIGRAPHS
976 /* Typing CTRL-K gets a digraph. */
977 if (*cp == Ctrl_K
978 && ((nv_cmds[idx].cmd_flags & NV_LANG)
979 || cp == &ca.extra_char)
980 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
981 {
982 c = get_digraph(FALSE);
983 if (c > 0)
984 {
985 *cp = c;
986# ifdef FEAT_CMDL_INFO
987 /* Guessing how to update showcmd here... */
988 del_from_showcmd(3);
989 need_flushbuf |= add_to_showcmd(*cp);
990# endif
991 }
992 }
993#endif
994
995#ifdef FEAT_LANGMAP
996 /* adjust chars > 127, except after "tTfFr" commands */
997 LANGMAP_ADJUST(*cp, !lang);
998#endif
999#ifdef FEAT_RIGHTLEFT
1000 /* adjust Hebrew mapped char */
1001 if (p_hkmap && lang && KeyTyped)
1002 *cp = hkmap(*cp);
1003# ifdef FEAT_FKMAP
1004 /* adjust Farsi mapped char */
1005 if (p_fkmap && lang && KeyTyped)
1006 *cp = fkmap(*cp);
1007# endif
1008#endif
1009 }
1010
1011 /*
1012 * When the next character is CTRL-\ a following CTRL-N means the
1013 * command is aborted and we go to Normal mode.
1014 */
1015 if (cp == &ca.extra_char
1016 && ca.nchar == Ctrl_BSL
1017 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1018 {
1019 ca.cmdchar = Ctrl_BSL;
1020 ca.nchar = ca.extra_char;
1021 idx = find_command(ca.cmdchar);
1022 }
1023 else if (*cp == Ctrl_BSL)
1024 {
1025 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1026
1027 /* There is a busy wait here when typing "f<C-\>" and then
1028 * something different from CTRL-N. Can't be avoided. */
1029 while ((c = vpeekc()) <= 0 && towait > 0L)
1030 {
1031 do_sleep(towait > 50L ? 50L : towait);
1032 towait -= 50L;
1033 }
1034 if (c > 0)
1035 {
1036 c = safe_vgetc();
1037 if (c != Ctrl_N && c != Ctrl_G)
1038 vungetc(c);
1039 else
1040 {
1041 ca.cmdchar = Ctrl_BSL;
1042 ca.nchar = c;
1043 idx = find_command(ca.cmdchar);
1044 }
1045 }
1046 }
1047
1048#ifdef FEAT_MBYTE
1049 /* When getting a text character and the next character is a
1050 * multi-byte character, it could be a composing character.
1051 * However, don't wait for it to arrive. */
1052 while (enc_utf8 && lang && (c = vpeekc()) > 0
1053 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1054 {
1055 c = safe_vgetc();
1056 if (!utf_iscomposing(c))
1057 {
1058 vungetc(c); /* it wasn't, put it back */
1059 break;
1060 }
1061 else if (ca.ncharC1 == 0)
1062 ca.ncharC1 = c;
1063 else
1064 ca.ncharC2 = c;
1065 }
1066#endif
1067 }
1068 --no_mapping;
1069 --allow_keys;
1070 }
1071
1072#ifdef FEAT_CMDL_INFO
1073 /*
1074 * Flush the showcmd characters onto the screen so we can see them while
1075 * the command is being executed. Only do this when the shown command was
1076 * actually displayed, otherwise this will slow down a lot when executing
1077 * mappings.
1078 */
1079 if (need_flushbuf)
1080 out_flush();
1081#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001082#ifdef FEAT_AUTOCMD
1083 did_cursorhold = FALSE;
1084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 State = NORMAL;
1087
1088 if (ca.nchar == ESC)
1089 {
1090 clearop(oap);
1091 if (restart_edit == 0 && goto_im())
1092 restart_edit = 'a';
1093 goto normal_end;
1094 }
1095
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001096 if (ca.cmdchar != K_IGNORE)
1097 {
1098 msg_didout = FALSE; /* don't scroll screen up for normal command */
1099 msg_col = 0;
1100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102#ifdef FEAT_VISUAL
1103 old_pos = curwin->w_cursor; /* remember where cursor was */
1104
1105 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1106 * mode. */
1107 if (!VIsual_active && km_startsel)
1108 {
1109 if (nv_cmds[idx].cmd_flags & NV_SS)
1110 {
1111 start_selection();
1112 unshift_special(&ca);
1113 idx = find_command(ca.cmdchar);
1114 }
1115 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1116 && (mod_mask & MOD_MASK_SHIFT))
1117 {
1118 start_selection();
1119 mod_mask &= ~MOD_MASK_SHIFT;
1120 }
1121 }
1122#endif
1123
1124 /*
1125 * Execute the command!
1126 * Call the command function found in the commands table.
1127 */
1128 ca.arg = nv_cmds[idx].cmd_arg;
1129 (nv_cmds[idx].cmd_func)(&ca);
1130
1131 /*
1132 * If we didn't start or finish an operator, reset oap->regname, unless we
1133 * need it later.
1134 */
1135 if (!finish_op
1136 && !oap->op_type
1137 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1138 {
1139 clearop(oap);
1140#ifdef FEAT_EVAL
1141 set_reg_var('"');
1142#endif
1143 }
1144
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001145 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001146 * character or "z333<cr>". */
1147 if (old_mapped_len > 0)
1148 old_mapped_len = typebuf_maplen();
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 /*
1151 * If an operation is pending, handle it...
1152 */
1153 do_pending_operator(&ca, old_col, FALSE);
1154
1155 /*
1156 * Wait for a moment when a message is displayed that will be overwritten
1157 * by the mode message.
1158 * In Visual mode and with "^O" in Insert mode, a short message will be
1159 * overwritten by the mode message. Wait a bit, until a key is hit.
1160 * In Visual mode, it's more important to keep the Visual area updated
1161 * than keeping a message (e.g. from a /pat search).
1162 * Only do this if the command was typed, not from a mapping.
1163 * Don't wait when emsg_silent is non-zero.
1164 * Also wait a bit after an error message, e.g. for "^O:".
1165 * Don't redraw the screen, it would remove the message.
1166 */
1167 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001168 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && (restart_edit != 0
1170#ifdef FEAT_VISUAL
1171 || (VIsual_active
1172 && old_pos.lnum == curwin->w_cursor.lnum
1173 && old_pos.col == curwin->w_cursor.col)
1174#endif
1175 )
1176 && (clear_cmdline
1177 || redraw_cmdline)
1178 && (msg_didout || (msg_didany && msg_scroll))
1179 && !msg_nowait
1180 && KeyTyped)
1181 || (restart_edit != 0
1182#ifdef FEAT_VISUAL
1183 && !VIsual_active
1184#endif
1185 && (msg_scroll
1186 || emsg_on_display)))
1187 && oap->regname == 0
1188 && !(ca.retval & CA_COMMAND_BUSY)
1189 && stuff_empty()
1190 && typebuf_typed()
1191 && emsg_silent == 0
1192 && !did_wait_return
1193 && oap->op_type == OP_NOP)
1194 {
1195 int save_State = State;
1196
1197 /* Draw the cursor with the right shape here */
1198 if (restart_edit != 0)
1199 State = INSERT;
1200
1201 /* If need to redraw, and there is a "keep_msg", redraw before the
1202 * delay */
1203 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1204 {
1205 char_u *kmsg;
1206
1207 kmsg = keep_msg;
1208 keep_msg = NULL;
1209 /* showmode() will clear keep_msg, but we want to use it anyway */
1210 update_screen(0);
1211 /* now reset it, otherwise it's put in the history again */
1212 keep_msg = kmsg;
1213 msg_attr(kmsg, keep_msg_attr);
1214 vim_free(kmsg);
1215 }
1216 setcursor();
1217 cursor_on();
1218 out_flush();
1219 if (msg_scroll || emsg_on_display)
1220 ui_delay(1000L, TRUE); /* wait at least one second */
1221 ui_delay(3000L, FALSE); /* wait up to three seconds */
1222 State = save_State;
1223
1224 msg_scroll = FALSE;
1225 emsg_on_display = FALSE;
1226 }
1227
1228 /*
1229 * Finish up after executing a Normal mode command.
1230 */
1231normal_end:
1232
1233 msg_nowait = FALSE;
1234
1235 /* Reset finish_op, in case it was set */
1236#ifdef CURSOR_SHAPE
1237 c = finish_op;
1238#endif
1239 finish_op = FALSE;
1240#ifdef CURSOR_SHAPE
1241 /* Redraw the cursor with another shape, if we were in Operator-pending
1242 * mode or did a replace command. */
1243 if (c || ca.cmdchar == 'r')
1244 {
1245 ui_cursor_shape(); /* may show different cursor shape */
1246# ifdef FEAT_MOUSESHAPE
1247 update_mouseshape(-1);
1248# endif
1249 }
1250#endif
1251
1252#ifdef FEAT_CMDL_INFO
1253 if (oap->op_type == OP_NOP && oap->regname == 0)
1254 clear_showcmd();
1255#endif
1256
1257 checkpcmark(); /* check if we moved since setting pcmark */
1258 vim_free(ca.searchbuf);
1259
1260#ifdef FEAT_MBYTE
1261 if (has_mbyte)
1262 mb_adjust_cursor();
1263#endif
1264
1265#ifdef FEAT_SCROLLBIND
1266 if (curwin->w_p_scb && toplevel)
1267 {
1268 validate_cursor(); /* may need to update w_leftcol */
1269 do_check_scrollbind(TRUE);
1270 }
1271#endif
1272
1273 /*
1274 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1275 * if still inside a mapping that started in Visual mode).
1276 * May switch from Visual to Select mode after CTRL-O command.
1277 */
1278 if ( oap->op_type == OP_NOP
1279#ifdef FEAT_VISUAL
1280 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1281 || restart_VIsual_select == 1)
1282#endif
1283 && !(ca.retval & CA_COMMAND_BUSY)
1284 && stuff_empty()
1285 && oap->regname == 0)
1286 {
1287#ifdef FEAT_VISUAL
1288 if (restart_VIsual_select == 1)
1289 {
1290 VIsual_select = TRUE;
1291 showmode();
1292 restart_VIsual_select = 0;
1293 }
1294#endif
1295 if (restart_edit != 0
1296#ifdef FEAT_VISUAL
1297 && !VIsual_active
1298#endif
1299 && old_mapped_len == 0)
1300 (void)edit(restart_edit, FALSE, 1L);
1301 }
1302
1303#ifdef FEAT_VISUAL
1304 if (restart_VIsual_select == 2)
1305 restart_VIsual_select = 1;
1306#endif
1307
1308 /* Save count before an operator for next time. */
1309 opcount = ca.opcount;
1310}
1311
1312/*
1313 * Handle an operator after visual mode or when the movement is finished
1314 */
1315 void
1316do_pending_operator(cap, old_col, gui_yank)
1317 cmdarg_T *cap;
1318 int old_col;
1319 int gui_yank;
1320{
1321 oparg_T *oap = cap->oap;
1322 pos_T old_cursor;
1323 int empty_region_error;
1324 int restart_edit_save;
1325
1326#ifdef FEAT_VISUAL
1327 /* The visual area is remembered for redo */
1328 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1329 static linenr_T redo_VIsual_line_count; /* number of lines */
1330 static colnr_T redo_VIsual_col; /* number of cols or end column */
1331 static long redo_VIsual_count; /* count for Visual operator */
1332# ifdef FEAT_VIRTUALEDIT
1333 int include_line_break = FALSE;
1334# endif
1335#endif
1336
1337#if defined(FEAT_CLIPBOARD)
1338 /*
1339 * Yank the visual area into the GUI selection register before we operate
1340 * on it and lose it forever.
1341 * Don't do it if a specific register was specified, so that ""x"*P works.
1342 * This could call do_pending_operator() recursively, but that's OK
1343 * because gui_yank will be TRUE for the nested call.
1344 */
1345 if (clip_star.available
1346 && oap->op_type != OP_NOP
1347 && !gui_yank
1348# ifdef FEAT_VISUAL
1349 && VIsual_active
1350 && !redo_VIsual_busy
1351# endif
1352 && oap->regname == 0)
1353 clip_auto_select();
1354#endif
1355 old_cursor = curwin->w_cursor;
1356
1357 /*
1358 * If an operation is pending, handle it...
1359 */
1360 if ((finish_op
1361#ifdef FEAT_VISUAL
1362 || VIsual_active
1363#endif
1364 ) && oap->op_type != OP_NOP)
1365 {
1366#ifdef FEAT_VISUAL
1367 oap->is_VIsual = VIsual_active;
1368 if (oap->motion_force == 'V')
1369 oap->motion_type = MLINE;
1370 else if (oap->motion_force == 'v')
1371 {
1372 /* If the motion was linewise, "inclusive" will not have been set.
1373 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1374 if (oap->motion_type == MLINE)
1375 oap->inclusive = FALSE;
1376 /* If the motion already was characterwise, toggle "inclusive" */
1377 else if (oap->motion_type == MCHAR)
1378 oap->inclusive = !oap->inclusive;
1379 oap->motion_type = MCHAR;
1380 }
1381 else if (oap->motion_force == Ctrl_V)
1382 {
1383 /* Change line- or characterwise motion into Visual block mode. */
1384 VIsual_active = TRUE;
1385 VIsual = oap->start;
1386 VIsual_mode = Ctrl_V;
1387 VIsual_select = FALSE;
1388 VIsual_reselect = FALSE;
1389 }
1390#endif
1391
1392 /* only redo yank when 'y' flag is in 'cpoptions' */
1393 /* never redo "zf" (define fold) */
1394 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1395#ifdef FEAT_VISUAL
1396 && (!VIsual_active || oap->motion_force)
1397#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001398 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#ifdef FEAT_FOLDING
1400 && oap->op_type != OP_FOLD
1401 && oap->op_type != OP_FOLDOPEN
1402 && oap->op_type != OP_FOLDOPENREC
1403 && oap->op_type != OP_FOLDCLOSE
1404 && oap->op_type != OP_FOLDCLOSEREC
1405 && oap->op_type != OP_FOLDDEL
1406 && oap->op_type != OP_FOLDDELREC
1407#endif
1408 )
1409 {
1410 prep_redo(oap->regname, cap->count0,
1411 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1412 oap->motion_force, cap->cmdchar, cap->nchar);
1413 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1414 {
1415 /*
1416 * If 'cpoptions' does not contain 'r', insert the search
1417 * pattern to really repeat the same command.
1418 */
1419 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001420 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 AppendToRedobuff(NL_STR);
1422 }
1423 else if (cap->cmdchar == ':')
1424 {
1425 /* do_cmdline() has stored the first typed line in
1426 * "repeat_cmdline". When several lines are typed repeating
1427 * won't be possible. */
1428 if (repeat_cmdline == NULL)
1429 ResetRedobuff();
1430 else
1431 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001432 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 AppendToRedobuff(NL_STR);
1434 vim_free(repeat_cmdline);
1435 repeat_cmdline = NULL;
1436 }
1437 }
1438 }
1439
1440#ifdef FEAT_VISUAL
1441 if (redo_VIsual_busy)
1442 {
1443 oap->start = curwin->w_cursor;
1444 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1445 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1446 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1447 VIsual_mode = redo_VIsual_mode;
1448 if (VIsual_mode == 'v')
1449 {
1450 if (redo_VIsual_line_count <= 1)
1451 curwin->w_cursor.col += redo_VIsual_col - 1;
1452 else
1453 curwin->w_cursor.col = redo_VIsual_col;
1454 }
1455 if (redo_VIsual_col == MAXCOL)
1456 {
1457 curwin->w_curswant = MAXCOL;
1458 coladvance((colnr_T)MAXCOL);
1459 }
1460 cap->count0 = redo_VIsual_count;
1461 if (redo_VIsual_count != 0)
1462 cap->count1 = redo_VIsual_count;
1463 else
1464 cap->count1 = 1;
1465 }
1466 else if (VIsual_active)
1467 {
1468 /* Save the current VIsual area for '< and '> marks, and "gv" */
1469 curbuf->b_visual_start = VIsual;
1470 curbuf->b_visual_end = curwin->w_cursor;
1471 curbuf->b_visual_mode = VIsual_mode;
1472# ifdef FEAT_EVAL
1473 curbuf->b_visual_mode_eval = VIsual_mode;
1474# endif
1475 curbuf->b_visual_curswant = curwin->w_curswant;
1476
1477 /* In Select mode, a linewise selection is operated upon like a
1478 * characterwise selection. */
1479 if (VIsual_select && VIsual_mode == 'V')
1480 {
1481 if (lt(VIsual, curwin->w_cursor))
1482 {
1483 VIsual.col = 0;
1484 curwin->w_cursor.col =
1485 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1486 }
1487 else
1488 {
1489 curwin->w_cursor.col = 0;
1490 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1491 }
1492 VIsual_mode = 'v';
1493 }
1494 /* If 'selection' is "exclusive", backup one character for
1495 * charwise selections. */
1496 else if (VIsual_mode == 'v')
1497 {
1498# ifdef FEAT_VIRTUALEDIT
1499 include_line_break =
1500# endif
1501 unadjust_for_sel();
1502 }
1503
1504 oap->start = VIsual;
1505 if (VIsual_mode == 'V')
1506 oap->start.col = 0;
1507 }
1508#endif /* FEAT_VISUAL */
1509
1510 /*
1511 * Set oap->start to the first position of the operated text, oap->end
1512 * to the end of the operated text. w_cursor is equal to oap->start.
1513 */
1514 if (lt(oap->start, curwin->w_cursor))
1515 {
1516#ifdef FEAT_FOLDING
1517 /* Include folded lines completely. */
1518 if (!VIsual_active)
1519 {
1520 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1521 oap->start.col = 0;
1522 if (hasFolding(curwin->w_cursor.lnum, NULL,
1523 &curwin->w_cursor.lnum))
1524 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1525 }
1526#endif
1527 oap->end = curwin->w_cursor;
1528 curwin->w_cursor = oap->start;
1529
1530 /* w_virtcol may have been updated; if the cursor goes back to its
1531 * previous position w_virtcol becomes invalid and isn't updated
1532 * automatically. */
1533 curwin->w_valid &= ~VALID_VIRTCOL;
1534 }
1535 else
1536 {
1537#ifdef FEAT_FOLDING
1538 /* Include folded lines completely. */
1539 if (!VIsual_active && oap->motion_type == MLINE)
1540 {
1541 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1542 NULL))
1543 curwin->w_cursor.col = 0;
1544 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1545 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1546 }
1547#endif
1548 oap->end = oap->start;
1549 oap->start = curwin->w_cursor;
1550 }
1551
1552 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1553
1554#ifdef FEAT_VIRTUALEDIT
1555 /* Set "virtual_op" before resetting VIsual_active. */
1556 virtual_op = virtual_active();
1557#endif
1558
1559#ifdef FEAT_VISUAL
1560 if (VIsual_active || redo_VIsual_busy)
1561 {
1562 if (VIsual_mode == Ctrl_V) /* block mode */
1563 {
1564 colnr_T start, end;
1565
1566 oap->block_mode = TRUE;
1567
1568 getvvcol(curwin, &(oap->start),
1569 &oap->start_vcol, NULL, &oap->end_vcol);
1570 if (!redo_VIsual_busy)
1571 {
1572 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1573
1574 if (start < oap->start_vcol)
1575 oap->start_vcol = start;
1576 if (end > oap->end_vcol)
1577 {
1578 if (*p_sel == 'e' && start >= 1
1579 && start - 1 >= oap->end_vcol)
1580 oap->end_vcol = start - 1;
1581 else
1582 oap->end_vcol = end;
1583 }
1584 }
1585
1586 /* if '$' was used, get oap->end_vcol from longest line */
1587 if (curwin->w_curswant == MAXCOL)
1588 {
1589 curwin->w_cursor.col = MAXCOL;
1590 oap->end_vcol = 0;
1591 for (curwin->w_cursor.lnum = oap->start.lnum;
1592 curwin->w_cursor.lnum <= oap->end.lnum;
1593 ++curwin->w_cursor.lnum)
1594 {
1595 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1596 if (end > oap->end_vcol)
1597 oap->end_vcol = end;
1598 }
1599 }
1600 else if (redo_VIsual_busy)
1601 oap->end_vcol = oap->start_vcol + redo_VIsual_col - 1;
1602 /*
1603 * Correct oap->end.col and oap->start.col to be the
1604 * upper-left and lower-right corner of the block area.
1605 *
1606 * (Actually, this does convert column positions into character
1607 * positions)
1608 */
1609 curwin->w_cursor.lnum = oap->end.lnum;
1610 coladvance(oap->end_vcol);
1611 oap->end = curwin->w_cursor;
1612
1613 curwin->w_cursor = oap->start;
1614 coladvance(oap->start_vcol);
1615 oap->start = curwin->w_cursor;
1616 }
1617
1618 if (!redo_VIsual_busy && !gui_yank)
1619 {
1620 /*
1621 * Prepare to reselect and redo Visual: this is based on the
1622 * size of the Visual text
1623 */
1624 resel_VIsual_mode = VIsual_mode;
1625 if (curwin->w_curswant == MAXCOL)
1626 resel_VIsual_col = MAXCOL;
1627 else if (VIsual_mode == Ctrl_V)
1628 resel_VIsual_col = oap->end_vcol - oap->start_vcol + 1;
1629 else if (oap->line_count > 1)
1630 resel_VIsual_col = oap->end.col;
1631 else
1632 resel_VIsual_col = oap->end.col - oap->start.col + 1;
1633 resel_VIsual_line_count = oap->line_count;
1634 }
1635
1636 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1637 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1638 && oap->op_type != OP_COLON
1639#ifdef FEAT_FOLDING
1640 && oap->op_type != OP_FOLD
1641 && oap->op_type != OP_FOLDOPEN
1642 && oap->op_type != OP_FOLDOPENREC
1643 && oap->op_type != OP_FOLDCLOSE
1644 && oap->op_type != OP_FOLDCLOSEREC
1645 && oap->op_type != OP_FOLDDEL
1646 && oap->op_type != OP_FOLDDELREC
1647#endif
1648 && oap->motion_force == NUL
1649 )
1650 {
1651 /* Prepare for redoing. Only use the nchar field for "r",
1652 * otherwise it might be the second char of the operator. */
1653 prep_redo(oap->regname, 0L, NUL, 'v',
1654 get_op_char(oap->op_type),
1655 get_extra_op_char(oap->op_type),
1656 oap->op_type == OP_REPLACE ? cap->nchar : NUL);
1657 if (!redo_VIsual_busy)
1658 {
1659 redo_VIsual_mode = resel_VIsual_mode;
1660 redo_VIsual_col = resel_VIsual_col;
1661 redo_VIsual_line_count = resel_VIsual_line_count;
1662 redo_VIsual_count = cap->count0;
1663 }
1664 }
1665
1666 /*
1667 * oap->inclusive defaults to TRUE.
1668 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1669 * FALSE. This makes "d}P" and "v}dP" work the same.
1670 */
1671 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1672 oap->inclusive = TRUE;
1673 if (VIsual_mode == 'V')
1674 oap->motion_type = MLINE;
1675 else
1676 {
1677 oap->motion_type = MCHAR;
1678 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
1679# ifdef FEAT_VIRTUALEDIT
1680 && (include_line_break || !virtual_op)
1681# endif
1682 )
1683 {
1684 oap->inclusive = FALSE;
1685 /* Try to include the newline, unless it's an operator
1686 * that works on lines only */
1687 if (*p_sel != 'o'
1688 && !op_on_lines(oap->op_type)
1689 && oap->end.lnum < curbuf->b_ml.ml_line_count)
1690 {
1691 ++oap->end.lnum;
1692 oap->end.col = 0;
1693# ifdef FEAT_VIRTUALEDIT
1694 oap->end.coladd = 0;
1695# endif
1696 ++oap->line_count;
1697 }
1698 }
1699 }
1700
1701 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 /*
1704 * Switch Visual off now, so screen updating does
1705 * not show inverted text when the screen is redrawn.
1706 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1707 * no screen redraw, so it is done here to remove the inverted
1708 * part.
1709 */
1710 if (!gui_yank)
1711 {
1712 VIsual_active = FALSE;
1713# ifdef FEAT_MOUSE
1714 setmouse();
1715 mouse_dragging = 0;
1716# endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00001717 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 clear_cmdline = TRUE; /* unshow visual mode later */
1719#ifdef FEAT_CMDL_INFO
1720 else
1721 clear_showcmd();
1722#endif
1723 if ((oap->op_type == OP_YANK
1724 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001725 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 || oap->op_type == OP_FILTER)
1727 && oap->motion_force == NUL)
1728 redraw_curbuf_later(INVERTED);
1729 }
1730 }
1731#endif
1732
1733#ifdef FEAT_MBYTE
1734 /* Include the trailing byte of a multi-byte char. */
1735 if (has_mbyte && oap->inclusive)
1736 {
1737 int l;
1738
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001739 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (l > 1)
1741 oap->end.col += l - 1;
1742 }
1743#endif
1744 curwin->w_set_curswant = TRUE;
1745
1746 /*
1747 * oap->empty is set when start and end are the same. The inclusive
1748 * flag affects this too, unless yanking and the end is on a NUL.
1749 */
1750 oap->empty = (oap->motion_type == MCHAR
1751 && (!oap->inclusive
1752 || (oap->op_type == OP_YANK
1753 && gchar_pos(&oap->end) == NUL))
1754 && equalpos(oap->start, oap->end)
1755#ifdef FEAT_VIRTUALEDIT
1756 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1757#endif
1758 );
1759 /*
1760 * For delete, change and yank, it's an error to operate on an
1761 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1762 */
1763 empty_region_error = (oap->empty
1764 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1765
1766#ifdef FEAT_VISUAL
1767 /* Force a redraw when operating on an empty Visual region, when
1768 * 'modifiable is off or creating a fold. */
1769 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
1770# ifdef FEAT_FOLDING
1771 || oap->op_type == OP_FOLD
1772# endif
1773 ))
1774 redraw_curbuf_later(INVERTED);
1775#endif
1776
1777 /*
1778 * If the end of an operator is in column one while oap->motion_type
1779 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1780 * character in the previous line. If op_start is on or before the
1781 * first non-blank in the line, the operator becomes linewise
1782 * (strange, but that's the way vi does it).
1783 */
1784 if ( oap->motion_type == MCHAR
1785 && oap->inclusive == FALSE
1786 && !(cap->retval & CA_NO_ADJ_OP_END)
1787 && oap->end.col == 0
1788#ifdef FEAT_VISUAL
1789 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001790 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
1792 && oap->line_count > 1)
1793 {
1794 oap->end_adjusted = TRUE; /* remember that we did this */
1795 --oap->line_count;
1796 --oap->end.lnum;
1797 if (inindent(0))
1798 oap->motion_type = MLINE;
1799 else
1800 {
1801 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1802 if (oap->end.col)
1803 {
1804 --oap->end.col;
1805 oap->inclusive = TRUE;
1806 }
1807 }
1808 }
1809 else
1810 oap->end_adjusted = FALSE;
1811
1812 switch (oap->op_type)
1813 {
1814 case OP_LSHIFT:
1815 case OP_RSHIFT:
1816 op_shift(oap, TRUE,
1817#ifdef FEAT_VISUAL
1818 oap->is_VIsual ? (int)cap->count1 :
1819#endif
1820 1);
1821 auto_format(FALSE, TRUE);
1822 break;
1823
1824 case OP_JOIN_NS:
1825 case OP_JOIN:
1826 if (oap->line_count < 2)
1827 oap->line_count = 2;
1828 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1829 curbuf->b_ml.ml_line_count)
1830 beep_flush();
1831 else
1832 {
1833 do_do_join(oap->line_count, oap->op_type == OP_JOIN);
1834 auto_format(FALSE, TRUE);
1835 }
1836 break;
1837
1838 case OP_DELETE:
1839#ifdef FEAT_VISUAL
1840 VIsual_reselect = FALSE; /* don't reselect now */
1841#endif
1842 if (empty_region_error)
1843 vim_beep();
1844 else
1845 {
1846 (void)op_delete(oap);
1847 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1848 u_save_cursor(); /* cursor line wasn't saved yet */
1849 auto_format(FALSE, TRUE);
1850 }
1851 break;
1852
1853 case OP_YANK:
1854 if (empty_region_error)
1855 {
1856 if (!gui_yank)
1857 vim_beep();
1858 }
1859 else
1860 (void)op_yank(oap, FALSE, !gui_yank);
1861 check_cursor_col();
1862 break;
1863
1864 case OP_CHANGE:
1865#ifdef FEAT_VISUAL
1866 VIsual_reselect = FALSE; /* don't reselect now */
1867#endif
1868 if (empty_region_error)
1869 vim_beep();
1870 else
1871 {
1872 /* This is a new edit command, not a restart. Need to
1873 * remember it to make 'insertmode' work with mappings for
1874 * Visual mode. But do this only once and not when typed and
1875 * 'insertmode' isn't set. */
1876 if (p_im || !KeyTyped)
1877 restart_edit_save = restart_edit;
1878 else
1879 restart_edit_save = 0;
1880 restart_edit = 0;
1881 /* Reset finish_op now, don't want it set inside edit(). */
1882 finish_op = FALSE;
1883 if (op_change(oap)) /* will call edit() */
1884 cap->retval |= CA_COMMAND_BUSY;
1885 if (restart_edit == 0)
1886 restart_edit = restart_edit_save;
1887 }
1888 break;
1889
1890 case OP_FILTER:
1891 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1892 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1893 else
1894 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1895
1896 case OP_INDENT:
1897 case OP_COLON:
1898
1899#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1900 /*
1901 * If 'equalprg' is empty, do the indenting internally.
1902 */
1903 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1904 {
1905# ifdef FEAT_LISP
1906 if (curbuf->b_p_lisp)
1907 {
1908 op_reindent(oap, get_lisp_indent);
1909 break;
1910 }
1911# endif
1912# ifdef FEAT_CINDENT
1913 op_reindent(oap,
1914# ifdef FEAT_EVAL
1915 *curbuf->b_p_inde != NUL ? get_expr_indent :
1916# endif
1917 get_c_indent);
1918 break;
1919# endif
1920 }
1921#endif
1922
1923 op_colon(oap);
1924 break;
1925
1926 case OP_TILDE:
1927 case OP_UPPER:
1928 case OP_LOWER:
1929 case OP_ROT13:
1930 if (empty_region_error)
1931 vim_beep();
1932 else
1933 op_tilde(oap);
1934 check_cursor_col();
1935 break;
1936
1937 case OP_FORMAT:
1938 if (*p_fp != NUL)
1939 op_colon(oap); /* use external command */
1940 else
1941 op_format(oap, FALSE); /* use internal function */
1942 break;
1943
1944 case OP_FORMAT2:
1945 op_format(oap, TRUE); /* use internal function */
1946 break;
1947
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001948 case OP_FUNCTION:
1949 op_function(oap); /* call 'operatorfunc' */
1950 break;
1951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 case OP_INSERT:
1953 case OP_APPEND:
1954#ifdef FEAT_VISUAL
1955 VIsual_reselect = FALSE; /* don't reselect now */
1956#endif
1957#ifdef FEAT_VISUALEXTRA
1958 if (empty_region_error)
1959 vim_beep();
1960 else
1961 {
1962 /* This is a new edit command, not a restart. Need to
1963 * remember it to make 'insertmode' work with mappings for
1964 * Visual mode. But do this only once. */
1965 restart_edit_save = restart_edit;
1966 restart_edit = 0;
1967
1968 op_insert(oap, cap->count1);
1969
1970 /* TODO: when inserting in several lines, should format all
1971 * the lines. */
1972 auto_format(FALSE, TRUE);
1973
1974 if (restart_edit == 0)
1975 restart_edit = restart_edit_save;
1976 }
1977#else
1978 vim_beep();
1979#endif
1980 break;
1981
1982 case OP_REPLACE:
1983#ifdef FEAT_VISUAL
1984 VIsual_reselect = FALSE; /* don't reselect now */
1985#endif
1986#ifdef FEAT_VISUALEXTRA
1987 if (empty_region_error)
1988#endif
1989 vim_beep();
1990#ifdef FEAT_VISUALEXTRA
1991 else
1992 op_replace(oap, cap->nchar);
1993#endif
1994 break;
1995
1996#ifdef FEAT_FOLDING
1997 case OP_FOLD:
1998 VIsual_reselect = FALSE; /* don't reselect now */
1999 foldCreate(oap->start.lnum, oap->end.lnum);
2000 break;
2001
2002 case OP_FOLDOPEN:
2003 case OP_FOLDOPENREC:
2004 case OP_FOLDCLOSE:
2005 case OP_FOLDCLOSEREC:
2006 VIsual_reselect = FALSE; /* don't reselect now */
2007 opFoldRange(oap->start.lnum, oap->end.lnum,
2008 oap->op_type == OP_FOLDOPEN
2009 || oap->op_type == OP_FOLDOPENREC,
2010 oap->op_type == OP_FOLDOPENREC
2011 || oap->op_type == OP_FOLDCLOSEREC,
2012 oap->is_VIsual);
2013 break;
2014
2015 case OP_FOLDDEL:
2016 case OP_FOLDDELREC:
2017 VIsual_reselect = FALSE; /* don't reselect now */
2018 deleteFold(oap->start.lnum, oap->end.lnum,
2019 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2020 break;
2021#endif
2022 default:
2023 clearopbeep(oap);
2024 }
2025#ifdef FEAT_VIRTUALEDIT
2026 virtual_op = MAYBE;
2027#endif
2028 if (!gui_yank)
2029 {
2030 /*
2031 * if 'sol' not set, go back to old column for some commands
2032 */
2033 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2034 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2035 || oap->op_type == OP_DELETE))
2036 coladvance(curwin->w_curswant = old_col);
2037 }
2038 else
2039 {
2040 curwin->w_cursor = old_cursor;
2041 }
2042#ifdef FEAT_VISUAL
2043 oap->block_mode = FALSE;
2044#endif
2045 clearop(oap);
2046 }
2047}
2048
2049/*
2050 * Handle indent and format operators and visual mode ":".
2051 */
2052 static void
2053op_colon(oap)
2054 oparg_T *oap;
2055{
2056 stuffcharReadbuff(':');
2057#ifdef FEAT_VISUAL
2058 if (oap->is_VIsual)
2059 stuffReadbuff((char_u *)"'<,'>");
2060 else
2061#endif
2062 {
2063 /*
2064 * Make the range look nice, so it can be repeated.
2065 */
2066 if (oap->start.lnum == curwin->w_cursor.lnum)
2067 stuffcharReadbuff('.');
2068 else
2069 stuffnumReadbuff((long)oap->start.lnum);
2070 if (oap->end.lnum != oap->start.lnum)
2071 {
2072 stuffcharReadbuff(',');
2073 if (oap->end.lnum == curwin->w_cursor.lnum)
2074 stuffcharReadbuff('.');
2075 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2076 stuffcharReadbuff('$');
2077 else if (oap->start.lnum == curwin->w_cursor.lnum)
2078 {
2079 stuffReadbuff((char_u *)".+");
2080 stuffnumReadbuff((long)oap->line_count - 1);
2081 }
2082 else
2083 stuffnumReadbuff((long)oap->end.lnum);
2084 }
2085 }
2086 if (oap->op_type != OP_COLON)
2087 stuffReadbuff((char_u *)"!");
2088 if (oap->op_type == OP_INDENT)
2089 {
2090#ifndef FEAT_CINDENT
2091 if (*get_equalprg() == NUL)
2092 stuffReadbuff((char_u *)"indent");
2093 else
2094#endif
2095 stuffReadbuff(get_equalprg());
2096 stuffReadbuff((char_u *)"\n");
2097 }
2098 else if (oap->op_type == OP_FORMAT)
2099 {
2100 if (*p_fp == NUL)
2101 stuffReadbuff((char_u *)"fmt");
2102 else
2103 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002104 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
2107 /*
2108 * do_cmdline() does the rest
2109 */
2110}
2111
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002112/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002113 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002114 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002115/*ARGSUSED*/
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002116 void
2117op_function(oap)
2118 oparg_T *oap;
2119{
2120#ifdef FEAT_EVAL
2121 char_u *(argv[1]);
2122
2123 if (*p_opfunc == NUL)
2124 EMSG(_("E774: 'operatorfunc' is empty"));
2125 else
2126 {
2127 /* Set '[ and '] marks to text to be operated on. */
2128 curbuf->b_op_start = oap->start;
2129 curbuf->b_op_end = oap->end;
2130 if (oap->motion_type != MLINE && !oap->inclusive)
2131 /* Exclude the end position. */
2132 decl(&curbuf->b_op_end);
2133
2134 if (oap->block_mode)
2135 argv[0] = (char_u *)"block";
2136 else if (oap->motion_type == MLINE)
2137 argv[0] = (char_u *)"line";
2138 else
2139 argv[0] = (char_u *)"char";
2140 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
2141 }
2142#else
2143 EMSG(_("E775: Eval feature not available"));
2144#endif
2145}
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#if defined(FEAT_MOUSE) || defined(PROTO)
2148/*
2149 * Do the appropriate action for the current mouse click in the current mode.
2150 * Not used for Command-line mode.
2151 *
2152 * Normal Mode:
2153 * event modi- position visual change action
2154 * fier cursor window
2155 * left press - yes end yes
2156 * left press C yes end yes "^]" (2)
2157 * left press S yes end yes "*" (2)
2158 * left drag - yes start if moved no
2159 * left relse - yes start if moved no
2160 * middle press - yes if not active no put register
2161 * middle press - yes if active no yank and put
2162 * right press - yes start or extend yes
2163 * right press S yes no change yes "#" (2)
2164 * right drag - yes extend no
2165 * right relse - yes extend no
2166 *
2167 * Insert or Replace Mode:
2168 * event modi- position visual change action
2169 * fier cursor window
2170 * left press - yes (cannot be active) yes
2171 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2172 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2173 * left drag - yes start or extend (1) no CTRL-O (1)
2174 * left relse - yes start or extend (1) no CTRL-O (1)
2175 * middle press - no (cannot be active) no put register
2176 * right press - yes start or extend yes CTRL-O
2177 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2178 *
2179 * (1) only if mouse pointer moved since press
2180 * (2) only if click is in same buffer
2181 *
2182 * Return TRUE if start_arrow() should be called for edit mode.
2183 */
2184 int
2185do_mouse(oap, c, dir, count, fixindent)
2186 oparg_T *oap; /* operator argument, can be NULL */
2187 int c; /* K_LEFTMOUSE, etc */
2188 int dir; /* Direction to 'put' if necessary */
2189 long count;
2190 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2191{
2192 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2193 static int got_click = FALSE; /* got a click some time back */
2194
2195 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2196 int is_click; /* If FALSE it's a drag or release event */
2197 int is_drag; /* If TRUE it's a drag event */
2198 int jump_flags = 0; /* flags for jump_to_mouse() */
2199 pos_T start_visual;
2200 int moved; /* Has cursor moved? */
2201 int in_status_line; /* mouse in status line */
2202#ifdef FEAT_VERTSPLIT
2203 int in_sep_line; /* mouse in vertical separator line */
2204#endif
2205 int c1, c2;
2206#if defined(FEAT_FOLDING)
2207 pos_T save_cursor;
2208#endif
2209 win_T *old_curwin = curwin;
2210#ifdef FEAT_VISUAL
2211 static pos_T orig_cursor;
2212 colnr_T leftcol, rightcol;
2213 pos_T end_visual;
2214 int diff;
2215 int old_active = VIsual_active;
2216 int old_mode = VIsual_mode;
2217#endif
2218 int regname;
2219
2220#if defined(FEAT_FOLDING)
2221 save_cursor = curwin->w_cursor;
2222#endif
2223
2224 /*
2225 * When GUI is active, always recognize mouse events, otherwise:
2226 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2227 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2228 * - For command line and insert mode 'mouse' is checked before calling
2229 * do_mouse().
2230 */
2231 if (do_always)
2232 do_always = FALSE;
2233 else
2234#ifdef FEAT_GUI
2235 if (!gui.in_use)
2236#endif
2237 {
2238#ifdef FEAT_VISUAL
2239 if (VIsual_active)
2240 {
2241 if (!mouse_has(MOUSE_VISUAL))
2242 return FALSE;
2243 }
2244 else
2245#endif
2246 if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
2247 return FALSE;
2248 }
2249
2250 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2251
2252#ifdef FEAT_MOUSESHAPE
2253 /* May have stopped dragging the status or separator line. The pointer is
2254 * most likely still on the status or separator line. */
2255 if (!is_drag && drag_status_line)
2256 {
2257 drag_status_line = FALSE;
2258 update_mouseshape(SHAPE_IDX_STATUS);
2259 }
2260# ifdef FEAT_VERTSPLIT
2261 if (!is_drag && drag_sep_line)
2262 {
2263 drag_sep_line = FALSE;
2264 update_mouseshape(SHAPE_IDX_VSEP);
2265 }
2266# endif
2267#endif
2268
2269 /*
2270 * Ignore drag and release events if we didn't get a click.
2271 */
2272 if (is_click)
2273 got_click = TRUE;
2274 else
2275 {
2276 if (!got_click) /* didn't get click, ignore */
2277 return FALSE;
2278 if (!is_drag) /* release, reset got_click */
2279 got_click = FALSE;
2280 }
2281
Bram Moolenaar64969662005-12-14 21:59:55 +00002282#ifndef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 /*
Bram Moolenaar64969662005-12-14 21:59:55 +00002284 * ALT is only used for starging/extending Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 */
2286 if ((mod_mask & MOD_MASK_ALT))
2287 return FALSE;
Bram Moolenaar64969662005-12-14 21:59:55 +00002288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 /*
2291 * CTRL right mouse button does CTRL-T
2292 */
2293 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2294 {
2295 if (State & INSERT)
2296 stuffcharReadbuff(Ctrl_O);
2297 if (count > 1)
2298 stuffnumReadbuff(count);
2299 stuffcharReadbuff(Ctrl_T);
2300 got_click = FALSE; /* ignore drag&release now */
2301 return FALSE;
2302 }
2303
2304 /*
2305 * CTRL only works with left mouse button
2306 */
2307 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2308 return FALSE;
2309
2310 /*
2311 * When a modifier is down, ignore drag and release events, as well as
2312 * multiple clicks and the middle mouse button.
2313 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2314 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002315 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2316 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 && (!is_click
2318 || (mod_mask & MOD_MASK_MULTI_CLICK)
2319 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002320 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 && mouse_model_popup()
2322 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002323 && !((mod_mask & MOD_MASK_ALT)
2324 && !mouse_model_popup()
2325 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 )
2327 return FALSE;
2328
2329 /*
2330 * If the button press was used as the movement command for an operator
2331 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2332 * drag/release events.
2333 */
2334 if (!is_click && which_button == MOUSE_MIDDLE)
2335 return FALSE;
2336
2337 if (oap != NULL)
2338 regname = oap->regname;
2339 else
2340 regname = 0;
2341
2342 /*
2343 * Middle mouse button does a 'put' of the selected text
2344 */
2345 if (which_button == MOUSE_MIDDLE)
2346 {
2347 if (State == NORMAL)
2348 {
2349 /*
2350 * If an operator was pending, we don't know what the user wanted
2351 * to do. Go back to normal mode: Clear the operator and beep().
2352 */
2353 if (oap != NULL && oap->op_type != OP_NOP)
2354 {
2355 clearopbeep(oap);
2356 return FALSE;
2357 }
2358
2359#ifdef FEAT_VISUAL
2360 /*
2361 * If visual was active, yank the highlighted text and put it
2362 * before the mouse pointer position.
2363 */
2364 if (VIsual_active)
2365 {
2366 stuffcharReadbuff('y');
2367 stuffcharReadbuff(K_MIDDLEMOUSE);
2368 do_always = TRUE; /* ignore 'mouse' setting next time */
2369 return FALSE;
2370 }
2371#endif
2372 /*
2373 * The rest is below jump_to_mouse()
2374 */
2375 }
2376
2377 else if ((State & INSERT) == 0)
2378 return FALSE;
2379
2380 /*
2381 * Middle click in insert mode doesn't move the mouse, just insert the
2382 * contents of a register. '.' register is special, can't insert that
2383 * with do_put().
2384 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2385 * happens for the GUI).
2386 */
2387 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2388 {
2389 if (regname == '.')
2390 insert_reg(regname, TRUE);
2391 else
2392 {
2393#ifdef FEAT_CLIPBOARD
2394 if (clip_star.available && regname == 0)
2395 regname = '*';
2396#endif
2397 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2398 insert_reg(regname, TRUE);
2399 else
2400 {
2401 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2402
2403 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2404 AppendCharToRedobuff(Ctrl_R);
2405 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2406 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2407 }
2408 }
2409 return FALSE;
2410 }
2411 }
2412
2413 /* When dragging or button-up stay in the same window. */
2414 if (!is_click)
2415 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2416
2417 start_visual.lnum = 0;
2418
2419 /*
2420 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2421 * right button up -> pop-up menu
2422 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002423 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 if (mouse_model_popup())
2426 {
2427 if (which_button == MOUSE_RIGHT
2428 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2429 {
2430 /*
2431 * NOTE: Ignore right button down and drag mouse events.
2432 * Windows only shows the popup menu on the button up event.
2433 */
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002434#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 if (!is_click)
2436 return FALSE;
2437#endif
2438#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2439 if (is_click || is_drag)
2440 return FALSE;
2441#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002442#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2444 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2445 if (gui.in_use)
2446 {
2447 jump_flags = 0;
2448 if (STRCMP(p_mousem, "popup_setpos") == 0)
2449 {
2450 /* First set the cursor position before showing the popup
2451 * menu. */
2452#ifdef FEAT_VISUAL
2453 if (VIsual_active)
2454 {
2455 pos_T m_pos;
2456
2457 /*
2458 * set MOUSE_MAY_STOP_VIS if we are outside the
2459 * selection or the current window (might have false
2460 * negative here)
2461 */
2462 if (mouse_row < W_WINROW(curwin)
2463 || mouse_row
2464 > (W_WINROW(curwin) + curwin->w_height))
2465 jump_flags = MOUSE_MAY_STOP_VIS;
2466 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2467 jump_flags = MOUSE_MAY_STOP_VIS;
2468 else
2469 {
2470 if ((lt(curwin->w_cursor, VIsual)
2471 && (lt(m_pos, curwin->w_cursor)
2472 || lt(VIsual, m_pos)))
2473 || (lt(VIsual, curwin->w_cursor)
2474 && (lt(m_pos, VIsual)
2475 || lt(curwin->w_cursor, m_pos))))
2476 {
2477 jump_flags = MOUSE_MAY_STOP_VIS;
2478 }
2479 else if (VIsual_mode == Ctrl_V)
2480 {
2481 getvcols(curwin, &curwin->w_cursor, &VIsual,
2482 &leftcol, &rightcol);
2483 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2484 if (m_pos.col < leftcol || m_pos.col > rightcol)
2485 jump_flags = MOUSE_MAY_STOP_VIS;
2486 }
2487 }
2488 }
2489 else
2490 jump_flags = MOUSE_MAY_STOP_VIS;
2491#endif
2492 }
2493 if (jump_flags)
2494 {
2495 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2496 update_curbuf(
2497#ifdef FEAT_VISUAL
2498 VIsual_active ? INVERTED :
2499#endif
2500 VALID);
2501 setcursor();
2502 out_flush(); /* Update before showing popup menu */
2503 }
2504# ifdef FEAT_MENU
2505 gui_show_popupmenu();
2506# endif
2507 return (jump_flags & CURSOR_MOVED) != 0;
2508 }
2509 else
2510 return FALSE;
2511#else
2512 return FALSE;
2513#endif
2514 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002515 if (which_button == MOUSE_LEFT
2516 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 {
2518 which_button = MOUSE_RIGHT;
2519 mod_mask &= ~MOD_MASK_SHIFT;
2520 }
2521 }
2522
2523#ifdef FEAT_VISUAL
2524 if ((State & (NORMAL | INSERT))
2525 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2526 {
2527 if (which_button == MOUSE_LEFT)
2528 {
2529 if (is_click)
2530 {
2531 /* stop Visual mode for a left click in a window, but not when
2532 * on a status line */
2533 if (VIsual_active)
2534 jump_flags |= MOUSE_MAY_STOP_VIS;
2535 }
2536 else if (mouse_has(MOUSE_VISUAL))
2537 jump_flags |= MOUSE_MAY_VIS;
2538 }
2539 else if (which_button == MOUSE_RIGHT)
2540 {
2541 if (is_click && VIsual_active)
2542 {
2543 /*
2544 * Remember the start and end of visual before moving the
2545 * cursor.
2546 */
2547 if (lt(curwin->w_cursor, VIsual))
2548 {
2549 start_visual = curwin->w_cursor;
2550 end_visual = VIsual;
2551 }
2552 else
2553 {
2554 start_visual = VIsual;
2555 end_visual = curwin->w_cursor;
2556 }
2557 }
2558 jump_flags |= MOUSE_FOCUS;
2559 if (mouse_has(MOUSE_VISUAL))
2560 jump_flags |= MOUSE_MAY_VIS;
2561 }
2562 }
2563#endif
2564
2565 /*
2566 * If an operator is pending, ignore all drags and releases until the
2567 * next mouse click.
2568 */
2569 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2570 {
2571 got_click = FALSE;
2572 oap->motion_type = MCHAR;
2573 }
2574
2575 /* When releasing the button let jump_to_mouse() know. */
2576 if (!is_click && !is_drag)
2577 jump_flags |= MOUSE_RELEASED;
2578
2579 /*
2580 * JUMP!
2581 */
2582 jump_flags = jump_to_mouse(jump_flags,
2583 oap == NULL ? NULL : &(oap->inclusive), which_button);
2584 moved = (jump_flags & CURSOR_MOVED);
2585 in_status_line = (jump_flags & IN_STATUS_LINE);
2586#ifdef FEAT_VERTSPLIT
2587 in_sep_line = (jump_flags & IN_SEP_LINE);
2588#endif
2589
2590#ifdef FEAT_NETBEANS_INTG
2591 if (usingNetbeans && isNetbeansBuffer(curbuf)
2592 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2593 {
2594 int key = KEY2TERMCAP1(c);
2595
2596 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2597 || key == (int)KE_RIGHTRELEASE)
2598 netbeans_button_release(which_button);
2599 }
2600#endif
2601
2602 /* When jumping to another window, clear a pending operator. That's a bit
2603 * friendlier than beeping and not jumping to that window. */
2604 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2605 clearop(oap);
2606
2607#ifdef FEAT_FOLDING
2608 if (mod_mask == 0
2609 && !is_drag
2610 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2611 && which_button == MOUSE_LEFT)
2612 {
2613 /* open or close a fold at this line */
2614 if (jump_flags & MOUSE_FOLD_OPEN)
2615 openFold(curwin->w_cursor.lnum, 1L);
2616 else
2617 closeFold(curwin->w_cursor.lnum, 1L);
2618 /* don't move the cursor if still in the same window */
2619 if (curwin == old_curwin)
2620 curwin->w_cursor = save_cursor;
2621 }
2622#endif
2623
2624#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2625 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2626 {
2627 clip_modeless(which_button, is_click, is_drag);
2628 return FALSE;
2629 }
2630#endif
2631
2632#ifdef FEAT_VISUAL
2633 /* Set global flag that we are extending the Visual area with mouse
2634 * dragging; temporarily mimimize 'scrolloff'. */
2635 if (VIsual_active && is_drag && p_so)
2636 {
2637 /* In the very first line, allow scrolling one line */
2638 if (mouse_row == 0)
2639 mouse_dragging = 2;
2640 else
2641 mouse_dragging = 1;
2642 }
2643
2644 /* When dragging the mouse above the window, scroll down. */
2645 if (is_drag && mouse_row < 0 && !in_status_line)
2646 {
2647 scroll_redraw(FALSE, 1L);
2648 mouse_row = 0;
2649 }
2650
2651 if (start_visual.lnum) /* right click in visual mode */
2652 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002653 /* When ALT is pressed make Visual mode blockwise. */
2654 if (mod_mask & MOD_MASK_ALT)
2655 VIsual_mode = Ctrl_V;
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 /*
2658 * In Visual-block mode, divide the area in four, pick up the corner
2659 * that is in the quarter that the cursor is in.
2660 */
2661 if (VIsual_mode == Ctrl_V)
2662 {
2663 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2664 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2665 end_visual.col = leftcol;
2666 else
2667 end_visual.col = rightcol;
2668 if (curwin->w_cursor.lnum <
2669 (start_visual.lnum + end_visual.lnum) / 2)
2670 end_visual.lnum = end_visual.lnum;
2671 else
2672 end_visual.lnum = start_visual.lnum;
2673
2674 /* move VIsual to the right column */
2675 start_visual = curwin->w_cursor; /* save the cursor pos */
2676 curwin->w_cursor = end_visual;
2677 coladvance(end_visual.col);
2678 VIsual = curwin->w_cursor;
2679 curwin->w_cursor = start_visual; /* restore the cursor */
2680 }
2681 else
2682 {
2683 /*
2684 * If the click is before the start of visual, change the start.
2685 * If the click is after the end of visual, change the end. If
2686 * the click is inside the visual, change the closest side.
2687 */
2688 if (lt(curwin->w_cursor, start_visual))
2689 VIsual = end_visual;
2690 else if (lt(end_visual, curwin->w_cursor))
2691 VIsual = start_visual;
2692 else
2693 {
2694 /* In the same line, compare column number */
2695 if (end_visual.lnum == start_visual.lnum)
2696 {
2697 if (curwin->w_cursor.col - start_visual.col >
2698 end_visual.col - curwin->w_cursor.col)
2699 VIsual = start_visual;
2700 else
2701 VIsual = end_visual;
2702 }
2703
2704 /* In different lines, compare line number */
2705 else
2706 {
2707 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2708 (end_visual.lnum - curwin->w_cursor.lnum);
2709
2710 if (diff > 0) /* closest to end */
2711 VIsual = start_visual;
2712 else if (diff < 0) /* closest to start */
2713 VIsual = end_visual;
2714 else /* in the middle line */
2715 {
2716 if (curwin->w_cursor.col <
2717 (start_visual.col + end_visual.col) / 2)
2718 VIsual = end_visual;
2719 else
2720 VIsual = start_visual;
2721 }
2722 }
2723 }
2724 }
2725 }
2726 /*
2727 * If Visual mode started in insert mode, execute "CTRL-O"
2728 */
2729 else if ((State & INSERT) && VIsual_active)
2730 stuffcharReadbuff(Ctrl_O);
2731#endif
2732
2733 /*
2734 * Middle mouse click: Put text before cursor.
2735 */
2736 if (which_button == MOUSE_MIDDLE)
2737 {
2738#ifdef FEAT_CLIPBOARD
2739 if (clip_star.available && regname == 0)
2740 regname = '*';
2741#endif
2742 if (yank_register_mline(regname))
2743 {
2744 if (mouse_past_bottom)
2745 dir = FORWARD;
2746 }
2747 else if (mouse_past_eol)
2748 dir = FORWARD;
2749
2750 if (fixindent)
2751 {
2752 c1 = (dir == BACKWARD) ? '[' : ']';
2753 c2 = 'p';
2754 }
2755 else
2756 {
2757 c1 = (dir == FORWARD) ? 'p' : 'P';
2758 c2 = NUL;
2759 }
2760 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2761
2762 /*
2763 * Remember where the paste started, so in edit() Insstart can be set
2764 * to this position
2765 */
2766 if (restart_edit != 0)
2767 where_paste_started = curwin->w_cursor;
2768 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2769 }
2770
2771#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2772 /*
2773 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2774 * error under the mouse pointer.
2775 */
2776 else if (((mod_mask & MOD_MASK_CTRL)
2777 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2778 && bt_quickfix(curbuf))
2779 {
2780 if (State & INSERT)
2781 stuffcharReadbuff(Ctrl_O);
2782 stuffReadbuff((char_u *)":.cc\n");
2783 got_click = FALSE; /* ignore drag&release now */
2784 }
2785#endif
2786
2787 /*
2788 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2789 * under the mouse pointer.
2790 */
2791 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2792 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2793 {
2794 if (State & INSERT)
2795 stuffcharReadbuff(Ctrl_O);
2796 stuffcharReadbuff(Ctrl_RSB);
2797 got_click = FALSE; /* ignore drag&release now */
2798 }
2799
2800 /*
2801 * Shift-Mouse click searches for the next occurrence of the word under
2802 * the mouse pointer
2803 */
2804 else if ((mod_mask & MOD_MASK_SHIFT))
2805 {
2806 if (State & INSERT
2807#ifdef FEAT_VISUAL
2808 || (VIsual_active && VIsual_select)
2809#endif
2810 )
2811 stuffcharReadbuff(Ctrl_O);
2812 if (which_button == MOUSE_LEFT)
2813 stuffcharReadbuff('*');
2814 else /* MOUSE_RIGHT */
2815 stuffcharReadbuff('#');
2816 }
2817
2818 /* Handle double clicks, unless on status line */
2819 else if (in_status_line)
2820 {
2821#ifdef FEAT_MOUSESHAPE
2822 if ((is_drag || is_click) && !drag_status_line)
2823 {
2824 drag_status_line = TRUE;
2825 update_mouseshape(-1);
2826 }
2827#endif
2828 }
2829#ifdef FEAT_VERTSPLIT
2830 else if (in_sep_line)
2831 {
2832# ifdef FEAT_MOUSESHAPE
2833 if ((is_drag || is_click) && !drag_sep_line)
2834 {
2835 drag_sep_line = TRUE;
2836 update_mouseshape(-1);
2837 }
2838# endif
2839 }
2840#endif
2841#ifdef FEAT_VISUAL
2842 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2843 && mouse_has(MOUSE_VISUAL))
2844 {
2845 if (is_click || !VIsual_active)
2846 {
2847 if (VIsual_active)
2848 orig_cursor = VIsual;
2849 else
2850 {
2851 check_visual_highlight();
2852 VIsual = curwin->w_cursor;
2853 orig_cursor = VIsual;
2854 VIsual_active = TRUE;
2855 VIsual_reselect = TRUE;
2856 /* start Select mode if 'selectmode' contains "mouse" */
2857 may_start_select('o');
2858 setmouse();
2859 }
2860 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00002861 {
2862 /* Double click with ALT pressed makes it blockwise. */
2863 if (mod_mask & MOD_MASK_ALT)
2864 VIsual_mode = Ctrl_V;
2865 else
2866 VIsual_mode = 'v';
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
2869 VIsual_mode = 'V';
2870 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
2871 VIsual_mode = Ctrl_V;
2872#ifdef FEAT_CLIPBOARD
2873 /* Make sure the clipboard gets updated. Needed because start and
2874 * end may still be the same, and the selection needs to be owned */
2875 clip_star.vmode = NUL;
2876#endif
2877 }
2878 /*
2879 * A double click selects a word or a block.
2880 */
2881 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2882 {
2883 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002884 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
2886 if (is_click)
2887 {
2888 /* If the character under the cursor (skipping white space) is
2889 * not a word character, try finding a match and select a (),
2890 * {}, [], #if/#endif, etc. block. */
2891 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002892 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 inc(&end_visual);
2894 if (oap != NULL)
2895 oap->motion_type = MCHAR;
2896 if (oap != NULL
2897 && VIsual_mode == 'v'
2898 && !vim_iswordc(gchar_pos(&end_visual))
2899 && equalpos(curwin->w_cursor, VIsual)
2900 && (pos = findmatch(oap, NUL)) != NULL)
2901 {
2902 curwin->w_cursor = *pos;
2903 if (oap->motion_type == MLINE)
2904 VIsual_mode = 'V';
2905 else if (*p_sel == 'e')
2906 {
2907 if (lt(curwin->w_cursor, VIsual))
2908 ++VIsual.col;
2909 else
2910 ++curwin->w_cursor.col;
2911 }
2912 }
2913 }
2914
2915 if (pos == NULL && (is_click || is_drag))
2916 {
2917 /* When not found a match or when dragging: extend to include
2918 * a word. */
2919 if (lt(curwin->w_cursor, orig_cursor))
2920 {
2921 find_start_of_word(&curwin->w_cursor);
2922 find_end_of_word(&VIsual);
2923 }
2924 else
2925 {
2926 find_start_of_word(&VIsual);
2927 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
2928#ifdef FEAT_MBYTE
2929 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002930 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#else
2932 ++curwin->w_cursor.col;
2933#endif
2934 find_end_of_word(&curwin->w_cursor);
2935 }
2936 }
2937 curwin->w_set_curswant = TRUE;
2938 }
2939 if (is_click)
2940 redraw_curbuf_later(INVERTED); /* update the inversion */
2941 }
2942 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00002943 {
2944 if (mod_mask & MOD_MASK_ALT)
2945 VIsual_mode = Ctrl_V;
2946 else
2947 VIsual_mode = 'v';
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 /* If Visual mode changed show it later. */
Bram Moolenaar09df3122006-01-23 22:23:09 +00002951 if (p_smd && msg_silent == 0
2952 && (VIsual_active != old_active || VIsual_mode != old_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 redraw_cmdline = TRUE;
2954#endif
2955
2956 return moved;
2957}
2958
2959#ifdef FEAT_VISUAL
2960/*
2961 * Move "pos" back to the start of the word it's in.
2962 */
2963 static void
2964find_start_of_word(pos)
2965 pos_T *pos;
2966{
2967 char_u *line;
2968 int cclass;
2969 int col;
2970
2971 line = ml_get(pos->lnum);
2972 cclass = get_mouse_class(line + pos->col);
2973
2974 while (pos->col > 0)
2975 {
2976 col = pos->col - 1;
2977#ifdef FEAT_MBYTE
2978 col -= (*mb_head_off)(line, line + col);
2979#endif
2980 if (get_mouse_class(line + col) != cclass)
2981 break;
2982 pos->col = col;
2983 }
2984}
2985
2986/*
2987 * Move "pos" forward to the end of the word it's in.
2988 * When 'selection' is "exclusive", the position is just after the word.
2989 */
2990 static void
2991find_end_of_word(pos)
2992 pos_T *pos;
2993{
2994 char_u *line;
2995 int cclass;
2996 int col;
2997
2998 line = ml_get(pos->lnum);
2999 if (*p_sel == 'e' && pos->col > 0)
3000 {
3001 --pos->col;
3002#ifdef FEAT_MBYTE
3003 pos->col -= (*mb_head_off)(line, line + pos->col);
3004#endif
3005 }
3006 cclass = get_mouse_class(line + pos->col);
3007 while (line[pos->col] != NUL)
3008 {
3009#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003010 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011#else
3012 col = pos->col + 1;
3013#endif
3014 if (get_mouse_class(line + col) != cclass)
3015 {
3016 if (*p_sel == 'e')
3017 pos->col = col;
3018 break;
3019 }
3020 pos->col = col;
3021 }
3022}
3023
3024/*
3025 * Get class of a character for selection: same class means same word.
3026 * 0: blank
3027 * 1: punctuation groups
3028 * 2: normal word character
3029 * >2: multi-byte word character.
3030 */
3031 static int
3032get_mouse_class(p)
3033 char_u *p;
3034{
3035 int c;
3036
3037#ifdef FEAT_MBYTE
3038 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3039 return mb_get_class(p);
3040#endif
3041
3042 c = *p;
3043 if (c == ' ' || c == '\t')
3044 return 0;
3045
3046 if (vim_iswordc(c))
3047 return 2;
3048
3049 /*
3050 * There are a few special cases where we want certain combinations of
3051 * characters to be considered as a single word. These are things like
3052 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3053 * character is in it's own class.
3054 */
3055 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3056 return 1;
3057 return c;
3058}
3059#endif /* FEAT_VISUAL */
3060#endif /* FEAT_MOUSE */
3061
3062#if defined(FEAT_VISUAL) || defined(PROTO)
3063/*
3064 * Check if highlighting for visual mode is possible, give a warning message
3065 * if not.
3066 */
3067 void
3068check_visual_highlight()
3069{
3070 static int did_check = FALSE;
3071
3072 if (full_screen)
3073 {
3074 if (!did_check && hl_attr(HLF_V) == 0)
3075 MSG(_("Warning: terminal cannot highlight"));
3076 did_check = TRUE;
3077 }
3078}
3079
3080/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003081 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 * This function should ALWAYS be called to end Visual mode, except from
3083 * do_pending_operator().
3084 */
3085 void
3086end_visual_mode()
3087{
3088#ifdef FEAT_CLIPBOARD
3089 /*
3090 * If we are using the clipboard, then remember what was selected in case
3091 * we need to paste it somewhere while we still own the selection.
3092 * Only do this when the clipboard is already owned. Don't want to grab
3093 * the selection when hitting ESC.
3094 */
3095 if (clip_star.available && clip_star.owned)
3096 clip_auto_select();
3097#endif
3098
3099 VIsual_active = FALSE;
3100#ifdef FEAT_MOUSE
3101 setmouse();
3102 mouse_dragging = 0;
3103#endif
3104
3105 /* Save the current VIsual area for '< and '> marks, and "gv" */
3106 curbuf->b_visual_mode = VIsual_mode;
3107#ifdef FEAT_EVAL
3108 curbuf->b_visual_mode_eval = VIsual_mode;
3109#endif
3110 curbuf->b_visual_start = VIsual;
3111 curbuf->b_visual_end = curwin->w_cursor;
3112 curbuf->b_visual_curswant = curwin->w_curswant;
3113#ifdef FEAT_VIRTUALEDIT
3114 if (!virtual_active())
3115 curwin->w_cursor.coladd = 0;
3116#endif
3117
Bram Moolenaar09df3122006-01-23 22:23:09 +00003118 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 clear_cmdline = TRUE; /* unshow visual mode later */
3120#ifdef FEAT_CMDL_INFO
3121 else
3122 clear_showcmd();
3123#endif
3124
3125 /* Don't leave the cursor past the end of the line */
3126 if (curwin->w_cursor.col > 0 && *ml_get_cursor() == NUL)
3127 --curwin->w_cursor.col;
3128}
3129
3130/*
3131 * Reset VIsual_active and VIsual_reselect.
3132 */
3133 void
3134reset_VIsual_and_resel()
3135{
3136 if (VIsual_active)
3137 {
3138 end_visual_mode();
3139 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3140 }
3141 VIsual_reselect = FALSE;
3142}
3143
3144/*
3145 * Reset VIsual_active and VIsual_reselect if it's set.
3146 */
3147 void
3148reset_VIsual()
3149{
3150 if (VIsual_active)
3151 {
3152 end_visual_mode();
3153 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3154 VIsual_reselect = FALSE;
3155 }
3156}
3157#endif /* FEAT_VISUAL */
3158
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003159#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3161
3162/*
3163 * Check for a balloon-eval special item to include when searching for an
3164 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3165 * Returns TRUE if the character at "*ptr" should be included.
3166 * "dir" is FORWARD or BACKWARD, the direction of searching.
3167 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3168 * "bnp" points to a counter for square brackets.
3169 */
3170 static int
3171find_is_eval_item(ptr, colp, bnp, dir)
3172 char_u *ptr;
3173 int *colp;
3174 int *bnp;
3175 int dir;
3176{
3177 /* Accept everything inside []. */
3178 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3179 ++*bnp;
3180 if (*bnp > 0)
3181 {
3182 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3183 --*bnp;
3184 return TRUE;
3185 }
3186
3187 /* skip over "s.var" */
3188 if (*ptr == '.')
3189 return TRUE;
3190
3191 /* two-character item: s->var */
3192 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3193 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3194 {
3195 *colp += dir;
3196 return TRUE;
3197 }
3198 return FALSE;
3199}
3200#endif
3201
3202/*
3203 * Find the identifier under or to the right of the cursor.
3204 * "find_type" can have one of three values:
3205 * FIND_IDENT: find an identifier (keyword)
3206 * FIND_STRING: find any non-white string
3207 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 *
3210 * There are three steps:
3211 * 1. Search forward for the start of an identifier/string. Doesn't move if
3212 * already on one.
3213 * 2. Search backward for the start of this identifier/string.
3214 * This doesn't match the real Vi but I like it a little better and it
3215 * shouldn't bother anyone.
3216 * 3. Search forward to the end of this identifier/string.
3217 * When FIND_IDENT isn't defined, we backup until a blank.
3218 *
3219 * Returns the length of the string, or zero if no string is found.
3220 * If a string is found, a pointer to the string is put in "*string". This
3221 * string is not always NUL terminated.
3222 */
3223 int
3224find_ident_under_cursor(string, find_type)
3225 char_u **string;
3226 int find_type;
3227{
3228 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3229 curwin->w_cursor.col, string, find_type);
3230}
3231
3232/*
3233 * Like find_ident_under_cursor(), but for any window and any position.
3234 * However: Uses 'iskeyword' from the current window!.
3235 */
3236 int
3237find_ident_at_pos(wp, lnum, startcol, string, find_type)
3238 win_T *wp;
3239 linenr_T lnum;
3240 colnr_T startcol;
3241 char_u **string;
3242 int find_type;
3243{
3244 char_u *ptr;
3245 int col = 0; /* init to shut up GCC */
3246 int i;
3247#ifdef FEAT_MBYTE
3248 int this_class = 0;
3249 int prev_class;
3250 int prevcol;
3251#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int bn = 0; /* bracket nesting */
3254#endif
3255
3256 /*
3257 * if i == 0: try to find an identifier
3258 * if i == 1: try to find any non-white string
3259 */
3260 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3261 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3262 {
3263 /*
3264 * 1. skip to start of identifier/string
3265 */
3266 col = startcol;
3267#ifdef FEAT_MBYTE
3268 if (has_mbyte)
3269 {
3270 while (ptr[col] != NUL)
3271 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003272# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /* Stop at a ']' to evaluate "a[x]". */
3274 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3275 break;
3276# endif
3277 this_class = mb_get_class(ptr + col);
3278 if (this_class != 0 && (i == 1 || this_class != 1))
3279 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003280 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 }
3283 else
3284#endif
3285 while (ptr[col] != NUL
3286 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3289# endif
3290 )
3291 ++col;
3292
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /* When starting on a ']' count it, so that we include the '['. */
3295 bn = ptr[col] == ']';
3296#endif
3297
3298 /*
3299 * 2. Back up to start of identifier/string.
3300 */
3301#ifdef FEAT_MBYTE
3302 if (has_mbyte)
3303 {
3304 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3307 this_class = mb_get_class((char_u *)"a");
3308 else
3309# endif
3310 this_class = mb_get_class(ptr + col);
3311 while (col > 0)
3312 {
3313 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3314 prev_class = mb_get_class(ptr + prevcol);
3315 if (this_class != prev_class
3316 && (i == 0
3317 || prev_class == 0
3318 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 && (!(find_type & FIND_EVAL)
3321 || prevcol == 0
3322 || !find_is_eval_item(ptr + prevcol, &prevcol,
3323 &bn, BACKWARD))
3324# endif
3325 )
3326 break;
3327 col = prevcol;
3328 }
3329
3330 /* If we don't want just any old string, or we've found an
3331 * identifier, stop searching. */
3332 if (this_class > 2)
3333 this_class = 2;
3334 if (!(find_type & FIND_STRING) || this_class == 2)
3335 break;
3336 }
3337 else
3338#endif
3339 {
3340 while (col > 0
3341 && ((i == 0
3342 ? vim_iswordc(ptr[col - 1])
3343 : (!vim_iswhite(ptr[col - 1])
3344 && (!(find_type & FIND_IDENT)
3345 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 || ((find_type & FIND_EVAL)
3348 && col > 1
3349 && find_is_eval_item(ptr + col - 1, &col,
3350 &bn, BACKWARD))
3351#endif
3352 ))
3353 --col;
3354
3355 /* If we don't want just any old string, or we've found an
3356 * identifier, stop searching. */
3357 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3358 break;
3359 }
3360 }
3361
3362 if (ptr[col] == NUL || (i == 0 && (
3363#ifdef FEAT_MBYTE
3364 has_mbyte ? this_class != 2 :
3365#endif
3366 !vim_iswordc(ptr[col]))))
3367 {
3368 /*
3369 * didn't find an identifier or string
3370 */
3371 if (find_type & FIND_STRING)
3372 EMSG(_("E348: No string under cursor"));
3373 else
3374 EMSG(_("E349: No identifier under cursor"));
3375 return 0;
3376 }
3377 ptr += col;
3378 *string = ptr;
3379
3380 /*
3381 * 3. Find the end if the identifier/string.
3382 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 bn = 0;
3385 startcol -= col;
3386#endif
3387 col = 0;
3388#ifdef FEAT_MBYTE
3389 if (has_mbyte)
3390 {
3391 /* Search for point of changing multibyte character class. */
3392 this_class = mb_get_class(ptr);
3393 while (ptr[col] != NUL
3394 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3395 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003396# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 || ((find_type & FIND_EVAL)
3398 && col <= (int)startcol
3399 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3400# endif
3401 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003402 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 else
3405#endif
3406 while ((i == 0 ? vim_iswordc(ptr[col])
3407 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003408# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 || ((find_type & FIND_EVAL)
3410 && col <= (int)startcol
3411 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3412# endif
3413 )
3414 {
3415 ++col;
3416 }
3417
3418 return col;
3419}
3420
3421/*
3422 * Prepare for redo of a normal command.
3423 */
3424 static void
3425prep_redo_cmd(cap)
3426 cmdarg_T *cap;
3427{
3428 prep_redo(cap->oap->regname, cap->count0,
3429 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3430}
3431
3432/*
3433 * Prepare for redo of any command.
3434 * Note that only the last argument can be a multi-byte char.
3435 */
3436 static void
3437prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3438 int regname;
3439 long num;
3440 int cmd1;
3441 int cmd2;
3442 int cmd3;
3443 int cmd4;
3444 int cmd5;
3445{
3446 ResetRedobuff();
3447 if (regname != 0) /* yank from specified buffer */
3448 {
3449 AppendCharToRedobuff('"');
3450 AppendCharToRedobuff(regname);
3451 }
3452 if (num)
3453 AppendNumberToRedobuff(num);
3454
3455 if (cmd1 != NUL)
3456 AppendCharToRedobuff(cmd1);
3457 if (cmd2 != NUL)
3458 AppendCharToRedobuff(cmd2);
3459 if (cmd3 != NUL)
3460 AppendCharToRedobuff(cmd3);
3461 if (cmd4 != NUL)
3462 AppendCharToRedobuff(cmd4);
3463 if (cmd5 != NUL)
3464 AppendCharToRedobuff(cmd5);
3465}
3466
3467/*
3468 * check for operator active and clear it
3469 *
3470 * return TRUE if operator was active
3471 */
3472 static int
3473checkclearop(oap)
3474 oparg_T *oap;
3475{
3476 if (oap->op_type == OP_NOP)
3477 return FALSE;
3478 clearopbeep(oap);
3479 return TRUE;
3480}
3481
3482/*
3483 * check for operator or Visual active and clear it
3484 *
3485 * return TRUE if operator was active
3486 */
3487 static int
3488checkclearopq(oap)
3489 oparg_T *oap;
3490{
3491 if (oap->op_type == OP_NOP
3492#ifdef FEAT_VISUAL
3493 && !VIsual_active
3494#endif
3495 )
3496 return FALSE;
3497 clearopbeep(oap);
3498 return TRUE;
3499}
3500
3501 static void
3502clearop(oap)
3503 oparg_T *oap;
3504{
3505 oap->op_type = OP_NOP;
3506 oap->regname = 0;
3507 oap->motion_force = NUL;
3508 oap->use_reg_one = FALSE;
3509}
3510
3511 static void
3512clearopbeep(oap)
3513 oparg_T *oap;
3514{
3515 clearop(oap);
3516 beep_flush();
3517}
3518
3519#ifdef FEAT_VISUAL
3520/*
3521 * Remove the shift modifier from a special key.
3522 */
3523 static void
3524unshift_special(cap)
3525 cmdarg_T *cap;
3526{
3527 switch (cap->cmdchar)
3528 {
3529 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3530 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3531 case K_S_UP: cap->cmdchar = K_UP; break;
3532 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3533 case K_S_HOME: cap->cmdchar = K_HOME; break;
3534 case K_S_END: cap->cmdchar = K_END; break;
3535 }
3536 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3537}
3538#endif
3539
3540#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3541/*
3542 * Routines for displaying a partly typed command
3543 */
3544
3545#ifdef FEAT_VISUAL /* need room for size of Visual area */
3546# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3547#else
3548# define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3549#endif
3550static char_u showcmd_buf[SHOWCMD_BUFLEN];
3551static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3552static int showcmd_is_clear = TRUE;
3553static int showcmd_visual = FALSE;
3554
3555static void display_showcmd __ARGS((void));
3556
3557 void
3558clear_showcmd()
3559{
3560 if (!p_sc)
3561 return;
3562
3563#ifdef FEAT_VISUAL
3564 if (VIsual_active && !char_avail())
3565 {
3566 int i = lt(VIsual, curwin->w_cursor);
3567 long lines;
3568 colnr_T leftcol, rightcol;
3569 linenr_T top, bot;
3570
3571 /* Show the size of the Visual area. */
3572 if (i)
3573 {
3574 top = VIsual.lnum;
3575 bot = curwin->w_cursor.lnum;
3576 }
3577 else
3578 {
3579 top = curwin->w_cursor.lnum;
3580 bot = VIsual.lnum;
3581 }
3582# ifdef FEAT_FOLDING
3583 /* Include closed folds as a whole. */
3584 hasFolding(top, &top, NULL);
3585 hasFolding(bot, NULL, &bot);
3586# endif
3587 lines = bot - top + 1;
3588
3589 if (VIsual_mode == Ctrl_V)
3590 {
3591 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
3592 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3593 (long)(rightcol - leftcol + 1));
3594 }
3595 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3596 sprintf((char *)showcmd_buf, "%ld", lines);
3597 else
3598 sprintf((char *)showcmd_buf, "%ld", (long)(i
3599 ? curwin->w_cursor.col - VIsual.col
3600 : VIsual.col - curwin->w_cursor.col) + (*p_sel != 'e'));
3601 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3602 showcmd_visual = TRUE;
3603 }
3604 else
3605#endif
3606 {
3607 showcmd_buf[0] = NUL;
3608 showcmd_visual = FALSE;
3609
3610 /* Don't actually display something if there is nothing to clear. */
3611 if (showcmd_is_clear)
3612 return;
3613 }
3614
3615 display_showcmd();
3616}
3617
3618/*
3619 * Add 'c' to string of shown command chars.
3620 * Return TRUE if output has been written (and setcursor() has been called).
3621 */
3622 int
3623add_to_showcmd(c)
3624 int c;
3625{
3626 char_u *p;
3627 int old_len;
3628 int extra_len;
3629 int overflow;
3630#if defined(FEAT_MOUSE)
3631 int i;
3632 static int ignore[] =
3633 {
3634#ifdef FEAT_GUI
3635 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3636 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
3637#endif
3638 K_IGNORE,
3639 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3640 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3641 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
3642 K_MOUSEDOWN, K_MOUSEUP,
3643 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003644 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 0
3646 };
3647#endif
3648
Bram Moolenaar09df3122006-01-23 22:23:09 +00003649 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return FALSE;
3651
3652 if (showcmd_visual)
3653 {
3654 showcmd_buf[0] = NUL;
3655 showcmd_visual = FALSE;
3656 }
3657
3658#if defined(FEAT_MOUSE)
3659 /* Ignore keys that are scrollbar updates and mouse clicks */
3660 if (IS_SPECIAL(c))
3661 for (i = 0; ignore[i] != 0; ++i)
3662 if (ignore[i] == c)
3663 return FALSE;
3664#endif
3665
3666 p = transchar(c);
3667 old_len = (int)STRLEN(showcmd_buf);
3668 extra_len = (int)STRLEN(p);
3669 overflow = old_len + extra_len - SHOWCMD_COLS;
3670 if (overflow > 0)
3671 STRCPY(showcmd_buf, showcmd_buf + overflow);
3672 STRCAT(showcmd_buf, p);
3673
3674 if (char_avail())
3675 return FALSE;
3676
3677 display_showcmd();
3678
3679 return TRUE;
3680}
3681
3682 void
3683add_to_showcmd_c(c)
3684 int c;
3685{
3686 if (!add_to_showcmd(c))
3687 setcursor();
3688}
3689
3690/*
3691 * Delete 'len' characters from the end of the shown command.
3692 */
3693 static void
3694del_from_showcmd(len)
3695 int len;
3696{
3697 int old_len;
3698
3699 if (!p_sc)
3700 return;
3701
3702 old_len = (int)STRLEN(showcmd_buf);
3703 if (len > old_len)
3704 len = old_len;
3705 showcmd_buf[old_len - len] = NUL;
3706
3707 if (!char_avail())
3708 display_showcmd();
3709}
3710
3711/*
3712 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3713 * something and there is a partial mapping.
3714 */
3715 void
3716push_showcmd()
3717{
3718 if (p_sc)
3719 STRCPY(old_showcmd_buf, showcmd_buf);
3720}
3721
3722 void
3723pop_showcmd()
3724{
3725 if (!p_sc)
3726 return;
3727
3728 STRCPY(showcmd_buf, old_showcmd_buf);
3729
3730 display_showcmd();
3731}
3732
3733 static void
3734display_showcmd()
3735{
3736 int len;
3737
3738 cursor_off();
3739
3740 len = (int)STRLEN(showcmd_buf);
3741 if (len == 0)
3742 showcmd_is_clear = TRUE;
3743 else
3744 {
3745 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3746 showcmd_is_clear = FALSE;
3747 }
3748
3749 /*
3750 * clear the rest of an old message by outputing up to SHOWCMD_COLS spaces
3751 */
3752 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3753
3754 setcursor(); /* put cursor back where it belongs */
3755}
3756#endif
3757
3758#ifdef FEAT_SCROLLBIND
3759/*
3760 * When "check" is FALSE, prepare for commands that scroll the window.
3761 * When "check" is TRUE, take care of scroll-binding after the window has
3762 * scrolled. Called from normal_cmd() and edit().
3763 */
3764 void
3765do_check_scrollbind(check)
3766 int check;
3767{
3768 static win_T *old_curwin = NULL;
3769 static linenr_T old_topline = 0;
3770#ifdef FEAT_DIFF
3771 static int old_topfill = 0;
3772#endif
3773 static buf_T *old_buf = NULL;
3774 static colnr_T old_leftcol = 0;
3775
3776 if (check && curwin->w_p_scb)
3777 {
3778 /* If a ":syncbind" command was just used, don't scroll, only reset
3779 * the values. */
3780 if (did_syncbind)
3781 did_syncbind = FALSE;
3782 else if (curwin == old_curwin)
3783 {
3784 /*
3785 * Synchronize other windows, as necessary according to
3786 * 'scrollbind'. Don't do this after an ":edit" command, except
3787 * when 'diff' is set.
3788 */
3789 if ((curwin->w_buffer == old_buf
3790#ifdef FEAT_DIFF
3791 || curwin->w_p_diff
3792#endif
3793 )
3794 && (curwin->w_topline != old_topline
3795#ifdef FEAT_DIFF
3796 || curwin->w_topfill != old_topfill
3797#endif
3798 || curwin->w_leftcol != old_leftcol))
3799 {
3800 check_scrollbind(curwin->w_topline - old_topline,
3801 (long)(curwin->w_leftcol - old_leftcol));
3802 }
3803 }
3804 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3805 {
3806 /*
3807 * When switching between windows, make sure that the relative
3808 * vertical offset is valid for the new window. The relative
3809 * offset is invalid whenever another 'scrollbind' window has
3810 * scrolled to a point that would force the current window to
3811 * scroll past the beginning or end of its buffer. When the
3812 * resync is performed, some of the other 'scrollbind' windows may
3813 * need to jump so that the current window's relative position is
3814 * visible on-screen.
3815 */
3816 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3817 }
3818 curwin->w_scbind_pos = curwin->w_topline;
3819 }
3820
3821 old_curwin = curwin;
3822 old_topline = curwin->w_topline;
3823#ifdef FEAT_DIFF
3824 old_topfill = curwin->w_topfill;
3825#endif
3826 old_buf = curwin->w_buffer;
3827 old_leftcol = curwin->w_leftcol;
3828}
3829
3830/*
3831 * Synchronize any windows that have "scrollbind" set, based on the
3832 * number of rows by which the current window has changed
3833 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3834 */
3835 void
3836check_scrollbind(topline_diff, leftcol_diff)
3837 linenr_T topline_diff;
3838 long leftcol_diff;
3839{
3840 int want_ver;
3841 int want_hor;
3842 win_T *old_curwin = curwin;
3843 buf_T *old_curbuf = curbuf;
3844#ifdef FEAT_VISUAL
3845 int old_VIsual_select = VIsual_select;
3846 int old_VIsual_active = VIsual_active;
3847#endif
3848 colnr_T tgt_leftcol = curwin->w_leftcol;
3849 long topline;
3850 long y;
3851
3852 /*
3853 * check 'scrollopt' string for vertical and horizontal scroll options
3854 */
3855 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3856#ifdef FEAT_DIFF
3857 want_ver |= old_curwin->w_p_diff;
3858#endif
3859 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3860
3861 /*
3862 * loop through the scrollbound windows and scroll accordingly
3863 */
3864#ifdef FEAT_VISUAL
3865 VIsual_select = VIsual_active = 0;
3866#endif
3867 for (curwin = firstwin; curwin; curwin = curwin->w_next)
3868 {
3869 curbuf = curwin->w_buffer;
3870 /* skip original window and windows with 'noscrollbind' */
3871 if (curwin != old_curwin && curwin->w_p_scb)
3872 {
3873 /*
3874 * do the vertical scroll
3875 */
3876 if (want_ver)
3877 {
3878#ifdef FEAT_DIFF
3879 if (old_curwin->w_p_diff && curwin->w_p_diff)
3880 {
3881 diff_set_topline(old_curwin, curwin);
3882 }
3883 else
3884#endif
3885 {
3886 curwin->w_scbind_pos += topline_diff;
3887 topline = curwin->w_scbind_pos;
3888 if (topline > curbuf->b_ml.ml_line_count)
3889 topline = curbuf->b_ml.ml_line_count;
3890 if (topline < 1)
3891 topline = 1;
3892
3893 y = topline - curwin->w_topline;
3894 if (y > 0)
3895 scrollup(y, FALSE);
3896 else
3897 scrolldown(-y, FALSE);
3898 }
3899
3900 redraw_later(VALID);
3901 cursor_correct();
3902#ifdef FEAT_WINDOWS
3903 curwin->w_redr_status = TRUE;
3904#endif
3905 }
3906
3907 /*
3908 * do the horizontal scroll
3909 */
3910 if (want_hor && curwin->w_leftcol != tgt_leftcol)
3911 {
3912 curwin->w_leftcol = tgt_leftcol;
3913 leftcol_changed();
3914 }
3915 }
3916 }
3917
3918 /*
3919 * reset current-window
3920 */
3921#ifdef FEAT_VISUAL
3922 VIsual_select = old_VIsual_select;
3923 VIsual_active = old_VIsual_active;
3924#endif
3925 curwin = old_curwin;
3926 curbuf = old_curbuf;
3927}
3928#endif /* #ifdef FEAT_SCROLLBIND */
3929
3930/*
3931 * Command character that's ignored.
3932 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
3933 * xon/xoff
3934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 static void
3936nv_ignore(cap)
3937 cmdarg_T *cap;
3938{
Bram Moolenaarfc735152005-03-22 22:54:12 +00003939 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940}
3941
3942/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00003943 * Command character that doesn't do anything, but unlike nv_ignore() does
3944 * start edit(). Used for "startinsert" executed while starting up.
3945 */
3946/*ARGSUSED */
3947 static void
3948nv_nop(cap)
3949 cmdarg_T *cap;
3950{
3951}
3952
3953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * Command character doesn't exist.
3955 */
3956 static void
3957nv_error(cap)
3958 cmdarg_T *cap;
3959{
3960 clearopbeep(cap->oap);
3961}
3962
3963/*
3964 * <Help> and <F1> commands.
3965 */
3966 static void
3967nv_help(cap)
3968 cmdarg_T *cap;
3969{
3970 if (!checkclearopq(cap->oap))
3971 ex_help(NULL);
3972}
3973
3974/*
3975 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
3976 */
3977 static void
3978nv_addsub(cap)
3979 cmdarg_T *cap;
3980{
3981 if (!checkclearopq(cap->oap)
3982 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
3983 prep_redo_cmd(cap);
3984}
3985
3986/*
3987 * CTRL-F, CTRL-B, etc: Scroll page up or down.
3988 */
3989 static void
3990nv_page(cap)
3991 cmdarg_T *cap;
3992{
3993 if (!checkclearop(cap->oap))
3994 (void)onepage(cap->arg, cap->count1);
3995}
3996
3997/*
3998 * Implementation of "gd" and "gD" command.
3999 */
4000 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004001nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004002 oparg_T *oap;
4003 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004004 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 char_u *ptr;
4008
Bram Moolenaard9d30582005-05-18 22:10:28 +00004009 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004010 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004012#ifdef FEAT_FOLDING
4013 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4014 foldOpenCursor();
4015#endif
4016}
4017
4018/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004019 * Search for variable declaration of "ptr[len]".
4020 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4021 * current file ("gD").
4022 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004023 * Return FAIL when not found.
4024 */
4025 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004026find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004027 char_u *ptr;
4028 int len;
4029 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004030 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004031 int searchflags; /* flags passed to searchit() */
4032{
4033 char_u *pat;
4034 pos_T old_pos;
4035 pos_T par_pos;
4036 pos_T found_pos;
4037 int t;
4038 int save_p_ws;
4039 int save_p_scs;
4040 int retval = OK;
4041 int incl;
4042
4043 if ((pat = alloc(len + 7)) == NULL)
4044 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004045
4046 /* Put "\V" before the pattern to avoid that the special meaning of "."
4047 * and "~" causes trouble. */
4048 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4049 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 old_pos = curwin->w_cursor;
4051 save_p_ws = p_ws;
4052 save_p_scs = p_scs;
4053 p_ws = FALSE; /* don't wrap around end of file now */
4054 p_scs = FALSE; /* don't switch ignorecase off now */
4055
4056 /*
4057 * With "gD" go to line 1.
4058 * With "gd" Search back for the start of the current function, then go
4059 * back until a blank line. If this fails go to line 1.
4060 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004061 if (!locally || !findpar(&incl, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
4063 setpcmark(); /* Set in findpar() otherwise */
4064 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004065 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
4068 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004069 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4071 --curwin->w_cursor.lnum;
4072 }
4073 curwin->w_cursor.col = 0;
4074
4075 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004076 found_pos.lnum = 0;
4077 for (;;)
4078 {
4079 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
4080 pat, 1L, searchflags, RE_LAST);
4081 if (curwin->w_cursor.lnum >= old_pos.lnum)
4082 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004083
4084 if (thisblock)
4085 {
4086 pos_T *pos;
4087
4088 /* Check that the block the match is in doesn't end before the
4089 * position where we started the search from. */
4090 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4091 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4092 && pos->lnum < old_pos.lnum)
4093 continue;
4094 }
4095
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004096 if (t == FAIL)
4097 {
4098 /* If we previously found a valid position, use it. */
4099 if (found_pos.lnum != 0)
4100 {
4101 curwin->w_cursor = found_pos;
4102 t = OK;
4103 }
4104 break;
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_COMMENTS
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004107 if (get_leader_len(ml_get_curline(), NULL, FALSE) > 0)
4108 {
4109 /* Ignore this line, continue at start of next line. */
4110 ++curwin->w_cursor.lnum;
4111 curwin->w_cursor.col = 0;
4112 continue;
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004115 if (!locally) /* global search: use first match found */
4116 break;
4117 if (curwin->w_cursor.lnum >= par_pos.lnum)
4118 {
4119 /* If we previously found a valid position, use it. */
4120 if (found_pos.lnum != 0)
4121 curwin->w_cursor = found_pos;
4122 break;
4123 }
4124
4125 /* For finding a local variable and the match is before the "{" search
4126 * to find a later match. For K&R style function declarations this
4127 * skips the function header without types. */
4128 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004130
4131 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004133 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 curwin->w_cursor = old_pos;
4135 }
4136 else
4137 {
4138 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /* "n" searches forward now */
4140 reset_search_dir();
4141 }
4142
4143 vim_free(pat);
4144 p_ws = save_p_ws;
4145 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004146
4147 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
4150/*
4151 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4152 * lines rather than lines in the file.
4153 * 'dist' must be positive.
4154 *
4155 * Return OK if able to move cursor, FAIL otherwise.
4156 */
4157 static int
4158nv_screengo(oap, dir, dist)
4159 oparg_T *oap;
4160 int dir;
4161 long dist;
4162{
4163 int linelen = linetabsize(ml_get_curline());
4164 int retval = OK;
4165 int atend = FALSE;
4166 int n;
4167 int col_off1; /* margin offset for first screen line */
4168 int col_off2; /* margin offset for wrapped screen line */
4169 int width1; /* text width for first screen line */
4170 int width2; /* test width for wrapped screen line */
4171
4172 oap->motion_type = MCHAR;
4173 oap->inclusive = FALSE;
4174
4175 col_off1 = curwin_col_off();
4176 col_off2 = col_off1 - curwin_col_off2();
4177 width1 = W_WIDTH(curwin) - col_off1;
4178 width2 = W_WIDTH(curwin) - col_off2;
4179
4180#ifdef FEAT_VERTSPLIT
4181 if (curwin->w_width != 0)
4182 {
4183#endif
4184 /*
4185 * Instead of sticking at the last character of the buffer line we
4186 * try to stick in the last column of the screen.
4187 */
4188 if (curwin->w_curswant == MAXCOL)
4189 {
4190 atend = TRUE;
4191 validate_virtcol();
4192 if (width1 <= 0)
4193 curwin->w_curswant = 0;
4194 else
4195 {
4196 curwin->w_curswant = width1 - 1;
4197 if (curwin->w_virtcol > curwin->w_curswant)
4198 curwin->w_curswant += ((curwin->w_virtcol
4199 - curwin->w_curswant - 1) / width2 + 1) * width2;
4200 }
4201 }
4202 else
4203 {
4204 if (linelen > width1)
4205 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4206 else
4207 n = width1;
4208 if (curwin->w_curswant > (colnr_T)n + 1)
4209 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4210 * width2;
4211 }
4212
4213 while (dist--)
4214 {
4215 if (dir == BACKWARD)
4216 {
4217 if ((long)curwin->w_curswant >= width2)
4218 /* move back within line */
4219 curwin->w_curswant -= width2;
4220 else
4221 {
4222 /* to previous line */
4223 if (curwin->w_cursor.lnum == 1)
4224 {
4225 retval = FAIL;
4226 break;
4227 }
4228 --curwin->w_cursor.lnum;
4229#ifdef FEAT_FOLDING
4230 /* Move to the start of a closed fold. Don't do that when
4231 * 'foldopen' contains "all": it will open in a moment. */
4232 if (!(fdo_flags & FDO_ALL))
4233 (void)hasFolding(curwin->w_cursor.lnum,
4234 &curwin->w_cursor.lnum, NULL);
4235#endif
4236 linelen = linetabsize(ml_get_curline());
4237 if (linelen > width1)
4238 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4239 + 1) * width2;
4240 }
4241 }
4242 else /* dir == FORWARD */
4243 {
4244 if (linelen > width1)
4245 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4246 else
4247 n = width1;
4248 if (curwin->w_curswant + width2 < (colnr_T)n)
4249 /* move forward within line */
4250 curwin->w_curswant += width2;
4251 else
4252 {
4253 /* to next line */
4254#ifdef FEAT_FOLDING
4255 /* Move to the end of a closed fold. */
4256 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4257 &curwin->w_cursor.lnum);
4258#endif
4259 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4260 {
4261 retval = FAIL;
4262 break;
4263 }
4264 curwin->w_cursor.lnum++;
4265 curwin->w_curswant %= width2;
4266 }
4267 }
4268 }
4269#ifdef FEAT_VERTSPLIT
4270 }
4271#endif
4272
4273 coladvance(curwin->w_curswant);
4274
4275#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4276 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4277 {
4278 /*
4279 * Check for landing on a character that got split at the end of the
4280 * last line. We want to advance a screenline, not end up in the same
4281 * screenline or move two screenlines.
4282 */
4283 validate_virtcol();
4284 if (curwin->w_virtcol > curwin->w_curswant
4285 && (curwin->w_curswant < (colnr_T)width1
4286 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4287 : ((curwin->w_curswant - width1) % width2
4288 > (colnr_T)width2 / 2)))
4289 --curwin->w_cursor.col;
4290 }
4291#endif
4292
4293 if (atend)
4294 curwin->w_curswant = MAXCOL; /* stick in the last column */
4295
4296 return retval;
4297}
4298
4299#ifdef FEAT_MOUSE
4300/*
4301 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4302 * when Shift or Ctrl is used.
4303 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4304 */
4305 static void
4306nv_mousescroll(cap)
4307 cmdarg_T *cap;
4308{
4309# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaara5792f52005-11-23 21:25:05 +00004310 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 /* Currently we only get the mouse coordinates in the GUI. */
4313 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
4314 {
4315 int row, col;
4316
4317 row = mouse_row;
4318 col = mouse_col;
4319
4320 /* find the window at the pointer coordinates */
4321 curwin = mouse_find_win(&row, &col);
4322 curbuf = curwin->w_buffer;
4323 }
4324# endif
4325
4326 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4327 {
4328 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4329 }
4330 else
4331 {
4332 cap->count1 = 3;
4333 cap->count0 = 3;
4334 nv_scroll_line(cap);
4335 }
4336
4337# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4338 curwin->w_redr_status = TRUE;
4339
4340 curwin = old_curwin;
4341 curbuf = curwin->w_buffer;
4342# endif
4343}
4344
4345/*
4346 * Mouse clicks and drags.
4347 */
4348 static void
4349nv_mouse(cap)
4350 cmdarg_T *cap;
4351{
4352 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4353}
4354#endif
4355
4356/*
4357 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4358 * cap->arg must be TRUE for CTRL-E.
4359 */
4360 static void
4361nv_scroll_line(cap)
4362 cmdarg_T *cap;
4363{
4364 if (!checkclearop(cap->oap))
4365 scroll_redraw(cap->arg, cap->count1);
4366}
4367
4368/*
4369 * Scroll "count" lines up or down, and redraw.
4370 */
4371 void
4372scroll_redraw(up, count)
4373 int up;
4374 long count;
4375{
4376 linenr_T prev_topline = curwin->w_topline;
4377#ifdef FEAT_DIFF
4378 int prev_topfill = curwin->w_topfill;
4379#endif
4380 linenr_T prev_lnum = curwin->w_cursor.lnum;
4381
4382 if (up)
4383 scrollup(count, TRUE);
4384 else
4385 scrolldown(count, TRUE);
4386 if (p_so)
4387 {
4388 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4389 * valid, otherwise the screen jumps back at the end of the file. */
4390 cursor_correct();
4391 check_cursor_moved(curwin);
4392 curwin->w_valid |= VALID_TOPLINE;
4393
4394 /* If moved back to where we were, at least move the cursor, otherwise
4395 * we get stuck at one position. Don't move the cursor up if the
4396 * first line of the buffer is already on the screen */
4397 while (curwin->w_topline == prev_topline
4398#ifdef FEAT_DIFF
4399 && curwin->w_topfill == prev_topfill
4400#endif
4401 )
4402 {
4403 if (up)
4404 {
4405 if (curwin->w_cursor.lnum > prev_lnum
4406 || cursor_down(1L, FALSE) == FAIL)
4407 break;
4408 }
4409 else
4410 {
4411 if (curwin->w_cursor.lnum < prev_lnum
4412 || prev_topline == 1L
4413 || cursor_up(1L, FALSE) == FAIL)
4414 break;
4415 }
4416 /* Mark w_topline as valid, otherwise the screen jumps back at the
4417 * end of the file. */
4418 check_cursor_moved(curwin);
4419 curwin->w_valid |= VALID_TOPLINE;
4420 }
4421 }
4422 if (curwin->w_cursor.lnum != prev_lnum)
4423 coladvance(curwin->w_curswant);
4424 redraw_later(VALID);
4425}
4426
4427/*
4428 * Commands that start with "z".
4429 */
4430 static void
4431nv_zet(cap)
4432 cmdarg_T *cap;
4433{
4434 long n;
4435 colnr_T col;
4436 int nchar = cap->nchar;
4437#ifdef FEAT_FOLDING
4438 long old_fdl = curwin->w_p_fdl;
4439 int old_fen = curwin->w_p_fen;
4440#endif
4441
4442 if (VIM_ISDIGIT(nchar))
4443 {
4444 /*
4445 * "z123{nchar}": edit the count before obtaining {nchar}
4446 */
4447 if (checkclearop(cap->oap))
4448 return;
4449 n = nchar - '0';
4450 for (;;)
4451 {
4452#ifdef USE_ON_FLY_SCROLL
4453 dont_scroll = TRUE; /* disallow scrolling here */
4454#endif
4455 ++no_mapping;
4456 ++allow_keys; /* no mapping for nchar, but allow key codes */
4457 nchar = safe_vgetc();
4458#ifdef FEAT_LANGMAP
4459 LANGMAP_ADJUST(nchar, TRUE);
4460#endif
4461 --no_mapping;
4462 --allow_keys;
4463#ifdef FEAT_CMDL_INFO
4464 (void)add_to_showcmd(nchar);
4465#endif
4466 if (nchar == K_DEL || nchar == K_KDEL)
4467 n /= 10;
4468 else if (VIM_ISDIGIT(nchar))
4469 n = n * 10 + (nchar - '0');
4470 else if (nchar == CAR)
4471 {
4472#ifdef FEAT_GUI
4473 need_mouse_correct = TRUE;
4474#endif
4475 win_setheight((int)n);
4476 break;
4477 }
4478 else if (nchar == 'l'
4479 || nchar == 'h'
4480 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004481 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
4483 cap->count1 = n ? n * cap->count1 : cap->count1;
4484 goto dozet;
4485 }
4486 else
4487 {
4488 clearopbeep(cap->oap);
4489 break;
4490 }
4491 }
4492 cap->oap->op_type = OP_NOP;
4493 return;
4494 }
4495
4496dozet:
4497 if (
4498#ifdef FEAT_FOLDING
4499 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4500 * and "zC" only in Visual mode. "zj" and "zk" are motion
4501 * commands. */
4502 cap->nchar != 'f' && cap->nchar != 'F'
4503 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4504 && cap->nchar != 'j' && cap->nchar != 'k'
4505 &&
4506#endif
4507 checkclearop(cap->oap))
4508 return;
4509
4510 /*
4511 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4512 * If line number given, set cursor.
4513 */
4514 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4515 && cap->count0
4516 && cap->count0 != curwin->w_cursor.lnum)
4517 {
4518 setpcmark();
4519 if (cap->count0 > curbuf->b_ml.ml_line_count)
4520 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4521 else
4522 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004523 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525
4526 switch (nchar)
4527 {
4528 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4529 case '+':
4530 if (cap->count0 == 0)
4531 {
4532 /* No count given: put cursor at the line below screen */
4533 validate_botline(); /* make sure w_botline is valid */
4534 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4535 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4536 else
4537 curwin->w_cursor.lnum = curwin->w_botline;
4538 }
4539 /* FALLTHROUGH */
4540 case NL:
4541 case CAR:
4542 case K_KENTER:
4543 beginline(BL_WHITE | BL_FIX);
4544 /* FALLTHROUGH */
4545
4546 case 't': scroll_cursor_top(0, TRUE);
4547 redraw_later(VALID);
4548 break;
4549
4550 /* "z." and "zz": put cursor in middle of screen */
4551 case '.': beginline(BL_WHITE | BL_FIX);
4552 /* FALLTHROUGH */
4553
4554 case 'z': scroll_cursor_halfway(TRUE);
4555 redraw_later(VALID);
4556 break;
4557
4558 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4559 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4560 * when <count> is at bottom of window, and puts that one at
4561 * bottom of window. */
4562 if (cap->count0 != 0)
4563 {
4564 scroll_cursor_bot(0, TRUE);
4565 curwin->w_cursor.lnum = curwin->w_topline;
4566 }
4567 else if (curwin->w_topline == 1)
4568 curwin->w_cursor.lnum = 1;
4569 else
4570 curwin->w_cursor.lnum = curwin->w_topline - 1;
4571 /* FALLTHROUGH */
4572 case '-':
4573 beginline(BL_WHITE | BL_FIX);
4574 /* FALLTHROUGH */
4575
4576 case 'b': scroll_cursor_bot(0, TRUE);
4577 redraw_later(VALID);
4578 break;
4579
4580 /* "zH" - scroll screen right half-page */
4581 case 'H':
4582 cap->count1 *= W_WIDTH(curwin) / 2;
4583 /* FALLTHROUGH */
4584
4585 /* "zh" - scroll screen to the right */
4586 case 'h':
4587 case K_LEFT:
4588 if (!curwin->w_p_wrap)
4589 {
4590 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4591 curwin->w_leftcol = 0;
4592 else
4593 curwin->w_leftcol -= (colnr_T)cap->count1;
4594 leftcol_changed();
4595 }
4596 break;
4597
4598 /* "zL" - scroll screen left half-page */
4599 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4600 /* FALLTHROUGH */
4601
4602 /* "zl" - scroll screen to the left */
4603 case 'l':
4604 case K_RIGHT:
4605 if (!curwin->w_p_wrap)
4606 {
4607 /* scroll the window left */
4608 curwin->w_leftcol += (colnr_T)cap->count1;
4609 leftcol_changed();
4610 }
4611 break;
4612
4613 /* "zs" - scroll screen, cursor at the start */
4614 case 's': if (!curwin->w_p_wrap)
4615 {
4616#ifdef FEAT_FOLDING
4617 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4618 col = 0; /* like the cursor is in col 0 */
4619 else
4620#endif
4621 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4622 if ((long)col > p_siso)
4623 col -= p_siso;
4624 else
4625 col = 0;
4626 if (curwin->w_leftcol != col)
4627 {
4628 curwin->w_leftcol = col;
4629 redraw_later(NOT_VALID);
4630 }
4631 }
4632 break;
4633
4634 /* "ze" - scroll screen, cursor at the end */
4635 case 'e': if (!curwin->w_p_wrap)
4636 {
4637#ifdef FEAT_FOLDING
4638 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4639 col = 0; /* like the cursor is in col 0 */
4640 else
4641#endif
4642 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4643 n = W_WIDTH(curwin) - curwin_col_off();
4644 if ((long)col + p_siso < n)
4645 col = 0;
4646 else
4647 col = col + p_siso - n + 1;
4648 if (curwin->w_leftcol != col)
4649 {
4650 curwin->w_leftcol = col;
4651 redraw_later(NOT_VALID);
4652 }
4653 }
4654 break;
4655
4656#ifdef FEAT_FOLDING
4657 /* "zF": create fold command */
4658 /* "zf": create fold operator */
4659 case 'F':
4660 case 'f': if (foldManualAllowed(TRUE))
4661 {
4662 cap->nchar = 'f';
4663 nv_operator(cap);
4664 curwin->w_p_fen = TRUE;
4665
4666 /* "zF" is like "zfzf" */
4667 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4668 {
4669 nv_operator(cap);
4670 finish_op = TRUE;
4671 }
4672 }
4673 else
4674 clearopbeep(cap->oap);
4675 break;
4676
4677 /* "zd": delete fold at cursor */
4678 /* "zD": delete fold at cursor recursively */
4679 case 'd':
4680 case 'D': if (foldManualAllowed(FALSE))
4681 {
4682 if (VIsual_active)
4683 nv_operator(cap);
4684 else
4685 deleteFold(curwin->w_cursor.lnum,
4686 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4687 }
4688 break;
4689
4690 /* "zE": erease all folds */
4691 case 'E': if (foldmethodIsManual(curwin))
4692 {
4693 clearFolding(curwin);
4694 changed_window_setting();
4695 }
4696 else if (foldmethodIsMarker(curwin))
4697 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4698 TRUE, FALSE);
4699 else
4700 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4701 break;
4702
4703 /* "zn": fold none: reset 'foldenable' */
4704 case 'n': curwin->w_p_fen = FALSE;
4705 break;
4706
4707 /* "zN": fold Normal: set 'foldenable' */
4708 case 'N': curwin->w_p_fen = TRUE;
4709 break;
4710
4711 /* "zi": invert folding: toggle 'foldenable' */
4712 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4713 break;
4714
4715 /* "za": open closed fold or close open fold at cursor */
4716 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4717 openFold(curwin->w_cursor.lnum, cap->count1);
4718 else
4719 {
4720 closeFold(curwin->w_cursor.lnum, cap->count1);
4721 curwin->w_p_fen = TRUE;
4722 }
4723 break;
4724
4725 /* "zA": open fold at cursor recursively */
4726 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4727 openFoldRecurse(curwin->w_cursor.lnum);
4728 else
4729 {
4730 closeFoldRecurse(curwin->w_cursor.lnum);
4731 curwin->w_p_fen = TRUE;
4732 }
4733 break;
4734
4735 /* "zo": open fold at cursor or Visual area */
4736 case 'o': if (VIsual_active)
4737 nv_operator(cap);
4738 else
4739 openFold(curwin->w_cursor.lnum, cap->count1);
4740 break;
4741
4742 /* "zO": open fold recursively */
4743 case 'O': if (VIsual_active)
4744 nv_operator(cap);
4745 else
4746 openFoldRecurse(curwin->w_cursor.lnum);
4747 break;
4748
4749 /* "zc": close fold at cursor or Visual area */
4750 case 'c': if (VIsual_active)
4751 nv_operator(cap);
4752 else
4753 closeFold(curwin->w_cursor.lnum, cap->count1);
4754 curwin->w_p_fen = TRUE;
4755 break;
4756
4757 /* "zC": close fold recursively */
4758 case 'C': if (VIsual_active)
4759 nv_operator(cap);
4760 else
4761 closeFoldRecurse(curwin->w_cursor.lnum);
4762 curwin->w_p_fen = TRUE;
4763 break;
4764
4765 /* "zv": open folds at the cursor */
4766 case 'v': foldOpenCursor();
4767 break;
4768
4769 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4770 case 'x': curwin->w_p_fen = TRUE;
4771 newFoldLevel(); /* update right now */
4772 foldOpenCursor();
4773 break;
4774
4775 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4776 case 'X': curwin->w_p_fen = TRUE;
4777 old_fdl = -1; /* force an update */
4778 break;
4779
4780 /* "zm": fold more */
4781 case 'm': if (curwin->w_p_fdl > 0)
4782 --curwin->w_p_fdl;
4783 old_fdl = -1; /* force an update */
4784 curwin->w_p_fen = TRUE;
4785 break;
4786
4787 /* "zM": close all folds */
4788 case 'M': curwin->w_p_fdl = 0;
4789 old_fdl = -1; /* force an update */
4790 curwin->w_p_fen = TRUE;
4791 break;
4792
4793 /* "zr": reduce folding */
4794 case 'r': ++curwin->w_p_fdl;
4795 break;
4796
4797 /* "zR": open all folds */
4798 case 'R': curwin->w_p_fdl = getDeepestNesting();
4799 old_fdl = -1; /* force an update */
4800 break;
4801
4802 case 'j': /* "zj" move to next fold downwards */
4803 case 'k': /* "zk" move to next fold upwards */
4804 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
4805 cap->count1) == FAIL)
4806 clearopbeep(cap->oap);
4807 break;
4808
4809#endif /* FEAT_FOLDING */
4810
Bram Moolenaarb765d632005-06-07 21:00:02 +00004811#ifdef FEAT_SYN_HL
4812 case 'g': /* "zg": add good word to word list */
4813 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00004814 case 'G': /* "zG": add good word to temp word list */
4815 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004816 {
4817 char_u *ptr = NULL;
4818 int len;
4819
4820 if (checkclearop(cap->oap))
4821 break;
4822# ifdef FEAT_VISUAL
4823 if (VIsual_active && get_visual_text(cap, &ptr, &len)
4824 == FAIL)
4825 return;
4826# endif
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004827 if (ptr == NULL)
4828 {
4829 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004830
4831 /* Find bad word under the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004832 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004833 if (len != 0 && curwin->w_cursor.col <= pos.col)
4834 ptr = ml_get_pos(&curwin->w_cursor);
4835 curwin->w_cursor = pos;
4836 }
4837
Bram Moolenaarb765d632005-06-07 21:00:02 +00004838 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4839 FIND_IDENT)) == 0)
4840 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004841 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004842 (nchar == 'G' || nchar == 'W') ? 0
4843 : (int)cap->count1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004844 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004845 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846
Bram Moolenaar43abc522005-12-10 20:15:02 +00004847 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00004848 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004849 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004850 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004851#endif
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 default: clearopbeep(cap->oap);
4854 }
4855
4856#ifdef FEAT_FOLDING
4857 /* Redraw when 'foldenable' changed */
4858 if (old_fen != curwin->w_p_fen)
4859 {
4860# ifdef FEAT_DIFF
4861 win_T *wp;
4862
4863 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4864 {
4865 /* Adjust 'foldenable' in diff-synced windows. */
4866 FOR_ALL_WINDOWS(wp)
4867 {
4868 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4869 {
4870 wp->w_p_fen = curwin->w_p_fen;
4871 changed_window_setting_win(wp);
4872 }
4873 }
4874 }
4875# endif
4876 changed_window_setting();
4877 }
4878
4879 /* Redraw when 'foldlevel' changed. */
4880 if (old_fdl != curwin->w_p_fdl)
4881 newFoldLevel();
4882#endif
4883}
4884
4885#ifdef FEAT_GUI
4886/*
4887 * Vertical scrollbar movement.
4888 */
4889 static void
4890nv_ver_scrollbar(cap)
4891 cmdarg_T *cap;
4892{
4893 if (cap->oap->op_type != OP_NOP)
4894 clearopbeep(cap->oap);
4895
4896 /* Even if an operator was pending, we still want to scroll */
4897 gui_do_scroll();
4898}
4899
4900/*
4901 * Horizontal scrollbar movement.
4902 */
4903 static void
4904nv_hor_scrollbar(cap)
4905 cmdarg_T *cap;
4906{
4907 if (cap->oap->op_type != OP_NOP)
4908 clearopbeep(cap->oap);
4909
4910 /* Even if an operator was pending, we still want to scroll */
4911 gui_do_horiz_scroll();
4912}
4913#endif
4914
4915/*
4916 * "Q" command.
4917 */
4918 static void
4919nv_exmode(cap)
4920 cmdarg_T *cap;
4921{
4922 /*
4923 * Ignore 'Q' in Visual mode, just give a beep.
4924 */
4925#ifdef FEAT_VISUAL
4926 if (VIsual_active)
4927 vim_beep();
4928 else
4929#endif
4930 if (!checkclearop(cap->oap))
4931 do_exmode(FALSE);
4932}
4933
4934/*
4935 * Handle a ":" command.
4936 */
4937 static void
4938nv_colon(cap)
4939 cmdarg_T *cap;
4940{
4941 int old_p_im;
4942
4943#ifdef FEAT_VISUAL
4944 if (VIsual_active)
4945 nv_operator(cap);
4946 else
4947#endif
4948 {
4949 if (cap->oap->op_type != OP_NOP)
4950 {
4951 /* Using ":" as a movement is characterwise exclusive. */
4952 cap->oap->motion_type = MCHAR;
4953 cap->oap->inclusive = FALSE;
4954 }
4955 else if (cap->count0)
4956 {
4957 /* translate "count:" into ":.,.+(count - 1)" */
4958 stuffcharReadbuff('.');
4959 if (cap->count0 > 1)
4960 {
4961 stuffReadbuff((char_u *)",.+");
4962 stuffnumReadbuff((long)cap->count0 - 1L);
4963 }
4964 }
4965
4966 /* When typing, don't type below an old message */
4967 if (KeyTyped)
4968 compute_cmdrow();
4969
4970 old_p_im = p_im;
4971
4972 /* get a command line and execute it */
4973 do_cmdline(NULL, getexline, NULL,
4974 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
4975
4976 /* If 'insertmode' changed, enter or exit Insert mode */
4977 if (p_im != old_p_im)
4978 {
4979 if (p_im)
4980 restart_edit = 'i';
4981 else
4982 restart_edit = 0;
4983 }
4984
4985 /* The start of the operator may have become invalid by the Ex
4986 * command. */
4987 if (cap->oap->op_type != OP_NOP
4988 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
4989 || cap->oap->start.col >
4990 STRLEN(ml_get(cap->oap->start.lnum))))
4991 clearopbeep(cap->oap);
4992 }
4993}
4994
4995/*
4996 * Handle CTRL-G command.
4997 */
4998 static void
4999nv_ctrlg(cap)
5000 cmdarg_T *cap;
5001{
5002#ifdef FEAT_VISUAL
5003 if (VIsual_active) /* toggle Selection/Visual mode */
5004 {
5005 VIsual_select = !VIsual_select;
5006 showmode();
5007 }
5008 else
5009#endif
5010 if (!checkclearop(cap->oap))
5011 /* print full name if count given or :cd used */
5012 fileinfo((int)cap->count0, FALSE, TRUE);
5013}
5014
5015/*
5016 * Handle CTRL-H <Backspace> command.
5017 */
5018 static void
5019nv_ctrlh(cap)
5020 cmdarg_T *cap;
5021{
5022#ifdef FEAT_VISUAL
5023 if (VIsual_active && VIsual_select)
5024 {
5025 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5026 v_visop(cap);
5027 }
5028 else
5029#endif
5030 nv_left(cap);
5031}
5032
5033/*
5034 * CTRL-L: clear screen and redraw.
5035 */
5036 static void
5037nv_clear(cap)
5038 cmdarg_T *cap;
5039{
5040 if (!checkclearop(cap->oap))
5041 {
5042#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5043 /*
5044 * Right now, the BeBox doesn't seem to have an easy way to detect
5045 * window resizing, so we cheat and make the user detect it
5046 * manually with CTRL-L instead
5047 */
5048 ui_get_shellsize();
5049#endif
5050#ifdef FEAT_SYN_HL
5051 /* Clear all syntax states to force resyncing. */
5052 syn_stack_free_all(curbuf);
5053#endif
5054 redraw_later(CLEAR);
5055 }
5056}
5057
5058/*
5059 * CTRL-O: In Select mode: switch to Visual mode for one command.
5060 * Otherwise: Go to older pcmark.
5061 */
5062 static void
5063nv_ctrlo(cap)
5064 cmdarg_T *cap;
5065{
5066#ifdef FEAT_VISUAL
5067 if (VIsual_active && VIsual_select)
5068 {
5069 VIsual_select = FALSE;
5070 showmode();
5071 restart_VIsual_select = 2; /* restart Select mode later */
5072 }
5073 else
5074#endif
5075 {
5076 cap->count1 = -cap->count1;
5077 nv_pcmark(cap);
5078 }
5079}
5080
5081/*
5082 * CTRL-^ command, short for ":e #"
5083 */
5084 static void
5085nv_hat(cap)
5086 cmdarg_T *cap;
5087{
5088 if (!checkclearopq(cap->oap))
5089 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5090 GETF_SETMARK|GETF_ALT, FALSE);
5091}
5092
5093/*
5094 * "Z" commands.
5095 */
5096 static void
5097nv_Zet(cap)
5098 cmdarg_T *cap;
5099{
5100 if (!checkclearopq(cap->oap))
5101 {
5102 switch (cap->nchar)
5103 {
5104 /* "ZZ": equivalent to ":x". */
5105 case 'Z': do_cmdline_cmd((char_u *)"x");
5106 break;
5107
5108 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5109 case 'Q': do_cmdline_cmd((char_u *)"q!");
5110 break;
5111
5112 default: clearopbeep(cap->oap);
5113 }
5114 }
5115}
5116
5117#if defined(FEAT_WINDOWS) || defined(PROTO)
5118/*
5119 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5120 */
5121 void
5122do_nv_ident(c1, c2)
5123 int c1;
5124 int c2;
5125{
5126 oparg_T oa;
5127 cmdarg_T ca;
5128
5129 clear_oparg(&oa);
5130 vim_memset(&ca, 0, sizeof(ca));
5131 ca.oap = &oa;
5132 ca.cmdchar = c1;
5133 ca.nchar = c2;
5134 nv_ident(&ca);
5135}
5136#endif
5137
5138/*
5139 * Handle the commands that use the word under the cursor.
5140 * [g] CTRL-] :ta to current identifier
5141 * [g] 'K' run program for current identifier
5142 * [g] '*' / to current identifier or string
5143 * [g] '#' ? to current identifier or string
5144 * g ']' :tselect for current identifier
5145 */
5146 static void
5147nv_ident(cap)
5148 cmdarg_T *cap;
5149{
5150 char_u *ptr = NULL;
5151 char_u *buf;
5152 char_u *p;
5153 char_u *kp; /* value of 'keywordprg' */
5154 int kp_help; /* 'keywordprg' is ":help" */
5155 int n = 0; /* init for GCC */
5156 int cmdchar;
5157 int g_cmd; /* "g" command */
5158 char_u *aux_ptr;
5159 int isman;
5160 int isman_s;
5161
5162 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5163 {
5164 cmdchar = cap->nchar;
5165 g_cmd = TRUE;
5166 }
5167 else
5168 {
5169 cmdchar = cap->cmdchar;
5170 g_cmd = FALSE;
5171 }
5172
5173 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5174 cmdchar = '#';
5175
5176 /*
5177 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5178 */
5179 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5180 {
5181#ifdef FEAT_VISUAL
5182 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5183 return;
5184#endif
5185 if (checkclearopq(cap->oap))
5186 return;
5187 }
5188
5189 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5190 (cmdchar == '*' || cmdchar == '#')
5191 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5192 {
5193 clearop(cap->oap);
5194 return;
5195 }
5196
5197 /* Allocate buffer to put the command in. Inserting backslashes can
5198 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5199 * and some numbers. */
5200 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5201 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5202 || STRCMP(kp, ":help") == 0);
5203 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5204 if (buf == NULL)
5205 return;
5206 buf[0] = NUL;
5207
5208 switch (cmdchar)
5209 {
5210 case '*':
5211 case '#':
5212 /*
5213 * Put cursor at start of word, makes search skip the word
5214 * under the cursor.
5215 * Call setpcmark() first, so "*``" puts the cursor back where
5216 * it was.
5217 */
5218 setpcmark();
5219 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5220
5221 if (!g_cmd && vim_iswordp(ptr))
5222 STRCPY(buf, "\\<");
5223 no_smartcase = TRUE; /* don't use 'smartcase' now */
5224 break;
5225
5226 case 'K':
5227 if (kp_help)
5228 STRCPY(buf, "he! ");
5229 else
5230 {
5231 /* When a count is given, turn it into a range. Is this
5232 * really what we want? */
5233 isman = (STRCMP(kp, "man") == 0);
5234 isman_s = (STRCMP(kp, "man -s") == 0);
5235 if (cap->count0 != 0 && !(isman || isman_s))
5236 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5237
5238 STRCAT(buf, "! ");
5239 if (cap->count0 == 0 && isman_s)
5240 STRCAT(buf, "man");
5241 else
5242 STRCAT(buf, kp);
5243 STRCAT(buf, " ");
5244 if (cap->count0 != 0 && (isman || isman_s))
5245 {
5246 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5247 STRCAT(buf, " ");
5248 }
5249 }
5250 break;
5251
5252 case ']':
5253#ifdef FEAT_CSCOPE
5254 if (p_cst)
5255 STRCPY(buf, "cstag ");
5256 else
5257#endif
5258 STRCPY(buf, "ts ");
5259 break;
5260
5261 default:
5262 if (curbuf->b_help)
5263 STRCPY(buf, "he! ");
5264 else if (g_cmd)
5265 STRCPY(buf, "tj ");
5266 else
5267 STRCPY(buf, "ta ");
5268 }
5269
5270 /*
5271 * Now grab the chars in the identifier
5272 */
5273 if (cmdchar == '*')
5274 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5275 else if (cmdchar == '#')
5276 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
5277 else if (cmdchar == 'K' && !kp_help)
5278 aux_ptr = (char_u *)" \t\\\"|!";
5279 else
5280 /* Don't escape spaces and Tabs in a tag with a backslash */
5281 aux_ptr = (char_u *)"\\|\"";
5282
5283 p = buf + STRLEN(buf);
5284 while (n-- > 0)
5285 {
5286 /* put a backslash before \ and some others */
5287 if (vim_strchr(aux_ptr, *ptr) != NULL)
5288 *p++ = '\\';
5289#ifdef FEAT_MBYTE
5290 /* When current byte is a part of multibyte character, copy all bytes
5291 * of that character. */
5292 if (has_mbyte)
5293 {
5294 int i;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005295 int len = (*mb_ptr2len)(ptr) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 for (i = 0; i < len && n >= 1; ++i, --n)
5298 *p++ = *ptr++;
5299 }
5300#endif
5301 *p++ = *ptr++;
5302 }
5303 *p = NUL;
5304
5305 /*
5306 * Execute the command.
5307 */
5308 if (cmdchar == '*' || cmdchar == '#')
5309 {
5310 if (!g_cmd && (
5311#ifdef FEAT_MBYTE
5312 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5313#endif
5314 vim_iswordc(ptr[-1])))
5315 STRCAT(buf, "\\>");
5316#ifdef FEAT_CMDHIST
5317 /* put pattern in search history */
5318 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5319#endif
5320 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5321 }
5322 else
5323 do_cmdline_cmd(buf);
5324
5325 vim_free(buf);
5326}
5327
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005328#if defined(FEAT_VISUAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329/*
5330 * Get visually selected text, within one line only.
5331 * Returns FAIL if more than one line selected.
5332 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005333 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334get_visual_text(cap, pp, lenp)
5335 cmdarg_T *cap;
5336 char_u **pp; /* return: start of selected text */
5337 int *lenp; /* return: length of selected text */
5338{
5339 if (VIsual_mode != 'V')
5340 unadjust_for_sel();
5341 if (VIsual.lnum != curwin->w_cursor.lnum)
5342 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005343 if (cap != NULL)
5344 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 return FAIL;
5346 }
5347 if (VIsual_mode == 'V')
5348 {
5349 *pp = ml_get_curline();
5350 *lenp = (int)STRLEN(*pp);
5351 }
5352 else
5353 {
5354 if (lt(curwin->w_cursor, VIsual))
5355 {
5356 *pp = ml_get_pos(&curwin->w_cursor);
5357 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5358 }
5359 else
5360 {
5361 *pp = ml_get_pos(&VIsual);
5362 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5363 }
5364#ifdef FEAT_MBYTE
5365 if (has_mbyte)
5366 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005367 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#endif
5369 }
5370 reset_VIsual_and_resel();
5371 return OK;
5372}
5373#endif
5374
5375/*
5376 * CTRL-T: backwards in tag stack
5377 */
5378 static void
5379nv_tagpop(cap)
5380 cmdarg_T *cap;
5381{
5382 if (!checkclearopq(cap->oap))
5383 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5384}
5385
5386/*
5387 * Handle scrolling command 'H', 'L' and 'M'.
5388 */
5389 static void
5390nv_scroll(cap)
5391 cmdarg_T *cap;
5392{
5393 int used = 0;
5394 long n;
5395#ifdef FEAT_FOLDING
5396 linenr_T lnum;
5397#endif
5398 int half;
5399
5400 cap->oap->motion_type = MLINE;
5401 setpcmark();
5402
5403 if (cap->cmdchar == 'L')
5404 {
5405 validate_botline(); /* make sure curwin->w_botline is valid */
5406 curwin->w_cursor.lnum = curwin->w_botline - 1;
5407 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5408 curwin->w_cursor.lnum = 1;
5409 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005410 {
5411#ifdef FEAT_FOLDING
5412 if (hasAnyFolding(curwin))
5413 {
5414 /* Count a fold for one screen line. */
5415 for (n = cap->count1 - 1; n > 0
5416 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5417 {
5418 (void)hasFolding(curwin->w_cursor.lnum,
5419 &curwin->w_cursor.lnum, NULL);
5420 --curwin->w_cursor.lnum;
5421 }
5422 }
5423 else
5424#endif
5425 curwin->w_cursor.lnum -= cap->count1 - 1;
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 else
5429 {
5430 if (cap->cmdchar == 'M')
5431 {
5432#ifdef FEAT_DIFF
5433 /* Don't count filler lines above the window. */
5434 used -= diff_check_fill(curwin, curwin->w_topline)
5435 - curwin->w_topfill;
5436#endif
5437 validate_botline(); /* make sure w_empty_rows is valid */
5438 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5439 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5440 {
5441#ifdef FEAT_DIFF
5442 /* Count half he number of filler lines to be "below this
5443 * line" and half to be "above the next line". */
5444 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5445 + n) / 2 >= half)
5446 {
5447 --n;
5448 break;
5449 }
5450#endif
5451 used += plines(curwin->w_topline + n);
5452 if (used >= half)
5453 break;
5454#ifdef FEAT_FOLDING
5455 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5456 n = lnum - curwin->w_topline;
5457#endif
5458 }
5459 if (n > 0 && used > curwin->w_height)
5460 --n;
5461 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005462 else /* (cap->cmdchar == 'H') */
5463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005465#ifdef FEAT_FOLDING
5466 if (hasAnyFolding(curwin))
5467 {
5468 /* Count a fold for one screen line. */
5469 lnum = curwin->w_topline;
5470 while (n-- > 0 && lnum < curwin->w_botline - 1)
5471 {
5472 hasFolding(lnum, NULL, &lnum);
5473 ++lnum;
5474 }
5475 n = lnum - curwin->w_topline;
5476 }
5477#endif
5478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 curwin->w_cursor.lnum = curwin->w_topline + n;
5480 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5481 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5482 }
5483
5484 cursor_correct(); /* correct for 'so' */
5485 beginline(BL_SOL | BL_FIX);
5486}
5487
5488/*
5489 * Cursor right commands.
5490 */
5491 static void
5492nv_right(cap)
5493 cmdarg_T *cap;
5494{
5495 long n;
5496#ifdef FEAT_VISUAL
5497 int PAST_LINE;
5498#else
5499# define PAST_LINE 0
5500#endif
5501
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005502 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5503 {
5504 /* <C-Right> and <S-Right> move a word or WORD right */
5505 if (mod_mask & MOD_MASK_CTRL)
5506 cap->arg = TRUE;
5507 nv_wordcmd(cap);
5508 return;
5509 }
5510
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 cap->oap->motion_type = MCHAR;
5512 cap->oap->inclusive = FALSE;
5513#ifdef FEAT_VISUAL
5514 PAST_LINE = (VIsual_active && *p_sel != 'o');
5515
5516# ifdef FEAT_VIRTUALEDIT
5517 /*
5518 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5519 * (theoretically) infinitly long.
5520 */
5521 if (virtual_active())
5522 PAST_LINE = 0;
5523# endif
5524#endif
5525
5526 for (n = cap->count1; n > 0; --n)
5527 {
5528 if ((!PAST_LINE && oneright() == FAIL)
5529 || (PAST_LINE && *ml_get_cursor() == NUL))
5530 {
5531 /*
5532 * <Space> wraps to next line if 'whichwrap' bit 1 set.
5533 * 'l' wraps to next line if 'whichwrap' bit 2 set.
5534 * CURS_RIGHT wraps to next line if 'whichwrap' bit 3 set
5535 */
5536 if ( ((cap->cmdchar == ' '
5537 && vim_strchr(p_ww, 's') != NULL)
5538 || (cap->cmdchar == 'l'
5539 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005540 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 && vim_strchr(p_ww, '>') != NULL))
5542 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5543 {
5544 /* When deleting we also count the NL as a character.
5545 * Set cap->oap->inclusive when last char in the line is
5546 * included, move to next line after that */
5547 if ( (cap->oap->op_type == OP_DELETE
5548 || cap->oap->op_type == OP_CHANGE)
5549 && !cap->oap->inclusive
5550 && !lineempty(curwin->w_cursor.lnum))
5551 cap->oap->inclusive = TRUE;
5552 else
5553 {
5554 ++curwin->w_cursor.lnum;
5555 curwin->w_cursor.col = 0;
5556#ifdef FEAT_VIRTUALEDIT
5557 curwin->w_cursor.coladd = 0;
5558#endif
5559 curwin->w_set_curswant = TRUE;
5560 cap->oap->inclusive = FALSE;
5561 }
5562 continue;
5563 }
5564 if (cap->oap->op_type == OP_NOP)
5565 {
5566 /* Only beep and flush if not moved at all */
5567 if (n == cap->count1)
5568 beep_flush();
5569 }
5570 else
5571 {
5572 if (!lineempty(curwin->w_cursor.lnum))
5573 cap->oap->inclusive = TRUE;
5574 }
5575 break;
5576 }
5577#ifdef FEAT_VISUAL
5578 else if (PAST_LINE)
5579 {
5580 curwin->w_set_curswant = TRUE;
5581# ifdef FEAT_VIRTUALEDIT
5582 if (virtual_active())
5583 oneright();
5584 else
5585# endif
5586 {
5587# ifdef FEAT_MBYTE
5588 if (has_mbyte)
5589 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005590 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
5592# endif
5593 ++curwin->w_cursor.col;
5594 }
5595 }
5596#endif
5597 }
5598#ifdef FEAT_FOLDING
5599 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5600 && cap->oap->op_type == OP_NOP)
5601 foldOpenCursor();
5602#endif
5603}
5604
5605/*
5606 * Cursor left commands.
5607 *
5608 * Returns TRUE when operator end should not be adjusted.
5609 */
5610 static void
5611nv_left(cap)
5612 cmdarg_T *cap;
5613{
5614 long n;
5615
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005616 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5617 {
5618 /* <C-Left> and <S-Left> move a word or WORD left */
5619 if (mod_mask & MOD_MASK_CTRL)
5620 cap->arg = 1;
5621 nv_bck_word(cap);
5622 return;
5623 }
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 cap->oap->motion_type = MCHAR;
5626 cap->oap->inclusive = FALSE;
5627 for (n = cap->count1; n > 0; --n)
5628 {
5629 if (oneleft() == FAIL)
5630 {
5631 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5632 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5633 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5634 */
5635 if ( (((cap->cmdchar == K_BS
5636 || cap->cmdchar == Ctrl_H)
5637 && vim_strchr(p_ww, 'b') != NULL)
5638 || (cap->cmdchar == 'h'
5639 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005640 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 && vim_strchr(p_ww, '<') != NULL))
5642 && curwin->w_cursor.lnum > 1)
5643 {
5644 --(curwin->w_cursor.lnum);
5645 coladvance((colnr_T)MAXCOL);
5646 curwin->w_set_curswant = TRUE;
5647
5648 /* When the NL before the first char has to be deleted we
5649 * put the cursor on the NUL after the previous line.
5650 * This is a very special case, be careful!
5651 * don't adjust op_end now, otherwise it won't work */
5652 if ( (cap->oap->op_type == OP_DELETE
5653 || cap->oap->op_type == OP_CHANGE)
5654 && !lineempty(curwin->w_cursor.lnum))
5655 {
5656 ++curwin->w_cursor.col;
5657 cap->retval |= CA_NO_ADJ_OP_END;
5658 }
5659 continue;
5660 }
5661 /* Only beep and flush if not moved at all */
5662 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
5663 beep_flush();
5664 break;
5665 }
5666 }
5667#ifdef FEAT_FOLDING
5668 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5669 && cap->oap->op_type == OP_NOP)
5670 foldOpenCursor();
5671#endif
5672}
5673
5674/*
5675 * Cursor up commands.
5676 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5677 */
5678 static void
5679nv_up(cap)
5680 cmdarg_T *cap;
5681{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005682 if (mod_mask & MOD_MASK_SHIFT)
5683 {
5684 /* <S-Up> is page up */
5685 cap->arg = BACKWARD;
5686 nv_page(cap);
5687 }
5688 else
5689 {
5690 cap->oap->motion_type = MLINE;
5691 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5692 clearopbeep(cap->oap);
5693 else if (cap->arg)
5694 beginline(BL_WHITE | BL_FIX);
5695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696}
5697
5698/*
5699 * Cursor down commands.
5700 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5701 */
5702 static void
5703nv_down(cap)
5704 cmdarg_T *cap;
5705{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005706 if (mod_mask & MOD_MASK_SHIFT)
5707 {
5708 /* <S-Down> is page down */
5709 cap->arg = FORWARD;
5710 nv_page(cap);
5711 }
5712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5714 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005715 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 do_cmdline_cmd((char_u *)".cc");
5717 else
5718#endif
5719 {
5720#ifdef FEAT_CMDWIN
5721 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005722 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 cmdwin_result = CAR;
5724 else
5725#endif
5726 {
5727 cap->oap->motion_type = MLINE;
5728 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5729 clearopbeep(cap->oap);
5730 else if (cap->arg)
5731 beginline(BL_WHITE | BL_FIX);
5732 }
5733 }
5734}
5735
5736#ifdef FEAT_SEARCHPATH
5737/*
5738 * Grab the file name under the cursor and edit it.
5739 */
5740 static void
5741nv_gotofile(cap)
5742 cmdarg_T *cap;
5743{
5744 char_u *ptr;
5745
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005746 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005749 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 return;
5751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005753 ptr = grab_file_name(cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 if (ptr != NULL)
5756 {
5757 /* do autowrite if necessary */
5758 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
5759 autowrite(curbuf, FALSE);
5760 setpcmark();
5761 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
5762 P_HID(curbuf) ? ECMD_HIDE : 0);
5763 vim_free(ptr);
5764 }
5765 else
5766 clearop(cap->oap);
5767}
5768#endif
5769
5770/*
5771 * <End> command: to end of current line or last line.
5772 */
5773 static void
5774nv_end(cap)
5775 cmdarg_T *cap;
5776{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005777 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005779 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 nv_goto(cap);
5781 cap->count1 = 1; /* to end of current line */
5782 }
5783 nv_dollar(cap);
5784}
5785
5786/*
5787 * Handle the "$" command.
5788 */
5789 static void
5790nv_dollar(cap)
5791 cmdarg_T *cap;
5792{
5793 cap->oap->motion_type = MCHAR;
5794 cap->oap->inclusive = TRUE;
5795#ifdef FEAT_VIRTUALEDIT
5796 /* In virtual mode when off the edge of a line and an operator
5797 * is pending (whew!) keep the cursor where it is.
5798 * Otherwise, send it to the end of the line. */
5799 if (!virtual_active() || gchar_cursor() != NUL
5800 || cap->oap->op_type == OP_NOP)
5801#endif
5802 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5803 if (cursor_down((long)(cap->count1 - 1),
5804 cap->oap->op_type == OP_NOP) == FAIL)
5805 clearopbeep(cap->oap);
5806#ifdef FEAT_FOLDING
5807 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5808 foldOpenCursor();
5809#endif
5810}
5811
5812/*
5813 * Implementation of '?' and '/' commands.
5814 * If cap->arg is TRUE don't set PC mark.
5815 */
5816 static void
5817nv_search(cap)
5818 cmdarg_T *cap;
5819{
5820 oparg_T *oap = cap->oap;
5821
5822 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5823 {
5824 /* Translate "g??" to "g?g?" */
5825 cap->cmdchar = 'g';
5826 cap->nchar = '?';
5827 nv_operator(cap);
5828 return;
5829 }
5830
5831 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
5832
5833 if (cap->searchbuf == NULL)
5834 {
5835 clearop(oap);
5836 return;
5837 }
5838
5839 normal_search(cap, cap->cmdchar, cap->searchbuf,
5840 (cap->arg ? 0 : SEARCH_MARK));
5841}
5842
5843/*
5844 * Handle "N" and "n" commands.
5845 * cap->arg is SEARCH_REV for "N", 0 for "n".
5846 */
5847 static void
5848nv_next(cap)
5849 cmdarg_T *cap;
5850{
5851 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5852}
5853
5854/*
5855 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5856 * Uses only cap->count1 and cap->oap from "cap".
5857 */
5858 static void
5859normal_search(cap, dir, pat, opt)
5860 cmdarg_T *cap;
5861 int dir;
5862 char_u *pat;
5863 int opt; /* extra flags for do_search() */
5864{
5865 int i;
5866
5867 cap->oap->motion_type = MCHAR;
5868 cap->oap->inclusive = FALSE;
5869 cap->oap->use_reg_one = TRUE;
5870 curwin->w_set_curswant = TRUE;
5871
5872 i = do_search(cap->oap, dir, pat, cap->count1,
5873 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG);
5874 if (i == 0)
5875 clearop(cap->oap);
5876 else
5877 {
5878 if (i == 2)
5879 cap->oap->motion_type = MLINE;
5880#ifdef FEAT_VIRTUALEDIT
5881 curwin->w_cursor.coladd = 0;
5882#endif
5883#ifdef FEAT_FOLDING
5884 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5885 foldOpenCursor();
5886#endif
5887 }
5888
5889 /* "/$" will put the cursor after the end of the line, may need to
5890 * correct that here */
5891 check_cursor();
5892}
5893
5894/*
5895 * Character search commands.
5896 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5897 * ',' and FALSE for ';'.
5898 * cap->nchar is NUL for ',' and ';' (repeat the search)
5899 */
5900 static void
5901nv_csearch(cap)
5902 cmdarg_T *cap;
5903{
5904 int t_cmd;
5905
5906 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5907 t_cmd = TRUE;
5908 else
5909 t_cmd = FALSE;
5910
5911 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5913 clearopbeep(cap->oap);
5914 else
5915 {
5916 curwin->w_set_curswant = TRUE;
5917#ifdef FEAT_VIRTUALEDIT
5918 /* Include a Tab for "tx" and for "dfx". */
5919 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5920 && (t_cmd || cap->oap->op_type != OP_NOP))
5921 {
5922 colnr_T scol, ecol;
5923
5924 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5925 curwin->w_cursor.coladd = ecol - scol;
5926 }
5927 else
5928 curwin->w_cursor.coladd = 0;
5929#endif
5930#ifdef FEAT_VISUAL
5931 adjust_for_sel(cap);
5932#endif
5933#ifdef FEAT_FOLDING
5934 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5935 foldOpenCursor();
5936#endif
5937 }
5938}
5939
5940/*
5941 * "[" and "]" commands.
5942 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5943 */
5944 static void
5945nv_brackets(cap)
5946 cmdarg_T *cap;
5947{
5948 pos_T new_pos;
5949 pos_T prev_pos;
5950 pos_T *pos = NULL; /* init for GCC */
5951 pos_T old_pos; /* cursor position before command */
5952 int flag;
5953 long n;
5954 int findc;
5955 int c;
5956
5957 cap->oap->motion_type = MCHAR;
5958 cap->oap->inclusive = FALSE;
5959 old_pos = curwin->w_cursor;
5960#ifdef FEAT_VIRTUALEDIT
5961 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
5962#endif
5963
5964#ifdef FEAT_SEARCHPATH
5965 /*
5966 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5967 */
5968 if (cap->nchar == 'f')
5969 nv_gotofile(cap);
5970 else
5971#endif
5972
5973#ifdef FEAT_FIND_ID
5974 /*
5975 * Find the occurence(s) of the identifier or define under cursor
5976 * in current and included files or jump to the first occurence.
5977 *
5978 * search list jump
5979 * fwd bwd fwd bwd fwd bwd
5980 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5981 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5982 */
5983 if (vim_strchr((char_u *)
5984#ifdef EBCDIC
5985 "iI\005dD\067",
5986#else
5987 "iI\011dD\004",
5988#endif
5989 cap->nchar) != NULL)
5990 {
5991 char_u *ptr;
5992 int len;
5993
5994 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5995 clearop(cap->oap);
5996 else
5997 {
5998 find_pattern_in_path(ptr, 0, len, TRUE,
5999 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6000 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6001 cap->count1,
6002 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6003 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6004 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6005 (linenr_T)MAXLNUM);
6006 curwin->w_set_curswant = TRUE;
6007 }
6008 }
6009 else
6010#endif
6011
6012 /*
6013 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6014 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6015 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6016 * "[m" or "]m" search for prev/next start of (Java) method.
6017 * "[M" or "]M" search for prev/next end of (Java) method.
6018 */
6019 if ( (cap->cmdchar == '['
6020 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6021 || (cap->cmdchar == ']'
6022 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6023 {
6024 if (cap->nchar == '*')
6025 cap->nchar = '/';
6026 new_pos.lnum = 0;
6027 prev_pos.lnum = 0;
6028 if (cap->nchar == 'm' || cap->nchar == 'M')
6029 {
6030 if (cap->cmdchar == '[')
6031 findc = '{';
6032 else
6033 findc = '}';
6034 n = 9999;
6035 }
6036 else
6037 {
6038 findc = cap->nchar;
6039 n = cap->count1;
6040 }
6041 for ( ; n > 0; --n)
6042 {
6043 if ((pos = findmatchlimit(cap->oap, findc,
6044 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6045 {
6046 if (new_pos.lnum == 0) /* nothing found */
6047 {
6048 if (cap->nchar != 'm' && cap->nchar != 'M')
6049 clearopbeep(cap->oap);
6050 }
6051 else
6052 pos = &new_pos; /* use last one found */
6053 break;
6054 }
6055 prev_pos = new_pos;
6056 curwin->w_cursor = *pos;
6057 new_pos = *pos;
6058 }
6059 curwin->w_cursor = old_pos;
6060
6061 /*
6062 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6063 * brought us to the match for "[m" and "]M" when inside a method.
6064 * Try finding the '{' or '}' we want to be at.
6065 * Also repeat for the given count.
6066 */
6067 if (cap->nchar == 'm' || cap->nchar == 'M')
6068 {
6069 /* norm is TRUE for "]M" and "[m" */
6070 int norm = ((findc == '{') == (cap->nchar == 'm'));
6071
6072 n = cap->count1;
6073 /* found a match: we were inside a method */
6074 if (prev_pos.lnum != 0)
6075 {
6076 pos = &prev_pos;
6077 curwin->w_cursor = prev_pos;
6078 if (norm)
6079 --n;
6080 }
6081 else
6082 pos = NULL;
6083 while (n > 0)
6084 {
6085 for (;;)
6086 {
6087 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6088 {
6089 /* if not found anything, that's an error */
6090 if (pos == NULL)
6091 clearopbeep(cap->oap);
6092 n = 0;
6093 break;
6094 }
6095 c = gchar_cursor();
6096 if (c == '{' || c == '}')
6097 {
6098 /* Must have found end/start of class: use it.
6099 * Or found the place to be at. */
6100 if ((c == findc && norm) || (n == 1 && !norm))
6101 {
6102 new_pos = curwin->w_cursor;
6103 pos = &new_pos;
6104 n = 0;
6105 }
6106 /* if no match found at all, we started outside of the
6107 * class and we're inside now. Just go on. */
6108 else if (new_pos.lnum == 0)
6109 {
6110 new_pos = curwin->w_cursor;
6111 pos = &new_pos;
6112 }
6113 /* found start/end of other method: go to match */
6114 else if ((pos = findmatchlimit(cap->oap, findc,
6115 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6116 0)) == NULL)
6117 n = 0;
6118 else
6119 curwin->w_cursor = *pos;
6120 break;
6121 }
6122 }
6123 --n;
6124 }
6125 curwin->w_cursor = old_pos;
6126 if (pos == NULL && new_pos.lnum != 0)
6127 clearopbeep(cap->oap);
6128 }
6129 if (pos != NULL)
6130 {
6131 setpcmark();
6132 curwin->w_cursor = *pos;
6133 curwin->w_set_curswant = TRUE;
6134#ifdef FEAT_FOLDING
6135 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6136 && cap->oap->op_type == OP_NOP)
6137 foldOpenCursor();
6138#endif
6139 }
6140 }
6141
6142 /*
6143 * "[[", "[]", "]]" and "][": move to start or end of function
6144 */
6145 else if (cap->nchar == '[' || cap->nchar == ']')
6146 {
6147 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6148 flag = '{';
6149 else
6150 flag = '}'; /* "][" or "[]" */
6151
6152 curwin->w_set_curswant = TRUE;
6153 /*
6154 * Imitate strange Vi behaviour: When using "]]" with an operator
6155 * we also stop at '}'.
6156 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006157 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 (cap->oap->op_type != OP_NOP
6159 && cap->arg == FORWARD && flag == '{')))
6160 clearopbeep(cap->oap);
6161 else
6162 {
6163 if (cap->oap->op_type == OP_NOP)
6164 beginline(BL_WHITE | BL_FIX);
6165#ifdef FEAT_FOLDING
6166 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6167 foldOpenCursor();
6168#endif
6169 }
6170 }
6171
6172 /*
6173 * "[p", "[P", "]P" and "]p": put with indent adjustment
6174 */
6175 else if (cap->nchar == 'p' || cap->nchar == 'P')
6176 {
6177 if (!checkclearopq(cap->oap))
6178 {
6179 prep_redo_cmd(cap);
6180 do_put(cap->oap->regname,
6181 (cap->cmdchar == ']' && cap->nchar == 'p') ? FORWARD : BACKWARD,
6182 cap->count1, PUT_FIXINDENT);
6183 }
6184 }
6185
6186 /*
6187 * "['", "[`", "]'" and "]`": jump to next mark
6188 */
6189 else if (cap->nchar == '\'' || cap->nchar == '`')
6190 {
6191 pos = &curwin->w_cursor;
6192 for (n = cap->count1; n > 0; --n)
6193 {
6194 prev_pos = *pos;
6195 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6196 cap->nchar == '\'');
6197 if (pos == NULL)
6198 break;
6199 }
6200 if (pos == NULL)
6201 pos = &prev_pos;
6202 nv_cursormark(cap, cap->nchar == '\'', pos);
6203 }
6204
6205#ifdef FEAT_MOUSE
6206 /*
6207 * [ or ] followed by a middle mouse click: put selected text with
6208 * indent adjustment. Any other button just does as usual.
6209 */
6210 else if (cap->nchar >= K_LEFTMOUSE && cap->nchar <= K_RIGHTRELEASE)
6211 {
6212 (void)do_mouse(cap->oap, cap->nchar,
6213 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6214 cap->count1, PUT_FIXINDENT);
6215 }
6216#endif /* FEAT_MOUSE */
6217
6218#ifdef FEAT_FOLDING
6219 /*
6220 * "[z" and "]z": move to start or end of open fold.
6221 */
6222 else if (cap->nchar == 'z')
6223 {
6224 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6225 cap->count1) == FAIL)
6226 clearopbeep(cap->oap);
6227 }
6228#endif
6229
6230#ifdef FEAT_DIFF
6231 /*
6232 * "[c" and "]c": move to next or previous diff-change.
6233 */
6234 else if (cap->nchar == 'c')
6235 {
6236 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6237 cap->count1) == FAIL)
6238 clearopbeep(cap->oap);
6239 }
6240#endif
6241
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006242#ifdef FEAT_SYN_HL
6243 /*
6244 * "[s", "[S", "]s" and "]S": move to next spell error.
6245 */
6246 else if (cap->nchar == 's' || cap->nchar == 'S')
6247 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006248 setpcmark();
6249 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006250 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6251 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006252 {
6253 clearopbeep(cap->oap);
6254 break;
6255 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006256 }
6257#endif
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 /* Not a valid cap->nchar. */
6260 else
6261 clearopbeep(cap->oap);
6262}
6263
6264/*
6265 * Handle Normal mode "%" command.
6266 */
6267 static void
6268nv_percent(cap)
6269 cmdarg_T *cap;
6270{
6271 pos_T *pos;
6272#ifdef FEAT_FOLDING
6273 linenr_T lnum = curwin->w_cursor.lnum;
6274#endif
6275
6276 cap->oap->inclusive = TRUE;
6277 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6278 {
6279 if (cap->count0 > 100)
6280 clearopbeep(cap->oap);
6281 else
6282 {
6283 cap->oap->motion_type = MLINE;
6284 setpcmark();
6285 /* Round up, so CTRL-G will give same value. Watch out for a
6286 * large line count, the line number must not go negative! */
6287 if (curbuf->b_ml.ml_line_count > 1000000)
6288 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6289 / 100L * cap->count0;
6290 else
6291 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6292 cap->count0 + 99L) / 100L;
6293 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6294 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6295 beginline(BL_SOL | BL_FIX);
6296 }
6297 }
6298 else /* "%" : go to matching paren */
6299 {
6300 cap->oap->motion_type = MCHAR;
6301 cap->oap->use_reg_one = TRUE;
6302 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6303 clearopbeep(cap->oap);
6304 else
6305 {
6306 setpcmark();
6307 curwin->w_cursor = *pos;
6308 curwin->w_set_curswant = TRUE;
6309#ifdef FEAT_VIRTUALEDIT
6310 curwin->w_cursor.coladd = 0;
6311#endif
6312#ifdef FEAT_VISUAL
6313 adjust_for_sel(cap);
6314#endif
6315 }
6316 }
6317#ifdef FEAT_FOLDING
6318 if (cap->oap->op_type == OP_NOP
6319 && lnum != curwin->w_cursor.lnum
6320 && (fdo_flags & FDO_PERCENT)
6321 && KeyTyped)
6322 foldOpenCursor();
6323#endif
6324}
6325
6326/*
6327 * Handle "(" and ")" commands.
6328 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6329 */
6330 static void
6331nv_brace(cap)
6332 cmdarg_T *cap;
6333{
6334 cap->oap->motion_type = MCHAR;
6335 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006336 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6337 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 curwin->w_set_curswant = TRUE;
6339
6340 if (findsent(cap->arg, cap->count1) == FAIL)
6341 clearopbeep(cap->oap);
6342 else
6343 {
6344#ifdef FEAT_VIRTUALEDIT
6345 curwin->w_cursor.coladd = 0;
6346#endif
6347#ifdef FEAT_FOLDING
6348 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6349 foldOpenCursor();
6350#endif
6351 }
6352}
6353
6354/*
6355 * "m" command: Mark a position.
6356 */
6357 static void
6358nv_mark(cap)
6359 cmdarg_T *cap;
6360{
6361 if (!checkclearop(cap->oap))
6362 {
6363 if (setmark(cap->nchar) == FAIL)
6364 clearopbeep(cap->oap);
6365 }
6366}
6367
6368/*
6369 * "{" and "}" commands.
6370 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6371 */
6372 static void
6373nv_findpar(cap)
6374 cmdarg_T *cap;
6375{
6376 cap->oap->motion_type = MCHAR;
6377 cap->oap->inclusive = FALSE;
6378 cap->oap->use_reg_one = TRUE;
6379 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006380 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 clearopbeep(cap->oap);
6382 else
6383 {
6384#ifdef FEAT_VIRTUALEDIT
6385 curwin->w_cursor.coladd = 0;
6386#endif
6387#ifdef FEAT_FOLDING
6388 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6389 foldOpenCursor();
6390#endif
6391 }
6392}
6393
6394/*
6395 * "u" command: Undo or make lower case.
6396 */
6397 static void
6398nv_undo(cap)
6399 cmdarg_T *cap;
6400{
6401 if (cap->oap->op_type == OP_LOWER
6402#ifdef FEAT_VISUAL
6403 || VIsual_active
6404#endif
6405 )
6406 {
6407 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6408 cap->cmdchar = 'g';
6409 cap->nchar = 'u';
6410 nv_operator(cap);
6411 }
6412 else
6413 nv_kundo(cap);
6414}
6415
6416/*
6417 * <Undo> command.
6418 */
6419 static void
6420nv_kundo(cap)
6421 cmdarg_T *cap;
6422{
6423 if (!checkclearopq(cap->oap))
6424 {
6425 u_undo((int)cap->count1);
6426 curwin->w_set_curswant = TRUE;
6427 }
6428}
6429
6430/*
6431 * Handle the "r" command.
6432 */
6433 static void
6434nv_replace(cap)
6435 cmdarg_T *cap;
6436{
6437 char_u *ptr;
6438 int had_ctrl_v;
6439 long n;
6440
6441 if (checkclearop(cap->oap))
6442 return;
6443
6444 /* get another character */
6445 if (cap->nchar == Ctrl_V)
6446 {
6447 had_ctrl_v = Ctrl_V;
6448 cap->nchar = get_literal();
6449 /* Don't redo a multibyte character with CTRL-V. */
6450 if (cap->nchar > DEL)
6451 had_ctrl_v = NUL;
6452 }
6453 else
6454 had_ctrl_v = NUL;
6455
6456#ifdef FEAT_VISUAL
6457 /* Visual mode "r" */
6458 if (VIsual_active)
6459 {
6460 nv_operator(cap);
6461 return;
6462 }
6463#endif
6464
6465#ifdef FEAT_VIRTUALEDIT
6466 /* Break tabs, etc. */
6467 if (virtual_active())
6468 {
6469 if (u_save_cursor() == FAIL)
6470 return;
6471 if (gchar_cursor() == NUL)
6472 {
6473 /* Add extra space and put the cursor on the first one. */
6474 coladvance_force((colnr_T)(getviscol() + cap->count1));
6475 curwin->w_cursor.col -= cap->count1;
6476 }
6477 else if (gchar_cursor() == TAB)
6478 coladvance_force(getviscol());
6479 }
6480#endif
6481
6482 /*
6483 * Check for a special key or not enough characters to replace.
6484 */
6485 ptr = ml_get_cursor();
6486 if (IS_SPECIAL(cap->nchar) || STRLEN(ptr) < (unsigned)cap->count1
6487#ifdef FEAT_MBYTE
6488 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6489#endif
6490 )
6491 {
6492 clearopbeep(cap->oap);
6493 return;
6494 }
6495
6496 /*
6497 * Replacing with a TAB is done by edit() when it is complicated because
6498 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6499 * Other characters are done below to avoid problems with things like
6500 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6501 */
6502 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6503 {
6504 stuffnumReadbuff(cap->count1);
6505 stuffcharReadbuff('R');
6506 stuffcharReadbuff('\t');
6507 stuffcharReadbuff(ESC);
6508 return;
6509 }
6510
6511 /* save line for undo */
6512 if (u_save_cursor() == FAIL)
6513 return;
6514
6515 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6516 {
6517 /*
6518 * Replace character(s) by a single newline.
6519 * Strange vi behaviour: Only one newline is inserted.
6520 * Delete the characters here.
6521 * Insert the newline with an insert command, takes care of
6522 * autoindent. The insert command depends on being on the last
6523 * character of a line or not.
6524 */
6525#ifdef FEAT_MBYTE
6526 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6527#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006528 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#endif
6530 stuffcharReadbuff('\r');
6531 stuffcharReadbuff(ESC);
6532
6533 /* Give 'r' to edit(), to get the redo command right. */
6534 invoke_edit(cap, TRUE, 'r', FALSE);
6535 }
6536 else
6537 {
6538 prep_redo(cap->oap->regname, cap->count1,
6539 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6540
6541 curbuf->b_op_start = curwin->w_cursor;
6542#ifdef FEAT_MBYTE
6543 if (has_mbyte)
6544 {
6545 int old_State = State;
6546
6547 if (cap->ncharC1 != 0)
6548 AppendCharToRedobuff(cap->ncharC1);
6549 if (cap->ncharC2 != 0)
6550 AppendCharToRedobuff(cap->ncharC2);
6551
6552 /* This is slow, but it handles replacing a single-byte with a
6553 * multi-byte and the other way around. Also handles adding
6554 * composing characters for utf-8. */
6555 for (n = cap->count1; n > 0; --n)
6556 {
6557 State = REPLACE;
6558 ins_char(cap->nchar);
6559 State = old_State;
6560 if (cap->ncharC1 != 0)
6561 ins_char(cap->ncharC1);
6562 if (cap->ncharC2 != 0)
6563 ins_char(cap->ncharC2);
6564 }
6565 }
6566 else
6567#endif
6568 {
6569 /*
6570 * Replace the characters within one line.
6571 */
6572 for (n = cap->count1; n > 0; --n)
6573 {
6574 /*
6575 * Get ptr again, because u_save and/or showmatch() will have
6576 * released the line. At the same time we let know that the
6577 * line will be changed.
6578 */
6579 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
6580 ptr[curwin->w_cursor.col] = cap->nchar;
6581 if (p_sm && msg_silent == 0)
6582 showmatch(cap->nchar);
6583 ++curwin->w_cursor.col;
6584 }
6585#ifdef FEAT_NETBEANS_INTG
6586 if (usingNetbeans)
6587 {
6588 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
6589
Bram Moolenaar009b2592004-10-24 19:18:58 +00006590 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
6591 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006593 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595#endif
6596
6597 /* mark the buffer as changed and prepare for displaying */
6598 changed_bytes(curwin->w_cursor.lnum,
6599 (colnr_T)(curwin->w_cursor.col - cap->count1));
6600 }
6601 --curwin->w_cursor.col; /* cursor on the last replaced char */
6602#ifdef FEAT_MBYTE
6603 /* if the character on the left of the current cursor is a multi-byte
6604 * character, move two characters left */
6605 if (has_mbyte)
6606 mb_adjust_cursor();
6607#endif
6608 curbuf->b_op_end = curwin->w_cursor;
6609 curwin->w_set_curswant = TRUE;
6610 set_last_insert(cap->nchar);
6611 }
6612}
6613
6614#ifdef FEAT_VISUAL
6615/*
6616 * 'o': Exchange start and end of Visual area.
6617 * 'O': same, but in block mode exchange left and right corners.
6618 */
6619 static void
6620v_swap_corners(cmdchar)
6621 int cmdchar;
6622{
6623 pos_T old_cursor;
6624 colnr_T left, right;
6625
6626 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
6627 {
6628 old_cursor = curwin->w_cursor;
6629 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
6630 curwin->w_cursor.lnum = VIsual.lnum;
6631 coladvance(left);
6632 VIsual = curwin->w_cursor;
6633
6634 curwin->w_cursor.lnum = old_cursor.lnum;
6635 curwin->w_curswant = right;
6636 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6637 * right one column */
6638 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
6639 ++curwin->w_curswant;
6640 coladvance(curwin->w_curswant);
6641 if (curwin->w_cursor.col == old_cursor.col
6642#ifdef FEAT_VIRTUALEDIT
6643 && (!virtual_active()
6644 || curwin->w_cursor.coladd == old_cursor.coladd)
6645#endif
6646 )
6647 {
6648 curwin->w_cursor.lnum = VIsual.lnum;
6649 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
6650 ++right;
6651 coladvance(right);
6652 VIsual = curwin->w_cursor;
6653
6654 curwin->w_cursor.lnum = old_cursor.lnum;
6655 coladvance(left);
6656 curwin->w_curswant = left;
6657 }
6658 }
6659 else
6660 {
6661 old_cursor = curwin->w_cursor;
6662 curwin->w_cursor = VIsual;
6663 VIsual = old_cursor;
6664 curwin->w_set_curswant = TRUE;
6665 }
6666}
6667#endif /* FEAT_VISUAL */
6668
6669/*
6670 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6671 */
6672 static void
6673nv_Replace(cap)
6674 cmdarg_T *cap;
6675{
6676#ifdef FEAT_VISUAL
6677 if (VIsual_active) /* "R" is replace lines */
6678 {
6679 cap->cmdchar = 'c';
6680 cap->nchar = NUL;
6681 VIsual_mode = 'V';
6682 nv_operator(cap);
6683 }
6684 else
6685#endif
6686 if (!checkclearopq(cap->oap))
6687 {
6688 if (!curbuf->b_p_ma)
6689 EMSG(_(e_modifiable));
6690 else
6691 {
6692#ifdef FEAT_VIRTUALEDIT
6693 if (virtual_active())
6694 coladvance(getviscol());
6695#endif
6696 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6697 }
6698 }
6699}
6700
6701#ifdef FEAT_VREPLACE
6702/*
6703 * "gr".
6704 */
6705 static void
6706nv_vreplace(cap)
6707 cmdarg_T *cap;
6708{
6709# ifdef FEAT_VISUAL
6710 if (VIsual_active)
6711 {
6712 cap->cmdchar = 'r';
6713 cap->nchar = cap->extra_char;
6714 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6715 }
6716 else
6717# endif
6718 if (!checkclearopq(cap->oap))
6719 {
6720 if (!curbuf->b_p_ma)
6721 EMSG(_(e_modifiable));
6722 else
6723 {
6724 if (cap->extra_char == Ctrl_V) /* get another character */
6725 cap->extra_char = get_literal();
6726 stuffcharReadbuff(cap->extra_char);
6727 stuffcharReadbuff(ESC);
6728# ifdef FEAT_VIRTUALEDIT
6729 if (virtual_active())
6730 coladvance(getviscol());
6731# endif
6732 invoke_edit(cap, TRUE, 'v', FALSE);
6733 }
6734 }
6735}
6736#endif
6737
6738/*
6739 * Swap case for "~" command, when it does not work like an operator.
6740 */
6741 static void
6742n_swapchar(cap)
6743 cmdarg_T *cap;
6744{
6745 long n;
6746 pos_T startpos;
6747 int did_change = 0;
6748#ifdef FEAT_NETBEANS_INTG
6749 pos_T pos;
6750 char_u *ptr;
6751 int count;
6752#endif
6753
6754 if (checkclearopq(cap->oap))
6755 return;
6756
6757 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
6758 {
6759 clearopbeep(cap->oap);
6760 return;
6761 }
6762
6763 prep_redo_cmd(cap);
6764
6765 if (u_save_cursor() == FAIL)
6766 return;
6767
6768 startpos = curwin->w_cursor;
6769#ifdef FEAT_NETBEANS_INTG
6770 pos = startpos;
6771#endif
6772 for (n = cap->count1; n > 0; --n)
6773 {
6774 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6775 inc_cursor();
6776 if (gchar_cursor() == NUL)
6777 {
6778 if (vim_strchr(p_ww, '~') != NULL
6779 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6780 {
6781#ifdef FEAT_NETBEANS_INTG
6782 if (usingNetbeans)
6783 {
6784 if (did_change)
6785 {
6786 ptr = ml_get(pos.lnum);
6787 count = STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006788 netbeans_removed(curbuf, pos.lnum, pos.col,
6789 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006791 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793 pos.col = 0;
6794 pos.lnum++;
6795 }
6796#endif
6797 ++curwin->w_cursor.lnum;
6798 curwin->w_cursor.col = 0;
6799 if (n > 1)
6800 {
6801 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6802 break;
6803 u_clearline();
6804 }
6805 }
6806 else
6807 break;
6808 }
6809 }
6810#ifdef FEAT_NETBEANS_INTG
6811 if (did_change && usingNetbeans)
6812 {
6813 ptr = ml_get(pos.lnum);
6814 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006815 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6816 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
6818#endif
6819
6820
6821 check_cursor();
6822 curwin->w_set_curswant = TRUE;
6823 if (did_change)
6824 {
6825 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6826 0L);
6827 curbuf->b_op_start = startpos;
6828 curbuf->b_op_end = curwin->w_cursor;
6829 if (curbuf->b_op_end.col > 0)
6830 --curbuf->b_op_end.col;
6831 }
6832}
6833
6834/*
6835 * Move cursor to mark.
6836 */
6837 static void
6838nv_cursormark(cap, flag, pos)
6839 cmdarg_T *cap;
6840 int flag;
6841 pos_T *pos;
6842{
6843 if (check_mark(pos) == FAIL)
6844 clearop(cap->oap);
6845 else
6846 {
6847 if (cap->cmdchar == '\''
6848 || cap->cmdchar == '`'
6849 || cap->cmdchar == '['
6850 || cap->cmdchar == ']')
6851 setpcmark();
6852 curwin->w_cursor = *pos;
6853 if (flag)
6854 beginline(BL_WHITE | BL_FIX);
6855 else
6856 check_cursor();
6857 }
6858 cap->oap->motion_type = flag ? MLINE : MCHAR;
6859 if (cap->cmdchar == '`')
6860 cap->oap->use_reg_one = TRUE;
6861 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6862 curwin->w_set_curswant = TRUE;
6863}
6864
6865#ifdef FEAT_VISUAL
6866/*
6867 * Handle commands that are operators in Visual mode.
6868 */
6869 static void
6870v_visop(cap)
6871 cmdarg_T *cap;
6872{
6873 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6874
6875 /* Uppercase means linewise, except in block mode, then "D" deletes till
6876 * the end of the line, and "C" replaces til EOL */
6877 if (isupper(cap->cmdchar))
6878 {
6879 if (VIsual_mode != Ctrl_V)
6880 VIsual_mode = 'V';
6881 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6882 curwin->w_curswant = MAXCOL;
6883 }
6884 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6885 nv_operator(cap);
6886}
6887#endif
6888
6889/*
6890 * "s" and "S" commands.
6891 */
6892 static void
6893nv_subst(cap)
6894 cmdarg_T *cap;
6895{
6896#ifdef FEAT_VISUAL
6897 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6898 {
6899 if (cap->cmdchar == 'S')
6900 VIsual_mode = 'V';
6901 cap->cmdchar = 'c';
6902 nv_operator(cap);
6903 }
6904 else
6905#endif
6906 nv_optrans(cap);
6907}
6908
6909/*
6910 * Abbreviated commands.
6911 */
6912 static void
6913nv_abbrev(cap)
6914 cmdarg_T *cap;
6915{
6916 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6917 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6918
6919#ifdef FEAT_VISUAL
6920 /* in Visual mode these commands are operators */
6921 if (VIsual_active)
6922 v_visop(cap);
6923 else
6924#endif
6925 nv_optrans(cap);
6926}
6927
6928/*
6929 * Translate a command into another command.
6930 */
6931 static void
6932nv_optrans(cap)
6933 cmdarg_T *cap;
6934{
6935 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6936 (char_u *)"d$", (char_u *)"c$",
6937 (char_u *)"cl", (char_u *)"cc",
6938 (char_u *)"yy", (char_u *)":s\r"};
6939 static char_u *str = (char_u *)"xXDCsSY&";
6940
6941 if (!checkclearopq(cap->oap))
6942 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006943 /* In Vi "2D" doesn't delete the next line. Can't translate it
6944 * either, because "2." should also not use the count. */
6945 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6946 {
6947 cap->oap->start = curwin->w_cursor;
6948 cap->oap->op_type = OP_DELETE;
6949 cap->count1 = 1;
6950 nv_dollar(cap);
6951 finish_op = TRUE;
6952 ResetRedobuff();
6953 AppendCharToRedobuff('D');
6954 }
6955 else
6956 {
6957 if (cap->count0)
6958 stuffnumReadbuff(cap->count0);
6959 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
6960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 }
6962 cap->opcount = 0;
6963}
6964
6965/*
6966 * "'" and "`" commands. Also for "g'" and "g`".
6967 * cap->arg is TRUE for "'" and "g'".
6968 */
6969 static void
6970nv_gomark(cap)
6971 cmdarg_T *cap;
6972{
6973 pos_T *pos;
6974 int c;
6975#ifdef FEAT_FOLDING
6976 linenr_T lnum = curwin->w_cursor.lnum;
6977 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6978#endif
6979
6980 if (cap->cmdchar == 'g')
6981 c = cap->extra_char;
6982 else
6983 c = cap->nchar;
6984 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6985 if (pos == (pos_T *)-1) /* jumped to other file */
6986 {
6987 if (cap->arg)
6988 {
6989 check_cursor_lnum();
6990 beginline(BL_WHITE | BL_FIX);
6991 }
6992 else
6993 check_cursor();
6994 }
6995 else
6996 nv_cursormark(cap, cap->arg, pos);
6997
6998#ifdef FEAT_VIRTUALEDIT
6999 /* May need to clear the coladd that a mark includes. */
7000 if (!virtual_active())
7001 curwin->w_cursor.coladd = 0;
7002#endif
7003#ifdef FEAT_FOLDING
7004 if (cap->oap->op_type == OP_NOP
7005 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7006 && (fdo_flags & FDO_MARK)
7007 && old_KeyTyped)
7008 foldOpenCursor();
7009#endif
7010}
7011
7012/*
7013 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7014 */
7015 static void
7016nv_pcmark(cap)
7017 cmdarg_T *cap;
7018{
7019#ifdef FEAT_JUMPLIST
7020 pos_T *pos;
7021# ifdef FEAT_FOLDING
7022 linenr_T lnum = curwin->w_cursor.lnum;
7023 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7024# endif
7025
7026 if (!checkclearopq(cap->oap))
7027 {
7028 if (cap->cmdchar == 'g')
7029 pos = movechangelist((int)cap->count1);
7030 else
7031 pos = movemark((int)cap->count1);
7032 if (pos == (pos_T *)-1) /* jump to other file */
7033 {
7034 curwin->w_set_curswant = TRUE;
7035 check_cursor();
7036 }
7037 else if (pos != NULL) /* can jump */
7038 nv_cursormark(cap, FALSE, pos);
7039 else if (cap->cmdchar == 'g')
7040 {
7041 if (curbuf->b_changelistlen == 0)
7042 EMSG(_("E664: changelist is empty"));
7043 else if (cap->count1 < 0)
7044 EMSG(_("E662: At start of changelist"));
7045 else
7046 EMSG(_("E663: At end of changelist"));
7047 }
7048 else
7049 clearopbeep(cap->oap);
7050# ifdef FEAT_FOLDING
7051 if (cap->oap->op_type == OP_NOP
7052 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7053 && (fdo_flags & FDO_MARK)
7054 && old_KeyTyped)
7055 foldOpenCursor();
7056# endif
7057 }
7058#else
7059 clearopbeep(cap->oap);
7060#endif
7061}
7062
7063/*
7064 * Handle '"' command.
7065 */
7066 static void
7067nv_regname(cap)
7068 cmdarg_T *cap;
7069{
7070 if (checkclearop(cap->oap))
7071 return;
7072#ifdef FEAT_EVAL
7073 if (cap->nchar == '=')
7074 cap->nchar = get_expr_register();
7075#endif
7076 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7077 {
7078 cap->oap->regname = cap->nchar;
7079 cap->opcount = cap->count0; /* remember count before '"' */
7080#ifdef FEAT_EVAL
7081 set_reg_var(cap->oap->regname);
7082#endif
7083 }
7084 else
7085 clearopbeep(cap->oap);
7086}
7087
7088#ifdef FEAT_VISUAL
7089/*
7090 * Handle "v", "V" and "CTRL-V" commands.
7091 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7092 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007093 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 */
7095 static void
7096nv_visual(cap)
7097 cmdarg_T *cap;
7098{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007099 if (cap->cmdchar == Ctrl_Q)
7100 cap->cmdchar = Ctrl_V;
7101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7103 * characterwise, linewise, or blockwise. */
7104 if (cap->oap->op_type != OP_NOP)
7105 {
7106 cap->oap->motion_force = cap->cmdchar;
7107 finish_op = FALSE; /* operator doesn't finish now but later */
7108 return;
7109 }
7110
7111 VIsual_select = cap->arg;
7112 if (VIsual_active) /* change Visual mode */
7113 {
7114 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7115 end_visual_mode();
7116 else /* toggle char/block mode */
7117 { /* or char/line mode */
7118 VIsual_mode = cap->cmdchar;
7119 showmode();
7120 }
7121 redraw_curbuf_later(INVERTED); /* update the inversion */
7122 }
7123 else /* start Visual mode */
7124 {
7125 check_visual_highlight();
7126 if (cap->count0) /* use previously selected part */
7127 {
7128 if (resel_VIsual_mode == NUL) /* there is none */
7129 {
7130 beep_flush();
7131 return;
7132 }
7133 VIsual = curwin->w_cursor;
7134
7135 VIsual_active = TRUE;
7136 VIsual_reselect = TRUE;
7137 if (!cap->arg)
7138 /* start Select mode when 'selectmode' contains "cmd" */
7139 may_start_select('c');
7140#ifdef FEAT_MOUSE
7141 setmouse();
7142#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007143 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 redraw_cmdline = TRUE; /* show visual mode later */
7145 /*
7146 * For V and ^V, we multiply the number of lines even if there
7147 * was only one -- webb
7148 */
7149 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7150 {
7151 curwin->w_cursor.lnum +=
7152 resel_VIsual_line_count * cap->count0 - 1;
7153 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7154 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7155 }
7156 VIsual_mode = resel_VIsual_mode;
7157 if (VIsual_mode == 'v')
7158 {
7159 if (resel_VIsual_line_count <= 1)
7160 curwin->w_cursor.col += resel_VIsual_col * cap->count0 - 1;
7161 else
7162 curwin->w_cursor.col = resel_VIsual_col;
7163 check_cursor_col();
7164 }
7165 if (resel_VIsual_col == MAXCOL)
7166 {
7167 curwin->w_curswant = MAXCOL;
7168 coladvance((colnr_T)MAXCOL);
7169 }
7170 else if (VIsual_mode == Ctrl_V)
7171 {
7172 validate_virtcol();
7173 curwin->w_curswant = curwin->w_virtcol
7174 + resel_VIsual_col * cap->count0 - 1;
7175 coladvance(curwin->w_curswant);
7176 }
7177 else
7178 curwin->w_set_curswant = TRUE;
7179 redraw_curbuf_later(INVERTED); /* show the inversion */
7180 }
7181 else
7182 {
7183 if (!cap->arg)
7184 /* start Select mode when 'selectmode' contains "cmd" */
7185 may_start_select('c');
7186 n_start_visual_mode(cap->cmdchar);
7187 }
7188 }
7189}
7190
7191/*
7192 * Start selection for Shift-movement keys.
7193 */
7194 void
7195start_selection()
7196{
7197 /* if 'selectmode' contains "key", start Select mode */
7198 may_start_select('k');
7199 n_start_visual_mode('v');
7200}
7201
7202/*
7203 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7204 */
7205 void
7206may_start_select(c)
7207 int c;
7208{
7209 VIsual_select = (stuff_empty() && typebuf_typed()
7210 && (vim_strchr(p_slm, c) != NULL));
7211}
7212
7213/*
7214 * Start Visual mode "c".
7215 * Should set VIsual_select before calling this.
7216 */
7217 static void
7218n_start_visual_mode(c)
7219 int c;
7220{
7221 VIsual_mode = c;
7222 VIsual_active = TRUE;
7223 VIsual_reselect = TRUE;
7224#ifdef FEAT_VIRTUALEDIT
7225 /* Corner case: the 0 position in a tab may change when going into
7226 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7227 */
7228 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
7229 coladvance(curwin->w_virtcol);
7230#endif
7231 VIsual = curwin->w_cursor;
7232
7233#ifdef FEAT_FOLDING
7234 foldAdjustVisual();
7235#endif
7236
7237#ifdef FEAT_MOUSE
7238 setmouse();
7239#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007240 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 redraw_cmdline = TRUE; /* show visual mode later */
7242#ifdef FEAT_CLIPBOARD
7243 /* Make sure the clipboard gets updated. Needed because start and
7244 * end may still be the same, and the selection needs to be owned */
7245 clip_star.vmode = NUL;
7246#endif
7247
7248 /* Only need to redraw this line, unless still need to redraw an old
7249 * Visual area (when 'lazyredraw' is set). */
7250 if (curwin->w_redr_type < INVERTED)
7251 {
7252 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7253 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7254 }
7255}
7256
7257#endif /* FEAT_VISUAL */
7258
7259/*
7260 * CTRL-W: Window commands
7261 */
7262 static void
7263nv_window(cap)
7264 cmdarg_T *cap;
7265{
7266#ifdef FEAT_WINDOWS
7267 if (!checkclearop(cap->oap))
7268 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7269#else
7270 (void)checkclearop(cap->oap);
7271#endif
7272}
7273
7274/*
7275 * CTRL-Z: Suspend
7276 */
7277 static void
7278nv_suspend(cap)
7279 cmdarg_T *cap;
7280{
7281 clearop(cap->oap);
7282#ifdef FEAT_VISUAL
7283 if (VIsual_active)
7284 end_visual_mode(); /* stop Visual mode */
7285#endif
7286 do_cmdline_cmd((char_u *)"st");
7287}
7288
7289/*
7290 * Commands starting with "g".
7291 */
7292 static void
7293nv_g_cmd(cap)
7294 cmdarg_T *cap;
7295{
7296 oparg_T *oap = cap->oap;
7297#ifdef FEAT_VISUAL
7298 pos_T tpos;
7299#endif
7300 int i;
7301 int flag = FALSE;
7302
7303 switch (cap->nchar)
7304 {
7305#ifdef MEM_PROFILE
7306 /*
7307 * "g^A": dump log of used memory.
7308 */
7309 case Ctrl_A:
7310 vim_mem_profile_dump();
7311 break;
7312#endif
7313
7314#ifdef FEAT_VREPLACE
7315 /*
7316 * "gR": Enter virtual replace mode.
7317 */
7318 case 'R':
7319 cap->arg = TRUE;
7320 nv_Replace(cap);
7321 break;
7322
7323 case 'r':
7324 nv_vreplace(cap);
7325 break;
7326#endif
7327
7328 case '&':
7329 do_cmdline_cmd((char_u *)"%s//~/&");
7330 break;
7331
7332#ifdef FEAT_VISUAL
7333 /*
7334 * "gv": Reselect the previous Visual area. If Visual already active,
7335 * exchange previous and current Visual area.
7336 */
7337 case 'v':
7338 if (checkclearop(oap))
7339 break;
7340
7341 if ( curbuf->b_visual_start.lnum == 0
7342 || curbuf->b_visual_start.lnum > curbuf->b_ml.ml_line_count
7343 || curbuf->b_visual_end.lnum == 0)
7344 beep_flush();
7345 else
7346 {
7347 /* set w_cursor to the start of the Visual area, tpos to the end */
7348 if (VIsual_active)
7349 {
7350 i = VIsual_mode;
7351 VIsual_mode = curbuf->b_visual_mode;
7352 curbuf->b_visual_mode = i;
7353# ifdef FEAT_EVAL
7354 curbuf->b_visual_mode_eval = i;
7355# endif
7356 i = curwin->w_curswant;
7357 curwin->w_curswant = curbuf->b_visual_curswant;
7358 curbuf->b_visual_curswant = i;
7359
7360 tpos = curbuf->b_visual_end;
7361 curbuf->b_visual_end = curwin->w_cursor;
7362 curwin->w_cursor = curbuf->b_visual_start;
7363 curbuf->b_visual_start = VIsual;
7364 }
7365 else
7366 {
7367 VIsual_mode = curbuf->b_visual_mode;
7368 curwin->w_curswant = curbuf->b_visual_curswant;
7369 tpos = curbuf->b_visual_end;
7370 curwin->w_cursor = curbuf->b_visual_start;
7371 }
7372
7373 VIsual_active = TRUE;
7374 VIsual_reselect = TRUE;
7375
7376 /* Set Visual to the start and w_cursor to the end of the Visual
7377 * area. Make sure they are on an existing character. */
7378 check_cursor();
7379 VIsual = curwin->w_cursor;
7380 curwin->w_cursor = tpos;
7381 check_cursor();
7382 update_topline();
7383 /*
7384 * When called from normal "g" command: start Select mode when
7385 * 'selectmode' contains "cmd". When called for K_SELECT, always
7386 * start Select mode.
7387 */
7388 if (cap->arg)
7389 VIsual_select = TRUE;
7390 else
7391 may_start_select('c');
7392#ifdef FEAT_MOUSE
7393 setmouse();
7394#endif
7395#ifdef FEAT_CLIPBOARD
7396 /* Make sure the clipboard gets updated. Needed because start and
7397 * end are still the same, and the selection needs to be owned */
7398 clip_star.vmode = NUL;
7399#endif
7400 redraw_curbuf_later(INVERTED);
7401 showmode();
7402 }
7403 break;
7404 /*
7405 * "gV": Don't reselect the previous Visual area after a Select mode
7406 * mapping of menu.
7407 */
7408 case 'V':
7409 VIsual_reselect = FALSE;
7410 break;
7411
7412 /*
7413 * "gh": start Select mode.
7414 * "gH": start Select line mode.
7415 * "g^H": start Select block mode.
7416 */
7417 case K_BS:
7418 cap->nchar = Ctrl_H;
7419 /* FALLTHROUGH */
7420 case 'h':
7421 case 'H':
7422 case Ctrl_H:
7423# ifdef EBCDIC
7424 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7425 if (cap->nchar == Ctrl_H)
7426 cap->cmdchar = Ctrl_V;
7427 else
7428# endif
7429 cap->cmdchar = cap->nchar + ('v' - 'h');
7430 cap->arg = TRUE;
7431 nv_visual(cap);
7432 break;
7433#endif /* FEAT_VISUAL */
7434
7435 /*
7436 * "gj" and "gk" two new funny movement keys -- up and down
7437 * movement based on *screen* line rather than *file* line.
7438 */
7439 case 'j':
7440 case K_DOWN:
7441 /* with 'nowrap' it works just like the normal "j" command; also when
7442 * in a closed fold */
7443 if (!curwin->w_p_wrap
7444#ifdef FEAT_FOLDING
7445 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7446#endif
7447 )
7448 {
7449 oap->motion_type = MLINE;
7450 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7451 }
7452 else
7453 i = nv_screengo(oap, FORWARD, cap->count1);
7454 if (i == FAIL)
7455 clearopbeep(oap);
7456 break;
7457
7458 case 'k':
7459 case K_UP:
7460 /* with 'nowrap' it works just like the normal "k" command; also when
7461 * in a closed fold */
7462 if (!curwin->w_p_wrap
7463#ifdef FEAT_FOLDING
7464 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7465#endif
7466 )
7467 {
7468 oap->motion_type = MLINE;
7469 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7470 }
7471 else
7472 i = nv_screengo(oap, BACKWARD, cap->count1);
7473 if (i == FAIL)
7474 clearopbeep(oap);
7475 break;
7476
7477 /*
7478 * "gJ": join two lines without inserting a space.
7479 */
7480 case 'J':
7481 nv_join(cap);
7482 break;
7483
7484 /*
7485 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7486 * "gm": middle of "g0" and "g$".
7487 */
7488 case '^':
7489 flag = TRUE;
7490 /* FALLTHROUGH */
7491
7492 case '0':
7493 case 'm':
7494 case K_HOME:
7495 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 oap->motion_type = MCHAR;
7497 oap->inclusive = FALSE;
7498 if (curwin->w_p_wrap
7499#ifdef FEAT_VERTSPLIT
7500 && curwin->w_width != 0
7501#endif
7502 )
7503 {
7504 int width1 = W_WIDTH(curwin) - curwin_col_off();
7505 int width2 = width1 + curwin_col_off2();
7506
7507 validate_virtcol();
7508 i = 0;
7509 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7510 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7511 }
7512 else
7513 i = curwin->w_leftcol;
7514 /* Go to the middle of the screen line. When 'number' is on and lines
7515 * are wrapping the middle can be more to the left.*/
7516 if (cap->nchar == 'm')
7517 i += (W_WIDTH(curwin) - curwin_col_off()
7518 + ((curwin->w_p_wrap && i > 0)
7519 ? curwin_col_off2() : 0)) / 2;
7520 coladvance((colnr_T)i);
7521 if (flag)
7522 {
7523 do
7524 i = gchar_cursor();
7525 while (vim_iswhite(i) && oneright() == OK);
7526 }
7527 curwin->w_set_curswant = TRUE;
7528 break;
7529
7530 case '_':
7531 /* "g_": to the last non-blank character in the line or <count> lines
7532 * downward. */
7533 cap->oap->motion_type = MCHAR;
7534 cap->oap->inclusive = TRUE;
7535 curwin->w_curswant = MAXCOL;
7536 if (cursor_down((long)(cap->count1 - 1),
7537 cap->oap->op_type == OP_NOP) == FAIL)
7538 clearopbeep(cap->oap);
7539 else
7540 {
7541 char_u *ptr = ml_get_curline();
7542
7543 /* In Visual mode we may end up after the line. */
7544 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
7545 --curwin->w_cursor.col;
7546
7547 /* Decrease the cursor column until it's on a non-blank. */
7548 while (curwin->w_cursor.col > 0
7549 && vim_iswhite(ptr[curwin->w_cursor.col]))
7550 --curwin->w_cursor.col;
7551 curwin->w_set_curswant = TRUE;
7552 }
7553 break;
7554
7555 case '$':
7556 case K_END:
7557 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 {
7559 int col_off = curwin_col_off();
7560
7561 oap->motion_type = MCHAR;
7562 oap->inclusive = TRUE;
7563 if (curwin->w_p_wrap
7564#ifdef FEAT_VERTSPLIT
7565 && curwin->w_width != 0
7566#endif
7567 )
7568 {
7569 curwin->w_curswant = MAXCOL; /* so we stay at the end */
7570 if (cap->count1 == 1)
7571 {
7572 int width1 = W_WIDTH(curwin) - col_off;
7573 int width2 = width1 + curwin_col_off2();
7574
7575 validate_virtcol();
7576 i = width1 - 1;
7577 if (curwin->w_virtcol >= (colnr_T)width1)
7578 i += ((curwin->w_virtcol - width1) / width2 + 1)
7579 * width2;
7580 coladvance((colnr_T)i);
7581#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7582 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
7583 {
7584 /*
7585 * Check for landing on a character that got split at
7586 * the end of the line. We do not want to advance to
7587 * the next screen line.
7588 */
7589 validate_virtcol();
7590 if (curwin->w_virtcol > (colnr_T)i)
7591 --curwin->w_cursor.col;
7592 }
7593#endif
7594 }
7595 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
7596 clearopbeep(oap);
7597 }
7598 else
7599 {
7600 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
7601 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007602
7603 /* Make sure we stick in this column. */
7604 validate_virtcol();
7605 curwin->w_curswant = curwin->w_virtcol;
7606 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608 }
7609 break;
7610
7611 /*
7612 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7613 */
7614 case '*':
7615 case '#':
7616#if POUND != '#'
7617 case POUND: /* pound sign (sometimes equal to '#') */
7618#endif
7619 case Ctrl_RSB: /* :tag or :tselect for current identifier */
7620 case ']': /* :tselect for current identifier */
7621 nv_ident(cap);
7622 break;
7623
7624 /*
7625 * ge and gE: go back to end of word
7626 */
7627 case 'e':
7628 case 'E':
7629 oap->motion_type = MCHAR;
7630 curwin->w_set_curswant = TRUE;
7631 oap->inclusive = TRUE;
7632 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
7633 clearopbeep(oap);
7634 break;
7635
7636 /*
7637 * "g CTRL-G": display info about cursor position
7638 */
7639 case Ctrl_G:
7640 cursor_pos_info();
7641 break;
7642
7643 /*
7644 * "gi": start Insert at the last position.
7645 */
7646 case 'i':
7647 if (curbuf->b_last_insert.lnum != 0)
7648 {
7649 curwin->w_cursor = curbuf->b_last_insert;
7650 check_cursor_lnum();
7651 i = (int)STRLEN(ml_get_curline());
7652 if (curwin->w_cursor.col > (colnr_T)i)
7653 {
7654#ifdef FEAT_VIRTUALEDIT
7655 if (virtual_active())
7656 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
7657#endif
7658 curwin->w_cursor.col = i;
7659 }
7660 }
7661 cap->cmdchar = 'i';
7662 nv_edit(cap);
7663 break;
7664
7665 /*
7666 * "gI": Start insert in column 1.
7667 */
7668 case 'I':
7669 beginline(0);
7670 if (!checkclearopq(oap))
7671 invoke_edit(cap, FALSE, 'g', FALSE);
7672 break;
7673
7674#ifdef FEAT_SEARCHPATH
7675 /*
7676 * "gf": goto file, edit file under cursor
7677 * "]f" and "[f": can also be used.
7678 */
7679 case 'f':
7680 nv_gotofile(cap);
7681 break;
7682#endif
7683
7684 /* "g'm" and "g`m": jump to mark without setting pcmark */
7685 case '\'':
7686 cap->arg = TRUE;
7687 /*FALLTHROUGH*/
7688 case '`':
7689 nv_gomark(cap);
7690 break;
7691
7692 /*
7693 * "gs": Goto sleep.
7694 */
7695 case 's':
7696 do_sleep(cap->count1 * 1000L);
7697 break;
7698
7699 /*
7700 * "ga": Display the ascii value of the character under the
7701 * cursor. It is displayed in decimal, hex, and octal. -- webb
7702 */
7703 case 'a':
7704 do_ascii(NULL);
7705 break;
7706
7707#ifdef FEAT_MBYTE
7708 /*
7709 * "g8": Display the bytes used for the UTF-8 character under the
7710 * cursor. It is displayed in hex.
7711 */
7712 case '8':
7713 show_utf8();
7714 break;
7715#endif
7716
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007717 case '<':
7718 show_sb_text();
7719 break;
7720
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 /*
7722 * "gg": Goto the first line in file. With a count it goes to
7723 * that line number like for "G". -- webb
7724 */
7725 case 'g':
7726 cap->arg = FALSE;
7727 nv_goto(cap);
7728 break;
7729
7730 /*
7731 * Two-character operators:
7732 * "gq" Format text
7733 * "gw" Format text and keep cursor position
7734 * "g~" Toggle the case of the text.
7735 * "gu" Change text to lower case.
7736 * "gU" Change text to upper case.
7737 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007738 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 */
7740 case 'q':
7741 case 'w':
7742 oap->cursor_start = curwin->w_cursor;
7743 /*FALLTHROUGH*/
7744 case '~':
7745 case 'u':
7746 case 'U':
7747 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007748 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 nv_operator(cap);
7750 break;
7751
7752 /*
7753 * "gd": Find first occurence of pattern under the cursor in the
7754 * current function
7755 * "gD": idem, but in the current file.
7756 */
7757 case 'd':
7758 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007759 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 break;
7761
7762#ifdef FEAT_MOUSE
7763 /*
7764 * g<*Mouse> : <C-*mouse>
7765 */
7766 case K_MIDDLEMOUSE:
7767 case K_MIDDLEDRAG:
7768 case K_MIDDLERELEASE:
7769 case K_LEFTMOUSE:
7770 case K_LEFTDRAG:
7771 case K_LEFTRELEASE:
7772 case K_RIGHTMOUSE:
7773 case K_RIGHTDRAG:
7774 case K_RIGHTRELEASE:
7775 case K_X1MOUSE:
7776 case K_X1DRAG:
7777 case K_X1RELEASE:
7778 case K_X2MOUSE:
7779 case K_X2DRAG:
7780 case K_X2RELEASE:
7781 mod_mask = MOD_MASK_CTRL;
7782 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7783 break;
7784#endif
7785
7786 case K_IGNORE:
7787 break;
7788
7789 /*
7790 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7791 */
7792 case 'p':
7793 case 'P':
7794 nv_put(cap);
7795 break;
7796
7797#ifdef FEAT_BYTEOFF
7798 /* "go": goto byte count from start of buffer */
7799 case 'o':
7800 goto_byte(cap->count0);
7801 break;
7802#endif
7803
7804 /* "gQ": improved Ex mode */
7805 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007806 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
7808 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007809 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 break;
7811 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 if (!checkclearopq(oap))
7814 do_exmode(TRUE);
7815 break;
7816
7817#ifdef FEAT_JUMPLIST
7818 case ',':
7819 nv_pcmark(cap);
7820 break;
7821
7822 case ';':
7823 cap->count1 = -cap->count1;
7824 nv_pcmark(cap);
7825 break;
7826#endif
7827
7828 default:
7829 clearopbeep(oap);
7830 break;
7831 }
7832}
7833
7834/*
7835 * Handle "o" and "O" commands.
7836 */
7837 static void
7838n_opencmd(cap)
7839 cmdarg_T *cap;
7840{
7841 if (!checkclearopq(cap->oap))
7842 {
7843#ifdef FEAT_FOLDING
7844 if (cap->cmdchar == 'O')
7845 /* Open above the first line of a folded sequence of lines */
7846 (void)hasFolding(curwin->w_cursor.lnum,
7847 &curwin->w_cursor.lnum, NULL);
7848 else
7849 /* Open below the last line of a folded sequence of lines */
7850 (void)hasFolding(curwin->w_cursor.lnum,
7851 NULL, &curwin->w_cursor.lnum);
7852#endif
7853 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7854 (cap->cmdchar == 'O' ? 1 : 0)),
7855 (linenr_T)(curwin->w_cursor.lnum +
7856 (cap->cmdchar == 'o' ? 1 : 0))
7857 ) == OK
7858 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7859#ifdef FEAT_COMMENTS
7860 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7861#endif
7862 0, 0))
7863 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007864 /* When '#' is in 'cpoptions' ignore the count. */
7865 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7866 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7868 }
7869 }
7870}
7871
7872/*
7873 * "." command: redo last change.
7874 */
7875 static void
7876nv_dot(cap)
7877 cmdarg_T *cap;
7878{
7879 if (!checkclearopq(cap->oap))
7880 {
7881 /*
7882 * If "restart_edit" is TRUE, the last but one command is repeated
7883 * instead of the last command (inserting text). This is used for
7884 * CTRL-O <.> in insert mode.
7885 */
7886 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7887 clearopbeep(cap->oap);
7888 }
7889}
7890
7891/*
7892 * CTRL-R: undo undo
7893 */
7894 static void
7895nv_redo(cap)
7896 cmdarg_T *cap;
7897{
7898 if (!checkclearopq(cap->oap))
7899 {
7900 u_redo((int)cap->count1);
7901 curwin->w_set_curswant = TRUE;
7902 }
7903}
7904
7905/*
7906 * Handle "U" command.
7907 */
7908 static void
7909nv_Undo(cap)
7910 cmdarg_T *cap;
7911{
7912 /* In Visual mode and typing "gUU" triggers an operator */
7913 if (cap->oap->op_type == OP_UPPER
7914#ifdef FEAT_VISUAL
7915 || VIsual_active
7916#endif
7917 )
7918 {
7919 /* translate "gUU" to "gUgU" */
7920 cap->cmdchar = 'g';
7921 cap->nchar = 'U';
7922 nv_operator(cap);
7923 }
7924 else if (!checkclearopq(cap->oap))
7925 {
7926 u_undoline();
7927 curwin->w_set_curswant = TRUE;
7928 }
7929}
7930
7931/*
7932 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7933 * single character.
7934 */
7935 static void
7936nv_tilde(cap)
7937 cmdarg_T *cap;
7938{
7939 if (!p_to
7940#ifdef FEAT_VISUAL
7941 && !VIsual_active
7942#endif
7943 && cap->oap->op_type != OP_TILDE)
7944 n_swapchar(cap);
7945 else
7946 nv_operator(cap);
7947}
7948
7949/*
7950 * Handle an operator command.
7951 * The actual work is done by do_pending_operator().
7952 */
7953 static void
7954nv_operator(cap)
7955 cmdarg_T *cap;
7956{
7957 int op_type;
7958
7959 op_type = get_op_type(cap->cmdchar, cap->nchar);
7960
7961 if (op_type == cap->oap->op_type) /* double operator works on lines */
7962 nv_lineop(cap);
7963 else if (!checkclearop(cap->oap))
7964 {
7965 cap->oap->start = curwin->w_cursor;
7966 cap->oap->op_type = op_type;
7967 }
7968}
7969
7970/*
7971 * Handle linewise operator "dd", "yy", etc.
7972 *
7973 * "_" is is a strange motion command that helps make operators more logical.
7974 * It is actually implemented, but not documented in the real Vi. This motion
7975 * command actually refers to "the current line". Commands like "dd" and "yy"
7976 * are really an alternate form of "d_" and "y_". It does accept a count, so
7977 * "d3_" works to delete 3 lines.
7978 */
7979 static void
7980nv_lineop(cap)
7981 cmdarg_T *cap;
7982{
7983 cap->oap->motion_type = MLINE;
7984 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7985 clearopbeep(cap->oap);
7986 else if ( cap->oap->op_type == OP_DELETE
7987 || cap->oap->op_type == OP_LSHIFT
7988 || cap->oap->op_type == OP_RSHIFT)
7989 beginline(BL_SOL | BL_FIX);
7990 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7991 beginline(BL_WHITE | BL_FIX);
7992}
7993
7994/*
7995 * <Home> command.
7996 */
7997 static void
7998nv_home(cap)
7999 cmdarg_T *cap;
8000{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008001 /* CTRL-HOME is like "gg" */
8002 if (mod_mask & MOD_MASK_CTRL)
8003 nv_goto(cap);
8004 else
8005 {
8006 cap->count0 = 1;
8007 nv_pipe(cap);
8008 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008009 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8010 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011}
8012
8013/*
8014 * "|" command.
8015 */
8016 static void
8017nv_pipe(cap)
8018 cmdarg_T *cap;
8019{
8020 cap->oap->motion_type = MCHAR;
8021 cap->oap->inclusive = FALSE;
8022 beginline(0);
8023 if (cap->count0 > 0)
8024 {
8025 coladvance((colnr_T)(cap->count0 - 1));
8026 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8027 }
8028 else
8029 curwin->w_curswant = 0;
8030 /* keep curswant at the column where we wanted to go, not where
8031 we ended; differs if line is too short */
8032 curwin->w_set_curswant = FALSE;
8033}
8034
8035/*
8036 * Handle back-word command "b" and "B".
8037 * cap->arg is 1 for "B"
8038 */
8039 static void
8040nv_bck_word(cap)
8041 cmdarg_T *cap;
8042{
8043 cap->oap->motion_type = MCHAR;
8044 cap->oap->inclusive = FALSE;
8045 curwin->w_set_curswant = TRUE;
8046 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8047 clearopbeep(cap->oap);
8048#ifdef FEAT_FOLDING
8049 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8050 foldOpenCursor();
8051#endif
8052}
8053
8054/*
8055 * Handle word motion commands "e", "E", "w" and "W".
8056 * cap->arg is TRUE for "E" and "W".
8057 */
8058 static void
8059nv_wordcmd(cap)
8060 cmdarg_T *cap;
8061{
8062 int n;
8063 int word_end;
8064 int flag = FALSE;
8065
8066 /*
8067 * Set inclusive for the "E" and "e" command.
8068 */
8069 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8070 word_end = TRUE;
8071 else
8072 word_end = FALSE;
8073 cap->oap->inclusive = word_end;
8074
8075 /*
8076 * "cw" and "cW" are a special case.
8077 */
8078 if (!word_end && cap->oap->op_type == OP_CHANGE)
8079 {
8080 n = gchar_cursor();
8081 if (n != NUL) /* not an empty line */
8082 {
8083 if (vim_iswhite(n))
8084 {
8085 /*
8086 * Reproduce a funny Vi behaviour: "cw" on a blank only
8087 * changes one character, not all blanks until the start of
8088 * the next word. Only do this when the 'w' flag is included
8089 * in 'cpoptions'.
8090 */
8091 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8092 {
8093 cap->oap->inclusive = TRUE;
8094 cap->oap->motion_type = MCHAR;
8095 return;
8096 }
8097 }
8098 else
8099 {
8100 /*
8101 * This is a little strange. To match what the real Vi does,
8102 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8103 * that we are not on a space or a TAB. This seems impolite
8104 * at first, but it's really more what we mean when we say
8105 * 'cw'.
8106 * Another strangeness: When standing on the end of a word
8107 * "ce" will change until the end of the next wordt, but "cw"
8108 * will change only one character! This is done by setting
8109 * flag.
8110 */
8111 cap->oap->inclusive = TRUE;
8112 word_end = TRUE;
8113 flag = TRUE;
8114 }
8115 }
8116 }
8117
8118 cap->oap->motion_type = MCHAR;
8119 curwin->w_set_curswant = TRUE;
8120 if (word_end)
8121 n = end_word(cap->count1, cap->arg, flag, FALSE);
8122 else
8123 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8124
8125 /* Don't leave the cursor on the NUL past a line */
8126 if (curwin->w_cursor.col && gchar_cursor() == NUL)
8127 {
8128 --curwin->w_cursor.col;
8129 cap->oap->inclusive = TRUE;
8130 }
8131
8132 if (n == FAIL && cap->oap->op_type == OP_NOP)
8133 clearopbeep(cap->oap);
8134 else
8135 {
8136#ifdef FEAT_VISUAL
8137 adjust_for_sel(cap);
8138#endif
8139#ifdef FEAT_FOLDING
8140 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8141 foldOpenCursor();
8142#endif
8143 }
8144}
8145
8146/*
8147 * "0" and "^" commands.
8148 * cap->arg is the argument for beginline().
8149 */
8150 static void
8151nv_beginline(cap)
8152 cmdarg_T *cap;
8153{
8154 cap->oap->motion_type = MCHAR;
8155 cap->oap->inclusive = FALSE;
8156 beginline(cap->arg);
8157#ifdef FEAT_FOLDING
8158 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8159 foldOpenCursor();
8160#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008161 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8162 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163}
8164
8165#ifdef FEAT_VISUAL
8166/*
8167 * In exclusive Visual mode, may include the last character.
8168 */
8169 static void
8170adjust_for_sel(cap)
8171 cmdarg_T *cap;
8172{
8173 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8174 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8175 {
8176# ifdef FEAT_MBYTE
8177 if (has_mbyte)
8178 inc_cursor();
8179 else
8180# endif
8181 ++curwin->w_cursor.col;
8182 cap->oap->inclusive = FALSE;
8183 }
8184}
8185
8186/*
8187 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8188 * Should check VIsual_mode before calling this.
8189 * Returns TRUE when backed up to the previous line.
8190 */
8191 static int
8192unadjust_for_sel()
8193{
8194 pos_T *pp;
8195
8196 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8197 {
8198 if (lt(VIsual, curwin->w_cursor))
8199 pp = &curwin->w_cursor;
8200 else
8201 pp = &VIsual;
8202#ifdef FEAT_VIRTUALEDIT
8203 if (pp->coladd > 0)
8204 --pp->coladd;
8205 else
8206#endif
8207 if (pp->col > 0)
8208 {
8209 --pp->col;
8210#ifdef FEAT_MBYTE
8211 mb_adjustpos(pp);
8212#endif
8213 }
8214 else if (pp->lnum > 1)
8215 {
8216 --pp->lnum;
8217 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8218 return TRUE;
8219 }
8220 }
8221 return FALSE;
8222}
8223
8224/*
8225 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8226 */
8227 static void
8228nv_select(cap)
8229 cmdarg_T *cap;
8230{
8231 if (VIsual_active)
8232 VIsual_select = TRUE;
8233 else if (VIsual_reselect)
8234 {
8235 cap->nchar = 'v'; /* fake "gv" command */
8236 cap->arg = TRUE;
8237 nv_g_cmd(cap);
8238 }
8239}
8240
8241#endif
8242
8243/*
8244 * "G", "gg", CTRL-END, CTRL-HOME.
8245 * cap->arg is TRUE for "G".
8246 */
8247 static void
8248nv_goto(cap)
8249 cmdarg_T *cap;
8250{
8251 linenr_T lnum;
8252
8253 if (cap->arg)
8254 lnum = curbuf->b_ml.ml_line_count;
8255 else
8256 lnum = 1L;
8257 cap->oap->motion_type = MLINE;
8258 setpcmark();
8259
8260 /* When a count is given, use it instead of the default lnum */
8261 if (cap->count0 != 0)
8262 lnum = cap->count0;
8263 if (lnum < 1L)
8264 lnum = 1L;
8265 else if (lnum > curbuf->b_ml.ml_line_count)
8266 lnum = curbuf->b_ml.ml_line_count;
8267 curwin->w_cursor.lnum = lnum;
8268 beginline(BL_SOL | BL_FIX);
8269#ifdef FEAT_FOLDING
8270 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8271 foldOpenCursor();
8272#endif
8273}
8274
8275/*
8276 * CTRL-\ in Normal mode.
8277 */
8278 static void
8279nv_normal(cap)
8280 cmdarg_T *cap;
8281{
8282 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8283 {
8284 clearop(cap->oap);
Bram Moolenaar09df3122006-01-23 22:23:09 +00008285 if (restart_edit != 0 && p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 clear_cmdline = TRUE; /* unshow mode later */
8287 restart_edit = 0;
8288#ifdef FEAT_CMDWIN
8289 if (cmdwin_type != 0)
8290 cmdwin_result = Ctrl_C;
8291#endif
8292#ifdef FEAT_VISUAL
8293 if (VIsual_active)
8294 {
8295 end_visual_mode(); /* stop Visual */
8296 redraw_curbuf_later(INVERTED);
8297 }
8298#endif
8299 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8300 if (cap->nchar == Ctrl_G && p_im)
8301 restart_edit = 'a';
8302 }
8303 else
8304 clearopbeep(cap->oap);
8305}
8306
8307/*
8308 * ESC in Normal mode: beep, but don't flush buffers.
8309 * Don't even beep if we are canceling a command.
8310 */
8311 static void
8312nv_esc(cap)
8313 cmdarg_T *cap;
8314{
8315 int no_reason;
8316
8317 no_reason = (cap->oap->op_type == OP_NOP
8318 && cap->opcount == 0
8319 && cap->count0 == 0
8320 && cap->oap->regname == 0
8321 && !p_im);
8322
8323 if (cap->arg) /* TRUE for CTRL-C */
8324 {
8325 if (restart_edit == 0
8326#ifdef FEAT_CMDWIN
8327 && cmdwin_type == 0
8328#endif
8329#ifdef FEAT_VISUAL
8330 && !VIsual_active
8331#endif
8332 && no_reason)
8333 MSG(_("Type :quit<Enter> to exit Vim"));
8334
8335 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8336 * set again below when halfway a mapping. */
8337 if (!p_im)
8338 restart_edit = 0;
8339#ifdef FEAT_CMDWIN
8340 if (cmdwin_type != 0)
8341 {
8342 cmdwin_result = K_IGNORE;
8343 got_int = FALSE; /* don't stop executing autocommands et al. */
8344 return;
8345 }
8346#endif
8347 }
8348
8349#ifdef FEAT_VISUAL
8350 if (VIsual_active)
8351 {
8352 end_visual_mode(); /* stop Visual */
8353 check_cursor_col(); /* make sure cursor is not beyond EOL */
8354 curwin->w_set_curswant = TRUE;
8355 redraw_curbuf_later(INVERTED);
8356 }
8357 else
8358#endif
8359 if (no_reason)
8360 vim_beep();
8361 clearop(cap->oap);
8362
8363 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8364 * set return to Insert mode afterwards. */
8365 if (restart_edit == 0 && goto_im()
8366#ifdef FEAT_EX_EXTRA
8367 && ex_normal_busy == 0
8368#endif
8369 )
8370 restart_edit = 'a';
8371}
8372
8373/*
8374 * Handle "A", "a", "I", "i" and <Insert> commands.
8375 */
8376 static void
8377nv_edit(cap)
8378 cmdarg_T *cap;
8379{
8380 /* <Insert> is equal to "i" */
8381 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8382 cap->cmdchar = 'i';
8383
8384#ifdef FEAT_VISUAL
8385 /* in Visual mode "A" and "I" are an operator */
8386 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8387 v_visop(cap);
8388
8389 /* in Visual mode and after an operator "a" and "i" are for text objects */
8390 else
8391#endif
8392 if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8393 && (cap->oap->op_type != OP_NOP
8394#ifdef FEAT_VISUAL
8395 || VIsual_active
8396#endif
8397 ))
8398 {
8399#ifdef FEAT_TEXTOBJ
8400 nv_object(cap);
8401#else
8402 clearopbeep(cap->oap);
8403#endif
8404 }
8405 else if (!curbuf->b_p_ma && !p_im)
8406 {
8407 /* Only give this error when 'insertmode' is off. */
8408 EMSG(_(e_modifiable));
8409 clearop(cap->oap);
8410 }
8411 else if (!checkclearopq(cap->oap))
8412 {
8413 switch (cap->cmdchar)
8414 {
8415 case 'A': /* "A"ppend after the line */
8416 curwin->w_set_curswant = TRUE;
8417#ifdef FEAT_VIRTUALEDIT
8418 if (ve_flags == VE_ALL)
8419 {
8420 int save_State = State;
8421
8422 /* Pretent Insert mode here to allow the cursor on the
8423 * character past the end of the line */
8424 State = INSERT;
8425 coladvance((colnr_T)MAXCOL);
8426 State = save_State;
8427 }
8428 else
8429#endif
8430 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8431 break;
8432
8433 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008434 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8435 beginline(BL_WHITE);
8436 else
8437 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 break;
8439
8440 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8441#ifdef FEAT_VIRTUALEDIT
8442 /* increment coladd when in virtual space, increment the
8443 * column otherwise, also to append after an unprintable char */
8444 if (virtual_active()
8445 && (curwin->w_cursor.coladd > 0
8446 || *ml_get_cursor() == NUL
8447 || *ml_get_cursor() == TAB))
8448 curwin->w_cursor.coladd++;
8449 else
8450#endif
8451 if (*ml_get_cursor() != NUL)
8452 inc_cursor();
8453 break;
8454 }
8455
8456#ifdef FEAT_VIRTUALEDIT
8457 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
8458 {
8459 int save_State = State;
8460
8461 /* Pretent Insert mode here to allow the cursor on the
8462 * character past the end of the line */
8463 State = INSERT;
8464 coladvance(getviscol());
8465 State = save_State;
8466 }
8467#endif
8468
8469 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
8470 }
8471}
8472
8473/*
8474 * Invoke edit() and take care of "restart_edit" and the return value.
8475 */
8476 static void
8477invoke_edit(cap, repl, cmd, startln)
8478 cmdarg_T *cap;
8479 int repl; /* "r" or "gr" command */
8480 int cmd;
8481 int startln;
8482{
8483 int restart_edit_save = 0;
8484
8485 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8486 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8487 * it. */
8488 if (repl || !stuff_empty())
8489 restart_edit_save = restart_edit;
8490 else
8491 restart_edit_save = 0;
8492
8493 /* Always reset "restart_edit", this is not a restarted edit. */
8494 restart_edit = 0;
8495
8496 if (edit(cmd, startln, cap->count1))
8497 cap->retval |= CA_COMMAND_BUSY;
8498
8499 if (restart_edit == 0)
8500 restart_edit = restart_edit_save;
8501}
8502
8503#ifdef FEAT_TEXTOBJ
8504/*
8505 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8506 */
8507 static void
8508nv_object(cap)
8509 cmdarg_T *cap;
8510{
8511 int flag;
8512 int include;
8513 char_u *mps_save;
8514
8515 if (cap->cmdchar == 'i')
8516 include = FALSE; /* "ix" = inner object: exclude white space */
8517 else
8518 include = TRUE; /* "ax" = an object: include white space */
8519
8520 /* Make sure (), [], {} and <> are in 'matchpairs' */
8521 mps_save = curbuf->b_p_mps;
8522 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
8523
8524 switch (cap->nchar)
8525 {
8526 case 'w': /* "aw" = a word */
8527 flag = current_word(cap->oap, cap->count1, include, FALSE);
8528 break;
8529 case 'W': /* "aW" = a WORD */
8530 flag = current_word(cap->oap, cap->count1, include, TRUE);
8531 break;
8532 case 'b': /* "ab" = a braces block */
8533 case '(':
8534 case ')':
8535 flag = current_block(cap->oap, cap->count1, include, '(', ')');
8536 break;
8537 case 'B': /* "aB" = a Brackets block */
8538 case '{':
8539 case '}':
8540 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8541 break;
8542 case '[': /* "a[" = a [] block */
8543 case ']':
8544 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8545 break;
8546 case '<': /* "a<" = a <> block */
8547 case '>':
8548 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8549 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008550 case 't': /* "at" = a tag block (xml and html) */
8551 flag = current_tagblock(cap->oap, cap->count1, include);
8552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 case 'p': /* "ap" = a paragraph */
8554 flag = current_par(cap->oap, cap->count1, include, 'p');
8555 break;
8556 case 's': /* "as" = a sentence */
8557 flag = current_sent(cap->oap, cap->count1, include);
8558 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008559 case '"': /* "a"" = a double quoted string */
8560 case '\'': /* "a'" = a single quoted string */
8561 case '`': /* "a`" = a backtick quoted string */
8562 flag = current_quote(cap->oap, cap->count1, include,
8563 cap->nchar);
8564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565#if 0 /* TODO */
8566 case 'S': /* "aS" = a section */
8567 case 'f': /* "af" = a filename */
8568 case 'u': /* "au" = a URL */
8569#endif
8570 default:
8571 flag = FAIL;
8572 break;
8573 }
8574
8575 curbuf->b_p_mps = mps_save;
8576 if (flag == FAIL)
8577 clearopbeep(cap->oap);
8578 adjust_cursor_col();
8579 curwin->w_set_curswant = TRUE;
8580}
8581#endif
8582
8583/*
8584 * "q" command: Start/stop recording.
8585 * "q:", "q/", "q?": edit command-line in command-line window.
8586 */
8587 static void
8588nv_record(cap)
8589 cmdarg_T *cap;
8590{
8591 if (cap->oap->op_type == OP_FORMAT)
8592 {
8593 /* "gqq" is the same as "gqgq": format line */
8594 cap->cmdchar = 'g';
8595 cap->nchar = 'q';
8596 nv_operator(cap);
8597 }
8598 else if (!checkclearop(cap->oap))
8599 {
8600#ifdef FEAT_CMDWIN
8601 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8602 {
8603 stuffcharReadbuff(cap->nchar);
8604 stuffcharReadbuff(K_CMDWIN);
8605 }
8606 else
8607#endif
8608 /* (stop) recording into a named register, unless executing a
8609 * register */
8610 if (!Exec_reg && do_record(cap->nchar) == FAIL)
8611 clearopbeep(cap->oap);
8612 }
8613}
8614
8615/*
8616 * Handle the "@r" command.
8617 */
8618 static void
8619nv_at(cap)
8620 cmdarg_T *cap;
8621{
8622 if (checkclearop(cap->oap))
8623 return;
8624#ifdef FEAT_EVAL
8625 if (cap->nchar == '=')
8626 {
8627 if (get_expr_register() == NUL)
8628 return;
8629 }
8630#endif
8631 while (cap->count1-- && !got_int)
8632 {
8633 if (do_execreg(cap->nchar, FALSE, FALSE) == FAIL)
8634 {
8635 clearopbeep(cap->oap);
8636 break;
8637 }
8638 line_breakcheck();
8639 }
8640}
8641
8642/*
8643 * Handle the CTRL-U and CTRL-D commands.
8644 */
8645 static void
8646nv_halfpage(cap)
8647 cmdarg_T *cap;
8648{
8649 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8650 || (cap->cmdchar == Ctrl_D
8651 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8652 clearopbeep(cap->oap);
8653 else if (!checkclearop(cap->oap))
8654 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8655}
8656
8657/*
8658 * Handle "J" or "gJ" command.
8659 */
8660 static void
8661nv_join(cap)
8662 cmdarg_T *cap;
8663{
8664#ifdef FEAT_VISUAL
8665 if (VIsual_active) /* join the visual lines */
8666 nv_operator(cap);
8667 else
8668#endif
8669 if (!checkclearop(cap->oap))
8670 {
8671 if (cap->count0 <= 1)
8672 cap->count0 = 2; /* default for join is two lines! */
8673 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8674 curbuf->b_ml.ml_line_count)
8675 clearopbeep(cap->oap); /* beyond last line */
8676 else
8677 {
8678 prep_redo(cap->oap->regname, cap->count0,
8679 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8680 do_do_join(cap->count0, cap->nchar == NUL);
8681 }
8682 }
8683}
8684
8685/*
8686 * "P", "gP", "p" and "gp" commands.
8687 */
8688 static void
8689nv_put(cap)
8690 cmdarg_T *cap;
8691{
8692#ifdef FEAT_VISUAL
8693 int regname = 0;
8694 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008695 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008696 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697#endif
8698 int dir;
8699 int flags = 0;
8700
8701 if (cap->oap->op_type != OP_NOP)
8702 {
8703#ifdef FEAT_DIFF
8704 /* "dp" is ":diffput" */
8705 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8706 {
8707 clearop(cap->oap);
8708 nv_diffgetput(TRUE);
8709 }
8710 else
8711#endif
8712 clearopbeep(cap->oap);
8713 }
8714 else
8715 {
8716 dir = (cap->cmdchar == 'P'
8717 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
8718 ? BACKWARD : FORWARD;
8719 prep_redo_cmd(cap);
8720 if (cap->cmdchar == 'g')
8721 flags |= PUT_CURSEND;
8722
8723#ifdef FEAT_VISUAL
8724 if (VIsual_active)
8725 {
8726 /* Putting in Visual mode: The put text replaces the selected
8727 * text. First delete the selected text, then put the new text.
8728 * Need to save and restore the registers that the delete
8729 * overwrites if the old contents is being put.
8730 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008731 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 regname = cap->oap->regname;
8733# ifdef FEAT_CLIPBOARD
8734 adjust_clip_reg(&regname);
8735# endif
8736 if (regname == 0 || VIM_ISDIGIT(regname)
8737# ifdef FEAT_CLIPBOARD
8738 || (clip_unnamed && (regname == '*' || regname == '+'))
8739# endif
8740
8741 )
8742 {
8743 /* the delete is going to overwrite the register we want to
8744 * put, save it first. */
8745 reg1 = get_register(regname, TRUE);
8746 }
8747
8748 /* Now delete the selected text. */
8749 cap->cmdchar = 'd';
8750 cap->nchar = NUL;
8751 cap->oap->regname = NUL;
8752 nv_operator(cap);
8753 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008754 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
8756 /* delete PUT_LINE_BACKWARD; */
8757 cap->oap->regname = regname;
8758
8759 if (reg1 != NULL)
8760 {
8761 /* Delete probably changed the register we want to put, save
8762 * it first. Then put back what was there before the delete. */
8763 reg2 = get_register(regname, FALSE);
8764 put_register(regname, reg1);
8765 }
8766
8767 /* When deleted a linewise Visual area, put the register as
8768 * lines to avoid it joined with the next line. When deletion was
8769 * characterwise, split a line when putting lines. */
8770 if (VIsual_mode == 'V')
8771 flags |= PUT_LINE;
8772 else if (VIsual_mode == 'v')
8773 flags |= PUT_LINE_SPLIT;
8774 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8775 flags |= PUT_LINE_FORWARD;
8776 dir = BACKWARD;
8777 if ((VIsual_mode != 'V'
8778 && curwin->w_cursor.col < curbuf->b_op_start.col)
8779 || (VIsual_mode == 'V'
8780 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8781 /* cursor is at the end of the line or end of file, put
8782 * forward. */
8783 dir = FORWARD;
8784 }
8785#endif
8786 do_put(cap->oap->regname, dir, cap->count1, flags);
8787
8788#ifdef FEAT_VISUAL
8789 /* If a register was saved, put it back now. */
8790 if (reg2 != NULL)
8791 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008792
8793 /* What to reselect with "gv"? Selecting the just put text seems to
8794 * be the most useful, since the original text was removed. */
8795 if (was_visual)
8796 {
8797 curbuf->b_visual_start = curbuf->b_op_start;
8798 curbuf->b_visual_end = curbuf->b_op_end;
8799 }
8800
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008801 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008802 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008803 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
8804 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#endif
8806 auto_format(FALSE, TRUE);
8807 }
8808}
8809
8810/*
8811 * "o" and "O" commands.
8812 */
8813 static void
8814nv_open(cap)
8815 cmdarg_T *cap;
8816{
8817#ifdef FEAT_DIFF
8818 /* "do" is ":diffget" */
8819 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8820 {
8821 clearop(cap->oap);
8822 nv_diffgetput(FALSE);
8823 }
8824 else
8825#endif
8826#ifdef FEAT_VISUAL
8827 if (VIsual_active) /* switch start and end of visual */
8828 v_swap_corners(cap->cmdchar);
8829 else
8830#endif
8831 n_opencmd(cap);
8832}
8833
8834#ifdef FEAT_SNIFF
8835/*ARGSUSED*/
8836 static void
8837nv_sniff(cap)
8838 cmdarg_T *cap;
8839{
8840 ProcessSniffRequests();
8841}
8842#endif
8843
8844#ifdef FEAT_NETBEANS_INTG
8845 static void
8846nv_nbcmd(cap)
8847 cmdarg_T *cap;
8848{
8849 netbeans_keycommand(cap->nchar);
8850}
8851#endif
8852
8853#ifdef FEAT_DND
8854/*ARGSUSED*/
8855 static void
8856nv_drop(cap)
8857 cmdarg_T *cap;
8858{
8859 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8860}
8861#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008862
8863#ifdef FEAT_AUTOCMD
8864/*
8865 * Trigger CursorHold event.
8866 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8867 * input buffer. "did_cursorhold" is set to avoid retriggering.
8868 */
8869/*ARGSUSED*/
8870 static void
8871nv_cursorhold(cap)
8872 cmdarg_T *cap;
8873{
8874 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8875 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00008876 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008877}
8878#endif