blob: c29ab1a48c758df3ef542ee928afa401d0f4a65f [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));
Bram Moolenaar46539112015-02-17 15:43:57 +0100103static int normal_search __ARGS((cmdarg_T *cap, int dir, char_u *pat, int opt));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104static void nv_csearch __ARGS((cmdarg_T *cap));
105static void nv_brackets __ARGS((cmdarg_T *cap));
106static void nv_percent __ARGS((cmdarg_T *cap));
107static void nv_brace __ARGS((cmdarg_T *cap));
108static void nv_mark __ARGS((cmdarg_T *cap));
109static void nv_findpar __ARGS((cmdarg_T *cap));
110static void nv_undo __ARGS((cmdarg_T *cap));
111static void nv_kundo __ARGS((cmdarg_T *cap));
112static void nv_Replace __ARGS((cmdarg_T *cap));
113#ifdef FEAT_VREPLACE
114static void nv_vreplace __ARGS((cmdarg_T *cap));
115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116static void v_swap_corners __ARGS((int cmdchar));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117static void nv_replace __ARGS((cmdarg_T *cap));
118static void n_swapchar __ARGS((cmdarg_T *cap));
119static void nv_cursormark __ARGS((cmdarg_T *cap, int flag, pos_T *pos));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120static void v_visop __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121static void nv_subst __ARGS((cmdarg_T *cap));
122static void nv_abbrev __ARGS((cmdarg_T *cap));
123static void nv_optrans __ARGS((cmdarg_T *cap));
124static void nv_gomark __ARGS((cmdarg_T *cap));
125static void nv_pcmark __ARGS((cmdarg_T *cap));
126static void nv_regname __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127static void nv_visual __ARGS((cmdarg_T *cap));
128static void n_start_visual_mode __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129static void nv_window __ARGS((cmdarg_T *cap));
130static void nv_suspend __ARGS((cmdarg_T *cap));
131static void nv_g_cmd __ARGS((cmdarg_T *cap));
132static void n_opencmd __ARGS((cmdarg_T *cap));
133static void nv_dot __ARGS((cmdarg_T *cap));
134static void nv_redo __ARGS((cmdarg_T *cap));
135static void nv_Undo __ARGS((cmdarg_T *cap));
136static void nv_tilde __ARGS((cmdarg_T *cap));
137static void nv_operator __ARGS((cmdarg_T *cap));
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000138#ifdef FEAT_EVAL
139static void set_op_var __ARGS((int optype));
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static void nv_lineop __ARGS((cmdarg_T *cap));
142static void nv_home __ARGS((cmdarg_T *cap));
143static void nv_pipe __ARGS((cmdarg_T *cap));
144static void nv_bck_word __ARGS((cmdarg_T *cap));
145static void nv_wordcmd __ARGS((cmdarg_T *cap));
146static void nv_beginline __ARGS((cmdarg_T *cap));
Bram Moolenaar1f14d572008-01-12 16:12:10 +0000147static void adjust_cursor __ARGS((oparg_T *oap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148static void adjust_for_sel __ARGS((cmdarg_T *cap));
149static int unadjust_for_sel __ARGS((void));
150static void nv_select __ARGS((cmdarg_T *cap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151static void nv_goto __ARGS((cmdarg_T *cap));
152static void nv_normal __ARGS((cmdarg_T *cap));
153static void nv_esc __ARGS((cmdarg_T *oap));
154static void nv_edit __ARGS((cmdarg_T *cap));
155static void invoke_edit __ARGS((cmdarg_T *cap, int repl, int cmd, int startln));
156#ifdef FEAT_TEXTOBJ
157static void nv_object __ARGS((cmdarg_T *cap));
158#endif
159static void nv_record __ARGS((cmdarg_T *cap));
160static void nv_at __ARGS((cmdarg_T *cap));
161static void nv_halfpage __ARGS((cmdarg_T *cap));
162static void nv_join __ARGS((cmdarg_T *cap));
163static void nv_put __ARGS((cmdarg_T *cap));
164static void nv_open __ARGS((cmdarg_T *cap));
165#ifdef FEAT_SNIFF
166static void nv_sniff __ARGS((cmdarg_T *cap));
167#endif
168#ifdef FEAT_NETBEANS_INTG
169static void nv_nbcmd __ARGS((cmdarg_T *cap));
170#endif
171#ifdef FEAT_DND
172static void nv_drop __ARGS((cmdarg_T *cap));
173#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#ifdef FEAT_AUTOCMD
175static void nv_cursorhold __ARGS((cmdarg_T *cap));
176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000178static char *e_noident = N_("E349: No identifier under cursor");
179
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180/*
181 * Function to be called for a Normal or Visual mode command.
182 * The argument is a cmdarg_T.
183 */
184typedef void (*nv_func_T) __ARGS((cmdarg_T *cap));
185
186/* Values for cmd_flags. */
187#define NV_NCH 0x01 /* may need to get a second char */
188#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
189#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
190#define NV_LANG 0x08 /* second char needs language adjustment */
191
192#define NV_SS 0x10 /* may start selection */
193#define NV_SSS 0x20 /* may start selection with shift modifier */
194#define NV_STS 0x40 /* may stop selection without shift modif. */
195#define NV_RL 0x80 /* 'rightleft' modifies command */
196#define NV_KEEPREG 0x100 /* don't clear regname */
197#define NV_NCW 0x200 /* not allowed in command-line window */
198
199/*
200 * Generally speaking, every Normal mode command should either clear any
201 * pending operator (with *clearop*()), or set the motion type variable
202 * oap->motion_type.
203 *
204 * When a cursor motion command is made, it is marked as being a character or
205 * line oriented motion. Then, if an operator is in effect, the operation
206 * becomes character or line oriented accordingly.
207 */
208
209/*
210 * This table contains one entry for every Normal or Visual mode command.
211 * The order doesn't matter, init_normal_cmds() will create a sorted index.
212 * It is faster when all keys from zero to '~' are present.
213 */
214static const struct nv_cmd
215{
216 int cmd_char; /* (first) command character */
217 nv_func_T cmd_func; /* function for this command */
218 short_u cmd_flags; /* NV_ flags */
219 short cmd_arg; /* value for ca.arg */
220} nv_cmds[] =
221{
222 {NUL, nv_error, 0, 0},
223 {Ctrl_A, nv_addsub, 0, 0},
224 {Ctrl_B, nv_page, NV_STS, BACKWARD},
225 {Ctrl_C, nv_esc, 0, TRUE},
226 {Ctrl_D, nv_halfpage, 0, 0},
227 {Ctrl_E, nv_scroll_line, 0, TRUE},
228 {Ctrl_F, nv_page, NV_STS, FORWARD},
229 {Ctrl_G, nv_ctrlg, 0, 0},
230 {Ctrl_H, nv_ctrlh, 0, 0},
231 {Ctrl_I, nv_pcmark, 0, 0},
232 {NL, nv_down, 0, FALSE},
233 {Ctrl_K, nv_error, 0, 0},
234 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000235 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {Ctrl_N, nv_down, NV_STS, FALSE},
237 {Ctrl_O, nv_ctrlo, 0, 0},
238 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000239 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 {Ctrl_R, nv_redo, 0, 0},
241 {Ctrl_S, nv_ignore, 0, 0},
242 {Ctrl_T, nv_tagpop, NV_NCW, 0},
243 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_V, nv_visual, 0, FALSE},
245 {'V', nv_visual, 0, FALSE},
246 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 {Ctrl_W, nv_window, 0, 0},
248 {Ctrl_X, nv_addsub, 0, 0},
249 {Ctrl_Y, nv_scroll_line, 0, FALSE},
250 {Ctrl_Z, nv_suspend, 0, 0},
251 {ESC, nv_esc, 0, FALSE},
252 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
253 {Ctrl_RSB, nv_ident, NV_NCW, 0},
254 {Ctrl_HAT, nv_hat, NV_NCW, 0},
255 {Ctrl__, nv_error, 0, 0},
256 {' ', nv_right, 0, 0},
257 {'!', nv_operator, 0, 0},
258 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
259 {'#', nv_ident, 0, 0},
260 {'$', nv_dollar, 0, 0},
261 {'%', nv_percent, 0, 0},
262 {'&', nv_optrans, 0, 0},
263 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
264 {'(', nv_brace, 0, BACKWARD},
265 {')', nv_brace, 0, FORWARD},
266 {'*', nv_ident, 0, 0},
267 {'+', nv_down, 0, TRUE},
268 {',', nv_csearch, 0, TRUE},
269 {'-', nv_up, 0, TRUE},
270 {'.', nv_dot, NV_KEEPREG, 0},
271 {'/', nv_search, 0, FALSE},
272 {'0', nv_beginline, 0, 0},
273 {'1', nv_ignore, 0, 0},
274 {'2', nv_ignore, 0, 0},
275 {'3', nv_ignore, 0, 0},
276 {'4', nv_ignore, 0, 0},
277 {'5', nv_ignore, 0, 0},
278 {'6', nv_ignore, 0, 0},
279 {'7', nv_ignore, 0, 0},
280 {'8', nv_ignore, 0, 0},
281 {'9', nv_ignore, 0, 0},
282 {':', nv_colon, 0, 0},
283 {';', nv_csearch, 0, FALSE},
284 {'<', nv_operator, NV_RL, 0},
285 {'=', nv_operator, 0, 0},
286 {'>', nv_operator, NV_RL, 0},
287 {'?', nv_search, 0, FALSE},
288 {'@', nv_at, NV_NCH_NOP, FALSE},
289 {'A', nv_edit, 0, 0},
290 {'B', nv_bck_word, 0, 1},
291 {'C', nv_abbrev, NV_KEEPREG, 0},
292 {'D', nv_abbrev, NV_KEEPREG, 0},
293 {'E', nv_wordcmd, 0, TRUE},
294 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
295 {'G', nv_goto, 0, TRUE},
296 {'H', nv_scroll, 0, 0},
297 {'I', nv_edit, 0, 0},
298 {'J', nv_join, 0, 0},
299 {'K', nv_ident, 0, 0},
300 {'L', nv_scroll, 0, 0},
301 {'M', nv_scroll, 0, 0},
302 {'N', nv_next, 0, SEARCH_REV},
303 {'O', nv_open, 0, 0},
304 {'P', nv_put, 0, 0},
305 {'Q', nv_exmode, NV_NCW, 0},
306 {'R', nv_Replace, 0, FALSE},
307 {'S', nv_subst, NV_KEEPREG, 0},
308 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
309 {'U', nv_Undo, 0, 0},
310 {'W', nv_wordcmd, 0, TRUE},
311 {'X', nv_abbrev, NV_KEEPREG, 0},
312 {'Y', nv_abbrev, NV_KEEPREG, 0},
313 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
314 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
315 {'\\', nv_error, 0, 0},
316 {']', nv_brackets, NV_NCH_ALW, FORWARD},
317 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
318 {'_', nv_lineop, 0, 0},
319 {'`', nv_gomark, NV_NCH_ALW, FALSE},
320 {'a', nv_edit, NV_NCH, 0},
321 {'b', nv_bck_word, 0, 0},
322 {'c', nv_operator, 0, 0},
323 {'d', nv_operator, 0, 0},
324 {'e', nv_wordcmd, 0, FALSE},
325 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
326 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
327 {'h', nv_left, NV_RL, 0},
328 {'i', nv_edit, NV_NCH, 0},
329 {'j', nv_down, 0, FALSE},
330 {'k', nv_up, 0, FALSE},
331 {'l', nv_right, NV_RL, 0},
332 {'m', nv_mark, NV_NCH_NOP, 0},
333 {'n', nv_next, 0, 0},
334 {'o', nv_open, 0, 0},
335 {'p', nv_put, 0, 0},
336 {'q', nv_record, NV_NCH, 0},
337 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
338 {'s', nv_subst, NV_KEEPREG, 0},
339 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
340 {'u', nv_undo, 0, 0},
341 {'w', nv_wordcmd, 0, FALSE},
342 {'x', nv_abbrev, NV_KEEPREG, 0},
343 {'y', nv_operator, 0, 0},
344 {'z', nv_zet, NV_NCH_ALW, 0},
345 {'{', nv_findpar, 0, BACKWARD},
346 {'|', nv_pipe, 0, 0},
347 {'}', nv_findpar, 0, FORWARD},
348 {'~', nv_tilde, 0, 0},
349
350 /* pound sign */
351 {POUND, nv_ident, 0, 0},
352#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200353 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
354 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
355 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
356 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {K_LEFTMOUSE, nv_mouse, 0, 0},
358 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
359 {K_LEFTDRAG, nv_mouse, 0, 0},
360 {K_LEFTRELEASE, nv_mouse, 0, 0},
361 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
418 {K_F8, farsi_fkey, 0, 0},
419 {K_F9, farsi_fkey, 0, 0},
420#endif
421#ifdef FEAT_SNIFF
422 {K_SNIFF, nv_sniff, 0, 0},
423#endif
424#ifdef FEAT_NETBEANS_INTG
425 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
426#endif
427#ifdef FEAT_DND
428 {K_DROP, nv_drop, NV_STS, 0},
429#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000430#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000431 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433};
434
435/* Number of commands in nv_cmds[]. */
436#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
437
438/* Sorted index of commands in nv_cmds[]. */
439static short nv_cmd_idx[NV_CMDS_SIZE];
440
441/* The highest index for which
442 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
443static int nv_max_linear;
444
445/*
446 * Compare functions for qsort() below, that checks the command character
447 * through the index in nv_cmd_idx[].
448 */
449 static int
450#ifdef __BORLANDC__
451_RTLENTRYF
452#endif
453nv_compare(s1, s2)
454 const void *s1;
455 const void *s2;
456{
457 int c1, c2;
458
459 /* The commands are sorted on absolute value. */
460 c1 = nv_cmds[*(const short *)s1].cmd_char;
461 c2 = nv_cmds[*(const short *)s2].cmd_char;
462 if (c1 < 0)
463 c1 = -c1;
464 if (c2 < 0)
465 c2 = -c2;
466 return c1 - c2;
467}
468
469/*
470 * Initialize the nv_cmd_idx[] table.
471 */
472 void
473init_normal_cmds()
474{
475 int i;
476
477 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000478 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 nv_cmd_idx[i] = i;
480
481 /* Sort the commands by the command character. */
482 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
483
484 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000485 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
487 break;
488 nv_max_linear = i - 1;
489}
490
491/*
492 * Search for a command in the commands table.
493 * Returns -1 for invalid command.
494 */
495 static int
496find_command(cmdchar)
497 int cmdchar;
498{
499 int i;
500 int idx;
501 int top, bot;
502 int c;
503
504#ifdef FEAT_MBYTE
505 /* A multi-byte character is never a command. */
506 if (cmdchar >= 0x100)
507 return -1;
508#endif
509
510 /* We use the absolute value of the character. Special keys have a
511 * negative value, but are sorted on their absolute value. */
512 if (cmdchar < 0)
513 cmdchar = -cmdchar;
514
515 /* If the character is in the first part: The character is the index into
516 * nv_cmd_idx[]. */
517 if (cmdchar <= nv_max_linear)
518 return nv_cmd_idx[cmdchar];
519
520 /* Perform a binary search. */
521 bot = nv_max_linear + 1;
522 top = NV_CMDS_SIZE - 1;
523 idx = -1;
524 while (bot <= top)
525 {
526 i = (top + bot) / 2;
527 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
528 if (c < 0)
529 c = -c;
530 if (cmdchar == c)
531 {
532 idx = nv_cmd_idx[i];
533 break;
534 }
535 if (cmdchar > c)
536 bot = i + 1;
537 else
538 top = i - 1;
539 }
540 return idx;
541}
542
543/*
544 * Execute a command in Normal mode.
545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 void
547normal_cmd(oap, toplevel)
548 oparg_T *oap;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000549 int toplevel UNUSED; /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 cmdarg_T ca; /* command arguments */
552 int c;
553 int ctrl_w = FALSE; /* got CTRL-W command */
554 int old_col = curwin->w_curswant;
555#ifdef FEAT_CMDL_INFO
556 int need_flushbuf; /* need to call out_flush() */
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 pos_T old_pos; /* cursor position before command */
559 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 static int old_mapped_len = 0;
561 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000562#ifdef FEAT_EVAL
563 int set_prevcount = FALSE;
564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
567 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000568
569 /* Use a count remembered from before entering an operator. After typing
570 * "3d" we return from normal_cmd() and come back here, the "3" is
571 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ca.opcount = opcount;
573
574#ifdef FEAT_SNIFF
575 want_sniff_request = sniff_connected;
576#endif
577
578 /*
579 * If there is an operator pending, then the command we take this time
580 * will terminate it. Finish_op tells us to finish the operation before
581 * returning this time (unless the operation was cancelled).
582 */
583#ifdef CURSOR_SHAPE
584 c = finish_op;
585#endif
586 finish_op = (oap->op_type != OP_NOP);
587#ifdef CURSOR_SHAPE
588 if (finish_op != c)
589 {
590 ui_cursor_shape(); /* may show different cursor shape */
591# ifdef FEAT_MOUSESHAPE
592 update_mouseshape(-1);
593# endif
594 }
595#endif
596
Bram Moolenaara983fe92008-07-31 20:04:27 +0000597 /* When not finishing an operator and no register name typed, reset the
598 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000602#ifdef FEAT_EVAL
603 set_prevcount = TRUE;
604#endif
605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaara983fe92008-07-31 20:04:27 +0000607#ifdef FEAT_AUTOCMD
608 /* Restore counts from before receiving K_CURSORHOLD. This means after
609 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
610 * "3 * 2". */
611 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
612 {
613 ca.opcount = oap->prev_opcount;
614 ca.count0 = oap->prev_count0;
615 oap->prev_opcount = 0;
616 oap->prev_count0 = 0;
617 }
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 State = NORMAL_BUSY;
623#ifdef USE_ON_FLY_SCROLL
624 dont_scroll = FALSE; /* allow scrolling here */
625#endif
626
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100627#ifdef FEAT_EVAL
628 /* Set v:count here, when called from main() and not a stuffed
629 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100630 * when there is no count. Do set it for redo. */
631 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100632 set_vcount_ca(&ca, &set_prevcount);
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 /*
636 * Get the command character from the user.
637 */
638 c = safe_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 /*
642 * If a mapping was started in Visual or Select mode, remember the length
643 * of the mapping. This is used below to not return to Insert mode for as
644 * long as the mapping is being executed.
645 */
646 if (restart_edit == 0)
647 old_mapped_len = 0;
648 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 old_mapped_len = typebuf_maplen();
651
652 if (c == NUL)
653 c = K_ZERO;
654
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 /*
656 * In Select mode, typed text replaces the selection.
657 */
658 if (VIsual_active
659 && VIsual_select
660 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
661 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000662 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
663 * 'insertmode' is set) fake a "d"elete command, Insert mode will
664 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000665 * Insert the typed character in the typeahead buffer, so that it can
666 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000667 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000668 if (restart_edit != 0)
669 c = 'd';
670 else
671 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000672 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200673 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676#ifdef FEAT_CMDL_INFO
677 need_flushbuf = add_to_showcmd(c);
678#endif
679
680getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
683 /*
684 * Handle a count before a command and compute ca.count0.
685 * Note that '0' is a command and not the start of a count, but it's
686 * part of a count after other digits.
687 */
688 while ( (c >= '1' && c <= '9')
689 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
690 {
691 if (c == K_DEL || c == K_KDEL)
692 {
693 ca.count0 /= 10;
694#ifdef FEAT_CMDL_INFO
695 del_from_showcmd(4); /* delete the digit and ~@% */
696#endif
697 }
698 else
699 ca.count0 = ca.count0 * 10 + (c - '0');
700 if (ca.count0 < 0) /* got too large! */
701 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000702#ifdef FEAT_EVAL
703 /* Set v:count here, when called from main() and not a stuffed
704 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100705 * right after the count. Do set it for redo. */
706 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100707 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (ctrl_w)
710 {
711 ++no_mapping;
712 ++allow_keys; /* no mapping for nchar, but keys */
713 }
714 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000715 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 --no_zero_mapping;
718 if (ctrl_w)
719 {
720 --no_mapping;
721 --allow_keys;
722 }
723#ifdef FEAT_CMDL_INFO
724 need_flushbuf |= add_to_showcmd(c);
725#endif
726 }
727
728 /*
729 * If we got CTRL-W there may be a/another count
730 */
731 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
732 {
733 ctrl_w = TRUE;
734 ca.opcount = ca.count0; /* remember first count */
735 ca.count0 = 0;
736 ++no_mapping;
737 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000738 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 --no_mapping;
741 --allow_keys;
742#ifdef FEAT_CMDL_INFO
743 need_flushbuf |= add_to_showcmd(c);
744#endif
745 goto getcount; /* jump back */
746 }
747 }
748
Bram Moolenaara983fe92008-07-31 20:04:27 +0000749#ifdef FEAT_AUTOCMD
750 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000752 /* Save the count values so that ca.opcount and ca.count0 are exactly
753 * the same when coming back here after handling K_CURSORHOLD. */
754 oap->prev_opcount = ca.opcount;
755 oap->prev_count0 = ca.count0;
756 }
757 else
758#endif
759 if (ca.opcount != 0)
760 {
761 /*
762 * If we're in the middle of an operator (including after entering a
763 * yank buffer with '"') AND we had a count before the operator, then
764 * that count overrides the current value of ca.count0.
765 * What this means effectively, is that commands like "3dw" get turned
766 * into "d3w" which makes things fall into place pretty neatly.
767 * If you give a count before AND after the operator, they are
768 * multiplied.
769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (ca.count0)
771 ca.count0 *= ca.opcount;
772 else
773 ca.count0 = ca.opcount;
774 }
775
776 /*
777 * Always remember the count. It will be set to zero (on the next call,
778 * above) when there is no pending operator.
779 * When called from main(), save the count for use by the "count" built-in
780 * variable.
781 */
782 ca.opcount = ca.count0;
783 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
784
785#ifdef FEAT_EVAL
786 /*
787 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100788 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100790 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000791 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#endif
793
794 /*
795 * Find the command character in the table of commands.
796 * For CTRL-W we already got nchar when looking for a count.
797 */
798 if (ctrl_w)
799 {
800 ca.nchar = c;
801 ca.cmdchar = Ctrl_W;
802 }
803 else
804 ca.cmdchar = c;
805 idx = find_command(ca.cmdchar);
806 if (idx < 0)
807 {
808 /* Not a known command: beep. */
809 clearopbeep(oap);
810 goto normal_end;
811 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000812
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000813 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200815 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000817 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 goto normal_end;
819 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000820#ifdef FEAT_AUTOCMD
821 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
822 goto normal_end;
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /*
826 * In Visual/Select mode, a few keys are handled in a special way.
827 */
828 if (VIsual_active)
829 {
830 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
831 if (km_stopsel
832 && (nv_cmds[idx].cmd_flags & NV_STS)
833 && !(mod_mask & MOD_MASK_SHIFT))
834 {
835 end_visual_mode();
836 redraw_curbuf_later(INVERTED);
837 }
838
839 /* Keys that work different when 'keymodel' contains "startsel" */
840 if (km_startsel)
841 {
842 if (nv_cmds[idx].cmd_flags & NV_SS)
843 {
844 unshift_special(&ca);
845 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000846 if (idx < 0)
847 {
848 /* Just in case */
849 clearopbeep(oap);
850 goto normal_end;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
854 && (mod_mask & MOD_MASK_SHIFT))
855 {
856 mod_mask &= ~MOD_MASK_SHIFT;
857 }
858 }
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
863 && (nv_cmds[idx].cmd_flags & NV_RL))
864 {
865 /* Invert horizontal movements and operations. Only when typed by the
866 * user directly, not when the result of a mapping or "x" translated
867 * to "dl". */
868 switch (ca.cmdchar)
869 {
870 case 'l': ca.cmdchar = 'h'; break;
871 case K_RIGHT: ca.cmdchar = K_LEFT; break;
872 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
873 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
874 case 'h': ca.cmdchar = 'l'; break;
875 case K_LEFT: ca.cmdchar = K_RIGHT; break;
876 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
877 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
878 case '>': ca.cmdchar = '<'; break;
879 case '<': ca.cmdchar = '>'; break;
880 }
881 idx = find_command(ca.cmdchar);
882 }
883#endif
884
885 /*
886 * Get an additional character if we need one.
887 */
888 if ((nv_cmds[idx].cmd_flags & NV_NCH)
889 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
890 && oap->op_type == OP_NOP)
891 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
892 || (ca.cmdchar == 'q'
893 && oap->op_type == OP_NOP
894 && !Recording
895 && !Exec_reg)
896 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100897 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 int *cp;
900 int repl = FALSE; /* get character for replace mode */
901 int lit = FALSE; /* get extra character literally */
902 int langmap_active = FALSE; /* using :lmap mappings */
903 int lang; /* getting a text character */
904#ifdef USE_IM_CONTROL
905 int save_smd; /* saved value of p_smd */
906#endif
907
908 ++no_mapping;
909 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000910#ifdef FEAT_AUTOCMD
911 /* Don't generate a CursorHold event here, most commands can't handle
912 * it, e.g., nv_replace(), nv_csearch(). */
913 did_cursorhold = TRUE;
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (ca.cmdchar == 'g')
916 {
917 /*
918 * For 'g' get the next character now, so that we can check for
919 * "gr", "g'" and "g`".
920 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000921 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_CMDL_INFO
924 need_flushbuf |= add_to_showcmd(ca.nchar);
925#endif
926 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100927 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 cp = &ca.extra_char; /* need to get a third character */
930 if (ca.nchar != 'r')
931 lit = TRUE; /* get it literally */
932 else
933 repl = TRUE; /* get it in replace mode */
934 }
935 else
936 cp = NULL; /* no third character needed */
937 }
938 else
939 {
940 if (ca.cmdchar == 'r') /* get it in replace mode */
941 repl = TRUE;
942 cp = &ca.nchar;
943 }
944 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
945
946 /*
947 * Get a second or third character.
948 */
949 if (cp != NULL)
950 {
951#ifdef CURSOR_SHAPE
952 if (repl)
953 {
954 State = REPLACE; /* pretend Replace mode */
955 ui_cursor_shape(); /* show different cursor shape */
956 }
957#endif
958 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
959 {
960 /* Allow mappings defined with ":lmap". */
961 --no_mapping;
962 --allow_keys;
963 if (repl)
964 State = LREPLACE;
965 else
966 State = LANGMAP;
967 langmap_active = TRUE;
968 }
969#ifdef USE_IM_CONTROL
970 save_smd = p_smd;
971 p_smd = FALSE; /* Don't let the IM code show the mode here */
972 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
973 im_set_active(TRUE);
974#endif
975
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000976 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 if (langmap_active)
979 {
980 /* Undo the decrement done above */
981 ++no_mapping;
982 ++allow_keys;
983 State = NORMAL_BUSY;
984 }
985#ifdef USE_IM_CONTROL
986 if (lang)
987 {
988 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
989 im_save_status(&curbuf->b_p_iminsert);
990 im_set_active(FALSE);
991 }
992 p_smd = save_smd;
993#endif
994#ifdef CURSOR_SHAPE
995 State = NORMAL_BUSY;
996#endif
997#ifdef FEAT_CMDL_INFO
998 need_flushbuf |= add_to_showcmd(*cp);
999#endif
1000
1001 if (!lit)
1002 {
1003#ifdef FEAT_DIGRAPHS
1004 /* Typing CTRL-K gets a digraph. */
1005 if (*cp == Ctrl_K
1006 && ((nv_cmds[idx].cmd_flags & NV_LANG)
1007 || cp == &ca.extra_char)
1008 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1009 {
1010 c = get_digraph(FALSE);
1011 if (c > 0)
1012 {
1013 *cp = c;
1014# ifdef FEAT_CMDL_INFO
1015 /* Guessing how to update showcmd here... */
1016 del_from_showcmd(3);
1017 need_flushbuf |= add_to_showcmd(*cp);
1018# endif
1019 }
1020 }
1021#endif
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /* adjust chars > 127, except after "tTfFr" commands */
1024 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_RIGHTLEFT
1026 /* adjust Hebrew mapped char */
1027 if (p_hkmap && lang && KeyTyped)
1028 *cp = hkmap(*cp);
1029# ifdef FEAT_FKMAP
1030 /* adjust Farsi mapped char */
1031 if (p_fkmap && lang && KeyTyped)
1032 *cp = fkmap(*cp);
1033# endif
1034#endif
1035 }
1036
1037 /*
1038 * When the next character is CTRL-\ a following CTRL-N means the
1039 * command is aborted and we go to Normal mode.
1040 */
1041 if (cp == &ca.extra_char
1042 && ca.nchar == Ctrl_BSL
1043 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1044 {
1045 ca.cmdchar = Ctrl_BSL;
1046 ca.nchar = ca.extra_char;
1047 idx = find_command(ca.cmdchar);
1048 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001049 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001050 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 else if (*cp == Ctrl_BSL)
1052 {
1053 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1054
1055 /* There is a busy wait here when typing "f<C-\>" and then
1056 * something different from CTRL-N. Can't be avoided. */
1057 while ((c = vpeekc()) <= 0 && towait > 0L)
1058 {
1059 do_sleep(towait > 50L ? 50L : towait);
1060 towait -= 50L;
1061 }
1062 if (c > 0)
1063 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001064 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (c != Ctrl_N && c != Ctrl_G)
1066 vungetc(c);
1067 else
1068 {
1069 ca.cmdchar = Ctrl_BSL;
1070 ca.nchar = c;
1071 idx = find_command(ca.cmdchar);
1072 }
1073 }
1074 }
1075
1076#ifdef FEAT_MBYTE
1077 /* When getting a text character and the next character is a
1078 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001079 * However, don't wait for it to arrive. Also, do enable mapping,
1080 * because if it's put back with vungetc() it's too late to apply
1081 * mapping. */
1082 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 while (enc_utf8 && lang && (c = vpeekc()) > 0
1084 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1085 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001086 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (!utf_iscomposing(c))
1088 {
1089 vungetc(c); /* it wasn't, put it back */
1090 break;
1091 }
1092 else if (ca.ncharC1 == 0)
1093 ca.ncharC1 = c;
1094 else
1095 ca.ncharC2 = c;
1096 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001097 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098#endif
1099 }
1100 --no_mapping;
1101 --allow_keys;
1102 }
1103
1104#ifdef FEAT_CMDL_INFO
1105 /*
1106 * Flush the showcmd characters onto the screen so we can see them while
1107 * the command is being executed. Only do this when the shown command was
1108 * actually displayed, otherwise this will slow down a lot when executing
1109 * mappings.
1110 */
1111 if (need_flushbuf)
1112 out_flush();
1113#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001114#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001115 if (ca.cmdchar != K_IGNORE)
1116 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 State = NORMAL;
1120
1121 if (ca.nchar == ESC)
1122 {
1123 clearop(oap);
1124 if (restart_edit == 0 && goto_im())
1125 restart_edit = 'a';
1126 goto normal_end;
1127 }
1128
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001129 if (ca.cmdchar != K_IGNORE)
1130 {
1131 msg_didout = FALSE; /* don't scroll screen up for normal command */
1132 msg_col = 0;
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 old_pos = curwin->w_cursor; /* remember where cursor was */
1136
1137 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1138 * mode. */
1139 if (!VIsual_active && km_startsel)
1140 {
1141 if (nv_cmds[idx].cmd_flags & NV_SS)
1142 {
1143 start_selection();
1144 unshift_special(&ca);
1145 idx = find_command(ca.cmdchar);
1146 }
1147 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1148 && (mod_mask & MOD_MASK_SHIFT))
1149 {
1150 start_selection();
1151 mod_mask &= ~MOD_MASK_SHIFT;
1152 }
1153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 /*
1156 * Execute the command!
1157 * Call the command function found in the commands table.
1158 */
1159 ca.arg = nv_cmds[idx].cmd_arg;
1160 (nv_cmds[idx].cmd_func)(&ca);
1161
1162 /*
1163 * If we didn't start or finish an operator, reset oap->regname, unless we
1164 * need it later.
1165 */
1166 if (!finish_op
1167 && !oap->op_type
1168 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1169 {
1170 clearop(oap);
1171#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001172 {
1173 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001174
Bram Moolenaar536681b2011-05-10 16:12:45 +02001175 /* Adjust the register according to 'clipboard', so that when
1176 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001177# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001178 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001179# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001180 set_reg_var(regname);
1181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#endif
1183 }
1184
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001185 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001186 * character or "z333<cr>". */
1187 if (old_mapped_len > 0)
1188 old_mapped_len = typebuf_maplen();
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 /*
1191 * If an operation is pending, handle it...
1192 */
1193 do_pending_operator(&ca, old_col, FALSE);
1194
1195 /*
1196 * Wait for a moment when a message is displayed that will be overwritten
1197 * by the mode message.
1198 * In Visual mode and with "^O" in Insert mode, a short message will be
1199 * overwritten by the mode message. Wait a bit, until a key is hit.
1200 * In Visual mode, it's more important to keep the Visual area updated
1201 * than keeping a message (e.g. from a /pat search).
1202 * Only do this if the command was typed, not from a mapping.
1203 * Don't wait when emsg_silent is non-zero.
1204 * Also wait a bit after an error message, e.g. for "^O:".
1205 * Don't redraw the screen, it would remove the message.
1206 */
1207 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001208 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 || (VIsual_active
1211 && old_pos.lnum == curwin->w_cursor.lnum
1212 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 )
1214 && (clear_cmdline
1215 || redraw_cmdline)
1216 && (msg_didout || (msg_didany && msg_scroll))
1217 && !msg_nowait
1218 && KeyTyped)
1219 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && (msg_scroll
1222 || emsg_on_display)))
1223 && oap->regname == 0
1224 && !(ca.retval & CA_COMMAND_BUSY)
1225 && stuff_empty()
1226 && typebuf_typed()
1227 && emsg_silent == 0
1228 && !did_wait_return
1229 && oap->op_type == OP_NOP)
1230 {
1231 int save_State = State;
1232
1233 /* Draw the cursor with the right shape here */
1234 if (restart_edit != 0)
1235 State = INSERT;
1236
1237 /* If need to redraw, and there is a "keep_msg", redraw before the
1238 * delay */
1239 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1240 {
1241 char_u *kmsg;
1242
1243 kmsg = keep_msg;
1244 keep_msg = NULL;
1245 /* showmode() will clear keep_msg, but we want to use it anyway */
1246 update_screen(0);
1247 /* now reset it, otherwise it's put in the history again */
1248 keep_msg = kmsg;
1249 msg_attr(kmsg, keep_msg_attr);
1250 vim_free(kmsg);
1251 }
1252 setcursor();
1253 cursor_on();
1254 out_flush();
1255 if (msg_scroll || emsg_on_display)
1256 ui_delay(1000L, TRUE); /* wait at least one second */
1257 ui_delay(3000L, FALSE); /* wait up to three seconds */
1258 State = save_State;
1259
1260 msg_scroll = FALSE;
1261 emsg_on_display = FALSE;
1262 }
1263
1264 /*
1265 * Finish up after executing a Normal mode command.
1266 */
1267normal_end:
1268
1269 msg_nowait = FALSE;
1270
1271 /* Reset finish_op, in case it was set */
1272#ifdef CURSOR_SHAPE
1273 c = finish_op;
1274#endif
1275 finish_op = FALSE;
1276#ifdef CURSOR_SHAPE
1277 /* Redraw the cursor with another shape, if we were in Operator-pending
1278 * mode or did a replace command. */
1279 if (c || ca.cmdchar == 'r')
1280 {
1281 ui_cursor_shape(); /* may show different cursor shape */
1282# ifdef FEAT_MOUSESHAPE
1283 update_mouseshape(-1);
1284# endif
1285 }
1286#endif
1287
1288#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001289 if (oap->op_type == OP_NOP && oap->regname == 0
1290# ifdef FEAT_AUTOCMD
1291 && ca.cmdchar != K_CURSORHOLD
1292# endif
1293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 clear_showcmd();
1295#endif
1296
1297 checkpcmark(); /* check if we moved since setting pcmark */
1298 vim_free(ca.searchbuf);
1299
1300#ifdef FEAT_MBYTE
1301 if (has_mbyte)
1302 mb_adjust_cursor();
1303#endif
1304
1305#ifdef FEAT_SCROLLBIND
1306 if (curwin->w_p_scb && toplevel)
1307 {
1308 validate_cursor(); /* may need to update w_leftcol */
1309 do_check_scrollbind(TRUE);
1310 }
1311#endif
1312
Bram Moolenaar860cae12010-06-05 23:22:07 +02001313#ifdef FEAT_CURSORBIND
1314 if (curwin->w_p_crb && toplevel)
1315 {
1316 validate_cursor(); /* may need to update w_leftcol */
1317 do_check_cursorbind();
1318 }
1319#endif
1320
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 /*
1322 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1323 * if still inside a mapping that started in Visual mode).
1324 * May switch from Visual to Select mode after CTRL-O command.
1325 */
1326 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1328 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && !(ca.retval & CA_COMMAND_BUSY)
1330 && stuff_empty()
1331 && oap->regname == 0)
1332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 1)
1334 {
1335 VIsual_select = TRUE;
1336 showmode();
1337 restart_VIsual_select = 0;
1338 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001339 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 (void)edit(restart_edit, FALSE, 1L);
1341 }
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (restart_VIsual_select == 2)
1344 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /* Save count before an operator for next time. */
1347 opcount = ca.opcount;
1348}
1349
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001350#ifdef FEAT_EVAL
1351/*
1352 * Set v:count and v:count1 according to "cap".
1353 * Set v:prevcount only when "set_prevcount" is TRUE.
1354 */
1355 static void
1356set_vcount_ca(cap, set_prevcount)
1357 cmdarg_T *cap;
1358 int *set_prevcount;
1359{
1360 long count = cap->count0;
1361
1362 /* multiply with cap->opcount the same way as above */
1363 if (cap->opcount != 0)
1364 count = cap->opcount * (count == 0 ? 1 : count);
1365 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1366 *set_prevcount = FALSE; /* only set v:prevcount once */
1367}
1368#endif
1369
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370/*
1371 * Handle an operator after visual mode or when the movement is finished
1372 */
1373 void
1374do_pending_operator(cap, old_col, gui_yank)
1375 cmdarg_T *cap;
1376 int old_col;
1377 int gui_yank;
1378{
1379 oparg_T *oap = cap->oap;
1380 pos_T old_cursor;
1381 int empty_region_error;
1382 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001383#ifdef FEAT_LINEBREAK
1384 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /* The visual area is remembered for redo */
1388 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1389 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001390 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001392#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#endif
1395
1396#if defined(FEAT_CLIPBOARD)
1397 /*
1398 * Yank the visual area into the GUI selection register before we operate
1399 * on it and lose it forever.
1400 * Don't do it if a specific register was specified, so that ""x"*P works.
1401 * This could call do_pending_operator() recursively, but that's OK
1402 * because gui_yank will be TRUE for the nested call.
1403 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001404 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 && oap->op_type != OP_NOP
1406 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 && VIsual_active
1408 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 && oap->regname == 0)
1410 clip_auto_select();
1411#endif
1412 old_cursor = curwin->w_cursor;
1413
1414 /*
1415 * If an operation is pending, handle it...
1416 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001417 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001419#ifdef FEAT_LINEBREAK
1420 /* Avoid a problem with unwanted linebreaks in block mode. */
1421 curwin->w_p_lbr = FALSE;
1422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 oap->is_VIsual = VIsual_active;
1424 if (oap->motion_force == 'V')
1425 oap->motion_type = MLINE;
1426 else if (oap->motion_force == 'v')
1427 {
1428 /* If the motion was linewise, "inclusive" will not have been set.
1429 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1430 if (oap->motion_type == MLINE)
1431 oap->inclusive = FALSE;
1432 /* If the motion already was characterwise, toggle "inclusive" */
1433 else if (oap->motion_type == MCHAR)
1434 oap->inclusive = !oap->inclusive;
1435 oap->motion_type = MCHAR;
1436 }
1437 else if (oap->motion_force == Ctrl_V)
1438 {
1439 /* Change line- or characterwise motion into Visual block mode. */
1440 VIsual_active = TRUE;
1441 VIsual = oap->start;
1442 VIsual_mode = Ctrl_V;
1443 VIsual_select = FALSE;
1444 VIsual_reselect = FALSE;
1445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar7afea822013-04-24 18:34:45 +02001447 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1448 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001450 && ((!VIsual_active || oap->motion_force)
1451 /* Also redo Operator-pending Visual mode mappings */
1452 || (VIsual_active && cap->cmdchar == ':'
1453 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001454 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef FEAT_FOLDING
1456 && oap->op_type != OP_FOLD
1457 && oap->op_type != OP_FOLDOPEN
1458 && oap->op_type != OP_FOLDOPENREC
1459 && oap->op_type != OP_FOLDCLOSE
1460 && oap->op_type != OP_FOLDCLOSEREC
1461 && oap->op_type != OP_FOLDDEL
1462 && oap->op_type != OP_FOLDDELREC
1463#endif
1464 )
1465 {
1466 prep_redo(oap->regname, cap->count0,
1467 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1468 oap->motion_force, cap->cmdchar, cap->nchar);
1469 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1470 {
1471 /*
1472 * If 'cpoptions' does not contain 'r', insert the search
1473 * pattern to really repeat the same command.
1474 */
1475 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001476 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 AppendToRedobuff(NL_STR);
1478 }
1479 else if (cap->cmdchar == ':')
1480 {
1481 /* do_cmdline() has stored the first typed line in
1482 * "repeat_cmdline". When several lines are typed repeating
1483 * won't be possible. */
1484 if (repeat_cmdline == NULL)
1485 ResetRedobuff();
1486 else
1487 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001488 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 AppendToRedobuff(NL_STR);
1490 vim_free(repeat_cmdline);
1491 repeat_cmdline = NULL;
1492 }
1493 }
1494 }
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (redo_VIsual_busy)
1497 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001498 /* Redo of an operation on a Visual area. Use the same size from
1499 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 oap->start = curwin->w_cursor;
1501 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1502 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1503 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1504 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001505 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001507 if (VIsual_mode == 'v')
1508 {
1509 if (redo_VIsual_line_count <= 1)
1510 {
1511 validate_virtcol();
1512 curwin->w_curswant =
1513 curwin->w_virtcol + redo_VIsual_vcol - 1;
1514 }
1515 else
1516 curwin->w_curswant = redo_VIsual_vcol;
1517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001519 {
1520 curwin->w_curswant = MAXCOL;
1521 }
1522 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 cap->count0 = redo_VIsual_count;
1525 if (redo_VIsual_count != 0)
1526 cap->count1 = redo_VIsual_count;
1527 else
1528 cap->count1 = 1;
1529 }
1530 else if (VIsual_active)
1531 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001532 if (!gui_yank)
1533 {
1534 /* Save the current VIsual area for '< and '> marks, and "gv" */
1535 curbuf->b_visual.vi_start = VIsual;
1536 curbuf->b_visual.vi_end = curwin->w_cursor;
1537 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001538 if (VIsual_mode_orig != NUL)
1539 {
1540 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1541 VIsual_mode_orig = NUL;
1542 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001543 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001545 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549 /* In Select mode, a linewise selection is operated upon like a
1550 * characterwise selection. */
1551 if (VIsual_select && VIsual_mode == 'V')
1552 {
1553 if (lt(VIsual, curwin->w_cursor))
1554 {
1555 VIsual.col = 0;
1556 curwin->w_cursor.col =
1557 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1558 }
1559 else
1560 {
1561 curwin->w_cursor.col = 0;
1562 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1563 }
1564 VIsual_mode = 'v';
1565 }
1566 /* If 'selection' is "exclusive", backup one character for
1567 * charwise selections. */
1568 else if (VIsual_mode == 'v')
1569 {
1570# ifdef FEAT_VIRTUALEDIT
1571 include_line_break =
1572# endif
1573 unadjust_for_sel();
1574 }
1575
1576 oap->start = VIsual;
1577 if (VIsual_mode == 'V')
1578 oap->start.col = 0;
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 /*
1582 * Set oap->start to the first position of the operated text, oap->end
1583 * to the end of the operated text. w_cursor is equal to oap->start.
1584 */
1585 if (lt(oap->start, curwin->w_cursor))
1586 {
1587#ifdef FEAT_FOLDING
1588 /* Include folded lines completely. */
1589 if (!VIsual_active)
1590 {
1591 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1592 oap->start.col = 0;
1593 if (hasFolding(curwin->w_cursor.lnum, NULL,
1594 &curwin->w_cursor.lnum))
1595 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1596 }
1597#endif
1598 oap->end = curwin->w_cursor;
1599 curwin->w_cursor = oap->start;
1600
1601 /* w_virtcol may have been updated; if the cursor goes back to its
1602 * previous position w_virtcol becomes invalid and isn't updated
1603 * automatically. */
1604 curwin->w_valid &= ~VALID_VIRTCOL;
1605 }
1606 else
1607 {
1608#ifdef FEAT_FOLDING
1609 /* Include folded lines completely. */
1610 if (!VIsual_active && oap->motion_type == MLINE)
1611 {
1612 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1613 NULL))
1614 curwin->w_cursor.col = 0;
1615 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1616 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1617 }
1618#endif
1619 oap->end = oap->start;
1620 oap->start = curwin->w_cursor;
1621 }
1622
1623 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1624
1625#ifdef FEAT_VIRTUALEDIT
1626 /* Set "virtual_op" before resetting VIsual_active. */
1627 virtual_op = virtual_active();
1628#endif
1629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (VIsual_active || redo_VIsual_busy)
1631 {
1632 if (VIsual_mode == Ctrl_V) /* block mode */
1633 {
1634 colnr_T start, end;
1635
1636 oap->block_mode = TRUE;
1637
1638 getvvcol(curwin, &(oap->start),
1639 &oap->start_vcol, NULL, &oap->end_vcol);
1640 if (!redo_VIsual_busy)
1641 {
1642 getvvcol(curwin, &(oap->end), &start, NULL, &end);
1643
1644 if (start < oap->start_vcol)
1645 oap->start_vcol = start;
1646 if (end > oap->end_vcol)
1647 {
1648 if (*p_sel == 'e' && start >= 1
1649 && start - 1 >= oap->end_vcol)
1650 oap->end_vcol = start - 1;
1651 else
1652 oap->end_vcol = end;
1653 }
1654 }
1655
1656 /* if '$' was used, get oap->end_vcol from longest line */
1657 if (curwin->w_curswant == MAXCOL)
1658 {
1659 curwin->w_cursor.col = MAXCOL;
1660 oap->end_vcol = 0;
1661 for (curwin->w_cursor.lnum = oap->start.lnum;
1662 curwin->w_cursor.lnum <= oap->end.lnum;
1663 ++curwin->w_cursor.lnum)
1664 {
1665 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
1666 if (end > oap->end_vcol)
1667 oap->end_vcol = end;
1668 }
1669 }
1670 else if (redo_VIsual_busy)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001671 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 /*
1673 * Correct oap->end.col and oap->start.col to be the
1674 * upper-left and lower-right corner of the block area.
1675 *
1676 * (Actually, this does convert column positions into character
1677 * positions)
1678 */
1679 curwin->w_cursor.lnum = oap->end.lnum;
1680 coladvance(oap->end_vcol);
1681 oap->end = curwin->w_cursor;
1682
1683 curwin->w_cursor = oap->start;
1684 coladvance(oap->start_vcol);
1685 oap->start = curwin->w_cursor;
1686 }
1687
1688 if (!redo_VIsual_busy && !gui_yank)
1689 {
1690 /*
1691 * Prepare to reselect and redo Visual: this is based on the
1692 * size of the Visual text
1693 */
1694 resel_VIsual_mode = VIsual_mode;
1695 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001696 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001698 {
1699 if (VIsual_mode != Ctrl_V)
1700 getvvcol(curwin, &(oap->end),
1701 NULL, NULL, &oap->end_vcol);
1702 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1703 {
1704 if (VIsual_mode != Ctrl_V)
1705 getvvcol(curwin, &(oap->start),
1706 &oap->start_vcol, NULL, NULL);
1707 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1708 }
1709 else
1710 resel_VIsual_vcol = oap->end_vcol;
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 resel_VIsual_line_count = oap->line_count;
1713 }
1714
1715 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1716 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1717 && oap->op_type != OP_COLON
1718#ifdef FEAT_FOLDING
1719 && oap->op_type != OP_FOLD
1720 && oap->op_type != OP_FOLDOPEN
1721 && oap->op_type != OP_FOLDOPENREC
1722 && oap->op_type != OP_FOLDCLOSE
1723 && oap->op_type != OP_FOLDCLOSEREC
1724 && oap->op_type != OP_FOLDDEL
1725 && oap->op_type != OP_FOLDDELREC
1726#endif
1727 && oap->motion_force == NUL
1728 )
1729 {
1730 /* Prepare for redoing. Only use the nchar field for "r",
1731 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001732 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1733 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001734 prep_redo(oap->regname, cap->count0,
1735 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1736 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001737 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001738 prep_redo(oap->regname, 0L, NUL, 'v',
1739 get_op_char(oap->op_type),
1740 get_extra_op_char(oap->op_type),
1741 oap->op_type == OP_REPLACE
1742 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (!redo_VIsual_busy)
1744 {
1745 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001746 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 redo_VIsual_line_count = resel_VIsual_line_count;
1748 redo_VIsual_count = cap->count0;
1749 }
1750 }
1751
1752 /*
1753 * oap->inclusive defaults to TRUE.
1754 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1755 * FALSE. This makes "d}P" and "v}dP" work the same.
1756 */
1757 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1758 oap->inclusive = TRUE;
1759 if (VIsual_mode == 'V')
1760 oap->motion_type = MLINE;
1761 else
1762 {
1763 oap->motion_type = MCHAR;
1764 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001765#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 )
1769 {
1770 oap->inclusive = FALSE;
1771 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001772 * that works on lines only. */
1773 if (*p_sel != 'o' && !op_on_lines(oap->op_type))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 {
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001775 if (oap->end.lnum < curbuf->b_ml.ml_line_count)
1776 {
1777 ++oap->end.lnum;
1778 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001779#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001780 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001781#endif
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001782 ++oap->line_count;
1783 }
1784 else
1785 {
1786 /* Cannot move below the last line, make the op
1787 * inclusive to tell the operation to include the
1788 * line break. */
1789 oap->inclusive = TRUE;
1790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 }
1793 }
1794
1795 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 /*
1798 * Switch Visual off now, so screen updating does
1799 * not show inverted text when the screen is redrawn.
1800 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1801 * no screen redraw, so it is done here to remove the inverted
1802 * part.
1803 */
1804 if (!gui_yank)
1805 {
1806 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001807#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 setmouse();
1809 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001810#endif
Bram Moolenaar28c258f2006-01-25 22:02:51 +00001811 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 clear_cmdline = TRUE; /* unshow visual mode later */
1813#ifdef FEAT_CMDL_INFO
1814 else
1815 clear_showcmd();
1816#endif
1817 if ((oap->op_type == OP_YANK
1818 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001819 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 || oap->op_type == OP_FILTER)
1821 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001822 {
1823#ifdef FEAT_LINEBREAK
1824 /* make sure redrawing is correct */
1825 curwin->w_p_lbr = lbr_saved;
1826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832#ifdef FEAT_MBYTE
1833 /* Include the trailing byte of a multi-byte char. */
1834 if (has_mbyte && oap->inclusive)
1835 {
1836 int l;
1837
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001838 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (l > 1)
1840 oap->end.col += l - 1;
1841 }
1842#endif
1843 curwin->w_set_curswant = TRUE;
1844
1845 /*
1846 * oap->empty is set when start and end are the same. The inclusive
1847 * flag affects this too, unless yanking and the end is on a NUL.
1848 */
1849 oap->empty = (oap->motion_type == MCHAR
1850 && (!oap->inclusive
1851 || (oap->op_type == OP_YANK
1852 && gchar_pos(&oap->end) == NUL))
1853 && equalpos(oap->start, oap->end)
1854#ifdef FEAT_VIRTUALEDIT
1855 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1856#endif
1857 );
1858 /*
1859 * For delete, change and yank, it's an error to operate on an
1860 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1861 */
1862 empty_region_error = (oap->empty
1863 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1864
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 /* Force a redraw when operating on an empty Visual region, when
1866 * 'modifiable is off or creating a fold. */
1867 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001868#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001872 {
1873#ifdef FEAT_LINEBREAK
1874 curwin->w_p_lbr = lbr_saved;
1875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 /*
1880 * If the end of an operator is in column one while oap->motion_type
1881 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1882 * character in the previous line. If op_start is on or before the
1883 * first non-blank in the line, the operator becomes linewise
1884 * (strange, but that's the way vi does it).
1885 */
1886 if ( oap->motion_type == MCHAR
1887 && oap->inclusive == FALSE
1888 && !(cap->retval & CA_NO_ADJ_OP_END)
1889 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001891 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 && oap->line_count > 1)
1893 {
1894 oap->end_adjusted = TRUE; /* remember that we did this */
1895 --oap->line_count;
1896 --oap->end.lnum;
1897 if (inindent(0))
1898 oap->motion_type = MLINE;
1899 else
1900 {
1901 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1902 if (oap->end.col)
1903 {
1904 --oap->end.col;
1905 oap->inclusive = TRUE;
1906 }
1907 }
1908 }
1909 else
1910 oap->end_adjusted = FALSE;
1911
1912 switch (oap->op_type)
1913 {
1914 case OP_LSHIFT:
1915 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001916 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 auto_format(FALSE, TRUE);
1918 break;
1919
1920 case OP_JOIN_NS:
1921 case OP_JOIN:
1922 if (oap->line_count < 2)
1923 oap->line_count = 2;
1924 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1925 curbuf->b_ml.ml_line_count)
1926 beep_flush();
1927 else
1928 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001929 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001930 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 auto_format(FALSE, TRUE);
1932 }
1933 break;
1934
1935 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001940 CancelRedo();
1941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
1943 {
1944 (void)op_delete(oap);
1945 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1946 u_save_cursor(); /* cursor line wasn't saved yet */
1947 auto_format(FALSE, TRUE);
1948 }
1949 break;
1950
1951 case OP_YANK:
1952 if (empty_region_error)
1953 {
1954 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001957 CancelRedo();
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
1960 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001961 {
1962#ifdef FEAT_LINEBREAK
1963 curwin->w_p_lbr = lbr_saved;
1964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 check_cursor_col();
1968 break;
1969
1970 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001975 CancelRedo();
1976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 else
1978 {
1979 /* This is a new edit command, not a restart. Need to
1980 * remember it to make 'insertmode' work with mappings for
1981 * Visual mode. But do this only once and not when typed and
1982 * 'insertmode' isn't set. */
1983 if (p_im || !KeyTyped)
1984 restart_edit_save = restart_edit;
1985 else
1986 restart_edit_save = 0;
1987 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001988#ifdef FEAT_LINEBREAK
1989 /* Restore linebreak, so that when the user edits it looks as
1990 * before. */
1991 curwin->w_p_lbr = lbr_saved;
1992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 /* Reset finish_op now, don't want it set inside edit(). */
1994 finish_op = FALSE;
1995 if (op_change(oap)) /* will call edit() */
1996 cap->retval |= CA_COMMAND_BUSY;
1997 if (restart_edit == 0)
1998 restart_edit = restart_edit_save;
1999 }
2000 break;
2001
2002 case OP_FILTER:
2003 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
2004 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
2005 else
2006 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
2007
2008 case OP_INDENT:
2009 case OP_COLON:
2010
2011#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
2012 /*
2013 * If 'equalprg' is empty, do the indenting internally.
2014 */
2015 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
2016 {
2017# ifdef FEAT_LISP
2018 if (curbuf->b_p_lisp)
2019 {
2020 op_reindent(oap, get_lisp_indent);
2021 break;
2022 }
2023# endif
2024# ifdef FEAT_CINDENT
2025 op_reindent(oap,
2026# ifdef FEAT_EVAL
2027 *curbuf->b_p_inde != NUL ? get_expr_indent :
2028# endif
2029 get_c_indent);
2030 break;
2031# endif
2032 }
2033#endif
2034
2035 op_colon(oap);
2036 break;
2037
2038 case OP_TILDE:
2039 case OP_UPPER:
2040 case OP_LOWER:
2041 case OP_ROT13:
2042 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002045 CancelRedo();
2046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 else
2048 op_tilde(oap);
2049 check_cursor_col();
2050 break;
2051
2052 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002053#if defined(FEAT_EVAL)
2054 if (*curbuf->b_p_fex != NUL)
2055 op_formatexpr(oap); /* use expression */
2056 else
2057#endif
2058 if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 op_colon(oap); /* use external command */
2060 else
2061 op_format(oap, FALSE); /* use internal function */
2062 break;
2063
2064 case OP_FORMAT2:
2065 op_format(oap, TRUE); /* use internal function */
2066 break;
2067
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002068 case OP_FUNCTION:
2069 op_function(oap); /* call 'operatorfunc' */
2070 break;
2071
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 case OP_INSERT:
2073 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#ifdef FEAT_VISUALEXTRA
2076 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002077 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002079 CancelRedo();
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 else
2082 {
2083 /* This is a new edit command, not a restart. Need to
2084 * remember it to make 'insertmode' work with mappings for
2085 * Visual mode. But do this only once. */
2086 restart_edit_save = restart_edit;
2087 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002088#ifdef FEAT_LINEBREAK
2089 /* Restore linebreak, so that when the user edits it looks as
2090 * before. */
2091 curwin->w_p_lbr = lbr_saved;
2092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002094#ifdef FEAT_LINEBREAK
2095 /* Reset linebreak, so that formatting works correctly. */
2096 curwin->w_p_lbr = FALSE;
2097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 /* TODO: when inserting in several lines, should format all
2100 * the lines. */
2101 auto_format(FALSE, TRUE);
2102
2103 if (restart_edit == 0)
2104 restart_edit = restart_edit_save;
2105 }
2106#else
2107 vim_beep();
2108#endif
2109 break;
2110
2111 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113#ifdef FEAT_VISUALEXTRA
2114 if (empty_region_error)
2115#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 vim_beep();
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002118 CancelRedo();
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#ifdef FEAT_VISUALEXTRA
2121 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002122 {
2123#ifdef FEAT_LINEBREAK
2124 /* Restore linebreak, so that when the user edits it looks as
2125 * before. */
2126 curwin->w_p_lbr = lbr_saved;
2127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#endif
2131 break;
2132
2133#ifdef FEAT_FOLDING
2134 case OP_FOLD:
2135 VIsual_reselect = FALSE; /* don't reselect now */
2136 foldCreate(oap->start.lnum, oap->end.lnum);
2137 break;
2138
2139 case OP_FOLDOPEN:
2140 case OP_FOLDOPENREC:
2141 case OP_FOLDCLOSE:
2142 case OP_FOLDCLOSEREC:
2143 VIsual_reselect = FALSE; /* don't reselect now */
2144 opFoldRange(oap->start.lnum, oap->end.lnum,
2145 oap->op_type == OP_FOLDOPEN
2146 || oap->op_type == OP_FOLDOPENREC,
2147 oap->op_type == OP_FOLDOPENREC
2148 || oap->op_type == OP_FOLDCLOSEREC,
2149 oap->is_VIsual);
2150 break;
2151
2152 case OP_FOLDDEL:
2153 case OP_FOLDDELREC:
2154 VIsual_reselect = FALSE; /* don't reselect now */
2155 deleteFold(oap->start.lnum, oap->end.lnum,
2156 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2157 break;
2158#endif
2159 default:
2160 clearopbeep(oap);
2161 }
2162#ifdef FEAT_VIRTUALEDIT
2163 virtual_op = MAYBE;
2164#endif
2165 if (!gui_yank)
2166 {
2167 /*
2168 * if 'sol' not set, go back to old column for some commands
2169 */
2170 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2171 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2172 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002173 {
2174#ifdef FEAT_LINEBREAK
2175 curwin->w_p_lbr = FALSE;
2176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 else
2181 {
2182 curwin->w_cursor = old_cursor;
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 clearop(oap);
2186 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002187#ifdef FEAT_LINEBREAK
2188 curwin->w_p_lbr = lbr_saved;
2189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190}
2191
2192/*
2193 * Handle indent and format operators and visual mode ":".
2194 */
2195 static void
2196op_colon(oap)
2197 oparg_T *oap;
2198{
2199 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (oap->is_VIsual)
2201 stuffReadbuff((char_u *)"'<,'>");
2202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 /*
2205 * Make the range look nice, so it can be repeated.
2206 */
2207 if (oap->start.lnum == curwin->w_cursor.lnum)
2208 stuffcharReadbuff('.');
2209 else
2210 stuffnumReadbuff((long)oap->start.lnum);
2211 if (oap->end.lnum != oap->start.lnum)
2212 {
2213 stuffcharReadbuff(',');
2214 if (oap->end.lnum == curwin->w_cursor.lnum)
2215 stuffcharReadbuff('.');
2216 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2217 stuffcharReadbuff('$');
2218 else if (oap->start.lnum == curwin->w_cursor.lnum)
2219 {
2220 stuffReadbuff((char_u *)".+");
2221 stuffnumReadbuff((long)oap->line_count - 1);
2222 }
2223 else
2224 stuffnumReadbuff((long)oap->end.lnum);
2225 }
2226 }
2227 if (oap->op_type != OP_COLON)
2228 stuffReadbuff((char_u *)"!");
2229 if (oap->op_type == OP_INDENT)
2230 {
2231#ifndef FEAT_CINDENT
2232 if (*get_equalprg() == NUL)
2233 stuffReadbuff((char_u *)"indent");
2234 else
2235#endif
2236 stuffReadbuff(get_equalprg());
2237 stuffReadbuff((char_u *)"\n");
2238 }
2239 else if (oap->op_type == OP_FORMAT)
2240 {
2241 if (*p_fp == NUL)
2242 stuffReadbuff((char_u *)"fmt");
2243 else
2244 stuffReadbuff(p_fp);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002245 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247
2248 /*
2249 * do_cmdline() does the rest
2250 */
2251}
2252
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002253/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002254 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002255 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002256 static void
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002257op_function(oap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00002258 oparg_T *oap UNUSED;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002259{
2260#ifdef FEAT_EVAL
2261 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002262# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002263 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002264# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002265
2266 if (*p_opfunc == NUL)
2267 EMSG(_("E774: 'operatorfunc' is empty"));
2268 else
2269 {
2270 /* Set '[ and '] marks to text to be operated on. */
2271 curbuf->b_op_start = oap->start;
2272 curbuf->b_op_end = oap->end;
2273 if (oap->motion_type != MLINE && !oap->inclusive)
2274 /* Exclude the end position. */
2275 decl(&curbuf->b_op_end);
2276
2277 if (oap->block_mode)
2278 argv[0] = (char_u *)"block";
2279 else if (oap->motion_type == MLINE)
2280 argv[0] = (char_u *)"line";
2281 else
2282 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002283
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002284# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002285 /* Reset virtual_op so that 'virtualedit' can be changed in the
2286 * function. */
2287 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002288# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002289
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002290 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002291
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002292# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002293 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002294# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002295 }
2296#else
2297 EMSG(_("E775: Eval feature not available"));
2298#endif
2299}
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301#if defined(FEAT_MOUSE) || defined(PROTO)
2302/*
2303 * Do the appropriate action for the current mouse click in the current mode.
2304 * Not used for Command-line mode.
2305 *
2306 * Normal Mode:
2307 * event modi- position visual change action
2308 * fier cursor window
2309 * left press - yes end yes
2310 * left press C yes end yes "^]" (2)
2311 * left press S yes end yes "*" (2)
2312 * left drag - yes start if moved no
2313 * left relse - yes start if moved no
2314 * middle press - yes if not active no put register
2315 * middle press - yes if active no yank and put
2316 * right press - yes start or extend yes
2317 * right press S yes no change yes "#" (2)
2318 * right drag - yes extend no
2319 * right relse - yes extend no
2320 *
2321 * Insert or Replace Mode:
2322 * event modi- position visual change action
2323 * fier cursor window
2324 * left press - yes (cannot be active) yes
2325 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2326 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2327 * left drag - yes start or extend (1) no CTRL-O (1)
2328 * left relse - yes start or extend (1) no CTRL-O (1)
2329 * middle press - no (cannot be active) no put register
2330 * right press - yes start or extend yes CTRL-O
2331 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2332 *
2333 * (1) only if mouse pointer moved since press
2334 * (2) only if click is in same buffer
2335 *
2336 * Return TRUE if start_arrow() should be called for edit mode.
2337 */
2338 int
2339do_mouse(oap, c, dir, count, fixindent)
2340 oparg_T *oap; /* operator argument, can be NULL */
2341 int c; /* K_LEFTMOUSE, etc */
2342 int dir; /* Direction to 'put' if necessary */
2343 long count;
2344 int fixindent; /* PUT_FIXINDENT if fixing indent necessary */
2345{
2346 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2347 static int got_click = FALSE; /* got a click some time back */
2348
2349 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2350 int is_click; /* If FALSE it's a drag or release event */
2351 int is_drag; /* If TRUE it's a drag event */
2352 int jump_flags = 0; /* flags for jump_to_mouse() */
2353 pos_T start_visual;
2354 int moved; /* Has cursor moved? */
2355 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002356#ifdef FEAT_WINDOWS
2357 static int in_tab_line = FALSE; /* mouse clicked in tab line */
2358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#ifdef FEAT_VERTSPLIT
2360 int in_sep_line; /* mouse in vertical separator line */
2361#endif
2362 int c1, c2;
2363#if defined(FEAT_FOLDING)
2364 pos_T save_cursor;
2365#endif
2366 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 static pos_T orig_cursor;
2368 colnr_T leftcol, rightcol;
2369 pos_T end_visual;
2370 int diff;
2371 int old_active = VIsual_active;
2372 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 int regname;
2374
2375#if defined(FEAT_FOLDING)
2376 save_cursor = curwin->w_cursor;
2377#endif
2378
2379 /*
2380 * When GUI is active, always recognize mouse events, otherwise:
2381 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2382 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2383 * - For command line and insert mode 'mouse' is checked before calling
2384 * do_mouse().
2385 */
2386 if (do_always)
2387 do_always = FALSE;
2388 else
2389#ifdef FEAT_GUI
2390 if (!gui.in_use)
2391#endif
2392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (VIsual_active)
2394 {
2395 if (!mouse_has(MOUSE_VISUAL))
2396 return FALSE;
2397 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002398 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 return FALSE;
2400 }
2401
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002402 for (;;)
2403 {
2404 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2405 if (is_drag)
2406 {
2407 /* If the next character is the same mouse event then use that
2408 * one. Speeds up dragging the status line. */
2409 if (vpeekc() != NUL)
2410 {
2411 int nc;
2412 int save_mouse_row = mouse_row;
2413 int save_mouse_col = mouse_col;
2414
2415 /* Need to get the character, peeking doesn't get the actual
2416 * one. */
2417 nc = safe_vgetc();
2418 if (c == nc)
2419 continue;
2420 vungetc(nc);
2421 mouse_row = save_mouse_row;
2422 mouse_col = save_mouse_col;
2423 }
2424 }
2425 break;
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427
2428#ifdef FEAT_MOUSESHAPE
2429 /* May have stopped dragging the status or separator line. The pointer is
2430 * most likely still on the status or separator line. */
2431 if (!is_drag && drag_status_line)
2432 {
2433 drag_status_line = FALSE;
2434 update_mouseshape(SHAPE_IDX_STATUS);
2435 }
2436# ifdef FEAT_VERTSPLIT
2437 if (!is_drag && drag_sep_line)
2438 {
2439 drag_sep_line = FALSE;
2440 update_mouseshape(SHAPE_IDX_VSEP);
2441 }
2442# endif
2443#endif
2444
2445 /*
2446 * Ignore drag and release events if we didn't get a click.
2447 */
2448 if (is_click)
2449 got_click = TRUE;
2450 else
2451 {
2452 if (!got_click) /* didn't get click, ignore */
2453 return FALSE;
2454 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002457#ifdef FEAT_WINDOWS
2458 if (in_tab_line)
2459 {
2460 in_tab_line = FALSE;
2461 return FALSE;
2462 }
2463#endif
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 }
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * CTRL right mouse button does CTRL-T
2469 */
2470 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2471 {
2472 if (State & INSERT)
2473 stuffcharReadbuff(Ctrl_O);
2474 if (count > 1)
2475 stuffnumReadbuff(count);
2476 stuffcharReadbuff(Ctrl_T);
2477 got_click = FALSE; /* ignore drag&release now */
2478 return FALSE;
2479 }
2480
2481 /*
2482 * CTRL only works with left mouse button
2483 */
2484 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2485 return FALSE;
2486
2487 /*
2488 * When a modifier is down, ignore drag and release events, as well as
2489 * multiple clicks and the middle mouse button.
2490 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2491 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002492 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2493 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 && (!is_click
2495 || (mod_mask & MOD_MASK_MULTI_CLICK)
2496 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002497 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 && mouse_model_popup()
2499 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002500 && !((mod_mask & MOD_MASK_ALT)
2501 && !mouse_model_popup()
2502 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 )
2504 return FALSE;
2505
2506 /*
2507 * If the button press was used as the movement command for an operator
2508 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2509 * drag/release events.
2510 */
2511 if (!is_click && which_button == MOUSE_MIDDLE)
2512 return FALSE;
2513
2514 if (oap != NULL)
2515 regname = oap->regname;
2516 else
2517 regname = 0;
2518
2519 /*
2520 * Middle mouse button does a 'put' of the selected text
2521 */
2522 if (which_button == MOUSE_MIDDLE)
2523 {
2524 if (State == NORMAL)
2525 {
2526 /*
2527 * If an operator was pending, we don't know what the user wanted
2528 * to do. Go back to normal mode: Clear the operator and beep().
2529 */
2530 if (oap != NULL && oap->op_type != OP_NOP)
2531 {
2532 clearopbeep(oap);
2533 return FALSE;
2534 }
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 /*
2537 * If visual was active, yank the highlighted text and put it
2538 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002539 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 */
2541 if (VIsual_active)
2542 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002543 if (VIsual_select)
2544 {
2545 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002546 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002547 }
2548 else
2549 {
2550 stuffcharReadbuff('y');
2551 stuffcharReadbuff(K_MIDDLEMOUSE);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 do_always = TRUE; /* ignore 'mouse' setting next time */
2554 return FALSE;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 /*
2557 * The rest is below jump_to_mouse()
2558 */
2559 }
2560
2561 else if ((State & INSERT) == 0)
2562 return FALSE;
2563
2564 /*
2565 * Middle click in insert mode doesn't move the mouse, just insert the
2566 * contents of a register. '.' register is special, can't insert that
2567 * with do_put().
2568 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2569 * happens for the GUI).
2570 */
2571 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2572 {
2573 if (regname == '.')
2574 insert_reg(regname, TRUE);
2575 else
2576 {
2577#ifdef FEAT_CLIPBOARD
2578 if (clip_star.available && regname == 0)
2579 regname = '*';
2580#endif
2581 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2582 insert_reg(regname, TRUE);
2583 else
2584 {
2585 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2586
2587 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2588 AppendCharToRedobuff(Ctrl_R);
2589 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2590 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2591 }
2592 }
2593 return FALSE;
2594 }
2595 }
2596
2597 /* When dragging or button-up stay in the same window. */
2598 if (!is_click)
2599 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2600
2601 start_visual.lnum = 0;
2602
Bram Moolenaarf740b292006-02-16 22:11:02 +00002603#ifdef FEAT_WINDOWS
2604 /* Check for clicking in the tab page line. */
2605 if (mouse_row == 0 && firstwin->w_winrow > 0)
2606 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002607 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002608 {
2609 if (in_tab_line)
2610 {
2611 c1 = TabPageIdxs[mouse_col];
2612 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2613 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002614 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002615 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002616
Bram Moolenaarf740b292006-02-16 22:11:02 +00002617 /* click in a tab selects that tab page */
2618 if (is_click
2619# ifdef FEAT_CMDWIN
2620 && cmdwin_type == 0
2621# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002623 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002624 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 c1 = TabPageIdxs[mouse_col];
2626 if (c1 >= 0)
2627 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002628 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2629 {
2630 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002631 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002632 tabpage_new();
2633 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2634 }
2635 else
2636 {
2637 /* Go to specified tab page, or next one if not clicking
2638 * on a label. */
2639 goto_tabpage(c1);
2640
2641 /* It's like clicking on the status line of a window. */
2642 if (curwin != old_curwin)
2643 end_visual_mode();
2644 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002645 }
2646 else if (c1 < 0)
2647 {
2648 tabpage_T *tp;
2649
2650 /* Close the current or specified tab page. */
2651 if (c1 == -999)
2652 tp = curtab;
2653 else
2654 tp = find_tabpage(-c1);
2655 if (tp == curtab)
2656 {
2657 if (first_tabpage->tp_next != NULL)
2658 tabpage_close(FALSE);
2659 }
2660 else if (tp != NULL)
2661 tabpage_close_other(tp, FALSE);
2662 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002663 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002664 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002665 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002666 else if (is_drag && in_tab_line)
2667 {
2668 c1 = TabPageIdxs[mouse_col];
2669 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2670 return FALSE;
2671 }
2672
Bram Moolenaarf740b292006-02-16 22:11:02 +00002673#endif
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /*
2676 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2677 * right button up -> pop-up menu
2678 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002679 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 */
2681 if (mouse_model_popup())
2682 {
2683 if (which_button == MOUSE_RIGHT
2684 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2685 {
2686 /*
2687 * NOTE: Ignore right button down and drag mouse events.
2688 * Windows only shows the popup menu on the button up event.
2689 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002690#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2691 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (!is_click)
2693 return FALSE;
2694#endif
2695#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2696 if (is_click || is_drag)
2697 return FALSE;
2698#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002699#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2701 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2702 if (gui.in_use)
2703 {
2704 jump_flags = 0;
2705 if (STRCMP(p_mousem, "popup_setpos") == 0)
2706 {
2707 /* First set the cursor position before showing the popup
2708 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (VIsual_active)
2710 {
2711 pos_T m_pos;
2712
2713 /*
2714 * set MOUSE_MAY_STOP_VIS if we are outside the
2715 * selection or the current window (might have false
2716 * negative here)
2717 */
2718 if (mouse_row < W_WINROW(curwin)
2719 || mouse_row
2720 > (W_WINROW(curwin) + curwin->w_height))
2721 jump_flags = MOUSE_MAY_STOP_VIS;
2722 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2723 jump_flags = MOUSE_MAY_STOP_VIS;
2724 else
2725 {
2726 if ((lt(curwin->w_cursor, VIsual)
2727 && (lt(m_pos, curwin->w_cursor)
2728 || lt(VIsual, m_pos)))
2729 || (lt(VIsual, curwin->w_cursor)
2730 && (lt(m_pos, VIsual)
2731 || lt(curwin->w_cursor, m_pos))))
2732 {
2733 jump_flags = MOUSE_MAY_STOP_VIS;
2734 }
2735 else if (VIsual_mode == Ctrl_V)
2736 {
2737 getvcols(curwin, &curwin->w_cursor, &VIsual,
2738 &leftcol, &rightcol);
2739 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2740 if (m_pos.col < leftcol || m_pos.col > rightcol)
2741 jump_flags = MOUSE_MAY_STOP_VIS;
2742 }
2743 }
2744 }
2745 else
2746 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 if (jump_flags)
2749 {
2750 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002751 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 setcursor();
2753 out_flush(); /* Update before showing popup menu */
2754 }
2755# ifdef FEAT_MENU
2756 gui_show_popupmenu();
2757# endif
2758 return (jump_flags & CURSOR_MOVED) != 0;
2759 }
2760 else
2761 return FALSE;
2762#else
2763 return FALSE;
2764#endif
2765 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002766 if (which_button == MOUSE_LEFT
2767 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
2769 which_button = MOUSE_RIGHT;
2770 mod_mask &= ~MOD_MASK_SHIFT;
2771 }
2772 }
2773
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if ((State & (NORMAL | INSERT))
2775 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2776 {
2777 if (which_button == MOUSE_LEFT)
2778 {
2779 if (is_click)
2780 {
2781 /* stop Visual mode for a left click in a window, but not when
2782 * on a status line */
2783 if (VIsual_active)
2784 jump_flags |= MOUSE_MAY_STOP_VIS;
2785 }
2786 else if (mouse_has(MOUSE_VISUAL))
2787 jump_flags |= MOUSE_MAY_VIS;
2788 }
2789 else if (which_button == MOUSE_RIGHT)
2790 {
2791 if (is_click && VIsual_active)
2792 {
2793 /*
2794 * Remember the start and end of visual before moving the
2795 * cursor.
2796 */
2797 if (lt(curwin->w_cursor, VIsual))
2798 {
2799 start_visual = curwin->w_cursor;
2800 end_visual = VIsual;
2801 }
2802 else
2803 {
2804 start_visual = VIsual;
2805 end_visual = curwin->w_cursor;
2806 }
2807 }
2808 jump_flags |= MOUSE_FOCUS;
2809 if (mouse_has(MOUSE_VISUAL))
2810 jump_flags |= MOUSE_MAY_VIS;
2811 }
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 /*
2815 * If an operator is pending, ignore all drags and releases until the
2816 * next mouse click.
2817 */
2818 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2819 {
2820 got_click = FALSE;
2821 oap->motion_type = MCHAR;
2822 }
2823
2824 /* When releasing the button let jump_to_mouse() know. */
2825 if (!is_click && !is_drag)
2826 jump_flags |= MOUSE_RELEASED;
2827
2828 /*
2829 * JUMP!
2830 */
2831 jump_flags = jump_to_mouse(jump_flags,
2832 oap == NULL ? NULL : &(oap->inclusive), which_button);
2833 moved = (jump_flags & CURSOR_MOVED);
2834 in_status_line = (jump_flags & IN_STATUS_LINE);
2835#ifdef FEAT_VERTSPLIT
2836 in_sep_line = (jump_flags & IN_SEP_LINE);
2837#endif
2838
2839#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002840 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2842 {
2843 int key = KEY2TERMCAP1(c);
2844
2845 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2846 || key == (int)KE_RIGHTRELEASE)
2847 netbeans_button_release(which_button);
2848 }
2849#endif
2850
2851 /* When jumping to another window, clear a pending operator. That's a bit
2852 * friendlier than beeping and not jumping to that window. */
2853 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2854 clearop(oap);
2855
2856#ifdef FEAT_FOLDING
2857 if (mod_mask == 0
2858 && !is_drag
2859 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2860 && which_button == MOUSE_LEFT)
2861 {
2862 /* open or close a fold at this line */
2863 if (jump_flags & MOUSE_FOLD_OPEN)
2864 openFold(curwin->w_cursor.lnum, 1L);
2865 else
2866 closeFold(curwin->w_cursor.lnum, 1L);
2867 /* don't move the cursor if still in the same window */
2868 if (curwin == old_curwin)
2869 curwin->w_cursor = save_cursor;
2870 }
2871#endif
2872
2873#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2874 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2875 {
2876 clip_modeless(which_button, is_click, is_drag);
2877 return FALSE;
2878 }
2879#endif
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002882 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (VIsual_active && is_drag && p_so)
2884 {
2885 /* In the very first line, allow scrolling one line */
2886 if (mouse_row == 0)
2887 mouse_dragging = 2;
2888 else
2889 mouse_dragging = 1;
2890 }
2891
2892 /* When dragging the mouse above the window, scroll down. */
2893 if (is_drag && mouse_row < 0 && !in_status_line)
2894 {
2895 scroll_redraw(FALSE, 1L);
2896 mouse_row = 0;
2897 }
2898
2899 if (start_visual.lnum) /* right click in visual mode */
2900 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002901 /* When ALT is pressed make Visual mode blockwise. */
2902 if (mod_mask & MOD_MASK_ALT)
2903 VIsual_mode = Ctrl_V;
2904
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 /*
2906 * In Visual-block mode, divide the area in four, pick up the corner
2907 * that is in the quarter that the cursor is in.
2908 */
2909 if (VIsual_mode == Ctrl_V)
2910 {
2911 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2912 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2913 end_visual.col = leftcol;
2914 else
2915 end_visual.col = rightcol;
2916 if (curwin->w_cursor.lnum <
2917 (start_visual.lnum + end_visual.lnum) / 2)
2918 end_visual.lnum = end_visual.lnum;
2919 else
2920 end_visual.lnum = start_visual.lnum;
2921
2922 /* move VIsual to the right column */
2923 start_visual = curwin->w_cursor; /* save the cursor pos */
2924 curwin->w_cursor = end_visual;
2925 coladvance(end_visual.col);
2926 VIsual = curwin->w_cursor;
2927 curwin->w_cursor = start_visual; /* restore the cursor */
2928 }
2929 else
2930 {
2931 /*
2932 * If the click is before the start of visual, change the start.
2933 * If the click is after the end of visual, change the end. If
2934 * the click is inside the visual, change the closest side.
2935 */
2936 if (lt(curwin->w_cursor, start_visual))
2937 VIsual = end_visual;
2938 else if (lt(end_visual, curwin->w_cursor))
2939 VIsual = start_visual;
2940 else
2941 {
2942 /* In the same line, compare column number */
2943 if (end_visual.lnum == start_visual.lnum)
2944 {
2945 if (curwin->w_cursor.col - start_visual.col >
2946 end_visual.col - curwin->w_cursor.col)
2947 VIsual = start_visual;
2948 else
2949 VIsual = end_visual;
2950 }
2951
2952 /* In different lines, compare line number */
2953 else
2954 {
2955 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2956 (end_visual.lnum - curwin->w_cursor.lnum);
2957
2958 if (diff > 0) /* closest to end */
2959 VIsual = start_visual;
2960 else if (diff < 0) /* closest to start */
2961 VIsual = end_visual;
2962 else /* in the middle line */
2963 {
2964 if (curwin->w_cursor.col <
2965 (start_visual.col + end_visual.col) / 2)
2966 VIsual = end_visual;
2967 else
2968 VIsual = start_visual;
2969 }
2970 }
2971 }
2972 }
2973 }
2974 /*
2975 * If Visual mode started in insert mode, execute "CTRL-O"
2976 */
2977 else if ((State & INSERT) && VIsual_active)
2978 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
2980 /*
2981 * Middle mouse click: Put text before cursor.
2982 */
2983 if (which_button == MOUSE_MIDDLE)
2984 {
2985#ifdef FEAT_CLIPBOARD
2986 if (clip_star.available && regname == 0)
2987 regname = '*';
2988#endif
2989 if (yank_register_mline(regname))
2990 {
2991 if (mouse_past_bottom)
2992 dir = FORWARD;
2993 }
2994 else if (mouse_past_eol)
2995 dir = FORWARD;
2996
2997 if (fixindent)
2998 {
2999 c1 = (dir == BACKWARD) ? '[' : ']';
3000 c2 = 'p';
3001 }
3002 else
3003 {
3004 c1 = (dir == FORWARD) ? 'p' : 'P';
3005 c2 = NUL;
3006 }
3007 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3008
3009 /*
3010 * Remember where the paste started, so in edit() Insstart can be set
3011 * to this position
3012 */
3013 if (restart_edit != 0)
3014 where_paste_started = curwin->w_cursor;
3015 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3016 }
3017
3018#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3019 /*
3020 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3021 * error under the mouse pointer.
3022 */
3023 else if (((mod_mask & MOD_MASK_CTRL)
3024 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3025 && bt_quickfix(curbuf))
3026 {
3027 if (State & INSERT)
3028 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003029 if (curwin->w_llist_ref == NULL) /* quickfix window */
3030 stuffReadbuff((char_u *)":.cc\n");
3031 else /* location list window */
3032 stuffReadbuff((char_u *)":.ll\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 got_click = FALSE; /* ignore drag&release now */
3034 }
3035#endif
3036
3037 /*
3038 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3039 * under the mouse pointer.
3040 */
3041 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3042 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3043 {
3044 if (State & INSERT)
3045 stuffcharReadbuff(Ctrl_O);
3046 stuffcharReadbuff(Ctrl_RSB);
3047 got_click = FALSE; /* ignore drag&release now */
3048 }
3049
3050 /*
3051 * Shift-Mouse click searches for the next occurrence of the word under
3052 * the mouse pointer
3053 */
3054 else if ((mod_mask & MOD_MASK_SHIFT))
3055 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003056 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 stuffcharReadbuff(Ctrl_O);
3058 if (which_button == MOUSE_LEFT)
3059 stuffcharReadbuff('*');
3060 else /* MOUSE_RIGHT */
3061 stuffcharReadbuff('#');
3062 }
3063
3064 /* Handle double clicks, unless on status line */
3065 else if (in_status_line)
3066 {
3067#ifdef FEAT_MOUSESHAPE
3068 if ((is_drag || is_click) && !drag_status_line)
3069 {
3070 drag_status_line = TRUE;
3071 update_mouseshape(-1);
3072 }
3073#endif
3074 }
3075#ifdef FEAT_VERTSPLIT
3076 else if (in_sep_line)
3077 {
3078# ifdef FEAT_MOUSESHAPE
3079 if ((is_drag || is_click) && !drag_sep_line)
3080 {
3081 drag_sep_line = TRUE;
3082 update_mouseshape(-1);
3083 }
3084# endif
3085 }
3086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3088 && mouse_has(MOUSE_VISUAL))
3089 {
3090 if (is_click || !VIsual_active)
3091 {
3092 if (VIsual_active)
3093 orig_cursor = VIsual;
3094 else
3095 {
3096 check_visual_highlight();
3097 VIsual = curwin->w_cursor;
3098 orig_cursor = VIsual;
3099 VIsual_active = TRUE;
3100 VIsual_reselect = TRUE;
3101 /* start Select mode if 'selectmode' contains "mouse" */
3102 may_start_select('o');
3103 setmouse();
3104 }
3105 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003106 {
3107 /* Double click with ALT pressed makes it blockwise. */
3108 if (mod_mask & MOD_MASK_ALT)
3109 VIsual_mode = Ctrl_V;
3110 else
3111 VIsual_mode = 'v';
3112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3114 VIsual_mode = 'V';
3115 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3116 VIsual_mode = Ctrl_V;
3117#ifdef FEAT_CLIPBOARD
3118 /* Make sure the clipboard gets updated. Needed because start and
3119 * end may still be the same, and the selection needs to be owned */
3120 clip_star.vmode = NUL;
3121#endif
3122 }
3123 /*
3124 * A double click selects a word or a block.
3125 */
3126 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3127 {
3128 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003129 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 if (is_click)
3132 {
3133 /* If the character under the cursor (skipping white space) is
3134 * not a word character, try finding a match and select a (),
3135 * {}, [], #if/#endif, etc. block. */
3136 end_visual = curwin->w_cursor;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003137 while (gc = gchar_pos(&end_visual), vim_iswhite(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 inc(&end_visual);
3139 if (oap != NULL)
3140 oap->motion_type = MCHAR;
3141 if (oap != NULL
3142 && VIsual_mode == 'v'
3143 && !vim_iswordc(gchar_pos(&end_visual))
3144 && equalpos(curwin->w_cursor, VIsual)
3145 && (pos = findmatch(oap, NUL)) != NULL)
3146 {
3147 curwin->w_cursor = *pos;
3148 if (oap->motion_type == MLINE)
3149 VIsual_mode = 'V';
3150 else if (*p_sel == 'e')
3151 {
3152 if (lt(curwin->w_cursor, VIsual))
3153 ++VIsual.col;
3154 else
3155 ++curwin->w_cursor.col;
3156 }
3157 }
3158 }
3159
3160 if (pos == NULL && (is_click || is_drag))
3161 {
3162 /* When not found a match or when dragging: extend to include
3163 * a word. */
3164 if (lt(curwin->w_cursor, orig_cursor))
3165 {
3166 find_start_of_word(&curwin->w_cursor);
3167 find_end_of_word(&VIsual);
3168 }
3169 else
3170 {
3171 find_start_of_word(&VIsual);
3172 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3173#ifdef FEAT_MBYTE
3174 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003175 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#else
3177 ++curwin->w_cursor.col;
3178#endif
3179 find_end_of_word(&curwin->w_cursor);
3180 }
3181 }
3182 curwin->w_set_curswant = TRUE;
3183 }
3184 if (is_click)
3185 redraw_curbuf_later(INVERTED); /* update the inversion */
3186 }
3187 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003188 {
3189 if (mod_mask & MOD_MASK_ALT)
3190 VIsual_mode = Ctrl_V;
3191 else
3192 VIsual_mode = 'v';
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003196 if ((!VIsual_active && old_active && mode_displayed)
3197 || (VIsual_active && p_smd && msg_silent == 0
3198 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 return moved;
3202}
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204/*
3205 * Move "pos" back to the start of the word it's in.
3206 */
3207 static void
3208find_start_of_word(pos)
3209 pos_T *pos;
3210{
3211 char_u *line;
3212 int cclass;
3213 int col;
3214
3215 line = ml_get(pos->lnum);
3216 cclass = get_mouse_class(line + pos->col);
3217
3218 while (pos->col > 0)
3219 {
3220 col = pos->col - 1;
3221#ifdef FEAT_MBYTE
3222 col -= (*mb_head_off)(line, line + col);
3223#endif
3224 if (get_mouse_class(line + col) != cclass)
3225 break;
3226 pos->col = col;
3227 }
3228}
3229
3230/*
3231 * Move "pos" forward to the end of the word it's in.
3232 * When 'selection' is "exclusive", the position is just after the word.
3233 */
3234 static void
3235find_end_of_word(pos)
3236 pos_T *pos;
3237{
3238 char_u *line;
3239 int cclass;
3240 int col;
3241
3242 line = ml_get(pos->lnum);
3243 if (*p_sel == 'e' && pos->col > 0)
3244 {
3245 --pos->col;
3246#ifdef FEAT_MBYTE
3247 pos->col -= (*mb_head_off)(line, line + pos->col);
3248#endif
3249 }
3250 cclass = get_mouse_class(line + pos->col);
3251 while (line[pos->col] != NUL)
3252 {
3253#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003254 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#else
3256 col = pos->col + 1;
3257#endif
3258 if (get_mouse_class(line + col) != cclass)
3259 {
3260 if (*p_sel == 'e')
3261 pos->col = col;
3262 break;
3263 }
3264 pos->col = col;
3265 }
3266}
3267
3268/*
3269 * Get class of a character for selection: same class means same word.
3270 * 0: blank
3271 * 1: punctuation groups
3272 * 2: normal word character
3273 * >2: multi-byte word character.
3274 */
3275 static int
3276get_mouse_class(p)
3277 char_u *p;
3278{
3279 int c;
3280
3281#ifdef FEAT_MBYTE
3282 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3283 return mb_get_class(p);
3284#endif
3285
3286 c = *p;
3287 if (c == ' ' || c == '\t')
3288 return 0;
3289
3290 if (vim_iswordc(c))
3291 return 2;
3292
3293 /*
3294 * There are a few special cases where we want certain combinations of
3295 * characters to be considered as a single word. These are things like
3296 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003297 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 */
3299 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3300 return 1;
3301 return c;
3302}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#endif /* FEAT_MOUSE */
3304
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305/*
3306 * Check if highlighting for visual mode is possible, give a warning message
3307 * if not.
3308 */
3309 void
3310check_visual_highlight()
3311{
3312 static int did_check = FALSE;
3313
3314 if (full_screen)
3315 {
3316 if (!did_check && hl_attr(HLF_V) == 0)
3317 MSG(_("Warning: terminal cannot highlight"));
3318 did_check = TRUE;
3319 }
3320}
3321
3322/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003323 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * This function should ALWAYS be called to end Visual mode, except from
3325 * do_pending_operator().
3326 */
3327 void
3328end_visual_mode()
3329{
3330#ifdef FEAT_CLIPBOARD
3331 /*
3332 * If we are using the clipboard, then remember what was selected in case
3333 * we need to paste it somewhere while we still own the selection.
3334 * Only do this when the clipboard is already owned. Don't want to grab
3335 * the selection when hitting ESC.
3336 */
3337 if (clip_star.available && clip_star.owned)
3338 clip_auto_select();
3339#endif
3340
3341 VIsual_active = FALSE;
3342#ifdef FEAT_MOUSE
3343 setmouse();
3344 mouse_dragging = 0;
3345#endif
3346
3347 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003348 curbuf->b_visual.vi_mode = VIsual_mode;
3349 curbuf->b_visual.vi_start = VIsual;
3350 curbuf->b_visual.vi_end = curwin->w_cursor;
3351 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#ifdef FEAT_EVAL
3353 curbuf->b_visual_mode_eval = VIsual_mode;
3354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#ifdef FEAT_VIRTUALEDIT
3356 if (!virtual_active())
3357 curwin->w_cursor.coladd = 0;
3358#endif
3359
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003360 if (mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 clear_cmdline = TRUE; /* unshow visual mode later */
3362#ifdef FEAT_CMDL_INFO
3363 else
3364 clear_showcmd();
3365#endif
3366
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003367 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368}
3369
3370/*
3371 * Reset VIsual_active and VIsual_reselect.
3372 */
3373 void
3374reset_VIsual_and_resel()
3375{
3376 if (VIsual_active)
3377 {
3378 end_visual_mode();
3379 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3380 }
3381 VIsual_reselect = FALSE;
3382}
3383
3384/*
3385 * Reset VIsual_active and VIsual_reselect if it's set.
3386 */
3387 void
3388reset_VIsual()
3389{
3390 if (VIsual_active)
3391 {
3392 end_visual_mode();
3393 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3394 VIsual_reselect = FALSE;
3395 }
3396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003398#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399static int find_is_eval_item __ARGS((char_u *ptr, int *colp, int *nbp, int dir));
3400
3401/*
3402 * Check for a balloon-eval special item to include when searching for an
3403 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3404 * Returns TRUE if the character at "*ptr" should be included.
3405 * "dir" is FORWARD or BACKWARD, the direction of searching.
3406 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3407 * "bnp" points to a counter for square brackets.
3408 */
3409 static int
3410find_is_eval_item(ptr, colp, bnp, dir)
3411 char_u *ptr;
3412 int *colp;
3413 int *bnp;
3414 int dir;
3415{
3416 /* Accept everything inside []. */
3417 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3418 ++*bnp;
3419 if (*bnp > 0)
3420 {
3421 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3422 --*bnp;
3423 return TRUE;
3424 }
3425
3426 /* skip over "s.var" */
3427 if (*ptr == '.')
3428 return TRUE;
3429
3430 /* two-character item: s->var */
3431 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3432 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3433 {
3434 *colp += dir;
3435 return TRUE;
3436 }
3437 return FALSE;
3438}
3439#endif
3440
3441/*
3442 * Find the identifier under or to the right of the cursor.
3443 * "find_type" can have one of three values:
3444 * FIND_IDENT: find an identifier (keyword)
3445 * FIND_STRING: find any non-white string
3446 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003447 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 *
3449 * There are three steps:
3450 * 1. Search forward for the start of an identifier/string. Doesn't move if
3451 * already on one.
3452 * 2. Search backward for the start of this identifier/string.
3453 * This doesn't match the real Vi but I like it a little better and it
3454 * shouldn't bother anyone.
3455 * 3. Search forward to the end of this identifier/string.
3456 * When FIND_IDENT isn't defined, we backup until a blank.
3457 *
3458 * Returns the length of the string, or zero if no string is found.
3459 * If a string is found, a pointer to the string is put in "*string". This
3460 * string is not always NUL terminated.
3461 */
3462 int
3463find_ident_under_cursor(string, find_type)
3464 char_u **string;
3465 int find_type;
3466{
3467 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3468 curwin->w_cursor.col, string, find_type);
3469}
3470
3471/*
3472 * Like find_ident_under_cursor(), but for any window and any position.
3473 * However: Uses 'iskeyword' from the current window!.
3474 */
3475 int
3476find_ident_at_pos(wp, lnum, startcol, string, find_type)
3477 win_T *wp;
3478 linenr_T lnum;
3479 colnr_T startcol;
3480 char_u **string;
3481 int find_type;
3482{
3483 char_u *ptr;
3484 int col = 0; /* init to shut up GCC */
3485 int i;
3486#ifdef FEAT_MBYTE
3487 int this_class = 0;
3488 int prev_class;
3489 int prevcol;
3490#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 int bn = 0; /* bracket nesting */
3493#endif
3494
3495 /*
3496 * if i == 0: try to find an identifier
3497 * if i == 1: try to find any non-white string
3498 */
3499 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3500 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3501 {
3502 /*
3503 * 1. skip to start of identifier/string
3504 */
3505 col = startcol;
3506#ifdef FEAT_MBYTE
3507 if (has_mbyte)
3508 {
3509 while (ptr[col] != NUL)
3510 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 /* Stop at a ']' to evaluate "a[x]". */
3513 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3514 break;
3515# endif
3516 this_class = mb_get_class(ptr + col);
3517 if (this_class != 0 && (i == 1 || this_class != 1))
3518 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003519 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 }
3522 else
3523#endif
3524 while (ptr[col] != NUL
3525 && (i == 0 ? !vim_iswordc(ptr[col]) : vim_iswhite(ptr[col]))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
3528# endif
3529 )
3530 ++col;
3531
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003532#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 /* When starting on a ']' count it, so that we include the '['. */
3534 bn = ptr[col] == ']';
3535#endif
3536
3537 /*
3538 * 2. Back up to start of identifier/string.
3539 */
3540#ifdef FEAT_MBYTE
3541 if (has_mbyte)
3542 {
3543 /* Remember class of character under cursor. */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3546 this_class = mb_get_class((char_u *)"a");
3547 else
3548# endif
3549 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003550 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
3552 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3553 prev_class = mb_get_class(ptr + prevcol);
3554 if (this_class != prev_class
3555 && (i == 0
3556 || prev_class == 0
3557 || (find_type & FIND_IDENT))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 && (!(find_type & FIND_EVAL)
3560 || prevcol == 0
3561 || !find_is_eval_item(ptr + prevcol, &prevcol,
3562 &bn, BACKWARD))
3563# endif
3564 )
3565 break;
3566 col = prevcol;
3567 }
3568
3569 /* If we don't want just any old string, or we've found an
3570 * identifier, stop searching. */
3571 if (this_class > 2)
3572 this_class = 2;
3573 if (!(find_type & FIND_STRING) || this_class == 2)
3574 break;
3575 }
3576 else
3577#endif
3578 {
3579 while (col > 0
3580 && ((i == 0
3581 ? vim_iswordc(ptr[col - 1])
3582 : (!vim_iswhite(ptr[col - 1])
3583 && (!(find_type & FIND_IDENT)
3584 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003585#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 || ((find_type & FIND_EVAL)
3587 && col > 1
3588 && find_is_eval_item(ptr + col - 1, &col,
3589 &bn, BACKWARD))
3590#endif
3591 ))
3592 --col;
3593
3594 /* If we don't want just any old string, or we've found an
3595 * identifier, stop searching. */
3596 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3597 break;
3598 }
3599 }
3600
3601 if (ptr[col] == NUL || (i == 0 && (
3602#ifdef FEAT_MBYTE
3603 has_mbyte ? this_class != 2 :
3604#endif
3605 !vim_iswordc(ptr[col]))))
3606 {
3607 /*
3608 * didn't find an identifier or string
3609 */
3610 if (find_type & FIND_STRING)
3611 EMSG(_("E348: No string under cursor"));
3612 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003613 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 return 0;
3615 }
3616 ptr += col;
3617 *string = ptr;
3618
3619 /*
3620 * 3. Find the end if the identifier/string.
3621 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003622#if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 bn = 0;
3624 startcol -= col;
3625#endif
3626 col = 0;
3627#ifdef FEAT_MBYTE
3628 if (has_mbyte)
3629 {
3630 /* Search for point of changing multibyte character class. */
3631 this_class = mb_get_class(ptr);
3632 while (ptr[col] != NUL
3633 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3634 : mb_get_class(ptr + col) != 0)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003635# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 || ((find_type & FIND_EVAL)
3637 && col <= (int)startcol
3638 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3639# endif
3640 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003641 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 else
3644#endif
3645 while ((i == 0 ? vim_iswordc(ptr[col])
3646 : (ptr[col] != NUL && !vim_iswhite(ptr[col])))
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003647# if defined(FEAT_BEVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 || ((find_type & FIND_EVAL)
3649 && col <= (int)startcol
3650 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
3651# endif
3652 )
3653 {
3654 ++col;
3655 }
3656
3657 return col;
3658}
3659
3660/*
3661 * Prepare for redo of a normal command.
3662 */
3663 static void
3664prep_redo_cmd(cap)
3665 cmdarg_T *cap;
3666{
3667 prep_redo(cap->oap->regname, cap->count0,
3668 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3669}
3670
3671/*
3672 * Prepare for redo of any command.
3673 * Note that only the last argument can be a multi-byte char.
3674 */
3675 static void
3676prep_redo(regname, num, cmd1, cmd2, cmd3, cmd4, cmd5)
3677 int regname;
3678 long num;
3679 int cmd1;
3680 int cmd2;
3681 int cmd3;
3682 int cmd4;
3683 int cmd5;
3684{
3685 ResetRedobuff();
3686 if (regname != 0) /* yank from specified buffer */
3687 {
3688 AppendCharToRedobuff('"');
3689 AppendCharToRedobuff(regname);
3690 }
3691 if (num)
3692 AppendNumberToRedobuff(num);
3693
3694 if (cmd1 != NUL)
3695 AppendCharToRedobuff(cmd1);
3696 if (cmd2 != NUL)
3697 AppendCharToRedobuff(cmd2);
3698 if (cmd3 != NUL)
3699 AppendCharToRedobuff(cmd3);
3700 if (cmd4 != NUL)
3701 AppendCharToRedobuff(cmd4);
3702 if (cmd5 != NUL)
3703 AppendCharToRedobuff(cmd5);
3704}
3705
3706/*
3707 * check for operator active and clear it
3708 *
3709 * return TRUE if operator was active
3710 */
3711 static int
3712checkclearop(oap)
3713 oparg_T *oap;
3714{
3715 if (oap->op_type == OP_NOP)
3716 return FALSE;
3717 clearopbeep(oap);
3718 return TRUE;
3719}
3720
3721/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003722 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003724 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 */
3726 static int
3727checkclearopq(oap)
3728 oparg_T *oap;
3729{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003730 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FALSE;
3732 clearopbeep(oap);
3733 return TRUE;
3734}
3735
3736 static void
3737clearop(oap)
3738 oparg_T *oap;
3739{
3740 oap->op_type = OP_NOP;
3741 oap->regname = 0;
3742 oap->motion_force = NUL;
3743 oap->use_reg_one = FALSE;
3744}
3745
3746 static void
3747clearopbeep(oap)
3748 oparg_T *oap;
3749{
3750 clearop(oap);
3751 beep_flush();
3752}
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754/*
3755 * Remove the shift modifier from a special key.
3756 */
3757 static void
3758unshift_special(cap)
3759 cmdarg_T *cap;
3760{
3761 switch (cap->cmdchar)
3762 {
3763 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3764 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3765 case K_S_UP: cap->cmdchar = K_UP; break;
3766 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3767 case K_S_HOME: cap->cmdchar = K_HOME; break;
3768 case K_S_END: cap->cmdchar = K_END; break;
3769 }
3770 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3774/*
3775 * Routines for displaying a partly typed command
3776 */
3777
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003778#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779static char_u showcmd_buf[SHOWCMD_BUFLEN];
3780static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3781static int showcmd_is_clear = TRUE;
3782static int showcmd_visual = FALSE;
3783
3784static void display_showcmd __ARGS((void));
3785
3786 void
3787clear_showcmd()
3788{
3789 if (!p_sc)
3790 return;
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (VIsual_active && !char_avail())
3793 {
Bram Moolenaar81d00072009-04-29 15:41:40 +00003794 int cursor_bot = lt(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 long lines;
3796 colnr_T leftcol, rightcol;
3797 linenr_T top, bot;
3798
3799 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003800 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 top = VIsual.lnum;
3803 bot = curwin->w_cursor.lnum;
3804 }
3805 else
3806 {
3807 top = curwin->w_cursor.lnum;
3808 bot = VIsual.lnum;
3809 }
3810# ifdef FEAT_FOLDING
3811 /* Include closed folds as a whole. */
3812 hasFolding(top, &top, NULL);
3813 hasFolding(bot, NULL, &bot);
3814# endif
3815 lines = bot - top + 1;
3816
3817 if (VIsual_mode == Ctrl_V)
3818 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003819# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003820 char_u *saved_sbr = p_sbr;
3821
3822 /* Make 'sbr' empty for a moment to get the correct size. */
3823 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003826# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003827 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3830 (long)(rightcol - leftcol + 1));
3831 }
3832 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3833 sprintf((char *)showcmd_buf, "%ld", lines);
3834 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003835 {
3836 char_u *s, *e;
3837 int l;
3838 int bytes = 0;
3839 int chars = 0;
3840
3841 if (cursor_bot)
3842 {
3843 s = ml_get_pos(&VIsual);
3844 e = ml_get_cursor();
3845 }
3846 else
3847 {
3848 s = ml_get_cursor();
3849 e = ml_get_pos(&VIsual);
3850 }
3851 while ((*p_sel != 'e') ? s <= e : s < e)
3852 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003853# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003854 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003855# else
3856 l = (*s == NUL) ? 0 : 1;
3857# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003858 if (l == 0)
3859 {
3860 ++bytes;
3861 ++chars;
3862 break; /* end of line */
3863 }
3864 bytes += l;
3865 ++chars;
3866 s += l;
3867 }
3868 if (bytes == chars)
3869 sprintf((char *)showcmd_buf, "%d", chars);
3870 else
3871 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3874 showcmd_visual = TRUE;
3875 }
3876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 showcmd_buf[0] = NUL;
3879 showcmd_visual = FALSE;
3880
3881 /* Don't actually display something if there is nothing to clear. */
3882 if (showcmd_is_clear)
3883 return;
3884 }
3885
3886 display_showcmd();
3887}
3888
3889/*
3890 * Add 'c' to string of shown command chars.
3891 * Return TRUE if output has been written (and setcursor() has been called).
3892 */
3893 int
3894add_to_showcmd(c)
3895 int c;
3896{
3897 char_u *p;
3898 int old_len;
3899 int extra_len;
3900 int overflow;
3901#if defined(FEAT_MOUSE)
3902 int i;
3903 static int ignore[] =
3904 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003905# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3907 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 K_IGNORE,
3910 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3911 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3912 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003913 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003915 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 0
3917 };
3918#endif
3919
Bram Moolenaar09df3122006-01-23 22:23:09 +00003920 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return FALSE;
3922
3923 if (showcmd_visual)
3924 {
3925 showcmd_buf[0] = NUL;
3926 showcmd_visual = FALSE;
3927 }
3928
3929#if defined(FEAT_MOUSE)
3930 /* Ignore keys that are scrollbar updates and mouse clicks */
3931 if (IS_SPECIAL(c))
3932 for (i = 0; ignore[i] != 0; ++i)
3933 if (ignore[i] == c)
3934 return FALSE;
3935#endif
3936
3937 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003938 if (*p == ' ')
3939 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 old_len = (int)STRLEN(showcmd_buf);
3941 extra_len = (int)STRLEN(p);
3942 overflow = old_len + extra_len - SHOWCMD_COLS;
3943 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003944 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3945 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 STRCAT(showcmd_buf, p);
3947
3948 if (char_avail())
3949 return FALSE;
3950
3951 display_showcmd();
3952
3953 return TRUE;
3954}
3955
3956 void
3957add_to_showcmd_c(c)
3958 int c;
3959{
3960 if (!add_to_showcmd(c))
3961 setcursor();
3962}
3963
3964/*
3965 * Delete 'len' characters from the end of the shown command.
3966 */
3967 static void
3968del_from_showcmd(len)
3969 int len;
3970{
3971 int old_len;
3972
3973 if (!p_sc)
3974 return;
3975
3976 old_len = (int)STRLEN(showcmd_buf);
3977 if (len > old_len)
3978 len = old_len;
3979 showcmd_buf[old_len - len] = NUL;
3980
3981 if (!char_avail())
3982 display_showcmd();
3983}
3984
3985/*
3986 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3987 * something and there is a partial mapping.
3988 */
3989 void
3990push_showcmd()
3991{
3992 if (p_sc)
3993 STRCPY(old_showcmd_buf, showcmd_buf);
3994}
3995
3996 void
3997pop_showcmd()
3998{
3999 if (!p_sc)
4000 return;
4001
4002 STRCPY(showcmd_buf, old_showcmd_buf);
4003
4004 display_showcmd();
4005}
4006
4007 static void
4008display_showcmd()
4009{
4010 int len;
4011
4012 cursor_off();
4013
4014 len = (int)STRLEN(showcmd_buf);
4015 if (len == 0)
4016 showcmd_is_clear = TRUE;
4017 else
4018 {
4019 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
4020 showcmd_is_clear = FALSE;
4021 }
4022
4023 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004024 * clear the rest of an old message by outputting up to SHOWCMD_COLS
4025 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 */
4027 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
4028
4029 setcursor(); /* put cursor back where it belongs */
4030}
4031#endif
4032
4033#ifdef FEAT_SCROLLBIND
4034/*
4035 * When "check" is FALSE, prepare for commands that scroll the window.
4036 * When "check" is TRUE, take care of scroll-binding after the window has
4037 * scrolled. Called from normal_cmd() and edit().
4038 */
4039 void
4040do_check_scrollbind(check)
4041 int check;
4042{
4043 static win_T *old_curwin = NULL;
4044 static linenr_T old_topline = 0;
4045#ifdef FEAT_DIFF
4046 static int old_topfill = 0;
4047#endif
4048 static buf_T *old_buf = NULL;
4049 static colnr_T old_leftcol = 0;
4050
4051 if (check && curwin->w_p_scb)
4052 {
4053 /* If a ":syncbind" command was just used, don't scroll, only reset
4054 * the values. */
4055 if (did_syncbind)
4056 did_syncbind = FALSE;
4057 else if (curwin == old_curwin)
4058 {
4059 /*
4060 * Synchronize other windows, as necessary according to
4061 * 'scrollbind'. Don't do this after an ":edit" command, except
4062 * when 'diff' is set.
4063 */
4064 if ((curwin->w_buffer == old_buf
4065#ifdef FEAT_DIFF
4066 || curwin->w_p_diff
4067#endif
4068 )
4069 && (curwin->w_topline != old_topline
4070#ifdef FEAT_DIFF
4071 || curwin->w_topfill != old_topfill
4072#endif
4073 || curwin->w_leftcol != old_leftcol))
4074 {
4075 check_scrollbind(curwin->w_topline - old_topline,
4076 (long)(curwin->w_leftcol - old_leftcol));
4077 }
4078 }
4079 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4080 {
4081 /*
4082 * When switching between windows, make sure that the relative
4083 * vertical offset is valid for the new window. The relative
4084 * offset is invalid whenever another 'scrollbind' window has
4085 * scrolled to a point that would force the current window to
4086 * scroll past the beginning or end of its buffer. When the
4087 * resync is performed, some of the other 'scrollbind' windows may
4088 * need to jump so that the current window's relative position is
4089 * visible on-screen.
4090 */
4091 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4092 }
4093 curwin->w_scbind_pos = curwin->w_topline;
4094 }
4095
4096 old_curwin = curwin;
4097 old_topline = curwin->w_topline;
4098#ifdef FEAT_DIFF
4099 old_topfill = curwin->w_topfill;
4100#endif
4101 old_buf = curwin->w_buffer;
4102 old_leftcol = curwin->w_leftcol;
4103}
4104
4105/*
4106 * Synchronize any windows that have "scrollbind" set, based on the
4107 * number of rows by which the current window has changed
4108 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4109 */
4110 void
4111check_scrollbind(topline_diff, leftcol_diff)
4112 linenr_T topline_diff;
4113 long leftcol_diff;
4114{
4115 int want_ver;
4116 int want_hor;
4117 win_T *old_curwin = curwin;
4118 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int old_VIsual_select = VIsual_select;
4120 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 colnr_T tgt_leftcol = curwin->w_leftcol;
4122 long topline;
4123 long y;
4124
4125 /*
4126 * check 'scrollopt' string for vertical and horizontal scroll options
4127 */
4128 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4129#ifdef FEAT_DIFF
4130 want_ver |= old_curwin->w_p_diff;
4131#endif
4132 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4133
4134 /*
4135 * loop through the scrollbound windows and scroll accordingly
4136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 VIsual_select = VIsual_active = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 for (curwin = firstwin; curwin; curwin = curwin->w_next)
4139 {
4140 curbuf = curwin->w_buffer;
4141 /* skip original window and windows with 'noscrollbind' */
4142 if (curwin != old_curwin && curwin->w_p_scb)
4143 {
4144 /*
4145 * do the vertical scroll
4146 */
4147 if (want_ver)
4148 {
4149#ifdef FEAT_DIFF
4150 if (old_curwin->w_p_diff && curwin->w_p_diff)
4151 {
4152 diff_set_topline(old_curwin, curwin);
4153 }
4154 else
4155#endif
4156 {
4157 curwin->w_scbind_pos += topline_diff;
4158 topline = curwin->w_scbind_pos;
4159 if (topline > curbuf->b_ml.ml_line_count)
4160 topline = curbuf->b_ml.ml_line_count;
4161 if (topline < 1)
4162 topline = 1;
4163
4164 y = topline - curwin->w_topline;
4165 if (y > 0)
4166 scrollup(y, FALSE);
4167 else
4168 scrolldown(-y, FALSE);
4169 }
4170
4171 redraw_later(VALID);
4172 cursor_correct();
4173#ifdef FEAT_WINDOWS
4174 curwin->w_redr_status = TRUE;
4175#endif
4176 }
4177
4178 /*
4179 * do the horizontal scroll
4180 */
4181 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4182 {
4183 curwin->w_leftcol = tgt_leftcol;
4184 leftcol_changed();
4185 }
4186 }
4187 }
4188
4189 /*
4190 * reset current-window
4191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 VIsual_select = old_VIsual_select;
4193 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 curwin = old_curwin;
4195 curbuf = old_curbuf;
4196}
4197#endif /* #ifdef FEAT_SCROLLBIND */
4198
4199/*
4200 * Command character that's ignored.
4201 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004202 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 static void
4205nv_ignore(cap)
4206 cmdarg_T *cap;
4207{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004208 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209}
4210
4211/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004212 * Command character that doesn't do anything, but unlike nv_ignore() does
4213 * start edit(). Used for "startinsert" executed while starting up.
4214 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004215 static void
4216nv_nop(cap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004217 cmdarg_T *cap UNUSED;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004218{
4219}
4220
4221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 * Command character doesn't exist.
4223 */
4224 static void
4225nv_error(cap)
4226 cmdarg_T *cap;
4227{
4228 clearopbeep(cap->oap);
4229}
4230
4231/*
4232 * <Help> and <F1> commands.
4233 */
4234 static void
4235nv_help(cap)
4236 cmdarg_T *cap;
4237{
4238 if (!checkclearopq(cap->oap))
4239 ex_help(NULL);
4240}
4241
4242/*
4243 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4244 */
4245 static void
4246nv_addsub(cap)
4247 cmdarg_T *cap;
4248{
4249 if (!checkclearopq(cap->oap)
4250 && do_addsub((int)cap->cmdchar, cap->count1) == OK)
4251 prep_redo_cmd(cap);
4252}
4253
4254/*
4255 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4256 */
4257 static void
4258nv_page(cap)
4259 cmdarg_T *cap;
4260{
4261 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004262 {
4263#ifdef FEAT_WINDOWS
4264 if (mod_mask & MOD_MASK_CTRL)
4265 {
4266 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4267 if (cap->arg == BACKWARD)
4268 goto_tabpage(-(int)cap->count1);
4269 else
4270 goto_tabpage((int)cap->count0);
4271 }
4272 else
4273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276}
4277
4278/*
4279 * Implementation of "gd" and "gD" command.
4280 */
4281 static void
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004282nv_gd(oap, nchar, thisblock)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004283 oparg_T *oap;
4284 int nchar;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004285 int thisblock; /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
4287 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 char_u *ptr;
4289
Bram Moolenaard9d30582005-05-18 22:10:28 +00004290 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004291 || find_decl(ptr, len, nchar == 'd', thisblock, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004293#ifdef FEAT_FOLDING
4294 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4295 foldOpenCursor();
4296#endif
4297}
4298
4299/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004300 * Search for variable declaration of "ptr[len]".
4301 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4302 * current file ("gD").
4303 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004304 * Return FAIL when not found.
4305 */
4306 int
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004307find_decl(ptr, len, locally, thisblock, searchflags)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004308 char_u *ptr;
4309 int len;
4310 int locally;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004311 int thisblock;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312 int searchflags; /* flags passed to searchit() */
4313{
4314 char_u *pat;
4315 pos_T old_pos;
4316 pos_T par_pos;
4317 pos_T found_pos;
4318 int t;
4319 int save_p_ws;
4320 int save_p_scs;
4321 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004322 int incll;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004323
4324 if ((pat = alloc(len + 7)) == NULL)
4325 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004326
4327 /* Put "\V" before the pattern to avoid that the special meaning of "."
4328 * and "~" causes trouble. */
4329 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4330 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 old_pos = curwin->w_cursor;
4332 save_p_ws = p_ws;
4333 save_p_scs = p_scs;
4334 p_ws = FALSE; /* don't wrap around end of file now */
4335 p_scs = FALSE; /* don't switch ignorecase off now */
4336
4337 /*
4338 * With "gD" go to line 1.
4339 * With "gd" Search back for the start of the current function, then go
4340 * back until a blank line. If this fails go to line 1.
4341 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004342 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
4344 setpcmark(); /* Set in findpar() otherwise */
4345 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004346 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 else
4349 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004350 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4352 --curwin->w_cursor.lnum;
4353 }
4354 curwin->w_cursor.col = 0;
4355
4356 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004357 clearpos(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004358 for (;;)
4359 {
4360 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaar76929292008-01-06 19:07:36 +00004361 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004362 if (curwin->w_cursor.lnum >= old_pos.lnum)
4363 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004364
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004365 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004366 {
4367 pos_T *pos;
4368
4369 /* Check that the block the match is in doesn't end before the
4370 * position where we started the search from. */
4371 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4372 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4373 && pos->lnum < old_pos.lnum)
4374 continue;
4375 }
4376
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 if (t == FAIL)
4378 {
4379 /* If we previously found a valid position, use it. */
4380 if (found_pos.lnum != 0)
4381 {
4382 curwin->w_cursor = found_pos;
4383 t = OK;
4384 }
4385 break;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004389 {
4390 /* Ignore this line, continue at start of next line. */
4391 ++curwin->w_cursor.lnum;
4392 curwin->w_cursor.col = 0;
4393 continue;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#endif
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004396 if (!locally) /* global search: use first match found */
4397 break;
4398 if (curwin->w_cursor.lnum >= par_pos.lnum)
4399 {
4400 /* If we previously found a valid position, use it. */
4401 if (found_pos.lnum != 0)
4402 curwin->w_cursor = found_pos;
4403 break;
4404 }
4405
4406 /* For finding a local variable and the match is before the "{" search
4407 * to find a later match. For K&R style function declarations this
4408 * skips the function header without types. */
4409 found_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004411
4412 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004414 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 curwin->w_cursor = old_pos;
4416 }
4417 else
4418 {
4419 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /* "n" searches forward now */
4421 reset_search_dir();
4422 }
4423
4424 vim_free(pat);
4425 p_ws = save_p_ws;
4426 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004427
4428 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429}
4430
4431/*
4432 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4433 * lines rather than lines in the file.
4434 * 'dist' must be positive.
4435 *
4436 * Return OK if able to move cursor, FAIL otherwise.
4437 */
4438 static int
4439nv_screengo(oap, dir, dist)
4440 oparg_T *oap;
4441 int dir;
4442 long dist;
4443{
4444 int linelen = linetabsize(ml_get_curline());
4445 int retval = OK;
4446 int atend = FALSE;
4447 int n;
4448 int col_off1; /* margin offset for first screen line */
4449 int col_off2; /* margin offset for wrapped screen line */
4450 int width1; /* text width for first screen line */
4451 int width2; /* test width for wrapped screen line */
4452
4453 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004454 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456 col_off1 = curwin_col_off();
4457 col_off2 = col_off1 - curwin_col_off2();
4458 width1 = W_WIDTH(curwin) - col_off1;
4459 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004460 if (width2 == 0)
4461 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463#ifdef FEAT_VERTSPLIT
4464 if (curwin->w_width != 0)
4465 {
4466#endif
4467 /*
4468 * Instead of sticking at the last character of the buffer line we
4469 * try to stick in the last column of the screen.
4470 */
4471 if (curwin->w_curswant == MAXCOL)
4472 {
4473 atend = TRUE;
4474 validate_virtcol();
4475 if (width1 <= 0)
4476 curwin->w_curswant = 0;
4477 else
4478 {
4479 curwin->w_curswant = width1 - 1;
4480 if (curwin->w_virtcol > curwin->w_curswant)
4481 curwin->w_curswant += ((curwin->w_virtcol
4482 - curwin->w_curswant - 1) / width2 + 1) * width2;
4483 }
4484 }
4485 else
4486 {
4487 if (linelen > width1)
4488 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4489 else
4490 n = width1;
4491 if (curwin->w_curswant > (colnr_T)n + 1)
4492 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4493 * width2;
4494 }
4495
4496 while (dist--)
4497 {
4498 if (dir == BACKWARD)
4499 {
4500 if ((long)curwin->w_curswant >= width2)
4501 /* move back within line */
4502 curwin->w_curswant -= width2;
4503 else
4504 {
4505 /* to previous line */
4506 if (curwin->w_cursor.lnum == 1)
4507 {
4508 retval = FAIL;
4509 break;
4510 }
4511 --curwin->w_cursor.lnum;
4512#ifdef FEAT_FOLDING
4513 /* Move to the start of a closed fold. Don't do that when
4514 * 'foldopen' contains "all": it will open in a moment. */
4515 if (!(fdo_flags & FDO_ALL))
4516 (void)hasFolding(curwin->w_cursor.lnum,
4517 &curwin->w_cursor.lnum, NULL);
4518#endif
4519 linelen = linetabsize(ml_get_curline());
4520 if (linelen > width1)
4521 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4522 + 1) * width2;
4523 }
4524 }
4525 else /* dir == FORWARD */
4526 {
4527 if (linelen > width1)
4528 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4529 else
4530 n = width1;
4531 if (curwin->w_curswant + width2 < (colnr_T)n)
4532 /* move forward within line */
4533 curwin->w_curswant += width2;
4534 else
4535 {
4536 /* to next line */
4537#ifdef FEAT_FOLDING
4538 /* Move to the end of a closed fold. */
4539 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4540 &curwin->w_cursor.lnum);
4541#endif
4542 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4543 {
4544 retval = FAIL;
4545 break;
4546 }
4547 curwin->w_cursor.lnum++;
4548 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004549 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 }
4552 }
4553#ifdef FEAT_VERTSPLIT
4554 }
4555#endif
4556
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004557 if (virtual_active() && atend)
4558 coladvance(MAXCOL);
4559 else
4560 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4563 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4564 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004565 colnr_T virtcol;
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * Check for landing on a character that got split at the end of the
4569 * last line. We want to advance a screenline, not end up in the same
4570 * screenline or move two screenlines.
4571 */
4572 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004573 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004574# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004575 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4576 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004577# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004578
4579 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 && (curwin->w_curswant < (colnr_T)width1
4581 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4582 : ((curwin->w_curswant - width1) % width2
4583 > (colnr_T)width2 / 2)))
4584 --curwin->w_cursor.col;
4585 }
4586#endif
4587
4588 if (atend)
4589 curwin->w_curswant = MAXCOL; /* stick in the last column */
4590
4591 return retval;
4592}
4593
4594#ifdef FEAT_MOUSE
4595/*
4596 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4597 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004598 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4599 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 */
4601 static void
4602nv_mousescroll(cap)
4603 cmdarg_T *cap;
4604{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004605# ifdef FEAT_WINDOWS
Bram Moolenaara5792f52005-11-23 21:25:05 +00004606 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004608 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 int row, col;
4611
4612 row = mouse_row;
4613 col = mouse_col;
4614
4615 /* find the window at the pointer coordinates */
4616 curwin = mouse_find_win(&row, &col);
4617 curbuf = curwin->w_buffer;
4618 }
4619# endif
4620
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004621 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004623 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4624 {
4625 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4626 }
4627 else
4628 {
4629 cap->count1 = 3;
4630 cap->count0 = 3;
4631 nv_scroll_line(cap);
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004634# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 else
4636 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004637 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4638 if (!curwin->w_p_wrap)
4639 {
4640 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004641
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004642 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4643 step = W_WIDTH(curwin);
4644 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4645 if (val < 0)
4646 val = 0;
4647
4648 gui_do_horiz_scroll(val, TRUE);
4649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004653# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 curwin->w_redr_status = TRUE;
4655
4656 curwin = old_curwin;
4657 curbuf = curwin->w_buffer;
4658# endif
4659}
4660
4661/*
4662 * Mouse clicks and drags.
4663 */
4664 static void
4665nv_mouse(cap)
4666 cmdarg_T *cap;
4667{
4668 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4669}
4670#endif
4671
4672/*
4673 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4674 * cap->arg must be TRUE for CTRL-E.
4675 */
4676 static void
4677nv_scroll_line(cap)
4678 cmdarg_T *cap;
4679{
4680 if (!checkclearop(cap->oap))
4681 scroll_redraw(cap->arg, cap->count1);
4682}
4683
4684/*
4685 * Scroll "count" lines up or down, and redraw.
4686 */
4687 void
4688scroll_redraw(up, count)
4689 int up;
4690 long count;
4691{
4692 linenr_T prev_topline = curwin->w_topline;
4693#ifdef FEAT_DIFF
4694 int prev_topfill = curwin->w_topfill;
4695#endif
4696 linenr_T prev_lnum = curwin->w_cursor.lnum;
4697
4698 if (up)
4699 scrollup(count, TRUE);
4700 else
4701 scrolldown(count, TRUE);
4702 if (p_so)
4703 {
4704 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4705 * valid, otherwise the screen jumps back at the end of the file. */
4706 cursor_correct();
4707 check_cursor_moved(curwin);
4708 curwin->w_valid |= VALID_TOPLINE;
4709
4710 /* If moved back to where we were, at least move the cursor, otherwise
4711 * we get stuck at one position. Don't move the cursor up if the
4712 * first line of the buffer is already on the screen */
4713 while (curwin->w_topline == prev_topline
4714#ifdef FEAT_DIFF
4715 && curwin->w_topfill == prev_topfill
4716#endif
4717 )
4718 {
4719 if (up)
4720 {
4721 if (curwin->w_cursor.lnum > prev_lnum
4722 || cursor_down(1L, FALSE) == FAIL)
4723 break;
4724 }
4725 else
4726 {
4727 if (curwin->w_cursor.lnum < prev_lnum
4728 || prev_topline == 1L
4729 || cursor_up(1L, FALSE) == FAIL)
4730 break;
4731 }
4732 /* Mark w_topline as valid, otherwise the screen jumps back at the
4733 * end of the file. */
4734 check_cursor_moved(curwin);
4735 curwin->w_valid |= VALID_TOPLINE;
4736 }
4737 }
4738 if (curwin->w_cursor.lnum != prev_lnum)
4739 coladvance(curwin->w_curswant);
4740 redraw_later(VALID);
4741}
4742
4743/*
4744 * Commands that start with "z".
4745 */
4746 static void
4747nv_zet(cap)
4748 cmdarg_T *cap;
4749{
4750 long n;
4751 colnr_T col;
4752 int nchar = cap->nchar;
4753#ifdef FEAT_FOLDING
4754 long old_fdl = curwin->w_p_fdl;
4755 int old_fen = curwin->w_p_fen;
4756#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004757#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004758 int undo = FALSE;
4759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 if (VIM_ISDIGIT(nchar))
4762 {
4763 /*
4764 * "z123{nchar}": edit the count before obtaining {nchar}
4765 */
4766 if (checkclearop(cap->oap))
4767 return;
4768 n = nchar - '0';
4769 for (;;)
4770 {
4771#ifdef USE_ON_FLY_SCROLL
4772 dont_scroll = TRUE; /* disallow scrolling here */
4773#endif
4774 ++no_mapping;
4775 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004776 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 --no_mapping;
4779 --allow_keys;
4780#ifdef FEAT_CMDL_INFO
4781 (void)add_to_showcmd(nchar);
4782#endif
4783 if (nchar == K_DEL || nchar == K_KDEL)
4784 n /= 10;
4785 else if (VIM_ISDIGIT(nchar))
4786 n = n * 10 + (nchar - '0');
4787 else if (nchar == CAR)
4788 {
4789#ifdef FEAT_GUI
4790 need_mouse_correct = TRUE;
4791#endif
4792 win_setheight((int)n);
4793 break;
4794 }
4795 else if (nchar == 'l'
4796 || nchar == 'h'
4797 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004798 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 cap->count1 = n ? n * cap->count1 : cap->count1;
4801 goto dozet;
4802 }
4803 else
4804 {
4805 clearopbeep(cap->oap);
4806 break;
4807 }
4808 }
4809 cap->oap->op_type = OP_NOP;
4810 return;
4811 }
4812
4813dozet:
4814 if (
4815#ifdef FEAT_FOLDING
4816 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4817 * and "zC" only in Visual mode. "zj" and "zk" are motion
4818 * commands. */
4819 cap->nchar != 'f' && cap->nchar != 'F'
4820 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4821 && cap->nchar != 'j' && cap->nchar != 'k'
4822 &&
4823#endif
4824 checkclearop(cap->oap))
4825 return;
4826
4827 /*
4828 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4829 * If line number given, set cursor.
4830 */
4831 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4832 && cap->count0
4833 && cap->count0 != curwin->w_cursor.lnum)
4834 {
4835 setpcmark();
4836 if (cap->count0 > curbuf->b_ml.ml_line_count)
4837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4838 else
4839 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004840 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842
4843 switch (nchar)
4844 {
4845 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4846 case '+':
4847 if (cap->count0 == 0)
4848 {
4849 /* No count given: put cursor at the line below screen */
4850 validate_botline(); /* make sure w_botline is valid */
4851 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4852 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4853 else
4854 curwin->w_cursor.lnum = curwin->w_botline;
4855 }
4856 /* FALLTHROUGH */
4857 case NL:
4858 case CAR:
4859 case K_KENTER:
4860 beginline(BL_WHITE | BL_FIX);
4861 /* FALLTHROUGH */
4862
4863 case 't': scroll_cursor_top(0, TRUE);
4864 redraw_later(VALID);
4865 break;
4866
4867 /* "z." and "zz": put cursor in middle of screen */
4868 case '.': beginline(BL_WHITE | BL_FIX);
4869 /* FALLTHROUGH */
4870
4871 case 'z': scroll_cursor_halfway(TRUE);
4872 redraw_later(VALID);
4873 break;
4874
4875 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4876 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4877 * when <count> is at bottom of window, and puts that one at
4878 * bottom of window. */
4879 if (cap->count0 != 0)
4880 {
4881 scroll_cursor_bot(0, TRUE);
4882 curwin->w_cursor.lnum = curwin->w_topline;
4883 }
4884 else if (curwin->w_topline == 1)
4885 curwin->w_cursor.lnum = 1;
4886 else
4887 curwin->w_cursor.lnum = curwin->w_topline - 1;
4888 /* FALLTHROUGH */
4889 case '-':
4890 beginline(BL_WHITE | BL_FIX);
4891 /* FALLTHROUGH */
4892
4893 case 'b': scroll_cursor_bot(0, TRUE);
4894 redraw_later(VALID);
4895 break;
4896
4897 /* "zH" - scroll screen right half-page */
4898 case 'H':
4899 cap->count1 *= W_WIDTH(curwin) / 2;
4900 /* FALLTHROUGH */
4901
4902 /* "zh" - scroll screen to the right */
4903 case 'h':
4904 case K_LEFT:
4905 if (!curwin->w_p_wrap)
4906 {
4907 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4908 curwin->w_leftcol = 0;
4909 else
4910 curwin->w_leftcol -= (colnr_T)cap->count1;
4911 leftcol_changed();
4912 }
4913 break;
4914
4915 /* "zL" - scroll screen left half-page */
4916 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4917 /* FALLTHROUGH */
4918
4919 /* "zl" - scroll screen to the left */
4920 case 'l':
4921 case K_RIGHT:
4922 if (!curwin->w_p_wrap)
4923 {
4924 /* scroll the window left */
4925 curwin->w_leftcol += (colnr_T)cap->count1;
4926 leftcol_changed();
4927 }
4928 break;
4929
4930 /* "zs" - scroll screen, cursor at the start */
4931 case 's': if (!curwin->w_p_wrap)
4932 {
4933#ifdef FEAT_FOLDING
4934 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4935 col = 0; /* like the cursor is in col 0 */
4936 else
4937#endif
4938 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4939 if ((long)col > p_siso)
4940 col -= p_siso;
4941 else
4942 col = 0;
4943 if (curwin->w_leftcol != col)
4944 {
4945 curwin->w_leftcol = col;
4946 redraw_later(NOT_VALID);
4947 }
4948 }
4949 break;
4950
4951 /* "ze" - scroll screen, cursor at the end */
4952 case 'e': if (!curwin->w_p_wrap)
4953 {
4954#ifdef FEAT_FOLDING
4955 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4956 col = 0; /* like the cursor is in col 0 */
4957 else
4958#endif
4959 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4960 n = W_WIDTH(curwin) - curwin_col_off();
4961 if ((long)col + p_siso < n)
4962 col = 0;
4963 else
4964 col = col + p_siso - n + 1;
4965 if (curwin->w_leftcol != col)
4966 {
4967 curwin->w_leftcol = col;
4968 redraw_later(NOT_VALID);
4969 }
4970 }
4971 break;
4972
4973#ifdef FEAT_FOLDING
4974 /* "zF": create fold command */
4975 /* "zf": create fold operator */
4976 case 'F':
4977 case 'f': if (foldManualAllowed(TRUE))
4978 {
4979 cap->nchar = 'f';
4980 nv_operator(cap);
4981 curwin->w_p_fen = TRUE;
4982
4983 /* "zF" is like "zfzf" */
4984 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4985 {
4986 nv_operator(cap);
4987 finish_op = TRUE;
4988 }
4989 }
4990 else
4991 clearopbeep(cap->oap);
4992 break;
4993
4994 /* "zd": delete fold at cursor */
4995 /* "zD": delete fold at cursor recursively */
4996 case 'd':
4997 case 'D': if (foldManualAllowed(FALSE))
4998 {
4999 if (VIsual_active)
5000 nv_operator(cap);
5001 else
5002 deleteFold(curwin->w_cursor.lnum,
5003 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5004 }
5005 break;
5006
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005007 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 case 'E': if (foldmethodIsManual(curwin))
5009 {
5010 clearFolding(curwin);
5011 changed_window_setting();
5012 }
5013 else if (foldmethodIsMarker(curwin))
5014 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5015 TRUE, FALSE);
5016 else
5017 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5018 break;
5019
5020 /* "zn": fold none: reset 'foldenable' */
5021 case 'n': curwin->w_p_fen = FALSE;
5022 break;
5023
5024 /* "zN": fold Normal: set 'foldenable' */
5025 case 'N': curwin->w_p_fen = TRUE;
5026 break;
5027
5028 /* "zi": invert folding: toggle 'foldenable' */
5029 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5030 break;
5031
5032 /* "za": open closed fold or close open fold at cursor */
5033 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5034 openFold(curwin->w_cursor.lnum, cap->count1);
5035 else
5036 {
5037 closeFold(curwin->w_cursor.lnum, cap->count1);
5038 curwin->w_p_fen = TRUE;
5039 }
5040 break;
5041
5042 /* "zA": open fold at cursor recursively */
5043 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5044 openFoldRecurse(curwin->w_cursor.lnum);
5045 else
5046 {
5047 closeFoldRecurse(curwin->w_cursor.lnum);
5048 curwin->w_p_fen = TRUE;
5049 }
5050 break;
5051
5052 /* "zo": open fold at cursor or Visual area */
5053 case 'o': if (VIsual_active)
5054 nv_operator(cap);
5055 else
5056 openFold(curwin->w_cursor.lnum, cap->count1);
5057 break;
5058
5059 /* "zO": open fold recursively */
5060 case 'O': if (VIsual_active)
5061 nv_operator(cap);
5062 else
5063 openFoldRecurse(curwin->w_cursor.lnum);
5064 break;
5065
5066 /* "zc": close fold at cursor or Visual area */
5067 case 'c': if (VIsual_active)
5068 nv_operator(cap);
5069 else
5070 closeFold(curwin->w_cursor.lnum, cap->count1);
5071 curwin->w_p_fen = TRUE;
5072 break;
5073
5074 /* "zC": close fold recursively */
5075 case 'C': if (VIsual_active)
5076 nv_operator(cap);
5077 else
5078 closeFoldRecurse(curwin->w_cursor.lnum);
5079 curwin->w_p_fen = TRUE;
5080 break;
5081
5082 /* "zv": open folds at the cursor */
5083 case 'v': foldOpenCursor();
5084 break;
5085
5086 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5087 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005088 curwin->w_foldinvalid = TRUE; /* recompute folds */
5089 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 foldOpenCursor();
5091 break;
5092
5093 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5094 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005095 curwin->w_foldinvalid = TRUE; /* recompute folds */
5096 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /* "zm": fold more */
5100 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005101 {
5102 curwin->w_p_fdl -= cap->count1;
5103 if (curwin->w_p_fdl < 0)
5104 curwin->w_p_fdl = 0;
5105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 old_fdl = -1; /* force an update */
5107 curwin->w_p_fen = TRUE;
5108 break;
5109
5110 /* "zM": close all folds */
5111 case 'M': curwin->w_p_fdl = 0;
5112 old_fdl = -1; /* force an update */
5113 curwin->w_p_fen = TRUE;
5114 break;
5115
5116 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005117 case 'r': curwin->w_p_fdl += cap->count1;
5118 {
5119 int d = getDeepestNesting();
5120
5121 if (curwin->w_p_fdl >= d)
5122 curwin->w_p_fdl = d;
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 break;
5125
5126 /* "zR": open all folds */
5127 case 'R': curwin->w_p_fdl = getDeepestNesting();
5128 old_fdl = -1; /* force an update */
5129 break;
5130
5131 case 'j': /* "zj" move to next fold downwards */
5132 case 'k': /* "zk" move to next fold upwards */
5133 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5134 cap->count1) == FAIL)
5135 clearopbeep(cap->oap);
5136 break;
5137
5138#endif /* FEAT_FOLDING */
5139
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005140#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005141 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5142 ++no_mapping;
5143 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005144 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005145 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005146 --no_mapping;
5147 --allow_keys;
5148#ifdef FEAT_CMDL_INFO
5149 (void)add_to_showcmd(nchar);
5150#endif
5151 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5152 {
5153 clearopbeep(cap->oap);
5154 break;
5155 }
5156 undo = TRUE;
5157 /*FALLTHROUGH*/
5158
Bram Moolenaarb765d632005-06-07 21:00:02 +00005159 case 'g': /* "zg": add good word to word list */
5160 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005161 case 'G': /* "zG": add good word to temp word list */
5162 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005163 {
5164 char_u *ptr = NULL;
5165 int len;
5166
5167 if (checkclearop(cap->oap))
5168 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005169 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5170 == FAIL)
5171 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005172 if (ptr == NULL)
5173 {
5174 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005175
Bram Moolenaar134bf072013-09-25 18:54:24 +02005176 /* Find bad word under the cursor. When 'spell' is
5177 * off this fails and find_ident_under_cursor() is
5178 * used below. */
5179 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005180 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005181 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005182 if (len != 0 && curwin->w_cursor.col <= pos.col)
5183 ptr = ml_get_pos(&curwin->w_cursor);
5184 curwin->w_cursor = pos;
5185 }
5186
Bram Moolenaarb765d632005-06-07 21:00:02 +00005187 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5188 FIND_IDENT)) == 0)
5189 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005190 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005191 (nchar == 'G' || nchar == 'W')
5192 ? 0 : (int)cap->count1,
5193 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005194 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005195 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196
Bram Moolenaar43abc522005-12-10 20:15:02 +00005197 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005198 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005199 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201#endif
5202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 default: clearopbeep(cap->oap);
5204 }
5205
5206#ifdef FEAT_FOLDING
5207 /* Redraw when 'foldenable' changed */
5208 if (old_fen != curwin->w_p_fen)
5209 {
5210# ifdef FEAT_DIFF
5211 win_T *wp;
5212
5213 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5214 {
5215 /* Adjust 'foldenable' in diff-synced windows. */
5216 FOR_ALL_WINDOWS(wp)
5217 {
5218 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5219 {
5220 wp->w_p_fen = curwin->w_p_fen;
5221 changed_window_setting_win(wp);
5222 }
5223 }
5224 }
5225# endif
5226 changed_window_setting();
5227 }
5228
5229 /* Redraw when 'foldlevel' changed. */
5230 if (old_fdl != curwin->w_p_fdl)
5231 newFoldLevel();
5232#endif
5233}
5234
5235#ifdef FEAT_GUI
5236/*
5237 * Vertical scrollbar movement.
5238 */
5239 static void
5240nv_ver_scrollbar(cap)
5241 cmdarg_T *cap;
5242{
5243 if (cap->oap->op_type != OP_NOP)
5244 clearopbeep(cap->oap);
5245
5246 /* Even if an operator was pending, we still want to scroll */
5247 gui_do_scroll();
5248}
5249
5250/*
5251 * Horizontal scrollbar movement.
5252 */
5253 static void
5254nv_hor_scrollbar(cap)
5255 cmdarg_T *cap;
5256{
5257 if (cap->oap->op_type != OP_NOP)
5258 clearopbeep(cap->oap);
5259
5260 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005261 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262}
5263#endif
5264
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005265#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005266/*
5267 * Click in GUI tab.
5268 */
5269 static void
5270nv_tabline(cap)
5271 cmdarg_T *cap;
5272{
5273 if (cap->oap->op_type != OP_NOP)
5274 clearopbeep(cap->oap);
5275
5276 /* Even if an operator was pending, we still want to jump tabs. */
5277 goto_tabpage(current_tab);
5278}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005279
5280/*
5281 * Selected item in tab line menu.
5282 */
5283 static void
5284nv_tabmenu(cap)
5285 cmdarg_T *cap;
5286{
5287 if (cap->oap->op_type != OP_NOP)
5288 clearopbeep(cap->oap);
5289
5290 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005291 handle_tabmenu();
5292}
5293
5294/*
5295 * Handle selecting an item of the GUI tab line menu.
5296 * Used in Normal and Insert mode.
5297 */
5298 void
5299handle_tabmenu()
5300{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005301 switch (current_tabmenu)
5302 {
5303 case TABLINE_MENU_CLOSE:
5304 if (current_tab == 0)
5305 do_cmdline_cmd((char_u *)"tabclose");
5306 else
5307 {
5308 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5309 current_tab);
5310 do_cmdline_cmd(IObuff);
5311 }
5312 break;
5313
5314 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005315 if (current_tab == 0)
5316 do_cmdline_cmd((char_u *)"$tabnew");
5317 else
5318 {
5319 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5320 current_tab - 1);
5321 do_cmdline_cmd(IObuff);
5322 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005323 break;
5324
5325 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005326 if (current_tab == 0)
5327 do_cmdline_cmd((char_u *)"browse $tabnew");
5328 else
5329 {
5330 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5331 current_tab - 1);
5332 do_cmdline_cmd(IObuff);
5333 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005334 break;
5335 }
5336}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005337#endif
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339/*
5340 * "Q" command.
5341 */
5342 static void
5343nv_exmode(cap)
5344 cmdarg_T *cap;
5345{
5346 /*
5347 * Ignore 'Q' in Visual mode, just give a beep.
5348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 if (VIsual_active)
5350 vim_beep();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005351 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 do_exmode(FALSE);
5353}
5354
5355/*
5356 * Handle a ":" command.
5357 */
5358 static void
5359nv_colon(cap)
5360 cmdarg_T *cap;
5361{
5362 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005363 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 if (VIsual_active)
5366 nv_operator(cap);
5367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 if (cap->oap->op_type != OP_NOP)
5370 {
5371 /* Using ":" as a movement is characterwise exclusive. */
5372 cap->oap->motion_type = MCHAR;
5373 cap->oap->inclusive = FALSE;
5374 }
5375 else if (cap->count0)
5376 {
5377 /* translate "count:" into ":.,.+(count - 1)" */
5378 stuffcharReadbuff('.');
5379 if (cap->count0 > 1)
5380 {
5381 stuffReadbuff((char_u *)",.+");
5382 stuffnumReadbuff((long)cap->count0 - 1L);
5383 }
5384 }
5385
5386 /* When typing, don't type below an old message */
5387 if (KeyTyped)
5388 compute_cmdrow();
5389
5390 old_p_im = p_im;
5391
5392 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005393 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5395
5396 /* If 'insertmode' changed, enter or exit Insert mode */
5397 if (p_im != old_p_im)
5398 {
5399 if (p_im)
5400 restart_edit = 'i';
5401 else
5402 restart_edit = 0;
5403 }
5404
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005405 if (cmd_result == FAIL)
5406 /* The Ex command failed, do not execute the operator. */
5407 clearop(cap->oap);
5408 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5410 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005411 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5412 || did_emsg
5413 ))
5414 /* The start of the operator has become invalid by the Ex command.
5415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 clearopbeep(cap->oap);
5417 }
5418}
5419
5420/*
5421 * Handle CTRL-G command.
5422 */
5423 static void
5424nv_ctrlg(cap)
5425 cmdarg_T *cap;
5426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 if (VIsual_active) /* toggle Selection/Visual mode */
5428 {
5429 VIsual_select = !VIsual_select;
5430 showmode();
5431 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005432 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 /* print full name if count given or :cd used */
5434 fileinfo((int)cap->count0, FALSE, TRUE);
5435}
5436
5437/*
5438 * Handle CTRL-H <Backspace> command.
5439 */
5440 static void
5441nv_ctrlh(cap)
5442 cmdarg_T *cap;
5443{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (VIsual_active && VIsual_select)
5445 {
5446 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5447 v_visop(cap);
5448 }
5449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 nv_left(cap);
5451}
5452
5453/*
5454 * CTRL-L: clear screen and redraw.
5455 */
5456 static void
5457nv_clear(cap)
5458 cmdarg_T *cap;
5459{
5460 if (!checkclearop(cap->oap))
5461 {
5462#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5463 /*
5464 * Right now, the BeBox doesn't seem to have an easy way to detect
5465 * window resizing, so we cheat and make the user detect it
5466 * manually with CTRL-L instead
5467 */
5468 ui_get_shellsize();
5469#endif
5470#ifdef FEAT_SYN_HL
5471 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005472 syn_stack_free_all(curwin->w_s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#endif
5474 redraw_later(CLEAR);
5475 }
5476}
5477
5478/*
5479 * CTRL-O: In Select mode: switch to Visual mode for one command.
5480 * Otherwise: Go to older pcmark.
5481 */
5482 static void
5483nv_ctrlo(cap)
5484 cmdarg_T *cap;
5485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if (VIsual_active && VIsual_select)
5487 {
5488 VIsual_select = FALSE;
5489 showmode();
5490 restart_VIsual_select = 2; /* restart Select mode later */
5491 }
5492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
5494 cap->count1 = -cap->count1;
5495 nv_pcmark(cap);
5496 }
5497}
5498
5499/*
5500 * CTRL-^ command, short for ":e #"
5501 */
5502 static void
5503nv_hat(cap)
5504 cmdarg_T *cap;
5505{
5506 if (!checkclearopq(cap->oap))
5507 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5508 GETF_SETMARK|GETF_ALT, FALSE);
5509}
5510
5511/*
5512 * "Z" commands.
5513 */
5514 static void
5515nv_Zet(cap)
5516 cmdarg_T *cap;
5517{
5518 if (!checkclearopq(cap->oap))
5519 {
5520 switch (cap->nchar)
5521 {
5522 /* "ZZ": equivalent to ":x". */
5523 case 'Z': do_cmdline_cmd((char_u *)"x");
5524 break;
5525
5526 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5527 case 'Q': do_cmdline_cmd((char_u *)"q!");
5528 break;
5529
5530 default: clearopbeep(cap->oap);
5531 }
5532 }
5533}
5534
5535#if defined(FEAT_WINDOWS) || defined(PROTO)
5536/*
5537 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5538 */
5539 void
5540do_nv_ident(c1, c2)
5541 int c1;
5542 int c2;
5543{
5544 oparg_T oa;
5545 cmdarg_T ca;
5546
5547 clear_oparg(&oa);
5548 vim_memset(&ca, 0, sizeof(ca));
5549 ca.oap = &oa;
5550 ca.cmdchar = c1;
5551 ca.nchar = c2;
5552 nv_ident(&ca);
5553}
5554#endif
5555
5556/*
5557 * Handle the commands that use the word under the cursor.
5558 * [g] CTRL-] :ta to current identifier
5559 * [g] 'K' run program for current identifier
5560 * [g] '*' / to current identifier or string
5561 * [g] '#' ? to current identifier or string
5562 * g ']' :tselect for current identifier
5563 */
5564 static void
5565nv_ident(cap)
5566 cmdarg_T *cap;
5567{
5568 char_u *ptr = NULL;
5569 char_u *buf;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005570 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 char_u *p;
5572 char_u *kp; /* value of 'keywordprg' */
5573 int kp_help; /* 'keywordprg' is ":help" */
5574 int n = 0; /* init for GCC */
5575 int cmdchar;
5576 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005577 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 char_u *aux_ptr;
5579 int isman;
5580 int isman_s;
5581
5582 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5583 {
5584 cmdchar = cap->nchar;
5585 g_cmd = TRUE;
5586 }
5587 else
5588 {
5589 cmdchar = cap->cmdchar;
5590 g_cmd = FALSE;
5591 }
5592
5593 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5594 cmdchar = '#';
5595
5596 /*
5597 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5598 */
5599 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5602 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 if (checkclearopq(cap->oap))
5604 return;
5605 }
5606
5607 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5608 (cmdchar == '*' || cmdchar == '#')
5609 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5610 {
5611 clearop(cap->oap);
5612 return;
5613 }
5614
5615 /* Allocate buffer to put the command in. Inserting backslashes can
5616 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5617 * and some numbers. */
5618 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5619 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5620 || STRCMP(kp, ":help") == 0);
5621 buf = alloc((unsigned)(n * 2 + 30 + STRLEN(kp)));
5622 if (buf == NULL)
5623 return;
5624 buf[0] = NUL;
5625
5626 switch (cmdchar)
5627 {
5628 case '*':
5629 case '#':
5630 /*
5631 * Put cursor at start of word, makes search skip the word
5632 * under the cursor.
5633 * Call setpcmark() first, so "*``" puts the cursor back where
5634 * it was.
5635 */
5636 setpcmark();
5637 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5638
5639 if (!g_cmd && vim_iswordp(ptr))
5640 STRCPY(buf, "\\<");
5641 no_smartcase = TRUE; /* don't use 'smartcase' now */
5642 break;
5643
5644 case 'K':
5645 if (kp_help)
5646 STRCPY(buf, "he! ");
5647 else
5648 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005649 /* An external command will probably use an argument starting
5650 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005651 while (*ptr == '-' && n > 0)
5652 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005653 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005654 --n;
5655 }
5656 if (n == 0)
5657 {
5658 EMSG(_(e_noident)); /* found dashes only */
5659 vim_free(buf);
5660 return;
5661 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 /* When a count is given, turn it into a range. Is this
5664 * really what we want? */
5665 isman = (STRCMP(kp, "man") == 0);
5666 isman_s = (STRCMP(kp, "man -s") == 0);
5667 if (cap->count0 != 0 && !(isman || isman_s))
5668 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5669
5670 STRCAT(buf, "! ");
5671 if (cap->count0 == 0 && isman_s)
5672 STRCAT(buf, "man");
5673 else
5674 STRCAT(buf, kp);
5675 STRCAT(buf, " ");
5676 if (cap->count0 != 0 && (isman || isman_s))
5677 {
5678 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5679 STRCAT(buf, " ");
5680 }
5681 }
5682 break;
5683
5684 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005685 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#ifdef FEAT_CSCOPE
5687 if (p_cst)
5688 STRCPY(buf, "cstag ");
5689 else
5690#endif
5691 STRCPY(buf, "ts ");
5692 break;
5693
5694 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005695 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 if (curbuf->b_help)
5697 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005699 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005700 if (g_cmd)
5701 STRCPY(buf, "tj ");
5702 else
5703 sprintf((char *)buf, "%ldta ", cap->count0);
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706
5707 /*
5708 * Now grab the chars in the identifier
5709 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005710 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005712 /* Escape the argument properly for a shell command */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005713 ptr = vim_strnsave(ptr, n);
Bram Moolenaar26df0922014-02-23 23:39:13 +01005714 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005715 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005716 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005718 vim_free(buf);
5719 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005721 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5722 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005723 {
5724 vim_free(buf);
5725 vim_free(p);
5726 return;
5727 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005728 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005729 STRCAT(buf, p);
5730 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005732 else
5733 {
5734 if (cmdchar == '*')
5735 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5736 else if (cmdchar == '#')
5737 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005738 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005739 {
5740 if (curbuf->b_help)
5741 /* ":help" handles unescaped argument */
5742 aux_ptr = (char_u *)"";
5743 else
5744 aux_ptr = (char_u *)"\\|\"\n[";
5745 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005746 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 aux_ptr = (char_u *)"\\|\"\n*?[";
5748
5749 p = buf + STRLEN(buf);
5750 while (n-- > 0)
5751 {
5752 /* put a backslash before \ and some others */
5753 if (vim_strchr(aux_ptr, *ptr) != NULL)
5754 *p++ = '\\';
5755#ifdef FEAT_MBYTE
5756 /* When current byte is a part of multibyte character, copy all
5757 * bytes of that character. */
5758 if (has_mbyte)
5759 {
5760 int i;
5761 int len = (*mb_ptr2len)(ptr) - 1;
5762
5763 for (i = 0; i < len && n >= 1; ++i, --n)
5764 *p++ = *ptr++;
5765 }
5766#endif
5767 *p++ = *ptr++;
5768 }
5769 *p = NUL;
5770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 /*
5773 * Execute the command.
5774 */
5775 if (cmdchar == '*' || cmdchar == '#')
5776 {
5777 if (!g_cmd && (
5778#ifdef FEAT_MBYTE
5779 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5780#endif
5781 vim_iswordc(ptr[-1])))
5782 STRCAT(buf, "\\>");
5783#ifdef FEAT_CMDHIST
5784 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005785 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5787#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005788 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else
5791 do_cmdline_cmd(buf);
5792
5793 vim_free(buf);
5794}
5795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796/*
5797 * Get visually selected text, within one line only.
5798 * Returns FAIL if more than one line selected.
5799 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005800 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801get_visual_text(cap, pp, lenp)
5802 cmdarg_T *cap;
5803 char_u **pp; /* return: start of selected text */
5804 int *lenp; /* return: length of selected text */
5805{
5806 if (VIsual_mode != 'V')
5807 unadjust_for_sel();
5808 if (VIsual.lnum != curwin->w_cursor.lnum)
5809 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005810 if (cap != NULL)
5811 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 return FAIL;
5813 }
5814 if (VIsual_mode == 'V')
5815 {
5816 *pp = ml_get_curline();
5817 *lenp = (int)STRLEN(*pp);
5818 }
5819 else
5820 {
5821 if (lt(curwin->w_cursor, VIsual))
5822 {
5823 *pp = ml_get_pos(&curwin->w_cursor);
5824 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5825 }
5826 else
5827 {
5828 *pp = ml_get_pos(&VIsual);
5829 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5830 }
5831#ifdef FEAT_MBYTE
5832 if (has_mbyte)
5833 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005834 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#endif
5836 }
5837 reset_VIsual_and_resel();
5838 return OK;
5839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841/*
5842 * CTRL-T: backwards in tag stack
5843 */
5844 static void
5845nv_tagpop(cap)
5846 cmdarg_T *cap;
5847{
5848 if (!checkclearopq(cap->oap))
5849 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5850}
5851
5852/*
5853 * Handle scrolling command 'H', 'L' and 'M'.
5854 */
5855 static void
5856nv_scroll(cap)
5857 cmdarg_T *cap;
5858{
5859 int used = 0;
5860 long n;
5861#ifdef FEAT_FOLDING
5862 linenr_T lnum;
5863#endif
5864 int half;
5865
5866 cap->oap->motion_type = MLINE;
5867 setpcmark();
5868
5869 if (cap->cmdchar == 'L')
5870 {
5871 validate_botline(); /* make sure curwin->w_botline is valid */
5872 curwin->w_cursor.lnum = curwin->w_botline - 1;
5873 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5874 curwin->w_cursor.lnum = 1;
5875 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005876 {
5877#ifdef FEAT_FOLDING
5878 if (hasAnyFolding(curwin))
5879 {
5880 /* Count a fold for one screen line. */
5881 for (n = cap->count1 - 1; n > 0
5882 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5883 {
5884 (void)hasFolding(curwin->w_cursor.lnum,
5885 &curwin->w_cursor.lnum, NULL);
5886 --curwin->w_cursor.lnum;
5887 }
5888 }
5889 else
5890#endif
5891 curwin->w_cursor.lnum -= cap->count1 - 1;
5892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
5894 else
5895 {
5896 if (cap->cmdchar == 'M')
5897 {
5898#ifdef FEAT_DIFF
5899 /* Don't count filler lines above the window. */
5900 used -= diff_check_fill(curwin, curwin->w_topline)
5901 - curwin->w_topfill;
5902#endif
5903 validate_botline(); /* make sure w_empty_rows is valid */
5904 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5905 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5906 {
5907#ifdef FEAT_DIFF
5908 /* Count half he number of filler lines to be "below this
5909 * line" and half to be "above the next line". */
5910 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5911 + n) / 2 >= half)
5912 {
5913 --n;
5914 break;
5915 }
5916#endif
5917 used += plines(curwin->w_topline + n);
5918 if (used >= half)
5919 break;
5920#ifdef FEAT_FOLDING
5921 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5922 n = lnum - curwin->w_topline;
5923#endif
5924 }
5925 if (n > 0 && used > curwin->w_height)
5926 --n;
5927 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005928 else /* (cap->cmdchar == 'H') */
5929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005931#ifdef FEAT_FOLDING
5932 if (hasAnyFolding(curwin))
5933 {
5934 /* Count a fold for one screen line. */
5935 lnum = curwin->w_topline;
5936 while (n-- > 0 && lnum < curwin->w_botline - 1)
5937 {
5938 hasFolding(lnum, NULL, &lnum);
5939 ++lnum;
5940 }
5941 n = lnum - curwin->w_topline;
5942 }
5943#endif
5944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 curwin->w_cursor.lnum = curwin->w_topline + n;
5946 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5947 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5948 }
5949
5950 cursor_correct(); /* correct for 'so' */
5951 beginline(BL_SOL | BL_FIX);
5952}
5953
5954/*
5955 * Cursor right commands.
5956 */
5957 static void
5958nv_right(cap)
5959 cmdarg_T *cap;
5960{
5961 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005962 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005964 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5965 {
5966 /* <C-Right> and <S-Right> move a word or WORD right */
5967 if (mod_mask & MOD_MASK_CTRL)
5968 cap->arg = TRUE;
5969 nv_wordcmd(cap);
5970 return;
5971 }
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 cap->oap->motion_type = MCHAR;
5974 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005975 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005977#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005979 * In virtual edit mode, there's no such thing as "past_line", as lines
5980 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
5982 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005983 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#endif
5985
5986 for (n = cap->count1; n > 0; --n)
5987 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005988 if ((!past_line && oneright() == FAIL)
5989 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005990 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 {
5992 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005993 * <Space> wraps to next line if 'whichwrap' has 's'.
5994 * 'l' wraps to next line if 'whichwrap' has 'l'.
5995 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 */
5997 if ( ((cap->cmdchar == ' '
5998 && vim_strchr(p_ww, 's') != NULL)
5999 || (cap->cmdchar == 'l'
6000 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006001 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 && vim_strchr(p_ww, '>') != NULL))
6003 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6004 {
6005 /* When deleting we also count the NL as a character.
6006 * Set cap->oap->inclusive when last char in the line is
6007 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006008 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 && !cap->oap->inclusive
6010 && !lineempty(curwin->w_cursor.lnum))
6011 cap->oap->inclusive = TRUE;
6012 else
6013 {
6014 ++curwin->w_cursor.lnum;
6015 curwin->w_cursor.col = 0;
6016#ifdef FEAT_VIRTUALEDIT
6017 curwin->w_cursor.coladd = 0;
6018#endif
6019 curwin->w_set_curswant = TRUE;
6020 cap->oap->inclusive = FALSE;
6021 }
6022 continue;
6023 }
6024 if (cap->oap->op_type == OP_NOP)
6025 {
6026 /* Only beep and flush if not moved at all */
6027 if (n == cap->count1)
6028 beep_flush();
6029 }
6030 else
6031 {
6032 if (!lineempty(curwin->w_cursor.lnum))
6033 cap->oap->inclusive = TRUE;
6034 }
6035 break;
6036 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006037 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
6039 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006040#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if (virtual_active())
6042 oneright();
6043 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006046#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (has_mbyte)
6048 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006049 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 ++curwin->w_cursor.col;
6053 }
6054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 }
6056#ifdef FEAT_FOLDING
6057 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6058 && cap->oap->op_type == OP_NOP)
6059 foldOpenCursor();
6060#endif
6061}
6062
6063/*
6064 * Cursor left commands.
6065 *
6066 * Returns TRUE when operator end should not be adjusted.
6067 */
6068 static void
6069nv_left(cap)
6070 cmdarg_T *cap;
6071{
6072 long n;
6073
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006074 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6075 {
6076 /* <C-Left> and <S-Left> move a word or WORD left */
6077 if (mod_mask & MOD_MASK_CTRL)
6078 cap->arg = 1;
6079 nv_bck_word(cap);
6080 return;
6081 }
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 cap->oap->motion_type = MCHAR;
6084 cap->oap->inclusive = FALSE;
6085 for (n = cap->count1; n > 0; --n)
6086 {
6087 if (oneleft() == FAIL)
6088 {
6089 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6090 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6091 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6092 */
6093 if ( (((cap->cmdchar == K_BS
6094 || cap->cmdchar == Ctrl_H)
6095 && vim_strchr(p_ww, 'b') != NULL)
6096 || (cap->cmdchar == 'h'
6097 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006098 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 && vim_strchr(p_ww, '<') != NULL))
6100 && curwin->w_cursor.lnum > 1)
6101 {
6102 --(curwin->w_cursor.lnum);
6103 coladvance((colnr_T)MAXCOL);
6104 curwin->w_set_curswant = TRUE;
6105
6106 /* When the NL before the first char has to be deleted we
6107 * put the cursor on the NUL after the previous line.
6108 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006109 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if ( (cap->oap->op_type == OP_DELETE
6111 || cap->oap->op_type == OP_CHANGE)
6112 && !lineempty(curwin->w_cursor.lnum))
6113 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006114 char_u *cp = ml_get_cursor();
6115
6116 if (*cp != NUL)
6117 {
6118#ifdef FEAT_MBYTE
6119 if (has_mbyte)
6120 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6121 else
6122#endif
6123 ++curwin->w_cursor.col;
6124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 cap->retval |= CA_NO_ADJ_OP_END;
6126 }
6127 continue;
6128 }
6129 /* Only beep and flush if not moved at all */
6130 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6131 beep_flush();
6132 break;
6133 }
6134 }
6135#ifdef FEAT_FOLDING
6136 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6137 && cap->oap->op_type == OP_NOP)
6138 foldOpenCursor();
6139#endif
6140}
6141
6142/*
6143 * Cursor up commands.
6144 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6145 */
6146 static void
6147nv_up(cap)
6148 cmdarg_T *cap;
6149{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006150 if (mod_mask & MOD_MASK_SHIFT)
6151 {
6152 /* <S-Up> is page up */
6153 cap->arg = BACKWARD;
6154 nv_page(cap);
6155 }
6156 else
6157 {
6158 cap->oap->motion_type = MLINE;
6159 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6160 clearopbeep(cap->oap);
6161 else if (cap->arg)
6162 beginline(BL_WHITE | BL_FIX);
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164}
6165
6166/*
6167 * Cursor down commands.
6168 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6169 */
6170 static void
6171nv_down(cap)
6172 cmdarg_T *cap;
6173{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006174 if (mod_mask & MOD_MASK_SHIFT)
6175 {
6176 /* <S-Down> is page down */
6177 cap->arg = FORWARD;
6178 nv_page(cap);
6179 }
6180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6182 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006183 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006184 if (curwin->w_llist_ref == NULL)
6185 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6186 else
6187 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 else
6189#endif
6190 {
6191#ifdef FEAT_CMDWIN
6192 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006193 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 cmdwin_result = CAR;
6195 else
6196#endif
6197 {
6198 cap->oap->motion_type = MLINE;
6199 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6200 clearopbeep(cap->oap);
6201 else if (cap->arg)
6202 beginline(BL_WHITE | BL_FIX);
6203 }
6204 }
6205}
6206
6207#ifdef FEAT_SEARCHPATH
6208/*
6209 * Grab the file name under the cursor and edit it.
6210 */
6211 static void
6212nv_gotofile(cap)
6213 cmdarg_T *cap;
6214{
6215 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006216 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006218 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006221 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 return;
6223 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006224#ifdef FEAT_AUTOCMD
6225 if (curbuf_locked())
6226 {
6227 clearop(cap->oap);
6228 return;
6229 }
6230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006232 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 if (ptr != NULL)
6235 {
6236 /* do autowrite if necessary */
6237 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !P_HID(curbuf))
6238 autowrite(curbuf, FALSE);
6239 setpcmark();
6240 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006241 P_HID(curbuf) ? ECMD_HIDE : 0, curwin);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006242 if (cap->nchar == 'F' && lnum >= 0)
6243 {
6244 curwin->w_cursor.lnum = lnum;
6245 check_cursor_lnum();
6246 beginline(BL_SOL | BL_FIX);
6247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 vim_free(ptr);
6249 }
6250 else
6251 clearop(cap->oap);
6252}
6253#endif
6254
6255/*
6256 * <End> command: to end of current line or last line.
6257 */
6258 static void
6259nv_end(cap)
6260 cmdarg_T *cap;
6261{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006262 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006264 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 nv_goto(cap);
6266 cap->count1 = 1; /* to end of current line */
6267 }
6268 nv_dollar(cap);
6269}
6270
6271/*
6272 * Handle the "$" command.
6273 */
6274 static void
6275nv_dollar(cap)
6276 cmdarg_T *cap;
6277{
6278 cap->oap->motion_type = MCHAR;
6279 cap->oap->inclusive = TRUE;
6280#ifdef FEAT_VIRTUALEDIT
6281 /* In virtual mode when off the edge of a line and an operator
6282 * is pending (whew!) keep the cursor where it is.
6283 * Otherwise, send it to the end of the line. */
6284 if (!virtual_active() || gchar_cursor() != NUL
6285 || cap->oap->op_type == OP_NOP)
6286#endif
6287 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6288 if (cursor_down((long)(cap->count1 - 1),
6289 cap->oap->op_type == OP_NOP) == FAIL)
6290 clearopbeep(cap->oap);
6291#ifdef FEAT_FOLDING
6292 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6293 foldOpenCursor();
6294#endif
6295}
6296
6297/*
6298 * Implementation of '?' and '/' commands.
6299 * If cap->arg is TRUE don't set PC mark.
6300 */
6301 static void
6302nv_search(cap)
6303 cmdarg_T *cap;
6304{
6305 oparg_T *oap = cap->oap;
6306
6307 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6308 {
6309 /* Translate "g??" to "g?g?" */
6310 cap->cmdchar = 'g';
6311 cap->nchar = '?';
6312 nv_operator(cap);
6313 return;
6314 }
6315
6316 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6317
6318 if (cap->searchbuf == NULL)
6319 {
6320 clearop(oap);
6321 return;
6322 }
6323
Bram Moolenaar46539112015-02-17 15:43:57 +01006324 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 (cap->arg ? 0 : SEARCH_MARK));
6326}
6327
6328/*
6329 * Handle "N" and "n" commands.
6330 * cap->arg is SEARCH_REV for "N", 0 for "n".
6331 */
6332 static void
6333nv_next(cap)
6334 cmdarg_T *cap;
6335{
Bram Moolenaar46539112015-02-17 15:43:57 +01006336 pos_T old = curwin->w_cursor;
6337 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6338
6339 if (i == 1 && equalpos(old, curwin->w_cursor))
6340 {
6341 /* Avoid getting stuck on the current cursor position, which can
6342 * happen when an offset is given and the cursor is on the last char
6343 * in the buffer: Repeat with count + 1. */
6344 cap->count1 += 1;
6345 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6346 cap->count1 -= 1;
6347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
6351 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6352 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006353 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006355 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356normal_search(cap, dir, pat, opt)
6357 cmdarg_T *cap;
6358 int dir;
6359 char_u *pat;
6360 int opt; /* extra flags for do_search() */
6361{
6362 int i;
6363
6364 cap->oap->motion_type = MCHAR;
6365 cap->oap->inclusive = FALSE;
6366 cap->oap->use_reg_one = TRUE;
6367 curwin->w_set_curswant = TRUE;
6368
6369 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00006370 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (i == 0)
6372 clearop(cap->oap);
6373 else
6374 {
6375 if (i == 2)
6376 cap->oap->motion_type = MLINE;
6377#ifdef FEAT_VIRTUALEDIT
6378 curwin->w_cursor.coladd = 0;
6379#endif
6380#ifdef FEAT_FOLDING
6381 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6382 foldOpenCursor();
6383#endif
6384 }
6385
6386 /* "/$" will put the cursor after the end of the line, may need to
6387 * correct that here */
6388 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006389 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390}
6391
6392/*
6393 * Character search commands.
6394 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6395 * ',' and FALSE for ';'.
6396 * cap->nchar is NUL for ',' and ';' (repeat the search)
6397 */
6398 static void
6399nv_csearch(cap)
6400 cmdarg_T *cap;
6401{
6402 int t_cmd;
6403
6404 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6405 t_cmd = TRUE;
6406 else
6407 t_cmd = FALSE;
6408
6409 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6411 clearopbeep(cap->oap);
6412 else
6413 {
6414 curwin->w_set_curswant = TRUE;
6415#ifdef FEAT_VIRTUALEDIT
6416 /* Include a Tab for "tx" and for "dfx". */
6417 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6418 && (t_cmd || cap->oap->op_type != OP_NOP))
6419 {
6420 colnr_T scol, ecol;
6421
6422 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6423 curwin->w_cursor.coladd = ecol - scol;
6424 }
6425 else
6426 curwin->w_cursor.coladd = 0;
6427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429#ifdef FEAT_FOLDING
6430 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6431 foldOpenCursor();
6432#endif
6433 }
6434}
6435
6436/*
6437 * "[" and "]" commands.
6438 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6439 */
6440 static void
6441nv_brackets(cap)
6442 cmdarg_T *cap;
6443{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006444 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 pos_T prev_pos;
6446 pos_T *pos = NULL; /* init for GCC */
6447 pos_T old_pos; /* cursor position before command */
6448 int flag;
6449 long n;
6450 int findc;
6451 int c;
6452
6453 cap->oap->motion_type = MCHAR;
6454 cap->oap->inclusive = FALSE;
6455 old_pos = curwin->w_cursor;
6456#ifdef FEAT_VIRTUALEDIT
6457 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6458#endif
6459
6460#ifdef FEAT_SEARCHPATH
6461 /*
6462 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6463 */
6464 if (cap->nchar == 'f')
6465 nv_gotofile(cap);
6466 else
6467#endif
6468
6469#ifdef FEAT_FIND_ID
6470 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006471 * Find the occurrence(s) of the identifier or define under cursor
6472 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 *
6474 * search list jump
6475 * fwd bwd fwd bwd fwd bwd
6476 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6477 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6478 */
6479 if (vim_strchr((char_u *)
6480#ifdef EBCDIC
6481 "iI\005dD\067",
6482#else
6483 "iI\011dD\004",
6484#endif
6485 cap->nchar) != NULL)
6486 {
6487 char_u *ptr;
6488 int len;
6489
6490 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6491 clearop(cap->oap);
6492 else
6493 {
6494 find_pattern_in_path(ptr, 0, len, TRUE,
6495 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6496 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6497 cap->count1,
6498 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6499 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6500 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6501 (linenr_T)MAXLNUM);
6502 curwin->w_set_curswant = TRUE;
6503 }
6504 }
6505 else
6506#endif
6507
6508 /*
6509 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6510 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6511 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6512 * "[m" or "]m" search for prev/next start of (Java) method.
6513 * "[M" or "]M" search for prev/next end of (Java) method.
6514 */
6515 if ( (cap->cmdchar == '['
6516 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6517 || (cap->cmdchar == ']'
6518 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6519 {
6520 if (cap->nchar == '*')
6521 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 prev_pos.lnum = 0;
6523 if (cap->nchar == 'm' || cap->nchar == 'M')
6524 {
6525 if (cap->cmdchar == '[')
6526 findc = '{';
6527 else
6528 findc = '}';
6529 n = 9999;
6530 }
6531 else
6532 {
6533 findc = cap->nchar;
6534 n = cap->count1;
6535 }
6536 for ( ; n > 0; --n)
6537 {
6538 if ((pos = findmatchlimit(cap->oap, findc,
6539 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6540 {
6541 if (new_pos.lnum == 0) /* nothing found */
6542 {
6543 if (cap->nchar != 'm' && cap->nchar != 'M')
6544 clearopbeep(cap->oap);
6545 }
6546 else
6547 pos = &new_pos; /* use last one found */
6548 break;
6549 }
6550 prev_pos = new_pos;
6551 curwin->w_cursor = *pos;
6552 new_pos = *pos;
6553 }
6554 curwin->w_cursor = old_pos;
6555
6556 /*
6557 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6558 * brought us to the match for "[m" and "]M" when inside a method.
6559 * Try finding the '{' or '}' we want to be at.
6560 * Also repeat for the given count.
6561 */
6562 if (cap->nchar == 'm' || cap->nchar == 'M')
6563 {
6564 /* norm is TRUE for "]M" and "[m" */
6565 int norm = ((findc == '{') == (cap->nchar == 'm'));
6566
6567 n = cap->count1;
6568 /* found a match: we were inside a method */
6569 if (prev_pos.lnum != 0)
6570 {
6571 pos = &prev_pos;
6572 curwin->w_cursor = prev_pos;
6573 if (norm)
6574 --n;
6575 }
6576 else
6577 pos = NULL;
6578 while (n > 0)
6579 {
6580 for (;;)
6581 {
6582 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6583 {
6584 /* if not found anything, that's an error */
6585 if (pos == NULL)
6586 clearopbeep(cap->oap);
6587 n = 0;
6588 break;
6589 }
6590 c = gchar_cursor();
6591 if (c == '{' || c == '}')
6592 {
6593 /* Must have found end/start of class: use it.
6594 * Or found the place to be at. */
6595 if ((c == findc && norm) || (n == 1 && !norm))
6596 {
6597 new_pos = curwin->w_cursor;
6598 pos = &new_pos;
6599 n = 0;
6600 }
6601 /* if no match found at all, we started outside of the
6602 * class and we're inside now. Just go on. */
6603 else if (new_pos.lnum == 0)
6604 {
6605 new_pos = curwin->w_cursor;
6606 pos = &new_pos;
6607 }
6608 /* found start/end of other method: go to match */
6609 else if ((pos = findmatchlimit(cap->oap, findc,
6610 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6611 0)) == NULL)
6612 n = 0;
6613 else
6614 curwin->w_cursor = *pos;
6615 break;
6616 }
6617 }
6618 --n;
6619 }
6620 curwin->w_cursor = old_pos;
6621 if (pos == NULL && new_pos.lnum != 0)
6622 clearopbeep(cap->oap);
6623 }
6624 if (pos != NULL)
6625 {
6626 setpcmark();
6627 curwin->w_cursor = *pos;
6628 curwin->w_set_curswant = TRUE;
6629#ifdef FEAT_FOLDING
6630 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6631 && cap->oap->op_type == OP_NOP)
6632 foldOpenCursor();
6633#endif
6634 }
6635 }
6636
6637 /*
6638 * "[[", "[]", "]]" and "][": move to start or end of function
6639 */
6640 else if (cap->nchar == '[' || cap->nchar == ']')
6641 {
6642 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6643 flag = '{';
6644 else
6645 flag = '}'; /* "][" or "[]" */
6646
6647 curwin->w_set_curswant = TRUE;
6648 /*
6649 * Imitate strange Vi behaviour: When using "]]" with an operator
6650 * we also stop at '}'.
6651 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006652 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 (cap->oap->op_type != OP_NOP
6654 && cap->arg == FORWARD && flag == '{')))
6655 clearopbeep(cap->oap);
6656 else
6657 {
6658 if (cap->oap->op_type == OP_NOP)
6659 beginline(BL_WHITE | BL_FIX);
6660#ifdef FEAT_FOLDING
6661 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6662 foldOpenCursor();
6663#endif
6664 }
6665 }
6666
6667 /*
6668 * "[p", "[P", "]P" and "]p": put with indent adjustment
6669 */
6670 else if (cap->nchar == 'p' || cap->nchar == 'P')
6671 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006672 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006674 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6675 ? FORWARD : BACKWARD;
6676 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006677 int was_visual = VIsual_active;
6678 int line_count = curbuf->b_ml.ml_line_count;
6679 pos_T start, end;
6680
6681 if (VIsual_active)
6682 {
6683 start = ltoreq(VIsual, curwin->w_cursor)
6684 ? VIsual : curwin->w_cursor;
6685 end = equalpos(start,VIsual) ? curwin->w_cursor : VIsual;
6686 curwin->w_cursor = (dir == BACKWARD ? start : end);
6687 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006688# ifdef FEAT_CLIPBOARD
6689 adjust_clip_reg(&regname);
6690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006692
6693 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006694 if (was_visual)
6695 {
6696 VIsual = start;
6697 curwin->w_cursor = end;
6698 if (dir == BACKWARD)
6699 {
6700 /* adjust lines */
6701 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6702 curwin->w_cursor.lnum +=
6703 curbuf->b_ml.ml_line_count - line_count;
6704 }
6705
6706 VIsual_active = TRUE;
6707 if (VIsual_mode == 'V')
6708 {
6709 /* delete visually selected lines */
6710 cap->cmdchar = 'd';
6711 cap->nchar = NUL;
6712 cap->oap->regname = regname;
6713 nv_operator(cap);
6714 do_pending_operator(cap, 0, FALSE);
6715 }
6716 if (VIsual_active)
6717 {
6718 end_visual_mode();
6719 redraw_later(SOME_VALID);
6720 }
6721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723 }
6724
6725 /*
6726 * "['", "[`", "]'" and "]`": jump to next mark
6727 */
6728 else if (cap->nchar == '\'' || cap->nchar == '`')
6729 {
6730 pos = &curwin->w_cursor;
6731 for (n = cap->count1; n > 0; --n)
6732 {
6733 prev_pos = *pos;
6734 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6735 cap->nchar == '\'');
6736 if (pos == NULL)
6737 break;
6738 }
6739 if (pos == NULL)
6740 pos = &prev_pos;
6741 nv_cursormark(cap, cap->nchar == '\'', pos);
6742 }
6743
6744#ifdef FEAT_MOUSE
6745 /*
6746 * [ or ] followed by a middle mouse click: put selected text with
6747 * indent adjustment. Any other button just does as usual.
6748 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006749 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 (void)do_mouse(cap->oap, cap->nchar,
6752 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6753 cap->count1, PUT_FIXINDENT);
6754 }
6755#endif /* FEAT_MOUSE */
6756
6757#ifdef FEAT_FOLDING
6758 /*
6759 * "[z" and "]z": move to start or end of open fold.
6760 */
6761 else if (cap->nchar == 'z')
6762 {
6763 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6764 cap->count1) == FAIL)
6765 clearopbeep(cap->oap);
6766 }
6767#endif
6768
6769#ifdef FEAT_DIFF
6770 /*
6771 * "[c" and "]c": move to next or previous diff-change.
6772 */
6773 else if (cap->nchar == 'c')
6774 {
6775 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6776 cap->count1) == FAIL)
6777 clearopbeep(cap->oap);
6778 }
6779#endif
6780
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006781#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006782 /*
6783 * "[s", "[S", "]s" and "]S": move to next spell error.
6784 */
6785 else if (cap->nchar == 's' || cap->nchar == 'S')
6786 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006787 setpcmark();
6788 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006789 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6790 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006791 {
6792 clearopbeep(cap->oap);
6793 break;
6794 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006795# ifdef FEAT_FOLDING
6796 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6797 foldOpenCursor();
6798# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006799 }
6800#endif
6801
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 /* Not a valid cap->nchar. */
6803 else
6804 clearopbeep(cap->oap);
6805}
6806
6807/*
6808 * Handle Normal mode "%" command.
6809 */
6810 static void
6811nv_percent(cap)
6812 cmdarg_T *cap;
6813{
6814 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006815#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 linenr_T lnum = curwin->w_cursor.lnum;
6817#endif
6818
6819 cap->oap->inclusive = TRUE;
6820 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6821 {
6822 if (cap->count0 > 100)
6823 clearopbeep(cap->oap);
6824 else
6825 {
6826 cap->oap->motion_type = MLINE;
6827 setpcmark();
6828 /* Round up, so CTRL-G will give same value. Watch out for a
6829 * large line count, the line number must not go negative! */
6830 if (curbuf->b_ml.ml_line_count > 1000000)
6831 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6832 / 100L * cap->count0;
6833 else
6834 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6835 cap->count0 + 99L) / 100L;
6836 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6838 beginline(BL_SOL | BL_FIX);
6839 }
6840 }
6841 else /* "%" : go to matching paren */
6842 {
6843 cap->oap->motion_type = MCHAR;
6844 cap->oap->use_reg_one = TRUE;
6845 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6846 clearopbeep(cap->oap);
6847 else
6848 {
6849 setpcmark();
6850 curwin->w_cursor = *pos;
6851 curwin->w_set_curswant = TRUE;
6852#ifdef FEAT_VIRTUALEDIT
6853 curwin->w_cursor.coladd = 0;
6854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
6857 }
6858#ifdef FEAT_FOLDING
6859 if (cap->oap->op_type == OP_NOP
6860 && lnum != curwin->w_cursor.lnum
6861 && (fdo_flags & FDO_PERCENT)
6862 && KeyTyped)
6863 foldOpenCursor();
6864#endif
6865}
6866
6867/*
6868 * Handle "(" and ")" commands.
6869 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6870 */
6871 static void
6872nv_brace(cap)
6873 cmdarg_T *cap;
6874{
6875 cap->oap->motion_type = MCHAR;
6876 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006877 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6878 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 curwin->w_set_curswant = TRUE;
6880
6881 if (findsent(cap->arg, cap->count1) == FAIL)
6882 clearopbeep(cap->oap);
6883 else
6884 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006885 /* Don't leave the cursor on the NUL past end of line. */
6886 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887#ifdef FEAT_VIRTUALEDIT
6888 curwin->w_cursor.coladd = 0;
6889#endif
6890#ifdef FEAT_FOLDING
6891 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6892 foldOpenCursor();
6893#endif
6894 }
6895}
6896
6897/*
6898 * "m" command: Mark a position.
6899 */
6900 static void
6901nv_mark(cap)
6902 cmdarg_T *cap;
6903{
6904 if (!checkclearop(cap->oap))
6905 {
6906 if (setmark(cap->nchar) == FAIL)
6907 clearopbeep(cap->oap);
6908 }
6909}
6910
6911/*
6912 * "{" and "}" commands.
6913 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6914 */
6915 static void
6916nv_findpar(cap)
6917 cmdarg_T *cap;
6918{
6919 cap->oap->motion_type = MCHAR;
6920 cap->oap->inclusive = FALSE;
6921 cap->oap->use_reg_one = TRUE;
6922 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006923 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 clearopbeep(cap->oap);
6925 else
6926 {
6927#ifdef FEAT_VIRTUALEDIT
6928 curwin->w_cursor.coladd = 0;
6929#endif
6930#ifdef FEAT_FOLDING
6931 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6932 foldOpenCursor();
6933#endif
6934 }
6935}
6936
6937/*
6938 * "u" command: Undo or make lower case.
6939 */
6940 static void
6941nv_undo(cap)
6942 cmdarg_T *cap;
6943{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006944 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6947 cap->cmdchar = 'g';
6948 cap->nchar = 'u';
6949 nv_operator(cap);
6950 }
6951 else
6952 nv_kundo(cap);
6953}
6954
6955/*
6956 * <Undo> command.
6957 */
6958 static void
6959nv_kundo(cap)
6960 cmdarg_T *cap;
6961{
6962 if (!checkclearopq(cap->oap))
6963 {
6964 u_undo((int)cap->count1);
6965 curwin->w_set_curswant = TRUE;
6966 }
6967}
6968
6969/*
6970 * Handle the "r" command.
6971 */
6972 static void
6973nv_replace(cap)
6974 cmdarg_T *cap;
6975{
6976 char_u *ptr;
6977 int had_ctrl_v;
6978 long n;
6979
6980 if (checkclearop(cap->oap))
6981 return;
6982
6983 /* get another character */
6984 if (cap->nchar == Ctrl_V)
6985 {
6986 had_ctrl_v = Ctrl_V;
6987 cap->nchar = get_literal();
6988 /* Don't redo a multibyte character with CTRL-V. */
6989 if (cap->nchar > DEL)
6990 had_ctrl_v = NUL;
6991 }
6992 else
6993 had_ctrl_v = NUL;
6994
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006995 /* Abort if the character is a special key. */
6996 if (IS_SPECIAL(cap->nchar))
6997 {
6998 clearopbeep(cap->oap);
6999 return;
7000 }
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 /* Visual mode "r" */
7003 if (VIsual_active)
7004 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007005 if (got_int)
7006 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007007 if (had_ctrl_v)
7008 {
7009 if (cap->nchar == '\r')
7010 cap->nchar = -1;
7011 else if (cap->nchar == '\n')
7012 cap->nchar = -2;
7013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 nv_operator(cap);
7015 return;
7016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018#ifdef FEAT_VIRTUALEDIT
7019 /* Break tabs, etc. */
7020 if (virtual_active())
7021 {
7022 if (u_save_cursor() == FAIL)
7023 return;
7024 if (gchar_cursor() == NUL)
7025 {
7026 /* Add extra space and put the cursor on the first one. */
7027 coladvance_force((colnr_T)(getviscol() + cap->count1));
7028 curwin->w_cursor.col -= cap->count1;
7029 }
7030 else if (gchar_cursor() == TAB)
7031 coladvance_force(getviscol());
7032 }
7033#endif
7034
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007035 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007037 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038#ifdef FEAT_MBYTE
7039 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7040#endif
7041 )
7042 {
7043 clearopbeep(cap->oap);
7044 return;
7045 }
7046
7047 /*
7048 * Replacing with a TAB is done by edit() when it is complicated because
7049 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7050 * Other characters are done below to avoid problems with things like
7051 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7052 */
7053 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7054 {
7055 stuffnumReadbuff(cap->count1);
7056 stuffcharReadbuff('R');
7057 stuffcharReadbuff('\t');
7058 stuffcharReadbuff(ESC);
7059 return;
7060 }
7061
7062 /* save line for undo */
7063 if (u_save_cursor() == FAIL)
7064 return;
7065
7066 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7067 {
7068 /*
7069 * Replace character(s) by a single newline.
7070 * Strange vi behaviour: Only one newline is inserted.
7071 * Delete the characters here.
7072 * Insert the newline with an insert command, takes care of
7073 * autoindent. The insert command depends on being on the last
7074 * character of a line or not.
7075 */
7076#ifdef FEAT_MBYTE
7077 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7078#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007079 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#endif
7081 stuffcharReadbuff('\r');
7082 stuffcharReadbuff(ESC);
7083
7084 /* Give 'r' to edit(), to get the redo command right. */
7085 invoke_edit(cap, TRUE, 'r', FALSE);
7086 }
7087 else
7088 {
7089 prep_redo(cap->oap->regname, cap->count1,
7090 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7091
7092 curbuf->b_op_start = curwin->w_cursor;
7093#ifdef FEAT_MBYTE
7094 if (has_mbyte)
7095 {
7096 int old_State = State;
7097
7098 if (cap->ncharC1 != 0)
7099 AppendCharToRedobuff(cap->ncharC1);
7100 if (cap->ncharC2 != 0)
7101 AppendCharToRedobuff(cap->ncharC2);
7102
7103 /* This is slow, but it handles replacing a single-byte with a
7104 * multi-byte and the other way around. Also handles adding
7105 * composing characters for utf-8. */
7106 for (n = cap->count1; n > 0; --n)
7107 {
7108 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007109 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7110 {
7111 int c = ins_copychar(curwin->w_cursor.lnum
7112 + (cap->nchar == Ctrl_Y ? -1 : 1));
7113 if (c != NUL)
7114 ins_char(c);
7115 else
7116 /* will be decremented further down */
7117 ++curwin->w_cursor.col;
7118 }
7119 else
7120 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 State = old_State;
7122 if (cap->ncharC1 != 0)
7123 ins_char(cap->ncharC1);
7124 if (cap->ncharC2 != 0)
7125 ins_char(cap->ncharC2);
7126 }
7127 }
7128 else
7129#endif
7130 {
7131 /*
7132 * Replace the characters within one line.
7133 */
7134 for (n = cap->count1; n > 0; --n)
7135 {
7136 /*
7137 * Get ptr again, because u_save and/or showmatch() will have
7138 * released the line. At the same time we let know that the
7139 * line will be changed.
7140 */
7141 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007142 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7143 {
7144 int c = ins_copychar(curwin->w_cursor.lnum
7145 + (cap->nchar == Ctrl_Y ? -1 : 1));
7146 if (c != NUL)
7147 ptr[curwin->w_cursor.col] = c;
7148 }
7149 else
7150 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (p_sm && msg_silent == 0)
7152 showmatch(cap->nchar);
7153 ++curwin->w_cursor.col;
7154 }
7155#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007156 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007158 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar009b2592004-10-24 19:18:58 +00007160 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007161 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007163 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
7165#endif
7166
7167 /* mark the buffer as changed and prepare for displaying */
7168 changed_bytes(curwin->w_cursor.lnum,
7169 (colnr_T)(curwin->w_cursor.col - cap->count1));
7170 }
7171 --curwin->w_cursor.col; /* cursor on the last replaced char */
7172#ifdef FEAT_MBYTE
7173 /* if the character on the left of the current cursor is a multi-byte
7174 * character, move two characters left */
7175 if (has_mbyte)
7176 mb_adjust_cursor();
7177#endif
7178 curbuf->b_op_end = curwin->w_cursor;
7179 curwin->w_set_curswant = TRUE;
7180 set_last_insert(cap->nchar);
7181 }
7182}
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184/*
7185 * 'o': Exchange start and end of Visual area.
7186 * 'O': same, but in block mode exchange left and right corners.
7187 */
7188 static void
7189v_swap_corners(cmdchar)
7190 int cmdchar;
7191{
7192 pos_T old_cursor;
7193 colnr_T left, right;
7194
7195 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7196 {
7197 old_cursor = curwin->w_cursor;
7198 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7199 curwin->w_cursor.lnum = VIsual.lnum;
7200 coladvance(left);
7201 VIsual = curwin->w_cursor;
7202
7203 curwin->w_cursor.lnum = old_cursor.lnum;
7204 curwin->w_curswant = right;
7205 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7206 * right one column */
7207 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7208 ++curwin->w_curswant;
7209 coladvance(curwin->w_curswant);
7210 if (curwin->w_cursor.col == old_cursor.col
7211#ifdef FEAT_VIRTUALEDIT
7212 && (!virtual_active()
7213 || curwin->w_cursor.coladd == old_cursor.coladd)
7214#endif
7215 )
7216 {
7217 curwin->w_cursor.lnum = VIsual.lnum;
7218 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7219 ++right;
7220 coladvance(right);
7221 VIsual = curwin->w_cursor;
7222
7223 curwin->w_cursor.lnum = old_cursor.lnum;
7224 coladvance(left);
7225 curwin->w_curswant = left;
7226 }
7227 }
7228 else
7229 {
7230 old_cursor = curwin->w_cursor;
7231 curwin->w_cursor = VIsual;
7232 VIsual = old_cursor;
7233 curwin->w_set_curswant = TRUE;
7234 }
7235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237/*
7238 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7239 */
7240 static void
7241nv_Replace(cap)
7242 cmdarg_T *cap;
7243{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (VIsual_active) /* "R" is replace lines */
7245 {
7246 cap->cmdchar = 'c';
7247 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007248 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 VIsual_mode = 'V';
7250 nv_operator(cap);
7251 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007252 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
7254 if (!curbuf->b_p_ma)
7255 EMSG(_(e_modifiable));
7256 else
7257 {
7258#ifdef FEAT_VIRTUALEDIT
7259 if (virtual_active())
7260 coladvance(getviscol());
7261#endif
7262 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7263 }
7264 }
7265}
7266
7267#ifdef FEAT_VREPLACE
7268/*
7269 * "gr".
7270 */
7271 static void
7272nv_vreplace(cap)
7273 cmdarg_T *cap;
7274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (VIsual_active)
7276 {
7277 cap->cmdchar = 'r';
7278 cap->nchar = cap->extra_char;
7279 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007281 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 {
7283 if (!curbuf->b_p_ma)
7284 EMSG(_(e_modifiable));
7285 else
7286 {
7287 if (cap->extra_char == Ctrl_V) /* get another character */
7288 cap->extra_char = get_literal();
7289 stuffcharReadbuff(cap->extra_char);
7290 stuffcharReadbuff(ESC);
7291# ifdef FEAT_VIRTUALEDIT
7292 if (virtual_active())
7293 coladvance(getviscol());
7294# endif
7295 invoke_edit(cap, TRUE, 'v', FALSE);
7296 }
7297 }
7298}
7299#endif
7300
7301/*
7302 * Swap case for "~" command, when it does not work like an operator.
7303 */
7304 static void
7305n_swapchar(cap)
7306 cmdarg_T *cap;
7307{
7308 long n;
7309 pos_T startpos;
7310 int did_change = 0;
7311#ifdef FEAT_NETBEANS_INTG
7312 pos_T pos;
7313 char_u *ptr;
7314 int count;
7315#endif
7316
7317 if (checkclearopq(cap->oap))
7318 return;
7319
7320 if (lineempty(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
7321 {
7322 clearopbeep(cap->oap);
7323 return;
7324 }
7325
7326 prep_redo_cmd(cap);
7327
7328 if (u_save_cursor() == FAIL)
7329 return;
7330
7331 startpos = curwin->w_cursor;
7332#ifdef FEAT_NETBEANS_INTG
7333 pos = startpos;
7334#endif
7335 for (n = cap->count1; n > 0; --n)
7336 {
7337 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7338 inc_cursor();
7339 if (gchar_cursor() == NUL)
7340 {
7341 if (vim_strchr(p_ww, '~') != NULL
7342 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7343 {
7344#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007345 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 if (did_change)
7348 {
7349 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007350 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007351 netbeans_removed(curbuf, pos.lnum, pos.col,
7352 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007354 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356 pos.col = 0;
7357 pos.lnum++;
7358 }
7359#endif
7360 ++curwin->w_cursor.lnum;
7361 curwin->w_cursor.col = 0;
7362 if (n > 1)
7363 {
7364 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7365 break;
7366 u_clearline();
7367 }
7368 }
7369 else
7370 break;
7371 }
7372 }
7373#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007374 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
7376 ptr = ml_get(pos.lnum);
7377 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007378 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7379 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381#endif
7382
7383
7384 check_cursor();
7385 curwin->w_set_curswant = TRUE;
7386 if (did_change)
7387 {
7388 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7389 0L);
7390 curbuf->b_op_start = startpos;
7391 curbuf->b_op_end = curwin->w_cursor;
7392 if (curbuf->b_op_end.col > 0)
7393 --curbuf->b_op_end.col;
7394 }
7395}
7396
7397/*
7398 * Move cursor to mark.
7399 */
7400 static void
7401nv_cursormark(cap, flag, pos)
7402 cmdarg_T *cap;
7403 int flag;
7404 pos_T *pos;
7405{
7406 if (check_mark(pos) == FAIL)
7407 clearop(cap->oap);
7408 else
7409 {
7410 if (cap->cmdchar == '\''
7411 || cap->cmdchar == '`'
7412 || cap->cmdchar == '['
7413 || cap->cmdchar == ']')
7414 setpcmark();
7415 curwin->w_cursor = *pos;
7416 if (flag)
7417 beginline(BL_WHITE | BL_FIX);
7418 else
7419 check_cursor();
7420 }
7421 cap->oap->motion_type = flag ? MLINE : MCHAR;
7422 if (cap->cmdchar == '`')
7423 cap->oap->use_reg_one = TRUE;
7424 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7425 curwin->w_set_curswant = TRUE;
7426}
7427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428/*
7429 * Handle commands that are operators in Visual mode.
7430 */
7431 static void
7432v_visop(cap)
7433 cmdarg_T *cap;
7434{
7435 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7436
7437 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007438 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (isupper(cap->cmdchar))
7440 {
7441 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007442 {
7443 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7447 curwin->w_curswant = MAXCOL;
7448 }
7449 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7450 nv_operator(cap);
7451}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453/*
7454 * "s" and "S" commands.
7455 */
7456 static void
7457nv_subst(cap)
7458 cmdarg_T *cap;
7459{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7461 {
7462 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007463 {
7464 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 cap->cmdchar = 'c';
7468 nv_operator(cap);
7469 }
7470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 nv_optrans(cap);
7472}
7473
7474/*
7475 * Abbreviated commands.
7476 */
7477 static void
7478nv_abbrev(cap)
7479 cmdarg_T *cap;
7480{
7481 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7482 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 /* in Visual mode these commands are operators */
7485 if (VIsual_active)
7486 v_visop(cap);
7487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 nv_optrans(cap);
7489}
7490
7491/*
7492 * Translate a command into another command.
7493 */
7494 static void
7495nv_optrans(cap)
7496 cmdarg_T *cap;
7497{
7498 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7499 (char_u *)"d$", (char_u *)"c$",
7500 (char_u *)"cl", (char_u *)"cc",
7501 (char_u *)"yy", (char_u *)":s\r"};
7502 static char_u *str = (char_u *)"xXDCsSY&";
7503
7504 if (!checkclearopq(cap->oap))
7505 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007506 /* In Vi "2D" doesn't delete the next line. Can't translate it
7507 * either, because "2." should also not use the count. */
7508 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7509 {
7510 cap->oap->start = curwin->w_cursor;
7511 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007512#ifdef FEAT_EVAL
7513 set_op_var(OP_DELETE);
7514#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007515 cap->count1 = 1;
7516 nv_dollar(cap);
7517 finish_op = TRUE;
7518 ResetRedobuff();
7519 AppendCharToRedobuff('D');
7520 }
7521 else
7522 {
7523 if (cap->count0)
7524 stuffnumReadbuff(cap->count0);
7525 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 }
7528 cap->opcount = 0;
7529}
7530
7531/*
7532 * "'" and "`" commands. Also for "g'" and "g`".
7533 * cap->arg is TRUE for "'" and "g'".
7534 */
7535 static void
7536nv_gomark(cap)
7537 cmdarg_T *cap;
7538{
7539 pos_T *pos;
7540 int c;
7541#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007542 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7544#endif
7545
7546 if (cap->cmdchar == 'g')
7547 c = cap->extra_char;
7548 else
7549 c = cap->nchar;
7550 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7551 if (pos == (pos_T *)-1) /* jumped to other file */
7552 {
7553 if (cap->arg)
7554 {
7555 check_cursor_lnum();
7556 beginline(BL_WHITE | BL_FIX);
7557 }
7558 else
7559 check_cursor();
7560 }
7561 else
7562 nv_cursormark(cap, cap->arg, pos);
7563
7564#ifdef FEAT_VIRTUALEDIT
7565 /* May need to clear the coladd that a mark includes. */
7566 if (!virtual_active())
7567 curwin->w_cursor.coladd = 0;
7568#endif
7569#ifdef FEAT_FOLDING
7570 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007571 && pos != NULL
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007572 && (pos == (pos_T *)-1 || !equalpos(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 && (fdo_flags & FDO_MARK)
7574 && old_KeyTyped)
7575 foldOpenCursor();
7576#endif
7577}
7578
7579/*
7580 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7581 */
7582 static void
7583nv_pcmark(cap)
7584 cmdarg_T *cap;
7585{
7586#ifdef FEAT_JUMPLIST
7587 pos_T *pos;
7588# ifdef FEAT_FOLDING
7589 linenr_T lnum = curwin->w_cursor.lnum;
7590 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7591# endif
7592
7593 if (!checkclearopq(cap->oap))
7594 {
7595 if (cap->cmdchar == 'g')
7596 pos = movechangelist((int)cap->count1);
7597 else
7598 pos = movemark((int)cap->count1);
7599 if (pos == (pos_T *)-1) /* jump to other file */
7600 {
7601 curwin->w_set_curswant = TRUE;
7602 check_cursor();
7603 }
7604 else if (pos != NULL) /* can jump */
7605 nv_cursormark(cap, FALSE, pos);
7606 else if (cap->cmdchar == 'g')
7607 {
7608 if (curbuf->b_changelistlen == 0)
7609 EMSG(_("E664: changelist is empty"));
7610 else if (cap->count1 < 0)
7611 EMSG(_("E662: At start of changelist"));
7612 else
7613 EMSG(_("E663: At end of changelist"));
7614 }
7615 else
7616 clearopbeep(cap->oap);
7617# ifdef FEAT_FOLDING
7618 if (cap->oap->op_type == OP_NOP
7619 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7620 && (fdo_flags & FDO_MARK)
7621 && old_KeyTyped)
7622 foldOpenCursor();
7623# endif
7624 }
7625#else
7626 clearopbeep(cap->oap);
7627#endif
7628}
7629
7630/*
7631 * Handle '"' command.
7632 */
7633 static void
7634nv_regname(cap)
7635 cmdarg_T *cap;
7636{
7637 if (checkclearop(cap->oap))
7638 return;
7639#ifdef FEAT_EVAL
7640 if (cap->nchar == '=')
7641 cap->nchar = get_expr_register();
7642#endif
7643 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7644 {
7645 cap->oap->regname = cap->nchar;
7646 cap->opcount = cap->count0; /* remember count before '"' */
7647#ifdef FEAT_EVAL
7648 set_reg_var(cap->oap->regname);
7649#endif
7650 }
7651 else
7652 clearopbeep(cap->oap);
7653}
7654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655/*
7656 * Handle "v", "V" and "CTRL-V" commands.
7657 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7658 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 */
7661 static void
7662nv_visual(cap)
7663 cmdarg_T *cap;
7664{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007665 if (cap->cmdchar == Ctrl_Q)
7666 cap->cmdchar = Ctrl_V;
7667
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7669 * characterwise, linewise, or blockwise. */
7670 if (cap->oap->op_type != OP_NOP)
7671 {
7672 cap->oap->motion_force = cap->cmdchar;
7673 finish_op = FALSE; /* operator doesn't finish now but later */
7674 return;
7675 }
7676
7677 VIsual_select = cap->arg;
7678 if (VIsual_active) /* change Visual mode */
7679 {
7680 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7681 end_visual_mode();
7682 else /* toggle char/block mode */
7683 { /* or char/line mode */
7684 VIsual_mode = cap->cmdchar;
7685 showmode();
7686 }
7687 redraw_curbuf_later(INVERTED); /* update the inversion */
7688 }
7689 else /* start Visual mode */
7690 {
7691 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007692 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007694 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 VIsual = curwin->w_cursor;
7696
7697 VIsual_active = TRUE;
7698 VIsual_reselect = TRUE;
7699 if (!cap->arg)
7700 /* start Select mode when 'selectmode' contains "cmd" */
7701 may_start_select('c');
7702#ifdef FEAT_MOUSE
7703 setmouse();
7704#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007705 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 redraw_cmdline = TRUE; /* show visual mode later */
7707 /*
7708 * For V and ^V, we multiply the number of lines even if there
7709 * was only one -- webb
7710 */
7711 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7712 {
7713 curwin->w_cursor.lnum +=
7714 resel_VIsual_line_count * cap->count0 - 1;
7715 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7716 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7717 }
7718 VIsual_mode = resel_VIsual_mode;
7719 if (VIsual_mode == 'v')
7720 {
7721 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007722 {
7723 validate_virtcol();
7724 curwin->w_curswant = curwin->w_virtcol
7725 + resel_VIsual_vcol * cap->count0 - 1;
7726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007728 curwin->w_curswant = resel_VIsual_vcol;
7729 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007731 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
7733 curwin->w_curswant = MAXCOL;
7734 coladvance((colnr_T)MAXCOL);
7735 }
7736 else if (VIsual_mode == Ctrl_V)
7737 {
7738 validate_virtcol();
7739 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007740 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 coladvance(curwin->w_curswant);
7742 }
7743 else
7744 curwin->w_set_curswant = TRUE;
7745 redraw_curbuf_later(INVERTED); /* show the inversion */
7746 }
7747 else
7748 {
7749 if (!cap->arg)
7750 /* start Select mode when 'selectmode' contains "cmd" */
7751 may_start_select('c');
7752 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007753 if (VIsual_mode != 'V' && *p_sel == 'e')
7754 ++cap->count1; /* include one more char */
7755 if (cap->count0 > 0 && --cap->count1 > 0)
7756 {
7757 /* With a count select that many characters or lines. */
7758 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7759 nv_right(cap);
7760 else if (VIsual_mode == 'V')
7761 nv_down(cap);
7762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764 }
7765}
7766
7767/*
7768 * Start selection for Shift-movement keys.
7769 */
7770 void
7771start_selection()
7772{
7773 /* if 'selectmode' contains "key", start Select mode */
7774 may_start_select('k');
7775 n_start_visual_mode('v');
7776}
7777
7778/*
7779 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7780 */
7781 void
7782may_start_select(c)
7783 int c;
7784{
7785 VIsual_select = (stuff_empty() && typebuf_typed()
7786 && (vim_strchr(p_slm, c) != NULL));
7787}
7788
7789/*
7790 * Start Visual mode "c".
7791 * Should set VIsual_select before calling this.
7792 */
7793 static void
7794n_start_visual_mode(c)
7795 int c;
7796{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007797#ifdef FEAT_CONCEAL
7798 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007799 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007800#endif
7801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 VIsual_mode = c;
7803 VIsual_active = TRUE;
7804 VIsual_reselect = TRUE;
7805#ifdef FEAT_VIRTUALEDIT
7806 /* Corner case: the 0 position in a tab may change when going into
7807 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7808 */
7809 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007810 {
7811 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814#endif
7815 VIsual = curwin->w_cursor;
7816
7817#ifdef FEAT_FOLDING
7818 foldAdjustVisual();
7819#endif
7820
7821#ifdef FEAT_MOUSE
7822 setmouse();
7823#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007824#ifdef FEAT_CONCEAL
7825 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007826 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007827#endif
7828
Bram Moolenaar09df3122006-01-23 22:23:09 +00007829 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 redraw_cmdline = TRUE; /* show visual mode later */
7831#ifdef FEAT_CLIPBOARD
7832 /* Make sure the clipboard gets updated. Needed because start and
7833 * end may still be the same, and the selection needs to be owned */
7834 clip_star.vmode = NUL;
7835#endif
7836
7837 /* Only need to redraw this line, unless still need to redraw an old
7838 * Visual area (when 'lazyredraw' is set). */
7839 if (curwin->w_redr_type < INVERTED)
7840 {
7841 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7842 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7843 }
7844}
7845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
7847/*
7848 * CTRL-W: Window commands
7849 */
7850 static void
7851nv_window(cap)
7852 cmdarg_T *cap;
7853{
7854#ifdef FEAT_WINDOWS
7855 if (!checkclearop(cap->oap))
7856 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7857#else
7858 (void)checkclearop(cap->oap);
7859#endif
7860}
7861
7862/*
7863 * CTRL-Z: Suspend
7864 */
7865 static void
7866nv_suspend(cap)
7867 cmdarg_T *cap;
7868{
7869 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (VIsual_active)
7871 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 do_cmdline_cmd((char_u *)"st");
7873}
7874
7875/*
7876 * Commands starting with "g".
7877 */
7878 static void
7879nv_g_cmd(cap)
7880 cmdarg_T *cap;
7881{
7882 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 int i;
7885 int flag = FALSE;
7886
7887 switch (cap->nchar)
7888 {
7889#ifdef MEM_PROFILE
7890 /*
7891 * "g^A": dump log of used memory.
7892 */
7893 case Ctrl_A:
7894 vim_mem_profile_dump();
7895 break;
7896#endif
7897
7898#ifdef FEAT_VREPLACE
7899 /*
7900 * "gR": Enter virtual replace mode.
7901 */
7902 case 'R':
7903 cap->arg = TRUE;
7904 nv_Replace(cap);
7905 break;
7906
7907 case 'r':
7908 nv_vreplace(cap);
7909 break;
7910#endif
7911
7912 case '&':
7913 do_cmdline_cmd((char_u *)"%s//~/&");
7914 break;
7915
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 /*
7917 * "gv": Reselect the previous Visual area. If Visual already active,
7918 * exchange previous and current Visual area.
7919 */
7920 case 'v':
7921 if (checkclearop(oap))
7922 break;
7923
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007924 if ( curbuf->b_visual.vi_start.lnum == 0
7925 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7926 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 beep_flush();
7928 else
7929 {
7930 /* set w_cursor to the start of the Visual area, tpos to the end */
7931 if (VIsual_active)
7932 {
7933 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007934 VIsual_mode = curbuf->b_visual.vi_mode;
7935 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936# ifdef FEAT_EVAL
7937 curbuf->b_visual_mode_eval = i;
7938# endif
7939 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007940 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7941 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007943 tpos = curbuf->b_visual.vi_end;
7944 curbuf->b_visual.vi_end = curwin->w_cursor;
7945 curwin->w_cursor = curbuf->b_visual.vi_start;
7946 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 }
7948 else
7949 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007950 VIsual_mode = curbuf->b_visual.vi_mode;
7951 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7952 tpos = curbuf->b_visual.vi_end;
7953 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 }
7955
7956 VIsual_active = TRUE;
7957 VIsual_reselect = TRUE;
7958
7959 /* Set Visual to the start and w_cursor to the end of the Visual
7960 * area. Make sure they are on an existing character. */
7961 check_cursor();
7962 VIsual = curwin->w_cursor;
7963 curwin->w_cursor = tpos;
7964 check_cursor();
7965 update_topline();
7966 /*
7967 * When called from normal "g" command: start Select mode when
7968 * 'selectmode' contains "cmd". When called for K_SELECT, always
7969 * start Select mode.
7970 */
7971 if (cap->arg)
7972 VIsual_select = TRUE;
7973 else
7974 may_start_select('c');
7975#ifdef FEAT_MOUSE
7976 setmouse();
7977#endif
7978#ifdef FEAT_CLIPBOARD
7979 /* Make sure the clipboard gets updated. Needed because start and
7980 * end are still the same, and the selection needs to be owned */
7981 clip_star.vmode = NUL;
7982#endif
7983 redraw_curbuf_later(INVERTED);
7984 showmode();
7985 }
7986 break;
7987 /*
7988 * "gV": Don't reselect the previous Visual area after a Select mode
7989 * mapping of menu.
7990 */
7991 case 'V':
7992 VIsual_reselect = FALSE;
7993 break;
7994
7995 /*
7996 * "gh": start Select mode.
7997 * "gH": start Select line mode.
7998 * "g^H": start Select block mode.
7999 */
8000 case K_BS:
8001 cap->nchar = Ctrl_H;
8002 /* FALLTHROUGH */
8003 case 'h':
8004 case 'H':
8005 case Ctrl_H:
8006# ifdef EBCDIC
8007 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8008 if (cap->nchar == Ctrl_H)
8009 cap->cmdchar = Ctrl_V;
8010 else
8011# endif
8012 cap->cmdchar = cap->nchar + ('v' - 'h');
8013 cap->arg = TRUE;
8014 nv_visual(cap);
8015 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008016
8017 /* "gn", "gN" visually select next/previous search match
8018 * "gn" selects next match
8019 * "gN" selects previous match
8020 */
8021 case 'N':
8022 case 'n':
8023 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008024 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
8027 /*
8028 * "gj" and "gk" two new funny movement keys -- up and down
8029 * movement based on *screen* line rather than *file* line.
8030 */
8031 case 'j':
8032 case K_DOWN:
8033 /* with 'nowrap' it works just like the normal "j" command; also when
8034 * in a closed fold */
8035 if (!curwin->w_p_wrap
8036#ifdef FEAT_FOLDING
8037 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8038#endif
8039 )
8040 {
8041 oap->motion_type = MLINE;
8042 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8043 }
8044 else
8045 i = nv_screengo(oap, FORWARD, cap->count1);
8046 if (i == FAIL)
8047 clearopbeep(oap);
8048 break;
8049
8050 case 'k':
8051 case K_UP:
8052 /* with 'nowrap' it works just like the normal "k" command; also when
8053 * in a closed fold */
8054 if (!curwin->w_p_wrap
8055#ifdef FEAT_FOLDING
8056 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8057#endif
8058 )
8059 {
8060 oap->motion_type = MLINE;
8061 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8062 }
8063 else
8064 i = nv_screengo(oap, BACKWARD, cap->count1);
8065 if (i == FAIL)
8066 clearopbeep(oap);
8067 break;
8068
8069 /*
8070 * "gJ": join two lines without inserting a space.
8071 */
8072 case 'J':
8073 nv_join(cap);
8074 break;
8075
8076 /*
8077 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8078 * "gm": middle of "g0" and "g$".
8079 */
8080 case '^':
8081 flag = TRUE;
8082 /* FALLTHROUGH */
8083
8084 case '0':
8085 case 'm':
8086 case K_HOME:
8087 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 oap->motion_type = MCHAR;
8089 oap->inclusive = FALSE;
8090 if (curwin->w_p_wrap
8091#ifdef FEAT_VERTSPLIT
8092 && curwin->w_width != 0
8093#endif
8094 )
8095 {
8096 int width1 = W_WIDTH(curwin) - curwin_col_off();
8097 int width2 = width1 + curwin_col_off2();
8098
8099 validate_virtcol();
8100 i = 0;
8101 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8102 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8103 }
8104 else
8105 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008106 /* Go to the middle of the screen line. When 'number' or
8107 * 'relativenumber' is on and lines are wrapping the middle can be more
8108 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 if (cap->nchar == 'm')
8110 i += (W_WIDTH(curwin) - curwin_col_off()
8111 + ((curwin->w_p_wrap && i > 0)
8112 ? curwin_col_off2() : 0)) / 2;
8113 coladvance((colnr_T)i);
8114 if (flag)
8115 {
8116 do
8117 i = gchar_cursor();
8118 while (vim_iswhite(i) && oneright() == OK);
8119 }
8120 curwin->w_set_curswant = TRUE;
8121 break;
8122
8123 case '_':
8124 /* "g_": to the last non-blank character in the line or <count> lines
8125 * downward. */
8126 cap->oap->motion_type = MCHAR;
8127 cap->oap->inclusive = TRUE;
8128 curwin->w_curswant = MAXCOL;
8129 if (cursor_down((long)(cap->count1 - 1),
8130 cap->oap->op_type == OP_NOP) == FAIL)
8131 clearopbeep(cap->oap);
8132 else
8133 {
8134 char_u *ptr = ml_get_curline();
8135
8136 /* In Visual mode we may end up after the line. */
8137 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8138 --curwin->w_cursor.col;
8139
8140 /* Decrease the cursor column until it's on a non-blank. */
8141 while (curwin->w_cursor.col > 0
8142 && vim_iswhite(ptr[curwin->w_cursor.col]))
8143 --curwin->w_cursor.col;
8144 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008145 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147 break;
8148
8149 case '$':
8150 case K_END:
8151 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
8153 int col_off = curwin_col_off();
8154
8155 oap->motion_type = MCHAR;
8156 oap->inclusive = TRUE;
8157 if (curwin->w_p_wrap
8158#ifdef FEAT_VERTSPLIT
8159 && curwin->w_width != 0
8160#endif
8161 )
8162 {
8163 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8164 if (cap->count1 == 1)
8165 {
8166 int width1 = W_WIDTH(curwin) - col_off;
8167 int width2 = width1 + curwin_col_off2();
8168
8169 validate_virtcol();
8170 i = width1 - 1;
8171 if (curwin->w_virtcol >= (colnr_T)width1)
8172 i += ((curwin->w_virtcol - width1) / width2 + 1)
8173 * width2;
8174 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008175
8176 /* Make sure we stick in this column. */
8177 validate_virtcol();
8178 curwin->w_curswant = curwin->w_virtcol;
8179 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8181 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8182 {
8183 /*
8184 * Check for landing on a character that got split at
8185 * the end of the line. We do not want to advance to
8186 * the next screen line.
8187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (curwin->w_virtcol > (colnr_T)i)
8189 --curwin->w_cursor.col;
8190 }
8191#endif
8192 }
8193 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8194 clearopbeep(oap);
8195 }
8196 else
8197 {
8198 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8199 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008200
8201 /* Make sure we stick in this column. */
8202 validate_virtcol();
8203 curwin->w_curswant = curwin->w_virtcol;
8204 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206 }
8207 break;
8208
8209 /*
8210 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8211 */
8212 case '*':
8213 case '#':
8214#if POUND != '#'
8215 case POUND: /* pound sign (sometimes equal to '#') */
8216#endif
8217 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8218 case ']': /* :tselect for current identifier */
8219 nv_ident(cap);
8220 break;
8221
8222 /*
8223 * ge and gE: go back to end of word
8224 */
8225 case 'e':
8226 case 'E':
8227 oap->motion_type = MCHAR;
8228 curwin->w_set_curswant = TRUE;
8229 oap->inclusive = TRUE;
8230 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8231 clearopbeep(oap);
8232 break;
8233
8234 /*
8235 * "g CTRL-G": display info about cursor position
8236 */
8237 case Ctrl_G:
8238 cursor_pos_info();
8239 break;
8240
8241 /*
8242 * "gi": start Insert at the last position.
8243 */
8244 case 'i':
8245 if (curbuf->b_last_insert.lnum != 0)
8246 {
8247 curwin->w_cursor = curbuf->b_last_insert;
8248 check_cursor_lnum();
8249 i = (int)STRLEN(ml_get_curline());
8250 if (curwin->w_cursor.col > (colnr_T)i)
8251 {
8252#ifdef FEAT_VIRTUALEDIT
8253 if (virtual_active())
8254 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8255#endif
8256 curwin->w_cursor.col = i;
8257 }
8258 }
8259 cap->cmdchar = 'i';
8260 nv_edit(cap);
8261 break;
8262
8263 /*
8264 * "gI": Start insert in column 1.
8265 */
8266 case 'I':
8267 beginline(0);
8268 if (!checkclearopq(oap))
8269 invoke_edit(cap, FALSE, 'g', FALSE);
8270 break;
8271
8272#ifdef FEAT_SEARCHPATH
8273 /*
8274 * "gf": goto file, edit file under cursor
8275 * "]f" and "[f": can also be used.
8276 */
8277 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008278 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 nv_gotofile(cap);
8280 break;
8281#endif
8282
8283 /* "g'm" and "g`m": jump to mark without setting pcmark */
8284 case '\'':
8285 cap->arg = TRUE;
8286 /*FALLTHROUGH*/
8287 case '`':
8288 nv_gomark(cap);
8289 break;
8290
8291 /*
8292 * "gs": Goto sleep.
8293 */
8294 case 's':
8295 do_sleep(cap->count1 * 1000L);
8296 break;
8297
8298 /*
8299 * "ga": Display the ascii value of the character under the
8300 * cursor. It is displayed in decimal, hex, and octal. -- webb
8301 */
8302 case 'a':
8303 do_ascii(NULL);
8304 break;
8305
8306#ifdef FEAT_MBYTE
8307 /*
8308 * "g8": Display the bytes used for the UTF-8 character under the
8309 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008310 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 */
8312 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008313 if (cap->count0 == 8)
8314 utf_find_illegal();
8315 else
8316 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
8318#endif
8319
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008320 case '<':
8321 show_sb_text();
8322 break;
8323
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 /*
8325 * "gg": Goto the first line in file. With a count it goes to
8326 * that line number like for "G". -- webb
8327 */
8328 case 'g':
8329 cap->arg = FALSE;
8330 nv_goto(cap);
8331 break;
8332
8333 /*
8334 * Two-character operators:
8335 * "gq" Format text
8336 * "gw" Format text and keep cursor position
8337 * "g~" Toggle the case of the text.
8338 * "gu" Change text to lower case.
8339 * "gU" Change text to upper case.
8340 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008341 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 */
8343 case 'q':
8344 case 'w':
8345 oap->cursor_start = curwin->w_cursor;
8346 /*FALLTHROUGH*/
8347 case '~':
8348 case 'u':
8349 case 'U':
8350 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008351 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 nv_operator(cap);
8353 break;
8354
8355 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008356 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 * current function
8358 * "gD": idem, but in the current file.
8359 */
8360 case 'd':
8361 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008362 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 break;
8364
8365#ifdef FEAT_MOUSE
8366 /*
8367 * g<*Mouse> : <C-*mouse>
8368 */
8369 case K_MIDDLEMOUSE:
8370 case K_MIDDLEDRAG:
8371 case K_MIDDLERELEASE:
8372 case K_LEFTMOUSE:
8373 case K_LEFTDRAG:
8374 case K_LEFTRELEASE:
8375 case K_RIGHTMOUSE:
8376 case K_RIGHTDRAG:
8377 case K_RIGHTRELEASE:
8378 case K_X1MOUSE:
8379 case K_X1DRAG:
8380 case K_X1RELEASE:
8381 case K_X2MOUSE:
8382 case K_X2DRAG:
8383 case K_X2RELEASE:
8384 mod_mask = MOD_MASK_CTRL;
8385 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8386 break;
8387#endif
8388
8389 case K_IGNORE:
8390 break;
8391
8392 /*
8393 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8394 */
8395 case 'p':
8396 case 'P':
8397 nv_put(cap);
8398 break;
8399
8400#ifdef FEAT_BYTEOFF
8401 /* "go": goto byte count from start of buffer */
8402 case 'o':
8403 goto_byte(cap->count0);
8404 break;
8405#endif
8406
8407 /* "gQ": improved Ex mode */
8408 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008409 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008412 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 break;
8414 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 if (!checkclearopq(oap))
8417 do_exmode(TRUE);
8418 break;
8419
8420#ifdef FEAT_JUMPLIST
8421 case ',':
8422 nv_pcmark(cap);
8423 break;
8424
8425 case ';':
8426 cap->count1 = -cap->count1;
8427 nv_pcmark(cap);
8428 break;
8429#endif
8430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008431#ifdef FEAT_WINDOWS
8432 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008433 if (!checkclearop(oap))
8434 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008435 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008436 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008437 if (!checkclearop(oap))
8438 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008439 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008440#endif
8441
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008442 case '+':
8443 case '-': /* "g+" and "g-": undo or redo along the timeline */
8444 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008445 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008446 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008447 break;
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 default:
8450 clearopbeep(oap);
8451 break;
8452 }
8453}
8454
8455/*
8456 * Handle "o" and "O" commands.
8457 */
8458 static void
8459n_opencmd(cap)
8460 cmdarg_T *cap;
8461{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008462#ifdef FEAT_CONCEAL
8463 linenr_T oldline = curwin->w_cursor.lnum;
8464#endif
8465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 if (!checkclearopq(cap->oap))
8467 {
8468#ifdef FEAT_FOLDING
8469 if (cap->cmdchar == 'O')
8470 /* Open above the first line of a folded sequence of lines */
8471 (void)hasFolding(curwin->w_cursor.lnum,
8472 &curwin->w_cursor.lnum, NULL);
8473 else
8474 /* Open below the last line of a folded sequence of lines */
8475 (void)hasFolding(curwin->w_cursor.lnum,
8476 NULL, &curwin->w_cursor.lnum);
8477#endif
8478 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8479 (cap->cmdchar == 'O' ? 1 : 0)),
8480 (linenr_T)(curwin->w_cursor.lnum +
8481 (cap->cmdchar == 'o' ? 1 : 0))
8482 ) == OK
8483 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8484#ifdef FEAT_COMMENTS
8485 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8486#endif
8487 0, 0))
8488 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008489#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008490 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008491 update_single_line(curwin, oldline);
8492#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008493 /* When '#' is in 'cpoptions' ignore the count. */
8494 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8495 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8497 }
8498 }
8499}
8500
8501/*
8502 * "." command: redo last change.
8503 */
8504 static void
8505nv_dot(cap)
8506 cmdarg_T *cap;
8507{
8508 if (!checkclearopq(cap->oap))
8509 {
8510 /*
8511 * If "restart_edit" is TRUE, the last but one command is repeated
8512 * instead of the last command (inserting text). This is used for
8513 * CTRL-O <.> in insert mode.
8514 */
8515 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8516 clearopbeep(cap->oap);
8517 }
8518}
8519
8520/*
8521 * CTRL-R: undo undo
8522 */
8523 static void
8524nv_redo(cap)
8525 cmdarg_T *cap;
8526{
8527 if (!checkclearopq(cap->oap))
8528 {
8529 u_redo((int)cap->count1);
8530 curwin->w_set_curswant = TRUE;
8531 }
8532}
8533
8534/*
8535 * Handle "U" command.
8536 */
8537 static void
8538nv_Undo(cap)
8539 cmdarg_T *cap;
8540{
8541 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008542 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {
8544 /* translate "gUU" to "gUgU" */
8545 cap->cmdchar = 'g';
8546 cap->nchar = 'U';
8547 nv_operator(cap);
8548 }
8549 else if (!checkclearopq(cap->oap))
8550 {
8551 u_undoline();
8552 curwin->w_set_curswant = TRUE;
8553 }
8554}
8555
8556/*
8557 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8558 * single character.
8559 */
8560 static void
8561nv_tilde(cap)
8562 cmdarg_T *cap;
8563{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008564 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 n_swapchar(cap);
8566 else
8567 nv_operator(cap);
8568}
8569
8570/*
8571 * Handle an operator command.
8572 * The actual work is done by do_pending_operator().
8573 */
8574 static void
8575nv_operator(cap)
8576 cmdarg_T *cap;
8577{
8578 int op_type;
8579
8580 op_type = get_op_type(cap->cmdchar, cap->nchar);
8581
8582 if (op_type == cap->oap->op_type) /* double operator works on lines */
8583 nv_lineop(cap);
8584 else if (!checkclearop(cap->oap))
8585 {
8586 cap->oap->start = curwin->w_cursor;
8587 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008588#ifdef FEAT_EVAL
8589 set_op_var(op_type);
8590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592}
8593
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008594#ifdef FEAT_EVAL
8595/*
8596 * Set v:operator to the characters for "optype".
8597 */
8598 static void
8599set_op_var(optype)
8600 int optype;
8601{
8602 char_u opchars[3];
8603
8604 if (optype == OP_NOP)
8605 set_vim_var_string(VV_OP, NULL, 0);
8606 else
8607 {
8608 opchars[0] = get_op_char(optype);
8609 opchars[1] = get_extra_op_char(optype);
8610 opchars[2] = NUL;
8611 set_vim_var_string(VV_OP, opchars, -1);
8612 }
8613}
8614#endif
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616/*
8617 * Handle linewise operator "dd", "yy", etc.
8618 *
8619 * "_" is is a strange motion command that helps make operators more logical.
8620 * It is actually implemented, but not documented in the real Vi. This motion
8621 * command actually refers to "the current line". Commands like "dd" and "yy"
8622 * are really an alternate form of "d_" and "y_". It does accept a count, so
8623 * "d3_" works to delete 3 lines.
8624 */
8625 static void
8626nv_lineop(cap)
8627 cmdarg_T *cap;
8628{
8629 cap->oap->motion_type = MLINE;
8630 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8631 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008632 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8633 && cap->oap->motion_force != 'v'
8634 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 || cap->oap->op_type == OP_LSHIFT
8636 || cap->oap->op_type == OP_RSHIFT)
8637 beginline(BL_SOL | BL_FIX);
8638 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8639 beginline(BL_WHITE | BL_FIX);
8640}
8641
8642/*
8643 * <Home> command.
8644 */
8645 static void
8646nv_home(cap)
8647 cmdarg_T *cap;
8648{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008649 /* CTRL-HOME is like "gg" */
8650 if (mod_mask & MOD_MASK_CTRL)
8651 nv_goto(cap);
8652 else
8653 {
8654 cap->count0 = 1;
8655 nv_pipe(cap);
8656 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008657 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8658 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659}
8660
8661/*
8662 * "|" command.
8663 */
8664 static void
8665nv_pipe(cap)
8666 cmdarg_T *cap;
8667{
8668 cap->oap->motion_type = MCHAR;
8669 cap->oap->inclusive = FALSE;
8670 beginline(0);
8671 if (cap->count0 > 0)
8672 {
8673 coladvance((colnr_T)(cap->count0 - 1));
8674 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8675 }
8676 else
8677 curwin->w_curswant = 0;
8678 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008679 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 curwin->w_set_curswant = FALSE;
8681}
8682
8683/*
8684 * Handle back-word command "b" and "B".
8685 * cap->arg is 1 for "B"
8686 */
8687 static void
8688nv_bck_word(cap)
8689 cmdarg_T *cap;
8690{
8691 cap->oap->motion_type = MCHAR;
8692 cap->oap->inclusive = FALSE;
8693 curwin->w_set_curswant = TRUE;
8694 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8695 clearopbeep(cap->oap);
8696#ifdef FEAT_FOLDING
8697 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8698 foldOpenCursor();
8699#endif
8700}
8701
8702/*
8703 * Handle word motion commands "e", "E", "w" and "W".
8704 * cap->arg is TRUE for "E" and "W".
8705 */
8706 static void
8707nv_wordcmd(cap)
8708 cmdarg_T *cap;
8709{
8710 int n;
8711 int word_end;
8712 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008713 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714
8715 /*
8716 * Set inclusive for the "E" and "e" command.
8717 */
8718 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8719 word_end = TRUE;
8720 else
8721 word_end = FALSE;
8722 cap->oap->inclusive = word_end;
8723
8724 /*
8725 * "cw" and "cW" are a special case.
8726 */
8727 if (!word_end && cap->oap->op_type == OP_CHANGE)
8728 {
8729 n = gchar_cursor();
8730 if (n != NUL) /* not an empty line */
8731 {
8732 if (vim_iswhite(n))
8733 {
8734 /*
8735 * Reproduce a funny Vi behaviour: "cw" on a blank only
8736 * changes one character, not all blanks until the start of
8737 * the next word. Only do this when the 'w' flag is included
8738 * in 'cpoptions'.
8739 */
8740 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8741 {
8742 cap->oap->inclusive = TRUE;
8743 cap->oap->motion_type = MCHAR;
8744 return;
8745 }
8746 }
8747 else
8748 {
8749 /*
8750 * This is a little strange. To match what the real Vi does,
8751 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8752 * that we are not on a space or a TAB. This seems impolite
8753 * at first, but it's really more what we mean when we say
8754 * 'cw'.
8755 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008756 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * will change only one character! This is done by setting
8758 * flag.
8759 */
8760 cap->oap->inclusive = TRUE;
8761 word_end = TRUE;
8762 flag = TRUE;
8763 }
8764 }
8765 }
8766
8767 cap->oap->motion_type = MCHAR;
8768 curwin->w_set_curswant = TRUE;
8769 if (word_end)
8770 n = end_word(cap->count1, cap->arg, flag, FALSE);
8771 else
8772 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8773
Bram Moolenaardfefb982008-04-01 10:06:39 +00008774 /* Don't leave the cursor on the NUL past the end of line. Unless we
8775 * didn't move it forward. */
8776 if (lt(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008777 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
8779 if (n == FAIL && cap->oap->op_type == OP_NOP)
8780 clearopbeep(cap->oap);
8781 else
8782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784#ifdef FEAT_FOLDING
8785 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8786 foldOpenCursor();
8787#endif
8788 }
8789}
8790
8791/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008792 * Used after a movement command: If the cursor ends up on the NUL after the
8793 * end of the line, may move it back to the last character and make the motion
8794 * inclusive.
8795 */
8796 static void
8797adjust_cursor(oap)
8798 oparg_T *oap;
8799{
8800 /* The cursor cannot remain on the NUL when:
8801 * - the column is > 0
8802 * - not in Visual mode or 'selection' is "o"
8803 * - 'virtualedit' is not "all" and not "onemore".
8804 */
8805 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008806 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008807#ifdef FEAT_VIRTUALEDIT
8808 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8809#endif
8810 )
8811 {
8812 --curwin->w_cursor.col;
8813#ifdef FEAT_MBYTE
8814 /* prevent cursor from moving on the trail byte */
8815 if (has_mbyte)
8816 mb_adjust_cursor();
8817#endif
8818 oap->inclusive = TRUE;
8819 }
8820}
8821
8822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 * "0" and "^" commands.
8824 * cap->arg is the argument for beginline().
8825 */
8826 static void
8827nv_beginline(cap)
8828 cmdarg_T *cap;
8829{
8830 cap->oap->motion_type = MCHAR;
8831 cap->oap->inclusive = FALSE;
8832 beginline(cap->arg);
8833#ifdef FEAT_FOLDING
8834 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8835 foldOpenCursor();
8836#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008837 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8838 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841/*
8842 * In exclusive Visual mode, may include the last character.
8843 */
8844 static void
8845adjust_for_sel(cap)
8846 cmdarg_T *cap;
8847{
8848 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
8849 && gchar_cursor() != NUL && lt(VIsual, curwin->w_cursor))
8850 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008851#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (has_mbyte)
8853 inc_cursor();
8854 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 ++curwin->w_cursor.col;
8857 cap->oap->inclusive = FALSE;
8858 }
8859}
8860
8861/*
8862 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8863 * Should check VIsual_mode before calling this.
8864 * Returns TRUE when backed up to the previous line.
8865 */
8866 static int
8867unadjust_for_sel()
8868{
8869 pos_T *pp;
8870
8871 if (*p_sel == 'e' && !equalpos(VIsual, curwin->w_cursor))
8872 {
8873 if (lt(VIsual, curwin->w_cursor))
8874 pp = &curwin->w_cursor;
8875 else
8876 pp = &VIsual;
8877#ifdef FEAT_VIRTUALEDIT
8878 if (pp->coladd > 0)
8879 --pp->coladd;
8880 else
8881#endif
8882 if (pp->col > 0)
8883 {
8884 --pp->col;
8885#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008886 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887#endif
8888 }
8889 else if (pp->lnum > 1)
8890 {
8891 --pp->lnum;
8892 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8893 return TRUE;
8894 }
8895 }
8896 return FALSE;
8897}
8898
8899/*
8900 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8901 */
8902 static void
8903nv_select(cap)
8904 cmdarg_T *cap;
8905{
8906 if (VIsual_active)
8907 VIsual_select = TRUE;
8908 else if (VIsual_reselect)
8909 {
8910 cap->nchar = 'v'; /* fake "gv" command */
8911 cap->arg = TRUE;
8912 nv_g_cmd(cap);
8913 }
8914}
8915
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917/*
8918 * "G", "gg", CTRL-END, CTRL-HOME.
8919 * cap->arg is TRUE for "G".
8920 */
8921 static void
8922nv_goto(cap)
8923 cmdarg_T *cap;
8924{
8925 linenr_T lnum;
8926
8927 if (cap->arg)
8928 lnum = curbuf->b_ml.ml_line_count;
8929 else
8930 lnum = 1L;
8931 cap->oap->motion_type = MLINE;
8932 setpcmark();
8933
8934 /* When a count is given, use it instead of the default lnum */
8935 if (cap->count0 != 0)
8936 lnum = cap->count0;
8937 if (lnum < 1L)
8938 lnum = 1L;
8939 else if (lnum > curbuf->b_ml.ml_line_count)
8940 lnum = curbuf->b_ml.ml_line_count;
8941 curwin->w_cursor.lnum = lnum;
8942 beginline(BL_SOL | BL_FIX);
8943#ifdef FEAT_FOLDING
8944 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8945 foldOpenCursor();
8946#endif
8947}
8948
8949/*
8950 * CTRL-\ in Normal mode.
8951 */
8952 static void
8953nv_normal(cap)
8954 cmdarg_T *cap;
8955{
8956 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8957 {
8958 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008959 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 clear_cmdline = TRUE; /* unshow mode later */
8961 restart_edit = 0;
8962#ifdef FEAT_CMDWIN
8963 if (cmdwin_type != 0)
8964 cmdwin_result = Ctrl_C;
8965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 if (VIsual_active)
8967 {
8968 end_visual_mode(); /* stop Visual */
8969 redraw_curbuf_later(INVERTED);
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8972 if (cap->nchar == Ctrl_G && p_im)
8973 restart_edit = 'a';
8974 }
8975 else
8976 clearopbeep(cap->oap);
8977}
8978
8979/*
8980 * ESC in Normal mode: beep, but don't flush buffers.
8981 * Don't even beep if we are canceling a command.
8982 */
8983 static void
8984nv_esc(cap)
8985 cmdarg_T *cap;
8986{
8987 int no_reason;
8988
8989 no_reason = (cap->oap->op_type == OP_NOP
8990 && cap->opcount == 0
8991 && cap->count0 == 0
8992 && cap->oap->regname == 0
8993 && !p_im);
8994
8995 if (cap->arg) /* TRUE for CTRL-C */
8996 {
8997 if (restart_edit == 0
8998#ifdef FEAT_CMDWIN
8999 && cmdwin_type == 0
9000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 && no_reason)
9003 MSG(_("Type :quit<Enter> to exit Vim"));
9004
9005 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9006 * set again below when halfway a mapping. */
9007 if (!p_im)
9008 restart_edit = 0;
9009#ifdef FEAT_CMDWIN
9010 if (cmdwin_type != 0)
9011 {
9012 cmdwin_result = K_IGNORE;
9013 got_int = FALSE; /* don't stop executing autocommands et al. */
9014 return;
9015 }
9016#endif
9017 }
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (VIsual_active)
9020 {
9021 end_visual_mode(); /* stop Visual */
9022 check_cursor_col(); /* make sure cursor is not beyond EOL */
9023 curwin->w_set_curswant = TRUE;
9024 redraw_curbuf_later(INVERTED);
9025 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009026 else if (no_reason)
9027 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 clearop(cap->oap);
9029
9030 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9031 * set return to Insert mode afterwards. */
9032 if (restart_edit == 0 && goto_im()
9033#ifdef FEAT_EX_EXTRA
9034 && ex_normal_busy == 0
9035#endif
9036 )
9037 restart_edit = 'a';
9038}
9039
9040/*
9041 * Handle "A", "a", "I", "i" and <Insert> commands.
9042 */
9043 static void
9044nv_edit(cap)
9045 cmdarg_T *cap;
9046{
9047 /* <Insert> is equal to "i" */
9048 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9049 cap->cmdchar = 'i';
9050
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 /* in Visual mode "A" and "I" are an operator */
9052 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9053 v_visop(cap);
9054
9055 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009056 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9057 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 {
9059#ifdef FEAT_TEXTOBJ
9060 nv_object(cap);
9061#else
9062 clearopbeep(cap->oap);
9063#endif
9064 }
9065 else if (!curbuf->b_p_ma && !p_im)
9066 {
9067 /* Only give this error when 'insertmode' is off. */
9068 EMSG(_(e_modifiable));
9069 clearop(cap->oap);
9070 }
9071 else if (!checkclearopq(cap->oap))
9072 {
9073 switch (cap->cmdchar)
9074 {
9075 case 'A': /* "A"ppend after the line */
9076 curwin->w_set_curswant = TRUE;
9077#ifdef FEAT_VIRTUALEDIT
9078 if (ve_flags == VE_ALL)
9079 {
9080 int save_State = State;
9081
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009082 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 * character past the end of the line */
9084 State = INSERT;
9085 coladvance((colnr_T)MAXCOL);
9086 State = save_State;
9087 }
9088 else
9089#endif
9090 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9091 break;
9092
9093 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009094 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9095 beginline(BL_WHITE);
9096 else
9097 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 break;
9099
9100 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9101#ifdef FEAT_VIRTUALEDIT
9102 /* increment coladd when in virtual space, increment the
9103 * column otherwise, also to append after an unprintable char */
9104 if (virtual_active()
9105 && (curwin->w_cursor.coladd > 0
9106 || *ml_get_cursor() == NUL
9107 || *ml_get_cursor() == TAB))
9108 curwin->w_cursor.coladd++;
9109 else
9110#endif
9111 if (*ml_get_cursor() != NUL)
9112 inc_cursor();
9113 break;
9114 }
9115
9116#ifdef FEAT_VIRTUALEDIT
9117 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9118 {
9119 int save_State = State;
9120
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009121 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 * character past the end of the line */
9123 State = INSERT;
9124 coladvance(getviscol());
9125 State = save_State;
9126 }
9127#endif
9128
9129 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9130 }
9131}
9132
9133/*
9134 * Invoke edit() and take care of "restart_edit" and the return value.
9135 */
9136 static void
9137invoke_edit(cap, repl, cmd, startln)
9138 cmdarg_T *cap;
9139 int repl; /* "r" or "gr" command */
9140 int cmd;
9141 int startln;
9142{
9143 int restart_edit_save = 0;
9144
9145 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9146 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9147 * it. */
9148 if (repl || !stuff_empty())
9149 restart_edit_save = restart_edit;
9150 else
9151 restart_edit_save = 0;
9152
9153 /* Always reset "restart_edit", this is not a restarted edit. */
9154 restart_edit = 0;
9155
9156 if (edit(cmd, startln, cap->count1))
9157 cap->retval |= CA_COMMAND_BUSY;
9158
9159 if (restart_edit == 0)
9160 restart_edit = restart_edit_save;
9161}
9162
9163#ifdef FEAT_TEXTOBJ
9164/*
9165 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9166 */
9167 static void
9168nv_object(cap)
9169 cmdarg_T *cap;
9170{
9171 int flag;
9172 int include;
9173 char_u *mps_save;
9174
9175 if (cap->cmdchar == 'i')
9176 include = FALSE; /* "ix" = inner object: exclude white space */
9177 else
9178 include = TRUE; /* "ax" = an object: include white space */
9179
9180 /* Make sure (), [], {} and <> are in 'matchpairs' */
9181 mps_save = curbuf->b_p_mps;
9182 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9183
9184 switch (cap->nchar)
9185 {
9186 case 'w': /* "aw" = a word */
9187 flag = current_word(cap->oap, cap->count1, include, FALSE);
9188 break;
9189 case 'W': /* "aW" = a WORD */
9190 flag = current_word(cap->oap, cap->count1, include, TRUE);
9191 break;
9192 case 'b': /* "ab" = a braces block */
9193 case '(':
9194 case ')':
9195 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9196 break;
9197 case 'B': /* "aB" = a Brackets block */
9198 case '{':
9199 case '}':
9200 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9201 break;
9202 case '[': /* "a[" = a [] block */
9203 case ']':
9204 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9205 break;
9206 case '<': /* "a<" = a <> block */
9207 case '>':
9208 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9209 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009210 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009211 /* Do not adjust oap->end in do_pending_operator()
9212 * otherwise there are different results for 'dit'
9213 * (note leading whitespace in last line):
9214 * 1) <b> 2) <b>
9215 * foobar foobar
9216 * </b> </b>
9217 */
9218 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009219 flag = current_tagblock(cap->oap, cap->count1, include);
9220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 case 'p': /* "ap" = a paragraph */
9222 flag = current_par(cap->oap, cap->count1, include, 'p');
9223 break;
9224 case 's': /* "as" = a sentence */
9225 flag = current_sent(cap->oap, cap->count1, include);
9226 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009227 case '"': /* "a"" = a double quoted string */
9228 case '\'': /* "a'" = a single quoted string */
9229 case '`': /* "a`" = a backtick quoted string */
9230 flag = current_quote(cap->oap, cap->count1, include,
9231 cap->nchar);
9232 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233#if 0 /* TODO */
9234 case 'S': /* "aS" = a section */
9235 case 'f': /* "af" = a filename */
9236 case 'u': /* "au" = a URL */
9237#endif
9238 default:
9239 flag = FAIL;
9240 break;
9241 }
9242
9243 curbuf->b_p_mps = mps_save;
9244 if (flag == FAIL)
9245 clearopbeep(cap->oap);
9246 adjust_cursor_col();
9247 curwin->w_set_curswant = TRUE;
9248}
9249#endif
9250
9251/*
9252 * "q" command: Start/stop recording.
9253 * "q:", "q/", "q?": edit command-line in command-line window.
9254 */
9255 static void
9256nv_record(cap)
9257 cmdarg_T *cap;
9258{
9259 if (cap->oap->op_type == OP_FORMAT)
9260 {
9261 /* "gqq" is the same as "gqgq": format line */
9262 cap->cmdchar = 'g';
9263 cap->nchar = 'q';
9264 nv_operator(cap);
9265 }
9266 else if (!checkclearop(cap->oap))
9267 {
9268#ifdef FEAT_CMDWIN
9269 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9270 {
9271 stuffcharReadbuff(cap->nchar);
9272 stuffcharReadbuff(K_CMDWIN);
9273 }
9274 else
9275#endif
9276 /* (stop) recording into a named register, unless executing a
9277 * register */
9278 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9279 clearopbeep(cap->oap);
9280 }
9281}
9282
9283/*
9284 * Handle the "@r" command.
9285 */
9286 static void
9287nv_at(cap)
9288 cmdarg_T *cap;
9289{
9290 if (checkclearop(cap->oap))
9291 return;
9292#ifdef FEAT_EVAL
9293 if (cap->nchar == '=')
9294 {
9295 if (get_expr_register() == NUL)
9296 return;
9297 }
9298#endif
9299 while (cap->count1-- && !got_int)
9300 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009301 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 {
9303 clearopbeep(cap->oap);
9304 break;
9305 }
9306 line_breakcheck();
9307 }
9308}
9309
9310/*
9311 * Handle the CTRL-U and CTRL-D commands.
9312 */
9313 static void
9314nv_halfpage(cap)
9315 cmdarg_T *cap;
9316{
9317 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9318 || (cap->cmdchar == Ctrl_D
9319 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9320 clearopbeep(cap->oap);
9321 else if (!checkclearop(cap->oap))
9322 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9323}
9324
9325/*
9326 * Handle "J" or "gJ" command.
9327 */
9328 static void
9329nv_join(cap)
9330 cmdarg_T *cap;
9331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (VIsual_active) /* join the visual lines */
9333 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009334 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 {
9336 if (cap->count0 <= 1)
9337 cap->count0 = 2; /* default for join is two lines! */
9338 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9339 curbuf->b_ml.ml_line_count)
9340 clearopbeep(cap->oap); /* beyond last line */
9341 else
9342 {
9343 prep_redo(cap->oap->regname, cap->count0,
9344 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009345 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 }
9347 }
9348}
9349
9350/*
9351 * "P", "gP", "p" and "gp" commands.
9352 */
9353 static void
9354nv_put(cap)
9355 cmdarg_T *cap;
9356{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 int regname = 0;
9358 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009359 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009360 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 int dir;
9362 int flags = 0;
9363
9364 if (cap->oap->op_type != OP_NOP)
9365 {
9366#ifdef FEAT_DIFF
9367 /* "dp" is ":diffput" */
9368 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9369 {
9370 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009371 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 }
9373 else
9374#endif
9375 clearopbeep(cap->oap);
9376 }
9377 else
9378 {
9379 dir = (cap->cmdchar == 'P'
9380 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9381 ? BACKWARD : FORWARD;
9382 prep_redo_cmd(cap);
9383 if (cap->cmdchar == 'g')
9384 flags |= PUT_CURSEND;
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (VIsual_active)
9387 {
9388 /* Putting in Visual mode: The put text replaces the selected
9389 * text. First delete the selected text, then put the new text.
9390 * Need to save and restore the registers that the delete
9391 * overwrites if the old contents is being put.
9392 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009393 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009395#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009397#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009398 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009399 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009400#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
9404 )
9405 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009406 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 * put, save it first. */
9408 reg1 = get_register(regname, TRUE);
9409 }
9410
9411 /* Now delete the selected text. */
9412 cap->cmdchar = 'd';
9413 cap->nchar = NUL;
9414 cap->oap->regname = NUL;
9415 nv_operator(cap);
9416 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009417 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418
9419 /* delete PUT_LINE_BACKWARD; */
9420 cap->oap->regname = regname;
9421
9422 if (reg1 != NULL)
9423 {
9424 /* Delete probably changed the register we want to put, save
9425 * it first. Then put back what was there before the delete. */
9426 reg2 = get_register(regname, FALSE);
9427 put_register(regname, reg1);
9428 }
9429
9430 /* When deleted a linewise Visual area, put the register as
9431 * lines to avoid it joined with the next line. When deletion was
9432 * characterwise, split a line when putting lines. */
9433 if (VIsual_mode == 'V')
9434 flags |= PUT_LINE;
9435 else if (VIsual_mode == 'v')
9436 flags |= PUT_LINE_SPLIT;
9437 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9438 flags |= PUT_LINE_FORWARD;
9439 dir = BACKWARD;
9440 if ((VIsual_mode != 'V'
9441 && curwin->w_cursor.col < curbuf->b_op_start.col)
9442 || (VIsual_mode == 'V'
9443 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9444 /* cursor is at the end of the line or end of file, put
9445 * forward. */
9446 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009447 /* May have been reset in do_put(). */
9448 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 do_put(cap->oap->regname, dir, cap->count1, flags);
9451
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 /* If a register was saved, put it back now. */
9453 if (reg2 != NULL)
9454 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009455
9456 /* What to reselect with "gv"? Selecting the just put text seems to
9457 * be the most useful, since the original text was removed. */
9458 if (was_visual)
9459 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009460 curbuf->b_visual.vi_start = curbuf->b_op_start;
9461 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009462 }
9463
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009464 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009465 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009466 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009467 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009468 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009469
9470 /* If the cursor was in that line, move it to the end of the last
9471 * line. */
9472 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9473 {
9474 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9475 coladvance((colnr_T)MAXCOL);
9476 }
9477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 auto_format(FALSE, TRUE);
9479 }
9480}
9481
9482/*
9483 * "o" and "O" commands.
9484 */
9485 static void
9486nv_open(cap)
9487 cmdarg_T *cap;
9488{
9489#ifdef FEAT_DIFF
9490 /* "do" is ":diffget" */
9491 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9492 {
9493 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009494 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 }
9496 else
9497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 if (VIsual_active) /* switch start and end of visual */
9499 v_swap_corners(cap->cmdchar);
9500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 n_opencmd(cap);
9502}
9503
9504#ifdef FEAT_SNIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 static void
9506nv_sniff(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009507 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
9509 ProcessSniffRequests();
9510}
9511#endif
9512
9513#ifdef FEAT_NETBEANS_INTG
9514 static void
9515nv_nbcmd(cap)
9516 cmdarg_T *cap;
9517{
9518 netbeans_keycommand(cap->nchar);
9519}
9520#endif
9521
9522#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 static void
9524nv_drop(cap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00009525 cmdarg_T *cap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9528}
9529#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009530
9531#ifdef FEAT_AUTOCMD
9532/*
9533 * Trigger CursorHold event.
9534 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9535 * input buffer. "did_cursorhold" is set to avoid retriggering.
9536 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009537 static void
9538nv_cursorhold(cap)
9539 cmdarg_T *cap;
9540{
9541 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9542 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009543 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009544}
9545#endif