blob: a8d74117c7e708b679b467b355cb567770363893 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
28static void set_vcount_ca __ARGS((cmdarg_T *cap, int *set_prevcount));
29#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000034 nv_compare __ARGS((const void *s1, const void *s2));
35static int find_command __ARGS((int cmdchar));
36static void op_colon __ARGS((oparg_T *oap));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +000037static void op_function __ARGS((oparg_T *oap));
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void find_start_of_word __ARGS((pos_T *));
40static void find_end_of_word __ARGS((pos_T *));
41static int get_mouse_class __ARGS((char_u *p));
42#endif
43static void prep_redo_cmd __ARGS((cmdarg_T *cap));
44static void prep_redo __ARGS((int regname, long, int, int, int, int, int));
45static int checkclearop __ARGS((oparg_T *oap));
46static int checkclearopq __ARGS((oparg_T *oap));
47static void clearop __ARGS((oparg_T *oap));
48static void clearopbeep __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000049static void unshift_special __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#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));
Bram Moolenaarba6c0522006-02-25 21:45:02 +000079static void nv_tabmenu __ARGS((cmdarg_T *cap));
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000081static void nv_exmode __ARGS((cmdarg_T *cap));
82static void nv_colon __ARGS((cmdarg_T *cap));
83static void nv_ctrlg __ARGS((cmdarg_T *cap));
84static void nv_ctrlh __ARGS((cmdarg_T *cap));
85static void nv_clear __ARGS((cmdarg_T *cap));
86static void nv_ctrlo __ARGS((cmdarg_T *cap));
87static void nv_hat __ARGS((cmdarg_T *cap));
88static void nv_Zet __ARGS((cmdarg_T *cap));
89static void nv_ident __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090static void nv_tagpop __ARGS((cmdarg_T *cap));
91static void nv_scroll __ARGS((cmdarg_T *cap));
92static void nv_right __ARGS((cmdarg_T *cap));
93static void nv_left __ARGS((cmdarg_T *cap));
94static void nv_up __ARGS((cmdarg_T *cap));
95static void nv_down __ARGS((cmdarg_T *cap));
96#ifdef FEAT_SEARCHPATH
97static void nv_gotofile __ARGS((cmdarg_T *cap));
98#endif
99static void nv_end __ARGS((cmdarg_T *cap));
100static void nv_dollar __ARGS((cmdarg_T *cap));
101static void nv_search __ARGS((cmdarg_T *cap));
102static void nv_next __ARGS((cmdarg_T *cap));
103static void normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
104static void nv_csearch __ARGS((cmdarg_T *cap));
105static void nv_brackets __ARGS((cmdarg_T *cap));
106static void nv_percent __ARGS((cmdarg_T *cap));
107static void nv_brace __ARGS((cmdarg_T *cap));
108static void nv_mark __ARGS((cmdarg_T *cap));
109static void nv_findpar __ARGS((cmdarg_T *cap));
110static void nv_undo __ARGS((cmdarg_T *cap));
111static void nv_kundo __ARGS((cmdarg_T *cap));
112static void nv_Replace __ARGS((cmdarg_T *cap));
113#ifdef FEAT_VREPLACE
114static void nv_vreplace __ARGS((cmdarg_T *cap));
115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void v_swap_corners __ARGS((int cmdchar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static void nv_replace __ARGS((cmdarg_T *cap));
118static void n_swapchar __ARGS((cmdarg_T *cap));
119static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static void v_visop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121static void nv_subst __ARGS((cmdarg_T *cap));
122static void nv_abbrev __ARGS((cmdarg_T *cap));
123static void nv_optrans __ARGS((cmdarg_T *cap));
124static void nv_gomark __ARGS((cmdarg_T *cap));
125static void nv_pcmark __ARGS((cmdarg_T *cap));
126static void nv_regname __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static void nv_visual __ARGS((cmdarg_T *cap));
128static void n_start_visual_mode __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void nv_window __ARGS((cmdarg_T *cap));
130static void nv_suspend __ARGS((cmdarg_T *cap));
131static void nv_g_cmd __ARGS((cmdarg_T *cap));
132static void n_opencmd __ARGS((cmdarg_T *cap));
133static void nv_dot __ARGS((cmdarg_T *cap));
134static void nv_redo __ARGS((cmdarg_T *cap));
135static void nv_Undo __ARGS((cmdarg_T *cap));
136static void nv_tilde __ARGS((cmdarg_T *cap));
137static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000138#ifdef FEAT_EVAL
139static void set_op_var __ARGS((int optype));
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static void nv_lineop __ARGS((cmdarg_T *cap));
142static void nv_home __ARGS((cmdarg_T *cap));
143static void nv_pipe __ARGS((cmdarg_T *cap));
144static void nv_bck_word __ARGS((cmdarg_T *cap));
145static void nv_wordcmd __ARGS((cmdarg_T *cap));
146static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000147static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void adjust_for_sel __ARGS((cmdarg_T *cap));
149static int unadjust_for_sel __ARGS((void));
150static void nv_select __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void nv_goto __ARGS((cmdarg_T *cap));
152static void nv_normal __ARGS((cmdarg_T *cap));
153static void nv_esc __ARGS((cmdarg_T *oap));
154static void nv_edit __ARGS((cmdarg_T *cap));
155static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
156#ifdef FEAT_TEXTOBJ
157static void nv_object __ARGS((cmdarg_T *cap));
158#endif
159static void nv_record __ARGS((cmdarg_T *cap));
160static void nv_at __ARGS((cmdarg_T *cap));
161static void nv_halfpage __ARGS((cmdarg_T *cap));
162static void nv_join __ARGS((cmdarg_T *cap));
163static void nv_put __ARGS((cmdarg_T *cap));
164static void nv_open __ARGS((cmdarg_T *cap));
165#ifdef FEAT_SNIFF
166static void nv_sniff __ARGS((cmdarg_T *cap));
167#endif
168#ifdef FEAT_NETBEANS_INTG
169static void nv_nbcmd __ARGS((cmdarg_T *cap));
170#endif
171#ifdef FEAT_DND
172static void nv_drop __ARGS((cmdarg_T *cap));
173#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#ifdef FEAT_AUTOCMD
175static void nv_cursorhold __ARGS((cmdarg_T *cap));
176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000178static char *e_noident = N_("E349: No identifier under cursor");
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * Function to be called for a Normal or Visual mode command.
182 * The argument is a cmdarg_T.
183 */
184typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
185
186/* Values for cmd_flags. */
187#define NV_NCH 0x01 /* may need to get a second char */
188#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
189#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
190#define NV_LANG 0x08 /* second char needs language adjustment */
191
192#define NV_SS 0x10 /* may start selection */
193#define NV_SSS 0x20 /* may start selection with shift modifier */
194#define NV_STS 0x40 /* may stop selection without shift modif. */
195#define NV_RL 0x80 /* 'rightleft' modifies command */
196#define NV_KEEPREG 0x100 /* don't clear regname */
197#define NV_NCW 0x200 /* not allowed in command-line window */
198
199/*
200 * Generally speaking, every Normal mode command should either clear any
201 * pending operator (with *clearop*()), or set the motion type variable
202 * oap->motion_type.
203 *
204 * When a cursor motion command is made, it is marked as being a character or
205 * line oriented motion. Then, if an operator is in effect, the operation
206 * becomes character or line oriented accordingly.
207 */
208
209/*
210 * This table contains one entry for every Normal or Visual mode command.
211 * The order doesn't matter, init_normal_cmds() will create a sorted index.
212 * It is faster when all keys from zero to '~' are present.
213 */
214static const struct nv_cmd
215{
216 int cmd_char; /* (first) command character */
217 nv_func_T cmd_func; /* function for this command */
218 short_u cmd_flags; /* NV_ flags */
219 short cmd_arg; /* value for ca.arg */
220} nv_cmds[] =
221{
222 {NUL, nv_error, 0, 0},
223 {Ctrl_A, nv_addsub, 0, 0},
224 {Ctrl_B, nv_page, NV_STS, BACKWARD},
225 {Ctrl_C, nv_esc, 0, TRUE},
226 {Ctrl_D, nv_halfpage, 0, 0},
227 {Ctrl_E, nv_scroll_line, 0, TRUE},
228 {Ctrl_F, nv_page, NV_STS, FORWARD},
229 {Ctrl_G, nv_ctrlg, 0, 0},
230 {Ctrl_H, nv_ctrlh, 0, 0},
231 {Ctrl_I, nv_pcmark, 0, 0},
232 {NL, nv_down, 0, FALSE},
233 {Ctrl_K, nv_error, 0, 0},
234 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000235 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {Ctrl_N, nv_down, NV_STS, FALSE},
237 {Ctrl_O, nv_ctrlo, 0, 0},
238 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000239 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {Ctrl_R, nv_redo, 0, 0},
241 {Ctrl_S, nv_ignore, 0, 0},
242 {Ctrl_T, nv_tagpop, NV_NCW, 0},
243 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_V, nv_visual, 0, FALSE},
245 {'V', nv_visual, 0, FALSE},
246 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_W, nv_window, 0, 0},
248 {Ctrl_X, nv_addsub, 0, 0},
249 {Ctrl_Y, nv_scroll_line, 0, FALSE},
250 {Ctrl_Z, nv_suspend, 0, 0},
251 {ESC, nv_esc, 0, FALSE},
252 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
253 {Ctrl_RSB, nv_ident, NV_NCW, 0},
254 {Ctrl_HAT, nv_hat, NV_NCW, 0},
255 {Ctrl__, nv_error, 0, 0},
256 {' ', nv_right, 0, 0},
257 {'!', nv_operator, 0, 0},
258 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
259 {'#', nv_ident, 0, 0},
260 {'$', nv_dollar, 0, 0},
261 {'%', nv_percent, 0, 0},
262 {'&', nv_optrans, 0, 0},
263 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
264 {'(', nv_brace, 0, BACKWARD},
265 {')', nv_brace, 0, FORWARD},
266 {'*', nv_ident, 0, 0},
267 {'+', nv_down, 0, TRUE},
268 {',', nv_csearch, 0, TRUE},
269 {'-', nv_up, 0, TRUE},
270 {'.', nv_dot, NV_KEEPREG, 0},
271 {'/', nv_search, 0, FALSE},
272 {'0', nv_beginline, 0, 0},
273 {'1', nv_ignore, 0, 0},
274 {'2', nv_ignore, 0, 0},
275 {'3', nv_ignore, 0, 0},
276 {'4', nv_ignore, 0, 0},
277 {'5', nv_ignore, 0, 0},
278 {'6', nv_ignore, 0, 0},
279 {'7', nv_ignore, 0, 0},
280 {'8', nv_ignore, 0, 0},
281 {'9', nv_ignore, 0, 0},
282 {':', nv_colon, 0, 0},
283 {';', nv_csearch, 0, FALSE},
284 {'<', nv_operator, NV_RL, 0},
285 {'=', nv_operator, 0, 0},
286 {'>', nv_operator, NV_RL, 0},
287 {'?', nv_search, 0, FALSE},
288 {'@', nv_at, NV_NCH_NOP, FALSE},
289 {'A', nv_edit, 0, 0},
290 {'B', nv_bck_word, 0, 1},
291 {'C', nv_abbrev, NV_KEEPREG, 0},
292 {'D', nv_abbrev, NV_KEEPREG, 0},
293 {'E', nv_wordcmd, 0, TRUE},
294 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
295 {'G', nv_goto, 0, TRUE},
296 {'H', nv_scroll, 0, 0},
297 {'I', nv_edit, 0, 0},
298 {'J', nv_join, 0, 0},
299 {'K', nv_ident, 0, 0},
300 {'L', nv_scroll, 0, 0},
301 {'M', nv_scroll, 0, 0},
302 {'N', nv_next, 0, SEARCH_REV},
303 {'O', nv_open, 0, 0},
304 {'P', nv_put, 0, 0},
305 {'Q', nv_exmode, NV_NCW, 0},
306 {'R', nv_Replace, 0, FALSE},
307 {'S', nv_subst, NV_KEEPREG, 0},
308 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
309 {'U', nv_Undo, 0, 0},
310 {'W', nv_wordcmd, 0, TRUE},
311 {'X', nv_abbrev, NV_KEEPREG, 0},
312 {'Y', nv_abbrev, NV_KEEPREG, 0},
313 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
314 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
315 {'\\', nv_error, 0, 0},
316 {']', nv_brackets, NV_NCH_ALW, FORWARD},
317 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
318 {'_', nv_lineop, 0, 0},
319 {'`', nv_gomark, NV_NCH_ALW, FALSE},
320 {'a', nv_edit, NV_NCH, 0},
321 {'b', nv_bck_word, 0, 0},
322 {'c', nv_operator, 0, 0},
323 {'d', nv_operator, 0, 0},
324 {'e', nv_wordcmd, 0, FALSE},
325 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
326 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
327 {'h', nv_left, NV_RL, 0},
328 {'i', nv_edit, NV_NCH, 0},
329 {'j', nv_down, 0, FALSE},
330 {'k', nv_up, 0, FALSE},
331 {'l', nv_right, NV_RL, 0},
332 {'m', nv_mark, NV_NCH_NOP, 0},
333 {'n', nv_next, 0, 0},
334 {'o', nv_open, 0, 0},
335 {'p', nv_put, 0, 0},
336 {'q', nv_record, NV_NCH, 0},
337 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
338 {'s', nv_subst, NV_KEEPREG, 0},
339 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
340 {'u', nv_undo, 0, 0},
341 {'w', nv_wordcmd, 0, FALSE},
342 {'x', nv_abbrev, NV_KEEPREG, 0},
343 {'y', nv_operator, 0, 0},
344 {'z', nv_zet, NV_NCH_ALW, 0},
345 {'{', nv_findpar, 0, BACKWARD},
346 {'|', nv_pipe, 0, 0},
347 {'}', nv_findpar, 0, FORWARD},
348 {'~', nv_tilde, 0, 0},
349
350 /* pound sign */
351 {POUND, nv_ident, 0, 0},
352#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200353 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
354 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
355 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
356 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {K_LEFTMOUSE, nv_mouse, 0, 0},
358 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
359 {K_LEFTDRAG, nv_mouse, 0, 0},
360 {K_LEFTRELEASE, nv_mouse, 0, 0},
361 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
418 {K_F8, farsi_fkey, 0, 0},
419 {K_F9, farsi_fkey, 0, 0},
420#endif
421#ifdef FEAT_SNIFF
422 {K_SNIFF, nv_sniff, 0, 0},
423#endif
424#ifdef FEAT_NETBEANS_INTG
425 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
426#endif
427#ifdef FEAT_DND
428 {K_DROP, nv_drop, NV_STS, 0},
429#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000430#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000431 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433};
434
435/* Number of commands in nv_cmds[]. */
436#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
437
438/* Sorted index of commands in nv_cmds[]. */
439static short nv_cmd_idx[NV_CMDS_SIZE];
440
441/* The highest index for which
442 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
443static int nv_max_linear;
444
445/*
446 * Compare functions for qsort() below, that checks the command character
447 * through the index in nv_cmd_idx[].
448 */
449 static int
450#ifdef __BORLANDC__
451_RTLENTRYF
452#endif
453nv_compare(s1, s2)
454 const void *s1;
455 const void *s2;
456{
457 int c1, c2;
458
459 /* The commands are sorted on absolute value. */
460 c1 = nv_cmds[*(const short *)s1].cmd_char;
461 c2 = nv_cmds[*(const short *)s2].cmd_char;
462 if (c1 < 0)
463 c1 = -c1;
464 if (c2 < 0)
465 c2 = -c2;
466 return c1 - c2;
467}
468
469/*
470 * Initialize the nv_cmd_idx[] table.
471 */
472 void
473init_normal_cmds()
474{
475 int i;
476
477 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000478 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 nv_cmd_idx[i] = i;
480
481 /* Sort the commands by the command character. */
482 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
483
484 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000485 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
487 break;
488 nv_max_linear = i - 1;
489}
490
491/*
492 * Search for a command in the commands table.
493 * Returns -1 for invalid command.
494 */
495 static int
496find_command(cmdchar)
497 int cmdchar;
498{
499 int i;
500 int idx;
501 int top, bot;
502 int c;
503
504#ifdef FEAT_MBYTE
505 /* A multi-byte character is never a command. */
506 if (cmdchar >= 0x100)
507 return -1;
508#endif
509
510 /* We use the absolute value of the character. Special keys have a
511 * negative value, but are sorted on their absolute value. */
512 if (cmdchar < 0)
513 cmdchar = -cmdchar;
514
515 /* If the character is in the first part: The character is the index into
516 * nv_cmd_idx[]. */
517 if (cmdchar <= nv_max_linear)
518 return nv_cmd_idx[cmdchar];
519
520 /* Perform a binary search. */
521 bot = nv_max_linear + 1;
522 top = NV_CMDS_SIZE - 1;
523 idx = -1;
524 while (bot <= top)
525 {
526 i = (top + bot) / 2;
527 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
528 if (c < 0)
529 c = -c;
530 if (cmdchar == c)
531 {
532 idx = nv_cmd_idx[i];
533 break;
534 }
535 if (cmdchar > c)
536 bot = i + 1;
537 else
538 top = i - 1;
539 }
540 return idx;
541}
542
543/*
544 * Execute a command in Normal mode.
545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 void
547normal_cmd(oap, toplevel)
548 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000549 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 cmdarg_T ca; /* command arguments */
552 int c;
553 int ctrl_w = FALSE; /* got CTRL-W command */
554 int old_col = curwin->w_curswant;
555#ifdef FEAT_CMDL_INFO
556 int need_flushbuf; /* need to call out_flush() */
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 pos_T old_pos; /* cursor position before command */
559 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 static int old_mapped_len = 0;
561 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000562#ifdef FEAT_EVAL
563 int set_prevcount = FALSE;
564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
567 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000568
569 /* Use a count remembered from before entering an operator. After typing
570 * "3d" we return from normal_cmd() and come back here, the "3" is
571 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ca.opcount = opcount;
573
574#ifdef FEAT_SNIFF
575 want_sniff_request = sniff_connected;
576#endif
577
578 /*
579 * If there is an operator pending, then the command we take this time
580 * will terminate it. Finish_op tells us to finish the operation before
581 * returning this time (unless the operation was cancelled).
582 */
583#ifdef CURSOR_SHAPE
584 c = finish_op;
585#endif
586 finish_op = (oap->op_type != OP_NOP);
587#ifdef CURSOR_SHAPE
588 if (finish_op != c)
589 {
590 ui_cursor_shape(); /* may show different cursor shape */
591# ifdef FEAT_MOUSESHAPE
592 update_mouseshape(-1);
593# endif
594 }
595#endif
596
Bram Moolenaara983fe92008-07-31 20:04:27 +0000597 /* When not finishing an operator and no register name typed, reset the
598 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000602#ifdef FEAT_EVAL
603 set_prevcount = TRUE;
604#endif
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaara983fe92008-07-31 20:04:27 +0000607#ifdef FEAT_AUTOCMD
608 /* Restore counts from before receiving K_CURSORHOLD. This means after
609 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
610 * "3 * 2". */
611 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
612 {
613 ca.opcount = oap->prev_opcount;
614 ca.count0 = oap->prev_count0;
615 oap->prev_opcount = 0;
616 oap->prev_count0 = 0;
617 }
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 State = NORMAL_BUSY;
623#ifdef USE_ON_FLY_SCROLL
624 dont_scroll = FALSE; /* allow scrolling here */
625#endif
626
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100627#ifdef FEAT_EVAL
628 /* Set v:count here, when called from main() and not a stuffed
629 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100630 * when there is no count. Do set it for redo. */
631 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100632 set_vcount_ca(&ca, &set_prevcount);
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 /*
636 * Get the command character from the user.
637 */
638 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 /*
642 * If a mapping was started in Visual or Select mode, remember the length
643 * of the mapping. This is used below to not return to Insert mode for as
644 * long as the mapping is being executed.
645 */
646 if (restart_edit == 0)
647 old_mapped_len = 0;
648 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 old_mapped_len = typebuf_maplen();
651
652 if (c == NUL)
653 c = K_ZERO;
654
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 /*
656 * In Select mode, typed text replaces the selection.
657 */
658 if (VIsual_active
659 && VIsual_select
660 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
661 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000662 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
663 * 'insertmode' is set) fake a "d"elete command, Insert mode will
664 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000665 * Insert the typed character in the typeahead buffer, so that it can
666 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000667 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000668 if (restart_edit != 0)
669 c = 'd';
670 else
671 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000672 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200673 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676#ifdef FEAT_CMDL_INFO
677 need_flushbuf = add_to_showcmd(c);
678#endif
679
680getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
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;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000702#ifdef FEAT_EVAL
703 /* Set v:count here, when called from main() and not a stuffed
704 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100705 * right after the count. Do set it for redo. */
706 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100707 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (ctrl_w)
710 {
711 ++no_mapping;
712 ++allow_keys; /* no mapping for nchar, but keys */
713 }
714 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000715 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 --no_zero_mapping;
718 if (ctrl_w)
719 {
720 --no_mapping;
721 --allow_keys;
722 }
723#ifdef FEAT_CMDL_INFO
724 need_flushbuf |= add_to_showcmd(c);
725#endif
726 }
727
728 /*
729 * If we got CTRL-W there may be a/another count
730 */
731 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
732 {
733 ctrl_w = TRUE;
734 ca.opcount = ca.count0; /* remember first count */
735 ca.count0 = 0;
736 ++no_mapping;
737 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000738 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 --no_mapping;
741 --allow_keys;
742#ifdef FEAT_CMDL_INFO
743 need_flushbuf |= add_to_showcmd(c);
744#endif
745 goto getcount; /* jump back */
746 }
747 }
748
Bram Moolenaara983fe92008-07-31 20:04:27 +0000749#ifdef FEAT_AUTOCMD
750 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000752 /* Save the count values so that ca.opcount and ca.count0 are exactly
753 * the same when coming back here after handling K_CURSORHOLD. */
754 oap->prev_opcount = ca.opcount;
755 oap->prev_count0 = ca.count0;
756 }
757 else
758#endif
759 if (ca.opcount != 0)
760 {
761 /*
762 * If we're in the middle of an operator (including after entering a
763 * yank buffer with '"') AND we had a count before the operator, then
764 * that count overrides the current value of ca.count0.
765 * What this means effectively, is that commands like "3dw" get turned
766 * into "d3w" which makes things fall into place pretty neatly.
767 * If you give a count before AND after the operator, they are
768 * multiplied.
769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (ca.count0)
771 ca.count0 *= ca.opcount;
772 else
773 ca.count0 = ca.opcount;
774 }
775
776 /*
777 * Always remember the count. It will be set to zero (on the next call,
778 * above) when there is no pending operator.
779 * When called from main(), save the count for use by the "count" built-in
780 * variable.
781 */
782 ca.opcount = ca.count0;
783 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
784
785#ifdef FEAT_EVAL
786 /*
787 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100788 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100790 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000791 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#endif
793
794 /*
795 * Find the command character in the table of commands.
796 * For CTRL-W we already got nchar when looking for a count.
797 */
798 if (ctrl_w)
799 {
800 ca.nchar = c;
801 ca.cmdchar = Ctrl_W;
802 }
803 else
804 ca.cmdchar = c;
805 idx = find_command(ca.cmdchar);
806 if (idx < 0)
807 {
808 /* Not a known command: beep. */
809 clearopbeep(oap);
810 goto normal_end;
811 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000812
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000813 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200815 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000817 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 goto normal_end;
819 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000820#ifdef FEAT_AUTOCMD
821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
822 goto normal_end;
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
826 * In Visual/Select mode, a few keys are handled in a special way.
827 */
828 if (VIsual_active)
829 {
830 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
831 if (km_stopsel
832 && (nv_cmds[idx].cmd_flags & NV_STS)
833 && !(mod_mask & MOD_MASK_SHIFT))
834 {
835 end_visual_mode();
836 redraw_curbuf_later(INVERTED);
837 }
838
839 /* Keys that work different when 'keymodel' contains "startsel" */
840 if (km_startsel)
841 {
842 if (nv_cmds[idx].cmd_flags & NV_SS)
843 {
844 unshift_special(&ca);
845 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 if (idx < 0)
847 {
848 /* Just in case */
849 clearopbeep(oap);
850 goto normal_end;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
854 && (mod_mask & MOD_MASK_SHIFT))
855 {
856 mod_mask &= ~MOD_MASK_SHIFT;
857 }
858 }
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
863 && (nv_cmds[idx].cmd_flags & NV_RL))
864 {
865 /* Invert horizontal movements and operations. Only when typed by the
866 * user directly, not when the result of a mapping or "x" translated
867 * to "dl". */
868 switch (ca.cmdchar)
869 {
870 case 'l': ca.cmdchar = 'h'; break;
871 case K_RIGHT: ca.cmdchar = K_LEFT; break;
872 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
873 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
874 case 'h': ca.cmdchar = 'l'; break;
875 case K_LEFT: ca.cmdchar = K_RIGHT; break;
876 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
877 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
878 case '>': ca.cmdchar = '<'; break;
879 case '<': ca.cmdchar = '>'; break;
880 }
881 idx = find_command(ca.cmdchar);
882 }
883#endif
884
885 /*
886 * Get an additional character if we need one.
887 */
888 if ((nv_cmds[idx].cmd_flags & NV_NCH)
889 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
890 && oap->op_type == OP_NOP)
891 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
892 || (ca.cmdchar == 'q'
893 && oap->op_type == OP_NOP
894 && !Recording
895 && !Exec_reg)
896 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 int *cp;
900 int repl = FALSE; /* get character for replace mode */
901 int lit = FALSE; /* get extra character literally */
902 int langmap_active = FALSE; /* using :lmap mappings */
903 int lang; /* getting a text character */
904#ifdef USE_IM_CONTROL
905 int save_smd; /* saved value of p_smd */
906#endif
907
908 ++no_mapping;
909 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000910#ifdef FEAT_AUTOCMD
911 /* Don't generate a CursorHold event here, most commands can't handle
912 * it, e.g., nv_replace(), nv_csearch(). */
913 did_cursorhold = TRUE;
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (ca.cmdchar == 'g')
916 {
917 /*
918 * For 'g' get the next character now, so that we can check for
919 * "gr", "g'" and "g`".
920 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_CMDL_INFO
924 need_flushbuf |= add_to_showcmd(ca.nchar);
925#endif
926 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100927 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 cp = &ca.extra_char; /* need to get a third character */
930 if (ca.nchar != 'r')
931 lit = TRUE; /* get it literally */
932 else
933 repl = TRUE; /* get it in replace mode */
934 }
935 else
936 cp = NULL; /* no third character needed */
937 }
938 else
939 {
940 if (ca.cmdchar == 'r') /* get it in replace mode */
941 repl = TRUE;
942 cp = &ca.nchar;
943 }
944 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
945
946 /*
947 * Get a second or third character.
948 */
949 if (cp != NULL)
950 {
951#ifdef CURSOR_SHAPE
952 if (repl)
953 {
954 State = REPLACE; /* pretend Replace mode */
955 ui_cursor_shape(); /* show different cursor shape */
956 }
957#endif
958 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
959 {
960 /* Allow mappings defined with ":lmap". */
961 --no_mapping;
962 --allow_keys;
963 if (repl)
964 State = LREPLACE;
965 else
966 State = LANGMAP;
967 langmap_active = TRUE;
968 }
969#ifdef USE_IM_CONTROL
970 save_smd = p_smd;
971 p_smd = FALSE; /* Don't let the IM code show the mode here */
972 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
973 im_set_active(TRUE);
974#endif
975
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000976 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 if (langmap_active)
979 {
980 /* Undo the decrement done above */
981 ++no_mapping;
982 ++allow_keys;
983 State = NORMAL_BUSY;
984 }
985#ifdef USE_IM_CONTROL
986 if (lang)
987 {
988 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
989 im_save_status(&curbuf->b_p_iminsert);
990 im_set_active(FALSE);
991 }
992 p_smd = save_smd;
993#endif
994#ifdef CURSOR_SHAPE
995 State = NORMAL_BUSY;
996#endif
997#ifdef FEAT_CMDL_INFO
998 need_flushbuf |= add_to_showcmd(*cp);
999#endif
1000
1001 if (!lit)
1002 {
1003#ifdef FEAT_DIGRAPHS
1004 /* Typing CTRL-K gets a digraph. */
1005 if (*cp == Ctrl_K
1006 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1007 || cp == &ca.extra_char)
1008 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1009 {
1010 c = get_digraph(FALSE);
1011 if (c > 0)
1012 {
1013 *cp = c;
1014# ifdef FEAT_CMDL_INFO
1015 /* Guessing how to update showcmd here... */
1016 del_from_showcmd(3);
1017 need_flushbuf |= add_to_showcmd(*cp);
1018# endif
1019 }
1020 }
1021#endif
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /* adjust chars > 127, except after "tTfFr" commands */
1024 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_RIGHTLEFT
1026 /* adjust Hebrew mapped char */
1027 if (p_hkmap && lang && KeyTyped)
1028 *cp = hkmap(*cp);
1029# ifdef FEAT_FKMAP
1030 /* adjust Farsi mapped char */
1031 if (p_fkmap && lang && KeyTyped)
1032 *cp = fkmap(*cp);
1033# endif
1034#endif
1035 }
1036
1037 /*
1038 * When the next character is CTRL-\ a following CTRL-N means the
1039 * command is aborted and we go to Normal mode.
1040 */
1041 if (cp == &ca.extra_char
1042 && ca.nchar == Ctrl_BSL
1043 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1044 {
1045 ca.cmdchar = Ctrl_BSL;
1046 ca.nchar = ca.extra_char;
1047 idx = find_command(ca.cmdchar);
1048 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001049 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001050 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else if (*cp == Ctrl_BSL)
1052 {
1053 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1054
1055 /* There is a busy wait here when typing "f<C-\>" and then
1056 * something different from CTRL-N. Can't be avoided. */
1057 while ((c = vpeekc()) <= 0 && towait > 0L)
1058 {
1059 do_sleep(towait > 50L ? 50L : towait);
1060 towait -= 50L;
1061 }
1062 if (c > 0)
1063 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001064 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (c != Ctrl_N && c != Ctrl_G)
1066 vungetc(c);
1067 else
1068 {
1069 ca.cmdchar = Ctrl_BSL;
1070 ca.nchar = c;
1071 idx = find_command(ca.cmdchar);
1072 }
1073 }
1074 }
1075
1076#ifdef FEAT_MBYTE
1077 /* When getting a text character and the next character is a
1078 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001079 * However, don't wait for it to arrive. Also, do enable mapping,
1080 * because if it's put back with vungetc() it's too late to apply
1081 * mapping. */
1082 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 while (enc_utf8 && lang && (c = vpeekc()) > 0
1084 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1085 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001086 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (!utf_iscomposing(c))
1088 {
1089 vungetc(c); /* it wasn't, put it back */
1090 break;
1091 }
1092 else if (ca.ncharC1 == 0)
1093 ca.ncharC1 = c;
1094 else
1095 ca.ncharC2 = c;
1096 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001097 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099 }
1100 --no_mapping;
1101 --allow_keys;
1102 }
1103
1104#ifdef FEAT_CMDL_INFO
1105 /*
1106 * Flush the showcmd characters onto the screen so we can see them while
1107 * the command is being executed. Only do this when the shown command was
1108 * actually displayed, otherwise this will slow down a lot when executing
1109 * mappings.
1110 */
1111 if (need_flushbuf)
1112 out_flush();
1113#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001114#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001115 if (ca.cmdchar != K_IGNORE)
1116 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 State = NORMAL;
1120
1121 if (ca.nchar == ESC)
1122 {
1123 clearop(oap);
1124 if (restart_edit == 0 && goto_im())
1125 restart_edit = 'a';
1126 goto normal_end;
1127 }
1128
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001129 if (ca.cmdchar != K_IGNORE)
1130 {
1131 msg_didout = FALSE; /* don't scroll screen up for normal command */
1132 msg_col = 0;
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 old_pos = curwin->w_cursor; /* remember where cursor was */
1136
1137 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1138 * mode. */
1139 if (!VIsual_active && km_startsel)
1140 {
1141 if (nv_cmds[idx].cmd_flags & NV_SS)
1142 {
1143 start_selection();
1144 unshift_special(&ca);
1145 idx = find_command(ca.cmdchar);
1146 }
1147 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1148 && (mod_mask & MOD_MASK_SHIFT))
1149 {
1150 start_selection();
1151 mod_mask &= ~MOD_MASK_SHIFT;
1152 }
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 /*
1156 * Execute the command!
1157 * Call the command function found in the commands table.
1158 */
1159 ca.arg = nv_cmds[idx].cmd_arg;
1160 (nv_cmds[idx].cmd_func)(&ca);
1161
1162 /*
1163 * If we didn't start or finish an operator, reset oap->regname, unless we
1164 * need it later.
1165 */
1166 if (!finish_op
1167 && !oap->op_type
1168 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1169 {
1170 clearop(oap);
1171#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001172 {
1173 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001174
Bram Moolenaar536681b2011-05-10 16:12:45 +02001175 /* Adjust the register according to 'clipboard', so that when
1176 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001177# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001178 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001179# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001180 set_reg_var(regname);
1181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183 }
1184
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001185 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001186 * character or "z333<cr>". */
1187 if (old_mapped_len > 0)
1188 old_mapped_len = typebuf_maplen();
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 /*
1191 * If an operation is pending, handle it...
1192 */
1193 do_pending_operator(&ca, old_col, FALSE);
1194
1195 /*
1196 * Wait for a moment when a message is displayed that will be overwritten
1197 * by the mode message.
1198 * In Visual mode and with "^O" in Insert mode, a short message will be
1199 * overwritten by the mode message. Wait a bit, until a key is hit.
1200 * In Visual mode, it's more important to keep the Visual area updated
1201 * than keeping a message (e.g. from a /pat search).
1202 * Only do this if the command was typed, not from a mapping.
1203 * Don't wait when emsg_silent is non-zero.
1204 * Also wait a bit after an error message, e.g. for "^O:".
1205 * Don't redraw the screen, it would remove the message.
1206 */
1207 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001208 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 || (VIsual_active
1211 && old_pos.lnum == curwin->w_cursor.lnum
1212 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 )
1214 && (clear_cmdline
1215 || redraw_cmdline)
1216 && (msg_didout || (msg_didany && msg_scroll))
1217 && !msg_nowait
1218 && KeyTyped)
1219 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && (msg_scroll
1222 || emsg_on_display)))
1223 && oap->regname == 0
1224 && !(ca.retval & CA_COMMAND_BUSY)
1225 && stuff_empty()
1226 && typebuf_typed()
1227 && emsg_silent == 0
1228 && !did_wait_return
1229 && oap->op_type == OP_NOP)
1230 {
1231 int save_State = State;
1232
1233 /* Draw the cursor with the right shape here */
1234 if (restart_edit != 0)
1235 State = INSERT;
1236
1237 /* If need to redraw, and there is a "keep_msg", redraw before the
1238 * delay */
1239 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1240 {
1241 char_u *kmsg;
1242
1243 kmsg = keep_msg;
1244 keep_msg = NULL;
1245 /* showmode() will clear keep_msg, but we want to use it anyway */
1246 update_screen(0);
1247 /* now reset it, otherwise it's put in the history again */
1248 keep_msg = kmsg;
1249 msg_attr(kmsg, keep_msg_attr);
1250 vim_free(kmsg);
1251 }
1252 setcursor();
1253 cursor_on();
1254 out_flush();
1255 if (msg_scroll || emsg_on_display)
1256 ui_delay(1000L, TRUE); /* wait at least one second */
1257 ui_delay(3000L, FALSE); /* wait up to three seconds */
1258 State = save_State;
1259
1260 msg_scroll = FALSE;
1261 emsg_on_display = FALSE;
1262 }
1263
1264 /*
1265 * Finish up after executing a Normal mode command.
1266 */
1267normal_end:
1268
1269 msg_nowait = FALSE;
1270
1271 /* Reset finish_op, in case it was set */
1272#ifdef CURSOR_SHAPE
1273 c = finish_op;
1274#endif
1275 finish_op = FALSE;
1276#ifdef CURSOR_SHAPE
1277 /* Redraw the cursor with another shape, if we were in Operator-pending
1278 * mode or did a replace command. */
1279 if (c || ca.cmdchar == 'r')
1280 {
1281 ui_cursor_shape(); /* may show different cursor shape */
1282# ifdef FEAT_MOUSESHAPE
1283 update_mouseshape(-1);
1284# endif
1285 }
1286#endif
1287
1288#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001289 if (oap->op_type == OP_NOP && oap->regname == 0
1290# ifdef FEAT_AUTOCMD
1291 && ca.cmdchar != K_CURSORHOLD
1292# endif
1293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 clear_showcmd();
1295#endif
1296
1297 checkpcmark(); /* check if we moved since setting pcmark */
1298 vim_free(ca.searchbuf);
1299
1300#ifdef FEAT_MBYTE
1301 if (has_mbyte)
1302 mb_adjust_cursor();
1303#endif
1304
1305#ifdef FEAT_SCROLLBIND
1306 if (curwin->w_p_scb && toplevel)
1307 {
1308 validate_cursor(); /* may need to update w_leftcol */
1309 do_check_scrollbind(TRUE);
1310 }
1311#endif
1312
Bram Moolenaar860cae12010-06-05 23:22:07 +02001313#ifdef FEAT_CURSORBIND
1314 if (curwin->w_p_crb && toplevel)
1315 {
1316 validate_cursor(); /* may need to update w_leftcol */
1317 do_check_cursorbind();
1318 }
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 /*
1322 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1323 * if still inside a mapping that started in Visual mode).
1324 * May switch from Visual to Select mode after CTRL-O command.
1325 */
1326 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1328 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && !(ca.retval & CA_COMMAND_BUSY)
1330 && stuff_empty()
1331 && oap->regname == 0)
1332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 1)
1334 {
1335 VIsual_select = TRUE;
1336 showmode();
1337 restart_VIsual_select = 0;
1338 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001339 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 (void)edit(restart_edit, FALSE, 1L);
1341 }
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (restart_VIsual_select == 2)
1344 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /* Save count before an operator for next time. */
1347 opcount = ca.opcount;
1348}
1349
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001350#ifdef FEAT_EVAL
1351/*
1352 * Set v:count and v:count1 according to "cap".
1353 * Set v:prevcount only when "set_prevcount" is TRUE.
1354 */
1355 static void
1356set_vcount_ca(cap, set_prevcount)
1357 cmdarg_T *cap;
1358 int *set_prevcount;
1359{
1360 long count = cap->count0;
1361
1362 /* multiply with cap->opcount the same way as above */
1363 if (cap->opcount != 0)
1364 count = cap->opcount * (count == 0 ? 1 : count);
1365 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1366 *set_prevcount = FALSE; /* only set v:prevcount once */
1367}
1368#endif
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * Handle an operator after visual mode or when the movement is finished
1372 */
1373 void
1374do_pending_operator(cap, old_col, gui_yank)
1375 cmdarg_T *cap;
1376 int old_col;
1377 int gui_yank;
1378{
1379 oparg_T *oap = cap->oap;
1380 pos_T old_cursor;
1381 int empty_region_error;
1382 int restart_edit_save;
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /* The visual area is remembered for redo */
1385 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1386 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001387 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001389#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#endif
1392
1393#if defined(FEAT_CLIPBOARD)
1394 /*
1395 * Yank the visual area into the GUI selection register before we operate
1396 * on it and lose it forever.
1397 * Don't do it if a specific register was specified, so that ""x"*P works.
1398 * This could call do_pending_operator() recursively, but that's OK
1399 * because gui_yank will be TRUE for the nested call.
1400 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001401 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && oap->op_type != OP_NOP
1403 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 && VIsual_active
1405 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 && oap->regname == 0)
1407 clip_auto_select();
1408#endif
1409 old_cursor = curwin->w_cursor;
1410
1411 /*
1412 * If an operation is pending, handle it...
1413 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001414 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 oap->is_VIsual = VIsual_active;
1417 if (oap->motion_force == 'V')
1418 oap->motion_type = MLINE;
1419 else if (oap->motion_force == 'v')
1420 {
1421 /* If the motion was linewise, "inclusive" will not have been set.
1422 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1423 if (oap->motion_type == MLINE)
1424 oap->inclusive = FALSE;
1425 /* If the motion already was characterwise, toggle "inclusive" */
1426 else if (oap->motion_type == MCHAR)
1427 oap->inclusive = !oap->inclusive;
1428 oap->motion_type = MCHAR;
1429 }
1430 else if (oap->motion_force == Ctrl_V)
1431 {
1432 /* Change line- or characterwise motion into Visual block mode. */
1433 VIsual_active = TRUE;
1434 VIsual = oap->start;
1435 VIsual_mode = Ctrl_V;
1436 VIsual_select = FALSE;
1437 VIsual_reselect = FALSE;
1438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar7afea822013-04-24 18:34:45 +02001440 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1441 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001443 && ((!VIsual_active || oap->motion_force)
1444 /* Also redo Operator-pending Visual mode mappings */
1445 || (VIsual_active && cap->cmdchar == ':'
1446 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001447 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448#ifdef FEAT_FOLDING
1449 && oap->op_type != OP_FOLD
1450 && oap->op_type != OP_FOLDOPEN
1451 && oap->op_type != OP_FOLDOPENREC
1452 && oap->op_type != OP_FOLDCLOSE
1453 && oap->op_type != OP_FOLDCLOSEREC
1454 && oap->op_type != OP_FOLDDEL
1455 && oap->op_type != OP_FOLDDELREC
1456#endif
1457 )
1458 {
1459 prep_redo(oap->regname, cap->count0,
1460 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1461 oap->motion_force, cap->cmdchar, cap->nchar);
1462 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1463 {
1464 /*
1465 * If 'cpoptions' does not contain 'r', insert the search
1466 * pattern to really repeat the same command.
1467 */
1468 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001469 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 AppendToRedobuff(NL_STR);
1471 }
1472 else if (cap->cmdchar == ':')
1473 {
1474 /* do_cmdline() has stored the first typed line in
1475 * "repeat_cmdline". When several lines are typed repeating
1476 * won't be possible. */
1477 if (repeat_cmdline == NULL)
1478 ResetRedobuff();
1479 else
1480 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001481 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 AppendToRedobuff(NL_STR);
1483 vim_free(repeat_cmdline);
1484 repeat_cmdline = NULL;
1485 }
1486 }
1487 }
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if (redo_VIsual_busy)
1490 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001491 /* Redo of an operation on a Visual area. Use the same size from
1492 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 oap->start = curwin->w_cursor;
1494 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1495 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1496 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1497 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001498 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001500 if (VIsual_mode == 'v')
1501 {
1502 if (redo_VIsual_line_count <= 1)
1503 {
1504 validate_virtcol();
1505 curwin->w_curswant =
1506 curwin->w_virtcol + redo_VIsual_vcol - 1;
1507 }
1508 else
1509 curwin->w_curswant = redo_VIsual_vcol;
1510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001512 {
1513 curwin->w_curswant = MAXCOL;
1514 }
1515 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 cap->count0 = redo_VIsual_count;
1518 if (redo_VIsual_count != 0)
1519 cap->count1 = redo_VIsual_count;
1520 else
1521 cap->count1 = 1;
1522 }
1523 else if (VIsual_active)
1524 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001525 if (!gui_yank)
1526 {
1527 /* Save the current VIsual area for '< and '> marks, and "gv" */
1528 curbuf->b_visual.vi_start = VIsual;
1529 curbuf->b_visual.vi_end = curwin->w_cursor;
1530 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001531 if (VIsual_mode_orig != NUL)
1532 {
1533 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1534 VIsual_mode_orig = NUL;
1535 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001536 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001538 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
1542 /* In Select mode, a linewise selection is operated upon like a
1543 * characterwise selection. */
1544 if (VIsual_select && VIsual_mode == 'V')
1545 {
1546 if (lt(VIsual, curwin->w_cursor))
1547 {
1548 VIsual.col = 0;
1549 curwin->w_cursor.col =
1550 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1551 }
1552 else
1553 {
1554 curwin->w_cursor.col = 0;
1555 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1556 }
1557 VIsual_mode = 'v';
1558 }
1559 /* If 'selection' is "exclusive", backup one character for
1560 * charwise selections. */
1561 else if (VIsual_mode == 'v')
1562 {
1563# ifdef FEAT_VIRTUALEDIT
1564 include_line_break =
1565# endif
1566 unadjust_for_sel();
1567 }
1568
1569 oap->start = VIsual;
1570 if (VIsual_mode == 'V')
1571 oap->start.col = 0;
1572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 /*
1575 * Set oap->start to the first position of the operated text, oap->end
1576 * to the end of the operated text. w_cursor is equal to oap->start.
1577 */
1578 if (lt(oap->start, curwin->w_cursor))
1579 {
1580#ifdef FEAT_FOLDING
1581 /* Include folded lines completely. */
1582 if (!VIsual_active)
1583 {
1584 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1585 oap->start.col = 0;
1586 if (hasFolding(curwin->w_cursor.lnum, NULL,
1587 &curwin->w_cursor.lnum))
1588 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1589 }
1590#endif
1591 oap->end = curwin->w_cursor;
1592 curwin->w_cursor = oap->start;
1593
1594 /* w_virtcol may have been updated; if the cursor goes back to its
1595 * previous position w_virtcol becomes invalid and isn't updated
1596 * automatically. */
1597 curwin->w_valid &= ~VALID_VIRTCOL;
1598 }
1599 else
1600 {
1601#ifdef FEAT_FOLDING
1602 /* Include folded lines completely. */
1603 if (!VIsual_active && oap->motion_type == MLINE)
1604 {
1605 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1606 NULL))
1607 curwin->w_cursor.col = 0;
1608 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1609 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1610 }
1611#endif
1612 oap->end = oap->start;
1613 oap->start = curwin->w_cursor;
1614 }
1615
1616 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1617
1618#ifdef FEAT_VIRTUALEDIT
1619 /* Set "virtual_op" before resetting VIsual_active. */
1620 virtual_op = virtual_active();
1621#endif
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (VIsual_active || redo_VIsual_busy)
1624 {
1625 if (VIsual_mode == Ctrl_V) /* block mode */
1626 {
1627 colnr_T start, end;
1628
1629 oap->block_mode = TRUE;
1630
1631 getvvcol(curwin, &(oap->start),
1632 &oap->start_vcol, NULL, &oap->end_vcol);
1633 if (!redo_VIsual_busy)
1634 {
1635 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1636
1637 if (start < oap->start_vcol)
1638 oap->start_vcol = start;
1639 if (end > oap->end_vcol)
1640 {
1641 if (*p_sel == 'e' && start >= 1
1642 && start - 1 >= oap->end_vcol)
1643 oap->end_vcol = start - 1;
1644 else
1645 oap->end_vcol = end;
1646 }
1647 }
1648
1649 /* if '$' was used, get oap->end_vcol from longest line */
1650 if (curwin->w_curswant == MAXCOL)
1651 {
1652 curwin->w_cursor.col = MAXCOL;
1653 oap->end_vcol = 0;
1654 for (curwin->w_cursor.lnum = oap->start.lnum;
1655 curwin->w_cursor.lnum <= oap->end.lnum;
1656 ++curwin->w_cursor.lnum)
1657 {
1658 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1659 if (end > oap->end_vcol)
1660 oap->end_vcol = end;
1661 }
1662 }
1663 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001664 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 /*
1666 * Correct oap->end.col and oap->start.col to be the
1667 * upper-left and lower-right corner of the block area.
1668 *
1669 * (Actually, this does convert column positions into character
1670 * positions)
1671 */
1672 curwin->w_cursor.lnum = oap->end.lnum;
1673 coladvance(oap->end_vcol);
1674 oap->end = curwin->w_cursor;
1675
1676 curwin->w_cursor = oap->start;
1677 coladvance(oap->start_vcol);
1678 oap->start = curwin->w_cursor;
1679 }
1680
1681 if (!redo_VIsual_busy && !gui_yank)
1682 {
1683 /*
1684 * Prepare to reselect and redo Visual: this is based on the
1685 * size of the Visual text
1686 */
1687 resel_VIsual_mode = VIsual_mode;
1688 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001689 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001691 {
1692 if (VIsual_mode != Ctrl_V)
1693 getvvcol(curwin, &(oap->end),
1694 NULL, NULL, &oap->end_vcol);
1695 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1696 {
1697 if (VIsual_mode != Ctrl_V)
1698 getvvcol(curwin, &(oap->start),
1699 &oap->start_vcol, NULL, NULL);
1700 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1701 }
1702 else
1703 resel_VIsual_vcol = oap->end_vcol;
1704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 resel_VIsual_line_count = oap->line_count;
1706 }
1707
1708 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1709 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1710 && oap->op_type != OP_COLON
1711#ifdef FEAT_FOLDING
1712 && oap->op_type != OP_FOLD
1713 && oap->op_type != OP_FOLDOPEN
1714 && oap->op_type != OP_FOLDOPENREC
1715 && oap->op_type != OP_FOLDCLOSE
1716 && oap->op_type != OP_FOLDCLOSEREC
1717 && oap->op_type != OP_FOLDDEL
1718 && oap->op_type != OP_FOLDDELREC
1719#endif
1720 && oap->motion_force == NUL
1721 )
1722 {
1723 /* Prepare for redoing. Only use the nchar field for "r",
1724 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001725 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1726 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001727 prep_redo(oap->regname, cap->count0,
1728 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1729 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001730 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001731 prep_redo(oap->regname, 0L, NUL, 'v',
1732 get_op_char(oap->op_type),
1733 get_extra_op_char(oap->op_type),
1734 oap->op_type == OP_REPLACE
1735 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (!redo_VIsual_busy)
1737 {
1738 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001739 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 redo_VIsual_line_count = resel_VIsual_line_count;
1741 redo_VIsual_count = cap->count0;
1742 }
1743 }
1744
1745 /*
1746 * oap->inclusive defaults to TRUE.
1747 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1748 * FALSE. This makes "d}P" and "v}dP" work the same.
1749 */
1750 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1751 oap->inclusive = TRUE;
1752 if (VIsual_mode == 'V')
1753 oap->motion_type = MLINE;
1754 else
1755 {
1756 oap->motion_type = MCHAR;
1757 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001758#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 )
1762 {
1763 oap->inclusive = FALSE;
1764 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001765 * that works on lines only. */
1766 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001768 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1769 {
1770 ++oap->end.lnum;
1771 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001772#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001773 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001774#endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001775 ++oap->line_count;
1776 }
1777 else
1778 {
1779 /* Cannot move below the last line, make the op
1780 * inclusive to tell the operation to include the
1781 * line break. */
1782 oap->inclusive = TRUE;
1783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 }
1786 }
1787
1788 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 /*
1791 * Switch Visual off now, so screen updating does
1792 * not show inverted text when the screen is redrawn.
1793 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1794 * no screen redraw, so it is done here to remove the inverted
1795 * part.
1796 */
1797 if (!gui_yank)
1798 {
1799 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 setmouse();
1802 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001803#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001804 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 clear_cmdline = TRUE; /* unshow visual mode later */
1806#ifdef FEAT_CMDL_INFO
1807 else
1808 clear_showcmd();
1809#endif
1810 if ((oap->op_type == OP_YANK
1811 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001812 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 || oap->op_type == OP_FILTER)
1814 && oap->motion_force == NUL)
1815 redraw_curbuf_later(INVERTED);
1816 }
1817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819#ifdef FEAT_MBYTE
1820 /* Include the trailing byte of a multi-byte char. */
1821 if (has_mbyte && oap->inclusive)
1822 {
1823 int l;
1824
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001825 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (l > 1)
1827 oap->end.col += l - 1;
1828 }
1829#endif
1830 curwin->w_set_curswant = TRUE;
1831
1832 /*
1833 * oap->empty is set when start and end are the same. The inclusive
1834 * flag affects this too, unless yanking and the end is on a NUL.
1835 */
1836 oap->empty = (oap->motion_type == MCHAR
1837 && (!oap->inclusive
1838 || (oap->op_type == OP_YANK
1839 && gchar_pos(&oap->end) == NUL))
1840 && equalpos(oap->start, oap->end)
1841#ifdef FEAT_VIRTUALEDIT
1842 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1843#endif
1844 );
1845 /*
1846 * For delete, change and yank, it's an error to operate on an
1847 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1848 */
1849 empty_region_error = (oap->empty
1850 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 /* Force a redraw when operating on an empty Visual region, when
1853 * 'modifiable is off or creating a fold. */
1854 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001855#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 ))
1859 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861 /*
1862 * If the end of an operator is in column one while oap->motion_type
1863 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1864 * character in the previous line. If op_start is on or before the
1865 * first non-blank in the line, the operator becomes linewise
1866 * (strange, but that's the way vi does it).
1867 */
1868 if ( oap->motion_type == MCHAR
1869 && oap->inclusive == FALSE
1870 && !(cap->retval & CA_NO_ADJ_OP_END)
1871 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001873 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 && oap->line_count > 1)
1875 {
1876 oap->end_adjusted = TRUE; /* remember that we did this */
1877 --oap->line_count;
1878 --oap->end.lnum;
1879 if (inindent(0))
1880 oap->motion_type = MLINE;
1881 else
1882 {
1883 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1884 if (oap->end.col)
1885 {
1886 --oap->end.col;
1887 oap->inclusive = TRUE;
1888 }
1889 }
1890 }
1891 else
1892 oap->end_adjusted = FALSE;
1893
1894 switch (oap->op_type)
1895 {
1896 case OP_LSHIFT:
1897 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001898 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 auto_format(FALSE, TRUE);
1900 break;
1901
1902 case OP_JOIN_NS:
1903 case OP_JOIN:
1904 if (oap->line_count < 2)
1905 oap->line_count = 2;
1906 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1907 curbuf->b_ml.ml_line_count)
1908 beep_flush();
1909 else
1910 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001911 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001912 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 auto_format(FALSE, TRUE);
1914 }
1915 break;
1916
1917 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001922 CancelRedo();
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
1925 {
1926 (void)op_delete(oap);
1927 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1928 u_save_cursor(); /* cursor line wasn't saved yet */
1929 auto_format(FALSE, TRUE);
1930 }
1931 break;
1932
1933 case OP_YANK:
1934 if (empty_region_error)
1935 {
1936 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001939 CancelRedo();
1940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942 else
1943 (void)op_yank(oap, FALSE, !gui_yank);
1944 check_cursor_col();
1945 break;
1946
1947 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001952 CancelRedo();
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else
1955 {
1956 /* This is a new edit command, not a restart. Need to
1957 * remember it to make 'insertmode' work with mappings for
1958 * Visual mode. But do this only once and not when typed and
1959 * 'insertmode' isn't set. */
1960 if (p_im || !KeyTyped)
1961 restart_edit_save = restart_edit;
1962 else
1963 restart_edit_save = 0;
1964 restart_edit = 0;
1965 /* Reset finish_op now, don't want it set inside edit(). */
1966 finish_op = FALSE;
1967 if (op_change(oap)) /* will call edit() */
1968 cap->retval |= CA_COMMAND_BUSY;
1969 if (restart_edit == 0)
1970 restart_edit = restart_edit_save;
1971 }
1972 break;
1973
1974 case OP_FILTER:
1975 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1976 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1977 else
1978 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1979
1980 case OP_INDENT:
1981 case OP_COLON:
1982
1983#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1984 /*
1985 * If 'equalprg' is empty, do the indenting internally.
1986 */
1987 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1988 {
1989# ifdef FEAT_LISP
1990 if (curbuf->b_p_lisp)
1991 {
1992 op_reindent(oap, get_lisp_indent);
1993 break;
1994 }
1995# endif
1996# ifdef FEAT_CINDENT
1997 op_reindent(oap,
1998# ifdef FEAT_EVAL
1999 *curbuf->b_p_inde != NUL ? get_expr_indent :
2000# endif
2001 get_c_indent);
2002 break;
2003# endif
2004 }
2005#endif
2006
2007 op_colon(oap);
2008 break;
2009
2010 case OP_TILDE:
2011 case OP_UPPER:
2012 case OP_LOWER:
2013 case OP_ROT13:
2014 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002017 CancelRedo();
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
2020 op_tilde(oap);
2021 check_cursor_col();
2022 break;
2023
2024 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002025#if defined(FEAT_EVAL)
2026 if (*curbuf->b_p_fex != NUL)
2027 op_formatexpr(oap); /* use expression */
2028 else
2029#endif
2030 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 op_colon(oap); /* use external command */
2032 else
2033 op_format(oap, FALSE); /* use internal function */
2034 break;
2035
2036 case OP_FORMAT2:
2037 op_format(oap, TRUE); /* use internal function */
2038 break;
2039
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002040 case OP_FUNCTION:
2041 op_function(oap); /* call 'operatorfunc' */
2042 break;
2043
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 case OP_INSERT:
2045 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#ifdef FEAT_VISUALEXTRA
2048 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002051 CancelRedo();
2052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 else
2054 {
2055 /* This is a new edit command, not a restart. Need to
2056 * remember it to make 'insertmode' work with mappings for
2057 * Visual mode. But do this only once. */
2058 restart_edit_save = restart_edit;
2059 restart_edit = 0;
2060
2061 op_insert(oap, cap->count1);
2062
2063 /* TODO: when inserting in several lines, should format all
2064 * the lines. */
2065 auto_format(FALSE, TRUE);
2066
2067 if (restart_edit == 0)
2068 restart_edit = restart_edit_save;
2069 }
2070#else
2071 vim_beep();
2072#endif
2073 break;
2074
2075 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#ifdef FEAT_VISUALEXTRA
2078 if (empty_region_error)
2079#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002082 CancelRedo();
2083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#ifdef FEAT_VISUALEXTRA
2085 else
2086 op_replace(oap, cap->nchar);
2087#endif
2088 break;
2089
2090#ifdef FEAT_FOLDING
2091 case OP_FOLD:
2092 VIsual_reselect = FALSE; /* don't reselect now */
2093 foldCreate(oap->start.lnum, oap->end.lnum);
2094 break;
2095
2096 case OP_FOLDOPEN:
2097 case OP_FOLDOPENREC:
2098 case OP_FOLDCLOSE:
2099 case OP_FOLDCLOSEREC:
2100 VIsual_reselect = FALSE; /* don't reselect now */
2101 opFoldRange(oap->start.lnum, oap->end.lnum,
2102 oap->op_type == OP_FOLDOPEN
2103 || oap->op_type == OP_FOLDOPENREC,
2104 oap->op_type == OP_FOLDOPENREC
2105 || oap->op_type == OP_FOLDCLOSEREC,
2106 oap->is_VIsual);
2107 break;
2108
2109 case OP_FOLDDEL:
2110 case OP_FOLDDELREC:
2111 VIsual_reselect = FALSE; /* don't reselect now */
2112 deleteFold(oap->start.lnum, oap->end.lnum,
2113 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2114 break;
2115#endif
2116 default:
2117 clearopbeep(oap);
2118 }
2119#ifdef FEAT_VIRTUALEDIT
2120 virtual_op = MAYBE;
2121#endif
2122 if (!gui_yank)
2123 {
2124 /*
2125 * if 'sol' not set, go back to old column for some commands
2126 */
2127 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2128 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2129 || oap->op_type == OP_DELETE))
2130 coladvance(curwin->w_curswant = old_col);
2131 }
2132 else
2133 {
2134 curwin->w_cursor = old_cursor;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 clearop(oap);
2138 }
2139}
2140
2141/*
2142 * Handle indent and format operators and visual mode ":".
2143 */
2144 static void
2145op_colon(oap)
2146 oparg_T *oap;
2147{
2148 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (oap->is_VIsual)
2150 stuffReadbuff((char_u *)"'<,'>");
2151 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 /*
2154 * Make the range look nice, so it can be repeated.
2155 */
2156 if (oap->start.lnum == curwin->w_cursor.lnum)
2157 stuffcharReadbuff('.');
2158 else
2159 stuffnumReadbuff((long)oap->start.lnum);
2160 if (oap->end.lnum != oap->start.lnum)
2161 {
2162 stuffcharReadbuff(',');
2163 if (oap->end.lnum == curwin->w_cursor.lnum)
2164 stuffcharReadbuff('.');
2165 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2166 stuffcharReadbuff('$');
2167 else if (oap->start.lnum == curwin->w_cursor.lnum)
2168 {
2169 stuffReadbuff((char_u *)".+");
2170 stuffnumReadbuff((long)oap->line_count - 1);
2171 }
2172 else
2173 stuffnumReadbuff((long)oap->end.lnum);
2174 }
2175 }
2176 if (oap->op_type != OP_COLON)
2177 stuffReadbuff((char_u *)"!");
2178 if (oap->op_type == OP_INDENT)
2179 {
2180#ifndef FEAT_CINDENT
2181 if (*get_equalprg() == NUL)
2182 stuffReadbuff((char_u *)"indent");
2183 else
2184#endif
2185 stuffReadbuff(get_equalprg());
2186 stuffReadbuff((char_u *)"\n");
2187 }
2188 else if (oap->op_type == OP_FORMAT)
2189 {
2190 if (*p_fp == NUL)
2191 stuffReadbuff((char_u *)"fmt");
2192 else
2193 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002194 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
2196
2197 /*
2198 * do_cmdline() does the rest
2199 */
2200}
2201
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002202/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002203 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002204 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002205 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002206op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002207 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002208{
2209#ifdef FEAT_EVAL
2210 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002211# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002212 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002213# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002214
2215 if (*p_opfunc == NUL)
2216 EMSG(_("E774: 'operatorfunc' is empty"));
2217 else
2218 {
2219 /* Set '[ and '] marks to text to be operated on. */
2220 curbuf->b_op_start = oap->start;
2221 curbuf->b_op_end = oap->end;
2222 if (oap->motion_type != MLINE && !oap->inclusive)
2223 /* Exclude the end position. */
2224 decl(&curbuf->b_op_end);
2225
2226 if (oap->block_mode)
2227 argv[0] = (char_u *)"block";
2228 else if (oap->motion_type == MLINE)
2229 argv[0] = (char_u *)"line";
2230 else
2231 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002232
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002233# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002234 /* Reset virtual_op so that 'virtualedit' can be changed in the
2235 * function. */
2236 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002237# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002238
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002239 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002240
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002241# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002242 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002243# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002244 }
2245#else
2246 EMSG(_("E775: Eval feature not available"));
2247#endif
2248}
2249
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#if defined(FEAT_MOUSE) || defined(PROTO)
2251/*
2252 * Do the appropriate action for the current mouse click in the current mode.
2253 * Not used for Command-line mode.
2254 *
2255 * Normal Mode:
2256 * event modi- position visual change action
2257 * fier cursor window
2258 * left press - yes end yes
2259 * left press C yes end yes "^]" (2)
2260 * left press S yes end yes "*" (2)
2261 * left drag - yes start if moved no
2262 * left relse - yes start if moved no
2263 * middle press - yes if not active no put register
2264 * middle press - yes if active no yank and put
2265 * right press - yes start or extend yes
2266 * right press S yes no change yes "#" (2)
2267 * right drag - yes extend no
2268 * right relse - yes extend no
2269 *
2270 * Insert or Replace Mode:
2271 * event modi- position visual change action
2272 * fier cursor window
2273 * left press - yes (cannot be active) yes
2274 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2275 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2276 * left drag - yes start or extend (1) no CTRL-O (1)
2277 * left relse - yes start or extend (1) no CTRL-O (1)
2278 * middle press - no (cannot be active) no put register
2279 * right press - yes start or extend yes CTRL-O
2280 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2281 *
2282 * (1) only if mouse pointer moved since press
2283 * (2) only if click is in same buffer
2284 *
2285 * Return TRUE if start_arrow() should be called for edit mode.
2286 */
2287 int
2288do_mouse(oap, c, dir, count, fixindent)
2289 oparg_T *oap; /* operator argument, can be NULL */
2290 int c; /* K_LEFTMOUSE, etc */
2291 int dir; /* Direction to 'put' if necessary */
2292 long count;
2293 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2294{
2295 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2296 static int got_click = FALSE; /* got a click some time back */
2297
2298 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2299 int is_click; /* If FALSE it's a drag or release event */
2300 int is_drag; /* If TRUE it's a drag event */
2301 int jump_flags = 0; /* flags for jump_to_mouse() */
2302 pos_T start_visual;
2303 int moved; /* Has cursor moved? */
2304 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002305#ifdef FEAT_WINDOWS
2306 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308#ifdef FEAT_VERTSPLIT
2309 int in_sep_line; /* mouse in vertical separator line */
2310#endif
2311 int c1, c2;
2312#if defined(FEAT_FOLDING)
2313 pos_T save_cursor;
2314#endif
2315 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 static pos_T orig_cursor;
2317 colnr_T leftcol, rightcol;
2318 pos_T end_visual;
2319 int diff;
2320 int old_active = VIsual_active;
2321 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 int regname;
2323
2324#if defined(FEAT_FOLDING)
2325 save_cursor = curwin->w_cursor;
2326#endif
2327
2328 /*
2329 * When GUI is active, always recognize mouse events, otherwise:
2330 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2331 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2332 * - For command line and insert mode 'mouse' is checked before calling
2333 * do_mouse().
2334 */
2335 if (do_always)
2336 do_always = FALSE;
2337 else
2338#ifdef FEAT_GUI
2339 if (!gui.in_use)
2340#endif
2341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (VIsual_active)
2343 {
2344 if (!mouse_has(MOUSE_VISUAL))
2345 return FALSE;
2346 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002347 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return FALSE;
2349 }
2350
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002351 for (;;)
2352 {
2353 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2354 if (is_drag)
2355 {
2356 /* If the next character is the same mouse event then use that
2357 * one. Speeds up dragging the status line. */
2358 if (vpeekc() != NUL)
2359 {
2360 int nc;
2361 int save_mouse_row = mouse_row;
2362 int save_mouse_col = mouse_col;
2363
2364 /* Need to get the character, peeking doesn't get the actual
2365 * one. */
2366 nc = safe_vgetc();
2367 if (c == nc)
2368 continue;
2369 vungetc(nc);
2370 mouse_row = save_mouse_row;
2371 mouse_col = save_mouse_col;
2372 }
2373 }
2374 break;
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377#ifdef FEAT_MOUSESHAPE
2378 /* May have stopped dragging the status or separator line. The pointer is
2379 * most likely still on the status or separator line. */
2380 if (!is_drag && drag_status_line)
2381 {
2382 drag_status_line = FALSE;
2383 update_mouseshape(SHAPE_IDX_STATUS);
2384 }
2385# ifdef FEAT_VERTSPLIT
2386 if (!is_drag && drag_sep_line)
2387 {
2388 drag_sep_line = FALSE;
2389 update_mouseshape(SHAPE_IDX_VSEP);
2390 }
2391# endif
2392#endif
2393
2394 /*
2395 * Ignore drag and release events if we didn't get a click.
2396 */
2397 if (is_click)
2398 got_click = TRUE;
2399 else
2400 {
2401 if (!got_click) /* didn't get click, ignore */
2402 return FALSE;
2403 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002406#ifdef FEAT_WINDOWS
2407 if (in_tab_line)
2408 {
2409 in_tab_line = FALSE;
2410 return FALSE;
2411 }
2412#endif
2413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 }
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 /*
2417 * CTRL right mouse button does CTRL-T
2418 */
2419 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2420 {
2421 if (State & INSERT)
2422 stuffcharReadbuff(Ctrl_O);
2423 if (count > 1)
2424 stuffnumReadbuff(count);
2425 stuffcharReadbuff(Ctrl_T);
2426 got_click = FALSE; /* ignore drag&release now */
2427 return FALSE;
2428 }
2429
2430 /*
2431 * CTRL only works with left mouse button
2432 */
2433 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2434 return FALSE;
2435
2436 /*
2437 * When a modifier is down, ignore drag and release events, as well as
2438 * multiple clicks and the middle mouse button.
2439 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2440 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002441 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2442 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 && (!is_click
2444 || (mod_mask & MOD_MASK_MULTI_CLICK)
2445 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002446 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 && mouse_model_popup()
2448 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002449 && !((mod_mask & MOD_MASK_ALT)
2450 && !mouse_model_popup()
2451 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 )
2453 return FALSE;
2454
2455 /*
2456 * If the button press was used as the movement command for an operator
2457 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2458 * drag/release events.
2459 */
2460 if (!is_click && which_button == MOUSE_MIDDLE)
2461 return FALSE;
2462
2463 if (oap != NULL)
2464 regname = oap->regname;
2465 else
2466 regname = 0;
2467
2468 /*
2469 * Middle mouse button does a 'put' of the selected text
2470 */
2471 if (which_button == MOUSE_MIDDLE)
2472 {
2473 if (State == NORMAL)
2474 {
2475 /*
2476 * If an operator was pending, we don't know what the user wanted
2477 * to do. Go back to normal mode: Clear the operator and beep().
2478 */
2479 if (oap != NULL && oap->op_type != OP_NOP)
2480 {
2481 clearopbeep(oap);
2482 return FALSE;
2483 }
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 /*
2486 * If visual was active, yank the highlighted text and put it
2487 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002488 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 if (VIsual_active)
2491 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002492 if (VIsual_select)
2493 {
2494 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002495 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002496 }
2497 else
2498 {
2499 stuffcharReadbuff('y');
2500 stuffcharReadbuff(K_MIDDLEMOUSE);
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 do_always = TRUE; /* ignore 'mouse' setting next time */
2503 return FALSE;
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 /*
2506 * The rest is below jump_to_mouse()
2507 */
2508 }
2509
2510 else if ((State & INSERT) == 0)
2511 return FALSE;
2512
2513 /*
2514 * Middle click in insert mode doesn't move the mouse, just insert the
2515 * contents of a register. '.' register is special, can't insert that
2516 * with do_put().
2517 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2518 * happens for the GUI).
2519 */
2520 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2521 {
2522 if (regname == '.')
2523 insert_reg(regname, TRUE);
2524 else
2525 {
2526#ifdef FEAT_CLIPBOARD
2527 if (clip_star.available && regname == 0)
2528 regname = '*';
2529#endif
2530 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2531 insert_reg(regname, TRUE);
2532 else
2533 {
2534 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2535
2536 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2537 AppendCharToRedobuff(Ctrl_R);
2538 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2539 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2540 }
2541 }
2542 return FALSE;
2543 }
2544 }
2545
2546 /* When dragging or button-up stay in the same window. */
2547 if (!is_click)
2548 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2549
2550 start_visual.lnum = 0;
2551
Bram Moolenaarf740b292006-02-16 22:11:02 +00002552#ifdef FEAT_WINDOWS
2553 /* Check for clicking in the tab page line. */
2554 if (mouse_row == 0 && firstwin->w_winrow > 0)
2555 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002556 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002557 {
2558 if (in_tab_line)
2559 {
2560 c1 = TabPageIdxs[mouse_col];
2561 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2562 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002563 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002564 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002565
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 /* click in a tab selects that tab page */
2567 if (is_click
2568# ifdef FEAT_CMDWIN
2569 && cmdwin_type == 0
2570# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002571 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002572 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002573 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002574 c1 = TabPageIdxs[mouse_col];
2575 if (c1 >= 0)
2576 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002577 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2578 {
2579 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002581 tabpage_new();
2582 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2583 }
2584 else
2585 {
2586 /* Go to specified tab page, or next one if not clicking
2587 * on a label. */
2588 goto_tabpage(c1);
2589
2590 /* It's like clicking on the status line of a window. */
2591 if (curwin != old_curwin)
2592 end_visual_mode();
2593 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594 }
2595 else if (c1 < 0)
2596 {
2597 tabpage_T *tp;
2598
2599 /* Close the current or specified tab page. */
2600 if (c1 == -999)
2601 tp = curtab;
2602 else
2603 tp = find_tabpage(-c1);
2604 if (tp == curtab)
2605 {
2606 if (first_tabpage->tp_next != NULL)
2607 tabpage_close(FALSE);
2608 }
2609 else if (tp != NULL)
2610 tabpage_close_other(tp, FALSE);
2611 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002612 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002615 else if (is_drag && in_tab_line)
2616 {
2617 c1 = TabPageIdxs[mouse_col];
2618 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2619 return FALSE;
2620 }
2621
Bram Moolenaarf740b292006-02-16 22:11:02 +00002622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 /*
2625 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2626 * right button up -> pop-up menu
2627 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002628 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 */
2630 if (mouse_model_popup())
2631 {
2632 if (which_button == MOUSE_RIGHT
2633 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2634 {
2635 /*
2636 * NOTE: Ignore right button down and drag mouse events.
2637 * Windows only shows the popup menu on the button up event.
2638 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002639#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2640 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (!is_click)
2642 return FALSE;
2643#endif
2644#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2645 if (is_click || is_drag)
2646 return FALSE;
2647#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002648#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2650 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2651 if (gui.in_use)
2652 {
2653 jump_flags = 0;
2654 if (STRCMP(p_mousem, "popup_setpos") == 0)
2655 {
2656 /* First set the cursor position before showing the popup
2657 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (VIsual_active)
2659 {
2660 pos_T m_pos;
2661
2662 /*
2663 * set MOUSE_MAY_STOP_VIS if we are outside the
2664 * selection or the current window (might have false
2665 * negative here)
2666 */
2667 if (mouse_row < W_WINROW(curwin)
2668 || mouse_row
2669 > (W_WINROW(curwin) + curwin->w_height))
2670 jump_flags = MOUSE_MAY_STOP_VIS;
2671 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2672 jump_flags = MOUSE_MAY_STOP_VIS;
2673 else
2674 {
2675 if ((lt(curwin->w_cursor, VIsual)
2676 && (lt(m_pos, curwin->w_cursor)
2677 || lt(VIsual, m_pos)))
2678 || (lt(VIsual, curwin->w_cursor)
2679 && (lt(m_pos, VIsual)
2680 || lt(curwin->w_cursor, m_pos))))
2681 {
2682 jump_flags = MOUSE_MAY_STOP_VIS;
2683 }
2684 else if (VIsual_mode == Ctrl_V)
2685 {
2686 getvcols(curwin, &curwin->w_cursor, &VIsual,
2687 &leftcol, &rightcol);
2688 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2689 if (m_pos.col < leftcol || m_pos.col > rightcol)
2690 jump_flags = MOUSE_MAY_STOP_VIS;
2691 }
2692 }
2693 }
2694 else
2695 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 if (jump_flags)
2698 {
2699 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002700 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 setcursor();
2702 out_flush(); /* Update before showing popup menu */
2703 }
2704# ifdef FEAT_MENU
2705 gui_show_popupmenu();
2706# endif
2707 return (jump_flags & CURSOR_MOVED) != 0;
2708 }
2709 else
2710 return FALSE;
2711#else
2712 return FALSE;
2713#endif
2714 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002715 if (which_button == MOUSE_LEFT
2716 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 which_button = MOUSE_RIGHT;
2719 mod_mask &= ~MOD_MASK_SHIFT;
2720 }
2721 }
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if ((State & (NORMAL | INSERT))
2724 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2725 {
2726 if (which_button == MOUSE_LEFT)
2727 {
2728 if (is_click)
2729 {
2730 /* stop Visual mode for a left click in a window, but not when
2731 * on a status line */
2732 if (VIsual_active)
2733 jump_flags |= MOUSE_MAY_STOP_VIS;
2734 }
2735 else if (mouse_has(MOUSE_VISUAL))
2736 jump_flags |= MOUSE_MAY_VIS;
2737 }
2738 else if (which_button == MOUSE_RIGHT)
2739 {
2740 if (is_click && VIsual_active)
2741 {
2742 /*
2743 * Remember the start and end of visual before moving the
2744 * cursor.
2745 */
2746 if (lt(curwin->w_cursor, VIsual))
2747 {
2748 start_visual = curwin->w_cursor;
2749 end_visual = VIsual;
2750 }
2751 else
2752 {
2753 start_visual = VIsual;
2754 end_visual = curwin->w_cursor;
2755 }
2756 }
2757 jump_flags |= MOUSE_FOCUS;
2758 if (mouse_has(MOUSE_VISUAL))
2759 jump_flags |= MOUSE_MAY_VIS;
2760 }
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 /*
2764 * If an operator is pending, ignore all drags and releases until the
2765 * next mouse click.
2766 */
2767 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2768 {
2769 got_click = FALSE;
2770 oap->motion_type = MCHAR;
2771 }
2772
2773 /* When releasing the button let jump_to_mouse() know. */
2774 if (!is_click && !is_drag)
2775 jump_flags |= MOUSE_RELEASED;
2776
2777 /*
2778 * JUMP!
2779 */
2780 jump_flags = jump_to_mouse(jump_flags,
2781 oap == NULL ? NULL : &(oap->inclusive), which_button);
2782 moved = (jump_flags & CURSOR_MOVED);
2783 in_status_line = (jump_flags & IN_STATUS_LINE);
2784#ifdef FEAT_VERTSPLIT
2785 in_sep_line = (jump_flags & IN_SEP_LINE);
2786#endif
2787
2788#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002789 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2791 {
2792 int key = KEY2TERMCAP1(c);
2793
2794 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2795 || key == (int)KE_RIGHTRELEASE)
2796 netbeans_button_release(which_button);
2797 }
2798#endif
2799
2800 /* When jumping to another window, clear a pending operator. That's a bit
2801 * friendlier than beeping and not jumping to that window. */
2802 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2803 clearop(oap);
2804
2805#ifdef FEAT_FOLDING
2806 if (mod_mask == 0
2807 && !is_drag
2808 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2809 && which_button == MOUSE_LEFT)
2810 {
2811 /* open or close a fold at this line */
2812 if (jump_flags & MOUSE_FOLD_OPEN)
2813 openFold(curwin->w_cursor.lnum, 1L);
2814 else
2815 closeFold(curwin->w_cursor.lnum, 1L);
2816 /* don't move the cursor if still in the same window */
2817 if (curwin == old_curwin)
2818 curwin->w_cursor = save_cursor;
2819 }
2820#endif
2821
2822#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2823 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2824 {
2825 clip_modeless(which_button, is_click, is_drag);
2826 return FALSE;
2827 }
2828#endif
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002831 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 if (VIsual_active && is_drag && p_so)
2833 {
2834 /* In the very first line, allow scrolling one line */
2835 if (mouse_row == 0)
2836 mouse_dragging = 2;
2837 else
2838 mouse_dragging = 1;
2839 }
2840
2841 /* When dragging the mouse above the window, scroll down. */
2842 if (is_drag && mouse_row < 0 && !in_status_line)
2843 {
2844 scroll_redraw(FALSE, 1L);
2845 mouse_row = 0;
2846 }
2847
2848 if (start_visual.lnum) /* right click in visual mode */
2849 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002850 /* When ALT is pressed make Visual mode blockwise. */
2851 if (mod_mask & MOD_MASK_ALT)
2852 VIsual_mode = Ctrl_V;
2853
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 /*
2855 * In Visual-block mode, divide the area in four, pick up the corner
2856 * that is in the quarter that the cursor is in.
2857 */
2858 if (VIsual_mode == Ctrl_V)
2859 {
2860 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2861 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2862 end_visual.col = leftcol;
2863 else
2864 end_visual.col = rightcol;
2865 if (curwin->w_cursor.lnum <
2866 (start_visual.lnum + end_visual.lnum) / 2)
2867 end_visual.lnum = end_visual.lnum;
2868 else
2869 end_visual.lnum = start_visual.lnum;
2870
2871 /* move VIsual to the right column */
2872 start_visual = curwin->w_cursor; /* save the cursor pos */
2873 curwin->w_cursor = end_visual;
2874 coladvance(end_visual.col);
2875 VIsual = curwin->w_cursor;
2876 curwin->w_cursor = start_visual; /* restore the cursor */
2877 }
2878 else
2879 {
2880 /*
2881 * If the click is before the start of visual, change the start.
2882 * If the click is after the end of visual, change the end. If
2883 * the click is inside the visual, change the closest side.
2884 */
2885 if (lt(curwin->w_cursor, start_visual))
2886 VIsual = end_visual;
2887 else if (lt(end_visual, curwin->w_cursor))
2888 VIsual = start_visual;
2889 else
2890 {
2891 /* In the same line, compare column number */
2892 if (end_visual.lnum == start_visual.lnum)
2893 {
2894 if (curwin->w_cursor.col - start_visual.col >
2895 end_visual.col - curwin->w_cursor.col)
2896 VIsual = start_visual;
2897 else
2898 VIsual = end_visual;
2899 }
2900
2901 /* In different lines, compare line number */
2902 else
2903 {
2904 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2905 (end_visual.lnum - curwin->w_cursor.lnum);
2906
2907 if (diff > 0) /* closest to end */
2908 VIsual = start_visual;
2909 else if (diff < 0) /* closest to start */
2910 VIsual = end_visual;
2911 else /* in the middle line */
2912 {
2913 if (curwin->w_cursor.col <
2914 (start_visual.col + end_visual.col) / 2)
2915 VIsual = end_visual;
2916 else
2917 VIsual = start_visual;
2918 }
2919 }
2920 }
2921 }
2922 }
2923 /*
2924 * If Visual mode started in insert mode, execute "CTRL-O"
2925 */
2926 else if ((State & INSERT) && VIsual_active)
2927 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
2929 /*
2930 * Middle mouse click: Put text before cursor.
2931 */
2932 if (which_button == MOUSE_MIDDLE)
2933 {
2934#ifdef FEAT_CLIPBOARD
2935 if (clip_star.available && regname == 0)
2936 regname = '*';
2937#endif
2938 if (yank_register_mline(regname))
2939 {
2940 if (mouse_past_bottom)
2941 dir = FORWARD;
2942 }
2943 else if (mouse_past_eol)
2944 dir = FORWARD;
2945
2946 if (fixindent)
2947 {
2948 c1 = (dir == BACKWARD) ? '[' : ']';
2949 c2 = 'p';
2950 }
2951 else
2952 {
2953 c1 = (dir == FORWARD) ? 'p' : 'P';
2954 c2 = NUL;
2955 }
2956 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2957
2958 /*
2959 * Remember where the paste started, so in edit() Insstart can be set
2960 * to this position
2961 */
2962 if (restart_edit != 0)
2963 where_paste_started = curwin->w_cursor;
2964 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2965 }
2966
2967#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2968 /*
2969 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2970 * error under the mouse pointer.
2971 */
2972 else if (((mod_mask & MOD_MASK_CTRL)
2973 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2974 && bt_quickfix(curbuf))
2975 {
2976 if (State & INSERT)
2977 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002978 if (curwin->w_llist_ref == NULL) /* quickfix window */
2979 stuffReadbuff((char_u *)":.cc\n");
2980 else /* location list window */
2981 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 got_click = FALSE; /* ignore drag&release now */
2983 }
2984#endif
2985
2986 /*
2987 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2988 * under the mouse pointer.
2989 */
2990 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2991 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2992 {
2993 if (State & INSERT)
2994 stuffcharReadbuff(Ctrl_O);
2995 stuffcharReadbuff(Ctrl_RSB);
2996 got_click = FALSE; /* ignore drag&release now */
2997 }
2998
2999 /*
3000 * Shift-Mouse click searches for the next occurrence of the word under
3001 * the mouse pointer
3002 */
3003 else if ((mod_mask & MOD_MASK_SHIFT))
3004 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003005 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 stuffcharReadbuff(Ctrl_O);
3007 if (which_button == MOUSE_LEFT)
3008 stuffcharReadbuff('*');
3009 else /* MOUSE_RIGHT */
3010 stuffcharReadbuff('#');
3011 }
3012
3013 /* Handle double clicks, unless on status line */
3014 else if (in_status_line)
3015 {
3016#ifdef FEAT_MOUSESHAPE
3017 if ((is_drag || is_click) && !drag_status_line)
3018 {
3019 drag_status_line = TRUE;
3020 update_mouseshape(-1);
3021 }
3022#endif
3023 }
3024#ifdef FEAT_VERTSPLIT
3025 else if (in_sep_line)
3026 {
3027# ifdef FEAT_MOUSESHAPE
3028 if ((is_drag || is_click) && !drag_sep_line)
3029 {
3030 drag_sep_line = TRUE;
3031 update_mouseshape(-1);
3032 }
3033# endif
3034 }
3035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3037 && mouse_has(MOUSE_VISUAL))
3038 {
3039 if (is_click || !VIsual_active)
3040 {
3041 if (VIsual_active)
3042 orig_cursor = VIsual;
3043 else
3044 {
3045 check_visual_highlight();
3046 VIsual = curwin->w_cursor;
3047 orig_cursor = VIsual;
3048 VIsual_active = TRUE;
3049 VIsual_reselect = TRUE;
3050 /* start Select mode if 'selectmode' contains "mouse" */
3051 may_start_select('o');
3052 setmouse();
3053 }
3054 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003055 {
3056 /* Double click with ALT pressed makes it blockwise. */
3057 if (mod_mask & MOD_MASK_ALT)
3058 VIsual_mode = Ctrl_V;
3059 else
3060 VIsual_mode = 'v';
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3063 VIsual_mode = 'V';
3064 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3065 VIsual_mode = Ctrl_V;
3066#ifdef FEAT_CLIPBOARD
3067 /* Make sure the clipboard gets updated. Needed because start and
3068 * end may still be the same, and the selection needs to be owned */
3069 clip_star.vmode = NUL;
3070#endif
3071 }
3072 /*
3073 * A double click selects a word or a block.
3074 */
3075 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3076 {
3077 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003078 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 if (is_click)
3081 {
3082 /* If the character under the cursor (skipping white space) is
3083 * not a word character, try finding a match and select a (),
3084 * {}, [], #if/#endif, etc. block. */
3085 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003086 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 inc(&end_visual);
3088 if (oap != NULL)
3089 oap->motion_type = MCHAR;
3090 if (oap != NULL
3091 && VIsual_mode == 'v'
3092 && !vim_iswordc(gchar_pos(&end_visual))
3093 && equalpos(curwin->w_cursor, VIsual)
3094 && (pos = findmatch(oap, NUL)) != NULL)
3095 {
3096 curwin->w_cursor = *pos;
3097 if (oap->motion_type == MLINE)
3098 VIsual_mode = 'V';
3099 else if (*p_sel == 'e')
3100 {
3101 if (lt(curwin->w_cursor, VIsual))
3102 ++VIsual.col;
3103 else
3104 ++curwin->w_cursor.col;
3105 }
3106 }
3107 }
3108
3109 if (pos == NULL && (is_click || is_drag))
3110 {
3111 /* When not found a match or when dragging: extend to include
3112 * a word. */
3113 if (lt(curwin->w_cursor, orig_cursor))
3114 {
3115 find_start_of_word(&curwin->w_cursor);
3116 find_end_of_word(&VIsual);
3117 }
3118 else
3119 {
3120 find_start_of_word(&VIsual);
3121 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3122#ifdef FEAT_MBYTE
3123 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003124 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#else
3126 ++curwin->w_cursor.col;
3127#endif
3128 find_end_of_word(&curwin->w_cursor);
3129 }
3130 }
3131 curwin->w_set_curswant = TRUE;
3132 }
3133 if (is_click)
3134 redraw_curbuf_later(INVERTED); /* update the inversion */
3135 }
3136 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003137 {
3138 if (mod_mask & MOD_MASK_ALT)
3139 VIsual_mode = Ctrl_V;
3140 else
3141 VIsual_mode = 'v';
3142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003145 if ((!VIsual_active && old_active && mode_displayed)
3146 || (VIsual_active && p_smd && msg_silent == 0
3147 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 return moved;
3151}
3152
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153/*
3154 * Move "pos" back to the start of the word it's in.
3155 */
3156 static void
3157find_start_of_word(pos)
3158 pos_T *pos;
3159{
3160 char_u *line;
3161 int cclass;
3162 int col;
3163
3164 line = ml_get(pos->lnum);
3165 cclass = get_mouse_class(line + pos->col);
3166
3167 while (pos->col > 0)
3168 {
3169 col = pos->col - 1;
3170#ifdef FEAT_MBYTE
3171 col -= (*mb_head_off)(line, line + col);
3172#endif
3173 if (get_mouse_class(line + col) != cclass)
3174 break;
3175 pos->col = col;
3176 }
3177}
3178
3179/*
3180 * Move "pos" forward to the end of the word it's in.
3181 * When 'selection' is "exclusive", the position is just after the word.
3182 */
3183 static void
3184find_end_of_word(pos)
3185 pos_T *pos;
3186{
3187 char_u *line;
3188 int cclass;
3189 int col;
3190
3191 line = ml_get(pos->lnum);
3192 if (*p_sel == 'e' && pos->col > 0)
3193 {
3194 --pos->col;
3195#ifdef FEAT_MBYTE
3196 pos->col -= (*mb_head_off)(line, line + pos->col);
3197#endif
3198 }
3199 cclass = get_mouse_class(line + pos->col);
3200 while (line[pos->col] != NUL)
3201 {
3202#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003203 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#else
3205 col = pos->col + 1;
3206#endif
3207 if (get_mouse_class(line + col) != cclass)
3208 {
3209 if (*p_sel == 'e')
3210 pos->col = col;
3211 break;
3212 }
3213 pos->col = col;
3214 }
3215}
3216
3217/*
3218 * Get class of a character for selection: same class means same word.
3219 * 0: blank
3220 * 1: punctuation groups
3221 * 2: normal word character
3222 * >2: multi-byte word character.
3223 */
3224 static int
3225get_mouse_class(p)
3226 char_u *p;
3227{
3228 int c;
3229
3230#ifdef FEAT_MBYTE
3231 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3232 return mb_get_class(p);
3233#endif
3234
3235 c = *p;
3236 if (c == ' ' || c == '\t')
3237 return 0;
3238
3239 if (vim_iswordc(c))
3240 return 2;
3241
3242 /*
3243 * There are a few special cases where we want certain combinations of
3244 * characters to be considered as a single word. These are things like
3245 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003246 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
3248 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3249 return 1;
3250 return c;
3251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif /* FEAT_MOUSE */
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254/*
3255 * Check if highlighting for visual mode is possible, give a warning message
3256 * if not.
3257 */
3258 void
3259check_visual_highlight()
3260{
3261 static int did_check = FALSE;
3262
3263 if (full_screen)
3264 {
3265 if (!did_check && hl_attr(HLF_V) == 0)
3266 MSG(_("Warning: terminal cannot highlight"));
3267 did_check = TRUE;
3268 }
3269}
3270
3271/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003272 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 * This function should ALWAYS be called to end Visual mode, except from
3274 * do_pending_operator().
3275 */
3276 void
3277end_visual_mode()
3278{
3279#ifdef FEAT_CLIPBOARD
3280 /*
3281 * If we are using the clipboard, then remember what was selected in case
3282 * we need to paste it somewhere while we still own the selection.
3283 * Only do this when the clipboard is already owned. Don't want to grab
3284 * the selection when hitting ESC.
3285 */
3286 if (clip_star.available && clip_star.owned)
3287 clip_auto_select();
3288#endif
3289
3290 VIsual_active = FALSE;
3291#ifdef FEAT_MOUSE
3292 setmouse();
3293 mouse_dragging = 0;
3294#endif
3295
3296 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003297 curbuf->b_visual.vi_mode = VIsual_mode;
3298 curbuf->b_visual.vi_start = VIsual;
3299 curbuf->b_visual.vi_end = curwin->w_cursor;
3300 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#ifdef FEAT_EVAL
3302 curbuf->b_visual_mode_eval = VIsual_mode;
3303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef FEAT_VIRTUALEDIT
3305 if (!virtual_active())
3306 curwin->w_cursor.coladd = 0;
3307#endif
3308
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003309 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 clear_cmdline = TRUE; /* unshow visual mode later */
3311#ifdef FEAT_CMDL_INFO
3312 else
3313 clear_showcmd();
3314#endif
3315
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003316 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317}
3318
3319/*
3320 * Reset VIsual_active and VIsual_reselect.
3321 */
3322 void
3323reset_VIsual_and_resel()
3324{
3325 if (VIsual_active)
3326 {
3327 end_visual_mode();
3328 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3329 }
3330 VIsual_reselect = FALSE;
3331}
3332
3333/*
3334 * Reset VIsual_active and VIsual_reselect if it's set.
3335 */
3336 void
3337reset_VIsual()
3338{
3339 if (VIsual_active)
3340 {
3341 end_visual_mode();
3342 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3343 VIsual_reselect = FALSE;
3344 }
3345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003347#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3349
3350/*
3351 * Check for a balloon-eval special item to include when searching for an
3352 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3353 * Returns TRUE if the character at "*ptr" should be included.
3354 * "dir" is FORWARD or BACKWARD, the direction of searching.
3355 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3356 * "bnp" points to a counter for square brackets.
3357 */
3358 static int
3359find_is_eval_item(ptr, colp, bnp, dir)
3360 char_u *ptr;
3361 int *colp;
3362 int *bnp;
3363 int dir;
3364{
3365 /* Accept everything inside []. */
3366 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3367 ++*bnp;
3368 if (*bnp > 0)
3369 {
3370 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3371 --*bnp;
3372 return TRUE;
3373 }
3374
3375 /* skip over "s.var" */
3376 if (*ptr == '.')
3377 return TRUE;
3378
3379 /* two-character item: s->var */
3380 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3381 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3382 {
3383 *colp += dir;
3384 return TRUE;
3385 }
3386 return FALSE;
3387}
3388#endif
3389
3390/*
3391 * Find the identifier under or to the right of the cursor.
3392 * "find_type" can have one of three values:
3393 * FIND_IDENT: find an identifier (keyword)
3394 * FIND_STRING: find any non-white string
3395 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003396 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 *
3398 * There are three steps:
3399 * 1. Search forward for the start of an identifier/string. Doesn't move if
3400 * already on one.
3401 * 2. Search backward for the start of this identifier/string.
3402 * This doesn't match the real Vi but I like it a little better and it
3403 * shouldn't bother anyone.
3404 * 3. Search forward to the end of this identifier/string.
3405 * When FIND_IDENT isn't defined, we backup until a blank.
3406 *
3407 * Returns the length of the string, or zero if no string is found.
3408 * If a string is found, a pointer to the string is put in "*string". This
3409 * string is not always NUL terminated.
3410 */
3411 int
3412find_ident_under_cursor(string, find_type)
3413 char_u **string;
3414 int find_type;
3415{
3416 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3417 curwin->w_cursor.col, string, find_type);
3418}
3419
3420/*
3421 * Like find_ident_under_cursor(), but for any window and any position.
3422 * However: Uses 'iskeyword' from the current window!.
3423 */
3424 int
3425find_ident_at_pos(wp, lnum, startcol, string, find_type)
3426 win_T *wp;
3427 linenr_T lnum;
3428 colnr_T startcol;
3429 char_u **string;
3430 int find_type;
3431{
3432 char_u *ptr;
3433 int col = 0; /* init to shut up GCC */
3434 int i;
3435#ifdef FEAT_MBYTE
3436 int this_class = 0;
3437 int prev_class;
3438 int prevcol;
3439#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003440#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 int bn = 0; /* bracket nesting */
3442#endif
3443
3444 /*
3445 * if i == 0: try to find an identifier
3446 * if i == 1: try to find any non-white string
3447 */
3448 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3449 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3450 {
3451 /*
3452 * 1. skip to start of identifier/string
3453 */
3454 col = startcol;
3455#ifdef FEAT_MBYTE
3456 if (has_mbyte)
3457 {
3458 while (ptr[col] != NUL)
3459 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /* Stop at a ']' to evaluate "a[x]". */
3462 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3463 break;
3464# endif
3465 this_class = mb_get_class(ptr + col);
3466 if (this_class != 0 && (i == 1 || this_class != 1))
3467 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003468 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 }
3471 else
3472#endif
3473 while (ptr[col] != NUL
3474 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3477# endif
3478 )
3479 ++col;
3480
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003481#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* When starting on a ']' count it, so that we include the '['. */
3483 bn = ptr[col] == ']';
3484#endif
3485
3486 /*
3487 * 2. Back up to start of identifier/string.
3488 */
3489#ifdef FEAT_MBYTE
3490 if (has_mbyte)
3491 {
3492 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3495 this_class = mb_get_class((char_u *)"a");
3496 else
3497# endif
3498 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003499 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3502 prev_class = mb_get_class(ptr + prevcol);
3503 if (this_class != prev_class
3504 && (i == 0
3505 || prev_class == 0
3506 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 && (!(find_type & FIND_EVAL)
3509 || prevcol == 0
3510 || !find_is_eval_item(ptr + prevcol, &prevcol,
3511 &bn, BACKWARD))
3512# endif
3513 )
3514 break;
3515 col = prevcol;
3516 }
3517
3518 /* If we don't want just any old string, or we've found an
3519 * identifier, stop searching. */
3520 if (this_class > 2)
3521 this_class = 2;
3522 if (!(find_type & FIND_STRING) || this_class == 2)
3523 break;
3524 }
3525 else
3526#endif
3527 {
3528 while (col > 0
3529 && ((i == 0
3530 ? vim_iswordc(ptr[col - 1])
3531 : (!vim_iswhite(ptr[col - 1])
3532 && (!(find_type & FIND_IDENT)
3533 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003534#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 || ((find_type & FIND_EVAL)
3536 && col > 1
3537 && find_is_eval_item(ptr + col - 1, &col,
3538 &bn, BACKWARD))
3539#endif
3540 ))
3541 --col;
3542
3543 /* If we don't want just any old string, or we've found an
3544 * identifier, stop searching. */
3545 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3546 break;
3547 }
3548 }
3549
3550 if (ptr[col] == NUL || (i == 0 && (
3551#ifdef FEAT_MBYTE
3552 has_mbyte ? this_class != 2 :
3553#endif
3554 !vim_iswordc(ptr[col]))))
3555 {
3556 /*
3557 * didn't find an identifier or string
3558 */
3559 if (find_type & FIND_STRING)
3560 EMSG(_("E348: No string under cursor"));
3561 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003562 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return 0;
3564 }
3565 ptr += col;
3566 *string = ptr;
3567
3568 /*
3569 * 3. Find the end if the identifier/string.
3570 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003571#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 bn = 0;
3573 startcol -= col;
3574#endif
3575 col = 0;
3576#ifdef FEAT_MBYTE
3577 if (has_mbyte)
3578 {
3579 /* Search for point of changing multibyte character class. */
3580 this_class = mb_get_class(ptr);
3581 while (ptr[col] != NUL
3582 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3583 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003584# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 || ((find_type & FIND_EVAL)
3586 && col <= (int)startcol
3587 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3588# endif
3589 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003590 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592 else
3593#endif
3594 while ((i == 0 ? vim_iswordc(ptr[col])
3595 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 || ((find_type & FIND_EVAL)
3598 && col <= (int)startcol
3599 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3600# endif
3601 )
3602 {
3603 ++col;
3604 }
3605
3606 return col;
3607}
3608
3609/*
3610 * Prepare for redo of a normal command.
3611 */
3612 static void
3613prep_redo_cmd(cap)
3614 cmdarg_T *cap;
3615{
3616 prep_redo(cap->oap->regname, cap->count0,
3617 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3618}
3619
3620/*
3621 * Prepare for redo of any command.
3622 * Note that only the last argument can be a multi-byte char.
3623 */
3624 static void
3625prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3626 int regname;
3627 long num;
3628 int cmd1;
3629 int cmd2;
3630 int cmd3;
3631 int cmd4;
3632 int cmd5;
3633{
3634 ResetRedobuff();
3635 if (regname != 0) /* yank from specified buffer */
3636 {
3637 AppendCharToRedobuff('"');
3638 AppendCharToRedobuff(regname);
3639 }
3640 if (num)
3641 AppendNumberToRedobuff(num);
3642
3643 if (cmd1 != NUL)
3644 AppendCharToRedobuff(cmd1);
3645 if (cmd2 != NUL)
3646 AppendCharToRedobuff(cmd2);
3647 if (cmd3 != NUL)
3648 AppendCharToRedobuff(cmd3);
3649 if (cmd4 != NUL)
3650 AppendCharToRedobuff(cmd4);
3651 if (cmd5 != NUL)
3652 AppendCharToRedobuff(cmd5);
3653}
3654
3655/*
3656 * check for operator active and clear it
3657 *
3658 * return TRUE if operator was active
3659 */
3660 static int
3661checkclearop(oap)
3662 oparg_T *oap;
3663{
3664 if (oap->op_type == OP_NOP)
3665 return FALSE;
3666 clearopbeep(oap);
3667 return TRUE;
3668}
3669
3670/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003671 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003673 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 static int
3676checkclearopq(oap)
3677 oparg_T *oap;
3678{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003679 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 return FALSE;
3681 clearopbeep(oap);
3682 return TRUE;
3683}
3684
3685 static void
3686clearop(oap)
3687 oparg_T *oap;
3688{
3689 oap->op_type = OP_NOP;
3690 oap->regname = 0;
3691 oap->motion_force = NUL;
3692 oap->use_reg_one = FALSE;
3693}
3694
3695 static void
3696clearopbeep(oap)
3697 oparg_T *oap;
3698{
3699 clearop(oap);
3700 beep_flush();
3701}
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703/*
3704 * Remove the shift modifier from a special key.
3705 */
3706 static void
3707unshift_special(cap)
3708 cmdarg_T *cap;
3709{
3710 switch (cap->cmdchar)
3711 {
3712 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3713 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3714 case K_S_UP: cap->cmdchar = K_UP; break;
3715 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3716 case K_S_HOME: cap->cmdchar = K_HOME; break;
3717 case K_S_END: cap->cmdchar = K_END; break;
3718 }
3719 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3720}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3723/*
3724 * Routines for displaying a partly typed command
3725 */
3726
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003727#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728static char_u showcmd_buf[SHOWCMD_BUFLEN];
3729static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3730static int showcmd_is_clear = TRUE;
3731static int showcmd_visual = FALSE;
3732
3733static void display_showcmd __ARGS((void));
3734
3735 void
3736clear_showcmd()
3737{
3738 if (!p_sc)
3739 return;
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (VIsual_active && !char_avail())
3742 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003743 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 long lines;
3745 colnr_T leftcol, rightcol;
3746 linenr_T top, bot;
3747
3748 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003749 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 top = VIsual.lnum;
3752 bot = curwin->w_cursor.lnum;
3753 }
3754 else
3755 {
3756 top = curwin->w_cursor.lnum;
3757 bot = VIsual.lnum;
3758 }
3759# ifdef FEAT_FOLDING
3760 /* Include closed folds as a whole. */
3761 hasFolding(top, &top, NULL);
3762 hasFolding(bot, NULL, &bot);
3763# endif
3764 lines = bot - top + 1;
3765
3766 if (VIsual_mode == Ctrl_V)
3767 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003768# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003769 char_u *saved_sbr = p_sbr;
3770
3771 /* Make 'sbr' empty for a moment to get the correct size. */
3772 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003775# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003776 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3779 (long)(rightcol - leftcol + 1));
3780 }
3781 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3782 sprintf((char *)showcmd_buf, "%ld", lines);
3783 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003784 {
3785 char_u *s, *e;
3786 int l;
3787 int bytes = 0;
3788 int chars = 0;
3789
3790 if (cursor_bot)
3791 {
3792 s = ml_get_pos(&VIsual);
3793 e = ml_get_cursor();
3794 }
3795 else
3796 {
3797 s = ml_get_cursor();
3798 e = ml_get_pos(&VIsual);
3799 }
3800 while ((*p_sel != 'e') ? s <= e : s < e)
3801 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003802# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003803 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003804# else
3805 l = (*s == NUL) ? 0 : 1;
3806# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003807 if (l == 0)
3808 {
3809 ++bytes;
3810 ++chars;
3811 break; /* end of line */
3812 }
3813 bytes += l;
3814 ++chars;
3815 s += l;
3816 }
3817 if (bytes == chars)
3818 sprintf((char *)showcmd_buf, "%d", chars);
3819 else
3820 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3823 showcmd_visual = TRUE;
3824 }
3825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
3827 showcmd_buf[0] = NUL;
3828 showcmd_visual = FALSE;
3829
3830 /* Don't actually display something if there is nothing to clear. */
3831 if (showcmd_is_clear)
3832 return;
3833 }
3834
3835 display_showcmd();
3836}
3837
3838/*
3839 * Add 'c' to string of shown command chars.
3840 * Return TRUE if output has been written (and setcursor() has been called).
3841 */
3842 int
3843add_to_showcmd(c)
3844 int c;
3845{
3846 char_u *p;
3847 int old_len;
3848 int extra_len;
3849 int overflow;
3850#if defined(FEAT_MOUSE)
3851 int i;
3852 static int ignore[] =
3853 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003854# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3856 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 K_IGNORE,
3859 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3860 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3861 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003862 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003864 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 0
3866 };
3867#endif
3868
Bram Moolenaar09df3122006-01-23 22:23:09 +00003869 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return FALSE;
3871
3872 if (showcmd_visual)
3873 {
3874 showcmd_buf[0] = NUL;
3875 showcmd_visual = FALSE;
3876 }
3877
3878#if defined(FEAT_MOUSE)
3879 /* Ignore keys that are scrollbar updates and mouse clicks */
3880 if (IS_SPECIAL(c))
3881 for (i = 0; ignore[i] != 0; ++i)
3882 if (ignore[i] == c)
3883 return FALSE;
3884#endif
3885
3886 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003887 if (*p == ' ')
3888 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 old_len = (int)STRLEN(showcmd_buf);
3890 extra_len = (int)STRLEN(p);
3891 overflow = old_len + extra_len - SHOWCMD_COLS;
3892 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003893 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3894 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 STRCAT(showcmd_buf, p);
3896
3897 if (char_avail())
3898 return FALSE;
3899
3900 display_showcmd();
3901
3902 return TRUE;
3903}
3904
3905 void
3906add_to_showcmd_c(c)
3907 int c;
3908{
3909 if (!add_to_showcmd(c))
3910 setcursor();
3911}
3912
3913/*
3914 * Delete 'len' characters from the end of the shown command.
3915 */
3916 static void
3917del_from_showcmd(len)
3918 int len;
3919{
3920 int old_len;
3921
3922 if (!p_sc)
3923 return;
3924
3925 old_len = (int)STRLEN(showcmd_buf);
3926 if (len > old_len)
3927 len = old_len;
3928 showcmd_buf[old_len - len] = NUL;
3929
3930 if (!char_avail())
3931 display_showcmd();
3932}
3933
3934/*
3935 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3936 * something and there is a partial mapping.
3937 */
3938 void
3939push_showcmd()
3940{
3941 if (p_sc)
3942 STRCPY(old_showcmd_buf, showcmd_buf);
3943}
3944
3945 void
3946pop_showcmd()
3947{
3948 if (!p_sc)
3949 return;
3950
3951 STRCPY(showcmd_buf, old_showcmd_buf);
3952
3953 display_showcmd();
3954}
3955
3956 static void
3957display_showcmd()
3958{
3959 int len;
3960
3961 cursor_off();
3962
3963 len = (int)STRLEN(showcmd_buf);
3964 if (len == 0)
3965 showcmd_is_clear = TRUE;
3966 else
3967 {
3968 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3969 showcmd_is_clear = FALSE;
3970 }
3971
3972 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003973 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3974 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
3976 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3977
3978 setcursor(); /* put cursor back where it belongs */
3979}
3980#endif
3981
3982#ifdef FEAT_SCROLLBIND
3983/*
3984 * When "check" is FALSE, prepare for commands that scroll the window.
3985 * When "check" is TRUE, take care of scroll-binding after the window has
3986 * scrolled. Called from normal_cmd() and edit().
3987 */
3988 void
3989do_check_scrollbind(check)
3990 int check;
3991{
3992 static win_T *old_curwin = NULL;
3993 static linenr_T old_topline = 0;
3994#ifdef FEAT_DIFF
3995 static int old_topfill = 0;
3996#endif
3997 static buf_T *old_buf = NULL;
3998 static colnr_T old_leftcol = 0;
3999
4000 if (check && curwin->w_p_scb)
4001 {
4002 /* If a ":syncbind" command was just used, don't scroll, only reset
4003 * the values. */
4004 if (did_syncbind)
4005 did_syncbind = FALSE;
4006 else if (curwin == old_curwin)
4007 {
4008 /*
4009 * Synchronize other windows, as necessary according to
4010 * 'scrollbind'. Don't do this after an ":edit" command, except
4011 * when 'diff' is set.
4012 */
4013 if ((curwin->w_buffer == old_buf
4014#ifdef FEAT_DIFF
4015 || curwin->w_p_diff
4016#endif
4017 )
4018 && (curwin->w_topline != old_topline
4019#ifdef FEAT_DIFF
4020 || curwin->w_topfill != old_topfill
4021#endif
4022 || curwin->w_leftcol != old_leftcol))
4023 {
4024 check_scrollbind(curwin->w_topline - old_topline,
4025 (long)(curwin->w_leftcol - old_leftcol));
4026 }
4027 }
4028 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4029 {
4030 /*
4031 * When switching between windows, make sure that the relative
4032 * vertical offset is valid for the new window. The relative
4033 * offset is invalid whenever another 'scrollbind' window has
4034 * scrolled to a point that would force the current window to
4035 * scroll past the beginning or end of its buffer. When the
4036 * resync is performed, some of the other 'scrollbind' windows may
4037 * need to jump so that the current window's relative position is
4038 * visible on-screen.
4039 */
4040 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4041 }
4042 curwin->w_scbind_pos = curwin->w_topline;
4043 }
4044
4045 old_curwin = curwin;
4046 old_topline = curwin->w_topline;
4047#ifdef FEAT_DIFF
4048 old_topfill = curwin->w_topfill;
4049#endif
4050 old_buf = curwin->w_buffer;
4051 old_leftcol = curwin->w_leftcol;
4052}
4053
4054/*
4055 * Synchronize any windows that have "scrollbind" set, based on the
4056 * number of rows by which the current window has changed
4057 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4058 */
4059 void
4060check_scrollbind(topline_diff, leftcol_diff)
4061 linenr_T topline_diff;
4062 long leftcol_diff;
4063{
4064 int want_ver;
4065 int want_hor;
4066 win_T *old_curwin = curwin;
4067 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 int old_VIsual_select = VIsual_select;
4069 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 colnr_T tgt_leftcol = curwin->w_leftcol;
4071 long topline;
4072 long y;
4073
4074 /*
4075 * check 'scrollopt' string for vertical and horizontal scroll options
4076 */
4077 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4078#ifdef FEAT_DIFF
4079 want_ver |= old_curwin->w_p_diff;
4080#endif
4081 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4082
4083 /*
4084 * loop through the scrollbound windows and scroll accordingly
4085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4088 {
4089 curbuf = curwin->w_buffer;
4090 /* skip original window and windows with 'noscrollbind' */
4091 if (curwin != old_curwin && curwin->w_p_scb)
4092 {
4093 /*
4094 * do the vertical scroll
4095 */
4096 if (want_ver)
4097 {
4098#ifdef FEAT_DIFF
4099 if (old_curwin->w_p_diff && curwin->w_p_diff)
4100 {
4101 diff_set_topline(old_curwin, curwin);
4102 }
4103 else
4104#endif
4105 {
4106 curwin->w_scbind_pos += topline_diff;
4107 topline = curwin->w_scbind_pos;
4108 if (topline > curbuf->b_ml.ml_line_count)
4109 topline = curbuf->b_ml.ml_line_count;
4110 if (topline < 1)
4111 topline = 1;
4112
4113 y = topline - curwin->w_topline;
4114 if (y > 0)
4115 scrollup(y, FALSE);
4116 else
4117 scrolldown(-y, FALSE);
4118 }
4119
4120 redraw_later(VALID);
4121 cursor_correct();
4122#ifdef FEAT_WINDOWS
4123 curwin->w_redr_status = TRUE;
4124#endif
4125 }
4126
4127 /*
4128 * do the horizontal scroll
4129 */
4130 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4131 {
4132 curwin->w_leftcol = tgt_leftcol;
4133 leftcol_changed();
4134 }
4135 }
4136 }
4137
4138 /*
4139 * reset current-window
4140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 VIsual_select = old_VIsual_select;
4142 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 curwin = old_curwin;
4144 curbuf = old_curbuf;
4145}
4146#endif /* #ifdef FEAT_SCROLLBIND */
4147
4148/*
4149 * Command character that's ignored.
4150 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004151 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 static void
4154nv_ignore(cap)
4155 cmdarg_T *cap;
4156{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004157 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158}
4159
4160/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004161 * Command character that doesn't do anything, but unlike nv_ignore() does
4162 * start edit(). Used for "startinsert" executed while starting up.
4163 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004164 static void
4165nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004166 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004167{
4168}
4169
4170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * Command character doesn't exist.
4172 */
4173 static void
4174nv_error(cap)
4175 cmdarg_T *cap;
4176{
4177 clearopbeep(cap->oap);
4178}
4179
4180/*
4181 * <Help> and <F1> commands.
4182 */
4183 static void
4184nv_help(cap)
4185 cmdarg_T *cap;
4186{
4187 if (!checkclearopq(cap->oap))
4188 ex_help(NULL);
4189}
4190
4191/*
4192 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4193 */
4194 static void
4195nv_addsub(cap)
4196 cmdarg_T *cap;
4197{
4198 if (!checkclearopq(cap->oap)
4199 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4200 prep_redo_cmd(cap);
4201}
4202
4203/*
4204 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4205 */
4206 static void
4207nv_page(cap)
4208 cmdarg_T *cap;
4209{
4210 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004211 {
4212#ifdef FEAT_WINDOWS
4213 if (mod_mask & MOD_MASK_CTRL)
4214 {
4215 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4216 if (cap->arg == BACKWARD)
4217 goto_tabpage(-(int)cap->count1);
4218 else
4219 goto_tabpage((int)cap->count0);
4220 }
4221 else
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225}
4226
4227/*
4228 * Implementation of "gd" and "gD" command.
4229 */
4230 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004231nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004232 oparg_T *oap;
4233 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004234 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u *ptr;
4238
Bram Moolenaard9d30582005-05-18 22:10:28 +00004239 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004240 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004242#ifdef FEAT_FOLDING
4243 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4244 foldOpenCursor();
4245#endif
4246}
4247
4248/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004249 * Search for variable declaration of "ptr[len]".
4250 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4251 * current file ("gD").
4252 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004253 * Return FAIL when not found.
4254 */
4255 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004256find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004257 char_u *ptr;
4258 int len;
4259 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004260 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004261 int searchflags; /* flags passed to searchit() */
4262{
4263 char_u *pat;
4264 pos_T old_pos;
4265 pos_T par_pos;
4266 pos_T found_pos;
4267 int t;
4268 int save_p_ws;
4269 int save_p_scs;
4270 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004271 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004272
4273 if ((pat = alloc(len + 7)) == NULL)
4274 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004275
4276 /* Put "\V" before the pattern to avoid that the special meaning of "."
4277 * and "~" causes trouble. */
4278 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4279 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 old_pos = curwin->w_cursor;
4281 save_p_ws = p_ws;
4282 save_p_scs = p_scs;
4283 p_ws = FALSE; /* don't wrap around end of file now */
4284 p_scs = FALSE; /* don't switch ignorecase off now */
4285
4286 /*
4287 * With "gD" go to line 1.
4288 * With "gd" Search back for the start of the current function, then go
4289 * back until a blank line. If this fails go to line 1.
4290 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004291 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 {
4293 setpcmark(); /* Set in findpar() otherwise */
4294 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004295 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 else
4298 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004299 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4301 --curwin->w_cursor.lnum;
4302 }
4303 curwin->w_cursor.col = 0;
4304
4305 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004306 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 for (;;)
4308 {
4309 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004310 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004311 if (curwin->w_cursor.lnum >= old_pos.lnum)
4312 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004313
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004314 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004315 {
4316 pos_T *pos;
4317
4318 /* Check that the block the match is in doesn't end before the
4319 * position where we started the search from. */
4320 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4321 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4322 && pos->lnum < old_pos.lnum)
4323 continue;
4324 }
4325
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326 if (t == FAIL)
4327 {
4328 /* If we previously found a valid position, use it. */
4329 if (found_pos.lnum != 0)
4330 {
4331 curwin->w_cursor = found_pos;
4332 t = OK;
4333 }
4334 break;
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004337 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004338 {
4339 /* Ignore this line, continue at start of next line. */
4340 ++curwin->w_cursor.lnum;
4341 curwin->w_cursor.col = 0;
4342 continue;
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 if (!locally) /* global search: use first match found */
4346 break;
4347 if (curwin->w_cursor.lnum >= par_pos.lnum)
4348 {
4349 /* If we previously found a valid position, use it. */
4350 if (found_pos.lnum != 0)
4351 curwin->w_cursor = found_pos;
4352 break;
4353 }
4354
4355 /* For finding a local variable and the match is before the "{" search
4356 * to find a later match. For K&R style function declarations this
4357 * skips the function header without types. */
4358 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004360
4361 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004363 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 curwin->w_cursor = old_pos;
4365 }
4366 else
4367 {
4368 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 /* "n" searches forward now */
4370 reset_search_dir();
4371 }
4372
4373 vim_free(pat);
4374 p_ws = save_p_ws;
4375 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376
4377 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378}
4379
4380/*
4381 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4382 * lines rather than lines in the file.
4383 * 'dist' must be positive.
4384 *
4385 * Return OK if able to move cursor, FAIL otherwise.
4386 */
4387 static int
4388nv_screengo(oap, dir, dist)
4389 oparg_T *oap;
4390 int dir;
4391 long dist;
4392{
4393 int linelen = linetabsize(ml_get_curline());
4394 int retval = OK;
4395 int atend = FALSE;
4396 int n;
4397 int col_off1; /* margin offset for first screen line */
4398 int col_off2; /* margin offset for wrapped screen line */
4399 int width1; /* text width for first screen line */
4400 int width2; /* test width for wrapped screen line */
4401
4402 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004403 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 col_off1 = curwin_col_off();
4406 col_off2 = col_off1 - curwin_col_off2();
4407 width1 = W_WIDTH(curwin) - col_off1;
4408 width2 = W_WIDTH(curwin) - col_off2;
4409
4410#ifdef FEAT_VERTSPLIT
4411 if (curwin->w_width != 0)
4412 {
4413#endif
4414 /*
4415 * Instead of sticking at the last character of the buffer line we
4416 * try to stick in the last column of the screen.
4417 */
4418 if (curwin->w_curswant == MAXCOL)
4419 {
4420 atend = TRUE;
4421 validate_virtcol();
4422 if (width1 <= 0)
4423 curwin->w_curswant = 0;
4424 else
4425 {
4426 curwin->w_curswant = width1 - 1;
4427 if (curwin->w_virtcol > curwin->w_curswant)
4428 curwin->w_curswant += ((curwin->w_virtcol
4429 - curwin->w_curswant - 1) / width2 + 1) * width2;
4430 }
4431 }
4432 else
4433 {
4434 if (linelen > width1)
4435 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4436 else
4437 n = width1;
4438 if (curwin->w_curswant > (colnr_T)n + 1)
4439 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4440 * width2;
4441 }
4442
4443 while (dist--)
4444 {
4445 if (dir == BACKWARD)
4446 {
4447 if ((long)curwin->w_curswant >= width2)
4448 /* move back within line */
4449 curwin->w_curswant -= width2;
4450 else
4451 {
4452 /* to previous line */
4453 if (curwin->w_cursor.lnum == 1)
4454 {
4455 retval = FAIL;
4456 break;
4457 }
4458 --curwin->w_cursor.lnum;
4459#ifdef FEAT_FOLDING
4460 /* Move to the start of a closed fold. Don't do that when
4461 * 'foldopen' contains "all": it will open in a moment. */
4462 if (!(fdo_flags & FDO_ALL))
4463 (void)hasFolding(curwin->w_cursor.lnum,
4464 &curwin->w_cursor.lnum, NULL);
4465#endif
4466 linelen = linetabsize(ml_get_curline());
4467 if (linelen > width1)
4468 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4469 + 1) * width2;
4470 }
4471 }
4472 else /* dir == FORWARD */
4473 {
4474 if (linelen > width1)
4475 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4476 else
4477 n = width1;
4478 if (curwin->w_curswant + width2 < (colnr_T)n)
4479 /* move forward within line */
4480 curwin->w_curswant += width2;
4481 else
4482 {
4483 /* to next line */
4484#ifdef FEAT_FOLDING
4485 /* Move to the end of a closed fold. */
4486 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4487 &curwin->w_cursor.lnum);
4488#endif
4489 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4490 {
4491 retval = FAIL;
4492 break;
4493 }
4494 curwin->w_cursor.lnum++;
4495 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004496 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 }
4499 }
4500#ifdef FEAT_VERTSPLIT
4501 }
4502#endif
4503
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004504 if (virtual_active() && atend)
4505 coladvance(MAXCOL);
4506 else
4507 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
4509#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4510 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4511 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004512 colnr_T virtcol;
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /*
4515 * Check for landing on a character that got split at the end of the
4516 * last line. We want to advance a screenline, not end up in the same
4517 * screenline or move two screenlines.
4518 */
4519 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004520 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004521# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004522 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4523 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004524# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004525
4526 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 && (curwin->w_curswant < (colnr_T)width1
4528 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4529 : ((curwin->w_curswant - width1) % width2
4530 > (colnr_T)width2 / 2)))
4531 --curwin->w_cursor.col;
4532 }
4533#endif
4534
4535 if (atend)
4536 curwin->w_curswant = MAXCOL; /* stick in the last column */
4537
4538 return retval;
4539}
4540
4541#ifdef FEAT_MOUSE
4542/*
4543 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4544 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004545 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4546 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
4548 static void
4549nv_mousescroll(cap)
4550 cmdarg_T *cap;
4551{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004552# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004553 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004555 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 int row, col;
4558
4559 row = mouse_row;
4560 col = mouse_col;
4561
4562 /* find the window at the pointer coordinates */
4563 curwin = mouse_find_win(&row, &col);
4564 curbuf = curwin->w_buffer;
4565 }
4566# endif
4567
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004568 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004570 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4571 {
4572 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4573 }
4574 else
4575 {
4576 cap->count1 = 3;
4577 cap->count0 = 3;
4578 nv_scroll_line(cap);
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004581# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else
4583 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004584 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4585 if (!curwin->w_p_wrap)
4586 {
4587 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004588
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4590 step = W_WIDTH(curwin);
4591 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4592 if (val < 0)
4593 val = 0;
4594
4595 gui_do_horiz_scroll(val, TRUE);
4596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004600# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 curwin->w_redr_status = TRUE;
4602
4603 curwin = old_curwin;
4604 curbuf = curwin->w_buffer;
4605# endif
4606}
4607
4608/*
4609 * Mouse clicks and drags.
4610 */
4611 static void
4612nv_mouse(cap)
4613 cmdarg_T *cap;
4614{
4615 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4616}
4617#endif
4618
4619/*
4620 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4621 * cap->arg must be TRUE for CTRL-E.
4622 */
4623 static void
4624nv_scroll_line(cap)
4625 cmdarg_T *cap;
4626{
4627 if (!checkclearop(cap->oap))
4628 scroll_redraw(cap->arg, cap->count1);
4629}
4630
4631/*
4632 * Scroll "count" lines up or down, and redraw.
4633 */
4634 void
4635scroll_redraw(up, count)
4636 int up;
4637 long count;
4638{
4639 linenr_T prev_topline = curwin->w_topline;
4640#ifdef FEAT_DIFF
4641 int prev_topfill = curwin->w_topfill;
4642#endif
4643 linenr_T prev_lnum = curwin->w_cursor.lnum;
4644
4645 if (up)
4646 scrollup(count, TRUE);
4647 else
4648 scrolldown(count, TRUE);
4649 if (p_so)
4650 {
4651 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4652 * valid, otherwise the screen jumps back at the end of the file. */
4653 cursor_correct();
4654 check_cursor_moved(curwin);
4655 curwin->w_valid |= VALID_TOPLINE;
4656
4657 /* If moved back to where we were, at least move the cursor, otherwise
4658 * we get stuck at one position. Don't move the cursor up if the
4659 * first line of the buffer is already on the screen */
4660 while (curwin->w_topline == prev_topline
4661#ifdef FEAT_DIFF
4662 && curwin->w_topfill == prev_topfill
4663#endif
4664 )
4665 {
4666 if (up)
4667 {
4668 if (curwin->w_cursor.lnum > prev_lnum
4669 || cursor_down(1L, FALSE) == FAIL)
4670 break;
4671 }
4672 else
4673 {
4674 if (curwin->w_cursor.lnum < prev_lnum
4675 || prev_topline == 1L
4676 || cursor_up(1L, FALSE) == FAIL)
4677 break;
4678 }
4679 /* Mark w_topline as valid, otherwise the screen jumps back at the
4680 * end of the file. */
4681 check_cursor_moved(curwin);
4682 curwin->w_valid |= VALID_TOPLINE;
4683 }
4684 }
4685 if (curwin->w_cursor.lnum != prev_lnum)
4686 coladvance(curwin->w_curswant);
4687 redraw_later(VALID);
4688}
4689
4690/*
4691 * Commands that start with "z".
4692 */
4693 static void
4694nv_zet(cap)
4695 cmdarg_T *cap;
4696{
4697 long n;
4698 colnr_T col;
4699 int nchar = cap->nchar;
4700#ifdef FEAT_FOLDING
4701 long old_fdl = curwin->w_p_fdl;
4702 int old_fen = curwin->w_p_fen;
4703#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004704#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004705 int undo = FALSE;
4706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 if (VIM_ISDIGIT(nchar))
4709 {
4710 /*
4711 * "z123{nchar}": edit the count before obtaining {nchar}
4712 */
4713 if (checkclearop(cap->oap))
4714 return;
4715 n = nchar - '0';
4716 for (;;)
4717 {
4718#ifdef USE_ON_FLY_SCROLL
4719 dont_scroll = TRUE; /* disallow scrolling here */
4720#endif
4721 ++no_mapping;
4722 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004723 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 --no_mapping;
4726 --allow_keys;
4727#ifdef FEAT_CMDL_INFO
4728 (void)add_to_showcmd(nchar);
4729#endif
4730 if (nchar == K_DEL || nchar == K_KDEL)
4731 n /= 10;
4732 else if (VIM_ISDIGIT(nchar))
4733 n = n * 10 + (nchar - '0');
4734 else if (nchar == CAR)
4735 {
4736#ifdef FEAT_GUI
4737 need_mouse_correct = TRUE;
4738#endif
4739 win_setheight((int)n);
4740 break;
4741 }
4742 else if (nchar == 'l'
4743 || nchar == 'h'
4744 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004745 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
4747 cap->count1 = n ? n * cap->count1 : cap->count1;
4748 goto dozet;
4749 }
4750 else
4751 {
4752 clearopbeep(cap->oap);
4753 break;
4754 }
4755 }
4756 cap->oap->op_type = OP_NOP;
4757 return;
4758 }
4759
4760dozet:
4761 if (
4762#ifdef FEAT_FOLDING
4763 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4764 * and "zC" only in Visual mode. "zj" and "zk" are motion
4765 * commands. */
4766 cap->nchar != 'f' && cap->nchar != 'F'
4767 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4768 && cap->nchar != 'j' && cap->nchar != 'k'
4769 &&
4770#endif
4771 checkclearop(cap->oap))
4772 return;
4773
4774 /*
4775 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4776 * If line number given, set cursor.
4777 */
4778 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4779 && cap->count0
4780 && cap->count0 != curwin->w_cursor.lnum)
4781 {
4782 setpcmark();
4783 if (cap->count0 > curbuf->b_ml.ml_line_count)
4784 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4785 else
4786 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004787 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789
4790 switch (nchar)
4791 {
4792 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4793 case '+':
4794 if (cap->count0 == 0)
4795 {
4796 /* No count given: put cursor at the line below screen */
4797 validate_botline(); /* make sure w_botline is valid */
4798 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4799 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4800 else
4801 curwin->w_cursor.lnum = curwin->w_botline;
4802 }
4803 /* FALLTHROUGH */
4804 case NL:
4805 case CAR:
4806 case K_KENTER:
4807 beginline(BL_WHITE | BL_FIX);
4808 /* FALLTHROUGH */
4809
4810 case 't': scroll_cursor_top(0, TRUE);
4811 redraw_later(VALID);
4812 break;
4813
4814 /* "z." and "zz": put cursor in middle of screen */
4815 case '.': beginline(BL_WHITE | BL_FIX);
4816 /* FALLTHROUGH */
4817
4818 case 'z': scroll_cursor_halfway(TRUE);
4819 redraw_later(VALID);
4820 break;
4821
4822 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4823 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4824 * when <count> is at bottom of window, and puts that one at
4825 * bottom of window. */
4826 if (cap->count0 != 0)
4827 {
4828 scroll_cursor_bot(0, TRUE);
4829 curwin->w_cursor.lnum = curwin->w_topline;
4830 }
4831 else if (curwin->w_topline == 1)
4832 curwin->w_cursor.lnum = 1;
4833 else
4834 curwin->w_cursor.lnum = curwin->w_topline - 1;
4835 /* FALLTHROUGH */
4836 case '-':
4837 beginline(BL_WHITE | BL_FIX);
4838 /* FALLTHROUGH */
4839
4840 case 'b': scroll_cursor_bot(0, TRUE);
4841 redraw_later(VALID);
4842 break;
4843
4844 /* "zH" - scroll screen right half-page */
4845 case 'H':
4846 cap->count1 *= W_WIDTH(curwin) / 2;
4847 /* FALLTHROUGH */
4848
4849 /* "zh" - scroll screen to the right */
4850 case 'h':
4851 case K_LEFT:
4852 if (!curwin->w_p_wrap)
4853 {
4854 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4855 curwin->w_leftcol = 0;
4856 else
4857 curwin->w_leftcol -= (colnr_T)cap->count1;
4858 leftcol_changed();
4859 }
4860 break;
4861
4862 /* "zL" - scroll screen left half-page */
4863 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4864 /* FALLTHROUGH */
4865
4866 /* "zl" - scroll screen to the left */
4867 case 'l':
4868 case K_RIGHT:
4869 if (!curwin->w_p_wrap)
4870 {
4871 /* scroll the window left */
4872 curwin->w_leftcol += (colnr_T)cap->count1;
4873 leftcol_changed();
4874 }
4875 break;
4876
4877 /* "zs" - scroll screen, cursor at the start */
4878 case 's': if (!curwin->w_p_wrap)
4879 {
4880#ifdef FEAT_FOLDING
4881 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4882 col = 0; /* like the cursor is in col 0 */
4883 else
4884#endif
4885 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4886 if ((long)col > p_siso)
4887 col -= p_siso;
4888 else
4889 col = 0;
4890 if (curwin->w_leftcol != col)
4891 {
4892 curwin->w_leftcol = col;
4893 redraw_later(NOT_VALID);
4894 }
4895 }
4896 break;
4897
4898 /* "ze" - scroll screen, cursor at the end */
4899 case 'e': if (!curwin->w_p_wrap)
4900 {
4901#ifdef FEAT_FOLDING
4902 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4903 col = 0; /* like the cursor is in col 0 */
4904 else
4905#endif
4906 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4907 n = W_WIDTH(curwin) - curwin_col_off();
4908 if ((long)col + p_siso < n)
4909 col = 0;
4910 else
4911 col = col + p_siso - n + 1;
4912 if (curwin->w_leftcol != col)
4913 {
4914 curwin->w_leftcol = col;
4915 redraw_later(NOT_VALID);
4916 }
4917 }
4918 break;
4919
4920#ifdef FEAT_FOLDING
4921 /* "zF": create fold command */
4922 /* "zf": create fold operator */
4923 case 'F':
4924 case 'f': if (foldManualAllowed(TRUE))
4925 {
4926 cap->nchar = 'f';
4927 nv_operator(cap);
4928 curwin->w_p_fen = TRUE;
4929
4930 /* "zF" is like "zfzf" */
4931 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4932 {
4933 nv_operator(cap);
4934 finish_op = TRUE;
4935 }
4936 }
4937 else
4938 clearopbeep(cap->oap);
4939 break;
4940
4941 /* "zd": delete fold at cursor */
4942 /* "zD": delete fold at cursor recursively */
4943 case 'd':
4944 case 'D': if (foldManualAllowed(FALSE))
4945 {
4946 if (VIsual_active)
4947 nv_operator(cap);
4948 else
4949 deleteFold(curwin->w_cursor.lnum,
4950 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4951 }
4952 break;
4953
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004954 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 case 'E': if (foldmethodIsManual(curwin))
4956 {
4957 clearFolding(curwin);
4958 changed_window_setting();
4959 }
4960 else if (foldmethodIsMarker(curwin))
4961 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4962 TRUE, FALSE);
4963 else
4964 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4965 break;
4966
4967 /* "zn": fold none: reset 'foldenable' */
4968 case 'n': curwin->w_p_fen = FALSE;
4969 break;
4970
4971 /* "zN": fold Normal: set 'foldenable' */
4972 case 'N': curwin->w_p_fen = TRUE;
4973 break;
4974
4975 /* "zi": invert folding: toggle 'foldenable' */
4976 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4977 break;
4978
4979 /* "za": open closed fold or close open fold at cursor */
4980 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4981 openFold(curwin->w_cursor.lnum, cap->count1);
4982 else
4983 {
4984 closeFold(curwin->w_cursor.lnum, cap->count1);
4985 curwin->w_p_fen = TRUE;
4986 }
4987 break;
4988
4989 /* "zA": open fold at cursor recursively */
4990 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4991 openFoldRecurse(curwin->w_cursor.lnum);
4992 else
4993 {
4994 closeFoldRecurse(curwin->w_cursor.lnum);
4995 curwin->w_p_fen = TRUE;
4996 }
4997 break;
4998
4999 /* "zo": open fold at cursor or Visual area */
5000 case 'o': if (VIsual_active)
5001 nv_operator(cap);
5002 else
5003 openFold(curwin->w_cursor.lnum, cap->count1);
5004 break;
5005
5006 /* "zO": open fold recursively */
5007 case 'O': if (VIsual_active)
5008 nv_operator(cap);
5009 else
5010 openFoldRecurse(curwin->w_cursor.lnum);
5011 break;
5012
5013 /* "zc": close fold at cursor or Visual area */
5014 case 'c': if (VIsual_active)
5015 nv_operator(cap);
5016 else
5017 closeFold(curwin->w_cursor.lnum, cap->count1);
5018 curwin->w_p_fen = TRUE;
5019 break;
5020
5021 /* "zC": close fold recursively */
5022 case 'C': if (VIsual_active)
5023 nv_operator(cap);
5024 else
5025 closeFoldRecurse(curwin->w_cursor.lnum);
5026 curwin->w_p_fen = TRUE;
5027 break;
5028
5029 /* "zv": open folds at the cursor */
5030 case 'v': foldOpenCursor();
5031 break;
5032
5033 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5034 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005035 curwin->w_foldinvalid = TRUE; /* recompute folds */
5036 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 foldOpenCursor();
5038 break;
5039
5040 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5041 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005042 curwin->w_foldinvalid = TRUE; /* recompute folds */
5043 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 break;
5045
5046 /* "zm": fold more */
5047 case 'm': if (curwin->w_p_fdl > 0)
5048 --curwin->w_p_fdl;
5049 old_fdl = -1; /* force an update */
5050 curwin->w_p_fen = TRUE;
5051 break;
5052
5053 /* "zM": close all folds */
5054 case 'M': curwin->w_p_fdl = 0;
5055 old_fdl = -1; /* force an update */
5056 curwin->w_p_fen = TRUE;
5057 break;
5058
5059 /* "zr": reduce folding */
5060 case 'r': ++curwin->w_p_fdl;
5061 break;
5062
5063 /* "zR": open all folds */
5064 case 'R': curwin->w_p_fdl = getDeepestNesting();
5065 old_fdl = -1; /* force an update */
5066 break;
5067
5068 case 'j': /* "zj" move to next fold downwards */
5069 case 'k': /* "zk" move to next fold upwards */
5070 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5071 cap->count1) == FAIL)
5072 clearopbeep(cap->oap);
5073 break;
5074
5075#endif /* FEAT_FOLDING */
5076
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005077#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005078 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5079 ++no_mapping;
5080 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005081 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005082 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005083 --no_mapping;
5084 --allow_keys;
5085#ifdef FEAT_CMDL_INFO
5086 (void)add_to_showcmd(nchar);
5087#endif
5088 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5089 {
5090 clearopbeep(cap->oap);
5091 break;
5092 }
5093 undo = TRUE;
5094 /*FALLTHROUGH*/
5095
Bram Moolenaarb765d632005-06-07 21:00:02 +00005096 case 'g': /* "zg": add good word to word list */
5097 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005098 case 'G': /* "zG": add good word to temp word list */
5099 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005100 {
5101 char_u *ptr = NULL;
5102 int len;
5103
5104 if (checkclearop(cap->oap))
5105 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005106 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5107 == FAIL)
5108 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005109 if (ptr == NULL)
5110 {
5111 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005112
Bram Moolenaar134bf072013-09-25 18:54:24 +02005113 /* Find bad word under the cursor. When 'spell' is
5114 * off this fails and find_ident_under_cursor() is
5115 * used below. */
5116 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005117 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005118 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005119 if (len != 0 && curwin->w_cursor.col <= pos.col)
5120 ptr = ml_get_pos(&curwin->w_cursor);
5121 curwin->w_cursor = pos;
5122 }
5123
Bram Moolenaarb765d632005-06-07 21:00:02 +00005124 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5125 FIND_IDENT)) == 0)
5126 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005127 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005128 (nchar == 'G' || nchar == 'W')
5129 ? 0 : (int)cap->count1,
5130 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005132 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133
Bram Moolenaar43abc522005-12-10 20:15:02 +00005134 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005135 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005136 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005137 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005138#endif
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 default: clearopbeep(cap->oap);
5141 }
5142
5143#ifdef FEAT_FOLDING
5144 /* Redraw when 'foldenable' changed */
5145 if (old_fen != curwin->w_p_fen)
5146 {
5147# ifdef FEAT_DIFF
5148 win_T *wp;
5149
5150 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5151 {
5152 /* Adjust 'foldenable' in diff-synced windows. */
5153 FOR_ALL_WINDOWS(wp)
5154 {
5155 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5156 {
5157 wp->w_p_fen = curwin->w_p_fen;
5158 changed_window_setting_win(wp);
5159 }
5160 }
5161 }
5162# endif
5163 changed_window_setting();
5164 }
5165
5166 /* Redraw when 'foldlevel' changed. */
5167 if (old_fdl != curwin->w_p_fdl)
5168 newFoldLevel();
5169#endif
5170}
5171
5172#ifdef FEAT_GUI
5173/*
5174 * Vertical scrollbar movement.
5175 */
5176 static void
5177nv_ver_scrollbar(cap)
5178 cmdarg_T *cap;
5179{
5180 if (cap->oap->op_type != OP_NOP)
5181 clearopbeep(cap->oap);
5182
5183 /* Even if an operator was pending, we still want to scroll */
5184 gui_do_scroll();
5185}
5186
5187/*
5188 * Horizontal scrollbar movement.
5189 */
5190 static void
5191nv_hor_scrollbar(cap)
5192 cmdarg_T *cap;
5193{
5194 if (cap->oap->op_type != OP_NOP)
5195 clearopbeep(cap->oap);
5196
5197 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005198 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199}
5200#endif
5201
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005202#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005203/*
5204 * Click in GUI tab.
5205 */
5206 static void
5207nv_tabline(cap)
5208 cmdarg_T *cap;
5209{
5210 if (cap->oap->op_type != OP_NOP)
5211 clearopbeep(cap->oap);
5212
5213 /* Even if an operator was pending, we still want to jump tabs. */
5214 goto_tabpage(current_tab);
5215}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005216
5217/*
5218 * Selected item in tab line menu.
5219 */
5220 static void
5221nv_tabmenu(cap)
5222 cmdarg_T *cap;
5223{
5224 if (cap->oap->op_type != OP_NOP)
5225 clearopbeep(cap->oap);
5226
5227 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005228 handle_tabmenu();
5229}
5230
5231/*
5232 * Handle selecting an item of the GUI tab line menu.
5233 * Used in Normal and Insert mode.
5234 */
5235 void
5236handle_tabmenu()
5237{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005238 switch (current_tabmenu)
5239 {
5240 case TABLINE_MENU_CLOSE:
5241 if (current_tab == 0)
5242 do_cmdline_cmd((char_u *)"tabclose");
5243 else
5244 {
5245 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5246 current_tab);
5247 do_cmdline_cmd(IObuff);
5248 }
5249 break;
5250
5251 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005252 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5253 current_tab > 0 ? current_tab - 1 : 999);
5254 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005255 break;
5256
5257 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005258 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5259 current_tab > 0 ? current_tab - 1 : 999);
5260 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005261 break;
5262 }
5263}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005264#endif
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266/*
5267 * "Q" command.
5268 */
5269 static void
5270nv_exmode(cap)
5271 cmdarg_T *cap;
5272{
5273 /*
5274 * Ignore 'Q' in Visual mode, just give a beep.
5275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (VIsual_active)
5277 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005278 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 do_exmode(FALSE);
5280}
5281
5282/*
5283 * Handle a ":" command.
5284 */
5285 static void
5286nv_colon(cap)
5287 cmdarg_T *cap;
5288{
5289 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005290 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (VIsual_active)
5293 nv_operator(cap);
5294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
5296 if (cap->oap->op_type != OP_NOP)
5297 {
5298 /* Using ":" as a movement is characterwise exclusive. */
5299 cap->oap->motion_type = MCHAR;
5300 cap->oap->inclusive = FALSE;
5301 }
5302 else if (cap->count0)
5303 {
5304 /* translate "count:" into ":.,.+(count - 1)" */
5305 stuffcharReadbuff('.');
5306 if (cap->count0 > 1)
5307 {
5308 stuffReadbuff((char_u *)",.+");
5309 stuffnumReadbuff((long)cap->count0 - 1L);
5310 }
5311 }
5312
5313 /* When typing, don't type below an old message */
5314 if (KeyTyped)
5315 compute_cmdrow();
5316
5317 old_p_im = p_im;
5318
5319 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005320 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5322
5323 /* If 'insertmode' changed, enter or exit Insert mode */
5324 if (p_im != old_p_im)
5325 {
5326 if (p_im)
5327 restart_edit = 'i';
5328 else
5329 restart_edit = 0;
5330 }
5331
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005332 if (cmd_result == FAIL)
5333 /* The Ex command failed, do not execute the operator. */
5334 clearop(cap->oap);
5335 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5337 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005338 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5339 || did_emsg
5340 ))
5341 /* The start of the operator has become invalid by the Ex command.
5342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 clearopbeep(cap->oap);
5344 }
5345}
5346
5347/*
5348 * Handle CTRL-G command.
5349 */
5350 static void
5351nv_ctrlg(cap)
5352 cmdarg_T *cap;
5353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (VIsual_active) /* toggle Selection/Visual mode */
5355 {
5356 VIsual_select = !VIsual_select;
5357 showmode();
5358 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005359 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 /* print full name if count given or :cd used */
5361 fileinfo((int)cap->count0, FALSE, TRUE);
5362}
5363
5364/*
5365 * Handle CTRL-H <Backspace> command.
5366 */
5367 static void
5368nv_ctrlh(cap)
5369 cmdarg_T *cap;
5370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (VIsual_active && VIsual_select)
5372 {
5373 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5374 v_visop(cap);
5375 }
5376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 nv_left(cap);
5378}
5379
5380/*
5381 * CTRL-L: clear screen and redraw.
5382 */
5383 static void
5384nv_clear(cap)
5385 cmdarg_T *cap;
5386{
5387 if (!checkclearop(cap->oap))
5388 {
5389#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5390 /*
5391 * Right now, the BeBox doesn't seem to have an easy way to detect
5392 * window resizing, so we cheat and make the user detect it
5393 * manually with CTRL-L instead
5394 */
5395 ui_get_shellsize();
5396#endif
5397#ifdef FEAT_SYN_HL
5398 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005399 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400#endif
5401 redraw_later(CLEAR);
5402 }
5403}
5404
5405/*
5406 * CTRL-O: In Select mode: switch to Visual mode for one command.
5407 * Otherwise: Go to older pcmark.
5408 */
5409 static void
5410nv_ctrlo(cap)
5411 cmdarg_T *cap;
5412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if (VIsual_active && VIsual_select)
5414 {
5415 VIsual_select = FALSE;
5416 showmode();
5417 restart_VIsual_select = 2; /* restart Select mode later */
5418 }
5419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 cap->count1 = -cap->count1;
5422 nv_pcmark(cap);
5423 }
5424}
5425
5426/*
5427 * CTRL-^ command, short for ":e #"
5428 */
5429 static void
5430nv_hat(cap)
5431 cmdarg_T *cap;
5432{
5433 if (!checkclearopq(cap->oap))
5434 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5435 GETF_SETMARK|GETF_ALT, FALSE);
5436}
5437
5438/*
5439 * "Z" commands.
5440 */
5441 static void
5442nv_Zet(cap)
5443 cmdarg_T *cap;
5444{
5445 if (!checkclearopq(cap->oap))
5446 {
5447 switch (cap->nchar)
5448 {
5449 /* "ZZ": equivalent to ":x". */
5450 case 'Z': do_cmdline_cmd((char_u *)"x");
5451 break;
5452
5453 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5454 case 'Q': do_cmdline_cmd((char_u *)"q!");
5455 break;
5456
5457 default: clearopbeep(cap->oap);
5458 }
5459 }
5460}
5461
5462#if defined(FEAT_WINDOWS) || defined(PROTO)
5463/*
5464 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5465 */
5466 void
5467do_nv_ident(c1, c2)
5468 int c1;
5469 int c2;
5470{
5471 oparg_T oa;
5472 cmdarg_T ca;
5473
5474 clear_oparg(&oa);
5475 vim_memset(&ca, 0, sizeof(ca));
5476 ca.oap = &oa;
5477 ca.cmdchar = c1;
5478 ca.nchar = c2;
5479 nv_ident(&ca);
5480}
5481#endif
5482
5483/*
5484 * Handle the commands that use the word under the cursor.
5485 * [g] CTRL-] :ta to current identifier
5486 * [g] 'K' run program for current identifier
5487 * [g] '*' / to current identifier or string
5488 * [g] '#' ? to current identifier or string
5489 * g ']' :tselect for current identifier
5490 */
5491 static void
5492nv_ident(cap)
5493 cmdarg_T *cap;
5494{
5495 char_u *ptr = NULL;
5496 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005497 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u *p;
5499 char_u *kp; /* value of 'keywordprg' */
5500 int kp_help; /* 'keywordprg' is ":help" */
5501 int n = 0; /* init for GCC */
5502 int cmdchar;
5503 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005504 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 char_u *aux_ptr;
5506 int isman;
5507 int isman_s;
5508
5509 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5510 {
5511 cmdchar = cap->nchar;
5512 g_cmd = TRUE;
5513 }
5514 else
5515 {
5516 cmdchar = cap->cmdchar;
5517 g_cmd = FALSE;
5518 }
5519
5520 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5521 cmdchar = '#';
5522
5523 /*
5524 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5525 */
5526 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5529 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (checkclearopq(cap->oap))
5531 return;
5532 }
5533
5534 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5535 (cmdchar == '*' || cmdchar == '#')
5536 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5537 {
5538 clearop(cap->oap);
5539 return;
5540 }
5541
5542 /* Allocate buffer to put the command in. Inserting backslashes can
5543 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5544 * and some numbers. */
5545 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5546 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5547 || STRCMP(kp, ":help") == 0);
5548 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5549 if (buf == NULL)
5550 return;
5551 buf[0] = NUL;
5552
5553 switch (cmdchar)
5554 {
5555 case '*':
5556 case '#':
5557 /*
5558 * Put cursor at start of word, makes search skip the word
5559 * under the cursor.
5560 * Call setpcmark() first, so "*``" puts the cursor back where
5561 * it was.
5562 */
5563 setpcmark();
5564 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5565
5566 if (!g_cmd && vim_iswordp(ptr))
5567 STRCPY(buf, "\\<");
5568 no_smartcase = TRUE; /* don't use 'smartcase' now */
5569 break;
5570
5571 case 'K':
5572 if (kp_help)
5573 STRCPY(buf, "he! ");
5574 else
5575 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005576 /* An external command will probably use an argument starting
5577 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005578 while (*ptr == '-' && n > 0)
5579 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005580 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005581 --n;
5582 }
5583 if (n == 0)
5584 {
5585 EMSG(_(e_noident)); /* found dashes only */
5586 vim_free(buf);
5587 return;
5588 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005589
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 /* When a count is given, turn it into a range. Is this
5591 * really what we want? */
5592 isman = (STRCMP(kp, "man") == 0);
5593 isman_s = (STRCMP(kp, "man -s") == 0);
5594 if (cap->count0 != 0 && !(isman || isman_s))
5595 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5596
5597 STRCAT(buf, "! ");
5598 if (cap->count0 == 0 && isman_s)
5599 STRCAT(buf, "man");
5600 else
5601 STRCAT(buf, kp);
5602 STRCAT(buf, " ");
5603 if (cap->count0 != 0 && (isman || isman_s))
5604 {
5605 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5606 STRCAT(buf, " ");
5607 }
5608 }
5609 break;
5610
5611 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005612 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613#ifdef FEAT_CSCOPE
5614 if (p_cst)
5615 STRCPY(buf, "cstag ");
5616 else
5617#endif
5618 STRCPY(buf, "ts ");
5619 break;
5620
5621 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005622 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if (curbuf->b_help)
5624 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005626 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005627 if (g_cmd)
5628 STRCPY(buf, "tj ");
5629 else
5630 sprintf((char *)buf, "%ldta ", cap->count0);
5631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633
5634 /*
5635 * Now grab the chars in the identifier
5636 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005637 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005639 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005640 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005641 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005642 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005643 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005645 vim_free(buf);
5646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005648 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5649 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005650 {
5651 vim_free(buf);
5652 vim_free(p);
5653 return;
5654 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005655 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005656 STRCAT(buf, p);
5657 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005659 else
5660 {
5661 if (cmdchar == '*')
5662 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5663 else if (cmdchar == '#')
5664 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005665 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005666 {
5667 if (curbuf->b_help)
5668 /* ":help" handles unescaped argument */
5669 aux_ptr = (char_u *)"";
5670 else
5671 aux_ptr = (char_u *)"\\|\"\n[";
5672 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005673 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005674 aux_ptr = (char_u *)"\\|\"\n*?[";
5675
5676 p = buf + STRLEN(buf);
5677 while (n-- > 0)
5678 {
5679 /* put a backslash before \ and some others */
5680 if (vim_strchr(aux_ptr, *ptr) != NULL)
5681 *p++ = '\\';
5682#ifdef FEAT_MBYTE
5683 /* When current byte is a part of multibyte character, copy all
5684 * bytes of that character. */
5685 if (has_mbyte)
5686 {
5687 int i;
5688 int len = (*mb_ptr2len)(ptr) - 1;
5689
5690 for (i = 0; i < len && n >= 1; ++i, --n)
5691 *p++ = *ptr++;
5692 }
5693#endif
5694 *p++ = *ptr++;
5695 }
5696 *p = NUL;
5697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 /*
5700 * Execute the command.
5701 */
5702 if (cmdchar == '*' || cmdchar == '#')
5703 {
5704 if (!g_cmd && (
5705#ifdef FEAT_MBYTE
5706 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5707#endif
5708 vim_iswordc(ptr[-1])))
5709 STRCAT(buf, "\\>");
5710#ifdef FEAT_CMDHIST
5711 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005712 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5714#endif
5715 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5716 }
5717 else
5718 do_cmdline_cmd(buf);
5719
5720 vim_free(buf);
5721}
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723/*
5724 * Get visually selected text, within one line only.
5725 * Returns FAIL if more than one line selected.
5726 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005727 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728get_visual_text(cap, pp, lenp)
5729 cmdarg_T *cap;
5730 char_u **pp; /* return: start of selected text */
5731 int *lenp; /* return: length of selected text */
5732{
5733 if (VIsual_mode != 'V')
5734 unadjust_for_sel();
5735 if (VIsual.lnum != curwin->w_cursor.lnum)
5736 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005737 if (cap != NULL)
5738 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 return FAIL;
5740 }
5741 if (VIsual_mode == 'V')
5742 {
5743 *pp = ml_get_curline();
5744 *lenp = (int)STRLEN(*pp);
5745 }
5746 else
5747 {
5748 if (lt(curwin->w_cursor, VIsual))
5749 {
5750 *pp = ml_get_pos(&curwin->w_cursor);
5751 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5752 }
5753 else
5754 {
5755 *pp = ml_get_pos(&VIsual);
5756 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5757 }
5758#ifdef FEAT_MBYTE
5759 if (has_mbyte)
5760 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005761 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#endif
5763 }
5764 reset_VIsual_and_resel();
5765 return OK;
5766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768/*
5769 * CTRL-T: backwards in tag stack
5770 */
5771 static void
5772nv_tagpop(cap)
5773 cmdarg_T *cap;
5774{
5775 if (!checkclearopq(cap->oap))
5776 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5777}
5778
5779/*
5780 * Handle scrolling command 'H', 'L' and 'M'.
5781 */
5782 static void
5783nv_scroll(cap)
5784 cmdarg_T *cap;
5785{
5786 int used = 0;
5787 long n;
5788#ifdef FEAT_FOLDING
5789 linenr_T lnum;
5790#endif
5791 int half;
5792
5793 cap->oap->motion_type = MLINE;
5794 setpcmark();
5795
5796 if (cap->cmdchar == 'L')
5797 {
5798 validate_botline(); /* make sure curwin->w_botline is valid */
5799 curwin->w_cursor.lnum = curwin->w_botline - 1;
5800 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5801 curwin->w_cursor.lnum = 1;
5802 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005803 {
5804#ifdef FEAT_FOLDING
5805 if (hasAnyFolding(curwin))
5806 {
5807 /* Count a fold for one screen line. */
5808 for (n = cap->count1 - 1; n > 0
5809 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5810 {
5811 (void)hasFolding(curwin->w_cursor.lnum,
5812 &curwin->w_cursor.lnum, NULL);
5813 --curwin->w_cursor.lnum;
5814 }
5815 }
5816 else
5817#endif
5818 curwin->w_cursor.lnum -= cap->count1 - 1;
5819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
5821 else
5822 {
5823 if (cap->cmdchar == 'M')
5824 {
5825#ifdef FEAT_DIFF
5826 /* Don't count filler lines above the window. */
5827 used -= diff_check_fill(curwin, curwin->w_topline)
5828 - curwin->w_topfill;
5829#endif
5830 validate_botline(); /* make sure w_empty_rows is valid */
5831 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5832 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5833 {
5834#ifdef FEAT_DIFF
5835 /* Count half he number of filler lines to be "below this
5836 * line" and half to be "above the next line". */
5837 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5838 + n) / 2 >= half)
5839 {
5840 --n;
5841 break;
5842 }
5843#endif
5844 used += plines(curwin->w_topline + n);
5845 if (used >= half)
5846 break;
5847#ifdef FEAT_FOLDING
5848 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5849 n = lnum - curwin->w_topline;
5850#endif
5851 }
5852 if (n > 0 && used > curwin->w_height)
5853 --n;
5854 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005855 else /* (cap->cmdchar == 'H') */
5856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005858#ifdef FEAT_FOLDING
5859 if (hasAnyFolding(curwin))
5860 {
5861 /* Count a fold for one screen line. */
5862 lnum = curwin->w_topline;
5863 while (n-- > 0 && lnum < curwin->w_botline - 1)
5864 {
5865 hasFolding(lnum, NULL, &lnum);
5866 ++lnum;
5867 }
5868 n = lnum - curwin->w_topline;
5869 }
5870#endif
5871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 curwin->w_cursor.lnum = curwin->w_topline + n;
5873 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5874 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5875 }
5876
5877 cursor_correct(); /* correct for 'so' */
5878 beginline(BL_SOL | BL_FIX);
5879}
5880
5881/*
5882 * Cursor right commands.
5883 */
5884 static void
5885nv_right(cap)
5886 cmdarg_T *cap;
5887{
5888 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005889 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005891 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5892 {
5893 /* <C-Right> and <S-Right> move a word or WORD right */
5894 if (mod_mask & MOD_MASK_CTRL)
5895 cap->arg = TRUE;
5896 nv_wordcmd(cap);
5897 return;
5898 }
5899
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 cap->oap->motion_type = MCHAR;
5901 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005902 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005904#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005906 * In virtual edit mode, there's no such thing as "past_line", as lines
5907 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 */
5909 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005910 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#endif
5912
5913 for (n = cap->count1; n > 0; --n)
5914 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005915 if ((!past_line && oneright() == FAIL)
5916 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
5919 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005920 * <Space> wraps to next line if 'whichwrap' has 's'.
5921 * 'l' wraps to next line if 'whichwrap' has 'l'.
5922 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 if ( ((cap->cmdchar == ' '
5925 && vim_strchr(p_ww, 's') != NULL)
5926 || (cap->cmdchar == 'l'
5927 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005928 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 && vim_strchr(p_ww, '>') != NULL))
5930 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5931 {
5932 /* When deleting we also count the NL as a character.
5933 * Set cap->oap->inclusive when last char in the line is
5934 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005935 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 && !cap->oap->inclusive
5937 && !lineempty(curwin->w_cursor.lnum))
5938 cap->oap->inclusive = TRUE;
5939 else
5940 {
5941 ++curwin->w_cursor.lnum;
5942 curwin->w_cursor.col = 0;
5943#ifdef FEAT_VIRTUALEDIT
5944 curwin->w_cursor.coladd = 0;
5945#endif
5946 curwin->w_set_curswant = TRUE;
5947 cap->oap->inclusive = FALSE;
5948 }
5949 continue;
5950 }
5951 if (cap->oap->op_type == OP_NOP)
5952 {
5953 /* Only beep and flush if not moved at all */
5954 if (n == cap->count1)
5955 beep_flush();
5956 }
5957 else
5958 {
5959 if (!lineempty(curwin->w_cursor.lnum))
5960 cap->oap->inclusive = TRUE;
5961 }
5962 break;
5963 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005964 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
5966 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005967#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (virtual_active())
5969 oneright();
5970 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005973#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 if (has_mbyte)
5975 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005976 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 ++curwin->w_cursor.col;
5980 }
5981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983#ifdef FEAT_FOLDING
5984 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5985 && cap->oap->op_type == OP_NOP)
5986 foldOpenCursor();
5987#endif
5988}
5989
5990/*
5991 * Cursor left commands.
5992 *
5993 * Returns TRUE when operator end should not be adjusted.
5994 */
5995 static void
5996nv_left(cap)
5997 cmdarg_T *cap;
5998{
5999 long n;
6000
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006001 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6002 {
6003 /* <C-Left> and <S-Left> move a word or WORD left */
6004 if (mod_mask & MOD_MASK_CTRL)
6005 cap->arg = 1;
6006 nv_bck_word(cap);
6007 return;
6008 }
6009
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 cap->oap->motion_type = MCHAR;
6011 cap->oap->inclusive = FALSE;
6012 for (n = cap->count1; n > 0; --n)
6013 {
6014 if (oneleft() == FAIL)
6015 {
6016 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6017 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6018 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6019 */
6020 if ( (((cap->cmdchar == K_BS
6021 || cap->cmdchar == Ctrl_H)
6022 && vim_strchr(p_ww, 'b') != NULL)
6023 || (cap->cmdchar == 'h'
6024 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006025 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 && vim_strchr(p_ww, '<') != NULL))
6027 && curwin->w_cursor.lnum > 1)
6028 {
6029 --(curwin->w_cursor.lnum);
6030 coladvance((colnr_T)MAXCOL);
6031 curwin->w_set_curswant = TRUE;
6032
6033 /* When the NL before the first char has to be deleted we
6034 * put the cursor on the NUL after the previous line.
6035 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006036 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 if ( (cap->oap->op_type == OP_DELETE
6038 || cap->oap->op_type == OP_CHANGE)
6039 && !lineempty(curwin->w_cursor.lnum))
6040 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006041 char_u *cp = ml_get_cursor();
6042
6043 if (*cp != NUL)
6044 {
6045#ifdef FEAT_MBYTE
6046 if (has_mbyte)
6047 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6048 else
6049#endif
6050 ++curwin->w_cursor.col;
6051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 cap->retval |= CA_NO_ADJ_OP_END;
6053 }
6054 continue;
6055 }
6056 /* Only beep and flush if not moved at all */
6057 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6058 beep_flush();
6059 break;
6060 }
6061 }
6062#ifdef FEAT_FOLDING
6063 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6064 && cap->oap->op_type == OP_NOP)
6065 foldOpenCursor();
6066#endif
6067}
6068
6069/*
6070 * Cursor up commands.
6071 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6072 */
6073 static void
6074nv_up(cap)
6075 cmdarg_T *cap;
6076{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006077 if (mod_mask & MOD_MASK_SHIFT)
6078 {
6079 /* <S-Up> is page up */
6080 cap->arg = BACKWARD;
6081 nv_page(cap);
6082 }
6083 else
6084 {
6085 cap->oap->motion_type = MLINE;
6086 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6087 clearopbeep(cap->oap);
6088 else if (cap->arg)
6089 beginline(BL_WHITE | BL_FIX);
6090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091}
6092
6093/*
6094 * Cursor down commands.
6095 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6096 */
6097 static void
6098nv_down(cap)
6099 cmdarg_T *cap;
6100{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 if (mod_mask & MOD_MASK_SHIFT)
6102 {
6103 /* <S-Down> is page down */
6104 cap->arg = FORWARD;
6105 nv_page(cap);
6106 }
6107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6109 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006110 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006111 if (curwin->w_llist_ref == NULL)
6112 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6113 else
6114 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else
6116#endif
6117 {
6118#ifdef FEAT_CMDWIN
6119 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006120 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 cmdwin_result = CAR;
6122 else
6123#endif
6124 {
6125 cap->oap->motion_type = MLINE;
6126 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6127 clearopbeep(cap->oap);
6128 else if (cap->arg)
6129 beginline(BL_WHITE | BL_FIX);
6130 }
6131 }
6132}
6133
6134#ifdef FEAT_SEARCHPATH
6135/*
6136 * Grab the file name under the cursor and edit it.
6137 */
6138 static void
6139nv_gotofile(cap)
6140 cmdarg_T *cap;
6141{
6142 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006143 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006145 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006148 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return;
6150 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006151#ifdef FEAT_AUTOCMD
6152 if (curbuf_locked())
6153 {
6154 clearop(cap->oap);
6155 return;
6156 }
6157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006159 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 if (ptr != NULL)
6162 {
6163 /* do autowrite if necessary */
6164 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6165 autowrite(curbuf, FALSE);
6166 setpcmark();
6167 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006168 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006169 if (cap->nchar == 'F' && lnum >= 0)
6170 {
6171 curwin->w_cursor.lnum = lnum;
6172 check_cursor_lnum();
6173 beginline(BL_SOL | BL_FIX);
6174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 vim_free(ptr);
6176 }
6177 else
6178 clearop(cap->oap);
6179}
6180#endif
6181
6182/*
6183 * <End> command: to end of current line or last line.
6184 */
6185 static void
6186nv_end(cap)
6187 cmdarg_T *cap;
6188{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006189 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006191 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 nv_goto(cap);
6193 cap->count1 = 1; /* to end of current line */
6194 }
6195 nv_dollar(cap);
6196}
6197
6198/*
6199 * Handle the "$" command.
6200 */
6201 static void
6202nv_dollar(cap)
6203 cmdarg_T *cap;
6204{
6205 cap->oap->motion_type = MCHAR;
6206 cap->oap->inclusive = TRUE;
6207#ifdef FEAT_VIRTUALEDIT
6208 /* In virtual mode when off the edge of a line and an operator
6209 * is pending (whew!) keep the cursor where it is.
6210 * Otherwise, send it to the end of the line. */
6211 if (!virtual_active() || gchar_cursor() != NUL
6212 || cap->oap->op_type == OP_NOP)
6213#endif
6214 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6215 if (cursor_down((long)(cap->count1 - 1),
6216 cap->oap->op_type == OP_NOP) == FAIL)
6217 clearopbeep(cap->oap);
6218#ifdef FEAT_FOLDING
6219 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6220 foldOpenCursor();
6221#endif
6222}
6223
6224/*
6225 * Implementation of '?' and '/' commands.
6226 * If cap->arg is TRUE don't set PC mark.
6227 */
6228 static void
6229nv_search(cap)
6230 cmdarg_T *cap;
6231{
6232 oparg_T *oap = cap->oap;
6233
6234 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6235 {
6236 /* Translate "g??" to "g?g?" */
6237 cap->cmdchar = 'g';
6238 cap->nchar = '?';
6239 nv_operator(cap);
6240 return;
6241 }
6242
6243 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6244
6245 if (cap->searchbuf == NULL)
6246 {
6247 clearop(oap);
6248 return;
6249 }
6250
6251 normal_search(cap, cap->cmdchar, cap->searchbuf,
6252 (cap->arg ? 0 : SEARCH_MARK));
6253}
6254
6255/*
6256 * Handle "N" and "n" commands.
6257 * cap->arg is SEARCH_REV for "N", 0 for "n".
6258 */
6259 static void
6260nv_next(cap)
6261 cmdarg_T *cap;
6262{
6263 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6264}
6265
6266/*
6267 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6268 * Uses only cap->count1 and cap->oap from "cap".
6269 */
6270 static void
6271normal_search(cap, dir, pat, opt)
6272 cmdarg_T *cap;
6273 int dir;
6274 char_u *pat;
6275 int opt; /* extra flags for do_search() */
6276{
6277 int i;
6278
6279 cap->oap->motion_type = MCHAR;
6280 cap->oap->inclusive = FALSE;
6281 cap->oap->use_reg_one = TRUE;
6282 curwin->w_set_curswant = TRUE;
6283
6284 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006285 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (i == 0)
6287 clearop(cap->oap);
6288 else
6289 {
6290 if (i == 2)
6291 cap->oap->motion_type = MLINE;
6292#ifdef FEAT_VIRTUALEDIT
6293 curwin->w_cursor.coladd = 0;
6294#endif
6295#ifdef FEAT_FOLDING
6296 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6297 foldOpenCursor();
6298#endif
6299 }
6300
6301 /* "/$" will put the cursor after the end of the line, may need to
6302 * correct that here */
6303 check_cursor();
6304}
6305
6306/*
6307 * Character search commands.
6308 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6309 * ',' and FALSE for ';'.
6310 * cap->nchar is NUL for ',' and ';' (repeat the search)
6311 */
6312 static void
6313nv_csearch(cap)
6314 cmdarg_T *cap;
6315{
6316 int t_cmd;
6317
6318 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6319 t_cmd = TRUE;
6320 else
6321 t_cmd = FALSE;
6322
6323 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6325 clearopbeep(cap->oap);
6326 else
6327 {
6328 curwin->w_set_curswant = TRUE;
6329#ifdef FEAT_VIRTUALEDIT
6330 /* Include a Tab for "tx" and for "dfx". */
6331 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6332 && (t_cmd || cap->oap->op_type != OP_NOP))
6333 {
6334 colnr_T scol, ecol;
6335
6336 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6337 curwin->w_cursor.coladd = ecol - scol;
6338 }
6339 else
6340 curwin->w_cursor.coladd = 0;
6341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343#ifdef FEAT_FOLDING
6344 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6345 foldOpenCursor();
6346#endif
6347 }
6348}
6349
6350/*
6351 * "[" and "]" commands.
6352 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6353 */
6354 static void
6355nv_brackets(cap)
6356 cmdarg_T *cap;
6357{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006358 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 pos_T prev_pos;
6360 pos_T *pos = NULL; /* init for GCC */
6361 pos_T old_pos; /* cursor position before command */
6362 int flag;
6363 long n;
6364 int findc;
6365 int c;
6366
6367 cap->oap->motion_type = MCHAR;
6368 cap->oap->inclusive = FALSE;
6369 old_pos = curwin->w_cursor;
6370#ifdef FEAT_VIRTUALEDIT
6371 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6372#endif
6373
6374#ifdef FEAT_SEARCHPATH
6375 /*
6376 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6377 */
6378 if (cap->nchar == 'f')
6379 nv_gotofile(cap);
6380 else
6381#endif
6382
6383#ifdef FEAT_FIND_ID
6384 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006385 * Find the occurrence(s) of the identifier or define under cursor
6386 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 *
6388 * search list jump
6389 * fwd bwd fwd bwd fwd bwd
6390 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6391 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6392 */
6393 if (vim_strchr((char_u *)
6394#ifdef EBCDIC
6395 "iI\005dD\067",
6396#else
6397 "iI\011dD\004",
6398#endif
6399 cap->nchar) != NULL)
6400 {
6401 char_u *ptr;
6402 int len;
6403
6404 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6405 clearop(cap->oap);
6406 else
6407 {
6408 find_pattern_in_path(ptr, 0, len, TRUE,
6409 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6410 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6411 cap->count1,
6412 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6413 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6414 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6415 (linenr_T)MAXLNUM);
6416 curwin->w_set_curswant = TRUE;
6417 }
6418 }
6419 else
6420#endif
6421
6422 /*
6423 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6424 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6425 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6426 * "[m" or "]m" search for prev/next start of (Java) method.
6427 * "[M" or "]M" search for prev/next end of (Java) method.
6428 */
6429 if ( (cap->cmdchar == '['
6430 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6431 || (cap->cmdchar == ']'
6432 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6433 {
6434 if (cap->nchar == '*')
6435 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 prev_pos.lnum = 0;
6437 if (cap->nchar == 'm' || cap->nchar == 'M')
6438 {
6439 if (cap->cmdchar == '[')
6440 findc = '{';
6441 else
6442 findc = '}';
6443 n = 9999;
6444 }
6445 else
6446 {
6447 findc = cap->nchar;
6448 n = cap->count1;
6449 }
6450 for ( ; n > 0; --n)
6451 {
6452 if ((pos = findmatchlimit(cap->oap, findc,
6453 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6454 {
6455 if (new_pos.lnum == 0) /* nothing found */
6456 {
6457 if (cap->nchar != 'm' && cap->nchar != 'M')
6458 clearopbeep(cap->oap);
6459 }
6460 else
6461 pos = &new_pos; /* use last one found */
6462 break;
6463 }
6464 prev_pos = new_pos;
6465 curwin->w_cursor = *pos;
6466 new_pos = *pos;
6467 }
6468 curwin->w_cursor = old_pos;
6469
6470 /*
6471 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6472 * brought us to the match for "[m" and "]M" when inside a method.
6473 * Try finding the '{' or '}' we want to be at.
6474 * Also repeat for the given count.
6475 */
6476 if (cap->nchar == 'm' || cap->nchar == 'M')
6477 {
6478 /* norm is TRUE for "]M" and "[m" */
6479 int norm = ((findc == '{') == (cap->nchar == 'm'));
6480
6481 n = cap->count1;
6482 /* found a match: we were inside a method */
6483 if (prev_pos.lnum != 0)
6484 {
6485 pos = &prev_pos;
6486 curwin->w_cursor = prev_pos;
6487 if (norm)
6488 --n;
6489 }
6490 else
6491 pos = NULL;
6492 while (n > 0)
6493 {
6494 for (;;)
6495 {
6496 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6497 {
6498 /* if not found anything, that's an error */
6499 if (pos == NULL)
6500 clearopbeep(cap->oap);
6501 n = 0;
6502 break;
6503 }
6504 c = gchar_cursor();
6505 if (c == '{' || c == '}')
6506 {
6507 /* Must have found end/start of class: use it.
6508 * Or found the place to be at. */
6509 if ((c == findc && norm) || (n == 1 && !norm))
6510 {
6511 new_pos = curwin->w_cursor;
6512 pos = &new_pos;
6513 n = 0;
6514 }
6515 /* if no match found at all, we started outside of the
6516 * class and we're inside now. Just go on. */
6517 else if (new_pos.lnum == 0)
6518 {
6519 new_pos = curwin->w_cursor;
6520 pos = &new_pos;
6521 }
6522 /* found start/end of other method: go to match */
6523 else if ((pos = findmatchlimit(cap->oap, findc,
6524 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6525 0)) == NULL)
6526 n = 0;
6527 else
6528 curwin->w_cursor = *pos;
6529 break;
6530 }
6531 }
6532 --n;
6533 }
6534 curwin->w_cursor = old_pos;
6535 if (pos == NULL && new_pos.lnum != 0)
6536 clearopbeep(cap->oap);
6537 }
6538 if (pos != NULL)
6539 {
6540 setpcmark();
6541 curwin->w_cursor = *pos;
6542 curwin->w_set_curswant = TRUE;
6543#ifdef FEAT_FOLDING
6544 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6545 && cap->oap->op_type == OP_NOP)
6546 foldOpenCursor();
6547#endif
6548 }
6549 }
6550
6551 /*
6552 * "[[", "[]", "]]" and "][": move to start or end of function
6553 */
6554 else if (cap->nchar == '[' || cap->nchar == ']')
6555 {
6556 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6557 flag = '{';
6558 else
6559 flag = '}'; /* "][" or "[]" */
6560
6561 curwin->w_set_curswant = TRUE;
6562 /*
6563 * Imitate strange Vi behaviour: When using "]]" with an operator
6564 * we also stop at '}'.
6565 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006566 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 (cap->oap->op_type != OP_NOP
6568 && cap->arg == FORWARD && flag == '{')))
6569 clearopbeep(cap->oap);
6570 else
6571 {
6572 if (cap->oap->op_type == OP_NOP)
6573 beginline(BL_WHITE | BL_FIX);
6574#ifdef FEAT_FOLDING
6575 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6576 foldOpenCursor();
6577#endif
6578 }
6579 }
6580
6581 /*
6582 * "[p", "[P", "]P" and "]p": put with indent adjustment
6583 */
6584 else if (cap->nchar == 'p' || cap->nchar == 'P')
6585 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006586 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006588 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6589 ? FORWARD : BACKWARD;
6590 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006591 int was_visual = VIsual_active;
6592 int line_count = curbuf->b_ml.ml_line_count;
6593 pos_T start, end;
6594
6595 if (VIsual_active)
6596 {
6597 start = ltoreq(VIsual, curwin->w_cursor)
6598 ? VIsual : curwin->w_cursor;
6599 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6600 curwin->w_cursor = (dir == BACKWARD ? start : end);
6601 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006602# ifdef FEAT_CLIPBOARD
6603 adjust_clip_reg(&regname);
6604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006606
6607 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006608 if (was_visual)
6609 {
6610 VIsual = start;
6611 curwin->w_cursor = end;
6612 if (dir == BACKWARD)
6613 {
6614 /* adjust lines */
6615 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6616 curwin->w_cursor.lnum +=
6617 curbuf->b_ml.ml_line_count - line_count;
6618 }
6619
6620 VIsual_active = TRUE;
6621 if (VIsual_mode == 'V')
6622 {
6623 /* delete visually selected lines */
6624 cap->cmdchar = 'd';
6625 cap->nchar = NUL;
6626 cap->oap->regname = regname;
6627 nv_operator(cap);
6628 do_pending_operator(cap, 0, FALSE);
6629 }
6630 if (VIsual_active)
6631 {
6632 end_visual_mode();
6633 redraw_later(SOME_VALID);
6634 }
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 }
6637 }
6638
6639 /*
6640 * "['", "[`", "]'" and "]`": jump to next mark
6641 */
6642 else if (cap->nchar == '\'' || cap->nchar == '`')
6643 {
6644 pos = &curwin->w_cursor;
6645 for (n = cap->count1; n > 0; --n)
6646 {
6647 prev_pos = *pos;
6648 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6649 cap->nchar == '\'');
6650 if (pos == NULL)
6651 break;
6652 }
6653 if (pos == NULL)
6654 pos = &prev_pos;
6655 nv_cursormark(cap, cap->nchar == '\'', pos);
6656 }
6657
6658#ifdef FEAT_MOUSE
6659 /*
6660 * [ or ] followed by a middle mouse click: put selected text with
6661 * indent adjustment. Any other button just does as usual.
6662 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006663 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
6665 (void)do_mouse(cap->oap, cap->nchar,
6666 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6667 cap->count1, PUT_FIXINDENT);
6668 }
6669#endif /* FEAT_MOUSE */
6670
6671#ifdef FEAT_FOLDING
6672 /*
6673 * "[z" and "]z": move to start or end of open fold.
6674 */
6675 else if (cap->nchar == 'z')
6676 {
6677 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6678 cap->count1) == FAIL)
6679 clearopbeep(cap->oap);
6680 }
6681#endif
6682
6683#ifdef FEAT_DIFF
6684 /*
6685 * "[c" and "]c": move to next or previous diff-change.
6686 */
6687 else if (cap->nchar == 'c')
6688 {
6689 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6690 cap->count1) == FAIL)
6691 clearopbeep(cap->oap);
6692 }
6693#endif
6694
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006695#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006696 /*
6697 * "[s", "[S", "]s" and "]S": move to next spell error.
6698 */
6699 else if (cap->nchar == 's' || cap->nchar == 'S')
6700 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006701 setpcmark();
6702 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006703 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6704 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006705 {
6706 clearopbeep(cap->oap);
6707 break;
6708 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006709# ifdef FEAT_FOLDING
6710 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6711 foldOpenCursor();
6712# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006713 }
6714#endif
6715
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 /* Not a valid cap->nchar. */
6717 else
6718 clearopbeep(cap->oap);
6719}
6720
6721/*
6722 * Handle Normal mode "%" command.
6723 */
6724 static void
6725nv_percent(cap)
6726 cmdarg_T *cap;
6727{
6728 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006729#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 linenr_T lnum = curwin->w_cursor.lnum;
6731#endif
6732
6733 cap->oap->inclusive = TRUE;
6734 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6735 {
6736 if (cap->count0 > 100)
6737 clearopbeep(cap->oap);
6738 else
6739 {
6740 cap->oap->motion_type = MLINE;
6741 setpcmark();
6742 /* Round up, so CTRL-G will give same value. Watch out for a
6743 * large line count, the line number must not go negative! */
6744 if (curbuf->b_ml.ml_line_count > 1000000)
6745 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6746 / 100L * cap->count0;
6747 else
6748 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6749 cap->count0 + 99L) / 100L;
6750 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6751 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6752 beginline(BL_SOL | BL_FIX);
6753 }
6754 }
6755 else /* "%" : go to matching paren */
6756 {
6757 cap->oap->motion_type = MCHAR;
6758 cap->oap->use_reg_one = TRUE;
6759 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6760 clearopbeep(cap->oap);
6761 else
6762 {
6763 setpcmark();
6764 curwin->w_cursor = *pos;
6765 curwin->w_set_curswant = TRUE;
6766#ifdef FEAT_VIRTUALEDIT
6767 curwin->w_cursor.coladd = 0;
6768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771 }
6772#ifdef FEAT_FOLDING
6773 if (cap->oap->op_type == OP_NOP
6774 && lnum != curwin->w_cursor.lnum
6775 && (fdo_flags & FDO_PERCENT)
6776 && KeyTyped)
6777 foldOpenCursor();
6778#endif
6779}
6780
6781/*
6782 * Handle "(" and ")" commands.
6783 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6784 */
6785 static void
6786nv_brace(cap)
6787 cmdarg_T *cap;
6788{
6789 cap->oap->motion_type = MCHAR;
6790 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006791 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6792 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 curwin->w_set_curswant = TRUE;
6794
6795 if (findsent(cap->arg, cap->count1) == FAIL)
6796 clearopbeep(cap->oap);
6797 else
6798 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006799 /* Don't leave the cursor on the NUL past end of line. */
6800 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801#ifdef FEAT_VIRTUALEDIT
6802 curwin->w_cursor.coladd = 0;
6803#endif
6804#ifdef FEAT_FOLDING
6805 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6806 foldOpenCursor();
6807#endif
6808 }
6809}
6810
6811/*
6812 * "m" command: Mark a position.
6813 */
6814 static void
6815nv_mark(cap)
6816 cmdarg_T *cap;
6817{
6818 if (!checkclearop(cap->oap))
6819 {
6820 if (setmark(cap->nchar) == FAIL)
6821 clearopbeep(cap->oap);
6822 }
6823}
6824
6825/*
6826 * "{" and "}" commands.
6827 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6828 */
6829 static void
6830nv_findpar(cap)
6831 cmdarg_T *cap;
6832{
6833 cap->oap->motion_type = MCHAR;
6834 cap->oap->inclusive = FALSE;
6835 cap->oap->use_reg_one = TRUE;
6836 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006837 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 clearopbeep(cap->oap);
6839 else
6840 {
6841#ifdef FEAT_VIRTUALEDIT
6842 curwin->w_cursor.coladd = 0;
6843#endif
6844#ifdef FEAT_FOLDING
6845 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6846 foldOpenCursor();
6847#endif
6848 }
6849}
6850
6851/*
6852 * "u" command: Undo or make lower case.
6853 */
6854 static void
6855nv_undo(cap)
6856 cmdarg_T *cap;
6857{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006858 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
6860 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6861 cap->cmdchar = 'g';
6862 cap->nchar = 'u';
6863 nv_operator(cap);
6864 }
6865 else
6866 nv_kundo(cap);
6867}
6868
6869/*
6870 * <Undo> command.
6871 */
6872 static void
6873nv_kundo(cap)
6874 cmdarg_T *cap;
6875{
6876 if (!checkclearopq(cap->oap))
6877 {
6878 u_undo((int)cap->count1);
6879 curwin->w_set_curswant = TRUE;
6880 }
6881}
6882
6883/*
6884 * Handle the "r" command.
6885 */
6886 static void
6887nv_replace(cap)
6888 cmdarg_T *cap;
6889{
6890 char_u *ptr;
6891 int had_ctrl_v;
6892 long n;
6893
6894 if (checkclearop(cap->oap))
6895 return;
6896
6897 /* get another character */
6898 if (cap->nchar == Ctrl_V)
6899 {
6900 had_ctrl_v = Ctrl_V;
6901 cap->nchar = get_literal();
6902 /* Don't redo a multibyte character with CTRL-V. */
6903 if (cap->nchar > DEL)
6904 had_ctrl_v = NUL;
6905 }
6906 else
6907 had_ctrl_v = NUL;
6908
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006909 /* Abort if the character is a special key. */
6910 if (IS_SPECIAL(cap->nchar))
6911 {
6912 clearopbeep(cap->oap);
6913 return;
6914 }
6915
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 /* Visual mode "r" */
6917 if (VIsual_active)
6918 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006919 if (got_int)
6920 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006921 if (had_ctrl_v)
6922 {
6923 if (cap->nchar == '\r')
6924 cap->nchar = -1;
6925 else if (cap->nchar == '\n')
6926 cap->nchar = -2;
6927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 nv_operator(cap);
6929 return;
6930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
6932#ifdef FEAT_VIRTUALEDIT
6933 /* Break tabs, etc. */
6934 if (virtual_active())
6935 {
6936 if (u_save_cursor() == FAIL)
6937 return;
6938 if (gchar_cursor() == NUL)
6939 {
6940 /* Add extra space and put the cursor on the first one. */
6941 coladvance_force((colnr_T)(getviscol() + cap->count1));
6942 curwin->w_cursor.col -= cap->count1;
6943 }
6944 else if (gchar_cursor() == TAB)
6945 coladvance_force(getviscol());
6946 }
6947#endif
6948
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006949 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006951 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952#ifdef FEAT_MBYTE
6953 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6954#endif
6955 )
6956 {
6957 clearopbeep(cap->oap);
6958 return;
6959 }
6960
6961 /*
6962 * Replacing with a TAB is done by edit() when it is complicated because
6963 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6964 * Other characters are done below to avoid problems with things like
6965 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6966 */
6967 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6968 {
6969 stuffnumReadbuff(cap->count1);
6970 stuffcharReadbuff('R');
6971 stuffcharReadbuff('\t');
6972 stuffcharReadbuff(ESC);
6973 return;
6974 }
6975
6976 /* save line for undo */
6977 if (u_save_cursor() == FAIL)
6978 return;
6979
6980 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6981 {
6982 /*
6983 * Replace character(s) by a single newline.
6984 * Strange vi behaviour: Only one newline is inserted.
6985 * Delete the characters here.
6986 * Insert the newline with an insert command, takes care of
6987 * autoindent. The insert command depends on being on the last
6988 * character of a line or not.
6989 */
6990#ifdef FEAT_MBYTE
6991 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6992#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006993 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994#endif
6995 stuffcharReadbuff('\r');
6996 stuffcharReadbuff(ESC);
6997
6998 /* Give 'r' to edit(), to get the redo command right. */
6999 invoke_edit(cap, TRUE, 'r', FALSE);
7000 }
7001 else
7002 {
7003 prep_redo(cap->oap->regname, cap->count1,
7004 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7005
7006 curbuf->b_op_start = curwin->w_cursor;
7007#ifdef FEAT_MBYTE
7008 if (has_mbyte)
7009 {
7010 int old_State = State;
7011
7012 if (cap->ncharC1 != 0)
7013 AppendCharToRedobuff(cap->ncharC1);
7014 if (cap->ncharC2 != 0)
7015 AppendCharToRedobuff(cap->ncharC2);
7016
7017 /* This is slow, but it handles replacing a single-byte with a
7018 * multi-byte and the other way around. Also handles adding
7019 * composing characters for utf-8. */
7020 for (n = cap->count1; n > 0; --n)
7021 {
7022 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007023 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7024 {
7025 int c = ins_copychar(curwin->w_cursor.lnum
7026 + (cap->nchar == Ctrl_Y ? -1 : 1));
7027 if (c != NUL)
7028 ins_char(c);
7029 else
7030 /* will be decremented further down */
7031 ++curwin->w_cursor.col;
7032 }
7033 else
7034 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 State = old_State;
7036 if (cap->ncharC1 != 0)
7037 ins_char(cap->ncharC1);
7038 if (cap->ncharC2 != 0)
7039 ins_char(cap->ncharC2);
7040 }
7041 }
7042 else
7043#endif
7044 {
7045 /*
7046 * Replace the characters within one line.
7047 */
7048 for (n = cap->count1; n > 0; --n)
7049 {
7050 /*
7051 * Get ptr again, because u_save and/or showmatch() will have
7052 * released the line. At the same time we let know that the
7053 * line will be changed.
7054 */
7055 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007056 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7057 {
7058 int c = ins_copychar(curwin->w_cursor.lnum
7059 + (cap->nchar == Ctrl_Y ? -1 : 1));
7060 if (c != NUL)
7061 ptr[curwin->w_cursor.col] = c;
7062 }
7063 else
7064 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (p_sm && msg_silent == 0)
7066 showmatch(cap->nchar);
7067 ++curwin->w_cursor.col;
7068 }
7069#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007070 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007072 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar009b2592004-10-24 19:18:58 +00007074 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007075 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007077 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079#endif
7080
7081 /* mark the buffer as changed and prepare for displaying */
7082 changed_bytes(curwin->w_cursor.lnum,
7083 (colnr_T)(curwin->w_cursor.col - cap->count1));
7084 }
7085 --curwin->w_cursor.col; /* cursor on the last replaced char */
7086#ifdef FEAT_MBYTE
7087 /* if the character on the left of the current cursor is a multi-byte
7088 * character, move two characters left */
7089 if (has_mbyte)
7090 mb_adjust_cursor();
7091#endif
7092 curbuf->b_op_end = curwin->w_cursor;
7093 curwin->w_set_curswant = TRUE;
7094 set_last_insert(cap->nchar);
7095 }
7096}
7097
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098/*
7099 * 'o': Exchange start and end of Visual area.
7100 * 'O': same, but in block mode exchange left and right corners.
7101 */
7102 static void
7103v_swap_corners(cmdchar)
7104 int cmdchar;
7105{
7106 pos_T old_cursor;
7107 colnr_T left, right;
7108
7109 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7110 {
7111 old_cursor = curwin->w_cursor;
7112 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7113 curwin->w_cursor.lnum = VIsual.lnum;
7114 coladvance(left);
7115 VIsual = curwin->w_cursor;
7116
7117 curwin->w_cursor.lnum = old_cursor.lnum;
7118 curwin->w_curswant = right;
7119 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7120 * right one column */
7121 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7122 ++curwin->w_curswant;
7123 coladvance(curwin->w_curswant);
7124 if (curwin->w_cursor.col == old_cursor.col
7125#ifdef FEAT_VIRTUALEDIT
7126 && (!virtual_active()
7127 || curwin->w_cursor.coladd == old_cursor.coladd)
7128#endif
7129 )
7130 {
7131 curwin->w_cursor.lnum = VIsual.lnum;
7132 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7133 ++right;
7134 coladvance(right);
7135 VIsual = curwin->w_cursor;
7136
7137 curwin->w_cursor.lnum = old_cursor.lnum;
7138 coladvance(left);
7139 curwin->w_curswant = left;
7140 }
7141 }
7142 else
7143 {
7144 old_cursor = curwin->w_cursor;
7145 curwin->w_cursor = VIsual;
7146 VIsual = old_cursor;
7147 curwin->w_set_curswant = TRUE;
7148 }
7149}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151/*
7152 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7153 */
7154 static void
7155nv_Replace(cap)
7156 cmdarg_T *cap;
7157{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (VIsual_active) /* "R" is replace lines */
7159 {
7160 cap->cmdchar = 'c';
7161 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007162 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 VIsual_mode = 'V';
7164 nv_operator(cap);
7165 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007166 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 {
7168 if (!curbuf->b_p_ma)
7169 EMSG(_(e_modifiable));
7170 else
7171 {
7172#ifdef FEAT_VIRTUALEDIT
7173 if (virtual_active())
7174 coladvance(getviscol());
7175#endif
7176 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7177 }
7178 }
7179}
7180
7181#ifdef FEAT_VREPLACE
7182/*
7183 * "gr".
7184 */
7185 static void
7186nv_vreplace(cap)
7187 cmdarg_T *cap;
7188{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (VIsual_active)
7190 {
7191 cap->cmdchar = 'r';
7192 cap->nchar = cap->extra_char;
7193 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7194 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007195 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {
7197 if (!curbuf->b_p_ma)
7198 EMSG(_(e_modifiable));
7199 else
7200 {
7201 if (cap->extra_char == Ctrl_V) /* get another character */
7202 cap->extra_char = get_literal();
7203 stuffcharReadbuff(cap->extra_char);
7204 stuffcharReadbuff(ESC);
7205# ifdef FEAT_VIRTUALEDIT
7206 if (virtual_active())
7207 coladvance(getviscol());
7208# endif
7209 invoke_edit(cap, TRUE, 'v', FALSE);
7210 }
7211 }
7212}
7213#endif
7214
7215/*
7216 * Swap case for "~" command, when it does not work like an operator.
7217 */
7218 static void
7219n_swapchar(cap)
7220 cmdarg_T *cap;
7221{
7222 long n;
7223 pos_T startpos;
7224 int did_change = 0;
7225#ifdef FEAT_NETBEANS_INTG
7226 pos_T pos;
7227 char_u *ptr;
7228 int count;
7229#endif
7230
7231 if (checkclearopq(cap->oap))
7232 return;
7233
7234 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7235 {
7236 clearopbeep(cap->oap);
7237 return;
7238 }
7239
7240 prep_redo_cmd(cap);
7241
7242 if (u_save_cursor() == FAIL)
7243 return;
7244
7245 startpos = curwin->w_cursor;
7246#ifdef FEAT_NETBEANS_INTG
7247 pos = startpos;
7248#endif
7249 for (n = cap->count1; n > 0; --n)
7250 {
7251 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7252 inc_cursor();
7253 if (gchar_cursor() == NUL)
7254 {
7255 if (vim_strchr(p_ww, '~') != NULL
7256 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7257 {
7258#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007259 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 {
7261 if (did_change)
7262 {
7263 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007264 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007265 netbeans_removed(curbuf, pos.lnum, pos.col,
7266 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007268 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 }
7270 pos.col = 0;
7271 pos.lnum++;
7272 }
7273#endif
7274 ++curwin->w_cursor.lnum;
7275 curwin->w_cursor.col = 0;
7276 if (n > 1)
7277 {
7278 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7279 break;
7280 u_clearline();
7281 }
7282 }
7283 else
7284 break;
7285 }
7286 }
7287#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007288 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 ptr = ml_get(pos.lnum);
7291 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007292 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7293 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295#endif
7296
7297
7298 check_cursor();
7299 curwin->w_set_curswant = TRUE;
7300 if (did_change)
7301 {
7302 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7303 0L);
7304 curbuf->b_op_start = startpos;
7305 curbuf->b_op_end = curwin->w_cursor;
7306 if (curbuf->b_op_end.col > 0)
7307 --curbuf->b_op_end.col;
7308 }
7309}
7310
7311/*
7312 * Move cursor to mark.
7313 */
7314 static void
7315nv_cursormark(cap, flag, pos)
7316 cmdarg_T *cap;
7317 int flag;
7318 pos_T *pos;
7319{
7320 if (check_mark(pos) == FAIL)
7321 clearop(cap->oap);
7322 else
7323 {
7324 if (cap->cmdchar == '\''
7325 || cap->cmdchar == '`'
7326 || cap->cmdchar == '['
7327 || cap->cmdchar == ']')
7328 setpcmark();
7329 curwin->w_cursor = *pos;
7330 if (flag)
7331 beginline(BL_WHITE | BL_FIX);
7332 else
7333 check_cursor();
7334 }
7335 cap->oap->motion_type = flag ? MLINE : MCHAR;
7336 if (cap->cmdchar == '`')
7337 cap->oap->use_reg_one = TRUE;
7338 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7339 curwin->w_set_curswant = TRUE;
7340}
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342/*
7343 * Handle commands that are operators in Visual mode.
7344 */
7345 static void
7346v_visop(cap)
7347 cmdarg_T *cap;
7348{
7349 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7350
7351 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007352 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 if (isupper(cap->cmdchar))
7354 {
7355 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007356 {
7357 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7361 curwin->w_curswant = MAXCOL;
7362 }
7363 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7364 nv_operator(cap);
7365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367/*
7368 * "s" and "S" commands.
7369 */
7370 static void
7371nv_subst(cap)
7372 cmdarg_T *cap;
7373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7375 {
7376 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007377 {
7378 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 cap->cmdchar = 'c';
7382 nv_operator(cap);
7383 }
7384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 nv_optrans(cap);
7386}
7387
7388/*
7389 * Abbreviated commands.
7390 */
7391 static void
7392nv_abbrev(cap)
7393 cmdarg_T *cap;
7394{
7395 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7396 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7397
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 /* in Visual mode these commands are operators */
7399 if (VIsual_active)
7400 v_visop(cap);
7401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 nv_optrans(cap);
7403}
7404
7405/*
7406 * Translate a command into another command.
7407 */
7408 static void
7409nv_optrans(cap)
7410 cmdarg_T *cap;
7411{
7412 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7413 (char_u *)"d$", (char_u *)"c$",
7414 (char_u *)"cl", (char_u *)"cc",
7415 (char_u *)"yy", (char_u *)":s\r"};
7416 static char_u *str = (char_u *)"xXDCsSY&";
7417
7418 if (!checkclearopq(cap->oap))
7419 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007420 /* In Vi "2D" doesn't delete the next line. Can't translate it
7421 * either, because "2." should also not use the count. */
7422 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7423 {
7424 cap->oap->start = curwin->w_cursor;
7425 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007426#ifdef FEAT_EVAL
7427 set_op_var(OP_DELETE);
7428#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007429 cap->count1 = 1;
7430 nv_dollar(cap);
7431 finish_op = TRUE;
7432 ResetRedobuff();
7433 AppendCharToRedobuff('D');
7434 }
7435 else
7436 {
7437 if (cap->count0)
7438 stuffnumReadbuff(cap->count0);
7439 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
7442 cap->opcount = 0;
7443}
7444
7445/*
7446 * "'" and "`" commands. Also for "g'" and "g`".
7447 * cap->arg is TRUE for "'" and "g'".
7448 */
7449 static void
7450nv_gomark(cap)
7451 cmdarg_T *cap;
7452{
7453 pos_T *pos;
7454 int c;
7455#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007456 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7458#endif
7459
7460 if (cap->cmdchar == 'g')
7461 c = cap->extra_char;
7462 else
7463 c = cap->nchar;
7464 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7465 if (pos == (pos_T *)-1) /* jumped to other file */
7466 {
7467 if (cap->arg)
7468 {
7469 check_cursor_lnum();
7470 beginline(BL_WHITE | BL_FIX);
7471 }
7472 else
7473 check_cursor();
7474 }
7475 else
7476 nv_cursormark(cap, cap->arg, pos);
7477
7478#ifdef FEAT_VIRTUALEDIT
7479 /* May need to clear the coladd that a mark includes. */
7480 if (!virtual_active())
7481 curwin->w_cursor.coladd = 0;
7482#endif
7483#ifdef FEAT_FOLDING
7484 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007485 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007486 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 && (fdo_flags & FDO_MARK)
7488 && old_KeyTyped)
7489 foldOpenCursor();
7490#endif
7491}
7492
7493/*
7494 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7495 */
7496 static void
7497nv_pcmark(cap)
7498 cmdarg_T *cap;
7499{
7500#ifdef FEAT_JUMPLIST
7501 pos_T *pos;
7502# ifdef FEAT_FOLDING
7503 linenr_T lnum = curwin->w_cursor.lnum;
7504 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7505# endif
7506
7507 if (!checkclearopq(cap->oap))
7508 {
7509 if (cap->cmdchar == 'g')
7510 pos = movechangelist((int)cap->count1);
7511 else
7512 pos = movemark((int)cap->count1);
7513 if (pos == (pos_T *)-1) /* jump to other file */
7514 {
7515 curwin->w_set_curswant = TRUE;
7516 check_cursor();
7517 }
7518 else if (pos != NULL) /* can jump */
7519 nv_cursormark(cap, FALSE, pos);
7520 else if (cap->cmdchar == 'g')
7521 {
7522 if (curbuf->b_changelistlen == 0)
7523 EMSG(_("E664: changelist is empty"));
7524 else if (cap->count1 < 0)
7525 EMSG(_("E662: At start of changelist"));
7526 else
7527 EMSG(_("E663: At end of changelist"));
7528 }
7529 else
7530 clearopbeep(cap->oap);
7531# ifdef FEAT_FOLDING
7532 if (cap->oap->op_type == OP_NOP
7533 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7534 && (fdo_flags & FDO_MARK)
7535 && old_KeyTyped)
7536 foldOpenCursor();
7537# endif
7538 }
7539#else
7540 clearopbeep(cap->oap);
7541#endif
7542}
7543
7544/*
7545 * Handle '"' command.
7546 */
7547 static void
7548nv_regname(cap)
7549 cmdarg_T *cap;
7550{
7551 if (checkclearop(cap->oap))
7552 return;
7553#ifdef FEAT_EVAL
7554 if (cap->nchar == '=')
7555 cap->nchar = get_expr_register();
7556#endif
7557 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7558 {
7559 cap->oap->regname = cap->nchar;
7560 cap->opcount = cap->count0; /* remember count before '"' */
7561#ifdef FEAT_EVAL
7562 set_reg_var(cap->oap->regname);
7563#endif
7564 }
7565 else
7566 clearopbeep(cap->oap);
7567}
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569/*
7570 * Handle "v", "V" and "CTRL-V" commands.
7571 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7572 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007573 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 */
7575 static void
7576nv_visual(cap)
7577 cmdarg_T *cap;
7578{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007579 if (cap->cmdchar == Ctrl_Q)
7580 cap->cmdchar = Ctrl_V;
7581
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7583 * characterwise, linewise, or blockwise. */
7584 if (cap->oap->op_type != OP_NOP)
7585 {
7586 cap->oap->motion_force = cap->cmdchar;
7587 finish_op = FALSE; /* operator doesn't finish now but later */
7588 return;
7589 }
7590
7591 VIsual_select = cap->arg;
7592 if (VIsual_active) /* change Visual mode */
7593 {
7594 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7595 end_visual_mode();
7596 else /* toggle char/block mode */
7597 { /* or char/line mode */
7598 VIsual_mode = cap->cmdchar;
7599 showmode();
7600 }
7601 redraw_curbuf_later(INVERTED); /* update the inversion */
7602 }
7603 else /* start Visual mode */
7604 {
7605 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007606 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007608 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 VIsual = curwin->w_cursor;
7610
7611 VIsual_active = TRUE;
7612 VIsual_reselect = TRUE;
7613 if (!cap->arg)
7614 /* start Select mode when 'selectmode' contains "cmd" */
7615 may_start_select('c');
7616#ifdef FEAT_MOUSE
7617 setmouse();
7618#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007619 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 redraw_cmdline = TRUE; /* show visual mode later */
7621 /*
7622 * For V and ^V, we multiply the number of lines even if there
7623 * was only one -- webb
7624 */
7625 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7626 {
7627 curwin->w_cursor.lnum +=
7628 resel_VIsual_line_count * cap->count0 - 1;
7629 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7630 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7631 }
7632 VIsual_mode = resel_VIsual_mode;
7633 if (VIsual_mode == 'v')
7634 {
7635 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007636 {
7637 validate_virtcol();
7638 curwin->w_curswant = curwin->w_virtcol
7639 + resel_VIsual_vcol * cap->count0 - 1;
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007642 curwin->w_curswant = resel_VIsual_vcol;
7643 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007645 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
7647 curwin->w_curswant = MAXCOL;
7648 coladvance((colnr_T)MAXCOL);
7649 }
7650 else if (VIsual_mode == Ctrl_V)
7651 {
7652 validate_virtcol();
7653 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007654 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 coladvance(curwin->w_curswant);
7656 }
7657 else
7658 curwin->w_set_curswant = TRUE;
7659 redraw_curbuf_later(INVERTED); /* show the inversion */
7660 }
7661 else
7662 {
7663 if (!cap->arg)
7664 /* start Select mode when 'selectmode' contains "cmd" */
7665 may_start_select('c');
7666 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007667 if (VIsual_mode != 'V' && *p_sel == 'e')
7668 ++cap->count1; /* include one more char */
7669 if (cap->count0 > 0 && --cap->count1 > 0)
7670 {
7671 /* With a count select that many characters or lines. */
7672 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7673 nv_right(cap);
7674 else if (VIsual_mode == 'V')
7675 nv_down(cap);
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 }
7679}
7680
7681/*
7682 * Start selection for Shift-movement keys.
7683 */
7684 void
7685start_selection()
7686{
7687 /* if 'selectmode' contains "key", start Select mode */
7688 may_start_select('k');
7689 n_start_visual_mode('v');
7690}
7691
7692/*
7693 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7694 */
7695 void
7696may_start_select(c)
7697 int c;
7698{
7699 VIsual_select = (stuff_empty() && typebuf_typed()
7700 && (vim_strchr(p_slm, c) != NULL));
7701}
7702
7703/*
7704 * Start Visual mode "c".
7705 * Should set VIsual_select before calling this.
7706 */
7707 static void
7708n_start_visual_mode(c)
7709 int c;
7710{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007711#ifdef FEAT_CONCEAL
7712 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007713 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007714#endif
7715
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 VIsual_mode = c;
7717 VIsual_active = TRUE;
7718 VIsual_reselect = TRUE;
7719#ifdef FEAT_VIRTUALEDIT
7720 /* Corner case: the 0 position in a tab may change when going into
7721 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7722 */
7723 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007724 {
7725 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728#endif
7729 VIsual = curwin->w_cursor;
7730
7731#ifdef FEAT_FOLDING
7732 foldAdjustVisual();
7733#endif
7734
7735#ifdef FEAT_MOUSE
7736 setmouse();
7737#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007738#ifdef FEAT_CONCEAL
7739 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007740 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007741#endif
7742
Bram Moolenaar09df3122006-01-23 22:23:09 +00007743 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 redraw_cmdline = TRUE; /* show visual mode later */
7745#ifdef FEAT_CLIPBOARD
7746 /* Make sure the clipboard gets updated. Needed because start and
7747 * end may still be the same, and the selection needs to be owned */
7748 clip_star.vmode = NUL;
7749#endif
7750
7751 /* Only need to redraw this line, unless still need to redraw an old
7752 * Visual area (when 'lazyredraw' is set). */
7753 if (curwin->w_redr_type < INVERTED)
7754 {
7755 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7756 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7757 }
7758}
7759
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
7761/*
7762 * CTRL-W: Window commands
7763 */
7764 static void
7765nv_window(cap)
7766 cmdarg_T *cap;
7767{
7768#ifdef FEAT_WINDOWS
7769 if (!checkclearop(cap->oap))
7770 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7771#else
7772 (void)checkclearop(cap->oap);
7773#endif
7774}
7775
7776/*
7777 * CTRL-Z: Suspend
7778 */
7779 static void
7780nv_suspend(cap)
7781 cmdarg_T *cap;
7782{
7783 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 if (VIsual_active)
7785 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 do_cmdline_cmd((char_u *)"st");
7787}
7788
7789/*
7790 * Commands starting with "g".
7791 */
7792 static void
7793nv_g_cmd(cap)
7794 cmdarg_T *cap;
7795{
7796 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 int i;
7799 int flag = FALSE;
7800
7801 switch (cap->nchar)
7802 {
7803#ifdef MEM_PROFILE
7804 /*
7805 * "g^A": dump log of used memory.
7806 */
7807 case Ctrl_A:
7808 vim_mem_profile_dump();
7809 break;
7810#endif
7811
7812#ifdef FEAT_VREPLACE
7813 /*
7814 * "gR": Enter virtual replace mode.
7815 */
7816 case 'R':
7817 cap->arg = TRUE;
7818 nv_Replace(cap);
7819 break;
7820
7821 case 'r':
7822 nv_vreplace(cap);
7823 break;
7824#endif
7825
7826 case '&':
7827 do_cmdline_cmd((char_u *)"%s//~/&");
7828 break;
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 /*
7831 * "gv": Reselect the previous Visual area. If Visual already active,
7832 * exchange previous and current Visual area.
7833 */
7834 case 'v':
7835 if (checkclearop(oap))
7836 break;
7837
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007838 if ( curbuf->b_visual.vi_start.lnum == 0
7839 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7840 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 beep_flush();
7842 else
7843 {
7844 /* set w_cursor to the start of the Visual area, tpos to the end */
7845 if (VIsual_active)
7846 {
7847 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007848 VIsual_mode = curbuf->b_visual.vi_mode;
7849 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850# ifdef FEAT_EVAL
7851 curbuf->b_visual_mode_eval = i;
7852# endif
7853 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007854 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7855 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007857 tpos = curbuf->b_visual.vi_end;
7858 curbuf->b_visual.vi_end = curwin->w_cursor;
7859 curwin->w_cursor = curbuf->b_visual.vi_start;
7860 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862 else
7863 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007864 VIsual_mode = curbuf->b_visual.vi_mode;
7865 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7866 tpos = curbuf->b_visual.vi_end;
7867 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869
7870 VIsual_active = TRUE;
7871 VIsual_reselect = TRUE;
7872
7873 /* Set Visual to the start and w_cursor to the end of the Visual
7874 * area. Make sure they are on an existing character. */
7875 check_cursor();
7876 VIsual = curwin->w_cursor;
7877 curwin->w_cursor = tpos;
7878 check_cursor();
7879 update_topline();
7880 /*
7881 * When called from normal "g" command: start Select mode when
7882 * 'selectmode' contains "cmd". When called for K_SELECT, always
7883 * start Select mode.
7884 */
7885 if (cap->arg)
7886 VIsual_select = TRUE;
7887 else
7888 may_start_select('c');
7889#ifdef FEAT_MOUSE
7890 setmouse();
7891#endif
7892#ifdef FEAT_CLIPBOARD
7893 /* Make sure the clipboard gets updated. Needed because start and
7894 * end are still the same, and the selection needs to be owned */
7895 clip_star.vmode = NUL;
7896#endif
7897 redraw_curbuf_later(INVERTED);
7898 showmode();
7899 }
7900 break;
7901 /*
7902 * "gV": Don't reselect the previous Visual area after a Select mode
7903 * mapping of menu.
7904 */
7905 case 'V':
7906 VIsual_reselect = FALSE;
7907 break;
7908
7909 /*
7910 * "gh": start Select mode.
7911 * "gH": start Select line mode.
7912 * "g^H": start Select block mode.
7913 */
7914 case K_BS:
7915 cap->nchar = Ctrl_H;
7916 /* FALLTHROUGH */
7917 case 'h':
7918 case 'H':
7919 case Ctrl_H:
7920# ifdef EBCDIC
7921 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7922 if (cap->nchar == Ctrl_H)
7923 cap->cmdchar = Ctrl_V;
7924 else
7925# endif
7926 cap->cmdchar = cap->nchar + ('v' - 'h');
7927 cap->arg = TRUE;
7928 nv_visual(cap);
7929 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007930
7931 /* "gn", "gN" visually select next/previous search match
7932 * "gn" selects next match
7933 * "gN" selects previous match
7934 */
7935 case 'N':
7936 case 'n':
7937 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007938 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
7941 /*
7942 * "gj" and "gk" two new funny movement keys -- up and down
7943 * movement based on *screen* line rather than *file* line.
7944 */
7945 case 'j':
7946 case K_DOWN:
7947 /* with 'nowrap' it works just like the normal "j" command; also when
7948 * in a closed fold */
7949 if (!curwin->w_p_wrap
7950#ifdef FEAT_FOLDING
7951 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7952#endif
7953 )
7954 {
7955 oap->motion_type = MLINE;
7956 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7957 }
7958 else
7959 i = nv_screengo(oap, FORWARD, cap->count1);
7960 if (i == FAIL)
7961 clearopbeep(oap);
7962 break;
7963
7964 case 'k':
7965 case K_UP:
7966 /* with 'nowrap' it works just like the normal "k" command; also when
7967 * in a closed fold */
7968 if (!curwin->w_p_wrap
7969#ifdef FEAT_FOLDING
7970 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7971#endif
7972 )
7973 {
7974 oap->motion_type = MLINE;
7975 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7976 }
7977 else
7978 i = nv_screengo(oap, BACKWARD, cap->count1);
7979 if (i == FAIL)
7980 clearopbeep(oap);
7981 break;
7982
7983 /*
7984 * "gJ": join two lines without inserting a space.
7985 */
7986 case 'J':
7987 nv_join(cap);
7988 break;
7989
7990 /*
7991 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7992 * "gm": middle of "g0" and "g$".
7993 */
7994 case '^':
7995 flag = TRUE;
7996 /* FALLTHROUGH */
7997
7998 case '0':
7999 case 'm':
8000 case K_HOME:
8001 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 oap->motion_type = MCHAR;
8003 oap->inclusive = FALSE;
8004 if (curwin->w_p_wrap
8005#ifdef FEAT_VERTSPLIT
8006 && curwin->w_width != 0
8007#endif
8008 )
8009 {
8010 int width1 = W_WIDTH(curwin) - curwin_col_off();
8011 int width2 = width1 + curwin_col_off2();
8012
8013 validate_virtcol();
8014 i = 0;
8015 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8016 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8017 }
8018 else
8019 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008020 /* Go to the middle of the screen line. When 'number' or
8021 * 'relativenumber' is on and lines are wrapping the middle can be more
8022 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 if (cap->nchar == 'm')
8024 i += (W_WIDTH(curwin) - curwin_col_off()
8025 + ((curwin->w_p_wrap && i > 0)
8026 ? curwin_col_off2() : 0)) / 2;
8027 coladvance((colnr_T)i);
8028 if (flag)
8029 {
8030 do
8031 i = gchar_cursor();
8032 while (vim_iswhite(i) && oneright() == OK);
8033 }
8034 curwin->w_set_curswant = TRUE;
8035 break;
8036
8037 case '_':
8038 /* "g_": to the last non-blank character in the line or <count> lines
8039 * downward. */
8040 cap->oap->motion_type = MCHAR;
8041 cap->oap->inclusive = TRUE;
8042 curwin->w_curswant = MAXCOL;
8043 if (cursor_down((long)(cap->count1 - 1),
8044 cap->oap->op_type == OP_NOP) == FAIL)
8045 clearopbeep(cap->oap);
8046 else
8047 {
8048 char_u *ptr = ml_get_curline();
8049
8050 /* In Visual mode we may end up after the line. */
8051 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8052 --curwin->w_cursor.col;
8053
8054 /* Decrease the cursor column until it's on a non-blank. */
8055 while (curwin->w_cursor.col > 0
8056 && vim_iswhite(ptr[curwin->w_cursor.col]))
8057 --curwin->w_cursor.col;
8058 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008059 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061 break;
8062
8063 case '$':
8064 case K_END:
8065 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {
8067 int col_off = curwin_col_off();
8068
8069 oap->motion_type = MCHAR;
8070 oap->inclusive = TRUE;
8071 if (curwin->w_p_wrap
8072#ifdef FEAT_VERTSPLIT
8073 && curwin->w_width != 0
8074#endif
8075 )
8076 {
8077 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8078 if (cap->count1 == 1)
8079 {
8080 int width1 = W_WIDTH(curwin) - col_off;
8081 int width2 = width1 + curwin_col_off2();
8082
8083 validate_virtcol();
8084 i = width1 - 1;
8085 if (curwin->w_virtcol >= (colnr_T)width1)
8086 i += ((curwin->w_virtcol - width1) / width2 + 1)
8087 * width2;
8088 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008089
8090 /* Make sure we stick in this column. */
8091 validate_virtcol();
8092 curwin->w_curswant = curwin->w_virtcol;
8093 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8095 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8096 {
8097 /*
8098 * Check for landing on a character that got split at
8099 * the end of the line. We do not want to advance to
8100 * the next screen line.
8101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (curwin->w_virtcol > (colnr_T)i)
8103 --curwin->w_cursor.col;
8104 }
8105#endif
8106 }
8107 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8108 clearopbeep(oap);
8109 }
8110 else
8111 {
8112 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8113 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008114
8115 /* Make sure we stick in this column. */
8116 validate_virtcol();
8117 curwin->w_curswant = curwin->w_virtcol;
8118 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
8120 }
8121 break;
8122
8123 /*
8124 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8125 */
8126 case '*':
8127 case '#':
8128#if POUND != '#'
8129 case POUND: /* pound sign (sometimes equal to '#') */
8130#endif
8131 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8132 case ']': /* :tselect for current identifier */
8133 nv_ident(cap);
8134 break;
8135
8136 /*
8137 * ge and gE: go back to end of word
8138 */
8139 case 'e':
8140 case 'E':
8141 oap->motion_type = MCHAR;
8142 curwin->w_set_curswant = TRUE;
8143 oap->inclusive = TRUE;
8144 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8145 clearopbeep(oap);
8146 break;
8147
8148 /*
8149 * "g CTRL-G": display info about cursor position
8150 */
8151 case Ctrl_G:
8152 cursor_pos_info();
8153 break;
8154
8155 /*
8156 * "gi": start Insert at the last position.
8157 */
8158 case 'i':
8159 if (curbuf->b_last_insert.lnum != 0)
8160 {
8161 curwin->w_cursor = curbuf->b_last_insert;
8162 check_cursor_lnum();
8163 i = (int)STRLEN(ml_get_curline());
8164 if (curwin->w_cursor.col > (colnr_T)i)
8165 {
8166#ifdef FEAT_VIRTUALEDIT
8167 if (virtual_active())
8168 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8169#endif
8170 curwin->w_cursor.col = i;
8171 }
8172 }
8173 cap->cmdchar = 'i';
8174 nv_edit(cap);
8175 break;
8176
8177 /*
8178 * "gI": Start insert in column 1.
8179 */
8180 case 'I':
8181 beginline(0);
8182 if (!checkclearopq(oap))
8183 invoke_edit(cap, FALSE, 'g', FALSE);
8184 break;
8185
8186#ifdef FEAT_SEARCHPATH
8187 /*
8188 * "gf": goto file, edit file under cursor
8189 * "]f" and "[f": can also be used.
8190 */
8191 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008192 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 nv_gotofile(cap);
8194 break;
8195#endif
8196
8197 /* "g'm" and "g`m": jump to mark without setting pcmark */
8198 case '\'':
8199 cap->arg = TRUE;
8200 /*FALLTHROUGH*/
8201 case '`':
8202 nv_gomark(cap);
8203 break;
8204
8205 /*
8206 * "gs": Goto sleep.
8207 */
8208 case 's':
8209 do_sleep(cap->count1 * 1000L);
8210 break;
8211
8212 /*
8213 * "ga": Display the ascii value of the character under the
8214 * cursor. It is displayed in decimal, hex, and octal. -- webb
8215 */
8216 case 'a':
8217 do_ascii(NULL);
8218 break;
8219
8220#ifdef FEAT_MBYTE
8221 /*
8222 * "g8": Display the bytes used for the UTF-8 character under the
8223 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008224 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 */
8226 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008227 if (cap->count0 == 8)
8228 utf_find_illegal();
8229 else
8230 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 break;
8232#endif
8233
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008234 case '<':
8235 show_sb_text();
8236 break;
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 /*
8239 * "gg": Goto the first line in file. With a count it goes to
8240 * that line number like for "G". -- webb
8241 */
8242 case 'g':
8243 cap->arg = FALSE;
8244 nv_goto(cap);
8245 break;
8246
8247 /*
8248 * Two-character operators:
8249 * "gq" Format text
8250 * "gw" Format text and keep cursor position
8251 * "g~" Toggle the case of the text.
8252 * "gu" Change text to lower case.
8253 * "gU" Change text to upper case.
8254 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008255 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 */
8257 case 'q':
8258 case 'w':
8259 oap->cursor_start = curwin->w_cursor;
8260 /*FALLTHROUGH*/
8261 case '~':
8262 case 'u':
8263 case 'U':
8264 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008265 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 nv_operator(cap);
8267 break;
8268
8269 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008270 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 * current function
8272 * "gD": idem, but in the current file.
8273 */
8274 case 'd':
8275 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008276 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 break;
8278
8279#ifdef FEAT_MOUSE
8280 /*
8281 * g<*Mouse> : <C-*mouse>
8282 */
8283 case K_MIDDLEMOUSE:
8284 case K_MIDDLEDRAG:
8285 case K_MIDDLERELEASE:
8286 case K_LEFTMOUSE:
8287 case K_LEFTDRAG:
8288 case K_LEFTRELEASE:
8289 case K_RIGHTMOUSE:
8290 case K_RIGHTDRAG:
8291 case K_RIGHTRELEASE:
8292 case K_X1MOUSE:
8293 case K_X1DRAG:
8294 case K_X1RELEASE:
8295 case K_X2MOUSE:
8296 case K_X2DRAG:
8297 case K_X2RELEASE:
8298 mod_mask = MOD_MASK_CTRL;
8299 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8300 break;
8301#endif
8302
8303 case K_IGNORE:
8304 break;
8305
8306 /*
8307 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8308 */
8309 case 'p':
8310 case 'P':
8311 nv_put(cap);
8312 break;
8313
8314#ifdef FEAT_BYTEOFF
8315 /* "go": goto byte count from start of buffer */
8316 case 'o':
8317 goto_byte(cap->count0);
8318 break;
8319#endif
8320
8321 /* "gQ": improved Ex mode */
8322 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008323 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
8325 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008326 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 break;
8328 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (!checkclearopq(oap))
8331 do_exmode(TRUE);
8332 break;
8333
8334#ifdef FEAT_JUMPLIST
8335 case ',':
8336 nv_pcmark(cap);
8337 break;
8338
8339 case ';':
8340 cap->count1 = -cap->count1;
8341 nv_pcmark(cap);
8342 break;
8343#endif
8344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008345#ifdef FEAT_WINDOWS
8346 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008347 if (!checkclearop(oap))
8348 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008349 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008350 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008351 if (!checkclearop(oap))
8352 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008353 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008354#endif
8355
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008356 case '+':
8357 case '-': /* "g+" and "g-": undo or redo along the timeline */
8358 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008359 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008360 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008361 break;
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 default:
8364 clearopbeep(oap);
8365 break;
8366 }
8367}
8368
8369/*
8370 * Handle "o" and "O" commands.
8371 */
8372 static void
8373n_opencmd(cap)
8374 cmdarg_T *cap;
8375{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008376#ifdef FEAT_CONCEAL
8377 linenr_T oldline = curwin->w_cursor.lnum;
8378#endif
8379
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 if (!checkclearopq(cap->oap))
8381 {
8382#ifdef FEAT_FOLDING
8383 if (cap->cmdchar == 'O')
8384 /* Open above the first line of a folded sequence of lines */
8385 (void)hasFolding(curwin->w_cursor.lnum,
8386 &curwin->w_cursor.lnum, NULL);
8387 else
8388 /* Open below the last line of a folded sequence of lines */
8389 (void)hasFolding(curwin->w_cursor.lnum,
8390 NULL, &curwin->w_cursor.lnum);
8391#endif
8392 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8393 (cap->cmdchar == 'O' ? 1 : 0)),
8394 (linenr_T)(curwin->w_cursor.lnum +
8395 (cap->cmdchar == 'o' ? 1 : 0))
8396 ) == OK
8397 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8398#ifdef FEAT_COMMENTS
8399 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8400#endif
8401 0, 0))
8402 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008403#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008404 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008405 update_single_line(curwin, oldline);
8406#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008407 /* When '#' is in 'cpoptions' ignore the count. */
8408 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8409 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8411 }
8412 }
8413}
8414
8415/*
8416 * "." command: redo last change.
8417 */
8418 static void
8419nv_dot(cap)
8420 cmdarg_T *cap;
8421{
8422 if (!checkclearopq(cap->oap))
8423 {
8424 /*
8425 * If "restart_edit" is TRUE, the last but one command is repeated
8426 * instead of the last command (inserting text). This is used for
8427 * CTRL-O <.> in insert mode.
8428 */
8429 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8430 clearopbeep(cap->oap);
8431 }
8432}
8433
8434/*
8435 * CTRL-R: undo undo
8436 */
8437 static void
8438nv_redo(cap)
8439 cmdarg_T *cap;
8440{
8441 if (!checkclearopq(cap->oap))
8442 {
8443 u_redo((int)cap->count1);
8444 curwin->w_set_curswant = TRUE;
8445 }
8446}
8447
8448/*
8449 * Handle "U" command.
8450 */
8451 static void
8452nv_Undo(cap)
8453 cmdarg_T *cap;
8454{
8455 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008456 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /* translate "gUU" to "gUgU" */
8459 cap->cmdchar = 'g';
8460 cap->nchar = 'U';
8461 nv_operator(cap);
8462 }
8463 else if (!checkclearopq(cap->oap))
8464 {
8465 u_undoline();
8466 curwin->w_set_curswant = TRUE;
8467 }
8468}
8469
8470/*
8471 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8472 * single character.
8473 */
8474 static void
8475nv_tilde(cap)
8476 cmdarg_T *cap;
8477{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008478 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 n_swapchar(cap);
8480 else
8481 nv_operator(cap);
8482}
8483
8484/*
8485 * Handle an operator command.
8486 * The actual work is done by do_pending_operator().
8487 */
8488 static void
8489nv_operator(cap)
8490 cmdarg_T *cap;
8491{
8492 int op_type;
8493
8494 op_type = get_op_type(cap->cmdchar, cap->nchar);
8495
8496 if (op_type == cap->oap->op_type) /* double operator works on lines */
8497 nv_lineop(cap);
8498 else if (!checkclearop(cap->oap))
8499 {
8500 cap->oap->start = curwin->w_cursor;
8501 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008502#ifdef FEAT_EVAL
8503 set_op_var(op_type);
8504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
8506}
8507
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008508#ifdef FEAT_EVAL
8509/*
8510 * Set v:operator to the characters for "optype".
8511 */
8512 static void
8513set_op_var(optype)
8514 int optype;
8515{
8516 char_u opchars[3];
8517
8518 if (optype == OP_NOP)
8519 set_vim_var_string(VV_OP, NULL, 0);
8520 else
8521 {
8522 opchars[0] = get_op_char(optype);
8523 opchars[1] = get_extra_op_char(optype);
8524 opchars[2] = NUL;
8525 set_vim_var_string(VV_OP, opchars, -1);
8526 }
8527}
8528#endif
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530/*
8531 * Handle linewise operator "dd", "yy", etc.
8532 *
8533 * "_" is is a strange motion command that helps make operators more logical.
8534 * It is actually implemented, but not documented in the real Vi. This motion
8535 * command actually refers to "the current line". Commands like "dd" and "yy"
8536 * are really an alternate form of "d_" and "y_". It does accept a count, so
8537 * "d3_" works to delete 3 lines.
8538 */
8539 static void
8540nv_lineop(cap)
8541 cmdarg_T *cap;
8542{
8543 cap->oap->motion_type = MLINE;
8544 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8545 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008546 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8547 && cap->oap->motion_force != 'v'
8548 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 || cap->oap->op_type == OP_LSHIFT
8550 || cap->oap->op_type == OP_RSHIFT)
8551 beginline(BL_SOL | BL_FIX);
8552 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8553 beginline(BL_WHITE | BL_FIX);
8554}
8555
8556/*
8557 * <Home> command.
8558 */
8559 static void
8560nv_home(cap)
8561 cmdarg_T *cap;
8562{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008563 /* CTRL-HOME is like "gg" */
8564 if (mod_mask & MOD_MASK_CTRL)
8565 nv_goto(cap);
8566 else
8567 {
8568 cap->count0 = 1;
8569 nv_pipe(cap);
8570 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008571 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8572 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573}
8574
8575/*
8576 * "|" command.
8577 */
8578 static void
8579nv_pipe(cap)
8580 cmdarg_T *cap;
8581{
8582 cap->oap->motion_type = MCHAR;
8583 cap->oap->inclusive = FALSE;
8584 beginline(0);
8585 if (cap->count0 > 0)
8586 {
8587 coladvance((colnr_T)(cap->count0 - 1));
8588 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8589 }
8590 else
8591 curwin->w_curswant = 0;
8592 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008593 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 curwin->w_set_curswant = FALSE;
8595}
8596
8597/*
8598 * Handle back-word command "b" and "B".
8599 * cap->arg is 1 for "B"
8600 */
8601 static void
8602nv_bck_word(cap)
8603 cmdarg_T *cap;
8604{
8605 cap->oap->motion_type = MCHAR;
8606 cap->oap->inclusive = FALSE;
8607 curwin->w_set_curswant = TRUE;
8608 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8609 clearopbeep(cap->oap);
8610#ifdef FEAT_FOLDING
8611 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8612 foldOpenCursor();
8613#endif
8614}
8615
8616/*
8617 * Handle word motion commands "e", "E", "w" and "W".
8618 * cap->arg is TRUE for "E" and "W".
8619 */
8620 static void
8621nv_wordcmd(cap)
8622 cmdarg_T *cap;
8623{
8624 int n;
8625 int word_end;
8626 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008627 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629 /*
8630 * Set inclusive for the "E" and "e" command.
8631 */
8632 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8633 word_end = TRUE;
8634 else
8635 word_end = FALSE;
8636 cap->oap->inclusive = word_end;
8637
8638 /*
8639 * "cw" and "cW" are a special case.
8640 */
8641 if (!word_end && cap->oap->op_type == OP_CHANGE)
8642 {
8643 n = gchar_cursor();
8644 if (n != NUL) /* not an empty line */
8645 {
8646 if (vim_iswhite(n))
8647 {
8648 /*
8649 * Reproduce a funny Vi behaviour: "cw" on a blank only
8650 * changes one character, not all blanks until the start of
8651 * the next word. Only do this when the 'w' flag is included
8652 * in 'cpoptions'.
8653 */
8654 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8655 {
8656 cap->oap->inclusive = TRUE;
8657 cap->oap->motion_type = MCHAR;
8658 return;
8659 }
8660 }
8661 else
8662 {
8663 /*
8664 * This is a little strange. To match what the real Vi does,
8665 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8666 * that we are not on a space or a TAB. This seems impolite
8667 * at first, but it's really more what we mean when we say
8668 * 'cw'.
8669 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008670 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * will change only one character! This is done by setting
8672 * flag.
8673 */
8674 cap->oap->inclusive = TRUE;
8675 word_end = TRUE;
8676 flag = TRUE;
8677 }
8678 }
8679 }
8680
8681 cap->oap->motion_type = MCHAR;
8682 curwin->w_set_curswant = TRUE;
8683 if (word_end)
8684 n = end_word(cap->count1, cap->arg, flag, FALSE);
8685 else
8686 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8687
Bram Moolenaardfefb982008-04-01 10:06:39 +00008688 /* Don't leave the cursor on the NUL past the end of line. Unless we
8689 * didn't move it forward. */
8690 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008691 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
8693 if (n == FAIL && cap->oap->op_type == OP_NOP)
8694 clearopbeep(cap->oap);
8695 else
8696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#ifdef FEAT_FOLDING
8699 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8700 foldOpenCursor();
8701#endif
8702 }
8703}
8704
8705/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008706 * Used after a movement command: If the cursor ends up on the NUL after the
8707 * end of the line, may move it back to the last character and make the motion
8708 * inclusive.
8709 */
8710 static void
8711adjust_cursor(oap)
8712 oparg_T *oap;
8713{
8714 /* The cursor cannot remain on the NUL when:
8715 * - the column is > 0
8716 * - not in Visual mode or 'selection' is "o"
8717 * - 'virtualedit' is not "all" and not "onemore".
8718 */
8719 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008720 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008721#ifdef FEAT_VIRTUALEDIT
8722 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8723#endif
8724 )
8725 {
8726 --curwin->w_cursor.col;
8727#ifdef FEAT_MBYTE
8728 /* prevent cursor from moving on the trail byte */
8729 if (has_mbyte)
8730 mb_adjust_cursor();
8731#endif
8732 oap->inclusive = TRUE;
8733 }
8734}
8735
8736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 * "0" and "^" commands.
8738 * cap->arg is the argument for beginline().
8739 */
8740 static void
8741nv_beginline(cap)
8742 cmdarg_T *cap;
8743{
8744 cap->oap->motion_type = MCHAR;
8745 cap->oap->inclusive = FALSE;
8746 beginline(cap->arg);
8747#ifdef FEAT_FOLDING
8748 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8749 foldOpenCursor();
8750#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008751 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8752 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755/*
8756 * In exclusive Visual mode, may include the last character.
8757 */
8758 static void
8759adjust_for_sel(cap)
8760 cmdarg_T *cap;
8761{
8762 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8763 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8764 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008765#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 if (has_mbyte)
8767 inc_cursor();
8768 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 ++curwin->w_cursor.col;
8771 cap->oap->inclusive = FALSE;
8772 }
8773}
8774
8775/*
8776 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8777 * Should check VIsual_mode before calling this.
8778 * Returns TRUE when backed up to the previous line.
8779 */
8780 static int
8781unadjust_for_sel()
8782{
8783 pos_T *pp;
8784
8785 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8786 {
8787 if (lt(VIsual, curwin->w_cursor))
8788 pp = &curwin->w_cursor;
8789 else
8790 pp = &VIsual;
8791#ifdef FEAT_VIRTUALEDIT
8792 if (pp->coladd > 0)
8793 --pp->coladd;
8794 else
8795#endif
8796 if (pp->col > 0)
8797 {
8798 --pp->col;
8799#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008800 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801#endif
8802 }
8803 else if (pp->lnum > 1)
8804 {
8805 --pp->lnum;
8806 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8807 return TRUE;
8808 }
8809 }
8810 return FALSE;
8811}
8812
8813/*
8814 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8815 */
8816 static void
8817nv_select(cap)
8818 cmdarg_T *cap;
8819{
8820 if (VIsual_active)
8821 VIsual_select = TRUE;
8822 else if (VIsual_reselect)
8823 {
8824 cap->nchar = 'v'; /* fake "gv" command */
8825 cap->arg = TRUE;
8826 nv_g_cmd(cap);
8827 }
8828}
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
8831/*
8832 * "G", "gg", CTRL-END, CTRL-HOME.
8833 * cap->arg is TRUE for "G".
8834 */
8835 static void
8836nv_goto(cap)
8837 cmdarg_T *cap;
8838{
8839 linenr_T lnum;
8840
8841 if (cap->arg)
8842 lnum = curbuf->b_ml.ml_line_count;
8843 else
8844 lnum = 1L;
8845 cap->oap->motion_type = MLINE;
8846 setpcmark();
8847
8848 /* When a count is given, use it instead of the default lnum */
8849 if (cap->count0 != 0)
8850 lnum = cap->count0;
8851 if (lnum < 1L)
8852 lnum = 1L;
8853 else if (lnum > curbuf->b_ml.ml_line_count)
8854 lnum = curbuf->b_ml.ml_line_count;
8855 curwin->w_cursor.lnum = lnum;
8856 beginline(BL_SOL | BL_FIX);
8857#ifdef FEAT_FOLDING
8858 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8859 foldOpenCursor();
8860#endif
8861}
8862
8863/*
8864 * CTRL-\ in Normal mode.
8865 */
8866 static void
8867nv_normal(cap)
8868 cmdarg_T *cap;
8869{
8870 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8871 {
8872 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008873 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 clear_cmdline = TRUE; /* unshow mode later */
8875 restart_edit = 0;
8876#ifdef FEAT_CMDWIN
8877 if (cmdwin_type != 0)
8878 cmdwin_result = Ctrl_C;
8879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (VIsual_active)
8881 {
8882 end_visual_mode(); /* stop Visual */
8883 redraw_curbuf_later(INVERTED);
8884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8886 if (cap->nchar == Ctrl_G && p_im)
8887 restart_edit = 'a';
8888 }
8889 else
8890 clearopbeep(cap->oap);
8891}
8892
8893/*
8894 * ESC in Normal mode: beep, but don't flush buffers.
8895 * Don't even beep if we are canceling a command.
8896 */
8897 static void
8898nv_esc(cap)
8899 cmdarg_T *cap;
8900{
8901 int no_reason;
8902
8903 no_reason = (cap->oap->op_type == OP_NOP
8904 && cap->opcount == 0
8905 && cap->count0 == 0
8906 && cap->oap->regname == 0
8907 && !p_im);
8908
8909 if (cap->arg) /* TRUE for CTRL-C */
8910 {
8911 if (restart_edit == 0
8912#ifdef FEAT_CMDWIN
8913 && cmdwin_type == 0
8914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 && no_reason)
8917 MSG(_("Type :quit<Enter> to exit Vim"));
8918
8919 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8920 * set again below when halfway a mapping. */
8921 if (!p_im)
8922 restart_edit = 0;
8923#ifdef FEAT_CMDWIN
8924 if (cmdwin_type != 0)
8925 {
8926 cmdwin_result = K_IGNORE;
8927 got_int = FALSE; /* don't stop executing autocommands et al. */
8928 return;
8929 }
8930#endif
8931 }
8932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 if (VIsual_active)
8934 {
8935 end_visual_mode(); /* stop Visual */
8936 check_cursor_col(); /* make sure cursor is not beyond EOL */
8937 curwin->w_set_curswant = TRUE;
8938 redraw_curbuf_later(INVERTED);
8939 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008940 else if (no_reason)
8941 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 clearop(cap->oap);
8943
8944 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8945 * set return to Insert mode afterwards. */
8946 if (restart_edit == 0 && goto_im()
8947#ifdef FEAT_EX_EXTRA
8948 && ex_normal_busy == 0
8949#endif
8950 )
8951 restart_edit = 'a';
8952}
8953
8954/*
8955 * Handle "A", "a", "I", "i" and <Insert> commands.
8956 */
8957 static void
8958nv_edit(cap)
8959 cmdarg_T *cap;
8960{
8961 /* <Insert> is equal to "i" */
8962 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8963 cap->cmdchar = 'i';
8964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 /* in Visual mode "A" and "I" are an operator */
8966 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8967 v_visop(cap);
8968
8969 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008970 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8971 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 {
8973#ifdef FEAT_TEXTOBJ
8974 nv_object(cap);
8975#else
8976 clearopbeep(cap->oap);
8977#endif
8978 }
8979 else if (!curbuf->b_p_ma && !p_im)
8980 {
8981 /* Only give this error when 'insertmode' is off. */
8982 EMSG(_(e_modifiable));
8983 clearop(cap->oap);
8984 }
8985 else if (!checkclearopq(cap->oap))
8986 {
8987 switch (cap->cmdchar)
8988 {
8989 case 'A': /* "A"ppend after the line */
8990 curwin->w_set_curswant = TRUE;
8991#ifdef FEAT_VIRTUALEDIT
8992 if (ve_flags == VE_ALL)
8993 {
8994 int save_State = State;
8995
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008996 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 * character past the end of the line */
8998 State = INSERT;
8999 coladvance((colnr_T)MAXCOL);
9000 State = save_State;
9001 }
9002 else
9003#endif
9004 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9005 break;
9006
9007 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009008 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9009 beginline(BL_WHITE);
9010 else
9011 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 break;
9013
9014 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9015#ifdef FEAT_VIRTUALEDIT
9016 /* increment coladd when in virtual space, increment the
9017 * column otherwise, also to append after an unprintable char */
9018 if (virtual_active()
9019 && (curwin->w_cursor.coladd > 0
9020 || *ml_get_cursor() == NUL
9021 || *ml_get_cursor() == TAB))
9022 curwin->w_cursor.coladd++;
9023 else
9024#endif
9025 if (*ml_get_cursor() != NUL)
9026 inc_cursor();
9027 break;
9028 }
9029
9030#ifdef FEAT_VIRTUALEDIT
9031 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9032 {
9033 int save_State = State;
9034
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009035 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 * character past the end of the line */
9037 State = INSERT;
9038 coladvance(getviscol());
9039 State = save_State;
9040 }
9041#endif
9042
9043 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9044 }
9045}
9046
9047/*
9048 * Invoke edit() and take care of "restart_edit" and the return value.
9049 */
9050 static void
9051invoke_edit(cap, repl, cmd, startln)
9052 cmdarg_T *cap;
9053 int repl; /* "r" or "gr" command */
9054 int cmd;
9055 int startln;
9056{
9057 int restart_edit_save = 0;
9058
9059 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9060 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9061 * it. */
9062 if (repl || !stuff_empty())
9063 restart_edit_save = restart_edit;
9064 else
9065 restart_edit_save = 0;
9066
9067 /* Always reset "restart_edit", this is not a restarted edit. */
9068 restart_edit = 0;
9069
9070 if (edit(cmd, startln, cap->count1))
9071 cap->retval |= CA_COMMAND_BUSY;
9072
9073 if (restart_edit == 0)
9074 restart_edit = restart_edit_save;
9075}
9076
9077#ifdef FEAT_TEXTOBJ
9078/*
9079 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9080 */
9081 static void
9082nv_object(cap)
9083 cmdarg_T *cap;
9084{
9085 int flag;
9086 int include;
9087 char_u *mps_save;
9088
9089 if (cap->cmdchar == 'i')
9090 include = FALSE; /* "ix" = inner object: exclude white space */
9091 else
9092 include = TRUE; /* "ax" = an object: include white space */
9093
9094 /* Make sure (), [], {} and <> are in 'matchpairs' */
9095 mps_save = curbuf->b_p_mps;
9096 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9097
9098 switch (cap->nchar)
9099 {
9100 case 'w': /* "aw" = a word */
9101 flag = current_word(cap->oap, cap->count1, include, FALSE);
9102 break;
9103 case 'W': /* "aW" = a WORD */
9104 flag = current_word(cap->oap, cap->count1, include, TRUE);
9105 break;
9106 case 'b': /* "ab" = a braces block */
9107 case '(':
9108 case ')':
9109 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9110 break;
9111 case 'B': /* "aB" = a Brackets block */
9112 case '{':
9113 case '}':
9114 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9115 break;
9116 case '[': /* "a[" = a [] block */
9117 case ']':
9118 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9119 break;
9120 case '<': /* "a<" = a <> block */
9121 case '>':
9122 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9123 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009124 case 't': /* "at" = a tag block (xml and html) */
9125 flag = current_tagblock(cap->oap, cap->count1, include);
9126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 case 'p': /* "ap" = a paragraph */
9128 flag = current_par(cap->oap, cap->count1, include, 'p');
9129 break;
9130 case 's': /* "as" = a sentence */
9131 flag = current_sent(cap->oap, cap->count1, include);
9132 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009133 case '"': /* "a"" = a double quoted string */
9134 case '\'': /* "a'" = a single quoted string */
9135 case '`': /* "a`" = a backtick quoted string */
9136 flag = current_quote(cap->oap, cap->count1, include,
9137 cap->nchar);
9138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139#if 0 /* TODO */
9140 case 'S': /* "aS" = a section */
9141 case 'f': /* "af" = a filename */
9142 case 'u': /* "au" = a URL */
9143#endif
9144 default:
9145 flag = FAIL;
9146 break;
9147 }
9148
9149 curbuf->b_p_mps = mps_save;
9150 if (flag == FAIL)
9151 clearopbeep(cap->oap);
9152 adjust_cursor_col();
9153 curwin->w_set_curswant = TRUE;
9154}
9155#endif
9156
9157/*
9158 * "q" command: Start/stop recording.
9159 * "q:", "q/", "q?": edit command-line in command-line window.
9160 */
9161 static void
9162nv_record(cap)
9163 cmdarg_T *cap;
9164{
9165 if (cap->oap->op_type == OP_FORMAT)
9166 {
9167 /* "gqq" is the same as "gqgq": format line */
9168 cap->cmdchar = 'g';
9169 cap->nchar = 'q';
9170 nv_operator(cap);
9171 }
9172 else if (!checkclearop(cap->oap))
9173 {
9174#ifdef FEAT_CMDWIN
9175 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9176 {
9177 stuffcharReadbuff(cap->nchar);
9178 stuffcharReadbuff(K_CMDWIN);
9179 }
9180 else
9181#endif
9182 /* (stop) recording into a named register, unless executing a
9183 * register */
9184 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9185 clearopbeep(cap->oap);
9186 }
9187}
9188
9189/*
9190 * Handle the "@r" command.
9191 */
9192 static void
9193nv_at(cap)
9194 cmdarg_T *cap;
9195{
9196 if (checkclearop(cap->oap))
9197 return;
9198#ifdef FEAT_EVAL
9199 if (cap->nchar == '=')
9200 {
9201 if (get_expr_register() == NUL)
9202 return;
9203 }
9204#endif
9205 while (cap->count1-- && !got_int)
9206 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009207 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
9209 clearopbeep(cap->oap);
9210 break;
9211 }
9212 line_breakcheck();
9213 }
9214}
9215
9216/*
9217 * Handle the CTRL-U and CTRL-D commands.
9218 */
9219 static void
9220nv_halfpage(cap)
9221 cmdarg_T *cap;
9222{
9223 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9224 || (cap->cmdchar == Ctrl_D
9225 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9226 clearopbeep(cap->oap);
9227 else if (!checkclearop(cap->oap))
9228 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9229}
9230
9231/*
9232 * Handle "J" or "gJ" command.
9233 */
9234 static void
9235nv_join(cap)
9236 cmdarg_T *cap;
9237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 if (VIsual_active) /* join the visual lines */
9239 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009240 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 {
9242 if (cap->count0 <= 1)
9243 cap->count0 = 2; /* default for join is two lines! */
9244 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9245 curbuf->b_ml.ml_line_count)
9246 clearopbeep(cap->oap); /* beyond last line */
9247 else
9248 {
9249 prep_redo(cap->oap->regname, cap->count0,
9250 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009251 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 }
9253 }
9254}
9255
9256/*
9257 * "P", "gP", "p" and "gp" commands.
9258 */
9259 static void
9260nv_put(cap)
9261 cmdarg_T *cap;
9262{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 int regname = 0;
9264 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009265 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009266 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 int dir;
9268 int flags = 0;
9269
9270 if (cap->oap->op_type != OP_NOP)
9271 {
9272#ifdef FEAT_DIFF
9273 /* "dp" is ":diffput" */
9274 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9275 {
9276 clearop(cap->oap);
9277 nv_diffgetput(TRUE);
9278 }
9279 else
9280#endif
9281 clearopbeep(cap->oap);
9282 }
9283 else
9284 {
9285 dir = (cap->cmdchar == 'P'
9286 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9287 ? BACKWARD : FORWARD;
9288 prep_redo_cmd(cap);
9289 if (cap->cmdchar == 'g')
9290 flags |= PUT_CURSEND;
9291
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 if (VIsual_active)
9293 {
9294 /* Putting in Visual mode: The put text replaces the selected
9295 * text. First delete the selected text, then put the new text.
9296 * Need to save and restore the registers that the delete
9297 * overwrites if the old contents is being put.
9298 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009299 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009301#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009303#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009304 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009305 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009306#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309
9310 )
9311 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009312 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 * put, save it first. */
9314 reg1 = get_register(regname, TRUE);
9315 }
9316
9317 /* Now delete the selected text. */
9318 cap->cmdchar = 'd';
9319 cap->nchar = NUL;
9320 cap->oap->regname = NUL;
9321 nv_operator(cap);
9322 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009323 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324
9325 /* delete PUT_LINE_BACKWARD; */
9326 cap->oap->regname = regname;
9327
9328 if (reg1 != NULL)
9329 {
9330 /* Delete probably changed the register we want to put, save
9331 * it first. Then put back what was there before the delete. */
9332 reg2 = get_register(regname, FALSE);
9333 put_register(regname, reg1);
9334 }
9335
9336 /* When deleted a linewise Visual area, put the register as
9337 * lines to avoid it joined with the next line. When deletion was
9338 * characterwise, split a line when putting lines. */
9339 if (VIsual_mode == 'V')
9340 flags |= PUT_LINE;
9341 else if (VIsual_mode == 'v')
9342 flags |= PUT_LINE_SPLIT;
9343 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9344 flags |= PUT_LINE_FORWARD;
9345 dir = BACKWARD;
9346 if ((VIsual_mode != 'V'
9347 && curwin->w_cursor.col < curbuf->b_op_start.col)
9348 || (VIsual_mode == 'V'
9349 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9350 /* cursor is at the end of the line or end of file, put
9351 * forward. */
9352 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009353 /* May have been reset in do_put(). */
9354 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 do_put(cap->oap->regname, dir, cap->count1, flags);
9357
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 /* If a register was saved, put it back now. */
9359 if (reg2 != NULL)
9360 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009361
9362 /* What to reselect with "gv"? Selecting the just put text seems to
9363 * be the most useful, since the original text was removed. */
9364 if (was_visual)
9365 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009366 curbuf->b_visual.vi_start = curbuf->b_op_start;
9367 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009368 }
9369
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009370 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009371 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009372 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009373 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009374 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009375
9376 /* If the cursor was in that line, move it to the end of the last
9377 * line. */
9378 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9379 {
9380 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9381 coladvance((colnr_T)MAXCOL);
9382 }
9383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 auto_format(FALSE, TRUE);
9385 }
9386}
9387
9388/*
9389 * "o" and "O" commands.
9390 */
9391 static void
9392nv_open(cap)
9393 cmdarg_T *cap;
9394{
9395#ifdef FEAT_DIFF
9396 /* "do" is ":diffget" */
9397 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9398 {
9399 clearop(cap->oap);
9400 nv_diffgetput(FALSE);
9401 }
9402 else
9403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 if (VIsual_active) /* switch start and end of visual */
9405 v_swap_corners(cap->cmdchar);
9406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 n_opencmd(cap);
9408}
9409
9410#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 static void
9412nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009413 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 ProcessSniffRequests();
9416}
9417#endif
9418
9419#ifdef FEAT_NETBEANS_INTG
9420 static void
9421nv_nbcmd(cap)
9422 cmdarg_T *cap;
9423{
9424 netbeans_keycommand(cap->nchar);
9425}
9426#endif
9427
9428#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 static void
9430nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009431 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9434}
9435#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009436
9437#ifdef FEAT_AUTOCMD
9438/*
9439 * Trigger CursorHold event.
9440 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9441 * input buffer. "did_cursorhold" is set to avoid retriggering.
9442 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009443 static void
9444nv_cursorhold(cap)
9445 cmdarg_T *cap;
9446{
9447 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9448 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009449 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009450}
9451#endif