blob: e5fa1401c0785ea891608d60ceedd2845cc3d183 [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.
1079 * However, don't wait for it to arrive. */
1080 while (enc_utf8 && lang && (c = vpeekc()) > 0
1081 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1082 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001083 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (!utf_iscomposing(c))
1085 {
1086 vungetc(c); /* it wasn't, put it back */
1087 break;
1088 }
1089 else if (ca.ncharC1 == 0)
1090 ca.ncharC1 = c;
1091 else
1092 ca.ncharC2 = c;
1093 }
1094#endif
1095 }
1096 --no_mapping;
1097 --allow_keys;
1098 }
1099
1100#ifdef FEAT_CMDL_INFO
1101 /*
1102 * Flush the showcmd characters onto the screen so we can see them while
1103 * the command is being executed. Only do this when the shown command was
1104 * actually displayed, otherwise this will slow down a lot when executing
1105 * mappings.
1106 */
1107 if (need_flushbuf)
1108 out_flush();
1109#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001110#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001111 if (ca.cmdchar != K_IGNORE)
1112 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
1115 State = NORMAL;
1116
1117 if (ca.nchar == ESC)
1118 {
1119 clearop(oap);
1120 if (restart_edit == 0 && goto_im())
1121 restart_edit = 'a';
1122 goto normal_end;
1123 }
1124
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001125 if (ca.cmdchar != K_IGNORE)
1126 {
1127 msg_didout = FALSE; /* don't scroll screen up for normal command */
1128 msg_col = 0;
1129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 old_pos = curwin->w_cursor; /* remember where cursor was */
1132
1133 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1134 * mode. */
1135 if (!VIsual_active && km_startsel)
1136 {
1137 if (nv_cmds[idx].cmd_flags & NV_SS)
1138 {
1139 start_selection();
1140 unshift_special(&ca);
1141 idx = find_command(ca.cmdchar);
1142 }
1143 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1144 && (mod_mask & MOD_MASK_SHIFT))
1145 {
1146 start_selection();
1147 mod_mask &= ~MOD_MASK_SHIFT;
1148 }
1149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 /*
1152 * Execute the command!
1153 * Call the command function found in the commands table.
1154 */
1155 ca.arg = nv_cmds[idx].cmd_arg;
1156 (nv_cmds[idx].cmd_func)(&ca);
1157
1158 /*
1159 * If we didn't start or finish an operator, reset oap->regname, unless we
1160 * need it later.
1161 */
1162 if (!finish_op
1163 && !oap->op_type
1164 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1165 {
1166 clearop(oap);
1167#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001168 {
1169 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001170
Bram Moolenaar536681b2011-05-10 16:12:45 +02001171 /* Adjust the register according to 'clipboard', so that when
1172 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001173# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001174 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001175# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001176 set_reg_var(regname);
1177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#endif
1179 }
1180
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001181 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001182 * character or "z333<cr>". */
1183 if (old_mapped_len > 0)
1184 old_mapped_len = typebuf_maplen();
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 /*
1187 * If an operation is pending, handle it...
1188 */
1189 do_pending_operator(&ca, old_col, FALSE);
1190
1191 /*
1192 * Wait for a moment when a message is displayed that will be overwritten
1193 * by the mode message.
1194 * In Visual mode and with "^O" in Insert mode, a short message will be
1195 * overwritten by the mode message. Wait a bit, until a key is hit.
1196 * In Visual mode, it's more important to keep the Visual area updated
1197 * than keeping a message (e.g. from a /pat search).
1198 * Only do this if the command was typed, not from a mapping.
1199 * Don't wait when emsg_silent is non-zero.
1200 * Also wait a bit after an error message, e.g. for "^O:".
1201 * Don't redraw the screen, it would remove the message.
1202 */
1203 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001204 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 || (VIsual_active
1207 && old_pos.lnum == curwin->w_cursor.lnum
1208 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 )
1210 && (clear_cmdline
1211 || redraw_cmdline)
1212 && (msg_didout || (msg_didany && msg_scroll))
1213 && !msg_nowait
1214 && KeyTyped)
1215 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 && (msg_scroll
1218 || emsg_on_display)))
1219 && oap->regname == 0
1220 && !(ca.retval & CA_COMMAND_BUSY)
1221 && stuff_empty()
1222 && typebuf_typed()
1223 && emsg_silent == 0
1224 && !did_wait_return
1225 && oap->op_type == OP_NOP)
1226 {
1227 int save_State = State;
1228
1229 /* Draw the cursor with the right shape here */
1230 if (restart_edit != 0)
1231 State = INSERT;
1232
1233 /* If need to redraw, and there is a "keep_msg", redraw before the
1234 * delay */
1235 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1236 {
1237 char_u *kmsg;
1238
1239 kmsg = keep_msg;
1240 keep_msg = NULL;
1241 /* showmode() will clear keep_msg, but we want to use it anyway */
1242 update_screen(0);
1243 /* now reset it, otherwise it's put in the history again */
1244 keep_msg = kmsg;
1245 msg_attr(kmsg, keep_msg_attr);
1246 vim_free(kmsg);
1247 }
1248 setcursor();
1249 cursor_on();
1250 out_flush();
1251 if (msg_scroll || emsg_on_display)
1252 ui_delay(1000L, TRUE); /* wait at least one second */
1253 ui_delay(3000L, FALSE); /* wait up to three seconds */
1254 State = save_State;
1255
1256 msg_scroll = FALSE;
1257 emsg_on_display = FALSE;
1258 }
1259
1260 /*
1261 * Finish up after executing a Normal mode command.
1262 */
1263normal_end:
1264
1265 msg_nowait = FALSE;
1266
1267 /* Reset finish_op, in case it was set */
1268#ifdef CURSOR_SHAPE
1269 c = finish_op;
1270#endif
1271 finish_op = FALSE;
1272#ifdef CURSOR_SHAPE
1273 /* Redraw the cursor with another shape, if we were in Operator-pending
1274 * mode or did a replace command. */
1275 if (c || ca.cmdchar == 'r')
1276 {
1277 ui_cursor_shape(); /* may show different cursor shape */
1278# ifdef FEAT_MOUSESHAPE
1279 update_mouseshape(-1);
1280# endif
1281 }
1282#endif
1283
1284#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001285 if (oap->op_type == OP_NOP && oap->regname == 0
1286# ifdef FEAT_AUTOCMD
1287 && ca.cmdchar != K_CURSORHOLD
1288# endif
1289 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 clear_showcmd();
1291#endif
1292
1293 checkpcmark(); /* check if we moved since setting pcmark */
1294 vim_free(ca.searchbuf);
1295
1296#ifdef FEAT_MBYTE
1297 if (has_mbyte)
1298 mb_adjust_cursor();
1299#endif
1300
1301#ifdef FEAT_SCROLLBIND
1302 if (curwin->w_p_scb && toplevel)
1303 {
1304 validate_cursor(); /* may need to update w_leftcol */
1305 do_check_scrollbind(TRUE);
1306 }
1307#endif
1308
Bram Moolenaar860cae12010-06-05 23:22:07 +02001309#ifdef FEAT_CURSORBIND
1310 if (curwin->w_p_crb && toplevel)
1311 {
1312 validate_cursor(); /* may need to update w_leftcol */
1313 do_check_cursorbind();
1314 }
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 /*
1318 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1319 * if still inside a mapping that started in Visual mode).
1320 * May switch from Visual to Select mode after CTRL-O command.
1321 */
1322 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1324 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && !(ca.retval & CA_COMMAND_BUSY)
1326 && stuff_empty()
1327 && oap->regname == 0)
1328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (restart_VIsual_select == 1)
1330 {
1331 VIsual_select = TRUE;
1332 showmode();
1333 restart_VIsual_select = 0;
1334 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001335 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 (void)edit(restart_edit, FALSE, 1L);
1337 }
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (restart_VIsual_select == 2)
1340 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 /* Save count before an operator for next time. */
1343 opcount = ca.opcount;
1344}
1345
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001346#ifdef FEAT_EVAL
1347/*
1348 * Set v:count and v:count1 according to "cap".
1349 * Set v:prevcount only when "set_prevcount" is TRUE.
1350 */
1351 static void
1352set_vcount_ca(cap, set_prevcount)
1353 cmdarg_T *cap;
1354 int *set_prevcount;
1355{
1356 long count = cap->count0;
1357
1358 /* multiply with cap->opcount the same way as above */
1359 if (cap->opcount != 0)
1360 count = cap->opcount * (count == 0 ? 1 : count);
1361 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1362 *set_prevcount = FALSE; /* only set v:prevcount once */
1363}
1364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366/*
1367 * Handle an operator after visual mode or when the movement is finished
1368 */
1369 void
1370do_pending_operator(cap, old_col, gui_yank)
1371 cmdarg_T *cap;
1372 int old_col;
1373 int gui_yank;
1374{
1375 oparg_T *oap = cap->oap;
1376 pos_T old_cursor;
1377 int empty_region_error;
1378 int restart_edit_save;
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 /* The visual area is remembered for redo */
1381 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1382 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001383 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001385#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389#if defined(FEAT_CLIPBOARD)
1390 /*
1391 * Yank the visual area into the GUI selection register before we operate
1392 * on it and lose it forever.
1393 * Don't do it if a specific register was specified, so that ""x"*P works.
1394 * This could call do_pending_operator() recursively, but that's OK
1395 * because gui_yank will be TRUE for the nested call.
1396 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001397 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 && oap->op_type != OP_NOP
1399 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 && VIsual_active
1401 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && oap->regname == 0)
1403 clip_auto_select();
1404#endif
1405 old_cursor = curwin->w_cursor;
1406
1407 /*
1408 * If an operation is pending, handle it...
1409 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001410 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 oap->is_VIsual = VIsual_active;
1413 if (oap->motion_force == 'V')
1414 oap->motion_type = MLINE;
1415 else if (oap->motion_force == 'v')
1416 {
1417 /* If the motion was linewise, "inclusive" will not have been set.
1418 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1419 if (oap->motion_type == MLINE)
1420 oap->inclusive = FALSE;
1421 /* If the motion already was characterwise, toggle "inclusive" */
1422 else if (oap->motion_type == MCHAR)
1423 oap->inclusive = !oap->inclusive;
1424 oap->motion_type = MCHAR;
1425 }
1426 else if (oap->motion_force == Ctrl_V)
1427 {
1428 /* Change line- or characterwise motion into Visual block mode. */
1429 VIsual_active = TRUE;
1430 VIsual = oap->start;
1431 VIsual_mode = Ctrl_V;
1432 VIsual_select = FALSE;
1433 VIsual_reselect = FALSE;
1434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar7afea822013-04-24 18:34:45 +02001436 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1437 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001439 && ((!VIsual_active || oap->motion_force)
1440 /* Also redo Operator-pending Visual mode mappings */
1441 || (VIsual_active && cap->cmdchar == ':'
1442 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001443 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444#ifdef FEAT_FOLDING
1445 && oap->op_type != OP_FOLD
1446 && oap->op_type != OP_FOLDOPEN
1447 && oap->op_type != OP_FOLDOPENREC
1448 && oap->op_type != OP_FOLDCLOSE
1449 && oap->op_type != OP_FOLDCLOSEREC
1450 && oap->op_type != OP_FOLDDEL
1451 && oap->op_type != OP_FOLDDELREC
1452#endif
1453 )
1454 {
1455 prep_redo(oap->regname, cap->count0,
1456 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1457 oap->motion_force, cap->cmdchar, cap->nchar);
1458 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1459 {
1460 /*
1461 * If 'cpoptions' does not contain 'r', insert the search
1462 * pattern to really repeat the same command.
1463 */
1464 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001465 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 AppendToRedobuff(NL_STR);
1467 }
1468 else if (cap->cmdchar == ':')
1469 {
1470 /* do_cmdline() has stored the first typed line in
1471 * "repeat_cmdline". When several lines are typed repeating
1472 * won't be possible. */
1473 if (repeat_cmdline == NULL)
1474 ResetRedobuff();
1475 else
1476 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001477 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 AppendToRedobuff(NL_STR);
1479 vim_free(repeat_cmdline);
1480 repeat_cmdline = NULL;
1481 }
1482 }
1483 }
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (redo_VIsual_busy)
1486 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001487 /* Redo of an operation on a Visual area. Use the same size from
1488 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 oap->start = curwin->w_cursor;
1490 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1491 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1492 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1493 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001494 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001496 if (VIsual_mode == 'v')
1497 {
1498 if (redo_VIsual_line_count <= 1)
1499 {
1500 validate_virtcol();
1501 curwin->w_curswant =
1502 curwin->w_virtcol + redo_VIsual_vcol - 1;
1503 }
1504 else
1505 curwin->w_curswant = redo_VIsual_vcol;
1506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001508 {
1509 curwin->w_curswant = MAXCOL;
1510 }
1511 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 cap->count0 = redo_VIsual_count;
1514 if (redo_VIsual_count != 0)
1515 cap->count1 = redo_VIsual_count;
1516 else
1517 cap->count1 = 1;
1518 }
1519 else if (VIsual_active)
1520 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001521 if (!gui_yank)
1522 {
1523 /* Save the current VIsual area for '< and '> marks, and "gv" */
1524 curbuf->b_visual.vi_start = VIsual;
1525 curbuf->b_visual.vi_end = curwin->w_cursor;
1526 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001527 if (VIsual_mode_orig != NUL)
1528 {
1529 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1530 VIsual_mode_orig = NUL;
1531 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001532 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001534 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 /* In Select mode, a linewise selection is operated upon like a
1539 * characterwise selection. */
1540 if (VIsual_select && VIsual_mode == 'V')
1541 {
1542 if (lt(VIsual, curwin->w_cursor))
1543 {
1544 VIsual.col = 0;
1545 curwin->w_cursor.col =
1546 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1547 }
1548 else
1549 {
1550 curwin->w_cursor.col = 0;
1551 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1552 }
1553 VIsual_mode = 'v';
1554 }
1555 /* If 'selection' is "exclusive", backup one character for
1556 * charwise selections. */
1557 else if (VIsual_mode == 'v')
1558 {
1559# ifdef FEAT_VIRTUALEDIT
1560 include_line_break =
1561# endif
1562 unadjust_for_sel();
1563 }
1564
1565 oap->start = VIsual;
1566 if (VIsual_mode == 'V')
1567 oap->start.col = 0;
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
1570 /*
1571 * Set oap->start to the first position of the operated text, oap->end
1572 * to the end of the operated text. w_cursor is equal to oap->start.
1573 */
1574 if (lt(oap->start, curwin->w_cursor))
1575 {
1576#ifdef FEAT_FOLDING
1577 /* Include folded lines completely. */
1578 if (!VIsual_active)
1579 {
1580 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1581 oap->start.col = 0;
1582 if (hasFolding(curwin->w_cursor.lnum, NULL,
1583 &curwin->w_cursor.lnum))
1584 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1585 }
1586#endif
1587 oap->end = curwin->w_cursor;
1588 curwin->w_cursor = oap->start;
1589
1590 /* w_virtcol may have been updated; if the cursor goes back to its
1591 * previous position w_virtcol becomes invalid and isn't updated
1592 * automatically. */
1593 curwin->w_valid &= ~VALID_VIRTCOL;
1594 }
1595 else
1596 {
1597#ifdef FEAT_FOLDING
1598 /* Include folded lines completely. */
1599 if (!VIsual_active && oap->motion_type == MLINE)
1600 {
1601 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1602 NULL))
1603 curwin->w_cursor.col = 0;
1604 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1605 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1606 }
1607#endif
1608 oap->end = oap->start;
1609 oap->start = curwin->w_cursor;
1610 }
1611
1612 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1613
1614#ifdef FEAT_VIRTUALEDIT
1615 /* Set "virtual_op" before resetting VIsual_active. */
1616 virtual_op = virtual_active();
1617#endif
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (VIsual_active || redo_VIsual_busy)
1620 {
1621 if (VIsual_mode == Ctrl_V) /* block mode */
1622 {
1623 colnr_T start, end;
1624
1625 oap->block_mode = TRUE;
1626
1627 getvvcol(curwin, &(oap->start),
1628 &oap->start_vcol, NULL, &oap->end_vcol);
1629 if (!redo_VIsual_busy)
1630 {
1631 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1632
1633 if (start < oap->start_vcol)
1634 oap->start_vcol = start;
1635 if (end > oap->end_vcol)
1636 {
1637 if (*p_sel == 'e' && start >= 1
1638 && start - 1 >= oap->end_vcol)
1639 oap->end_vcol = start - 1;
1640 else
1641 oap->end_vcol = end;
1642 }
1643 }
1644
1645 /* if '$' was used, get oap->end_vcol from longest line */
1646 if (curwin->w_curswant == MAXCOL)
1647 {
1648 curwin->w_cursor.col = MAXCOL;
1649 oap->end_vcol = 0;
1650 for (curwin->w_cursor.lnum = oap->start.lnum;
1651 curwin->w_cursor.lnum <= oap->end.lnum;
1652 ++curwin->w_cursor.lnum)
1653 {
1654 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1655 if (end > oap->end_vcol)
1656 oap->end_vcol = end;
1657 }
1658 }
1659 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001660 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /*
1662 * Correct oap->end.col and oap->start.col to be the
1663 * upper-left and lower-right corner of the block area.
1664 *
1665 * (Actually, this does convert column positions into character
1666 * positions)
1667 */
1668 curwin->w_cursor.lnum = oap->end.lnum;
1669 coladvance(oap->end_vcol);
1670 oap->end = curwin->w_cursor;
1671
1672 curwin->w_cursor = oap->start;
1673 coladvance(oap->start_vcol);
1674 oap->start = curwin->w_cursor;
1675 }
1676
1677 if (!redo_VIsual_busy && !gui_yank)
1678 {
1679 /*
1680 * Prepare to reselect and redo Visual: this is based on the
1681 * size of the Visual text
1682 */
1683 resel_VIsual_mode = VIsual_mode;
1684 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001685 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001687 {
1688 if (VIsual_mode != Ctrl_V)
1689 getvvcol(curwin, &(oap->end),
1690 NULL, NULL, &oap->end_vcol);
1691 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1692 {
1693 if (VIsual_mode != Ctrl_V)
1694 getvvcol(curwin, &(oap->start),
1695 &oap->start_vcol, NULL, NULL);
1696 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1697 }
1698 else
1699 resel_VIsual_vcol = oap->end_vcol;
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 resel_VIsual_line_count = oap->line_count;
1702 }
1703
1704 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1705 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1706 && oap->op_type != OP_COLON
1707#ifdef FEAT_FOLDING
1708 && oap->op_type != OP_FOLD
1709 && oap->op_type != OP_FOLDOPEN
1710 && oap->op_type != OP_FOLDOPENREC
1711 && oap->op_type != OP_FOLDCLOSE
1712 && oap->op_type != OP_FOLDCLOSEREC
1713 && oap->op_type != OP_FOLDDEL
1714 && oap->op_type != OP_FOLDDELREC
1715#endif
1716 && oap->motion_force == NUL
1717 )
1718 {
1719 /* Prepare for redoing. Only use the nchar field for "r",
1720 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001721 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1722 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001723 prep_redo(oap->regname, cap->count0,
1724 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1725 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001726 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001727 prep_redo(oap->regname, 0L, NUL, 'v',
1728 get_op_char(oap->op_type),
1729 get_extra_op_char(oap->op_type),
1730 oap->op_type == OP_REPLACE
1731 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (!redo_VIsual_busy)
1733 {
1734 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001735 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 redo_VIsual_line_count = resel_VIsual_line_count;
1737 redo_VIsual_count = cap->count0;
1738 }
1739 }
1740
1741 /*
1742 * oap->inclusive defaults to TRUE.
1743 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1744 * FALSE. This makes "d}P" and "v}dP" work the same.
1745 */
1746 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1747 oap->inclusive = TRUE;
1748 if (VIsual_mode == 'V')
1749 oap->motion_type = MLINE;
1750 else
1751 {
1752 oap->motion_type = MCHAR;
1753 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001754#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 )
1758 {
1759 oap->inclusive = FALSE;
1760 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001761 * that works on lines only. */
1762 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001764 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1765 {
1766 ++oap->end.lnum;
1767 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001768#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001769 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001770#endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001771 ++oap->line_count;
1772 }
1773 else
1774 {
1775 /* Cannot move below the last line, make the op
1776 * inclusive to tell the operation to include the
1777 * line break. */
1778 oap->inclusive = TRUE;
1779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 }
1781 }
1782 }
1783
1784 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
1787 * Switch Visual off now, so screen updating does
1788 * not show inverted text when the screen is redrawn.
1789 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1790 * no screen redraw, so it is done here to remove the inverted
1791 * part.
1792 */
1793 if (!gui_yank)
1794 {
1795 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001796#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 setmouse();
1798 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001799#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001800 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 clear_cmdline = TRUE; /* unshow visual mode later */
1802#ifdef FEAT_CMDL_INFO
1803 else
1804 clear_showcmd();
1805#endif
1806 if ((oap->op_type == OP_YANK
1807 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001808 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 || oap->op_type == OP_FILTER)
1810 && oap->motion_force == NUL)
1811 redraw_curbuf_later(INVERTED);
1812 }
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815#ifdef FEAT_MBYTE
1816 /* Include the trailing byte of a multi-byte char. */
1817 if (has_mbyte && oap->inclusive)
1818 {
1819 int l;
1820
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001821 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (l > 1)
1823 oap->end.col += l - 1;
1824 }
1825#endif
1826 curwin->w_set_curswant = TRUE;
1827
1828 /*
1829 * oap->empty is set when start and end are the same. The inclusive
1830 * flag affects this too, unless yanking and the end is on a NUL.
1831 */
1832 oap->empty = (oap->motion_type == MCHAR
1833 && (!oap->inclusive
1834 || (oap->op_type == OP_YANK
1835 && gchar_pos(&oap->end) == NUL))
1836 && equalpos(oap->start, oap->end)
1837#ifdef FEAT_VIRTUALEDIT
1838 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1839#endif
1840 );
1841 /*
1842 * For delete, change and yank, it's an error to operate on an
1843 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1844 */
1845 empty_region_error = (oap->empty
1846 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 /* Force a redraw when operating on an empty Visual region, when
1849 * 'modifiable is off or creating a fold. */
1850 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001851#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 ))
1855 redraw_curbuf_later(INVERTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 /*
1858 * If the end of an operator is in column one while oap->motion_type
1859 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1860 * character in the previous line. If op_start is on or before the
1861 * first non-blank in the line, the operator becomes linewise
1862 * (strange, but that's the way vi does it).
1863 */
1864 if ( oap->motion_type == MCHAR
1865 && oap->inclusive == FALSE
1866 && !(cap->retval & CA_NO_ADJ_OP_END)
1867 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001869 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 && oap->line_count > 1)
1871 {
1872 oap->end_adjusted = TRUE; /* remember that we did this */
1873 --oap->line_count;
1874 --oap->end.lnum;
1875 if (inindent(0))
1876 oap->motion_type = MLINE;
1877 else
1878 {
1879 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1880 if (oap->end.col)
1881 {
1882 --oap->end.col;
1883 oap->inclusive = TRUE;
1884 }
1885 }
1886 }
1887 else
1888 oap->end_adjusted = FALSE;
1889
1890 switch (oap->op_type)
1891 {
1892 case OP_LSHIFT:
1893 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001894 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 auto_format(FALSE, TRUE);
1896 break;
1897
1898 case OP_JOIN_NS:
1899 case OP_JOIN:
1900 if (oap->line_count < 2)
1901 oap->line_count = 2;
1902 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1903 curbuf->b_ml.ml_line_count)
1904 beep_flush();
1905 else
1906 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001907 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001908 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 auto_format(FALSE, TRUE);
1910 }
1911 break;
1912
1913 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001918 CancelRedo();
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
1921 {
1922 (void)op_delete(oap);
1923 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1924 u_save_cursor(); /* cursor line wasn't saved yet */
1925 auto_format(FALSE, TRUE);
1926 }
1927 break;
1928
1929 case OP_YANK:
1930 if (empty_region_error)
1931 {
1932 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001935 CancelRedo();
1936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938 else
1939 (void)op_yank(oap, FALSE, !gui_yank);
1940 check_cursor_col();
1941 break;
1942
1943 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001948 CancelRedo();
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 else
1951 {
1952 /* This is a new edit command, not a restart. Need to
1953 * remember it to make 'insertmode' work with mappings for
1954 * Visual mode. But do this only once and not when typed and
1955 * 'insertmode' isn't set. */
1956 if (p_im || !KeyTyped)
1957 restart_edit_save = restart_edit;
1958 else
1959 restart_edit_save = 0;
1960 restart_edit = 0;
1961 /* Reset finish_op now, don't want it set inside edit(). */
1962 finish_op = FALSE;
1963 if (op_change(oap)) /* will call edit() */
1964 cap->retval |= CA_COMMAND_BUSY;
1965 if (restart_edit == 0)
1966 restart_edit = restart_edit_save;
1967 }
1968 break;
1969
1970 case OP_FILTER:
1971 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1972 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1973 else
1974 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1975
1976 case OP_INDENT:
1977 case OP_COLON:
1978
1979#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1980 /*
1981 * If 'equalprg' is empty, do the indenting internally.
1982 */
1983 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1984 {
1985# ifdef FEAT_LISP
1986 if (curbuf->b_p_lisp)
1987 {
1988 op_reindent(oap, get_lisp_indent);
1989 break;
1990 }
1991# endif
1992# ifdef FEAT_CINDENT
1993 op_reindent(oap,
1994# ifdef FEAT_EVAL
1995 *curbuf->b_p_inde != NUL ? get_expr_indent :
1996# endif
1997 get_c_indent);
1998 break;
1999# endif
2000 }
2001#endif
2002
2003 op_colon(oap);
2004 break;
2005
2006 case OP_TILDE:
2007 case OP_UPPER:
2008 case OP_LOWER:
2009 case OP_ROT13:
2010 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002013 CancelRedo();
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else
2016 op_tilde(oap);
2017 check_cursor_col();
2018 break;
2019
2020 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002021#if defined(FEAT_EVAL)
2022 if (*curbuf->b_p_fex != NUL)
2023 op_formatexpr(oap); /* use expression */
2024 else
2025#endif
2026 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 op_colon(oap); /* use external command */
2028 else
2029 op_format(oap, FALSE); /* use internal function */
2030 break;
2031
2032 case OP_FORMAT2:
2033 op_format(oap, TRUE); /* use internal function */
2034 break;
2035
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002036 case OP_FUNCTION:
2037 op_function(oap); /* call 'operatorfunc' */
2038 break;
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 case OP_INSERT:
2041 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#ifdef FEAT_VISUALEXTRA
2044 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002045 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002047 CancelRedo();
2048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 else
2050 {
2051 /* This is a new edit command, not a restart. Need to
2052 * remember it to make 'insertmode' work with mappings for
2053 * Visual mode. But do this only once. */
2054 restart_edit_save = restart_edit;
2055 restart_edit = 0;
2056
2057 op_insert(oap, cap->count1);
2058
2059 /* TODO: when inserting in several lines, should format all
2060 * the lines. */
2061 auto_format(FALSE, TRUE);
2062
2063 if (restart_edit == 0)
2064 restart_edit = restart_edit_save;
2065 }
2066#else
2067 vim_beep();
2068#endif
2069 break;
2070
2071 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#ifdef FEAT_VISUALEXTRA
2074 if (empty_region_error)
2075#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002078 CancelRedo();
2079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#ifdef FEAT_VISUALEXTRA
2081 else
2082 op_replace(oap, cap->nchar);
2083#endif
2084 break;
2085
2086#ifdef FEAT_FOLDING
2087 case OP_FOLD:
2088 VIsual_reselect = FALSE; /* don't reselect now */
2089 foldCreate(oap->start.lnum, oap->end.lnum);
2090 break;
2091
2092 case OP_FOLDOPEN:
2093 case OP_FOLDOPENREC:
2094 case OP_FOLDCLOSE:
2095 case OP_FOLDCLOSEREC:
2096 VIsual_reselect = FALSE; /* don't reselect now */
2097 opFoldRange(oap->start.lnum, oap->end.lnum,
2098 oap->op_type == OP_FOLDOPEN
2099 || oap->op_type == OP_FOLDOPENREC,
2100 oap->op_type == OP_FOLDOPENREC
2101 || oap->op_type == OP_FOLDCLOSEREC,
2102 oap->is_VIsual);
2103 break;
2104
2105 case OP_FOLDDEL:
2106 case OP_FOLDDELREC:
2107 VIsual_reselect = FALSE; /* don't reselect now */
2108 deleteFold(oap->start.lnum, oap->end.lnum,
2109 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2110 break;
2111#endif
2112 default:
2113 clearopbeep(oap);
2114 }
2115#ifdef FEAT_VIRTUALEDIT
2116 virtual_op = MAYBE;
2117#endif
2118 if (!gui_yank)
2119 {
2120 /*
2121 * if 'sol' not set, go back to old column for some commands
2122 */
2123 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2124 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2125 || oap->op_type == OP_DELETE))
2126 coladvance(curwin->w_curswant = old_col);
2127 }
2128 else
2129 {
2130 curwin->w_cursor = old_cursor;
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 clearop(oap);
2134 }
2135}
2136
2137/*
2138 * Handle indent and format operators and visual mode ":".
2139 */
2140 static void
2141op_colon(oap)
2142 oparg_T *oap;
2143{
2144 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (oap->is_VIsual)
2146 stuffReadbuff((char_u *)"'<,'>");
2147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 /*
2150 * Make the range look nice, so it can be repeated.
2151 */
2152 if (oap->start.lnum == curwin->w_cursor.lnum)
2153 stuffcharReadbuff('.');
2154 else
2155 stuffnumReadbuff((long)oap->start.lnum);
2156 if (oap->end.lnum != oap->start.lnum)
2157 {
2158 stuffcharReadbuff(',');
2159 if (oap->end.lnum == curwin->w_cursor.lnum)
2160 stuffcharReadbuff('.');
2161 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2162 stuffcharReadbuff('$');
2163 else if (oap->start.lnum == curwin->w_cursor.lnum)
2164 {
2165 stuffReadbuff((char_u *)".+");
2166 stuffnumReadbuff((long)oap->line_count - 1);
2167 }
2168 else
2169 stuffnumReadbuff((long)oap->end.lnum);
2170 }
2171 }
2172 if (oap->op_type != OP_COLON)
2173 stuffReadbuff((char_u *)"!");
2174 if (oap->op_type == OP_INDENT)
2175 {
2176#ifndef FEAT_CINDENT
2177 if (*get_equalprg() == NUL)
2178 stuffReadbuff((char_u *)"indent");
2179 else
2180#endif
2181 stuffReadbuff(get_equalprg());
2182 stuffReadbuff((char_u *)"\n");
2183 }
2184 else if (oap->op_type == OP_FORMAT)
2185 {
2186 if (*p_fp == NUL)
2187 stuffReadbuff((char_u *)"fmt");
2188 else
2189 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002190 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 }
2192
2193 /*
2194 * do_cmdline() does the rest
2195 */
2196}
2197
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002198/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002199 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002200 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002201 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002202op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002203 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002204{
2205#ifdef FEAT_EVAL
2206 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002207# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002208 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002209# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002210
2211 if (*p_opfunc == NUL)
2212 EMSG(_("E774: 'operatorfunc' is empty"));
2213 else
2214 {
2215 /* Set '[ and '] marks to text to be operated on. */
2216 curbuf->b_op_start = oap->start;
2217 curbuf->b_op_end = oap->end;
2218 if (oap->motion_type != MLINE && !oap->inclusive)
2219 /* Exclude the end position. */
2220 decl(&curbuf->b_op_end);
2221
2222 if (oap->block_mode)
2223 argv[0] = (char_u *)"block";
2224 else if (oap->motion_type == MLINE)
2225 argv[0] = (char_u *)"line";
2226 else
2227 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002228
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002229# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002230 /* Reset virtual_op so that 'virtualedit' can be changed in the
2231 * function. */
2232 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002233# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002234
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002235 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002236
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002237# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002238 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002239# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002240 }
2241#else
2242 EMSG(_("E775: Eval feature not available"));
2243#endif
2244}
2245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#if defined(FEAT_MOUSE) || defined(PROTO)
2247/*
2248 * Do the appropriate action for the current mouse click in the current mode.
2249 * Not used for Command-line mode.
2250 *
2251 * Normal Mode:
2252 * event modi- position visual change action
2253 * fier cursor window
2254 * left press - yes end yes
2255 * left press C yes end yes "^]" (2)
2256 * left press S yes end yes "*" (2)
2257 * left drag - yes start if moved no
2258 * left relse - yes start if moved no
2259 * middle press - yes if not active no put register
2260 * middle press - yes if active no yank and put
2261 * right press - yes start or extend yes
2262 * right press S yes no change yes "#" (2)
2263 * right drag - yes extend no
2264 * right relse - yes extend no
2265 *
2266 * Insert or Replace Mode:
2267 * event modi- position visual change action
2268 * fier cursor window
2269 * left press - yes (cannot be active) yes
2270 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2271 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2272 * left drag - yes start or extend (1) no CTRL-O (1)
2273 * left relse - yes start or extend (1) no CTRL-O (1)
2274 * middle press - no (cannot be active) no put register
2275 * right press - yes start or extend yes CTRL-O
2276 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2277 *
2278 * (1) only if mouse pointer moved since press
2279 * (2) only if click is in same buffer
2280 *
2281 * Return TRUE if start_arrow() should be called for edit mode.
2282 */
2283 int
2284do_mouse(oap, c, dir, count, fixindent)
2285 oparg_T *oap; /* operator argument, can be NULL */
2286 int c; /* K_LEFTMOUSE, etc */
2287 int dir; /* Direction to 'put' if necessary */
2288 long count;
2289 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2290{
2291 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2292 static int got_click = FALSE; /* got a click some time back */
2293
2294 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2295 int is_click; /* If FALSE it's a drag or release event */
2296 int is_drag; /* If TRUE it's a drag event */
2297 int jump_flags = 0; /* flags for jump_to_mouse() */
2298 pos_T start_visual;
2299 int moved; /* Has cursor moved? */
2300 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002301#ifdef FEAT_WINDOWS
2302 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#ifdef FEAT_VERTSPLIT
2305 int in_sep_line; /* mouse in vertical separator line */
2306#endif
2307 int c1, c2;
2308#if defined(FEAT_FOLDING)
2309 pos_T save_cursor;
2310#endif
2311 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 static pos_T orig_cursor;
2313 colnr_T leftcol, rightcol;
2314 pos_T end_visual;
2315 int diff;
2316 int old_active = VIsual_active;
2317 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 int regname;
2319
2320#if defined(FEAT_FOLDING)
2321 save_cursor = curwin->w_cursor;
2322#endif
2323
2324 /*
2325 * When GUI is active, always recognize mouse events, otherwise:
2326 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2327 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2328 * - For command line and insert mode 'mouse' is checked before calling
2329 * do_mouse().
2330 */
2331 if (do_always)
2332 do_always = FALSE;
2333 else
2334#ifdef FEAT_GUI
2335 if (!gui.in_use)
2336#endif
2337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (VIsual_active)
2339 {
2340 if (!mouse_has(MOUSE_VISUAL))
2341 return FALSE;
2342 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002343 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 return FALSE;
2345 }
2346
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002347 for (;;)
2348 {
2349 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2350 if (is_drag)
2351 {
2352 /* If the next character is the same mouse event then use that
2353 * one. Speeds up dragging the status line. */
2354 if (vpeekc() != NUL)
2355 {
2356 int nc;
2357 int save_mouse_row = mouse_row;
2358 int save_mouse_col = mouse_col;
2359
2360 /* Need to get the character, peeking doesn't get the actual
2361 * one. */
2362 nc = safe_vgetc();
2363 if (c == nc)
2364 continue;
2365 vungetc(nc);
2366 mouse_row = save_mouse_row;
2367 mouse_col = save_mouse_col;
2368 }
2369 }
2370 break;
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373#ifdef FEAT_MOUSESHAPE
2374 /* May have stopped dragging the status or separator line. The pointer is
2375 * most likely still on the status or separator line. */
2376 if (!is_drag && drag_status_line)
2377 {
2378 drag_status_line = FALSE;
2379 update_mouseshape(SHAPE_IDX_STATUS);
2380 }
2381# ifdef FEAT_VERTSPLIT
2382 if (!is_drag && drag_sep_line)
2383 {
2384 drag_sep_line = FALSE;
2385 update_mouseshape(SHAPE_IDX_VSEP);
2386 }
2387# endif
2388#endif
2389
2390 /*
2391 * Ignore drag and release events if we didn't get a click.
2392 */
2393 if (is_click)
2394 got_click = TRUE;
2395 else
2396 {
2397 if (!got_click) /* didn't get click, ignore */
2398 return FALSE;
2399 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002402#ifdef FEAT_WINDOWS
2403 if (in_tab_line)
2404 {
2405 in_tab_line = FALSE;
2406 return FALSE;
2407 }
2408#endif
2409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 /*
2413 * CTRL right mouse button does CTRL-T
2414 */
2415 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2416 {
2417 if (State & INSERT)
2418 stuffcharReadbuff(Ctrl_O);
2419 if (count > 1)
2420 stuffnumReadbuff(count);
2421 stuffcharReadbuff(Ctrl_T);
2422 got_click = FALSE; /* ignore drag&release now */
2423 return FALSE;
2424 }
2425
2426 /*
2427 * CTRL only works with left mouse button
2428 */
2429 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2430 return FALSE;
2431
2432 /*
2433 * When a modifier is down, ignore drag and release events, as well as
2434 * multiple clicks and the middle mouse button.
2435 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2436 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002437 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2438 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 && (!is_click
2440 || (mod_mask & MOD_MASK_MULTI_CLICK)
2441 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002442 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 && mouse_model_popup()
2444 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002445 && !((mod_mask & MOD_MASK_ALT)
2446 && !mouse_model_popup()
2447 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 )
2449 return FALSE;
2450
2451 /*
2452 * If the button press was used as the movement command for an operator
2453 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2454 * drag/release events.
2455 */
2456 if (!is_click && which_button == MOUSE_MIDDLE)
2457 return FALSE;
2458
2459 if (oap != NULL)
2460 regname = oap->regname;
2461 else
2462 regname = 0;
2463
2464 /*
2465 * Middle mouse button does a 'put' of the selected text
2466 */
2467 if (which_button == MOUSE_MIDDLE)
2468 {
2469 if (State == NORMAL)
2470 {
2471 /*
2472 * If an operator was pending, we don't know what the user wanted
2473 * to do. Go back to normal mode: Clear the operator and beep().
2474 */
2475 if (oap != NULL && oap->op_type != OP_NOP)
2476 {
2477 clearopbeep(oap);
2478 return FALSE;
2479 }
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 /*
2482 * If visual was active, yank the highlighted text and put it
2483 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002484 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
2486 if (VIsual_active)
2487 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002488 if (VIsual_select)
2489 {
2490 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002491 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002492 }
2493 else
2494 {
2495 stuffcharReadbuff('y');
2496 stuffcharReadbuff(K_MIDDLEMOUSE);
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 do_always = TRUE; /* ignore 'mouse' setting next time */
2499 return FALSE;
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * The rest is below jump_to_mouse()
2503 */
2504 }
2505
2506 else if ((State & INSERT) == 0)
2507 return FALSE;
2508
2509 /*
2510 * Middle click in insert mode doesn't move the mouse, just insert the
2511 * contents of a register. '.' register is special, can't insert that
2512 * with do_put().
2513 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2514 * happens for the GUI).
2515 */
2516 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2517 {
2518 if (regname == '.')
2519 insert_reg(regname, TRUE);
2520 else
2521 {
2522#ifdef FEAT_CLIPBOARD
2523 if (clip_star.available && regname == 0)
2524 regname = '*';
2525#endif
2526 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2527 insert_reg(regname, TRUE);
2528 else
2529 {
2530 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2531
2532 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2533 AppendCharToRedobuff(Ctrl_R);
2534 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2535 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2536 }
2537 }
2538 return FALSE;
2539 }
2540 }
2541
2542 /* When dragging or button-up stay in the same window. */
2543 if (!is_click)
2544 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2545
2546 start_visual.lnum = 0;
2547
Bram Moolenaarf740b292006-02-16 22:11:02 +00002548#ifdef FEAT_WINDOWS
2549 /* Check for clicking in the tab page line. */
2550 if (mouse_row == 0 && firstwin->w_winrow > 0)
2551 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002552 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002553 {
2554 if (in_tab_line)
2555 {
2556 c1 = TabPageIdxs[mouse_col];
2557 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2558 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002559 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002560 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002561
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562 /* click in a tab selects that tab page */
2563 if (is_click
2564# ifdef FEAT_CMDWIN
2565 && cmdwin_type == 0
2566# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002569 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002570 c1 = TabPageIdxs[mouse_col];
2571 if (c1 >= 0)
2572 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002573 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2574 {
2575 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002577 tabpage_new();
2578 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2579 }
2580 else
2581 {
2582 /* Go to specified tab page, or next one if not clicking
2583 * on a label. */
2584 goto_tabpage(c1);
2585
2586 /* It's like clicking on the status line of a window. */
2587 if (curwin != old_curwin)
2588 end_visual_mode();
2589 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002590 }
2591 else if (c1 < 0)
2592 {
2593 tabpage_T *tp;
2594
2595 /* Close the current or specified tab page. */
2596 if (c1 == -999)
2597 tp = curtab;
2598 else
2599 tp = find_tabpage(-c1);
2600 if (tp == curtab)
2601 {
2602 if (first_tabpage->tp_next != NULL)
2603 tabpage_close(FALSE);
2604 }
2605 else if (tp != NULL)
2606 tabpage_close_other(tp, FALSE);
2607 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002608 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002610 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002611 else if (is_drag && in_tab_line)
2612 {
2613 c1 = TabPageIdxs[mouse_col];
2614 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2615 return FALSE;
2616 }
2617
Bram Moolenaarf740b292006-02-16 22:11:02 +00002618#endif
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /*
2621 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2622 * right button up -> pop-up menu
2623 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002624 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 */
2626 if (mouse_model_popup())
2627 {
2628 if (which_button == MOUSE_RIGHT
2629 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2630 {
2631 /*
2632 * NOTE: Ignore right button down and drag mouse events.
2633 * Windows only shows the popup menu on the button up event.
2634 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002635#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2636 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 if (!is_click)
2638 return FALSE;
2639#endif
2640#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2641 if (is_click || is_drag)
2642 return FALSE;
2643#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002644#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2646 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2647 if (gui.in_use)
2648 {
2649 jump_flags = 0;
2650 if (STRCMP(p_mousem, "popup_setpos") == 0)
2651 {
2652 /* First set the cursor position before showing the popup
2653 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 if (VIsual_active)
2655 {
2656 pos_T m_pos;
2657
2658 /*
2659 * set MOUSE_MAY_STOP_VIS if we are outside the
2660 * selection or the current window (might have false
2661 * negative here)
2662 */
2663 if (mouse_row < W_WINROW(curwin)
2664 || mouse_row
2665 > (W_WINROW(curwin) + curwin->w_height))
2666 jump_flags = MOUSE_MAY_STOP_VIS;
2667 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2668 jump_flags = MOUSE_MAY_STOP_VIS;
2669 else
2670 {
2671 if ((lt(curwin->w_cursor, VIsual)
2672 && (lt(m_pos, curwin->w_cursor)
2673 || lt(VIsual, m_pos)))
2674 || (lt(VIsual, curwin->w_cursor)
2675 && (lt(m_pos, VIsual)
2676 || lt(curwin->w_cursor, m_pos))))
2677 {
2678 jump_flags = MOUSE_MAY_STOP_VIS;
2679 }
2680 else if (VIsual_mode == Ctrl_V)
2681 {
2682 getvcols(curwin, &curwin->w_cursor, &VIsual,
2683 &leftcol, &rightcol);
2684 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2685 if (m_pos.col < leftcol || m_pos.col > rightcol)
2686 jump_flags = MOUSE_MAY_STOP_VIS;
2687 }
2688 }
2689 }
2690 else
2691 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693 if (jump_flags)
2694 {
2695 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002696 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 setcursor();
2698 out_flush(); /* Update before showing popup menu */
2699 }
2700# ifdef FEAT_MENU
2701 gui_show_popupmenu();
2702# endif
2703 return (jump_flags & CURSOR_MOVED) != 0;
2704 }
2705 else
2706 return FALSE;
2707#else
2708 return FALSE;
2709#endif
2710 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002711 if (which_button == MOUSE_LEFT
2712 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
2714 which_button = MOUSE_RIGHT;
2715 mod_mask &= ~MOD_MASK_SHIFT;
2716 }
2717 }
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if ((State & (NORMAL | INSERT))
2720 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2721 {
2722 if (which_button == MOUSE_LEFT)
2723 {
2724 if (is_click)
2725 {
2726 /* stop Visual mode for a left click in a window, but not when
2727 * on a status line */
2728 if (VIsual_active)
2729 jump_flags |= MOUSE_MAY_STOP_VIS;
2730 }
2731 else if (mouse_has(MOUSE_VISUAL))
2732 jump_flags |= MOUSE_MAY_VIS;
2733 }
2734 else if (which_button == MOUSE_RIGHT)
2735 {
2736 if (is_click && VIsual_active)
2737 {
2738 /*
2739 * Remember the start and end of visual before moving the
2740 * cursor.
2741 */
2742 if (lt(curwin->w_cursor, VIsual))
2743 {
2744 start_visual = curwin->w_cursor;
2745 end_visual = VIsual;
2746 }
2747 else
2748 {
2749 start_visual = VIsual;
2750 end_visual = curwin->w_cursor;
2751 }
2752 }
2753 jump_flags |= MOUSE_FOCUS;
2754 if (mouse_has(MOUSE_VISUAL))
2755 jump_flags |= MOUSE_MAY_VIS;
2756 }
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 /*
2760 * If an operator is pending, ignore all drags and releases until the
2761 * next mouse click.
2762 */
2763 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2764 {
2765 got_click = FALSE;
2766 oap->motion_type = MCHAR;
2767 }
2768
2769 /* When releasing the button let jump_to_mouse() know. */
2770 if (!is_click && !is_drag)
2771 jump_flags |= MOUSE_RELEASED;
2772
2773 /*
2774 * JUMP!
2775 */
2776 jump_flags = jump_to_mouse(jump_flags,
2777 oap == NULL ? NULL : &(oap->inclusive), which_button);
2778 moved = (jump_flags & CURSOR_MOVED);
2779 in_status_line = (jump_flags & IN_STATUS_LINE);
2780#ifdef FEAT_VERTSPLIT
2781 in_sep_line = (jump_flags & IN_SEP_LINE);
2782#endif
2783
2784#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002785 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2787 {
2788 int key = KEY2TERMCAP1(c);
2789
2790 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2791 || key == (int)KE_RIGHTRELEASE)
2792 netbeans_button_release(which_button);
2793 }
2794#endif
2795
2796 /* When jumping to another window, clear a pending operator. That's a bit
2797 * friendlier than beeping and not jumping to that window. */
2798 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2799 clearop(oap);
2800
2801#ifdef FEAT_FOLDING
2802 if (mod_mask == 0
2803 && !is_drag
2804 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2805 && which_button == MOUSE_LEFT)
2806 {
2807 /* open or close a fold at this line */
2808 if (jump_flags & MOUSE_FOLD_OPEN)
2809 openFold(curwin->w_cursor.lnum, 1L);
2810 else
2811 closeFold(curwin->w_cursor.lnum, 1L);
2812 /* don't move the cursor if still in the same window */
2813 if (curwin == old_curwin)
2814 curwin->w_cursor = save_cursor;
2815 }
2816#endif
2817
2818#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2819 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2820 {
2821 clip_modeless(which_button, is_click, is_drag);
2822 return FALSE;
2823 }
2824#endif
2825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002827 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (VIsual_active && is_drag && p_so)
2829 {
2830 /* In the very first line, allow scrolling one line */
2831 if (mouse_row == 0)
2832 mouse_dragging = 2;
2833 else
2834 mouse_dragging = 1;
2835 }
2836
2837 /* When dragging the mouse above the window, scroll down. */
2838 if (is_drag && mouse_row < 0 && !in_status_line)
2839 {
2840 scroll_redraw(FALSE, 1L);
2841 mouse_row = 0;
2842 }
2843
2844 if (start_visual.lnum) /* right click in visual mode */
2845 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002846 /* When ALT is pressed make Visual mode blockwise. */
2847 if (mod_mask & MOD_MASK_ALT)
2848 VIsual_mode = Ctrl_V;
2849
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 /*
2851 * In Visual-block mode, divide the area in four, pick up the corner
2852 * that is in the quarter that the cursor is in.
2853 */
2854 if (VIsual_mode == Ctrl_V)
2855 {
2856 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2857 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2858 end_visual.col = leftcol;
2859 else
2860 end_visual.col = rightcol;
2861 if (curwin->w_cursor.lnum <
2862 (start_visual.lnum + end_visual.lnum) / 2)
2863 end_visual.lnum = end_visual.lnum;
2864 else
2865 end_visual.lnum = start_visual.lnum;
2866
2867 /* move VIsual to the right column */
2868 start_visual = curwin->w_cursor; /* save the cursor pos */
2869 curwin->w_cursor = end_visual;
2870 coladvance(end_visual.col);
2871 VIsual = curwin->w_cursor;
2872 curwin->w_cursor = start_visual; /* restore the cursor */
2873 }
2874 else
2875 {
2876 /*
2877 * If the click is before the start of visual, change the start.
2878 * If the click is after the end of visual, change the end. If
2879 * the click is inside the visual, change the closest side.
2880 */
2881 if (lt(curwin->w_cursor, start_visual))
2882 VIsual = end_visual;
2883 else if (lt(end_visual, curwin->w_cursor))
2884 VIsual = start_visual;
2885 else
2886 {
2887 /* In the same line, compare column number */
2888 if (end_visual.lnum == start_visual.lnum)
2889 {
2890 if (curwin->w_cursor.col - start_visual.col >
2891 end_visual.col - curwin->w_cursor.col)
2892 VIsual = start_visual;
2893 else
2894 VIsual = end_visual;
2895 }
2896
2897 /* In different lines, compare line number */
2898 else
2899 {
2900 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2901 (end_visual.lnum - curwin->w_cursor.lnum);
2902
2903 if (diff > 0) /* closest to end */
2904 VIsual = start_visual;
2905 else if (diff < 0) /* closest to start */
2906 VIsual = end_visual;
2907 else /* in the middle line */
2908 {
2909 if (curwin->w_cursor.col <
2910 (start_visual.col + end_visual.col) / 2)
2911 VIsual = end_visual;
2912 else
2913 VIsual = start_visual;
2914 }
2915 }
2916 }
2917 }
2918 }
2919 /*
2920 * If Visual mode started in insert mode, execute "CTRL-O"
2921 */
2922 else if ((State & INSERT) && VIsual_active)
2923 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
2926 * Middle mouse click: Put text before cursor.
2927 */
2928 if (which_button == MOUSE_MIDDLE)
2929 {
2930#ifdef FEAT_CLIPBOARD
2931 if (clip_star.available && regname == 0)
2932 regname = '*';
2933#endif
2934 if (yank_register_mline(regname))
2935 {
2936 if (mouse_past_bottom)
2937 dir = FORWARD;
2938 }
2939 else if (mouse_past_eol)
2940 dir = FORWARD;
2941
2942 if (fixindent)
2943 {
2944 c1 = (dir == BACKWARD) ? '[' : ']';
2945 c2 = 'p';
2946 }
2947 else
2948 {
2949 c1 = (dir == FORWARD) ? 'p' : 'P';
2950 c2 = NUL;
2951 }
2952 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2953
2954 /*
2955 * Remember where the paste started, so in edit() Insstart can be set
2956 * to this position
2957 */
2958 if (restart_edit != 0)
2959 where_paste_started = curwin->w_cursor;
2960 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2961 }
2962
2963#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2964 /*
2965 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2966 * error under the mouse pointer.
2967 */
2968 else if (((mod_mask & MOD_MASK_CTRL)
2969 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2970 && bt_quickfix(curbuf))
2971 {
2972 if (State & INSERT)
2973 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002974 if (curwin->w_llist_ref == NULL) /* quickfix window */
2975 stuffReadbuff((char_u *)":.cc\n");
2976 else /* location list window */
2977 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 got_click = FALSE; /* ignore drag&release now */
2979 }
2980#endif
2981
2982 /*
2983 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2984 * under the mouse pointer.
2985 */
2986 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2987 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2988 {
2989 if (State & INSERT)
2990 stuffcharReadbuff(Ctrl_O);
2991 stuffcharReadbuff(Ctrl_RSB);
2992 got_click = FALSE; /* ignore drag&release now */
2993 }
2994
2995 /*
2996 * Shift-Mouse click searches for the next occurrence of the word under
2997 * the mouse pointer
2998 */
2999 else if ((mod_mask & MOD_MASK_SHIFT))
3000 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003001 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 stuffcharReadbuff(Ctrl_O);
3003 if (which_button == MOUSE_LEFT)
3004 stuffcharReadbuff('*');
3005 else /* MOUSE_RIGHT */
3006 stuffcharReadbuff('#');
3007 }
3008
3009 /* Handle double clicks, unless on status line */
3010 else if (in_status_line)
3011 {
3012#ifdef FEAT_MOUSESHAPE
3013 if ((is_drag || is_click) && !drag_status_line)
3014 {
3015 drag_status_line = TRUE;
3016 update_mouseshape(-1);
3017 }
3018#endif
3019 }
3020#ifdef FEAT_VERTSPLIT
3021 else if (in_sep_line)
3022 {
3023# ifdef FEAT_MOUSESHAPE
3024 if ((is_drag || is_click) && !drag_sep_line)
3025 {
3026 drag_sep_line = TRUE;
3027 update_mouseshape(-1);
3028 }
3029# endif
3030 }
3031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3033 && mouse_has(MOUSE_VISUAL))
3034 {
3035 if (is_click || !VIsual_active)
3036 {
3037 if (VIsual_active)
3038 orig_cursor = VIsual;
3039 else
3040 {
3041 check_visual_highlight();
3042 VIsual = curwin->w_cursor;
3043 orig_cursor = VIsual;
3044 VIsual_active = TRUE;
3045 VIsual_reselect = TRUE;
3046 /* start Select mode if 'selectmode' contains "mouse" */
3047 may_start_select('o');
3048 setmouse();
3049 }
3050 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003051 {
3052 /* Double click with ALT pressed makes it blockwise. */
3053 if (mod_mask & MOD_MASK_ALT)
3054 VIsual_mode = Ctrl_V;
3055 else
3056 VIsual_mode = 'v';
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3059 VIsual_mode = 'V';
3060 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3061 VIsual_mode = Ctrl_V;
3062#ifdef FEAT_CLIPBOARD
3063 /* Make sure the clipboard gets updated. Needed because start and
3064 * end may still be the same, and the selection needs to be owned */
3065 clip_star.vmode = NUL;
3066#endif
3067 }
3068 /*
3069 * A double click selects a word or a block.
3070 */
3071 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3072 {
3073 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003074 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
3076 if (is_click)
3077 {
3078 /* If the character under the cursor (skipping white space) is
3079 * not a word character, try finding a match and select a (),
3080 * {}, [], #if/#endif, etc. block. */
3081 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003082 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 inc(&end_visual);
3084 if (oap != NULL)
3085 oap->motion_type = MCHAR;
3086 if (oap != NULL
3087 && VIsual_mode == 'v'
3088 && !vim_iswordc(gchar_pos(&end_visual))
3089 && equalpos(curwin->w_cursor, VIsual)
3090 && (pos = findmatch(oap, NUL)) != NULL)
3091 {
3092 curwin->w_cursor = *pos;
3093 if (oap->motion_type == MLINE)
3094 VIsual_mode = 'V';
3095 else if (*p_sel == 'e')
3096 {
3097 if (lt(curwin->w_cursor, VIsual))
3098 ++VIsual.col;
3099 else
3100 ++curwin->w_cursor.col;
3101 }
3102 }
3103 }
3104
3105 if (pos == NULL && (is_click || is_drag))
3106 {
3107 /* When not found a match or when dragging: extend to include
3108 * a word. */
3109 if (lt(curwin->w_cursor, orig_cursor))
3110 {
3111 find_start_of_word(&curwin->w_cursor);
3112 find_end_of_word(&VIsual);
3113 }
3114 else
3115 {
3116 find_start_of_word(&VIsual);
3117 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3118#ifdef FEAT_MBYTE
3119 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003120 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#else
3122 ++curwin->w_cursor.col;
3123#endif
3124 find_end_of_word(&curwin->w_cursor);
3125 }
3126 }
3127 curwin->w_set_curswant = TRUE;
3128 }
3129 if (is_click)
3130 redraw_curbuf_later(INVERTED); /* update the inversion */
3131 }
3132 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003133 {
3134 if (mod_mask & MOD_MASK_ALT)
3135 VIsual_mode = Ctrl_V;
3136 else
3137 VIsual_mode = 'v';
3138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003141 if ((!VIsual_active && old_active && mode_displayed)
3142 || (VIsual_active && p_smd && msg_silent == 0
3143 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145
3146 return moved;
3147}
3148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149/*
3150 * Move "pos" back to the start of the word it's in.
3151 */
3152 static void
3153find_start_of_word(pos)
3154 pos_T *pos;
3155{
3156 char_u *line;
3157 int cclass;
3158 int col;
3159
3160 line = ml_get(pos->lnum);
3161 cclass = get_mouse_class(line + pos->col);
3162
3163 while (pos->col > 0)
3164 {
3165 col = pos->col - 1;
3166#ifdef FEAT_MBYTE
3167 col -= (*mb_head_off)(line, line + col);
3168#endif
3169 if (get_mouse_class(line + col) != cclass)
3170 break;
3171 pos->col = col;
3172 }
3173}
3174
3175/*
3176 * Move "pos" forward to the end of the word it's in.
3177 * When 'selection' is "exclusive", the position is just after the word.
3178 */
3179 static void
3180find_end_of_word(pos)
3181 pos_T *pos;
3182{
3183 char_u *line;
3184 int cclass;
3185 int col;
3186
3187 line = ml_get(pos->lnum);
3188 if (*p_sel == 'e' && pos->col > 0)
3189 {
3190 --pos->col;
3191#ifdef FEAT_MBYTE
3192 pos->col -= (*mb_head_off)(line, line + pos->col);
3193#endif
3194 }
3195 cclass = get_mouse_class(line + pos->col);
3196 while (line[pos->col] != NUL)
3197 {
3198#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003199 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#else
3201 col = pos->col + 1;
3202#endif
3203 if (get_mouse_class(line + col) != cclass)
3204 {
3205 if (*p_sel == 'e')
3206 pos->col = col;
3207 break;
3208 }
3209 pos->col = col;
3210 }
3211}
3212
3213/*
3214 * Get class of a character for selection: same class means same word.
3215 * 0: blank
3216 * 1: punctuation groups
3217 * 2: normal word character
3218 * >2: multi-byte word character.
3219 */
3220 static int
3221get_mouse_class(p)
3222 char_u *p;
3223{
3224 int c;
3225
3226#ifdef FEAT_MBYTE
3227 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3228 return mb_get_class(p);
3229#endif
3230
3231 c = *p;
3232 if (c == ' ' || c == '\t')
3233 return 0;
3234
3235 if (vim_iswordc(c))
3236 return 2;
3237
3238 /*
3239 * There are a few special cases where we want certain combinations of
3240 * characters to be considered as a single word. These are things like
3241 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003242 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
3244 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3245 return 1;
3246 return c;
3247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248#endif /* FEAT_MOUSE */
3249
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250/*
3251 * Check if highlighting for visual mode is possible, give a warning message
3252 * if not.
3253 */
3254 void
3255check_visual_highlight()
3256{
3257 static int did_check = FALSE;
3258
3259 if (full_screen)
3260 {
3261 if (!did_check && hl_attr(HLF_V) == 0)
3262 MSG(_("Warning: terminal cannot highlight"));
3263 did_check = TRUE;
3264 }
3265}
3266
3267/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003268 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * This function should ALWAYS be called to end Visual mode, except from
3270 * do_pending_operator().
3271 */
3272 void
3273end_visual_mode()
3274{
3275#ifdef FEAT_CLIPBOARD
3276 /*
3277 * If we are using the clipboard, then remember what was selected in case
3278 * we need to paste it somewhere while we still own the selection.
3279 * Only do this when the clipboard is already owned. Don't want to grab
3280 * the selection when hitting ESC.
3281 */
3282 if (clip_star.available && clip_star.owned)
3283 clip_auto_select();
3284#endif
3285
3286 VIsual_active = FALSE;
3287#ifdef FEAT_MOUSE
3288 setmouse();
3289 mouse_dragging = 0;
3290#endif
3291
3292 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003293 curbuf->b_visual.vi_mode = VIsual_mode;
3294 curbuf->b_visual.vi_start = VIsual;
3295 curbuf->b_visual.vi_end = curwin->w_cursor;
3296 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#ifdef FEAT_EVAL
3298 curbuf->b_visual_mode_eval = VIsual_mode;
3299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef FEAT_VIRTUALEDIT
3301 if (!virtual_active())
3302 curwin->w_cursor.coladd = 0;
3303#endif
3304
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003305 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 clear_cmdline = TRUE; /* unshow visual mode later */
3307#ifdef FEAT_CMDL_INFO
3308 else
3309 clear_showcmd();
3310#endif
3311
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003312 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313}
3314
3315/*
3316 * Reset VIsual_active and VIsual_reselect.
3317 */
3318 void
3319reset_VIsual_and_resel()
3320{
3321 if (VIsual_active)
3322 {
3323 end_visual_mode();
3324 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3325 }
3326 VIsual_reselect = FALSE;
3327}
3328
3329/*
3330 * Reset VIsual_active and VIsual_reselect if it's set.
3331 */
3332 void
3333reset_VIsual()
3334{
3335 if (VIsual_active)
3336 {
3337 end_visual_mode();
3338 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3339 VIsual_reselect = FALSE;
3340 }
3341}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3345
3346/*
3347 * Check for a balloon-eval special item to include when searching for an
3348 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3349 * Returns TRUE if the character at "*ptr" should be included.
3350 * "dir" is FORWARD or BACKWARD, the direction of searching.
3351 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3352 * "bnp" points to a counter for square brackets.
3353 */
3354 static int
3355find_is_eval_item(ptr, colp, bnp, dir)
3356 char_u *ptr;
3357 int *colp;
3358 int *bnp;
3359 int dir;
3360{
3361 /* Accept everything inside []. */
3362 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3363 ++*bnp;
3364 if (*bnp > 0)
3365 {
3366 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3367 --*bnp;
3368 return TRUE;
3369 }
3370
3371 /* skip over "s.var" */
3372 if (*ptr == '.')
3373 return TRUE;
3374
3375 /* two-character item: s->var */
3376 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3377 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3378 {
3379 *colp += dir;
3380 return TRUE;
3381 }
3382 return FALSE;
3383}
3384#endif
3385
3386/*
3387 * Find the identifier under or to the right of the cursor.
3388 * "find_type" can have one of three values:
3389 * FIND_IDENT: find an identifier (keyword)
3390 * FIND_STRING: find any non-white string
3391 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003392 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * There are three steps:
3395 * 1. Search forward for the start of an identifier/string. Doesn't move if
3396 * already on one.
3397 * 2. Search backward for the start of this identifier/string.
3398 * This doesn't match the real Vi but I like it a little better and it
3399 * shouldn't bother anyone.
3400 * 3. Search forward to the end of this identifier/string.
3401 * When FIND_IDENT isn't defined, we backup until a blank.
3402 *
3403 * Returns the length of the string, or zero if no string is found.
3404 * If a string is found, a pointer to the string is put in "*string". This
3405 * string is not always NUL terminated.
3406 */
3407 int
3408find_ident_under_cursor(string, find_type)
3409 char_u **string;
3410 int find_type;
3411{
3412 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3413 curwin->w_cursor.col, string, find_type);
3414}
3415
3416/*
3417 * Like find_ident_under_cursor(), but for any window and any position.
3418 * However: Uses 'iskeyword' from the current window!.
3419 */
3420 int
3421find_ident_at_pos(wp, lnum, startcol, string, find_type)
3422 win_T *wp;
3423 linenr_T lnum;
3424 colnr_T startcol;
3425 char_u **string;
3426 int find_type;
3427{
3428 char_u *ptr;
3429 int col = 0; /* init to shut up GCC */
3430 int i;
3431#ifdef FEAT_MBYTE
3432 int this_class = 0;
3433 int prev_class;
3434 int prevcol;
3435#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 int bn = 0; /* bracket nesting */
3438#endif
3439
3440 /*
3441 * if i == 0: try to find an identifier
3442 * if i == 1: try to find any non-white string
3443 */
3444 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3445 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3446 {
3447 /*
3448 * 1. skip to start of identifier/string
3449 */
3450 col = startcol;
3451#ifdef FEAT_MBYTE
3452 if (has_mbyte)
3453 {
3454 while (ptr[col] != NUL)
3455 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 /* Stop at a ']' to evaluate "a[x]". */
3458 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3459 break;
3460# endif
3461 this_class = mb_get_class(ptr + col);
3462 if (this_class != 0 && (i == 1 || this_class != 1))
3463 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003464 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 }
3467 else
3468#endif
3469 while (ptr[col] != NUL
3470 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3473# endif
3474 )
3475 ++col;
3476
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* When starting on a ']' count it, so that we include the '['. */
3479 bn = ptr[col] == ']';
3480#endif
3481
3482 /*
3483 * 2. Back up to start of identifier/string.
3484 */
3485#ifdef FEAT_MBYTE
3486 if (has_mbyte)
3487 {
3488 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3491 this_class = mb_get_class((char_u *)"a");
3492 else
3493# endif
3494 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3498 prev_class = mb_get_class(ptr + prevcol);
3499 if (this_class != prev_class
3500 && (i == 0
3501 || prev_class == 0
3502 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003503# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 && (!(find_type & FIND_EVAL)
3505 || prevcol == 0
3506 || !find_is_eval_item(ptr + prevcol, &prevcol,
3507 &bn, BACKWARD))
3508# endif
3509 )
3510 break;
3511 col = prevcol;
3512 }
3513
3514 /* If we don't want just any old string, or we've found an
3515 * identifier, stop searching. */
3516 if (this_class > 2)
3517 this_class = 2;
3518 if (!(find_type & FIND_STRING) || this_class == 2)
3519 break;
3520 }
3521 else
3522#endif
3523 {
3524 while (col > 0
3525 && ((i == 0
3526 ? vim_iswordc(ptr[col - 1])
3527 : (!vim_iswhite(ptr[col - 1])
3528 && (!(find_type & FIND_IDENT)
3529 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 || ((find_type & FIND_EVAL)
3532 && col > 1
3533 && find_is_eval_item(ptr + col - 1, &col,
3534 &bn, BACKWARD))
3535#endif
3536 ))
3537 --col;
3538
3539 /* If we don't want just any old string, or we've found an
3540 * identifier, stop searching. */
3541 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3542 break;
3543 }
3544 }
3545
3546 if (ptr[col] == NUL || (i == 0 && (
3547#ifdef FEAT_MBYTE
3548 has_mbyte ? this_class != 2 :
3549#endif
3550 !vim_iswordc(ptr[col]))))
3551 {
3552 /*
3553 * didn't find an identifier or string
3554 */
3555 if (find_type & FIND_STRING)
3556 EMSG(_("E348: No string under cursor"));
3557 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003558 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 return 0;
3560 }
3561 ptr += col;
3562 *string = ptr;
3563
3564 /*
3565 * 3. Find the end if the identifier/string.
3566 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003567#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 bn = 0;
3569 startcol -= col;
3570#endif
3571 col = 0;
3572#ifdef FEAT_MBYTE
3573 if (has_mbyte)
3574 {
3575 /* Search for point of changing multibyte character class. */
3576 this_class = mb_get_class(ptr);
3577 while (ptr[col] != NUL
3578 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3579 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003580# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 || ((find_type & FIND_EVAL)
3582 && col <= (int)startcol
3583 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3584# endif
3585 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003586 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 else
3589#endif
3590 while ((i == 0 ? vim_iswordc(ptr[col])
3591 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003592# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || ((find_type & FIND_EVAL)
3594 && col <= (int)startcol
3595 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3596# endif
3597 )
3598 {
3599 ++col;
3600 }
3601
3602 return col;
3603}
3604
3605/*
3606 * Prepare for redo of a normal command.
3607 */
3608 static void
3609prep_redo_cmd(cap)
3610 cmdarg_T *cap;
3611{
3612 prep_redo(cap->oap->regname, cap->count0,
3613 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3614}
3615
3616/*
3617 * Prepare for redo of any command.
3618 * Note that only the last argument can be a multi-byte char.
3619 */
3620 static void
3621prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3622 int regname;
3623 long num;
3624 int cmd1;
3625 int cmd2;
3626 int cmd3;
3627 int cmd4;
3628 int cmd5;
3629{
3630 ResetRedobuff();
3631 if (regname != 0) /* yank from specified buffer */
3632 {
3633 AppendCharToRedobuff('"');
3634 AppendCharToRedobuff(regname);
3635 }
3636 if (num)
3637 AppendNumberToRedobuff(num);
3638
3639 if (cmd1 != NUL)
3640 AppendCharToRedobuff(cmd1);
3641 if (cmd2 != NUL)
3642 AppendCharToRedobuff(cmd2);
3643 if (cmd3 != NUL)
3644 AppendCharToRedobuff(cmd3);
3645 if (cmd4 != NUL)
3646 AppendCharToRedobuff(cmd4);
3647 if (cmd5 != NUL)
3648 AppendCharToRedobuff(cmd5);
3649}
3650
3651/*
3652 * check for operator active and clear it
3653 *
3654 * return TRUE if operator was active
3655 */
3656 static int
3657checkclearop(oap)
3658 oparg_T *oap;
3659{
3660 if (oap->op_type == OP_NOP)
3661 return FALSE;
3662 clearopbeep(oap);
3663 return TRUE;
3664}
3665
3666/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003667 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003669 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 */
3671 static int
3672checkclearopq(oap)
3673 oparg_T *oap;
3674{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003675 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FALSE;
3677 clearopbeep(oap);
3678 return TRUE;
3679}
3680
3681 static void
3682clearop(oap)
3683 oparg_T *oap;
3684{
3685 oap->op_type = OP_NOP;
3686 oap->regname = 0;
3687 oap->motion_force = NUL;
3688 oap->use_reg_one = FALSE;
3689}
3690
3691 static void
3692clearopbeep(oap)
3693 oparg_T *oap;
3694{
3695 clearop(oap);
3696 beep_flush();
3697}
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699/*
3700 * Remove the shift modifier from a special key.
3701 */
3702 static void
3703unshift_special(cap)
3704 cmdarg_T *cap;
3705{
3706 switch (cap->cmdchar)
3707 {
3708 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3709 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3710 case K_S_UP: cap->cmdchar = K_UP; break;
3711 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3712 case K_S_HOME: cap->cmdchar = K_HOME; break;
3713 case K_S_END: cap->cmdchar = K_END; break;
3714 }
3715 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
3718#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3719/*
3720 * Routines for displaying a partly typed command
3721 */
3722
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003723#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724static char_u showcmd_buf[SHOWCMD_BUFLEN];
3725static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3726static int showcmd_is_clear = TRUE;
3727static int showcmd_visual = FALSE;
3728
3729static void display_showcmd __ARGS((void));
3730
3731 void
3732clear_showcmd()
3733{
3734 if (!p_sc)
3735 return;
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (VIsual_active && !char_avail())
3738 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003739 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 long lines;
3741 colnr_T leftcol, rightcol;
3742 linenr_T top, bot;
3743
3744 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003745 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 top = VIsual.lnum;
3748 bot = curwin->w_cursor.lnum;
3749 }
3750 else
3751 {
3752 top = curwin->w_cursor.lnum;
3753 bot = VIsual.lnum;
3754 }
3755# ifdef FEAT_FOLDING
3756 /* Include closed folds as a whole. */
3757 hasFolding(top, &top, NULL);
3758 hasFolding(bot, NULL, &bot);
3759# endif
3760 lines = bot - top + 1;
3761
3762 if (VIsual_mode == Ctrl_V)
3763 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003764# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003765 char_u *saved_sbr = p_sbr;
3766
3767 /* Make 'sbr' empty for a moment to get the correct size. */
3768 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003771# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003772 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3775 (long)(rightcol - leftcol + 1));
3776 }
3777 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3778 sprintf((char *)showcmd_buf, "%ld", lines);
3779 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003780 {
3781 char_u *s, *e;
3782 int l;
3783 int bytes = 0;
3784 int chars = 0;
3785
3786 if (cursor_bot)
3787 {
3788 s = ml_get_pos(&VIsual);
3789 e = ml_get_cursor();
3790 }
3791 else
3792 {
3793 s = ml_get_cursor();
3794 e = ml_get_pos(&VIsual);
3795 }
3796 while ((*p_sel != 'e') ? s <= e : s < e)
3797 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003798# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003799 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003800# else
3801 l = (*s == NUL) ? 0 : 1;
3802# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003803 if (l == 0)
3804 {
3805 ++bytes;
3806 ++chars;
3807 break; /* end of line */
3808 }
3809 bytes += l;
3810 ++chars;
3811 s += l;
3812 }
3813 if (bytes == chars)
3814 sprintf((char *)showcmd_buf, "%d", chars);
3815 else
3816 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3819 showcmd_visual = TRUE;
3820 }
3821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
3823 showcmd_buf[0] = NUL;
3824 showcmd_visual = FALSE;
3825
3826 /* Don't actually display something if there is nothing to clear. */
3827 if (showcmd_is_clear)
3828 return;
3829 }
3830
3831 display_showcmd();
3832}
3833
3834/*
3835 * Add 'c' to string of shown command chars.
3836 * Return TRUE if output has been written (and setcursor() has been called).
3837 */
3838 int
3839add_to_showcmd(c)
3840 int c;
3841{
3842 char_u *p;
3843 int old_len;
3844 int extra_len;
3845 int overflow;
3846#if defined(FEAT_MOUSE)
3847 int i;
3848 static int ignore[] =
3849 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003850# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3852 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 K_IGNORE,
3855 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3856 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3857 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003858 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003860 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 0
3862 };
3863#endif
3864
Bram Moolenaar09df3122006-01-23 22:23:09 +00003865 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return FALSE;
3867
3868 if (showcmd_visual)
3869 {
3870 showcmd_buf[0] = NUL;
3871 showcmd_visual = FALSE;
3872 }
3873
3874#if defined(FEAT_MOUSE)
3875 /* Ignore keys that are scrollbar updates and mouse clicks */
3876 if (IS_SPECIAL(c))
3877 for (i = 0; ignore[i] != 0; ++i)
3878 if (ignore[i] == c)
3879 return FALSE;
3880#endif
3881
3882 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003883 if (*p == ' ')
3884 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 old_len = (int)STRLEN(showcmd_buf);
3886 extra_len = (int)STRLEN(p);
3887 overflow = old_len + extra_len - SHOWCMD_COLS;
3888 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003889 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3890 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 STRCAT(showcmd_buf, p);
3892
3893 if (char_avail())
3894 return FALSE;
3895
3896 display_showcmd();
3897
3898 return TRUE;
3899}
3900
3901 void
3902add_to_showcmd_c(c)
3903 int c;
3904{
3905 if (!add_to_showcmd(c))
3906 setcursor();
3907}
3908
3909/*
3910 * Delete 'len' characters from the end of the shown command.
3911 */
3912 static void
3913del_from_showcmd(len)
3914 int len;
3915{
3916 int old_len;
3917
3918 if (!p_sc)
3919 return;
3920
3921 old_len = (int)STRLEN(showcmd_buf);
3922 if (len > old_len)
3923 len = old_len;
3924 showcmd_buf[old_len - len] = NUL;
3925
3926 if (!char_avail())
3927 display_showcmd();
3928}
3929
3930/*
3931 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3932 * something and there is a partial mapping.
3933 */
3934 void
3935push_showcmd()
3936{
3937 if (p_sc)
3938 STRCPY(old_showcmd_buf, showcmd_buf);
3939}
3940
3941 void
3942pop_showcmd()
3943{
3944 if (!p_sc)
3945 return;
3946
3947 STRCPY(showcmd_buf, old_showcmd_buf);
3948
3949 display_showcmd();
3950}
3951
3952 static void
3953display_showcmd()
3954{
3955 int len;
3956
3957 cursor_off();
3958
3959 len = (int)STRLEN(showcmd_buf);
3960 if (len == 0)
3961 showcmd_is_clear = TRUE;
3962 else
3963 {
3964 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3965 showcmd_is_clear = FALSE;
3966 }
3967
3968 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003969 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3970 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 */
3972 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3973
3974 setcursor(); /* put cursor back where it belongs */
3975}
3976#endif
3977
3978#ifdef FEAT_SCROLLBIND
3979/*
3980 * When "check" is FALSE, prepare for commands that scroll the window.
3981 * When "check" is TRUE, take care of scroll-binding after the window has
3982 * scrolled. Called from normal_cmd() and edit().
3983 */
3984 void
3985do_check_scrollbind(check)
3986 int check;
3987{
3988 static win_T *old_curwin = NULL;
3989 static linenr_T old_topline = 0;
3990#ifdef FEAT_DIFF
3991 static int old_topfill = 0;
3992#endif
3993 static buf_T *old_buf = NULL;
3994 static colnr_T old_leftcol = 0;
3995
3996 if (check && curwin->w_p_scb)
3997 {
3998 /* If a ":syncbind" command was just used, don't scroll, only reset
3999 * the values. */
4000 if (did_syncbind)
4001 did_syncbind = FALSE;
4002 else if (curwin == old_curwin)
4003 {
4004 /*
4005 * Synchronize other windows, as necessary according to
4006 * 'scrollbind'. Don't do this after an ":edit" command, except
4007 * when 'diff' is set.
4008 */
4009 if ((curwin->w_buffer == old_buf
4010#ifdef FEAT_DIFF
4011 || curwin->w_p_diff
4012#endif
4013 )
4014 && (curwin->w_topline != old_topline
4015#ifdef FEAT_DIFF
4016 || curwin->w_topfill != old_topfill
4017#endif
4018 || curwin->w_leftcol != old_leftcol))
4019 {
4020 check_scrollbind(curwin->w_topline - old_topline,
4021 (long)(curwin->w_leftcol - old_leftcol));
4022 }
4023 }
4024 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4025 {
4026 /*
4027 * When switching between windows, make sure that the relative
4028 * vertical offset is valid for the new window. The relative
4029 * offset is invalid whenever another 'scrollbind' window has
4030 * scrolled to a point that would force the current window to
4031 * scroll past the beginning or end of its buffer. When the
4032 * resync is performed, some of the other 'scrollbind' windows may
4033 * need to jump so that the current window's relative position is
4034 * visible on-screen.
4035 */
4036 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4037 }
4038 curwin->w_scbind_pos = curwin->w_topline;
4039 }
4040
4041 old_curwin = curwin;
4042 old_topline = curwin->w_topline;
4043#ifdef FEAT_DIFF
4044 old_topfill = curwin->w_topfill;
4045#endif
4046 old_buf = curwin->w_buffer;
4047 old_leftcol = curwin->w_leftcol;
4048}
4049
4050/*
4051 * Synchronize any windows that have "scrollbind" set, based on the
4052 * number of rows by which the current window has changed
4053 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4054 */
4055 void
4056check_scrollbind(topline_diff, leftcol_diff)
4057 linenr_T topline_diff;
4058 long leftcol_diff;
4059{
4060 int want_ver;
4061 int want_hor;
4062 win_T *old_curwin = curwin;
4063 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 int old_VIsual_select = VIsual_select;
4065 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 colnr_T tgt_leftcol = curwin->w_leftcol;
4067 long topline;
4068 long y;
4069
4070 /*
4071 * check 'scrollopt' string for vertical and horizontal scroll options
4072 */
4073 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4074#ifdef FEAT_DIFF
4075 want_ver |= old_curwin->w_p_diff;
4076#endif
4077 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4078
4079 /*
4080 * loop through the scrollbound windows and scroll accordingly
4081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4084 {
4085 curbuf = curwin->w_buffer;
4086 /* skip original window and windows with 'noscrollbind' */
4087 if (curwin != old_curwin && curwin->w_p_scb)
4088 {
4089 /*
4090 * do the vertical scroll
4091 */
4092 if (want_ver)
4093 {
4094#ifdef FEAT_DIFF
4095 if (old_curwin->w_p_diff && curwin->w_p_diff)
4096 {
4097 diff_set_topline(old_curwin, curwin);
4098 }
4099 else
4100#endif
4101 {
4102 curwin->w_scbind_pos += topline_diff;
4103 topline = curwin->w_scbind_pos;
4104 if (topline > curbuf->b_ml.ml_line_count)
4105 topline = curbuf->b_ml.ml_line_count;
4106 if (topline < 1)
4107 topline = 1;
4108
4109 y = topline - curwin->w_topline;
4110 if (y > 0)
4111 scrollup(y, FALSE);
4112 else
4113 scrolldown(-y, FALSE);
4114 }
4115
4116 redraw_later(VALID);
4117 cursor_correct();
4118#ifdef FEAT_WINDOWS
4119 curwin->w_redr_status = TRUE;
4120#endif
4121 }
4122
4123 /*
4124 * do the horizontal scroll
4125 */
4126 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4127 {
4128 curwin->w_leftcol = tgt_leftcol;
4129 leftcol_changed();
4130 }
4131 }
4132 }
4133
4134 /*
4135 * reset current-window
4136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 VIsual_select = old_VIsual_select;
4138 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 curwin = old_curwin;
4140 curbuf = old_curbuf;
4141}
4142#endif /* #ifdef FEAT_SCROLLBIND */
4143
4144/*
4145 * Command character that's ignored.
4146 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004147 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 static void
4150nv_ignore(cap)
4151 cmdarg_T *cap;
4152{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004153 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155
4156/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004157 * Command character that doesn't do anything, but unlike nv_ignore() does
4158 * start edit(). Used for "startinsert" executed while starting up.
4159 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004160 static void
4161nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004162 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004163{
4164}
4165
4166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Command character doesn't exist.
4168 */
4169 static void
4170nv_error(cap)
4171 cmdarg_T *cap;
4172{
4173 clearopbeep(cap->oap);
4174}
4175
4176/*
4177 * <Help> and <F1> commands.
4178 */
4179 static void
4180nv_help(cap)
4181 cmdarg_T *cap;
4182{
4183 if (!checkclearopq(cap->oap))
4184 ex_help(NULL);
4185}
4186
4187/*
4188 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4189 */
4190 static void
4191nv_addsub(cap)
4192 cmdarg_T *cap;
4193{
4194 if (!checkclearopq(cap->oap)
4195 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4196 prep_redo_cmd(cap);
4197}
4198
4199/*
4200 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4201 */
4202 static void
4203nv_page(cap)
4204 cmdarg_T *cap;
4205{
4206 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004207 {
4208#ifdef FEAT_WINDOWS
4209 if (mod_mask & MOD_MASK_CTRL)
4210 {
4211 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4212 if (cap->arg == BACKWARD)
4213 goto_tabpage(-(int)cap->count1);
4214 else
4215 goto_tabpage((int)cap->count0);
4216 }
4217 else
4218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
4223/*
4224 * Implementation of "gd" and "gD" command.
4225 */
4226 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004227nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004228 oparg_T *oap;
4229 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004230 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 char_u *ptr;
4234
Bram Moolenaard9d30582005-05-18 22:10:28 +00004235 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004236 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238#ifdef FEAT_FOLDING
4239 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4240 foldOpenCursor();
4241#endif
4242}
4243
4244/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004245 * Search for variable declaration of "ptr[len]".
4246 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4247 * current file ("gD").
4248 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004249 * Return FAIL when not found.
4250 */
4251 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004252find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004253 char_u *ptr;
4254 int len;
4255 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004256 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004257 int searchflags; /* flags passed to searchit() */
4258{
4259 char_u *pat;
4260 pos_T old_pos;
4261 pos_T par_pos;
4262 pos_T found_pos;
4263 int t;
4264 int save_p_ws;
4265 int save_p_scs;
4266 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004267 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004268
4269 if ((pat = alloc(len + 7)) == NULL)
4270 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004271
4272 /* Put "\V" before the pattern to avoid that the special meaning of "."
4273 * and "~" causes trouble. */
4274 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4275 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 old_pos = curwin->w_cursor;
4277 save_p_ws = p_ws;
4278 save_p_scs = p_scs;
4279 p_ws = FALSE; /* don't wrap around end of file now */
4280 p_scs = FALSE; /* don't switch ignorecase off now */
4281
4282 /*
4283 * With "gD" go to line 1.
4284 * With "gd" Search back for the start of the current function, then go
4285 * back until a blank line. If this fails go to line 1.
4286 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004287 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 {
4289 setpcmark(); /* Set in findpar() otherwise */
4290 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004291 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 else
4294 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004295 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4297 --curwin->w_cursor.lnum;
4298 }
4299 curwin->w_cursor.col = 0;
4300
4301 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004302 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303 for (;;)
4304 {
4305 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004306 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 if (curwin->w_cursor.lnum >= old_pos.lnum)
4308 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004309
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004310 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004311 {
4312 pos_T *pos;
4313
4314 /* Check that the block the match is in doesn't end before the
4315 * position where we started the search from. */
4316 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4317 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4318 && pos->lnum < old_pos.lnum)
4319 continue;
4320 }
4321
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004322 if (t == FAIL)
4323 {
4324 /* If we previously found a valid position, use it. */
4325 if (found_pos.lnum != 0)
4326 {
4327 curwin->w_cursor = found_pos;
4328 t = OK;
4329 }
4330 break;
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004333 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004334 {
4335 /* Ignore this line, continue at start of next line. */
4336 ++curwin->w_cursor.lnum;
4337 curwin->w_cursor.col = 0;
4338 continue;
4339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004341 if (!locally) /* global search: use first match found */
4342 break;
4343 if (curwin->w_cursor.lnum >= par_pos.lnum)
4344 {
4345 /* If we previously found a valid position, use it. */
4346 if (found_pos.lnum != 0)
4347 curwin->w_cursor = found_pos;
4348 break;
4349 }
4350
4351 /* For finding a local variable and the match is before the "{" search
4352 * to find a later match. For K&R style function declarations this
4353 * skips the function header without types. */
4354 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356
4357 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004359 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 curwin->w_cursor = old_pos;
4361 }
4362 else
4363 {
4364 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /* "n" searches forward now */
4366 reset_search_dir();
4367 }
4368
4369 vim_free(pat);
4370 p_ws = save_p_ws;
4371 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372
4373 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374}
4375
4376/*
4377 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4378 * lines rather than lines in the file.
4379 * 'dist' must be positive.
4380 *
4381 * Return OK if able to move cursor, FAIL otherwise.
4382 */
4383 static int
4384nv_screengo(oap, dir, dist)
4385 oparg_T *oap;
4386 int dir;
4387 long dist;
4388{
4389 int linelen = linetabsize(ml_get_curline());
4390 int retval = OK;
4391 int atend = FALSE;
4392 int n;
4393 int col_off1; /* margin offset for first screen line */
4394 int col_off2; /* margin offset for wrapped screen line */
4395 int width1; /* text width for first screen line */
4396 int width2; /* test width for wrapped screen line */
4397
4398 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004399 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 col_off1 = curwin_col_off();
4402 col_off2 = col_off1 - curwin_col_off2();
4403 width1 = W_WIDTH(curwin) - col_off1;
4404 width2 = W_WIDTH(curwin) - col_off2;
4405
4406#ifdef FEAT_VERTSPLIT
4407 if (curwin->w_width != 0)
4408 {
4409#endif
4410 /*
4411 * Instead of sticking at the last character of the buffer line we
4412 * try to stick in the last column of the screen.
4413 */
4414 if (curwin->w_curswant == MAXCOL)
4415 {
4416 atend = TRUE;
4417 validate_virtcol();
4418 if (width1 <= 0)
4419 curwin->w_curswant = 0;
4420 else
4421 {
4422 curwin->w_curswant = width1 - 1;
4423 if (curwin->w_virtcol > curwin->w_curswant)
4424 curwin->w_curswant += ((curwin->w_virtcol
4425 - curwin->w_curswant - 1) / width2 + 1) * width2;
4426 }
4427 }
4428 else
4429 {
4430 if (linelen > width1)
4431 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4432 else
4433 n = width1;
4434 if (curwin->w_curswant > (colnr_T)n + 1)
4435 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4436 * width2;
4437 }
4438
4439 while (dist--)
4440 {
4441 if (dir == BACKWARD)
4442 {
4443 if ((long)curwin->w_curswant >= width2)
4444 /* move back within line */
4445 curwin->w_curswant -= width2;
4446 else
4447 {
4448 /* to previous line */
4449 if (curwin->w_cursor.lnum == 1)
4450 {
4451 retval = FAIL;
4452 break;
4453 }
4454 --curwin->w_cursor.lnum;
4455#ifdef FEAT_FOLDING
4456 /* Move to the start of a closed fold. Don't do that when
4457 * 'foldopen' contains "all": it will open in a moment. */
4458 if (!(fdo_flags & FDO_ALL))
4459 (void)hasFolding(curwin->w_cursor.lnum,
4460 &curwin->w_cursor.lnum, NULL);
4461#endif
4462 linelen = linetabsize(ml_get_curline());
4463 if (linelen > width1)
4464 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4465 + 1) * width2;
4466 }
4467 }
4468 else /* dir == FORWARD */
4469 {
4470 if (linelen > width1)
4471 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4472 else
4473 n = width1;
4474 if (curwin->w_curswant + width2 < (colnr_T)n)
4475 /* move forward within line */
4476 curwin->w_curswant += width2;
4477 else
4478 {
4479 /* to next line */
4480#ifdef FEAT_FOLDING
4481 /* Move to the end of a closed fold. */
4482 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4483 &curwin->w_cursor.lnum);
4484#endif
4485 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4486 {
4487 retval = FAIL;
4488 break;
4489 }
4490 curwin->w_cursor.lnum++;
4491 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004492 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 }
4495 }
4496#ifdef FEAT_VERTSPLIT
4497 }
4498#endif
4499
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004500 if (virtual_active() && atend)
4501 coladvance(MAXCOL);
4502 else
4503 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4506 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4507 {
4508 /*
4509 * Check for landing on a character that got split at the end of the
4510 * last line. We want to advance a screenline, not end up in the same
4511 * screenline or move two screenlines.
4512 */
4513 validate_virtcol();
4514 if (curwin->w_virtcol > curwin->w_curswant
4515 && (curwin->w_curswant < (colnr_T)width1
4516 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4517 : ((curwin->w_curswant - width1) % width2
4518 > (colnr_T)width2 / 2)))
4519 --curwin->w_cursor.col;
4520 }
4521#endif
4522
4523 if (atend)
4524 curwin->w_curswant = MAXCOL; /* stick in the last column */
4525
4526 return retval;
4527}
4528
4529#ifdef FEAT_MOUSE
4530/*
4531 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4532 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004533 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4534 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 */
4536 static void
4537nv_mousescroll(cap)
4538 cmdarg_T *cap;
4539{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004540# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004541 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004543 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 int row, col;
4546
4547 row = mouse_row;
4548 col = mouse_col;
4549
4550 /* find the window at the pointer coordinates */
4551 curwin = mouse_find_win(&row, &col);
4552 curbuf = curwin->w_buffer;
4553 }
4554# endif
4555
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004556 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004558 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4559 {
4560 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4561 }
4562 else
4563 {
4564 cap->count1 = 3;
4565 cap->count0 = 3;
4566 nv_scroll_line(cap);
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004569# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else
4571 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004572 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4573 if (!curwin->w_p_wrap)
4574 {
4575 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004576
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004577 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4578 step = W_WIDTH(curwin);
4579 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4580 if (val < 0)
4581 val = 0;
4582
4583 gui_do_horiz_scroll(val, TRUE);
4584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004588# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 curwin->w_redr_status = TRUE;
4590
4591 curwin = old_curwin;
4592 curbuf = curwin->w_buffer;
4593# endif
4594}
4595
4596/*
4597 * Mouse clicks and drags.
4598 */
4599 static void
4600nv_mouse(cap)
4601 cmdarg_T *cap;
4602{
4603 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4604}
4605#endif
4606
4607/*
4608 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4609 * cap->arg must be TRUE for CTRL-E.
4610 */
4611 static void
4612nv_scroll_line(cap)
4613 cmdarg_T *cap;
4614{
4615 if (!checkclearop(cap->oap))
4616 scroll_redraw(cap->arg, cap->count1);
4617}
4618
4619/*
4620 * Scroll "count" lines up or down, and redraw.
4621 */
4622 void
4623scroll_redraw(up, count)
4624 int up;
4625 long count;
4626{
4627 linenr_T prev_topline = curwin->w_topline;
4628#ifdef FEAT_DIFF
4629 int prev_topfill = curwin->w_topfill;
4630#endif
4631 linenr_T prev_lnum = curwin->w_cursor.lnum;
4632
4633 if (up)
4634 scrollup(count, TRUE);
4635 else
4636 scrolldown(count, TRUE);
4637 if (p_so)
4638 {
4639 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4640 * valid, otherwise the screen jumps back at the end of the file. */
4641 cursor_correct();
4642 check_cursor_moved(curwin);
4643 curwin->w_valid |= VALID_TOPLINE;
4644
4645 /* If moved back to where we were, at least move the cursor, otherwise
4646 * we get stuck at one position. Don't move the cursor up if the
4647 * first line of the buffer is already on the screen */
4648 while (curwin->w_topline == prev_topline
4649#ifdef FEAT_DIFF
4650 && curwin->w_topfill == prev_topfill
4651#endif
4652 )
4653 {
4654 if (up)
4655 {
4656 if (curwin->w_cursor.lnum > prev_lnum
4657 || cursor_down(1L, FALSE) == FAIL)
4658 break;
4659 }
4660 else
4661 {
4662 if (curwin->w_cursor.lnum < prev_lnum
4663 || prev_topline == 1L
4664 || cursor_up(1L, FALSE) == FAIL)
4665 break;
4666 }
4667 /* Mark w_topline as valid, otherwise the screen jumps back at the
4668 * end of the file. */
4669 check_cursor_moved(curwin);
4670 curwin->w_valid |= VALID_TOPLINE;
4671 }
4672 }
4673 if (curwin->w_cursor.lnum != prev_lnum)
4674 coladvance(curwin->w_curswant);
4675 redraw_later(VALID);
4676}
4677
4678/*
4679 * Commands that start with "z".
4680 */
4681 static void
4682nv_zet(cap)
4683 cmdarg_T *cap;
4684{
4685 long n;
4686 colnr_T col;
4687 int nchar = cap->nchar;
4688#ifdef FEAT_FOLDING
4689 long old_fdl = curwin->w_p_fdl;
4690 int old_fen = curwin->w_p_fen;
4691#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004692#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004693 int undo = FALSE;
4694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 if (VIM_ISDIGIT(nchar))
4697 {
4698 /*
4699 * "z123{nchar}": edit the count before obtaining {nchar}
4700 */
4701 if (checkclearop(cap->oap))
4702 return;
4703 n = nchar - '0';
4704 for (;;)
4705 {
4706#ifdef USE_ON_FLY_SCROLL
4707 dont_scroll = TRUE; /* disallow scrolling here */
4708#endif
4709 ++no_mapping;
4710 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004711 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 --no_mapping;
4714 --allow_keys;
4715#ifdef FEAT_CMDL_INFO
4716 (void)add_to_showcmd(nchar);
4717#endif
4718 if (nchar == K_DEL || nchar == K_KDEL)
4719 n /= 10;
4720 else if (VIM_ISDIGIT(nchar))
4721 n = n * 10 + (nchar - '0');
4722 else if (nchar == CAR)
4723 {
4724#ifdef FEAT_GUI
4725 need_mouse_correct = TRUE;
4726#endif
4727 win_setheight((int)n);
4728 break;
4729 }
4730 else if (nchar == 'l'
4731 || nchar == 'h'
4732 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004733 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 cap->count1 = n ? n * cap->count1 : cap->count1;
4736 goto dozet;
4737 }
4738 else
4739 {
4740 clearopbeep(cap->oap);
4741 break;
4742 }
4743 }
4744 cap->oap->op_type = OP_NOP;
4745 return;
4746 }
4747
4748dozet:
4749 if (
4750#ifdef FEAT_FOLDING
4751 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4752 * and "zC" only in Visual mode. "zj" and "zk" are motion
4753 * commands. */
4754 cap->nchar != 'f' && cap->nchar != 'F'
4755 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4756 && cap->nchar != 'j' && cap->nchar != 'k'
4757 &&
4758#endif
4759 checkclearop(cap->oap))
4760 return;
4761
4762 /*
4763 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4764 * If line number given, set cursor.
4765 */
4766 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4767 && cap->count0
4768 && cap->count0 != curwin->w_cursor.lnum)
4769 {
4770 setpcmark();
4771 if (cap->count0 > curbuf->b_ml.ml_line_count)
4772 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4773 else
4774 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004775 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777
4778 switch (nchar)
4779 {
4780 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4781 case '+':
4782 if (cap->count0 == 0)
4783 {
4784 /* No count given: put cursor at the line below screen */
4785 validate_botline(); /* make sure w_botline is valid */
4786 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4787 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4788 else
4789 curwin->w_cursor.lnum = curwin->w_botline;
4790 }
4791 /* FALLTHROUGH */
4792 case NL:
4793 case CAR:
4794 case K_KENTER:
4795 beginline(BL_WHITE | BL_FIX);
4796 /* FALLTHROUGH */
4797
4798 case 't': scroll_cursor_top(0, TRUE);
4799 redraw_later(VALID);
4800 break;
4801
4802 /* "z." and "zz": put cursor in middle of screen */
4803 case '.': beginline(BL_WHITE | BL_FIX);
4804 /* FALLTHROUGH */
4805
4806 case 'z': scroll_cursor_halfway(TRUE);
4807 redraw_later(VALID);
4808 break;
4809
4810 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4811 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4812 * when <count> is at bottom of window, and puts that one at
4813 * bottom of window. */
4814 if (cap->count0 != 0)
4815 {
4816 scroll_cursor_bot(0, TRUE);
4817 curwin->w_cursor.lnum = curwin->w_topline;
4818 }
4819 else if (curwin->w_topline == 1)
4820 curwin->w_cursor.lnum = 1;
4821 else
4822 curwin->w_cursor.lnum = curwin->w_topline - 1;
4823 /* FALLTHROUGH */
4824 case '-':
4825 beginline(BL_WHITE | BL_FIX);
4826 /* FALLTHROUGH */
4827
4828 case 'b': scroll_cursor_bot(0, TRUE);
4829 redraw_later(VALID);
4830 break;
4831
4832 /* "zH" - scroll screen right half-page */
4833 case 'H':
4834 cap->count1 *= W_WIDTH(curwin) / 2;
4835 /* FALLTHROUGH */
4836
4837 /* "zh" - scroll screen to the right */
4838 case 'h':
4839 case K_LEFT:
4840 if (!curwin->w_p_wrap)
4841 {
4842 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4843 curwin->w_leftcol = 0;
4844 else
4845 curwin->w_leftcol -= (colnr_T)cap->count1;
4846 leftcol_changed();
4847 }
4848 break;
4849
4850 /* "zL" - scroll screen left half-page */
4851 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4852 /* FALLTHROUGH */
4853
4854 /* "zl" - scroll screen to the left */
4855 case 'l':
4856 case K_RIGHT:
4857 if (!curwin->w_p_wrap)
4858 {
4859 /* scroll the window left */
4860 curwin->w_leftcol += (colnr_T)cap->count1;
4861 leftcol_changed();
4862 }
4863 break;
4864
4865 /* "zs" - scroll screen, cursor at the start */
4866 case 's': if (!curwin->w_p_wrap)
4867 {
4868#ifdef FEAT_FOLDING
4869 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4870 col = 0; /* like the cursor is in col 0 */
4871 else
4872#endif
4873 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4874 if ((long)col > p_siso)
4875 col -= p_siso;
4876 else
4877 col = 0;
4878 if (curwin->w_leftcol != col)
4879 {
4880 curwin->w_leftcol = col;
4881 redraw_later(NOT_VALID);
4882 }
4883 }
4884 break;
4885
4886 /* "ze" - scroll screen, cursor at the end */
4887 case 'e': 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, NULL, NULL, &col);
4895 n = W_WIDTH(curwin) - curwin_col_off();
4896 if ((long)col + p_siso < n)
4897 col = 0;
4898 else
4899 col = col + p_siso - n + 1;
4900 if (curwin->w_leftcol != col)
4901 {
4902 curwin->w_leftcol = col;
4903 redraw_later(NOT_VALID);
4904 }
4905 }
4906 break;
4907
4908#ifdef FEAT_FOLDING
4909 /* "zF": create fold command */
4910 /* "zf": create fold operator */
4911 case 'F':
4912 case 'f': if (foldManualAllowed(TRUE))
4913 {
4914 cap->nchar = 'f';
4915 nv_operator(cap);
4916 curwin->w_p_fen = TRUE;
4917
4918 /* "zF" is like "zfzf" */
4919 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4920 {
4921 nv_operator(cap);
4922 finish_op = TRUE;
4923 }
4924 }
4925 else
4926 clearopbeep(cap->oap);
4927 break;
4928
4929 /* "zd": delete fold at cursor */
4930 /* "zD": delete fold at cursor recursively */
4931 case 'd':
4932 case 'D': if (foldManualAllowed(FALSE))
4933 {
4934 if (VIsual_active)
4935 nv_operator(cap);
4936 else
4937 deleteFold(curwin->w_cursor.lnum,
4938 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4939 }
4940 break;
4941
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004942 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 case 'E': if (foldmethodIsManual(curwin))
4944 {
4945 clearFolding(curwin);
4946 changed_window_setting();
4947 }
4948 else if (foldmethodIsMarker(curwin))
4949 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4950 TRUE, FALSE);
4951 else
4952 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4953 break;
4954
4955 /* "zn": fold none: reset 'foldenable' */
4956 case 'n': curwin->w_p_fen = FALSE;
4957 break;
4958
4959 /* "zN": fold Normal: set 'foldenable' */
4960 case 'N': curwin->w_p_fen = TRUE;
4961 break;
4962
4963 /* "zi": invert folding: toggle 'foldenable' */
4964 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4965 break;
4966
4967 /* "za": open closed fold or close open fold at cursor */
4968 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4969 openFold(curwin->w_cursor.lnum, cap->count1);
4970 else
4971 {
4972 closeFold(curwin->w_cursor.lnum, cap->count1);
4973 curwin->w_p_fen = TRUE;
4974 }
4975 break;
4976
4977 /* "zA": open fold at cursor recursively */
4978 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4979 openFoldRecurse(curwin->w_cursor.lnum);
4980 else
4981 {
4982 closeFoldRecurse(curwin->w_cursor.lnum);
4983 curwin->w_p_fen = TRUE;
4984 }
4985 break;
4986
4987 /* "zo": open fold at cursor or Visual area */
4988 case 'o': if (VIsual_active)
4989 nv_operator(cap);
4990 else
4991 openFold(curwin->w_cursor.lnum, cap->count1);
4992 break;
4993
4994 /* "zO": open fold recursively */
4995 case 'O': if (VIsual_active)
4996 nv_operator(cap);
4997 else
4998 openFoldRecurse(curwin->w_cursor.lnum);
4999 break;
5000
5001 /* "zc": close fold at cursor or Visual area */
5002 case 'c': if (VIsual_active)
5003 nv_operator(cap);
5004 else
5005 closeFold(curwin->w_cursor.lnum, cap->count1);
5006 curwin->w_p_fen = TRUE;
5007 break;
5008
5009 /* "zC": close fold recursively */
5010 case 'C': if (VIsual_active)
5011 nv_operator(cap);
5012 else
5013 closeFoldRecurse(curwin->w_cursor.lnum);
5014 curwin->w_p_fen = TRUE;
5015 break;
5016
5017 /* "zv": open folds at the cursor */
5018 case 'v': foldOpenCursor();
5019 break;
5020
5021 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5022 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005023 curwin->w_foldinvalid = TRUE; /* recompute folds */
5024 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 foldOpenCursor();
5026 break;
5027
5028 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5029 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005030 curwin->w_foldinvalid = TRUE; /* recompute folds */
5031 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 break;
5033
5034 /* "zm": fold more */
5035 case 'm': if (curwin->w_p_fdl > 0)
5036 --curwin->w_p_fdl;
5037 old_fdl = -1; /* force an update */
5038 curwin->w_p_fen = TRUE;
5039 break;
5040
5041 /* "zM": close all folds */
5042 case 'M': curwin->w_p_fdl = 0;
5043 old_fdl = -1; /* force an update */
5044 curwin->w_p_fen = TRUE;
5045 break;
5046
5047 /* "zr": reduce folding */
5048 case 'r': ++curwin->w_p_fdl;
5049 break;
5050
5051 /* "zR": open all folds */
5052 case 'R': curwin->w_p_fdl = getDeepestNesting();
5053 old_fdl = -1; /* force an update */
5054 break;
5055
5056 case 'j': /* "zj" move to next fold downwards */
5057 case 'k': /* "zk" move to next fold upwards */
5058 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5059 cap->count1) == FAIL)
5060 clearopbeep(cap->oap);
5061 break;
5062
5063#endif /* FEAT_FOLDING */
5064
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005065#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005066 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5067 ++no_mapping;
5068 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005069 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005070 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005071 --no_mapping;
5072 --allow_keys;
5073#ifdef FEAT_CMDL_INFO
5074 (void)add_to_showcmd(nchar);
5075#endif
5076 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5077 {
5078 clearopbeep(cap->oap);
5079 break;
5080 }
5081 undo = TRUE;
5082 /*FALLTHROUGH*/
5083
Bram Moolenaarb765d632005-06-07 21:00:02 +00005084 case 'g': /* "zg": add good word to word list */
5085 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005086 case 'G': /* "zG": add good word to temp word list */
5087 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005088 {
5089 char_u *ptr = NULL;
5090 int len;
5091
5092 if (checkclearop(cap->oap))
5093 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005094 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5095 == FAIL)
5096 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005097 if (ptr == NULL)
5098 {
5099 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005100
Bram Moolenaar134bf072013-09-25 18:54:24 +02005101 /* Find bad word under the cursor. When 'spell' is
5102 * off this fails and find_ident_under_cursor() is
5103 * used below. */
5104 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005105 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005106 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005107 if (len != 0 && curwin->w_cursor.col <= pos.col)
5108 ptr = ml_get_pos(&curwin->w_cursor);
5109 curwin->w_cursor = pos;
5110 }
5111
Bram Moolenaarb765d632005-06-07 21:00:02 +00005112 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5113 FIND_IDENT)) == 0)
5114 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005115 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005116 (nchar == 'G' || nchar == 'W')
5117 ? 0 : (int)cap->count1,
5118 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005119 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005120 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121
Bram Moolenaar43abc522005-12-10 20:15:02 +00005122 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005123 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005124 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005125 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005126#endif
5127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 default: clearopbeep(cap->oap);
5129 }
5130
5131#ifdef FEAT_FOLDING
5132 /* Redraw when 'foldenable' changed */
5133 if (old_fen != curwin->w_p_fen)
5134 {
5135# ifdef FEAT_DIFF
5136 win_T *wp;
5137
5138 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5139 {
5140 /* Adjust 'foldenable' in diff-synced windows. */
5141 FOR_ALL_WINDOWS(wp)
5142 {
5143 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5144 {
5145 wp->w_p_fen = curwin->w_p_fen;
5146 changed_window_setting_win(wp);
5147 }
5148 }
5149 }
5150# endif
5151 changed_window_setting();
5152 }
5153
5154 /* Redraw when 'foldlevel' changed. */
5155 if (old_fdl != curwin->w_p_fdl)
5156 newFoldLevel();
5157#endif
5158}
5159
5160#ifdef FEAT_GUI
5161/*
5162 * Vertical scrollbar movement.
5163 */
5164 static void
5165nv_ver_scrollbar(cap)
5166 cmdarg_T *cap;
5167{
5168 if (cap->oap->op_type != OP_NOP)
5169 clearopbeep(cap->oap);
5170
5171 /* Even if an operator was pending, we still want to scroll */
5172 gui_do_scroll();
5173}
5174
5175/*
5176 * Horizontal scrollbar movement.
5177 */
5178 static void
5179nv_hor_scrollbar(cap)
5180 cmdarg_T *cap;
5181{
5182 if (cap->oap->op_type != OP_NOP)
5183 clearopbeep(cap->oap);
5184
5185 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005186 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187}
5188#endif
5189
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005190#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005191/*
5192 * Click in GUI tab.
5193 */
5194 static void
5195nv_tabline(cap)
5196 cmdarg_T *cap;
5197{
5198 if (cap->oap->op_type != OP_NOP)
5199 clearopbeep(cap->oap);
5200
5201 /* Even if an operator was pending, we still want to jump tabs. */
5202 goto_tabpage(current_tab);
5203}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005204
5205/*
5206 * Selected item in tab line menu.
5207 */
5208 static void
5209nv_tabmenu(cap)
5210 cmdarg_T *cap;
5211{
5212 if (cap->oap->op_type != OP_NOP)
5213 clearopbeep(cap->oap);
5214
5215 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005216 handle_tabmenu();
5217}
5218
5219/*
5220 * Handle selecting an item of the GUI tab line menu.
5221 * Used in Normal and Insert mode.
5222 */
5223 void
5224handle_tabmenu()
5225{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005226 switch (current_tabmenu)
5227 {
5228 case TABLINE_MENU_CLOSE:
5229 if (current_tab == 0)
5230 do_cmdline_cmd((char_u *)"tabclose");
5231 else
5232 {
5233 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5234 current_tab);
5235 do_cmdline_cmd(IObuff);
5236 }
5237 break;
5238
5239 case TABLINE_MENU_NEW:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005240 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5241 current_tab > 0 ? current_tab - 1 : 999);
5242 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005243 break;
5244
5245 case TABLINE_MENU_OPEN:
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005246 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5247 current_tab > 0 ? current_tab - 1 : 999);
5248 do_cmdline_cmd(IObuff);
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005249 break;
5250 }
5251}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005252#endif
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254/*
5255 * "Q" command.
5256 */
5257 static void
5258nv_exmode(cap)
5259 cmdarg_T *cap;
5260{
5261 /*
5262 * Ignore 'Q' in Visual mode, just give a beep.
5263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 if (VIsual_active)
5265 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005266 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 do_exmode(FALSE);
5268}
5269
5270/*
5271 * Handle a ":" command.
5272 */
5273 static void
5274nv_colon(cap)
5275 cmdarg_T *cap;
5276{
5277 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005278 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (VIsual_active)
5281 nv_operator(cap);
5282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {
5284 if (cap->oap->op_type != OP_NOP)
5285 {
5286 /* Using ":" as a movement is characterwise exclusive. */
5287 cap->oap->motion_type = MCHAR;
5288 cap->oap->inclusive = FALSE;
5289 }
5290 else if (cap->count0)
5291 {
5292 /* translate "count:" into ":.,.+(count - 1)" */
5293 stuffcharReadbuff('.');
5294 if (cap->count0 > 1)
5295 {
5296 stuffReadbuff((char_u *)",.+");
5297 stuffnumReadbuff((long)cap->count0 - 1L);
5298 }
5299 }
5300
5301 /* When typing, don't type below an old message */
5302 if (KeyTyped)
5303 compute_cmdrow();
5304
5305 old_p_im = p_im;
5306
5307 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005308 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5310
5311 /* If 'insertmode' changed, enter or exit Insert mode */
5312 if (p_im != old_p_im)
5313 {
5314 if (p_im)
5315 restart_edit = 'i';
5316 else
5317 restart_edit = 0;
5318 }
5319
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005320 if (cmd_result == FAIL)
5321 /* The Ex command failed, do not execute the operator. */
5322 clearop(cap->oap);
5323 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5325 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005326 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5327 || did_emsg
5328 ))
5329 /* The start of the operator has become invalid by the Ex command.
5330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 clearopbeep(cap->oap);
5332 }
5333}
5334
5335/*
5336 * Handle CTRL-G command.
5337 */
5338 static void
5339nv_ctrlg(cap)
5340 cmdarg_T *cap;
5341{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (VIsual_active) /* toggle Selection/Visual mode */
5343 {
5344 VIsual_select = !VIsual_select;
5345 showmode();
5346 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005347 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 /* print full name if count given or :cd used */
5349 fileinfo((int)cap->count0, FALSE, TRUE);
5350}
5351
5352/*
5353 * Handle CTRL-H <Backspace> command.
5354 */
5355 static void
5356nv_ctrlh(cap)
5357 cmdarg_T *cap;
5358{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (VIsual_active && VIsual_select)
5360 {
5361 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5362 v_visop(cap);
5363 }
5364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 nv_left(cap);
5366}
5367
5368/*
5369 * CTRL-L: clear screen and redraw.
5370 */
5371 static void
5372nv_clear(cap)
5373 cmdarg_T *cap;
5374{
5375 if (!checkclearop(cap->oap))
5376 {
5377#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5378 /*
5379 * Right now, the BeBox doesn't seem to have an easy way to detect
5380 * window resizing, so we cheat and make the user detect it
5381 * manually with CTRL-L instead
5382 */
5383 ui_get_shellsize();
5384#endif
5385#ifdef FEAT_SYN_HL
5386 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005387 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388#endif
5389 redraw_later(CLEAR);
5390 }
5391}
5392
5393/*
5394 * CTRL-O: In Select mode: switch to Visual mode for one command.
5395 * Otherwise: Go to older pcmark.
5396 */
5397 static void
5398nv_ctrlo(cap)
5399 cmdarg_T *cap;
5400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 if (VIsual_active && VIsual_select)
5402 {
5403 VIsual_select = FALSE;
5404 showmode();
5405 restart_VIsual_select = 2; /* restart Select mode later */
5406 }
5407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 cap->count1 = -cap->count1;
5410 nv_pcmark(cap);
5411 }
5412}
5413
5414/*
5415 * CTRL-^ command, short for ":e #"
5416 */
5417 static void
5418nv_hat(cap)
5419 cmdarg_T *cap;
5420{
5421 if (!checkclearopq(cap->oap))
5422 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5423 GETF_SETMARK|GETF_ALT, FALSE);
5424}
5425
5426/*
5427 * "Z" commands.
5428 */
5429 static void
5430nv_Zet(cap)
5431 cmdarg_T *cap;
5432{
5433 if (!checkclearopq(cap->oap))
5434 {
5435 switch (cap->nchar)
5436 {
5437 /* "ZZ": equivalent to ":x". */
5438 case 'Z': do_cmdline_cmd((char_u *)"x");
5439 break;
5440
5441 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5442 case 'Q': do_cmdline_cmd((char_u *)"q!");
5443 break;
5444
5445 default: clearopbeep(cap->oap);
5446 }
5447 }
5448}
5449
5450#if defined(FEAT_WINDOWS) || defined(PROTO)
5451/*
5452 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5453 */
5454 void
5455do_nv_ident(c1, c2)
5456 int c1;
5457 int c2;
5458{
5459 oparg_T oa;
5460 cmdarg_T ca;
5461
5462 clear_oparg(&oa);
5463 vim_memset(&ca, 0, sizeof(ca));
5464 ca.oap = &oa;
5465 ca.cmdchar = c1;
5466 ca.nchar = c2;
5467 nv_ident(&ca);
5468}
5469#endif
5470
5471/*
5472 * Handle the commands that use the word under the cursor.
5473 * [g] CTRL-] :ta to current identifier
5474 * [g] 'K' run program for current identifier
5475 * [g] '*' / to current identifier or string
5476 * [g] '#' ? to current identifier or string
5477 * g ']' :tselect for current identifier
5478 */
5479 static void
5480nv_ident(cap)
5481 cmdarg_T *cap;
5482{
5483 char_u *ptr = NULL;
5484 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005485 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 char_u *p;
5487 char_u *kp; /* value of 'keywordprg' */
5488 int kp_help; /* 'keywordprg' is ":help" */
5489 int n = 0; /* init for GCC */
5490 int cmdchar;
5491 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005492 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 char_u *aux_ptr;
5494 int isman;
5495 int isman_s;
5496
5497 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5498 {
5499 cmdchar = cap->nchar;
5500 g_cmd = TRUE;
5501 }
5502 else
5503 {
5504 cmdchar = cap->cmdchar;
5505 g_cmd = FALSE;
5506 }
5507
5508 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5509 cmdchar = '#';
5510
5511 /*
5512 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5513 */
5514 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5517 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (checkclearopq(cap->oap))
5519 return;
5520 }
5521
5522 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5523 (cmdchar == '*' || cmdchar == '#')
5524 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5525 {
5526 clearop(cap->oap);
5527 return;
5528 }
5529
5530 /* Allocate buffer to put the command in. Inserting backslashes can
5531 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5532 * and some numbers. */
5533 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5534 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5535 || STRCMP(kp, ":help") == 0);
5536 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5537 if (buf == NULL)
5538 return;
5539 buf[0] = NUL;
5540
5541 switch (cmdchar)
5542 {
5543 case '*':
5544 case '#':
5545 /*
5546 * Put cursor at start of word, makes search skip the word
5547 * under the cursor.
5548 * Call setpcmark() first, so "*``" puts the cursor back where
5549 * it was.
5550 */
5551 setpcmark();
5552 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5553
5554 if (!g_cmd && vim_iswordp(ptr))
5555 STRCPY(buf, "\\<");
5556 no_smartcase = TRUE; /* don't use 'smartcase' now */
5557 break;
5558
5559 case 'K':
5560 if (kp_help)
5561 STRCPY(buf, "he! ");
5562 else
5563 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005564 /* An external command will probably use an argument starting
5565 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005566 while (*ptr == '-' && n > 0)
5567 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005568 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005569 --n;
5570 }
5571 if (n == 0)
5572 {
5573 EMSG(_(e_noident)); /* found dashes only */
5574 vim_free(buf);
5575 return;
5576 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 /* When a count is given, turn it into a range. Is this
5579 * really what we want? */
5580 isman = (STRCMP(kp, "man") == 0);
5581 isman_s = (STRCMP(kp, "man -s") == 0);
5582 if (cap->count0 != 0 && !(isman || isman_s))
5583 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5584
5585 STRCAT(buf, "! ");
5586 if (cap->count0 == 0 && isman_s)
5587 STRCAT(buf, "man");
5588 else
5589 STRCAT(buf, kp);
5590 STRCAT(buf, " ");
5591 if (cap->count0 != 0 && (isman || isman_s))
5592 {
5593 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5594 STRCAT(buf, " ");
5595 }
5596 }
5597 break;
5598
5599 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005600 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601#ifdef FEAT_CSCOPE
5602 if (p_cst)
5603 STRCPY(buf, "cstag ");
5604 else
5605#endif
5606 STRCPY(buf, "ts ");
5607 break;
5608
5609 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005610 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (curbuf->b_help)
5612 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005614 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005615 if (g_cmd)
5616 STRCPY(buf, "tj ");
5617 else
5618 sprintf((char *)buf, "%ldta ", cap->count0);
5619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621
5622 /*
5623 * Now grab the chars in the identifier
5624 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005625 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005627 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005628 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005629 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005630 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005631 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005633 vim_free(buf);
5634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005636 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5637 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005638 {
5639 vim_free(buf);
5640 vim_free(p);
5641 return;
5642 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005643 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005644 STRCAT(buf, p);
5645 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005647 else
5648 {
5649 if (cmdchar == '*')
5650 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5651 else if (cmdchar == '#')
5652 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005653 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005654 {
5655 if (curbuf->b_help)
5656 /* ":help" handles unescaped argument */
5657 aux_ptr = (char_u *)"";
5658 else
5659 aux_ptr = (char_u *)"\\|\"\n[";
5660 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005661 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005662 aux_ptr = (char_u *)"\\|\"\n*?[";
5663
5664 p = buf + STRLEN(buf);
5665 while (n-- > 0)
5666 {
5667 /* put a backslash before \ and some others */
5668 if (vim_strchr(aux_ptr, *ptr) != NULL)
5669 *p++ = '\\';
5670#ifdef FEAT_MBYTE
5671 /* When current byte is a part of multibyte character, copy all
5672 * bytes of that character. */
5673 if (has_mbyte)
5674 {
5675 int i;
5676 int len = (*mb_ptr2len)(ptr) - 1;
5677
5678 for (i = 0; i < len && n >= 1; ++i, --n)
5679 *p++ = *ptr++;
5680 }
5681#endif
5682 *p++ = *ptr++;
5683 }
5684 *p = NUL;
5685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687 /*
5688 * Execute the command.
5689 */
5690 if (cmdchar == '*' || cmdchar == '#')
5691 {
5692 if (!g_cmd && (
5693#ifdef FEAT_MBYTE
5694 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5695#endif
5696 vim_iswordc(ptr[-1])))
5697 STRCAT(buf, "\\>");
5698#ifdef FEAT_CMDHIST
5699 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005700 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5702#endif
5703 normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
5704 }
5705 else
5706 do_cmdline_cmd(buf);
5707
5708 vim_free(buf);
5709}
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711/*
5712 * Get visually selected text, within one line only.
5713 * Returns FAIL if more than one line selected.
5714 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005715 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716get_visual_text(cap, pp, lenp)
5717 cmdarg_T *cap;
5718 char_u **pp; /* return: start of selected text */
5719 int *lenp; /* return: length of selected text */
5720{
5721 if (VIsual_mode != 'V')
5722 unadjust_for_sel();
5723 if (VIsual.lnum != curwin->w_cursor.lnum)
5724 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005725 if (cap != NULL)
5726 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 return FAIL;
5728 }
5729 if (VIsual_mode == 'V')
5730 {
5731 *pp = ml_get_curline();
5732 *lenp = (int)STRLEN(*pp);
5733 }
5734 else
5735 {
5736 if (lt(curwin->w_cursor, VIsual))
5737 {
5738 *pp = ml_get_pos(&curwin->w_cursor);
5739 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5740 }
5741 else
5742 {
5743 *pp = ml_get_pos(&VIsual);
5744 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5745 }
5746#ifdef FEAT_MBYTE
5747 if (has_mbyte)
5748 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005749 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750#endif
5751 }
5752 reset_VIsual_and_resel();
5753 return OK;
5754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
5756/*
5757 * CTRL-T: backwards in tag stack
5758 */
5759 static void
5760nv_tagpop(cap)
5761 cmdarg_T *cap;
5762{
5763 if (!checkclearopq(cap->oap))
5764 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5765}
5766
5767/*
5768 * Handle scrolling command 'H', 'L' and 'M'.
5769 */
5770 static void
5771nv_scroll(cap)
5772 cmdarg_T *cap;
5773{
5774 int used = 0;
5775 long n;
5776#ifdef FEAT_FOLDING
5777 linenr_T lnum;
5778#endif
5779 int half;
5780
5781 cap->oap->motion_type = MLINE;
5782 setpcmark();
5783
5784 if (cap->cmdchar == 'L')
5785 {
5786 validate_botline(); /* make sure curwin->w_botline is valid */
5787 curwin->w_cursor.lnum = curwin->w_botline - 1;
5788 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5789 curwin->w_cursor.lnum = 1;
5790 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005791 {
5792#ifdef FEAT_FOLDING
5793 if (hasAnyFolding(curwin))
5794 {
5795 /* Count a fold for one screen line. */
5796 for (n = cap->count1 - 1; n > 0
5797 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5798 {
5799 (void)hasFolding(curwin->w_cursor.lnum,
5800 &curwin->w_cursor.lnum, NULL);
5801 --curwin->w_cursor.lnum;
5802 }
5803 }
5804 else
5805#endif
5806 curwin->w_cursor.lnum -= cap->count1 - 1;
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else
5810 {
5811 if (cap->cmdchar == 'M')
5812 {
5813#ifdef FEAT_DIFF
5814 /* Don't count filler lines above the window. */
5815 used -= diff_check_fill(curwin, curwin->w_topline)
5816 - curwin->w_topfill;
5817#endif
5818 validate_botline(); /* make sure w_empty_rows is valid */
5819 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5820 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5821 {
5822#ifdef FEAT_DIFF
5823 /* Count half he number of filler lines to be "below this
5824 * line" and half to be "above the next line". */
5825 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5826 + n) / 2 >= half)
5827 {
5828 --n;
5829 break;
5830 }
5831#endif
5832 used += plines(curwin->w_topline + n);
5833 if (used >= half)
5834 break;
5835#ifdef FEAT_FOLDING
5836 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5837 n = lnum - curwin->w_topline;
5838#endif
5839 }
5840 if (n > 0 && used > curwin->w_height)
5841 --n;
5842 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005843 else /* (cap->cmdchar == 'H') */
5844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005846#ifdef FEAT_FOLDING
5847 if (hasAnyFolding(curwin))
5848 {
5849 /* Count a fold for one screen line. */
5850 lnum = curwin->w_topline;
5851 while (n-- > 0 && lnum < curwin->w_botline - 1)
5852 {
5853 hasFolding(lnum, NULL, &lnum);
5854 ++lnum;
5855 }
5856 n = lnum - curwin->w_topline;
5857 }
5858#endif
5859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 curwin->w_cursor.lnum = curwin->w_topline + n;
5861 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5862 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5863 }
5864
5865 cursor_correct(); /* correct for 'so' */
5866 beginline(BL_SOL | BL_FIX);
5867}
5868
5869/*
5870 * Cursor right commands.
5871 */
5872 static void
5873nv_right(cap)
5874 cmdarg_T *cap;
5875{
5876 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005877 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005879 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5880 {
5881 /* <C-Right> and <S-Right> move a word or WORD right */
5882 if (mod_mask & MOD_MASK_CTRL)
5883 cap->arg = TRUE;
5884 nv_wordcmd(cap);
5885 return;
5886 }
5887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 cap->oap->motion_type = MCHAR;
5889 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005890 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005892#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005894 * In virtual edit mode, there's no such thing as "past_line", as lines
5895 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 */
5897 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005898 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899#endif
5900
5901 for (n = cap->count1; n > 0; --n)
5902 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005903 if ((!past_line && oneright() == FAIL)
5904 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
5907 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005908 * <Space> wraps to next line if 'whichwrap' has 's'.
5909 * 'l' wraps to next line if 'whichwrap' has 'l'.
5910 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 */
5912 if ( ((cap->cmdchar == ' '
5913 && vim_strchr(p_ww, 's') != NULL)
5914 || (cap->cmdchar == 'l'
5915 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005916 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 && vim_strchr(p_ww, '>') != NULL))
5918 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5919 {
5920 /* When deleting we also count the NL as a character.
5921 * Set cap->oap->inclusive when last char in the line is
5922 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005923 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 && !cap->oap->inclusive
5925 && !lineempty(curwin->w_cursor.lnum))
5926 cap->oap->inclusive = TRUE;
5927 else
5928 {
5929 ++curwin->w_cursor.lnum;
5930 curwin->w_cursor.col = 0;
5931#ifdef FEAT_VIRTUALEDIT
5932 curwin->w_cursor.coladd = 0;
5933#endif
5934 curwin->w_set_curswant = TRUE;
5935 cap->oap->inclusive = FALSE;
5936 }
5937 continue;
5938 }
5939 if (cap->oap->op_type == OP_NOP)
5940 {
5941 /* Only beep and flush if not moved at all */
5942 if (n == cap->count1)
5943 beep_flush();
5944 }
5945 else
5946 {
5947 if (!lineempty(curwin->w_cursor.lnum))
5948 cap->oap->inclusive = TRUE;
5949 }
5950 break;
5951 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005952 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005955#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (virtual_active())
5957 oneright();
5958 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005961#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (has_mbyte)
5963 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005964 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 ++curwin->w_cursor.col;
5968 }
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 }
5971#ifdef FEAT_FOLDING
5972 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5973 && cap->oap->op_type == OP_NOP)
5974 foldOpenCursor();
5975#endif
5976}
5977
5978/*
5979 * Cursor left commands.
5980 *
5981 * Returns TRUE when operator end should not be adjusted.
5982 */
5983 static void
5984nv_left(cap)
5985 cmdarg_T *cap;
5986{
5987 long n;
5988
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005989 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5990 {
5991 /* <C-Left> and <S-Left> move a word or WORD left */
5992 if (mod_mask & MOD_MASK_CTRL)
5993 cap->arg = 1;
5994 nv_bck_word(cap);
5995 return;
5996 }
5997
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 cap->oap->motion_type = MCHAR;
5999 cap->oap->inclusive = FALSE;
6000 for (n = cap->count1; n > 0; --n)
6001 {
6002 if (oneleft() == FAIL)
6003 {
6004 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6005 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6006 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6007 */
6008 if ( (((cap->cmdchar == K_BS
6009 || cap->cmdchar == Ctrl_H)
6010 && vim_strchr(p_ww, 'b') != NULL)
6011 || (cap->cmdchar == 'h'
6012 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006013 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 && vim_strchr(p_ww, '<') != NULL))
6015 && curwin->w_cursor.lnum > 1)
6016 {
6017 --(curwin->w_cursor.lnum);
6018 coladvance((colnr_T)MAXCOL);
6019 curwin->w_set_curswant = TRUE;
6020
6021 /* When the NL before the first char has to be deleted we
6022 * put the cursor on the NUL after the previous line.
6023 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006024 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if ( (cap->oap->op_type == OP_DELETE
6026 || cap->oap->op_type == OP_CHANGE)
6027 && !lineempty(curwin->w_cursor.lnum))
6028 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006029 char_u *cp = ml_get_cursor();
6030
6031 if (*cp != NUL)
6032 {
6033#ifdef FEAT_MBYTE
6034 if (has_mbyte)
6035 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6036 else
6037#endif
6038 ++curwin->w_cursor.col;
6039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 cap->retval |= CA_NO_ADJ_OP_END;
6041 }
6042 continue;
6043 }
6044 /* Only beep and flush if not moved at all */
6045 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6046 beep_flush();
6047 break;
6048 }
6049 }
6050#ifdef FEAT_FOLDING
6051 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6052 && cap->oap->op_type == OP_NOP)
6053 foldOpenCursor();
6054#endif
6055}
6056
6057/*
6058 * Cursor up commands.
6059 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6060 */
6061 static void
6062nv_up(cap)
6063 cmdarg_T *cap;
6064{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006065 if (mod_mask & MOD_MASK_SHIFT)
6066 {
6067 /* <S-Up> is page up */
6068 cap->arg = BACKWARD;
6069 nv_page(cap);
6070 }
6071 else
6072 {
6073 cap->oap->motion_type = MLINE;
6074 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6075 clearopbeep(cap->oap);
6076 else if (cap->arg)
6077 beginline(BL_WHITE | BL_FIX);
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079}
6080
6081/*
6082 * Cursor down commands.
6083 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6084 */
6085 static void
6086nv_down(cap)
6087 cmdarg_T *cap;
6088{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006089 if (mod_mask & MOD_MASK_SHIFT)
6090 {
6091 /* <S-Down> is page down */
6092 cap->arg = FORWARD;
6093 nv_page(cap);
6094 }
6095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6097 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006098 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006099 if (curwin->w_llist_ref == NULL)
6100 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6101 else
6102 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 else
6104#endif
6105 {
6106#ifdef FEAT_CMDWIN
6107 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006108 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 cmdwin_result = CAR;
6110 else
6111#endif
6112 {
6113 cap->oap->motion_type = MLINE;
6114 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6115 clearopbeep(cap->oap);
6116 else if (cap->arg)
6117 beginline(BL_WHITE | BL_FIX);
6118 }
6119 }
6120}
6121
6122#ifdef FEAT_SEARCHPATH
6123/*
6124 * Grab the file name under the cursor and edit it.
6125 */
6126 static void
6127nv_gotofile(cap)
6128 cmdarg_T *cap;
6129{
6130 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006131 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006133 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006136 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return;
6138 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006139#ifdef FEAT_AUTOCMD
6140 if (curbuf_locked())
6141 {
6142 clearop(cap->oap);
6143 return;
6144 }
6145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006147 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149 if (ptr != NULL)
6150 {
6151 /* do autowrite if necessary */
6152 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6153 autowrite(curbuf, FALSE);
6154 setpcmark();
6155 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006156 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006157 if (cap->nchar == 'F' && lnum >= 0)
6158 {
6159 curwin->w_cursor.lnum = lnum;
6160 check_cursor_lnum();
6161 beginline(BL_SOL | BL_FIX);
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 vim_free(ptr);
6164 }
6165 else
6166 clearop(cap->oap);
6167}
6168#endif
6169
6170/*
6171 * <End> command: to end of current line or last line.
6172 */
6173 static void
6174nv_end(cap)
6175 cmdarg_T *cap;
6176{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006177 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006179 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 nv_goto(cap);
6181 cap->count1 = 1; /* to end of current line */
6182 }
6183 nv_dollar(cap);
6184}
6185
6186/*
6187 * Handle the "$" command.
6188 */
6189 static void
6190nv_dollar(cap)
6191 cmdarg_T *cap;
6192{
6193 cap->oap->motion_type = MCHAR;
6194 cap->oap->inclusive = TRUE;
6195#ifdef FEAT_VIRTUALEDIT
6196 /* In virtual mode when off the edge of a line and an operator
6197 * is pending (whew!) keep the cursor where it is.
6198 * Otherwise, send it to the end of the line. */
6199 if (!virtual_active() || gchar_cursor() != NUL
6200 || cap->oap->op_type == OP_NOP)
6201#endif
6202 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6203 if (cursor_down((long)(cap->count1 - 1),
6204 cap->oap->op_type == OP_NOP) == FAIL)
6205 clearopbeep(cap->oap);
6206#ifdef FEAT_FOLDING
6207 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6208 foldOpenCursor();
6209#endif
6210}
6211
6212/*
6213 * Implementation of '?' and '/' commands.
6214 * If cap->arg is TRUE don't set PC mark.
6215 */
6216 static void
6217nv_search(cap)
6218 cmdarg_T *cap;
6219{
6220 oparg_T *oap = cap->oap;
6221
6222 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6223 {
6224 /* Translate "g??" to "g?g?" */
6225 cap->cmdchar = 'g';
6226 cap->nchar = '?';
6227 nv_operator(cap);
6228 return;
6229 }
6230
6231 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6232
6233 if (cap->searchbuf == NULL)
6234 {
6235 clearop(oap);
6236 return;
6237 }
6238
6239 normal_search(cap, cap->cmdchar, cap->searchbuf,
6240 (cap->arg ? 0 : SEARCH_MARK));
6241}
6242
6243/*
6244 * Handle "N" and "n" commands.
6245 * cap->arg is SEARCH_REV for "N", 0 for "n".
6246 */
6247 static void
6248nv_next(cap)
6249 cmdarg_T *cap;
6250{
6251 normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6252}
6253
6254/*
6255 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6256 * Uses only cap->count1 and cap->oap from "cap".
6257 */
6258 static void
6259normal_search(cap, dir, pat, opt)
6260 cmdarg_T *cap;
6261 int dir;
6262 char_u *pat;
6263 int opt; /* extra flags for do_search() */
6264{
6265 int i;
6266
6267 cap->oap->motion_type = MCHAR;
6268 cap->oap->inclusive = FALSE;
6269 cap->oap->use_reg_one = TRUE;
6270 curwin->w_set_curswant = TRUE;
6271
6272 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006273 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (i == 0)
6275 clearop(cap->oap);
6276 else
6277 {
6278 if (i == 2)
6279 cap->oap->motion_type = MLINE;
6280#ifdef FEAT_VIRTUALEDIT
6281 curwin->w_cursor.coladd = 0;
6282#endif
6283#ifdef FEAT_FOLDING
6284 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6285 foldOpenCursor();
6286#endif
6287 }
6288
6289 /* "/$" will put the cursor after the end of the line, may need to
6290 * correct that here */
6291 check_cursor();
6292}
6293
6294/*
6295 * Character search commands.
6296 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6297 * ',' and FALSE for ';'.
6298 * cap->nchar is NUL for ',' and ';' (repeat the search)
6299 */
6300 static void
6301nv_csearch(cap)
6302 cmdarg_T *cap;
6303{
6304 int t_cmd;
6305
6306 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6307 t_cmd = TRUE;
6308 else
6309 t_cmd = FALSE;
6310
6311 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6313 clearopbeep(cap->oap);
6314 else
6315 {
6316 curwin->w_set_curswant = TRUE;
6317#ifdef FEAT_VIRTUALEDIT
6318 /* Include a Tab for "tx" and for "dfx". */
6319 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6320 && (t_cmd || cap->oap->op_type != OP_NOP))
6321 {
6322 colnr_T scol, ecol;
6323
6324 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6325 curwin->w_cursor.coladd = ecol - scol;
6326 }
6327 else
6328 curwin->w_cursor.coladd = 0;
6329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331#ifdef FEAT_FOLDING
6332 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6333 foldOpenCursor();
6334#endif
6335 }
6336}
6337
6338/*
6339 * "[" and "]" commands.
6340 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6341 */
6342 static void
6343nv_brackets(cap)
6344 cmdarg_T *cap;
6345{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006346 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 pos_T prev_pos;
6348 pos_T *pos = NULL; /* init for GCC */
6349 pos_T old_pos; /* cursor position before command */
6350 int flag;
6351 long n;
6352 int findc;
6353 int c;
6354
6355 cap->oap->motion_type = MCHAR;
6356 cap->oap->inclusive = FALSE;
6357 old_pos = curwin->w_cursor;
6358#ifdef FEAT_VIRTUALEDIT
6359 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6360#endif
6361
6362#ifdef FEAT_SEARCHPATH
6363 /*
6364 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6365 */
6366 if (cap->nchar == 'f')
6367 nv_gotofile(cap);
6368 else
6369#endif
6370
6371#ifdef FEAT_FIND_ID
6372 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006373 * Find the occurrence(s) of the identifier or define under cursor
6374 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 *
6376 * search list jump
6377 * fwd bwd fwd bwd fwd bwd
6378 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6379 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6380 */
6381 if (vim_strchr((char_u *)
6382#ifdef EBCDIC
6383 "iI\005dD\067",
6384#else
6385 "iI\011dD\004",
6386#endif
6387 cap->nchar) != NULL)
6388 {
6389 char_u *ptr;
6390 int len;
6391
6392 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6393 clearop(cap->oap);
6394 else
6395 {
6396 find_pattern_in_path(ptr, 0, len, TRUE,
6397 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6398 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6399 cap->count1,
6400 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6401 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6402 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6403 (linenr_T)MAXLNUM);
6404 curwin->w_set_curswant = TRUE;
6405 }
6406 }
6407 else
6408#endif
6409
6410 /*
6411 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6412 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6413 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6414 * "[m" or "]m" search for prev/next start of (Java) method.
6415 * "[M" or "]M" search for prev/next end of (Java) method.
6416 */
6417 if ( (cap->cmdchar == '['
6418 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6419 || (cap->cmdchar == ']'
6420 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6421 {
6422 if (cap->nchar == '*')
6423 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 prev_pos.lnum = 0;
6425 if (cap->nchar == 'm' || cap->nchar == 'M')
6426 {
6427 if (cap->cmdchar == '[')
6428 findc = '{';
6429 else
6430 findc = '}';
6431 n = 9999;
6432 }
6433 else
6434 {
6435 findc = cap->nchar;
6436 n = cap->count1;
6437 }
6438 for ( ; n > 0; --n)
6439 {
6440 if ((pos = findmatchlimit(cap->oap, findc,
6441 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6442 {
6443 if (new_pos.lnum == 0) /* nothing found */
6444 {
6445 if (cap->nchar != 'm' && cap->nchar != 'M')
6446 clearopbeep(cap->oap);
6447 }
6448 else
6449 pos = &new_pos; /* use last one found */
6450 break;
6451 }
6452 prev_pos = new_pos;
6453 curwin->w_cursor = *pos;
6454 new_pos = *pos;
6455 }
6456 curwin->w_cursor = old_pos;
6457
6458 /*
6459 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6460 * brought us to the match for "[m" and "]M" when inside a method.
6461 * Try finding the '{' or '}' we want to be at.
6462 * Also repeat for the given count.
6463 */
6464 if (cap->nchar == 'm' || cap->nchar == 'M')
6465 {
6466 /* norm is TRUE for "]M" and "[m" */
6467 int norm = ((findc == '{') == (cap->nchar == 'm'));
6468
6469 n = cap->count1;
6470 /* found a match: we were inside a method */
6471 if (prev_pos.lnum != 0)
6472 {
6473 pos = &prev_pos;
6474 curwin->w_cursor = prev_pos;
6475 if (norm)
6476 --n;
6477 }
6478 else
6479 pos = NULL;
6480 while (n > 0)
6481 {
6482 for (;;)
6483 {
6484 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6485 {
6486 /* if not found anything, that's an error */
6487 if (pos == NULL)
6488 clearopbeep(cap->oap);
6489 n = 0;
6490 break;
6491 }
6492 c = gchar_cursor();
6493 if (c == '{' || c == '}')
6494 {
6495 /* Must have found end/start of class: use it.
6496 * Or found the place to be at. */
6497 if ((c == findc && norm) || (n == 1 && !norm))
6498 {
6499 new_pos = curwin->w_cursor;
6500 pos = &new_pos;
6501 n = 0;
6502 }
6503 /* if no match found at all, we started outside of the
6504 * class and we're inside now. Just go on. */
6505 else if (new_pos.lnum == 0)
6506 {
6507 new_pos = curwin->w_cursor;
6508 pos = &new_pos;
6509 }
6510 /* found start/end of other method: go to match */
6511 else if ((pos = findmatchlimit(cap->oap, findc,
6512 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6513 0)) == NULL)
6514 n = 0;
6515 else
6516 curwin->w_cursor = *pos;
6517 break;
6518 }
6519 }
6520 --n;
6521 }
6522 curwin->w_cursor = old_pos;
6523 if (pos == NULL && new_pos.lnum != 0)
6524 clearopbeep(cap->oap);
6525 }
6526 if (pos != NULL)
6527 {
6528 setpcmark();
6529 curwin->w_cursor = *pos;
6530 curwin->w_set_curswant = TRUE;
6531#ifdef FEAT_FOLDING
6532 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6533 && cap->oap->op_type == OP_NOP)
6534 foldOpenCursor();
6535#endif
6536 }
6537 }
6538
6539 /*
6540 * "[[", "[]", "]]" and "][": move to start or end of function
6541 */
6542 else if (cap->nchar == '[' || cap->nchar == ']')
6543 {
6544 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6545 flag = '{';
6546 else
6547 flag = '}'; /* "][" or "[]" */
6548
6549 curwin->w_set_curswant = TRUE;
6550 /*
6551 * Imitate strange Vi behaviour: When using "]]" with an operator
6552 * we also stop at '}'.
6553 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006554 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 (cap->oap->op_type != OP_NOP
6556 && cap->arg == FORWARD && flag == '{')))
6557 clearopbeep(cap->oap);
6558 else
6559 {
6560 if (cap->oap->op_type == OP_NOP)
6561 beginline(BL_WHITE | BL_FIX);
6562#ifdef FEAT_FOLDING
6563 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6564 foldOpenCursor();
6565#endif
6566 }
6567 }
6568
6569 /*
6570 * "[p", "[P", "]P" and "]p": put with indent adjustment
6571 */
6572 else if (cap->nchar == 'p' || cap->nchar == 'P')
6573 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006574 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006576 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6577 ? FORWARD : BACKWARD;
6578 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006579 int was_visual = VIsual_active;
6580 int line_count = curbuf->b_ml.ml_line_count;
6581 pos_T start, end;
6582
6583 if (VIsual_active)
6584 {
6585 start = ltoreq(VIsual, curwin->w_cursor)
6586 ? VIsual : curwin->w_cursor;
6587 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6588 curwin->w_cursor = (dir == BACKWARD ? start : end);
6589 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006590# ifdef FEAT_CLIPBOARD
6591 adjust_clip_reg(&regname);
6592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006594
6595 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006596 if (was_visual)
6597 {
6598 VIsual = start;
6599 curwin->w_cursor = end;
6600 if (dir == BACKWARD)
6601 {
6602 /* adjust lines */
6603 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6604 curwin->w_cursor.lnum +=
6605 curbuf->b_ml.ml_line_count - line_count;
6606 }
6607
6608 VIsual_active = TRUE;
6609 if (VIsual_mode == 'V')
6610 {
6611 /* delete visually selected lines */
6612 cap->cmdchar = 'd';
6613 cap->nchar = NUL;
6614 cap->oap->regname = regname;
6615 nv_operator(cap);
6616 do_pending_operator(cap, 0, FALSE);
6617 }
6618 if (VIsual_active)
6619 {
6620 end_visual_mode();
6621 redraw_later(SOME_VALID);
6622 }
6623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
6625 }
6626
6627 /*
6628 * "['", "[`", "]'" and "]`": jump to next mark
6629 */
6630 else if (cap->nchar == '\'' || cap->nchar == '`')
6631 {
6632 pos = &curwin->w_cursor;
6633 for (n = cap->count1; n > 0; --n)
6634 {
6635 prev_pos = *pos;
6636 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6637 cap->nchar == '\'');
6638 if (pos == NULL)
6639 break;
6640 }
6641 if (pos == NULL)
6642 pos = &prev_pos;
6643 nv_cursormark(cap, cap->nchar == '\'', pos);
6644 }
6645
6646#ifdef FEAT_MOUSE
6647 /*
6648 * [ or ] followed by a middle mouse click: put selected text with
6649 * indent adjustment. Any other button just does as usual.
6650 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006651 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 {
6653 (void)do_mouse(cap->oap, cap->nchar,
6654 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6655 cap->count1, PUT_FIXINDENT);
6656 }
6657#endif /* FEAT_MOUSE */
6658
6659#ifdef FEAT_FOLDING
6660 /*
6661 * "[z" and "]z": move to start or end of open fold.
6662 */
6663 else if (cap->nchar == 'z')
6664 {
6665 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6666 cap->count1) == FAIL)
6667 clearopbeep(cap->oap);
6668 }
6669#endif
6670
6671#ifdef FEAT_DIFF
6672 /*
6673 * "[c" and "]c": move to next or previous diff-change.
6674 */
6675 else if (cap->nchar == 'c')
6676 {
6677 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6678 cap->count1) == FAIL)
6679 clearopbeep(cap->oap);
6680 }
6681#endif
6682
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006683#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006684 /*
6685 * "[s", "[S", "]s" and "]S": move to next spell error.
6686 */
6687 else if (cap->nchar == 's' || cap->nchar == 'S')
6688 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006689 setpcmark();
6690 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006691 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6692 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006693 {
6694 clearopbeep(cap->oap);
6695 break;
6696 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006697# ifdef FEAT_FOLDING
6698 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6699 foldOpenCursor();
6700# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006701 }
6702#endif
6703
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 /* Not a valid cap->nchar. */
6705 else
6706 clearopbeep(cap->oap);
6707}
6708
6709/*
6710 * Handle Normal mode "%" command.
6711 */
6712 static void
6713nv_percent(cap)
6714 cmdarg_T *cap;
6715{
6716 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006717#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 linenr_T lnum = curwin->w_cursor.lnum;
6719#endif
6720
6721 cap->oap->inclusive = TRUE;
6722 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6723 {
6724 if (cap->count0 > 100)
6725 clearopbeep(cap->oap);
6726 else
6727 {
6728 cap->oap->motion_type = MLINE;
6729 setpcmark();
6730 /* Round up, so CTRL-G will give same value. Watch out for a
6731 * large line count, the line number must not go negative! */
6732 if (curbuf->b_ml.ml_line_count > 1000000)
6733 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6734 / 100L * cap->count0;
6735 else
6736 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6737 cap->count0 + 99L) / 100L;
6738 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6739 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6740 beginline(BL_SOL | BL_FIX);
6741 }
6742 }
6743 else /* "%" : go to matching paren */
6744 {
6745 cap->oap->motion_type = MCHAR;
6746 cap->oap->use_reg_one = TRUE;
6747 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6748 clearopbeep(cap->oap);
6749 else
6750 {
6751 setpcmark();
6752 curwin->w_cursor = *pos;
6753 curwin->w_set_curswant = TRUE;
6754#ifdef FEAT_VIRTUALEDIT
6755 curwin->w_cursor.coladd = 0;
6756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 }
6759 }
6760#ifdef FEAT_FOLDING
6761 if (cap->oap->op_type == OP_NOP
6762 && lnum != curwin->w_cursor.lnum
6763 && (fdo_flags & FDO_PERCENT)
6764 && KeyTyped)
6765 foldOpenCursor();
6766#endif
6767}
6768
6769/*
6770 * Handle "(" and ")" commands.
6771 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6772 */
6773 static void
6774nv_brace(cap)
6775 cmdarg_T *cap;
6776{
6777 cap->oap->motion_type = MCHAR;
6778 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006779 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6780 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 curwin->w_set_curswant = TRUE;
6782
6783 if (findsent(cap->arg, cap->count1) == FAIL)
6784 clearopbeep(cap->oap);
6785 else
6786 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006787 /* Don't leave the cursor on the NUL past end of line. */
6788 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789#ifdef FEAT_VIRTUALEDIT
6790 curwin->w_cursor.coladd = 0;
6791#endif
6792#ifdef FEAT_FOLDING
6793 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6794 foldOpenCursor();
6795#endif
6796 }
6797}
6798
6799/*
6800 * "m" command: Mark a position.
6801 */
6802 static void
6803nv_mark(cap)
6804 cmdarg_T *cap;
6805{
6806 if (!checkclearop(cap->oap))
6807 {
6808 if (setmark(cap->nchar) == FAIL)
6809 clearopbeep(cap->oap);
6810 }
6811}
6812
6813/*
6814 * "{" and "}" commands.
6815 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6816 */
6817 static void
6818nv_findpar(cap)
6819 cmdarg_T *cap;
6820{
6821 cap->oap->motion_type = MCHAR;
6822 cap->oap->inclusive = FALSE;
6823 cap->oap->use_reg_one = TRUE;
6824 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006825 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 clearopbeep(cap->oap);
6827 else
6828 {
6829#ifdef FEAT_VIRTUALEDIT
6830 curwin->w_cursor.coladd = 0;
6831#endif
6832#ifdef FEAT_FOLDING
6833 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6834 foldOpenCursor();
6835#endif
6836 }
6837}
6838
6839/*
6840 * "u" command: Undo or make lower case.
6841 */
6842 static void
6843nv_undo(cap)
6844 cmdarg_T *cap;
6845{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006846 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {
6848 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6849 cap->cmdchar = 'g';
6850 cap->nchar = 'u';
6851 nv_operator(cap);
6852 }
6853 else
6854 nv_kundo(cap);
6855}
6856
6857/*
6858 * <Undo> command.
6859 */
6860 static void
6861nv_kundo(cap)
6862 cmdarg_T *cap;
6863{
6864 if (!checkclearopq(cap->oap))
6865 {
6866 u_undo((int)cap->count1);
6867 curwin->w_set_curswant = TRUE;
6868 }
6869}
6870
6871/*
6872 * Handle the "r" command.
6873 */
6874 static void
6875nv_replace(cap)
6876 cmdarg_T *cap;
6877{
6878 char_u *ptr;
6879 int had_ctrl_v;
6880 long n;
6881
6882 if (checkclearop(cap->oap))
6883 return;
6884
6885 /* get another character */
6886 if (cap->nchar == Ctrl_V)
6887 {
6888 had_ctrl_v = Ctrl_V;
6889 cap->nchar = get_literal();
6890 /* Don't redo a multibyte character with CTRL-V. */
6891 if (cap->nchar > DEL)
6892 had_ctrl_v = NUL;
6893 }
6894 else
6895 had_ctrl_v = NUL;
6896
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006897 /* Abort if the character is a special key. */
6898 if (IS_SPECIAL(cap->nchar))
6899 {
6900 clearopbeep(cap->oap);
6901 return;
6902 }
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 /* Visual mode "r" */
6905 if (VIsual_active)
6906 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006907 if (got_int)
6908 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006909 if (had_ctrl_v)
6910 {
6911 if (cap->nchar == '\r')
6912 cap->nchar = -1;
6913 else if (cap->nchar == '\n')
6914 cap->nchar = -2;
6915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 nv_operator(cap);
6917 return;
6918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920#ifdef FEAT_VIRTUALEDIT
6921 /* Break tabs, etc. */
6922 if (virtual_active())
6923 {
6924 if (u_save_cursor() == FAIL)
6925 return;
6926 if (gchar_cursor() == NUL)
6927 {
6928 /* Add extra space and put the cursor on the first one. */
6929 coladvance_force((colnr_T)(getviscol() + cap->count1));
6930 curwin->w_cursor.col -= cap->count1;
6931 }
6932 else if (gchar_cursor() == TAB)
6933 coladvance_force(getviscol());
6934 }
6935#endif
6936
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006937 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006939 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940#ifdef FEAT_MBYTE
6941 || (has_mbyte && mb_charlen(ptr) < cap->count1)
6942#endif
6943 )
6944 {
6945 clearopbeep(cap->oap);
6946 return;
6947 }
6948
6949 /*
6950 * Replacing with a TAB is done by edit() when it is complicated because
6951 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6952 * Other characters are done below to avoid problems with things like
6953 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6954 */
6955 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6956 {
6957 stuffnumReadbuff(cap->count1);
6958 stuffcharReadbuff('R');
6959 stuffcharReadbuff('\t');
6960 stuffcharReadbuff(ESC);
6961 return;
6962 }
6963
6964 /* save line for undo */
6965 if (u_save_cursor() == FAIL)
6966 return;
6967
6968 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6969 {
6970 /*
6971 * Replace character(s) by a single newline.
6972 * Strange vi behaviour: Only one newline is inserted.
6973 * Delete the characters here.
6974 * Insert the newline with an insert command, takes care of
6975 * autoindent. The insert command depends on being on the last
6976 * character of a line or not.
6977 */
6978#ifdef FEAT_MBYTE
6979 (void)del_chars(cap->count1, FALSE); /* delete the characters */
6980#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00006981 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982#endif
6983 stuffcharReadbuff('\r');
6984 stuffcharReadbuff(ESC);
6985
6986 /* Give 'r' to edit(), to get the redo command right. */
6987 invoke_edit(cap, TRUE, 'r', FALSE);
6988 }
6989 else
6990 {
6991 prep_redo(cap->oap->regname, cap->count1,
6992 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6993
6994 curbuf->b_op_start = curwin->w_cursor;
6995#ifdef FEAT_MBYTE
6996 if (has_mbyte)
6997 {
6998 int old_State = State;
6999
7000 if (cap->ncharC1 != 0)
7001 AppendCharToRedobuff(cap->ncharC1);
7002 if (cap->ncharC2 != 0)
7003 AppendCharToRedobuff(cap->ncharC2);
7004
7005 /* This is slow, but it handles replacing a single-byte with a
7006 * multi-byte and the other way around. Also handles adding
7007 * composing characters for utf-8. */
7008 for (n = cap->count1; n > 0; --n)
7009 {
7010 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007011 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7012 {
7013 int c = ins_copychar(curwin->w_cursor.lnum
7014 + (cap->nchar == Ctrl_Y ? -1 : 1));
7015 if (c != NUL)
7016 ins_char(c);
7017 else
7018 /* will be decremented further down */
7019 ++curwin->w_cursor.col;
7020 }
7021 else
7022 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 State = old_State;
7024 if (cap->ncharC1 != 0)
7025 ins_char(cap->ncharC1);
7026 if (cap->ncharC2 != 0)
7027 ins_char(cap->ncharC2);
7028 }
7029 }
7030 else
7031#endif
7032 {
7033 /*
7034 * Replace the characters within one line.
7035 */
7036 for (n = cap->count1; n > 0; --n)
7037 {
7038 /*
7039 * Get ptr again, because u_save and/or showmatch() will have
7040 * released the line. At the same time we let know that the
7041 * line will be changed.
7042 */
7043 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007044 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7045 {
7046 int c = ins_copychar(curwin->w_cursor.lnum
7047 + (cap->nchar == Ctrl_Y ? -1 : 1));
7048 if (c != NUL)
7049 ptr[curwin->w_cursor.col] = c;
7050 }
7051 else
7052 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (p_sm && msg_silent == 0)
7054 showmatch(cap->nchar);
7055 ++curwin->w_cursor.col;
7056 }
7057#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007058 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007060 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar009b2592004-10-24 19:18:58 +00007062 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007063 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007065 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 }
7067#endif
7068
7069 /* mark the buffer as changed and prepare for displaying */
7070 changed_bytes(curwin->w_cursor.lnum,
7071 (colnr_T)(curwin->w_cursor.col - cap->count1));
7072 }
7073 --curwin->w_cursor.col; /* cursor on the last replaced char */
7074#ifdef FEAT_MBYTE
7075 /* if the character on the left of the current cursor is a multi-byte
7076 * character, move two characters left */
7077 if (has_mbyte)
7078 mb_adjust_cursor();
7079#endif
7080 curbuf->b_op_end = curwin->w_cursor;
7081 curwin->w_set_curswant = TRUE;
7082 set_last_insert(cap->nchar);
7083 }
7084}
7085
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086/*
7087 * 'o': Exchange start and end of Visual area.
7088 * 'O': same, but in block mode exchange left and right corners.
7089 */
7090 static void
7091v_swap_corners(cmdchar)
7092 int cmdchar;
7093{
7094 pos_T old_cursor;
7095 colnr_T left, right;
7096
7097 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7098 {
7099 old_cursor = curwin->w_cursor;
7100 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7101 curwin->w_cursor.lnum = VIsual.lnum;
7102 coladvance(left);
7103 VIsual = curwin->w_cursor;
7104
7105 curwin->w_cursor.lnum = old_cursor.lnum;
7106 curwin->w_curswant = right;
7107 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7108 * right one column */
7109 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7110 ++curwin->w_curswant;
7111 coladvance(curwin->w_curswant);
7112 if (curwin->w_cursor.col == old_cursor.col
7113#ifdef FEAT_VIRTUALEDIT
7114 && (!virtual_active()
7115 || curwin->w_cursor.coladd == old_cursor.coladd)
7116#endif
7117 )
7118 {
7119 curwin->w_cursor.lnum = VIsual.lnum;
7120 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7121 ++right;
7122 coladvance(right);
7123 VIsual = curwin->w_cursor;
7124
7125 curwin->w_cursor.lnum = old_cursor.lnum;
7126 coladvance(left);
7127 curwin->w_curswant = left;
7128 }
7129 }
7130 else
7131 {
7132 old_cursor = curwin->w_cursor;
7133 curwin->w_cursor = VIsual;
7134 VIsual = old_cursor;
7135 curwin->w_set_curswant = TRUE;
7136 }
7137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139/*
7140 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7141 */
7142 static void
7143nv_Replace(cap)
7144 cmdarg_T *cap;
7145{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (VIsual_active) /* "R" is replace lines */
7147 {
7148 cap->cmdchar = 'c';
7149 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007150 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 VIsual_mode = 'V';
7152 nv_operator(cap);
7153 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007154 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
7156 if (!curbuf->b_p_ma)
7157 EMSG(_(e_modifiable));
7158 else
7159 {
7160#ifdef FEAT_VIRTUALEDIT
7161 if (virtual_active())
7162 coladvance(getviscol());
7163#endif
7164 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7165 }
7166 }
7167}
7168
7169#ifdef FEAT_VREPLACE
7170/*
7171 * "gr".
7172 */
7173 static void
7174nv_vreplace(cap)
7175 cmdarg_T *cap;
7176{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (VIsual_active)
7178 {
7179 cap->cmdchar = 'r';
7180 cap->nchar = cap->extra_char;
7181 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7182 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007183 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
7185 if (!curbuf->b_p_ma)
7186 EMSG(_(e_modifiable));
7187 else
7188 {
7189 if (cap->extra_char == Ctrl_V) /* get another character */
7190 cap->extra_char = get_literal();
7191 stuffcharReadbuff(cap->extra_char);
7192 stuffcharReadbuff(ESC);
7193# ifdef FEAT_VIRTUALEDIT
7194 if (virtual_active())
7195 coladvance(getviscol());
7196# endif
7197 invoke_edit(cap, TRUE, 'v', FALSE);
7198 }
7199 }
7200}
7201#endif
7202
7203/*
7204 * Swap case for "~" command, when it does not work like an operator.
7205 */
7206 static void
7207n_swapchar(cap)
7208 cmdarg_T *cap;
7209{
7210 long n;
7211 pos_T startpos;
7212 int did_change = 0;
7213#ifdef FEAT_NETBEANS_INTG
7214 pos_T pos;
7215 char_u *ptr;
7216 int count;
7217#endif
7218
7219 if (checkclearopq(cap->oap))
7220 return;
7221
7222 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7223 {
7224 clearopbeep(cap->oap);
7225 return;
7226 }
7227
7228 prep_redo_cmd(cap);
7229
7230 if (u_save_cursor() == FAIL)
7231 return;
7232
7233 startpos = curwin->w_cursor;
7234#ifdef FEAT_NETBEANS_INTG
7235 pos = startpos;
7236#endif
7237 for (n = cap->count1; n > 0; --n)
7238 {
7239 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7240 inc_cursor();
7241 if (gchar_cursor() == NUL)
7242 {
7243 if (vim_strchr(p_ww, '~') != NULL
7244 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7245 {
7246#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007247 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249 if (did_change)
7250 {
7251 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007252 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007253 netbeans_removed(curbuf, pos.lnum, pos.col,
7254 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007256 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 pos.col = 0;
7259 pos.lnum++;
7260 }
7261#endif
7262 ++curwin->w_cursor.lnum;
7263 curwin->w_cursor.col = 0;
7264 if (n > 1)
7265 {
7266 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7267 break;
7268 u_clearline();
7269 }
7270 }
7271 else
7272 break;
7273 }
7274 }
7275#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007276 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
7278 ptr = ml_get(pos.lnum);
7279 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007280 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7281 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283#endif
7284
7285
7286 check_cursor();
7287 curwin->w_set_curswant = TRUE;
7288 if (did_change)
7289 {
7290 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7291 0L);
7292 curbuf->b_op_start = startpos;
7293 curbuf->b_op_end = curwin->w_cursor;
7294 if (curbuf->b_op_end.col > 0)
7295 --curbuf->b_op_end.col;
7296 }
7297}
7298
7299/*
7300 * Move cursor to mark.
7301 */
7302 static void
7303nv_cursormark(cap, flag, pos)
7304 cmdarg_T *cap;
7305 int flag;
7306 pos_T *pos;
7307{
7308 if (check_mark(pos) == FAIL)
7309 clearop(cap->oap);
7310 else
7311 {
7312 if (cap->cmdchar == '\''
7313 || cap->cmdchar == '`'
7314 || cap->cmdchar == '['
7315 || cap->cmdchar == ']')
7316 setpcmark();
7317 curwin->w_cursor = *pos;
7318 if (flag)
7319 beginline(BL_WHITE | BL_FIX);
7320 else
7321 check_cursor();
7322 }
7323 cap->oap->motion_type = flag ? MLINE : MCHAR;
7324 if (cap->cmdchar == '`')
7325 cap->oap->use_reg_one = TRUE;
7326 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7327 curwin->w_set_curswant = TRUE;
7328}
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330/*
7331 * Handle commands that are operators in Visual mode.
7332 */
7333 static void
7334v_visop(cap)
7335 cmdarg_T *cap;
7336{
7337 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7338
7339 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007340 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 if (isupper(cap->cmdchar))
7342 {
7343 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007344 {
7345 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7349 curwin->w_curswant = MAXCOL;
7350 }
7351 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7352 nv_operator(cap);
7353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
7355/*
7356 * "s" and "S" commands.
7357 */
7358 static void
7359nv_subst(cap)
7360 cmdarg_T *cap;
7361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7363 {
7364 if (cap->cmdchar == 'S')
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 cap->cmdchar = 'c';
7370 nv_operator(cap);
7371 }
7372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 nv_optrans(cap);
7374}
7375
7376/*
7377 * Abbreviated commands.
7378 */
7379 static void
7380nv_abbrev(cap)
7381 cmdarg_T *cap;
7382{
7383 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7384 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 /* in Visual mode these commands are operators */
7387 if (VIsual_active)
7388 v_visop(cap);
7389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 nv_optrans(cap);
7391}
7392
7393/*
7394 * Translate a command into another command.
7395 */
7396 static void
7397nv_optrans(cap)
7398 cmdarg_T *cap;
7399{
7400 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7401 (char_u *)"d$", (char_u *)"c$",
7402 (char_u *)"cl", (char_u *)"cc",
7403 (char_u *)"yy", (char_u *)":s\r"};
7404 static char_u *str = (char_u *)"xXDCsSY&";
7405
7406 if (!checkclearopq(cap->oap))
7407 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007408 /* In Vi "2D" doesn't delete the next line. Can't translate it
7409 * either, because "2." should also not use the count. */
7410 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7411 {
7412 cap->oap->start = curwin->w_cursor;
7413 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007414#ifdef FEAT_EVAL
7415 set_op_var(OP_DELETE);
7416#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007417 cap->count1 = 1;
7418 nv_dollar(cap);
7419 finish_op = TRUE;
7420 ResetRedobuff();
7421 AppendCharToRedobuff('D');
7422 }
7423 else
7424 {
7425 if (cap->count0)
7426 stuffnumReadbuff(cap->count0);
7427 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 cap->opcount = 0;
7431}
7432
7433/*
7434 * "'" and "`" commands. Also for "g'" and "g`".
7435 * cap->arg is TRUE for "'" and "g'".
7436 */
7437 static void
7438nv_gomark(cap)
7439 cmdarg_T *cap;
7440{
7441 pos_T *pos;
7442 int c;
7443#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007444 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7446#endif
7447
7448 if (cap->cmdchar == 'g')
7449 c = cap->extra_char;
7450 else
7451 c = cap->nchar;
7452 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7453 if (pos == (pos_T *)-1) /* jumped to other file */
7454 {
7455 if (cap->arg)
7456 {
7457 check_cursor_lnum();
7458 beginline(BL_WHITE | BL_FIX);
7459 }
7460 else
7461 check_cursor();
7462 }
7463 else
7464 nv_cursormark(cap, cap->arg, pos);
7465
7466#ifdef FEAT_VIRTUALEDIT
7467 /* May need to clear the coladd that a mark includes. */
7468 if (!virtual_active())
7469 curwin->w_cursor.coladd = 0;
7470#endif
7471#ifdef FEAT_FOLDING
7472 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007473 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007474 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 && (fdo_flags & FDO_MARK)
7476 && old_KeyTyped)
7477 foldOpenCursor();
7478#endif
7479}
7480
7481/*
7482 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7483 */
7484 static void
7485nv_pcmark(cap)
7486 cmdarg_T *cap;
7487{
7488#ifdef FEAT_JUMPLIST
7489 pos_T *pos;
7490# ifdef FEAT_FOLDING
7491 linenr_T lnum = curwin->w_cursor.lnum;
7492 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7493# endif
7494
7495 if (!checkclearopq(cap->oap))
7496 {
7497 if (cap->cmdchar == 'g')
7498 pos = movechangelist((int)cap->count1);
7499 else
7500 pos = movemark((int)cap->count1);
7501 if (pos == (pos_T *)-1) /* jump to other file */
7502 {
7503 curwin->w_set_curswant = TRUE;
7504 check_cursor();
7505 }
7506 else if (pos != NULL) /* can jump */
7507 nv_cursormark(cap, FALSE, pos);
7508 else if (cap->cmdchar == 'g')
7509 {
7510 if (curbuf->b_changelistlen == 0)
7511 EMSG(_("E664: changelist is empty"));
7512 else if (cap->count1 < 0)
7513 EMSG(_("E662: At start of changelist"));
7514 else
7515 EMSG(_("E663: At end of changelist"));
7516 }
7517 else
7518 clearopbeep(cap->oap);
7519# ifdef FEAT_FOLDING
7520 if (cap->oap->op_type == OP_NOP
7521 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7522 && (fdo_flags & FDO_MARK)
7523 && old_KeyTyped)
7524 foldOpenCursor();
7525# endif
7526 }
7527#else
7528 clearopbeep(cap->oap);
7529#endif
7530}
7531
7532/*
7533 * Handle '"' command.
7534 */
7535 static void
7536nv_regname(cap)
7537 cmdarg_T *cap;
7538{
7539 if (checkclearop(cap->oap))
7540 return;
7541#ifdef FEAT_EVAL
7542 if (cap->nchar == '=')
7543 cap->nchar = get_expr_register();
7544#endif
7545 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7546 {
7547 cap->oap->regname = cap->nchar;
7548 cap->opcount = cap->count0; /* remember count before '"' */
7549#ifdef FEAT_EVAL
7550 set_reg_var(cap->oap->regname);
7551#endif
7552 }
7553 else
7554 clearopbeep(cap->oap);
7555}
7556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557/*
7558 * Handle "v", "V" and "CTRL-V" commands.
7559 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7560 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007561 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 */
7563 static void
7564nv_visual(cap)
7565 cmdarg_T *cap;
7566{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007567 if (cap->cmdchar == Ctrl_Q)
7568 cap->cmdchar = Ctrl_V;
7569
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7571 * characterwise, linewise, or blockwise. */
7572 if (cap->oap->op_type != OP_NOP)
7573 {
7574 cap->oap->motion_force = cap->cmdchar;
7575 finish_op = FALSE; /* operator doesn't finish now but later */
7576 return;
7577 }
7578
7579 VIsual_select = cap->arg;
7580 if (VIsual_active) /* change Visual mode */
7581 {
7582 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7583 end_visual_mode();
7584 else /* toggle char/block mode */
7585 { /* or char/line mode */
7586 VIsual_mode = cap->cmdchar;
7587 showmode();
7588 }
7589 redraw_curbuf_later(INVERTED); /* update the inversion */
7590 }
7591 else /* start Visual mode */
7592 {
7593 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007594 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007596 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 VIsual = curwin->w_cursor;
7598
7599 VIsual_active = TRUE;
7600 VIsual_reselect = TRUE;
7601 if (!cap->arg)
7602 /* start Select mode when 'selectmode' contains "cmd" */
7603 may_start_select('c');
7604#ifdef FEAT_MOUSE
7605 setmouse();
7606#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007607 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 redraw_cmdline = TRUE; /* show visual mode later */
7609 /*
7610 * For V and ^V, we multiply the number of lines even if there
7611 * was only one -- webb
7612 */
7613 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7614 {
7615 curwin->w_cursor.lnum +=
7616 resel_VIsual_line_count * cap->count0 - 1;
7617 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7618 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7619 }
7620 VIsual_mode = resel_VIsual_mode;
7621 if (VIsual_mode == 'v')
7622 {
7623 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007624 {
7625 validate_virtcol();
7626 curwin->w_curswant = curwin->w_virtcol
7627 + resel_VIsual_vcol * cap->count0 - 1;
7628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007630 curwin->w_curswant = resel_VIsual_vcol;
7631 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007633 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 {
7635 curwin->w_curswant = MAXCOL;
7636 coladvance((colnr_T)MAXCOL);
7637 }
7638 else if (VIsual_mode == Ctrl_V)
7639 {
7640 validate_virtcol();
7641 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007642 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 coladvance(curwin->w_curswant);
7644 }
7645 else
7646 curwin->w_set_curswant = TRUE;
7647 redraw_curbuf_later(INVERTED); /* show the inversion */
7648 }
7649 else
7650 {
7651 if (!cap->arg)
7652 /* start Select mode when 'selectmode' contains "cmd" */
7653 may_start_select('c');
7654 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007655 if (VIsual_mode != 'V' && *p_sel == 'e')
7656 ++cap->count1; /* include one more char */
7657 if (cap->count0 > 0 && --cap->count1 > 0)
7658 {
7659 /* With a count select that many characters or lines. */
7660 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7661 nv_right(cap);
7662 else if (VIsual_mode == 'V')
7663 nv_down(cap);
7664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666 }
7667}
7668
7669/*
7670 * Start selection for Shift-movement keys.
7671 */
7672 void
7673start_selection()
7674{
7675 /* if 'selectmode' contains "key", start Select mode */
7676 may_start_select('k');
7677 n_start_visual_mode('v');
7678}
7679
7680/*
7681 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7682 */
7683 void
7684may_start_select(c)
7685 int c;
7686{
7687 VIsual_select = (stuff_empty() && typebuf_typed()
7688 && (vim_strchr(p_slm, c) != NULL));
7689}
7690
7691/*
7692 * Start Visual mode "c".
7693 * Should set VIsual_select before calling this.
7694 */
7695 static void
7696n_start_visual_mode(c)
7697 int c;
7698{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007699#ifdef FEAT_CONCEAL
7700 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007701 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007702#endif
7703
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 VIsual_mode = c;
7705 VIsual_active = TRUE;
7706 VIsual_reselect = TRUE;
7707#ifdef FEAT_VIRTUALEDIT
7708 /* Corner case: the 0 position in a tab may change when going into
7709 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7710 */
7711 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007712 {
7713 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716#endif
7717 VIsual = curwin->w_cursor;
7718
7719#ifdef FEAT_FOLDING
7720 foldAdjustVisual();
7721#endif
7722
7723#ifdef FEAT_MOUSE
7724 setmouse();
7725#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007726#ifdef FEAT_CONCEAL
7727 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007728 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007729#endif
7730
Bram Moolenaar09df3122006-01-23 22:23:09 +00007731 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 redraw_cmdline = TRUE; /* show visual mode later */
7733#ifdef FEAT_CLIPBOARD
7734 /* Make sure the clipboard gets updated. Needed because start and
7735 * end may still be the same, and the selection needs to be owned */
7736 clip_star.vmode = NUL;
7737#endif
7738
7739 /* Only need to redraw this line, unless still need to redraw an old
7740 * Visual area (when 'lazyredraw' is set). */
7741 if (curwin->w_redr_type < INVERTED)
7742 {
7743 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7744 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7745 }
7746}
7747
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
7749/*
7750 * CTRL-W: Window commands
7751 */
7752 static void
7753nv_window(cap)
7754 cmdarg_T *cap;
7755{
7756#ifdef FEAT_WINDOWS
7757 if (!checkclearop(cap->oap))
7758 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7759#else
7760 (void)checkclearop(cap->oap);
7761#endif
7762}
7763
7764/*
7765 * CTRL-Z: Suspend
7766 */
7767 static void
7768nv_suspend(cap)
7769 cmdarg_T *cap;
7770{
7771 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (VIsual_active)
7773 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 do_cmdline_cmd((char_u *)"st");
7775}
7776
7777/*
7778 * Commands starting with "g".
7779 */
7780 static void
7781nv_g_cmd(cap)
7782 cmdarg_T *cap;
7783{
7784 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 int i;
7787 int flag = FALSE;
7788
7789 switch (cap->nchar)
7790 {
7791#ifdef MEM_PROFILE
7792 /*
7793 * "g^A": dump log of used memory.
7794 */
7795 case Ctrl_A:
7796 vim_mem_profile_dump();
7797 break;
7798#endif
7799
7800#ifdef FEAT_VREPLACE
7801 /*
7802 * "gR": Enter virtual replace mode.
7803 */
7804 case 'R':
7805 cap->arg = TRUE;
7806 nv_Replace(cap);
7807 break;
7808
7809 case 'r':
7810 nv_vreplace(cap);
7811 break;
7812#endif
7813
7814 case '&':
7815 do_cmdline_cmd((char_u *)"%s//~/&");
7816 break;
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 /*
7819 * "gv": Reselect the previous Visual area. If Visual already active,
7820 * exchange previous and current Visual area.
7821 */
7822 case 'v':
7823 if (checkclearop(oap))
7824 break;
7825
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007826 if ( curbuf->b_visual.vi_start.lnum == 0
7827 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7828 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 beep_flush();
7830 else
7831 {
7832 /* set w_cursor to the start of the Visual area, tpos to the end */
7833 if (VIsual_active)
7834 {
7835 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007836 VIsual_mode = curbuf->b_visual.vi_mode;
7837 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838# ifdef FEAT_EVAL
7839 curbuf->b_visual_mode_eval = i;
7840# endif
7841 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007842 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7843 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007845 tpos = curbuf->b_visual.vi_end;
7846 curbuf->b_visual.vi_end = curwin->w_cursor;
7847 curwin->w_cursor = curbuf->b_visual.vi_start;
7848 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850 else
7851 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007852 VIsual_mode = curbuf->b_visual.vi_mode;
7853 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7854 tpos = curbuf->b_visual.vi_end;
7855 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857
7858 VIsual_active = TRUE;
7859 VIsual_reselect = TRUE;
7860
7861 /* Set Visual to the start and w_cursor to the end of the Visual
7862 * area. Make sure they are on an existing character. */
7863 check_cursor();
7864 VIsual = curwin->w_cursor;
7865 curwin->w_cursor = tpos;
7866 check_cursor();
7867 update_topline();
7868 /*
7869 * When called from normal "g" command: start Select mode when
7870 * 'selectmode' contains "cmd". When called for K_SELECT, always
7871 * start Select mode.
7872 */
7873 if (cap->arg)
7874 VIsual_select = TRUE;
7875 else
7876 may_start_select('c');
7877#ifdef FEAT_MOUSE
7878 setmouse();
7879#endif
7880#ifdef FEAT_CLIPBOARD
7881 /* Make sure the clipboard gets updated. Needed because start and
7882 * end are still the same, and the selection needs to be owned */
7883 clip_star.vmode = NUL;
7884#endif
7885 redraw_curbuf_later(INVERTED);
7886 showmode();
7887 }
7888 break;
7889 /*
7890 * "gV": Don't reselect the previous Visual area after a Select mode
7891 * mapping of menu.
7892 */
7893 case 'V':
7894 VIsual_reselect = FALSE;
7895 break;
7896
7897 /*
7898 * "gh": start Select mode.
7899 * "gH": start Select line mode.
7900 * "g^H": start Select block mode.
7901 */
7902 case K_BS:
7903 cap->nchar = Ctrl_H;
7904 /* FALLTHROUGH */
7905 case 'h':
7906 case 'H':
7907 case Ctrl_H:
7908# ifdef EBCDIC
7909 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7910 if (cap->nchar == Ctrl_H)
7911 cap->cmdchar = Ctrl_V;
7912 else
7913# endif
7914 cap->cmdchar = cap->nchar + ('v' - 'h');
7915 cap->arg = TRUE;
7916 nv_visual(cap);
7917 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007918
7919 /* "gn", "gN" visually select next/previous search match
7920 * "gn" selects next match
7921 * "gN" selects previous match
7922 */
7923 case 'N':
7924 case 'n':
7925 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007926 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 /*
7930 * "gj" and "gk" two new funny movement keys -- up and down
7931 * movement based on *screen* line rather than *file* line.
7932 */
7933 case 'j':
7934 case K_DOWN:
7935 /* with 'nowrap' it works just like the normal "j" command; also when
7936 * in a closed fold */
7937 if (!curwin->w_p_wrap
7938#ifdef FEAT_FOLDING
7939 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7940#endif
7941 )
7942 {
7943 oap->motion_type = MLINE;
7944 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7945 }
7946 else
7947 i = nv_screengo(oap, FORWARD, cap->count1);
7948 if (i == FAIL)
7949 clearopbeep(oap);
7950 break;
7951
7952 case 'k':
7953 case K_UP:
7954 /* with 'nowrap' it works just like the normal "k" command; also when
7955 * in a closed fold */
7956 if (!curwin->w_p_wrap
7957#ifdef FEAT_FOLDING
7958 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7959#endif
7960 )
7961 {
7962 oap->motion_type = MLINE;
7963 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7964 }
7965 else
7966 i = nv_screengo(oap, BACKWARD, cap->count1);
7967 if (i == FAIL)
7968 clearopbeep(oap);
7969 break;
7970
7971 /*
7972 * "gJ": join two lines without inserting a space.
7973 */
7974 case 'J':
7975 nv_join(cap);
7976 break;
7977
7978 /*
7979 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7980 * "gm": middle of "g0" and "g$".
7981 */
7982 case '^':
7983 flag = TRUE;
7984 /* FALLTHROUGH */
7985
7986 case '0':
7987 case 'm':
7988 case K_HOME:
7989 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 oap->motion_type = MCHAR;
7991 oap->inclusive = FALSE;
7992 if (curwin->w_p_wrap
7993#ifdef FEAT_VERTSPLIT
7994 && curwin->w_width != 0
7995#endif
7996 )
7997 {
7998 int width1 = W_WIDTH(curwin) - curwin_col_off();
7999 int width2 = width1 + curwin_col_off2();
8000
8001 validate_virtcol();
8002 i = 0;
8003 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8004 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8005 }
8006 else
8007 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008008 /* Go to the middle of the screen line. When 'number' or
8009 * 'relativenumber' is on and lines are wrapping the middle can be more
8010 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (cap->nchar == 'm')
8012 i += (W_WIDTH(curwin) - curwin_col_off()
8013 + ((curwin->w_p_wrap && i > 0)
8014 ? curwin_col_off2() : 0)) / 2;
8015 coladvance((colnr_T)i);
8016 if (flag)
8017 {
8018 do
8019 i = gchar_cursor();
8020 while (vim_iswhite(i) && oneright() == OK);
8021 }
8022 curwin->w_set_curswant = TRUE;
8023 break;
8024
8025 case '_':
8026 /* "g_": to the last non-blank character in the line or <count> lines
8027 * downward. */
8028 cap->oap->motion_type = MCHAR;
8029 cap->oap->inclusive = TRUE;
8030 curwin->w_curswant = MAXCOL;
8031 if (cursor_down((long)(cap->count1 - 1),
8032 cap->oap->op_type == OP_NOP) == FAIL)
8033 clearopbeep(cap->oap);
8034 else
8035 {
8036 char_u *ptr = ml_get_curline();
8037
8038 /* In Visual mode we may end up after the line. */
8039 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8040 --curwin->w_cursor.col;
8041
8042 /* Decrease the cursor column until it's on a non-blank. */
8043 while (curwin->w_cursor.col > 0
8044 && vim_iswhite(ptr[curwin->w_cursor.col]))
8045 --curwin->w_cursor.col;
8046 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008047 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 }
8049 break;
8050
8051 case '$':
8052 case K_END:
8053 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 int col_off = curwin_col_off();
8056
8057 oap->motion_type = MCHAR;
8058 oap->inclusive = TRUE;
8059 if (curwin->w_p_wrap
8060#ifdef FEAT_VERTSPLIT
8061 && curwin->w_width != 0
8062#endif
8063 )
8064 {
8065 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8066 if (cap->count1 == 1)
8067 {
8068 int width1 = W_WIDTH(curwin) - col_off;
8069 int width2 = width1 + curwin_col_off2();
8070
8071 validate_virtcol();
8072 i = width1 - 1;
8073 if (curwin->w_virtcol >= (colnr_T)width1)
8074 i += ((curwin->w_virtcol - width1) / width2 + 1)
8075 * width2;
8076 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008077
8078 /* Make sure we stick in this column. */
8079 validate_virtcol();
8080 curwin->w_curswant = curwin->w_virtcol;
8081 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8083 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8084 {
8085 /*
8086 * Check for landing on a character that got split at
8087 * the end of the line. We do not want to advance to
8088 * the next screen line.
8089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (curwin->w_virtcol > (colnr_T)i)
8091 --curwin->w_cursor.col;
8092 }
8093#endif
8094 }
8095 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8096 clearopbeep(oap);
8097 }
8098 else
8099 {
8100 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8101 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008102
8103 /* Make sure we stick in this column. */
8104 validate_virtcol();
8105 curwin->w_curswant = curwin->w_virtcol;
8106 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108 }
8109 break;
8110
8111 /*
8112 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8113 */
8114 case '*':
8115 case '#':
8116#if POUND != '#'
8117 case POUND: /* pound sign (sometimes equal to '#') */
8118#endif
8119 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8120 case ']': /* :tselect for current identifier */
8121 nv_ident(cap);
8122 break;
8123
8124 /*
8125 * ge and gE: go back to end of word
8126 */
8127 case 'e':
8128 case 'E':
8129 oap->motion_type = MCHAR;
8130 curwin->w_set_curswant = TRUE;
8131 oap->inclusive = TRUE;
8132 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8133 clearopbeep(oap);
8134 break;
8135
8136 /*
8137 * "g CTRL-G": display info about cursor position
8138 */
8139 case Ctrl_G:
8140 cursor_pos_info();
8141 break;
8142
8143 /*
8144 * "gi": start Insert at the last position.
8145 */
8146 case 'i':
8147 if (curbuf->b_last_insert.lnum != 0)
8148 {
8149 curwin->w_cursor = curbuf->b_last_insert;
8150 check_cursor_lnum();
8151 i = (int)STRLEN(ml_get_curline());
8152 if (curwin->w_cursor.col > (colnr_T)i)
8153 {
8154#ifdef FEAT_VIRTUALEDIT
8155 if (virtual_active())
8156 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8157#endif
8158 curwin->w_cursor.col = i;
8159 }
8160 }
8161 cap->cmdchar = 'i';
8162 nv_edit(cap);
8163 break;
8164
8165 /*
8166 * "gI": Start insert in column 1.
8167 */
8168 case 'I':
8169 beginline(0);
8170 if (!checkclearopq(oap))
8171 invoke_edit(cap, FALSE, 'g', FALSE);
8172 break;
8173
8174#ifdef FEAT_SEARCHPATH
8175 /*
8176 * "gf": goto file, edit file under cursor
8177 * "]f" and "[f": can also be used.
8178 */
8179 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008180 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 nv_gotofile(cap);
8182 break;
8183#endif
8184
8185 /* "g'm" and "g`m": jump to mark without setting pcmark */
8186 case '\'':
8187 cap->arg = TRUE;
8188 /*FALLTHROUGH*/
8189 case '`':
8190 nv_gomark(cap);
8191 break;
8192
8193 /*
8194 * "gs": Goto sleep.
8195 */
8196 case 's':
8197 do_sleep(cap->count1 * 1000L);
8198 break;
8199
8200 /*
8201 * "ga": Display the ascii value of the character under the
8202 * cursor. It is displayed in decimal, hex, and octal. -- webb
8203 */
8204 case 'a':
8205 do_ascii(NULL);
8206 break;
8207
8208#ifdef FEAT_MBYTE
8209 /*
8210 * "g8": Display the bytes used for the UTF-8 character under the
8211 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008212 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 */
8214 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008215 if (cap->count0 == 8)
8216 utf_find_illegal();
8217 else
8218 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 break;
8220#endif
8221
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008222 case '<':
8223 show_sb_text();
8224 break;
8225
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 /*
8227 * "gg": Goto the first line in file. With a count it goes to
8228 * that line number like for "G". -- webb
8229 */
8230 case 'g':
8231 cap->arg = FALSE;
8232 nv_goto(cap);
8233 break;
8234
8235 /*
8236 * Two-character operators:
8237 * "gq" Format text
8238 * "gw" Format text and keep cursor position
8239 * "g~" Toggle the case of the text.
8240 * "gu" Change text to lower case.
8241 * "gU" Change text to upper case.
8242 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008243 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 case 'q':
8246 case 'w':
8247 oap->cursor_start = curwin->w_cursor;
8248 /*FALLTHROUGH*/
8249 case '~':
8250 case 'u':
8251 case 'U':
8252 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008253 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 nv_operator(cap);
8255 break;
8256
8257 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008258 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 * current function
8260 * "gD": idem, but in the current file.
8261 */
8262 case 'd':
8263 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008264 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 break;
8266
8267#ifdef FEAT_MOUSE
8268 /*
8269 * g<*Mouse> : <C-*mouse>
8270 */
8271 case K_MIDDLEMOUSE:
8272 case K_MIDDLEDRAG:
8273 case K_MIDDLERELEASE:
8274 case K_LEFTMOUSE:
8275 case K_LEFTDRAG:
8276 case K_LEFTRELEASE:
8277 case K_RIGHTMOUSE:
8278 case K_RIGHTDRAG:
8279 case K_RIGHTRELEASE:
8280 case K_X1MOUSE:
8281 case K_X1DRAG:
8282 case K_X1RELEASE:
8283 case K_X2MOUSE:
8284 case K_X2DRAG:
8285 case K_X2RELEASE:
8286 mod_mask = MOD_MASK_CTRL;
8287 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8288 break;
8289#endif
8290
8291 case K_IGNORE:
8292 break;
8293
8294 /*
8295 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8296 */
8297 case 'p':
8298 case 'P':
8299 nv_put(cap);
8300 break;
8301
8302#ifdef FEAT_BYTEOFF
8303 /* "go": goto byte count from start of buffer */
8304 case 'o':
8305 goto_byte(cap->count0);
8306 break;
8307#endif
8308
8309 /* "gQ": improved Ex mode */
8310 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008311 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {
8313 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008314 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 break;
8316 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (!checkclearopq(oap))
8319 do_exmode(TRUE);
8320 break;
8321
8322#ifdef FEAT_JUMPLIST
8323 case ',':
8324 nv_pcmark(cap);
8325 break;
8326
8327 case ';':
8328 cap->count1 = -cap->count1;
8329 nv_pcmark(cap);
8330 break;
8331#endif
8332
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008333#ifdef FEAT_WINDOWS
8334 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008335 if (!checkclearop(oap))
8336 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008338 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008339 if (!checkclearop(oap))
8340 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008341 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008342#endif
8343
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008344 case '+':
8345 case '-': /* "g+" and "g-": undo or redo along the timeline */
8346 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008347 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008348 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008349 break;
8350
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 default:
8352 clearopbeep(oap);
8353 break;
8354 }
8355}
8356
8357/*
8358 * Handle "o" and "O" commands.
8359 */
8360 static void
8361n_opencmd(cap)
8362 cmdarg_T *cap;
8363{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008364#ifdef FEAT_CONCEAL
8365 linenr_T oldline = curwin->w_cursor.lnum;
8366#endif
8367
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 if (!checkclearopq(cap->oap))
8369 {
8370#ifdef FEAT_FOLDING
8371 if (cap->cmdchar == 'O')
8372 /* Open above the first line of a folded sequence of lines */
8373 (void)hasFolding(curwin->w_cursor.lnum,
8374 &curwin->w_cursor.lnum, NULL);
8375 else
8376 /* Open below the last line of a folded sequence of lines */
8377 (void)hasFolding(curwin->w_cursor.lnum,
8378 NULL, &curwin->w_cursor.lnum);
8379#endif
8380 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8381 (cap->cmdchar == 'O' ? 1 : 0)),
8382 (linenr_T)(curwin->w_cursor.lnum +
8383 (cap->cmdchar == 'o' ? 1 : 0))
8384 ) == OK
8385 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8386#ifdef FEAT_COMMENTS
8387 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8388#endif
8389 0, 0))
8390 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008391#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008392 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008393 update_single_line(curwin, oldline);
8394#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008395 /* When '#' is in 'cpoptions' ignore the count. */
8396 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8397 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8399 }
8400 }
8401}
8402
8403/*
8404 * "." command: redo last change.
8405 */
8406 static void
8407nv_dot(cap)
8408 cmdarg_T *cap;
8409{
8410 if (!checkclearopq(cap->oap))
8411 {
8412 /*
8413 * If "restart_edit" is TRUE, the last but one command is repeated
8414 * instead of the last command (inserting text). This is used for
8415 * CTRL-O <.> in insert mode.
8416 */
8417 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8418 clearopbeep(cap->oap);
8419 }
8420}
8421
8422/*
8423 * CTRL-R: undo undo
8424 */
8425 static void
8426nv_redo(cap)
8427 cmdarg_T *cap;
8428{
8429 if (!checkclearopq(cap->oap))
8430 {
8431 u_redo((int)cap->count1);
8432 curwin->w_set_curswant = TRUE;
8433 }
8434}
8435
8436/*
8437 * Handle "U" command.
8438 */
8439 static void
8440nv_Undo(cap)
8441 cmdarg_T *cap;
8442{
8443 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008444 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
8446 /* translate "gUU" to "gUgU" */
8447 cap->cmdchar = 'g';
8448 cap->nchar = 'U';
8449 nv_operator(cap);
8450 }
8451 else if (!checkclearopq(cap->oap))
8452 {
8453 u_undoline();
8454 curwin->w_set_curswant = TRUE;
8455 }
8456}
8457
8458/*
8459 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8460 * single character.
8461 */
8462 static void
8463nv_tilde(cap)
8464 cmdarg_T *cap;
8465{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008466 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 n_swapchar(cap);
8468 else
8469 nv_operator(cap);
8470}
8471
8472/*
8473 * Handle an operator command.
8474 * The actual work is done by do_pending_operator().
8475 */
8476 static void
8477nv_operator(cap)
8478 cmdarg_T *cap;
8479{
8480 int op_type;
8481
8482 op_type = get_op_type(cap->cmdchar, cap->nchar);
8483
8484 if (op_type == cap->oap->op_type) /* double operator works on lines */
8485 nv_lineop(cap);
8486 else if (!checkclearop(cap->oap))
8487 {
8488 cap->oap->start = curwin->w_cursor;
8489 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008490#ifdef FEAT_EVAL
8491 set_op_var(op_type);
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 }
8494}
8495
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008496#ifdef FEAT_EVAL
8497/*
8498 * Set v:operator to the characters for "optype".
8499 */
8500 static void
8501set_op_var(optype)
8502 int optype;
8503{
8504 char_u opchars[3];
8505
8506 if (optype == OP_NOP)
8507 set_vim_var_string(VV_OP, NULL, 0);
8508 else
8509 {
8510 opchars[0] = get_op_char(optype);
8511 opchars[1] = get_extra_op_char(optype);
8512 opchars[2] = NUL;
8513 set_vim_var_string(VV_OP, opchars, -1);
8514 }
8515}
8516#endif
8517
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518/*
8519 * Handle linewise operator "dd", "yy", etc.
8520 *
8521 * "_" is is a strange motion command that helps make operators more logical.
8522 * It is actually implemented, but not documented in the real Vi. This motion
8523 * command actually refers to "the current line". Commands like "dd" and "yy"
8524 * are really an alternate form of "d_" and "y_". It does accept a count, so
8525 * "d3_" works to delete 3 lines.
8526 */
8527 static void
8528nv_lineop(cap)
8529 cmdarg_T *cap;
8530{
8531 cap->oap->motion_type = MLINE;
8532 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8533 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008534 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8535 && cap->oap->motion_force != 'v'
8536 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 || cap->oap->op_type == OP_LSHIFT
8538 || cap->oap->op_type == OP_RSHIFT)
8539 beginline(BL_SOL | BL_FIX);
8540 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8541 beginline(BL_WHITE | BL_FIX);
8542}
8543
8544/*
8545 * <Home> command.
8546 */
8547 static void
8548nv_home(cap)
8549 cmdarg_T *cap;
8550{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008551 /* CTRL-HOME is like "gg" */
8552 if (mod_mask & MOD_MASK_CTRL)
8553 nv_goto(cap);
8554 else
8555 {
8556 cap->count0 = 1;
8557 nv_pipe(cap);
8558 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008559 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8560 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
8562
8563/*
8564 * "|" command.
8565 */
8566 static void
8567nv_pipe(cap)
8568 cmdarg_T *cap;
8569{
8570 cap->oap->motion_type = MCHAR;
8571 cap->oap->inclusive = FALSE;
8572 beginline(0);
8573 if (cap->count0 > 0)
8574 {
8575 coladvance((colnr_T)(cap->count0 - 1));
8576 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8577 }
8578 else
8579 curwin->w_curswant = 0;
8580 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008581 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 curwin->w_set_curswant = FALSE;
8583}
8584
8585/*
8586 * Handle back-word command "b" and "B".
8587 * cap->arg is 1 for "B"
8588 */
8589 static void
8590nv_bck_word(cap)
8591 cmdarg_T *cap;
8592{
8593 cap->oap->motion_type = MCHAR;
8594 cap->oap->inclusive = FALSE;
8595 curwin->w_set_curswant = TRUE;
8596 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8597 clearopbeep(cap->oap);
8598#ifdef FEAT_FOLDING
8599 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8600 foldOpenCursor();
8601#endif
8602}
8603
8604/*
8605 * Handle word motion commands "e", "E", "w" and "W".
8606 * cap->arg is TRUE for "E" and "W".
8607 */
8608 static void
8609nv_wordcmd(cap)
8610 cmdarg_T *cap;
8611{
8612 int n;
8613 int word_end;
8614 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008615 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 /*
8618 * Set inclusive for the "E" and "e" command.
8619 */
8620 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8621 word_end = TRUE;
8622 else
8623 word_end = FALSE;
8624 cap->oap->inclusive = word_end;
8625
8626 /*
8627 * "cw" and "cW" are a special case.
8628 */
8629 if (!word_end && cap->oap->op_type == OP_CHANGE)
8630 {
8631 n = gchar_cursor();
8632 if (n != NUL) /* not an empty line */
8633 {
8634 if (vim_iswhite(n))
8635 {
8636 /*
8637 * Reproduce a funny Vi behaviour: "cw" on a blank only
8638 * changes one character, not all blanks until the start of
8639 * the next word. Only do this when the 'w' flag is included
8640 * in 'cpoptions'.
8641 */
8642 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8643 {
8644 cap->oap->inclusive = TRUE;
8645 cap->oap->motion_type = MCHAR;
8646 return;
8647 }
8648 }
8649 else
8650 {
8651 /*
8652 * This is a little strange. To match what the real Vi does,
8653 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8654 * that we are not on a space or a TAB. This seems impolite
8655 * at first, but it's really more what we mean when we say
8656 * 'cw'.
8657 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008658 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 * will change only one character! This is done by setting
8660 * flag.
8661 */
8662 cap->oap->inclusive = TRUE;
8663 word_end = TRUE;
8664 flag = TRUE;
8665 }
8666 }
8667 }
8668
8669 cap->oap->motion_type = MCHAR;
8670 curwin->w_set_curswant = TRUE;
8671 if (word_end)
8672 n = end_word(cap->count1, cap->arg, flag, FALSE);
8673 else
8674 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8675
Bram Moolenaardfefb982008-04-01 10:06:39 +00008676 /* Don't leave the cursor on the NUL past the end of line. Unless we
8677 * didn't move it forward. */
8678 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008679 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 if (n == FAIL && cap->oap->op_type == OP_NOP)
8682 clearopbeep(cap->oap);
8683 else
8684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686#ifdef FEAT_FOLDING
8687 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8688 foldOpenCursor();
8689#endif
8690 }
8691}
8692
8693/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008694 * Used after a movement command: If the cursor ends up on the NUL after the
8695 * end of the line, may move it back to the last character and make the motion
8696 * inclusive.
8697 */
8698 static void
8699adjust_cursor(oap)
8700 oparg_T *oap;
8701{
8702 /* The cursor cannot remain on the NUL when:
8703 * - the column is > 0
8704 * - not in Visual mode or 'selection' is "o"
8705 * - 'virtualedit' is not "all" and not "onemore".
8706 */
8707 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008708 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008709#ifdef FEAT_VIRTUALEDIT
8710 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8711#endif
8712 )
8713 {
8714 --curwin->w_cursor.col;
8715#ifdef FEAT_MBYTE
8716 /* prevent cursor from moving on the trail byte */
8717 if (has_mbyte)
8718 mb_adjust_cursor();
8719#endif
8720 oap->inclusive = TRUE;
8721 }
8722}
8723
8724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 * "0" and "^" commands.
8726 * cap->arg is the argument for beginline().
8727 */
8728 static void
8729nv_beginline(cap)
8730 cmdarg_T *cap;
8731{
8732 cap->oap->motion_type = MCHAR;
8733 cap->oap->inclusive = FALSE;
8734 beginline(cap->arg);
8735#ifdef FEAT_FOLDING
8736 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8737 foldOpenCursor();
8738#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008739 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8740 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
8744 * In exclusive Visual mode, may include the last character.
8745 */
8746 static void
8747adjust_for_sel(cap)
8748 cmdarg_T *cap;
8749{
8750 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8751 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8752 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008753#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 if (has_mbyte)
8755 inc_cursor();
8756 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 ++curwin->w_cursor.col;
8759 cap->oap->inclusive = FALSE;
8760 }
8761}
8762
8763/*
8764 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8765 * Should check VIsual_mode before calling this.
8766 * Returns TRUE when backed up to the previous line.
8767 */
8768 static int
8769unadjust_for_sel()
8770{
8771 pos_T *pp;
8772
8773 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8774 {
8775 if (lt(VIsual, curwin->w_cursor))
8776 pp = &curwin->w_cursor;
8777 else
8778 pp = &VIsual;
8779#ifdef FEAT_VIRTUALEDIT
8780 if (pp->coladd > 0)
8781 --pp->coladd;
8782 else
8783#endif
8784 if (pp->col > 0)
8785 {
8786 --pp->col;
8787#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008788 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#endif
8790 }
8791 else if (pp->lnum > 1)
8792 {
8793 --pp->lnum;
8794 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8795 return TRUE;
8796 }
8797 }
8798 return FALSE;
8799}
8800
8801/*
8802 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8803 */
8804 static void
8805nv_select(cap)
8806 cmdarg_T *cap;
8807{
8808 if (VIsual_active)
8809 VIsual_select = TRUE;
8810 else if (VIsual_reselect)
8811 {
8812 cap->nchar = 'v'; /* fake "gv" command */
8813 cap->arg = TRUE;
8814 nv_g_cmd(cap);
8815 }
8816}
8817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
8819/*
8820 * "G", "gg", CTRL-END, CTRL-HOME.
8821 * cap->arg is TRUE for "G".
8822 */
8823 static void
8824nv_goto(cap)
8825 cmdarg_T *cap;
8826{
8827 linenr_T lnum;
8828
8829 if (cap->arg)
8830 lnum = curbuf->b_ml.ml_line_count;
8831 else
8832 lnum = 1L;
8833 cap->oap->motion_type = MLINE;
8834 setpcmark();
8835
8836 /* When a count is given, use it instead of the default lnum */
8837 if (cap->count0 != 0)
8838 lnum = cap->count0;
8839 if (lnum < 1L)
8840 lnum = 1L;
8841 else if (lnum > curbuf->b_ml.ml_line_count)
8842 lnum = curbuf->b_ml.ml_line_count;
8843 curwin->w_cursor.lnum = lnum;
8844 beginline(BL_SOL | BL_FIX);
8845#ifdef FEAT_FOLDING
8846 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8847 foldOpenCursor();
8848#endif
8849}
8850
8851/*
8852 * CTRL-\ in Normal mode.
8853 */
8854 static void
8855nv_normal(cap)
8856 cmdarg_T *cap;
8857{
8858 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8859 {
8860 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008861 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 clear_cmdline = TRUE; /* unshow mode later */
8863 restart_edit = 0;
8864#ifdef FEAT_CMDWIN
8865 if (cmdwin_type != 0)
8866 cmdwin_result = Ctrl_C;
8867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 if (VIsual_active)
8869 {
8870 end_visual_mode(); /* stop Visual */
8871 redraw_curbuf_later(INVERTED);
8872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8874 if (cap->nchar == Ctrl_G && p_im)
8875 restart_edit = 'a';
8876 }
8877 else
8878 clearopbeep(cap->oap);
8879}
8880
8881/*
8882 * ESC in Normal mode: beep, but don't flush buffers.
8883 * Don't even beep if we are canceling a command.
8884 */
8885 static void
8886nv_esc(cap)
8887 cmdarg_T *cap;
8888{
8889 int no_reason;
8890
8891 no_reason = (cap->oap->op_type == OP_NOP
8892 && cap->opcount == 0
8893 && cap->count0 == 0
8894 && cap->oap->regname == 0
8895 && !p_im);
8896
8897 if (cap->arg) /* TRUE for CTRL-C */
8898 {
8899 if (restart_edit == 0
8900#ifdef FEAT_CMDWIN
8901 && cmdwin_type == 0
8902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 && no_reason)
8905 MSG(_("Type :quit<Enter> to exit Vim"));
8906
8907 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8908 * set again below when halfway a mapping. */
8909 if (!p_im)
8910 restart_edit = 0;
8911#ifdef FEAT_CMDWIN
8912 if (cmdwin_type != 0)
8913 {
8914 cmdwin_result = K_IGNORE;
8915 got_int = FALSE; /* don't stop executing autocommands et al. */
8916 return;
8917 }
8918#endif
8919 }
8920
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (VIsual_active)
8922 {
8923 end_visual_mode(); /* stop Visual */
8924 check_cursor_col(); /* make sure cursor is not beyond EOL */
8925 curwin->w_set_curswant = TRUE;
8926 redraw_curbuf_later(INVERTED);
8927 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008928 else if (no_reason)
8929 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 clearop(cap->oap);
8931
8932 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8933 * set return to Insert mode afterwards. */
8934 if (restart_edit == 0 && goto_im()
8935#ifdef FEAT_EX_EXTRA
8936 && ex_normal_busy == 0
8937#endif
8938 )
8939 restart_edit = 'a';
8940}
8941
8942/*
8943 * Handle "A", "a", "I", "i" and <Insert> commands.
8944 */
8945 static void
8946nv_edit(cap)
8947 cmdarg_T *cap;
8948{
8949 /* <Insert> is equal to "i" */
8950 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8951 cap->cmdchar = 'i';
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 /* in Visual mode "A" and "I" are an operator */
8954 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
8955 v_visop(cap);
8956
8957 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008958 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8959 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
8961#ifdef FEAT_TEXTOBJ
8962 nv_object(cap);
8963#else
8964 clearopbeep(cap->oap);
8965#endif
8966 }
8967 else if (!curbuf->b_p_ma && !p_im)
8968 {
8969 /* Only give this error when 'insertmode' is off. */
8970 EMSG(_(e_modifiable));
8971 clearop(cap->oap);
8972 }
8973 else if (!checkclearopq(cap->oap))
8974 {
8975 switch (cap->cmdchar)
8976 {
8977 case 'A': /* "A"ppend after the line */
8978 curwin->w_set_curswant = TRUE;
8979#ifdef FEAT_VIRTUALEDIT
8980 if (ve_flags == VE_ALL)
8981 {
8982 int save_State = State;
8983
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008984 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 * character past the end of the line */
8986 State = INSERT;
8987 coladvance((colnr_T)MAXCOL);
8988 State = save_State;
8989 }
8990 else
8991#endif
8992 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8993 break;
8994
8995 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008996 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
8997 beginline(BL_WHITE);
8998 else
8999 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 break;
9001
9002 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9003#ifdef FEAT_VIRTUALEDIT
9004 /* increment coladd when in virtual space, increment the
9005 * column otherwise, also to append after an unprintable char */
9006 if (virtual_active()
9007 && (curwin->w_cursor.coladd > 0
9008 || *ml_get_cursor() == NUL
9009 || *ml_get_cursor() == TAB))
9010 curwin->w_cursor.coladd++;
9011 else
9012#endif
9013 if (*ml_get_cursor() != NUL)
9014 inc_cursor();
9015 break;
9016 }
9017
9018#ifdef FEAT_VIRTUALEDIT
9019 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9020 {
9021 int save_State = State;
9022
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009023 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 * character past the end of the line */
9025 State = INSERT;
9026 coladvance(getviscol());
9027 State = save_State;
9028 }
9029#endif
9030
9031 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9032 }
9033}
9034
9035/*
9036 * Invoke edit() and take care of "restart_edit" and the return value.
9037 */
9038 static void
9039invoke_edit(cap, repl, cmd, startln)
9040 cmdarg_T *cap;
9041 int repl; /* "r" or "gr" command */
9042 int cmd;
9043 int startln;
9044{
9045 int restart_edit_save = 0;
9046
9047 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9048 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9049 * it. */
9050 if (repl || !stuff_empty())
9051 restart_edit_save = restart_edit;
9052 else
9053 restart_edit_save = 0;
9054
9055 /* Always reset "restart_edit", this is not a restarted edit. */
9056 restart_edit = 0;
9057
9058 if (edit(cmd, startln, cap->count1))
9059 cap->retval |= CA_COMMAND_BUSY;
9060
9061 if (restart_edit == 0)
9062 restart_edit = restart_edit_save;
9063}
9064
9065#ifdef FEAT_TEXTOBJ
9066/*
9067 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9068 */
9069 static void
9070nv_object(cap)
9071 cmdarg_T *cap;
9072{
9073 int flag;
9074 int include;
9075 char_u *mps_save;
9076
9077 if (cap->cmdchar == 'i')
9078 include = FALSE; /* "ix" = inner object: exclude white space */
9079 else
9080 include = TRUE; /* "ax" = an object: include white space */
9081
9082 /* Make sure (), [], {} and <> are in 'matchpairs' */
9083 mps_save = curbuf->b_p_mps;
9084 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9085
9086 switch (cap->nchar)
9087 {
9088 case 'w': /* "aw" = a word */
9089 flag = current_word(cap->oap, cap->count1, include, FALSE);
9090 break;
9091 case 'W': /* "aW" = a WORD */
9092 flag = current_word(cap->oap, cap->count1, include, TRUE);
9093 break;
9094 case 'b': /* "ab" = a braces block */
9095 case '(':
9096 case ')':
9097 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9098 break;
9099 case 'B': /* "aB" = a Brackets block */
9100 case '{':
9101 case '}':
9102 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9103 break;
9104 case '[': /* "a[" = a [] block */
9105 case ']':
9106 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9107 break;
9108 case '<': /* "a<" = a <> block */
9109 case '>':
9110 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9111 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009112 case 't': /* "at" = a tag block (xml and html) */
9113 flag = current_tagblock(cap->oap, cap->count1, include);
9114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 case 'p': /* "ap" = a paragraph */
9116 flag = current_par(cap->oap, cap->count1, include, 'p');
9117 break;
9118 case 's': /* "as" = a sentence */
9119 flag = current_sent(cap->oap, cap->count1, include);
9120 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009121 case '"': /* "a"" = a double quoted string */
9122 case '\'': /* "a'" = a single quoted string */
9123 case '`': /* "a`" = a backtick quoted string */
9124 flag = current_quote(cap->oap, cap->count1, include,
9125 cap->nchar);
9126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127#if 0 /* TODO */
9128 case 'S': /* "aS" = a section */
9129 case 'f': /* "af" = a filename */
9130 case 'u': /* "au" = a URL */
9131#endif
9132 default:
9133 flag = FAIL;
9134 break;
9135 }
9136
9137 curbuf->b_p_mps = mps_save;
9138 if (flag == FAIL)
9139 clearopbeep(cap->oap);
9140 adjust_cursor_col();
9141 curwin->w_set_curswant = TRUE;
9142}
9143#endif
9144
9145/*
9146 * "q" command: Start/stop recording.
9147 * "q:", "q/", "q?": edit command-line in command-line window.
9148 */
9149 static void
9150nv_record(cap)
9151 cmdarg_T *cap;
9152{
9153 if (cap->oap->op_type == OP_FORMAT)
9154 {
9155 /* "gqq" is the same as "gqgq": format line */
9156 cap->cmdchar = 'g';
9157 cap->nchar = 'q';
9158 nv_operator(cap);
9159 }
9160 else if (!checkclearop(cap->oap))
9161 {
9162#ifdef FEAT_CMDWIN
9163 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9164 {
9165 stuffcharReadbuff(cap->nchar);
9166 stuffcharReadbuff(K_CMDWIN);
9167 }
9168 else
9169#endif
9170 /* (stop) recording into a named register, unless executing a
9171 * register */
9172 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9173 clearopbeep(cap->oap);
9174 }
9175}
9176
9177/*
9178 * Handle the "@r" command.
9179 */
9180 static void
9181nv_at(cap)
9182 cmdarg_T *cap;
9183{
9184 if (checkclearop(cap->oap))
9185 return;
9186#ifdef FEAT_EVAL
9187 if (cap->nchar == '=')
9188 {
9189 if (get_expr_register() == NUL)
9190 return;
9191 }
9192#endif
9193 while (cap->count1-- && !got_int)
9194 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009195 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
9197 clearopbeep(cap->oap);
9198 break;
9199 }
9200 line_breakcheck();
9201 }
9202}
9203
9204/*
9205 * Handle the CTRL-U and CTRL-D commands.
9206 */
9207 static void
9208nv_halfpage(cap)
9209 cmdarg_T *cap;
9210{
9211 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9212 || (cap->cmdchar == Ctrl_D
9213 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9214 clearopbeep(cap->oap);
9215 else if (!checkclearop(cap->oap))
9216 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9217}
9218
9219/*
9220 * Handle "J" or "gJ" command.
9221 */
9222 static void
9223nv_join(cap)
9224 cmdarg_T *cap;
9225{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 if (VIsual_active) /* join the visual lines */
9227 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009228 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 {
9230 if (cap->count0 <= 1)
9231 cap->count0 = 2; /* default for join is two lines! */
9232 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9233 curbuf->b_ml.ml_line_count)
9234 clearopbeep(cap->oap); /* beyond last line */
9235 else
9236 {
9237 prep_redo(cap->oap->regname, cap->count0,
9238 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009239 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
9241 }
9242}
9243
9244/*
9245 * "P", "gP", "p" and "gp" commands.
9246 */
9247 static void
9248nv_put(cap)
9249 cmdarg_T *cap;
9250{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 int regname = 0;
9252 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009253 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009254 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 int dir;
9256 int flags = 0;
9257
9258 if (cap->oap->op_type != OP_NOP)
9259 {
9260#ifdef FEAT_DIFF
9261 /* "dp" is ":diffput" */
9262 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9263 {
9264 clearop(cap->oap);
9265 nv_diffgetput(TRUE);
9266 }
9267 else
9268#endif
9269 clearopbeep(cap->oap);
9270 }
9271 else
9272 {
9273 dir = (cap->cmdchar == 'P'
9274 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9275 ? BACKWARD : FORWARD;
9276 prep_redo_cmd(cap);
9277 if (cap->cmdchar == 'g')
9278 flags |= PUT_CURSEND;
9279
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 if (VIsual_active)
9281 {
9282 /* Putting in Visual mode: The put text replaces the selected
9283 * text. First delete the selected text, then put the new text.
9284 * Need to save and restore the registers that the delete
9285 * overwrites if the old contents is being put.
9286 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009287 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009289#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009291#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009292 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009293 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009294#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297
9298 )
9299 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009300 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 * put, save it first. */
9302 reg1 = get_register(regname, TRUE);
9303 }
9304
9305 /* Now delete the selected text. */
9306 cap->cmdchar = 'd';
9307 cap->nchar = NUL;
9308 cap->oap->regname = NUL;
9309 nv_operator(cap);
9310 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009311 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
9313 /* delete PUT_LINE_BACKWARD; */
9314 cap->oap->regname = regname;
9315
9316 if (reg1 != NULL)
9317 {
9318 /* Delete probably changed the register we want to put, save
9319 * it first. Then put back what was there before the delete. */
9320 reg2 = get_register(regname, FALSE);
9321 put_register(regname, reg1);
9322 }
9323
9324 /* When deleted a linewise Visual area, put the register as
9325 * lines to avoid it joined with the next line. When deletion was
9326 * characterwise, split a line when putting lines. */
9327 if (VIsual_mode == 'V')
9328 flags |= PUT_LINE;
9329 else if (VIsual_mode == 'v')
9330 flags |= PUT_LINE_SPLIT;
9331 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9332 flags |= PUT_LINE_FORWARD;
9333 dir = BACKWARD;
9334 if ((VIsual_mode != 'V'
9335 && curwin->w_cursor.col < curbuf->b_op_start.col)
9336 || (VIsual_mode == 'V'
9337 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9338 /* cursor is at the end of the line or end of file, put
9339 * forward. */
9340 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009341 /* May have been reset in do_put(). */
9342 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 do_put(cap->oap->regname, dir, cap->count1, flags);
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 /* If a register was saved, put it back now. */
9347 if (reg2 != NULL)
9348 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009349
9350 /* What to reselect with "gv"? Selecting the just put text seems to
9351 * be the most useful, since the original text was removed. */
9352 if (was_visual)
9353 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009354 curbuf->b_visual.vi_start = curbuf->b_op_start;
9355 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009356 }
9357
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009358 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009359 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009360 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009361 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009362 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009363
9364 /* If the cursor was in that line, move it to the end of the last
9365 * line. */
9366 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9367 {
9368 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9369 coladvance((colnr_T)MAXCOL);
9370 }
9371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 auto_format(FALSE, TRUE);
9373 }
9374}
9375
9376/*
9377 * "o" and "O" commands.
9378 */
9379 static void
9380nv_open(cap)
9381 cmdarg_T *cap;
9382{
9383#ifdef FEAT_DIFF
9384 /* "do" is ":diffget" */
9385 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9386 {
9387 clearop(cap->oap);
9388 nv_diffgetput(FALSE);
9389 }
9390 else
9391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 if (VIsual_active) /* switch start and end of visual */
9393 v_swap_corners(cap->cmdchar);
9394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 n_opencmd(cap);
9396}
9397
9398#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 static void
9400nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009401 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
9403 ProcessSniffRequests();
9404}
9405#endif
9406
9407#ifdef FEAT_NETBEANS_INTG
9408 static void
9409nv_nbcmd(cap)
9410 cmdarg_T *cap;
9411{
9412 netbeans_keycommand(cap->nchar);
9413}
9414#endif
9415
9416#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 static void
9418nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009419 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9422}
9423#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009424
9425#ifdef FEAT_AUTOCMD
9426/*
9427 * Trigger CursorHold event.
9428 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9429 * input buffer. "did_cursorhold" is set to avoid retriggering.
9430 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009431 static void
9432nv_cursorhold(cap)
9433 cmdarg_T *cap;
9434{
9435 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9436 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009437 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009438}
9439#endif