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