comparison src/normal.c @ 0:76efa0be13f1

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