1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
19 * The Visual area is remembered for reselection.
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 */
25 static int restart_VIsual_select
= 0;
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
));
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
));
48 static void unshift_special
__ARGS((cmdarg_T
*cap
));
51 static void del_from_showcmd
__ARGS((int));
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.
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
));
68 static void nv_mousescroll
__ARGS((cmdarg_T
*cap
));
69 static void nv_mouse
__ARGS((cmdarg_T
*cap
));
71 static void nv_scroll_line
__ARGS((cmdarg_T
*cap
));
72 static void nv_zet
__ARGS((cmdarg_T
*cap
));
74 static void nv_ver_scrollbar
__ARGS((cmdarg_T
*cap
));
75 static void nv_hor_scrollbar
__ARGS((cmdarg_T
*cap
));
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline
__ARGS((cmdarg_T
*cap
));
79 static void nv_tabmenu
__ARGS((cmdarg_T
*cap
));
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
));
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
));
114 static void nv_vreplace
__ARGS((cmdarg_T
*cap
));
117 static void v_swap_corners
__ARGS((int cmdchar
));
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
));
123 static void v_visop
__ARGS((cmdarg_T
*cap
));
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
));
132 static void nv_visual
__ARGS((cmdarg_T
*cap
));
133 static void n_start_visual_mode
__ARGS((int c
));
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
));
145 static void set_op_var
__ARGS((int optype
));
147 static void nv_lineop
__ARGS((cmdarg_T
*cap
));
148 static void nv_home
__ARGS((cmdarg_T
*cap
));
149 static void nv_pipe
__ARGS((cmdarg_T
*cap
));
150 static void nv_bck_word
__ARGS((cmdarg_T
*cap
));
151 static void nv_wordcmd
__ARGS((cmdarg_T
*cap
));
152 static void nv_beginline
__ARGS((cmdarg_T
*cap
));
153 static void adjust_cursor
__ARGS((oparg_T
*oap
));
155 static void adjust_for_sel
__ARGS((cmdarg_T
*cap
));
156 static int unadjust_for_sel
__ARGS((void));
157 static void nv_select
__ARGS((cmdarg_T
*cap
));
159 static void nv_goto
__ARGS((cmdarg_T
*cap
));
160 static void nv_normal
__ARGS((cmdarg_T
*cap
));
161 static void nv_esc
__ARGS((cmdarg_T
*oap
));
162 static void nv_edit
__ARGS((cmdarg_T
*cap
));
163 static void invoke_edit
__ARGS((cmdarg_T
*cap
, int repl
, int cmd
, int startln
));
165 static void nv_object
__ARGS((cmdarg_T
*cap
));
167 static void nv_record
__ARGS((cmdarg_T
*cap
));
168 static void nv_at
__ARGS((cmdarg_T
*cap
));
169 static void nv_halfpage
__ARGS((cmdarg_T
*cap
));
170 static void nv_join
__ARGS((cmdarg_T
*cap
));
171 static void nv_put
__ARGS((cmdarg_T
*cap
));
172 static void nv_open
__ARGS((cmdarg_T
*cap
));
174 static void nv_sniff
__ARGS((cmdarg_T
*cap
));
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd
__ARGS((cmdarg_T
*cap
));
180 static void nv_drop
__ARGS((cmdarg_T
*cap
));
183 static void nv_cursorhold
__ARGS((cmdarg_T
*cap
));
186 static char *e_noident
= N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T
) __ARGS((cmdarg_T
*cap
));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char
; /* (first) command character */
225 nv_func_T cmd_func
; /* function for this command */
226 short_u cmd_flags
; /* NV_ flags */
227 short cmd_arg
; /* value for ca.arg */
230 {NUL
, nv_error
, 0, 0},
231 {Ctrl_A
, nv_addsub
, 0, 0},
232 {Ctrl_B
, nv_page
, NV_STS
, BACKWARD
},
233 {Ctrl_C
, nv_esc
, 0, TRUE
},
234 {Ctrl_D
, nv_halfpage
, 0, 0},
235 {Ctrl_E
, nv_scroll_line
, 0, TRUE
},
236 {Ctrl_F
, nv_page
, NV_STS
, FORWARD
},
237 {Ctrl_G
, nv_ctrlg
, 0, 0},
238 {Ctrl_H
, nv_ctrlh
, 0, 0},
239 {Ctrl_I
, nv_pcmark
, 0, 0},
240 {NL
, nv_down
, 0, FALSE
},
241 {Ctrl_K
, nv_error
, 0, 0},
242 {Ctrl_L
, nv_clear
, 0, 0},
243 {Ctrl_M
, nv_down
, 0, TRUE
},
244 {Ctrl_N
, nv_down
, NV_STS
, FALSE
},
245 {Ctrl_O
, nv_ctrlo
, 0, 0},
246 {Ctrl_P
, nv_up
, NV_STS
, FALSE
},
248 {Ctrl_Q
, nv_visual
, 0, FALSE
},
250 {Ctrl_Q
, nv_ignore
, 0, 0},
252 {Ctrl_R
, nv_redo
, 0, 0},
253 {Ctrl_S
, nv_ignore
, 0, 0},
254 {Ctrl_T
, nv_tagpop
, NV_NCW
, 0},
255 {Ctrl_U
, nv_halfpage
, 0, 0},
257 {Ctrl_V
, nv_visual
, 0, FALSE
},
258 {'V', nv_visual
, 0, FALSE
},
259 {'v', nv_visual
, 0, FALSE
},
261 {Ctrl_V
, nv_error
, 0, 0},
262 {'V', nv_error
, 0, 0},
263 {'v', nv_error
, 0, 0},
265 {Ctrl_W
, nv_window
, 0, 0},
266 {Ctrl_X
, nv_addsub
, 0, 0},
267 {Ctrl_Y
, nv_scroll_line
, 0, FALSE
},
268 {Ctrl_Z
, nv_suspend
, 0, 0},
269 {ESC
, nv_esc
, 0, FALSE
},
270 {Ctrl_BSL
, nv_normal
, NV_NCH_ALW
, 0},
271 {Ctrl_RSB
, nv_ident
, NV_NCW
, 0},
272 {Ctrl_HAT
, nv_hat
, NV_NCW
, 0},
273 {Ctrl__
, nv_error
, 0, 0},
274 {' ', nv_right
, 0, 0},
275 {'!', nv_operator
, 0, 0},
276 {'"', nv_regname
, NV_NCH_NOP
|NV_KEEPREG
, 0},
277 {'#', nv_ident
, 0, 0},
278 {'$', nv_dollar
, 0, 0},
279 {'%', nv_percent
, 0, 0},
280 {'&', nv_optrans
, 0, 0},
281 {'\'', nv_gomark
, NV_NCH_ALW
, TRUE
},
282 {'(', nv_brace
, 0, BACKWARD
},
283 {')', nv_brace
, 0, FORWARD
},
284 {'*', nv_ident
, 0, 0},
285 {'+', nv_down
, 0, TRUE
},
286 {',', nv_csearch
, 0, TRUE
},
287 {'-', nv_up
, 0, TRUE
},
288 {'.', nv_dot
, NV_KEEPREG
, 0},
289 {'/', nv_search
, 0, FALSE
},
290 {'0', nv_beginline
, 0, 0},
291 {'1', nv_ignore
, 0, 0},
292 {'2', nv_ignore
, 0, 0},
293 {'3', nv_ignore
, 0, 0},
294 {'4', nv_ignore
, 0, 0},
295 {'5', nv_ignore
, 0, 0},
296 {'6', nv_ignore
, 0, 0},
297 {'7', nv_ignore
, 0, 0},
298 {'8', nv_ignore
, 0, 0},
299 {'9', nv_ignore
, 0, 0},
300 {':', nv_colon
, 0, 0},
301 {';', nv_csearch
, 0, FALSE
},
302 {'<', nv_operator
, NV_RL
, 0},
303 {'=', nv_operator
, 0, 0},
304 {'>', nv_operator
, NV_RL
, 0},
305 {'?', nv_search
, 0, FALSE
},
306 {'@', nv_at
, NV_NCH_NOP
, FALSE
},
307 {'A', nv_edit
, 0, 0},
308 {'B', nv_bck_word
, 0, 1},
309 {'C', nv_abbrev
, NV_KEEPREG
, 0},
310 {'D', nv_abbrev
, NV_KEEPREG
, 0},
311 {'E', nv_wordcmd
, 0, TRUE
},
312 {'F', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
313 {'G', nv_goto
, 0, TRUE
},
314 {'H', nv_scroll
, 0, 0},
315 {'I', nv_edit
, 0, 0},
316 {'J', nv_join
, 0, 0},
317 {'K', nv_ident
, 0, 0},
318 {'L', nv_scroll
, 0, 0},
319 {'M', nv_scroll
, 0, 0},
320 {'N', nv_next
, 0, SEARCH_REV
},
321 {'O', nv_open
, 0, 0},
323 {'Q', nv_exmode
, NV_NCW
, 0},
324 {'R', nv_Replace
, 0, FALSE
},
325 {'S', nv_subst
, NV_KEEPREG
, 0},
326 {'T', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
327 {'U', nv_Undo
, 0, 0},
328 {'W', nv_wordcmd
, 0, TRUE
},
329 {'X', nv_abbrev
, NV_KEEPREG
, 0},
330 {'Y', nv_abbrev
, NV_KEEPREG
, 0},
331 {'Z', nv_Zet
, NV_NCH_NOP
|NV_NCW
, 0},
332 {'[', nv_brackets
, NV_NCH_ALW
, BACKWARD
},
333 {'\\', nv_error
, 0, 0},
334 {']', nv_brackets
, NV_NCH_ALW
, FORWARD
},
335 {'^', nv_beginline
, 0, BL_WHITE
| BL_FIX
},
336 {'_', nv_lineop
, 0, 0},
337 {'`', nv_gomark
, NV_NCH_ALW
, FALSE
},
338 {'a', nv_edit
, NV_NCH
, 0},
339 {'b', nv_bck_word
, 0, 0},
340 {'c', nv_operator
, 0, 0},
341 {'d', nv_operator
, 0, 0},
342 {'e', nv_wordcmd
, 0, FALSE
},
343 {'f', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
344 {'g', nv_g_cmd
, NV_NCH_ALW
, FALSE
},
345 {'h', nv_left
, NV_RL
, 0},
346 {'i', nv_edit
, NV_NCH
, 0},
347 {'j', nv_down
, 0, FALSE
},
348 {'k', nv_up
, 0, FALSE
},
349 {'l', nv_right
, NV_RL
, 0},
350 {'m', nv_mark
, NV_NCH_NOP
, 0},
351 {'n', nv_next
, 0, 0},
352 {'o', nv_open
, 0, 0},
354 {'q', nv_record
, NV_NCH
, 0},
355 {'r', nv_replace
, NV_NCH_NOP
|NV_LANG
, 0},
356 {'s', nv_subst
, NV_KEEPREG
, 0},
357 {'t', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
358 {'u', nv_undo
, 0, 0},
359 {'w', nv_wordcmd
, 0, FALSE
},
360 {'x', nv_abbrev
, NV_KEEPREG
, 0},
361 {'y', nv_operator
, 0, 0},
362 {'z', nv_zet
, NV_NCH_ALW
, 0},
363 {'{', nv_findpar
, 0, BACKWARD
},
364 {'|', nv_pipe
, 0, 0},
365 {'}', nv_findpar
, 0, FORWARD
},
366 {'~', nv_tilde
, 0, 0},
369 {POUND
, nv_ident
, 0, 0},
371 {K_MOUSEUP
, nv_mousescroll
, 0, TRUE
},
372 {K_MOUSEDOWN
, nv_mousescroll
, 0, FALSE
},
373 {K_LEFTMOUSE
, nv_mouse
, 0, 0},
374 {K_LEFTMOUSE_NM
, nv_mouse
, 0, 0},
375 {K_LEFTDRAG
, nv_mouse
, 0, 0},
376 {K_LEFTRELEASE
, nv_mouse
, 0, 0},
377 {K_LEFTRELEASE_NM
, nv_mouse
, 0, 0},
378 {K_MIDDLEMOUSE
, nv_mouse
, 0, 0},
379 {K_MIDDLEDRAG
, nv_mouse
, 0, 0},
380 {K_MIDDLERELEASE
, nv_mouse
, 0, 0},
381 {K_RIGHTMOUSE
, nv_mouse
, 0, 0},
382 {K_RIGHTDRAG
, nv_mouse
, 0, 0},
383 {K_RIGHTRELEASE
, nv_mouse
, 0, 0},
384 {K_X1MOUSE
, nv_mouse
, 0, 0},
385 {K_X1DRAG
, nv_mouse
, 0, 0},
386 {K_X1RELEASE
, nv_mouse
, 0, 0},
387 {K_X2MOUSE
, nv_mouse
, 0, 0},
388 {K_X2DRAG
, nv_mouse
, 0, 0},
389 {K_X2RELEASE
, nv_mouse
, 0, 0},
391 {K_IGNORE
, nv_ignore
, NV_KEEPREG
, 0},
392 {K_NOP
, nv_nop
, 0, 0},
393 {K_INS
, nv_edit
, 0, 0},
394 {K_KINS
, nv_edit
, 0, 0},
395 {K_BS
, nv_ctrlh
, 0, 0},
396 {K_UP
, nv_up
, NV_SSS
|NV_STS
, FALSE
},
397 {K_S_UP
, nv_page
, NV_SS
, BACKWARD
},
398 {K_DOWN
, nv_down
, NV_SSS
|NV_STS
, FALSE
},
399 {K_S_DOWN
, nv_page
, NV_SS
, FORWARD
},
400 {K_LEFT
, nv_left
, NV_SSS
|NV_STS
|NV_RL
, 0},
401 {K_S_LEFT
, nv_bck_word
, NV_SS
|NV_RL
, 0},
402 {K_C_LEFT
, nv_bck_word
, NV_SSS
|NV_RL
|NV_STS
, 1},
403 {K_RIGHT
, nv_right
, NV_SSS
|NV_STS
|NV_RL
, 0},
404 {K_S_RIGHT
, nv_wordcmd
, NV_SS
|NV_RL
, FALSE
},
405 {K_C_RIGHT
, nv_wordcmd
, NV_SSS
|NV_RL
|NV_STS
, TRUE
},
406 {K_PAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
407 {K_KPAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
408 {K_PAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
409 {K_KPAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
410 {K_END
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
411 {K_KEND
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
412 {K_S_END
, nv_end
, NV_SS
, FALSE
},
413 {K_C_END
, nv_end
, NV_SSS
|NV_STS
, TRUE
},
414 {K_HOME
, nv_home
, NV_SSS
|NV_STS
, 0},
415 {K_KHOME
, nv_home
, NV_SSS
|NV_STS
, 0},
416 {K_S_HOME
, nv_home
, NV_SS
, 0},
417 {K_C_HOME
, nv_goto
, NV_SSS
|NV_STS
, FALSE
},
418 {K_DEL
, nv_abbrev
, 0, 0},
419 {K_KDEL
, nv_abbrev
, 0, 0},
420 {K_UNDO
, nv_kundo
, 0, 0},
421 {K_HELP
, nv_help
, NV_NCW
, 0},
422 {K_F1
, nv_help
, NV_NCW
, 0},
423 {K_XF1
, nv_help
, NV_NCW
, 0},
425 {K_SELECT
, nv_select
, 0, 0},
428 {K_VER_SCROLLBAR
, nv_ver_scrollbar
, 0, 0},
429 {K_HOR_SCROLLBAR
, nv_hor_scrollbar
, 0, 0},
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE
, nv_tabline
, 0, 0},
433 {K_TABMENU
, nv_tabmenu
, 0, 0},
436 {K_F8
, farsi_fkey
, 0, 0},
437 {K_F9
, farsi_fkey
, 0, 0},
440 {K_SNIFF
, nv_sniff
, 0, 0},
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21
, nv_nbcmd
, NV_NCH_ALW
, 0},
446 {K_DROP
, nv_drop
, NV_STS
, 0},
449 {K_CURSORHOLD
, nv_cursorhold
, NV_KEEPREG
, 0},
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx
[NV_CMDS_SIZE
];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear
;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
477 /* The commands are sorted on absolute value. */
478 c1
= nv_cmds
[*(const short *)s1
].cmd_char
;
479 c2
= nv_cmds
[*(const short *)s2
].cmd_char
;
488 * Initialize the nv_cmd_idx[] table.
495 /* Fill the index table with a one to one relation. */
496 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx
, (size_t)NV_CMDS_SIZE
, sizeof(short), nv_compare
);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
504 if (i
!= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
)
506 nv_max_linear
= i
- 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
514 find_command(cmdchar
)
523 /* A multi-byte character is never a command. */
524 if (cmdchar
>= 0x100)
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
533 /* If the character is in the first part: The character is the index into
535 if (cmdchar
<= nv_max_linear
)
536 return nv_cmd_idx
[cmdchar
];
538 /* Perform a binary search. */
539 bot
= nv_max_linear
+ 1;
540 top
= NV_CMDS_SIZE
- 1;
545 c
= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
;
562 * Execute a command in Normal mode.
566 normal_cmd(oap
, toplevel
)
568 int toplevel
; /* TRUE when called from main() */
570 cmdarg_T ca
; /* command arguments */
572 int ctrl_w
= FALSE
; /* got CTRL-W command */
573 int old_col
= curwin
->w_curswant
;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf
; /* need to call out_flush() */
578 pos_T old_pos
; /* cursor position before command */
580 static int old_mapped_len
= 0;
584 vim_memset(&ca
, 0, sizeof(ca
)); /* also resets ca.retval */
587 /* Use a count remembered from before entering an operator. After typing
588 * "3d" we return from normal_cmd() and come back here, the "3" is
589 * remembered in "opcount". */
590 ca
.opcount
= opcount
;
593 want_sniff_request
= sniff_connected
;
597 * If there is an operator pending, then the command we take this time
598 * will terminate it. Finish_op tells us to finish the operation before
599 * returning this time (unless the operation was cancelled).
604 finish_op
= (oap
->op_type
!= OP_NOP
);
608 ui_cursor_shape(); /* may show different cursor shape */
609 # ifdef FEAT_MOUSESHAPE
610 update_mouseshape(-1);
615 /* When not finishing an operator and no register name typed, reset the
617 if (!finish_op
&& !oap
->regname
)
621 /* Restore counts from before receiving K_CURSORHOLD. This means after
622 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
624 if (oap
->prev_opcount
> 0 || oap
->prev_count0
> 0)
626 ca
.opcount
= oap
->prev_opcount
;
627 ca
.count0
= oap
->prev_count0
;
628 oap
->prev_opcount
= 0;
629 oap
->prev_count0
= 0;
634 mapped_len
= typebuf_maplen();
638 #ifdef USE_ON_FLY_SCROLL
639 dont_scroll
= FALSE
; /* allow scrolling here */
643 * Get the command character from the user.
648 LANGMAP_ADJUST(c
, TRUE
);
653 * If a mapping was started in Visual or Select mode, remember the length
654 * of the mapping. This is used below to not return to Insert mode for as
655 * long as the mapping is being executed.
657 if (restart_edit
== 0)
659 else if (old_mapped_len
660 || (VIsual_active
&& mapped_len
== 0 && typebuf_maplen() > 0))
661 old_mapped_len
= typebuf_maplen();
669 * In Select mode, typed text replaces the selection.
673 && (vim_isprintc(c
) || c
== NL
|| c
== CAR
|| c
== K_KENTER
))
675 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
676 * 'insertmode' is set) fake a "d"elete command, Insert mode will
677 * restart automatically.
678 * Insert the typed character in the typeahead buffer, so that it can
679 * be mapped in Insert mode. Required for ":lmap" to work. */
681 if (restart_edit
!= 0)
685 msg_nowait
= TRUE
; /* don't delay going to insert mode */
689 #ifdef FEAT_CMDL_INFO
690 need_flushbuf
= add_to_showcmd(c
);
695 if (!(VIsual_active
&& VIsual_select
))
699 * Handle a count before a command and compute ca.count0.
700 * Note that '0' is a command and not the start of a count, but it's
701 * part of a count after other digits.
703 while ( (c
>= '1' && c
<= '9')
704 || (ca
.count0
!= 0 && (c
== K_DEL
|| c
== K_KDEL
|| c
== '0')))
706 if (c
== K_DEL
|| c
== K_KDEL
)
709 #ifdef FEAT_CMDL_INFO
710 del_from_showcmd(4); /* delete the digit and ~@% */
714 ca
.count0
= ca
.count0
* 10 + (c
- '0');
715 if (ca
.count0
< 0) /* got too large! */
716 ca
.count0
= 999999999L;
718 /* Set v:count here, when called from main() and not a stuffed
719 * command, so that v:count can be used in an expression mapping
720 * right after the count. */
721 if (toplevel
&& stuff_empty())
722 set_vcount(ca
.count0
, ca
.count0
== 0 ? 1 : ca
.count0
);
727 ++allow_keys
; /* no mapping for nchar, but keys */
729 ++no_zero_mapping
; /* don't map zero here */
732 LANGMAP_ADJUST(c
, TRUE
);
740 #ifdef FEAT_CMDL_INFO
741 need_flushbuf
|= add_to_showcmd(c
);
746 * If we got CTRL-W there may be a/another count
748 if (c
== Ctrl_W
&& !ctrl_w
&& oap
->op_type
== OP_NOP
)
751 ca
.opcount
= ca
.count0
; /* remember first count */
754 ++allow_keys
; /* no mapping for nchar, but keys */
755 c
= plain_vgetc(); /* get next character */
757 LANGMAP_ADJUST(c
, TRUE
);
761 #ifdef FEAT_CMDL_INFO
762 need_flushbuf
|= add_to_showcmd(c
);
764 goto getcount
; /* jump back */
769 if (c
== K_CURSORHOLD
)
771 /* Save the count values so that ca.opcount and ca.count0 are exactly
772 * the same when coming back here after handling K_CURSORHOLD. */
773 oap
->prev_opcount
= ca
.opcount
;
774 oap
->prev_count0
= ca
.count0
;
781 * If we're in the middle of an operator (including after entering a
782 * yank buffer with '"') AND we had a count before the operator, then
783 * that count overrides the current value of ca.count0.
784 * What this means effectively, is that commands like "3dw" get turned
785 * into "d3w" which makes things fall into place pretty neatly.
786 * If you give a count before AND after the operator, they are
790 ca
.count0
*= ca
.opcount
;
792 ca
.count0
= ca
.opcount
;
796 * Always remember the count. It will be set to zero (on the next call,
797 * above) when there is no pending operator.
798 * When called from main(), save the count for use by the "count" built-in
801 ca
.opcount
= ca
.count0
;
802 ca
.count1
= (ca
.count0
== 0 ? 1 : ca
.count0
);
806 * Only set v:count when called from main() and not a stuffed command.
808 if (toplevel
&& stuff_empty())
809 set_vcount(ca
.count0
, ca
.count1
);
813 * Find the command character in the table of commands.
814 * For CTRL-W we already got nchar when looking for a count.
823 idx
= find_command(ca
.cmdchar
);
826 /* Not a known command: beep. */
831 if (text_locked() && (nv_cmds
[idx
].cmd_flags
& NV_NCW
))
833 /* This command is not allowed while editing a ccmdline: beep. */
839 if ((nv_cmds
[idx
].cmd_flags
& NV_NCW
) && curbuf_locked())
845 * In Visual/Select mode, a few keys are handled in a special way.
849 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
851 && (nv_cmds
[idx
].cmd_flags
& NV_STS
)
852 && !(mod_mask
& MOD_MASK_SHIFT
))
855 redraw_curbuf_later(INVERTED
);
858 /* Keys that work different when 'keymodel' contains "startsel" */
861 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
863 unshift_special(&ca
);
864 idx
= find_command(ca
.cmdchar
);
872 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
873 && (mod_mask
& MOD_MASK_SHIFT
))
875 mod_mask
&= ~MOD_MASK_SHIFT
;
881 #ifdef FEAT_RIGHTLEFT
882 if (curwin
->w_p_rl
&& KeyTyped
&& !KeyStuffed
883 && (nv_cmds
[idx
].cmd_flags
& NV_RL
))
885 /* Invert horizontal movements and operations. Only when typed by the
886 * user directly, not when the result of a mapping or "x" translated
890 case 'l': ca
.cmdchar
= 'h'; break;
891 case K_RIGHT
: ca
.cmdchar
= K_LEFT
; break;
892 case K_S_RIGHT
: ca
.cmdchar
= K_S_LEFT
; break;
893 case K_C_RIGHT
: ca
.cmdchar
= K_C_LEFT
; break;
894 case 'h': ca
.cmdchar
= 'l'; break;
895 case K_LEFT
: ca
.cmdchar
= K_RIGHT
; break;
896 case K_S_LEFT
: ca
.cmdchar
= K_S_RIGHT
; break;
897 case K_C_LEFT
: ca
.cmdchar
= K_C_RIGHT
; break;
898 case '>': ca
.cmdchar
= '<'; break;
899 case '<': ca
.cmdchar
= '>'; break;
901 idx
= find_command(ca
.cmdchar
);
906 * Get an additional character if we need one.
908 if ((nv_cmds
[idx
].cmd_flags
& NV_NCH
)
909 && (((nv_cmds
[idx
].cmd_flags
& NV_NCH_NOP
) == NV_NCH_NOP
910 && oap
->op_type
== OP_NOP
)
911 || (nv_cmds
[idx
].cmd_flags
& NV_NCH_ALW
) == NV_NCH_ALW
912 || (ca
.cmdchar
== 'q'
913 && oap
->op_type
== OP_NOP
916 || ((ca
.cmdchar
== 'a' || ca
.cmdchar
== 'i')
917 && (oap
->op_type
!= OP_NOP
924 int repl
= FALSE
; /* get character for replace mode */
925 int lit
= FALSE
; /* get extra character literally */
926 int langmap_active
= FALSE
; /* using :lmap mappings */
927 int lang
; /* getting a text character */
928 #ifdef USE_IM_CONTROL
929 int save_smd
; /* saved value of p_smd */
933 ++allow_keys
; /* no mapping for nchar, but allow key codes */
935 /* Don't generate a CursorHold event here, most commands can't handle
936 * it, e.g., nv_replace(), nv_csearch(). */
937 did_cursorhold
= TRUE
;
939 if (ca
.cmdchar
== 'g')
942 * For 'g' get the next character now, so that we can check for
943 * "gr", "g'" and "g`".
945 ca
.nchar
= plain_vgetc();
947 LANGMAP_ADJUST(ca
.nchar
, TRUE
);
949 #ifdef FEAT_CMDL_INFO
950 need_flushbuf
|= add_to_showcmd(ca
.nchar
);
952 if (ca
.nchar
== 'r' || ca
.nchar
== '\'' || ca
.nchar
== '`'
953 || ca
.nchar
== Ctrl_BSL
)
955 cp
= &ca
.extra_char
; /* need to get a third character */
957 lit
= TRUE
; /* get it literally */
959 repl
= TRUE
; /* get it in replace mode */
962 cp
= NULL
; /* no third character needed */
966 if (ca
.cmdchar
== 'r') /* get it in replace mode */
970 lang
= (repl
|| (nv_cmds
[idx
].cmd_flags
& NV_LANG
));
973 * Get a second or third character.
980 State
= REPLACE
; /* pretend Replace mode */
981 ui_cursor_shape(); /* show different cursor shape */
984 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
986 /* Allow mappings defined with ":lmap". */
993 langmap_active
= TRUE
;
995 #ifdef USE_IM_CONTROL
997 p_smd
= FALSE
; /* Don't let the IM code show the mode here */
998 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_IM
)
1002 *cp
= plain_vgetc();
1006 /* Undo the decrement done above */
1009 State
= NORMAL_BUSY
;
1011 #ifdef USE_IM_CONTROL
1014 if (curbuf
->b_p_iminsert
!= B_IMODE_LMAP
)
1015 im_save_status(&curbuf
->b_p_iminsert
);
1016 im_set_active(FALSE
);
1021 State
= NORMAL_BUSY
;
1023 #ifdef FEAT_CMDL_INFO
1024 need_flushbuf
|= add_to_showcmd(*cp
);
1029 #ifdef FEAT_DIGRAPHS
1030 /* Typing CTRL-K gets a digraph. */
1032 && ((nv_cmds
[idx
].cmd_flags
& NV_LANG
)
1033 || cp
== &ca
.extra_char
)
1034 && vim_strchr(p_cpo
, CPO_DIGRAPH
) == NULL
)
1036 c
= get_digraph(FALSE
);
1040 # ifdef FEAT_CMDL_INFO
1041 /* Guessing how to update showcmd here... */
1042 del_from_showcmd(3);
1043 need_flushbuf
|= add_to_showcmd(*cp
);
1050 /* adjust chars > 127, except after "tTfFr" commands */
1051 LANGMAP_ADJUST(*cp
, !lang
);
1053 #ifdef FEAT_RIGHTLEFT
1054 /* adjust Hebrew mapped char */
1055 if (p_hkmap
&& lang
&& KeyTyped
)
1058 /* adjust Farsi mapped char */
1059 if (p_fkmap
&& lang
&& KeyTyped
)
1066 * When the next character is CTRL-\ a following CTRL-N means the
1067 * command is aborted and we go to Normal mode.
1069 if (cp
== &ca
.extra_char
1070 && ca
.nchar
== Ctrl_BSL
1071 && (ca
.extra_char
== Ctrl_N
|| ca
.extra_char
== Ctrl_G
))
1073 ca
.cmdchar
= Ctrl_BSL
;
1074 ca
.nchar
= ca
.extra_char
;
1075 idx
= find_command(ca
.cmdchar
);
1077 else if (*cp
== Ctrl_BSL
)
1079 long towait
= (p_ttm
>= 0 ? p_ttm
: p_tm
);
1081 /* There is a busy wait here when typing "f<C-\>" and then
1082 * something different from CTRL-N. Can't be avoided. */
1083 while ((c
= vpeekc()) <= 0 && towait
> 0L)
1085 do_sleep(towait
> 50L ? 50L : towait
);
1091 if (c
!= Ctrl_N
&& c
!= Ctrl_G
)
1095 ca
.cmdchar
= Ctrl_BSL
;
1097 idx
= find_command(ca
.cmdchar
);
1103 /* When getting a text character and the next character is a
1104 * multi-byte character, it could be a composing character.
1105 * However, don't wait for it to arrive. */
1106 while (enc_utf8
&& lang
&& (c
= vpeekc()) > 0
1107 && (c
>= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1110 if (!utf_iscomposing(c
))
1112 vungetc(c
); /* it wasn't, put it back */
1115 else if (ca
.ncharC1
== 0)
1126 #ifdef FEAT_CMDL_INFO
1128 * Flush the showcmd characters onto the screen so we can see them while
1129 * the command is being executed. Only do this when the shown command was
1130 * actually displayed, otherwise this will slow down a lot when executing
1137 if (ca
.cmdchar
!= K_IGNORE
)
1138 did_cursorhold
= FALSE
;
1143 if (ca
.nchar
== ESC
)
1146 if (restart_edit
== 0 && goto_im())
1151 if (ca
.cmdchar
!= K_IGNORE
)
1153 msg_didout
= FALSE
; /* don't scroll screen up for normal command */
1158 old_pos
= curwin
->w_cursor
; /* remember where cursor was */
1160 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1162 if (!VIsual_active
&& km_startsel
)
1164 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
1167 unshift_special(&ca
);
1168 idx
= find_command(ca
.cmdchar
);
1170 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
1171 && (mod_mask
& MOD_MASK_SHIFT
))
1174 mod_mask
&= ~MOD_MASK_SHIFT
;
1180 * Execute the command!
1181 * Call the command function found in the commands table.
1183 ca
.arg
= nv_cmds
[idx
].cmd_arg
;
1184 (nv_cmds
[idx
].cmd_func
)(&ca
);
1187 * If we didn't start or finish an operator, reset oap->regname, unless we
1192 && (idx
< 0 || !(nv_cmds
[idx
].cmd_flags
& NV_KEEPREG
)))
1201 /* Get the length of mapped chars again after typing a count, second
1202 * character or "z333<cr>". */
1203 if (old_mapped_len
> 0)
1204 old_mapped_len
= typebuf_maplen();
1208 * If an operation is pending, handle it...
1210 do_pending_operator(&ca
, old_col
, FALSE
);
1213 * Wait for a moment when a message is displayed that will be overwritten
1214 * by the mode message.
1215 * In Visual mode and with "^O" in Insert mode, a short message will be
1216 * overwritten by the mode message. Wait a bit, until a key is hit.
1217 * In Visual mode, it's more important to keep the Visual area updated
1218 * than keeping a message (e.g. from a /pat search).
1219 * Only do this if the command was typed, not from a mapping.
1220 * Don't wait when emsg_silent is non-zero.
1221 * Also wait a bit after an error message, e.g. for "^O:".
1222 * Don't redraw the screen, it would remove the message.
1226 && (restart_edit
!= 0
1229 && old_pos
.lnum
== curwin
->w_cursor
.lnum
1230 && old_pos
.col
== curwin
->w_cursor
.col
)
1235 && (msg_didout
|| (msg_didany
&& msg_scroll
))
1238 || (restart_edit
!= 0
1243 || emsg_on_display
)))
1244 && oap
->regname
== 0
1245 && !(ca
.retval
& CA_COMMAND_BUSY
)
1250 && oap
->op_type
== OP_NOP
)
1252 int save_State
= State
;
1254 /* Draw the cursor with the right shape here */
1255 if (restart_edit
!= 0)
1258 /* If need to redraw, and there is a "keep_msg", redraw before the
1260 if (must_redraw
&& keep_msg
!= NULL
&& !emsg_on_display
)
1266 /* showmode() will clear keep_msg, but we want to use it anyway */
1268 /* now reset it, otherwise it's put in the history again */
1270 msg_attr(kmsg
, keep_msg_attr
);
1276 if (msg_scroll
|| emsg_on_display
)
1277 ui_delay(1000L, TRUE
); /* wait at least one second */
1278 ui_delay(3000L, FALSE
); /* wait up to three seconds */
1282 emsg_on_display
= FALSE
;
1286 * Finish up after executing a Normal mode command.
1292 /* Reset finish_op, in case it was set */
1298 /* Redraw the cursor with another shape, if we were in Operator-pending
1299 * mode or did a replace command. */
1300 if (c
|| ca
.cmdchar
== 'r')
1302 ui_cursor_shape(); /* may show different cursor shape */
1303 # ifdef FEAT_MOUSESHAPE
1304 update_mouseshape(-1);
1309 #ifdef FEAT_CMDL_INFO
1310 if (oap
->op_type
== OP_NOP
&& oap
->regname
== 0
1311 # ifdef FEAT_AUTOCMD
1312 && ca
.cmdchar
!= K_CURSORHOLD
1318 checkpcmark(); /* check if we moved since setting pcmark */
1319 vim_free(ca
.searchbuf
);
1326 #ifdef FEAT_SCROLLBIND
1327 if (curwin
->w_p_scb
&& toplevel
)
1329 validate_cursor(); /* may need to update w_leftcol */
1330 do_check_scrollbind(TRUE
);
1335 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1336 * if still inside a mapping that started in Visual mode).
1337 * May switch from Visual to Select mode after CTRL-O command.
1339 if ( oap
->op_type
== OP_NOP
1341 && ((restart_edit
!= 0 && !VIsual_active
&& old_mapped_len
== 0)
1342 || restart_VIsual_select
== 1)
1344 && restart_edit
!= 0
1346 && !(ca
.retval
& CA_COMMAND_BUSY
)
1348 && oap
->regname
== 0)
1351 if (restart_VIsual_select
== 1)
1353 VIsual_select
= TRUE
;
1355 restart_VIsual_select
= 0;
1358 if (restart_edit
!= 0
1360 && !VIsual_active
&& old_mapped_len
== 0
1363 (void)edit(restart_edit
, FALSE
, 1L);
1367 if (restart_VIsual_select
== 2)
1368 restart_VIsual_select
= 1;
1371 /* Save count before an operator for next time. */
1372 opcount
= ca
.opcount
;
1376 * Handle an operator after visual mode or when the movement is finished
1379 do_pending_operator(cap
, old_col
, gui_yank
)
1384 oparg_T
*oap
= cap
->oap
;
1386 int empty_region_error
;
1387 int restart_edit_save
;
1390 /* The visual area is remembered for redo */
1391 static int redo_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
1392 static linenr_T redo_VIsual_line_count
; /* number of lines */
1393 static colnr_T redo_VIsual_col
; /* number of cols or end column */
1394 static long redo_VIsual_count
; /* count for Visual operator */
1395 # ifdef FEAT_VIRTUALEDIT
1396 int include_line_break
= FALSE
;
1400 #if defined(FEAT_CLIPBOARD)
1402 * Yank the visual area into the GUI selection register before we operate
1403 * on it and lose it forever.
1404 * Don't do it if a specific register was specified, so that ""x"*P works.
1405 * This could call do_pending_operator() recursively, but that's OK
1406 * because gui_yank will be TRUE for the nested call.
1408 if (clip_star
.available
1409 && oap
->op_type
!= OP_NOP
1413 && !redo_VIsual_busy
1415 && oap
->regname
== 0)
1418 old_cursor
= curwin
->w_cursor
;
1421 * If an operation is pending, handle it...
1427 ) && oap
->op_type
!= OP_NOP
)
1430 oap
->is_VIsual
= VIsual_active
;
1431 if (oap
->motion_force
== 'V')
1432 oap
->motion_type
= MLINE
;
1433 else if (oap
->motion_force
== 'v')
1435 /* If the motion was linewise, "inclusive" will not have been set.
1436 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1437 if (oap
->motion_type
== MLINE
)
1438 oap
->inclusive
= FALSE
;
1439 /* If the motion already was characterwise, toggle "inclusive" */
1440 else if (oap
->motion_type
== MCHAR
)
1441 oap
->inclusive
= !oap
->inclusive
;
1442 oap
->motion_type
= MCHAR
;
1444 else if (oap
->motion_force
== Ctrl_V
)
1446 /* Change line- or characterwise motion into Visual block mode. */
1447 VIsual_active
= TRUE
;
1448 VIsual
= oap
->start
;
1449 VIsual_mode
= Ctrl_V
;
1450 VIsual_select
= FALSE
;
1451 VIsual_reselect
= FALSE
;
1455 /* only redo yank when 'y' flag is in 'cpoptions' */
1456 /* never redo "zf" (define fold) */
1457 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1459 && (!VIsual_active
|| oap
->motion_force
)
1461 && cap
->cmdchar
!= 'D'
1463 && oap
->op_type
!= OP_FOLD
1464 && oap
->op_type
!= OP_FOLDOPEN
1465 && oap
->op_type
!= OP_FOLDOPENREC
1466 && oap
->op_type
!= OP_FOLDCLOSE
1467 && oap
->op_type
!= OP_FOLDCLOSEREC
1468 && oap
->op_type
!= OP_FOLDDEL
1469 && oap
->op_type
!= OP_FOLDDELREC
1473 prep_redo(oap
->regname
, cap
->count0
,
1474 get_op_char(oap
->op_type
), get_extra_op_char(oap
->op_type
),
1475 oap
->motion_force
, cap
->cmdchar
, cap
->nchar
);
1476 if (cap
->cmdchar
== '/' || cap
->cmdchar
== '?') /* was a search */
1479 * If 'cpoptions' does not contain 'r', insert the search
1480 * pattern to really repeat the same command.
1482 if (vim_strchr(p_cpo
, CPO_REDO
) == NULL
)
1483 AppendToRedobuffLit(cap
->searchbuf
, -1);
1484 AppendToRedobuff(NL_STR
);
1486 else if (cap
->cmdchar
== ':')
1488 /* do_cmdline() has stored the first typed line in
1489 * "repeat_cmdline". When several lines are typed repeating
1490 * won't be possible. */
1491 if (repeat_cmdline
== NULL
)
1495 AppendToRedobuffLit(repeat_cmdline
, -1);
1496 AppendToRedobuff(NL_STR
);
1497 vim_free(repeat_cmdline
);
1498 repeat_cmdline
= NULL
;
1504 if (redo_VIsual_busy
)
1506 oap
->start
= curwin
->w_cursor
;
1507 curwin
->w_cursor
.lnum
+= redo_VIsual_line_count
- 1;
1508 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1509 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1510 VIsual_mode
= redo_VIsual_mode
;
1511 if (VIsual_mode
== 'v')
1513 if (redo_VIsual_line_count
<= 1)
1514 curwin
->w_cursor
.col
+= redo_VIsual_col
- 1;
1516 curwin
->w_cursor
.col
= redo_VIsual_col
;
1518 if (redo_VIsual_col
== MAXCOL
)
1520 curwin
->w_curswant
= MAXCOL
;
1521 coladvance((colnr_T
)MAXCOL
);
1523 cap
->count0
= redo_VIsual_count
;
1524 if (redo_VIsual_count
!= 0)
1525 cap
->count1
= redo_VIsual_count
;
1529 else if (VIsual_active
)
1533 /* Save the current VIsual area for '< and '> marks, and "gv" */
1534 curbuf
->b_visual
.vi_start
= VIsual
;
1535 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
1536 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
1537 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
1539 curbuf
->b_visual_mode_eval
= VIsual_mode
;
1543 /* In Select mode, a linewise selection is operated upon like a
1544 * characterwise selection. */
1545 if (VIsual_select
&& VIsual_mode
== 'V')
1547 if (lt(VIsual
, curwin
->w_cursor
))
1550 curwin
->w_cursor
.col
=
1551 (colnr_T
)STRLEN(ml_get(curwin
->w_cursor
.lnum
));
1555 curwin
->w_cursor
.col
= 0;
1556 VIsual
.col
= (colnr_T
)STRLEN(ml_get(VIsual
.lnum
));
1560 /* If 'selection' is "exclusive", backup one character for
1561 * charwise selections. */
1562 else if (VIsual_mode
== 'v')
1564 # ifdef FEAT_VIRTUALEDIT
1565 include_line_break
=
1570 oap
->start
= VIsual
;
1571 if (VIsual_mode
== 'V')
1574 #endif /* FEAT_VISUAL */
1577 * Set oap->start to the first position of the operated text, oap->end
1578 * to the end of the operated text. w_cursor is equal to oap->start.
1580 if (lt(oap
->start
, curwin
->w_cursor
))
1583 /* Include folded lines completely. */
1586 if (hasFolding(oap
->start
.lnum
, &oap
->start
.lnum
, NULL
))
1588 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
,
1589 &curwin
->w_cursor
.lnum
))
1590 curwin
->w_cursor
.col
= (colnr_T
)STRLEN(ml_get_curline());
1593 oap
->end
= curwin
->w_cursor
;
1594 curwin
->w_cursor
= oap
->start
;
1596 /* w_virtcol may have been updated; if the cursor goes back to its
1597 * previous position w_virtcol becomes invalid and isn't updated
1599 curwin
->w_valid
&= ~VALID_VIRTCOL
;
1604 /* Include folded lines completely. */
1605 if (!VIsual_active
&& oap
->motion_type
== MLINE
)
1607 if (hasFolding(curwin
->w_cursor
.lnum
, &curwin
->w_cursor
.lnum
,
1609 curwin
->w_cursor
.col
= 0;
1610 if (hasFolding(oap
->start
.lnum
, NULL
, &oap
->start
.lnum
))
1611 oap
->start
.col
= (colnr_T
)STRLEN(ml_get(oap
->start
.lnum
));
1614 oap
->end
= oap
->start
;
1615 oap
->start
= curwin
->w_cursor
;
1618 oap
->line_count
= oap
->end
.lnum
- oap
->start
.lnum
+ 1;
1620 #ifdef FEAT_VIRTUALEDIT
1621 /* Set "virtual_op" before resetting VIsual_active. */
1622 virtual_op
= virtual_active();
1626 if (VIsual_active
|| redo_VIsual_busy
)
1628 if (VIsual_mode
== Ctrl_V
) /* block mode */
1632 oap
->block_mode
= TRUE
;
1634 getvvcol(curwin
, &(oap
->start
),
1635 &oap
->start_vcol
, NULL
, &oap
->end_vcol
);
1636 if (!redo_VIsual_busy
)
1638 getvvcol(curwin
, &(oap
->end
), &start
, NULL
, &end
);
1640 if (start
< oap
->start_vcol
)
1641 oap
->start_vcol
= start
;
1642 if (end
> oap
->end_vcol
)
1644 if (*p_sel
== 'e' && start
>= 1
1645 && start
- 1 >= oap
->end_vcol
)
1646 oap
->end_vcol
= start
- 1;
1648 oap
->end_vcol
= end
;
1652 /* if '$' was used, get oap->end_vcol from longest line */
1653 if (curwin
->w_curswant
== MAXCOL
)
1655 curwin
->w_cursor
.col
= MAXCOL
;
1657 for (curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
1658 curwin
->w_cursor
.lnum
<= oap
->end
.lnum
;
1659 ++curwin
->w_cursor
.lnum
)
1661 getvvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &end
);
1662 if (end
> oap
->end_vcol
)
1663 oap
->end_vcol
= end
;
1666 else if (redo_VIsual_busy
)
1667 oap
->end_vcol
= oap
->start_vcol
+ redo_VIsual_col
- 1;
1669 * Correct oap->end.col and oap->start.col to be the
1670 * upper-left and lower-right corner of the block area.
1672 * (Actually, this does convert column positions into character
1675 curwin
->w_cursor
.lnum
= oap
->end
.lnum
;
1676 coladvance(oap
->end_vcol
);
1677 oap
->end
= curwin
->w_cursor
;
1679 curwin
->w_cursor
= oap
->start
;
1680 coladvance(oap
->start_vcol
);
1681 oap
->start
= curwin
->w_cursor
;
1684 if (!redo_VIsual_busy
&& !gui_yank
)
1687 * Prepare to reselect and redo Visual: this is based on the
1688 * size of the Visual text
1690 resel_VIsual_mode
= VIsual_mode
;
1691 if (curwin
->w_curswant
== MAXCOL
)
1692 resel_VIsual_col
= MAXCOL
;
1693 else if (VIsual_mode
== Ctrl_V
)
1694 resel_VIsual_col
= oap
->end_vcol
- oap
->start_vcol
+ 1;
1695 else if (oap
->line_count
> 1)
1696 resel_VIsual_col
= oap
->end
.col
;
1698 resel_VIsual_col
= oap
->end
.col
- oap
->start
.col
+ 1;
1699 resel_VIsual_line_count
= oap
->line_count
;
1702 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1703 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1704 && oap
->op_type
!= OP_COLON
1706 && oap
->op_type
!= OP_FOLD
1707 && oap
->op_type
!= OP_FOLDOPEN
1708 && oap
->op_type
!= OP_FOLDOPENREC
1709 && oap
->op_type
!= OP_FOLDCLOSE
1710 && oap
->op_type
!= OP_FOLDCLOSEREC
1711 && oap
->op_type
!= OP_FOLDDEL
1712 && oap
->op_type
!= OP_FOLDDELREC
1714 && oap
->motion_force
== NUL
1717 /* Prepare for redoing. Only use the nchar field for "r",
1718 * otherwise it might be the second char of the operator. */
1719 prep_redo(oap
->regname
, 0L, NUL
, 'v',
1720 get_op_char(oap
->op_type
),
1721 get_extra_op_char(oap
->op_type
),
1722 oap
->op_type
== OP_REPLACE
? cap
->nchar
: NUL
);
1723 if (!redo_VIsual_busy
)
1725 redo_VIsual_mode
= resel_VIsual_mode
;
1726 redo_VIsual_col
= resel_VIsual_col
;
1727 redo_VIsual_line_count
= resel_VIsual_line_count
;
1728 redo_VIsual_count
= cap
->count0
;
1733 * oap->inclusive defaults to TRUE.
1734 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1735 * FALSE. This makes "d}P" and "v}dP" work the same.
1737 if (oap
->motion_force
== NUL
|| oap
->motion_type
== MLINE
)
1738 oap
->inclusive
= TRUE
;
1739 if (VIsual_mode
== 'V')
1740 oap
->motion_type
= MLINE
;
1743 oap
->motion_type
= MCHAR
;
1744 if (VIsual_mode
!= Ctrl_V
&& *ml_get_pos(&(oap
->end
)) == NUL
1745 # ifdef FEAT_VIRTUALEDIT
1746 && (include_line_break
|| !virtual_op
)
1750 oap
->inclusive
= FALSE
;
1751 /* Try to include the newline, unless it's an operator
1752 * that works on lines only */
1754 && !op_on_lines(oap
->op_type
)
1755 && oap
->end
.lnum
< curbuf
->b_ml
.ml_line_count
)
1759 # ifdef FEAT_VIRTUALEDIT
1760 oap
->end
.coladd
= 0;
1767 redo_VIsual_busy
= FALSE
;
1770 * Switch Visual off now, so screen updating does
1771 * not show inverted text when the screen is redrawn.
1772 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1773 * no screen redraw, so it is done here to remove the inverted
1778 VIsual_active
= FALSE
;
1784 clear_cmdline
= TRUE
; /* unshow visual mode later */
1785 #ifdef FEAT_CMDL_INFO
1789 if ((oap
->op_type
== OP_YANK
1790 || oap
->op_type
== OP_COLON
1791 || oap
->op_type
== OP_FUNCTION
1792 || oap
->op_type
== OP_FILTER
)
1793 && oap
->motion_force
== NUL
)
1794 redraw_curbuf_later(INVERTED
);
1800 /* Include the trailing byte of a multi-byte char. */
1801 if (has_mbyte
&& oap
->inclusive
)
1805 l
= (*mb_ptr2len
)(ml_get_pos(&oap
->end
));
1807 oap
->end
.col
+= l
- 1;
1810 curwin
->w_set_curswant
= TRUE
;
1813 * oap->empty is set when start and end are the same. The inclusive
1814 * flag affects this too, unless yanking and the end is on a NUL.
1816 oap
->empty
= (oap
->motion_type
== MCHAR
1818 || (oap
->op_type
== OP_YANK
1819 && gchar_pos(&oap
->end
) == NUL
))
1820 && equalpos(oap
->start
, oap
->end
)
1821 #ifdef FEAT_VIRTUALEDIT
1822 && !(virtual_op
&& oap
->start
.coladd
!= oap
->end
.coladd
)
1826 * For delete, change and yank, it's an error to operate on an
1827 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1829 empty_region_error
= (oap
->empty
1830 && vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
);
1833 /* Force a redraw when operating on an empty Visual region, when
1834 * 'modifiable is off or creating a fold. */
1835 if (oap
->is_VIsual
&& (oap
->empty
|| !curbuf
->b_p_ma
1836 # ifdef FEAT_FOLDING
1837 || oap
->op_type
== OP_FOLD
1840 redraw_curbuf_later(INVERTED
);
1844 * If the end of an operator is in column one while oap->motion_type
1845 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1846 * character in the previous line. If op_start is on or before the
1847 * first non-blank in the line, the operator becomes linewise
1848 * (strange, but that's the way vi does it).
1850 if ( oap
->motion_type
== MCHAR
1851 && oap
->inclusive
== FALSE
1852 && !(cap
->retval
& CA_NO_ADJ_OP_END
)
1853 && oap
->end
.col
== 0
1855 && (!oap
->is_VIsual
|| *p_sel
== 'o')
1858 && oap
->line_count
> 1)
1860 oap
->end_adjusted
= TRUE
; /* remember that we did this */
1864 oap
->motion_type
= MLINE
;
1867 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
1871 oap
->inclusive
= TRUE
;
1876 oap
->end_adjusted
= FALSE
;
1878 switch (oap
->op_type
)
1884 oap
->is_VIsual
? (int)cap
->count1
:
1887 auto_format(FALSE
, TRUE
);
1892 if (oap
->line_count
< 2)
1893 oap
->line_count
= 2;
1894 if (curwin
->w_cursor
.lnum
+ oap
->line_count
- 1 >
1895 curbuf
->b_ml
.ml_line_count
)
1899 do_do_join(oap
->line_count
, oap
->op_type
== OP_JOIN
);
1900 auto_format(FALSE
, TRUE
);
1906 VIsual_reselect
= FALSE
; /* don't reselect now */
1908 if (empty_region_error
)
1912 (void)op_delete(oap
);
1913 if (oap
->motion_type
== MLINE
&& has_format_option(FO_AUTO
))
1914 u_save_cursor(); /* cursor line wasn't saved yet */
1915 auto_format(FALSE
, TRUE
);
1920 if (empty_region_error
)
1926 (void)op_yank(oap
, FALSE
, !gui_yank
);
1932 VIsual_reselect
= FALSE
; /* don't reselect now */
1934 if (empty_region_error
)
1938 /* This is a new edit command, not a restart. Need to
1939 * remember it to make 'insertmode' work with mappings for
1940 * Visual mode. But do this only once and not when typed and
1941 * 'insertmode' isn't set. */
1942 if (p_im
|| !KeyTyped
)
1943 restart_edit_save
= restart_edit
;
1945 restart_edit_save
= 0;
1947 /* Reset finish_op now, don't want it set inside edit(). */
1949 if (op_change(oap
)) /* will call edit() */
1950 cap
->retval
|= CA_COMMAND_BUSY
;
1951 if (restart_edit
== 0)
1952 restart_edit
= restart_edit_save
;
1957 if (vim_strchr(p_cpo
, CPO_FILTER
) != NULL
)
1958 AppendToRedobuff((char_u
*)"!\r"); /* use any last used !cmd */
1960 bangredo
= TRUE
; /* do_bang() will put cmd in redo buffer */
1965 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1967 * If 'equalprg' is empty, do the indenting internally.
1969 if (oap
->op_type
== OP_INDENT
&& *get_equalprg() == NUL
)
1972 if (curbuf
->b_p_lisp
)
1974 op_reindent(oap
, get_lisp_indent
);
1978 # ifdef FEAT_CINDENT
1981 *curbuf
->b_p_inde
!= NUL
? get_expr_indent
:
1996 if (empty_region_error
)
2004 #if defined(FEAT_EVAL)
2005 if (*curbuf
->b_p_fex
!= NUL
)
2006 op_formatexpr(oap
); /* use expression */
2010 op_colon(oap
); /* use external command */
2012 op_format(oap
, FALSE
); /* use internal function */
2016 op_format(oap
, TRUE
); /* use internal function */
2020 op_function(oap
); /* call 'operatorfunc' */
2026 VIsual_reselect
= FALSE
; /* don't reselect now */
2028 #ifdef FEAT_VISUALEXTRA
2029 if (empty_region_error
)
2033 /* This is a new edit command, not a restart. Need to
2034 * remember it to make 'insertmode' work with mappings for
2035 * Visual mode. But do this only once. */
2036 restart_edit_save
= restart_edit
;
2039 op_insert(oap
, cap
->count1
);
2041 /* TODO: when inserting in several lines, should format all
2043 auto_format(FALSE
, TRUE
);
2045 if (restart_edit
== 0)
2046 restart_edit
= restart_edit_save
;
2055 VIsual_reselect
= FALSE
; /* don't reselect now */
2057 #ifdef FEAT_VISUALEXTRA
2058 if (empty_region_error
)
2061 #ifdef FEAT_VISUALEXTRA
2063 op_replace(oap
, cap
->nchar
);
2069 VIsual_reselect
= FALSE
; /* don't reselect now */
2070 foldCreate(oap
->start
.lnum
, oap
->end
.lnum
);
2074 case OP_FOLDOPENREC
:
2076 case OP_FOLDCLOSEREC
:
2077 VIsual_reselect
= FALSE
; /* don't reselect now */
2078 opFoldRange(oap
->start
.lnum
, oap
->end
.lnum
,
2079 oap
->op_type
== OP_FOLDOPEN
2080 || oap
->op_type
== OP_FOLDOPENREC
,
2081 oap
->op_type
== OP_FOLDOPENREC
2082 || oap
->op_type
== OP_FOLDCLOSEREC
,
2088 VIsual_reselect
= FALSE
; /* don't reselect now */
2089 deleteFold(oap
->start
.lnum
, oap
->end
.lnum
,
2090 oap
->op_type
== OP_FOLDDELREC
, oap
->is_VIsual
);
2096 #ifdef FEAT_VIRTUALEDIT
2102 * if 'sol' not set, go back to old column for some commands
2104 if (!p_sol
&& oap
->motion_type
== MLINE
&& !oap
->end_adjusted
2105 && (oap
->op_type
== OP_LSHIFT
|| oap
->op_type
== OP_RSHIFT
2106 || oap
->op_type
== OP_DELETE
))
2107 coladvance(curwin
->w_curswant
= old_col
);
2111 curwin
->w_cursor
= old_cursor
;
2114 oap
->block_mode
= FALSE
;
2121 * Handle indent and format operators and visual mode ":".
2127 stuffcharReadbuff(':');
2130 stuffReadbuff((char_u
*)"'<,'>");
2135 * Make the range look nice, so it can be repeated.
2137 if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2138 stuffcharReadbuff('.');
2140 stuffnumReadbuff((long)oap
->start
.lnum
);
2141 if (oap
->end
.lnum
!= oap
->start
.lnum
)
2143 stuffcharReadbuff(',');
2144 if (oap
->end
.lnum
== curwin
->w_cursor
.lnum
)
2145 stuffcharReadbuff('.');
2146 else if (oap
->end
.lnum
== curbuf
->b_ml
.ml_line_count
)
2147 stuffcharReadbuff('$');
2148 else if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2150 stuffReadbuff((char_u
*)".+");
2151 stuffnumReadbuff((long)oap
->line_count
- 1);
2154 stuffnumReadbuff((long)oap
->end
.lnum
);
2157 if (oap
->op_type
!= OP_COLON
)
2158 stuffReadbuff((char_u
*)"!");
2159 if (oap
->op_type
== OP_INDENT
)
2161 #ifndef FEAT_CINDENT
2162 if (*get_equalprg() == NUL
)
2163 stuffReadbuff((char_u
*)"indent");
2166 stuffReadbuff(get_equalprg());
2167 stuffReadbuff((char_u
*)"\n");
2169 else if (oap
->op_type
== OP_FORMAT
)
2172 stuffReadbuff((char_u
*)"fmt");
2174 stuffReadbuff(p_fp
);
2175 stuffReadbuff((char_u
*)"\n']");
2179 * do_cmdline() does the rest
2184 * Handle the "g@" operator: call 'operatorfunc'.
2194 if (*p_opfunc
== NUL
)
2195 EMSG(_("E774: 'operatorfunc' is empty"));
2198 /* Set '[ and '] marks to text to be operated on. */
2199 curbuf
->b_op_start
= oap
->start
;
2200 curbuf
->b_op_end
= oap
->end
;
2201 if (oap
->motion_type
!= MLINE
&& !oap
->inclusive
)
2202 /* Exclude the end position. */
2203 decl(&curbuf
->b_op_end
);
2205 if (oap
->block_mode
)
2206 argv
[0] = (char_u
*)"block";
2207 else if (oap
->motion_type
== MLINE
)
2208 argv
[0] = (char_u
*)"line";
2210 argv
[0] = (char_u
*)"char";
2211 (void)call_func_retnr(p_opfunc
, 1, argv
, FALSE
);
2214 EMSG(_("E775: Eval feature not available"));
2218 #if defined(FEAT_MOUSE) || defined(PROTO)
2220 * Do the appropriate action for the current mouse click in the current mode.
2221 * Not used for Command-line mode.
2224 * event modi- position visual change action
2225 * fier cursor window
2226 * left press - yes end yes
2227 * left press C yes end yes "^]" (2)
2228 * left press S yes end yes "*" (2)
2229 * left drag - yes start if moved no
2230 * left relse - yes start if moved no
2231 * middle press - yes if not active no put register
2232 * middle press - yes if active no yank and put
2233 * right press - yes start or extend yes
2234 * right press S yes no change yes "#" (2)
2235 * right drag - yes extend no
2236 * right relse - yes extend no
2238 * Insert or Replace Mode:
2239 * event modi- position visual change action
2240 * fier cursor window
2241 * left press - yes (cannot be active) yes
2242 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2243 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2244 * left drag - yes start or extend (1) no CTRL-O (1)
2245 * left relse - yes start or extend (1) no CTRL-O (1)
2246 * middle press - no (cannot be active) no put register
2247 * right press - yes start or extend yes CTRL-O
2248 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2250 * (1) only if mouse pointer moved since press
2251 * (2) only if click is in same buffer
2253 * Return TRUE if start_arrow() should be called for edit mode.
2256 do_mouse(oap
, c
, dir
, count
, fixindent
)
2257 oparg_T
*oap
; /* operator argument, can be NULL */
2258 int c
; /* K_LEFTMOUSE, etc */
2259 int dir
; /* Direction to 'put' if necessary */
2261 int fixindent
; /* PUT_FIXINDENT if fixing indent necessary */
2263 static int do_always
= FALSE
; /* ignore 'mouse' setting next time */
2264 static int got_click
= FALSE
; /* got a click some time back */
2266 int which_button
; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2267 int is_click
; /* If FALSE it's a drag or release event */
2268 int is_drag
; /* If TRUE it's a drag event */
2269 int jump_flags
= 0; /* flags for jump_to_mouse() */
2271 int moved
; /* Has cursor moved? */
2272 int in_status_line
; /* mouse in status line */
2273 #ifdef FEAT_VERTSPLIT
2274 int in_sep_line
; /* mouse in vertical separator line */
2277 #if defined(FEAT_FOLDING)
2280 win_T
*old_curwin
= curwin
;
2282 static pos_T orig_cursor
;
2283 colnr_T leftcol
, rightcol
;
2286 int old_active
= VIsual_active
;
2287 int old_mode
= VIsual_mode
;
2291 #if defined(FEAT_FOLDING)
2292 save_cursor
= curwin
->w_cursor
;
2296 * When GUI is active, always recognize mouse events, otherwise:
2297 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2298 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2299 * - For command line and insert mode 'mouse' is checked before calling
2312 if (!mouse_has(MOUSE_VISUAL
))
2317 if (State
== NORMAL
&& !mouse_has(MOUSE_NORMAL
))
2321 which_button
= get_mouse_button(KEY2TERMCAP1(c
), &is_click
, &is_drag
);
2323 #ifdef FEAT_MOUSESHAPE
2324 /* May have stopped dragging the status or separator line. The pointer is
2325 * most likely still on the status or separator line. */
2326 if (!is_drag
&& drag_status_line
)
2328 drag_status_line
= FALSE
;
2329 update_mouseshape(SHAPE_IDX_STATUS
);
2331 # ifdef FEAT_VERTSPLIT
2332 if (!is_drag
&& drag_sep_line
)
2334 drag_sep_line
= FALSE
;
2335 update_mouseshape(SHAPE_IDX_VSEP
);
2341 * Ignore drag and release events if we didn't get a click.
2347 if (!got_click
) /* didn't get click, ignore */
2349 if (!is_drag
) /* release, reset got_click */
2355 * ALT is only used for starging/extending Visual mode.
2357 if ((mod_mask
& MOD_MASK_ALT
))
2362 * CTRL right mouse button does CTRL-T
2364 if (is_click
&& (mod_mask
& MOD_MASK_CTRL
) && which_button
== MOUSE_RIGHT
)
2367 stuffcharReadbuff(Ctrl_O
);
2369 stuffnumReadbuff(count
);
2370 stuffcharReadbuff(Ctrl_T
);
2371 got_click
= FALSE
; /* ignore drag&release now */
2376 * CTRL only works with left mouse button
2378 if ((mod_mask
& MOD_MASK_CTRL
) && which_button
!= MOUSE_LEFT
)
2382 * When a modifier is down, ignore drag and release events, as well as
2383 * multiple clicks and the middle mouse button.
2384 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2386 if ((mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
| MOD_MASK_ALT
2389 || (mod_mask
& MOD_MASK_MULTI_CLICK
)
2390 || which_button
== MOUSE_MIDDLE
)
2391 && !((mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
))
2392 && mouse_model_popup()
2393 && which_button
== MOUSE_LEFT
)
2394 && !((mod_mask
& MOD_MASK_ALT
)
2395 && !mouse_model_popup()
2396 && which_button
== MOUSE_RIGHT
)
2401 * If the button press was used as the movement command for an operator
2402 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2403 * drag/release events.
2405 if (!is_click
&& which_button
== MOUSE_MIDDLE
)
2409 regname
= oap
->regname
;
2414 * Middle mouse button does a 'put' of the selected text
2416 if (which_button
== MOUSE_MIDDLE
)
2418 if (State
== NORMAL
)
2421 * If an operator was pending, we don't know what the user wanted
2422 * to do. Go back to normal mode: Clear the operator and beep().
2424 if (oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2432 * If visual was active, yank the highlighted text and put it
2433 * before the mouse pointer position.
2434 * In Select mode replace the highlighted text with the clipboard.
2440 stuffcharReadbuff(Ctrl_G
);
2441 stuffReadbuff((char_u
*)"\"+p");
2445 stuffcharReadbuff('y');
2446 stuffcharReadbuff(K_MIDDLEMOUSE
);
2448 do_always
= TRUE
; /* ignore 'mouse' setting next time */
2453 * The rest is below jump_to_mouse()
2457 else if ((State
& INSERT
) == 0)
2461 * Middle click in insert mode doesn't move the mouse, just insert the
2462 * contents of a register. '.' register is special, can't insert that
2464 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2465 * happens for the GUI).
2467 if ((State
& INSERT
) || !mouse_has(MOUSE_NORMAL
))
2470 insert_reg(regname
, TRUE
);
2473 #ifdef FEAT_CLIPBOARD
2474 if (clip_star
.available
&& regname
== 0)
2477 if ((State
& REPLACE_FLAG
) && !yank_register_mline(regname
))
2478 insert_reg(regname
, TRUE
);
2481 do_put(regname
, BACKWARD
, 1L, fixindent
| PUT_CURSEND
);
2483 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2484 AppendCharToRedobuff(Ctrl_R
);
2485 AppendCharToRedobuff(fixindent
? Ctrl_P
: Ctrl_O
);
2486 AppendCharToRedobuff(regname
== 0 ? '"' : regname
);
2493 /* When dragging or button-up stay in the same window. */
2495 jump_flags
|= MOUSE_FOCUS
| MOUSE_DID_MOVE
;
2497 start_visual
.lnum
= 0;
2500 /* Check for clicking in the tab page line. */
2501 if (mouse_row
== 0 && firstwin
->w_winrow
> 0)
2505 got_click
= FALSE
; /* ignore mouse-up and drag events */
2507 /* click in a tab selects that tab page */
2512 && mouse_col
< Columns
)
2514 c1
= TabPageIdxs
[mouse_col
];
2517 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2519 /* double click opens new page */
2522 tabpage_move(c1
== 0 ? 9999 : c1
- 1);
2526 /* Go to specified tab page, or next one if not clicking
2530 /* It's like clicking on the status line of a window. */
2531 if (curwin
!= old_curwin
)
2539 /* Close the current or specified tab page. */
2543 tp
= find_tabpage(-c1
);
2546 if (first_tabpage
->tp_next
!= NULL
)
2547 tabpage_close(FALSE
);
2549 else if (tp
!= NULL
)
2550 tabpage_close_other(tp
, FALSE
);
2558 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2559 * right button up -> pop-up menu
2560 * shift-left button -> right button
2561 * alt-left button -> alt-right button
2563 if (mouse_model_popup())
2565 if (which_button
== MOUSE_RIGHT
2566 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2569 * NOTE: Ignore right button down and drag mouse events.
2570 * Windows only shows the popup menu on the button up event.
2572 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2573 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2577 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2578 if (is_click
|| is_drag
)
2581 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2582 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2583 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2587 if (STRCMP(p_mousem
, "popup_setpos") == 0)
2589 /* First set the cursor position before showing the popup
2597 * set MOUSE_MAY_STOP_VIS if we are outside the
2598 * selection or the current window (might have false
2601 if (mouse_row
< W_WINROW(curwin
)
2603 > (W_WINROW(curwin
) + curwin
->w_height
))
2604 jump_flags
= MOUSE_MAY_STOP_VIS
;
2605 else if (get_fpos_of_mouse(&m_pos
) != IN_BUFFER
)
2606 jump_flags
= MOUSE_MAY_STOP_VIS
;
2609 if ((lt(curwin
->w_cursor
, VIsual
)
2610 && (lt(m_pos
, curwin
->w_cursor
)
2611 || lt(VIsual
, m_pos
)))
2612 || (lt(VIsual
, curwin
->w_cursor
)
2613 && (lt(m_pos
, VIsual
)
2614 || lt(curwin
->w_cursor
, m_pos
))))
2616 jump_flags
= MOUSE_MAY_STOP_VIS
;
2618 else if (VIsual_mode
== Ctrl_V
)
2620 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
,
2621 &leftcol
, &rightcol
);
2622 getvcol(curwin
, &m_pos
, NULL
, &m_pos
.col
, NULL
);
2623 if (m_pos
.col
< leftcol
|| m_pos
.col
> rightcol
)
2624 jump_flags
= MOUSE_MAY_STOP_VIS
;
2629 jump_flags
= MOUSE_MAY_STOP_VIS
;
2634 jump_flags
= jump_to_mouse(jump_flags
, NULL
, which_button
);
2637 VIsual_active
? INVERTED
:
2641 out_flush(); /* Update before showing popup menu */
2644 gui_show_popupmenu();
2646 return (jump_flags
& CURSOR_MOVED
) != 0;
2654 if (which_button
== MOUSE_LEFT
2655 && (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
)))
2657 which_button
= MOUSE_RIGHT
;
2658 mod_mask
&= ~MOD_MASK_SHIFT
;
2663 if ((State
& (NORMAL
| INSERT
))
2664 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2666 if (which_button
== MOUSE_LEFT
)
2670 /* stop Visual mode for a left click in a window, but not when
2671 * on a status line */
2673 jump_flags
|= MOUSE_MAY_STOP_VIS
;
2675 else if (mouse_has(MOUSE_VISUAL
))
2676 jump_flags
|= MOUSE_MAY_VIS
;
2678 else if (which_button
== MOUSE_RIGHT
)
2680 if (is_click
&& VIsual_active
)
2683 * Remember the start and end of visual before moving the
2686 if (lt(curwin
->w_cursor
, VIsual
))
2688 start_visual
= curwin
->w_cursor
;
2689 end_visual
= VIsual
;
2693 start_visual
= VIsual
;
2694 end_visual
= curwin
->w_cursor
;
2697 jump_flags
|= MOUSE_FOCUS
;
2698 if (mouse_has(MOUSE_VISUAL
))
2699 jump_flags
|= MOUSE_MAY_VIS
;
2705 * If an operator is pending, ignore all drags and releases until the
2708 if (!is_drag
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2711 oap
->motion_type
= MCHAR
;
2714 /* When releasing the button let jump_to_mouse() know. */
2715 if (!is_click
&& !is_drag
)
2716 jump_flags
|= MOUSE_RELEASED
;
2721 jump_flags
= jump_to_mouse(jump_flags
,
2722 oap
== NULL
? NULL
: &(oap
->inclusive
), which_button
);
2723 moved
= (jump_flags
& CURSOR_MOVED
);
2724 in_status_line
= (jump_flags
& IN_STATUS_LINE
);
2725 #ifdef FEAT_VERTSPLIT
2726 in_sep_line
= (jump_flags
& IN_SEP_LINE
);
2729 #ifdef FEAT_NETBEANS_INTG
2730 if (usingNetbeans
&& isNetbeansBuffer(curbuf
)
2731 && !(jump_flags
& (IN_STATUS_LINE
| IN_SEP_LINE
)))
2733 int key
= KEY2TERMCAP1(c
);
2735 if (key
== (int)KE_LEFTRELEASE
|| key
== (int)KE_MIDDLERELEASE
2736 || key
== (int)KE_RIGHTRELEASE
)
2737 netbeans_button_release(which_button
);
2741 /* When jumping to another window, clear a pending operator. That's a bit
2742 * friendlier than beeping and not jumping to that window. */
2743 if (curwin
!= old_curwin
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2749 && (jump_flags
& (MOUSE_FOLD_CLOSE
| MOUSE_FOLD_OPEN
))
2750 && which_button
== MOUSE_LEFT
)
2752 /* open or close a fold at this line */
2753 if (jump_flags
& MOUSE_FOLD_OPEN
)
2754 openFold(curwin
->w_cursor
.lnum
, 1L);
2756 closeFold(curwin
->w_cursor
.lnum
, 1L);
2757 /* don't move the cursor if still in the same window */
2758 if (curwin
== old_curwin
)
2759 curwin
->w_cursor
= save_cursor
;
2763 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2764 if ((jump_flags
& IN_OTHER_WIN
) && !VIsual_active
&& clip_star
.available
)
2766 clip_modeless(which_button
, is_click
, is_drag
);
2772 /* Set global flag that we are extending the Visual area with mouse
2773 * dragging; temporarily minimize 'scrolloff'. */
2774 if (VIsual_active
&& is_drag
&& p_so
)
2776 /* In the very first line, allow scrolling one line */
2783 /* When dragging the mouse above the window, scroll down. */
2784 if (is_drag
&& mouse_row
< 0 && !in_status_line
)
2786 scroll_redraw(FALSE
, 1L);
2790 if (start_visual
.lnum
) /* right click in visual mode */
2792 /* When ALT is pressed make Visual mode blockwise. */
2793 if (mod_mask
& MOD_MASK_ALT
)
2794 VIsual_mode
= Ctrl_V
;
2797 * In Visual-block mode, divide the area in four, pick up the corner
2798 * that is in the quarter that the cursor is in.
2800 if (VIsual_mode
== Ctrl_V
)
2802 getvcols(curwin
, &start_visual
, &end_visual
, &leftcol
, &rightcol
);
2803 if (curwin
->w_curswant
> (leftcol
+ rightcol
) / 2)
2804 end_visual
.col
= leftcol
;
2806 end_visual
.col
= rightcol
;
2807 if (curwin
->w_cursor
.lnum
<
2808 (start_visual
.lnum
+ end_visual
.lnum
) / 2)
2809 end_visual
.lnum
= end_visual
.lnum
;
2811 end_visual
.lnum
= start_visual
.lnum
;
2813 /* move VIsual to the right column */
2814 start_visual
= curwin
->w_cursor
; /* save the cursor pos */
2815 curwin
->w_cursor
= end_visual
;
2816 coladvance(end_visual
.col
);
2817 VIsual
= curwin
->w_cursor
;
2818 curwin
->w_cursor
= start_visual
; /* restore the cursor */
2823 * If the click is before the start of visual, change the start.
2824 * If the click is after the end of visual, change the end. If
2825 * the click is inside the visual, change the closest side.
2827 if (lt(curwin
->w_cursor
, start_visual
))
2828 VIsual
= end_visual
;
2829 else if (lt(end_visual
, curwin
->w_cursor
))
2830 VIsual
= start_visual
;
2833 /* In the same line, compare column number */
2834 if (end_visual
.lnum
== start_visual
.lnum
)
2836 if (curwin
->w_cursor
.col
- start_visual
.col
>
2837 end_visual
.col
- curwin
->w_cursor
.col
)
2838 VIsual
= start_visual
;
2840 VIsual
= end_visual
;
2843 /* In different lines, compare line number */
2846 diff
= (curwin
->w_cursor
.lnum
- start_visual
.lnum
) -
2847 (end_visual
.lnum
- curwin
->w_cursor
.lnum
);
2849 if (diff
> 0) /* closest to end */
2850 VIsual
= start_visual
;
2851 else if (diff
< 0) /* closest to start */
2852 VIsual
= end_visual
;
2853 else /* in the middle line */
2855 if (curwin
->w_cursor
.col
<
2856 (start_visual
.col
+ end_visual
.col
) / 2)
2857 VIsual
= end_visual
;
2859 VIsual
= start_visual
;
2866 * If Visual mode started in insert mode, execute "CTRL-O"
2868 else if ((State
& INSERT
) && VIsual_active
)
2869 stuffcharReadbuff(Ctrl_O
);
2873 * Middle mouse click: Put text before cursor.
2875 if (which_button
== MOUSE_MIDDLE
)
2877 #ifdef FEAT_CLIPBOARD
2878 if (clip_star
.available
&& regname
== 0)
2881 if (yank_register_mline(regname
))
2883 if (mouse_past_bottom
)
2886 else if (mouse_past_eol
)
2891 c1
= (dir
== BACKWARD
) ? '[' : ']';
2896 c1
= (dir
== FORWARD
) ? 'p' : 'P';
2899 prep_redo(regname
, count
, NUL
, c1
, NUL
, c2
, NUL
);
2902 * Remember where the paste started, so in edit() Insstart can be set
2905 if (restart_edit
!= 0)
2906 where_paste_started
= curwin
->w_cursor
;
2907 do_put(regname
, dir
, count
, fixindent
| PUT_CURSEND
);
2910 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2912 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2913 * error under the mouse pointer.
2915 else if (((mod_mask
& MOD_MASK_CTRL
)
2916 || (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2917 && bt_quickfix(curbuf
))
2920 stuffcharReadbuff(Ctrl_O
);
2921 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
2922 stuffReadbuff((char_u
*)":.cc\n");
2923 else /* location list window */
2924 stuffReadbuff((char_u
*)":.ll\n");
2925 got_click
= FALSE
; /* ignore drag&release now */
2930 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2931 * under the mouse pointer.
2933 else if ((mod_mask
& MOD_MASK_CTRL
) || (curbuf
->b_help
2934 && (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
))
2937 stuffcharReadbuff(Ctrl_O
);
2938 stuffcharReadbuff(Ctrl_RSB
);
2939 got_click
= FALSE
; /* ignore drag&release now */
2943 * Shift-Mouse click searches for the next occurrence of the word under
2946 else if ((mod_mask
& MOD_MASK_SHIFT
))
2950 || (VIsual_active
&& VIsual_select
)
2953 stuffcharReadbuff(Ctrl_O
);
2954 if (which_button
== MOUSE_LEFT
)
2955 stuffcharReadbuff('*');
2956 else /* MOUSE_RIGHT */
2957 stuffcharReadbuff('#');
2960 /* Handle double clicks, unless on status line */
2961 else if (in_status_line
)
2963 #ifdef FEAT_MOUSESHAPE
2964 if ((is_drag
|| is_click
) && !drag_status_line
)
2966 drag_status_line
= TRUE
;
2967 update_mouseshape(-1);
2971 #ifdef FEAT_VERTSPLIT
2972 else if (in_sep_line
)
2974 # ifdef FEAT_MOUSESHAPE
2975 if ((is_drag
|| is_click
) && !drag_sep_line
)
2977 drag_sep_line
= TRUE
;
2978 update_mouseshape(-1);
2984 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) && (State
& (NORMAL
| INSERT
))
2985 && mouse_has(MOUSE_VISUAL
))
2987 if (is_click
|| !VIsual_active
)
2990 orig_cursor
= VIsual
;
2993 check_visual_highlight();
2994 VIsual
= curwin
->w_cursor
;
2995 orig_cursor
= VIsual
;
2996 VIsual_active
= TRUE
;
2997 VIsual_reselect
= TRUE
;
2998 /* start Select mode if 'selectmode' contains "mouse" */
2999 may_start_select('o');
3002 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3004 /* Double click with ALT pressed makes it blockwise. */
3005 if (mod_mask
& MOD_MASK_ALT
)
3006 VIsual_mode
= Ctrl_V
;
3010 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_3CLICK
)
3012 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_4CLICK
)
3013 VIsual_mode
= Ctrl_V
;
3014 #ifdef FEAT_CLIPBOARD
3015 /* Make sure the clipboard gets updated. Needed because start and
3016 * end may still be the same, and the selection needs to be owned */
3017 clip_star
.vmode
= NUL
;
3021 * A double click selects a word or a block.
3023 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3030 /* If the character under the cursor (skipping white space) is
3031 * not a word character, try finding a match and select a (),
3032 * {}, [], #if/#endif, etc. block. */
3033 end_visual
= curwin
->w_cursor
;
3034 while (gc
= gchar_pos(&end_visual
), vim_iswhite(gc
))
3037 oap
->motion_type
= MCHAR
;
3039 && VIsual_mode
== 'v'
3040 && !vim_iswordc(gchar_pos(&end_visual
))
3041 && equalpos(curwin
->w_cursor
, VIsual
)
3042 && (pos
= findmatch(oap
, NUL
)) != NULL
)
3044 curwin
->w_cursor
= *pos
;
3045 if (oap
->motion_type
== MLINE
)
3047 else if (*p_sel
== 'e')
3049 if (lt(curwin
->w_cursor
, VIsual
))
3052 ++curwin
->w_cursor
.col
;
3057 if (pos
== NULL
&& (is_click
|| is_drag
))
3059 /* When not found a match or when dragging: extend to include
3061 if (lt(curwin
->w_cursor
, orig_cursor
))
3063 find_start_of_word(&curwin
->w_cursor
);
3064 find_end_of_word(&VIsual
);
3068 find_start_of_word(&VIsual
);
3069 if (*p_sel
== 'e' && *ml_get_cursor() != NUL
)
3071 curwin
->w_cursor
.col
+=
3072 (*mb_ptr2len
)(ml_get_cursor());
3074 ++curwin
->w_cursor
.col
;
3076 find_end_of_word(&curwin
->w_cursor
);
3079 curwin
->w_set_curswant
= TRUE
;
3082 redraw_curbuf_later(INVERTED
); /* update the inversion */
3084 else if (VIsual_active
&& !old_active
)
3086 if (mod_mask
& MOD_MASK_ALT
)
3087 VIsual_mode
= Ctrl_V
;
3092 /* If Visual mode changed show it later. */
3093 if ((!VIsual_active
&& old_active
&& mode_displayed
)
3094 || (VIsual_active
&& p_smd
&& msg_silent
== 0
3095 && (!old_active
|| VIsual_mode
!= old_mode
)))
3096 redraw_cmdline
= TRUE
;
3104 * Move "pos" back to the start of the word it's in.
3107 find_start_of_word(pos
)
3114 line
= ml_get(pos
->lnum
);
3115 cclass
= get_mouse_class(line
+ pos
->col
);
3117 while (pos
->col
> 0)
3121 col
-= (*mb_head_off
)(line
, line
+ col
);
3123 if (get_mouse_class(line
+ col
) != cclass
)
3130 * Move "pos" forward to the end of the word it's in.
3131 * When 'selection' is "exclusive", the position is just after the word.
3134 find_end_of_word(pos
)
3141 line
= ml_get(pos
->lnum
);
3142 if (*p_sel
== 'e' && pos
->col
> 0)
3146 pos
->col
-= (*mb_head_off
)(line
, line
+ pos
->col
);
3149 cclass
= get_mouse_class(line
+ pos
->col
);
3150 while (line
[pos
->col
] != NUL
)
3153 col
= pos
->col
+ (*mb_ptr2len
)(line
+ pos
->col
);
3157 if (get_mouse_class(line
+ col
) != cclass
)
3168 * Get class of a character for selection: same class means same word.
3170 * 1: punctuation groups
3171 * 2: normal word character
3172 * >2: multi-byte word character.
3181 if (has_mbyte
&& MB_BYTE2LEN(p
[0]) > 1)
3182 return mb_get_class(p
);
3186 if (c
== ' ' || c
== '\t')
3193 * There are a few special cases where we want certain combinations of
3194 * characters to be considered as a single word. These are things like
3195 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3196 * character is in it's own class.
3198 if (c
!= NUL
&& vim_strchr((char_u
*)"-+*/%<>&|^!=", c
) != NULL
)
3202 #endif /* FEAT_VISUAL */
3203 #endif /* FEAT_MOUSE */
3205 #if defined(FEAT_VISUAL) || defined(PROTO)
3207 * Check if highlighting for visual mode is possible, give a warning message
3211 check_visual_highlight()
3213 static int did_check
= FALSE
;
3217 if (!did_check
&& hl_attr(HLF_V
) == 0)
3218 MSG(_("Warning: terminal cannot highlight"));
3225 * This function should ALWAYS be called to end Visual mode, except from
3226 * do_pending_operator().
3231 #ifdef FEAT_CLIPBOARD
3233 * If we are using the clipboard, then remember what was selected in case
3234 * we need to paste it somewhere while we still own the selection.
3235 * Only do this when the clipboard is already owned. Don't want to grab
3236 * the selection when hitting ESC.
3238 if (clip_star
.available
&& clip_star
.owned
)
3242 VIsual_active
= FALSE
;
3248 /* Save the current VIsual area for '< and '> marks, and "gv" */
3249 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
3250 curbuf
->b_visual
.vi_start
= VIsual
;
3251 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
3252 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
3254 curbuf
->b_visual_mode_eval
= VIsual_mode
;
3256 #ifdef FEAT_VIRTUALEDIT
3257 if (!virtual_active())
3258 curwin
->w_cursor
.coladd
= 0;
3262 clear_cmdline
= TRUE
; /* unshow visual mode later */
3263 #ifdef FEAT_CMDL_INFO
3268 adjust_cursor_eol();
3272 * Reset VIsual_active and VIsual_reselect.
3275 reset_VIsual_and_resel()
3280 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3282 VIsual_reselect
= FALSE
;
3286 * Reset VIsual_active and VIsual_reselect if it's set.
3294 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3295 VIsual_reselect
= FALSE
;
3298 #endif /* FEAT_VISUAL */
3300 #if defined(FEAT_BEVAL)
3301 static int find_is_eval_item
__ARGS((char_u
*ptr
, int *colp
, int *nbp
, int dir
));
3304 * Check for a balloon-eval special item to include when searching for an
3305 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3306 * Returns TRUE if the character at "*ptr" should be included.
3307 * "dir" is FORWARD or BACKWARD, the direction of searching.
3308 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3309 * "bnp" points to a counter for square brackets.
3312 find_is_eval_item(ptr
, colp
, bnp
, dir
)
3318 /* Accept everything inside []. */
3319 if ((*ptr
== ']' && dir
== BACKWARD
) || (*ptr
== '[' && dir
== FORWARD
))
3323 if ((*ptr
== '[' && dir
== BACKWARD
) || (*ptr
== ']' && dir
== FORWARD
))
3328 /* skip over "s.var" */
3332 /* two-character item: s->var */
3333 if (ptr
[dir
== BACKWARD
? 0 : 1] == '>'
3334 && ptr
[dir
== BACKWARD
? -1 : 0] == '-')
3344 * Find the identifier under or to the right of the cursor.
3345 * "find_type" can have one of three values:
3346 * FIND_IDENT: find an identifier (keyword)
3347 * FIND_STRING: find any non-white string
3348 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3349 * FIND_EVAL: find text useful for C program debugging
3351 * There are three steps:
3352 * 1. Search forward for the start of an identifier/string. Doesn't move if
3354 * 2. Search backward for the start of this identifier/string.
3355 * This doesn't match the real Vi but I like it a little better and it
3356 * shouldn't bother anyone.
3357 * 3. Search forward to the end of this identifier/string.
3358 * When FIND_IDENT isn't defined, we backup until a blank.
3360 * Returns the length of the string, or zero if no string is found.
3361 * If a string is found, a pointer to the string is put in "*string". This
3362 * string is not always NUL terminated.
3365 find_ident_under_cursor(string
, find_type
)
3369 return find_ident_at_pos(curwin
, curwin
->w_cursor
.lnum
,
3370 curwin
->w_cursor
.col
, string
, find_type
);
3374 * Like find_ident_under_cursor(), but for any window and any position.
3375 * However: Uses 'iskeyword' from the current window!.
3378 find_ident_at_pos(wp
, lnum
, startcol
, string
, find_type
)
3386 int col
= 0; /* init to shut up GCC */
3393 #if defined(FEAT_BEVAL)
3394 int bn
= 0; /* bracket nesting */
3398 * if i == 0: try to find an identifier
3399 * if i == 1: try to find any non-white string
3401 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
3402 for (i
= (find_type
& FIND_IDENT
) ? 0 : 1; i
< 2; ++i
)
3405 * 1. skip to start of identifier/string
3411 while (ptr
[col
] != NUL
)
3413 # if defined(FEAT_BEVAL)
3414 /* Stop at a ']' to evaluate "a[x]". */
3415 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3418 this_class
= mb_get_class(ptr
+ col
);
3419 if (this_class
!= 0 && (i
== 1 || this_class
!= 1))
3421 col
+= (*mb_ptr2len
)(ptr
+ col
);
3426 while (ptr
[col
] != NUL
3427 && (i
== 0 ? !vim_iswordc(ptr
[col
]) : vim_iswhite(ptr
[col
]))
3428 # if defined(FEAT_BEVAL)
3429 && (!(find_type
& FIND_EVAL
) || ptr
[col
] != ']')
3434 #if defined(FEAT_BEVAL)
3435 /* When starting on a ']' count it, so that we include the '['. */
3436 bn
= ptr
[col
] == ']';
3440 * 2. Back up to start of identifier/string.
3445 /* Remember class of character under cursor. */
3446 # if defined(FEAT_BEVAL)
3447 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3448 this_class
= mb_get_class((char_u
*)"a");
3451 this_class
= mb_get_class(ptr
+ col
);
3452 while (col
> 0 && this_class
!= 0)
3454 prevcol
= col
- 1 - (*mb_head_off
)(ptr
, ptr
+ col
- 1);
3455 prev_class
= mb_get_class(ptr
+ prevcol
);
3456 if (this_class
!= prev_class
3459 || (find_type
& FIND_IDENT
))
3460 # if defined(FEAT_BEVAL)
3461 && (!(find_type
& FIND_EVAL
)
3463 || !find_is_eval_item(ptr
+ prevcol
, &prevcol
,
3471 /* If we don't want just any old string, or we've found an
3472 * identifier, stop searching. */
3475 if (!(find_type
& FIND_STRING
) || this_class
== 2)
3483 ? vim_iswordc(ptr
[col
- 1])
3484 : (!vim_iswhite(ptr
[col
- 1])
3485 && (!(find_type
& FIND_IDENT
)
3486 || !vim_iswordc(ptr
[col
- 1]))))
3487 #if defined(FEAT_BEVAL)
3488 || ((find_type
& FIND_EVAL
)
3490 && find_is_eval_item(ptr
+ col
- 1, &col
,
3496 /* If we don't want just any old string, or we've found an
3497 * identifier, stop searching. */
3498 if (!(find_type
& FIND_STRING
) || vim_iswordc(ptr
[col
]))
3503 if (ptr
[col
] == NUL
|| (i
== 0 && (
3505 has_mbyte
? this_class
!= 2 :
3507 !vim_iswordc(ptr
[col
]))))
3510 * didn't find an identifier or string
3512 if (find_type
& FIND_STRING
)
3513 EMSG(_("E348: No string under cursor"));
3522 * 3. Find the end if the identifier/string.
3524 #if defined(FEAT_BEVAL)
3532 /* Search for point of changing multibyte character class. */
3533 this_class
= mb_get_class(ptr
);
3534 while (ptr
[col
] != NUL
3535 && ((i
== 0 ? mb_get_class(ptr
+ col
) == this_class
3536 : mb_get_class(ptr
+ col
) != 0)
3537 # if defined(FEAT_BEVAL)
3538 || ((find_type
& FIND_EVAL
)
3539 && col
<= (int)startcol
3540 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3543 col
+= (*mb_ptr2len
)(ptr
+ col
);
3547 while ((i
== 0 ? vim_iswordc(ptr
[col
])
3548 : (ptr
[col
] != NUL
&& !vim_iswhite(ptr
[col
])))
3549 # if defined(FEAT_BEVAL)
3550 || ((find_type
& FIND_EVAL
)
3551 && col
<= (int)startcol
3552 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3563 * Prepare for redo of a normal command.
3569 prep_redo(cap
->oap
->regname
, cap
->count0
,
3570 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
3574 * Prepare for redo of any command.
3575 * Note that only the last argument can be a multi-byte char.
3578 prep_redo(regname
, num
, cmd1
, cmd2
, cmd3
, cmd4
, cmd5
)
3588 if (regname
!= 0) /* yank from specified buffer */
3590 AppendCharToRedobuff('"');
3591 AppendCharToRedobuff(regname
);
3594 AppendNumberToRedobuff(num
);
3597 AppendCharToRedobuff(cmd1
);
3599 AppendCharToRedobuff(cmd2
);
3601 AppendCharToRedobuff(cmd3
);
3603 AppendCharToRedobuff(cmd4
);
3605 AppendCharToRedobuff(cmd5
);
3609 * check for operator active and clear it
3611 * return TRUE if operator was active
3617 if (oap
->op_type
== OP_NOP
)
3624 * Check for operator or Visual active. Clear active operator.
3626 * Return TRUE if operator or Visual was active.
3632 if (oap
->op_type
== OP_NOP
3646 oap
->op_type
= OP_NOP
;
3648 oap
->motion_force
= NUL
;
3649 oap
->use_reg_one
= FALSE
;
3662 * Remove the shift modifier from a special key.
3665 unshift_special(cap
)
3668 switch (cap
->cmdchar
)
3670 case K_S_RIGHT
: cap
->cmdchar
= K_RIGHT
; break;
3671 case K_S_LEFT
: cap
->cmdchar
= K_LEFT
; break;
3672 case K_S_UP
: cap
->cmdchar
= K_UP
; break;
3673 case K_S_DOWN
: cap
->cmdchar
= K_DOWN
; break;
3674 case K_S_HOME
: cap
->cmdchar
= K_HOME
; break;
3675 case K_S_END
: cap
->cmdchar
= K_END
; break;
3677 cap
->cmdchar
= simplify_key(cap
->cmdchar
, &mod_mask
);
3681 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3683 * Routines for displaying a partly typed command
3686 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3687 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3689 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3691 static char_u showcmd_buf
[SHOWCMD_BUFLEN
];
3692 static char_u old_showcmd_buf
[SHOWCMD_BUFLEN
]; /* For push_showcmd() */
3693 static int showcmd_is_clear
= TRUE
;
3694 static int showcmd_visual
= FALSE
;
3696 static void display_showcmd
__ARGS((void));
3705 if (VIsual_active
&& !char_avail())
3707 int i
= lt(VIsual
, curwin
->w_cursor
);
3709 colnr_T leftcol
, rightcol
;
3712 /* Show the size of the Visual area. */
3716 bot
= curwin
->w_cursor
.lnum
;
3720 top
= curwin
->w_cursor
.lnum
;
3723 # ifdef FEAT_FOLDING
3724 /* Include closed folds as a whole. */
3725 hasFolding(top
, &top
, NULL
);
3726 hasFolding(bot
, NULL
, &bot
);
3728 lines
= bot
- top
+ 1;
3730 if (VIsual_mode
== Ctrl_V
)
3732 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
, &leftcol
, &rightcol
);
3733 sprintf((char *)showcmd_buf
, "%ldx%ld", lines
,
3734 (long)(rightcol
- leftcol
+ 1));
3736 else if (VIsual_mode
== 'V' || VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
3737 sprintf((char *)showcmd_buf
, "%ld", lines
);
3739 sprintf((char *)showcmd_buf
, "%ld", (long)(i
3740 ? curwin
->w_cursor
.col
- VIsual
.col
3741 : VIsual
.col
- curwin
->w_cursor
.col
) + (*p_sel
!= 'e'));
3742 showcmd_buf
[SHOWCMD_COLS
] = NUL
; /* truncate */
3743 showcmd_visual
= TRUE
;
3748 showcmd_buf
[0] = NUL
;
3749 showcmd_visual
= FALSE
;
3751 /* Don't actually display something if there is nothing to clear. */
3752 if (showcmd_is_clear
)
3760 * Add 'c' to string of shown command chars.
3761 * Return TRUE if output has been written (and setcursor() has been called).
3771 #if defined(FEAT_MOUSE)
3773 static int ignore
[] =
3776 K_VER_SCROLLBAR
, K_HOR_SCROLLBAR
,
3777 K_LEFTMOUSE_NM
, K_LEFTRELEASE_NM
,
3780 K_LEFTMOUSE
, K_LEFTDRAG
, K_LEFTRELEASE
,
3781 K_MIDDLEMOUSE
, K_MIDDLEDRAG
, K_MIDDLERELEASE
,
3782 K_RIGHTMOUSE
, K_RIGHTDRAG
, K_RIGHTRELEASE
,
3783 K_MOUSEDOWN
, K_MOUSEUP
,
3784 K_X1MOUSE
, K_X1DRAG
, K_X1RELEASE
, K_X2MOUSE
, K_X2DRAG
, K_X2RELEASE
,
3790 if (!p_sc
|| msg_silent
!= 0)
3795 showcmd_buf
[0] = NUL
;
3796 showcmd_visual
= FALSE
;
3799 #if defined(FEAT_MOUSE)
3800 /* Ignore keys that are scrollbar updates and mouse clicks */
3802 for (i
= 0; ignore
[i
] != 0; ++i
)
3808 old_len
= (int)STRLEN(showcmd_buf
);
3809 extra_len
= (int)STRLEN(p
);
3810 overflow
= old_len
+ extra_len
- SHOWCMD_COLS
;
3812 mch_memmove(showcmd_buf
, showcmd_buf
+ overflow
,
3813 old_len
- overflow
+ 1);
3814 STRCAT(showcmd_buf
, p
);
3828 if (!add_to_showcmd(c
))
3833 * Delete 'len' characters from the end of the shown command.
3836 del_from_showcmd(len
)
3844 old_len
= (int)STRLEN(showcmd_buf
);
3847 showcmd_buf
[old_len
- len
] = NUL
;
3854 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3855 * something and there is a partial mapping.
3861 STRCPY(old_showcmd_buf
, showcmd_buf
);
3870 STRCPY(showcmd_buf
, old_showcmd_buf
);
3882 len
= (int)STRLEN(showcmd_buf
);
3884 showcmd_is_clear
= TRUE
;
3887 screen_puts(showcmd_buf
, (int)Rows
- 1, sc_col
, 0);
3888 showcmd_is_clear
= FALSE
;
3892 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3895 screen_puts((char_u
*)" " + len
, (int)Rows
- 1, sc_col
+ len
, 0);
3897 setcursor(); /* put cursor back where it belongs */
3901 #ifdef FEAT_SCROLLBIND
3903 * When "check" is FALSE, prepare for commands that scroll the window.
3904 * When "check" is TRUE, take care of scroll-binding after the window has
3905 * scrolled. Called from normal_cmd() and edit().
3908 do_check_scrollbind(check
)
3911 static win_T
*old_curwin
= NULL
;
3912 static linenr_T old_topline
= 0;
3914 static int old_topfill
= 0;
3916 static buf_T
*old_buf
= NULL
;
3917 static colnr_T old_leftcol
= 0;
3919 if (check
&& curwin
->w_p_scb
)
3921 /* If a ":syncbind" command was just used, don't scroll, only reset
3924 did_syncbind
= FALSE
;
3925 else if (curwin
== old_curwin
)
3928 * Synchronize other windows, as necessary according to
3929 * 'scrollbind'. Don't do this after an ":edit" command, except
3930 * when 'diff' is set.
3932 if ((curwin
->w_buffer
== old_buf
3937 && (curwin
->w_topline
!= old_topline
3939 || curwin
->w_topfill
!= old_topfill
3941 || curwin
->w_leftcol
!= old_leftcol
))
3943 check_scrollbind(curwin
->w_topline
- old_topline
,
3944 (long)(curwin
->w_leftcol
- old_leftcol
));
3947 else if (vim_strchr(p_sbo
, 'j')) /* jump flag set in 'scrollopt' */
3950 * When switching between windows, make sure that the relative
3951 * vertical offset is valid for the new window. The relative
3952 * offset is invalid whenever another 'scrollbind' window has
3953 * scrolled to a point that would force the current window to
3954 * scroll past the beginning or end of its buffer. When the
3955 * resync is performed, some of the other 'scrollbind' windows may
3956 * need to jump so that the current window's relative position is
3957 * visible on-screen.
3959 check_scrollbind(curwin
->w_topline
- curwin
->w_scbind_pos
, 0L);
3961 curwin
->w_scbind_pos
= curwin
->w_topline
;
3964 old_curwin
= curwin
;
3965 old_topline
= curwin
->w_topline
;
3967 old_topfill
= curwin
->w_topfill
;
3969 old_buf
= curwin
->w_buffer
;
3970 old_leftcol
= curwin
->w_leftcol
;
3974 * Synchronize any windows that have "scrollbind" set, based on the
3975 * number of rows by which the current window has changed
3976 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3979 check_scrollbind(topline_diff
, leftcol_diff
)
3980 linenr_T topline_diff
;
3985 win_T
*old_curwin
= curwin
;
3986 buf_T
*old_curbuf
= curbuf
;
3988 int old_VIsual_select
= VIsual_select
;
3989 int old_VIsual_active
= VIsual_active
;
3991 colnr_T tgt_leftcol
= curwin
->w_leftcol
;
3996 * check 'scrollopt' string for vertical and horizontal scroll options
3998 want_ver
= (vim_strchr(p_sbo
, 'v') && topline_diff
!= 0);
4000 want_ver
|= old_curwin
->w_p_diff
;
4002 want_hor
= (vim_strchr(p_sbo
, 'h') && (leftcol_diff
|| topline_diff
!= 0));
4005 * loop through the scrollbound windows and scroll accordingly
4008 VIsual_select
= VIsual_active
= 0;
4010 for (curwin
= firstwin
; curwin
; curwin
= curwin
->w_next
)
4012 curbuf
= curwin
->w_buffer
;
4013 /* skip original window and windows with 'noscrollbind' */
4014 if (curwin
!= old_curwin
&& curwin
->w_p_scb
)
4017 * do the vertical scroll
4022 if (old_curwin
->w_p_diff
&& curwin
->w_p_diff
)
4024 diff_set_topline(old_curwin
, curwin
);
4029 curwin
->w_scbind_pos
+= topline_diff
;
4030 topline
= curwin
->w_scbind_pos
;
4031 if (topline
> curbuf
->b_ml
.ml_line_count
)
4032 topline
= curbuf
->b_ml
.ml_line_count
;
4036 y
= topline
- curwin
->w_topline
;
4040 scrolldown(-y
, FALSE
);
4043 redraw_later(VALID
);
4046 curwin
->w_redr_status
= TRUE
;
4051 * do the horizontal scroll
4053 if (want_hor
&& curwin
->w_leftcol
!= tgt_leftcol
)
4055 curwin
->w_leftcol
= tgt_leftcol
;
4062 * reset current-window
4065 VIsual_select
= old_VIsual_select
;
4066 VIsual_active
= old_VIsual_active
;
4068 curwin
= old_curwin
;
4069 curbuf
= old_curbuf
;
4071 #endif /* #ifdef FEAT_SCROLLBIND */
4074 * Command character that's ignored.
4075 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4082 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */
4086 * Command character that doesn't do anything, but unlike nv_ignore() does
4087 * start edit(). Used for "startinsert" executed while starting up.
4097 * Command character doesn't exist.
4103 clearopbeep(cap
->oap
);
4107 * <Help> and <F1> commands.
4113 if (!checkclearopq(cap
->oap
))
4118 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4124 if (!checkclearopq(cap
->oap
)
4125 && do_addsub((int)cap
->cmdchar
, cap
->count1
) == OK
)
4130 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4136 if (!checkclearop(cap
->oap
))
4139 if (mod_mask
& MOD_MASK_CTRL
)
4141 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4142 if (cap
->arg
== BACKWARD
)
4143 goto_tabpage(-(int)cap
->count1
);
4145 goto_tabpage((int)cap
->count0
);
4149 (void)onepage(cap
->arg
, cap
->count1
);
4154 * Implementation of "gd" and "gD" command.
4157 nv_gd(oap
, nchar
, thisblock
)
4160 int thisblock
; /* 1 for "1gd" and "1gD" */
4165 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0
4166 || find_decl(ptr
, len
, nchar
== 'd', thisblock
, 0) == FAIL
)
4169 else if ((fdo_flags
& FDO_SEARCH
) && KeyTyped
&& oap
->op_type
== OP_NOP
)
4175 * Search for variable declaration of "ptr[len]".
4176 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4177 * current file ("gD").
4178 * When "thisblock" is TRUE check the {} block scope.
4179 * Return FAIL when not found.
4182 find_decl(ptr
, len
, locally
, thisblock
, searchflags
)
4187 int searchflags
; /* flags passed to searchit() */
4199 if ((pat
= alloc(len
+ 7)) == NULL
)
4202 /* Put "\V" before the pattern to avoid that the special meaning of "."
4203 * and "~" causes trouble. */
4204 sprintf((char *)pat
, vim_iswordp(ptr
) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4206 old_pos
= curwin
->w_cursor
;
4209 p_ws
= FALSE
; /* don't wrap around end of file now */
4210 p_scs
= FALSE
; /* don't switch ignorecase off now */
4213 * With "gD" go to line 1.
4214 * With "gd" Search back for the start of the current function, then go
4215 * back until a blank line. If this fails go to line 1.
4217 if (!locally
|| !findpar(&incll
, BACKWARD
, 1L, '{', FALSE
))
4219 setpcmark(); /* Set in findpar() otherwise */
4220 curwin
->w_cursor
.lnum
= 1;
4221 par_pos
= curwin
->w_cursor
;
4225 par_pos
= curwin
->w_cursor
;
4226 while (curwin
->w_cursor
.lnum
> 1 && *skipwhite(ml_get_curline()) != NUL
)
4227 --curwin
->w_cursor
.lnum
;
4229 curwin
->w_cursor
.col
= 0;
4231 /* Search forward for the identifier, ignore comment lines. */
4232 clearpos(&found_pos
);
4235 t
= searchit(curwin
, curbuf
, &curwin
->w_cursor
, FORWARD
,
4236 pat
, 1L, searchflags
, RE_LAST
, (linenr_T
)0, NULL
);
4237 if (curwin
->w_cursor
.lnum
>= old_pos
.lnum
)
4238 t
= FAIL
; /* match after start is failure too */
4240 if (thisblock
&& t
!= FAIL
)
4244 /* Check that the block the match is in doesn't end before the
4245 * position where we started the search from. */
4246 if ((pos
= findmatchlimit(NULL
, '}', FM_FORWARD
,
4247 (int)(old_pos
.lnum
- curwin
->w_cursor
.lnum
+ 1))) != NULL
4248 && pos
->lnum
< old_pos
.lnum
)
4254 /* If we previously found a valid position, use it. */
4255 if (found_pos
.lnum
!= 0)
4257 curwin
->w_cursor
= found_pos
;
4262 #ifdef FEAT_COMMENTS
4263 if (get_leader_len(ml_get_curline(), NULL
, FALSE
) > 0)
4265 /* Ignore this line, continue at start of next line. */
4266 ++curwin
->w_cursor
.lnum
;
4267 curwin
->w_cursor
.col
= 0;
4271 if (!locally
) /* global search: use first match found */
4273 if (curwin
->w_cursor
.lnum
>= par_pos
.lnum
)
4275 /* If we previously found a valid position, use it. */
4276 if (found_pos
.lnum
!= 0)
4277 curwin
->w_cursor
= found_pos
;
4281 /* For finding a local variable and the match is before the "{" search
4282 * to find a later match. For K&R style function declarations this
4283 * skips the function header without types. */
4284 found_pos
= curwin
->w_cursor
;
4290 curwin
->w_cursor
= old_pos
;
4294 curwin
->w_set_curswant
= TRUE
;
4295 /* "n" searches forward now */
4307 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4308 * lines rather than lines in the file.
4309 * 'dist' must be positive.
4311 * Return OK if able to move cursor, FAIL otherwise.
4314 nv_screengo(oap
, dir
, dist
)
4319 int linelen
= linetabsize(ml_get_curline());
4323 int col_off1
; /* margin offset for first screen line */
4324 int col_off2
; /* margin offset for wrapped screen line */
4325 int width1
; /* text width for first screen line */
4326 int width2
; /* test width for wrapped screen line */
4328 oap
->motion_type
= MCHAR
;
4329 oap
->inclusive
= FALSE
;
4331 col_off1
= curwin_col_off();
4332 col_off2
= col_off1
- curwin_col_off2();
4333 width1
= W_WIDTH(curwin
) - col_off1
;
4334 width2
= W_WIDTH(curwin
) - col_off2
;
4336 #ifdef FEAT_VERTSPLIT
4337 if (curwin
->w_width
!= 0)
4341 * Instead of sticking at the last character of the buffer line we
4342 * try to stick in the last column of the screen.
4344 if (curwin
->w_curswant
== MAXCOL
)
4349 curwin
->w_curswant
= 0;
4352 curwin
->w_curswant
= width1
- 1;
4353 if (curwin
->w_virtcol
> curwin
->w_curswant
)
4354 curwin
->w_curswant
+= ((curwin
->w_virtcol
4355 - curwin
->w_curswant
- 1) / width2
+ 1) * width2
;
4360 if (linelen
> width1
)
4361 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4364 if (curwin
->w_curswant
> (colnr_T
)n
+ 1)
4365 curwin
->w_curswant
-= ((curwin
->w_curswant
- n
) / width2
+ 1)
4371 if (dir
== BACKWARD
)
4373 if ((long)curwin
->w_curswant
>= width2
)
4374 /* move back within line */
4375 curwin
->w_curswant
-= width2
;
4378 /* to previous line */
4379 if (curwin
->w_cursor
.lnum
== 1)
4384 --curwin
->w_cursor
.lnum
;
4386 /* Move to the start of a closed fold. Don't do that when
4387 * 'foldopen' contains "all": it will open in a moment. */
4388 if (!(fdo_flags
& FDO_ALL
))
4389 (void)hasFolding(curwin
->w_cursor
.lnum
,
4390 &curwin
->w_cursor
.lnum
, NULL
);
4392 linelen
= linetabsize(ml_get_curline());
4393 if (linelen
> width1
)
4394 curwin
->w_curswant
+= (((linelen
- width1
- 1) / width2
)
4398 else /* dir == FORWARD */
4400 if (linelen
> width1
)
4401 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4404 if (curwin
->w_curswant
+ width2
< (colnr_T
)n
)
4405 /* move forward within line */
4406 curwin
->w_curswant
+= width2
;
4411 /* Move to the end of a closed fold. */
4412 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
4413 &curwin
->w_cursor
.lnum
);
4415 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4420 curwin
->w_cursor
.lnum
++;
4421 curwin
->w_curswant
%= width2
;
4425 #ifdef FEAT_VERTSPLIT
4429 coladvance(curwin
->w_curswant
);
4431 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4432 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
4435 * Check for landing on a character that got split at the end of the
4436 * last line. We want to advance a screenline, not end up in the same
4437 * screenline or move two screenlines.
4440 if (curwin
->w_virtcol
> curwin
->w_curswant
4441 && (curwin
->w_curswant
< (colnr_T
)width1
4442 ? (curwin
->w_curswant
> (colnr_T
)width1
/ 2)
4443 : ((curwin
->w_curswant
- width1
) % width2
4444 > (colnr_T
)width2
/ 2)))
4445 --curwin
->w_cursor
.col
;
4450 curwin
->w_curswant
= MAXCOL
; /* stick in the last column */
4457 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4458 * when Shift or Ctrl is used.
4459 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4465 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4466 win_T
*old_curwin
= curwin
;
4468 /* Currently we only get the mouse coordinates in the GUI. */
4469 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
4476 /* find the window at the pointer coordinates */
4477 curwin
= mouse_find_win(&row
, &col
);
4478 curbuf
= curwin
->w_buffer
;
4482 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
4484 (void)onepage(cap
->arg
? FORWARD
: BACKWARD
, 1L);
4490 nv_scroll_line(cap
);
4493 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4494 curwin
->w_redr_status
= TRUE
;
4496 curwin
= old_curwin
;
4497 curbuf
= curwin
->w_buffer
;
4502 * Mouse clicks and drags.
4508 (void)do_mouse(cap
->oap
, cap
->cmdchar
, BACKWARD
, cap
->count1
, 0);
4513 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4514 * cap->arg must be TRUE for CTRL-E.
4520 if (!checkclearop(cap
->oap
))
4521 scroll_redraw(cap
->arg
, cap
->count1
);
4525 * Scroll "count" lines up or down, and redraw.
4528 scroll_redraw(up
, count
)
4532 linenr_T prev_topline
= curwin
->w_topline
;
4534 int prev_topfill
= curwin
->w_topfill
;
4536 linenr_T prev_lnum
= curwin
->w_cursor
.lnum
;
4539 scrollup(count
, TRUE
);
4541 scrolldown(count
, TRUE
);
4544 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4545 * valid, otherwise the screen jumps back at the end of the file. */
4547 check_cursor_moved(curwin
);
4548 curwin
->w_valid
|= VALID_TOPLINE
;
4550 /* If moved back to where we were, at least move the cursor, otherwise
4551 * we get stuck at one position. Don't move the cursor up if the
4552 * first line of the buffer is already on the screen */
4553 while (curwin
->w_topline
== prev_topline
4555 && curwin
->w_topfill
== prev_topfill
4561 if (curwin
->w_cursor
.lnum
> prev_lnum
4562 || cursor_down(1L, FALSE
) == FAIL
)
4567 if (curwin
->w_cursor
.lnum
< prev_lnum
4568 || prev_topline
== 1L
4569 || cursor_up(1L, FALSE
) == FAIL
)
4572 /* Mark w_topline as valid, otherwise the screen jumps back at the
4573 * end of the file. */
4574 check_cursor_moved(curwin
);
4575 curwin
->w_valid
|= VALID_TOPLINE
;
4578 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
4579 coladvance(curwin
->w_curswant
);
4580 redraw_later(VALID
);
4584 * Commands that start with "z".
4592 int nchar
= cap
->nchar
;
4594 long old_fdl
= curwin
->w_p_fdl
;
4595 int old_fen
= curwin
->w_p_fen
;
4601 if (VIM_ISDIGIT(nchar
))
4604 * "z123{nchar}": edit the count before obtaining {nchar}
4606 if (checkclearop(cap
->oap
))
4611 #ifdef USE_ON_FLY_SCROLL
4612 dont_scroll
= TRUE
; /* disallow scrolling here */
4615 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4616 nchar
= plain_vgetc();
4618 LANGMAP_ADJUST(nchar
, TRUE
);
4622 #ifdef FEAT_CMDL_INFO
4623 (void)add_to_showcmd(nchar
);
4625 if (nchar
== K_DEL
|| nchar
== K_KDEL
)
4627 else if (VIM_ISDIGIT(nchar
))
4628 n
= n
* 10 + (nchar
- '0');
4629 else if (nchar
== CAR
)
4632 need_mouse_correct
= TRUE
;
4634 win_setheight((int)n
);
4637 else if (nchar
== 'l'
4640 || nchar
== K_RIGHT
)
4642 cap
->count1
= n
? n
* cap
->count1
: cap
->count1
;
4647 clearopbeep(cap
->oap
);
4651 cap
->oap
->op_type
= OP_NOP
;
4658 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4659 * and "zC" only in Visual mode. "zj" and "zk" are motion
4661 cap
->nchar
!= 'f' && cap
->nchar
!= 'F'
4662 && !(VIsual_active
&& vim_strchr((char_u
*)"dcCoO", cap
->nchar
))
4663 && cap
->nchar
!= 'j' && cap
->nchar
!= 'k'
4666 checkclearop(cap
->oap
))
4670 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4671 * If line number given, set cursor.
4673 if ((vim_strchr((char_u
*)"+\r\nt.z^-b", nchar
) != NULL
)
4675 && cap
->count0
!= curwin
->w_cursor
.lnum
)
4678 if (cap
->count0
> curbuf
->b_ml
.ml_line_count
)
4679 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4681 curwin
->w_cursor
.lnum
= cap
->count0
;
4687 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4689 if (cap
->count0
== 0)
4691 /* No count given: put cursor at the line below screen */
4692 validate_botline(); /* make sure w_botline is valid */
4693 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
4694 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4696 curwin
->w_cursor
.lnum
= curwin
->w_botline
;
4702 beginline(BL_WHITE
| BL_FIX
);
4705 case 't': scroll_cursor_top(0, TRUE
);
4706 redraw_later(VALID
);
4709 /* "z." and "zz": put cursor in middle of screen */
4710 case '.': beginline(BL_WHITE
| BL_FIX
);
4713 case 'z': scroll_cursor_halfway(TRUE
);
4714 redraw_later(VALID
);
4717 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4718 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4719 * when <count> is at bottom of window, and puts that one at
4720 * bottom of window. */
4721 if (cap
->count0
!= 0)
4723 scroll_cursor_bot(0, TRUE
);
4724 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
4726 else if (curwin
->w_topline
== 1)
4727 curwin
->w_cursor
.lnum
= 1;
4729 curwin
->w_cursor
.lnum
= curwin
->w_topline
- 1;
4732 beginline(BL_WHITE
| BL_FIX
);
4735 case 'b': scroll_cursor_bot(0, TRUE
);
4736 redraw_later(VALID
);
4739 /* "zH" - scroll screen right half-page */
4741 cap
->count1
*= W_WIDTH(curwin
) / 2;
4744 /* "zh" - scroll screen to the right */
4747 if (!curwin
->w_p_wrap
)
4749 if ((colnr_T
)cap
->count1
> curwin
->w_leftcol
)
4750 curwin
->w_leftcol
= 0;
4752 curwin
->w_leftcol
-= (colnr_T
)cap
->count1
;
4757 /* "zL" - scroll screen left half-page */
4758 case 'L': cap
->count1
*= W_WIDTH(curwin
) / 2;
4761 /* "zl" - scroll screen to the left */
4764 if (!curwin
->w_p_wrap
)
4766 /* scroll the window left */
4767 curwin
->w_leftcol
+= (colnr_T
)cap
->count1
;
4772 /* "zs" - scroll screen, cursor at the start */
4773 case 's': if (!curwin
->w_p_wrap
)
4776 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4777 col
= 0; /* like the cursor is in col 0 */
4780 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, NULL
);
4781 if ((long)col
> p_siso
)
4785 if (curwin
->w_leftcol
!= col
)
4787 curwin
->w_leftcol
= col
;
4788 redraw_later(NOT_VALID
);
4793 /* "ze" - scroll screen, cursor at the end */
4794 case 'e': if (!curwin
->w_p_wrap
)
4797 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4798 col
= 0; /* like the cursor is in col 0 */
4801 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
4802 n
= W_WIDTH(curwin
) - curwin_col_off();
4803 if ((long)col
+ p_siso
< n
)
4806 col
= col
+ p_siso
- n
+ 1;
4807 if (curwin
->w_leftcol
!= col
)
4809 curwin
->w_leftcol
= col
;
4810 redraw_later(NOT_VALID
);
4816 /* "zF": create fold command */
4817 /* "zf": create fold operator */
4819 case 'f': if (foldManualAllowed(TRUE
))
4823 curwin
->w_p_fen
= TRUE
;
4825 /* "zF" is like "zfzf" */
4826 if (nchar
== 'F' && cap
->oap
->op_type
== OP_FOLD
)
4833 clearopbeep(cap
->oap
);
4836 /* "zd": delete fold at cursor */
4837 /* "zD": delete fold at cursor recursively */
4839 case 'D': if (foldManualAllowed(FALSE
))
4844 deleteFold(curwin
->w_cursor
.lnum
,
4845 curwin
->w_cursor
.lnum
, nchar
== 'D', FALSE
);
4849 /* "zE": erease all folds */
4850 case 'E': if (foldmethodIsManual(curwin
))
4852 clearFolding(curwin
);
4853 changed_window_setting();
4855 else if (foldmethodIsMarker(curwin
))
4856 deleteFold((linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
4859 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4862 /* "zn": fold none: reset 'foldenable' */
4863 case 'n': curwin
->w_p_fen
= FALSE
;
4866 /* "zN": fold Normal: set 'foldenable' */
4867 case 'N': curwin
->w_p_fen
= TRUE
;
4870 /* "zi": invert folding: toggle 'foldenable' */
4871 case 'i': curwin
->w_p_fen
= !curwin
->w_p_fen
;
4874 /* "za": open closed fold or close open fold at cursor */
4875 case 'a': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4876 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4879 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4880 curwin
->w_p_fen
= TRUE
;
4884 /* "zA": open fold at cursor recursively */
4885 case 'A': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4886 openFoldRecurse(curwin
->w_cursor
.lnum
);
4889 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4890 curwin
->w_p_fen
= TRUE
;
4894 /* "zo": open fold at cursor or Visual area */
4895 case 'o': if (VIsual_active
)
4898 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4901 /* "zO": open fold recursively */
4902 case 'O': if (VIsual_active
)
4905 openFoldRecurse(curwin
->w_cursor
.lnum
);
4908 /* "zc": close fold at cursor or Visual area */
4909 case 'c': if (VIsual_active
)
4912 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4913 curwin
->w_p_fen
= TRUE
;
4916 /* "zC": close fold recursively */
4917 case 'C': if (VIsual_active
)
4920 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4921 curwin
->w_p_fen
= TRUE
;
4924 /* "zv": open folds at the cursor */
4925 case 'v': foldOpenCursor();
4928 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4929 case 'x': curwin
->w_p_fen
= TRUE
;
4930 newFoldLevel(); /* update right now */
4934 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4935 case 'X': curwin
->w_p_fen
= TRUE
;
4936 old_fdl
= -1; /* force an update */
4939 /* "zm": fold more */
4940 case 'm': if (curwin
->w_p_fdl
> 0)
4942 old_fdl
= -1; /* force an update */
4943 curwin
->w_p_fen
= TRUE
;
4946 /* "zM": close all folds */
4947 case 'M': curwin
->w_p_fdl
= 0;
4948 old_fdl
= -1; /* force an update */
4949 curwin
->w_p_fen
= TRUE
;
4952 /* "zr": reduce folding */
4953 case 'r': ++curwin
->w_p_fdl
;
4956 /* "zR": open all folds */
4957 case 'R': curwin
->w_p_fdl
= getDeepestNesting();
4958 old_fdl
= -1; /* force an update */
4961 case 'j': /* "zj" move to next fold downwards */
4962 case 'k': /* "zk" move to next fold upwards */
4963 if (foldMoveTo(TRUE
, nchar
== 'j' ? FORWARD
: BACKWARD
,
4964 cap
->count1
) == FAIL
)
4965 clearopbeep(cap
->oap
);
4968 #endif /* FEAT_FOLDING */
4971 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4973 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4974 nchar
= plain_vgetc();
4976 LANGMAP_ADJUST(nchar
, TRUE
);
4980 #ifdef FEAT_CMDL_INFO
4981 (void)add_to_showcmd(nchar
);
4983 if (vim_strchr((char_u
*)"gGwW", nchar
) == NULL
)
4985 clearopbeep(cap
->oap
);
4991 case 'g': /* "zg": add good word to word list */
4992 case 'w': /* "zw": add wrong word to word list */
4993 case 'G': /* "zG": add good word to temp word list */
4994 case 'W': /* "zW": add wrong word to temp word list */
4999 if (checkclearop(cap
->oap
))
5002 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &len
)
5008 pos_T pos
= curwin
->w_cursor
;
5010 /* Find bad word under the cursor. */
5011 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
);
5012 if (len
!= 0 && curwin
->w_cursor
.col
<= pos
.col
)
5013 ptr
= ml_get_pos(&curwin
->w_cursor
);
5014 curwin
->w_cursor
= pos
;
5017 if (ptr
== NULL
&& (len
= find_ident_under_cursor(&ptr
,
5020 spell_add_word(ptr
, len
, nchar
== 'w' || nchar
== 'W',
5021 (nchar
== 'G' || nchar
== 'W')
5022 ? 0 : (int)cap
->count1
,
5027 case '=': /* "z=": suggestions for a badly spelled word */
5028 if (!checkclearop(cap
->oap
))
5029 spell_suggest((int)cap
->count0
);
5033 default: clearopbeep(cap
->oap
);
5037 /* Redraw when 'foldenable' changed */
5038 if (old_fen
!= curwin
->w_p_fen
)
5043 if (foldmethodIsDiff(curwin
) && curwin
->w_p_scb
)
5045 /* Adjust 'foldenable' in diff-synced windows. */
5048 if (wp
!= curwin
&& foldmethodIsDiff(wp
) && wp
->w_p_scb
)
5050 wp
->w_p_fen
= curwin
->w_p_fen
;
5051 changed_window_setting_win(wp
);
5056 changed_window_setting();
5059 /* Redraw when 'foldlevel' changed. */
5060 if (old_fdl
!= curwin
->w_p_fdl
)
5067 * Vertical scrollbar movement.
5070 nv_ver_scrollbar(cap
)
5073 if (cap
->oap
->op_type
!= OP_NOP
)
5074 clearopbeep(cap
->oap
);
5076 /* Even if an operator was pending, we still want to scroll */
5081 * Horizontal scrollbar movement.
5084 nv_hor_scrollbar(cap
)
5087 if (cap
->oap
->op_type
!= OP_NOP
)
5088 clearopbeep(cap
->oap
);
5090 /* Even if an operator was pending, we still want to scroll */
5091 gui_do_horiz_scroll();
5095 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5103 if (cap
->oap
->op_type
!= OP_NOP
)
5104 clearopbeep(cap
->oap
);
5106 /* Even if an operator was pending, we still want to jump tabs. */
5107 goto_tabpage(current_tab
);
5111 * Selected item in tab line menu.
5117 if (cap
->oap
->op_type
!= OP_NOP
)
5118 clearopbeep(cap
->oap
);
5120 /* Even if an operator was pending, we still want to jump tabs. */
5125 * Handle selecting an item of the GUI tab line menu.
5126 * Used in Normal and Insert mode.
5131 switch (current_tabmenu
)
5133 case TABLINE_MENU_CLOSE
:
5134 if (current_tab
== 0)
5135 do_cmdline_cmd((char_u
*)"tabclose");
5138 vim_snprintf((char *)IObuff
, IOSIZE
, "tabclose %d",
5140 do_cmdline_cmd(IObuff
);
5144 case TABLINE_MENU_NEW
:
5145 vim_snprintf((char *)IObuff
, IOSIZE
, "%dtabnew",
5146 current_tab
> 0 ? current_tab
- 1 : 999);
5147 do_cmdline_cmd(IObuff
);
5150 case TABLINE_MENU_OPEN
:
5151 vim_snprintf((char *)IObuff
, IOSIZE
, "browse %dtabnew",
5152 current_tab
> 0 ? current_tab
- 1 : 999);
5153 do_cmdline_cmd(IObuff
);
5167 * Ignore 'Q' in Visual mode, just give a beep.
5174 if (!checkclearop(cap
->oap
))
5179 * Handle a ":" command.
5193 if (cap
->oap
->op_type
!= OP_NOP
)
5195 /* Using ":" as a movement is characterwise exclusive. */
5196 cap
->oap
->motion_type
= MCHAR
;
5197 cap
->oap
->inclusive
= FALSE
;
5199 else if (cap
->count0
)
5201 /* translate "count:" into ":.,.+(count - 1)" */
5202 stuffcharReadbuff('.');
5203 if (cap
->count0
> 1)
5205 stuffReadbuff((char_u
*)",.+");
5206 stuffnumReadbuff((long)cap
->count0
- 1L);
5210 /* When typing, don't type below an old message */
5216 /* get a command line and execute it */
5217 do_cmdline(NULL
, getexline
, NULL
,
5218 cap
->oap
->op_type
!= OP_NOP
? DOCMD_KEEPLINE
: 0);
5220 /* If 'insertmode' changed, enter or exit Insert mode */
5221 if (p_im
!= old_p_im
)
5229 /* The start of the operator may have become invalid by the Ex
5231 if (cap
->oap
->op_type
!= OP_NOP
5232 && (cap
->oap
->start
.lnum
> curbuf
->b_ml
.ml_line_count
5233 || cap
->oap
->start
.col
>
5234 STRLEN(ml_get(cap
->oap
->start
.lnum
))))
5235 clearopbeep(cap
->oap
);
5240 * Handle CTRL-G command.
5247 if (VIsual_active
) /* toggle Selection/Visual mode */
5249 VIsual_select
= !VIsual_select
;
5254 if (!checkclearop(cap
->oap
))
5255 /* print full name if count given or :cd used */
5256 fileinfo((int)cap
->count0
, FALSE
, TRUE
);
5260 * Handle CTRL-H <Backspace> command.
5267 if (VIsual_active
&& VIsual_select
)
5269 cap
->cmdchar
= 'x'; /* BS key behaves like 'x' in Select mode */
5278 * CTRL-L: clear screen and redraw.
5284 if (!checkclearop(cap
->oap
))
5286 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5288 * Right now, the BeBox doesn't seem to have an easy way to detect
5289 * window resizing, so we cheat and make the user detect it
5290 * manually with CTRL-L instead
5295 /* Clear all syntax states to force resyncing. */
5296 syn_stack_free_all(curbuf
);
5298 redraw_later(CLEAR
);
5303 * CTRL-O: In Select mode: switch to Visual mode for one command.
5304 * Otherwise: Go to older pcmark.
5311 if (VIsual_active
&& VIsual_select
)
5313 VIsual_select
= FALSE
;
5315 restart_VIsual_select
= 2; /* restart Select mode later */
5320 cap
->count1
= -cap
->count1
;
5326 * CTRL-^ command, short for ":e #"
5332 if (!checkclearopq(cap
->oap
))
5333 (void)buflist_getfile((int)cap
->count0
, (linenr_T
)0,
5334 GETF_SETMARK
|GETF_ALT
, FALSE
);
5344 if (!checkclearopq(cap
->oap
))
5348 /* "ZZ": equivalent to ":x". */
5349 case 'Z': do_cmdline_cmd((char_u
*)"x");
5352 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5353 case 'Q': do_cmdline_cmd((char_u
*)"q!");
5356 default: clearopbeep(cap
->oap
);
5361 #if defined(FEAT_WINDOWS) || defined(PROTO)
5363 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5374 vim_memset(&ca
, 0, sizeof(ca
));
5383 * Handle the commands that use the word under the cursor.
5384 * [g] CTRL-] :ta to current identifier
5385 * [g] 'K' run program for current identifier
5386 * [g] '*' / to current identifier or string
5387 * [g] '#' ? to current identifier or string
5388 * g ']' :tselect for current identifier
5397 char_u
*kp
; /* value of 'keywordprg' */
5398 int kp_help
; /* 'keywordprg' is ":help" */
5399 int n
= 0; /* init for GCC */
5401 int g_cmd
; /* "g" command */
5406 if (cap
->cmdchar
== 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5408 cmdchar
= cap
->nchar
;
5413 cmdchar
= cap
->cmdchar
;
5417 if (cmdchar
== POUND
) /* the pound sign, '#' for English keyboards */
5421 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5423 if (cmdchar
== ']' || cmdchar
== Ctrl_RSB
|| cmdchar
== 'K')
5426 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &n
) == FAIL
)
5429 if (checkclearopq(cap
->oap
))
5433 if (ptr
== NULL
&& (n
= find_ident_under_cursor(&ptr
,
5434 (cmdchar
== '*' || cmdchar
== '#')
5435 ? FIND_IDENT
|FIND_STRING
: FIND_IDENT
)) == 0)
5441 /* Allocate buffer to put the command in. Inserting backslashes can
5442 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5443 * and some numbers. */
5444 kp
= (*curbuf
->b_p_kp
== NUL
? p_kp
: curbuf
->b_p_kp
);
5445 kp_help
= (*kp
== NUL
|| STRCMP(kp
, ":he") == 0
5446 || STRCMP(kp
, ":help") == 0);
5447 buf
= alloc((unsigned)(n
* 2 + 30 + STRLEN(kp
)));
5457 * Put cursor at start of word, makes search skip the word
5459 * Call setpcmark() first, so "*``" puts the cursor back where
5463 curwin
->w_cursor
.col
= (colnr_T
) (ptr
- ml_get_curline());
5465 if (!g_cmd
&& vim_iswordp(ptr
))
5467 no_smartcase
= TRUE
; /* don't use 'smartcase' now */
5472 STRCPY(buf
, "he! ");
5475 /* An external command will probably use an argument starting
5476 * with "-" as an option. To avoid trouble we skip the "-". */
5477 while (*ptr
== '-' && n
> 0)
5484 EMSG(_(e_noident
)); /* found dashes only */
5489 /* When a count is given, turn it into a range. Is this
5490 * really what we want? */
5491 isman
= (STRCMP(kp
, "man") == 0);
5492 isman_s
= (STRCMP(kp
, "man -s") == 0);
5493 if (cap
->count0
!= 0 && !(isman
|| isman_s
))
5494 sprintf((char *)buf
, ".,.+%ld", cap
->count0
- 1);
5497 if (cap
->count0
== 0 && isman_s
)
5502 if (cap
->count0
!= 0 && (isman
|| isman_s
))
5504 sprintf((char *)buf
+ STRLEN(buf
), "%ld", cap
->count0
);
5513 STRCPY(buf
, "cstag ");
5521 STRCPY(buf
, "he! ");
5525 sprintf((char *)buf
, "%ldta ", cap
->count0
);
5529 * Now grab the chars in the identifier
5531 if (cmdchar
== 'K' && !kp_help
)
5533 /* Escape the argument properly for a shell command */
5534 ptr
= vim_strnsave(ptr
, n
);
5535 p
= vim_strsave_shellescape(ptr
, TRUE
);
5542 buf
= (char_u
*)vim_realloc(buf
, STRLEN(buf
) + STRLEN(p
) + 1);
5555 aux_ptr
= (char_u
*)(p_magic
? "/.*~[^$\\" : "/^$\\");
5556 else if (cmdchar
== '#')
5557 aux_ptr
= (char_u
*)(p_magic
? "/?.*~[^$\\" : "/?^$\\");
5559 /* Don't escape spaces and Tabs in a tag with a backslash */
5560 aux_ptr
= (char_u
*)"\\|\"\n*?[";
5562 p
= buf
+ STRLEN(buf
);
5565 /* put a backslash before \ and some others */
5566 if (vim_strchr(aux_ptr
, *ptr
) != NULL
)
5569 /* When current byte is a part of multibyte character, copy all
5570 * bytes of that character. */
5574 int len
= (*mb_ptr2len
)(ptr
) - 1;
5576 for (i
= 0; i
< len
&& n
>= 1; ++i
, --n
)
5586 * Execute the command.
5588 if (cmdchar
== '*' || cmdchar
== '#')
5592 has_mbyte
? vim_iswordp(mb_prevptr(ml_get_curline(), ptr
)) :
5594 vim_iswordc(ptr
[-1])))
5597 /* put pattern in search history */
5598 add_to_history(HIST_SEARCH
, buf
, TRUE
, NUL
);
5600 normal_search(cap
, cmdchar
== '*' ? '/' : '?', buf
, 0);
5603 do_cmdline_cmd(buf
);
5608 #if defined(FEAT_VISUAL) || defined(PROTO)
5610 * Get visually selected text, within one line only.
5611 * Returns FAIL if more than one line selected.
5614 get_visual_text(cap
, pp
, lenp
)
5616 char_u
**pp
; /* return: start of selected text */
5617 int *lenp
; /* return: length of selected text */
5619 if (VIsual_mode
!= 'V')
5621 if (VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
5624 clearopbeep(cap
->oap
);
5627 if (VIsual_mode
== 'V')
5629 *pp
= ml_get_curline();
5630 *lenp
= (int)STRLEN(*pp
);
5634 if (lt(curwin
->w_cursor
, VIsual
))
5636 *pp
= ml_get_pos(&curwin
->w_cursor
);
5637 *lenp
= VIsual
.col
- curwin
->w_cursor
.col
+ 1;
5641 *pp
= ml_get_pos(&VIsual
);
5642 *lenp
= curwin
->w_cursor
.col
- VIsual
.col
+ 1;
5646 /* Correct the length to include the whole last character. */
5647 *lenp
+= (*mb_ptr2len
)(*pp
+ (*lenp
- 1)) - 1;
5650 reset_VIsual_and_resel();
5656 * CTRL-T: backwards in tag stack
5662 if (!checkclearopq(cap
->oap
))
5663 do_tag((char_u
*)"", DT_POP
, (int)cap
->count1
, FALSE
, TRUE
);
5667 * Handle scrolling command 'H', 'L' and 'M'.
5680 cap
->oap
->motion_type
= MLINE
;
5683 if (cap
->cmdchar
== 'L')
5685 validate_botline(); /* make sure curwin->w_botline is valid */
5686 curwin
->w_cursor
.lnum
= curwin
->w_botline
- 1;
5687 if (cap
->count1
- 1 >= curwin
->w_cursor
.lnum
)
5688 curwin
->w_cursor
.lnum
= 1;
5692 if (hasAnyFolding(curwin
))
5694 /* Count a fold for one screen line. */
5695 for (n
= cap
->count1
- 1; n
> 0
5696 && curwin
->w_cursor
.lnum
> curwin
->w_topline
; --n
)
5698 (void)hasFolding(curwin
->w_cursor
.lnum
,
5699 &curwin
->w_cursor
.lnum
, NULL
);
5700 --curwin
->w_cursor
.lnum
;
5705 curwin
->w_cursor
.lnum
-= cap
->count1
- 1;
5710 if (cap
->cmdchar
== 'M')
5713 /* Don't count filler lines above the window. */
5714 used
-= diff_check_fill(curwin
, curwin
->w_topline
)
5715 - curwin
->w_topfill
;
5717 validate_botline(); /* make sure w_empty_rows is valid */
5718 half
= (curwin
->w_height
- curwin
->w_empty_rows
+ 1) / 2;
5719 for (n
= 0; curwin
->w_topline
+ n
< curbuf
->b_ml
.ml_line_count
; ++n
)
5722 /* Count half he number of filler lines to be "below this
5723 * line" and half to be "above the next line". */
5724 if (n
> 0 && used
+ diff_check_fill(curwin
, curwin
->w_topline
5731 used
+= plines(curwin
->w_topline
+ n
);
5735 if (hasFolding(curwin
->w_topline
+ n
, NULL
, &lnum
))
5736 n
= lnum
- curwin
->w_topline
;
5739 if (n
> 0 && used
> curwin
->w_height
)
5742 else /* (cap->cmdchar == 'H') */
5744 n
= cap
->count1
- 1;
5746 if (hasAnyFolding(curwin
))
5748 /* Count a fold for one screen line. */
5749 lnum
= curwin
->w_topline
;
5750 while (n
-- > 0 && lnum
< curwin
->w_botline
- 1)
5752 hasFolding(lnum
, NULL
, &lnum
);
5755 n
= lnum
- curwin
->w_topline
;
5759 curwin
->w_cursor
.lnum
= curwin
->w_topline
+ n
;
5760 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5761 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5764 cursor_correct(); /* correct for 'so' */
5765 beginline(BL_SOL
| BL_FIX
);
5769 * Cursor right commands.
5779 # define PAST_LINE 0
5782 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5784 /* <C-Right> and <S-Right> move a word or WORD right */
5785 if (mod_mask
& MOD_MASK_CTRL
)
5791 cap
->oap
->motion_type
= MCHAR
;
5792 cap
->oap
->inclusive
= FALSE
;
5794 PAST_LINE
= (VIsual_active
&& *p_sel
!= 'o');
5796 # ifdef FEAT_VIRTUALEDIT
5798 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5799 * (theoretically) infinitely long.
5801 if (virtual_active())
5806 for (n
= cap
->count1
; n
> 0; --n
)
5808 if ((!PAST_LINE
&& oneright() == FAIL
)
5809 || (PAST_LINE
&& *ml_get_cursor() == NUL
))
5812 * <Space> wraps to next line if 'whichwrap' has 's'.
5813 * 'l' wraps to next line if 'whichwrap' has 'l'.
5814 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5816 if ( ((cap
->cmdchar
== ' '
5817 && vim_strchr(p_ww
, 's') != NULL
)
5818 || (cap
->cmdchar
== 'l'
5819 && vim_strchr(p_ww
, 'l') != NULL
)
5820 || (cap
->cmdchar
== K_RIGHT
5821 && vim_strchr(p_ww
, '>') != NULL
))
5822 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
5824 /* When deleting we also count the NL as a character.
5825 * Set cap->oap->inclusive when last char in the line is
5826 * included, move to next line after that */
5827 if ( cap
->oap
->op_type
!= OP_NOP
5828 && !cap
->oap
->inclusive
5829 && !lineempty(curwin
->w_cursor
.lnum
))
5830 cap
->oap
->inclusive
= TRUE
;
5833 ++curwin
->w_cursor
.lnum
;
5834 curwin
->w_cursor
.col
= 0;
5835 #ifdef FEAT_VIRTUALEDIT
5836 curwin
->w_cursor
.coladd
= 0;
5838 curwin
->w_set_curswant
= TRUE
;
5839 cap
->oap
->inclusive
= FALSE
;
5843 if (cap
->oap
->op_type
== OP_NOP
)
5845 /* Only beep and flush if not moved at all */
5846 if (n
== cap
->count1
)
5851 if (!lineempty(curwin
->w_cursor
.lnum
))
5852 cap
->oap
->inclusive
= TRUE
;
5859 curwin
->w_set_curswant
= TRUE
;
5860 # ifdef FEAT_VIRTUALEDIT
5861 if (virtual_active())
5868 curwin
->w_cursor
.col
+=
5869 (*mb_ptr2len
)(ml_get_cursor());
5872 ++curwin
->w_cursor
.col
;
5878 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5879 && cap
->oap
->op_type
== OP_NOP
)
5885 * Cursor left commands.
5887 * Returns TRUE when operator end should not be adjusted.
5895 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5897 /* <C-Left> and <S-Left> move a word or WORD left */
5898 if (mod_mask
& MOD_MASK_CTRL
)
5904 cap
->oap
->motion_type
= MCHAR
;
5905 cap
->oap
->inclusive
= FALSE
;
5906 for (n
= cap
->count1
; n
> 0; --n
)
5908 if (oneleft() == FAIL
)
5910 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5911 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5912 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5914 if ( (((cap
->cmdchar
== K_BS
5915 || cap
->cmdchar
== Ctrl_H
)
5916 && vim_strchr(p_ww
, 'b') != NULL
)
5917 || (cap
->cmdchar
== 'h'
5918 && vim_strchr(p_ww
, 'h') != NULL
)
5919 || (cap
->cmdchar
== K_LEFT
5920 && vim_strchr(p_ww
, '<') != NULL
))
5921 && curwin
->w_cursor
.lnum
> 1)
5923 --(curwin
->w_cursor
.lnum
);
5924 coladvance((colnr_T
)MAXCOL
);
5925 curwin
->w_set_curswant
= TRUE
;
5927 /* When the NL before the first char has to be deleted we
5928 * put the cursor on the NUL after the previous line.
5929 * This is a very special case, be careful!
5930 * Don't adjust op_end now, otherwise it won't work. */
5931 if ( (cap
->oap
->op_type
== OP_DELETE
5932 || cap
->oap
->op_type
== OP_CHANGE
)
5933 && !lineempty(curwin
->w_cursor
.lnum
))
5935 if (*ml_get_cursor() != NUL
)
5936 ++curwin
->w_cursor
.col
;
5937 cap
->retval
|= CA_NO_ADJ_OP_END
;
5941 /* Only beep and flush if not moved at all */
5942 else if (cap
->oap
->op_type
== OP_NOP
&& n
== cap
->count1
)
5948 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5949 && cap
->oap
->op_type
== OP_NOP
)
5955 * Cursor up commands.
5956 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5962 if (mod_mask
& MOD_MASK_SHIFT
)
5964 /* <S-Up> is page up */
5965 cap
->arg
= BACKWARD
;
5970 cap
->oap
->motion_type
= MLINE
;
5971 if (cursor_up(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5972 clearopbeep(cap
->oap
);
5974 beginline(BL_WHITE
| BL_FIX
);
5979 * Cursor down commands.
5980 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5986 if (mod_mask
& MOD_MASK_SHIFT
)
5988 /* <S-Down> is page down */
5993 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5994 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5995 if (bt_quickfix(curbuf
) && cap
->cmdchar
== CAR
)
5996 if (curwin
->w_llist_ref
== NULL
)
5997 do_cmdline_cmd((char_u
*)".cc"); /* quickfix window */
5999 do_cmdline_cmd((char_u
*)".ll"); /* location list window */
6004 /* In the cmdline window a <CR> executes the command. */
6005 if (cmdwin_type
!= 0 && cap
->cmdchar
== CAR
)
6006 cmdwin_result
= CAR
;
6010 cap
->oap
->motion_type
= MLINE
;
6011 if (cursor_down(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6012 clearopbeep(cap
->oap
);
6014 beginline(BL_WHITE
| BL_FIX
);
6019 #ifdef FEAT_SEARCHPATH
6021 * Grab the file name under the cursor and edit it.
6032 clearopbeep(cap
->oap
);
6037 if (curbuf_locked())
6044 ptr
= grab_file_name(cap
->count1
, &lnum
);
6048 /* do autowrite if necessary */
6049 if (curbufIsChanged() && curbuf
->b_nwindows
<= 1 && !P_HID(curbuf
))
6050 autowrite(curbuf
, FALSE
);
6052 (void)do_ecmd(0, ptr
, NULL
, NULL
, ECMD_LAST
,
6053 P_HID(curbuf
) ? ECMD_HIDE
: 0);
6054 if (cap
->nchar
== 'F' && lnum
>= 0)
6056 curwin
->w_cursor
.lnum
= lnum
;
6057 check_cursor_lnum();
6058 beginline(BL_SOL
| BL_FIX
);
6068 * <End> command: to end of current line or last line.
6074 if (cap
->arg
|| (mod_mask
& MOD_MASK_CTRL
)) /* CTRL-END = goto last line */
6078 cap
->count1
= 1; /* to end of current line */
6084 * Handle the "$" command.
6090 cap
->oap
->motion_type
= MCHAR
;
6091 cap
->oap
->inclusive
= TRUE
;
6092 #ifdef FEAT_VIRTUALEDIT
6093 /* In virtual mode when off the edge of a line and an operator
6094 * is pending (whew!) keep the cursor where it is.
6095 * Otherwise, send it to the end of the line. */
6096 if (!virtual_active() || gchar_cursor() != NUL
6097 || cap
->oap
->op_type
== OP_NOP
)
6099 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
6100 if (cursor_down((long)(cap
->count1
- 1),
6101 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6102 clearopbeep(cap
->oap
);
6104 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6110 * Implementation of '?' and '/' commands.
6111 * If cap->arg is TRUE don't set PC mark.
6117 oparg_T
*oap
= cap
->oap
;
6119 if (cap
->cmdchar
== '?' && cap
->oap
->op_type
== OP_ROT13
)
6121 /* Translate "g??" to "g?g?" */
6128 cap
->searchbuf
= getcmdline(cap
->cmdchar
, cap
->count1
, 0);
6130 if (cap
->searchbuf
== NULL
)
6136 normal_search(cap
, cap
->cmdchar
, cap
->searchbuf
,
6137 (cap
->arg
? 0 : SEARCH_MARK
));
6141 * Handle "N" and "n" commands.
6142 * cap->arg is SEARCH_REV for "N", 0 for "n".
6148 normal_search(cap
, 0, NULL
, SEARCH_MARK
| cap
->arg
);
6152 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6153 * Uses only cap->count1 and cap->oap from "cap".
6156 normal_search(cap
, dir
, pat
, opt
)
6160 int opt
; /* extra flags for do_search() */
6164 cap
->oap
->motion_type
= MCHAR
;
6165 cap
->oap
->inclusive
= FALSE
;
6166 cap
->oap
->use_reg_one
= TRUE
;
6167 curwin
->w_set_curswant
= TRUE
;
6169 i
= do_search(cap
->oap
, dir
, pat
, cap
->count1
,
6170 opt
| SEARCH_OPT
| SEARCH_ECHO
| SEARCH_MSG
, NULL
);
6176 cap
->oap
->motion_type
= MLINE
;
6177 #ifdef FEAT_VIRTUALEDIT
6178 curwin
->w_cursor
.coladd
= 0;
6181 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6186 /* "/$" will put the cursor after the end of the line, may need to
6187 * correct that here */
6192 * Character search commands.
6193 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6194 * ',' and FALSE for ';'.
6195 * cap->nchar is NUL for ',' and ';' (repeat the search)
6203 if (cap
->cmdchar
== 't' || cap
->cmdchar
== 'T')
6208 cap
->oap
->motion_type
= MCHAR
;
6209 if (IS_SPECIAL(cap
->nchar
) || searchc(cap
, t_cmd
) == FAIL
)
6210 clearopbeep(cap
->oap
);
6213 curwin
->w_set_curswant
= TRUE
;
6214 #ifdef FEAT_VIRTUALEDIT
6215 /* Include a Tab for "tx" and for "dfx". */
6216 if (gchar_cursor() == TAB
&& virtual_active() && cap
->arg
== FORWARD
6217 && (t_cmd
|| cap
->oap
->op_type
!= OP_NOP
))
6221 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
6222 curwin
->w_cursor
.coladd
= ecol
- scol
;
6225 curwin
->w_cursor
.coladd
= 0;
6228 adjust_for_sel(cap
);
6231 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6238 * "[" and "]" commands.
6239 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6247 pos_T
*pos
= NULL
; /* init for GCC */
6248 pos_T old_pos
; /* cursor position before command */
6254 cap
->oap
->motion_type
= MCHAR
;
6255 cap
->oap
->inclusive
= FALSE
;
6256 old_pos
= curwin
->w_cursor
;
6257 #ifdef FEAT_VIRTUALEDIT
6258 curwin
->w_cursor
.coladd
= 0; /* TODO: don't do this for an error. */
6261 #ifdef FEAT_SEARCHPATH
6263 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6265 if (cap
->nchar
== 'f')
6272 * Find the occurrence(s) of the identifier or define under cursor
6273 * in current and included files or jump to the first occurrence.
6276 * fwd bwd fwd bwd fwd bwd
6277 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6278 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6280 if (vim_strchr((char_u
*)
6286 cap
->nchar
) != NULL
)
6291 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0)
6295 find_pattern_in_path(ptr
, 0, len
, TRUE
,
6296 cap
->count0
== 0 ? !isupper(cap
->nchar
) : FALSE
,
6297 ((cap
->nchar
& 0xf) == ('d' & 0xf)) ? FIND_DEFINE
: FIND_ANY
,
6299 isupper(cap
->nchar
) ? ACTION_SHOW_ALL
:
6300 islower(cap
->nchar
) ? ACTION_SHOW
: ACTION_GOTO
,
6301 cap
->cmdchar
== ']' ? curwin
->w_cursor
.lnum
+ 1 : (linenr_T
)1,
6303 curwin
->w_set_curswant
= TRUE
;
6310 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6311 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6312 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6313 * "[m" or "]m" search for prev/next start of (Java) method.
6314 * "[M" or "]M" search for prev/next end of (Java) method.
6316 if ( (cap
->cmdchar
== '['
6317 && vim_strchr((char_u
*)"{(*/#mM", cap
->nchar
) != NULL
)
6318 || (cap
->cmdchar
== ']'
6319 && vim_strchr((char_u
*)"})*/#mM", cap
->nchar
) != NULL
))
6321 if (cap
->nchar
== '*')
6325 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6327 if (cap
->cmdchar
== '[')
6340 if ((pos
= findmatchlimit(cap
->oap
, findc
,
6341 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
, 0)) == NULL
)
6343 if (new_pos
.lnum
== 0) /* nothing found */
6345 if (cap
->nchar
!= 'm' && cap
->nchar
!= 'M')
6346 clearopbeep(cap
->oap
);
6349 pos
= &new_pos
; /* use last one found */
6353 curwin
->w_cursor
= *pos
;
6356 curwin
->w_cursor
= old_pos
;
6359 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6360 * brought us to the match for "[m" and "]M" when inside a method.
6361 * Try finding the '{' or '}' we want to be at.
6362 * Also repeat for the given count.
6364 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6366 /* norm is TRUE for "]M" and "[m" */
6367 int norm
= ((findc
== '{') == (cap
->nchar
== 'm'));
6370 /* found a match: we were inside a method */
6371 if (prev_pos
.lnum
!= 0)
6374 curwin
->w_cursor
= prev_pos
;
6384 if ((findc
== '{' ? dec_cursor() : inc_cursor()) < 0)
6386 /* if not found anything, that's an error */
6388 clearopbeep(cap
->oap
);
6393 if (c
== '{' || c
== '}')
6395 /* Must have found end/start of class: use it.
6396 * Or found the place to be at. */
6397 if ((c
== findc
&& norm
) || (n
== 1 && !norm
))
6399 new_pos
= curwin
->w_cursor
;
6403 /* if no match found at all, we started outside of the
6404 * class and we're inside now. Just go on. */
6405 else if (new_pos
.lnum
== 0)
6407 new_pos
= curwin
->w_cursor
;
6410 /* found start/end of other method: go to match */
6411 else if ((pos
= findmatchlimit(cap
->oap
, findc
,
6412 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
,
6416 curwin
->w_cursor
= *pos
;
6422 curwin
->w_cursor
= old_pos
;
6423 if (pos
== NULL
&& new_pos
.lnum
!= 0)
6424 clearopbeep(cap
->oap
);
6429 curwin
->w_cursor
= *pos
;
6430 curwin
->w_set_curswant
= TRUE
;
6432 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
6433 && cap
->oap
->op_type
== OP_NOP
)
6440 * "[[", "[]", "]]" and "][": move to start or end of function
6442 else if (cap
->nchar
== '[' || cap
->nchar
== ']')
6444 if (cap
->nchar
== cap
->cmdchar
) /* "]]" or "[[" */
6447 flag
= '}'; /* "][" or "[]" */
6449 curwin
->w_set_curswant
= TRUE
;
6451 * Imitate strange Vi behaviour: When using "]]" with an operator
6452 * we also stop at '}'.
6454 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, flag
,
6455 (cap
->oap
->op_type
!= OP_NOP
6456 && cap
->arg
== FORWARD
&& flag
== '{')))
6457 clearopbeep(cap
->oap
);
6460 if (cap
->oap
->op_type
== OP_NOP
)
6461 beginline(BL_WHITE
| BL_FIX
);
6463 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6470 * "[p", "[P", "]P" and "]p": put with indent adjustment
6472 else if (cap
->nchar
== 'p' || cap
->nchar
== 'P')
6474 if (!checkclearop(cap
->oap
))
6477 do_put(cap
->oap
->regname
,
6478 (cap
->cmdchar
== ']' && cap
->nchar
== 'p') ? FORWARD
: BACKWARD
,
6479 cap
->count1
, PUT_FIXINDENT
);
6484 * "['", "[`", "]'" and "]`": jump to next mark
6486 else if (cap
->nchar
== '\'' || cap
->nchar
== '`')
6488 pos
= &curwin
->w_cursor
;
6489 for (n
= cap
->count1
; n
> 0; --n
)
6492 pos
= getnextmark(pos
, cap
->cmdchar
== '[' ? BACKWARD
: FORWARD
,
6493 cap
->nchar
== '\'');
6499 nv_cursormark(cap
, cap
->nchar
== '\'', pos
);
6504 * [ or ] followed by a middle mouse click: put selected text with
6505 * indent adjustment. Any other button just does as usual.
6507 else if (cap
->nchar
>= K_LEFTMOUSE
&& cap
->nchar
<= K_RIGHTRELEASE
)
6509 (void)do_mouse(cap
->oap
, cap
->nchar
,
6510 (cap
->cmdchar
== ']') ? FORWARD
: BACKWARD
,
6511 cap
->count1
, PUT_FIXINDENT
);
6513 #endif /* FEAT_MOUSE */
6517 * "[z" and "]z": move to start or end of open fold.
6519 else if (cap
->nchar
== 'z')
6521 if (foldMoveTo(FALSE
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6522 cap
->count1
) == FAIL
)
6523 clearopbeep(cap
->oap
);
6529 * "[c" and "]c": move to next or previous diff-change.
6531 else if (cap
->nchar
== 'c')
6533 if (diff_move_to(cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6534 cap
->count1
) == FAIL
)
6535 clearopbeep(cap
->oap
);
6541 * "[s", "[S", "]s" and "]S": move to next spell error.
6543 else if (cap
->nchar
== 's' || cap
->nchar
== 'S')
6546 for (n
= 0; n
< cap
->count1
; ++n
)
6547 if (spell_move_to(curwin
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6548 cap
->nchar
== 's' ? TRUE
: FALSE
, FALSE
, NULL
) == 0)
6550 clearopbeep(cap
->oap
);
6553 # ifdef FEAT_FOLDING
6554 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6560 /* Not a valid cap->nchar. */
6562 clearopbeep(cap
->oap
);
6566 * Handle Normal mode "%" command.
6574 linenr_T lnum
= curwin
->w_cursor
.lnum
;
6577 cap
->oap
->inclusive
= TRUE
;
6578 if (cap
->count0
) /* {cnt}% : goto {cnt} percentage in file */
6580 if (cap
->count0
> 100)
6581 clearopbeep(cap
->oap
);
6584 cap
->oap
->motion_type
= MLINE
;
6586 /* Round up, so CTRL-G will give same value. Watch out for a
6587 * large line count, the line number must not go negative! */
6588 if (curbuf
->b_ml
.ml_line_count
> 1000000)
6589 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
+ 99L)
6590 / 100L * cap
->count0
;
6592 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
*
6593 cap
->count0
+ 99L) / 100L;
6594 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6595 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6596 beginline(BL_SOL
| BL_FIX
);
6599 else /* "%" : go to matching paren */
6601 cap
->oap
->motion_type
= MCHAR
;
6602 cap
->oap
->use_reg_one
= TRUE
;
6603 if ((pos
= findmatch(cap
->oap
, NUL
)) == NULL
)
6604 clearopbeep(cap
->oap
);
6608 curwin
->w_cursor
= *pos
;
6609 curwin
->w_set_curswant
= TRUE
;
6610 #ifdef FEAT_VIRTUALEDIT
6611 curwin
->w_cursor
.coladd
= 0;
6614 adjust_for_sel(cap
);
6619 if (cap
->oap
->op_type
== OP_NOP
6620 && lnum
!= curwin
->w_cursor
.lnum
6621 && (fdo_flags
& FDO_PERCENT
)
6628 * Handle "(" and ")" commands.
6629 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6635 cap
->oap
->motion_type
= MCHAR
;
6636 cap
->oap
->use_reg_one
= TRUE
;
6637 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6638 cap
->oap
->inclusive
= FALSE
;
6639 curwin
->w_set_curswant
= TRUE
;
6641 if (findsent(cap
->arg
, cap
->count1
) == FAIL
)
6642 clearopbeep(cap
->oap
);
6645 /* Don't leave the cursor on the NUL past end of line. */
6646 adjust_cursor(cap
->oap
);
6647 #ifdef FEAT_VIRTUALEDIT
6648 curwin
->w_cursor
.coladd
= 0;
6651 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6658 * "m" command: Mark a position.
6664 if (!checkclearop(cap
->oap
))
6666 if (setmark(cap
->nchar
) == FAIL
)
6667 clearopbeep(cap
->oap
);
6672 * "{" and "}" commands.
6673 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6679 cap
->oap
->motion_type
= MCHAR
;
6680 cap
->oap
->inclusive
= FALSE
;
6681 cap
->oap
->use_reg_one
= TRUE
;
6682 curwin
->w_set_curswant
= TRUE
;
6683 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, NUL
, FALSE
))
6684 clearopbeep(cap
->oap
);
6687 #ifdef FEAT_VIRTUALEDIT
6688 curwin
->w_cursor
.coladd
= 0;
6691 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6698 * "u" command: Undo or make lower case.
6704 if (cap
->oap
->op_type
== OP_LOWER
6710 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6726 if (!checkclearopq(cap
->oap
))
6728 u_undo((int)cap
->count1
);
6729 curwin
->w_set_curswant
= TRUE
;
6734 * Handle the "r" command.
6744 if (checkclearop(cap
->oap
))
6747 /* get another character */
6748 if (cap
->nchar
== Ctrl_V
)
6750 had_ctrl_v
= Ctrl_V
;
6751 cap
->nchar
= get_literal();
6752 /* Don't redo a multibyte character with CTRL-V. */
6753 if (cap
->nchar
> DEL
)
6759 /* Abort if the character is a special key. */
6760 if (IS_SPECIAL(cap
->nchar
))
6762 clearopbeep(cap
->oap
);
6767 /* Visual mode "r" */
6775 #ifdef FEAT_VIRTUALEDIT
6776 /* Break tabs, etc. */
6777 if (virtual_active())
6779 if (u_save_cursor() == FAIL
)
6781 if (gchar_cursor() == NUL
)
6783 /* Add extra space and put the cursor on the first one. */
6784 coladvance_force((colnr_T
)(getviscol() + cap
->count1
));
6785 curwin
->w_cursor
.col
-= cap
->count1
;
6787 else if (gchar_cursor() == TAB
)
6788 coladvance_force(getviscol());
6792 /* Abort if not enough characters to replace. */
6793 ptr
= ml_get_cursor();
6794 if (STRLEN(ptr
) < (unsigned)cap
->count1
6796 || (has_mbyte
&& mb_charlen(ptr
) < cap
->count1
)
6800 clearopbeep(cap
->oap
);
6805 * Replacing with a TAB is done by edit() when it is complicated because
6806 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6807 * Other characters are done below to avoid problems with things like
6808 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6810 if (had_ctrl_v
!= Ctrl_V
&& cap
->nchar
== '\t' && (curbuf
->b_p_et
|| p_sta
))
6812 stuffnumReadbuff(cap
->count1
);
6813 stuffcharReadbuff('R');
6814 stuffcharReadbuff('\t');
6815 stuffcharReadbuff(ESC
);
6819 /* save line for undo */
6820 if (u_save_cursor() == FAIL
)
6823 if (had_ctrl_v
!= Ctrl_V
&& (cap
->nchar
== '\r' || cap
->nchar
== '\n'))
6826 * Replace character(s) by a single newline.
6827 * Strange vi behaviour: Only one newline is inserted.
6828 * Delete the characters here.
6829 * Insert the newline with an insert command, takes care of
6830 * autoindent. The insert command depends on being on the last
6831 * character of a line or not.
6834 (void)del_chars(cap
->count1
, FALSE
); /* delete the characters */
6836 (void)del_bytes(cap
->count1
, FALSE
, FALSE
); /* delete the characters */
6838 stuffcharReadbuff('\r');
6839 stuffcharReadbuff(ESC
);
6841 /* Give 'r' to edit(), to get the redo command right. */
6842 invoke_edit(cap
, TRUE
, 'r', FALSE
);
6846 prep_redo(cap
->oap
->regname
, cap
->count1
,
6847 NUL
, 'r', NUL
, had_ctrl_v
, cap
->nchar
);
6849 curbuf
->b_op_start
= curwin
->w_cursor
;
6853 int old_State
= State
;
6855 if (cap
->ncharC1
!= 0)
6856 AppendCharToRedobuff(cap
->ncharC1
);
6857 if (cap
->ncharC2
!= 0)
6858 AppendCharToRedobuff(cap
->ncharC2
);
6860 /* This is slow, but it handles replacing a single-byte with a
6861 * multi-byte and the other way around. Also handles adding
6862 * composing characters for utf-8. */
6863 for (n
= cap
->count1
; n
> 0; --n
)
6866 ins_char(cap
->nchar
);
6868 if (cap
->ncharC1
!= 0)
6869 ins_char(cap
->ncharC1
);
6870 if (cap
->ncharC2
!= 0)
6871 ins_char(cap
->ncharC2
);
6878 * Replace the characters within one line.
6880 for (n
= cap
->count1
; n
> 0; --n
)
6883 * Get ptr again, because u_save and/or showmatch() will have
6884 * released the line. At the same time we let know that the
6885 * line will be changed.
6887 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
6888 ptr
[curwin
->w_cursor
.col
] = cap
->nchar
;
6889 if (p_sm
&& msg_silent
== 0)
6890 showmatch(cap
->nchar
);
6891 ++curwin
->w_cursor
.col
;
6893 #ifdef FEAT_NETBEANS_INTG
6896 colnr_T start
= (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
);
6898 netbeans_removed(curbuf
, curwin
->w_cursor
.lnum
, start
,
6900 netbeans_inserted(curbuf
, curwin
->w_cursor
.lnum
, start
,
6901 &ptr
[start
], (int)cap
->count1
);
6905 /* mark the buffer as changed and prepare for displaying */
6906 changed_bytes(curwin
->w_cursor
.lnum
,
6907 (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
));
6909 --curwin
->w_cursor
.col
; /* cursor on the last replaced char */
6911 /* if the character on the left of the current cursor is a multi-byte
6912 * character, move two characters left */
6916 curbuf
->b_op_end
= curwin
->w_cursor
;
6917 curwin
->w_set_curswant
= TRUE
;
6918 set_last_insert(cap
->nchar
);
6924 * 'o': Exchange start and end of Visual area.
6925 * 'O': same, but in block mode exchange left and right corners.
6928 v_swap_corners(cmdchar
)
6932 colnr_T left
, right
;
6934 if (cmdchar
== 'O' && VIsual_mode
== Ctrl_V
)
6936 old_cursor
= curwin
->w_cursor
;
6937 getvcols(curwin
, &old_cursor
, &VIsual
, &left
, &right
);
6938 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6940 VIsual
= curwin
->w_cursor
;
6942 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6943 curwin
->w_curswant
= right
;
6944 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6945 * right one column */
6946 if (old_cursor
.lnum
>= VIsual
.lnum
&& *p_sel
== 'e')
6947 ++curwin
->w_curswant
;
6948 coladvance(curwin
->w_curswant
);
6949 if (curwin
->w_cursor
.col
== old_cursor
.col
6950 #ifdef FEAT_VIRTUALEDIT
6951 && (!virtual_active()
6952 || curwin
->w_cursor
.coladd
== old_cursor
.coladd
)
6956 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6957 if (old_cursor
.lnum
<= VIsual
.lnum
&& *p_sel
== 'e')
6960 VIsual
= curwin
->w_cursor
;
6962 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6964 curwin
->w_curswant
= left
;
6969 old_cursor
= curwin
->w_cursor
;
6970 curwin
->w_cursor
= VIsual
;
6971 VIsual
= old_cursor
;
6972 curwin
->w_set_curswant
= TRUE
;
6975 #endif /* FEAT_VISUAL */
6978 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6985 if (VIsual_active
) /* "R" is replace lines */
6994 if (!checkclearopq(cap
->oap
))
6996 if (!curbuf
->b_p_ma
)
6997 EMSG(_(e_modifiable
));
7000 #ifdef FEAT_VIRTUALEDIT
7001 if (virtual_active())
7002 coladvance(getviscol());
7004 invoke_edit(cap
, FALSE
, cap
->arg
? 'V' : 'R', FALSE
);
7009 #ifdef FEAT_VREPLACE
7021 cap
->nchar
= cap
->extra_char
;
7022 nv_replace(cap
); /* Do same as "r" in Visual mode for now */
7026 if (!checkclearopq(cap
->oap
))
7028 if (!curbuf
->b_p_ma
)
7029 EMSG(_(e_modifiable
));
7032 if (cap
->extra_char
== Ctrl_V
) /* get another character */
7033 cap
->extra_char
= get_literal();
7034 stuffcharReadbuff(cap
->extra_char
);
7035 stuffcharReadbuff(ESC
);
7036 # ifdef FEAT_VIRTUALEDIT
7037 if (virtual_active())
7038 coladvance(getviscol());
7040 invoke_edit(cap
, TRUE
, 'v', FALSE
);
7047 * Swap case for "~" command, when it does not work like an operator.
7056 #ifdef FEAT_NETBEANS_INTG
7062 if (checkclearopq(cap
->oap
))
7065 if (lineempty(curwin
->w_cursor
.lnum
) && vim_strchr(p_ww
, '~') == NULL
)
7067 clearopbeep(cap
->oap
);
7073 if (u_save_cursor() == FAIL
)
7076 startpos
= curwin
->w_cursor
;
7077 #ifdef FEAT_NETBEANS_INTG
7080 for (n
= cap
->count1
; n
> 0; --n
)
7082 did_change
|= swapchar(cap
->oap
->op_type
, &curwin
->w_cursor
);
7084 if (gchar_cursor() == NUL
)
7086 if (vim_strchr(p_ww
, '~') != NULL
7087 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
7089 #ifdef FEAT_NETBEANS_INTG
7094 ptr
= ml_get(pos
.lnum
);
7095 count
= (int)STRLEN(ptr
) - pos
.col
;
7096 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
,
7098 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
7099 &ptr
[pos
.col
], count
);
7105 ++curwin
->w_cursor
.lnum
;
7106 curwin
->w_cursor
.col
= 0;
7109 if (u_savesub(curwin
->w_cursor
.lnum
) == FAIL
)
7118 #ifdef FEAT_NETBEANS_INTG
7119 if (did_change
&& usingNetbeans
)
7121 ptr
= ml_get(pos
.lnum
);
7122 count
= curwin
->w_cursor
.col
- pos
.col
;
7123 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
7124 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
, &ptr
[pos
.col
], count
);
7130 curwin
->w_set_curswant
= TRUE
;
7133 changed_lines(startpos
.lnum
, startpos
.col
, curwin
->w_cursor
.lnum
+ 1,
7135 curbuf
->b_op_start
= startpos
;
7136 curbuf
->b_op_end
= curwin
->w_cursor
;
7137 if (curbuf
->b_op_end
.col
> 0)
7138 --curbuf
->b_op_end
.col
;
7143 * Move cursor to mark.
7146 nv_cursormark(cap
, flag
, pos
)
7151 if (check_mark(pos
) == FAIL
)
7155 if (cap
->cmdchar
== '\''
7156 || cap
->cmdchar
== '`'
7157 || cap
->cmdchar
== '['
7158 || cap
->cmdchar
== ']')
7160 curwin
->w_cursor
= *pos
;
7162 beginline(BL_WHITE
| BL_FIX
);
7166 cap
->oap
->motion_type
= flag
? MLINE
: MCHAR
;
7167 if (cap
->cmdchar
== '`')
7168 cap
->oap
->use_reg_one
= TRUE
;
7169 cap
->oap
->inclusive
= FALSE
; /* ignored if not MCHAR */
7170 curwin
->w_set_curswant
= TRUE
;
7175 * Handle commands that are operators in Visual mode.
7181 static char_u trans
[] = "YyDdCcxdXdAAIIrr";
7183 /* Uppercase means linewise, except in block mode, then "D" deletes till
7184 * the end of the line, and "C" replaces til EOL */
7185 if (isupper(cap
->cmdchar
))
7187 if (VIsual_mode
!= Ctrl_V
)
7189 else if (cap
->cmdchar
== 'C' || cap
->cmdchar
== 'D')
7190 curwin
->w_curswant
= MAXCOL
;
7192 cap
->cmdchar
= *(vim_strchr(trans
, cap
->cmdchar
) + 1);
7198 * "s" and "S" commands.
7205 if (VIsual_active
) /* "vs" and "vS" are the same as "vc" */
7207 if (cap
->cmdchar
== 'S')
7218 * Abbreviated commands.
7224 if (cap
->cmdchar
== K_DEL
|| cap
->cmdchar
== K_KDEL
)
7225 cap
->cmdchar
= 'x'; /* DEL key behaves like 'x' */
7228 /* in Visual mode these commands are operators */
7237 * Translate a command into another command.
7243 static char_u
*(ar
[8]) = {(char_u
*)"dl", (char_u
*)"dh",
7244 (char_u
*)"d$", (char_u
*)"c$",
7245 (char_u
*)"cl", (char_u
*)"cc",
7246 (char_u
*)"yy", (char_u
*)":s\r"};
7247 static char_u
*str
= (char_u
*)"xXDCsSY&";
7249 if (!checkclearopq(cap
->oap
))
7251 /* In Vi "2D" doesn't delete the next line. Can't translate it
7252 * either, because "2." should also not use the count. */
7253 if (cap
->cmdchar
== 'D' && vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
7255 cap
->oap
->start
= curwin
->w_cursor
;
7256 cap
->oap
->op_type
= OP_DELETE
;
7258 set_op_var(OP_DELETE
);
7264 AppendCharToRedobuff('D');
7269 stuffnumReadbuff(cap
->count0
);
7270 stuffReadbuff(ar
[(int)(vim_strchr(str
, cap
->cmdchar
) - str
)]);
7277 * "'" and "`" commands. Also for "g'" and "g`".
7278 * cap->arg is TRUE for "'" and "g'".
7287 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7288 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7291 if (cap
->cmdchar
== 'g')
7292 c
= cap
->extra_char
;
7295 pos
= getmark(c
, (cap
->oap
->op_type
== OP_NOP
));
7296 if (pos
== (pos_T
*)-1) /* jumped to other file */
7300 check_cursor_lnum();
7301 beginline(BL_WHITE
| BL_FIX
);
7307 nv_cursormark(cap
, cap
->arg
, pos
);
7309 #ifdef FEAT_VIRTUALEDIT
7310 /* May need to clear the coladd that a mark includes. */
7311 if (!virtual_active())
7312 curwin
->w_cursor
.coladd
= 0;
7315 if (cap
->oap
->op_type
== OP_NOP
7316 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7317 && (fdo_flags
& FDO_MARK
)
7324 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7330 #ifdef FEAT_JUMPLIST
7332 # ifdef FEAT_FOLDING
7333 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7334 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7337 if (!checkclearopq(cap
->oap
))
7339 if (cap
->cmdchar
== 'g')
7340 pos
= movechangelist((int)cap
->count1
);
7342 pos
= movemark((int)cap
->count1
);
7343 if (pos
== (pos_T
*)-1) /* jump to other file */
7345 curwin
->w_set_curswant
= TRUE
;
7348 else if (pos
!= NULL
) /* can jump */
7349 nv_cursormark(cap
, FALSE
, pos
);
7350 else if (cap
->cmdchar
== 'g')
7352 if (curbuf
->b_changelistlen
== 0)
7353 EMSG(_("E664: changelist is empty"));
7354 else if (cap
->count1
< 0)
7355 EMSG(_("E662: At start of changelist"));
7357 EMSG(_("E663: At end of changelist"));
7360 clearopbeep(cap
->oap
);
7361 # ifdef FEAT_FOLDING
7362 if (cap
->oap
->op_type
== OP_NOP
7363 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7364 && (fdo_flags
& FDO_MARK
)
7370 clearopbeep(cap
->oap
);
7375 * Handle '"' command.
7381 if (checkclearop(cap
->oap
))
7384 if (cap
->nchar
== '=')
7385 cap
->nchar
= get_expr_register();
7387 if (cap
->nchar
!= NUL
&& valid_yank_reg(cap
->nchar
, FALSE
))
7389 cap
->oap
->regname
= cap
->nchar
;
7390 cap
->opcount
= cap
->count0
; /* remember count before '"' */
7392 set_reg_var(cap
->oap
->regname
);
7396 clearopbeep(cap
->oap
);
7401 * Handle "v", "V" and "CTRL-V" commands.
7402 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7404 * Handle CTRL-Q just like CTRL-V.
7410 if (cap
->cmdchar
== Ctrl_Q
)
7411 cap
->cmdchar
= Ctrl_V
;
7413 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7414 * characterwise, linewise, or blockwise. */
7415 if (cap
->oap
->op_type
!= OP_NOP
)
7417 cap
->oap
->motion_force
= cap
->cmdchar
;
7418 finish_op
= FALSE
; /* operator doesn't finish now but later */
7422 VIsual_select
= cap
->arg
;
7423 if (VIsual_active
) /* change Visual mode */
7425 if (VIsual_mode
== cap
->cmdchar
) /* stop visual mode */
7427 else /* toggle char/block mode */
7428 { /* or char/line mode */
7429 VIsual_mode
= cap
->cmdchar
;
7432 redraw_curbuf_later(INVERTED
); /* update the inversion */
7434 else /* start Visual mode */
7436 check_visual_highlight();
7437 if (cap
->count0
) /* use previously selected part */
7439 if (resel_VIsual_mode
== NUL
) /* there is none */
7444 VIsual
= curwin
->w_cursor
;
7446 VIsual_active
= TRUE
;
7447 VIsual_reselect
= TRUE
;
7449 /* start Select mode when 'selectmode' contains "cmd" */
7450 may_start_select('c');
7454 if (p_smd
&& msg_silent
== 0)
7455 redraw_cmdline
= TRUE
; /* show visual mode later */
7457 * For V and ^V, we multiply the number of lines even if there
7458 * was only one -- webb
7460 if (resel_VIsual_mode
!= 'v' || resel_VIsual_line_count
> 1)
7462 curwin
->w_cursor
.lnum
+=
7463 resel_VIsual_line_count
* cap
->count0
- 1;
7464 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
7465 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
7467 VIsual_mode
= resel_VIsual_mode
;
7468 if (VIsual_mode
== 'v')
7470 if (resel_VIsual_line_count
<= 1)
7471 curwin
->w_cursor
.col
+= resel_VIsual_col
* cap
->count0
- 1;
7473 curwin
->w_cursor
.col
= resel_VIsual_col
;
7476 if (resel_VIsual_col
== MAXCOL
)
7478 curwin
->w_curswant
= MAXCOL
;
7479 coladvance((colnr_T
)MAXCOL
);
7481 else if (VIsual_mode
== Ctrl_V
)
7484 curwin
->w_curswant
= curwin
->w_virtcol
7485 + resel_VIsual_col
* cap
->count0
- 1;
7486 coladvance(curwin
->w_curswant
);
7489 curwin
->w_set_curswant
= TRUE
;
7490 redraw_curbuf_later(INVERTED
); /* show the inversion */
7495 /* start Select mode when 'selectmode' contains "cmd" */
7496 may_start_select('c');
7497 n_start_visual_mode(cap
->cmdchar
);
7503 * Start selection for Shift-movement keys.
7508 /* if 'selectmode' contains "key", start Select mode */
7509 may_start_select('k');
7510 n_start_visual_mode('v');
7514 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7520 VIsual_select
= (stuff_empty() && typebuf_typed()
7521 && (vim_strchr(p_slm
, c
) != NULL
));
7525 * Start Visual mode "c".
7526 * Should set VIsual_select before calling this.
7529 n_start_visual_mode(c
)
7533 VIsual_active
= TRUE
;
7534 VIsual_reselect
= TRUE
;
7535 #ifdef FEAT_VIRTUALEDIT
7536 /* Corner case: the 0 position in a tab may change when going into
7537 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7539 if (c
== Ctrl_V
&& (ve_flags
& VE_BLOCK
) && gchar_cursor() == TAB
)
7540 coladvance(curwin
->w_virtcol
);
7542 VIsual
= curwin
->w_cursor
;
7551 if (p_smd
&& msg_silent
== 0)
7552 redraw_cmdline
= TRUE
; /* show visual mode later */
7553 #ifdef FEAT_CLIPBOARD
7554 /* Make sure the clipboard gets updated. Needed because start and
7555 * end may still be the same, and the selection needs to be owned */
7556 clip_star
.vmode
= NUL
;
7559 /* Only need to redraw this line, unless still need to redraw an old
7560 * Visual area (when 'lazyredraw' is set). */
7561 if (curwin
->w_redr_type
< INVERTED
)
7563 curwin
->w_old_cursor_lnum
= curwin
->w_cursor
.lnum
;
7564 curwin
->w_old_visual_lnum
= curwin
->w_cursor
.lnum
;
7568 #endif /* FEAT_VISUAL */
7571 * CTRL-W: Window commands
7578 if (!checkclearop(cap
->oap
))
7579 do_window(cap
->nchar
, cap
->count0
, NUL
); /* everything is in window.c */
7581 (void)checkclearop(cap
->oap
);
7595 end_visual_mode(); /* stop Visual mode */
7597 do_cmdline_cmd((char_u
*)"st");
7601 * Commands starting with "g".
7607 oparg_T
*oap
= cap
->oap
;
7618 * "g^A": dump log of used memory.
7621 vim_mem_profile_dump();
7625 #ifdef FEAT_VREPLACE
7627 * "gR": Enter virtual replace mode.
7640 do_cmdline_cmd((char_u
*)"%s//~/&");
7645 * "gv": Reselect the previous Visual area. If Visual already active,
7646 * exchange previous and current Visual area.
7649 if (checkclearop(oap
))
7652 if ( curbuf
->b_visual
.vi_start
.lnum
== 0
7653 || curbuf
->b_visual
.vi_start
.lnum
> curbuf
->b_ml
.ml_line_count
7654 || curbuf
->b_visual
.vi_end
.lnum
== 0)
7658 /* set w_cursor to the start of the Visual area, tpos to the end */
7662 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7663 curbuf
->b_visual
.vi_mode
= i
;
7665 curbuf
->b_visual_mode_eval
= i
;
7667 i
= curwin
->w_curswant
;
7668 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7669 curbuf
->b_visual
.vi_curswant
= i
;
7671 tpos
= curbuf
->b_visual
.vi_end
;
7672 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
7673 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7674 curbuf
->b_visual
.vi_start
= VIsual
;
7678 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7679 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7680 tpos
= curbuf
->b_visual
.vi_end
;
7681 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7684 VIsual_active
= TRUE
;
7685 VIsual_reselect
= TRUE
;
7687 /* Set Visual to the start and w_cursor to the end of the Visual
7688 * area. Make sure they are on an existing character. */
7690 VIsual
= curwin
->w_cursor
;
7691 curwin
->w_cursor
= tpos
;
7695 * When called from normal "g" command: start Select mode when
7696 * 'selectmode' contains "cmd". When called for K_SELECT, always
7697 * start Select mode.
7700 VIsual_select
= TRUE
;
7702 may_start_select('c');
7706 #ifdef FEAT_CLIPBOARD
7707 /* Make sure the clipboard gets updated. Needed because start and
7708 * end are still the same, and the selection needs to be owned */
7709 clip_star
.vmode
= NUL
;
7711 redraw_curbuf_later(INVERTED
);
7716 * "gV": Don't reselect the previous Visual area after a Select mode
7720 VIsual_reselect
= FALSE
;
7724 * "gh": start Select mode.
7725 * "gH": start Select line mode.
7726 * "g^H": start Select block mode.
7729 cap
->nchar
= Ctrl_H
;
7735 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7736 if (cap
->nchar
== Ctrl_H
)
7737 cap
->cmdchar
= Ctrl_V
;
7740 cap
->cmdchar
= cap
->nchar
+ ('v' - 'h');
7744 #endif /* FEAT_VISUAL */
7747 * "gj" and "gk" two new funny movement keys -- up and down
7748 * movement based on *screen* line rather than *file* line.
7752 /* with 'nowrap' it works just like the normal "j" command; also when
7753 * in a closed fold */
7754 if (!curwin
->w_p_wrap
7756 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7760 oap
->motion_type
= MLINE
;
7761 i
= cursor_down(cap
->count1
, oap
->op_type
== OP_NOP
);
7764 i
= nv_screengo(oap
, FORWARD
, cap
->count1
);
7771 /* with 'nowrap' it works just like the normal "k" command; also when
7772 * in a closed fold */
7773 if (!curwin
->w_p_wrap
7775 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7779 oap
->motion_type
= MLINE
;
7780 i
= cursor_up(cap
->count1
, oap
->op_type
== OP_NOP
);
7783 i
= nv_screengo(oap
, BACKWARD
, cap
->count1
);
7789 * "gJ": join two lines without inserting a space.
7796 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7797 * "gm": middle of "g0" and "g$".
7807 oap
->motion_type
= MCHAR
;
7808 oap
->inclusive
= FALSE
;
7809 if (curwin
->w_p_wrap
7810 #ifdef FEAT_VERTSPLIT
7811 && curwin
->w_width
!= 0
7815 int width1
= W_WIDTH(curwin
) - curwin_col_off();
7816 int width2
= width1
+ curwin_col_off2();
7820 if (curwin
->w_virtcol
>= (colnr_T
)width1
&& width2
> 0)
7821 i
= (curwin
->w_virtcol
- width1
) / width2
* width2
+ width1
;
7824 i
= curwin
->w_leftcol
;
7825 /* Go to the middle of the screen line. When 'number' is on and lines
7826 * are wrapping the middle can be more to the left.*/
7827 if (cap
->nchar
== 'm')
7828 i
+= (W_WIDTH(curwin
) - curwin_col_off()
7829 + ((curwin
->w_p_wrap
&& i
> 0)
7830 ? curwin_col_off2() : 0)) / 2;
7831 coladvance((colnr_T
)i
);
7836 while (vim_iswhite(i
) && oneright() == OK
);
7838 curwin
->w_set_curswant
= TRUE
;
7842 /* "g_": to the last non-blank character in the line or <count> lines
7844 cap
->oap
->motion_type
= MCHAR
;
7845 cap
->oap
->inclusive
= TRUE
;
7846 curwin
->w_curswant
= MAXCOL
;
7847 if (cursor_down((long)(cap
->count1
- 1),
7848 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
7849 clearopbeep(cap
->oap
);
7852 char_u
*ptr
= ml_get_curline();
7854 /* In Visual mode we may end up after the line. */
7855 if (curwin
->w_cursor
.col
> 0 && ptr
[curwin
->w_cursor
.col
] == NUL
)
7856 --curwin
->w_cursor
.col
;
7858 /* Decrease the cursor column until it's on a non-blank. */
7859 while (curwin
->w_cursor
.col
> 0
7860 && vim_iswhite(ptr
[curwin
->w_cursor
.col
]))
7861 --curwin
->w_cursor
.col
;
7862 curwin
->w_set_curswant
= TRUE
;
7870 int col_off
= curwin_col_off();
7872 oap
->motion_type
= MCHAR
;
7873 oap
->inclusive
= TRUE
;
7874 if (curwin
->w_p_wrap
7875 #ifdef FEAT_VERTSPLIT
7876 && curwin
->w_width
!= 0
7880 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
7881 if (cap
->count1
== 1)
7883 int width1
= W_WIDTH(curwin
) - col_off
;
7884 int width2
= width1
+ curwin_col_off2();
7888 if (curwin
->w_virtcol
>= (colnr_T
)width1
)
7889 i
+= ((curwin
->w_virtcol
- width1
) / width2
+ 1)
7891 coladvance((colnr_T
)i
);
7892 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7893 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
7896 * Check for landing on a character that got split at
7897 * the end of the line. We do not want to advance to
7898 * the next screen line.
7901 if (curwin
->w_virtcol
> (colnr_T
)i
)
7902 --curwin
->w_cursor
.col
;
7906 else if (nv_screengo(oap
, FORWARD
, cap
->count1
- 1) == FAIL
)
7911 i
= curwin
->w_leftcol
+ W_WIDTH(curwin
) - col_off
- 1;
7912 coladvance((colnr_T
)i
);
7914 /* Make sure we stick in this column. */
7916 curwin
->w_curswant
= curwin
->w_virtcol
;
7917 curwin
->w_set_curswant
= FALSE
;
7923 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7928 case POUND
: /* pound sign (sometimes equal to '#') */
7930 case Ctrl_RSB
: /* :tag or :tselect for current identifier */
7931 case ']': /* :tselect for current identifier */
7936 * ge and gE: go back to end of word
7940 oap
->motion_type
= MCHAR
;
7941 curwin
->w_set_curswant
= TRUE
;
7942 oap
->inclusive
= TRUE
;
7943 if (bckend_word(cap
->count1
, cap
->nchar
== 'E', FALSE
) == FAIL
)
7948 * "g CTRL-G": display info about cursor position
7955 * "gi": start Insert at the last position.
7958 if (curbuf
->b_last_insert
.lnum
!= 0)
7960 curwin
->w_cursor
= curbuf
->b_last_insert
;
7961 check_cursor_lnum();
7962 i
= (int)STRLEN(ml_get_curline());
7963 if (curwin
->w_cursor
.col
> (colnr_T
)i
)
7965 #ifdef FEAT_VIRTUALEDIT
7966 if (virtual_active())
7967 curwin
->w_cursor
.coladd
+= curwin
->w_cursor
.col
- i
;
7969 curwin
->w_cursor
.col
= i
;
7977 * "gI": Start insert in column 1.
7981 if (!checkclearopq(oap
))
7982 invoke_edit(cap
, FALSE
, 'g', FALSE
);
7985 #ifdef FEAT_SEARCHPATH
7987 * "gf": goto file, edit file under cursor
7988 * "]f" and "[f": can also be used.
7996 /* "g'm" and "g`m": jump to mark without setting pcmark */
8008 do_sleep(cap
->count1
* 1000L);
8012 * "ga": Display the ascii value of the character under the
8013 * cursor. It is displayed in decimal, hex, and octal. -- webb
8021 * "g8": Display the bytes used for the UTF-8 character under the
8022 * cursor. It is displayed in hex.
8023 * "8g8" finds illegal byte sequence.
8026 if (cap
->count0
== 8)
8038 * "gg": Goto the first line in file. With a count it goes to
8039 * that line number like for "G". -- webb
8047 * Two-character operators:
8049 * "gw" Format text and keep cursor position
8050 * "g~" Toggle the case of the text.
8051 * "gu" Change text to lower case.
8052 * "gU" Change text to upper case.
8053 * "g?" rot13 encoding
8054 * "g@" call 'operatorfunc'
8058 oap
->cursor_start
= curwin
->w_cursor
;
8069 * "gd": Find first occurrence of pattern under the cursor in the
8071 * "gD": idem, but in the current file.
8075 nv_gd(oap
, cap
->nchar
, (int)cap
->count0
);
8080 * g<*Mouse> : <C-*mouse>
8084 case K_MIDDLERELEASE
:
8090 case K_RIGHTRELEASE
:
8097 mod_mask
= MOD_MASK_CTRL
;
8098 (void)do_mouse(oap
, cap
->nchar
, BACKWARD
, cap
->count1
, 0);
8106 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8114 /* "go": goto byte count from start of buffer */
8116 goto_byte(cap
->count0
);
8120 /* "gQ": improved Ex mode */
8124 clearopbeep(cap
->oap
);
8129 if (!checkclearopq(oap
))
8133 #ifdef FEAT_JUMPLIST
8139 cap
->count1
= -cap
->count1
;
8146 goto_tabpage((int)cap
->count0
);
8149 goto_tabpage(-(int)cap
->count1
);
8154 case '-': /* "g+" and "g-": undo or redo along the timeline */
8155 if (!checkclearopq(oap
))
8156 undo_time(cap
->nchar
== '-' ? -cap
->count1
: cap
->count1
,
8167 * Handle "o" and "O" commands.
8173 if (!checkclearopq(cap
->oap
))
8176 if (cap
->cmdchar
== 'O')
8177 /* Open above the first line of a folded sequence of lines */
8178 (void)hasFolding(curwin
->w_cursor
.lnum
,
8179 &curwin
->w_cursor
.lnum
, NULL
);
8181 /* Open below the last line of a folded sequence of lines */
8182 (void)hasFolding(curwin
->w_cursor
.lnum
,
8183 NULL
, &curwin
->w_cursor
.lnum
);
8185 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
-
8186 (cap
->cmdchar
== 'O' ? 1 : 0)),
8187 (linenr_T
)(curwin
->w_cursor
.lnum
+
8188 (cap
->cmdchar
== 'o' ? 1 : 0))
8190 && open_line(cap
->cmdchar
== 'O' ? BACKWARD
: FORWARD
,
8191 #ifdef FEAT_COMMENTS
8192 has_format_option(FO_OPEN_COMS
) ? OPENLINE_DO_COM
:
8196 /* When '#' is in 'cpoptions' ignore the count. */
8197 if (vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
8199 invoke_edit(cap
, FALSE
, cap
->cmdchar
, TRUE
);
8205 * "." command: redo last change.
8211 if (!checkclearopq(cap
->oap
))
8214 * If "restart_edit" is TRUE, the last but one command is repeated
8215 * instead of the last command (inserting text). This is used for
8216 * CTRL-O <.> in insert mode.
8218 if (start_redo(cap
->count0
, restart_edit
!= 0 && !arrow_used
) == FAIL
)
8219 clearopbeep(cap
->oap
);
8230 if (!checkclearopq(cap
->oap
))
8232 u_redo((int)cap
->count1
);
8233 curwin
->w_set_curswant
= TRUE
;
8238 * Handle "U" command.
8244 /* In Visual mode and typing "gUU" triggers an operator */
8245 if (cap
->oap
->op_type
== OP_UPPER
8251 /* translate "gUU" to "gUgU" */
8256 else if (!checkclearopq(cap
->oap
))
8259 curwin
->w_set_curswant
= TRUE
;
8264 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8275 && cap
->oap
->op_type
!= OP_TILDE
)
8282 * Handle an operator command.
8283 * The actual work is done by do_pending_operator().
8291 op_type
= get_op_type(cap
->cmdchar
, cap
->nchar
);
8293 if (op_type
== cap
->oap
->op_type
) /* double operator works on lines */
8295 else if (!checkclearop(cap
->oap
))
8297 cap
->oap
->start
= curwin
->w_cursor
;
8298 cap
->oap
->op_type
= op_type
;
8300 set_op_var(op_type
);
8307 * Set v:operator to the characters for "optype".
8315 if (optype
== OP_NOP
)
8316 set_vim_var_string(VV_OP
, NULL
, 0);
8319 opchars
[0] = get_op_char(optype
);
8320 opchars
[1] = get_extra_op_char(optype
);
8322 set_vim_var_string(VV_OP
, opchars
, -1);
8328 * Handle linewise operator "dd", "yy", etc.
8330 * "_" is is a strange motion command that helps make operators more logical.
8331 * It is actually implemented, but not documented in the real Vi. This motion
8332 * command actually refers to "the current line". Commands like "dd" and "yy"
8333 * are really an alternate form of "d_" and "y_". It does accept a count, so
8334 * "d3_" works to delete 3 lines.
8340 cap
->oap
->motion_type
= MLINE
;
8341 if (cursor_down(cap
->count1
- 1L, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
8342 clearopbeep(cap
->oap
);
8343 else if ( cap
->oap
->op_type
== OP_DELETE
8344 || cap
->oap
->op_type
== OP_LSHIFT
8345 || cap
->oap
->op_type
== OP_RSHIFT
)
8346 beginline(BL_SOL
| BL_FIX
);
8347 else if (cap
->oap
->op_type
!= OP_YANK
) /* 'Y' does not move cursor */
8348 beginline(BL_WHITE
| BL_FIX
);
8358 /* CTRL-HOME is like "gg" */
8359 if (mod_mask
& MOD_MASK_CTRL
)
8366 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8367 one-character line). */
8377 cap
->oap
->motion_type
= MCHAR
;
8378 cap
->oap
->inclusive
= FALSE
;
8380 if (cap
->count0
> 0)
8382 coladvance((colnr_T
)(cap
->count0
- 1));
8383 curwin
->w_curswant
= (colnr_T
)(cap
->count0
- 1);
8386 curwin
->w_curswant
= 0;
8387 /* keep curswant at the column where we wanted to go, not where
8388 we ended; differs if line is too short */
8389 curwin
->w_set_curswant
= FALSE
;
8393 * Handle back-word command "b" and "B".
8394 * cap->arg is 1 for "B"
8400 cap
->oap
->motion_type
= MCHAR
;
8401 cap
->oap
->inclusive
= FALSE
;
8402 curwin
->w_set_curswant
= TRUE
;
8403 if (bck_word(cap
->count1
, cap
->arg
, FALSE
) == FAIL
)
8404 clearopbeep(cap
->oap
);
8406 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8412 * Handle word motion commands "e", "E", "w" and "W".
8413 * cap->arg is TRUE for "E" and "W".
8422 pos_T startpos
= curwin
->w_cursor
;
8425 * Set inclusive for the "E" and "e" command.
8427 if (cap
->cmdchar
== 'e' || cap
->cmdchar
== 'E')
8431 cap
->oap
->inclusive
= word_end
;
8434 * "cw" and "cW" are a special case.
8436 if (!word_end
&& cap
->oap
->op_type
== OP_CHANGE
)
8439 if (n
!= NUL
) /* not an empty line */
8444 * Reproduce a funny Vi behaviour: "cw" on a blank only
8445 * changes one character, not all blanks until the start of
8446 * the next word. Only do this when the 'w' flag is included
8449 if (cap
->count1
== 1 && vim_strchr(p_cpo
, CPO_CW
) != NULL
)
8451 cap
->oap
->inclusive
= TRUE
;
8452 cap
->oap
->motion_type
= MCHAR
;
8459 * This is a little strange. To match what the real Vi does,
8460 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8461 * that we are not on a space or a TAB. This seems impolite
8462 * at first, but it's really more what we mean when we say
8464 * Another strangeness: When standing on the end of a word
8465 * "ce" will change until the end of the next wordt, but "cw"
8466 * will change only one character! This is done by setting
8469 cap
->oap
->inclusive
= TRUE
;
8476 cap
->oap
->motion_type
= MCHAR
;
8477 curwin
->w_set_curswant
= TRUE
;
8479 n
= end_word(cap
->count1
, cap
->arg
, flag
, FALSE
);
8481 n
= fwd_word(cap
->count1
, cap
->arg
, cap
->oap
->op_type
!= OP_NOP
);
8483 /* Don't leave the cursor on the NUL past the end of line. Unless we
8484 * didn't move it forward. */
8485 if (lt(startpos
, curwin
->w_cursor
))
8486 adjust_cursor(cap
->oap
);
8488 if (n
== FAIL
&& cap
->oap
->op_type
== OP_NOP
)
8489 clearopbeep(cap
->oap
);
8493 adjust_for_sel(cap
);
8496 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8503 * Used after a movement command: If the cursor ends up on the NUL after the
8504 * end of the line, may move it back to the last character and make the motion
8511 /* The cursor cannot remain on the NUL when:
8512 * - the column is > 0
8513 * - not in Visual mode or 'selection' is "o"
8514 * - 'virtualedit' is not "all" and not "onemore".
8516 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
8518 && (!VIsual_active
|| *p_sel
== 'o')
8520 #ifdef FEAT_VIRTUALEDIT
8521 && !virtual_active() && (ve_flags
& VE_ONEMORE
) == 0
8525 --curwin
->w_cursor
.col
;
8527 /* prevent cursor from moving on the trail byte */
8531 oap
->inclusive
= TRUE
;
8536 * "0" and "^" commands.
8537 * cap->arg is the argument for beginline().
8543 cap
->oap
->motion_type
= MCHAR
;
8544 cap
->oap
->inclusive
= FALSE
;
8545 beginline(cap
->arg
);
8547 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8550 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8551 one-character line). */
8556 * In exclusive Visual mode, may include the last character.
8562 if (VIsual_active
&& cap
->oap
->inclusive
&& *p_sel
== 'e'
8563 && gchar_cursor() != NUL
&& lt(VIsual
, curwin
->w_cursor
))
8570 ++curwin
->w_cursor
.col
;
8571 cap
->oap
->inclusive
= FALSE
;
8576 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8577 * Should check VIsual_mode before calling this.
8578 * Returns TRUE when backed up to the previous line.
8585 if (*p_sel
== 'e' && !equalpos(VIsual
, curwin
->w_cursor
))
8587 if (lt(VIsual
, curwin
->w_cursor
))
8588 pp
= &curwin
->w_cursor
;
8591 #ifdef FEAT_VIRTUALEDIT
8603 else if (pp
->lnum
> 1)
8606 pp
->col
= (colnr_T
)STRLEN(ml_get(pp
->lnum
));
8614 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8621 VIsual_select
= TRUE
;
8622 else if (VIsual_reselect
)
8624 cap
->nchar
= 'v'; /* fake "gv" command */
8633 * "G", "gg", CTRL-END, CTRL-HOME.
8634 * cap->arg is TRUE for "G".
8643 lnum
= curbuf
->b_ml
.ml_line_count
;
8646 cap
->oap
->motion_type
= MLINE
;
8649 /* When a count is given, use it instead of the default lnum */
8650 if (cap
->count0
!= 0)
8654 else if (lnum
> curbuf
->b_ml
.ml_line_count
)
8655 lnum
= curbuf
->b_ml
.ml_line_count
;
8656 curwin
->w_cursor
.lnum
= lnum
;
8657 beginline(BL_SOL
| BL_FIX
);
8659 if ((fdo_flags
& FDO_JUMP
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8665 * CTRL-\ in Normal mode.
8671 if (cap
->nchar
== Ctrl_N
|| cap
->nchar
== Ctrl_G
)
8674 if (restart_edit
!= 0 && mode_displayed
)
8675 clear_cmdline
= TRUE
; /* unshow mode later */
8678 if (cmdwin_type
!= 0)
8679 cmdwin_result
= Ctrl_C
;
8684 end_visual_mode(); /* stop Visual */
8685 redraw_curbuf_later(INVERTED
);
8688 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8689 if (cap
->nchar
== Ctrl_G
&& p_im
)
8693 clearopbeep(cap
->oap
);
8697 * ESC in Normal mode: beep, but don't flush buffers.
8698 * Don't even beep if we are canceling a command.
8706 no_reason
= (cap
->oap
->op_type
== OP_NOP
8707 && cap
->opcount
== 0
8709 && cap
->oap
->regname
== 0
8712 if (cap
->arg
) /* TRUE for CTRL-C */
8714 if (restart_edit
== 0
8722 MSG(_("Type :quit<Enter> to exit Vim"));
8724 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8725 * set again below when halfway a mapping. */
8729 if (cmdwin_type
!= 0)
8731 cmdwin_result
= K_IGNORE
;
8732 got_int
= FALSE
; /* don't stop executing autocommands et al. */
8741 end_visual_mode(); /* stop Visual */
8742 check_cursor_col(); /* make sure cursor is not beyond EOL */
8743 curwin
->w_set_curswant
= TRUE
;
8744 redraw_curbuf_later(INVERTED
);
8752 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8753 * set return to Insert mode afterwards. */
8754 if (restart_edit
== 0 && goto_im()
8755 #ifdef FEAT_EX_EXTRA
8756 && ex_normal_busy
== 0
8763 * Handle "A", "a", "I", "i" and <Insert> commands.
8769 /* <Insert> is equal to "i" */
8770 if (cap
->cmdchar
== K_INS
|| cap
->cmdchar
== K_KINS
)
8774 /* in Visual mode "A" and "I" are an operator */
8775 if (VIsual_active
&& (cap
->cmdchar
== 'A' || cap
->cmdchar
== 'I'))
8778 /* in Visual mode and after an operator "a" and "i" are for text objects */
8781 if ((cap
->cmdchar
== 'a' || cap
->cmdchar
== 'i')
8782 && (cap
->oap
->op_type
!= OP_NOP
8791 clearopbeep(cap
->oap
);
8794 else if (!curbuf
->b_p_ma
&& !p_im
)
8796 /* Only give this error when 'insertmode' is off. */
8797 EMSG(_(e_modifiable
));
8800 else if (!checkclearopq(cap
->oap
))
8802 switch (cap
->cmdchar
)
8804 case 'A': /* "A"ppend after the line */
8805 curwin
->w_set_curswant
= TRUE
;
8806 #ifdef FEAT_VIRTUALEDIT
8807 if (ve_flags
== VE_ALL
)
8809 int save_State
= State
;
8811 /* Pretent Insert mode here to allow the cursor on the
8812 * character past the end of the line */
8814 coladvance((colnr_T
)MAXCOL
);
8819 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8822 case 'I': /* "I"nsert before the first non-blank */
8823 if (vim_strchr(p_cpo
, CPO_INSEND
) == NULL
)
8824 beginline(BL_WHITE
);
8826 beginline(BL_WHITE
|BL_FIX
);
8829 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8830 #ifdef FEAT_VIRTUALEDIT
8831 /* increment coladd when in virtual space, increment the
8832 * column otherwise, also to append after an unprintable char */
8833 if (virtual_active()
8834 && (curwin
->w_cursor
.coladd
> 0
8835 || *ml_get_cursor() == NUL
8836 || *ml_get_cursor() == TAB
))
8837 curwin
->w_cursor
.coladd
++;
8840 if (*ml_get_cursor() != NUL
)
8845 #ifdef FEAT_VIRTUALEDIT
8846 if (curwin
->w_cursor
.coladd
&& cap
->cmdchar
!= 'A')
8848 int save_State
= State
;
8850 /* Pretent Insert mode here to allow the cursor on the
8851 * character past the end of the line */
8853 coladvance(getviscol());
8858 invoke_edit(cap
, FALSE
, cap
->cmdchar
, FALSE
);
8863 * Invoke edit() and take care of "restart_edit" and the return value.
8866 invoke_edit(cap
, repl
, cmd
, startln
)
8868 int repl
; /* "r" or "gr" command */
8872 int restart_edit_save
= 0;
8874 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8875 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8877 if (repl
|| !stuff_empty())
8878 restart_edit_save
= restart_edit
;
8880 restart_edit_save
= 0;
8882 /* Always reset "restart_edit", this is not a restarted edit. */
8885 if (edit(cmd
, startln
, cap
->count1
))
8886 cap
->retval
|= CA_COMMAND_BUSY
;
8888 if (restart_edit
== 0)
8889 restart_edit
= restart_edit_save
;
8894 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8904 if (cap
->cmdchar
== 'i')
8905 include
= FALSE
; /* "ix" = inner object: exclude white space */
8907 include
= TRUE
; /* "ax" = an object: include white space */
8909 /* Make sure (), [], {} and <> are in 'matchpairs' */
8910 mps_save
= curbuf
->b_p_mps
;
8911 curbuf
->b_p_mps
= (char_u
*)"(:),{:},[:],<:>";
8915 case 'w': /* "aw" = a word */
8916 flag
= current_word(cap
->oap
, cap
->count1
, include
, FALSE
);
8918 case 'W': /* "aW" = a WORD */
8919 flag
= current_word(cap
->oap
, cap
->count1
, include
, TRUE
);
8921 case 'b': /* "ab" = a braces block */
8924 flag
= current_block(cap
->oap
, cap
->count1
, include
, '(', ')');
8926 case 'B': /* "aB" = a Brackets block */
8929 flag
= current_block(cap
->oap
, cap
->count1
, include
, '{', '}');
8931 case '[': /* "a[" = a [] block */
8933 flag
= current_block(cap
->oap
, cap
->count1
, include
, '[', ']');
8935 case '<': /* "a<" = a <> block */
8937 flag
= current_block(cap
->oap
, cap
->count1
, include
, '<', '>');
8939 case 't': /* "at" = a tag block (xml and html) */
8940 flag
= current_tagblock(cap
->oap
, cap
->count1
, include
);
8942 case 'p': /* "ap" = a paragraph */
8943 flag
= current_par(cap
->oap
, cap
->count1
, include
, 'p');
8945 case 's': /* "as" = a sentence */
8946 flag
= current_sent(cap
->oap
, cap
->count1
, include
);
8948 case '"': /* "a"" = a double quoted string */
8949 case '\'': /* "a'" = a single quoted string */
8950 case '`': /* "a`" = a backtick quoted string */
8951 flag
= current_quote(cap
->oap
, cap
->count1
, include
,
8955 case 'S': /* "aS" = a section */
8956 case 'f': /* "af" = a filename */
8957 case 'u': /* "au" = a URL */
8964 curbuf
->b_p_mps
= mps_save
;
8966 clearopbeep(cap
->oap
);
8967 adjust_cursor_col();
8968 curwin
->w_set_curswant
= TRUE
;
8973 * "q" command: Start/stop recording.
8974 * "q:", "q/", "q?": edit command-line in command-line window.
8980 if (cap
->oap
->op_type
== OP_FORMAT
)
8982 /* "gqq" is the same as "gqgq": format line */
8987 else if (!checkclearop(cap
->oap
))
8990 if (cap
->nchar
== ':' || cap
->nchar
== '/' || cap
->nchar
== '?')
8992 stuffcharReadbuff(cap
->nchar
);
8993 stuffcharReadbuff(K_CMDWIN
);
8997 /* (stop) recording into a named register, unless executing a
8999 if (!Exec_reg
&& do_record(cap
->nchar
) == FAIL
)
9000 clearopbeep(cap
->oap
);
9005 * Handle the "@r" command.
9011 if (checkclearop(cap
->oap
))
9014 if (cap
->nchar
== '=')
9016 if (get_expr_register() == NUL
)
9020 while (cap
->count1
-- && !got_int
)
9022 if (do_execreg(cap
->nchar
, FALSE
, FALSE
, FALSE
) == FAIL
)
9024 clearopbeep(cap
->oap
);
9032 * Handle the CTRL-U and CTRL-D commands.
9038 if ((cap
->cmdchar
== Ctrl_U
&& curwin
->w_cursor
.lnum
== 1)
9039 || (cap
->cmdchar
== Ctrl_D
9040 && curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
))
9041 clearopbeep(cap
->oap
);
9042 else if (!checkclearop(cap
->oap
))
9043 halfpage(cap
->cmdchar
== Ctrl_D
, cap
->count0
);
9047 * Handle "J" or "gJ" command.
9054 if (VIsual_active
) /* join the visual lines */
9058 if (!checkclearop(cap
->oap
))
9060 if (cap
->count0
<= 1)
9061 cap
->count0
= 2; /* default for join is two lines! */
9062 if (curwin
->w_cursor
.lnum
+ cap
->count0
- 1 >
9063 curbuf
->b_ml
.ml_line_count
)
9064 clearopbeep(cap
->oap
); /* beyond last line */
9067 prep_redo(cap
->oap
->regname
, cap
->count0
,
9068 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
9069 do_do_join(cap
->count0
, cap
->nchar
== NUL
);
9075 * "P", "gP", "p" and "gp" commands.
9083 void *reg1
= NULL
, *reg2
= NULL
;
9085 int was_visual
= FALSE
;
9090 if (cap
->oap
->op_type
!= OP_NOP
)
9093 /* "dp" is ":diffput" */
9094 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'p')
9097 nv_diffgetput(TRUE
);
9101 clearopbeep(cap
->oap
);
9105 dir
= (cap
->cmdchar
== 'P'
9106 || (cap
->cmdchar
== 'g' && cap
->nchar
== 'P'))
9107 ? BACKWARD
: FORWARD
;
9109 if (cap
->cmdchar
== 'g')
9110 flags
|= PUT_CURSEND
;
9115 /* Putting in Visual mode: The put text replaces the selected
9116 * text. First delete the selected text, then put the new text.
9117 * Need to save and restore the registers that the delete
9118 * overwrites if the old contents is being put.
9121 regname
= cap
->oap
->regname
;
9122 # ifdef FEAT_CLIPBOARD
9123 adjust_clip_reg(®name
);
9125 if (regname
== 0 || VIM_ISDIGIT(regname
)
9126 # ifdef FEAT_CLIPBOARD
9127 || (clip_unnamed
&& (regname
== '*' || regname
== '+'))
9132 /* the delete is going to overwrite the register we want to
9133 * put, save it first. */
9134 reg1
= get_register(regname
, TRUE
);
9137 /* Now delete the selected text. */
9140 cap
->oap
->regname
= NUL
;
9142 do_pending_operator(cap
, 0, FALSE
);
9143 empty
= (curbuf
->b_ml
.ml_flags
& ML_EMPTY
);
9145 /* delete PUT_LINE_BACKWARD; */
9146 cap
->oap
->regname
= regname
;
9150 /* Delete probably changed the register we want to put, save
9151 * it first. Then put back what was there before the delete. */
9152 reg2
= get_register(regname
, FALSE
);
9153 put_register(regname
, reg1
);
9156 /* When deleted a linewise Visual area, put the register as
9157 * lines to avoid it joined with the next line. When deletion was
9158 * characterwise, split a line when putting lines. */
9159 if (VIsual_mode
== 'V')
9161 else if (VIsual_mode
== 'v')
9162 flags
|= PUT_LINE_SPLIT
;
9163 if (VIsual_mode
== Ctrl_V
&& dir
== FORWARD
)
9164 flags
|= PUT_LINE_FORWARD
;
9166 if ((VIsual_mode
!= 'V'
9167 && curwin
->w_cursor
.col
< curbuf
->b_op_start
.col
)
9168 || (VIsual_mode
== 'V'
9169 && curwin
->w_cursor
.lnum
< curbuf
->b_op_start
.lnum
))
9170 /* cursor is at the end of the line or end of file, put
9175 do_put(cap
->oap
->regname
, dir
, cap
->count1
, flags
);
9178 /* If a register was saved, put it back now. */
9180 put_register(regname
, reg2
);
9182 /* What to reselect with "gv"? Selecting the just put text seems to
9183 * be the most useful, since the original text was removed. */
9186 curbuf
->b_visual
.vi_start
= curbuf
->b_op_start
;
9187 curbuf
->b_visual
.vi_end
= curbuf
->b_op_end
;
9190 /* When all lines were selected and deleted do_put() leaves an empty
9191 * line that needs to be deleted now. */
9192 if (empty
&& *ml_get(curbuf
->b_ml
.ml_line_count
) == NUL
)
9194 ml_delete(curbuf
->b_ml
.ml_line_count
, TRUE
);
9196 /* If the cursor was in that line, move it to the end of the last
9198 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
9200 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9201 coladvance((colnr_T
)MAXCOL
);
9205 auto_format(FALSE
, TRUE
);
9210 * "o" and "O" commands.
9217 /* "do" is ":diffget" */
9218 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'o')
9221 nv_diffgetput(FALSE
);
9226 if (VIsual_active
) /* switch start and end of visual */
9227 v_swap_corners(cap
->cmdchar
);
9239 ProcessSniffRequests();
9243 #ifdef FEAT_NETBEANS_INTG
9248 netbeans_keycommand(cap
->nchar
);
9258 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9264 * Trigger CursorHold event.
9265 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9266 * input buffer. "did_cursorhold" is set to avoid retriggering.
9273 apply_autocmds(EVENT_CURSORHOLD
, NULL
, NULL
, FALSE
, curbuf
);
9274 did_cursorhold
= TRUE
;
9275 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */