blob: 8384d6ffea14eb07455551b520783a86ca78bfd7 [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;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001383#ifdef FEAT_LINEBREAK
1384 int lbr_saved = curwin->w_p_lbr;
1385
1386 curwin->w_p_lbr = FALSE; /* avoid a problem with unwanted linebreaks in
1387 * block mode */
1388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /* The visual area is remembered for redo */
1391 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1392 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001393 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001395#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398
1399#if defined(FEAT_CLIPBOARD)
1400 /*
1401 * Yank the visual area into the GUI selection register before we operate
1402 * on it and lose it forever.
1403 * Don't do it if a specific register was specified, so that ""x"*P works.
1404 * This could call do_pending_operator() recursively, but that's OK
1405 * because gui_yank will be TRUE for the nested call.
1406 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001407 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 && oap->op_type != OP_NOP
1409 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 && VIsual_active
1411 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 && oap->regname == 0)
1413 clip_auto_select();
1414#endif
1415 old_cursor = curwin->w_cursor;
1416
1417 /*
1418 * If an operation is pending, handle it...
1419 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001420 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 oap->is_VIsual = VIsual_active;
1423 if (oap->motion_force == 'V')
1424 oap->motion_type = MLINE;
1425 else if (oap->motion_force == 'v')
1426 {
1427 /* If the motion was linewise, "inclusive" will not have been set.
1428 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1429 if (oap->motion_type == MLINE)
1430 oap->inclusive = FALSE;
1431 /* If the motion already was characterwise, toggle "inclusive" */
1432 else if (oap->motion_type == MCHAR)
1433 oap->inclusive = !oap->inclusive;
1434 oap->motion_type = MCHAR;
1435 }
1436 else if (oap->motion_force == Ctrl_V)
1437 {
1438 /* Change line- or characterwise motion into Visual block mode. */
1439 VIsual_active = TRUE;
1440 VIsual = oap->start;
1441 VIsual_mode = Ctrl_V;
1442 VIsual_select = FALSE;
1443 VIsual_reselect = FALSE;
1444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
Bram Moolenaar7afea822013-04-24 18:34:45 +02001446 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1447 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001449 && ((!VIsual_active || oap->motion_force)
1450 /* Also redo Operator-pending Visual mode mappings */
1451 || (VIsual_active && cap->cmdchar == ':'
1452 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001453 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#ifdef FEAT_FOLDING
1455 && oap->op_type != OP_FOLD
1456 && oap->op_type != OP_FOLDOPEN
1457 && oap->op_type != OP_FOLDOPENREC
1458 && oap->op_type != OP_FOLDCLOSE
1459 && oap->op_type != OP_FOLDCLOSEREC
1460 && oap->op_type != OP_FOLDDEL
1461 && oap->op_type != OP_FOLDDELREC
1462#endif
1463 )
1464 {
1465 prep_redo(oap->regname, cap->count0,
1466 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1467 oap->motion_force, cap->cmdchar, cap->nchar);
1468 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1469 {
1470 /*
1471 * If 'cpoptions' does not contain 'r', insert the search
1472 * pattern to really repeat the same command.
1473 */
1474 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001475 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 AppendToRedobuff(NL_STR);
1477 }
1478 else if (cap->cmdchar == ':')
1479 {
1480 /* do_cmdline() has stored the first typed line in
1481 * "repeat_cmdline". When several lines are typed repeating
1482 * won't be possible. */
1483 if (repeat_cmdline == NULL)
1484 ResetRedobuff();
1485 else
1486 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001487 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 AppendToRedobuff(NL_STR);
1489 vim_free(repeat_cmdline);
1490 repeat_cmdline = NULL;
1491 }
1492 }
1493 }
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (redo_VIsual_busy)
1496 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001497 /* Redo of an operation on a Visual area. Use the same size from
1498 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 oap->start = curwin->w_cursor;
1500 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1501 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1502 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1503 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001504 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001506 if (VIsual_mode == 'v')
1507 {
1508 if (redo_VIsual_line_count <= 1)
1509 {
1510 validate_virtcol();
1511 curwin->w_curswant =
1512 curwin->w_virtcol + redo_VIsual_vcol - 1;
1513 }
1514 else
1515 curwin->w_curswant = redo_VIsual_vcol;
1516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001518 {
1519 curwin->w_curswant = MAXCOL;
1520 }
1521 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523 cap->count0 = redo_VIsual_count;
1524 if (redo_VIsual_count != 0)
1525 cap->count1 = redo_VIsual_count;
1526 else
1527 cap->count1 = 1;
1528 }
1529 else if (VIsual_active)
1530 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001531 if (!gui_yank)
1532 {
1533 /* Save the current VIsual area for '< and '> marks, and "gv" */
1534 curbuf->b_visual.vi_start = VIsual;
1535 curbuf->b_visual.vi_end = curwin->w_cursor;
1536 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001537 if (VIsual_mode_orig != NUL)
1538 {
1539 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1540 VIsual_mode_orig = NUL;
1541 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001542 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001544 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 /* In Select mode, a linewise selection is operated upon like a
1549 * characterwise selection. */
1550 if (VIsual_select && VIsual_mode == 'V')
1551 {
1552 if (lt(VIsual, curwin->w_cursor))
1553 {
1554 VIsual.col = 0;
1555 curwin->w_cursor.col =
1556 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1557 }
1558 else
1559 {
1560 curwin->w_cursor.col = 0;
1561 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1562 }
1563 VIsual_mode = 'v';
1564 }
1565 /* If 'selection' is "exclusive", backup one character for
1566 * charwise selections. */
1567 else if (VIsual_mode == 'v')
1568 {
1569# ifdef FEAT_VIRTUALEDIT
1570 include_line_break =
1571# endif
1572 unadjust_for_sel();
1573 }
1574
1575 oap->start = VIsual;
1576 if (VIsual_mode == 'V')
1577 oap->start.col = 0;
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 /*
1581 * Set oap->start to the first position of the operated text, oap->end
1582 * to the end of the operated text. w_cursor is equal to oap->start.
1583 */
1584 if (lt(oap->start, curwin->w_cursor))
1585 {
1586#ifdef FEAT_FOLDING
1587 /* Include folded lines completely. */
1588 if (!VIsual_active)
1589 {
1590 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1591 oap->start.col = 0;
1592 if (hasFolding(curwin->w_cursor.lnum, NULL,
1593 &curwin->w_cursor.lnum))
1594 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1595 }
1596#endif
1597 oap->end = curwin->w_cursor;
1598 curwin->w_cursor = oap->start;
1599
1600 /* w_virtcol may have been updated; if the cursor goes back to its
1601 * previous position w_virtcol becomes invalid and isn't updated
1602 * automatically. */
1603 curwin->w_valid &= ~VALID_VIRTCOL;
1604 }
1605 else
1606 {
1607#ifdef FEAT_FOLDING
1608 /* Include folded lines completely. */
1609 if (!VIsual_active && oap->motion_type == MLINE)
1610 {
1611 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1612 NULL))
1613 curwin->w_cursor.col = 0;
1614 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1615 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1616 }
1617#endif
1618 oap->end = oap->start;
1619 oap->start = curwin->w_cursor;
1620 }
1621
1622 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1623
1624#ifdef FEAT_VIRTUALEDIT
1625 /* Set "virtual_op" before resetting VIsual_active. */
1626 virtual_op = virtual_active();
1627#endif
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (VIsual_active || redo_VIsual_busy)
1630 {
1631 if (VIsual_mode == Ctrl_V) /* block mode */
1632 {
1633 colnr_T start, end;
1634
1635 oap->block_mode = TRUE;
1636
1637 getvvcol(curwin, &(oap->start),
1638 &oap->start_vcol, NULL, &oap->end_vcol);
1639 if (!redo_VIsual_busy)
1640 {
1641 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1642
1643 if (start < oap->start_vcol)
1644 oap->start_vcol = start;
1645 if (end > oap->end_vcol)
1646 {
1647 if (*p_sel == 'e' && start >= 1
1648 && start - 1 >= oap->end_vcol)
1649 oap->end_vcol = start - 1;
1650 else
1651 oap->end_vcol = end;
1652 }
1653 }
1654
1655 /* if '$' was used, get oap->end_vcol from longest line */
1656 if (curwin->w_curswant == MAXCOL)
1657 {
1658 curwin->w_cursor.col = MAXCOL;
1659 oap->end_vcol = 0;
1660 for (curwin->w_cursor.lnum = oap->start.lnum;
1661 curwin->w_cursor.lnum <= oap->end.lnum;
1662 ++curwin->w_cursor.lnum)
1663 {
1664 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1665 if (end > oap->end_vcol)
1666 oap->end_vcol = end;
1667 }
1668 }
1669 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001670 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 /*
1672 * Correct oap->end.col and oap->start.col to be the
1673 * upper-left and lower-right corner of the block area.
1674 *
1675 * (Actually, this does convert column positions into character
1676 * positions)
1677 */
1678 curwin->w_cursor.lnum = oap->end.lnum;
1679 coladvance(oap->end_vcol);
1680 oap->end = curwin->w_cursor;
1681
1682 curwin->w_cursor = oap->start;
1683 coladvance(oap->start_vcol);
1684 oap->start = curwin->w_cursor;
1685 }
1686
1687 if (!redo_VIsual_busy && !gui_yank)
1688 {
1689 /*
1690 * Prepare to reselect and redo Visual: this is based on the
1691 * size of the Visual text
1692 */
1693 resel_VIsual_mode = VIsual_mode;
1694 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001695 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001697 {
1698 if (VIsual_mode != Ctrl_V)
1699 getvvcol(curwin, &(oap->end),
1700 NULL, NULL, &oap->end_vcol);
1701 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1702 {
1703 if (VIsual_mode != Ctrl_V)
1704 getvvcol(curwin, &(oap->start),
1705 &oap->start_vcol, NULL, NULL);
1706 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1707 }
1708 else
1709 resel_VIsual_vcol = oap->end_vcol;
1710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 resel_VIsual_line_count = oap->line_count;
1712 }
1713
1714 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1715 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1716 && oap->op_type != OP_COLON
1717#ifdef FEAT_FOLDING
1718 && oap->op_type != OP_FOLD
1719 && oap->op_type != OP_FOLDOPEN
1720 && oap->op_type != OP_FOLDOPENREC
1721 && oap->op_type != OP_FOLDCLOSE
1722 && oap->op_type != OP_FOLDCLOSEREC
1723 && oap->op_type != OP_FOLDDEL
1724 && oap->op_type != OP_FOLDDELREC
1725#endif
1726 && oap->motion_force == NUL
1727 )
1728 {
1729 /* Prepare for redoing. Only use the nchar field for "r",
1730 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001731 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1732 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001733 prep_redo(oap->regname, cap->count0,
1734 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1735 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001736 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001737 prep_redo(oap->regname, 0L, NUL, 'v',
1738 get_op_char(oap->op_type),
1739 get_extra_op_char(oap->op_type),
1740 oap->op_type == OP_REPLACE
1741 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (!redo_VIsual_busy)
1743 {
1744 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001745 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 redo_VIsual_line_count = resel_VIsual_line_count;
1747 redo_VIsual_count = cap->count0;
1748 }
1749 }
1750
1751 /*
1752 * oap->inclusive defaults to TRUE.
1753 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1754 * FALSE. This makes "d}P" and "v}dP" work the same.
1755 */
1756 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1757 oap->inclusive = TRUE;
1758 if (VIsual_mode == 'V')
1759 oap->motion_type = MLINE;
1760 else
1761 {
1762 oap->motion_type = MCHAR;
1763 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001764#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 )
1768 {
1769 oap->inclusive = FALSE;
1770 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001771 * that works on lines only. */
1772 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001774 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1775 {
1776 ++oap->end.lnum;
1777 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001778#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001779 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001780#endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001781 ++oap->line_count;
1782 }
1783 else
1784 {
1785 /* Cannot move below the last line, make the op
1786 * inclusive to tell the operation to include the
1787 * line break. */
1788 oap->inclusive = TRUE;
1789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 }
1791 }
1792 }
1793
1794 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 /*
1797 * Switch Visual off now, so screen updating does
1798 * not show inverted text when the screen is redrawn.
1799 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1800 * no screen redraw, so it is done here to remove the inverted
1801 * part.
1802 */
1803 if (!gui_yank)
1804 {
1805 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001806#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 setmouse();
1808 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001809#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001810 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 clear_cmdline = TRUE; /* unshow visual mode later */
1812#ifdef FEAT_CMDL_INFO
1813 else
1814 clear_showcmd();
1815#endif
1816 if ((oap->op_type == OP_YANK
1817 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001818 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 || oap->op_type == OP_FILTER)
1820 && oap->motion_force == NUL)
1821 redraw_curbuf_later(INVERTED);
1822 }
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825#ifdef FEAT_MBYTE
1826 /* Include the trailing byte of a multi-byte char. */
1827 if (has_mbyte && oap->inclusive)
1828 {
1829 int l;
1830
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001831 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (l > 1)
1833 oap->end.col += l - 1;
1834 }
1835#endif
1836 curwin->w_set_curswant = TRUE;
1837
1838 /*
1839 * oap->empty is set when start and end are the same. The inclusive
1840 * flag affects this too, unless yanking and the end is on a NUL.
1841 */
1842 oap->empty = (oap->motion_type == MCHAR
1843 && (!oap->inclusive
1844 || (oap->op_type == OP_YANK
1845 && gchar_pos(&oap->end) == NUL))
1846 && equalpos(oap->start, oap->end)
1847#ifdef FEAT_VIRTUALEDIT
1848 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1849#endif
1850 );
1851 /*
1852 * For delete, change and yank, it's an error to operate on an
1853 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1854 */
1855 empty_region_error = (oap->empty
1856 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 /* Force a redraw when operating on an empty Visual region, when
1859 * 'modifiable is off or creating a fold. */
1860 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001861#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 ))
1865 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866
1867 /*
1868 * If the end of an operator is in column one while oap->motion_type
1869 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1870 * character in the previous line. If op_start is on or before the
1871 * first non-blank in the line, the operator becomes linewise
1872 * (strange, but that's the way vi does it).
1873 */
1874 if ( oap->motion_type == MCHAR
1875 && oap->inclusive == FALSE
1876 && !(cap->retval & CA_NO_ADJ_OP_END)
1877 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001879 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 && oap->line_count > 1)
1881 {
1882 oap->end_adjusted = TRUE; /* remember that we did this */
1883 --oap->line_count;
1884 --oap->end.lnum;
1885 if (inindent(0))
1886 oap->motion_type = MLINE;
1887 else
1888 {
1889 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1890 if (oap->end.col)
1891 {
1892 --oap->end.col;
1893 oap->inclusive = TRUE;
1894 }
1895 }
1896 }
1897 else
1898 oap->end_adjusted = FALSE;
1899
1900 switch (oap->op_type)
1901 {
1902 case OP_LSHIFT:
1903 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001904 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 auto_format(FALSE, TRUE);
1906 break;
1907
1908 case OP_JOIN_NS:
1909 case OP_JOIN:
1910 if (oap->line_count < 2)
1911 oap->line_count = 2;
1912 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1913 curbuf->b_ml.ml_line_count)
1914 beep_flush();
1915 else
1916 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001917 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001918 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 auto_format(FALSE, TRUE);
1920 }
1921 break;
1922
1923 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001928 CancelRedo();
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 else
1931 {
1932 (void)op_delete(oap);
1933 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1934 u_save_cursor(); /* cursor line wasn't saved yet */
1935 auto_format(FALSE, TRUE);
1936 }
1937 break;
1938
1939 case OP_YANK:
1940 if (empty_region_error)
1941 {
1942 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001943 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001945 CancelRedo();
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else
1949 (void)op_yank(oap, FALSE, !gui_yank);
1950 check_cursor_col();
1951 break;
1952
1953 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001958 CancelRedo();
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961 {
1962 /* This is a new edit command, not a restart. Need to
1963 * remember it to make 'insertmode' work with mappings for
1964 * Visual mode. But do this only once and not when typed and
1965 * 'insertmode' isn't set. */
1966 if (p_im || !KeyTyped)
1967 restart_edit_save = restart_edit;
1968 else
1969 restart_edit_save = 0;
1970 restart_edit = 0;
1971 /* Reset finish_op now, don't want it set inside edit(). */
1972 finish_op = FALSE;
1973 if (op_change(oap)) /* will call edit() */
1974 cap->retval |= CA_COMMAND_BUSY;
1975 if (restart_edit == 0)
1976 restart_edit = restart_edit_save;
1977 }
1978 break;
1979
1980 case OP_FILTER:
1981 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1982 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1983 else
1984 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1985
1986 case OP_INDENT:
1987 case OP_COLON:
1988
1989#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1990 /*
1991 * If 'equalprg' is empty, do the indenting internally.
1992 */
1993 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1994 {
1995# ifdef FEAT_LISP
1996 if (curbuf->b_p_lisp)
1997 {
1998 op_reindent(oap, get_lisp_indent);
1999 break;
2000 }
2001# endif
2002# ifdef FEAT_CINDENT
2003 op_reindent(oap,
2004# ifdef FEAT_EVAL
2005 *curbuf->b_p_inde != NUL ? get_expr_indent :
2006# endif
2007 get_c_indent);
2008 break;
2009# endif
2010 }
2011#endif
2012
2013 op_colon(oap);
2014 break;
2015
2016 case OP_TILDE:
2017 case OP_UPPER:
2018 case OP_LOWER:
2019 case OP_ROT13:
2020 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002023 CancelRedo();
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 else
2026 op_tilde(oap);
2027 check_cursor_col();
2028 break;
2029
2030 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002031#if defined(FEAT_EVAL)
2032 if (*curbuf->b_p_fex != NUL)
2033 op_formatexpr(oap); /* use expression */
2034 else
2035#endif
2036 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 op_colon(oap); /* use external command */
2038 else
2039 op_format(oap, FALSE); /* use internal function */
2040 break;
2041
2042 case OP_FORMAT2:
2043 op_format(oap, TRUE); /* use internal function */
2044 break;
2045
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002046 case OP_FUNCTION:
2047 op_function(oap); /* call 'operatorfunc' */
2048 break;
2049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 case OP_INSERT:
2051 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#ifdef FEAT_VISUALEXTRA
2054 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002057 CancelRedo();
2058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 else
2060 {
2061 /* This is a new edit command, not a restart. Need to
2062 * remember it to make 'insertmode' work with mappings for
2063 * Visual mode. But do this only once. */
2064 restart_edit_save = restart_edit;
2065 restart_edit = 0;
2066
2067 op_insert(oap, cap->count1);
2068
2069 /* TODO: when inserting in several lines, should format all
2070 * the lines. */
2071 auto_format(FALSE, TRUE);
2072
2073 if (restart_edit == 0)
2074 restart_edit = restart_edit_save;
2075 }
2076#else
2077 vim_beep();
2078#endif
2079 break;
2080
2081 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#ifdef FEAT_VISUALEXTRA
2084 if (empty_region_error)
2085#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002088 CancelRedo();
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090#ifdef FEAT_VISUALEXTRA
2091 else
2092 op_replace(oap, cap->nchar);
2093#endif
2094 break;
2095
2096#ifdef FEAT_FOLDING
2097 case OP_FOLD:
2098 VIsual_reselect = FALSE; /* don't reselect now */
2099 foldCreate(oap->start.lnum, oap->end.lnum);
2100 break;
2101
2102 case OP_FOLDOPEN:
2103 case OP_FOLDOPENREC:
2104 case OP_FOLDCLOSE:
2105 case OP_FOLDCLOSEREC:
2106 VIsual_reselect = FALSE; /* don't reselect now */
2107 opFoldRange(oap->start.lnum, oap->end.lnum,
2108 oap->op_type == OP_FOLDOPEN
2109 || oap->op_type == OP_FOLDOPENREC,
2110 oap->op_type == OP_FOLDOPENREC
2111 || oap->op_type == OP_FOLDCLOSEREC,
2112 oap->is_VIsual);
2113 break;
2114
2115 case OP_FOLDDEL:
2116 case OP_FOLDDELREC:
2117 VIsual_reselect = FALSE; /* don't reselect now */
2118 deleteFold(oap->start.lnum, oap->end.lnum,
2119 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2120 break;
2121#endif
2122 default:
2123 clearopbeep(oap);
2124 }
2125#ifdef FEAT_VIRTUALEDIT
2126 virtual_op = MAYBE;
2127#endif
2128 if (!gui_yank)
2129 {
2130 /*
2131 * if 'sol' not set, go back to old column for some commands
2132 */
2133 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2134 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2135 || oap->op_type == OP_DELETE))
2136 coladvance(curwin->w_curswant = old_col);
2137 }
2138 else
2139 {
2140 curwin->w_cursor = old_cursor;
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 clearop(oap);
2144 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002145#ifdef FEAT_LINEBREAK
2146 curwin->w_p_lbr = lbr_saved;
2147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148}
2149
2150/*
2151 * Handle indent and format operators and visual mode ":".
2152 */
2153 static void
2154op_colon(oap)
2155 oparg_T *oap;
2156{
2157 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (oap->is_VIsual)
2159 stuffReadbuff((char_u *)"'<,'>");
2160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 /*
2163 * Make the range look nice, so it can be repeated.
2164 */
2165 if (oap->start.lnum == curwin->w_cursor.lnum)
2166 stuffcharReadbuff('.');
2167 else
2168 stuffnumReadbuff((long)oap->start.lnum);
2169 if (oap->end.lnum != oap->start.lnum)
2170 {
2171 stuffcharReadbuff(',');
2172 if (oap->end.lnum == curwin->w_cursor.lnum)
2173 stuffcharReadbuff('.');
2174 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2175 stuffcharReadbuff('$');
2176 else if (oap->start.lnum == curwin->w_cursor.lnum)
2177 {
2178 stuffReadbuff((char_u *)".+");
2179 stuffnumReadbuff((long)oap->line_count - 1);
2180 }
2181 else
2182 stuffnumReadbuff((long)oap->end.lnum);
2183 }
2184 }
2185 if (oap->op_type != OP_COLON)
2186 stuffReadbuff((char_u *)"!");
2187 if (oap->op_type == OP_INDENT)
2188 {
2189#ifndef FEAT_CINDENT
2190 if (*get_equalprg() == NUL)
2191 stuffReadbuff((char_u *)"indent");
2192 else
2193#endif
2194 stuffReadbuff(get_equalprg());
2195 stuffReadbuff((char_u *)"\n");
2196 }
2197 else if (oap->op_type == OP_FORMAT)
2198 {
2199 if (*p_fp == NUL)
2200 stuffReadbuff((char_u *)"fmt");
2201 else
2202 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002203 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205
2206 /*
2207 * do_cmdline() does the rest
2208 */
2209}
2210
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002211/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002212 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002213 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002214 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002215op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002216 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002217{
2218#ifdef FEAT_EVAL
2219 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002220# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002221 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002222# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002223
2224 if (*p_opfunc == NUL)
2225 EMSG(_("E774: 'operatorfunc' is empty"));
2226 else
2227 {
2228 /* Set '[ and '] marks to text to be operated on. */
2229 curbuf->b_op_start = oap->start;
2230 curbuf->b_op_end = oap->end;
2231 if (oap->motion_type != MLINE && !oap->inclusive)
2232 /* Exclude the end position. */
2233 decl(&curbuf->b_op_end);
2234
2235 if (oap->block_mode)
2236 argv[0] = (char_u *)"block";
2237 else if (oap->motion_type == MLINE)
2238 argv[0] = (char_u *)"line";
2239 else
2240 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002241
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002242# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002243 /* Reset virtual_op so that 'virtualedit' can be changed in the
2244 * function. */
2245 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002246# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002247
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002248 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002249
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002250# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002251 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002252# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002253 }
2254#else
2255 EMSG(_("E775: Eval feature not available"));
2256#endif
2257}
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#if defined(FEAT_MOUSE) || defined(PROTO)
2260/*
2261 * Do the appropriate action for the current mouse click in the current mode.
2262 * Not used for Command-line mode.
2263 *
2264 * Normal Mode:
2265 * event modi- position visual change action
2266 * fier cursor window
2267 * left press - yes end yes
2268 * left press C yes end yes "^]" (2)
2269 * left press S yes end yes "*" (2)
2270 * left drag - yes start if moved no
2271 * left relse - yes start if moved no
2272 * middle press - yes if not active no put register
2273 * middle press - yes if active no yank and put
2274 * right press - yes start or extend yes
2275 * right press S yes no change yes "#" (2)
2276 * right drag - yes extend no
2277 * right relse - yes extend no
2278 *
2279 * Insert or Replace Mode:
2280 * event modi- position visual change action
2281 * fier cursor window
2282 * left press - yes (cannot be active) yes
2283 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2284 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2285 * left drag - yes start or extend (1) no CTRL-O (1)
2286 * left relse - yes start or extend (1) no CTRL-O (1)
2287 * middle press - no (cannot be active) no put register
2288 * right press - yes start or extend yes CTRL-O
2289 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2290 *
2291 * (1) only if mouse pointer moved since press
2292 * (2) only if click is in same buffer
2293 *
2294 * Return TRUE if start_arrow() should be called for edit mode.
2295 */
2296 int
2297do_mouse(oap, c, dir, count, fixindent)
2298 oparg_T *oap; /* operator argument, can be NULL */
2299 int c; /* K_LEFTMOUSE, etc */
2300 int dir; /* Direction to 'put' if necessary */
2301 long count;
2302 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2303{
2304 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2305 static int got_click = FALSE; /* got a click some time back */
2306
2307 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2308 int is_click; /* If FALSE it's a drag or release event */
2309 int is_drag; /* If TRUE it's a drag event */
2310 int jump_flags = 0; /* flags for jump_to_mouse() */
2311 pos_T start_visual;
2312 int moved; /* Has cursor moved? */
2313 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002314#ifdef FEAT_WINDOWS
2315 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#ifdef FEAT_VERTSPLIT
2318 int in_sep_line; /* mouse in vertical separator line */
2319#endif
2320 int c1, c2;
2321#if defined(FEAT_FOLDING)
2322 pos_T save_cursor;
2323#endif
2324 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 static pos_T orig_cursor;
2326 colnr_T leftcol, rightcol;
2327 pos_T end_visual;
2328 int diff;
2329 int old_active = VIsual_active;
2330 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 int regname;
2332
2333#if defined(FEAT_FOLDING)
2334 save_cursor = curwin->w_cursor;
2335#endif
2336
2337 /*
2338 * When GUI is active, always recognize mouse events, otherwise:
2339 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2340 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2341 * - For command line and insert mode 'mouse' is checked before calling
2342 * do_mouse().
2343 */
2344 if (do_always)
2345 do_always = FALSE;
2346 else
2347#ifdef FEAT_GUI
2348 if (!gui.in_use)
2349#endif
2350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (VIsual_active)
2352 {
2353 if (!mouse_has(MOUSE_VISUAL))
2354 return FALSE;
2355 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002356 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 return FALSE;
2358 }
2359
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002360 for (;;)
2361 {
2362 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2363 if (is_drag)
2364 {
2365 /* If the next character is the same mouse event then use that
2366 * one. Speeds up dragging the status line. */
2367 if (vpeekc() != NUL)
2368 {
2369 int nc;
2370 int save_mouse_row = mouse_row;
2371 int save_mouse_col = mouse_col;
2372
2373 /* Need to get the character, peeking doesn't get the actual
2374 * one. */
2375 nc = safe_vgetc();
2376 if (c == nc)
2377 continue;
2378 vungetc(nc);
2379 mouse_row = save_mouse_row;
2380 mouse_col = save_mouse_col;
2381 }
2382 }
2383 break;
2384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386#ifdef FEAT_MOUSESHAPE
2387 /* May have stopped dragging the status or separator line. The pointer is
2388 * most likely still on the status or separator line. */
2389 if (!is_drag && drag_status_line)
2390 {
2391 drag_status_line = FALSE;
2392 update_mouseshape(SHAPE_IDX_STATUS);
2393 }
2394# ifdef FEAT_VERTSPLIT
2395 if (!is_drag && drag_sep_line)
2396 {
2397 drag_sep_line = FALSE;
2398 update_mouseshape(SHAPE_IDX_VSEP);
2399 }
2400# endif
2401#endif
2402
2403 /*
2404 * Ignore drag and release events if we didn't get a click.
2405 */
2406 if (is_click)
2407 got_click = TRUE;
2408 else
2409 {
2410 if (!got_click) /* didn't get click, ignore */
2411 return FALSE;
2412 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002415#ifdef FEAT_WINDOWS
2416 if (in_tab_line)
2417 {
2418 in_tab_line = FALSE;
2419 return FALSE;
2420 }
2421#endif
2422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /*
2426 * CTRL right mouse button does CTRL-T
2427 */
2428 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2429 {
2430 if (State & INSERT)
2431 stuffcharReadbuff(Ctrl_O);
2432 if (count > 1)
2433 stuffnumReadbuff(count);
2434 stuffcharReadbuff(Ctrl_T);
2435 got_click = FALSE; /* ignore drag&release now */
2436 return FALSE;
2437 }
2438
2439 /*
2440 * CTRL only works with left mouse button
2441 */
2442 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2443 return FALSE;
2444
2445 /*
2446 * When a modifier is down, ignore drag and release events, as well as
2447 * multiple clicks and the middle mouse button.
2448 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2449 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002450 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2451 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 && (!is_click
2453 || (mod_mask & MOD_MASK_MULTI_CLICK)
2454 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002455 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 && mouse_model_popup()
2457 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002458 && !((mod_mask & MOD_MASK_ALT)
2459 && !mouse_model_popup()
2460 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 )
2462 return FALSE;
2463
2464 /*
2465 * If the button press was used as the movement command for an operator
2466 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2467 * drag/release events.
2468 */
2469 if (!is_click && which_button == MOUSE_MIDDLE)
2470 return FALSE;
2471
2472 if (oap != NULL)
2473 regname = oap->regname;
2474 else
2475 regname = 0;
2476
2477 /*
2478 * Middle mouse button does a 'put' of the selected text
2479 */
2480 if (which_button == MOUSE_MIDDLE)
2481 {
2482 if (State == NORMAL)
2483 {
2484 /*
2485 * If an operator was pending, we don't know what the user wanted
2486 * to do. Go back to normal mode: Clear the operator and beep().
2487 */
2488 if (oap != NULL && oap->op_type != OP_NOP)
2489 {
2490 clearopbeep(oap);
2491 return FALSE;
2492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 /*
2495 * If visual was active, yank the highlighted text and put it
2496 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002497 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
2499 if (VIsual_active)
2500 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002501 if (VIsual_select)
2502 {
2503 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002504 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002505 }
2506 else
2507 {
2508 stuffcharReadbuff('y');
2509 stuffcharReadbuff(K_MIDDLEMOUSE);
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 do_always = TRUE; /* ignore 'mouse' setting next time */
2512 return FALSE;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /*
2515 * The rest is below jump_to_mouse()
2516 */
2517 }
2518
2519 else if ((State & INSERT) == 0)
2520 return FALSE;
2521
2522 /*
2523 * Middle click in insert mode doesn't move the mouse, just insert the
2524 * contents of a register. '.' register is special, can't insert that
2525 * with do_put().
2526 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2527 * happens for the GUI).
2528 */
2529 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2530 {
2531 if (regname == '.')
2532 insert_reg(regname, TRUE);
2533 else
2534 {
2535#ifdef FEAT_CLIPBOARD
2536 if (clip_star.available && regname == 0)
2537 regname = '*';
2538#endif
2539 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2540 insert_reg(regname, TRUE);
2541 else
2542 {
2543 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2544
2545 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2546 AppendCharToRedobuff(Ctrl_R);
2547 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2548 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2549 }
2550 }
2551 return FALSE;
2552 }
2553 }
2554
2555 /* When dragging or button-up stay in the same window. */
2556 if (!is_click)
2557 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2558
2559 start_visual.lnum = 0;
2560
Bram Moolenaarf740b292006-02-16 22:11:02 +00002561#ifdef FEAT_WINDOWS
2562 /* Check for clicking in the tab page line. */
2563 if (mouse_row == 0 && firstwin->w_winrow > 0)
2564 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002565 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002566 {
2567 if (in_tab_line)
2568 {
2569 c1 = TabPageIdxs[mouse_col];
2570 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2571 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002572 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002573 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 /* click in a tab selects that tab page */
2576 if (is_click
2577# ifdef FEAT_CMDWIN
2578 && cmdwin_type == 0
2579# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002580 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002581 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002582 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583 c1 = TabPageIdxs[mouse_col];
2584 if (c1 >= 0)
2585 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002586 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2587 {
2588 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002590 tabpage_new();
2591 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2592 }
2593 else
2594 {
2595 /* Go to specified tab page, or next one if not clicking
2596 * on a label. */
2597 goto_tabpage(c1);
2598
2599 /* It's like clicking on the status line of a window. */
2600 if (curwin != old_curwin)
2601 end_visual_mode();
2602 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002603 }
2604 else if (c1 < 0)
2605 {
2606 tabpage_T *tp;
2607
2608 /* Close the current or specified tab page. */
2609 if (c1 == -999)
2610 tp = curtab;
2611 else
2612 tp = find_tabpage(-c1);
2613 if (tp == curtab)
2614 {
2615 if (first_tabpage->tp_next != NULL)
2616 tabpage_close(FALSE);
2617 }
2618 else if (tp != NULL)
2619 tabpage_close_other(tp, FALSE);
2620 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002621 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002624 else if (is_drag && in_tab_line)
2625 {
2626 c1 = TabPageIdxs[mouse_col];
2627 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2628 return FALSE;
2629 }
2630
Bram Moolenaarf740b292006-02-16 22:11:02 +00002631#endif
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2635 * right button up -> pop-up menu
2636 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002637 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 */
2639 if (mouse_model_popup())
2640 {
2641 if (which_button == MOUSE_RIGHT
2642 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2643 {
2644 /*
2645 * NOTE: Ignore right button down and drag mouse events.
2646 * Windows only shows the popup menu on the button up event.
2647 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002648#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2649 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (!is_click)
2651 return FALSE;
2652#endif
2653#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2654 if (is_click || is_drag)
2655 return FALSE;
2656#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002657#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2659 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2660 if (gui.in_use)
2661 {
2662 jump_flags = 0;
2663 if (STRCMP(p_mousem, "popup_setpos") == 0)
2664 {
2665 /* First set the cursor position before showing the popup
2666 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (VIsual_active)
2668 {
2669 pos_T m_pos;
2670
2671 /*
2672 * set MOUSE_MAY_STOP_VIS if we are outside the
2673 * selection or the current window (might have false
2674 * negative here)
2675 */
2676 if (mouse_row < W_WINROW(curwin)
2677 || mouse_row
2678 > (W_WINROW(curwin) + curwin->w_height))
2679 jump_flags = MOUSE_MAY_STOP_VIS;
2680 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2681 jump_flags = MOUSE_MAY_STOP_VIS;
2682 else
2683 {
2684 if ((lt(curwin->w_cursor, VIsual)
2685 && (lt(m_pos, curwin->w_cursor)
2686 || lt(VIsual, m_pos)))
2687 || (lt(VIsual, curwin->w_cursor)
2688 && (lt(m_pos, VIsual)
2689 || lt(curwin->w_cursor, m_pos))))
2690 {
2691 jump_flags = MOUSE_MAY_STOP_VIS;
2692 }
2693 else if (VIsual_mode == Ctrl_V)
2694 {
2695 getvcols(curwin, &curwin->w_cursor, &VIsual,
2696 &leftcol, &rightcol);
2697 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2698 if (m_pos.col < leftcol || m_pos.col > rightcol)
2699 jump_flags = MOUSE_MAY_STOP_VIS;
2700 }
2701 }
2702 }
2703 else
2704 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706 if (jump_flags)
2707 {
2708 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002709 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 setcursor();
2711 out_flush(); /* Update before showing popup menu */
2712 }
2713# ifdef FEAT_MENU
2714 gui_show_popupmenu();
2715# endif
2716 return (jump_flags & CURSOR_MOVED) != 0;
2717 }
2718 else
2719 return FALSE;
2720#else
2721 return FALSE;
2722#endif
2723 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002724 if (which_button == MOUSE_LEFT
2725 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 which_button = MOUSE_RIGHT;
2728 mod_mask &= ~MOD_MASK_SHIFT;
2729 }
2730 }
2731
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 if ((State & (NORMAL | INSERT))
2733 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2734 {
2735 if (which_button == MOUSE_LEFT)
2736 {
2737 if (is_click)
2738 {
2739 /* stop Visual mode for a left click in a window, but not when
2740 * on a status line */
2741 if (VIsual_active)
2742 jump_flags |= MOUSE_MAY_STOP_VIS;
2743 }
2744 else if (mouse_has(MOUSE_VISUAL))
2745 jump_flags |= MOUSE_MAY_VIS;
2746 }
2747 else if (which_button == MOUSE_RIGHT)
2748 {
2749 if (is_click && VIsual_active)
2750 {
2751 /*
2752 * Remember the start and end of visual before moving the
2753 * cursor.
2754 */
2755 if (lt(curwin->w_cursor, VIsual))
2756 {
2757 start_visual = curwin->w_cursor;
2758 end_visual = VIsual;
2759 }
2760 else
2761 {
2762 start_visual = VIsual;
2763 end_visual = curwin->w_cursor;
2764 }
2765 }
2766 jump_flags |= MOUSE_FOCUS;
2767 if (mouse_has(MOUSE_VISUAL))
2768 jump_flags |= MOUSE_MAY_VIS;
2769 }
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 /*
2773 * If an operator is pending, ignore all drags and releases until the
2774 * next mouse click.
2775 */
2776 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2777 {
2778 got_click = FALSE;
2779 oap->motion_type = MCHAR;
2780 }
2781
2782 /* When releasing the button let jump_to_mouse() know. */
2783 if (!is_click && !is_drag)
2784 jump_flags |= MOUSE_RELEASED;
2785
2786 /*
2787 * JUMP!
2788 */
2789 jump_flags = jump_to_mouse(jump_flags,
2790 oap == NULL ? NULL : &(oap->inclusive), which_button);
2791 moved = (jump_flags & CURSOR_MOVED);
2792 in_status_line = (jump_flags & IN_STATUS_LINE);
2793#ifdef FEAT_VERTSPLIT
2794 in_sep_line = (jump_flags & IN_SEP_LINE);
2795#endif
2796
2797#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002798 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2800 {
2801 int key = KEY2TERMCAP1(c);
2802
2803 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2804 || key == (int)KE_RIGHTRELEASE)
2805 netbeans_button_release(which_button);
2806 }
2807#endif
2808
2809 /* When jumping to another window, clear a pending operator. That's a bit
2810 * friendlier than beeping and not jumping to that window. */
2811 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2812 clearop(oap);
2813
2814#ifdef FEAT_FOLDING
2815 if (mod_mask == 0
2816 && !is_drag
2817 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2818 && which_button == MOUSE_LEFT)
2819 {
2820 /* open or close a fold at this line */
2821 if (jump_flags & MOUSE_FOLD_OPEN)
2822 openFold(curwin->w_cursor.lnum, 1L);
2823 else
2824 closeFold(curwin->w_cursor.lnum, 1L);
2825 /* don't move the cursor if still in the same window */
2826 if (curwin == old_curwin)
2827 curwin->w_cursor = save_cursor;
2828 }
2829#endif
2830
2831#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2832 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2833 {
2834 clip_modeless(which_button, is_click, is_drag);
2835 return FALSE;
2836 }
2837#endif
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002840 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 if (VIsual_active && is_drag && p_so)
2842 {
2843 /* In the very first line, allow scrolling one line */
2844 if (mouse_row == 0)
2845 mouse_dragging = 2;
2846 else
2847 mouse_dragging = 1;
2848 }
2849
2850 /* When dragging the mouse above the window, scroll down. */
2851 if (is_drag && mouse_row < 0 && !in_status_line)
2852 {
2853 scroll_redraw(FALSE, 1L);
2854 mouse_row = 0;
2855 }
2856
2857 if (start_visual.lnum) /* right click in visual mode */
2858 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002859 /* When ALT is pressed make Visual mode blockwise. */
2860 if (mod_mask & MOD_MASK_ALT)
2861 VIsual_mode = Ctrl_V;
2862
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 /*
2864 * In Visual-block mode, divide the area in four, pick up the corner
2865 * that is in the quarter that the cursor is in.
2866 */
2867 if (VIsual_mode == Ctrl_V)
2868 {
2869 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2870 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2871 end_visual.col = leftcol;
2872 else
2873 end_visual.col = rightcol;
2874 if (curwin->w_cursor.lnum <
2875 (start_visual.lnum + end_visual.lnum) / 2)
2876 end_visual.lnum = end_visual.lnum;
2877 else
2878 end_visual.lnum = start_visual.lnum;
2879
2880 /* move VIsual to the right column */
2881 start_visual = curwin->w_cursor; /* save the cursor pos */
2882 curwin->w_cursor = end_visual;
2883 coladvance(end_visual.col);
2884 VIsual = curwin->w_cursor;
2885 curwin->w_cursor = start_visual; /* restore the cursor */
2886 }
2887 else
2888 {
2889 /*
2890 * If the click is before the start of visual, change the start.
2891 * If the click is after the end of visual, change the end. If
2892 * the click is inside the visual, change the closest side.
2893 */
2894 if (lt(curwin->w_cursor, start_visual))
2895 VIsual = end_visual;
2896 else if (lt(end_visual, curwin->w_cursor))
2897 VIsual = start_visual;
2898 else
2899 {
2900 /* In the same line, compare column number */
2901 if (end_visual.lnum == start_visual.lnum)
2902 {
2903 if (curwin->w_cursor.col - start_visual.col >
2904 end_visual.col - curwin->w_cursor.col)
2905 VIsual = start_visual;
2906 else
2907 VIsual = end_visual;
2908 }
2909
2910 /* In different lines, compare line number */
2911 else
2912 {
2913 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2914 (end_visual.lnum - curwin->w_cursor.lnum);
2915
2916 if (diff > 0) /* closest to end */
2917 VIsual = start_visual;
2918 else if (diff < 0) /* closest to start */
2919 VIsual = end_visual;
2920 else /* in the middle line */
2921 {
2922 if (curwin->w_cursor.col <
2923 (start_visual.col + end_visual.col) / 2)
2924 VIsual = end_visual;
2925 else
2926 VIsual = start_visual;
2927 }
2928 }
2929 }
2930 }
2931 }
2932 /*
2933 * If Visual mode started in insert mode, execute "CTRL-O"
2934 */
2935 else if ((State & INSERT) && VIsual_active)
2936 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
2938 /*
2939 * Middle mouse click: Put text before cursor.
2940 */
2941 if (which_button == MOUSE_MIDDLE)
2942 {
2943#ifdef FEAT_CLIPBOARD
2944 if (clip_star.available && regname == 0)
2945 regname = '*';
2946#endif
2947 if (yank_register_mline(regname))
2948 {
2949 if (mouse_past_bottom)
2950 dir = FORWARD;
2951 }
2952 else if (mouse_past_eol)
2953 dir = FORWARD;
2954
2955 if (fixindent)
2956 {
2957 c1 = (dir == BACKWARD) ? '[' : ']';
2958 c2 = 'p';
2959 }
2960 else
2961 {
2962 c1 = (dir == FORWARD) ? 'p' : 'P';
2963 c2 = NUL;
2964 }
2965 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2966
2967 /*
2968 * Remember where the paste started, so in edit() Insstart can be set
2969 * to this position
2970 */
2971 if (restart_edit != 0)
2972 where_paste_started = curwin->w_cursor;
2973 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2974 }
2975
2976#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2977 /*
2978 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2979 * error under the mouse pointer.
2980 */
2981 else if (((mod_mask & MOD_MASK_CTRL)
2982 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2983 && bt_quickfix(curbuf))
2984 {
2985 if (State & INSERT)
2986 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002987 if (curwin->w_llist_ref == NULL) /* quickfix window */
2988 stuffReadbuff((char_u *)":.cc\n");
2989 else /* location list window */
2990 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 got_click = FALSE; /* ignore drag&release now */
2992 }
2993#endif
2994
2995 /*
2996 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2997 * under the mouse pointer.
2998 */
2999 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3000 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3001 {
3002 if (State & INSERT)
3003 stuffcharReadbuff(Ctrl_O);
3004 stuffcharReadbuff(Ctrl_RSB);
3005 got_click = FALSE; /* ignore drag&release now */
3006 }
3007
3008 /*
3009 * Shift-Mouse click searches for the next occurrence of the word under
3010 * the mouse pointer
3011 */
3012 else if ((mod_mask & MOD_MASK_SHIFT))
3013 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003014 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 stuffcharReadbuff(Ctrl_O);
3016 if (which_button == MOUSE_LEFT)
3017 stuffcharReadbuff('*');
3018 else /* MOUSE_RIGHT */
3019 stuffcharReadbuff('#');
3020 }
3021
3022 /* Handle double clicks, unless on status line */
3023 else if (in_status_line)
3024 {
3025#ifdef FEAT_MOUSESHAPE
3026 if ((is_drag || is_click) && !drag_status_line)
3027 {
3028 drag_status_line = TRUE;
3029 update_mouseshape(-1);
3030 }
3031#endif
3032 }
3033#ifdef FEAT_VERTSPLIT
3034 else if (in_sep_line)
3035 {
3036# ifdef FEAT_MOUSESHAPE
3037 if ((is_drag || is_click) && !drag_sep_line)
3038 {
3039 drag_sep_line = TRUE;
3040 update_mouseshape(-1);
3041 }
3042# endif
3043 }
3044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3046 && mouse_has(MOUSE_VISUAL))
3047 {
3048 if (is_click || !VIsual_active)
3049 {
3050 if (VIsual_active)
3051 orig_cursor = VIsual;
3052 else
3053 {
3054 check_visual_highlight();
3055 VIsual = curwin->w_cursor;
3056 orig_cursor = VIsual;
3057 VIsual_active = TRUE;
3058 VIsual_reselect = TRUE;
3059 /* start Select mode if 'selectmode' contains "mouse" */
3060 may_start_select('o');
3061 setmouse();
3062 }
3063 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003064 {
3065 /* Double click with ALT pressed makes it blockwise. */
3066 if (mod_mask & MOD_MASK_ALT)
3067 VIsual_mode = Ctrl_V;
3068 else
3069 VIsual_mode = 'v';
3070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3072 VIsual_mode = 'V';
3073 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3074 VIsual_mode = Ctrl_V;
3075#ifdef FEAT_CLIPBOARD
3076 /* Make sure the clipboard gets updated. Needed because start and
3077 * end may still be the same, and the selection needs to be owned */
3078 clip_star.vmode = NUL;
3079#endif
3080 }
3081 /*
3082 * A double click selects a word or a block.
3083 */
3084 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3085 {
3086 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003087 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 if (is_click)
3090 {
3091 /* If the character under the cursor (skipping white space) is
3092 * not a word character, try finding a match and select a (),
3093 * {}, [], #if/#endif, etc. block. */
3094 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003095 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 inc(&end_visual);
3097 if (oap != NULL)
3098 oap->motion_type = MCHAR;
3099 if (oap != NULL
3100 && VIsual_mode == 'v'
3101 && !vim_iswordc(gchar_pos(&end_visual))
3102 && equalpos(curwin->w_cursor, VIsual)
3103 && (pos = findmatch(oap, NUL)) != NULL)
3104 {
3105 curwin->w_cursor = *pos;
3106 if (oap->motion_type == MLINE)
3107 VIsual_mode = 'V';
3108 else if (*p_sel == 'e')
3109 {
3110 if (lt(curwin->w_cursor, VIsual))
3111 ++VIsual.col;
3112 else
3113 ++curwin->w_cursor.col;
3114 }
3115 }
3116 }
3117
3118 if (pos == NULL && (is_click || is_drag))
3119 {
3120 /* When not found a match or when dragging: extend to include
3121 * a word. */
3122 if (lt(curwin->w_cursor, orig_cursor))
3123 {
3124 find_start_of_word(&curwin->w_cursor);
3125 find_end_of_word(&VIsual);
3126 }
3127 else
3128 {
3129 find_start_of_word(&VIsual);
3130 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3131#ifdef FEAT_MBYTE
3132 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003133 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#else
3135 ++curwin->w_cursor.col;
3136#endif
3137 find_end_of_word(&curwin->w_cursor);
3138 }
3139 }
3140 curwin->w_set_curswant = TRUE;
3141 }
3142 if (is_click)
3143 redraw_curbuf_later(INVERTED); /* update the inversion */
3144 }
3145 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003146 {
3147 if (mod_mask & MOD_MASK_ALT)
3148 VIsual_mode = Ctrl_V;
3149 else
3150 VIsual_mode = 'v';
3151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003154 if ((!VIsual_active && old_active && mode_displayed)
3155 || (VIsual_active && p_smd && msg_silent == 0
3156 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 return moved;
3160}
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162/*
3163 * Move "pos" back to the start of the word it's in.
3164 */
3165 static void
3166find_start_of_word(pos)
3167 pos_T *pos;
3168{
3169 char_u *line;
3170 int cclass;
3171 int col;
3172
3173 line = ml_get(pos->lnum);
3174 cclass = get_mouse_class(line + pos->col);
3175
3176 while (pos->col > 0)
3177 {
3178 col = pos->col - 1;
3179#ifdef FEAT_MBYTE
3180 col -= (*mb_head_off)(line, line + col);
3181#endif
3182 if (get_mouse_class(line + col) != cclass)
3183 break;
3184 pos->col = col;
3185 }
3186}
3187
3188/*
3189 * Move "pos" forward to the end of the word it's in.
3190 * When 'selection' is "exclusive", the position is just after the word.
3191 */
3192 static void
3193find_end_of_word(pos)
3194 pos_T *pos;
3195{
3196 char_u *line;
3197 int cclass;
3198 int col;
3199
3200 line = ml_get(pos->lnum);
3201 if (*p_sel == 'e' && pos->col > 0)
3202 {
3203 --pos->col;
3204#ifdef FEAT_MBYTE
3205 pos->col -= (*mb_head_off)(line, line + pos->col);
3206#endif
3207 }
3208 cclass = get_mouse_class(line + pos->col);
3209 while (line[pos->col] != NUL)
3210 {
3211#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003212 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213#else
3214 col = pos->col + 1;
3215#endif
3216 if (get_mouse_class(line + col) != cclass)
3217 {
3218 if (*p_sel == 'e')
3219 pos->col = col;
3220 break;
3221 }
3222 pos->col = col;
3223 }
3224}
3225
3226/*
3227 * Get class of a character for selection: same class means same word.
3228 * 0: blank
3229 * 1: punctuation groups
3230 * 2: normal word character
3231 * >2: multi-byte word character.
3232 */
3233 static int
3234get_mouse_class(p)
3235 char_u *p;
3236{
3237 int c;
3238
3239#ifdef FEAT_MBYTE
3240 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3241 return mb_get_class(p);
3242#endif
3243
3244 c = *p;
3245 if (c == ' ' || c == '\t')
3246 return 0;
3247
3248 if (vim_iswordc(c))
3249 return 2;
3250
3251 /*
3252 * There are a few special cases where we want certain combinations of
3253 * characters to be considered as a single word. These are things like
3254 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003255 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 */
3257 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3258 return 1;
3259 return c;
3260}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#endif /* FEAT_MOUSE */
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263/*
3264 * Check if highlighting for visual mode is possible, give a warning message
3265 * if not.
3266 */
3267 void
3268check_visual_highlight()
3269{
3270 static int did_check = FALSE;
3271
3272 if (full_screen)
3273 {
3274 if (!did_check && hl_attr(HLF_V) == 0)
3275 MSG(_("Warning: terminal cannot highlight"));
3276 did_check = TRUE;
3277 }
3278}
3279
3280/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003281 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 * This function should ALWAYS be called to end Visual mode, except from
3283 * do_pending_operator().
3284 */
3285 void
3286end_visual_mode()
3287{
3288#ifdef FEAT_CLIPBOARD
3289 /*
3290 * If we are using the clipboard, then remember what was selected in case
3291 * we need to paste it somewhere while we still own the selection.
3292 * Only do this when the clipboard is already owned. Don't want to grab
3293 * the selection when hitting ESC.
3294 */
3295 if (clip_star.available && clip_star.owned)
3296 clip_auto_select();
3297#endif
3298
3299 VIsual_active = FALSE;
3300#ifdef FEAT_MOUSE
3301 setmouse();
3302 mouse_dragging = 0;
3303#endif
3304
3305 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003306 curbuf->b_visual.vi_mode = VIsual_mode;
3307 curbuf->b_visual.vi_start = VIsual;
3308 curbuf->b_visual.vi_end = curwin->w_cursor;
3309 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#ifdef FEAT_EVAL
3311 curbuf->b_visual_mode_eval = VIsual_mode;
3312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#ifdef FEAT_VIRTUALEDIT
3314 if (!virtual_active())
3315 curwin->w_cursor.coladd = 0;
3316#endif
3317
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003318 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 clear_cmdline = TRUE; /* unshow visual mode later */
3320#ifdef FEAT_CMDL_INFO
3321 else
3322 clear_showcmd();
3323#endif
3324
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003325 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
3329 * Reset VIsual_active and VIsual_reselect.
3330 */
3331 void
3332reset_VIsual_and_resel()
3333{
3334 if (VIsual_active)
3335 {
3336 end_visual_mode();
3337 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3338 }
3339 VIsual_reselect = FALSE;
3340}
3341
3342/*
3343 * Reset VIsual_active and VIsual_reselect if it's set.
3344 */
3345 void
3346reset_VIsual()
3347{
3348 if (VIsual_active)
3349 {
3350 end_visual_mode();
3351 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3352 VIsual_reselect = FALSE;
3353 }
3354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003356#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3358
3359/*
3360 * Check for a balloon-eval special item to include when searching for an
3361 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3362 * Returns TRUE if the character at "*ptr" should be included.
3363 * "dir" is FORWARD or BACKWARD, the direction of searching.
3364 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3365 * "bnp" points to a counter for square brackets.
3366 */
3367 static int
3368find_is_eval_item(ptr, colp, bnp, dir)
3369 char_u *ptr;
3370 int *colp;
3371 int *bnp;
3372 int dir;
3373{
3374 /* Accept everything inside []. */
3375 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3376 ++*bnp;
3377 if (*bnp > 0)
3378 {
3379 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3380 --*bnp;
3381 return TRUE;
3382 }
3383
3384 /* skip over "s.var" */
3385 if (*ptr == '.')
3386 return TRUE;
3387
3388 /* two-character item: s->var */
3389 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3390 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3391 {
3392 *colp += dir;
3393 return TRUE;
3394 }
3395 return FALSE;
3396}
3397#endif
3398
3399/*
3400 * Find the identifier under or to the right of the cursor.
3401 * "find_type" can have one of three values:
3402 * FIND_IDENT: find an identifier (keyword)
3403 * FIND_STRING: find any non-white string
3404 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * There are three steps:
3408 * 1. Search forward for the start of an identifier/string. Doesn't move if
3409 * already on one.
3410 * 2. Search backward for the start of this identifier/string.
3411 * This doesn't match the real Vi but I like it a little better and it
3412 * shouldn't bother anyone.
3413 * 3. Search forward to the end of this identifier/string.
3414 * When FIND_IDENT isn't defined, we backup until a blank.
3415 *
3416 * Returns the length of the string, or zero if no string is found.
3417 * If a string is found, a pointer to the string is put in "*string". This
3418 * string is not always NUL terminated.
3419 */
3420 int
3421find_ident_under_cursor(string, find_type)
3422 char_u **string;
3423 int find_type;
3424{
3425 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3426 curwin->w_cursor.col, string, find_type);
3427}
3428
3429/*
3430 * Like find_ident_under_cursor(), but for any window and any position.
3431 * However: Uses 'iskeyword' from the current window!.
3432 */
3433 int
3434find_ident_at_pos(wp, lnum, startcol, string, find_type)
3435 win_T *wp;
3436 linenr_T lnum;
3437 colnr_T startcol;
3438 char_u **string;
3439 int find_type;
3440{
3441 char_u *ptr;
3442 int col = 0; /* init to shut up GCC */
3443 int i;
3444#ifdef FEAT_MBYTE
3445 int this_class = 0;
3446 int prev_class;
3447 int prevcol;
3448#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003449#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 int bn = 0; /* bracket nesting */
3451#endif
3452
3453 /*
3454 * if i == 0: try to find an identifier
3455 * if i == 1: try to find any non-white string
3456 */
3457 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3458 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3459 {
3460 /*
3461 * 1. skip to start of identifier/string
3462 */
3463 col = startcol;
3464#ifdef FEAT_MBYTE
3465 if (has_mbyte)
3466 {
3467 while (ptr[col] != NUL)
3468 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 /* Stop at a ']' to evaluate "a[x]". */
3471 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3472 break;
3473# endif
3474 this_class = mb_get_class(ptr + col);
3475 if (this_class != 0 && (i == 1 || this_class != 1))
3476 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003477 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 }
3479 }
3480 else
3481#endif
3482 while (ptr[col] != NUL
3483 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3486# endif
3487 )
3488 ++col;
3489
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* When starting on a ']' count it, so that we include the '['. */
3492 bn = ptr[col] == ']';
3493#endif
3494
3495 /*
3496 * 2. Back up to start of identifier/string.
3497 */
3498#ifdef FEAT_MBYTE
3499 if (has_mbyte)
3500 {
3501 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3504 this_class = mb_get_class((char_u *)"a");
3505 else
3506# endif
3507 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003508 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
3510 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3511 prev_class = mb_get_class(ptr + prevcol);
3512 if (this_class != prev_class
3513 && (i == 0
3514 || prev_class == 0
3515 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 && (!(find_type & FIND_EVAL)
3518 || prevcol == 0
3519 || !find_is_eval_item(ptr + prevcol, &prevcol,
3520 &bn, BACKWARD))
3521# endif
3522 )
3523 break;
3524 col = prevcol;
3525 }
3526
3527 /* If we don't want just any old string, or we've found an
3528 * identifier, stop searching. */
3529 if (this_class > 2)
3530 this_class = 2;
3531 if (!(find_type & FIND_STRING) || this_class == 2)
3532 break;
3533 }
3534 else
3535#endif
3536 {
3537 while (col > 0
3538 && ((i == 0
3539 ? vim_iswordc(ptr[col - 1])
3540 : (!vim_iswhite(ptr[col - 1])
3541 && (!(find_type & FIND_IDENT)
3542 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 || ((find_type & FIND_EVAL)
3545 && col > 1
3546 && find_is_eval_item(ptr + col - 1, &col,
3547 &bn, BACKWARD))
3548#endif
3549 ))
3550 --col;
3551
3552 /* If we don't want just any old string, or we've found an
3553 * identifier, stop searching. */
3554 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3555 break;
3556 }
3557 }
3558
3559 if (ptr[col] == NUL || (i == 0 && (
3560#ifdef FEAT_MBYTE
3561 has_mbyte ? this_class != 2 :
3562#endif
3563 !vim_iswordc(ptr[col]))))
3564 {
3565 /*
3566 * didn't find an identifier or string
3567 */
3568 if (find_type & FIND_STRING)
3569 EMSG(_("E348: No string under cursor"));
3570 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003571 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 return 0;
3573 }
3574 ptr += col;
3575 *string = ptr;
3576
3577 /*
3578 * 3. Find the end if the identifier/string.
3579 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 bn = 0;
3582 startcol -= col;
3583#endif
3584 col = 0;
3585#ifdef FEAT_MBYTE
3586 if (has_mbyte)
3587 {
3588 /* Search for point of changing multibyte character class. */
3589 this_class = mb_get_class(ptr);
3590 while (ptr[col] != NUL
3591 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3592 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 || ((find_type & FIND_EVAL)
3595 && col <= (int)startcol
3596 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3597# endif
3598 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003599 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601 else
3602#endif
3603 while ((i == 0 ? vim_iswordc(ptr[col])
3604 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003605# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 || ((find_type & FIND_EVAL)
3607 && col <= (int)startcol
3608 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3609# endif
3610 )
3611 {
3612 ++col;
3613 }
3614
3615 return col;
3616}
3617
3618/*
3619 * Prepare for redo of a normal command.
3620 */
3621 static void
3622prep_redo_cmd(cap)
3623 cmdarg_T *cap;
3624{
3625 prep_redo(cap->oap->regname, cap->count0,
3626 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3627}
3628
3629/*
3630 * Prepare for redo of any command.
3631 * Note that only the last argument can be a multi-byte char.
3632 */
3633 static void
3634prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3635 int regname;
3636 long num;
3637 int cmd1;
3638 int cmd2;
3639 int cmd3;
3640 int cmd4;
3641 int cmd5;
3642{
3643 ResetRedobuff();
3644 if (regname != 0) /* yank from specified buffer */
3645 {
3646 AppendCharToRedobuff('"');
3647 AppendCharToRedobuff(regname);
3648 }
3649 if (num)
3650 AppendNumberToRedobuff(num);
3651
3652 if (cmd1 != NUL)
3653 AppendCharToRedobuff(cmd1);
3654 if (cmd2 != NUL)
3655 AppendCharToRedobuff(cmd2);
3656 if (cmd3 != NUL)
3657 AppendCharToRedobuff(cmd3);
3658 if (cmd4 != NUL)
3659 AppendCharToRedobuff(cmd4);
3660 if (cmd5 != NUL)
3661 AppendCharToRedobuff(cmd5);
3662}
3663
3664/*
3665 * check for operator active and clear it
3666 *
3667 * return TRUE if operator was active
3668 */
3669 static int
3670checkclearop(oap)
3671 oparg_T *oap;
3672{
3673 if (oap->op_type == OP_NOP)
3674 return FALSE;
3675 clearopbeep(oap);
3676 return TRUE;
3677}
3678
3679/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003680 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003682 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
3684 static int
3685checkclearopq(oap)
3686 oparg_T *oap;
3687{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003688 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 return FALSE;
3690 clearopbeep(oap);
3691 return TRUE;
3692}
3693
3694 static void
3695clearop(oap)
3696 oparg_T *oap;
3697{
3698 oap->op_type = OP_NOP;
3699 oap->regname = 0;
3700 oap->motion_force = NUL;
3701 oap->use_reg_one = FALSE;
3702}
3703
3704 static void
3705clearopbeep(oap)
3706 oparg_T *oap;
3707{
3708 clearop(oap);
3709 beep_flush();
3710}
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712/*
3713 * Remove the shift modifier from a special key.
3714 */
3715 static void
3716unshift_special(cap)
3717 cmdarg_T *cap;
3718{
3719 switch (cap->cmdchar)
3720 {
3721 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3722 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3723 case K_S_UP: cap->cmdchar = K_UP; break;
3724 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3725 case K_S_HOME: cap->cmdchar = K_HOME; break;
3726 case K_S_END: cap->cmdchar = K_END; break;
3727 }
3728 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3732/*
3733 * Routines for displaying a partly typed command
3734 */
3735
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003736#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737static char_u showcmd_buf[SHOWCMD_BUFLEN];
3738static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3739static int showcmd_is_clear = TRUE;
3740static int showcmd_visual = FALSE;
3741
3742static void display_showcmd __ARGS((void));
3743
3744 void
3745clear_showcmd()
3746{
3747 if (!p_sc)
3748 return;
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (VIsual_active && !char_avail())
3751 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003752 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 long lines;
3754 colnr_T leftcol, rightcol;
3755 linenr_T top, bot;
3756
3757 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003758 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 top = VIsual.lnum;
3761 bot = curwin->w_cursor.lnum;
3762 }
3763 else
3764 {
3765 top = curwin->w_cursor.lnum;
3766 bot = VIsual.lnum;
3767 }
3768# ifdef FEAT_FOLDING
3769 /* Include closed folds as a whole. */
3770 hasFolding(top, &top, NULL);
3771 hasFolding(bot, NULL, &bot);
3772# endif
3773 lines = bot - top + 1;
3774
3775 if (VIsual_mode == Ctrl_V)
3776 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003777# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003778 char_u *saved_sbr = p_sbr;
3779
3780 /* Make 'sbr' empty for a moment to get the correct size. */
3781 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003784# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003785 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3788 (long)(rightcol - leftcol + 1));
3789 }
3790 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3791 sprintf((char *)showcmd_buf, "%ld", lines);
3792 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003793 {
3794 char_u *s, *e;
3795 int l;
3796 int bytes = 0;
3797 int chars = 0;
3798
3799 if (cursor_bot)
3800 {
3801 s = ml_get_pos(&VIsual);
3802 e = ml_get_cursor();
3803 }
3804 else
3805 {
3806 s = ml_get_cursor();
3807 e = ml_get_pos(&VIsual);
3808 }
3809 while ((*p_sel != 'e') ? s <= e : s < e)
3810 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003811# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003812 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003813# else
3814 l = (*s == NUL) ? 0 : 1;
3815# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003816 if (l == 0)
3817 {
3818 ++bytes;
3819 ++chars;
3820 break; /* end of line */
3821 }
3822 bytes += l;
3823 ++chars;
3824 s += l;
3825 }
3826 if (bytes == chars)
3827 sprintf((char *)showcmd_buf, "%d", chars);
3828 else
3829 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3832 showcmd_visual = TRUE;
3833 }
3834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
3836 showcmd_buf[0] = NUL;
3837 showcmd_visual = FALSE;
3838
3839 /* Don't actually display something if there is nothing to clear. */
3840 if (showcmd_is_clear)
3841 return;
3842 }
3843
3844 display_showcmd();
3845}
3846
3847/*
3848 * Add 'c' to string of shown command chars.
3849 * Return TRUE if output has been written (and setcursor() has been called).
3850 */
3851 int
3852add_to_showcmd(c)
3853 int c;
3854{
3855 char_u *p;
3856 int old_len;
3857 int extra_len;
3858 int overflow;
3859#if defined(FEAT_MOUSE)
3860 int i;
3861 static int ignore[] =
3862 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003863# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3865 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 K_IGNORE,
3868 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3869 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3870 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003871 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003873 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 0
3875 };
3876#endif
3877
Bram Moolenaar09df3122006-01-23 22:23:09 +00003878 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return FALSE;
3880
3881 if (showcmd_visual)
3882 {
3883 showcmd_buf[0] = NUL;
3884 showcmd_visual = FALSE;
3885 }
3886
3887#if defined(FEAT_MOUSE)
3888 /* Ignore keys that are scrollbar updates and mouse clicks */
3889 if (IS_SPECIAL(c))
3890 for (i = 0; ignore[i] != 0; ++i)
3891 if (ignore[i] == c)
3892 return FALSE;
3893#endif
3894
3895 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003896 if (*p == ' ')
3897 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 old_len = (int)STRLEN(showcmd_buf);
3899 extra_len = (int)STRLEN(p);
3900 overflow = old_len + extra_len - SHOWCMD_COLS;
3901 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003902 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3903 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 STRCAT(showcmd_buf, p);
3905
3906 if (char_avail())
3907 return FALSE;
3908
3909 display_showcmd();
3910
3911 return TRUE;
3912}
3913
3914 void
3915add_to_showcmd_c(c)
3916 int c;
3917{
3918 if (!add_to_showcmd(c))
3919 setcursor();
3920}
3921
3922/*
3923 * Delete 'len' characters from the end of the shown command.
3924 */
3925 static void
3926del_from_showcmd(len)
3927 int len;
3928{
3929 int old_len;
3930
3931 if (!p_sc)
3932 return;
3933
3934 old_len = (int)STRLEN(showcmd_buf);
3935 if (len > old_len)
3936 len = old_len;
3937 showcmd_buf[old_len - len] = NUL;
3938
3939 if (!char_avail())
3940 display_showcmd();
3941}
3942
3943/*
3944 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3945 * something and there is a partial mapping.
3946 */
3947 void
3948push_showcmd()
3949{
3950 if (p_sc)
3951 STRCPY(old_showcmd_buf, showcmd_buf);
3952}
3953
3954 void
3955pop_showcmd()
3956{
3957 if (!p_sc)
3958 return;
3959
3960 STRCPY(showcmd_buf, old_showcmd_buf);
3961
3962 display_showcmd();
3963}
3964
3965 static void
3966display_showcmd()
3967{
3968 int len;
3969
3970 cursor_off();
3971
3972 len = (int)STRLEN(showcmd_buf);
3973 if (len == 0)
3974 showcmd_is_clear = TRUE;
3975 else
3976 {
3977 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3978 showcmd_is_clear = FALSE;
3979 }
3980
3981 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003982 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3983 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 */
3985 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3986
3987 setcursor(); /* put cursor back where it belongs */
3988}
3989#endif
3990
3991#ifdef FEAT_SCROLLBIND
3992/*
3993 * When "check" is FALSE, prepare for commands that scroll the window.
3994 * When "check" is TRUE, take care of scroll-binding after the window has
3995 * scrolled. Called from normal_cmd() and edit().
3996 */
3997 void
3998do_check_scrollbind(check)
3999 int check;
4000{
4001 static win_T *old_curwin = NULL;
4002 static linenr_T old_topline = 0;
4003#ifdef FEAT_DIFF
4004 static int old_topfill = 0;
4005#endif
4006 static buf_T *old_buf = NULL;
4007 static colnr_T old_leftcol = 0;
4008
4009 if (check && curwin->w_p_scb)
4010 {
4011 /* If a ":syncbind" command was just used, don't scroll, only reset
4012 * the values. */
4013 if (did_syncbind)
4014 did_syncbind = FALSE;
4015 else if (curwin == old_curwin)
4016 {
4017 /*
4018 * Synchronize other windows, as necessary according to
4019 * 'scrollbind'. Don't do this after an ":edit" command, except
4020 * when 'diff' is set.
4021 */
4022 if ((curwin->w_buffer == old_buf
4023#ifdef FEAT_DIFF
4024 || curwin->w_p_diff
4025#endif
4026 )
4027 && (curwin->w_topline != old_topline
4028#ifdef FEAT_DIFF
4029 || curwin->w_topfill != old_topfill
4030#endif
4031 || curwin->w_leftcol != old_leftcol))
4032 {
4033 check_scrollbind(curwin->w_topline - old_topline,
4034 (long)(curwin->w_leftcol - old_leftcol));
4035 }
4036 }
4037 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4038 {
4039 /*
4040 * When switching between windows, make sure that the relative
4041 * vertical offset is valid for the new window. The relative
4042 * offset is invalid whenever another 'scrollbind' window has
4043 * scrolled to a point that would force the current window to
4044 * scroll past the beginning or end of its buffer. When the
4045 * resync is performed, some of the other 'scrollbind' windows may
4046 * need to jump so that the current window's relative position is
4047 * visible on-screen.
4048 */
4049 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4050 }
4051 curwin->w_scbind_pos = curwin->w_topline;
4052 }
4053
4054 old_curwin = curwin;
4055 old_topline = curwin->w_topline;
4056#ifdef FEAT_DIFF
4057 old_topfill = curwin->w_topfill;
4058#endif
4059 old_buf = curwin->w_buffer;
4060 old_leftcol = curwin->w_leftcol;
4061}
4062
4063/*
4064 * Synchronize any windows that have "scrollbind" set, based on the
4065 * number of rows by which the current window has changed
4066 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4067 */
4068 void
4069check_scrollbind(topline_diff, leftcol_diff)
4070 linenr_T topline_diff;
4071 long leftcol_diff;
4072{
4073 int want_ver;
4074 int want_hor;
4075 win_T *old_curwin = curwin;
4076 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int old_VIsual_select = VIsual_select;
4078 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 colnr_T tgt_leftcol = curwin->w_leftcol;
4080 long topline;
4081 long y;
4082
4083 /*
4084 * check 'scrollopt' string for vertical and horizontal scroll options
4085 */
4086 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4087#ifdef FEAT_DIFF
4088 want_ver |= old_curwin->w_p_diff;
4089#endif
4090 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4091
4092 /*
4093 * loop through the scrollbound windows and scroll accordingly
4094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4097 {
4098 curbuf = curwin->w_buffer;
4099 /* skip original window and windows with 'noscrollbind' */
4100 if (curwin != old_curwin && curwin->w_p_scb)
4101 {
4102 /*
4103 * do the vertical scroll
4104 */
4105 if (want_ver)
4106 {
4107#ifdef FEAT_DIFF
4108 if (old_curwin->w_p_diff && curwin->w_p_diff)
4109 {
4110 diff_set_topline(old_curwin, curwin);
4111 }
4112 else
4113#endif
4114 {
4115 curwin->w_scbind_pos += topline_diff;
4116 topline = curwin->w_scbind_pos;
4117 if (topline > curbuf->b_ml.ml_line_count)
4118 topline = curbuf->b_ml.ml_line_count;
4119 if (topline < 1)
4120 topline = 1;
4121
4122 y = topline - curwin->w_topline;
4123 if (y > 0)
4124 scrollup(y, FALSE);
4125 else
4126 scrolldown(-y, FALSE);
4127 }
4128
4129 redraw_later(VALID);
4130 cursor_correct();
4131#ifdef FEAT_WINDOWS
4132 curwin->w_redr_status = TRUE;
4133#endif
4134 }
4135
4136 /*
4137 * do the horizontal scroll
4138 */
4139 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4140 {
4141 curwin->w_leftcol = tgt_leftcol;
4142 leftcol_changed();
4143 }
4144 }
4145 }
4146
4147 /*
4148 * reset current-window
4149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 VIsual_select = old_VIsual_select;
4151 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 curwin = old_curwin;
4153 curbuf = old_curbuf;
4154}
4155#endif /* #ifdef FEAT_SCROLLBIND */
4156
4157/*
4158 * Command character that's ignored.
4159 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004160 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 static void
4163nv_ignore(cap)
4164 cmdarg_T *cap;
4165{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004166 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167}
4168
4169/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004170 * Command character that doesn't do anything, but unlike nv_ignore() does
4171 * start edit(). Used for "startinsert" executed while starting up.
4172 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004173 static void
4174nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004175 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004176{
4177}
4178
4179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Command character doesn't exist.
4181 */
4182 static void
4183nv_error(cap)
4184 cmdarg_T *cap;
4185{
4186 clearopbeep(cap->oap);
4187}
4188
4189/*
4190 * <Help> and <F1> commands.
4191 */
4192 static void
4193nv_help(cap)
4194 cmdarg_T *cap;
4195{
4196 if (!checkclearopq(cap->oap))
4197 ex_help(NULL);
4198}
4199
4200/*
4201 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4202 */
4203 static void
4204nv_addsub(cap)
4205 cmdarg_T *cap;
4206{
4207 if (!checkclearopq(cap->oap)
4208 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4209 prep_redo_cmd(cap);
4210}
4211
4212/*
4213 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4214 */
4215 static void
4216nv_page(cap)
4217 cmdarg_T *cap;
4218{
4219 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004220 {
4221#ifdef FEAT_WINDOWS
4222 if (mod_mask & MOD_MASK_CTRL)
4223 {
4224 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4225 if (cap->arg == BACKWARD)
4226 goto_tabpage(-(int)cap->count1);
4227 else
4228 goto_tabpage((int)cap->count0);
4229 }
4230 else
4231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234}
4235
4236/*
4237 * Implementation of "gd" and "gD" command.
4238 */
4239 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004240nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004241 oparg_T *oap;
4242 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004243 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 char_u *ptr;
4247
Bram Moolenaard9d30582005-05-18 22:10:28 +00004248 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004249 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004251#ifdef FEAT_FOLDING
4252 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4253 foldOpenCursor();
4254#endif
4255}
4256
4257/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004258 * Search for variable declaration of "ptr[len]".
4259 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4260 * current file ("gD").
4261 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 * Return FAIL when not found.
4263 */
4264 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004265find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004266 char_u *ptr;
4267 int len;
4268 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004269 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004270 int searchflags; /* flags passed to searchit() */
4271{
4272 char_u *pat;
4273 pos_T old_pos;
4274 pos_T par_pos;
4275 pos_T found_pos;
4276 int t;
4277 int save_p_ws;
4278 int save_p_scs;
4279 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004280 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004281
4282 if ((pat = alloc(len + 7)) == NULL)
4283 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004284
4285 /* Put "\V" before the pattern to avoid that the special meaning of "."
4286 * and "~" causes trouble. */
4287 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4288 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 old_pos = curwin->w_cursor;
4290 save_p_ws = p_ws;
4291 save_p_scs = p_scs;
4292 p_ws = FALSE; /* don't wrap around end of file now */
4293 p_scs = FALSE; /* don't switch ignorecase off now */
4294
4295 /*
4296 * With "gD" go to line 1.
4297 * With "gd" Search back for the start of the current function, then go
4298 * back until a blank line. If this fails go to line 1.
4299 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004300 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302 setpcmark(); /* Set in findpar() otherwise */
4303 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004304 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004308 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4310 --curwin->w_cursor.lnum;
4311 }
4312 curwin->w_cursor.col = 0;
4313
4314 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004315 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316 for (;;)
4317 {
4318 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004319 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004320 if (curwin->w_cursor.lnum >= old_pos.lnum)
4321 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004322
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004323 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004324 {
4325 pos_T *pos;
4326
4327 /* Check that the block the match is in doesn't end before the
4328 * position where we started the search from. */
4329 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4330 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4331 && pos->lnum < old_pos.lnum)
4332 continue;
4333 }
4334
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 if (t == FAIL)
4336 {
4337 /* If we previously found a valid position, use it. */
4338 if (found_pos.lnum != 0)
4339 {
4340 curwin->w_cursor = found_pos;
4341 t = OK;
4342 }
4343 break;
4344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004346 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 {
4348 /* Ignore this line, continue at start of next line. */
4349 ++curwin->w_cursor.lnum;
4350 curwin->w_cursor.col = 0;
4351 continue;
4352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 if (!locally) /* global search: use first match found */
4355 break;
4356 if (curwin->w_cursor.lnum >= par_pos.lnum)
4357 {
4358 /* If we previously found a valid position, use it. */
4359 if (found_pos.lnum != 0)
4360 curwin->w_cursor = found_pos;
4361 break;
4362 }
4363
4364 /* For finding a local variable and the match is before the "{" search
4365 * to find a later match. For K&R style function declarations this
4366 * skips the function header without types. */
4367 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004369
4370 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 curwin->w_cursor = old_pos;
4374 }
4375 else
4376 {
4377 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 /* "n" searches forward now */
4379 reset_search_dir();
4380 }
4381
4382 vim_free(pat);
4383 p_ws = save_p_ws;
4384 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004385
4386 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387}
4388
4389/*
4390 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4391 * lines rather than lines in the file.
4392 * 'dist' must be positive.
4393 *
4394 * Return OK if able to move cursor, FAIL otherwise.
4395 */
4396 static int
4397nv_screengo(oap, dir, dist)
4398 oparg_T *oap;
4399 int dir;
4400 long dist;
4401{
4402 int linelen = linetabsize(ml_get_curline());
4403 int retval = OK;
4404 int atend = FALSE;
4405 int n;
4406 int col_off1; /* margin offset for first screen line */
4407 int col_off2; /* margin offset for wrapped screen line */
4408 int width1; /* text width for first screen line */
4409 int width2; /* test width for wrapped screen line */
4410
4411 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004412 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 col_off1 = curwin_col_off();
4415 col_off2 = col_off1 - curwin_col_off2();
4416 width1 = W_WIDTH(curwin) - col_off1;
4417 width2 = W_WIDTH(curwin) - col_off2;
4418
4419#ifdef FEAT_VERTSPLIT
4420 if (curwin->w_width != 0)
4421 {
4422#endif
4423 /*
4424 * Instead of sticking at the last character of the buffer line we
4425 * try to stick in the last column of the screen.
4426 */
4427 if (curwin->w_curswant == MAXCOL)
4428 {
4429 atend = TRUE;
4430 validate_virtcol();
4431 if (width1 <= 0)
4432 curwin->w_curswant = 0;
4433 else
4434 {
4435 curwin->w_curswant = width1 - 1;
4436 if (curwin->w_virtcol > curwin->w_curswant)
4437 curwin->w_curswant += ((curwin->w_virtcol
4438 - curwin->w_curswant - 1) / width2 + 1) * width2;
4439 }
4440 }
4441 else
4442 {
4443 if (linelen > width1)
4444 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4445 else
4446 n = width1;
4447 if (curwin->w_curswant > (colnr_T)n + 1)
4448 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4449 * width2;
4450 }
4451
4452 while (dist--)
4453 {
4454 if (dir == BACKWARD)
4455 {
4456 if ((long)curwin->w_curswant >= width2)
4457 /* move back within line */
4458 curwin->w_curswant -= width2;
4459 else
4460 {
4461 /* to previous line */
4462 if (curwin->w_cursor.lnum == 1)
4463 {
4464 retval = FAIL;
4465 break;
4466 }
4467 --curwin->w_cursor.lnum;
4468#ifdef FEAT_FOLDING
4469 /* Move to the start of a closed fold. Don't do that when
4470 * 'foldopen' contains "all": it will open in a moment. */
4471 if (!(fdo_flags & FDO_ALL))
4472 (void)hasFolding(curwin->w_cursor.lnum,
4473 &curwin->w_cursor.lnum, NULL);
4474#endif
4475 linelen = linetabsize(ml_get_curline());
4476 if (linelen > width1)
4477 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4478 + 1) * width2;
4479 }
4480 }
4481 else /* dir == FORWARD */
4482 {
4483 if (linelen > width1)
4484 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4485 else
4486 n = width1;
4487 if (curwin->w_curswant + width2 < (colnr_T)n)
4488 /* move forward within line */
4489 curwin->w_curswant += width2;
4490 else
4491 {
4492 /* to next line */
4493#ifdef FEAT_FOLDING
4494 /* Move to the end of a closed fold. */
4495 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4496 &curwin->w_cursor.lnum);
4497#endif
4498 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4499 {
4500 retval = FAIL;
4501 break;
4502 }
4503 curwin->w_cursor.lnum++;
4504 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004505 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 }
4508 }
4509#ifdef FEAT_VERTSPLIT
4510 }
4511#endif
4512
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004513 if (virtual_active() && atend)
4514 coladvance(MAXCOL);
4515 else
4516 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4519 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4520 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004521 colnr_T virtcol;
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 /*
4524 * Check for landing on a character that got split at the end of the
4525 * last line. We want to advance a screenline, not end up in the same
4526 * screenline or move two screenlines.
4527 */
4528 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004529 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004530# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004531 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4532 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004533# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004534
4535 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && (curwin->w_curswant < (colnr_T)width1
4537 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4538 : ((curwin->w_curswant - width1) % width2
4539 > (colnr_T)width2 / 2)))
4540 --curwin->w_cursor.col;
4541 }
4542#endif
4543
4544 if (atend)
4545 curwin->w_curswant = MAXCOL; /* stick in the last column */
4546
4547 return retval;
4548}
4549
4550#ifdef FEAT_MOUSE
4551/*
4552 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4553 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004554 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4555 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 */
4557 static void
4558nv_mousescroll(cap)
4559 cmdarg_T *cap;
4560{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004561# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004562 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004564 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 int row, col;
4567
4568 row = mouse_row;
4569 col = mouse_col;
4570
4571 /* find the window at the pointer coordinates */
4572 curwin = mouse_find_win(&row, &col);
4573 curbuf = curwin->w_buffer;
4574 }
4575# endif
4576
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004577 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004579 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4580 {
4581 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4582 }
4583 else
4584 {
4585 cap->count1 = 3;
4586 cap->count0 = 3;
4587 nv_scroll_line(cap);
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004590# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else
4592 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004593 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4594 if (!curwin->w_p_wrap)
4595 {
4596 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004597
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004598 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4599 step = W_WIDTH(curwin);
4600 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4601 if (val < 0)
4602 val = 0;
4603
4604 gui_do_horiz_scroll(val, TRUE);
4605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004609# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 curwin->w_redr_status = TRUE;
4611
4612 curwin = old_curwin;
4613 curbuf = curwin->w_buffer;
4614# endif
4615}
4616
4617/*
4618 * Mouse clicks and drags.
4619 */
4620 static void
4621nv_mouse(cap)
4622 cmdarg_T *cap;
4623{
4624 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4625}
4626#endif
4627
4628/*
4629 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4630 * cap->arg must be TRUE for CTRL-E.
4631 */
4632 static void
4633nv_scroll_line(cap)
4634 cmdarg_T *cap;
4635{
4636 if (!checkclearop(cap->oap))
4637 scroll_redraw(cap->arg, cap->count1);
4638}
4639
4640/*
4641 * Scroll "count" lines up or down, and redraw.
4642 */
4643 void
4644scroll_redraw(up, count)
4645 int up;
4646 long count;
4647{
4648 linenr_T prev_topline = curwin->w_topline;
4649#ifdef FEAT_DIFF
4650 int prev_topfill = curwin->w_topfill;
4651#endif
4652 linenr_T prev_lnum = curwin->w_cursor.lnum;
4653
4654 if (up)
4655 scrollup(count, TRUE);
4656 else
4657 scrolldown(count, TRUE);
4658 if (p_so)
4659 {
4660 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4661 * valid, otherwise the screen jumps back at the end of the file. */
4662 cursor_correct();
4663 check_cursor_moved(curwin);
4664 curwin->w_valid |= VALID_TOPLINE;
4665
4666 /* If moved back to where we were, at least move the cursor, otherwise
4667 * we get stuck at one position. Don't move the cursor up if the
4668 * first line of the buffer is already on the screen */
4669 while (curwin->w_topline == prev_topline
4670#ifdef FEAT_DIFF
4671 && curwin->w_topfill == prev_topfill
4672#endif
4673 )
4674 {
4675 if (up)
4676 {
4677 if (curwin->w_cursor.lnum > prev_lnum
4678 || cursor_down(1L, FALSE) == FAIL)
4679 break;
4680 }
4681 else
4682 {
4683 if (curwin->w_cursor.lnum < prev_lnum
4684 || prev_topline == 1L
4685 || cursor_up(1L, FALSE) == FAIL)
4686 break;
4687 }
4688 /* Mark w_topline as valid, otherwise the screen jumps back at the
4689 * end of the file. */
4690 check_cursor_moved(curwin);
4691 curwin->w_valid |= VALID_TOPLINE;
4692 }
4693 }
4694 if (curwin->w_cursor.lnum != prev_lnum)
4695 coladvance(curwin->w_curswant);
4696 redraw_later(VALID);
4697}
4698
4699/*
4700 * Commands that start with "z".
4701 */
4702 static void
4703nv_zet(cap)
4704 cmdarg_T *cap;
4705{
4706 long n;
4707 colnr_T col;
4708 int nchar = cap->nchar;
4709#ifdef FEAT_FOLDING
4710 long old_fdl = curwin->w_p_fdl;
4711 int old_fen = curwin->w_p_fen;
4712#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004713#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004714 int undo = FALSE;
4715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 if (VIM_ISDIGIT(nchar))
4718 {
4719 /*
4720 * "z123{nchar}": edit the count before obtaining {nchar}
4721 */
4722 if (checkclearop(cap->oap))
4723 return;
4724 n = nchar - '0';
4725 for (;;)
4726 {
4727#ifdef USE_ON_FLY_SCROLL
4728 dont_scroll = TRUE; /* disallow scrolling here */
4729#endif
4730 ++no_mapping;
4731 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004732 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 --no_mapping;
4735 --allow_keys;
4736#ifdef FEAT_CMDL_INFO
4737 (void)add_to_showcmd(nchar);
4738#endif
4739 if (nchar == K_DEL || nchar == K_KDEL)
4740 n /= 10;
4741 else if (VIM_ISDIGIT(nchar))
4742 n = n * 10 + (nchar - '0');
4743 else if (nchar == CAR)
4744 {
4745#ifdef FEAT_GUI
4746 need_mouse_correct = TRUE;
4747#endif
4748 win_setheight((int)n);
4749 break;
4750 }
4751 else if (nchar == 'l'
4752 || nchar == 'h'
4753 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004754 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 cap->count1 = n ? n * cap->count1 : cap->count1;
4757 goto dozet;
4758 }
4759 else
4760 {
4761 clearopbeep(cap->oap);
4762 break;
4763 }
4764 }
4765 cap->oap->op_type = OP_NOP;
4766 return;
4767 }
4768
4769dozet:
4770 if (
4771#ifdef FEAT_FOLDING
4772 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4773 * and "zC" only in Visual mode. "zj" and "zk" are motion
4774 * commands. */
4775 cap->nchar != 'f' && cap->nchar != 'F'
4776 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4777 && cap->nchar != 'j' && cap->nchar != 'k'
4778 &&
4779#endif
4780 checkclearop(cap->oap))
4781 return;
4782
4783 /*
4784 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4785 * If line number given, set cursor.
4786 */
4787 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4788 && cap->count0
4789 && cap->count0 != curwin->w_cursor.lnum)
4790 {
4791 setpcmark();
4792 if (cap->count0 > curbuf->b_ml.ml_line_count)
4793 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4794 else
4795 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004796 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798
4799 switch (nchar)
4800 {
4801 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4802 case '+':
4803 if (cap->count0 == 0)
4804 {
4805 /* No count given: put cursor at the line below screen */
4806 validate_botline(); /* make sure w_botline is valid */
4807 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4808 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4809 else
4810 curwin->w_cursor.lnum = curwin->w_botline;
4811 }
4812 /* FALLTHROUGH */
4813 case NL:
4814 case CAR:
4815 case K_KENTER:
4816 beginline(BL_WHITE | BL_FIX);
4817 /* FALLTHROUGH */
4818
4819 case 't': scroll_cursor_top(0, TRUE);
4820 redraw_later(VALID);
4821 break;
4822
4823 /* "z." and "zz": put cursor in middle of screen */
4824 case '.': beginline(BL_WHITE | BL_FIX);
4825 /* FALLTHROUGH */
4826
4827 case 'z': scroll_cursor_halfway(TRUE);
4828 redraw_later(VALID);
4829 break;
4830
4831 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4832 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4833 * when <count> is at bottom of window, and puts that one at
4834 * bottom of window. */
4835 if (cap->count0 != 0)
4836 {
4837 scroll_cursor_bot(0, TRUE);
4838 curwin->w_cursor.lnum = curwin->w_topline;
4839 }
4840 else if (curwin->w_topline == 1)
4841 curwin->w_cursor.lnum = 1;
4842 else
4843 curwin->w_cursor.lnum = curwin->w_topline - 1;
4844 /* FALLTHROUGH */
4845 case '-':
4846 beginline(BL_WHITE | BL_FIX);
4847 /* FALLTHROUGH */
4848
4849 case 'b': scroll_cursor_bot(0, TRUE);
4850 redraw_later(VALID);
4851 break;
4852
4853 /* "zH" - scroll screen right half-page */
4854 case 'H':
4855 cap->count1 *= W_WIDTH(curwin) / 2;
4856 /* FALLTHROUGH */
4857
4858 /* "zh" - scroll screen to the right */
4859 case 'h':
4860 case K_LEFT:
4861 if (!curwin->w_p_wrap)
4862 {
4863 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4864 curwin->w_leftcol = 0;
4865 else
4866 curwin->w_leftcol -= (colnr_T)cap->count1;
4867 leftcol_changed();
4868 }
4869 break;
4870
4871 /* "zL" - scroll screen left half-page */
4872 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4873 /* FALLTHROUGH */
4874
4875 /* "zl" - scroll screen to the left */
4876 case 'l':
4877 case K_RIGHT:
4878 if (!curwin->w_p_wrap)
4879 {
4880 /* scroll the window left */
4881 curwin->w_leftcol += (colnr_T)cap->count1;
4882 leftcol_changed();
4883 }
4884 break;
4885
4886 /* "zs" - scroll screen, cursor at the start */
4887 case 's': if (!curwin->w_p_wrap)
4888 {
4889#ifdef FEAT_FOLDING
4890 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4891 col = 0; /* like the cursor is in col 0 */
4892 else
4893#endif
4894 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4895 if ((long)col > p_siso)
4896 col -= p_siso;
4897 else
4898 col = 0;
4899 if (curwin->w_leftcol != col)
4900 {
4901 curwin->w_leftcol = col;
4902 redraw_later(NOT_VALID);
4903 }
4904 }
4905 break;
4906
4907 /* "ze" - scroll screen, cursor at the end */
4908 case 'e': if (!curwin->w_p_wrap)
4909 {
4910#ifdef FEAT_FOLDING
4911 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4912 col = 0; /* like the cursor is in col 0 */
4913 else
4914#endif
4915 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4916 n = W_WIDTH(curwin) - curwin_col_off();
4917 if ((long)col + p_siso < n)
4918 col = 0;
4919 else
4920 col = col + p_siso - n + 1;
4921 if (curwin->w_leftcol != col)
4922 {
4923 curwin->w_leftcol = col;
4924 redraw_later(NOT_VALID);
4925 }
4926 }
4927 break;
4928
4929#ifdef FEAT_FOLDING
4930 /* "zF": create fold command */
4931 /* "zf": create fold operator */
4932 case 'F':
4933 case 'f': if (foldManualAllowed(TRUE))
4934 {
4935 cap->nchar = 'f';
4936 nv_operator(cap);
4937 curwin->w_p_fen = TRUE;
4938
4939 /* "zF" is like "zfzf" */
4940 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4941 {
4942 nv_operator(cap);
4943 finish_op = TRUE;
4944 }
4945 }
4946 else
4947 clearopbeep(cap->oap);
4948 break;
4949
4950 /* "zd": delete fold at cursor */
4951 /* "zD": delete fold at cursor recursively */
4952 case 'd':
4953 case 'D': if (foldManualAllowed(FALSE))
4954 {
4955 if (VIsual_active)
4956 nv_operator(cap);
4957 else
4958 deleteFold(curwin->w_cursor.lnum,
4959 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4960 }
4961 break;
4962
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004963 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 case 'E': if (foldmethodIsManual(curwin))
4965 {
4966 clearFolding(curwin);
4967 changed_window_setting();
4968 }
4969 else if (foldmethodIsMarker(curwin))
4970 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4971 TRUE, FALSE);
4972 else
4973 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4974 break;
4975
4976 /* "zn": fold none: reset 'foldenable' */
4977 case 'n': curwin->w_p_fen = FALSE;
4978 break;
4979
4980 /* "zN": fold Normal: set 'foldenable' */
4981 case 'N': curwin->w_p_fen = TRUE;
4982 break;
4983
4984 /* "zi": invert folding: toggle 'foldenable' */
4985 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4986 break;
4987
4988 /* "za": open closed fold or close open fold at cursor */
4989 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4990 openFold(curwin->w_cursor.lnum, cap->count1);
4991 else
4992 {
4993 closeFold(curwin->w_cursor.lnum, cap->count1);
4994 curwin->w_p_fen = TRUE;
4995 }
4996 break;
4997
4998 /* "zA": open fold at cursor recursively */
4999 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5000 openFoldRecurse(curwin->w_cursor.lnum);
5001 else
5002 {
5003 closeFoldRecurse(curwin->w_cursor.lnum);
5004 curwin->w_p_fen = TRUE;
5005 }
5006 break;
5007
5008 /* "zo": open fold at cursor or Visual area */
5009 case 'o': if (VIsual_active)
5010 nv_operator(cap);
5011 else
5012 openFold(curwin->w_cursor.lnum, cap->count1);
5013 break;
5014
5015 /* "zO": open fold recursively */
5016 case 'O': if (VIsual_active)
5017 nv_operator(cap);
5018 else
5019 openFoldRecurse(curwin->w_cursor.lnum);
5020 break;
5021
5022 /* "zc": close fold at cursor or Visual area */
5023 case 'c': if (VIsual_active)
5024 nv_operator(cap);
5025 else
5026 closeFold(curwin->w_cursor.lnum, cap->count1);
5027 curwin->w_p_fen = TRUE;
5028 break;
5029
5030 /* "zC": close fold recursively */
5031 case 'C': if (VIsual_active)
5032 nv_operator(cap);
5033 else
5034 closeFoldRecurse(curwin->w_cursor.lnum);
5035 curwin->w_p_fen = TRUE;
5036 break;
5037
5038 /* "zv": open folds at the cursor */
5039 case 'v': foldOpenCursor();
5040 break;
5041
5042 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5043 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005044 curwin->w_foldinvalid = TRUE; /* recompute folds */
5045 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 foldOpenCursor();
5047 break;
5048
5049 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5050 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005051 curwin->w_foldinvalid = TRUE; /* recompute folds */
5052 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 break;
5054
5055 /* "zm": fold more */
5056 case 'm': if (curwin->w_p_fdl > 0)
5057 --curwin->w_p_fdl;
5058 old_fdl = -1; /* force an update */
5059 curwin->w_p_fen = TRUE;
5060 break;
5061
5062 /* "zM": close all folds */
5063 case 'M': curwin->w_p_fdl = 0;
5064 old_fdl = -1; /* force an update */
5065 curwin->w_p_fen = TRUE;
5066 break;
5067
5068 /* "zr": reduce folding */
5069 case 'r': ++curwin->w_p_fdl;
5070 break;
5071
5072 /* "zR": open all folds */
5073 case 'R': curwin->w_p_fdl = getDeepestNesting();
5074 old_fdl = -1; /* force an update */
5075 break;
5076
5077 case 'j': /* "zj" move to next fold downwards */
5078 case 'k': /* "zk" move to next fold upwards */
5079 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5080 cap->count1) == FAIL)
5081 clearopbeep(cap->oap);
5082 break;
5083
5084#endif /* FEAT_FOLDING */
5085
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005086#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005087 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5088 ++no_mapping;
5089 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005090 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005091 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005092 --no_mapping;
5093 --allow_keys;
5094#ifdef FEAT_CMDL_INFO
5095 (void)add_to_showcmd(nchar);
5096#endif
5097 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5098 {
5099 clearopbeep(cap->oap);
5100 break;
5101 }
5102 undo = TRUE;
5103 /*FALLTHROUGH*/
5104
Bram Moolenaarb765d632005-06-07 21:00:02 +00005105 case 'g': /* "zg": add good word to word list */
5106 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005107 case 'G': /* "zG": add good word to temp word list */
5108 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005109 {
5110 char_u *ptr = NULL;
5111 int len;
5112
5113 if (checkclearop(cap->oap))
5114 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005115 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5116 == FAIL)
5117 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005118 if (ptr == NULL)
5119 {
5120 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005121
Bram Moolenaar134bf072013-09-25 18:54:24 +02005122 /* Find bad word under the cursor. When 'spell' is
5123 * off this fails and find_ident_under_cursor() is
5124 * used below. */
5125 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005126 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005127 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005128 if (len != 0 && curwin->w_cursor.col <= pos.col)
5129 ptr = ml_get_pos(&curwin->w_cursor);
5130 curwin->w_cursor = pos;
5131 }
5132
Bram Moolenaarb765d632005-06-07 21:00:02 +00005133 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5134 FIND_IDENT)) == 0)
5135 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005136 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005137 (nchar == 'G' || nchar == 'W')
5138 ? 0 : (int)cap->count1,
5139 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005140 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005141 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005142
Bram Moolenaar43abc522005-12-10 20:15:02 +00005143 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005144 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005145 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005147#endif
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 default: clearopbeep(cap->oap);
5150 }
5151
5152#ifdef FEAT_FOLDING
5153 /* Redraw when 'foldenable' changed */
5154 if (old_fen != curwin->w_p_fen)
5155 {
5156# ifdef FEAT_DIFF
5157 win_T *wp;
5158
5159 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5160 {
5161 /* Adjust 'foldenable' in diff-synced windows. */
5162 FOR_ALL_WINDOWS(wp)
5163 {
5164 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5165 {
5166 wp->w_p_fen = curwin->w_p_fen;
5167 changed_window_setting_win(wp);
5168 }
5169 }
5170 }
5171# endif
5172 changed_window_setting();
5173 }
5174
5175 /* Redraw when 'foldlevel' changed. */
5176 if (old_fdl != curwin->w_p_fdl)
5177 newFoldLevel();
5178#endif
5179}
5180
5181#ifdef FEAT_GUI
5182/*
5183 * Vertical scrollbar movement.
5184 */
5185 static void
5186nv_ver_scrollbar(cap)
5187 cmdarg_T *cap;
5188{
5189 if (cap->oap->op_type != OP_NOP)
5190 clearopbeep(cap->oap);
5191
5192 /* Even if an operator was pending, we still want to scroll */
5193 gui_do_scroll();
5194}
5195
5196/*
5197 * Horizontal scrollbar movement.
5198 */
5199 static void
5200nv_hor_scrollbar(cap)
5201 cmdarg_T *cap;
5202{
5203 if (cap->oap->op_type != OP_NOP)
5204 clearopbeep(cap->oap);
5205
5206 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005207 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208}
5209#endif
5210
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005211#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005212/*
5213 * Click in GUI tab.
5214 */
5215 static void
5216nv_tabline(cap)
5217 cmdarg_T *cap;
5218{
5219 if (cap->oap->op_type != OP_NOP)
5220 clearopbeep(cap->oap);
5221
5222 /* Even if an operator was pending, we still want to jump tabs. */
5223 goto_tabpage(current_tab);
5224}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005225
5226/*
5227 * Selected item in tab line menu.
5228 */
5229 static void
5230nv_tabmenu(cap)
5231 cmdarg_T *cap;
5232{
5233 if (cap->oap->op_type != OP_NOP)
5234 clearopbeep(cap->oap);
5235
5236 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005237 handle_tabmenu();
5238}
5239
5240/*
5241 * Handle selecting an item of the GUI tab line menu.
5242 * Used in Normal and Insert mode.
5243 */
5244 void
5245handle_tabmenu()
5246{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005247 switch (current_tabmenu)
5248 {
5249 case TABLINE_MENU_CLOSE:
5250 if (current_tab == 0)
5251 do_cmdline_cmd((char_u *)"tabclose");
5252 else
5253 {
5254 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5255 current_tab);
5256 do_cmdline_cmd(IObuff);
5257 }
5258 break;
5259
5260 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005261 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5262 current_tab > 0 ? current_tab - 1 : 999);
5263 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005264 break;
5265
5266 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005267 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5268 current_tab > 0 ? current_tab - 1 : 999);
5269 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005270 break;
5271 }
5272}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005273#endif
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275/*
5276 * "Q" command.
5277 */
5278 static void
5279nv_exmode(cap)
5280 cmdarg_T *cap;
5281{
5282 /*
5283 * Ignore 'Q' in Visual mode, just give a beep.
5284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 if (VIsual_active)
5286 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005287 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 do_exmode(FALSE);
5289}
5290
5291/*
5292 * Handle a ":" command.
5293 */
5294 static void
5295nv_colon(cap)
5296 cmdarg_T *cap;
5297{
5298 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005299 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (VIsual_active)
5302 nv_operator(cap);
5303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
5305 if (cap->oap->op_type != OP_NOP)
5306 {
5307 /* Using ":" as a movement is characterwise exclusive. */
5308 cap->oap->motion_type = MCHAR;
5309 cap->oap->inclusive = FALSE;
5310 }
5311 else if (cap->count0)
5312 {
5313 /* translate "count:" into ":.,.+(count - 1)" */
5314 stuffcharReadbuff('.');
5315 if (cap->count0 > 1)
5316 {
5317 stuffReadbuff((char_u *)",.+");
5318 stuffnumReadbuff((long)cap->count0 - 1L);
5319 }
5320 }
5321
5322 /* When typing, don't type below an old message */
5323 if (KeyTyped)
5324 compute_cmdrow();
5325
5326 old_p_im = p_im;
5327
5328 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005329 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5331
5332 /* If 'insertmode' changed, enter or exit Insert mode */
5333 if (p_im != old_p_im)
5334 {
5335 if (p_im)
5336 restart_edit = 'i';
5337 else
5338 restart_edit = 0;
5339 }
5340
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005341 if (cmd_result == FAIL)
5342 /* The Ex command failed, do not execute the operator. */
5343 clearop(cap->oap);
5344 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5346 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005347 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5348 || did_emsg
5349 ))
5350 /* The start of the operator has become invalid by the Ex command.
5351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 clearopbeep(cap->oap);
5353 }
5354}
5355
5356/*
5357 * Handle CTRL-G command.
5358 */
5359 static void
5360nv_ctrlg(cap)
5361 cmdarg_T *cap;
5362{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (VIsual_active) /* toggle Selection/Visual mode */
5364 {
5365 VIsual_select = !VIsual_select;
5366 showmode();
5367 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005368 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /* print full name if count given or :cd used */
5370 fileinfo((int)cap->count0, FALSE, TRUE);
5371}
5372
5373/*
5374 * Handle CTRL-H <Backspace> command.
5375 */
5376 static void
5377nv_ctrlh(cap)
5378 cmdarg_T *cap;
5379{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (VIsual_active && VIsual_select)
5381 {
5382 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5383 v_visop(cap);
5384 }
5385 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 nv_left(cap);
5387}
5388
5389/*
5390 * CTRL-L: clear screen and redraw.
5391 */
5392 static void
5393nv_clear(cap)
5394 cmdarg_T *cap;
5395{
5396 if (!checkclearop(cap->oap))
5397 {
5398#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5399 /*
5400 * Right now, the BeBox doesn't seem to have an easy way to detect
5401 * window resizing, so we cheat and make the user detect it
5402 * manually with CTRL-L instead
5403 */
5404 ui_get_shellsize();
5405#endif
5406#ifdef FEAT_SYN_HL
5407 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005408 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409#endif
5410 redraw_later(CLEAR);
5411 }
5412}
5413
5414/*
5415 * CTRL-O: In Select mode: switch to Visual mode for one command.
5416 * Otherwise: Go to older pcmark.
5417 */
5418 static void
5419nv_ctrlo(cap)
5420 cmdarg_T *cap;
5421{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (VIsual_active && VIsual_select)
5423 {
5424 VIsual_select = FALSE;
5425 showmode();
5426 restart_VIsual_select = 2; /* restart Select mode later */
5427 }
5428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 cap->count1 = -cap->count1;
5431 nv_pcmark(cap);
5432 }
5433}
5434
5435/*
5436 * CTRL-^ command, short for ":e #"
5437 */
5438 static void
5439nv_hat(cap)
5440 cmdarg_T *cap;
5441{
5442 if (!checkclearopq(cap->oap))
5443 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5444 GETF_SETMARK|GETF_ALT, FALSE);
5445}
5446
5447/*
5448 * "Z" commands.
5449 */
5450 static void
5451nv_Zet(cap)
5452 cmdarg_T *cap;
5453{
5454 if (!checkclearopq(cap->oap))
5455 {
5456 switch (cap->nchar)
5457 {
5458 /* "ZZ": equivalent to ":x". */
5459 case 'Z': do_cmdline_cmd((char_u *)"x");
5460 break;
5461
5462 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5463 case 'Q': do_cmdline_cmd((char_u *)"q!");
5464 break;
5465
5466 default: clearopbeep(cap->oap);
5467 }
5468 }
5469}
5470
5471#if defined(FEAT_WINDOWS) || defined(PROTO)
5472/*
5473 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5474 */
5475 void
5476do_nv_ident(c1, c2)
5477 int c1;
5478 int c2;
5479{
5480 oparg_T oa;
5481 cmdarg_T ca;
5482
5483 clear_oparg(&oa);
5484 vim_memset(&ca, 0, sizeof(ca));
5485 ca.oap = &oa;
5486 ca.cmdchar = c1;
5487 ca.nchar = c2;
5488 nv_ident(&ca);
5489}
5490#endif
5491
5492/*
5493 * Handle the commands that use the word under the cursor.
5494 * [g] CTRL-] :ta to current identifier
5495 * [g] 'K' run program for current identifier
5496 * [g] '*' / to current identifier or string
5497 * [g] '#' ? to current identifier or string
5498 * g ']' :tselect for current identifier
5499 */
5500 static void
5501nv_ident(cap)
5502 cmdarg_T *cap;
5503{
5504 char_u *ptr = NULL;
5505 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005506 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 char_u *p;
5508 char_u *kp; /* value of 'keywordprg' */
5509 int kp_help; /* 'keywordprg' is ":help" */
5510 int n = 0; /* init for GCC */
5511 int cmdchar;
5512 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005513 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 char_u *aux_ptr;
5515 int isman;
5516 int isman_s;
5517
5518 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5519 {
5520 cmdchar = cap->nchar;
5521 g_cmd = TRUE;
5522 }
5523 else
5524 {
5525 cmdchar = cap->cmdchar;
5526 g_cmd = FALSE;
5527 }
5528
5529 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5530 cmdchar = '#';
5531
5532 /*
5533 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5534 */
5535 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5538 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (checkclearopq(cap->oap))
5540 return;
5541 }
5542
5543 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5544 (cmdchar == '*' || cmdchar == '#')
5545 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5546 {
5547 clearop(cap->oap);
5548 return;
5549 }
5550
5551 /* Allocate buffer to put the command in. Inserting backslashes can
5552 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5553 * and some numbers. */
5554 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5555 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5556 || STRCMP(kp, ":help") == 0);
5557 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5558 if (buf == NULL)
5559 return;
5560 buf[0] = NUL;
5561
5562 switch (cmdchar)
5563 {
5564 case '*':
5565 case '#':
5566 /*
5567 * Put cursor at start of word, makes search skip the word
5568 * under the cursor.
5569 * Call setpcmark() first, so "*``" puts the cursor back where
5570 * it was.
5571 */
5572 setpcmark();
5573 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5574
5575 if (!g_cmd && vim_iswordp(ptr))
5576 STRCPY(buf, "\\<");
5577 no_smartcase = TRUE; /* don't use 'smartcase' now */
5578 break;
5579
5580 case 'K':
5581 if (kp_help)
5582 STRCPY(buf, "he! ");
5583 else
5584 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005585 /* An external command will probably use an argument starting
5586 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005587 while (*ptr == '-' && n > 0)
5588 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005589 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005590 --n;
5591 }
5592 if (n == 0)
5593 {
5594 EMSG(_(e_noident)); /* found dashes only */
5595 vim_free(buf);
5596 return;
5597 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 /* When a count is given, turn it into a range. Is this
5600 * really what we want? */
5601 isman = (STRCMP(kp, "man") == 0);
5602 isman_s = (STRCMP(kp, "man -s") == 0);
5603 if (cap->count0 != 0 && !(isman || isman_s))
5604 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5605
5606 STRCAT(buf, "! ");
5607 if (cap->count0 == 0 && isman_s)
5608 STRCAT(buf, "man");
5609 else
5610 STRCAT(buf, kp);
5611 STRCAT(buf, " ");
5612 if (cap->count0 != 0 && (isman || isman_s))
5613 {
5614 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5615 STRCAT(buf, " ");
5616 }
5617 }
5618 break;
5619
5620 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005621 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622#ifdef FEAT_CSCOPE
5623 if (p_cst)
5624 STRCPY(buf, "cstag ");
5625 else
5626#endif
5627 STRCPY(buf, "ts ");
5628 break;
5629
5630 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005631 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 if (curbuf->b_help)
5633 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005635 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005636 if (g_cmd)
5637 STRCPY(buf, "tj ");
5638 else
5639 sprintf((char *)buf, "%ldta ", cap->count0);
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642
5643 /*
5644 * Now grab the chars in the identifier
5645 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005646 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005648 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005649 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005650 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005651 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005652 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005654 vim_free(buf);
5655 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005657 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5658 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005659 {
5660 vim_free(buf);
5661 vim_free(p);
5662 return;
5663 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005664 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005665 STRCAT(buf, p);
5666 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668 else
5669 {
5670 if (cmdchar == '*')
5671 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5672 else if (cmdchar == '#')
5673 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005674 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005675 {
5676 if (curbuf->b_help)
5677 /* ":help" handles unescaped argument */
5678 aux_ptr = (char_u *)"";
5679 else
5680 aux_ptr = (char_u *)"\\|\"\n[";
5681 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005682 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005683 aux_ptr = (char_u *)"\\|\"\n*?[";
5684
5685 p = buf + STRLEN(buf);
5686 while (n-- > 0)
5687 {
5688 /* put a backslash before \ and some others */
5689 if (vim_strchr(aux_ptr, *ptr) != NULL)
5690 *p++ = '\\';
5691#ifdef FEAT_MBYTE
5692 /* When current byte is a part of multibyte character, copy all
5693 * bytes of that character. */
5694 if (has_mbyte)
5695 {
5696 int i;
5697 int len = (*mb_ptr2len)(ptr) - 1;
5698
5699 for (i = 0; i < len && n >= 1; ++i, --n)
5700 *p++ = *ptr++;
5701 }
5702#endif
5703 *p++ = *ptr++;
5704 }
5705 *p = NUL;
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
5708 /*
5709 * Execute the command.
5710 */
5711 if (cmdchar == '*' || cmdchar == '#')
5712 {
5713 if (!g_cmd && (
5714#ifdef FEAT_MBYTE
5715 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5716#endif
5717 vim_iswordc(ptr[-1])))
5718 STRCAT(buf, "\\>");
5719#ifdef FEAT_CMDHIST
5720 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005721 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5723#endif
5724 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5725 }
5726 else
5727 do_cmdline_cmd(buf);
5728
5729 vim_free(buf);
5730}
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732/*
5733 * Get visually selected text, within one line only.
5734 * Returns FAIL if more than one line selected.
5735 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005736 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737get_visual_text(cap, pp, lenp)
5738 cmdarg_T *cap;
5739 char_u **pp; /* return: start of selected text */
5740 int *lenp; /* return: length of selected text */
5741{
5742 if (VIsual_mode != 'V')
5743 unadjust_for_sel();
5744 if (VIsual.lnum != curwin->w_cursor.lnum)
5745 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005746 if (cap != NULL)
5747 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 return FAIL;
5749 }
5750 if (VIsual_mode == 'V')
5751 {
5752 *pp = ml_get_curline();
5753 *lenp = (int)STRLEN(*pp);
5754 }
5755 else
5756 {
5757 if (lt(curwin->w_cursor, VIsual))
5758 {
5759 *pp = ml_get_pos(&curwin->w_cursor);
5760 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5761 }
5762 else
5763 {
5764 *pp = ml_get_pos(&VIsual);
5765 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5766 }
5767#ifdef FEAT_MBYTE
5768 if (has_mbyte)
5769 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005770 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#endif
5772 }
5773 reset_VIsual_and_resel();
5774 return OK;
5775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777/*
5778 * CTRL-T: backwards in tag stack
5779 */
5780 static void
5781nv_tagpop(cap)
5782 cmdarg_T *cap;
5783{
5784 if (!checkclearopq(cap->oap))
5785 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5786}
5787
5788/*
5789 * Handle scrolling command 'H', 'L' and 'M'.
5790 */
5791 static void
5792nv_scroll(cap)
5793 cmdarg_T *cap;
5794{
5795 int used = 0;
5796 long n;
5797#ifdef FEAT_FOLDING
5798 linenr_T lnum;
5799#endif
5800 int half;
5801
5802 cap->oap->motion_type = MLINE;
5803 setpcmark();
5804
5805 if (cap->cmdchar == 'L')
5806 {
5807 validate_botline(); /* make sure curwin->w_botline is valid */
5808 curwin->w_cursor.lnum = curwin->w_botline - 1;
5809 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5810 curwin->w_cursor.lnum = 1;
5811 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005812 {
5813#ifdef FEAT_FOLDING
5814 if (hasAnyFolding(curwin))
5815 {
5816 /* Count a fold for one screen line. */
5817 for (n = cap->count1 - 1; n > 0
5818 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5819 {
5820 (void)hasFolding(curwin->w_cursor.lnum,
5821 &curwin->w_cursor.lnum, NULL);
5822 --curwin->w_cursor.lnum;
5823 }
5824 }
5825 else
5826#endif
5827 curwin->w_cursor.lnum -= cap->count1 - 1;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 else
5831 {
5832 if (cap->cmdchar == 'M')
5833 {
5834#ifdef FEAT_DIFF
5835 /* Don't count filler lines above the window. */
5836 used -= diff_check_fill(curwin, curwin->w_topline)
5837 - curwin->w_topfill;
5838#endif
5839 validate_botline(); /* make sure w_empty_rows is valid */
5840 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5841 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5842 {
5843#ifdef FEAT_DIFF
5844 /* Count half he number of filler lines to be "below this
5845 * line" and half to be "above the next line". */
5846 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5847 + n) / 2 >= half)
5848 {
5849 --n;
5850 break;
5851 }
5852#endif
5853 used += plines(curwin->w_topline + n);
5854 if (used >= half)
5855 break;
5856#ifdef FEAT_FOLDING
5857 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5858 n = lnum - curwin->w_topline;
5859#endif
5860 }
5861 if (n > 0 && used > curwin->w_height)
5862 --n;
5863 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005864 else /* (cap->cmdchar == 'H') */
5865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005867#ifdef FEAT_FOLDING
5868 if (hasAnyFolding(curwin))
5869 {
5870 /* Count a fold for one screen line. */
5871 lnum = curwin->w_topline;
5872 while (n-- > 0 && lnum < curwin->w_botline - 1)
5873 {
5874 hasFolding(lnum, NULL, &lnum);
5875 ++lnum;
5876 }
5877 n = lnum - curwin->w_topline;
5878 }
5879#endif
5880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 curwin->w_cursor.lnum = curwin->w_topline + n;
5882 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5884 }
5885
5886 cursor_correct(); /* correct for 'so' */
5887 beginline(BL_SOL | BL_FIX);
5888}
5889
5890/*
5891 * Cursor right commands.
5892 */
5893 static void
5894nv_right(cap)
5895 cmdarg_T *cap;
5896{
5897 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005898 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005900 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5901 {
5902 /* <C-Right> and <S-Right> move a word or WORD right */
5903 if (mod_mask & MOD_MASK_CTRL)
5904 cap->arg = TRUE;
5905 nv_wordcmd(cap);
5906 return;
5907 }
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 cap->oap->motion_type = MCHAR;
5910 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005911 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005913#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005915 * In virtual edit mode, there's no such thing as "past_line", as lines
5916 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005919 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#endif
5921
5922 for (n = cap->count1; n > 0; --n)
5923 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005924 if ((!past_line && oneright() == FAIL)
5925 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005926 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005929 * <Space> wraps to next line if 'whichwrap' has 's'.
5930 * 'l' wraps to next line if 'whichwrap' has 'l'.
5931 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 */
5933 if ( ((cap->cmdchar == ' '
5934 && vim_strchr(p_ww, 's') != NULL)
5935 || (cap->cmdchar == 'l'
5936 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005937 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 && vim_strchr(p_ww, '>') != NULL))
5939 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5940 {
5941 /* When deleting we also count the NL as a character.
5942 * Set cap->oap->inclusive when last char in the line is
5943 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005944 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 && !cap->oap->inclusive
5946 && !lineempty(curwin->w_cursor.lnum))
5947 cap->oap->inclusive = TRUE;
5948 else
5949 {
5950 ++curwin->w_cursor.lnum;
5951 curwin->w_cursor.col = 0;
5952#ifdef FEAT_VIRTUALEDIT
5953 curwin->w_cursor.coladd = 0;
5954#endif
5955 curwin->w_set_curswant = TRUE;
5956 cap->oap->inclusive = FALSE;
5957 }
5958 continue;
5959 }
5960 if (cap->oap->op_type == OP_NOP)
5961 {
5962 /* Only beep and flush if not moved at all */
5963 if (n == cap->count1)
5964 beep_flush();
5965 }
5966 else
5967 {
5968 if (!lineempty(curwin->w_cursor.lnum))
5969 cap->oap->inclusive = TRUE;
5970 }
5971 break;
5972 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005973 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005976#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (virtual_active())
5978 oneright();
5979 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005982#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (has_mbyte)
5984 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005985 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 ++curwin->w_cursor.col;
5989 }
5990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
5992#ifdef FEAT_FOLDING
5993 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5994 && cap->oap->op_type == OP_NOP)
5995 foldOpenCursor();
5996#endif
5997}
5998
5999/*
6000 * Cursor left commands.
6001 *
6002 * Returns TRUE when operator end should not be adjusted.
6003 */
6004 static void
6005nv_left(cap)
6006 cmdarg_T *cap;
6007{
6008 long n;
6009
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006010 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6011 {
6012 /* <C-Left> and <S-Left> move a word or WORD left */
6013 if (mod_mask & MOD_MASK_CTRL)
6014 cap->arg = 1;
6015 nv_bck_word(cap);
6016 return;
6017 }
6018
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 cap->oap->motion_type = MCHAR;
6020 cap->oap->inclusive = FALSE;
6021 for (n = cap->count1; n > 0; --n)
6022 {
6023 if (oneleft() == FAIL)
6024 {
6025 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6026 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6027 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6028 */
6029 if ( (((cap->cmdchar == K_BS
6030 || cap->cmdchar == Ctrl_H)
6031 && vim_strchr(p_ww, 'b') != NULL)
6032 || (cap->cmdchar == 'h'
6033 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006034 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 && vim_strchr(p_ww, '<') != NULL))
6036 && curwin->w_cursor.lnum > 1)
6037 {
6038 --(curwin->w_cursor.lnum);
6039 coladvance((colnr_T)MAXCOL);
6040 curwin->w_set_curswant = TRUE;
6041
6042 /* When the NL before the first char has to be deleted we
6043 * put the cursor on the NUL after the previous line.
6044 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006045 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if ( (cap->oap->op_type == OP_DELETE
6047 || cap->oap->op_type == OP_CHANGE)
6048 && !lineempty(curwin->w_cursor.lnum))
6049 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006050 char_u *cp = ml_get_cursor();
6051
6052 if (*cp != NUL)
6053 {
6054#ifdef FEAT_MBYTE
6055 if (has_mbyte)
6056 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6057 else
6058#endif
6059 ++curwin->w_cursor.col;
6060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 cap->retval |= CA_NO_ADJ_OP_END;
6062 }
6063 continue;
6064 }
6065 /* Only beep and flush if not moved at all */
6066 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6067 beep_flush();
6068 break;
6069 }
6070 }
6071#ifdef FEAT_FOLDING
6072 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6073 && cap->oap->op_type == OP_NOP)
6074 foldOpenCursor();
6075#endif
6076}
6077
6078/*
6079 * Cursor up commands.
6080 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6081 */
6082 static void
6083nv_up(cap)
6084 cmdarg_T *cap;
6085{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006086 if (mod_mask & MOD_MASK_SHIFT)
6087 {
6088 /* <S-Up> is page up */
6089 cap->arg = BACKWARD;
6090 nv_page(cap);
6091 }
6092 else
6093 {
6094 cap->oap->motion_type = MLINE;
6095 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6096 clearopbeep(cap->oap);
6097 else if (cap->arg)
6098 beginline(BL_WHITE | BL_FIX);
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100}
6101
6102/*
6103 * Cursor down commands.
6104 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6105 */
6106 static void
6107nv_down(cap)
6108 cmdarg_T *cap;
6109{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006110 if (mod_mask & MOD_MASK_SHIFT)
6111 {
6112 /* <S-Down> is page down */
6113 cap->arg = FORWARD;
6114 nv_page(cap);
6115 }
6116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6118 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006119 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006120 if (curwin->w_llist_ref == NULL)
6121 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6122 else
6123 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 else
6125#endif
6126 {
6127#ifdef FEAT_CMDWIN
6128 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006129 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 cmdwin_result = CAR;
6131 else
6132#endif
6133 {
6134 cap->oap->motion_type = MLINE;
6135 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6136 clearopbeep(cap->oap);
6137 else if (cap->arg)
6138 beginline(BL_WHITE | BL_FIX);
6139 }
6140 }
6141}
6142
6143#ifdef FEAT_SEARCHPATH
6144/*
6145 * Grab the file name under the cursor and edit it.
6146 */
6147 static void
6148nv_gotofile(cap)
6149 cmdarg_T *cap;
6150{
6151 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006152 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006154 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
6156 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006157 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return;
6159 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006160#ifdef FEAT_AUTOCMD
6161 if (curbuf_locked())
6162 {
6163 clearop(cap->oap);
6164 return;
6165 }
6166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006168 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170 if (ptr != NULL)
6171 {
6172 /* do autowrite if necessary */
6173 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6174 autowrite(curbuf, FALSE);
6175 setpcmark();
6176 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006177 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006178 if (cap->nchar == 'F' && lnum >= 0)
6179 {
6180 curwin->w_cursor.lnum = lnum;
6181 check_cursor_lnum();
6182 beginline(BL_SOL | BL_FIX);
6183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 vim_free(ptr);
6185 }
6186 else
6187 clearop(cap->oap);
6188}
6189#endif
6190
6191/*
6192 * <End> command: to end of current line or last line.
6193 */
6194 static void
6195nv_end(cap)
6196 cmdarg_T *cap;
6197{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006198 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006200 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 nv_goto(cap);
6202 cap->count1 = 1; /* to end of current line */
6203 }
6204 nv_dollar(cap);
6205}
6206
6207/*
6208 * Handle the "$" command.
6209 */
6210 static void
6211nv_dollar(cap)
6212 cmdarg_T *cap;
6213{
6214 cap->oap->motion_type = MCHAR;
6215 cap->oap->inclusive = TRUE;
6216#ifdef FEAT_VIRTUALEDIT
6217 /* In virtual mode when off the edge of a line and an operator
6218 * is pending (whew!) keep the cursor where it is.
6219 * Otherwise, send it to the end of the line. */
6220 if (!virtual_active() || gchar_cursor() != NUL
6221 || cap->oap->op_type == OP_NOP)
6222#endif
6223 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6224 if (cursor_down((long)(cap->count1 - 1),
6225 cap->oap->op_type == OP_NOP) == FAIL)
6226 clearopbeep(cap->oap);
6227#ifdef FEAT_FOLDING
6228 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6229 foldOpenCursor();
6230#endif
6231}
6232
6233/*
6234 * Implementation of '?' and '/' commands.
6235 * If cap->arg is TRUE don't set PC mark.
6236 */
6237 static void
6238nv_search(cap)
6239 cmdarg_T *cap;
6240{
6241 oparg_T *oap = cap->oap;
6242
6243 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6244 {
6245 /* Translate "g??" to "g?g?" */
6246 cap->cmdchar = 'g';
6247 cap->nchar = '?';
6248 nv_operator(cap);
6249 return;
6250 }
6251
6252 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6253
6254 if (cap->searchbuf == NULL)
6255 {
6256 clearop(oap);
6257 return;
6258 }
6259
6260 normal_search(cap, cap->cmdchar, cap->searchbuf,
6261 (cap->arg ? 0 : SEARCH_MARK));
6262}
6263
6264/*
6265 * Handle "N" and "n" commands.
6266 * cap->arg is SEARCH_REV for "N", 0 for "n".
6267 */
6268 static void
6269nv_next(cap)
6270 cmdarg_T *cap;
6271{
6272 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6273}
6274
6275/*
6276 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6277 * Uses only cap->count1 and cap->oap from "cap".
6278 */
6279 static void
6280normal_search(cap, dir, pat, opt)
6281 cmdarg_T *cap;
6282 int dir;
6283 char_u *pat;
6284 int opt; /* extra flags for do_search() */
6285{
6286 int i;
6287
6288 cap->oap->motion_type = MCHAR;
6289 cap->oap->inclusive = FALSE;
6290 cap->oap->use_reg_one = TRUE;
6291 curwin->w_set_curswant = TRUE;
6292
6293 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006294 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (i == 0)
6296 clearop(cap->oap);
6297 else
6298 {
6299 if (i == 2)
6300 cap->oap->motion_type = MLINE;
6301#ifdef FEAT_VIRTUALEDIT
6302 curwin->w_cursor.coladd = 0;
6303#endif
6304#ifdef FEAT_FOLDING
6305 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6306 foldOpenCursor();
6307#endif
6308 }
6309
6310 /* "/$" will put the cursor after the end of the line, may need to
6311 * correct that here */
6312 check_cursor();
6313}
6314
6315/*
6316 * Character search commands.
6317 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6318 * ',' and FALSE for ';'.
6319 * cap->nchar is NUL for ',' and ';' (repeat the search)
6320 */
6321 static void
6322nv_csearch(cap)
6323 cmdarg_T *cap;
6324{
6325 int t_cmd;
6326
6327 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6328 t_cmd = TRUE;
6329 else
6330 t_cmd = FALSE;
6331
6332 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6334 clearopbeep(cap->oap);
6335 else
6336 {
6337 curwin->w_set_curswant = TRUE;
6338#ifdef FEAT_VIRTUALEDIT
6339 /* Include a Tab for "tx" and for "dfx". */
6340 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6341 && (t_cmd || cap->oap->op_type != OP_NOP))
6342 {
6343 colnr_T scol, ecol;
6344
6345 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6346 curwin->w_cursor.coladd = ecol - scol;
6347 }
6348 else
6349 curwin->w_cursor.coladd = 0;
6350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352#ifdef FEAT_FOLDING
6353 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6354 foldOpenCursor();
6355#endif
6356 }
6357}
6358
6359/*
6360 * "[" and "]" commands.
6361 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6362 */
6363 static void
6364nv_brackets(cap)
6365 cmdarg_T *cap;
6366{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006367 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 pos_T prev_pos;
6369 pos_T *pos = NULL; /* init for GCC */
6370 pos_T old_pos; /* cursor position before command */
6371 int flag;
6372 long n;
6373 int findc;
6374 int c;
6375
6376 cap->oap->motion_type = MCHAR;
6377 cap->oap->inclusive = FALSE;
6378 old_pos = curwin->w_cursor;
6379#ifdef FEAT_VIRTUALEDIT
6380 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6381#endif
6382
6383#ifdef FEAT_SEARCHPATH
6384 /*
6385 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6386 */
6387 if (cap->nchar == 'f')
6388 nv_gotofile(cap);
6389 else
6390#endif
6391
6392#ifdef FEAT_FIND_ID
6393 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006394 * Find the occurrence(s) of the identifier or define under cursor
6395 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 *
6397 * search list jump
6398 * fwd bwd fwd bwd fwd bwd
6399 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6400 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6401 */
6402 if (vim_strchr((char_u *)
6403#ifdef EBCDIC
6404 "iI\005dD\067",
6405#else
6406 "iI\011dD\004",
6407#endif
6408 cap->nchar) != NULL)
6409 {
6410 char_u *ptr;
6411 int len;
6412
6413 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6414 clearop(cap->oap);
6415 else
6416 {
6417 find_pattern_in_path(ptr, 0, len, TRUE,
6418 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6419 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6420 cap->count1,
6421 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6422 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6423 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6424 (linenr_T)MAXLNUM);
6425 curwin->w_set_curswant = TRUE;
6426 }
6427 }
6428 else
6429#endif
6430
6431 /*
6432 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6433 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6434 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6435 * "[m" or "]m" search for prev/next start of (Java) method.
6436 * "[M" or "]M" search for prev/next end of (Java) method.
6437 */
6438 if ( (cap->cmdchar == '['
6439 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6440 || (cap->cmdchar == ']'
6441 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6442 {
6443 if (cap->nchar == '*')
6444 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 prev_pos.lnum = 0;
6446 if (cap->nchar == 'm' || cap->nchar == 'M')
6447 {
6448 if (cap->cmdchar == '[')
6449 findc = '{';
6450 else
6451 findc = '}';
6452 n = 9999;
6453 }
6454 else
6455 {
6456 findc = cap->nchar;
6457 n = cap->count1;
6458 }
6459 for ( ; n > 0; --n)
6460 {
6461 if ((pos = findmatchlimit(cap->oap, findc,
6462 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6463 {
6464 if (new_pos.lnum == 0) /* nothing found */
6465 {
6466 if (cap->nchar != 'm' && cap->nchar != 'M')
6467 clearopbeep(cap->oap);
6468 }
6469 else
6470 pos = &new_pos; /* use last one found */
6471 break;
6472 }
6473 prev_pos = new_pos;
6474 curwin->w_cursor = *pos;
6475 new_pos = *pos;
6476 }
6477 curwin->w_cursor = old_pos;
6478
6479 /*
6480 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6481 * brought us to the match for "[m" and "]M" when inside a method.
6482 * Try finding the '{' or '}' we want to be at.
6483 * Also repeat for the given count.
6484 */
6485 if (cap->nchar == 'm' || cap->nchar == 'M')
6486 {
6487 /* norm is TRUE for "]M" and "[m" */
6488 int norm = ((findc == '{') == (cap->nchar == 'm'));
6489
6490 n = cap->count1;
6491 /* found a match: we were inside a method */
6492 if (prev_pos.lnum != 0)
6493 {
6494 pos = &prev_pos;
6495 curwin->w_cursor = prev_pos;
6496 if (norm)
6497 --n;
6498 }
6499 else
6500 pos = NULL;
6501 while (n > 0)
6502 {
6503 for (;;)
6504 {
6505 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6506 {
6507 /* if not found anything, that's an error */
6508 if (pos == NULL)
6509 clearopbeep(cap->oap);
6510 n = 0;
6511 break;
6512 }
6513 c = gchar_cursor();
6514 if (c == '{' || c == '}')
6515 {
6516 /* Must have found end/start of class: use it.
6517 * Or found the place to be at. */
6518 if ((c == findc && norm) || (n == 1 && !norm))
6519 {
6520 new_pos = curwin->w_cursor;
6521 pos = &new_pos;
6522 n = 0;
6523 }
6524 /* if no match found at all, we started outside of the
6525 * class and we're inside now. Just go on. */
6526 else if (new_pos.lnum == 0)
6527 {
6528 new_pos = curwin->w_cursor;
6529 pos = &new_pos;
6530 }
6531 /* found start/end of other method: go to match */
6532 else if ((pos = findmatchlimit(cap->oap, findc,
6533 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6534 0)) == NULL)
6535 n = 0;
6536 else
6537 curwin->w_cursor = *pos;
6538 break;
6539 }
6540 }
6541 --n;
6542 }
6543 curwin->w_cursor = old_pos;
6544 if (pos == NULL && new_pos.lnum != 0)
6545 clearopbeep(cap->oap);
6546 }
6547 if (pos != NULL)
6548 {
6549 setpcmark();
6550 curwin->w_cursor = *pos;
6551 curwin->w_set_curswant = TRUE;
6552#ifdef FEAT_FOLDING
6553 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6554 && cap->oap->op_type == OP_NOP)
6555 foldOpenCursor();
6556#endif
6557 }
6558 }
6559
6560 /*
6561 * "[[", "[]", "]]" and "][": move to start or end of function
6562 */
6563 else if (cap->nchar == '[' || cap->nchar == ']')
6564 {
6565 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6566 flag = '{';
6567 else
6568 flag = '}'; /* "][" or "[]" */
6569
6570 curwin->w_set_curswant = TRUE;
6571 /*
6572 * Imitate strange Vi behaviour: When using "]]" with an operator
6573 * we also stop at '}'.
6574 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006575 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 (cap->oap->op_type != OP_NOP
6577 && cap->arg == FORWARD && flag == '{')))
6578 clearopbeep(cap->oap);
6579 else
6580 {
6581 if (cap->oap->op_type == OP_NOP)
6582 beginline(BL_WHITE | BL_FIX);
6583#ifdef FEAT_FOLDING
6584 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6585 foldOpenCursor();
6586#endif
6587 }
6588 }
6589
6590 /*
6591 * "[p", "[P", "]P" and "]p": put with indent adjustment
6592 */
6593 else if (cap->nchar == 'p' || cap->nchar == 'P')
6594 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006595 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006597 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6598 ? FORWARD : BACKWARD;
6599 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006600 int was_visual = VIsual_active;
6601 int line_count = curbuf->b_ml.ml_line_count;
6602 pos_T start, end;
6603
6604 if (VIsual_active)
6605 {
6606 start = ltoreq(VIsual, curwin->w_cursor)
6607 ? VIsual : curwin->w_cursor;
6608 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6609 curwin->w_cursor = (dir == BACKWARD ? start : end);
6610 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006611# ifdef FEAT_CLIPBOARD
6612 adjust_clip_reg(&regname);
6613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006615
6616 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006617 if (was_visual)
6618 {
6619 VIsual = start;
6620 curwin->w_cursor = end;
6621 if (dir == BACKWARD)
6622 {
6623 /* adjust lines */
6624 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6625 curwin->w_cursor.lnum +=
6626 curbuf->b_ml.ml_line_count - line_count;
6627 }
6628
6629 VIsual_active = TRUE;
6630 if (VIsual_mode == 'V')
6631 {
6632 /* delete visually selected lines */
6633 cap->cmdchar = 'd';
6634 cap->nchar = NUL;
6635 cap->oap->regname = regname;
6636 nv_operator(cap);
6637 do_pending_operator(cap, 0, FALSE);
6638 }
6639 if (VIsual_active)
6640 {
6641 end_visual_mode();
6642 redraw_later(SOME_VALID);
6643 }
6644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646 }
6647
6648 /*
6649 * "['", "[`", "]'" and "]`": jump to next mark
6650 */
6651 else if (cap->nchar == '\'' || cap->nchar == '`')
6652 {
6653 pos = &curwin->w_cursor;
6654 for (n = cap->count1; n > 0; --n)
6655 {
6656 prev_pos = *pos;
6657 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6658 cap->nchar == '\'');
6659 if (pos == NULL)
6660 break;
6661 }
6662 if (pos == NULL)
6663 pos = &prev_pos;
6664 nv_cursormark(cap, cap->nchar == '\'', pos);
6665 }
6666
6667#ifdef FEAT_MOUSE
6668 /*
6669 * [ or ] followed by a middle mouse click: put selected text with
6670 * indent adjustment. Any other button just does as usual.
6671 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006672 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
6674 (void)do_mouse(cap->oap, cap->nchar,
6675 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6676 cap->count1, PUT_FIXINDENT);
6677 }
6678#endif /* FEAT_MOUSE */
6679
6680#ifdef FEAT_FOLDING
6681 /*
6682 * "[z" and "]z": move to start or end of open fold.
6683 */
6684 else if (cap->nchar == 'z')
6685 {
6686 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6687 cap->count1) == FAIL)
6688 clearopbeep(cap->oap);
6689 }
6690#endif
6691
6692#ifdef FEAT_DIFF
6693 /*
6694 * "[c" and "]c": move to next or previous diff-change.
6695 */
6696 else if (cap->nchar == 'c')
6697 {
6698 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6699 cap->count1) == FAIL)
6700 clearopbeep(cap->oap);
6701 }
6702#endif
6703
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006704#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006705 /*
6706 * "[s", "[S", "]s" and "]S": move to next spell error.
6707 */
6708 else if (cap->nchar == 's' || cap->nchar == 'S')
6709 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006710 setpcmark();
6711 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006712 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6713 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006714 {
6715 clearopbeep(cap->oap);
6716 break;
6717 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006718# ifdef FEAT_FOLDING
6719 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6720 foldOpenCursor();
6721# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006722 }
6723#endif
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 /* Not a valid cap->nchar. */
6726 else
6727 clearopbeep(cap->oap);
6728}
6729
6730/*
6731 * Handle Normal mode "%" command.
6732 */
6733 static void
6734nv_percent(cap)
6735 cmdarg_T *cap;
6736{
6737 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006738#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 linenr_T lnum = curwin->w_cursor.lnum;
6740#endif
6741
6742 cap->oap->inclusive = TRUE;
6743 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6744 {
6745 if (cap->count0 > 100)
6746 clearopbeep(cap->oap);
6747 else
6748 {
6749 cap->oap->motion_type = MLINE;
6750 setpcmark();
6751 /* Round up, so CTRL-G will give same value. Watch out for a
6752 * large line count, the line number must not go negative! */
6753 if (curbuf->b_ml.ml_line_count > 1000000)
6754 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6755 / 100L * cap->count0;
6756 else
6757 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6758 cap->count0 + 99L) / 100L;
6759 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6760 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6761 beginline(BL_SOL | BL_FIX);
6762 }
6763 }
6764 else /* "%" : go to matching paren */
6765 {
6766 cap->oap->motion_type = MCHAR;
6767 cap->oap->use_reg_one = TRUE;
6768 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6769 clearopbeep(cap->oap);
6770 else
6771 {
6772 setpcmark();
6773 curwin->w_cursor = *pos;
6774 curwin->w_set_curswant = TRUE;
6775#ifdef FEAT_VIRTUALEDIT
6776 curwin->w_cursor.coladd = 0;
6777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 }
6780 }
6781#ifdef FEAT_FOLDING
6782 if (cap->oap->op_type == OP_NOP
6783 && lnum != curwin->w_cursor.lnum
6784 && (fdo_flags & FDO_PERCENT)
6785 && KeyTyped)
6786 foldOpenCursor();
6787#endif
6788}
6789
6790/*
6791 * Handle "(" and ")" commands.
6792 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6793 */
6794 static void
6795nv_brace(cap)
6796 cmdarg_T *cap;
6797{
6798 cap->oap->motion_type = MCHAR;
6799 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006800 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6801 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 curwin->w_set_curswant = TRUE;
6803
6804 if (findsent(cap->arg, cap->count1) == FAIL)
6805 clearopbeep(cap->oap);
6806 else
6807 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006808 /* Don't leave the cursor on the NUL past end of line. */
6809 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810#ifdef FEAT_VIRTUALEDIT
6811 curwin->w_cursor.coladd = 0;
6812#endif
6813#ifdef FEAT_FOLDING
6814 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6815 foldOpenCursor();
6816#endif
6817 }
6818}
6819
6820/*
6821 * "m" command: Mark a position.
6822 */
6823 static void
6824nv_mark(cap)
6825 cmdarg_T *cap;
6826{
6827 if (!checkclearop(cap->oap))
6828 {
6829 if (setmark(cap->nchar) == FAIL)
6830 clearopbeep(cap->oap);
6831 }
6832}
6833
6834/*
6835 * "{" and "}" commands.
6836 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6837 */
6838 static void
6839nv_findpar(cap)
6840 cmdarg_T *cap;
6841{
6842 cap->oap->motion_type = MCHAR;
6843 cap->oap->inclusive = FALSE;
6844 cap->oap->use_reg_one = TRUE;
6845 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006846 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 clearopbeep(cap->oap);
6848 else
6849 {
6850#ifdef FEAT_VIRTUALEDIT
6851 curwin->w_cursor.coladd = 0;
6852#endif
6853#ifdef FEAT_FOLDING
6854 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6855 foldOpenCursor();
6856#endif
6857 }
6858}
6859
6860/*
6861 * "u" command: Undo or make lower case.
6862 */
6863 static void
6864nv_undo(cap)
6865 cmdarg_T *cap;
6866{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006867 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
6869 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6870 cap->cmdchar = 'g';
6871 cap->nchar = 'u';
6872 nv_operator(cap);
6873 }
6874 else
6875 nv_kundo(cap);
6876}
6877
6878/*
6879 * <Undo> command.
6880 */
6881 static void
6882nv_kundo(cap)
6883 cmdarg_T *cap;
6884{
6885 if (!checkclearopq(cap->oap))
6886 {
6887 u_undo((int)cap->count1);
6888 curwin->w_set_curswant = TRUE;
6889 }
6890}
6891
6892/*
6893 * Handle the "r" command.
6894 */
6895 static void
6896nv_replace(cap)
6897 cmdarg_T *cap;
6898{
6899 char_u *ptr;
6900 int had_ctrl_v;
6901 long n;
6902
6903 if (checkclearop(cap->oap))
6904 return;
6905
6906 /* get another character */
6907 if (cap->nchar == Ctrl_V)
6908 {
6909 had_ctrl_v = Ctrl_V;
6910 cap->nchar = get_literal();
6911 /* Don't redo a multibyte character with CTRL-V. */
6912 if (cap->nchar > DEL)
6913 had_ctrl_v = NUL;
6914 }
6915 else
6916 had_ctrl_v = NUL;
6917
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006918 /* Abort if the character is a special key. */
6919 if (IS_SPECIAL(cap->nchar))
6920 {
6921 clearopbeep(cap->oap);
6922 return;
6923 }
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /* Visual mode "r" */
6926 if (VIsual_active)
6927 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006928 if (got_int)
6929 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006930 if (had_ctrl_v)
6931 {
6932 if (cap->nchar == '\r')
6933 cap->nchar = -1;
6934 else if (cap->nchar == '\n')
6935 cap->nchar = -2;
6936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 nv_operator(cap);
6938 return;
6939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941#ifdef FEAT_VIRTUALEDIT
6942 /* Break tabs, etc. */
6943 if (virtual_active())
6944 {
6945 if (u_save_cursor() == FAIL)
6946 return;
6947 if (gchar_cursor() == NUL)
6948 {
6949 /* Add extra space and put the cursor on the first one. */
6950 coladvance_force((colnr_T)(getviscol() + cap->count1));
6951 curwin->w_cursor.col -= cap->count1;
6952 }
6953 else if (gchar_cursor() == TAB)
6954 coladvance_force(getviscol());
6955 }
6956#endif
6957
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006958 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006960 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#ifdef FEAT_MBYTE
6962 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6963#endif
6964 )
6965 {
6966 clearopbeep(cap->oap);
6967 return;
6968 }
6969
6970 /*
6971 * Replacing with a TAB is done by edit() when it is complicated because
6972 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6973 * Other characters are done below to avoid problems with things like
6974 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6975 */
6976 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6977 {
6978 stuffnumReadbuff(cap->count1);
6979 stuffcharReadbuff('R');
6980 stuffcharReadbuff('\t');
6981 stuffcharReadbuff(ESC);
6982 return;
6983 }
6984
6985 /* save line for undo */
6986 if (u_save_cursor() == FAIL)
6987 return;
6988
6989 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6990 {
6991 /*
6992 * Replace character(s) by a single newline.
6993 * Strange vi behaviour: Only one newline is inserted.
6994 * Delete the characters here.
6995 * Insert the newline with an insert command, takes care of
6996 * autoindent. The insert command depends on being on the last
6997 * character of a line or not.
6998 */
6999#ifdef FEAT_MBYTE
7000 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7001#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007002 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003#endif
7004 stuffcharReadbuff('\r');
7005 stuffcharReadbuff(ESC);
7006
7007 /* Give 'r' to edit(), to get the redo command right. */
7008 invoke_edit(cap, TRUE, 'r', FALSE);
7009 }
7010 else
7011 {
7012 prep_redo(cap->oap->regname, cap->count1,
7013 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7014
7015 curbuf->b_op_start = curwin->w_cursor;
7016#ifdef FEAT_MBYTE
7017 if (has_mbyte)
7018 {
7019 int old_State = State;
7020
7021 if (cap->ncharC1 != 0)
7022 AppendCharToRedobuff(cap->ncharC1);
7023 if (cap->ncharC2 != 0)
7024 AppendCharToRedobuff(cap->ncharC2);
7025
7026 /* This is slow, but it handles replacing a single-byte with a
7027 * multi-byte and the other way around. Also handles adding
7028 * composing characters for utf-8. */
7029 for (n = cap->count1; n > 0; --n)
7030 {
7031 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007032 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7033 {
7034 int c = ins_copychar(curwin->w_cursor.lnum
7035 + (cap->nchar == Ctrl_Y ? -1 : 1));
7036 if (c != NUL)
7037 ins_char(c);
7038 else
7039 /* will be decremented further down */
7040 ++curwin->w_cursor.col;
7041 }
7042 else
7043 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 State = old_State;
7045 if (cap->ncharC1 != 0)
7046 ins_char(cap->ncharC1);
7047 if (cap->ncharC2 != 0)
7048 ins_char(cap->ncharC2);
7049 }
7050 }
7051 else
7052#endif
7053 {
7054 /*
7055 * Replace the characters within one line.
7056 */
7057 for (n = cap->count1; n > 0; --n)
7058 {
7059 /*
7060 * Get ptr again, because u_save and/or showmatch() will have
7061 * released the line. At the same time we let know that the
7062 * line will be changed.
7063 */
7064 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007065 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7066 {
7067 int c = ins_copychar(curwin->w_cursor.lnum
7068 + (cap->nchar == Ctrl_Y ? -1 : 1));
7069 if (c != NUL)
7070 ptr[curwin->w_cursor.col] = c;
7071 }
7072 else
7073 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 if (p_sm && msg_silent == 0)
7075 showmatch(cap->nchar);
7076 ++curwin->w_cursor.col;
7077 }
7078#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007079 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007081 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
Bram Moolenaar009b2592004-10-24 19:18:58 +00007083 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007084 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007086 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088#endif
7089
7090 /* mark the buffer as changed and prepare for displaying */
7091 changed_bytes(curwin->w_cursor.lnum,
7092 (colnr_T)(curwin->w_cursor.col - cap->count1));
7093 }
7094 --curwin->w_cursor.col; /* cursor on the last replaced char */
7095#ifdef FEAT_MBYTE
7096 /* if the character on the left of the current cursor is a multi-byte
7097 * character, move two characters left */
7098 if (has_mbyte)
7099 mb_adjust_cursor();
7100#endif
7101 curbuf->b_op_end = curwin->w_cursor;
7102 curwin->w_set_curswant = TRUE;
7103 set_last_insert(cap->nchar);
7104 }
7105}
7106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107/*
7108 * 'o': Exchange start and end of Visual area.
7109 * 'O': same, but in block mode exchange left and right corners.
7110 */
7111 static void
7112v_swap_corners(cmdchar)
7113 int cmdchar;
7114{
7115 pos_T old_cursor;
7116 colnr_T left, right;
7117
7118 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7119 {
7120 old_cursor = curwin->w_cursor;
7121 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7122 curwin->w_cursor.lnum = VIsual.lnum;
7123 coladvance(left);
7124 VIsual = curwin->w_cursor;
7125
7126 curwin->w_cursor.lnum = old_cursor.lnum;
7127 curwin->w_curswant = right;
7128 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7129 * right one column */
7130 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7131 ++curwin->w_curswant;
7132 coladvance(curwin->w_curswant);
7133 if (curwin->w_cursor.col == old_cursor.col
7134#ifdef FEAT_VIRTUALEDIT
7135 && (!virtual_active()
7136 || curwin->w_cursor.coladd == old_cursor.coladd)
7137#endif
7138 )
7139 {
7140 curwin->w_cursor.lnum = VIsual.lnum;
7141 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7142 ++right;
7143 coladvance(right);
7144 VIsual = curwin->w_cursor;
7145
7146 curwin->w_cursor.lnum = old_cursor.lnum;
7147 coladvance(left);
7148 curwin->w_curswant = left;
7149 }
7150 }
7151 else
7152 {
7153 old_cursor = curwin->w_cursor;
7154 curwin->w_cursor = VIsual;
7155 VIsual = old_cursor;
7156 curwin->w_set_curswant = TRUE;
7157 }
7158}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160/*
7161 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7162 */
7163 static void
7164nv_Replace(cap)
7165 cmdarg_T *cap;
7166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (VIsual_active) /* "R" is replace lines */
7168 {
7169 cap->cmdchar = 'c';
7170 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007171 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 VIsual_mode = 'V';
7173 nv_operator(cap);
7174 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007175 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 if (!curbuf->b_p_ma)
7178 EMSG(_(e_modifiable));
7179 else
7180 {
7181#ifdef FEAT_VIRTUALEDIT
7182 if (virtual_active())
7183 coladvance(getviscol());
7184#endif
7185 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7186 }
7187 }
7188}
7189
7190#ifdef FEAT_VREPLACE
7191/*
7192 * "gr".
7193 */
7194 static void
7195nv_vreplace(cap)
7196 cmdarg_T *cap;
7197{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 if (VIsual_active)
7199 {
7200 cap->cmdchar = 'r';
7201 cap->nchar = cap->extra_char;
7202 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7203 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007204 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206 if (!curbuf->b_p_ma)
7207 EMSG(_(e_modifiable));
7208 else
7209 {
7210 if (cap->extra_char == Ctrl_V) /* get another character */
7211 cap->extra_char = get_literal();
7212 stuffcharReadbuff(cap->extra_char);
7213 stuffcharReadbuff(ESC);
7214# ifdef FEAT_VIRTUALEDIT
7215 if (virtual_active())
7216 coladvance(getviscol());
7217# endif
7218 invoke_edit(cap, TRUE, 'v', FALSE);
7219 }
7220 }
7221}
7222#endif
7223
7224/*
7225 * Swap case for "~" command, when it does not work like an operator.
7226 */
7227 static void
7228n_swapchar(cap)
7229 cmdarg_T *cap;
7230{
7231 long n;
7232 pos_T startpos;
7233 int did_change = 0;
7234#ifdef FEAT_NETBEANS_INTG
7235 pos_T pos;
7236 char_u *ptr;
7237 int count;
7238#endif
7239
7240 if (checkclearopq(cap->oap))
7241 return;
7242
7243 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7244 {
7245 clearopbeep(cap->oap);
7246 return;
7247 }
7248
7249 prep_redo_cmd(cap);
7250
7251 if (u_save_cursor() == FAIL)
7252 return;
7253
7254 startpos = curwin->w_cursor;
7255#ifdef FEAT_NETBEANS_INTG
7256 pos = startpos;
7257#endif
7258 for (n = cap->count1; n > 0; --n)
7259 {
7260 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7261 inc_cursor();
7262 if (gchar_cursor() == NUL)
7263 {
7264 if (vim_strchr(p_ww, '~') != NULL
7265 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7266 {
7267#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007268 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 if (did_change)
7271 {
7272 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007273 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007274 netbeans_removed(curbuf, pos.lnum, pos.col,
7275 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007277 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 pos.col = 0;
7280 pos.lnum++;
7281 }
7282#endif
7283 ++curwin->w_cursor.lnum;
7284 curwin->w_cursor.col = 0;
7285 if (n > 1)
7286 {
7287 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7288 break;
7289 u_clearline();
7290 }
7291 }
7292 else
7293 break;
7294 }
7295 }
7296#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007297 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 {
7299 ptr = ml_get(pos.lnum);
7300 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007301 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7302 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
7304#endif
7305
7306
7307 check_cursor();
7308 curwin->w_set_curswant = TRUE;
7309 if (did_change)
7310 {
7311 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7312 0L);
7313 curbuf->b_op_start = startpos;
7314 curbuf->b_op_end = curwin->w_cursor;
7315 if (curbuf->b_op_end.col > 0)
7316 --curbuf->b_op_end.col;
7317 }
7318}
7319
7320/*
7321 * Move cursor to mark.
7322 */
7323 static void
7324nv_cursormark(cap, flag, pos)
7325 cmdarg_T *cap;
7326 int flag;
7327 pos_T *pos;
7328{
7329 if (check_mark(pos) == FAIL)
7330 clearop(cap->oap);
7331 else
7332 {
7333 if (cap->cmdchar == '\''
7334 || cap->cmdchar == '`'
7335 || cap->cmdchar == '['
7336 || cap->cmdchar == ']')
7337 setpcmark();
7338 curwin->w_cursor = *pos;
7339 if (flag)
7340 beginline(BL_WHITE | BL_FIX);
7341 else
7342 check_cursor();
7343 }
7344 cap->oap->motion_type = flag ? MLINE : MCHAR;
7345 if (cap->cmdchar == '`')
7346 cap->oap->use_reg_one = TRUE;
7347 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7348 curwin->w_set_curswant = TRUE;
7349}
7350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351/*
7352 * Handle commands that are operators in Visual mode.
7353 */
7354 static void
7355v_visop(cap)
7356 cmdarg_T *cap;
7357{
7358 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7359
7360 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007361 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (isupper(cap->cmdchar))
7363 {
7364 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007365 {
7366 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7370 curwin->w_curswant = MAXCOL;
7371 }
7372 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7373 nv_operator(cap);
7374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376/*
7377 * "s" and "S" commands.
7378 */
7379 static void
7380nv_subst(cap)
7381 cmdarg_T *cap;
7382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7384 {
7385 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007386 {
7387 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 cap->cmdchar = 'c';
7391 nv_operator(cap);
7392 }
7393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 nv_optrans(cap);
7395}
7396
7397/*
7398 * Abbreviated commands.
7399 */
7400 static void
7401nv_abbrev(cap)
7402 cmdarg_T *cap;
7403{
7404 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7405 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7406
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 /* in Visual mode these commands are operators */
7408 if (VIsual_active)
7409 v_visop(cap);
7410 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 nv_optrans(cap);
7412}
7413
7414/*
7415 * Translate a command into another command.
7416 */
7417 static void
7418nv_optrans(cap)
7419 cmdarg_T *cap;
7420{
7421 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7422 (char_u *)"d$", (char_u *)"c$",
7423 (char_u *)"cl", (char_u *)"cc",
7424 (char_u *)"yy", (char_u *)":s\r"};
7425 static char_u *str = (char_u *)"xXDCsSY&";
7426
7427 if (!checkclearopq(cap->oap))
7428 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007429 /* In Vi "2D" doesn't delete the next line. Can't translate it
7430 * either, because "2." should also not use the count. */
7431 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7432 {
7433 cap->oap->start = curwin->w_cursor;
7434 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007435#ifdef FEAT_EVAL
7436 set_op_var(OP_DELETE);
7437#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007438 cap->count1 = 1;
7439 nv_dollar(cap);
7440 finish_op = TRUE;
7441 ResetRedobuff();
7442 AppendCharToRedobuff('D');
7443 }
7444 else
7445 {
7446 if (cap->count0)
7447 stuffnumReadbuff(cap->count0);
7448 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 cap->opcount = 0;
7452}
7453
7454/*
7455 * "'" and "`" commands. Also for "g'" and "g`".
7456 * cap->arg is TRUE for "'" and "g'".
7457 */
7458 static void
7459nv_gomark(cap)
7460 cmdarg_T *cap;
7461{
7462 pos_T *pos;
7463 int c;
7464#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007465 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7467#endif
7468
7469 if (cap->cmdchar == 'g')
7470 c = cap->extra_char;
7471 else
7472 c = cap->nchar;
7473 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7474 if (pos == (pos_T *)-1) /* jumped to other file */
7475 {
7476 if (cap->arg)
7477 {
7478 check_cursor_lnum();
7479 beginline(BL_WHITE | BL_FIX);
7480 }
7481 else
7482 check_cursor();
7483 }
7484 else
7485 nv_cursormark(cap, cap->arg, pos);
7486
7487#ifdef FEAT_VIRTUALEDIT
7488 /* May need to clear the coladd that a mark includes. */
7489 if (!virtual_active())
7490 curwin->w_cursor.coladd = 0;
7491#endif
7492#ifdef FEAT_FOLDING
7493 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007494 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007495 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 && (fdo_flags & FDO_MARK)
7497 && old_KeyTyped)
7498 foldOpenCursor();
7499#endif
7500}
7501
7502/*
7503 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7504 */
7505 static void
7506nv_pcmark(cap)
7507 cmdarg_T *cap;
7508{
7509#ifdef FEAT_JUMPLIST
7510 pos_T *pos;
7511# ifdef FEAT_FOLDING
7512 linenr_T lnum = curwin->w_cursor.lnum;
7513 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7514# endif
7515
7516 if (!checkclearopq(cap->oap))
7517 {
7518 if (cap->cmdchar == 'g')
7519 pos = movechangelist((int)cap->count1);
7520 else
7521 pos = movemark((int)cap->count1);
7522 if (pos == (pos_T *)-1) /* jump to other file */
7523 {
7524 curwin->w_set_curswant = TRUE;
7525 check_cursor();
7526 }
7527 else if (pos != NULL) /* can jump */
7528 nv_cursormark(cap, FALSE, pos);
7529 else if (cap->cmdchar == 'g')
7530 {
7531 if (curbuf->b_changelistlen == 0)
7532 EMSG(_("E664: changelist is empty"));
7533 else if (cap->count1 < 0)
7534 EMSG(_("E662: At start of changelist"));
7535 else
7536 EMSG(_("E663: At end of changelist"));
7537 }
7538 else
7539 clearopbeep(cap->oap);
7540# ifdef FEAT_FOLDING
7541 if (cap->oap->op_type == OP_NOP
7542 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7543 && (fdo_flags & FDO_MARK)
7544 && old_KeyTyped)
7545 foldOpenCursor();
7546# endif
7547 }
7548#else
7549 clearopbeep(cap->oap);
7550#endif
7551}
7552
7553/*
7554 * Handle '"' command.
7555 */
7556 static void
7557nv_regname(cap)
7558 cmdarg_T *cap;
7559{
7560 if (checkclearop(cap->oap))
7561 return;
7562#ifdef FEAT_EVAL
7563 if (cap->nchar == '=')
7564 cap->nchar = get_expr_register();
7565#endif
7566 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7567 {
7568 cap->oap->regname = cap->nchar;
7569 cap->opcount = cap->count0; /* remember count before '"' */
7570#ifdef FEAT_EVAL
7571 set_reg_var(cap->oap->regname);
7572#endif
7573 }
7574 else
7575 clearopbeep(cap->oap);
7576}
7577
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578/*
7579 * Handle "v", "V" and "CTRL-V" commands.
7580 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7581 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007582 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 */
7584 static void
7585nv_visual(cap)
7586 cmdarg_T *cap;
7587{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007588 if (cap->cmdchar == Ctrl_Q)
7589 cap->cmdchar = Ctrl_V;
7590
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7592 * characterwise, linewise, or blockwise. */
7593 if (cap->oap->op_type != OP_NOP)
7594 {
7595 cap->oap->motion_force = cap->cmdchar;
7596 finish_op = FALSE; /* operator doesn't finish now but later */
7597 return;
7598 }
7599
7600 VIsual_select = cap->arg;
7601 if (VIsual_active) /* change Visual mode */
7602 {
7603 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7604 end_visual_mode();
7605 else /* toggle char/block mode */
7606 { /* or char/line mode */
7607 VIsual_mode = cap->cmdchar;
7608 showmode();
7609 }
7610 redraw_curbuf_later(INVERTED); /* update the inversion */
7611 }
7612 else /* start Visual mode */
7613 {
7614 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007615 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007617 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 VIsual = curwin->w_cursor;
7619
7620 VIsual_active = TRUE;
7621 VIsual_reselect = TRUE;
7622 if (!cap->arg)
7623 /* start Select mode when 'selectmode' contains "cmd" */
7624 may_start_select('c');
7625#ifdef FEAT_MOUSE
7626 setmouse();
7627#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007628 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 redraw_cmdline = TRUE; /* show visual mode later */
7630 /*
7631 * For V and ^V, we multiply the number of lines even if there
7632 * was only one -- webb
7633 */
7634 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7635 {
7636 curwin->w_cursor.lnum +=
7637 resel_VIsual_line_count * cap->count0 - 1;
7638 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7640 }
7641 VIsual_mode = resel_VIsual_mode;
7642 if (VIsual_mode == 'v')
7643 {
7644 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007645 {
7646 validate_virtcol();
7647 curwin->w_curswant = curwin->w_virtcol
7648 + resel_VIsual_vcol * cap->count0 - 1;
7649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007651 curwin->w_curswant = resel_VIsual_vcol;
7652 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007654 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656 curwin->w_curswant = MAXCOL;
7657 coladvance((colnr_T)MAXCOL);
7658 }
7659 else if (VIsual_mode == Ctrl_V)
7660 {
7661 validate_virtcol();
7662 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007663 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 coladvance(curwin->w_curswant);
7665 }
7666 else
7667 curwin->w_set_curswant = TRUE;
7668 redraw_curbuf_later(INVERTED); /* show the inversion */
7669 }
7670 else
7671 {
7672 if (!cap->arg)
7673 /* start Select mode when 'selectmode' contains "cmd" */
7674 may_start_select('c');
7675 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007676 if (VIsual_mode != 'V' && *p_sel == 'e')
7677 ++cap->count1; /* include one more char */
7678 if (cap->count0 > 0 && --cap->count1 > 0)
7679 {
7680 /* With a count select that many characters or lines. */
7681 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7682 nv_right(cap);
7683 else if (VIsual_mode == 'V')
7684 nv_down(cap);
7685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 }
7688}
7689
7690/*
7691 * Start selection for Shift-movement keys.
7692 */
7693 void
7694start_selection()
7695{
7696 /* if 'selectmode' contains "key", start Select mode */
7697 may_start_select('k');
7698 n_start_visual_mode('v');
7699}
7700
7701/*
7702 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7703 */
7704 void
7705may_start_select(c)
7706 int c;
7707{
7708 VIsual_select = (stuff_empty() && typebuf_typed()
7709 && (vim_strchr(p_slm, c) != NULL));
7710}
7711
7712/*
7713 * Start Visual mode "c".
7714 * Should set VIsual_select before calling this.
7715 */
7716 static void
7717n_start_visual_mode(c)
7718 int c;
7719{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007720#ifdef FEAT_CONCEAL
7721 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007722 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007723#endif
7724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 VIsual_mode = c;
7726 VIsual_active = TRUE;
7727 VIsual_reselect = TRUE;
7728#ifdef FEAT_VIRTUALEDIT
7729 /* Corner case: the 0 position in a tab may change when going into
7730 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7731 */
7732 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007733 {
7734 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737#endif
7738 VIsual = curwin->w_cursor;
7739
7740#ifdef FEAT_FOLDING
7741 foldAdjustVisual();
7742#endif
7743
7744#ifdef FEAT_MOUSE
7745 setmouse();
7746#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007747#ifdef FEAT_CONCEAL
7748 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007749 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007750#endif
7751
Bram Moolenaar09df3122006-01-23 22:23:09 +00007752 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 redraw_cmdline = TRUE; /* show visual mode later */
7754#ifdef FEAT_CLIPBOARD
7755 /* Make sure the clipboard gets updated. Needed because start and
7756 * end may still be the same, and the selection needs to be owned */
7757 clip_star.vmode = NUL;
7758#endif
7759
7760 /* Only need to redraw this line, unless still need to redraw an old
7761 * Visual area (when 'lazyredraw' is set). */
7762 if (curwin->w_redr_type < INVERTED)
7763 {
7764 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7765 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7766 }
7767}
7768
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
7770/*
7771 * CTRL-W: Window commands
7772 */
7773 static void
7774nv_window(cap)
7775 cmdarg_T *cap;
7776{
7777#ifdef FEAT_WINDOWS
7778 if (!checkclearop(cap->oap))
7779 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7780#else
7781 (void)checkclearop(cap->oap);
7782#endif
7783}
7784
7785/*
7786 * CTRL-Z: Suspend
7787 */
7788 static void
7789nv_suspend(cap)
7790 cmdarg_T *cap;
7791{
7792 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 if (VIsual_active)
7794 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 do_cmdline_cmd((char_u *)"st");
7796}
7797
7798/*
7799 * Commands starting with "g".
7800 */
7801 static void
7802nv_g_cmd(cap)
7803 cmdarg_T *cap;
7804{
7805 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int i;
7808 int flag = FALSE;
7809
7810 switch (cap->nchar)
7811 {
7812#ifdef MEM_PROFILE
7813 /*
7814 * "g^A": dump log of used memory.
7815 */
7816 case Ctrl_A:
7817 vim_mem_profile_dump();
7818 break;
7819#endif
7820
7821#ifdef FEAT_VREPLACE
7822 /*
7823 * "gR": Enter virtual replace mode.
7824 */
7825 case 'R':
7826 cap->arg = TRUE;
7827 nv_Replace(cap);
7828 break;
7829
7830 case 'r':
7831 nv_vreplace(cap);
7832 break;
7833#endif
7834
7835 case '&':
7836 do_cmdline_cmd((char_u *)"%s//~/&");
7837 break;
7838
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 /*
7840 * "gv": Reselect the previous Visual area. If Visual already active,
7841 * exchange previous and current Visual area.
7842 */
7843 case 'v':
7844 if (checkclearop(oap))
7845 break;
7846
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007847 if ( curbuf->b_visual.vi_start.lnum == 0
7848 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7849 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 beep_flush();
7851 else
7852 {
7853 /* set w_cursor to the start of the Visual area, tpos to the end */
7854 if (VIsual_active)
7855 {
7856 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007857 VIsual_mode = curbuf->b_visual.vi_mode;
7858 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859# ifdef FEAT_EVAL
7860 curbuf->b_visual_mode_eval = i;
7861# endif
7862 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007863 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7864 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007866 tpos = curbuf->b_visual.vi_end;
7867 curbuf->b_visual.vi_end = curwin->w_cursor;
7868 curwin->w_cursor = curbuf->b_visual.vi_start;
7869 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 else
7872 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007873 VIsual_mode = curbuf->b_visual.vi_mode;
7874 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7875 tpos = curbuf->b_visual.vi_end;
7876 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878
7879 VIsual_active = TRUE;
7880 VIsual_reselect = TRUE;
7881
7882 /* Set Visual to the start and w_cursor to the end of the Visual
7883 * area. Make sure they are on an existing character. */
7884 check_cursor();
7885 VIsual = curwin->w_cursor;
7886 curwin->w_cursor = tpos;
7887 check_cursor();
7888 update_topline();
7889 /*
7890 * When called from normal "g" command: start Select mode when
7891 * 'selectmode' contains "cmd". When called for K_SELECT, always
7892 * start Select mode.
7893 */
7894 if (cap->arg)
7895 VIsual_select = TRUE;
7896 else
7897 may_start_select('c');
7898#ifdef FEAT_MOUSE
7899 setmouse();
7900#endif
7901#ifdef FEAT_CLIPBOARD
7902 /* Make sure the clipboard gets updated. Needed because start and
7903 * end are still the same, and the selection needs to be owned */
7904 clip_star.vmode = NUL;
7905#endif
7906 redraw_curbuf_later(INVERTED);
7907 showmode();
7908 }
7909 break;
7910 /*
7911 * "gV": Don't reselect the previous Visual area after a Select mode
7912 * mapping of menu.
7913 */
7914 case 'V':
7915 VIsual_reselect = FALSE;
7916 break;
7917
7918 /*
7919 * "gh": start Select mode.
7920 * "gH": start Select line mode.
7921 * "g^H": start Select block mode.
7922 */
7923 case K_BS:
7924 cap->nchar = Ctrl_H;
7925 /* FALLTHROUGH */
7926 case 'h':
7927 case 'H':
7928 case Ctrl_H:
7929# ifdef EBCDIC
7930 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7931 if (cap->nchar == Ctrl_H)
7932 cap->cmdchar = Ctrl_V;
7933 else
7934# endif
7935 cap->cmdchar = cap->nchar + ('v' - 'h');
7936 cap->arg = TRUE;
7937 nv_visual(cap);
7938 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007939
7940 /* "gn", "gN" visually select next/previous search match
7941 * "gn" selects next match
7942 * "gN" selects previous match
7943 */
7944 case 'N':
7945 case 'n':
7946 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007947 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
7950 /*
7951 * "gj" and "gk" two new funny movement keys -- up and down
7952 * movement based on *screen* line rather than *file* line.
7953 */
7954 case 'j':
7955 case K_DOWN:
7956 /* with 'nowrap' it works just like the normal "j" command; also when
7957 * in a closed fold */
7958 if (!curwin->w_p_wrap
7959#ifdef FEAT_FOLDING
7960 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7961#endif
7962 )
7963 {
7964 oap->motion_type = MLINE;
7965 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7966 }
7967 else
7968 i = nv_screengo(oap, FORWARD, cap->count1);
7969 if (i == FAIL)
7970 clearopbeep(oap);
7971 break;
7972
7973 case 'k':
7974 case K_UP:
7975 /* with 'nowrap' it works just like the normal "k" command; also when
7976 * in a closed fold */
7977 if (!curwin->w_p_wrap
7978#ifdef FEAT_FOLDING
7979 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7980#endif
7981 )
7982 {
7983 oap->motion_type = MLINE;
7984 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7985 }
7986 else
7987 i = nv_screengo(oap, BACKWARD, cap->count1);
7988 if (i == FAIL)
7989 clearopbeep(oap);
7990 break;
7991
7992 /*
7993 * "gJ": join two lines without inserting a space.
7994 */
7995 case 'J':
7996 nv_join(cap);
7997 break;
7998
7999 /*
8000 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8001 * "gm": middle of "g0" and "g$".
8002 */
8003 case '^':
8004 flag = TRUE;
8005 /* FALLTHROUGH */
8006
8007 case '0':
8008 case 'm':
8009 case K_HOME:
8010 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 oap->motion_type = MCHAR;
8012 oap->inclusive = FALSE;
8013 if (curwin->w_p_wrap
8014#ifdef FEAT_VERTSPLIT
8015 && curwin->w_width != 0
8016#endif
8017 )
8018 {
8019 int width1 = W_WIDTH(curwin) - curwin_col_off();
8020 int width2 = width1 + curwin_col_off2();
8021
8022 validate_virtcol();
8023 i = 0;
8024 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8025 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8026 }
8027 else
8028 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008029 /* Go to the middle of the screen line. When 'number' or
8030 * 'relativenumber' is on and lines are wrapping the middle can be more
8031 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (cap->nchar == 'm')
8033 i += (W_WIDTH(curwin) - curwin_col_off()
8034 + ((curwin->w_p_wrap && i > 0)
8035 ? curwin_col_off2() : 0)) / 2;
8036 coladvance((colnr_T)i);
8037 if (flag)
8038 {
8039 do
8040 i = gchar_cursor();
8041 while (vim_iswhite(i) && oneright() == OK);
8042 }
8043 curwin->w_set_curswant = TRUE;
8044 break;
8045
8046 case '_':
8047 /* "g_": to the last non-blank character in the line or <count> lines
8048 * downward. */
8049 cap->oap->motion_type = MCHAR;
8050 cap->oap->inclusive = TRUE;
8051 curwin->w_curswant = MAXCOL;
8052 if (cursor_down((long)(cap->count1 - 1),
8053 cap->oap->op_type == OP_NOP) == FAIL)
8054 clearopbeep(cap->oap);
8055 else
8056 {
8057 char_u *ptr = ml_get_curline();
8058
8059 /* In Visual mode we may end up after the line. */
8060 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8061 --curwin->w_cursor.col;
8062
8063 /* Decrease the cursor column until it's on a non-blank. */
8064 while (curwin->w_cursor.col > 0
8065 && vim_iswhite(ptr[curwin->w_cursor.col]))
8066 --curwin->w_cursor.col;
8067 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008068 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 }
8070 break;
8071
8072 case '$':
8073 case K_END:
8074 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 {
8076 int col_off = curwin_col_off();
8077
8078 oap->motion_type = MCHAR;
8079 oap->inclusive = TRUE;
8080 if (curwin->w_p_wrap
8081#ifdef FEAT_VERTSPLIT
8082 && curwin->w_width != 0
8083#endif
8084 )
8085 {
8086 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8087 if (cap->count1 == 1)
8088 {
8089 int width1 = W_WIDTH(curwin) - col_off;
8090 int width2 = width1 + curwin_col_off2();
8091
8092 validate_virtcol();
8093 i = width1 - 1;
8094 if (curwin->w_virtcol >= (colnr_T)width1)
8095 i += ((curwin->w_virtcol - width1) / width2 + 1)
8096 * width2;
8097 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008098
8099 /* Make sure we stick in this column. */
8100 validate_virtcol();
8101 curwin->w_curswant = curwin->w_virtcol;
8102 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8104 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8105 {
8106 /*
8107 * Check for landing on a character that got split at
8108 * the end of the line. We do not want to advance to
8109 * the next screen line.
8110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (curwin->w_virtcol > (colnr_T)i)
8112 --curwin->w_cursor.col;
8113 }
8114#endif
8115 }
8116 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8117 clearopbeep(oap);
8118 }
8119 else
8120 {
8121 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8122 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008123
8124 /* Make sure we stick in this column. */
8125 validate_virtcol();
8126 curwin->w_curswant = curwin->w_virtcol;
8127 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129 }
8130 break;
8131
8132 /*
8133 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8134 */
8135 case '*':
8136 case '#':
8137#if POUND != '#'
8138 case POUND: /* pound sign (sometimes equal to '#') */
8139#endif
8140 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8141 case ']': /* :tselect for current identifier */
8142 nv_ident(cap);
8143 break;
8144
8145 /*
8146 * ge and gE: go back to end of word
8147 */
8148 case 'e':
8149 case 'E':
8150 oap->motion_type = MCHAR;
8151 curwin->w_set_curswant = TRUE;
8152 oap->inclusive = TRUE;
8153 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8154 clearopbeep(oap);
8155 break;
8156
8157 /*
8158 * "g CTRL-G": display info about cursor position
8159 */
8160 case Ctrl_G:
8161 cursor_pos_info();
8162 break;
8163
8164 /*
8165 * "gi": start Insert at the last position.
8166 */
8167 case 'i':
8168 if (curbuf->b_last_insert.lnum != 0)
8169 {
8170 curwin->w_cursor = curbuf->b_last_insert;
8171 check_cursor_lnum();
8172 i = (int)STRLEN(ml_get_curline());
8173 if (curwin->w_cursor.col > (colnr_T)i)
8174 {
8175#ifdef FEAT_VIRTUALEDIT
8176 if (virtual_active())
8177 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8178#endif
8179 curwin->w_cursor.col = i;
8180 }
8181 }
8182 cap->cmdchar = 'i';
8183 nv_edit(cap);
8184 break;
8185
8186 /*
8187 * "gI": Start insert in column 1.
8188 */
8189 case 'I':
8190 beginline(0);
8191 if (!checkclearopq(oap))
8192 invoke_edit(cap, FALSE, 'g', FALSE);
8193 break;
8194
8195#ifdef FEAT_SEARCHPATH
8196 /*
8197 * "gf": goto file, edit file under cursor
8198 * "]f" and "[f": can also be used.
8199 */
8200 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008201 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 nv_gotofile(cap);
8203 break;
8204#endif
8205
8206 /* "g'm" and "g`m": jump to mark without setting pcmark */
8207 case '\'':
8208 cap->arg = TRUE;
8209 /*FALLTHROUGH*/
8210 case '`':
8211 nv_gomark(cap);
8212 break;
8213
8214 /*
8215 * "gs": Goto sleep.
8216 */
8217 case 's':
8218 do_sleep(cap->count1 * 1000L);
8219 break;
8220
8221 /*
8222 * "ga": Display the ascii value of the character under the
8223 * cursor. It is displayed in decimal, hex, and octal. -- webb
8224 */
8225 case 'a':
8226 do_ascii(NULL);
8227 break;
8228
8229#ifdef FEAT_MBYTE
8230 /*
8231 * "g8": Display the bytes used for the UTF-8 character under the
8232 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008233 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 */
8235 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008236 if (cap->count0 == 8)
8237 utf_find_illegal();
8238 else
8239 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 break;
8241#endif
8242
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008243 case '<':
8244 show_sb_text();
8245 break;
8246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 /*
8248 * "gg": Goto the first line in file. With a count it goes to
8249 * that line number like for "G". -- webb
8250 */
8251 case 'g':
8252 cap->arg = FALSE;
8253 nv_goto(cap);
8254 break;
8255
8256 /*
8257 * Two-character operators:
8258 * "gq" Format text
8259 * "gw" Format text and keep cursor position
8260 * "g~" Toggle the case of the text.
8261 * "gu" Change text to lower case.
8262 * "gU" Change text to upper case.
8263 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008264 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 */
8266 case 'q':
8267 case 'w':
8268 oap->cursor_start = curwin->w_cursor;
8269 /*FALLTHROUGH*/
8270 case '~':
8271 case 'u':
8272 case 'U':
8273 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008274 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 nv_operator(cap);
8276 break;
8277
8278 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008279 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 * current function
8281 * "gD": idem, but in the current file.
8282 */
8283 case 'd':
8284 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008285 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 break;
8287
8288#ifdef FEAT_MOUSE
8289 /*
8290 * g<*Mouse> : <C-*mouse>
8291 */
8292 case K_MIDDLEMOUSE:
8293 case K_MIDDLEDRAG:
8294 case K_MIDDLERELEASE:
8295 case K_LEFTMOUSE:
8296 case K_LEFTDRAG:
8297 case K_LEFTRELEASE:
8298 case K_RIGHTMOUSE:
8299 case K_RIGHTDRAG:
8300 case K_RIGHTRELEASE:
8301 case K_X1MOUSE:
8302 case K_X1DRAG:
8303 case K_X1RELEASE:
8304 case K_X2MOUSE:
8305 case K_X2DRAG:
8306 case K_X2RELEASE:
8307 mod_mask = MOD_MASK_CTRL;
8308 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8309 break;
8310#endif
8311
8312 case K_IGNORE:
8313 break;
8314
8315 /*
8316 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8317 */
8318 case 'p':
8319 case 'P':
8320 nv_put(cap);
8321 break;
8322
8323#ifdef FEAT_BYTEOFF
8324 /* "go": goto byte count from start of buffer */
8325 case 'o':
8326 goto_byte(cap->count0);
8327 break;
8328#endif
8329
8330 /* "gQ": improved Ex mode */
8331 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008332 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008335 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 break;
8337 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 if (!checkclearopq(oap))
8340 do_exmode(TRUE);
8341 break;
8342
8343#ifdef FEAT_JUMPLIST
8344 case ',':
8345 nv_pcmark(cap);
8346 break;
8347
8348 case ';':
8349 cap->count1 = -cap->count1;
8350 nv_pcmark(cap);
8351 break;
8352#endif
8353
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008354#ifdef FEAT_WINDOWS
8355 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008356 if (!checkclearop(oap))
8357 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008358 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008359 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008360 if (!checkclearop(oap))
8361 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008362 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008363#endif
8364
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008365 case '+':
8366 case '-': /* "g+" and "g-": undo or redo along the timeline */
8367 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008368 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008369 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008370 break;
8371
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 default:
8373 clearopbeep(oap);
8374 break;
8375 }
8376}
8377
8378/*
8379 * Handle "o" and "O" commands.
8380 */
8381 static void
8382n_opencmd(cap)
8383 cmdarg_T *cap;
8384{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008385#ifdef FEAT_CONCEAL
8386 linenr_T oldline = curwin->w_cursor.lnum;
8387#endif
8388
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (!checkclearopq(cap->oap))
8390 {
8391#ifdef FEAT_FOLDING
8392 if (cap->cmdchar == 'O')
8393 /* Open above the first line of a folded sequence of lines */
8394 (void)hasFolding(curwin->w_cursor.lnum,
8395 &curwin->w_cursor.lnum, NULL);
8396 else
8397 /* Open below the last line of a folded sequence of lines */
8398 (void)hasFolding(curwin->w_cursor.lnum,
8399 NULL, &curwin->w_cursor.lnum);
8400#endif
8401 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8402 (cap->cmdchar == 'O' ? 1 : 0)),
8403 (linenr_T)(curwin->w_cursor.lnum +
8404 (cap->cmdchar == 'o' ? 1 : 0))
8405 ) == OK
8406 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8407#ifdef FEAT_COMMENTS
8408 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8409#endif
8410 0, 0))
8411 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008412#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008413 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008414 update_single_line(curwin, oldline);
8415#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008416 /* When '#' is in 'cpoptions' ignore the count. */
8417 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8418 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8420 }
8421 }
8422}
8423
8424/*
8425 * "." command: redo last change.
8426 */
8427 static void
8428nv_dot(cap)
8429 cmdarg_T *cap;
8430{
8431 if (!checkclearopq(cap->oap))
8432 {
8433 /*
8434 * If "restart_edit" is TRUE, the last but one command is repeated
8435 * instead of the last command (inserting text). This is used for
8436 * CTRL-O <.> in insert mode.
8437 */
8438 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8439 clearopbeep(cap->oap);
8440 }
8441}
8442
8443/*
8444 * CTRL-R: undo undo
8445 */
8446 static void
8447nv_redo(cap)
8448 cmdarg_T *cap;
8449{
8450 if (!checkclearopq(cap->oap))
8451 {
8452 u_redo((int)cap->count1);
8453 curwin->w_set_curswant = TRUE;
8454 }
8455}
8456
8457/*
8458 * Handle "U" command.
8459 */
8460 static void
8461nv_Undo(cap)
8462 cmdarg_T *cap;
8463{
8464 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008465 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {
8467 /* translate "gUU" to "gUgU" */
8468 cap->cmdchar = 'g';
8469 cap->nchar = 'U';
8470 nv_operator(cap);
8471 }
8472 else if (!checkclearopq(cap->oap))
8473 {
8474 u_undoline();
8475 curwin->w_set_curswant = TRUE;
8476 }
8477}
8478
8479/*
8480 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8481 * single character.
8482 */
8483 static void
8484nv_tilde(cap)
8485 cmdarg_T *cap;
8486{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008487 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 n_swapchar(cap);
8489 else
8490 nv_operator(cap);
8491}
8492
8493/*
8494 * Handle an operator command.
8495 * The actual work is done by do_pending_operator().
8496 */
8497 static void
8498nv_operator(cap)
8499 cmdarg_T *cap;
8500{
8501 int op_type;
8502
8503 op_type = get_op_type(cap->cmdchar, cap->nchar);
8504
8505 if (op_type == cap->oap->op_type) /* double operator works on lines */
8506 nv_lineop(cap);
8507 else if (!checkclearop(cap->oap))
8508 {
8509 cap->oap->start = curwin->w_cursor;
8510 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008511#ifdef FEAT_EVAL
8512 set_op_var(op_type);
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 }
8515}
8516
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008517#ifdef FEAT_EVAL
8518/*
8519 * Set v:operator to the characters for "optype".
8520 */
8521 static void
8522set_op_var(optype)
8523 int optype;
8524{
8525 char_u opchars[3];
8526
8527 if (optype == OP_NOP)
8528 set_vim_var_string(VV_OP, NULL, 0);
8529 else
8530 {
8531 opchars[0] = get_op_char(optype);
8532 opchars[1] = get_extra_op_char(optype);
8533 opchars[2] = NUL;
8534 set_vim_var_string(VV_OP, opchars, -1);
8535 }
8536}
8537#endif
8538
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539/*
8540 * Handle linewise operator "dd", "yy", etc.
8541 *
8542 * "_" is is a strange motion command that helps make operators more logical.
8543 * It is actually implemented, but not documented in the real Vi. This motion
8544 * command actually refers to "the current line". Commands like "dd" and "yy"
8545 * are really an alternate form of "d_" and "y_". It does accept a count, so
8546 * "d3_" works to delete 3 lines.
8547 */
8548 static void
8549nv_lineop(cap)
8550 cmdarg_T *cap;
8551{
8552 cap->oap->motion_type = MLINE;
8553 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8554 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008555 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8556 && cap->oap->motion_force != 'v'
8557 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 || cap->oap->op_type == OP_LSHIFT
8559 || cap->oap->op_type == OP_RSHIFT)
8560 beginline(BL_SOL | BL_FIX);
8561 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8562 beginline(BL_WHITE | BL_FIX);
8563}
8564
8565/*
8566 * <Home> command.
8567 */
8568 static void
8569nv_home(cap)
8570 cmdarg_T *cap;
8571{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008572 /* CTRL-HOME is like "gg" */
8573 if (mod_mask & MOD_MASK_CTRL)
8574 nv_goto(cap);
8575 else
8576 {
8577 cap->count0 = 1;
8578 nv_pipe(cap);
8579 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008580 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8581 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582}
8583
8584/*
8585 * "|" command.
8586 */
8587 static void
8588nv_pipe(cap)
8589 cmdarg_T *cap;
8590{
8591 cap->oap->motion_type = MCHAR;
8592 cap->oap->inclusive = FALSE;
8593 beginline(0);
8594 if (cap->count0 > 0)
8595 {
8596 coladvance((colnr_T)(cap->count0 - 1));
8597 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8598 }
8599 else
8600 curwin->w_curswant = 0;
8601 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008602 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 curwin->w_set_curswant = FALSE;
8604}
8605
8606/*
8607 * Handle back-word command "b" and "B".
8608 * cap->arg is 1 for "B"
8609 */
8610 static void
8611nv_bck_word(cap)
8612 cmdarg_T *cap;
8613{
8614 cap->oap->motion_type = MCHAR;
8615 cap->oap->inclusive = FALSE;
8616 curwin->w_set_curswant = TRUE;
8617 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8618 clearopbeep(cap->oap);
8619#ifdef FEAT_FOLDING
8620 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8621 foldOpenCursor();
8622#endif
8623}
8624
8625/*
8626 * Handle word motion commands "e", "E", "w" and "W".
8627 * cap->arg is TRUE for "E" and "W".
8628 */
8629 static void
8630nv_wordcmd(cap)
8631 cmdarg_T *cap;
8632{
8633 int n;
8634 int word_end;
8635 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008636 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638 /*
8639 * Set inclusive for the "E" and "e" command.
8640 */
8641 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8642 word_end = TRUE;
8643 else
8644 word_end = FALSE;
8645 cap->oap->inclusive = word_end;
8646
8647 /*
8648 * "cw" and "cW" are a special case.
8649 */
8650 if (!word_end && cap->oap->op_type == OP_CHANGE)
8651 {
8652 n = gchar_cursor();
8653 if (n != NUL) /* not an empty line */
8654 {
8655 if (vim_iswhite(n))
8656 {
8657 /*
8658 * Reproduce a funny Vi behaviour: "cw" on a blank only
8659 * changes one character, not all blanks until the start of
8660 * the next word. Only do this when the 'w' flag is included
8661 * in 'cpoptions'.
8662 */
8663 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8664 {
8665 cap->oap->inclusive = TRUE;
8666 cap->oap->motion_type = MCHAR;
8667 return;
8668 }
8669 }
8670 else
8671 {
8672 /*
8673 * This is a little strange. To match what the real Vi does,
8674 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8675 * that we are not on a space or a TAB. This seems impolite
8676 * at first, but it's really more what we mean when we say
8677 * 'cw'.
8678 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008679 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 * will change only one character! This is done by setting
8681 * flag.
8682 */
8683 cap->oap->inclusive = TRUE;
8684 word_end = TRUE;
8685 flag = TRUE;
8686 }
8687 }
8688 }
8689
8690 cap->oap->motion_type = MCHAR;
8691 curwin->w_set_curswant = TRUE;
8692 if (word_end)
8693 n = end_word(cap->count1, cap->arg, flag, FALSE);
8694 else
8695 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8696
Bram Moolenaardfefb982008-04-01 10:06:39 +00008697 /* Don't leave the cursor on the NUL past the end of line. Unless we
8698 * didn't move it forward. */
8699 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008700 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702 if (n == FAIL && cap->oap->op_type == OP_NOP)
8703 clearopbeep(cap->oap);
8704 else
8705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707#ifdef FEAT_FOLDING
8708 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8709 foldOpenCursor();
8710#endif
8711 }
8712}
8713
8714/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008715 * Used after a movement command: If the cursor ends up on the NUL after the
8716 * end of the line, may move it back to the last character and make the motion
8717 * inclusive.
8718 */
8719 static void
8720adjust_cursor(oap)
8721 oparg_T *oap;
8722{
8723 /* The cursor cannot remain on the NUL when:
8724 * - the column is > 0
8725 * - not in Visual mode or 'selection' is "o"
8726 * - 'virtualedit' is not "all" and not "onemore".
8727 */
8728 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008729 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008730#ifdef FEAT_VIRTUALEDIT
8731 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8732#endif
8733 )
8734 {
8735 --curwin->w_cursor.col;
8736#ifdef FEAT_MBYTE
8737 /* prevent cursor from moving on the trail byte */
8738 if (has_mbyte)
8739 mb_adjust_cursor();
8740#endif
8741 oap->inclusive = TRUE;
8742 }
8743}
8744
8745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 * "0" and "^" commands.
8747 * cap->arg is the argument for beginline().
8748 */
8749 static void
8750nv_beginline(cap)
8751 cmdarg_T *cap;
8752{
8753 cap->oap->motion_type = MCHAR;
8754 cap->oap->inclusive = FALSE;
8755 beginline(cap->arg);
8756#ifdef FEAT_FOLDING
8757 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8758 foldOpenCursor();
8759#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008760 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8761 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762}
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764/*
8765 * In exclusive Visual mode, may include the last character.
8766 */
8767 static void
8768adjust_for_sel(cap)
8769 cmdarg_T *cap;
8770{
8771 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8772 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8773 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008774#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (has_mbyte)
8776 inc_cursor();
8777 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 ++curwin->w_cursor.col;
8780 cap->oap->inclusive = FALSE;
8781 }
8782}
8783
8784/*
8785 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8786 * Should check VIsual_mode before calling this.
8787 * Returns TRUE when backed up to the previous line.
8788 */
8789 static int
8790unadjust_for_sel()
8791{
8792 pos_T *pp;
8793
8794 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8795 {
8796 if (lt(VIsual, curwin->w_cursor))
8797 pp = &curwin->w_cursor;
8798 else
8799 pp = &VIsual;
8800#ifdef FEAT_VIRTUALEDIT
8801 if (pp->coladd > 0)
8802 --pp->coladd;
8803 else
8804#endif
8805 if (pp->col > 0)
8806 {
8807 --pp->col;
8808#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008809 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810#endif
8811 }
8812 else if (pp->lnum > 1)
8813 {
8814 --pp->lnum;
8815 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8816 return TRUE;
8817 }
8818 }
8819 return FALSE;
8820}
8821
8822/*
8823 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8824 */
8825 static void
8826nv_select(cap)
8827 cmdarg_T *cap;
8828{
8829 if (VIsual_active)
8830 VIsual_select = TRUE;
8831 else if (VIsual_reselect)
8832 {
8833 cap->nchar = 'v'; /* fake "gv" command */
8834 cap->arg = TRUE;
8835 nv_g_cmd(cap);
8836 }
8837}
8838
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
8840/*
8841 * "G", "gg", CTRL-END, CTRL-HOME.
8842 * cap->arg is TRUE for "G".
8843 */
8844 static void
8845nv_goto(cap)
8846 cmdarg_T *cap;
8847{
8848 linenr_T lnum;
8849
8850 if (cap->arg)
8851 lnum = curbuf->b_ml.ml_line_count;
8852 else
8853 lnum = 1L;
8854 cap->oap->motion_type = MLINE;
8855 setpcmark();
8856
8857 /* When a count is given, use it instead of the default lnum */
8858 if (cap->count0 != 0)
8859 lnum = cap->count0;
8860 if (lnum < 1L)
8861 lnum = 1L;
8862 else if (lnum > curbuf->b_ml.ml_line_count)
8863 lnum = curbuf->b_ml.ml_line_count;
8864 curwin->w_cursor.lnum = lnum;
8865 beginline(BL_SOL | BL_FIX);
8866#ifdef FEAT_FOLDING
8867 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8868 foldOpenCursor();
8869#endif
8870}
8871
8872/*
8873 * CTRL-\ in Normal mode.
8874 */
8875 static void
8876nv_normal(cap)
8877 cmdarg_T *cap;
8878{
8879 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8880 {
8881 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008882 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 clear_cmdline = TRUE; /* unshow mode later */
8884 restart_edit = 0;
8885#ifdef FEAT_CMDWIN
8886 if (cmdwin_type != 0)
8887 cmdwin_result = Ctrl_C;
8888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (VIsual_active)
8890 {
8891 end_visual_mode(); /* stop Visual */
8892 redraw_curbuf_later(INVERTED);
8893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8895 if (cap->nchar == Ctrl_G && p_im)
8896 restart_edit = 'a';
8897 }
8898 else
8899 clearopbeep(cap->oap);
8900}
8901
8902/*
8903 * ESC in Normal mode: beep, but don't flush buffers.
8904 * Don't even beep if we are canceling a command.
8905 */
8906 static void
8907nv_esc(cap)
8908 cmdarg_T *cap;
8909{
8910 int no_reason;
8911
8912 no_reason = (cap->oap->op_type == OP_NOP
8913 && cap->opcount == 0
8914 && cap->count0 == 0
8915 && cap->oap->regname == 0
8916 && !p_im);
8917
8918 if (cap->arg) /* TRUE for CTRL-C */
8919 {
8920 if (restart_edit == 0
8921#ifdef FEAT_CMDWIN
8922 && cmdwin_type == 0
8923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 && no_reason)
8926 MSG(_("Type :quit<Enter> to exit Vim"));
8927
8928 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8929 * set again below when halfway a mapping. */
8930 if (!p_im)
8931 restart_edit = 0;
8932#ifdef FEAT_CMDWIN
8933 if (cmdwin_type != 0)
8934 {
8935 cmdwin_result = K_IGNORE;
8936 got_int = FALSE; /* don't stop executing autocommands et al. */
8937 return;
8938 }
8939#endif
8940 }
8941
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 if (VIsual_active)
8943 {
8944 end_visual_mode(); /* stop Visual */
8945 check_cursor_col(); /* make sure cursor is not beyond EOL */
8946 curwin->w_set_curswant = TRUE;
8947 redraw_curbuf_later(INVERTED);
8948 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008949 else if (no_reason)
8950 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 clearop(cap->oap);
8952
8953 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8954 * set return to Insert mode afterwards. */
8955 if (restart_edit == 0 && goto_im()
8956#ifdef FEAT_EX_EXTRA
8957 && ex_normal_busy == 0
8958#endif
8959 )
8960 restart_edit = 'a';
8961}
8962
8963/*
8964 * Handle "A", "a", "I", "i" and <Insert> commands.
8965 */
8966 static void
8967nv_edit(cap)
8968 cmdarg_T *cap;
8969{
8970 /* <Insert> is equal to "i" */
8971 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8972 cap->cmdchar = 'i';
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 /* in Visual mode "A" and "I" are an operator */
8975 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8976 v_visop(cap);
8977
8978 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008979 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8980 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982#ifdef FEAT_TEXTOBJ
8983 nv_object(cap);
8984#else
8985 clearopbeep(cap->oap);
8986#endif
8987 }
8988 else if (!curbuf->b_p_ma && !p_im)
8989 {
8990 /* Only give this error when 'insertmode' is off. */
8991 EMSG(_(e_modifiable));
8992 clearop(cap->oap);
8993 }
8994 else if (!checkclearopq(cap->oap))
8995 {
8996 switch (cap->cmdchar)
8997 {
8998 case 'A': /* "A"ppend after the line */
8999 curwin->w_set_curswant = TRUE;
9000#ifdef FEAT_VIRTUALEDIT
9001 if (ve_flags == VE_ALL)
9002 {
9003 int save_State = State;
9004
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009005 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 * character past the end of the line */
9007 State = INSERT;
9008 coladvance((colnr_T)MAXCOL);
9009 State = save_State;
9010 }
9011 else
9012#endif
9013 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9014 break;
9015
9016 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009017 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9018 beginline(BL_WHITE);
9019 else
9020 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 break;
9022
9023 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9024#ifdef FEAT_VIRTUALEDIT
9025 /* increment coladd when in virtual space, increment the
9026 * column otherwise, also to append after an unprintable char */
9027 if (virtual_active()
9028 && (curwin->w_cursor.coladd > 0
9029 || *ml_get_cursor() == NUL
9030 || *ml_get_cursor() == TAB))
9031 curwin->w_cursor.coladd++;
9032 else
9033#endif
9034 if (*ml_get_cursor() != NUL)
9035 inc_cursor();
9036 break;
9037 }
9038
9039#ifdef FEAT_VIRTUALEDIT
9040 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9041 {
9042 int save_State = State;
9043
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009044 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 * character past the end of the line */
9046 State = INSERT;
9047 coladvance(getviscol());
9048 State = save_State;
9049 }
9050#endif
9051
9052 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9053 }
9054}
9055
9056/*
9057 * Invoke edit() and take care of "restart_edit" and the return value.
9058 */
9059 static void
9060invoke_edit(cap, repl, cmd, startln)
9061 cmdarg_T *cap;
9062 int repl; /* "r" or "gr" command */
9063 int cmd;
9064 int startln;
9065{
9066 int restart_edit_save = 0;
9067
9068 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9069 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9070 * it. */
9071 if (repl || !stuff_empty())
9072 restart_edit_save = restart_edit;
9073 else
9074 restart_edit_save = 0;
9075
9076 /* Always reset "restart_edit", this is not a restarted edit. */
9077 restart_edit = 0;
9078
9079 if (edit(cmd, startln, cap->count1))
9080 cap->retval |= CA_COMMAND_BUSY;
9081
9082 if (restart_edit == 0)
9083 restart_edit = restart_edit_save;
9084}
9085
9086#ifdef FEAT_TEXTOBJ
9087/*
9088 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9089 */
9090 static void
9091nv_object(cap)
9092 cmdarg_T *cap;
9093{
9094 int flag;
9095 int include;
9096 char_u *mps_save;
9097
9098 if (cap->cmdchar == 'i')
9099 include = FALSE; /* "ix" = inner object: exclude white space */
9100 else
9101 include = TRUE; /* "ax" = an object: include white space */
9102
9103 /* Make sure (), [], {} and <> are in 'matchpairs' */
9104 mps_save = curbuf->b_p_mps;
9105 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9106
9107 switch (cap->nchar)
9108 {
9109 case 'w': /* "aw" = a word */
9110 flag = current_word(cap->oap, cap->count1, include, FALSE);
9111 break;
9112 case 'W': /* "aW" = a WORD */
9113 flag = current_word(cap->oap, cap->count1, include, TRUE);
9114 break;
9115 case 'b': /* "ab" = a braces block */
9116 case '(':
9117 case ')':
9118 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9119 break;
9120 case 'B': /* "aB" = a Brackets block */
9121 case '{':
9122 case '}':
9123 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9124 break;
9125 case '[': /* "a[" = a [] block */
9126 case ']':
9127 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9128 break;
9129 case '<': /* "a<" = a <> block */
9130 case '>':
9131 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9132 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009133 case 't': /* "at" = a tag block (xml and html) */
9134 flag = current_tagblock(cap->oap, cap->count1, include);
9135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 case 'p': /* "ap" = a paragraph */
9137 flag = current_par(cap->oap, cap->count1, include, 'p');
9138 break;
9139 case 's': /* "as" = a sentence */
9140 flag = current_sent(cap->oap, cap->count1, include);
9141 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009142 case '"': /* "a"" = a double quoted string */
9143 case '\'': /* "a'" = a single quoted string */
9144 case '`': /* "a`" = a backtick quoted string */
9145 flag = current_quote(cap->oap, cap->count1, include,
9146 cap->nchar);
9147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#if 0 /* TODO */
9149 case 'S': /* "aS" = a section */
9150 case 'f': /* "af" = a filename */
9151 case 'u': /* "au" = a URL */
9152#endif
9153 default:
9154 flag = FAIL;
9155 break;
9156 }
9157
9158 curbuf->b_p_mps = mps_save;
9159 if (flag == FAIL)
9160 clearopbeep(cap->oap);
9161 adjust_cursor_col();
9162 curwin->w_set_curswant = TRUE;
9163}
9164#endif
9165
9166/*
9167 * "q" command: Start/stop recording.
9168 * "q:", "q/", "q?": edit command-line in command-line window.
9169 */
9170 static void
9171nv_record(cap)
9172 cmdarg_T *cap;
9173{
9174 if (cap->oap->op_type == OP_FORMAT)
9175 {
9176 /* "gqq" is the same as "gqgq": format line */
9177 cap->cmdchar = 'g';
9178 cap->nchar = 'q';
9179 nv_operator(cap);
9180 }
9181 else if (!checkclearop(cap->oap))
9182 {
9183#ifdef FEAT_CMDWIN
9184 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9185 {
9186 stuffcharReadbuff(cap->nchar);
9187 stuffcharReadbuff(K_CMDWIN);
9188 }
9189 else
9190#endif
9191 /* (stop) recording into a named register, unless executing a
9192 * register */
9193 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9194 clearopbeep(cap->oap);
9195 }
9196}
9197
9198/*
9199 * Handle the "@r" command.
9200 */
9201 static void
9202nv_at(cap)
9203 cmdarg_T *cap;
9204{
9205 if (checkclearop(cap->oap))
9206 return;
9207#ifdef FEAT_EVAL
9208 if (cap->nchar == '=')
9209 {
9210 if (get_expr_register() == NUL)
9211 return;
9212 }
9213#endif
9214 while (cap->count1-- && !got_int)
9215 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009216 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 {
9218 clearopbeep(cap->oap);
9219 break;
9220 }
9221 line_breakcheck();
9222 }
9223}
9224
9225/*
9226 * Handle the CTRL-U and CTRL-D commands.
9227 */
9228 static void
9229nv_halfpage(cap)
9230 cmdarg_T *cap;
9231{
9232 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9233 || (cap->cmdchar == Ctrl_D
9234 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9235 clearopbeep(cap->oap);
9236 else if (!checkclearop(cap->oap))
9237 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9238}
9239
9240/*
9241 * Handle "J" or "gJ" command.
9242 */
9243 static void
9244nv_join(cap)
9245 cmdarg_T *cap;
9246{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 if (VIsual_active) /* join the visual lines */
9248 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009249 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
9251 if (cap->count0 <= 1)
9252 cap->count0 = 2; /* default for join is two lines! */
9253 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9254 curbuf->b_ml.ml_line_count)
9255 clearopbeep(cap->oap); /* beyond last line */
9256 else
9257 {
9258 prep_redo(cap->oap->regname, cap->count0,
9259 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009260 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 }
9262 }
9263}
9264
9265/*
9266 * "P", "gP", "p" and "gp" commands.
9267 */
9268 static void
9269nv_put(cap)
9270 cmdarg_T *cap;
9271{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 int regname = 0;
9273 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009274 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009275 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 int dir;
9277 int flags = 0;
9278
9279 if (cap->oap->op_type != OP_NOP)
9280 {
9281#ifdef FEAT_DIFF
9282 /* "dp" is ":diffput" */
9283 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9284 {
9285 clearop(cap->oap);
9286 nv_diffgetput(TRUE);
9287 }
9288 else
9289#endif
9290 clearopbeep(cap->oap);
9291 }
9292 else
9293 {
9294 dir = (cap->cmdchar == 'P'
9295 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9296 ? BACKWARD : FORWARD;
9297 prep_redo_cmd(cap);
9298 if (cap->cmdchar == 'g')
9299 flags |= PUT_CURSEND;
9300
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 if (VIsual_active)
9302 {
9303 /* Putting in Visual mode: The put text replaces the selected
9304 * text. First delete the selected text, then put the new text.
9305 * Need to save and restore the registers that the delete
9306 * overwrites if the old contents is being put.
9307 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009308 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009310#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009312#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009313 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009314 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009315#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
9319 )
9320 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009321 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 * put, save it first. */
9323 reg1 = get_register(regname, TRUE);
9324 }
9325
9326 /* Now delete the selected text. */
9327 cap->cmdchar = 'd';
9328 cap->nchar = NUL;
9329 cap->oap->regname = NUL;
9330 nv_operator(cap);
9331 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009332 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
9334 /* delete PUT_LINE_BACKWARD; */
9335 cap->oap->regname = regname;
9336
9337 if (reg1 != NULL)
9338 {
9339 /* Delete probably changed the register we want to put, save
9340 * it first. Then put back what was there before the delete. */
9341 reg2 = get_register(regname, FALSE);
9342 put_register(regname, reg1);
9343 }
9344
9345 /* When deleted a linewise Visual area, put the register as
9346 * lines to avoid it joined with the next line. When deletion was
9347 * characterwise, split a line when putting lines. */
9348 if (VIsual_mode == 'V')
9349 flags |= PUT_LINE;
9350 else if (VIsual_mode == 'v')
9351 flags |= PUT_LINE_SPLIT;
9352 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9353 flags |= PUT_LINE_FORWARD;
9354 dir = BACKWARD;
9355 if ((VIsual_mode != 'V'
9356 && curwin->w_cursor.col < curbuf->b_op_start.col)
9357 || (VIsual_mode == 'V'
9358 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9359 /* cursor is at the end of the line or end of file, put
9360 * forward. */
9361 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009362 /* May have been reset in do_put(). */
9363 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 do_put(cap->oap->regname, dir, cap->count1, flags);
9366
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 /* If a register was saved, put it back now. */
9368 if (reg2 != NULL)
9369 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009370
9371 /* What to reselect with "gv"? Selecting the just put text seems to
9372 * be the most useful, since the original text was removed. */
9373 if (was_visual)
9374 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009375 curbuf->b_visual.vi_start = curbuf->b_op_start;
9376 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009377 }
9378
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009379 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009380 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009381 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009382 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009383 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009384
9385 /* If the cursor was in that line, move it to the end of the last
9386 * line. */
9387 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9388 {
9389 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9390 coladvance((colnr_T)MAXCOL);
9391 }
9392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 auto_format(FALSE, TRUE);
9394 }
9395}
9396
9397/*
9398 * "o" and "O" commands.
9399 */
9400 static void
9401nv_open(cap)
9402 cmdarg_T *cap;
9403{
9404#ifdef FEAT_DIFF
9405 /* "do" is ":diffget" */
9406 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9407 {
9408 clearop(cap->oap);
9409 nv_diffgetput(FALSE);
9410 }
9411 else
9412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 if (VIsual_active) /* switch start and end of visual */
9414 v_swap_corners(cap->cmdchar);
9415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 n_opencmd(cap);
9417}
9418
9419#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 static void
9421nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009422 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424 ProcessSniffRequests();
9425}
9426#endif
9427
9428#ifdef FEAT_NETBEANS_INTG
9429 static void
9430nv_nbcmd(cap)
9431 cmdarg_T *cap;
9432{
9433 netbeans_keycommand(cap->nchar);
9434}
9435#endif
9436
9437#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 static void
9439nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009440 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9443}
9444#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009445
9446#ifdef FEAT_AUTOCMD
9447/*
9448 * Trigger CursorHold event.
9449 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9450 * input buffer. "did_cursorhold" is set to avoid retriggering.
9451 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009452 static void
9453nv_cursorhold(cap)
9454 cmdarg_T *cap;
9455{
9456 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9457 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009458 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009459}
9460#endif