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
));
187 * Function to be called for a Normal or Visual mode command.
188 * The argument is a cmdarg_T.
190 typedef void (*nv_func_T
) __ARGS((cmdarg_T
*cap
));
192 /* Values for cmd_flags. */
193 #define NV_NCH 0x01 /* may need to get a second char */
194 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
195 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
196 #define NV_LANG 0x08 /* second char needs language adjustment */
198 #define NV_SS 0x10 /* may start selection */
199 #define NV_SSS 0x20 /* may start selection with shift modifier */
200 #define NV_STS 0x40 /* may stop selection without shift modif. */
201 #define NV_RL 0x80 /* 'rightleft' modifies command */
202 #define NV_KEEPREG 0x100 /* don't clear regname */
203 #define NV_NCW 0x200 /* not allowed in command-line window */
206 * Generally speaking, every Normal mode command should either clear any
207 * pending operator (with *clearop*()), or set the motion type variable
210 * When a cursor motion command is made, it is marked as being a character or
211 * line oriented motion. Then, if an operator is in effect, the operation
212 * becomes character or line oriented accordingly.
216 * This table contains one entry for every Normal or Visual mode command.
217 * The order doesn't matter, init_normal_cmds() will create a sorted index.
218 * It is faster when all keys from zero to '~' are present.
220 static const struct nv_cmd
222 int cmd_char
; /* (first) command character */
223 nv_func_T cmd_func
; /* function for this command */
224 short_u cmd_flags
; /* NV_ flags */
225 short cmd_arg
; /* value for ca.arg */
228 {NUL
, nv_error
, 0, 0},
229 {Ctrl_A
, nv_addsub
, 0, 0},
230 {Ctrl_B
, nv_page
, NV_STS
, BACKWARD
},
231 {Ctrl_C
, nv_esc
, 0, TRUE
},
232 {Ctrl_D
, nv_halfpage
, 0, 0},
233 {Ctrl_E
, nv_scroll_line
, 0, TRUE
},
234 {Ctrl_F
, nv_page
, NV_STS
, FORWARD
},
235 {Ctrl_G
, nv_ctrlg
, 0, 0},
236 {Ctrl_H
, nv_ctrlh
, 0, 0},
237 {Ctrl_I
, nv_pcmark
, 0, 0},
238 {NL
, nv_down
, 0, FALSE
},
239 {Ctrl_K
, nv_error
, 0, 0},
240 {Ctrl_L
, nv_clear
, 0, 0},
241 {Ctrl_M
, nv_down
, 0, TRUE
},
242 {Ctrl_N
, nv_down
, NV_STS
, FALSE
},
243 {Ctrl_O
, nv_ctrlo
, 0, 0},
244 {Ctrl_P
, nv_up
, NV_STS
, FALSE
},
246 {Ctrl_Q
, nv_visual
, 0, FALSE
},
248 {Ctrl_Q
, nv_ignore
, 0, 0},
250 {Ctrl_R
, nv_redo
, 0, 0},
251 {Ctrl_S
, nv_ignore
, 0, 0},
252 {Ctrl_T
, nv_tagpop
, NV_NCW
, 0},
253 {Ctrl_U
, nv_halfpage
, 0, 0},
255 {Ctrl_V
, nv_visual
, 0, FALSE
},
256 {'V', nv_visual
, 0, FALSE
},
257 {'v', nv_visual
, 0, FALSE
},
259 {Ctrl_V
, nv_error
, 0, 0},
260 {'V', nv_error
, 0, 0},
261 {'v', nv_error
, 0, 0},
263 {Ctrl_W
, nv_window
, 0, 0},
264 {Ctrl_X
, nv_addsub
, 0, 0},
265 {Ctrl_Y
, nv_scroll_line
, 0, FALSE
},
266 {Ctrl_Z
, nv_suspend
, 0, 0},
267 {ESC
, nv_esc
, 0, FALSE
},
268 {Ctrl_BSL
, nv_normal
, NV_NCH_ALW
, 0},
269 {Ctrl_RSB
, nv_ident
, NV_NCW
, 0},
270 {Ctrl_HAT
, nv_hat
, NV_NCW
, 0},
271 {Ctrl__
, nv_error
, 0, 0},
272 {' ', nv_right
, 0, 0},
273 {'!', nv_operator
, 0, 0},
274 {'"', nv_regname
, NV_NCH_NOP
|NV_KEEPREG
, 0},
275 {'#', nv_ident
, 0, 0},
276 {'$', nv_dollar
, 0, 0},
277 {'%', nv_percent
, 0, 0},
278 {'&', nv_optrans
, 0, 0},
279 {'\'', nv_gomark
, NV_NCH_ALW
, TRUE
},
280 {'(', nv_brace
, 0, BACKWARD
},
281 {')', nv_brace
, 0, FORWARD
},
282 {'*', nv_ident
, 0, 0},
283 {'+', nv_down
, 0, TRUE
},
284 {',', nv_csearch
, 0, TRUE
},
285 {'-', nv_up
, 0, TRUE
},
286 {'.', nv_dot
, NV_KEEPREG
, 0},
287 {'/', nv_search
, 0, FALSE
},
288 {'0', nv_beginline
, 0, 0},
289 {'1', nv_ignore
, 0, 0},
290 {'2', nv_ignore
, 0, 0},
291 {'3', nv_ignore
, 0, 0},
292 {'4', nv_ignore
, 0, 0},
293 {'5', nv_ignore
, 0, 0},
294 {'6', nv_ignore
, 0, 0},
295 {'7', nv_ignore
, 0, 0},
296 {'8', nv_ignore
, 0, 0},
297 {'9', nv_ignore
, 0, 0},
298 {':', nv_colon
, 0, 0},
299 {';', nv_csearch
, 0, FALSE
},
300 {'<', nv_operator
, NV_RL
, 0},
301 {'=', nv_operator
, 0, 0},
302 {'>', nv_operator
, NV_RL
, 0},
303 {'?', nv_search
, 0, FALSE
},
304 {'@', nv_at
, NV_NCH_NOP
, FALSE
},
305 {'A', nv_edit
, 0, 0},
306 {'B', nv_bck_word
, 0, 1},
307 {'C', nv_abbrev
, NV_KEEPREG
, 0},
308 {'D', nv_abbrev
, NV_KEEPREG
, 0},
309 {'E', nv_wordcmd
, 0, TRUE
},
310 {'F', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
311 {'G', nv_goto
, 0, TRUE
},
312 {'H', nv_scroll
, 0, 0},
313 {'I', nv_edit
, 0, 0},
314 {'J', nv_join
, 0, 0},
315 {'K', nv_ident
, 0, 0},
316 {'L', nv_scroll
, 0, 0},
317 {'M', nv_scroll
, 0, 0},
318 {'N', nv_next
, 0, SEARCH_REV
},
319 {'O', nv_open
, 0, 0},
321 {'Q', nv_exmode
, NV_NCW
, 0},
322 {'R', nv_Replace
, 0, FALSE
},
323 {'S', nv_subst
, NV_KEEPREG
, 0},
324 {'T', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
325 {'U', nv_Undo
, 0, 0},
326 {'W', nv_wordcmd
, 0, TRUE
},
327 {'X', nv_abbrev
, NV_KEEPREG
, 0},
328 {'Y', nv_abbrev
, NV_KEEPREG
, 0},
329 {'Z', nv_Zet
, NV_NCH_NOP
|NV_NCW
, 0},
330 {'[', nv_brackets
, NV_NCH_ALW
, BACKWARD
},
331 {'\\', nv_error
, 0, 0},
332 {']', nv_brackets
, NV_NCH_ALW
, FORWARD
},
333 {'^', nv_beginline
, 0, BL_WHITE
| BL_FIX
},
334 {'_', nv_lineop
, 0, 0},
335 {'`', nv_gomark
, NV_NCH_ALW
, FALSE
},
336 {'a', nv_edit
, NV_NCH
, 0},
337 {'b', nv_bck_word
, 0, 0},
338 {'c', nv_operator
, 0, 0},
339 {'d', nv_operator
, 0, 0},
340 {'e', nv_wordcmd
, 0, FALSE
},
341 {'f', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
342 {'g', nv_g_cmd
, NV_NCH_ALW
, FALSE
},
343 {'h', nv_left
, NV_RL
, 0},
344 {'i', nv_edit
, NV_NCH
, 0},
345 {'j', nv_down
, 0, FALSE
},
346 {'k', nv_up
, 0, FALSE
},
347 {'l', nv_right
, NV_RL
, 0},
348 {'m', nv_mark
, NV_NCH_NOP
, 0},
349 {'n', nv_next
, 0, 0},
350 {'o', nv_open
, 0, 0},
352 {'q', nv_record
, NV_NCH
, 0},
353 {'r', nv_replace
, NV_NCH_NOP
|NV_LANG
, 0},
354 {'s', nv_subst
, NV_KEEPREG
, 0},
355 {'t', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
356 {'u', nv_undo
, 0, 0},
357 {'w', nv_wordcmd
, 0, FALSE
},
358 {'x', nv_abbrev
, NV_KEEPREG
, 0},
359 {'y', nv_operator
, 0, 0},
360 {'z', nv_zet
, NV_NCH_ALW
, 0},
361 {'{', nv_findpar
, 0, BACKWARD
},
362 {'|', nv_pipe
, 0, 0},
363 {'}', nv_findpar
, 0, FORWARD
},
364 {'~', nv_tilde
, 0, 0},
367 {POUND
, nv_ident
, 0, 0},
369 {K_MOUSEUP
, nv_mousescroll
, 0, TRUE
},
370 {K_MOUSEDOWN
, nv_mousescroll
, 0, FALSE
},
371 {K_LEFTMOUSE
, nv_mouse
, 0, 0},
372 {K_LEFTMOUSE_NM
, nv_mouse
, 0, 0},
373 {K_LEFTDRAG
, nv_mouse
, 0, 0},
374 {K_LEFTRELEASE
, nv_mouse
, 0, 0},
375 {K_LEFTRELEASE_NM
, nv_mouse
, 0, 0},
376 {K_MIDDLEMOUSE
, nv_mouse
, 0, 0},
377 {K_MIDDLEDRAG
, nv_mouse
, 0, 0},
378 {K_MIDDLERELEASE
, nv_mouse
, 0, 0},
379 {K_RIGHTMOUSE
, nv_mouse
, 0, 0},
380 {K_RIGHTDRAG
, nv_mouse
, 0, 0},
381 {K_RIGHTRELEASE
, nv_mouse
, 0, 0},
382 {K_X1MOUSE
, nv_mouse
, 0, 0},
383 {K_X1DRAG
, nv_mouse
, 0, 0},
384 {K_X1RELEASE
, nv_mouse
, 0, 0},
385 {K_X2MOUSE
, nv_mouse
, 0, 0},
386 {K_X2DRAG
, nv_mouse
, 0, 0},
387 {K_X2RELEASE
, nv_mouse
, 0, 0},
389 {K_IGNORE
, nv_ignore
, NV_KEEPREG
, 0},
390 {K_NOP
, nv_nop
, 0, 0},
391 {K_INS
, nv_edit
, 0, 0},
392 {K_KINS
, nv_edit
, 0, 0},
393 {K_BS
, nv_ctrlh
, 0, 0},
394 {K_UP
, nv_up
, NV_SSS
|NV_STS
, FALSE
},
395 {K_S_UP
, nv_page
, NV_SS
, BACKWARD
},
396 {K_DOWN
, nv_down
, NV_SSS
|NV_STS
, FALSE
},
397 {K_S_DOWN
, nv_page
, NV_SS
, FORWARD
},
398 {K_LEFT
, nv_left
, NV_SSS
|NV_STS
|NV_RL
, 0},
399 {K_S_LEFT
, nv_bck_word
, NV_SS
|NV_RL
, 0},
400 {K_C_LEFT
, nv_bck_word
, NV_SSS
|NV_RL
|NV_STS
, 1},
401 {K_RIGHT
, nv_right
, NV_SSS
|NV_STS
|NV_RL
, 0},
402 {K_S_RIGHT
, nv_wordcmd
, NV_SS
|NV_RL
, FALSE
},
403 {K_C_RIGHT
, nv_wordcmd
, NV_SSS
|NV_RL
|NV_STS
, TRUE
},
404 {K_PAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
405 {K_KPAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
406 {K_PAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
407 {K_KPAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
408 {K_END
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
409 {K_KEND
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
410 {K_S_END
, nv_end
, NV_SS
, FALSE
},
411 {K_C_END
, nv_end
, NV_SSS
|NV_STS
, TRUE
},
412 {K_HOME
, nv_home
, NV_SSS
|NV_STS
, 0},
413 {K_KHOME
, nv_home
, NV_SSS
|NV_STS
, 0},
414 {K_S_HOME
, nv_home
, NV_SS
, 0},
415 {K_C_HOME
, nv_goto
, NV_SSS
|NV_STS
, FALSE
},
416 {K_DEL
, nv_abbrev
, 0, 0},
417 {K_KDEL
, nv_abbrev
, 0, 0},
418 {K_UNDO
, nv_kundo
, 0, 0},
419 {K_HELP
, nv_help
, NV_NCW
, 0},
420 {K_F1
, nv_help
, NV_NCW
, 0},
421 {K_XF1
, nv_help
, NV_NCW
, 0},
423 {K_SELECT
, nv_select
, 0, 0},
426 {K_VER_SCROLLBAR
, nv_ver_scrollbar
, 0, 0},
427 {K_HOR_SCROLLBAR
, nv_hor_scrollbar
, 0, 0},
429 #ifdef FEAT_GUI_TABLINE
430 {K_TABLINE
, nv_tabline
, 0, 0},
431 {K_TABMENU
, nv_tabmenu
, 0, 0},
434 {K_F8
, farsi_fkey
, 0, 0},
435 {K_F9
, farsi_fkey
, 0, 0},
438 {K_SNIFF
, nv_sniff
, 0, 0},
440 #ifdef FEAT_NETBEANS_INTG
441 {K_F21
, nv_nbcmd
, NV_NCH_ALW
, 0},
444 {K_DROP
, nv_drop
, NV_STS
, 0},
447 {K_CURSORHOLD
, nv_cursorhold
, NV_KEEPREG
, 0},
451 /* Number of commands in nv_cmds[]. */
452 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
454 /* Sorted index of commands in nv_cmds[]. */
455 static short nv_cmd_idx
[NV_CMDS_SIZE
];
457 /* The highest index for which
458 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
459 static int nv_max_linear
;
462 * Compare functions for qsort() below, that checks the command character
463 * through the index in nv_cmd_idx[].
475 /* The commands are sorted on absolute value. */
476 c1
= nv_cmds
[*(const short *)s1
].cmd_char
;
477 c2
= nv_cmds
[*(const short *)s2
].cmd_char
;
486 * Initialize the nv_cmd_idx[] table.
493 /* Fill the index table with a one to one relation. */
494 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
497 /* Sort the commands by the command character. */
498 qsort((void *)&nv_cmd_idx
, (size_t)NV_CMDS_SIZE
, sizeof(short), nv_compare
);
500 /* Find the first entry that can't be indexed by the command character. */
501 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
502 if (i
!= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
)
504 nv_max_linear
= i
- 1;
508 * Search for a command in the commands table.
509 * Returns -1 for invalid command.
512 find_command(cmdchar
)
521 /* A multi-byte character is never a command. */
522 if (cmdchar
>= 0x100)
526 /* We use the absolute value of the character. Special keys have a
527 * negative value, but are sorted on their absolute value. */
531 /* If the character is in the first part: The character is the index into
533 if (cmdchar
<= nv_max_linear
)
534 return nv_cmd_idx
[cmdchar
];
536 /* Perform a binary search. */
537 bot
= nv_max_linear
+ 1;
538 top
= NV_CMDS_SIZE
- 1;
543 c
= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
;
560 * Execute a command in Normal mode.
564 normal_cmd(oap
, toplevel
)
566 int toplevel
; /* TRUE when called from main() */
568 cmdarg_T ca
; /* command arguments */
570 int ctrl_w
= FALSE
; /* got CTRL-W command */
571 int old_col
= curwin
->w_curswant
;
572 #ifdef FEAT_CMDL_INFO
573 int need_flushbuf
; /* need to call out_flush() */
576 pos_T old_pos
; /* cursor position before command */
578 static int old_mapped_len
= 0;
582 vim_memset(&ca
, 0, sizeof(ca
)); /* also resets ca.retval */
585 /* Use a count remembered from before entering an operator. After typing
586 * "3d" we return from normal_cmd() and come back here, the "3" is
587 * remembered in "opcount". */
588 ca
.opcount
= opcount
;
591 want_sniff_request
= sniff_connected
;
595 * If there is an operator pending, then the command we take this time
596 * will terminate it. Finish_op tells us to finish the operation before
597 * returning this time (unless the operation was cancelled).
602 finish_op
= (oap
->op_type
!= OP_NOP
);
606 ui_cursor_shape(); /* may show different cursor shape */
607 # ifdef FEAT_MOUSESHAPE
608 update_mouseshape(-1);
613 /* When not finishing an operator and no register name typed, reset the
615 if (!finish_op
&& !oap
->regname
)
619 /* Restore counts from before receiving K_CURSORHOLD. This means after
620 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
622 if (oap
->prev_opcount
> 0 || oap
->prev_count0
> 0)
624 ca
.opcount
= oap
->prev_opcount
;
625 ca
.count0
= oap
->prev_count0
;
626 oap
->prev_opcount
= 0;
627 oap
->prev_count0
= 0;
632 mapped_len
= typebuf_maplen();
636 #ifdef USE_ON_FLY_SCROLL
637 dont_scroll
= FALSE
; /* allow scrolling here */
641 * Get the command character from the user.
646 LANGMAP_ADJUST(c
, TRUE
);
651 * If a mapping was started in Visual or Select mode, remember the length
652 * of the mapping. This is used below to not return to Insert mode for as
653 * long as the mapping is being executed.
655 if (restart_edit
== 0)
657 else if (old_mapped_len
658 || (VIsual_active
&& mapped_len
== 0 && typebuf_maplen() > 0))
659 old_mapped_len
= typebuf_maplen();
667 * In Select mode, typed text replaces the selection.
671 && (vim_isprintc(c
) || c
== NL
|| c
== CAR
|| c
== K_KENTER
))
673 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
674 * 'insertmode' is set) fake a "d"elete command, Insert mode will
675 * restart automatically.
676 * Insert the typed character in the typeahead buffer, so that it can
677 * be mapped in Insert mode. Required for ":lmap" to work. */
679 if (restart_edit
!= 0)
683 msg_nowait
= TRUE
; /* don't delay going to insert mode */
687 #ifdef FEAT_CMDL_INFO
688 need_flushbuf
= add_to_showcmd(c
);
693 if (!(VIsual_active
&& VIsual_select
))
697 * Handle a count before a command and compute ca.count0.
698 * Note that '0' is a command and not the start of a count, but it's
699 * part of a count after other digits.
701 while ( (c
>= '1' && c
<= '9')
702 || (ca
.count0
!= 0 && (c
== K_DEL
|| c
== K_KDEL
|| c
== '0')))
704 if (c
== K_DEL
|| c
== K_KDEL
)
707 #ifdef FEAT_CMDL_INFO
708 del_from_showcmd(4); /* delete the digit and ~@% */
712 ca
.count0
= ca
.count0
* 10 + (c
- '0');
713 if (ca
.count0
< 0) /* got too large! */
714 ca
.count0
= 999999999L;
716 /* Set v:count here, when called from main() and not a stuffed
717 * command, so that v:count can be used in an expression mapping
718 * right after the count. */
719 if (toplevel
&& stuff_empty())
720 set_vcount(ca
.count0
, ca
.count0
== 0 ? 1 : ca
.count0
);
725 ++allow_keys
; /* no mapping for nchar, but keys */
727 ++no_zero_mapping
; /* don't map zero here */
730 LANGMAP_ADJUST(c
, TRUE
);
738 #ifdef FEAT_CMDL_INFO
739 need_flushbuf
|= add_to_showcmd(c
);
744 * If we got CTRL-W there may be a/another count
746 if (c
== Ctrl_W
&& !ctrl_w
&& oap
->op_type
== OP_NOP
)
749 ca
.opcount
= ca
.count0
; /* remember first count */
752 ++allow_keys
; /* no mapping for nchar, but keys */
753 c
= plain_vgetc(); /* get next character */
755 LANGMAP_ADJUST(c
, TRUE
);
759 #ifdef FEAT_CMDL_INFO
760 need_flushbuf
|= add_to_showcmd(c
);
762 goto getcount
; /* jump back */
767 if (c
== K_CURSORHOLD
)
769 /* Save the count values so that ca.opcount and ca.count0 are exactly
770 * the same when coming back here after handling K_CURSORHOLD. */
771 oap
->prev_opcount
= ca
.opcount
;
772 oap
->prev_count0
= ca
.count0
;
779 * If we're in the middle of an operator (including after entering a
780 * yank buffer with '"') AND we had a count before the operator, then
781 * that count overrides the current value of ca.count0.
782 * What this means effectively, is that commands like "3dw" get turned
783 * into "d3w" which makes things fall into place pretty neatly.
784 * If you give a count before AND after the operator, they are
788 ca
.count0
*= ca
.opcount
;
790 ca
.count0
= ca
.opcount
;
794 * Always remember the count. It will be set to zero (on the next call,
795 * above) when there is no pending operator.
796 * When called from main(), save the count for use by the "count" built-in
799 ca
.opcount
= ca
.count0
;
800 ca
.count1
= (ca
.count0
== 0 ? 1 : ca
.count0
);
804 * Only set v:count when called from main() and not a stuffed command.
806 if (toplevel
&& stuff_empty())
807 set_vcount(ca
.count0
, ca
.count1
);
811 * Find the command character in the table of commands.
812 * For CTRL-W we already got nchar when looking for a count.
821 idx
= find_command(ca
.cmdchar
);
824 /* Not a known command: beep. */
829 if (text_locked() && (nv_cmds
[idx
].cmd_flags
& NV_NCW
))
831 /* This command is not allowed while editing a ccmdline: beep. */
837 if ((nv_cmds
[idx
].cmd_flags
& NV_NCW
) && curbuf_locked())
843 * In Visual/Select mode, a few keys are handled in a special way.
847 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
849 && (nv_cmds
[idx
].cmd_flags
& NV_STS
)
850 && !(mod_mask
& MOD_MASK_SHIFT
))
853 redraw_curbuf_later(INVERTED
);
856 /* Keys that work different when 'keymodel' contains "startsel" */
859 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
861 unshift_special(&ca
);
862 idx
= find_command(ca
.cmdchar
);
870 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
871 && (mod_mask
& MOD_MASK_SHIFT
))
873 mod_mask
&= ~MOD_MASK_SHIFT
;
879 #ifdef FEAT_RIGHTLEFT
880 if (curwin
->w_p_rl
&& KeyTyped
&& !KeyStuffed
881 && (nv_cmds
[idx
].cmd_flags
& NV_RL
))
883 /* Invert horizontal movements and operations. Only when typed by the
884 * user directly, not when the result of a mapping or "x" translated
888 case 'l': ca
.cmdchar
= 'h'; break;
889 case K_RIGHT
: ca
.cmdchar
= K_LEFT
; break;
890 case K_S_RIGHT
: ca
.cmdchar
= K_S_LEFT
; break;
891 case K_C_RIGHT
: ca
.cmdchar
= K_C_LEFT
; break;
892 case 'h': ca
.cmdchar
= 'l'; break;
893 case K_LEFT
: ca
.cmdchar
= K_RIGHT
; break;
894 case K_S_LEFT
: ca
.cmdchar
= K_S_RIGHT
; break;
895 case K_C_LEFT
: ca
.cmdchar
= K_C_RIGHT
; break;
896 case '>': ca
.cmdchar
= '<'; break;
897 case '<': ca
.cmdchar
= '>'; break;
899 idx
= find_command(ca
.cmdchar
);
904 * Get an additional character if we need one.
906 if ((nv_cmds
[idx
].cmd_flags
& NV_NCH
)
907 && (((nv_cmds
[idx
].cmd_flags
& NV_NCH_NOP
) == NV_NCH_NOP
908 && oap
->op_type
== OP_NOP
)
909 || (nv_cmds
[idx
].cmd_flags
& NV_NCH_ALW
) == NV_NCH_ALW
910 || (ca
.cmdchar
== 'q'
911 && oap
->op_type
== OP_NOP
914 || ((ca
.cmdchar
== 'a' || ca
.cmdchar
== 'i')
915 && (oap
->op_type
!= OP_NOP
922 int repl
= FALSE
; /* get character for replace mode */
923 int lit
= FALSE
; /* get extra character literally */
924 int langmap_active
= FALSE
; /* using :lmap mappings */
925 int lang
; /* getting a text character */
926 #ifdef USE_IM_CONTROL
927 int save_smd
; /* saved value of p_smd */
931 ++allow_keys
; /* no mapping for nchar, but allow key codes */
933 /* Don't generate a CursorHold event here, most commands can't handle
934 * it, e.g., nv_replace(), nv_csearch(). */
935 did_cursorhold
= TRUE
;
937 if (ca
.cmdchar
== 'g')
940 * For 'g' get the next character now, so that we can check for
941 * "gr", "g'" and "g`".
943 ca
.nchar
= plain_vgetc();
945 LANGMAP_ADJUST(ca
.nchar
, TRUE
);
947 #ifdef FEAT_CMDL_INFO
948 need_flushbuf
|= add_to_showcmd(ca
.nchar
);
950 if (ca
.nchar
== 'r' || ca
.nchar
== '\'' || ca
.nchar
== '`'
951 || ca
.nchar
== Ctrl_BSL
)
953 cp
= &ca
.extra_char
; /* need to get a third character */
955 lit
= TRUE
; /* get it literally */
957 repl
= TRUE
; /* get it in replace mode */
960 cp
= NULL
; /* no third character needed */
964 if (ca
.cmdchar
== 'r') /* get it in replace mode */
968 lang
= (repl
|| (nv_cmds
[idx
].cmd_flags
& NV_LANG
));
971 * Get a second or third character.
978 State
= REPLACE
; /* pretend Replace mode */
979 ui_cursor_shape(); /* show different cursor shape */
982 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
984 /* Allow mappings defined with ":lmap". */
991 langmap_active
= TRUE
;
993 #ifdef USE_IM_CONTROL
995 p_smd
= FALSE
; /* Don't let the IM code show the mode here */
996 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_IM
)
1000 *cp
= plain_vgetc();
1004 /* Undo the decrement done above */
1007 State
= NORMAL_BUSY
;
1009 #ifdef USE_IM_CONTROL
1012 if (curbuf
->b_p_iminsert
!= B_IMODE_LMAP
)
1013 im_save_status(&curbuf
->b_p_iminsert
);
1014 im_set_active(FALSE
);
1019 State
= NORMAL_BUSY
;
1021 #ifdef FEAT_CMDL_INFO
1022 need_flushbuf
|= add_to_showcmd(*cp
);
1027 #ifdef FEAT_DIGRAPHS
1028 /* Typing CTRL-K gets a digraph. */
1030 && ((nv_cmds
[idx
].cmd_flags
& NV_LANG
)
1031 || cp
== &ca
.extra_char
)
1032 && vim_strchr(p_cpo
, CPO_DIGRAPH
) == NULL
)
1034 c
= get_digraph(FALSE
);
1038 # ifdef FEAT_CMDL_INFO
1039 /* Guessing how to update showcmd here... */
1040 del_from_showcmd(3);
1041 need_flushbuf
|= add_to_showcmd(*cp
);
1048 /* adjust chars > 127, except after "tTfFr" commands */
1049 LANGMAP_ADJUST(*cp
, !lang
);
1051 #ifdef FEAT_RIGHTLEFT
1052 /* adjust Hebrew mapped char */
1053 if (p_hkmap
&& lang
&& KeyTyped
)
1056 /* adjust Farsi mapped char */
1057 if (p_fkmap
&& lang
&& KeyTyped
)
1064 * When the next character is CTRL-\ a following CTRL-N means the
1065 * command is aborted and we go to Normal mode.
1067 if (cp
== &ca
.extra_char
1068 && ca
.nchar
== Ctrl_BSL
1069 && (ca
.extra_char
== Ctrl_N
|| ca
.extra_char
== Ctrl_G
))
1071 ca
.cmdchar
= Ctrl_BSL
;
1072 ca
.nchar
= ca
.extra_char
;
1073 idx
= find_command(ca
.cmdchar
);
1075 else if (*cp
== Ctrl_BSL
)
1077 long towait
= (p_ttm
>= 0 ? p_ttm
: p_tm
);
1079 /* There is a busy wait here when typing "f<C-\>" and then
1080 * something different from CTRL-N. Can't be avoided. */
1081 while ((c
= vpeekc()) <= 0 && towait
> 0L)
1083 do_sleep(towait
> 50L ? 50L : towait
);
1089 if (c
!= Ctrl_N
&& c
!= Ctrl_G
)
1093 ca
.cmdchar
= Ctrl_BSL
;
1095 idx
= find_command(ca
.cmdchar
);
1101 /* When getting a text character and the next character is a
1102 * multi-byte character, it could be a composing character.
1103 * However, don't wait for it to arrive. */
1104 while (enc_utf8
&& lang
&& (c
= vpeekc()) > 0
1105 && (c
>= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1108 if (!utf_iscomposing(c
))
1110 vungetc(c
); /* it wasn't, put it back */
1113 else if (ca
.ncharC1
== 0)
1124 #ifdef FEAT_CMDL_INFO
1126 * Flush the showcmd characters onto the screen so we can see them while
1127 * the command is being executed. Only do this when the shown command was
1128 * actually displayed, otherwise this will slow down a lot when executing
1135 if (ca
.cmdchar
!= K_IGNORE
)
1136 did_cursorhold
= FALSE
;
1141 if (ca
.nchar
== ESC
)
1144 if (restart_edit
== 0 && goto_im())
1149 if (ca
.cmdchar
!= K_IGNORE
)
1151 msg_didout
= FALSE
; /* don't scroll screen up for normal command */
1156 old_pos
= curwin
->w_cursor
; /* remember where cursor was */
1158 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1160 if (!VIsual_active
&& km_startsel
)
1162 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
1165 unshift_special(&ca
);
1166 idx
= find_command(ca
.cmdchar
);
1168 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
1169 && (mod_mask
& MOD_MASK_SHIFT
))
1172 mod_mask
&= ~MOD_MASK_SHIFT
;
1178 * Execute the command!
1179 * Call the command function found in the commands table.
1181 ca
.arg
= nv_cmds
[idx
].cmd_arg
;
1182 (nv_cmds
[idx
].cmd_func
)(&ca
);
1185 * If we didn't start or finish an operator, reset oap->regname, unless we
1190 && (idx
< 0 || !(nv_cmds
[idx
].cmd_flags
& NV_KEEPREG
)))
1199 /* Get the length of mapped chars again after typing a count, second
1200 * character or "z333<cr>". */
1201 if (old_mapped_len
> 0)
1202 old_mapped_len
= typebuf_maplen();
1206 * If an operation is pending, handle it...
1208 do_pending_operator(&ca
, old_col
, FALSE
);
1211 * Wait for a moment when a message is displayed that will be overwritten
1212 * by the mode message.
1213 * In Visual mode and with "^O" in Insert mode, a short message will be
1214 * overwritten by the mode message. Wait a bit, until a key is hit.
1215 * In Visual mode, it's more important to keep the Visual area updated
1216 * than keeping a message (e.g. from a /pat search).
1217 * Only do this if the command was typed, not from a mapping.
1218 * Don't wait when emsg_silent is non-zero.
1219 * Also wait a bit after an error message, e.g. for "^O:".
1220 * Don't redraw the screen, it would remove the message.
1224 && (restart_edit
!= 0
1227 && old_pos
.lnum
== curwin
->w_cursor
.lnum
1228 && old_pos
.col
== curwin
->w_cursor
.col
)
1233 && (msg_didout
|| (msg_didany
&& msg_scroll
))
1236 || (restart_edit
!= 0
1241 || emsg_on_display
)))
1242 && oap
->regname
== 0
1243 && !(ca
.retval
& CA_COMMAND_BUSY
)
1248 && oap
->op_type
== OP_NOP
)
1250 int save_State
= State
;
1252 /* Draw the cursor with the right shape here */
1253 if (restart_edit
!= 0)
1256 /* If need to redraw, and there is a "keep_msg", redraw before the
1258 if (must_redraw
&& keep_msg
!= NULL
&& !emsg_on_display
)
1264 /* showmode() will clear keep_msg, but we want to use it anyway */
1266 /* now reset it, otherwise it's put in the history again */
1268 msg_attr(kmsg
, keep_msg_attr
);
1274 if (msg_scroll
|| emsg_on_display
)
1275 ui_delay(1000L, TRUE
); /* wait at least one second */
1276 ui_delay(3000L, FALSE
); /* wait up to three seconds */
1280 emsg_on_display
= FALSE
;
1284 * Finish up after executing a Normal mode command.
1290 /* Reset finish_op, in case it was set */
1296 /* Redraw the cursor with another shape, if we were in Operator-pending
1297 * mode or did a replace command. */
1298 if (c
|| ca
.cmdchar
== 'r')
1300 ui_cursor_shape(); /* may show different cursor shape */
1301 # ifdef FEAT_MOUSESHAPE
1302 update_mouseshape(-1);
1307 #ifdef FEAT_CMDL_INFO
1308 if (oap
->op_type
== OP_NOP
&& oap
->regname
== 0
1309 # ifdef FEAT_AUTOCMD
1310 && ca
.cmdchar
!= K_CURSORHOLD
1316 checkpcmark(); /* check if we moved since setting pcmark */
1317 vim_free(ca
.searchbuf
);
1324 #ifdef FEAT_SCROLLBIND
1325 if (curwin
->w_p_scb
&& toplevel
)
1327 validate_cursor(); /* may need to update w_leftcol */
1328 do_check_scrollbind(TRUE
);
1333 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1334 * if still inside a mapping that started in Visual mode).
1335 * May switch from Visual to Select mode after CTRL-O command.
1337 if ( oap
->op_type
== OP_NOP
1339 && ((restart_edit
!= 0 && !VIsual_active
&& old_mapped_len
== 0)
1340 || restart_VIsual_select
== 1)
1342 && restart_edit
!= 0
1344 && !(ca
.retval
& CA_COMMAND_BUSY
)
1346 && oap
->regname
== 0)
1349 if (restart_VIsual_select
== 1)
1351 VIsual_select
= TRUE
;
1353 restart_VIsual_select
= 0;
1356 if (restart_edit
!= 0
1358 && !VIsual_active
&& old_mapped_len
== 0
1361 (void)edit(restart_edit
, FALSE
, 1L);
1365 if (restart_VIsual_select
== 2)
1366 restart_VIsual_select
= 1;
1369 /* Save count before an operator for next time. */
1370 opcount
= ca
.opcount
;
1374 * Handle an operator after visual mode or when the movement is finished
1377 do_pending_operator(cap
, old_col
, gui_yank
)
1382 oparg_T
*oap
= cap
->oap
;
1384 int empty_region_error
;
1385 int restart_edit_save
;
1388 /* The visual area is remembered for redo */
1389 static int redo_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
1390 static linenr_T redo_VIsual_line_count
; /* number of lines */
1391 static colnr_T redo_VIsual_col
; /* number of cols or end column */
1392 static long redo_VIsual_count
; /* count for Visual operator */
1393 # ifdef FEAT_VIRTUALEDIT
1394 int include_line_break
= FALSE
;
1398 #if defined(FEAT_CLIPBOARD)
1400 * Yank the visual area into the GUI selection register before we operate
1401 * on it and lose it forever.
1402 * Don't do it if a specific register was specified, so that ""x"*P works.
1403 * This could call do_pending_operator() recursively, but that's OK
1404 * because gui_yank will be TRUE for the nested call.
1406 if (clip_star
.available
1407 && oap
->op_type
!= OP_NOP
1411 && !redo_VIsual_busy
1413 && oap
->regname
== 0)
1416 old_cursor
= curwin
->w_cursor
;
1419 * If an operation is pending, handle it...
1425 ) && oap
->op_type
!= OP_NOP
)
1428 oap
->is_VIsual
= VIsual_active
;
1429 if (oap
->motion_force
== 'V')
1430 oap
->motion_type
= MLINE
;
1431 else if (oap
->motion_force
== 'v')
1433 /* If the motion was linewise, "inclusive" will not have been set.
1434 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1435 if (oap
->motion_type
== MLINE
)
1436 oap
->inclusive
= FALSE
;
1437 /* If the motion already was characterwise, toggle "inclusive" */
1438 else if (oap
->motion_type
== MCHAR
)
1439 oap
->inclusive
= !oap
->inclusive
;
1440 oap
->motion_type
= MCHAR
;
1442 else if (oap
->motion_force
== Ctrl_V
)
1444 /* Change line- or characterwise motion into Visual block mode. */
1445 VIsual_active
= TRUE
;
1446 VIsual
= oap
->start
;
1447 VIsual_mode
= Ctrl_V
;
1448 VIsual_select
= FALSE
;
1449 VIsual_reselect
= FALSE
;
1453 /* only redo yank when 'y' flag is in 'cpoptions' */
1454 /* never redo "zf" (define fold) */
1455 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1457 && (!VIsual_active
|| oap
->motion_force
)
1459 && cap
->cmdchar
!= 'D'
1461 && oap
->op_type
!= OP_FOLD
1462 && oap
->op_type
!= OP_FOLDOPEN
1463 && oap
->op_type
!= OP_FOLDOPENREC
1464 && oap
->op_type
!= OP_FOLDCLOSE
1465 && oap
->op_type
!= OP_FOLDCLOSEREC
1466 && oap
->op_type
!= OP_FOLDDEL
1467 && oap
->op_type
!= OP_FOLDDELREC
1471 prep_redo(oap
->regname
, cap
->count0
,
1472 get_op_char(oap
->op_type
), get_extra_op_char(oap
->op_type
),
1473 oap
->motion_force
, cap
->cmdchar
, cap
->nchar
);
1474 if (cap
->cmdchar
== '/' || cap
->cmdchar
== '?') /* was a search */
1477 * If 'cpoptions' does not contain 'r', insert the search
1478 * pattern to really repeat the same command.
1480 if (vim_strchr(p_cpo
, CPO_REDO
) == NULL
)
1481 AppendToRedobuffLit(cap
->searchbuf
, -1);
1482 AppendToRedobuff(NL_STR
);
1484 else if (cap
->cmdchar
== ':')
1486 /* do_cmdline() has stored the first typed line in
1487 * "repeat_cmdline". When several lines are typed repeating
1488 * won't be possible. */
1489 if (repeat_cmdline
== NULL
)
1493 AppendToRedobuffLit(repeat_cmdline
, -1);
1494 AppendToRedobuff(NL_STR
);
1495 vim_free(repeat_cmdline
);
1496 repeat_cmdline
= NULL
;
1502 if (redo_VIsual_busy
)
1504 oap
->start
= curwin
->w_cursor
;
1505 curwin
->w_cursor
.lnum
+= redo_VIsual_line_count
- 1;
1506 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1507 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1508 VIsual_mode
= redo_VIsual_mode
;
1509 if (VIsual_mode
== 'v')
1511 if (redo_VIsual_line_count
<= 1)
1512 curwin
->w_cursor
.col
+= redo_VIsual_col
- 1;
1514 curwin
->w_cursor
.col
= redo_VIsual_col
;
1516 if (redo_VIsual_col
== MAXCOL
)
1518 curwin
->w_curswant
= MAXCOL
;
1519 coladvance((colnr_T
)MAXCOL
);
1521 cap
->count0
= redo_VIsual_count
;
1522 if (redo_VIsual_count
!= 0)
1523 cap
->count1
= redo_VIsual_count
;
1527 else if (VIsual_active
)
1531 /* Save the current VIsual area for '< and '> marks, and "gv" */
1532 curbuf
->b_visual
.vi_start
= VIsual
;
1533 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
1534 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
1535 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
1537 curbuf
->b_visual_mode_eval
= VIsual_mode
;
1541 /* In Select mode, a linewise selection is operated upon like a
1542 * characterwise selection. */
1543 if (VIsual_select
&& VIsual_mode
== 'V')
1545 if (lt(VIsual
, curwin
->w_cursor
))
1548 curwin
->w_cursor
.col
=
1549 (colnr_T
)STRLEN(ml_get(curwin
->w_cursor
.lnum
));
1553 curwin
->w_cursor
.col
= 0;
1554 VIsual
.col
= (colnr_T
)STRLEN(ml_get(VIsual
.lnum
));
1558 /* If 'selection' is "exclusive", backup one character for
1559 * charwise selections. */
1560 else if (VIsual_mode
== 'v')
1562 # ifdef FEAT_VIRTUALEDIT
1563 include_line_break
=
1568 oap
->start
= VIsual
;
1569 if (VIsual_mode
== 'V')
1572 #endif /* FEAT_VISUAL */
1575 * Set oap->start to the first position of the operated text, oap->end
1576 * to the end of the operated text. w_cursor is equal to oap->start.
1578 if (lt(oap
->start
, curwin
->w_cursor
))
1581 /* Include folded lines completely. */
1584 if (hasFolding(oap
->start
.lnum
, &oap
->start
.lnum
, NULL
))
1586 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
,
1587 &curwin
->w_cursor
.lnum
))
1588 curwin
->w_cursor
.col
= (colnr_T
)STRLEN(ml_get_curline());
1591 oap
->end
= curwin
->w_cursor
;
1592 curwin
->w_cursor
= oap
->start
;
1594 /* w_virtcol may have been updated; if the cursor goes back to its
1595 * previous position w_virtcol becomes invalid and isn't updated
1597 curwin
->w_valid
&= ~VALID_VIRTCOL
;
1602 /* Include folded lines completely. */
1603 if (!VIsual_active
&& oap
->motion_type
== MLINE
)
1605 if (hasFolding(curwin
->w_cursor
.lnum
, &curwin
->w_cursor
.lnum
,
1607 curwin
->w_cursor
.col
= 0;
1608 if (hasFolding(oap
->start
.lnum
, NULL
, &oap
->start
.lnum
))
1609 oap
->start
.col
= (colnr_T
)STRLEN(ml_get(oap
->start
.lnum
));
1612 oap
->end
= oap
->start
;
1613 oap
->start
= curwin
->w_cursor
;
1616 oap
->line_count
= oap
->end
.lnum
- oap
->start
.lnum
+ 1;
1618 #ifdef FEAT_VIRTUALEDIT
1619 /* Set "virtual_op" before resetting VIsual_active. */
1620 virtual_op
= virtual_active();
1624 if (VIsual_active
|| redo_VIsual_busy
)
1626 if (VIsual_mode
== Ctrl_V
) /* block mode */
1630 oap
->block_mode
= TRUE
;
1632 getvvcol(curwin
, &(oap
->start
),
1633 &oap
->start_vcol
, NULL
, &oap
->end_vcol
);
1634 if (!redo_VIsual_busy
)
1636 getvvcol(curwin
, &(oap
->end
), &start
, NULL
, &end
);
1638 if (start
< oap
->start_vcol
)
1639 oap
->start_vcol
= start
;
1640 if (end
> oap
->end_vcol
)
1642 if (*p_sel
== 'e' && start
>= 1
1643 && start
- 1 >= oap
->end_vcol
)
1644 oap
->end_vcol
= start
- 1;
1646 oap
->end_vcol
= end
;
1650 /* if '$' was used, get oap->end_vcol from longest line */
1651 if (curwin
->w_curswant
== MAXCOL
)
1653 curwin
->w_cursor
.col
= MAXCOL
;
1655 for (curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
1656 curwin
->w_cursor
.lnum
<= oap
->end
.lnum
;
1657 ++curwin
->w_cursor
.lnum
)
1659 getvvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &end
);
1660 if (end
> oap
->end_vcol
)
1661 oap
->end_vcol
= end
;
1664 else if (redo_VIsual_busy
)
1665 oap
->end_vcol
= oap
->start_vcol
+ redo_VIsual_col
- 1;
1667 * Correct oap->end.col and oap->start.col to be the
1668 * upper-left and lower-right corner of the block area.
1670 * (Actually, this does convert column positions into character
1673 curwin
->w_cursor
.lnum
= oap
->end
.lnum
;
1674 coladvance(oap
->end_vcol
);
1675 oap
->end
= curwin
->w_cursor
;
1677 curwin
->w_cursor
= oap
->start
;
1678 coladvance(oap
->start_vcol
);
1679 oap
->start
= curwin
->w_cursor
;
1682 if (!redo_VIsual_busy
&& !gui_yank
)
1685 * Prepare to reselect and redo Visual: this is based on the
1686 * size of the Visual text
1688 resel_VIsual_mode
= VIsual_mode
;
1689 if (curwin
->w_curswant
== MAXCOL
)
1690 resel_VIsual_col
= MAXCOL
;
1691 else if (VIsual_mode
== Ctrl_V
)
1692 resel_VIsual_col
= oap
->end_vcol
- oap
->start_vcol
+ 1;
1693 else if (oap
->line_count
> 1)
1694 resel_VIsual_col
= oap
->end
.col
;
1696 resel_VIsual_col
= oap
->end
.col
- oap
->start
.col
+ 1;
1697 resel_VIsual_line_count
= oap
->line_count
;
1700 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1701 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1702 && oap
->op_type
!= OP_COLON
1704 && oap
->op_type
!= OP_FOLD
1705 && oap
->op_type
!= OP_FOLDOPEN
1706 && oap
->op_type
!= OP_FOLDOPENREC
1707 && oap
->op_type
!= OP_FOLDCLOSE
1708 && oap
->op_type
!= OP_FOLDCLOSEREC
1709 && oap
->op_type
!= OP_FOLDDEL
1710 && oap
->op_type
!= OP_FOLDDELREC
1712 && oap
->motion_force
== NUL
1715 /* Prepare for redoing. Only use the nchar field for "r",
1716 * otherwise it might be the second char of the operator. */
1717 prep_redo(oap
->regname
, 0L, NUL
, 'v',
1718 get_op_char(oap
->op_type
),
1719 get_extra_op_char(oap
->op_type
),
1720 oap
->op_type
== OP_REPLACE
? cap
->nchar
: NUL
);
1721 if (!redo_VIsual_busy
)
1723 redo_VIsual_mode
= resel_VIsual_mode
;
1724 redo_VIsual_col
= resel_VIsual_col
;
1725 redo_VIsual_line_count
= resel_VIsual_line_count
;
1726 redo_VIsual_count
= cap
->count0
;
1731 * oap->inclusive defaults to TRUE.
1732 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1733 * FALSE. This makes "d}P" and "v}dP" work the same.
1735 if (oap
->motion_force
== NUL
|| oap
->motion_type
== MLINE
)
1736 oap
->inclusive
= TRUE
;
1737 if (VIsual_mode
== 'V')
1738 oap
->motion_type
= MLINE
;
1741 oap
->motion_type
= MCHAR
;
1742 if (VIsual_mode
!= Ctrl_V
&& *ml_get_pos(&(oap
->end
)) == NUL
1743 # ifdef FEAT_VIRTUALEDIT
1744 && (include_line_break
|| !virtual_op
)
1748 oap
->inclusive
= FALSE
;
1749 /* Try to include the newline, unless it's an operator
1750 * that works on lines only */
1752 && !op_on_lines(oap
->op_type
)
1753 && oap
->end
.lnum
< curbuf
->b_ml
.ml_line_count
)
1757 # ifdef FEAT_VIRTUALEDIT
1758 oap
->end
.coladd
= 0;
1765 redo_VIsual_busy
= FALSE
;
1768 * Switch Visual off now, so screen updating does
1769 * not show inverted text when the screen is redrawn.
1770 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1771 * no screen redraw, so it is done here to remove the inverted
1776 VIsual_active
= FALSE
;
1782 clear_cmdline
= TRUE
; /* unshow visual mode later */
1783 #ifdef FEAT_CMDL_INFO
1787 if ((oap
->op_type
== OP_YANK
1788 || oap
->op_type
== OP_COLON
1789 || oap
->op_type
== OP_FUNCTION
1790 || oap
->op_type
== OP_FILTER
)
1791 && oap
->motion_force
== NUL
)
1792 redraw_curbuf_later(INVERTED
);
1798 /* Include the trailing byte of a multi-byte char. */
1799 if (has_mbyte
&& oap
->inclusive
)
1803 l
= (*mb_ptr2len
)(ml_get_pos(&oap
->end
));
1805 oap
->end
.col
+= l
- 1;
1808 curwin
->w_set_curswant
= TRUE
;
1811 * oap->empty is set when start and end are the same. The inclusive
1812 * flag affects this too, unless yanking and the end is on a NUL.
1814 oap
->empty
= (oap
->motion_type
== MCHAR
1816 || (oap
->op_type
== OP_YANK
1817 && gchar_pos(&oap
->end
) == NUL
))
1818 && equalpos(oap
->start
, oap
->end
)
1819 #ifdef FEAT_VIRTUALEDIT
1820 && !(virtual_op
&& oap
->start
.coladd
!= oap
->end
.coladd
)
1824 * For delete, change and yank, it's an error to operate on an
1825 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1827 empty_region_error
= (oap
->empty
1828 && vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
);
1831 /* Force a redraw when operating on an empty Visual region, when
1832 * 'modifiable is off or creating a fold. */
1833 if (oap
->is_VIsual
&& (oap
->empty
|| !curbuf
->b_p_ma
1834 # ifdef FEAT_FOLDING
1835 || oap
->op_type
== OP_FOLD
1838 redraw_curbuf_later(INVERTED
);
1842 * If the end of an operator is in column one while oap->motion_type
1843 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1844 * character in the previous line. If op_start is on or before the
1845 * first non-blank in the line, the operator becomes linewise
1846 * (strange, but that's the way vi does it).
1848 if ( oap
->motion_type
== MCHAR
1849 && oap
->inclusive
== FALSE
1850 && !(cap
->retval
& CA_NO_ADJ_OP_END
)
1851 && oap
->end
.col
== 0
1853 && (!oap
->is_VIsual
|| *p_sel
== 'o')
1856 && oap
->line_count
> 1)
1858 oap
->end_adjusted
= TRUE
; /* remember that we did this */
1862 oap
->motion_type
= MLINE
;
1865 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
1869 oap
->inclusive
= TRUE
;
1874 oap
->end_adjusted
= FALSE
;
1876 switch (oap
->op_type
)
1882 oap
->is_VIsual
? (int)cap
->count1
:
1885 auto_format(FALSE
, TRUE
);
1890 if (oap
->line_count
< 2)
1891 oap
->line_count
= 2;
1892 if (curwin
->w_cursor
.lnum
+ oap
->line_count
- 1 >
1893 curbuf
->b_ml
.ml_line_count
)
1897 do_do_join(oap
->line_count
, oap
->op_type
== OP_JOIN
);
1898 auto_format(FALSE
, TRUE
);
1904 VIsual_reselect
= FALSE
; /* don't reselect now */
1906 if (empty_region_error
)
1910 (void)op_delete(oap
);
1911 if (oap
->motion_type
== MLINE
&& has_format_option(FO_AUTO
))
1912 u_save_cursor(); /* cursor line wasn't saved yet */
1913 auto_format(FALSE
, TRUE
);
1918 if (empty_region_error
)
1924 (void)op_yank(oap
, FALSE
, !gui_yank
);
1930 VIsual_reselect
= FALSE
; /* don't reselect now */
1932 if (empty_region_error
)
1936 /* This is a new edit command, not a restart. Need to
1937 * remember it to make 'insertmode' work with mappings for
1938 * Visual mode. But do this only once and not when typed and
1939 * 'insertmode' isn't set. */
1940 if (p_im
|| !KeyTyped
)
1941 restart_edit_save
= restart_edit
;
1943 restart_edit_save
= 0;
1945 /* Reset finish_op now, don't want it set inside edit(). */
1947 if (op_change(oap
)) /* will call edit() */
1948 cap
->retval
|= CA_COMMAND_BUSY
;
1949 if (restart_edit
== 0)
1950 restart_edit
= restart_edit_save
;
1955 if (vim_strchr(p_cpo
, CPO_FILTER
) != NULL
)
1956 AppendToRedobuff((char_u
*)"!\r"); /* use any last used !cmd */
1958 bangredo
= TRUE
; /* do_bang() will put cmd in redo buffer */
1963 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1965 * If 'equalprg' is empty, do the indenting internally.
1967 if (oap
->op_type
== OP_INDENT
&& *get_equalprg() == NUL
)
1970 if (curbuf
->b_p_lisp
)
1972 op_reindent(oap
, get_lisp_indent
);
1976 # ifdef FEAT_CINDENT
1979 *curbuf
->b_p_inde
!= NUL
? get_expr_indent
:
1994 if (empty_region_error
)
2002 #if defined(FEAT_EVAL)
2003 if (*curbuf
->b_p_fex
!= NUL
)
2004 op_formatexpr(oap
); /* use expression */
2008 op_colon(oap
); /* use external command */
2010 op_format(oap
, FALSE
); /* use internal function */
2014 op_format(oap
, TRUE
); /* use internal function */
2018 op_function(oap
); /* call 'operatorfunc' */
2024 VIsual_reselect
= FALSE
; /* don't reselect now */
2026 #ifdef FEAT_VISUALEXTRA
2027 if (empty_region_error
)
2031 /* This is a new edit command, not a restart. Need to
2032 * remember it to make 'insertmode' work with mappings for
2033 * Visual mode. But do this only once. */
2034 restart_edit_save
= restart_edit
;
2037 op_insert(oap
, cap
->count1
);
2039 /* TODO: when inserting in several lines, should format all
2041 auto_format(FALSE
, TRUE
);
2043 if (restart_edit
== 0)
2044 restart_edit
= restart_edit_save
;
2053 VIsual_reselect
= FALSE
; /* don't reselect now */
2055 #ifdef FEAT_VISUALEXTRA
2056 if (empty_region_error
)
2059 #ifdef FEAT_VISUALEXTRA
2061 op_replace(oap
, cap
->nchar
);
2067 VIsual_reselect
= FALSE
; /* don't reselect now */
2068 foldCreate(oap
->start
.lnum
, oap
->end
.lnum
);
2072 case OP_FOLDOPENREC
:
2074 case OP_FOLDCLOSEREC
:
2075 VIsual_reselect
= FALSE
; /* don't reselect now */
2076 opFoldRange(oap
->start
.lnum
, oap
->end
.lnum
,
2077 oap
->op_type
== OP_FOLDOPEN
2078 || oap
->op_type
== OP_FOLDOPENREC
,
2079 oap
->op_type
== OP_FOLDOPENREC
2080 || oap
->op_type
== OP_FOLDCLOSEREC
,
2086 VIsual_reselect
= FALSE
; /* don't reselect now */
2087 deleteFold(oap
->start
.lnum
, oap
->end
.lnum
,
2088 oap
->op_type
== OP_FOLDDELREC
, oap
->is_VIsual
);
2094 #ifdef FEAT_VIRTUALEDIT
2100 * if 'sol' not set, go back to old column for some commands
2102 if (!p_sol
&& oap
->motion_type
== MLINE
&& !oap
->end_adjusted
2103 && (oap
->op_type
== OP_LSHIFT
|| oap
->op_type
== OP_RSHIFT
2104 || oap
->op_type
== OP_DELETE
))
2105 coladvance(curwin
->w_curswant
= old_col
);
2109 curwin
->w_cursor
= old_cursor
;
2112 oap
->block_mode
= FALSE
;
2119 * Handle indent and format operators and visual mode ":".
2125 stuffcharReadbuff(':');
2128 stuffReadbuff((char_u
*)"'<,'>");
2133 * Make the range look nice, so it can be repeated.
2135 if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2136 stuffcharReadbuff('.');
2138 stuffnumReadbuff((long)oap
->start
.lnum
);
2139 if (oap
->end
.lnum
!= oap
->start
.lnum
)
2141 stuffcharReadbuff(',');
2142 if (oap
->end
.lnum
== curwin
->w_cursor
.lnum
)
2143 stuffcharReadbuff('.');
2144 else if (oap
->end
.lnum
== curbuf
->b_ml
.ml_line_count
)
2145 stuffcharReadbuff('$');
2146 else if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2148 stuffReadbuff((char_u
*)".+");
2149 stuffnumReadbuff((long)oap
->line_count
- 1);
2152 stuffnumReadbuff((long)oap
->end
.lnum
);
2155 if (oap
->op_type
!= OP_COLON
)
2156 stuffReadbuff((char_u
*)"!");
2157 if (oap
->op_type
== OP_INDENT
)
2159 #ifndef FEAT_CINDENT
2160 if (*get_equalprg() == NUL
)
2161 stuffReadbuff((char_u
*)"indent");
2164 stuffReadbuff(get_equalprg());
2165 stuffReadbuff((char_u
*)"\n");
2167 else if (oap
->op_type
== OP_FORMAT
)
2170 stuffReadbuff((char_u
*)"fmt");
2172 stuffReadbuff(p_fp
);
2173 stuffReadbuff((char_u
*)"\n']");
2177 * do_cmdline() does the rest
2182 * Handle the "g@" operator: call 'operatorfunc'.
2192 if (*p_opfunc
== NUL
)
2193 EMSG(_("E774: 'operatorfunc' is empty"));
2196 /* Set '[ and '] marks to text to be operated on. */
2197 curbuf
->b_op_start
= oap
->start
;
2198 curbuf
->b_op_end
= oap
->end
;
2199 if (oap
->motion_type
!= MLINE
&& !oap
->inclusive
)
2200 /* Exclude the end position. */
2201 decl(&curbuf
->b_op_end
);
2203 if (oap
->block_mode
)
2204 argv
[0] = (char_u
*)"block";
2205 else if (oap
->motion_type
== MLINE
)
2206 argv
[0] = (char_u
*)"line";
2208 argv
[0] = (char_u
*)"char";
2209 (void)call_func_retnr(p_opfunc
, 1, argv
, FALSE
);
2212 EMSG(_("E775: Eval feature not available"));
2216 #if defined(FEAT_MOUSE) || defined(PROTO)
2218 * Do the appropriate action for the current mouse click in the current mode.
2219 * Not used for Command-line mode.
2222 * event modi- position visual change action
2223 * fier cursor window
2224 * left press - yes end yes
2225 * left press C yes end yes "^]" (2)
2226 * left press S yes end yes "*" (2)
2227 * left drag - yes start if moved no
2228 * left relse - yes start if moved no
2229 * middle press - yes if not active no put register
2230 * middle press - yes if active no yank and put
2231 * right press - yes start or extend yes
2232 * right press S yes no change yes "#" (2)
2233 * right drag - yes extend no
2234 * right relse - yes extend no
2236 * Insert or Replace Mode:
2237 * event modi- position visual change action
2238 * fier cursor window
2239 * left press - yes (cannot be active) yes
2240 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2241 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2242 * left drag - yes start or extend (1) no CTRL-O (1)
2243 * left relse - yes start or extend (1) no CTRL-O (1)
2244 * middle press - no (cannot be active) no put register
2245 * right press - yes start or extend yes CTRL-O
2246 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2248 * (1) only if mouse pointer moved since press
2249 * (2) only if click is in same buffer
2251 * Return TRUE if start_arrow() should be called for edit mode.
2254 do_mouse(oap
, c
, dir
, count
, fixindent
)
2255 oparg_T
*oap
; /* operator argument, can be NULL */
2256 int c
; /* K_LEFTMOUSE, etc */
2257 int dir
; /* Direction to 'put' if necessary */
2259 int fixindent
; /* PUT_FIXINDENT if fixing indent necessary */
2261 static int do_always
= FALSE
; /* ignore 'mouse' setting next time */
2262 static int got_click
= FALSE
; /* got a click some time back */
2264 int which_button
; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2265 int is_click
; /* If FALSE it's a drag or release event */
2266 int is_drag
; /* If TRUE it's a drag event */
2267 int jump_flags
= 0; /* flags for jump_to_mouse() */
2269 int moved
; /* Has cursor moved? */
2270 int in_status_line
; /* mouse in status line */
2271 #ifdef FEAT_VERTSPLIT
2272 int in_sep_line
; /* mouse in vertical separator line */
2275 #if defined(FEAT_FOLDING)
2278 win_T
*old_curwin
= curwin
;
2280 static pos_T orig_cursor
;
2281 colnr_T leftcol
, rightcol
;
2284 int old_active
= VIsual_active
;
2285 int old_mode
= VIsual_mode
;
2289 #if defined(FEAT_FOLDING)
2290 save_cursor
= curwin
->w_cursor
;
2294 * When GUI is active, always recognize mouse events, otherwise:
2295 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2296 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2297 * - For command line and insert mode 'mouse' is checked before calling
2310 if (!mouse_has(MOUSE_VISUAL
))
2315 if (State
== NORMAL
&& !mouse_has(MOUSE_NORMAL
))
2319 which_button
= get_mouse_button(KEY2TERMCAP1(c
), &is_click
, &is_drag
);
2321 #ifdef FEAT_MOUSESHAPE
2322 /* May have stopped dragging the status or separator line. The pointer is
2323 * most likely still on the status or separator line. */
2324 if (!is_drag
&& drag_status_line
)
2326 drag_status_line
= FALSE
;
2327 update_mouseshape(SHAPE_IDX_STATUS
);
2329 # ifdef FEAT_VERTSPLIT
2330 if (!is_drag
&& drag_sep_line
)
2332 drag_sep_line
= FALSE
;
2333 update_mouseshape(SHAPE_IDX_VSEP
);
2339 * Ignore drag and release events if we didn't get a click.
2345 if (!got_click
) /* didn't get click, ignore */
2347 if (!is_drag
) /* release, reset got_click */
2353 * ALT is only used for starging/extending Visual mode.
2355 if ((mod_mask
& MOD_MASK_ALT
))
2360 * CTRL right mouse button does CTRL-T
2362 if (is_click
&& (mod_mask
& MOD_MASK_CTRL
) && which_button
== MOUSE_RIGHT
)
2365 stuffcharReadbuff(Ctrl_O
);
2367 stuffnumReadbuff(count
);
2368 stuffcharReadbuff(Ctrl_T
);
2369 got_click
= FALSE
; /* ignore drag&release now */
2374 * CTRL only works with left mouse button
2376 if ((mod_mask
& MOD_MASK_CTRL
) && which_button
!= MOUSE_LEFT
)
2380 * When a modifier is down, ignore drag and release events, as well as
2381 * multiple clicks and the middle mouse button.
2382 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2384 if ((mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
| MOD_MASK_ALT
2387 || (mod_mask
& MOD_MASK_MULTI_CLICK
)
2388 || which_button
== MOUSE_MIDDLE
)
2389 && !((mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
))
2390 && mouse_model_popup()
2391 && which_button
== MOUSE_LEFT
)
2392 && !((mod_mask
& MOD_MASK_ALT
)
2393 && !mouse_model_popup()
2394 && which_button
== MOUSE_RIGHT
)
2399 * If the button press was used as the movement command for an operator
2400 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2401 * drag/release events.
2403 if (!is_click
&& which_button
== MOUSE_MIDDLE
)
2407 regname
= oap
->regname
;
2412 * Middle mouse button does a 'put' of the selected text
2414 if (which_button
== MOUSE_MIDDLE
)
2416 if (State
== NORMAL
)
2419 * If an operator was pending, we don't know what the user wanted
2420 * to do. Go back to normal mode: Clear the operator and beep().
2422 if (oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2430 * If visual was active, yank the highlighted text and put it
2431 * before the mouse pointer position.
2432 * In Select mode replace the highlighted text with the clipboard.
2438 stuffcharReadbuff(Ctrl_G
);
2439 stuffReadbuff((char_u
*)"\"+p");
2443 stuffcharReadbuff('y');
2444 stuffcharReadbuff(K_MIDDLEMOUSE
);
2446 do_always
= TRUE
; /* ignore 'mouse' setting next time */
2451 * The rest is below jump_to_mouse()
2455 else if ((State
& INSERT
) == 0)
2459 * Middle click in insert mode doesn't move the mouse, just insert the
2460 * contents of a register. '.' register is special, can't insert that
2462 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2463 * happens for the GUI).
2465 if ((State
& INSERT
) || !mouse_has(MOUSE_NORMAL
))
2468 insert_reg(regname
, TRUE
);
2471 #ifdef FEAT_CLIPBOARD
2472 if (clip_star
.available
&& regname
== 0)
2475 if ((State
& REPLACE_FLAG
) && !yank_register_mline(regname
))
2476 insert_reg(regname
, TRUE
);
2479 do_put(regname
, BACKWARD
, 1L, fixindent
| PUT_CURSEND
);
2481 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2482 AppendCharToRedobuff(Ctrl_R
);
2483 AppendCharToRedobuff(fixindent
? Ctrl_P
: Ctrl_O
);
2484 AppendCharToRedobuff(regname
== 0 ? '"' : regname
);
2491 /* When dragging or button-up stay in the same window. */
2493 jump_flags
|= MOUSE_FOCUS
| MOUSE_DID_MOVE
;
2495 start_visual
.lnum
= 0;
2498 /* Check for clicking in the tab page line. */
2499 if (mouse_row
== 0 && firstwin
->w_winrow
> 0)
2503 got_click
= FALSE
; /* ignore mouse-up and drag events */
2505 /* click in a tab selects that tab page */
2510 && mouse_col
< Columns
)
2512 c1
= TabPageIdxs
[mouse_col
];
2515 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2517 /* double click opens new page */
2520 tabpage_move(c1
== 0 ? 9999 : c1
- 1);
2524 /* Go to specified tab page, or next one if not clicking
2528 /* It's like clicking on the status line of a window. */
2529 if (curwin
!= old_curwin
)
2537 /* Close the current or specified tab page. */
2541 tp
= find_tabpage(-c1
);
2544 if (first_tabpage
->tp_next
!= NULL
)
2545 tabpage_close(FALSE
);
2547 else if (tp
!= NULL
)
2548 tabpage_close_other(tp
, FALSE
);
2556 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2557 * right button up -> pop-up menu
2558 * shift-left button -> right button
2559 * alt-left button -> alt-right button
2561 if (mouse_model_popup())
2563 if (which_button
== MOUSE_RIGHT
2564 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2567 * NOTE: Ignore right button down and drag mouse events.
2568 * Windows only shows the popup menu on the button up event.
2570 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2571 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2575 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2576 if (is_click
|| is_drag
)
2579 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2580 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2581 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2585 if (STRCMP(p_mousem
, "popup_setpos") == 0)
2587 /* First set the cursor position before showing the popup
2595 * set MOUSE_MAY_STOP_VIS if we are outside the
2596 * selection or the current window (might have false
2599 if (mouse_row
< W_WINROW(curwin
)
2601 > (W_WINROW(curwin
) + curwin
->w_height
))
2602 jump_flags
= MOUSE_MAY_STOP_VIS
;
2603 else if (get_fpos_of_mouse(&m_pos
) != IN_BUFFER
)
2604 jump_flags
= MOUSE_MAY_STOP_VIS
;
2607 if ((lt(curwin
->w_cursor
, VIsual
)
2608 && (lt(m_pos
, curwin
->w_cursor
)
2609 || lt(VIsual
, m_pos
)))
2610 || (lt(VIsual
, curwin
->w_cursor
)
2611 && (lt(m_pos
, VIsual
)
2612 || lt(curwin
->w_cursor
, m_pos
))))
2614 jump_flags
= MOUSE_MAY_STOP_VIS
;
2616 else if (VIsual_mode
== Ctrl_V
)
2618 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
,
2619 &leftcol
, &rightcol
);
2620 getvcol(curwin
, &m_pos
, NULL
, &m_pos
.col
, NULL
);
2621 if (m_pos
.col
< leftcol
|| m_pos
.col
> rightcol
)
2622 jump_flags
= MOUSE_MAY_STOP_VIS
;
2627 jump_flags
= MOUSE_MAY_STOP_VIS
;
2632 jump_flags
= jump_to_mouse(jump_flags
, NULL
, which_button
);
2635 VIsual_active
? INVERTED
:
2639 out_flush(); /* Update before showing popup menu */
2642 gui_show_popupmenu();
2644 return (jump_flags
& CURSOR_MOVED
) != 0;
2652 if (which_button
== MOUSE_LEFT
2653 && (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
)))
2655 which_button
= MOUSE_RIGHT
;
2656 mod_mask
&= ~MOD_MASK_SHIFT
;
2661 if ((State
& (NORMAL
| INSERT
))
2662 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2664 if (which_button
== MOUSE_LEFT
)
2668 /* stop Visual mode for a left click in a window, but not when
2669 * on a status line */
2671 jump_flags
|= MOUSE_MAY_STOP_VIS
;
2673 else if (mouse_has(MOUSE_VISUAL
))
2674 jump_flags
|= MOUSE_MAY_VIS
;
2676 else if (which_button
== MOUSE_RIGHT
)
2678 if (is_click
&& VIsual_active
)
2681 * Remember the start and end of visual before moving the
2684 if (lt(curwin
->w_cursor
, VIsual
))
2686 start_visual
= curwin
->w_cursor
;
2687 end_visual
= VIsual
;
2691 start_visual
= VIsual
;
2692 end_visual
= curwin
->w_cursor
;
2695 jump_flags
|= MOUSE_FOCUS
;
2696 if (mouse_has(MOUSE_VISUAL
))
2697 jump_flags
|= MOUSE_MAY_VIS
;
2703 * If an operator is pending, ignore all drags and releases until the
2706 if (!is_drag
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2709 oap
->motion_type
= MCHAR
;
2712 /* When releasing the button let jump_to_mouse() know. */
2713 if (!is_click
&& !is_drag
)
2714 jump_flags
|= MOUSE_RELEASED
;
2719 jump_flags
= jump_to_mouse(jump_flags
,
2720 oap
== NULL
? NULL
: &(oap
->inclusive
), which_button
);
2721 moved
= (jump_flags
& CURSOR_MOVED
);
2722 in_status_line
= (jump_flags
& IN_STATUS_LINE
);
2723 #ifdef FEAT_VERTSPLIT
2724 in_sep_line
= (jump_flags
& IN_SEP_LINE
);
2727 #ifdef FEAT_NETBEANS_INTG
2728 if (usingNetbeans
&& isNetbeansBuffer(curbuf
)
2729 && !(jump_flags
& (IN_STATUS_LINE
| IN_SEP_LINE
)))
2731 int key
= KEY2TERMCAP1(c
);
2733 if (key
== (int)KE_LEFTRELEASE
|| key
== (int)KE_MIDDLERELEASE
2734 || key
== (int)KE_RIGHTRELEASE
)
2735 netbeans_button_release(which_button
);
2739 /* When jumping to another window, clear a pending operator. That's a bit
2740 * friendlier than beeping and not jumping to that window. */
2741 if (curwin
!= old_curwin
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2747 && (jump_flags
& (MOUSE_FOLD_CLOSE
| MOUSE_FOLD_OPEN
))
2748 && which_button
== MOUSE_LEFT
)
2750 /* open or close a fold at this line */
2751 if (jump_flags
& MOUSE_FOLD_OPEN
)
2752 openFold(curwin
->w_cursor
.lnum
, 1L);
2754 closeFold(curwin
->w_cursor
.lnum
, 1L);
2755 /* don't move the cursor if still in the same window */
2756 if (curwin
== old_curwin
)
2757 curwin
->w_cursor
= save_cursor
;
2761 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2762 if ((jump_flags
& IN_OTHER_WIN
) && !VIsual_active
&& clip_star
.available
)
2764 clip_modeless(which_button
, is_click
, is_drag
);
2770 /* Set global flag that we are extending the Visual area with mouse
2771 * dragging; temporarily minimize 'scrolloff'. */
2772 if (VIsual_active
&& is_drag
&& p_so
)
2774 /* In the very first line, allow scrolling one line */
2781 /* When dragging the mouse above the window, scroll down. */
2782 if (is_drag
&& mouse_row
< 0 && !in_status_line
)
2784 scroll_redraw(FALSE
, 1L);
2788 if (start_visual
.lnum
) /* right click in visual mode */
2790 /* When ALT is pressed make Visual mode blockwise. */
2791 if (mod_mask
& MOD_MASK_ALT
)
2792 VIsual_mode
= Ctrl_V
;
2795 * In Visual-block mode, divide the area in four, pick up the corner
2796 * that is in the quarter that the cursor is in.
2798 if (VIsual_mode
== Ctrl_V
)
2800 getvcols(curwin
, &start_visual
, &end_visual
, &leftcol
, &rightcol
);
2801 if (curwin
->w_curswant
> (leftcol
+ rightcol
) / 2)
2802 end_visual
.col
= leftcol
;
2804 end_visual
.col
= rightcol
;
2805 if (curwin
->w_cursor
.lnum
<
2806 (start_visual
.lnum
+ end_visual
.lnum
) / 2)
2807 end_visual
.lnum
= end_visual
.lnum
;
2809 end_visual
.lnum
= start_visual
.lnum
;
2811 /* move VIsual to the right column */
2812 start_visual
= curwin
->w_cursor
; /* save the cursor pos */
2813 curwin
->w_cursor
= end_visual
;
2814 coladvance(end_visual
.col
);
2815 VIsual
= curwin
->w_cursor
;
2816 curwin
->w_cursor
= start_visual
; /* restore the cursor */
2821 * If the click is before the start of visual, change the start.
2822 * If the click is after the end of visual, change the end. If
2823 * the click is inside the visual, change the closest side.
2825 if (lt(curwin
->w_cursor
, start_visual
))
2826 VIsual
= end_visual
;
2827 else if (lt(end_visual
, curwin
->w_cursor
))
2828 VIsual
= start_visual
;
2831 /* In the same line, compare column number */
2832 if (end_visual
.lnum
== start_visual
.lnum
)
2834 if (curwin
->w_cursor
.col
- start_visual
.col
>
2835 end_visual
.col
- curwin
->w_cursor
.col
)
2836 VIsual
= start_visual
;
2838 VIsual
= end_visual
;
2841 /* In different lines, compare line number */
2844 diff
= (curwin
->w_cursor
.lnum
- start_visual
.lnum
) -
2845 (end_visual
.lnum
- curwin
->w_cursor
.lnum
);
2847 if (diff
> 0) /* closest to end */
2848 VIsual
= start_visual
;
2849 else if (diff
< 0) /* closest to start */
2850 VIsual
= end_visual
;
2851 else /* in the middle line */
2853 if (curwin
->w_cursor
.col
<
2854 (start_visual
.col
+ end_visual
.col
) / 2)
2855 VIsual
= end_visual
;
2857 VIsual
= start_visual
;
2864 * If Visual mode started in insert mode, execute "CTRL-O"
2866 else if ((State
& INSERT
) && VIsual_active
)
2867 stuffcharReadbuff(Ctrl_O
);
2871 * Middle mouse click: Put text before cursor.
2873 if (which_button
== MOUSE_MIDDLE
)
2875 #ifdef FEAT_CLIPBOARD
2876 if (clip_star
.available
&& regname
== 0)
2879 if (yank_register_mline(regname
))
2881 if (mouse_past_bottom
)
2884 else if (mouse_past_eol
)
2889 c1
= (dir
== BACKWARD
) ? '[' : ']';
2894 c1
= (dir
== FORWARD
) ? 'p' : 'P';
2897 prep_redo(regname
, count
, NUL
, c1
, NUL
, c2
, NUL
);
2900 * Remember where the paste started, so in edit() Insstart can be set
2903 if (restart_edit
!= 0)
2904 where_paste_started
= curwin
->w_cursor
;
2905 do_put(regname
, dir
, count
, fixindent
| PUT_CURSEND
);
2908 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2910 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2911 * error under the mouse pointer.
2913 else if (((mod_mask
& MOD_MASK_CTRL
)
2914 || (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2915 && bt_quickfix(curbuf
))
2918 stuffcharReadbuff(Ctrl_O
);
2919 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
2920 stuffReadbuff((char_u
*)":.cc\n");
2921 else /* location list window */
2922 stuffReadbuff((char_u
*)":.ll\n");
2923 got_click
= FALSE
; /* ignore drag&release now */
2928 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2929 * under the mouse pointer.
2931 else if ((mod_mask
& MOD_MASK_CTRL
) || (curbuf
->b_help
2932 && (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
))
2935 stuffcharReadbuff(Ctrl_O
);
2936 stuffcharReadbuff(Ctrl_RSB
);
2937 got_click
= FALSE
; /* ignore drag&release now */
2941 * Shift-Mouse click searches for the next occurrence of the word under
2944 else if ((mod_mask
& MOD_MASK_SHIFT
))
2948 || (VIsual_active
&& VIsual_select
)
2951 stuffcharReadbuff(Ctrl_O
);
2952 if (which_button
== MOUSE_LEFT
)
2953 stuffcharReadbuff('*');
2954 else /* MOUSE_RIGHT */
2955 stuffcharReadbuff('#');
2958 /* Handle double clicks, unless on status line */
2959 else if (in_status_line
)
2961 #ifdef FEAT_MOUSESHAPE
2962 if ((is_drag
|| is_click
) && !drag_status_line
)
2964 drag_status_line
= TRUE
;
2965 update_mouseshape(-1);
2969 #ifdef FEAT_VERTSPLIT
2970 else if (in_sep_line
)
2972 # ifdef FEAT_MOUSESHAPE
2973 if ((is_drag
|| is_click
) && !drag_sep_line
)
2975 drag_sep_line
= TRUE
;
2976 update_mouseshape(-1);
2982 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) && (State
& (NORMAL
| INSERT
))
2983 && mouse_has(MOUSE_VISUAL
))
2985 if (is_click
|| !VIsual_active
)
2988 orig_cursor
= VIsual
;
2991 check_visual_highlight();
2992 VIsual
= curwin
->w_cursor
;
2993 orig_cursor
= VIsual
;
2994 VIsual_active
= TRUE
;
2995 VIsual_reselect
= TRUE
;
2996 /* start Select mode if 'selectmode' contains "mouse" */
2997 may_start_select('o');
3000 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3002 /* Double click with ALT pressed makes it blockwise. */
3003 if (mod_mask
& MOD_MASK_ALT
)
3004 VIsual_mode
= Ctrl_V
;
3008 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_3CLICK
)
3010 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_4CLICK
)
3011 VIsual_mode
= Ctrl_V
;
3012 #ifdef FEAT_CLIPBOARD
3013 /* Make sure the clipboard gets updated. Needed because start and
3014 * end may still be the same, and the selection needs to be owned */
3015 clip_star
.vmode
= NUL
;
3019 * A double click selects a word or a block.
3021 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3028 /* If the character under the cursor (skipping white space) is
3029 * not a word character, try finding a match and select a (),
3030 * {}, [], #if/#endif, etc. block. */
3031 end_visual
= curwin
->w_cursor
;
3032 while (gc
= gchar_pos(&end_visual
), vim_iswhite(gc
))
3035 oap
->motion_type
= MCHAR
;
3037 && VIsual_mode
== 'v'
3038 && !vim_iswordc(gchar_pos(&end_visual
))
3039 && equalpos(curwin
->w_cursor
, VIsual
)
3040 && (pos
= findmatch(oap
, NUL
)) != NULL
)
3042 curwin
->w_cursor
= *pos
;
3043 if (oap
->motion_type
== MLINE
)
3045 else if (*p_sel
== 'e')
3047 if (lt(curwin
->w_cursor
, VIsual
))
3050 ++curwin
->w_cursor
.col
;
3055 if (pos
== NULL
&& (is_click
|| is_drag
))
3057 /* When not found a match or when dragging: extend to include
3059 if (lt(curwin
->w_cursor
, orig_cursor
))
3061 find_start_of_word(&curwin
->w_cursor
);
3062 find_end_of_word(&VIsual
);
3066 find_start_of_word(&VIsual
);
3067 if (*p_sel
== 'e' && *ml_get_cursor() != NUL
)
3069 curwin
->w_cursor
.col
+=
3070 (*mb_ptr2len
)(ml_get_cursor());
3072 ++curwin
->w_cursor
.col
;
3074 find_end_of_word(&curwin
->w_cursor
);
3077 curwin
->w_set_curswant
= TRUE
;
3080 redraw_curbuf_later(INVERTED
); /* update the inversion */
3082 else if (VIsual_active
&& !old_active
)
3084 if (mod_mask
& MOD_MASK_ALT
)
3085 VIsual_mode
= Ctrl_V
;
3090 /* If Visual mode changed show it later. */
3091 if ((!VIsual_active
&& old_active
&& mode_displayed
)
3092 || (VIsual_active
&& p_smd
&& msg_silent
== 0
3093 && (!old_active
|| VIsual_mode
!= old_mode
)))
3094 redraw_cmdline
= TRUE
;
3102 * Move "pos" back to the start of the word it's in.
3105 find_start_of_word(pos
)
3112 line
= ml_get(pos
->lnum
);
3113 cclass
= get_mouse_class(line
+ pos
->col
);
3115 while (pos
->col
> 0)
3119 col
-= (*mb_head_off
)(line
, line
+ col
);
3121 if (get_mouse_class(line
+ col
) != cclass
)
3128 * Move "pos" forward to the end of the word it's in.
3129 * When 'selection' is "exclusive", the position is just after the word.
3132 find_end_of_word(pos
)
3139 line
= ml_get(pos
->lnum
);
3140 if (*p_sel
== 'e' && pos
->col
> 0)
3144 pos
->col
-= (*mb_head_off
)(line
, line
+ pos
->col
);
3147 cclass
= get_mouse_class(line
+ pos
->col
);
3148 while (line
[pos
->col
] != NUL
)
3151 col
= pos
->col
+ (*mb_ptr2len
)(line
+ pos
->col
);
3155 if (get_mouse_class(line
+ col
) != cclass
)
3166 * Get class of a character for selection: same class means same word.
3168 * 1: punctuation groups
3169 * 2: normal word character
3170 * >2: multi-byte word character.
3179 if (has_mbyte
&& MB_BYTE2LEN(p
[0]) > 1)
3180 return mb_get_class(p
);
3184 if (c
== ' ' || c
== '\t')
3191 * There are a few special cases where we want certain combinations of
3192 * characters to be considered as a single word. These are things like
3193 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3194 * character is in it's own class.
3196 if (c
!= NUL
&& vim_strchr((char_u
*)"-+*/%<>&|^!=", c
) != NULL
)
3200 #endif /* FEAT_VISUAL */
3201 #endif /* FEAT_MOUSE */
3203 #if defined(FEAT_VISUAL) || defined(PROTO)
3205 * Check if highlighting for visual mode is possible, give a warning message
3209 check_visual_highlight()
3211 static int did_check
= FALSE
;
3215 if (!did_check
&& hl_attr(HLF_V
) == 0)
3216 MSG(_("Warning: terminal cannot highlight"));
3223 * This function should ALWAYS be called to end Visual mode, except from
3224 * do_pending_operator().
3229 #ifdef FEAT_CLIPBOARD
3231 * If we are using the clipboard, then remember what was selected in case
3232 * we need to paste it somewhere while we still own the selection.
3233 * Only do this when the clipboard is already owned. Don't want to grab
3234 * the selection when hitting ESC.
3236 if (clip_star
.available
&& clip_star
.owned
)
3240 VIsual_active
= FALSE
;
3246 /* Save the current VIsual area for '< and '> marks, and "gv" */
3247 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
3248 curbuf
->b_visual
.vi_start
= VIsual
;
3249 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
3250 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
3252 curbuf
->b_visual_mode_eval
= VIsual_mode
;
3254 #ifdef FEAT_VIRTUALEDIT
3255 if (!virtual_active())
3256 curwin
->w_cursor
.coladd
= 0;
3260 clear_cmdline
= TRUE
; /* unshow visual mode later */
3261 #ifdef FEAT_CMDL_INFO
3266 adjust_cursor_eol();
3270 * Reset VIsual_active and VIsual_reselect.
3273 reset_VIsual_and_resel()
3278 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3280 VIsual_reselect
= FALSE
;
3284 * Reset VIsual_active and VIsual_reselect if it's set.
3292 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3293 VIsual_reselect
= FALSE
;
3296 #endif /* FEAT_VISUAL */
3298 #if defined(FEAT_BEVAL)
3299 static int find_is_eval_item
__ARGS((char_u
*ptr
, int *colp
, int *nbp
, int dir
));
3302 * Check for a balloon-eval special item to include when searching for an
3303 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3304 * Returns TRUE if the character at "*ptr" should be included.
3305 * "dir" is FORWARD or BACKWARD, the direction of searching.
3306 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3307 * "bnp" points to a counter for square brackets.
3310 find_is_eval_item(ptr
, colp
, bnp
, dir
)
3316 /* Accept everything inside []. */
3317 if ((*ptr
== ']' && dir
== BACKWARD
) || (*ptr
== '[' && dir
== FORWARD
))
3321 if ((*ptr
== '[' && dir
== BACKWARD
) || (*ptr
== ']' && dir
== FORWARD
))
3326 /* skip over "s.var" */
3330 /* two-character item: s->var */
3331 if (ptr
[dir
== BACKWARD
? 0 : 1] == '>'
3332 && ptr
[dir
== BACKWARD
? -1 : 0] == '-')
3342 * Find the identifier under or to the right of the cursor.
3343 * "find_type" can have one of three values:
3344 * FIND_IDENT: find an identifier (keyword)
3345 * FIND_STRING: find any non-white string
3346 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3347 * FIND_EVAL: find text useful for C program debugging
3349 * There are three steps:
3350 * 1. Search forward for the start of an identifier/string. Doesn't move if
3352 * 2. Search backward for the start of this identifier/string.
3353 * This doesn't match the real Vi but I like it a little better and it
3354 * shouldn't bother anyone.
3355 * 3. Search forward to the end of this identifier/string.
3356 * When FIND_IDENT isn't defined, we backup until a blank.
3358 * Returns the length of the string, or zero if no string is found.
3359 * If a string is found, a pointer to the string is put in "*string". This
3360 * string is not always NUL terminated.
3363 find_ident_under_cursor(string
, find_type
)
3367 return find_ident_at_pos(curwin
, curwin
->w_cursor
.lnum
,
3368 curwin
->w_cursor
.col
, string
, find_type
);
3372 * Like find_ident_under_cursor(), but for any window and any position.
3373 * However: Uses 'iskeyword' from the current window!.
3376 find_ident_at_pos(wp
, lnum
, startcol
, string
, find_type
)
3384 int col
= 0; /* init to shut up GCC */
3391 #if defined(FEAT_BEVAL)
3392 int bn
= 0; /* bracket nesting */
3396 * if i == 0: try to find an identifier
3397 * if i == 1: try to find any non-white string
3399 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
3400 for (i
= (find_type
& FIND_IDENT
) ? 0 : 1; i
< 2; ++i
)
3403 * 1. skip to start of identifier/string
3409 while (ptr
[col
] != NUL
)
3411 # if defined(FEAT_BEVAL)
3412 /* Stop at a ']' to evaluate "a[x]". */
3413 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3416 this_class
= mb_get_class(ptr
+ col
);
3417 if (this_class
!= 0 && (i
== 1 || this_class
!= 1))
3419 col
+= (*mb_ptr2len
)(ptr
+ col
);
3424 while (ptr
[col
] != NUL
3425 && (i
== 0 ? !vim_iswordc(ptr
[col
]) : vim_iswhite(ptr
[col
]))
3426 # if defined(FEAT_BEVAL)
3427 && (!(find_type
& FIND_EVAL
) || ptr
[col
] != ']')
3432 #if defined(FEAT_BEVAL)
3433 /* When starting on a ']' count it, so that we include the '['. */
3434 bn
= ptr
[col
] == ']';
3438 * 2. Back up to start of identifier/string.
3443 /* Remember class of character under cursor. */
3444 # if defined(FEAT_BEVAL)
3445 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3446 this_class
= mb_get_class((char_u
*)"a");
3449 this_class
= mb_get_class(ptr
+ col
);
3450 while (col
> 0 && this_class
!= 0)
3452 prevcol
= col
- 1 - (*mb_head_off
)(ptr
, ptr
+ col
- 1);
3453 prev_class
= mb_get_class(ptr
+ prevcol
);
3454 if (this_class
!= prev_class
3457 || (find_type
& FIND_IDENT
))
3458 # if defined(FEAT_BEVAL)
3459 && (!(find_type
& FIND_EVAL
)
3461 || !find_is_eval_item(ptr
+ prevcol
, &prevcol
,
3469 /* If we don't want just any old string, or we've found an
3470 * identifier, stop searching. */
3473 if (!(find_type
& FIND_STRING
) || this_class
== 2)
3481 ? vim_iswordc(ptr
[col
- 1])
3482 : (!vim_iswhite(ptr
[col
- 1])
3483 && (!(find_type
& FIND_IDENT
)
3484 || !vim_iswordc(ptr
[col
- 1]))))
3485 #if defined(FEAT_BEVAL)
3486 || ((find_type
& FIND_EVAL
)
3488 && find_is_eval_item(ptr
+ col
- 1, &col
,
3494 /* If we don't want just any old string, or we've found an
3495 * identifier, stop searching. */
3496 if (!(find_type
& FIND_STRING
) || vim_iswordc(ptr
[col
]))
3501 if (ptr
[col
] == NUL
|| (i
== 0 && (
3503 has_mbyte
? this_class
!= 2 :
3505 !vim_iswordc(ptr
[col
]))))
3508 * didn't find an identifier or string
3510 if (find_type
& FIND_STRING
)
3511 EMSG(_("E348: No string under cursor"));
3513 EMSG(_("E349: No identifier under cursor"));
3520 * 3. Find the end if the identifier/string.
3522 #if defined(FEAT_BEVAL)
3530 /* Search for point of changing multibyte character class. */
3531 this_class
= mb_get_class(ptr
);
3532 while (ptr
[col
] != NUL
3533 && ((i
== 0 ? mb_get_class(ptr
+ col
) == this_class
3534 : mb_get_class(ptr
+ col
) != 0)
3535 # if defined(FEAT_BEVAL)
3536 || ((find_type
& FIND_EVAL
)
3537 && col
<= (int)startcol
3538 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3541 col
+= (*mb_ptr2len
)(ptr
+ col
);
3545 while ((i
== 0 ? vim_iswordc(ptr
[col
])
3546 : (ptr
[col
] != NUL
&& !vim_iswhite(ptr
[col
])))
3547 # if defined(FEAT_BEVAL)
3548 || ((find_type
& FIND_EVAL
)
3549 && col
<= (int)startcol
3550 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3561 * Prepare for redo of a normal command.
3567 prep_redo(cap
->oap
->regname
, cap
->count0
,
3568 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
3572 * Prepare for redo of any command.
3573 * Note that only the last argument can be a multi-byte char.
3576 prep_redo(regname
, num
, cmd1
, cmd2
, cmd3
, cmd4
, cmd5
)
3586 if (regname
!= 0) /* yank from specified buffer */
3588 AppendCharToRedobuff('"');
3589 AppendCharToRedobuff(regname
);
3592 AppendNumberToRedobuff(num
);
3595 AppendCharToRedobuff(cmd1
);
3597 AppendCharToRedobuff(cmd2
);
3599 AppendCharToRedobuff(cmd3
);
3601 AppendCharToRedobuff(cmd4
);
3603 AppendCharToRedobuff(cmd5
);
3607 * check for operator active and clear it
3609 * return TRUE if operator was active
3615 if (oap
->op_type
== OP_NOP
)
3622 * Check for operator or Visual active. Clear active operator.
3624 * Return TRUE if operator or Visual was active.
3630 if (oap
->op_type
== OP_NOP
3644 oap
->op_type
= OP_NOP
;
3646 oap
->motion_force
= NUL
;
3647 oap
->use_reg_one
= FALSE
;
3660 * Remove the shift modifier from a special key.
3663 unshift_special(cap
)
3666 switch (cap
->cmdchar
)
3668 case K_S_RIGHT
: cap
->cmdchar
= K_RIGHT
; break;
3669 case K_S_LEFT
: cap
->cmdchar
= K_LEFT
; break;
3670 case K_S_UP
: cap
->cmdchar
= K_UP
; break;
3671 case K_S_DOWN
: cap
->cmdchar
= K_DOWN
; break;
3672 case K_S_HOME
: cap
->cmdchar
= K_HOME
; break;
3673 case K_S_END
: cap
->cmdchar
= K_END
; break;
3675 cap
->cmdchar
= simplify_key(cap
->cmdchar
, &mod_mask
);
3679 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3681 * Routines for displaying a partly typed command
3684 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3685 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3687 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3689 static char_u showcmd_buf
[SHOWCMD_BUFLEN
];
3690 static char_u old_showcmd_buf
[SHOWCMD_BUFLEN
]; /* For push_showcmd() */
3691 static int showcmd_is_clear
= TRUE
;
3692 static int showcmd_visual
= FALSE
;
3694 static void display_showcmd
__ARGS((void));
3703 if (VIsual_active
&& !char_avail())
3705 int i
= lt(VIsual
, curwin
->w_cursor
);
3707 colnr_T leftcol
, rightcol
;
3710 /* Show the size of the Visual area. */
3714 bot
= curwin
->w_cursor
.lnum
;
3718 top
= curwin
->w_cursor
.lnum
;
3721 # ifdef FEAT_FOLDING
3722 /* Include closed folds as a whole. */
3723 hasFolding(top
, &top
, NULL
);
3724 hasFolding(bot
, NULL
, &bot
);
3726 lines
= bot
- top
+ 1;
3728 if (VIsual_mode
== Ctrl_V
)
3730 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
, &leftcol
, &rightcol
);
3731 sprintf((char *)showcmd_buf
, "%ldx%ld", lines
,
3732 (long)(rightcol
- leftcol
+ 1));
3734 else if (VIsual_mode
== 'V' || VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
3735 sprintf((char *)showcmd_buf
, "%ld", lines
);
3737 sprintf((char *)showcmd_buf
, "%ld", (long)(i
3738 ? curwin
->w_cursor
.col
- VIsual
.col
3739 : VIsual
.col
- curwin
->w_cursor
.col
) + (*p_sel
!= 'e'));
3740 showcmd_buf
[SHOWCMD_COLS
] = NUL
; /* truncate */
3741 showcmd_visual
= TRUE
;
3746 showcmd_buf
[0] = NUL
;
3747 showcmd_visual
= FALSE
;
3749 /* Don't actually display something if there is nothing to clear. */
3750 if (showcmd_is_clear
)
3758 * Add 'c' to string of shown command chars.
3759 * Return TRUE if output has been written (and setcursor() has been called).
3769 #if defined(FEAT_MOUSE)
3771 static int ignore
[] =
3774 K_VER_SCROLLBAR
, K_HOR_SCROLLBAR
,
3775 K_LEFTMOUSE_NM
, K_LEFTRELEASE_NM
,
3778 K_LEFTMOUSE
, K_LEFTDRAG
, K_LEFTRELEASE
,
3779 K_MIDDLEMOUSE
, K_MIDDLEDRAG
, K_MIDDLERELEASE
,
3780 K_RIGHTMOUSE
, K_RIGHTDRAG
, K_RIGHTRELEASE
,
3781 K_MOUSEDOWN
, K_MOUSEUP
,
3782 K_X1MOUSE
, K_X1DRAG
, K_X1RELEASE
, K_X2MOUSE
, K_X2DRAG
, K_X2RELEASE
,
3788 if (!p_sc
|| msg_silent
!= 0)
3793 showcmd_buf
[0] = NUL
;
3794 showcmd_visual
= FALSE
;
3797 #if defined(FEAT_MOUSE)
3798 /* Ignore keys that are scrollbar updates and mouse clicks */
3800 for (i
= 0; ignore
[i
] != 0; ++i
)
3806 old_len
= (int)STRLEN(showcmd_buf
);
3807 extra_len
= (int)STRLEN(p
);
3808 overflow
= old_len
+ extra_len
- SHOWCMD_COLS
;
3810 mch_memmove(showcmd_buf
, showcmd_buf
+ overflow
,
3811 old_len
- overflow
+ 1);
3812 STRCAT(showcmd_buf
, p
);
3826 if (!add_to_showcmd(c
))
3831 * Delete 'len' characters from the end of the shown command.
3834 del_from_showcmd(len
)
3842 old_len
= (int)STRLEN(showcmd_buf
);
3845 showcmd_buf
[old_len
- len
] = NUL
;
3852 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3853 * something and there is a partial mapping.
3859 STRCPY(old_showcmd_buf
, showcmd_buf
);
3868 STRCPY(showcmd_buf
, old_showcmd_buf
);
3880 len
= (int)STRLEN(showcmd_buf
);
3882 showcmd_is_clear
= TRUE
;
3885 screen_puts(showcmd_buf
, (int)Rows
- 1, sc_col
, 0);
3886 showcmd_is_clear
= FALSE
;
3890 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3893 screen_puts((char_u
*)" " + len
, (int)Rows
- 1, sc_col
+ len
, 0);
3895 setcursor(); /* put cursor back where it belongs */
3899 #ifdef FEAT_SCROLLBIND
3901 * When "check" is FALSE, prepare for commands that scroll the window.
3902 * When "check" is TRUE, take care of scroll-binding after the window has
3903 * scrolled. Called from normal_cmd() and edit().
3906 do_check_scrollbind(check
)
3909 static win_T
*old_curwin
= NULL
;
3910 static linenr_T old_topline
= 0;
3912 static int old_topfill
= 0;
3914 static buf_T
*old_buf
= NULL
;
3915 static colnr_T old_leftcol
= 0;
3917 if (check
&& curwin
->w_p_scb
)
3919 /* If a ":syncbind" command was just used, don't scroll, only reset
3922 did_syncbind
= FALSE
;
3923 else if (curwin
== old_curwin
)
3926 * Synchronize other windows, as necessary according to
3927 * 'scrollbind'. Don't do this after an ":edit" command, except
3928 * when 'diff' is set.
3930 if ((curwin
->w_buffer
== old_buf
3935 && (curwin
->w_topline
!= old_topline
3937 || curwin
->w_topfill
!= old_topfill
3939 || curwin
->w_leftcol
!= old_leftcol
))
3941 check_scrollbind(curwin
->w_topline
- old_topline
,
3942 (long)(curwin
->w_leftcol
- old_leftcol
));
3945 else if (vim_strchr(p_sbo
, 'j')) /* jump flag set in 'scrollopt' */
3948 * When switching between windows, make sure that the relative
3949 * vertical offset is valid for the new window. The relative
3950 * offset is invalid whenever another 'scrollbind' window has
3951 * scrolled to a point that would force the current window to
3952 * scroll past the beginning or end of its buffer. When the
3953 * resync is performed, some of the other 'scrollbind' windows may
3954 * need to jump so that the current window's relative position is
3955 * visible on-screen.
3957 check_scrollbind(curwin
->w_topline
- curwin
->w_scbind_pos
, 0L);
3959 curwin
->w_scbind_pos
= curwin
->w_topline
;
3962 old_curwin
= curwin
;
3963 old_topline
= curwin
->w_topline
;
3965 old_topfill
= curwin
->w_topfill
;
3967 old_buf
= curwin
->w_buffer
;
3968 old_leftcol
= curwin
->w_leftcol
;
3972 * Synchronize any windows that have "scrollbind" set, based on the
3973 * number of rows by which the current window has changed
3974 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3977 check_scrollbind(topline_diff
, leftcol_diff
)
3978 linenr_T topline_diff
;
3983 win_T
*old_curwin
= curwin
;
3984 buf_T
*old_curbuf
= curbuf
;
3986 int old_VIsual_select
= VIsual_select
;
3987 int old_VIsual_active
= VIsual_active
;
3989 colnr_T tgt_leftcol
= curwin
->w_leftcol
;
3994 * check 'scrollopt' string for vertical and horizontal scroll options
3996 want_ver
= (vim_strchr(p_sbo
, 'v') && topline_diff
!= 0);
3998 want_ver
|= old_curwin
->w_p_diff
;
4000 want_hor
= (vim_strchr(p_sbo
, 'h') && (leftcol_diff
|| topline_diff
!= 0));
4003 * loop through the scrollbound windows and scroll accordingly
4006 VIsual_select
= VIsual_active
= 0;
4008 for (curwin
= firstwin
; curwin
; curwin
= curwin
->w_next
)
4010 curbuf
= curwin
->w_buffer
;
4011 /* skip original window and windows with 'noscrollbind' */
4012 if (curwin
!= old_curwin
&& curwin
->w_p_scb
)
4015 * do the vertical scroll
4020 if (old_curwin
->w_p_diff
&& curwin
->w_p_diff
)
4022 diff_set_topline(old_curwin
, curwin
);
4027 curwin
->w_scbind_pos
+= topline_diff
;
4028 topline
= curwin
->w_scbind_pos
;
4029 if (topline
> curbuf
->b_ml
.ml_line_count
)
4030 topline
= curbuf
->b_ml
.ml_line_count
;
4034 y
= topline
- curwin
->w_topline
;
4038 scrolldown(-y
, FALSE
);
4041 redraw_later(VALID
);
4044 curwin
->w_redr_status
= TRUE
;
4049 * do the horizontal scroll
4051 if (want_hor
&& curwin
->w_leftcol
!= tgt_leftcol
)
4053 curwin
->w_leftcol
= tgt_leftcol
;
4060 * reset current-window
4063 VIsual_select
= old_VIsual_select
;
4064 VIsual_active
= old_VIsual_active
;
4066 curwin
= old_curwin
;
4067 curbuf
= old_curbuf
;
4069 #endif /* #ifdef FEAT_SCROLLBIND */
4072 * Command character that's ignored.
4073 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4080 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */
4084 * Command character that doesn't do anything, but unlike nv_ignore() does
4085 * start edit(). Used for "startinsert" executed while starting up.
4095 * Command character doesn't exist.
4101 clearopbeep(cap
->oap
);
4105 * <Help> and <F1> commands.
4111 if (!checkclearopq(cap
->oap
))
4116 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4122 if (!checkclearopq(cap
->oap
)
4123 && do_addsub((int)cap
->cmdchar
, cap
->count1
) == OK
)
4128 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4134 if (!checkclearop(cap
->oap
))
4137 if (mod_mask
& MOD_MASK_CTRL
)
4139 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4140 if (cap
->arg
== BACKWARD
)
4141 goto_tabpage(-(int)cap
->count1
);
4143 goto_tabpage((int)cap
->count0
);
4147 (void)onepage(cap
->arg
, cap
->count1
);
4152 * Implementation of "gd" and "gD" command.
4155 nv_gd(oap
, nchar
, thisblock
)
4158 int thisblock
; /* 1 for "1gd" and "1gD" */
4163 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0
4164 || find_decl(ptr
, len
, nchar
== 'd', thisblock
, 0) == FAIL
)
4167 else if ((fdo_flags
& FDO_SEARCH
) && KeyTyped
&& oap
->op_type
== OP_NOP
)
4173 * Search for variable declaration of "ptr[len]".
4174 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4175 * current file ("gD").
4176 * When "thisblock" is TRUE check the {} block scope.
4177 * Return FAIL when not found.
4180 find_decl(ptr
, len
, locally
, thisblock
, searchflags
)
4185 int searchflags
; /* flags passed to searchit() */
4197 if ((pat
= alloc(len
+ 7)) == NULL
)
4200 /* Put "\V" before the pattern to avoid that the special meaning of "."
4201 * and "~" causes trouble. */
4202 sprintf((char *)pat
, vim_iswordp(ptr
) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4204 old_pos
= curwin
->w_cursor
;
4207 p_ws
= FALSE
; /* don't wrap around end of file now */
4208 p_scs
= FALSE
; /* don't switch ignorecase off now */
4211 * With "gD" go to line 1.
4212 * With "gd" Search back for the start of the current function, then go
4213 * back until a blank line. If this fails go to line 1.
4215 if (!locally
|| !findpar(&incll
, BACKWARD
, 1L, '{', FALSE
))
4217 setpcmark(); /* Set in findpar() otherwise */
4218 curwin
->w_cursor
.lnum
= 1;
4219 par_pos
= curwin
->w_cursor
;
4223 par_pos
= curwin
->w_cursor
;
4224 while (curwin
->w_cursor
.lnum
> 1 && *skipwhite(ml_get_curline()) != NUL
)
4225 --curwin
->w_cursor
.lnum
;
4227 curwin
->w_cursor
.col
= 0;
4229 /* Search forward for the identifier, ignore comment lines. */
4230 clearpos(&found_pos
);
4233 t
= searchit(curwin
, curbuf
, &curwin
->w_cursor
, FORWARD
,
4234 pat
, 1L, searchflags
, RE_LAST
, (linenr_T
)0, NULL
);
4235 if (curwin
->w_cursor
.lnum
>= old_pos
.lnum
)
4236 t
= FAIL
; /* match after start is failure too */
4238 if (thisblock
&& t
!= FAIL
)
4242 /* Check that the block the match is in doesn't end before the
4243 * position where we started the search from. */
4244 if ((pos
= findmatchlimit(NULL
, '}', FM_FORWARD
,
4245 (int)(old_pos
.lnum
- curwin
->w_cursor
.lnum
+ 1))) != NULL
4246 && pos
->lnum
< old_pos
.lnum
)
4252 /* If we previously found a valid position, use it. */
4253 if (found_pos
.lnum
!= 0)
4255 curwin
->w_cursor
= found_pos
;
4260 #ifdef FEAT_COMMENTS
4261 if (get_leader_len(ml_get_curline(), NULL
, FALSE
) > 0)
4263 /* Ignore this line, continue at start of next line. */
4264 ++curwin
->w_cursor
.lnum
;
4265 curwin
->w_cursor
.col
= 0;
4269 if (!locally
) /* global search: use first match found */
4271 if (curwin
->w_cursor
.lnum
>= par_pos
.lnum
)
4273 /* If we previously found a valid position, use it. */
4274 if (found_pos
.lnum
!= 0)
4275 curwin
->w_cursor
= found_pos
;
4279 /* For finding a local variable and the match is before the "{" search
4280 * to find a later match. For K&R style function declarations this
4281 * skips the function header without types. */
4282 found_pos
= curwin
->w_cursor
;
4288 curwin
->w_cursor
= old_pos
;
4292 curwin
->w_set_curswant
= TRUE
;
4293 /* "n" searches forward now */
4305 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4306 * lines rather than lines in the file.
4307 * 'dist' must be positive.
4309 * Return OK if able to move cursor, FAIL otherwise.
4312 nv_screengo(oap
, dir
, dist
)
4317 int linelen
= linetabsize(ml_get_curline());
4321 int col_off1
; /* margin offset for first screen line */
4322 int col_off2
; /* margin offset for wrapped screen line */
4323 int width1
; /* text width for first screen line */
4324 int width2
; /* test width for wrapped screen line */
4326 oap
->motion_type
= MCHAR
;
4327 oap
->inclusive
= FALSE
;
4329 col_off1
= curwin_col_off();
4330 col_off2
= col_off1
- curwin_col_off2();
4331 width1
= W_WIDTH(curwin
) - col_off1
;
4332 width2
= W_WIDTH(curwin
) - col_off2
;
4334 #ifdef FEAT_VERTSPLIT
4335 if (curwin
->w_width
!= 0)
4339 * Instead of sticking at the last character of the buffer line we
4340 * try to stick in the last column of the screen.
4342 if (curwin
->w_curswant
== MAXCOL
)
4347 curwin
->w_curswant
= 0;
4350 curwin
->w_curswant
= width1
- 1;
4351 if (curwin
->w_virtcol
> curwin
->w_curswant
)
4352 curwin
->w_curswant
+= ((curwin
->w_virtcol
4353 - curwin
->w_curswant
- 1) / width2
+ 1) * width2
;
4358 if (linelen
> width1
)
4359 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4362 if (curwin
->w_curswant
> (colnr_T
)n
+ 1)
4363 curwin
->w_curswant
-= ((curwin
->w_curswant
- n
) / width2
+ 1)
4369 if (dir
== BACKWARD
)
4371 if ((long)curwin
->w_curswant
>= width2
)
4372 /* move back within line */
4373 curwin
->w_curswant
-= width2
;
4376 /* to previous line */
4377 if (curwin
->w_cursor
.lnum
== 1)
4382 --curwin
->w_cursor
.lnum
;
4384 /* Move to the start of a closed fold. Don't do that when
4385 * 'foldopen' contains "all": it will open in a moment. */
4386 if (!(fdo_flags
& FDO_ALL
))
4387 (void)hasFolding(curwin
->w_cursor
.lnum
,
4388 &curwin
->w_cursor
.lnum
, NULL
);
4390 linelen
= linetabsize(ml_get_curline());
4391 if (linelen
> width1
)
4392 curwin
->w_curswant
+= (((linelen
- width1
- 1) / width2
)
4396 else /* dir == FORWARD */
4398 if (linelen
> width1
)
4399 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4402 if (curwin
->w_curswant
+ width2
< (colnr_T
)n
)
4403 /* move forward within line */
4404 curwin
->w_curswant
+= width2
;
4409 /* Move to the end of a closed fold. */
4410 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
4411 &curwin
->w_cursor
.lnum
);
4413 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4418 curwin
->w_cursor
.lnum
++;
4419 curwin
->w_curswant
%= width2
;
4423 #ifdef FEAT_VERTSPLIT
4427 coladvance(curwin
->w_curswant
);
4429 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4430 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
4433 * Check for landing on a character that got split at the end of the
4434 * last line. We want to advance a screenline, not end up in the same
4435 * screenline or move two screenlines.
4438 if (curwin
->w_virtcol
> curwin
->w_curswant
4439 && (curwin
->w_curswant
< (colnr_T
)width1
4440 ? (curwin
->w_curswant
> (colnr_T
)width1
/ 2)
4441 : ((curwin
->w_curswant
- width1
) % width2
4442 > (colnr_T
)width2
/ 2)))
4443 --curwin
->w_cursor
.col
;
4448 curwin
->w_curswant
= MAXCOL
; /* stick in the last column */
4455 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4456 * when Shift or Ctrl is used.
4457 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4463 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4464 win_T
*old_curwin
= curwin
;
4466 /* Currently we only get the mouse coordinates in the GUI. */
4467 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
4474 /* find the window at the pointer coordinates */
4475 curwin
= mouse_find_win(&row
, &col
);
4476 curbuf
= curwin
->w_buffer
;
4480 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
4482 (void)onepage(cap
->arg
? FORWARD
: BACKWARD
, 1L);
4488 nv_scroll_line(cap
);
4491 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4492 curwin
->w_redr_status
= TRUE
;
4494 curwin
= old_curwin
;
4495 curbuf
= curwin
->w_buffer
;
4500 * Mouse clicks and drags.
4506 (void)do_mouse(cap
->oap
, cap
->cmdchar
, BACKWARD
, cap
->count1
, 0);
4511 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4512 * cap->arg must be TRUE for CTRL-E.
4518 if (!checkclearop(cap
->oap
))
4519 scroll_redraw(cap
->arg
, cap
->count1
);
4523 * Scroll "count" lines up or down, and redraw.
4526 scroll_redraw(up
, count
)
4530 linenr_T prev_topline
= curwin
->w_topline
;
4532 int prev_topfill
= curwin
->w_topfill
;
4534 linenr_T prev_lnum
= curwin
->w_cursor
.lnum
;
4537 scrollup(count
, TRUE
);
4539 scrolldown(count
, TRUE
);
4542 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4543 * valid, otherwise the screen jumps back at the end of the file. */
4545 check_cursor_moved(curwin
);
4546 curwin
->w_valid
|= VALID_TOPLINE
;
4548 /* If moved back to where we were, at least move the cursor, otherwise
4549 * we get stuck at one position. Don't move the cursor up if the
4550 * first line of the buffer is already on the screen */
4551 while (curwin
->w_topline
== prev_topline
4553 && curwin
->w_topfill
== prev_topfill
4559 if (curwin
->w_cursor
.lnum
> prev_lnum
4560 || cursor_down(1L, FALSE
) == FAIL
)
4565 if (curwin
->w_cursor
.lnum
< prev_lnum
4566 || prev_topline
== 1L
4567 || cursor_up(1L, FALSE
) == FAIL
)
4570 /* Mark w_topline as valid, otherwise the screen jumps back at the
4571 * end of the file. */
4572 check_cursor_moved(curwin
);
4573 curwin
->w_valid
|= VALID_TOPLINE
;
4576 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
4577 coladvance(curwin
->w_curswant
);
4578 redraw_later(VALID
);
4582 * Commands that start with "z".
4590 int nchar
= cap
->nchar
;
4592 long old_fdl
= curwin
->w_p_fdl
;
4593 int old_fen
= curwin
->w_p_fen
;
4599 if (VIM_ISDIGIT(nchar
))
4602 * "z123{nchar}": edit the count before obtaining {nchar}
4604 if (checkclearop(cap
->oap
))
4609 #ifdef USE_ON_FLY_SCROLL
4610 dont_scroll
= TRUE
; /* disallow scrolling here */
4613 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4614 nchar
= plain_vgetc();
4616 LANGMAP_ADJUST(nchar
, TRUE
);
4620 #ifdef FEAT_CMDL_INFO
4621 (void)add_to_showcmd(nchar
);
4623 if (nchar
== K_DEL
|| nchar
== K_KDEL
)
4625 else if (VIM_ISDIGIT(nchar
))
4626 n
= n
* 10 + (nchar
- '0');
4627 else if (nchar
== CAR
)
4630 need_mouse_correct
= TRUE
;
4632 win_setheight((int)n
);
4635 else if (nchar
== 'l'
4638 || nchar
== K_RIGHT
)
4640 cap
->count1
= n
? n
* cap
->count1
: cap
->count1
;
4645 clearopbeep(cap
->oap
);
4649 cap
->oap
->op_type
= OP_NOP
;
4656 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4657 * and "zC" only in Visual mode. "zj" and "zk" are motion
4659 cap
->nchar
!= 'f' && cap
->nchar
!= 'F'
4660 && !(VIsual_active
&& vim_strchr((char_u
*)"dcCoO", cap
->nchar
))
4661 && cap
->nchar
!= 'j' && cap
->nchar
!= 'k'
4664 checkclearop(cap
->oap
))
4668 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4669 * If line number given, set cursor.
4671 if ((vim_strchr((char_u
*)"+\r\nt.z^-b", nchar
) != NULL
)
4673 && cap
->count0
!= curwin
->w_cursor
.lnum
)
4676 if (cap
->count0
> curbuf
->b_ml
.ml_line_count
)
4677 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4679 curwin
->w_cursor
.lnum
= cap
->count0
;
4685 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4687 if (cap
->count0
== 0)
4689 /* No count given: put cursor at the line below screen */
4690 validate_botline(); /* make sure w_botline is valid */
4691 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
4692 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4694 curwin
->w_cursor
.lnum
= curwin
->w_botline
;
4700 beginline(BL_WHITE
| BL_FIX
);
4703 case 't': scroll_cursor_top(0, TRUE
);
4704 redraw_later(VALID
);
4707 /* "z." and "zz": put cursor in middle of screen */
4708 case '.': beginline(BL_WHITE
| BL_FIX
);
4711 case 'z': scroll_cursor_halfway(TRUE
);
4712 redraw_later(VALID
);
4715 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4716 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4717 * when <count> is at bottom of window, and puts that one at
4718 * bottom of window. */
4719 if (cap
->count0
!= 0)
4721 scroll_cursor_bot(0, TRUE
);
4722 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
4724 else if (curwin
->w_topline
== 1)
4725 curwin
->w_cursor
.lnum
= 1;
4727 curwin
->w_cursor
.lnum
= curwin
->w_topline
- 1;
4730 beginline(BL_WHITE
| BL_FIX
);
4733 case 'b': scroll_cursor_bot(0, TRUE
);
4734 redraw_later(VALID
);
4737 /* "zH" - scroll screen right half-page */
4739 cap
->count1
*= W_WIDTH(curwin
) / 2;
4742 /* "zh" - scroll screen to the right */
4745 if (!curwin
->w_p_wrap
)
4747 if ((colnr_T
)cap
->count1
> curwin
->w_leftcol
)
4748 curwin
->w_leftcol
= 0;
4750 curwin
->w_leftcol
-= (colnr_T
)cap
->count1
;
4755 /* "zL" - scroll screen left half-page */
4756 case 'L': cap
->count1
*= W_WIDTH(curwin
) / 2;
4759 /* "zl" - scroll screen to the left */
4762 if (!curwin
->w_p_wrap
)
4764 /* scroll the window left */
4765 curwin
->w_leftcol
+= (colnr_T
)cap
->count1
;
4770 /* "zs" - scroll screen, cursor at the start */
4771 case 's': if (!curwin
->w_p_wrap
)
4774 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4775 col
= 0; /* like the cursor is in col 0 */
4778 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, NULL
);
4779 if ((long)col
> p_siso
)
4783 if (curwin
->w_leftcol
!= col
)
4785 curwin
->w_leftcol
= col
;
4786 redraw_later(NOT_VALID
);
4791 /* "ze" - scroll screen, cursor at the end */
4792 case 'e': if (!curwin
->w_p_wrap
)
4795 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4796 col
= 0; /* like the cursor is in col 0 */
4799 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
4800 n
= W_WIDTH(curwin
) - curwin_col_off();
4801 if ((long)col
+ p_siso
< n
)
4804 col
= col
+ p_siso
- n
+ 1;
4805 if (curwin
->w_leftcol
!= col
)
4807 curwin
->w_leftcol
= col
;
4808 redraw_later(NOT_VALID
);
4814 /* "zF": create fold command */
4815 /* "zf": create fold operator */
4817 case 'f': if (foldManualAllowed(TRUE
))
4821 curwin
->w_p_fen
= TRUE
;
4823 /* "zF" is like "zfzf" */
4824 if (nchar
== 'F' && cap
->oap
->op_type
== OP_FOLD
)
4831 clearopbeep(cap
->oap
);
4834 /* "zd": delete fold at cursor */
4835 /* "zD": delete fold at cursor recursively */
4837 case 'D': if (foldManualAllowed(FALSE
))
4842 deleteFold(curwin
->w_cursor
.lnum
,
4843 curwin
->w_cursor
.lnum
, nchar
== 'D', FALSE
);
4847 /* "zE": erease all folds */
4848 case 'E': if (foldmethodIsManual(curwin
))
4850 clearFolding(curwin
);
4851 changed_window_setting();
4853 else if (foldmethodIsMarker(curwin
))
4854 deleteFold((linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
4857 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4860 /* "zn": fold none: reset 'foldenable' */
4861 case 'n': curwin
->w_p_fen
= FALSE
;
4864 /* "zN": fold Normal: set 'foldenable' */
4865 case 'N': curwin
->w_p_fen
= TRUE
;
4868 /* "zi": invert folding: toggle 'foldenable' */
4869 case 'i': curwin
->w_p_fen
= !curwin
->w_p_fen
;
4872 /* "za": open closed fold or close open fold at cursor */
4873 case 'a': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4874 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4877 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4878 curwin
->w_p_fen
= TRUE
;
4882 /* "zA": open fold at cursor recursively */
4883 case 'A': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4884 openFoldRecurse(curwin
->w_cursor
.lnum
);
4887 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4888 curwin
->w_p_fen
= TRUE
;
4892 /* "zo": open fold at cursor or Visual area */
4893 case 'o': if (VIsual_active
)
4896 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4899 /* "zO": open fold recursively */
4900 case 'O': if (VIsual_active
)
4903 openFoldRecurse(curwin
->w_cursor
.lnum
);
4906 /* "zc": close fold at cursor or Visual area */
4907 case 'c': if (VIsual_active
)
4910 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4911 curwin
->w_p_fen
= TRUE
;
4914 /* "zC": close fold recursively */
4915 case 'C': if (VIsual_active
)
4918 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4919 curwin
->w_p_fen
= TRUE
;
4922 /* "zv": open folds at the cursor */
4923 case 'v': foldOpenCursor();
4926 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4927 case 'x': curwin
->w_p_fen
= TRUE
;
4928 newFoldLevel(); /* update right now */
4932 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4933 case 'X': curwin
->w_p_fen
= TRUE
;
4934 old_fdl
= -1; /* force an update */
4937 /* "zm": fold more */
4938 case 'm': if (curwin
->w_p_fdl
> 0)
4940 old_fdl
= -1; /* force an update */
4941 curwin
->w_p_fen
= TRUE
;
4944 /* "zM": close all folds */
4945 case 'M': curwin
->w_p_fdl
= 0;
4946 old_fdl
= -1; /* force an update */
4947 curwin
->w_p_fen
= TRUE
;
4950 /* "zr": reduce folding */
4951 case 'r': ++curwin
->w_p_fdl
;
4954 /* "zR": open all folds */
4955 case 'R': curwin
->w_p_fdl
= getDeepestNesting();
4956 old_fdl
= -1; /* force an update */
4959 case 'j': /* "zj" move to next fold downwards */
4960 case 'k': /* "zk" move to next fold upwards */
4961 if (foldMoveTo(TRUE
, nchar
== 'j' ? FORWARD
: BACKWARD
,
4962 cap
->count1
) == FAIL
)
4963 clearopbeep(cap
->oap
);
4966 #endif /* FEAT_FOLDING */
4969 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4971 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4972 nchar
= plain_vgetc();
4974 LANGMAP_ADJUST(nchar
, TRUE
);
4978 #ifdef FEAT_CMDL_INFO
4979 (void)add_to_showcmd(nchar
);
4981 if (vim_strchr((char_u
*)"gGwW", nchar
) == NULL
)
4983 clearopbeep(cap
->oap
);
4989 case 'g': /* "zg": add good word to word list */
4990 case 'w': /* "zw": add wrong word to word list */
4991 case 'G': /* "zG": add good word to temp word list */
4992 case 'W': /* "zW": add wrong word to temp word list */
4997 if (checkclearop(cap
->oap
))
5000 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &len
)
5006 pos_T pos
= curwin
->w_cursor
;
5008 /* Find bad word under the cursor. */
5009 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
);
5010 if (len
!= 0 && curwin
->w_cursor
.col
<= pos
.col
)
5011 ptr
= ml_get_pos(&curwin
->w_cursor
);
5012 curwin
->w_cursor
= pos
;
5015 if (ptr
== NULL
&& (len
= find_ident_under_cursor(&ptr
,
5018 spell_add_word(ptr
, len
, nchar
== 'w' || nchar
== 'W',
5019 (nchar
== 'G' || nchar
== 'W')
5020 ? 0 : (int)cap
->count1
,
5025 case '=': /* "z=": suggestions for a badly spelled word */
5026 if (!checkclearop(cap
->oap
))
5027 spell_suggest((int)cap
->count0
);
5031 default: clearopbeep(cap
->oap
);
5035 /* Redraw when 'foldenable' changed */
5036 if (old_fen
!= curwin
->w_p_fen
)
5041 if (foldmethodIsDiff(curwin
) && curwin
->w_p_scb
)
5043 /* Adjust 'foldenable' in diff-synced windows. */
5046 if (wp
!= curwin
&& foldmethodIsDiff(wp
) && wp
->w_p_scb
)
5048 wp
->w_p_fen
= curwin
->w_p_fen
;
5049 changed_window_setting_win(wp
);
5054 changed_window_setting();
5057 /* Redraw when 'foldlevel' changed. */
5058 if (old_fdl
!= curwin
->w_p_fdl
)
5065 * Vertical scrollbar movement.
5068 nv_ver_scrollbar(cap
)
5071 if (cap
->oap
->op_type
!= OP_NOP
)
5072 clearopbeep(cap
->oap
);
5074 /* Even if an operator was pending, we still want to scroll */
5079 * Horizontal scrollbar movement.
5082 nv_hor_scrollbar(cap
)
5085 if (cap
->oap
->op_type
!= OP_NOP
)
5086 clearopbeep(cap
->oap
);
5088 /* Even if an operator was pending, we still want to scroll */
5089 gui_do_horiz_scroll();
5093 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5101 if (cap
->oap
->op_type
!= OP_NOP
)
5102 clearopbeep(cap
->oap
);
5104 /* Even if an operator was pending, we still want to jump tabs. */
5105 goto_tabpage(current_tab
);
5109 * Selected item in tab line menu.
5115 if (cap
->oap
->op_type
!= OP_NOP
)
5116 clearopbeep(cap
->oap
);
5118 /* Even if an operator was pending, we still want to jump tabs. */
5123 * Handle selecting an item of the GUI tab line menu.
5124 * Used in Normal and Insert mode.
5129 switch (current_tabmenu
)
5131 case TABLINE_MENU_CLOSE
:
5132 if (current_tab
== 0)
5133 do_cmdline_cmd((char_u
*)"tabclose");
5136 vim_snprintf((char *)IObuff
, IOSIZE
, "tabclose %d",
5138 do_cmdline_cmd(IObuff
);
5142 case TABLINE_MENU_NEW
:
5143 vim_snprintf((char *)IObuff
, IOSIZE
, "%dtabnew",
5144 current_tab
> 0 ? current_tab
- 1 : 999);
5145 do_cmdline_cmd(IObuff
);
5148 case TABLINE_MENU_OPEN
:
5149 vim_snprintf((char *)IObuff
, IOSIZE
, "browse %dtabnew",
5150 current_tab
> 0 ? current_tab
- 1 : 999);
5151 do_cmdline_cmd(IObuff
);
5165 * Ignore 'Q' in Visual mode, just give a beep.
5172 if (!checkclearop(cap
->oap
))
5177 * Handle a ":" command.
5191 if (cap
->oap
->op_type
!= OP_NOP
)
5193 /* Using ":" as a movement is characterwise exclusive. */
5194 cap
->oap
->motion_type
= MCHAR
;
5195 cap
->oap
->inclusive
= FALSE
;
5197 else if (cap
->count0
)
5199 /* translate "count:" into ":.,.+(count - 1)" */
5200 stuffcharReadbuff('.');
5201 if (cap
->count0
> 1)
5203 stuffReadbuff((char_u
*)",.+");
5204 stuffnumReadbuff((long)cap
->count0
- 1L);
5208 /* When typing, don't type below an old message */
5214 /* get a command line and execute it */
5215 do_cmdline(NULL
, getexline
, NULL
,
5216 cap
->oap
->op_type
!= OP_NOP
? DOCMD_KEEPLINE
: 0);
5218 /* If 'insertmode' changed, enter or exit Insert mode */
5219 if (p_im
!= old_p_im
)
5227 /* The start of the operator may have become invalid by the Ex
5229 if (cap
->oap
->op_type
!= OP_NOP
5230 && (cap
->oap
->start
.lnum
> curbuf
->b_ml
.ml_line_count
5231 || cap
->oap
->start
.col
>
5232 STRLEN(ml_get(cap
->oap
->start
.lnum
))))
5233 clearopbeep(cap
->oap
);
5238 * Handle CTRL-G command.
5245 if (VIsual_active
) /* toggle Selection/Visual mode */
5247 VIsual_select
= !VIsual_select
;
5252 if (!checkclearop(cap
->oap
))
5253 /* print full name if count given or :cd used */
5254 fileinfo((int)cap
->count0
, FALSE
, TRUE
);
5258 * Handle CTRL-H <Backspace> command.
5265 if (VIsual_active
&& VIsual_select
)
5267 cap
->cmdchar
= 'x'; /* BS key behaves like 'x' in Select mode */
5276 * CTRL-L: clear screen and redraw.
5282 if (!checkclearop(cap
->oap
))
5284 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5286 * Right now, the BeBox doesn't seem to have an easy way to detect
5287 * window resizing, so we cheat and make the user detect it
5288 * manually with CTRL-L instead
5293 /* Clear all syntax states to force resyncing. */
5294 syn_stack_free_all(curbuf
);
5296 redraw_later(CLEAR
);
5301 * CTRL-O: In Select mode: switch to Visual mode for one command.
5302 * Otherwise: Go to older pcmark.
5309 if (VIsual_active
&& VIsual_select
)
5311 VIsual_select
= FALSE
;
5313 restart_VIsual_select
= 2; /* restart Select mode later */
5318 cap
->count1
= -cap
->count1
;
5324 * CTRL-^ command, short for ":e #"
5330 if (!checkclearopq(cap
->oap
))
5331 (void)buflist_getfile((int)cap
->count0
, (linenr_T
)0,
5332 GETF_SETMARK
|GETF_ALT
, FALSE
);
5342 if (!checkclearopq(cap
->oap
))
5346 /* "ZZ": equivalent to ":x". */
5347 case 'Z': do_cmdline_cmd((char_u
*)"x");
5350 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5351 case 'Q': do_cmdline_cmd((char_u
*)"q!");
5354 default: clearopbeep(cap
->oap
);
5359 #if defined(FEAT_WINDOWS) || defined(PROTO)
5361 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5372 vim_memset(&ca
, 0, sizeof(ca
));
5381 * Handle the commands that use the word under the cursor.
5382 * [g] CTRL-] :ta to current identifier
5383 * [g] 'K' run program for current identifier
5384 * [g] '*' / to current identifier or string
5385 * [g] '#' ? to current identifier or string
5386 * g ']' :tselect for current identifier
5395 char_u
*kp
; /* value of 'keywordprg' */
5396 int kp_help
; /* 'keywordprg' is ":help" */
5397 int n
= 0; /* init for GCC */
5399 int g_cmd
; /* "g" command */
5404 if (cap
->cmdchar
== 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5406 cmdchar
= cap
->nchar
;
5411 cmdchar
= cap
->cmdchar
;
5415 if (cmdchar
== POUND
) /* the pound sign, '#' for English keyboards */
5419 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5421 if (cmdchar
== ']' || cmdchar
== Ctrl_RSB
|| cmdchar
== 'K')
5424 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &n
) == FAIL
)
5427 if (checkclearopq(cap
->oap
))
5431 if (ptr
== NULL
&& (n
= find_ident_under_cursor(&ptr
,
5432 (cmdchar
== '*' || cmdchar
== '#')
5433 ? FIND_IDENT
|FIND_STRING
: FIND_IDENT
)) == 0)
5439 /* Allocate buffer to put the command in. Inserting backslashes can
5440 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5441 * and some numbers. */
5442 kp
= (*curbuf
->b_p_kp
== NUL
? p_kp
: curbuf
->b_p_kp
);
5443 kp_help
= (*kp
== NUL
|| STRCMP(kp
, ":he") == 0
5444 || STRCMP(kp
, ":help") == 0);
5445 buf
= alloc((unsigned)(n
* 2 + 30 + STRLEN(kp
)));
5455 * Put cursor at start of word, makes search skip the word
5457 * Call setpcmark() first, so "*``" puts the cursor back where
5461 curwin
->w_cursor
.col
= (colnr_T
) (ptr
- ml_get_curline());
5463 if (!g_cmd
&& vim_iswordp(ptr
))
5465 no_smartcase
= TRUE
; /* don't use 'smartcase' now */
5470 STRCPY(buf
, "he! ");
5473 /* An external command will probably use an argument starting
5474 * with "-" as an option. To avoid trouble we skip the "-". */
5478 /* When a count is given, turn it into a range. Is this
5479 * really what we want? */
5480 isman
= (STRCMP(kp
, "man") == 0);
5481 isman_s
= (STRCMP(kp
, "man -s") == 0);
5482 if (cap
->count0
!= 0 && !(isman
|| isman_s
))
5483 sprintf((char *)buf
, ".,.+%ld", cap
->count0
- 1);
5486 if (cap
->count0
== 0 && isman_s
)
5491 if (cap
->count0
!= 0 && (isman
|| isman_s
))
5493 sprintf((char *)buf
+ STRLEN(buf
), "%ld", cap
->count0
);
5502 STRCPY(buf
, "cstag ");
5510 STRCPY(buf
, "he! ");
5514 sprintf((char *)buf
, "%ldta ", cap
->count0
);
5518 * Now grab the chars in the identifier
5520 if (cmdchar
== 'K' && !kp_help
)
5522 /* Escape the argument properly for a shell command */
5523 p
= vim_strsave_shellescape(ptr
, TRUE
);
5529 buf
= (char_u
*)vim_realloc(buf
, STRLEN(buf
) + STRLEN(p
) + 1);
5542 aux_ptr
= (char_u
*)(p_magic
? "/.*~[^$\\" : "/^$\\");
5543 else if (cmdchar
== '#')
5544 aux_ptr
= (char_u
*)(p_magic
? "/?.*~[^$\\" : "/?^$\\");
5546 /* Don't escape spaces and Tabs in a tag with a backslash */
5547 aux_ptr
= (char_u
*)"\\|\"\n*?[";
5549 p
= buf
+ STRLEN(buf
);
5552 /* put a backslash before \ and some others */
5553 if (vim_strchr(aux_ptr
, *ptr
) != NULL
)
5556 /* When current byte is a part of multibyte character, copy all
5557 * bytes of that character. */
5561 int len
= (*mb_ptr2len
)(ptr
) - 1;
5563 for (i
= 0; i
< len
&& n
>= 1; ++i
, --n
)
5573 * Execute the command.
5575 if (cmdchar
== '*' || cmdchar
== '#')
5579 has_mbyte
? vim_iswordp(mb_prevptr(ml_get_curline(), ptr
)) :
5581 vim_iswordc(ptr
[-1])))
5584 /* put pattern in search history */
5585 add_to_history(HIST_SEARCH
, buf
, TRUE
, NUL
);
5587 normal_search(cap
, cmdchar
== '*' ? '/' : '?', buf
, 0);
5590 do_cmdline_cmd(buf
);
5595 #if defined(FEAT_VISUAL) || defined(PROTO)
5597 * Get visually selected text, within one line only.
5598 * Returns FAIL if more than one line selected.
5601 get_visual_text(cap
, pp
, lenp
)
5603 char_u
**pp
; /* return: start of selected text */
5604 int *lenp
; /* return: length of selected text */
5606 if (VIsual_mode
!= 'V')
5608 if (VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
5611 clearopbeep(cap
->oap
);
5614 if (VIsual_mode
== 'V')
5616 *pp
= ml_get_curline();
5617 *lenp
= (int)STRLEN(*pp
);
5621 if (lt(curwin
->w_cursor
, VIsual
))
5623 *pp
= ml_get_pos(&curwin
->w_cursor
);
5624 *lenp
= VIsual
.col
- curwin
->w_cursor
.col
+ 1;
5628 *pp
= ml_get_pos(&VIsual
);
5629 *lenp
= curwin
->w_cursor
.col
- VIsual
.col
+ 1;
5633 /* Correct the length to include the whole last character. */
5634 *lenp
+= (*mb_ptr2len
)(*pp
+ (*lenp
- 1)) - 1;
5637 reset_VIsual_and_resel();
5643 * CTRL-T: backwards in tag stack
5649 if (!checkclearopq(cap
->oap
))
5650 do_tag((char_u
*)"", DT_POP
, (int)cap
->count1
, FALSE
, TRUE
);
5654 * Handle scrolling command 'H', 'L' and 'M'.
5667 cap
->oap
->motion_type
= MLINE
;
5670 if (cap
->cmdchar
== 'L')
5672 validate_botline(); /* make sure curwin->w_botline is valid */
5673 curwin
->w_cursor
.lnum
= curwin
->w_botline
- 1;
5674 if (cap
->count1
- 1 >= curwin
->w_cursor
.lnum
)
5675 curwin
->w_cursor
.lnum
= 1;
5679 if (hasAnyFolding(curwin
))
5681 /* Count a fold for one screen line. */
5682 for (n
= cap
->count1
- 1; n
> 0
5683 && curwin
->w_cursor
.lnum
> curwin
->w_topline
; --n
)
5685 (void)hasFolding(curwin
->w_cursor
.lnum
,
5686 &curwin
->w_cursor
.lnum
, NULL
);
5687 --curwin
->w_cursor
.lnum
;
5692 curwin
->w_cursor
.lnum
-= cap
->count1
- 1;
5697 if (cap
->cmdchar
== 'M')
5700 /* Don't count filler lines above the window. */
5701 used
-= diff_check_fill(curwin
, curwin
->w_topline
)
5702 - curwin
->w_topfill
;
5704 validate_botline(); /* make sure w_empty_rows is valid */
5705 half
= (curwin
->w_height
- curwin
->w_empty_rows
+ 1) / 2;
5706 for (n
= 0; curwin
->w_topline
+ n
< curbuf
->b_ml
.ml_line_count
; ++n
)
5709 /* Count half he number of filler lines to be "below this
5710 * line" and half to be "above the next line". */
5711 if (n
> 0 && used
+ diff_check_fill(curwin
, curwin
->w_topline
5718 used
+= plines(curwin
->w_topline
+ n
);
5722 if (hasFolding(curwin
->w_topline
+ n
, NULL
, &lnum
))
5723 n
= lnum
- curwin
->w_topline
;
5726 if (n
> 0 && used
> curwin
->w_height
)
5729 else /* (cap->cmdchar == 'H') */
5731 n
= cap
->count1
- 1;
5733 if (hasAnyFolding(curwin
))
5735 /* Count a fold for one screen line. */
5736 lnum
= curwin
->w_topline
;
5737 while (n
-- > 0 && lnum
< curwin
->w_botline
- 1)
5739 hasFolding(lnum
, NULL
, &lnum
);
5742 n
= lnum
- curwin
->w_topline
;
5746 curwin
->w_cursor
.lnum
= curwin
->w_topline
+ n
;
5747 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5748 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5751 cursor_correct(); /* correct for 'so' */
5752 beginline(BL_SOL
| BL_FIX
);
5756 * Cursor right commands.
5766 # define PAST_LINE 0
5769 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5771 /* <C-Right> and <S-Right> move a word or WORD right */
5772 if (mod_mask
& MOD_MASK_CTRL
)
5778 cap
->oap
->motion_type
= MCHAR
;
5779 cap
->oap
->inclusive
= FALSE
;
5781 PAST_LINE
= (VIsual_active
&& *p_sel
!= 'o');
5783 # ifdef FEAT_VIRTUALEDIT
5785 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5786 * (theoretically) infinitely long.
5788 if (virtual_active())
5793 for (n
= cap
->count1
; n
> 0; --n
)
5795 if ((!PAST_LINE
&& oneright() == FAIL
)
5796 || (PAST_LINE
&& *ml_get_cursor() == NUL
))
5799 * <Space> wraps to next line if 'whichwrap' has 's'.
5800 * 'l' wraps to next line if 'whichwrap' has 'l'.
5801 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5803 if ( ((cap
->cmdchar
== ' '
5804 && vim_strchr(p_ww
, 's') != NULL
)
5805 || (cap
->cmdchar
== 'l'
5806 && vim_strchr(p_ww
, 'l') != NULL
)
5807 || (cap
->cmdchar
== K_RIGHT
5808 && vim_strchr(p_ww
, '>') != NULL
))
5809 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
5811 /* When deleting we also count the NL as a character.
5812 * Set cap->oap->inclusive when last char in the line is
5813 * included, move to next line after that */
5814 if ( cap
->oap
->op_type
!= OP_NOP
5815 && !cap
->oap
->inclusive
5816 && !lineempty(curwin
->w_cursor
.lnum
))
5817 cap
->oap
->inclusive
= TRUE
;
5820 ++curwin
->w_cursor
.lnum
;
5821 curwin
->w_cursor
.col
= 0;
5822 #ifdef FEAT_VIRTUALEDIT
5823 curwin
->w_cursor
.coladd
= 0;
5825 curwin
->w_set_curswant
= TRUE
;
5826 cap
->oap
->inclusive
= FALSE
;
5830 if (cap
->oap
->op_type
== OP_NOP
)
5832 /* Only beep and flush if not moved at all */
5833 if (n
== cap
->count1
)
5838 if (!lineempty(curwin
->w_cursor
.lnum
))
5839 cap
->oap
->inclusive
= TRUE
;
5846 curwin
->w_set_curswant
= TRUE
;
5847 # ifdef FEAT_VIRTUALEDIT
5848 if (virtual_active())
5855 curwin
->w_cursor
.col
+=
5856 (*mb_ptr2len
)(ml_get_cursor());
5859 ++curwin
->w_cursor
.col
;
5865 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5866 && cap
->oap
->op_type
== OP_NOP
)
5872 * Cursor left commands.
5874 * Returns TRUE when operator end should not be adjusted.
5882 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5884 /* <C-Left> and <S-Left> move a word or WORD left */
5885 if (mod_mask
& MOD_MASK_CTRL
)
5891 cap
->oap
->motion_type
= MCHAR
;
5892 cap
->oap
->inclusive
= FALSE
;
5893 for (n
= cap
->count1
; n
> 0; --n
)
5895 if (oneleft() == FAIL
)
5897 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5898 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5899 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5901 if ( (((cap
->cmdchar
== K_BS
5902 || cap
->cmdchar
== Ctrl_H
)
5903 && vim_strchr(p_ww
, 'b') != NULL
)
5904 || (cap
->cmdchar
== 'h'
5905 && vim_strchr(p_ww
, 'h') != NULL
)
5906 || (cap
->cmdchar
== K_LEFT
5907 && vim_strchr(p_ww
, '<') != NULL
))
5908 && curwin
->w_cursor
.lnum
> 1)
5910 --(curwin
->w_cursor
.lnum
);
5911 coladvance((colnr_T
)MAXCOL
);
5912 curwin
->w_set_curswant
= TRUE
;
5914 /* When the NL before the first char has to be deleted we
5915 * put the cursor on the NUL after the previous line.
5916 * This is a very special case, be careful!
5917 * Don't adjust op_end now, otherwise it won't work. */
5918 if ( (cap
->oap
->op_type
== OP_DELETE
5919 || cap
->oap
->op_type
== OP_CHANGE
)
5920 && !lineempty(curwin
->w_cursor
.lnum
))
5922 if (*ml_get_cursor() != NUL
)
5923 ++curwin
->w_cursor
.col
;
5924 cap
->retval
|= CA_NO_ADJ_OP_END
;
5928 /* Only beep and flush if not moved at all */
5929 else if (cap
->oap
->op_type
== OP_NOP
&& n
== cap
->count1
)
5935 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5936 && cap
->oap
->op_type
== OP_NOP
)
5942 * Cursor up commands.
5943 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5949 if (mod_mask
& MOD_MASK_SHIFT
)
5951 /* <S-Up> is page up */
5952 cap
->arg
= BACKWARD
;
5957 cap
->oap
->motion_type
= MLINE
;
5958 if (cursor_up(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5959 clearopbeep(cap
->oap
);
5961 beginline(BL_WHITE
| BL_FIX
);
5966 * Cursor down commands.
5967 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5973 if (mod_mask
& MOD_MASK_SHIFT
)
5975 /* <S-Down> is page down */
5980 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5981 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5982 if (bt_quickfix(curbuf
) && cap
->cmdchar
== CAR
)
5983 if (curwin
->w_llist_ref
== NULL
)
5984 do_cmdline_cmd((char_u
*)".cc"); /* quickfix window */
5986 do_cmdline_cmd((char_u
*)".ll"); /* location list window */
5991 /* In the cmdline window a <CR> executes the command. */
5992 if (cmdwin_type
!= 0 && cap
->cmdchar
== CAR
)
5993 cmdwin_result
= CAR
;
5997 cap
->oap
->motion_type
= MLINE
;
5998 if (cursor_down(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5999 clearopbeep(cap
->oap
);
6001 beginline(BL_WHITE
| BL_FIX
);
6006 #ifdef FEAT_SEARCHPATH
6008 * Grab the file name under the cursor and edit it.
6019 clearopbeep(cap
->oap
);
6024 if (curbuf_locked())
6031 ptr
= grab_file_name(cap
->count1
, &lnum
);
6035 /* do autowrite if necessary */
6036 if (curbufIsChanged() && curbuf
->b_nwindows
<= 1 && !P_HID(curbuf
))
6037 autowrite(curbuf
, FALSE
);
6039 (void)do_ecmd(0, ptr
, NULL
, NULL
, ECMD_LAST
,
6040 P_HID(curbuf
) ? ECMD_HIDE
: 0);
6041 if (cap
->nchar
== 'F' && lnum
>= 0)
6043 curwin
->w_cursor
.lnum
= lnum
;
6044 check_cursor_lnum();
6045 beginline(BL_SOL
| BL_FIX
);
6055 * <End> command: to end of current line or last line.
6061 if (cap
->arg
|| (mod_mask
& MOD_MASK_CTRL
)) /* CTRL-END = goto last line */
6065 cap
->count1
= 1; /* to end of current line */
6071 * Handle the "$" command.
6077 cap
->oap
->motion_type
= MCHAR
;
6078 cap
->oap
->inclusive
= TRUE
;
6079 #ifdef FEAT_VIRTUALEDIT
6080 /* In virtual mode when off the edge of a line and an operator
6081 * is pending (whew!) keep the cursor where it is.
6082 * Otherwise, send it to the end of the line. */
6083 if (!virtual_active() || gchar_cursor() != NUL
6084 || cap
->oap
->op_type
== OP_NOP
)
6086 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
6087 if (cursor_down((long)(cap
->count1
- 1),
6088 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6089 clearopbeep(cap
->oap
);
6091 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6097 * Implementation of '?' and '/' commands.
6098 * If cap->arg is TRUE don't set PC mark.
6104 oparg_T
*oap
= cap
->oap
;
6106 if (cap
->cmdchar
== '?' && cap
->oap
->op_type
== OP_ROT13
)
6108 /* Translate "g??" to "g?g?" */
6115 cap
->searchbuf
= getcmdline(cap
->cmdchar
, cap
->count1
, 0);
6117 if (cap
->searchbuf
== NULL
)
6123 normal_search(cap
, cap
->cmdchar
, cap
->searchbuf
,
6124 (cap
->arg
? 0 : SEARCH_MARK
));
6128 * Handle "N" and "n" commands.
6129 * cap->arg is SEARCH_REV for "N", 0 for "n".
6135 normal_search(cap
, 0, NULL
, SEARCH_MARK
| cap
->arg
);
6139 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6140 * Uses only cap->count1 and cap->oap from "cap".
6143 normal_search(cap
, dir
, pat
, opt
)
6147 int opt
; /* extra flags for do_search() */
6151 cap
->oap
->motion_type
= MCHAR
;
6152 cap
->oap
->inclusive
= FALSE
;
6153 cap
->oap
->use_reg_one
= TRUE
;
6154 curwin
->w_set_curswant
= TRUE
;
6156 i
= do_search(cap
->oap
, dir
, pat
, cap
->count1
,
6157 opt
| SEARCH_OPT
| SEARCH_ECHO
| SEARCH_MSG
, NULL
);
6163 cap
->oap
->motion_type
= MLINE
;
6164 #ifdef FEAT_VIRTUALEDIT
6165 curwin
->w_cursor
.coladd
= 0;
6168 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6173 /* "/$" will put the cursor after the end of the line, may need to
6174 * correct that here */
6179 * Character search commands.
6180 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6181 * ',' and FALSE for ';'.
6182 * cap->nchar is NUL for ',' and ';' (repeat the search)
6190 if (cap
->cmdchar
== 't' || cap
->cmdchar
== 'T')
6195 cap
->oap
->motion_type
= MCHAR
;
6196 if (IS_SPECIAL(cap
->nchar
) || searchc(cap
, t_cmd
) == FAIL
)
6197 clearopbeep(cap
->oap
);
6200 curwin
->w_set_curswant
= TRUE
;
6201 #ifdef FEAT_VIRTUALEDIT
6202 /* Include a Tab for "tx" and for "dfx". */
6203 if (gchar_cursor() == TAB
&& virtual_active() && cap
->arg
== FORWARD
6204 && (t_cmd
|| cap
->oap
->op_type
!= OP_NOP
))
6208 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
6209 curwin
->w_cursor
.coladd
= ecol
- scol
;
6212 curwin
->w_cursor
.coladd
= 0;
6215 adjust_for_sel(cap
);
6218 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6225 * "[" and "]" commands.
6226 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6234 pos_T
*pos
= NULL
; /* init for GCC */
6235 pos_T old_pos
; /* cursor position before command */
6241 cap
->oap
->motion_type
= MCHAR
;
6242 cap
->oap
->inclusive
= FALSE
;
6243 old_pos
= curwin
->w_cursor
;
6244 #ifdef FEAT_VIRTUALEDIT
6245 curwin
->w_cursor
.coladd
= 0; /* TODO: don't do this for an error. */
6248 #ifdef FEAT_SEARCHPATH
6250 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6252 if (cap
->nchar
== 'f')
6259 * Find the occurrence(s) of the identifier or define under cursor
6260 * in current and included files or jump to the first occurrence.
6263 * fwd bwd fwd bwd fwd bwd
6264 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6265 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6267 if (vim_strchr((char_u
*)
6273 cap
->nchar
) != NULL
)
6278 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0)
6282 find_pattern_in_path(ptr
, 0, len
, TRUE
,
6283 cap
->count0
== 0 ? !isupper(cap
->nchar
) : FALSE
,
6284 ((cap
->nchar
& 0xf) == ('d' & 0xf)) ? FIND_DEFINE
: FIND_ANY
,
6286 isupper(cap
->nchar
) ? ACTION_SHOW_ALL
:
6287 islower(cap
->nchar
) ? ACTION_SHOW
: ACTION_GOTO
,
6288 cap
->cmdchar
== ']' ? curwin
->w_cursor
.lnum
+ 1 : (linenr_T
)1,
6290 curwin
->w_set_curswant
= TRUE
;
6297 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6298 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6299 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6300 * "[m" or "]m" search for prev/next start of (Java) method.
6301 * "[M" or "]M" search for prev/next end of (Java) method.
6303 if ( (cap
->cmdchar
== '['
6304 && vim_strchr((char_u
*)"{(*/#mM", cap
->nchar
) != NULL
)
6305 || (cap
->cmdchar
== ']'
6306 && vim_strchr((char_u
*)"})*/#mM", cap
->nchar
) != NULL
))
6308 if (cap
->nchar
== '*')
6312 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6314 if (cap
->cmdchar
== '[')
6327 if ((pos
= findmatchlimit(cap
->oap
, findc
,
6328 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
, 0)) == NULL
)
6330 if (new_pos
.lnum
== 0) /* nothing found */
6332 if (cap
->nchar
!= 'm' && cap
->nchar
!= 'M')
6333 clearopbeep(cap
->oap
);
6336 pos
= &new_pos
; /* use last one found */
6340 curwin
->w_cursor
= *pos
;
6343 curwin
->w_cursor
= old_pos
;
6346 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6347 * brought us to the match for "[m" and "]M" when inside a method.
6348 * Try finding the '{' or '}' we want to be at.
6349 * Also repeat for the given count.
6351 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6353 /* norm is TRUE for "]M" and "[m" */
6354 int norm
= ((findc
== '{') == (cap
->nchar
== 'm'));
6357 /* found a match: we were inside a method */
6358 if (prev_pos
.lnum
!= 0)
6361 curwin
->w_cursor
= prev_pos
;
6371 if ((findc
== '{' ? dec_cursor() : inc_cursor()) < 0)
6373 /* if not found anything, that's an error */
6375 clearopbeep(cap
->oap
);
6380 if (c
== '{' || c
== '}')
6382 /* Must have found end/start of class: use it.
6383 * Or found the place to be at. */
6384 if ((c
== findc
&& norm
) || (n
== 1 && !norm
))
6386 new_pos
= curwin
->w_cursor
;
6390 /* if no match found at all, we started outside of the
6391 * class and we're inside now. Just go on. */
6392 else if (new_pos
.lnum
== 0)
6394 new_pos
= curwin
->w_cursor
;
6397 /* found start/end of other method: go to match */
6398 else if ((pos
= findmatchlimit(cap
->oap
, findc
,
6399 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
,
6403 curwin
->w_cursor
= *pos
;
6409 curwin
->w_cursor
= old_pos
;
6410 if (pos
== NULL
&& new_pos
.lnum
!= 0)
6411 clearopbeep(cap
->oap
);
6416 curwin
->w_cursor
= *pos
;
6417 curwin
->w_set_curswant
= TRUE
;
6419 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
6420 && cap
->oap
->op_type
== OP_NOP
)
6427 * "[[", "[]", "]]" and "][": move to start or end of function
6429 else if (cap
->nchar
== '[' || cap
->nchar
== ']')
6431 if (cap
->nchar
== cap
->cmdchar
) /* "]]" or "[[" */
6434 flag
= '}'; /* "][" or "[]" */
6436 curwin
->w_set_curswant
= TRUE
;
6438 * Imitate strange Vi behaviour: When using "]]" with an operator
6439 * we also stop at '}'.
6441 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, flag
,
6442 (cap
->oap
->op_type
!= OP_NOP
6443 && cap
->arg
== FORWARD
&& flag
== '{')))
6444 clearopbeep(cap
->oap
);
6447 if (cap
->oap
->op_type
== OP_NOP
)
6448 beginline(BL_WHITE
| BL_FIX
);
6450 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6457 * "[p", "[P", "]P" and "]p": put with indent adjustment
6459 else if (cap
->nchar
== 'p' || cap
->nchar
== 'P')
6461 if (!checkclearop(cap
->oap
))
6464 do_put(cap
->oap
->regname
,
6465 (cap
->cmdchar
== ']' && cap
->nchar
== 'p') ? FORWARD
: BACKWARD
,
6466 cap
->count1
, PUT_FIXINDENT
);
6471 * "['", "[`", "]'" and "]`": jump to next mark
6473 else if (cap
->nchar
== '\'' || cap
->nchar
== '`')
6475 pos
= &curwin
->w_cursor
;
6476 for (n
= cap
->count1
; n
> 0; --n
)
6479 pos
= getnextmark(pos
, cap
->cmdchar
== '[' ? BACKWARD
: FORWARD
,
6480 cap
->nchar
== '\'');
6486 nv_cursormark(cap
, cap
->nchar
== '\'', pos
);
6491 * [ or ] followed by a middle mouse click: put selected text with
6492 * indent adjustment. Any other button just does as usual.
6494 else if (cap
->nchar
>= K_LEFTMOUSE
&& cap
->nchar
<= K_RIGHTRELEASE
)
6496 (void)do_mouse(cap
->oap
, cap
->nchar
,
6497 (cap
->cmdchar
== ']') ? FORWARD
: BACKWARD
,
6498 cap
->count1
, PUT_FIXINDENT
);
6500 #endif /* FEAT_MOUSE */
6504 * "[z" and "]z": move to start or end of open fold.
6506 else if (cap
->nchar
== 'z')
6508 if (foldMoveTo(FALSE
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6509 cap
->count1
) == FAIL
)
6510 clearopbeep(cap
->oap
);
6516 * "[c" and "]c": move to next or previous diff-change.
6518 else if (cap
->nchar
== 'c')
6520 if (diff_move_to(cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6521 cap
->count1
) == FAIL
)
6522 clearopbeep(cap
->oap
);
6528 * "[s", "[S", "]s" and "]S": move to next spell error.
6530 else if (cap
->nchar
== 's' || cap
->nchar
== 'S')
6533 for (n
= 0; n
< cap
->count1
; ++n
)
6534 if (spell_move_to(curwin
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6535 cap
->nchar
== 's' ? TRUE
: FALSE
, FALSE
, NULL
) == 0)
6537 clearopbeep(cap
->oap
);
6540 # ifdef FEAT_FOLDING
6541 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6547 /* Not a valid cap->nchar. */
6549 clearopbeep(cap
->oap
);
6553 * Handle Normal mode "%" command.
6561 linenr_T lnum
= curwin
->w_cursor
.lnum
;
6564 cap
->oap
->inclusive
= TRUE
;
6565 if (cap
->count0
) /* {cnt}% : goto {cnt} percentage in file */
6567 if (cap
->count0
> 100)
6568 clearopbeep(cap
->oap
);
6571 cap
->oap
->motion_type
= MLINE
;
6573 /* Round up, so CTRL-G will give same value. Watch out for a
6574 * large line count, the line number must not go negative! */
6575 if (curbuf
->b_ml
.ml_line_count
> 1000000)
6576 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
+ 99L)
6577 / 100L * cap
->count0
;
6579 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
*
6580 cap
->count0
+ 99L) / 100L;
6581 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6582 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6583 beginline(BL_SOL
| BL_FIX
);
6586 else /* "%" : go to matching paren */
6588 cap
->oap
->motion_type
= MCHAR
;
6589 cap
->oap
->use_reg_one
= TRUE
;
6590 if ((pos
= findmatch(cap
->oap
, NUL
)) == NULL
)
6591 clearopbeep(cap
->oap
);
6595 curwin
->w_cursor
= *pos
;
6596 curwin
->w_set_curswant
= TRUE
;
6597 #ifdef FEAT_VIRTUALEDIT
6598 curwin
->w_cursor
.coladd
= 0;
6601 adjust_for_sel(cap
);
6606 if (cap
->oap
->op_type
== OP_NOP
6607 && lnum
!= curwin
->w_cursor
.lnum
6608 && (fdo_flags
& FDO_PERCENT
)
6615 * Handle "(" and ")" commands.
6616 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6622 cap
->oap
->motion_type
= MCHAR
;
6623 cap
->oap
->use_reg_one
= TRUE
;
6624 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6625 cap
->oap
->inclusive
= FALSE
;
6626 curwin
->w_set_curswant
= TRUE
;
6628 if (findsent(cap
->arg
, cap
->count1
) == FAIL
)
6629 clearopbeep(cap
->oap
);
6632 /* Don't leave the cursor on the NUL past end of line. */
6633 adjust_cursor(cap
->oap
);
6634 #ifdef FEAT_VIRTUALEDIT
6635 curwin
->w_cursor
.coladd
= 0;
6638 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6645 * "m" command: Mark a position.
6651 if (!checkclearop(cap
->oap
))
6653 if (setmark(cap
->nchar
) == FAIL
)
6654 clearopbeep(cap
->oap
);
6659 * "{" and "}" commands.
6660 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6666 cap
->oap
->motion_type
= MCHAR
;
6667 cap
->oap
->inclusive
= FALSE
;
6668 cap
->oap
->use_reg_one
= TRUE
;
6669 curwin
->w_set_curswant
= TRUE
;
6670 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, NUL
, FALSE
))
6671 clearopbeep(cap
->oap
);
6674 #ifdef FEAT_VIRTUALEDIT
6675 curwin
->w_cursor
.coladd
= 0;
6678 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6685 * "u" command: Undo or make lower case.
6691 if (cap
->oap
->op_type
== OP_LOWER
6697 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6713 if (!checkclearopq(cap
->oap
))
6715 u_undo((int)cap
->count1
);
6716 curwin
->w_set_curswant
= TRUE
;
6721 * Handle the "r" command.
6731 if (checkclearop(cap
->oap
))
6734 /* get another character */
6735 if (cap
->nchar
== Ctrl_V
)
6737 had_ctrl_v
= Ctrl_V
;
6738 cap
->nchar
= get_literal();
6739 /* Don't redo a multibyte character with CTRL-V. */
6740 if (cap
->nchar
> DEL
)
6746 /* Abort if the character is a special key. */
6747 if (IS_SPECIAL(cap
->nchar
))
6749 clearopbeep(cap
->oap
);
6754 /* Visual mode "r" */
6762 #ifdef FEAT_VIRTUALEDIT
6763 /* Break tabs, etc. */
6764 if (virtual_active())
6766 if (u_save_cursor() == FAIL
)
6768 if (gchar_cursor() == NUL
)
6770 /* Add extra space and put the cursor on the first one. */
6771 coladvance_force((colnr_T
)(getviscol() + cap
->count1
));
6772 curwin
->w_cursor
.col
-= cap
->count1
;
6774 else if (gchar_cursor() == TAB
)
6775 coladvance_force(getviscol());
6779 /* Abort if not enough characters to replace. */
6780 ptr
= ml_get_cursor();
6781 if (STRLEN(ptr
) < (unsigned)cap
->count1
6783 || (has_mbyte
&& mb_charlen(ptr
) < cap
->count1
)
6787 clearopbeep(cap
->oap
);
6792 * Replacing with a TAB is done by edit() when it is complicated because
6793 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6794 * Other characters are done below to avoid problems with things like
6795 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6797 if (had_ctrl_v
!= Ctrl_V
&& cap
->nchar
== '\t' && (curbuf
->b_p_et
|| p_sta
))
6799 stuffnumReadbuff(cap
->count1
);
6800 stuffcharReadbuff('R');
6801 stuffcharReadbuff('\t');
6802 stuffcharReadbuff(ESC
);
6806 /* save line for undo */
6807 if (u_save_cursor() == FAIL
)
6810 if (had_ctrl_v
!= Ctrl_V
&& (cap
->nchar
== '\r' || cap
->nchar
== '\n'))
6813 * Replace character(s) by a single newline.
6814 * Strange vi behaviour: Only one newline is inserted.
6815 * Delete the characters here.
6816 * Insert the newline with an insert command, takes care of
6817 * autoindent. The insert command depends on being on the last
6818 * character of a line or not.
6821 (void)del_chars(cap
->count1
, FALSE
); /* delete the characters */
6823 (void)del_bytes(cap
->count1
, FALSE
, FALSE
); /* delete the characters */
6825 stuffcharReadbuff('\r');
6826 stuffcharReadbuff(ESC
);
6828 /* Give 'r' to edit(), to get the redo command right. */
6829 invoke_edit(cap
, TRUE
, 'r', FALSE
);
6833 prep_redo(cap
->oap
->regname
, cap
->count1
,
6834 NUL
, 'r', NUL
, had_ctrl_v
, cap
->nchar
);
6836 curbuf
->b_op_start
= curwin
->w_cursor
;
6840 int old_State
= State
;
6842 if (cap
->ncharC1
!= 0)
6843 AppendCharToRedobuff(cap
->ncharC1
);
6844 if (cap
->ncharC2
!= 0)
6845 AppendCharToRedobuff(cap
->ncharC2
);
6847 /* This is slow, but it handles replacing a single-byte with a
6848 * multi-byte and the other way around. Also handles adding
6849 * composing characters for utf-8. */
6850 for (n
= cap
->count1
; n
> 0; --n
)
6853 ins_char(cap
->nchar
);
6855 if (cap
->ncharC1
!= 0)
6856 ins_char(cap
->ncharC1
);
6857 if (cap
->ncharC2
!= 0)
6858 ins_char(cap
->ncharC2
);
6865 * Replace the characters within one line.
6867 for (n
= cap
->count1
; n
> 0; --n
)
6870 * Get ptr again, because u_save and/or showmatch() will have
6871 * released the line. At the same time we let know that the
6872 * line will be changed.
6874 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
6875 ptr
[curwin
->w_cursor
.col
] = cap
->nchar
;
6876 if (p_sm
&& msg_silent
== 0)
6877 showmatch(cap
->nchar
);
6878 ++curwin
->w_cursor
.col
;
6880 #ifdef FEAT_NETBEANS_INTG
6883 colnr_T start
= (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
);
6885 netbeans_removed(curbuf
, curwin
->w_cursor
.lnum
, start
,
6887 netbeans_inserted(curbuf
, curwin
->w_cursor
.lnum
, start
,
6888 &ptr
[start
], (int)cap
->count1
);
6892 /* mark the buffer as changed and prepare for displaying */
6893 changed_bytes(curwin
->w_cursor
.lnum
,
6894 (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
));
6896 --curwin
->w_cursor
.col
; /* cursor on the last replaced char */
6898 /* if the character on the left of the current cursor is a multi-byte
6899 * character, move two characters left */
6903 curbuf
->b_op_end
= curwin
->w_cursor
;
6904 curwin
->w_set_curswant
= TRUE
;
6905 set_last_insert(cap
->nchar
);
6911 * 'o': Exchange start and end of Visual area.
6912 * 'O': same, but in block mode exchange left and right corners.
6915 v_swap_corners(cmdchar
)
6919 colnr_T left
, right
;
6921 if (cmdchar
== 'O' && VIsual_mode
== Ctrl_V
)
6923 old_cursor
= curwin
->w_cursor
;
6924 getvcols(curwin
, &old_cursor
, &VIsual
, &left
, &right
);
6925 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6927 VIsual
= curwin
->w_cursor
;
6929 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6930 curwin
->w_curswant
= right
;
6931 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6932 * right one column */
6933 if (old_cursor
.lnum
>= VIsual
.lnum
&& *p_sel
== 'e')
6934 ++curwin
->w_curswant
;
6935 coladvance(curwin
->w_curswant
);
6936 if (curwin
->w_cursor
.col
== old_cursor
.col
6937 #ifdef FEAT_VIRTUALEDIT
6938 && (!virtual_active()
6939 || curwin
->w_cursor
.coladd
== old_cursor
.coladd
)
6943 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6944 if (old_cursor
.lnum
<= VIsual
.lnum
&& *p_sel
== 'e')
6947 VIsual
= curwin
->w_cursor
;
6949 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6951 curwin
->w_curswant
= left
;
6956 old_cursor
= curwin
->w_cursor
;
6957 curwin
->w_cursor
= VIsual
;
6958 VIsual
= old_cursor
;
6959 curwin
->w_set_curswant
= TRUE
;
6962 #endif /* FEAT_VISUAL */
6965 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6972 if (VIsual_active
) /* "R" is replace lines */
6981 if (!checkclearopq(cap
->oap
))
6983 if (!curbuf
->b_p_ma
)
6984 EMSG(_(e_modifiable
));
6987 #ifdef FEAT_VIRTUALEDIT
6988 if (virtual_active())
6989 coladvance(getviscol());
6991 invoke_edit(cap
, FALSE
, cap
->arg
? 'V' : 'R', FALSE
);
6996 #ifdef FEAT_VREPLACE
7008 cap
->nchar
= cap
->extra_char
;
7009 nv_replace(cap
); /* Do same as "r" in Visual mode for now */
7013 if (!checkclearopq(cap
->oap
))
7015 if (!curbuf
->b_p_ma
)
7016 EMSG(_(e_modifiable
));
7019 if (cap
->extra_char
== Ctrl_V
) /* get another character */
7020 cap
->extra_char
= get_literal();
7021 stuffcharReadbuff(cap
->extra_char
);
7022 stuffcharReadbuff(ESC
);
7023 # ifdef FEAT_VIRTUALEDIT
7024 if (virtual_active())
7025 coladvance(getviscol());
7027 invoke_edit(cap
, TRUE
, 'v', FALSE
);
7034 * Swap case for "~" command, when it does not work like an operator.
7043 #ifdef FEAT_NETBEANS_INTG
7049 if (checkclearopq(cap
->oap
))
7052 if (lineempty(curwin
->w_cursor
.lnum
) && vim_strchr(p_ww
, '~') == NULL
)
7054 clearopbeep(cap
->oap
);
7060 if (u_save_cursor() == FAIL
)
7063 startpos
= curwin
->w_cursor
;
7064 #ifdef FEAT_NETBEANS_INTG
7067 for (n
= cap
->count1
; n
> 0; --n
)
7069 did_change
|= swapchar(cap
->oap
->op_type
, &curwin
->w_cursor
);
7071 if (gchar_cursor() == NUL
)
7073 if (vim_strchr(p_ww
, '~') != NULL
7074 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
7076 #ifdef FEAT_NETBEANS_INTG
7081 ptr
= ml_get(pos
.lnum
);
7082 count
= (int)STRLEN(ptr
) - pos
.col
;
7083 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
,
7085 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
7086 &ptr
[pos
.col
], count
);
7092 ++curwin
->w_cursor
.lnum
;
7093 curwin
->w_cursor
.col
= 0;
7096 if (u_savesub(curwin
->w_cursor
.lnum
) == FAIL
)
7105 #ifdef FEAT_NETBEANS_INTG
7106 if (did_change
&& usingNetbeans
)
7108 ptr
= ml_get(pos
.lnum
);
7109 count
= curwin
->w_cursor
.col
- pos
.col
;
7110 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
7111 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
, &ptr
[pos
.col
], count
);
7117 curwin
->w_set_curswant
= TRUE
;
7120 changed_lines(startpos
.lnum
, startpos
.col
, curwin
->w_cursor
.lnum
+ 1,
7122 curbuf
->b_op_start
= startpos
;
7123 curbuf
->b_op_end
= curwin
->w_cursor
;
7124 if (curbuf
->b_op_end
.col
> 0)
7125 --curbuf
->b_op_end
.col
;
7130 * Move cursor to mark.
7133 nv_cursormark(cap
, flag
, pos
)
7138 if (check_mark(pos
) == FAIL
)
7142 if (cap
->cmdchar
== '\''
7143 || cap
->cmdchar
== '`'
7144 || cap
->cmdchar
== '['
7145 || cap
->cmdchar
== ']')
7147 curwin
->w_cursor
= *pos
;
7149 beginline(BL_WHITE
| BL_FIX
);
7153 cap
->oap
->motion_type
= flag
? MLINE
: MCHAR
;
7154 if (cap
->cmdchar
== '`')
7155 cap
->oap
->use_reg_one
= TRUE
;
7156 cap
->oap
->inclusive
= FALSE
; /* ignored if not MCHAR */
7157 curwin
->w_set_curswant
= TRUE
;
7162 * Handle commands that are operators in Visual mode.
7168 static char_u trans
[] = "YyDdCcxdXdAAIIrr";
7170 /* Uppercase means linewise, except in block mode, then "D" deletes till
7171 * the end of the line, and "C" replaces til EOL */
7172 if (isupper(cap
->cmdchar
))
7174 if (VIsual_mode
!= Ctrl_V
)
7176 else if (cap
->cmdchar
== 'C' || cap
->cmdchar
== 'D')
7177 curwin
->w_curswant
= MAXCOL
;
7179 cap
->cmdchar
= *(vim_strchr(trans
, cap
->cmdchar
) + 1);
7185 * "s" and "S" commands.
7192 if (VIsual_active
) /* "vs" and "vS" are the same as "vc" */
7194 if (cap
->cmdchar
== 'S')
7205 * Abbreviated commands.
7211 if (cap
->cmdchar
== K_DEL
|| cap
->cmdchar
== K_KDEL
)
7212 cap
->cmdchar
= 'x'; /* DEL key behaves like 'x' */
7215 /* in Visual mode these commands are operators */
7224 * Translate a command into another command.
7230 static char_u
*(ar
[8]) = {(char_u
*)"dl", (char_u
*)"dh",
7231 (char_u
*)"d$", (char_u
*)"c$",
7232 (char_u
*)"cl", (char_u
*)"cc",
7233 (char_u
*)"yy", (char_u
*)":s\r"};
7234 static char_u
*str
= (char_u
*)"xXDCsSY&";
7236 if (!checkclearopq(cap
->oap
))
7238 /* In Vi "2D" doesn't delete the next line. Can't translate it
7239 * either, because "2." should also not use the count. */
7240 if (cap
->cmdchar
== 'D' && vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
7242 cap
->oap
->start
= curwin
->w_cursor
;
7243 cap
->oap
->op_type
= OP_DELETE
;
7245 set_op_var(OP_DELETE
);
7251 AppendCharToRedobuff('D');
7256 stuffnumReadbuff(cap
->count0
);
7257 stuffReadbuff(ar
[(int)(vim_strchr(str
, cap
->cmdchar
) - str
)]);
7264 * "'" and "`" commands. Also for "g'" and "g`".
7265 * cap->arg is TRUE for "'" and "g'".
7274 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7275 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7278 if (cap
->cmdchar
== 'g')
7279 c
= cap
->extra_char
;
7282 pos
= getmark(c
, (cap
->oap
->op_type
== OP_NOP
));
7283 if (pos
== (pos_T
*)-1) /* jumped to other file */
7287 check_cursor_lnum();
7288 beginline(BL_WHITE
| BL_FIX
);
7294 nv_cursormark(cap
, cap
->arg
, pos
);
7296 #ifdef FEAT_VIRTUALEDIT
7297 /* May need to clear the coladd that a mark includes. */
7298 if (!virtual_active())
7299 curwin
->w_cursor
.coladd
= 0;
7302 if (cap
->oap
->op_type
== OP_NOP
7303 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7304 && (fdo_flags
& FDO_MARK
)
7311 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7317 #ifdef FEAT_JUMPLIST
7319 # ifdef FEAT_FOLDING
7320 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7321 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7324 if (!checkclearopq(cap
->oap
))
7326 if (cap
->cmdchar
== 'g')
7327 pos
= movechangelist((int)cap
->count1
);
7329 pos
= movemark((int)cap
->count1
);
7330 if (pos
== (pos_T
*)-1) /* jump to other file */
7332 curwin
->w_set_curswant
= TRUE
;
7335 else if (pos
!= NULL
) /* can jump */
7336 nv_cursormark(cap
, FALSE
, pos
);
7337 else if (cap
->cmdchar
== 'g')
7339 if (curbuf
->b_changelistlen
== 0)
7340 EMSG(_("E664: changelist is empty"));
7341 else if (cap
->count1
< 0)
7342 EMSG(_("E662: At start of changelist"));
7344 EMSG(_("E663: At end of changelist"));
7347 clearopbeep(cap
->oap
);
7348 # ifdef FEAT_FOLDING
7349 if (cap
->oap
->op_type
== OP_NOP
7350 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7351 && (fdo_flags
& FDO_MARK
)
7357 clearopbeep(cap
->oap
);
7362 * Handle '"' command.
7368 if (checkclearop(cap
->oap
))
7371 if (cap
->nchar
== '=')
7372 cap
->nchar
= get_expr_register();
7374 if (cap
->nchar
!= NUL
&& valid_yank_reg(cap
->nchar
, FALSE
))
7376 cap
->oap
->regname
= cap
->nchar
;
7377 cap
->opcount
= cap
->count0
; /* remember count before '"' */
7379 set_reg_var(cap
->oap
->regname
);
7383 clearopbeep(cap
->oap
);
7388 * Handle "v", "V" and "CTRL-V" commands.
7389 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7391 * Handle CTRL-Q just like CTRL-V.
7397 if (cap
->cmdchar
== Ctrl_Q
)
7398 cap
->cmdchar
= Ctrl_V
;
7400 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7401 * characterwise, linewise, or blockwise. */
7402 if (cap
->oap
->op_type
!= OP_NOP
)
7404 cap
->oap
->motion_force
= cap
->cmdchar
;
7405 finish_op
= FALSE
; /* operator doesn't finish now but later */
7409 VIsual_select
= cap
->arg
;
7410 if (VIsual_active
) /* change Visual mode */
7412 if (VIsual_mode
== cap
->cmdchar
) /* stop visual mode */
7414 else /* toggle char/block mode */
7415 { /* or char/line mode */
7416 VIsual_mode
= cap
->cmdchar
;
7419 redraw_curbuf_later(INVERTED
); /* update the inversion */
7421 else /* start Visual mode */
7423 check_visual_highlight();
7424 if (cap
->count0
) /* use previously selected part */
7426 if (resel_VIsual_mode
== NUL
) /* there is none */
7431 VIsual
= curwin
->w_cursor
;
7433 VIsual_active
= TRUE
;
7434 VIsual_reselect
= TRUE
;
7436 /* start Select mode when 'selectmode' contains "cmd" */
7437 may_start_select('c');
7441 if (p_smd
&& msg_silent
== 0)
7442 redraw_cmdline
= TRUE
; /* show visual mode later */
7444 * For V and ^V, we multiply the number of lines even if there
7445 * was only one -- webb
7447 if (resel_VIsual_mode
!= 'v' || resel_VIsual_line_count
> 1)
7449 curwin
->w_cursor
.lnum
+=
7450 resel_VIsual_line_count
* cap
->count0
- 1;
7451 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
7452 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
7454 VIsual_mode
= resel_VIsual_mode
;
7455 if (VIsual_mode
== 'v')
7457 if (resel_VIsual_line_count
<= 1)
7458 curwin
->w_cursor
.col
+= resel_VIsual_col
* cap
->count0
- 1;
7460 curwin
->w_cursor
.col
= resel_VIsual_col
;
7463 if (resel_VIsual_col
== MAXCOL
)
7465 curwin
->w_curswant
= MAXCOL
;
7466 coladvance((colnr_T
)MAXCOL
);
7468 else if (VIsual_mode
== Ctrl_V
)
7471 curwin
->w_curswant
= curwin
->w_virtcol
7472 + resel_VIsual_col
* cap
->count0
- 1;
7473 coladvance(curwin
->w_curswant
);
7476 curwin
->w_set_curswant
= TRUE
;
7477 redraw_curbuf_later(INVERTED
); /* show the inversion */
7482 /* start Select mode when 'selectmode' contains "cmd" */
7483 may_start_select('c');
7484 n_start_visual_mode(cap
->cmdchar
);
7490 * Start selection for Shift-movement keys.
7495 /* if 'selectmode' contains "key", start Select mode */
7496 may_start_select('k');
7497 n_start_visual_mode('v');
7501 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7507 VIsual_select
= (stuff_empty() && typebuf_typed()
7508 && (vim_strchr(p_slm
, c
) != NULL
));
7512 * Start Visual mode "c".
7513 * Should set VIsual_select before calling this.
7516 n_start_visual_mode(c
)
7520 VIsual_active
= TRUE
;
7521 VIsual_reselect
= TRUE
;
7522 #ifdef FEAT_VIRTUALEDIT
7523 /* Corner case: the 0 position in a tab may change when going into
7524 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7526 if (c
== Ctrl_V
&& (ve_flags
& VE_BLOCK
) && gchar_cursor() == TAB
)
7527 coladvance(curwin
->w_virtcol
);
7529 VIsual
= curwin
->w_cursor
;
7538 if (p_smd
&& msg_silent
== 0)
7539 redraw_cmdline
= TRUE
; /* show visual mode later */
7540 #ifdef FEAT_CLIPBOARD
7541 /* Make sure the clipboard gets updated. Needed because start and
7542 * end may still be the same, and the selection needs to be owned */
7543 clip_star
.vmode
= NUL
;
7546 /* Only need to redraw this line, unless still need to redraw an old
7547 * Visual area (when 'lazyredraw' is set). */
7548 if (curwin
->w_redr_type
< INVERTED
)
7550 curwin
->w_old_cursor_lnum
= curwin
->w_cursor
.lnum
;
7551 curwin
->w_old_visual_lnum
= curwin
->w_cursor
.lnum
;
7555 #endif /* FEAT_VISUAL */
7558 * CTRL-W: Window commands
7565 if (!checkclearop(cap
->oap
))
7566 do_window(cap
->nchar
, cap
->count0
, NUL
); /* everything is in window.c */
7568 (void)checkclearop(cap
->oap
);
7582 end_visual_mode(); /* stop Visual mode */
7584 do_cmdline_cmd((char_u
*)"st");
7588 * Commands starting with "g".
7594 oparg_T
*oap
= cap
->oap
;
7605 * "g^A": dump log of used memory.
7608 vim_mem_profile_dump();
7612 #ifdef FEAT_VREPLACE
7614 * "gR": Enter virtual replace mode.
7627 do_cmdline_cmd((char_u
*)"%s//~/&");
7632 * "gv": Reselect the previous Visual area. If Visual already active,
7633 * exchange previous and current Visual area.
7636 if (checkclearop(oap
))
7639 if ( curbuf
->b_visual
.vi_start
.lnum
== 0
7640 || curbuf
->b_visual
.vi_start
.lnum
> curbuf
->b_ml
.ml_line_count
7641 || curbuf
->b_visual
.vi_end
.lnum
== 0)
7645 /* set w_cursor to the start of the Visual area, tpos to the end */
7649 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7650 curbuf
->b_visual
.vi_mode
= i
;
7652 curbuf
->b_visual_mode_eval
= i
;
7654 i
= curwin
->w_curswant
;
7655 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7656 curbuf
->b_visual
.vi_curswant
= i
;
7658 tpos
= curbuf
->b_visual
.vi_end
;
7659 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
7660 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7661 curbuf
->b_visual
.vi_start
= VIsual
;
7665 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7666 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7667 tpos
= curbuf
->b_visual
.vi_end
;
7668 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7671 VIsual_active
= TRUE
;
7672 VIsual_reselect
= TRUE
;
7674 /* Set Visual to the start and w_cursor to the end of the Visual
7675 * area. Make sure they are on an existing character. */
7677 VIsual
= curwin
->w_cursor
;
7678 curwin
->w_cursor
= tpos
;
7682 * When called from normal "g" command: start Select mode when
7683 * 'selectmode' contains "cmd". When called for K_SELECT, always
7684 * start Select mode.
7687 VIsual_select
= TRUE
;
7689 may_start_select('c');
7693 #ifdef FEAT_CLIPBOARD
7694 /* Make sure the clipboard gets updated. Needed because start and
7695 * end are still the same, and the selection needs to be owned */
7696 clip_star
.vmode
= NUL
;
7698 redraw_curbuf_later(INVERTED
);
7703 * "gV": Don't reselect the previous Visual area after a Select mode
7707 VIsual_reselect
= FALSE
;
7711 * "gh": start Select mode.
7712 * "gH": start Select line mode.
7713 * "g^H": start Select block mode.
7716 cap
->nchar
= Ctrl_H
;
7722 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7723 if (cap
->nchar
== Ctrl_H
)
7724 cap
->cmdchar
= Ctrl_V
;
7727 cap
->cmdchar
= cap
->nchar
+ ('v' - 'h');
7731 #endif /* FEAT_VISUAL */
7734 * "gj" and "gk" two new funny movement keys -- up and down
7735 * movement based on *screen* line rather than *file* line.
7739 /* with 'nowrap' it works just like the normal "j" command; also when
7740 * in a closed fold */
7741 if (!curwin
->w_p_wrap
7743 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7747 oap
->motion_type
= MLINE
;
7748 i
= cursor_down(cap
->count1
, oap
->op_type
== OP_NOP
);
7751 i
= nv_screengo(oap
, FORWARD
, cap
->count1
);
7758 /* with 'nowrap' it works just like the normal "k" command; also when
7759 * in a closed fold */
7760 if (!curwin
->w_p_wrap
7762 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7766 oap
->motion_type
= MLINE
;
7767 i
= cursor_up(cap
->count1
, oap
->op_type
== OP_NOP
);
7770 i
= nv_screengo(oap
, BACKWARD
, cap
->count1
);
7776 * "gJ": join two lines without inserting a space.
7783 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7784 * "gm": middle of "g0" and "g$".
7794 oap
->motion_type
= MCHAR
;
7795 oap
->inclusive
= FALSE
;
7796 if (curwin
->w_p_wrap
7797 #ifdef FEAT_VERTSPLIT
7798 && curwin
->w_width
!= 0
7802 int width1
= W_WIDTH(curwin
) - curwin_col_off();
7803 int width2
= width1
+ curwin_col_off2();
7807 if (curwin
->w_virtcol
>= (colnr_T
)width1
&& width2
> 0)
7808 i
= (curwin
->w_virtcol
- width1
) / width2
* width2
+ width1
;
7811 i
= curwin
->w_leftcol
;
7812 /* Go to the middle of the screen line. When 'number' is on and lines
7813 * are wrapping the middle can be more to the left.*/
7814 if (cap
->nchar
== 'm')
7815 i
+= (W_WIDTH(curwin
) - curwin_col_off()
7816 + ((curwin
->w_p_wrap
&& i
> 0)
7817 ? curwin_col_off2() : 0)) / 2;
7818 coladvance((colnr_T
)i
);
7823 while (vim_iswhite(i
) && oneright() == OK
);
7825 curwin
->w_set_curswant
= TRUE
;
7829 /* "g_": to the last non-blank character in the line or <count> lines
7831 cap
->oap
->motion_type
= MCHAR
;
7832 cap
->oap
->inclusive
= TRUE
;
7833 curwin
->w_curswant
= MAXCOL
;
7834 if (cursor_down((long)(cap
->count1
- 1),
7835 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
7836 clearopbeep(cap
->oap
);
7839 char_u
*ptr
= ml_get_curline();
7841 /* In Visual mode we may end up after the line. */
7842 if (curwin
->w_cursor
.col
> 0 && ptr
[curwin
->w_cursor
.col
] == NUL
)
7843 --curwin
->w_cursor
.col
;
7845 /* Decrease the cursor column until it's on a non-blank. */
7846 while (curwin
->w_cursor
.col
> 0
7847 && vim_iswhite(ptr
[curwin
->w_cursor
.col
]))
7848 --curwin
->w_cursor
.col
;
7849 curwin
->w_set_curswant
= TRUE
;
7857 int col_off
= curwin_col_off();
7859 oap
->motion_type
= MCHAR
;
7860 oap
->inclusive
= TRUE
;
7861 if (curwin
->w_p_wrap
7862 #ifdef FEAT_VERTSPLIT
7863 && curwin
->w_width
!= 0
7867 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
7868 if (cap
->count1
== 1)
7870 int width1
= W_WIDTH(curwin
) - col_off
;
7871 int width2
= width1
+ curwin_col_off2();
7875 if (curwin
->w_virtcol
>= (colnr_T
)width1
)
7876 i
+= ((curwin
->w_virtcol
- width1
) / width2
+ 1)
7878 coladvance((colnr_T
)i
);
7879 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7880 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
7883 * Check for landing on a character that got split at
7884 * the end of the line. We do not want to advance to
7885 * the next screen line.
7888 if (curwin
->w_virtcol
> (colnr_T
)i
)
7889 --curwin
->w_cursor
.col
;
7893 else if (nv_screengo(oap
, FORWARD
, cap
->count1
- 1) == FAIL
)
7898 i
= curwin
->w_leftcol
+ W_WIDTH(curwin
) - col_off
- 1;
7899 coladvance((colnr_T
)i
);
7901 /* Make sure we stick in this column. */
7903 curwin
->w_curswant
= curwin
->w_virtcol
;
7904 curwin
->w_set_curswant
= FALSE
;
7910 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7915 case POUND
: /* pound sign (sometimes equal to '#') */
7917 case Ctrl_RSB
: /* :tag or :tselect for current identifier */
7918 case ']': /* :tselect for current identifier */
7923 * ge and gE: go back to end of word
7927 oap
->motion_type
= MCHAR
;
7928 curwin
->w_set_curswant
= TRUE
;
7929 oap
->inclusive
= TRUE
;
7930 if (bckend_word(cap
->count1
, cap
->nchar
== 'E', FALSE
) == FAIL
)
7935 * "g CTRL-G": display info about cursor position
7942 * "gi": start Insert at the last position.
7945 if (curbuf
->b_last_insert
.lnum
!= 0)
7947 curwin
->w_cursor
= curbuf
->b_last_insert
;
7948 check_cursor_lnum();
7949 i
= (int)STRLEN(ml_get_curline());
7950 if (curwin
->w_cursor
.col
> (colnr_T
)i
)
7952 #ifdef FEAT_VIRTUALEDIT
7953 if (virtual_active())
7954 curwin
->w_cursor
.coladd
+= curwin
->w_cursor
.col
- i
;
7956 curwin
->w_cursor
.col
= i
;
7964 * "gI": Start insert in column 1.
7968 if (!checkclearopq(oap
))
7969 invoke_edit(cap
, FALSE
, 'g', FALSE
);
7972 #ifdef FEAT_SEARCHPATH
7974 * "gf": goto file, edit file under cursor
7975 * "]f" and "[f": can also be used.
7983 /* "g'm" and "g`m": jump to mark without setting pcmark */
7995 do_sleep(cap
->count1
* 1000L);
7999 * "ga": Display the ascii value of the character under the
8000 * cursor. It is displayed in decimal, hex, and octal. -- webb
8008 * "g8": Display the bytes used for the UTF-8 character under the
8009 * cursor. It is displayed in hex.
8010 * "8g8" finds illegal byte sequence.
8013 if (cap
->count0
== 8)
8025 * "gg": Goto the first line in file. With a count it goes to
8026 * that line number like for "G". -- webb
8034 * Two-character operators:
8036 * "gw" Format text and keep cursor position
8037 * "g~" Toggle the case of the text.
8038 * "gu" Change text to lower case.
8039 * "gU" Change text to upper case.
8040 * "g?" rot13 encoding
8041 * "g@" call 'operatorfunc'
8045 oap
->cursor_start
= curwin
->w_cursor
;
8056 * "gd": Find first occurrence of pattern under the cursor in the
8058 * "gD": idem, but in the current file.
8062 nv_gd(oap
, cap
->nchar
, (int)cap
->count0
);
8067 * g<*Mouse> : <C-*mouse>
8071 case K_MIDDLERELEASE
:
8077 case K_RIGHTRELEASE
:
8084 mod_mask
= MOD_MASK_CTRL
;
8085 (void)do_mouse(oap
, cap
->nchar
, BACKWARD
, cap
->count1
, 0);
8093 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8101 /* "go": goto byte count from start of buffer */
8103 goto_byte(cap
->count0
);
8107 /* "gQ": improved Ex mode */
8111 clearopbeep(cap
->oap
);
8116 if (!checkclearopq(oap
))
8120 #ifdef FEAT_JUMPLIST
8126 cap
->count1
= -cap
->count1
;
8133 goto_tabpage((int)cap
->count0
);
8136 goto_tabpage(-(int)cap
->count1
);
8141 case '-': /* "g+" and "g-": undo or redo along the timeline */
8142 if (!checkclearopq(oap
))
8143 undo_time(cap
->nchar
== '-' ? -cap
->count1
: cap
->count1
,
8154 * Handle "o" and "O" commands.
8160 if (!checkclearopq(cap
->oap
))
8163 if (cap
->cmdchar
== 'O')
8164 /* Open above the first line of a folded sequence of lines */
8165 (void)hasFolding(curwin
->w_cursor
.lnum
,
8166 &curwin
->w_cursor
.lnum
, NULL
);
8168 /* Open below the last line of a folded sequence of lines */
8169 (void)hasFolding(curwin
->w_cursor
.lnum
,
8170 NULL
, &curwin
->w_cursor
.lnum
);
8172 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
-
8173 (cap
->cmdchar
== 'O' ? 1 : 0)),
8174 (linenr_T
)(curwin
->w_cursor
.lnum
+
8175 (cap
->cmdchar
== 'o' ? 1 : 0))
8177 && open_line(cap
->cmdchar
== 'O' ? BACKWARD
: FORWARD
,
8178 #ifdef FEAT_COMMENTS
8179 has_format_option(FO_OPEN_COMS
) ? OPENLINE_DO_COM
:
8183 /* When '#' is in 'cpoptions' ignore the count. */
8184 if (vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
8186 invoke_edit(cap
, FALSE
, cap
->cmdchar
, TRUE
);
8192 * "." command: redo last change.
8198 if (!checkclearopq(cap
->oap
))
8201 * If "restart_edit" is TRUE, the last but one command is repeated
8202 * instead of the last command (inserting text). This is used for
8203 * CTRL-O <.> in insert mode.
8205 if (start_redo(cap
->count0
, restart_edit
!= 0 && !arrow_used
) == FAIL
)
8206 clearopbeep(cap
->oap
);
8217 if (!checkclearopq(cap
->oap
))
8219 u_redo((int)cap
->count1
);
8220 curwin
->w_set_curswant
= TRUE
;
8225 * Handle "U" command.
8231 /* In Visual mode and typing "gUU" triggers an operator */
8232 if (cap
->oap
->op_type
== OP_UPPER
8238 /* translate "gUU" to "gUgU" */
8243 else if (!checkclearopq(cap
->oap
))
8246 curwin
->w_set_curswant
= TRUE
;
8251 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8262 && cap
->oap
->op_type
!= OP_TILDE
)
8269 * Handle an operator command.
8270 * The actual work is done by do_pending_operator().
8278 op_type
= get_op_type(cap
->cmdchar
, cap
->nchar
);
8280 if (op_type
== cap
->oap
->op_type
) /* double operator works on lines */
8282 else if (!checkclearop(cap
->oap
))
8284 cap
->oap
->start
= curwin
->w_cursor
;
8285 cap
->oap
->op_type
= op_type
;
8287 set_op_var(op_type
);
8294 * Set v:operator to the characters for "optype".
8302 if (optype
== OP_NOP
)
8303 set_vim_var_string(VV_OP
, NULL
, 0);
8306 opchars
[0] = get_op_char(optype
);
8307 opchars
[1] = get_extra_op_char(optype
);
8309 set_vim_var_string(VV_OP
, opchars
, -1);
8315 * Handle linewise operator "dd", "yy", etc.
8317 * "_" is is a strange motion command that helps make operators more logical.
8318 * It is actually implemented, but not documented in the real Vi. This motion
8319 * command actually refers to "the current line". Commands like "dd" and "yy"
8320 * are really an alternate form of "d_" and "y_". It does accept a count, so
8321 * "d3_" works to delete 3 lines.
8327 cap
->oap
->motion_type
= MLINE
;
8328 if (cursor_down(cap
->count1
- 1L, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
8329 clearopbeep(cap
->oap
);
8330 else if ( cap
->oap
->op_type
== OP_DELETE
8331 || cap
->oap
->op_type
== OP_LSHIFT
8332 || cap
->oap
->op_type
== OP_RSHIFT
)
8333 beginline(BL_SOL
| BL_FIX
);
8334 else if (cap
->oap
->op_type
!= OP_YANK
) /* 'Y' does not move cursor */
8335 beginline(BL_WHITE
| BL_FIX
);
8345 /* CTRL-HOME is like "gg" */
8346 if (mod_mask
& MOD_MASK_CTRL
)
8353 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8354 one-character line). */
8364 cap
->oap
->motion_type
= MCHAR
;
8365 cap
->oap
->inclusive
= FALSE
;
8367 if (cap
->count0
> 0)
8369 coladvance((colnr_T
)(cap
->count0
- 1));
8370 curwin
->w_curswant
= (colnr_T
)(cap
->count0
- 1);
8373 curwin
->w_curswant
= 0;
8374 /* keep curswant at the column where we wanted to go, not where
8375 we ended; differs if line is too short */
8376 curwin
->w_set_curswant
= FALSE
;
8380 * Handle back-word command "b" and "B".
8381 * cap->arg is 1 for "B"
8387 cap
->oap
->motion_type
= MCHAR
;
8388 cap
->oap
->inclusive
= FALSE
;
8389 curwin
->w_set_curswant
= TRUE
;
8390 if (bck_word(cap
->count1
, cap
->arg
, FALSE
) == FAIL
)
8391 clearopbeep(cap
->oap
);
8393 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8399 * Handle word motion commands "e", "E", "w" and "W".
8400 * cap->arg is TRUE for "E" and "W".
8409 pos_T startpos
= curwin
->w_cursor
;
8412 * Set inclusive for the "E" and "e" command.
8414 if (cap
->cmdchar
== 'e' || cap
->cmdchar
== 'E')
8418 cap
->oap
->inclusive
= word_end
;
8421 * "cw" and "cW" are a special case.
8423 if (!word_end
&& cap
->oap
->op_type
== OP_CHANGE
)
8426 if (n
!= NUL
) /* not an empty line */
8431 * Reproduce a funny Vi behaviour: "cw" on a blank only
8432 * changes one character, not all blanks until the start of
8433 * the next word. Only do this when the 'w' flag is included
8436 if (cap
->count1
== 1 && vim_strchr(p_cpo
, CPO_CW
) != NULL
)
8438 cap
->oap
->inclusive
= TRUE
;
8439 cap
->oap
->motion_type
= MCHAR
;
8446 * This is a little strange. To match what the real Vi does,
8447 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8448 * that we are not on a space or a TAB. This seems impolite
8449 * at first, but it's really more what we mean when we say
8451 * Another strangeness: When standing on the end of a word
8452 * "ce" will change until the end of the next wordt, but "cw"
8453 * will change only one character! This is done by setting
8456 cap
->oap
->inclusive
= TRUE
;
8463 cap
->oap
->motion_type
= MCHAR
;
8464 curwin
->w_set_curswant
= TRUE
;
8466 n
= end_word(cap
->count1
, cap
->arg
, flag
, FALSE
);
8468 n
= fwd_word(cap
->count1
, cap
->arg
, cap
->oap
->op_type
!= OP_NOP
);
8470 /* Don't leave the cursor on the NUL past the end of line. Unless we
8471 * didn't move it forward. */
8472 if (lt(startpos
, curwin
->w_cursor
))
8473 adjust_cursor(cap
->oap
);
8475 if (n
== FAIL
&& cap
->oap
->op_type
== OP_NOP
)
8476 clearopbeep(cap
->oap
);
8480 adjust_for_sel(cap
);
8483 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8490 * Used after a movement command: If the cursor ends up on the NUL after the
8491 * end of the line, may move it back to the last character and make the motion
8498 /* The cursor cannot remain on the NUL when:
8499 * - the column is > 0
8500 * - not in Visual mode or 'selection' is "o"
8501 * - 'virtualedit' is not "all" and not "onemore".
8503 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
8505 && (!VIsual_active
|| *p_sel
== 'o')
8507 #ifdef FEAT_VIRTUALEDIT
8508 && !virtual_active() && (ve_flags
& VE_ONEMORE
) == 0
8512 --curwin
->w_cursor
.col
;
8514 /* prevent cursor from moving on the trail byte */
8518 oap
->inclusive
= TRUE
;
8523 * "0" and "^" commands.
8524 * cap->arg is the argument for beginline().
8530 cap
->oap
->motion_type
= MCHAR
;
8531 cap
->oap
->inclusive
= FALSE
;
8532 beginline(cap
->arg
);
8534 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8537 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8538 one-character line). */
8543 * In exclusive Visual mode, may include the last character.
8549 if (VIsual_active
&& cap
->oap
->inclusive
&& *p_sel
== 'e'
8550 && gchar_cursor() != NUL
&& lt(VIsual
, curwin
->w_cursor
))
8557 ++curwin
->w_cursor
.col
;
8558 cap
->oap
->inclusive
= FALSE
;
8563 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8564 * Should check VIsual_mode before calling this.
8565 * Returns TRUE when backed up to the previous line.
8572 if (*p_sel
== 'e' && !equalpos(VIsual
, curwin
->w_cursor
))
8574 if (lt(VIsual
, curwin
->w_cursor
))
8575 pp
= &curwin
->w_cursor
;
8578 #ifdef FEAT_VIRTUALEDIT
8590 else if (pp
->lnum
> 1)
8593 pp
->col
= (colnr_T
)STRLEN(ml_get(pp
->lnum
));
8601 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8608 VIsual_select
= TRUE
;
8609 else if (VIsual_reselect
)
8611 cap
->nchar
= 'v'; /* fake "gv" command */
8620 * "G", "gg", CTRL-END, CTRL-HOME.
8621 * cap->arg is TRUE for "G".
8630 lnum
= curbuf
->b_ml
.ml_line_count
;
8633 cap
->oap
->motion_type
= MLINE
;
8636 /* When a count is given, use it instead of the default lnum */
8637 if (cap
->count0
!= 0)
8641 else if (lnum
> curbuf
->b_ml
.ml_line_count
)
8642 lnum
= curbuf
->b_ml
.ml_line_count
;
8643 curwin
->w_cursor
.lnum
= lnum
;
8644 beginline(BL_SOL
| BL_FIX
);
8646 if ((fdo_flags
& FDO_JUMP
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8652 * CTRL-\ in Normal mode.
8658 if (cap
->nchar
== Ctrl_N
|| cap
->nchar
== Ctrl_G
)
8661 if (restart_edit
!= 0 && mode_displayed
)
8662 clear_cmdline
= TRUE
; /* unshow mode later */
8665 if (cmdwin_type
!= 0)
8666 cmdwin_result
= Ctrl_C
;
8671 end_visual_mode(); /* stop Visual */
8672 redraw_curbuf_later(INVERTED
);
8675 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8676 if (cap
->nchar
== Ctrl_G
&& p_im
)
8680 clearopbeep(cap
->oap
);
8684 * ESC in Normal mode: beep, but don't flush buffers.
8685 * Don't even beep if we are canceling a command.
8693 no_reason
= (cap
->oap
->op_type
== OP_NOP
8694 && cap
->opcount
== 0
8696 && cap
->oap
->regname
== 0
8699 if (cap
->arg
) /* TRUE for CTRL-C */
8701 if (restart_edit
== 0
8709 MSG(_("Type :quit<Enter> to exit Vim"));
8711 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8712 * set again below when halfway a mapping. */
8716 if (cmdwin_type
!= 0)
8718 cmdwin_result
= K_IGNORE
;
8719 got_int
= FALSE
; /* don't stop executing autocommands et al. */
8728 end_visual_mode(); /* stop Visual */
8729 check_cursor_col(); /* make sure cursor is not beyond EOL */
8730 curwin
->w_set_curswant
= TRUE
;
8731 redraw_curbuf_later(INVERTED
);
8739 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8740 * set return to Insert mode afterwards. */
8741 if (restart_edit
== 0 && goto_im()
8742 #ifdef FEAT_EX_EXTRA
8743 && ex_normal_busy
== 0
8750 * Handle "A", "a", "I", "i" and <Insert> commands.
8756 /* <Insert> is equal to "i" */
8757 if (cap
->cmdchar
== K_INS
|| cap
->cmdchar
== K_KINS
)
8761 /* in Visual mode "A" and "I" are an operator */
8762 if (VIsual_active
&& (cap
->cmdchar
== 'A' || cap
->cmdchar
== 'I'))
8765 /* in Visual mode and after an operator "a" and "i" are for text objects */
8768 if ((cap
->cmdchar
== 'a' || cap
->cmdchar
== 'i')
8769 && (cap
->oap
->op_type
!= OP_NOP
8778 clearopbeep(cap
->oap
);
8781 else if (!curbuf
->b_p_ma
&& !p_im
)
8783 /* Only give this error when 'insertmode' is off. */
8784 EMSG(_(e_modifiable
));
8787 else if (!checkclearopq(cap
->oap
))
8789 switch (cap
->cmdchar
)
8791 case 'A': /* "A"ppend after the line */
8792 curwin
->w_set_curswant
= TRUE
;
8793 #ifdef FEAT_VIRTUALEDIT
8794 if (ve_flags
== VE_ALL
)
8796 int save_State
= State
;
8798 /* Pretent Insert mode here to allow the cursor on the
8799 * character past the end of the line */
8801 coladvance((colnr_T
)MAXCOL
);
8806 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8809 case 'I': /* "I"nsert before the first non-blank */
8810 if (vim_strchr(p_cpo
, CPO_INSEND
) == NULL
)
8811 beginline(BL_WHITE
);
8813 beginline(BL_WHITE
|BL_FIX
);
8816 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8817 #ifdef FEAT_VIRTUALEDIT
8818 /* increment coladd when in virtual space, increment the
8819 * column otherwise, also to append after an unprintable char */
8820 if (virtual_active()
8821 && (curwin
->w_cursor
.coladd
> 0
8822 || *ml_get_cursor() == NUL
8823 || *ml_get_cursor() == TAB
))
8824 curwin
->w_cursor
.coladd
++;
8827 if (*ml_get_cursor() != NUL
)
8832 #ifdef FEAT_VIRTUALEDIT
8833 if (curwin
->w_cursor
.coladd
&& cap
->cmdchar
!= 'A')
8835 int save_State
= State
;
8837 /* Pretent Insert mode here to allow the cursor on the
8838 * character past the end of the line */
8840 coladvance(getviscol());
8845 invoke_edit(cap
, FALSE
, cap
->cmdchar
, FALSE
);
8850 * Invoke edit() and take care of "restart_edit" and the return value.
8853 invoke_edit(cap
, repl
, cmd
, startln
)
8855 int repl
; /* "r" or "gr" command */
8859 int restart_edit_save
= 0;
8861 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8862 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8864 if (repl
|| !stuff_empty())
8865 restart_edit_save
= restart_edit
;
8867 restart_edit_save
= 0;
8869 /* Always reset "restart_edit", this is not a restarted edit. */
8872 if (edit(cmd
, startln
, cap
->count1
))
8873 cap
->retval
|= CA_COMMAND_BUSY
;
8875 if (restart_edit
== 0)
8876 restart_edit
= restart_edit_save
;
8881 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8891 if (cap
->cmdchar
== 'i')
8892 include
= FALSE
; /* "ix" = inner object: exclude white space */
8894 include
= TRUE
; /* "ax" = an object: include white space */
8896 /* Make sure (), [], {} and <> are in 'matchpairs' */
8897 mps_save
= curbuf
->b_p_mps
;
8898 curbuf
->b_p_mps
= (char_u
*)"(:),{:},[:],<:>";
8902 case 'w': /* "aw" = a word */
8903 flag
= current_word(cap
->oap
, cap
->count1
, include
, FALSE
);
8905 case 'W': /* "aW" = a WORD */
8906 flag
= current_word(cap
->oap
, cap
->count1
, include
, TRUE
);
8908 case 'b': /* "ab" = a braces block */
8911 flag
= current_block(cap
->oap
, cap
->count1
, include
, '(', ')');
8913 case 'B': /* "aB" = a Brackets block */
8916 flag
= current_block(cap
->oap
, cap
->count1
, include
, '{', '}');
8918 case '[': /* "a[" = a [] block */
8920 flag
= current_block(cap
->oap
, cap
->count1
, include
, '[', ']');
8922 case '<': /* "a<" = a <> block */
8924 flag
= current_block(cap
->oap
, cap
->count1
, include
, '<', '>');
8926 case 't': /* "at" = a tag block (xml and html) */
8927 flag
= current_tagblock(cap
->oap
, cap
->count1
, include
);
8929 case 'p': /* "ap" = a paragraph */
8930 flag
= current_par(cap
->oap
, cap
->count1
, include
, 'p');
8932 case 's': /* "as" = a sentence */
8933 flag
= current_sent(cap
->oap
, cap
->count1
, include
);
8935 case '"': /* "a"" = a double quoted string */
8936 case '\'': /* "a'" = a single quoted string */
8937 case '`': /* "a`" = a backtick quoted string */
8938 flag
= current_quote(cap
->oap
, cap
->count1
, include
,
8942 case 'S': /* "aS" = a section */
8943 case 'f': /* "af" = a filename */
8944 case 'u': /* "au" = a URL */
8951 curbuf
->b_p_mps
= mps_save
;
8953 clearopbeep(cap
->oap
);
8954 adjust_cursor_col();
8955 curwin
->w_set_curswant
= TRUE
;
8960 * "q" command: Start/stop recording.
8961 * "q:", "q/", "q?": edit command-line in command-line window.
8967 if (cap
->oap
->op_type
== OP_FORMAT
)
8969 /* "gqq" is the same as "gqgq": format line */
8974 else if (!checkclearop(cap
->oap
))
8977 if (cap
->nchar
== ':' || cap
->nchar
== '/' || cap
->nchar
== '?')
8979 stuffcharReadbuff(cap
->nchar
);
8980 stuffcharReadbuff(K_CMDWIN
);
8984 /* (stop) recording into a named register, unless executing a
8986 if (!Exec_reg
&& do_record(cap
->nchar
) == FAIL
)
8987 clearopbeep(cap
->oap
);
8992 * Handle the "@r" command.
8998 if (checkclearop(cap
->oap
))
9001 if (cap
->nchar
== '=')
9003 if (get_expr_register() == NUL
)
9007 while (cap
->count1
-- && !got_int
)
9009 if (do_execreg(cap
->nchar
, FALSE
, FALSE
, FALSE
) == FAIL
)
9011 clearopbeep(cap
->oap
);
9019 * Handle the CTRL-U and CTRL-D commands.
9025 if ((cap
->cmdchar
== Ctrl_U
&& curwin
->w_cursor
.lnum
== 1)
9026 || (cap
->cmdchar
== Ctrl_D
9027 && curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
))
9028 clearopbeep(cap
->oap
);
9029 else if (!checkclearop(cap
->oap
))
9030 halfpage(cap
->cmdchar
== Ctrl_D
, cap
->count0
);
9034 * Handle "J" or "gJ" command.
9041 if (VIsual_active
) /* join the visual lines */
9045 if (!checkclearop(cap
->oap
))
9047 if (cap
->count0
<= 1)
9048 cap
->count0
= 2; /* default for join is two lines! */
9049 if (curwin
->w_cursor
.lnum
+ cap
->count0
- 1 >
9050 curbuf
->b_ml
.ml_line_count
)
9051 clearopbeep(cap
->oap
); /* beyond last line */
9054 prep_redo(cap
->oap
->regname
, cap
->count0
,
9055 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
9056 do_do_join(cap
->count0
, cap
->nchar
== NUL
);
9062 * "P", "gP", "p" and "gp" commands.
9070 void *reg1
= NULL
, *reg2
= NULL
;
9072 int was_visual
= FALSE
;
9077 if (cap
->oap
->op_type
!= OP_NOP
)
9080 /* "dp" is ":diffput" */
9081 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'p')
9084 nv_diffgetput(TRUE
);
9088 clearopbeep(cap
->oap
);
9092 dir
= (cap
->cmdchar
== 'P'
9093 || (cap
->cmdchar
== 'g' && cap
->nchar
== 'P'))
9094 ? BACKWARD
: FORWARD
;
9096 if (cap
->cmdchar
== 'g')
9097 flags
|= PUT_CURSEND
;
9102 /* Putting in Visual mode: The put text replaces the selected
9103 * text. First delete the selected text, then put the new text.
9104 * Need to save and restore the registers that the delete
9105 * overwrites if the old contents is being put.
9108 regname
= cap
->oap
->regname
;
9109 # ifdef FEAT_CLIPBOARD
9110 adjust_clip_reg(®name
);
9112 if (regname
== 0 || VIM_ISDIGIT(regname
)
9113 # ifdef FEAT_CLIPBOARD
9114 || (clip_unnamed
&& (regname
== '*' || regname
== '+'))
9119 /* the delete is going to overwrite the register we want to
9120 * put, save it first. */
9121 reg1
= get_register(regname
, TRUE
);
9124 /* Now delete the selected text. */
9127 cap
->oap
->regname
= NUL
;
9129 do_pending_operator(cap
, 0, FALSE
);
9130 empty
= (curbuf
->b_ml
.ml_flags
& ML_EMPTY
);
9132 /* delete PUT_LINE_BACKWARD; */
9133 cap
->oap
->regname
= regname
;
9137 /* Delete probably changed the register we want to put, save
9138 * it first. Then put back what was there before the delete. */
9139 reg2
= get_register(regname
, FALSE
);
9140 put_register(regname
, reg1
);
9143 /* When deleted a linewise Visual area, put the register as
9144 * lines to avoid it joined with the next line. When deletion was
9145 * characterwise, split a line when putting lines. */
9146 if (VIsual_mode
== 'V')
9148 else if (VIsual_mode
== 'v')
9149 flags
|= PUT_LINE_SPLIT
;
9150 if (VIsual_mode
== Ctrl_V
&& dir
== FORWARD
)
9151 flags
|= PUT_LINE_FORWARD
;
9153 if ((VIsual_mode
!= 'V'
9154 && curwin
->w_cursor
.col
< curbuf
->b_op_start
.col
)
9155 || (VIsual_mode
== 'V'
9156 && curwin
->w_cursor
.lnum
< curbuf
->b_op_start
.lnum
))
9157 /* cursor is at the end of the line or end of file, put
9162 do_put(cap
->oap
->regname
, dir
, cap
->count1
, flags
);
9165 /* If a register was saved, put it back now. */
9167 put_register(regname
, reg2
);
9169 /* What to reselect with "gv"? Selecting the just put text seems to
9170 * be the most useful, since the original text was removed. */
9173 curbuf
->b_visual
.vi_start
= curbuf
->b_op_start
;
9174 curbuf
->b_visual
.vi_end
= curbuf
->b_op_end
;
9177 /* When all lines were selected and deleted do_put() leaves an empty
9178 * line that needs to be deleted now. */
9179 if (empty
&& *ml_get(curbuf
->b_ml
.ml_line_count
) == NUL
)
9181 ml_delete(curbuf
->b_ml
.ml_line_count
, TRUE
);
9183 /* If the cursor was in that line, move it to the end of the last
9185 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
9187 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9188 coladvance((colnr_T
)MAXCOL
);
9192 auto_format(FALSE
, TRUE
);
9197 * "o" and "O" commands.
9204 /* "do" is ":diffget" */
9205 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'o')
9208 nv_diffgetput(FALSE
);
9213 if (VIsual_active
) /* switch start and end of visual */
9214 v_swap_corners(cap
->cmdchar
);
9226 ProcessSniffRequests();
9230 #ifdef FEAT_NETBEANS_INTG
9235 netbeans_keycommand(cap
->nchar
);
9245 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9251 * Trigger CursorHold event.
9252 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9253 * input buffer. "did_cursorhold" is set to avoid retriggering.
9260 apply_autocmds(EVENT_CURSORHOLD
, NULL
, NULL
, FALSE
, curbuf
);
9261 did_cursorhold
= TRUE
;
9262 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */